OpenMP: Update documentation of metadirective implementation status.
[gcc.git] / gcc / function.cc
blob2ad430a8013d48bb923243df261dfbae1923c678
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987-2025 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
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.
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.
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. */
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"
89 /* So we can assign to cfun in this file. */
90 #undef cfun
92 #ifndef STACK_ALIGNMENT_NEEDED
93 #define STACK_ALIGNMENT_NEEDED 1
94 #endif
96 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
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))
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))
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. */
111 int virtuals_instantiated;
113 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
114 static GTY(()) int funcdef_no;
116 /* These variables hold pointers to functions to create and destroy
117 target specific, per-function data structures. */
118 struct machine_function * (*init_machine_status) (void);
120 /* The currently compiled function. */
121 struct function *cfun = 0;
123 /* These hashes record the prologue and epilogue insns. */
125 struct insn_cache_hasher : ggc_cache_ptr_hash<rtx_def>
127 static hashval_t hash (rtx x) { return htab_hash_pointer (x); }
128 static bool equal (rtx a, rtx b) { return a == b; }
131 static GTY((cache))
132 hash_table<insn_cache_hasher> *prologue_insn_hash;
133 static GTY((cache))
134 hash_table<insn_cache_hasher> *epilogue_insn_hash;
137 hash_table<used_type_hasher> *types_used_by_vars_hash = NULL;
138 vec<tree, va_gc> *types_used_by_cur_var_decl;
140 /* Forward declarations. */
142 static class temp_slot *find_temp_slot_from_address (rtx);
143 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
144 static void pad_below (struct args_size *, machine_mode, tree);
145 static void reorder_blocks_1 (rtx_insn *, tree, vec<tree> *);
146 static int all_blocks (tree, tree *);
147 static tree *get_block_vector (tree, int *);
148 extern 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'. */
151 static void record_insns (rtx_insn *, rtx, hash_table<insn_cache_hasher> **)
152 ATTRIBUTE_UNUSED;
153 static bool contains (const rtx_insn *, hash_table<insn_cache_hasher> *);
154 static void prepare_function_start (void);
155 static void do_clobber_return_reg (rtx, void *);
156 static void do_use_return_reg (rtx, void *);
159 /* Stack of nested functions. */
160 /* Keep track of the cfun stack. */
162 static vec<function *> function_context_stack;
164 /* Save the current context for compilation of a nested function.
165 This is called from language-specific code. */
167 void
168 push_function_context (void)
170 if (cfun == 0)
171 allocate_struct_function (NULL, false);
173 function_context_stack.safe_push (cfun);
174 set_cfun (NULL);
177 /* Restore the last saved context, at the end of a nested function.
178 This function is called from language-specific code. */
180 void
181 pop_function_context (void)
183 struct function *p = function_context_stack.pop ();
184 set_cfun (p);
185 current_function_decl = p->decl;
187 /* Reset variables that have known state during rtx generation. */
188 virtuals_instantiated = 0;
189 generating_concat_p = 1;
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. */
196 void
197 free_after_parsing (struct function *f)
199 f->language = 0;
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. */
206 void
207 free_after_compilation (struct function *f)
209 prologue_insn_hash = NULL;
210 epilogue_insn_hash = NULL;
212 free (crtl->emit.regno_pointer_align);
214 memset (crtl, 0, 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;
221 regno_reg_rtx = NULL;
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. */
228 poly_int64
229 get_frame_size (void)
231 if (FRAME_GROWS_DOWNWARD)
232 return -frame_offset;
233 else
234 return frame_offset;
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. */
241 bool
242 frame_offset_overflow (poly_int64 offset, tree func)
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);
250 if (!coeffs_in_range_p (size, 0U, limit))
252 unsigned HOST_WIDE_INT hwisize;
253 if (size.is_constant (&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;
264 return false;
267 /* Return the minimum spill slot alignment for a register of mode MODE. */
269 unsigned int
270 spill_slot_alignment (machine_mode mode ATTRIBUTE_UNUSED)
272 return STACK_SLOT_ALIGNMENT (NULL_TREE, mode, GET_MODE_ALIGNMENT (mode));
275 /* Return stack slot alignment in bits for TYPE and MODE. */
277 static unsigned int
278 get_stack_local_alignment (tree type, machine_mode mode)
280 unsigned int alignment;
282 if (mode == BLKmode)
283 alignment = BIGGEST_ALIGNMENT;
284 else
285 alignment = GET_MODE_ALIGNMENT (mode);
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);
292 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
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. */
302 static bool
303 try_fit_stack_local (poly_int64 start, poly_int64 length,
304 poly_int64 size, unsigned int alignment,
305 poly_int64 *poffset)
307 poly_int64 this_frame_offset;
308 int frame_off, frame_alignment, frame_phase;
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;
316 /* Round the frame offset to the specified alignment. */
318 if (FRAME_GROWS_DOWNWARD)
319 this_frame_offset
320 = (aligned_lower_bound (start + length - size - frame_phase, alignment)
321 + frame_phase);
322 else
323 this_frame_offset
324 = aligned_upper_bound (start - frame_phase, alignment) + frame_phase;
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 (this_frame_offset, start))
331 if (known_eq (frame_offset, start))
332 frame_offset = this_frame_offset;
333 else
334 return false;
336 else if (maybe_gt (this_frame_offset + size, start + length))
338 if (known_eq (frame_offset, start + length))
339 frame_offset = this_frame_offset + size;
340 else
341 return false;
344 *poffset = this_frame_offset;
345 return true;
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. */
352 static void
353 add_frame_space (poly_int64 start, poly_int64 end)
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;
362 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
363 with machine mode MODE.
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.
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.
377 We do not round to stack_boundary here. */
380 assign_stack_local_1 (machine_mode mode, poly_int64 size,
381 int align, int kind)
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;
388 if (align == 0)
390 alignment = get_stack_local_alignment (NULL, mode);
391 alignment /= BITS_PER_UNIT;
393 else if (align == -1)
395 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
396 size = aligned_upper_bound (size, alignment);
398 else if (align == -2)
399 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
400 else
401 alignment = align / BITS_PER_UNIT;
403 alignment_in_bits = alignment * BITS_PER_UNIT;
405 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
406 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
408 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
409 alignment = MAX_SUPPORTED_STACK_ALIGNMENT / BITS_PER_UNIT;
412 if (SUPPORTS_STACK_ALIGNMENT)
414 if (crtl->stack_alignment_estimated < alignment_in_bits)
416 if (!crtl->stack_realign_processed)
417 crtl->stack_alignment_estimated = alignment_in_bits;
418 else
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)
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;
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;
447 if (mode != BLKmode || maybe_ne (size, 0))
449 if (kind & ASLK_RECORD_PAD)
451 class frame_space **psp;
453 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
455 class frame_space *space = *psp;
456 if (!try_fit_stack_local (space->start, space->length, size,
457 alignment, &slot_offset))
458 continue;
459 *psp = space->next;
460 if (known_gt (slot_offset, space->start))
461 add_frame_space (space->start, slot_offset);
462 if (known_lt (slot_offset + size, space->start + space->length))
463 add_frame_space (slot_offset + size,
464 space->start + space->length);
465 goto found_space;
469 else if (!STACK_ALIGNMENT_NEEDED)
471 slot_offset = frame_offset;
472 goto found_space;
475 old_frame_offset = frame_offset;
477 if (FRAME_GROWS_DOWNWARD)
479 frame_offset -= size;
480 try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
482 if (kind & ASLK_RECORD_PAD)
484 if (known_gt (slot_offset, frame_offset))
485 add_frame_space (frame_offset, slot_offset);
486 if (known_lt (slot_offset + size, old_frame_offset))
487 add_frame_space (slot_offset + size, old_frame_offset);
490 else
492 frame_offset += size;
493 try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
495 if (kind & ASLK_RECORD_PAD)
497 if (known_gt (slot_offset, old_frame_offset))
498 add_frame_space (old_frame_offset, slot_offset);
499 if (known_lt (slot_offset + size, frame_offset))
500 add_frame_space (slot_offset + size, frame_offset);
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)
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 (GET_MODE_SIZE (mode), size))
516 bigend_correction = size - GET_MODE_SIZE (mode);
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));
532 x = gen_rtx_MEM (mode, addr);
533 set_mem_align (x, alignment_in_bits);
534 MEM_NOTRAP_P (x) = 1;
536 vec_safe_push (stack_slot_list, x);
538 if (frame_offset_overflow (frame_offset, current_function_decl))
539 frame_offset = 0;
541 return x;
544 /* Wrap up assign_stack_local_1 with last parameter as false. */
547 assign_stack_local (machine_mode mode, poly_int64 size, int align)
549 return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
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.
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. */
566 class GTY(()) temp_slot {
567 public:
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;
595 /* Entry for the below hash table. */
596 struct GTY((for_user)) temp_slot_address_entry {
597 hashval_t hash;
598 rtx address;
599 class temp_slot *temp_slot;
602 struct temp_address_hasher : ggc_ptr_hash<temp_slot_address_entry>
604 static hashval_t hash (temp_slot_address_entry *);
605 static bool equal (temp_slot_address_entry *, temp_slot_address_entry *);
608 /* A table of addresses that represent a stack slot. The table is a mapping
609 from address RTXen to a temp slot. */
610 static GTY(()) hash_table<temp_address_hasher> *temp_slot_address_table;
611 static size_t n_temp_slots_in_use;
613 /* Removes temporary slot TEMP from LIST. */
615 static void
616 cut_slot_from_list (class temp_slot *temp, class temp_slot **list)
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;
625 temp->prev = temp->next = NULL;
628 /* Inserts temporary slot TEMP to LIST. */
630 static void
631 insert_slot_to_list (class temp_slot *temp, class temp_slot **list)
633 temp->next = *list;
634 if (*list)
635 (*list)->prev = temp;
636 temp->prev = NULL;
637 *list = temp;
640 /* Returns the list of used temp slots at LEVEL. */
642 static class temp_slot **
643 temp_slots_at_level (int level)
645 if (level >= (int) vec_safe_length (used_temp_slots))
646 vec_safe_grow_cleared (used_temp_slots, level + 1, true);
648 return &(*used_temp_slots)[level];
651 /* Returns the maximal temporary slot level. */
653 static int
654 max_slot_level (void)
656 if (!used_temp_slots)
657 return -1;
659 return used_temp_slots->length () - 1;
662 /* Moves temporary slot TEMP to LEVEL. */
664 static void
665 move_slot_to_level (class temp_slot *temp, int level)
667 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
668 insert_slot_to_list (temp, temp_slots_at_level (level));
669 temp->level = level;
672 /* Make temporary slot TEMP available. */
674 static void
675 make_slot_available (class temp_slot *temp)
677 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
678 insert_slot_to_list (temp, &avail_temp_slots);
679 temp->in_use = false;
680 temp->level = -1;
681 n_temp_slots_in_use--;
684 /* Compute the hash value for an address -> temp slot mapping.
685 The value is cached on the mapping entry. */
686 static hashval_t
687 temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
689 int do_not_record = 0;
690 return hash_rtx (t->address, GET_MODE (t->address),
691 &do_not_record, NULL, false);
694 /* Return the hash value for an address -> temp slot mapping. */
695 hashval_t
696 temp_address_hasher::hash (temp_slot_address_entry *t)
698 return t->hash;
701 /* Compare two address -> temp slot mapping entries. */
702 bool
703 temp_address_hasher::equal (temp_slot_address_entry *t1,
704 temp_slot_address_entry *t2)
706 return exp_equiv_p (t1->address, t2->address, 0, true);
709 /* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
710 static void
711 insert_temp_slot_address (rtx address, class temp_slot *temp_slot)
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 (t, t->hash, INSERT) = t;
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. */
723 remove_unused_temp_slot_addresses_1 (temp_slot_address_entry **slot, void *)
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;
731 /* Remove all mappings of addresses to unused temp slots. */
732 static void
733 remove_unused_temp_slot_addresses (void)
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 ();
743 /* Find the temp slot corresponding to the object at address X. */
745 static class temp_slot *
746 find_temp_slot_from_address (rtx x)
748 class temp_slot *p;
749 struct temp_slot_address_entry tmp, *t;
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 (&tmp);
756 t = temp_slot_address_table->find_with_hash (&tmp, tmp.hash);
757 if (t)
758 return t->temp_slot;
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;
769 /* Last resort: Address is a virtual stack var address. */
770 poly_int64 offset;
771 if (strip_offset (x, &offset) == virtual_stack_vars_rtx)
773 int i;
774 for (i = max_slot_level (); i >= 0; i--)
775 for (p = *temp_slots_at_level (i); p; p = p->next)
776 if (known_in_range_p (offset, p->base_offset, p->full_size))
777 return p;
780 return NULL;
783 /* Allocate a temporary stack slot and record it for possible later
784 reuse.
786 MODE is the machine mode to be given to the returned rtx.
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.
791 TYPE is the type that will be used for the stack slot. */
794 assign_stack_temp_for_type (machine_mode mode, poly_int64 size, tree type)
796 unsigned int align;
797 class temp_slot *p, *best_p = 0, *selected = NULL, **pp;
798 rtx slot;
800 gcc_assert (known_size_p (size));
802 align = get_stack_local_alignment (type, mode);
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.
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)
813 for (p = avail_temp_slots; p; p = p->next)
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))))
824 if (p->align == align && known_eq (p->size, size))
826 selected = p;
827 cut_slot_from_list (selected, &avail_temp_slots);
828 best_p = 0;
829 break;
831 best_p = p;
836 /* Make our best, if any, the one to use. */
837 if (best_p)
839 selected = best_p;
840 cut_slot_from_list (selected, &avail_temp_slots);
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)
847 int alignment = best_p->align / BITS_PER_UNIT;
848 poly_int64 rounded_size = aligned_upper_bound (size, alignment);
850 if (known_ge (best_p->size - rounded_size, alignment))
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 (p, &avail_temp_slots);
862 vec_safe_push (stack_slot_list, p->slot);
864 best_p->size = rounded_size;
865 best_p->full_size = rounded_size;
870 /* If we still didn't find one, make a new temporary. */
871 if (selected == 0)
873 poly_int64 frame_offset_old = frame_offset;
875 p = ggc_alloc<temp_slot> ();
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.
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 (mode == BLKmode
887 ? aligned_upper_bound (size,
888 (int) align
889 / BITS_PER_UNIT)
890 : size),
891 align, 0);
893 p->align = align;
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;
907 /* Now define the fields used by combine_temp_slots. */
908 if (FRAME_GROWS_DOWNWARD)
910 p->base_offset = frame_offset;
911 p->full_size = frame_offset_old - frame_offset;
913 else
915 p->base_offset = frame_offset_old;
916 p->full_size = frame_offset - frame_offset_old;
919 selected = p;
922 p = selected;
923 p->in_use = true;
924 p->type = type;
925 p->level = temp_slot_level;
926 n_temp_slots_in_use++;
928 pp = temp_slots_at_level (p->level);
929 insert_slot_to_list (p, pp);
930 insert_temp_slot_address (XEXP (p->slot, 0), p);
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, slot);
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);
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;
947 return slot;
950 /* Allocate a temporary stack slot and record it for possible later
951 reuse. First two arguments are same as in preceding function. */
954 assign_stack_temp (machine_mode mode, poly_int64 size)
956 return assign_stack_temp_for_type (mode, size, NULL_TREE);
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. */
969 assign_temp (tree type_or_decl, int memory_required,
970 int dont_promote ATTRIBUTE_UNUSED)
972 tree type, decl;
973 machine_mode mode;
974 #ifdef PROMOTE_MODE
975 int unsignedp;
976 #endif
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;
983 mode = TYPE_MODE (type);
984 #ifdef PROMOTE_MODE
985 unsignedp = TYPE_UNSIGNED (type);
986 #endif
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));
992 if (mode == BLKmode || memory_required)
994 poly_int64 size;
995 rtx tmp;
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), &size))
1001 size = max_int_size_in_bytes (type);
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;
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 (size)
1014 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
1016 error ("size of variable %q+D is too large", decl);
1017 size = 1;
1020 tmp = assign_stack_temp_for_type (mode, size, type);
1021 return tmp;
1024 #ifdef PROMOTE_MODE
1025 if (! dont_promote)
1026 mode = promote_mode (type, mode, &unsignedp);
1027 #endif
1029 return gen_reg_rtx (mode);
1032 /* Combine temporary stack slots which are adjacent on the stack.
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. */
1038 static void
1039 combine_temp_slots (void)
1041 class temp_slot *p, *q, *next, *next_q;
1042 int num_slots;
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;
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;
1056 for (p = avail_temp_slots; p; p = next)
1058 int delete_p = 0;
1060 next = p->next;
1062 if (GET_MODE (p->slot) != BLKmode)
1063 continue;
1065 for (q = p->next; q; q = next_q)
1067 int delete_q = 0;
1069 next_q = q->next;
1071 if (GET_MODE (q->slot) != BLKmode)
1072 continue;
1074 if (known_eq (p->base_offset + p->full_size, q->base_offset))
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;
1081 else if (known_eq (q->base_offset + q->full_size, p->base_offset))
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;
1089 if (delete_q)
1090 cut_slot_from_list (q, &avail_temp_slots);
1093 /* Either delete P or advance past it. */
1094 if (delete_p)
1095 cut_slot_from_list (p, &avail_temp_slots);
1099 /* Indicate that NEW_RTX is an alternate way of referring to the temp
1100 slot that previously was known by OLD_RTX. */
1102 void
1103 update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1105 class temp_slot *p;
1107 if (rtx_equal_p (old_rtx, new_rtx))
1108 return;
1110 p = find_temp_slot_from_address (old_rtx);
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)
1120 if (GET_CODE (old_rtx) != PLUS)
1121 return;
1123 if (REG_P (new_rtx))
1125 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1126 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1127 return;
1129 else if (GET_CODE (new_rtx) != PLUS)
1130 return;
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));
1141 return;
1144 /* Otherwise add an alias for the temp's address. */
1145 insert_temp_slot_address (new_rtx, p);
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.
1153 This is called when an ({...}) construct occurs and a statement
1154 returns a value in memory. */
1156 void
1157 preserve_temp_slots (rtx x)
1159 class temp_slot *p = 0, *next;
1161 if (x == 0)
1162 return;
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);
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;
1174 /* First see if we can find a match. */
1175 if (p == 0)
1176 p = find_temp_slot_from_address (XEXP (x, 0));
1178 if (p != 0)
1180 if (p->level == temp_slot_level)
1181 move_slot_to_level (p, temp_slot_level - 1);
1182 return;
1185 /* Otherwise, preserve all non-kept slots at this level. */
1186 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1188 next = p->next;
1189 move_slot_to_level (p, temp_slot_level - 1);
1193 /* Free all temporaries used so far. This is normally called at the
1194 end of generating code for a statement. */
1196 void
1197 free_temp_slots (void)
1199 class temp_slot *p, *next;
1200 bool some_available = false;
1202 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1204 next = p->next;
1205 make_slot_available (p);
1206 some_available = true;
1209 if (some_available)
1211 remove_unused_temp_slot_addresses ();
1212 combine_temp_slots ();
1216 /* Push deeper into the nesting level for stack temporaries. */
1218 void
1219 push_temp_slots (void)
1221 temp_slot_level++;
1224 /* Pop a temporary nesting level. All slots in use in the current level
1225 are freed. */
1227 void
1228 pop_temp_slots (void)
1230 free_temp_slots ();
1231 temp_slot_level--;
1234 /* Initialize temporary slots. */
1236 void
1237 init_temp_slots (void)
1239 /* We have not allocated any temporaries yet. */
1240 avail_temp_slots = 0;
1241 vec_alloc (used_temp_slots, 0);
1242 temp_slot_level = 0;
1243 n_temp_slots_in_use = 0;
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 (32);
1248 else
1249 temp_slot_address_table->empty ();
1252 /* Functions and data structures to keep track of the values hard regs
1253 had at the start of the function. */
1255 /* Private type used by get_hard_reg_initial_reg, get_hard_reg_initial_val,
1256 and has_hard_reg_initial_val.. */
1257 struct GTY(()) initial_value_pair {
1258 rtx hard_reg;
1259 rtx pseudo;
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. */
1264 struct GTY(()) initial_value_struct {
1265 int num_entries;
1266 int max_entries;
1267 initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
1270 /* If a pseudo represents an initial hard reg (or expression), return
1271 it, else return NULL_RTX. */
1274 get_hard_reg_initial_reg (rtx reg)
1276 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1277 int i;
1279 if (ivs == 0)
1280 return NULL_RTX;
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;
1286 return NULL_RTX;
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. */
1293 get_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1295 struct initial_value_struct *ivs;
1296 rtx rv;
1298 rv = has_hard_reg_initial_val (mode, regno);
1299 if (rv)
1300 return rv;
1302 ivs = crtl->hard_reg_initial_vals;
1303 if (ivs == 0)
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> (5);
1309 crtl->hard_reg_initial_vals = ivs;
1312 if (ivs->num_entries >= ivs->max_entries)
1314 ivs->max_entries += 5;
1315 ivs->entries = GGC_RESIZEVEC (initial_value_pair, ivs->entries,
1316 ivs->max_entries);
1319 ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
1320 ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
1322 return ivs->entries[ivs->num_entries++].pseudo;
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. */
1330 has_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1332 struct initial_value_struct *ivs;
1333 int i;
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;
1342 return NULL_RTX;
1345 void
1346 emit_initial_value_sets (void)
1348 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1349 int i;
1350 rtx_insn *seq;
1352 if (ivs == 0)
1353 return;
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 = get_insns ();
1359 end_sequence ();
1361 emit_insn_at_entry (seq);
1364 /* Return the hardreg-pseudoreg initial values pair entry I and
1365 TRUE if I is a valid entry, or FALSE if I is not a valid entry. */
1366 bool
1367 initial_value_entry (int i, rtx *hreg, rtx *preg)
1369 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1370 if (!ivs || i >= ivs->num_entries)
1371 return false;
1373 *hreg = ivs->entries[i].hard_reg;
1374 *preg = ivs->entries[i].pseudo;
1375 return true;
1378 /* These routines are responsible for converting virtual register references
1379 to the actual hard register references once RTL generation is complete.
1381 The following four variables are used for communication between the
1382 routines. They contain the offsets of the virtual registers from their
1383 respective hard registers. */
1385 static poly_int64 in_arg_offset;
1386 static poly_int64 var_offset;
1387 static poly_int64 dynamic_offset;
1388 static poly_int64 out_arg_offset;
1389 static poly_int64 cfa_offset;
1391 /* In most machines, the stack pointer register is equivalent to the bottom
1392 of the stack. */
1394 #ifndef STACK_POINTER_OFFSET
1395 #define STACK_POINTER_OFFSET 0
1396 #endif
1398 #if defined (REG_PARM_STACK_SPACE) && !defined (INCOMING_REG_PARM_STACK_SPACE)
1399 #define INCOMING_REG_PARM_STACK_SPACE REG_PARM_STACK_SPACE
1400 #endif
1402 /* If not defined, pick an appropriate default for the offset of dynamically
1403 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1404 INCOMING_REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1406 #ifndef STACK_DYNAMIC_OFFSET
1408 /* The bottom of the stack points to the actual arguments. If
1409 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1410 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1411 stack space for register parameters is not pushed by the caller, but
1412 rather part of the fixed stack areas and hence not included in
1413 `crtl->outgoing_args_size'. Nevertheless, we must allow
1414 for it when allocating stack dynamic objects. */
1416 #ifdef INCOMING_REG_PARM_STACK_SPACE
1417 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1418 ((ACCUMULATE_OUTGOING_ARGS \
1419 ? (crtl->outgoing_args_size \
1420 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1421 : INCOMING_REG_PARM_STACK_SPACE (FNDECL))) \
1422 : 0) + (STACK_POINTER_OFFSET))
1423 #else
1424 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1425 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : poly_int64 (0)) \
1426 + (STACK_POINTER_OFFSET))
1427 #endif
1428 #endif
1431 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1432 is a virtual register, return the equivalent hard register and set the
1433 offset indirectly through the pointer. Otherwise, return 0. */
1435 static rtx
1436 instantiate_new_reg (rtx x, poly_int64 *poffset)
1438 rtx new_rtx;
1439 poly_int64 offset;
1441 if (x == virtual_incoming_args_rtx)
1443 if (stack_realign_drap)
1445 /* Replace virtual_incoming_args_rtx with internal arg
1446 pointer if DRAP is used to realign stack. */
1447 new_rtx = crtl->args.internal_arg_pointer;
1448 offset = 0;
1450 else
1451 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1453 else if (x == virtual_stack_vars_rtx)
1454 new_rtx = frame_pointer_rtx, offset = var_offset;
1455 else if (x == virtual_stack_dynamic_rtx)
1456 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1457 else if (x == virtual_outgoing_args_rtx)
1458 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1459 else if (x == virtual_cfa_rtx)
1461 #ifdef FRAME_POINTER_CFA_OFFSET
1462 new_rtx = frame_pointer_rtx;
1463 #else
1464 new_rtx = arg_pointer_rtx;
1465 #endif
1466 offset = cfa_offset;
1468 else if (x == virtual_preferred_stack_boundary_rtx)
1470 new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1471 offset = 0;
1473 else
1474 return NULL_RTX;
1476 *poffset = offset;
1477 return new_rtx;
1480 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1481 registers present inside of *LOC. The expression is simplified,
1482 as much as possible, but is not to be considered "valid" in any sense
1483 implied by the target. Return true if any change is made. */
1485 static bool
1486 instantiate_virtual_regs_in_rtx (rtx *loc)
1488 if (!*loc)
1489 return false;
1490 bool changed = false;
1491 subrtx_ptr_iterator::array_type array;
1492 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
1494 rtx *loc = *iter;
1495 if (rtx x = *loc)
1497 rtx new_rtx;
1498 poly_int64 offset;
1499 switch (GET_CODE (x))
1501 case REG:
1502 new_rtx = instantiate_new_reg (x, &offset);
1503 if (new_rtx)
1505 *loc = plus_constant (GET_MODE (x), new_rtx, offset);
1506 changed = true;
1508 iter.skip_subrtxes ();
1509 break;
1511 case PLUS:
1512 new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1513 if (new_rtx)
1515 XEXP (x, 0) = new_rtx;
1516 *loc = plus_constant (GET_MODE (x), x, offset, true);
1517 changed = true;
1518 iter.skip_subrtxes ();
1519 break;
1522 /* FIXME -- from old code */
1523 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1524 we can commute the PLUS and SUBREG because pointers into the
1525 frame are well-behaved. */
1526 break;
1528 default:
1529 break;
1533 return changed;
1536 /* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1537 matches the predicate for insn CODE operand OPERAND. */
1539 static bool
1540 safe_insn_predicate (int code, int operand, rtx x)
1542 return code < 0 || insn_operand_matches ((enum insn_code) code, operand, x);
1545 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1546 registers present inside of insn. The result will be a valid insn. */
1548 static void
1549 instantiate_virtual_regs_in_insn (rtx_insn *insn)
1551 poly_int64 offset;
1552 int insn_code, i;
1553 bool any_change = false;
1554 rtx set, new_rtx, x;
1555 rtx_insn *seq;
1557 /* There are some special cases to be handled first. */
1558 set = single_set (insn);
1559 if (set)
1561 /* We're allowed to assign to a virtual register. This is interpreted
1562 to mean that the underlying register gets assigned the inverse
1563 transformation. This is used, for example, in the handling of
1564 non-local gotos. */
1565 new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1566 if (new_rtx)
1568 start_sequence ();
1570 instantiate_virtual_regs_in_rtx (&SET_SRC (set));
1571 x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
1572 gen_int_mode (-offset, GET_MODE (new_rtx)));
1573 x = force_operand (x, new_rtx);
1574 if (x != new_rtx)
1575 emit_move_insn (new_rtx, x);
1577 seq = get_insns ();
1578 end_sequence ();
1580 emit_insn_before (seq, insn);
1581 delete_insn (insn);
1582 return;
1585 /* Handle a straight copy from a virtual register by generating a
1586 new add insn. The difference between this and falling through
1587 to the generic case is avoiding a new pseudo and eliminating a
1588 move insn in the initial rtl stream. */
1589 new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1590 if (new_rtx
1591 && maybe_ne (offset, 0)
1592 && REG_P (SET_DEST (set))
1593 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1595 start_sequence ();
1597 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS, new_rtx,
1598 gen_int_mode (offset,
1599 GET_MODE (SET_DEST (set))),
1600 SET_DEST (set), 1, OPTAB_LIB_WIDEN);
1601 if (x != SET_DEST (set))
1602 emit_move_insn (SET_DEST (set), x);
1604 seq = get_insns ();
1605 end_sequence ();
1607 emit_insn_before (seq, insn);
1608 delete_insn (insn);
1609 return;
1612 extract_insn (insn);
1613 insn_code = INSN_CODE (insn);
1615 /* Handle a plus involving a virtual register by determining if the
1616 operands remain valid if they're modified in place. */
1617 poly_int64 delta;
1618 if (GET_CODE (SET_SRC (set)) == PLUS
1619 && recog_data.n_operands >= 3
1620 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1621 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1622 && poly_int_rtx_p (recog_data.operand[2], &delta)
1623 && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
1625 offset += delta;
1627 /* If the sum is zero, then replace with a plain move. */
1628 if (known_eq (offset, 0)
1629 && REG_P (SET_DEST (set))
1630 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1632 start_sequence ();
1633 emit_move_insn (SET_DEST (set), new_rtx);
1634 seq = get_insns ();
1635 end_sequence ();
1637 emit_insn_before (seq, insn);
1638 delete_insn (insn);
1639 return;
1642 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1644 /* Using validate_change and apply_change_group here leaves
1645 recog_data in an invalid state. Since we know exactly what
1646 we want to check, do those two by hand. */
1647 if (safe_insn_predicate (insn_code, 1, new_rtx)
1648 && safe_insn_predicate (insn_code, 2, x))
1650 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1651 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1652 any_change = true;
1654 /* Fall through into the regular operand fixup loop in
1655 order to take care of operands other than 1 and 2. */
1659 else
1661 extract_insn (insn);
1662 insn_code = INSN_CODE (insn);
1665 /* In the general case, we expect virtual registers to appear only in
1666 operands, and then only as either bare registers or inside memories. */
1667 for (i = 0; i < recog_data.n_operands; ++i)
1669 x = recog_data.operand[i];
1670 switch (GET_CODE (x))
1672 case MEM:
1674 rtx addr = XEXP (x, 0);
1676 if (!instantiate_virtual_regs_in_rtx (&addr))
1677 continue;
1679 start_sequence ();
1680 x = replace_equiv_address (x, addr, true);
1681 /* It may happen that the address with the virtual reg
1682 was valid (e.g. based on the virtual stack reg, which might
1683 be acceptable to the predicates with all offsets), whereas
1684 the address now isn't anymore, for instance when the address
1685 is still offsetted, but the base reg isn't virtual-stack-reg
1686 anymore. Below we would do a force_reg on the whole operand,
1687 but this insn might actually only accept memory. Hence,
1688 before doing that last resort, try to reload the address into
1689 a register, so this operand stays a MEM. */
1690 if (!safe_insn_predicate (insn_code, i, x))
1692 addr = force_reg (GET_MODE (addr), addr);
1693 x = replace_equiv_address (x, addr, true);
1695 seq = get_insns ();
1696 end_sequence ();
1697 if (seq)
1698 emit_insn_before (seq, insn);
1700 break;
1702 case REG:
1703 new_rtx = instantiate_new_reg (x, &offset);
1704 if (new_rtx == NULL)
1705 continue;
1706 if (known_eq (offset, 0))
1707 x = new_rtx;
1708 else
1710 start_sequence ();
1712 /* Careful, special mode predicates may have stuff in
1713 insn_data[insn_code].operand[i].mode that isn't useful
1714 to us for computing a new value. */
1715 /* ??? Recognize address_operand and/or "p" constraints
1716 to see if (plus new offset) is a valid before we put
1717 this through expand_simple_binop. */
1718 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1719 gen_int_mode (offset, GET_MODE (x)),
1720 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1721 seq = get_insns ();
1722 end_sequence ();
1723 emit_insn_before (seq, insn);
1725 break;
1727 case SUBREG:
1728 new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1729 if (new_rtx == NULL)
1730 continue;
1731 if (maybe_ne (offset, 0))
1733 start_sequence ();
1734 new_rtx = expand_simple_binop
1735 (GET_MODE (new_rtx), PLUS, new_rtx,
1736 gen_int_mode (offset, GET_MODE (new_rtx)),
1737 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1738 seq = get_insns ();
1739 end_sequence ();
1740 emit_insn_before (seq, insn);
1742 x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1743 GET_MODE (new_rtx), SUBREG_BYTE (x));
1744 gcc_assert (x);
1745 break;
1747 default:
1748 continue;
1751 /* At this point, X contains the new value for the operand.
1752 Validate the new value vs the insn predicate. Note that
1753 asm insns will have insn_code -1 here. */
1754 if (!safe_insn_predicate (insn_code, i, x))
1756 start_sequence ();
1757 if (REG_P (x))
1759 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1760 x = copy_to_reg (x);
1762 else
1763 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1764 seq = get_insns ();
1765 end_sequence ();
1766 if (seq)
1767 emit_insn_before (seq, insn);
1770 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1771 any_change = true;
1774 if (any_change)
1776 /* Propagate operand changes into the duplicates. */
1777 for (i = 0; i < recog_data.n_dups; ++i)
1778 *recog_data.dup_loc[i]
1779 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1781 /* Force re-recognition of the instruction for validation. */
1782 INSN_CODE (insn) = -1;
1785 if (asm_noperands (PATTERN (insn)) >= 0)
1787 if (!check_asm_operands (PATTERN (insn)))
1789 error_for_asm (insn, "impossible constraint in %<asm%>");
1790 /* For asm goto, instead of fixing up all the edges
1791 just clear the template and clear input and output operands
1792 and strip away clobbers. */
1793 if (JUMP_P (insn))
1795 rtx asm_op = extract_asm_operands (PATTERN (insn));
1796 PATTERN (insn) = asm_op;
1797 PUT_MODE (asm_op, VOIDmode);
1798 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1799 ASM_OPERANDS_OUTPUT_CONSTRAINT (asm_op) = "";
1800 ASM_OPERANDS_OUTPUT_IDX (asm_op) = 0;
1801 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1802 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1804 else
1805 delete_insn (insn);
1808 else
1810 if (recog_memoized (insn) < 0)
1811 fatal_insn_not_found (insn);
1815 /* Subroutine of instantiate_decls. Given RTL representing a decl,
1816 do any instantiation required. */
1818 void
1819 instantiate_decl_rtl (rtx x)
1821 rtx addr;
1823 if (x == 0)
1824 return;
1826 /* If this is a CONCAT, recurse for the pieces. */
1827 if (GET_CODE (x) == CONCAT)
1829 instantiate_decl_rtl (XEXP (x, 0));
1830 instantiate_decl_rtl (XEXP (x, 1));
1831 return;
1834 /* If this is not a MEM, no need to do anything. Similarly if the
1835 address is a constant or a register that is not a virtual register. */
1836 if (!MEM_P (x))
1837 return;
1839 addr = XEXP (x, 0);
1840 if (CONSTANT_P (addr)
1841 || (REG_P (addr)
1842 && !VIRTUAL_REGISTER_P (addr)))
1843 return;
1845 instantiate_virtual_regs_in_rtx (&XEXP (x, 0));
1848 /* Helper for instantiate_decls called via walk_tree: Process all decls
1849 in the given DECL_VALUE_EXPR. */
1851 static tree
1852 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1854 tree t = *tp;
1855 if (! EXPR_P (t))
1857 *walk_subtrees = 0;
1858 if (DECL_P (t))
1860 if (DECL_RTL_SET_P (t))
1861 instantiate_decl_rtl (DECL_RTL (t));
1862 if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1863 && DECL_INCOMING_RTL (t))
1864 instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1865 if ((VAR_P (t) || TREE_CODE (t) == RESULT_DECL)
1866 && DECL_HAS_VALUE_EXPR_P (t))
1868 tree v = DECL_VALUE_EXPR (t);
1869 walk_tree (&v, instantiate_expr, NULL, NULL);
1873 return NULL;
1876 /* Subroutine of instantiate_decls: Process all decls in the given
1877 BLOCK node and all its subblocks. */
1879 static void
1880 instantiate_decls_1 (tree let)
1882 tree t;
1884 for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1886 if (DECL_RTL_SET_P (t))
1887 instantiate_decl_rtl (DECL_RTL (t));
1888 if (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
1890 tree v = DECL_VALUE_EXPR (t);
1891 walk_tree (&v, instantiate_expr, NULL, NULL);
1895 /* Process all subblocks. */
1896 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1897 instantiate_decls_1 (t);
1900 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1901 all virtual registers in their DECL_RTL's. */
1903 static void
1904 instantiate_decls (tree fndecl)
1906 tree decl;
1907 unsigned ix;
1909 /* Process all parameters of the function. */
1910 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1912 instantiate_decl_rtl (DECL_RTL (decl));
1913 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1914 if (DECL_HAS_VALUE_EXPR_P (decl))
1916 tree v = DECL_VALUE_EXPR (decl);
1917 walk_tree (&v, instantiate_expr, NULL, NULL);
1921 if ((decl = DECL_RESULT (fndecl))
1922 && TREE_CODE (decl) == RESULT_DECL)
1924 if (DECL_RTL_SET_P (decl))
1925 instantiate_decl_rtl (DECL_RTL (decl));
1926 if (DECL_HAS_VALUE_EXPR_P (decl))
1928 tree v = DECL_VALUE_EXPR (decl);
1929 walk_tree (&v, instantiate_expr, NULL, NULL);
1933 /* Process the saved static chain if it exists. */
1934 decl = DECL_STRUCT_FUNCTION (fndecl)->static_chain_decl;
1935 if (decl && DECL_HAS_VALUE_EXPR_P (decl))
1936 instantiate_decl_rtl (DECL_RTL (DECL_VALUE_EXPR (decl)));
1938 /* Now process all variables defined in the function or its subblocks. */
1939 if (DECL_INITIAL (fndecl))
1940 instantiate_decls_1 (DECL_INITIAL (fndecl));
1942 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1943 if (DECL_RTL_SET_P (decl))
1944 instantiate_decl_rtl (DECL_RTL (decl));
1945 vec_free (cfun->local_decls);
1948 /* Return the value of STACK_DYNAMIC_OFFSET for the current function.
1949 This is done through a function wrapper so that the macro sees a
1950 predictable set of included files. */
1952 poly_int64
1953 get_stack_dynamic_offset ()
1955 return STACK_DYNAMIC_OFFSET (current_function_decl);
1958 /* Pass through the INSNS of function FNDECL and convert virtual register
1959 references to hard register references. */
1961 static void
1962 instantiate_virtual_regs (void)
1964 rtx_insn *insn;
1966 /* Compute the offsets to use for this function. */
1967 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1968 var_offset = targetm.starting_frame_offset ();
1969 dynamic_offset = get_stack_dynamic_offset ();
1970 out_arg_offset = STACK_POINTER_OFFSET;
1971 #ifdef FRAME_POINTER_CFA_OFFSET
1972 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1973 #else
1974 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1975 #endif
1977 /* Initialize recognition, indicating that volatile is OK. */
1978 init_recog ();
1980 /* Scan through all the insns, instantiating every virtual register still
1981 present. */
1982 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1983 if (INSN_P (insn))
1985 /* These patterns in the instruction stream can never be recognized.
1986 Fortunately, they shouldn't contain virtual registers either. */
1987 if (GET_CODE (PATTERN (insn)) == USE
1988 || GET_CODE (PATTERN (insn)) == CLOBBER
1989 || GET_CODE (PATTERN (insn)) == ASM_INPUT
1990 || DEBUG_MARKER_INSN_P (insn))
1991 continue;
1992 else if (DEBUG_BIND_INSN_P (insn))
1993 instantiate_virtual_regs_in_rtx (INSN_VAR_LOCATION_PTR (insn));
1994 else
1995 instantiate_virtual_regs_in_insn (insn);
1997 if (insn->deleted ())
1998 continue;
2000 instantiate_virtual_regs_in_rtx (&REG_NOTES (insn));
2002 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
2003 if (CALL_P (insn))
2004 instantiate_virtual_regs_in_rtx (&CALL_INSN_FUNCTION_USAGE (insn));
2007 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
2008 instantiate_decls (current_function_decl);
2010 targetm.instantiate_decls ();
2012 /* Indicate that, from now on, assign_stack_local should use
2013 frame_pointer_rtx. */
2014 virtuals_instantiated = 1;
2017 namespace {
2019 const pass_data pass_data_instantiate_virtual_regs =
2021 RTL_PASS, /* type */
2022 "vregs", /* name */
2023 OPTGROUP_NONE, /* optinfo_flags */
2024 TV_NONE, /* tv_id */
2025 0, /* properties_required */
2026 0, /* properties_provided */
2027 0, /* properties_destroyed */
2028 0, /* todo_flags_start */
2029 0, /* todo_flags_finish */
2032 class pass_instantiate_virtual_regs : public rtl_opt_pass
2034 public:
2035 pass_instantiate_virtual_regs (gcc::context *ctxt)
2036 : rtl_opt_pass (pass_data_instantiate_virtual_regs, ctxt)
2039 /* opt_pass methods: */
2040 unsigned int execute (function *) final override
2042 instantiate_virtual_regs ();
2043 return 0;
2046 }; // class pass_instantiate_virtual_regs
2048 } // anon namespace
2050 rtl_opt_pass *
2051 make_pass_instantiate_virtual_regs (gcc::context *ctxt)
2053 return new pass_instantiate_virtual_regs (ctxt);
2057 /* Return true if EXP is an aggregate type (or a value with aggregate type).
2058 This means a type for which function calls must pass an address to the
2059 function or get an address back from the function.
2060 EXP may be a type node or an expression (whose type is tested). */
2062 bool
2063 aggregate_value_p (const_tree exp, const_tree fntype)
2065 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
2066 int i, regno, nregs;
2067 rtx reg;
2069 if (fntype)
2070 switch (TREE_CODE (fntype))
2072 case CALL_EXPR:
2074 tree fndecl = get_callee_fndecl (fntype);
2075 if (fndecl)
2076 fntype = TREE_TYPE (fndecl);
2077 else if (CALL_EXPR_FN (fntype))
2078 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype)));
2079 else
2080 /* For internal functions, assume nothing needs to be
2081 returned in memory. */
2082 return false;
2084 break;
2085 case FUNCTION_DECL:
2086 fntype = TREE_TYPE (fntype);
2087 break;
2088 case FUNCTION_TYPE:
2089 case METHOD_TYPE:
2090 break;
2091 case IDENTIFIER_NODE:
2092 fntype = NULL_TREE;
2093 break;
2094 default:
2095 /* We don't expect other tree types here. */
2096 gcc_unreachable ();
2099 if (VOID_TYPE_P (type))
2100 return false;
2102 if (error_operand_p (fntype))
2103 return false;
2105 /* If a record should be passed the same as its first (and only) member
2106 don't pass it as an aggregate. */
2107 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2108 return aggregate_value_p (first_field (type), fntype);
2110 /* If the front end has decided that this needs to be passed by
2111 reference, do so. */
2112 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2113 && DECL_BY_REFERENCE (exp))
2114 return true;
2116 /* Function types that are TREE_ADDRESSABLE force return in memory. */
2117 if (fntype && TREE_ADDRESSABLE (fntype))
2118 return true;
2120 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2121 and thus can't be returned in registers. */
2122 if (TREE_ADDRESSABLE (type))
2123 return true;
2125 if (TYPE_EMPTY_P (type))
2126 return false;
2128 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2129 return true;
2131 if (targetm.calls.return_in_memory (type, fntype))
2132 return true;
2134 /* Make sure we have suitable call-clobbered regs to return
2135 the value in; if not, we must return it in memory. */
2136 reg = hard_function_value (type, 0, fntype, 0);
2138 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2139 it is OK. */
2140 if (!REG_P (reg))
2141 return false;
2143 /* Use the default ABI if the type of the function isn't known.
2144 The scheme for handling interoperability between different ABIs
2145 requires us to be able to tell when we're calling a function with
2146 a nondefault ABI. */
2147 const predefined_function_abi &abi = (fntype
2148 ? fntype_abi (fntype)
2149 : default_function_abi);
2150 regno = REGNO (reg);
2151 nregs = hard_regno_nregs (regno, TYPE_MODE (type));
2152 for (i = 0; i < nregs; i++)
2153 if (!fixed_regs[regno + i] && !abi.clobbers_full_reg_p (regno + i))
2154 return true;
2156 return false;
2159 /* Return true if we should assign DECL a pseudo register; false if it
2160 should live on the local stack. */
2162 bool
2163 use_register_for_decl (const_tree decl)
2165 if (TREE_CODE (decl) == SSA_NAME)
2167 /* We often try to use the SSA_NAME, instead of its underlying
2168 decl, to get type information and guide decisions, to avoid
2169 differences of behavior between anonymous and named
2170 variables, but in this one case we have to go for the actual
2171 variable if there is one. The main reason is that, at least
2172 at -O0, we want to place user variables on the stack, but we
2173 don't mind using pseudos for anonymous or ignored temps.
2174 Should we take the SSA_NAME, we'd conclude all SSA_NAMEs
2175 should go in pseudos, whereas their corresponding variables
2176 might have to go on the stack. So, disregarding the decl
2177 here would negatively impact debug info at -O0, enable
2178 coalescing between SSA_NAMEs that ought to get different
2179 stack/pseudo assignments, and get the incoming argument
2180 processing thoroughly confused by PARM_DECLs expected to live
2181 in stack slots but assigned to pseudos. */
2182 if (!SSA_NAME_VAR (decl))
2183 return TYPE_MODE (TREE_TYPE (decl)) != BLKmode
2184 && !(flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)));
2186 decl = SSA_NAME_VAR (decl);
2189 /* Honor volatile. */
2190 if (TREE_SIDE_EFFECTS (decl))
2191 return false;
2193 /* Honor addressability. */
2194 if (TREE_ADDRESSABLE (decl))
2195 return false;
2197 /* RESULT_DECLs are a bit special in that they're assigned without
2198 regard to use_register_for_decl, but we generally only store in
2199 them. If we coalesce their SSA NAMEs, we'd better return a
2200 result that matches the assignment in expand_function_start. */
2201 if (TREE_CODE (decl) == RESULT_DECL)
2203 /* If it's not an aggregate, we're going to use a REG or a
2204 PARALLEL containing a REG. */
2205 if (!aggregate_value_p (decl, current_function_decl))
2206 return true;
2208 /* If expand_function_start determines the return value, we'll
2209 use MEM if it's not by reference. */
2210 if (cfun->returns_pcc_struct
2211 || (targetm.calls.struct_value_rtx
2212 (TREE_TYPE (current_function_decl), 1)))
2213 return DECL_BY_REFERENCE (decl);
2215 /* Otherwise, we're taking an extra all.function_result_decl
2216 argument. It's set up in assign_parms_augmented_arg_list,
2217 under the (negated) conditions above, and then it's used to
2218 set up the RESULT_DECL rtl in assign_params, after looping
2219 over all parameters. Now, if the RESULT_DECL is not by
2220 reference, we'll use a MEM either way. */
2221 if (!DECL_BY_REFERENCE (decl))
2222 return false;
2224 /* Otherwise, if RESULT_DECL is DECL_BY_REFERENCE, it will take
2225 the function_result_decl's assignment. Since it's a pointer,
2226 we can short-circuit a number of the tests below, and we must
2227 duplicate them because we don't have the function_result_decl
2228 to test. */
2229 if (!targetm.calls.allocate_stack_slots_for_args ())
2230 return true;
2231 /* We don't set DECL_IGNORED_P for the function_result_decl. */
2232 if (optimize)
2233 return true;
2234 /* Needed for [[musttail]] which can operate even at -O0 */
2235 if (cfun->tail_call_marked)
2236 return true;
2237 /* We don't set DECL_REGISTER for the function_result_decl. */
2238 return false;
2241 /* Only register-like things go in registers. */
2242 if (DECL_MODE (decl) == BLKmode)
2243 return false;
2245 /* If -ffloat-store specified, don't put explicit float variables
2246 into registers. */
2247 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2248 propagates values across these stores, and it probably shouldn't. */
2249 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2250 return false;
2252 if (!targetm.calls.allocate_stack_slots_for_args ())
2253 return true;
2255 /* If we're not interested in tracking debugging information for
2256 this decl, then we can certainly put it in a register. */
2257 if (DECL_IGNORED_P (decl))
2258 return true;
2260 if (optimize)
2261 return true;
2263 /* Thunks force a tail call even at -O0 so we need to avoid creating a
2264 dangling reference in case the parameter is passed by reference. */
2265 if (TREE_CODE (decl) == PARM_DECL && cfun->tail_call_marked)
2266 return true;
2268 if (!DECL_REGISTER (decl))
2269 return false;
2271 /* When not optimizing, disregard register keyword for types that
2272 could have methods, otherwise the methods won't be callable from
2273 the debugger. */
2274 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
2275 return false;
2277 return true;
2280 /* Structures to communicate between the subroutines of assign_parms.
2281 The first holds data persistent across all parameters, the second
2282 is cleared out for each parameter. */
2284 struct assign_parm_data_all
2286 /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2287 should become a job of the target or otherwise encapsulated. */
2288 CUMULATIVE_ARGS args_so_far_v;
2289 cumulative_args_t args_so_far;
2290 struct args_size stack_args_size;
2291 tree function_result_decl;
2292 tree orig_fnargs;
2293 rtx_insn *first_conversion_insn;
2294 rtx_insn *last_conversion_insn;
2295 HOST_WIDE_INT pretend_args_size;
2296 HOST_WIDE_INT extra_pretend_bytes;
2297 int reg_parm_stack_space;
2300 struct assign_parm_data_one
2302 tree nominal_type;
2303 function_arg_info arg;
2304 rtx entry_parm;
2305 rtx stack_parm;
2306 machine_mode nominal_mode;
2307 machine_mode passed_mode;
2308 struct locate_and_pad_arg_data locate;
2309 int partial;
2312 /* A subroutine of assign_parms. Initialize ALL. */
2314 static void
2315 assign_parms_initialize_all (struct assign_parm_data_all *all)
2317 tree fntype ATTRIBUTE_UNUSED;
2319 memset (all, 0, sizeof (*all));
2321 fntype = TREE_TYPE (current_function_decl);
2323 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
2324 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2325 #else
2326 INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2327 current_function_decl, -1);
2328 #endif
2329 all->args_so_far = pack_cumulative_args (&all->args_so_far_v);
2331 #ifdef INCOMING_REG_PARM_STACK_SPACE
2332 all->reg_parm_stack_space
2333 = INCOMING_REG_PARM_STACK_SPACE (current_function_decl);
2334 #endif
2337 /* If ARGS contains entries with complex types, split the entry into two
2338 entries of the component type. Return a new list of substitutions are
2339 needed, else the old list. */
2341 static void
2342 split_complex_args (vec<tree> *args)
2344 unsigned i;
2345 tree p;
2347 FOR_EACH_VEC_ELT (*args, i, p)
2349 tree type = TREE_TYPE (p);
2350 if (TREE_CODE (type) == COMPLEX_TYPE
2351 && targetm.calls.split_complex_arg (type))
2353 tree decl;
2354 tree subtype = TREE_TYPE (type);
2355 bool addressable = TREE_ADDRESSABLE (p);
2357 /* Rewrite the PARM_DECL's type with its component. */
2358 p = copy_node (p);
2359 TREE_TYPE (p) = subtype;
2360 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2361 SET_DECL_MODE (p, VOIDmode);
2362 DECL_SIZE (p) = NULL;
2363 DECL_SIZE_UNIT (p) = NULL;
2364 /* If this arg must go in memory, put it in a pseudo here.
2365 We can't allow it to go in memory as per normal parms,
2366 because the usual place might not have the imag part
2367 adjacent to the real part. */
2368 DECL_ARTIFICIAL (p) = addressable;
2369 DECL_IGNORED_P (p) = addressable;
2370 TREE_ADDRESSABLE (p) = 0;
2371 layout_decl (p, 0);
2372 (*args)[i] = p;
2374 /* Build a second synthetic decl. */
2375 decl = build_decl (EXPR_LOCATION (p),
2376 PARM_DECL, NULL_TREE, subtype);
2377 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2378 DECL_ARTIFICIAL (decl) = addressable;
2379 DECL_IGNORED_P (decl) = addressable;
2380 layout_decl (decl, 0);
2381 args->safe_insert (++i, decl);
2386 /* A subroutine of assign_parms. Adjust the parameter list to incorporate
2387 the hidden struct return argument, and (abi willing) complex args.
2388 Return the new parameter list. */
2390 static vec<tree>
2391 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2393 tree fndecl = current_function_decl;
2394 tree fntype = TREE_TYPE (fndecl);
2395 vec<tree> fnargs = vNULL;
2396 tree arg;
2398 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2399 fnargs.safe_push (arg);
2401 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2403 /* If struct value address is treated as the first argument, make it so. */
2404 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2405 && ! cfun->returns_pcc_struct
2406 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2408 tree type = build_pointer_type (TREE_TYPE (fntype));
2409 tree decl;
2411 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2412 PARM_DECL, get_identifier (".result_ptr"), type);
2413 DECL_ARG_TYPE (decl) = type;
2414 DECL_ARTIFICIAL (decl) = 1;
2415 DECL_NAMELESS (decl) = 1;
2416 TREE_CONSTANT (decl) = 1;
2417 /* We don't set DECL_IGNORED_P or DECL_REGISTER here. If this
2418 changes, the end of the RESULT_DECL handling block in
2419 use_register_for_decl must be adjusted to match. */
2421 DECL_CHAIN (decl) = all->orig_fnargs;
2422 all->orig_fnargs = decl;
2423 fnargs.safe_insert (0, decl);
2425 all->function_result_decl = decl;
2428 /* If the target wants to split complex arguments into scalars, do so. */
2429 if (targetm.calls.split_complex_arg)
2430 split_complex_args (&fnargs);
2432 return fnargs;
2435 /* A subroutine of assign_parms. Examine PARM and pull out type and mode
2436 data for the parameter. Incorporate ABI specifics such as pass-by-
2437 reference and type promotion. */
2439 static void
2440 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2441 struct assign_parm_data_one *data)
2443 int unsignedp;
2445 *data = assign_parm_data_one ();
2447 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
2448 if (!cfun->stdarg)
2449 data->arg.named = 1; /* No variadic parms. */
2450 else if (DECL_CHAIN (parm))
2451 data->arg.named = 1; /* Not the last non-variadic parm. */
2452 else if (targetm.calls.strict_argument_naming (all->args_so_far))
2453 data->arg.named = 1; /* Only variadic ones are unnamed. */
2454 else
2455 data->arg.named = 0; /* Treat as variadic. */
2457 data->nominal_type = TREE_TYPE (parm);
2458 data->arg.type = DECL_ARG_TYPE (parm);
2460 /* Look out for errors propagating this far. Also, if the parameter's
2461 type is void then its value doesn't matter. */
2462 if (TREE_TYPE (parm) == error_mark_node
2463 /* This can happen after weird syntax errors
2464 or if an enum type is defined among the parms. */
2465 || TREE_CODE (parm) != PARM_DECL
2466 || data->arg.type == NULL
2467 || VOID_TYPE_P (data->nominal_type))
2469 data->nominal_type = data->arg.type = void_type_node;
2470 data->nominal_mode = data->passed_mode = data->arg.mode = VOIDmode;
2471 return;
2474 /* Find mode of arg as it is passed, and mode of arg as it should be
2475 during execution of this function. */
2476 data->passed_mode = data->arg.mode = TYPE_MODE (data->arg.type);
2477 data->nominal_mode = TYPE_MODE (data->nominal_type);
2479 /* If the parm is to be passed as a transparent union or record, use the
2480 type of the first field for the tests below. We have already verified
2481 that the modes are the same. */
2482 if (RECORD_OR_UNION_TYPE_P (data->arg.type)
2483 && TYPE_TRANSPARENT_AGGR (data->arg.type))
2484 data->arg.type = TREE_TYPE (first_field (data->arg.type));
2486 /* See if this arg was passed by invisible reference. */
2487 if (apply_pass_by_reference_rules (&all->args_so_far_v, data->arg))
2489 data->nominal_type = data->arg.type;
2490 data->passed_mode = data->nominal_mode = data->arg.mode;
2493 /* Find mode as it is passed by the ABI. */
2494 unsignedp = TYPE_UNSIGNED (data->arg.type);
2495 data->arg.mode
2496 = promote_function_mode (data->arg.type, data->arg.mode, &unsignedp,
2497 TREE_TYPE (current_function_decl), 0);
2500 /* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2502 static void
2503 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2504 struct assign_parm_data_one *data, bool no_rtl)
2506 int varargs_pretend_bytes = 0;
2508 function_arg_info last_named_arg = data->arg;
2509 last_named_arg.named = true;
2510 targetm.calls.setup_incoming_varargs (all->args_so_far, last_named_arg,
2511 &varargs_pretend_bytes, no_rtl);
2513 /* If the back-end has requested extra stack space, record how much is
2514 needed. Do not change pretend_args_size otherwise since it may be
2515 nonzero from an earlier partial argument. */
2516 if (varargs_pretend_bytes > 0)
2517 all->pretend_args_size = varargs_pretend_bytes;
2520 /* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2521 the incoming location of the current parameter. */
2523 static void
2524 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2525 struct assign_parm_data_one *data)
2527 HOST_WIDE_INT pretend_bytes = 0;
2528 rtx entry_parm;
2529 bool in_regs;
2531 if (data->arg.mode == VOIDmode)
2533 data->entry_parm = data->stack_parm = const0_rtx;
2534 return;
2537 targetm.calls.warn_parameter_passing_abi (all->args_so_far,
2538 data->arg.type);
2540 entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2541 data->arg);
2542 if (entry_parm == 0)
2543 data->arg.mode = data->passed_mode;
2545 /* Determine parm's home in the stack, in case it arrives in the stack
2546 or we should pretend it did. Compute the stack position and rtx where
2547 the argument arrives and its size.
2549 There is one complexity here: If this was a parameter that would
2550 have been passed in registers, but wasn't only because it is
2551 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2552 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2553 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2554 as it was the previous time. */
2555 in_regs = (entry_parm != 0);
2556 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2557 in_regs = true;
2558 #endif
2559 if (!in_regs && !data->arg.named)
2561 if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2563 rtx tem;
2564 function_arg_info named_arg = data->arg;
2565 named_arg.named = true;
2566 tem = targetm.calls.function_incoming_arg (all->args_so_far,
2567 named_arg);
2568 in_regs = tem != NULL;
2572 /* If this parameter was passed both in registers and in the stack, use
2573 the copy on the stack. */
2574 if (targetm.calls.must_pass_in_stack (data->arg))
2575 entry_parm = 0;
2577 if (entry_parm)
2579 int partial;
2581 partial = targetm.calls.arg_partial_bytes (all->args_so_far, data->arg);
2582 data->partial = partial;
2584 /* The caller might already have allocated stack space for the
2585 register parameters. */
2586 if (partial != 0 && all->reg_parm_stack_space == 0)
2588 /* Part of this argument is passed in registers and part
2589 is passed on the stack. Ask the prologue code to extend
2590 the stack part so that we can recreate the full value.
2592 PRETEND_BYTES is the size of the registers we need to store.
2593 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2594 stack space that the prologue should allocate.
2596 Internally, gcc assumes that the argument pointer is aligned
2597 to STACK_BOUNDARY bits. This is used both for alignment
2598 optimizations (see init_emit) and to locate arguments that are
2599 aligned to more than PARM_BOUNDARY bits. We must preserve this
2600 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2601 a stack boundary. */
2603 /* We assume at most one partial arg, and it must be the first
2604 argument on the stack. */
2605 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2607 pretend_bytes = partial;
2608 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2610 /* We want to align relative to the actual stack pointer, so
2611 don't include this in the stack size until later. */
2612 all->extra_pretend_bytes = all->pretend_args_size;
2616 locate_and_pad_parm (data->arg.mode, data->arg.type, in_regs,
2617 all->reg_parm_stack_space,
2618 entry_parm ? data->partial : 0, current_function_decl,
2619 &all->stack_args_size, &data->locate);
2621 /* Update parm_stack_boundary if this parameter is passed in the
2622 stack. */
2623 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2624 crtl->parm_stack_boundary = data->locate.boundary;
2626 /* Adjust offsets to include the pretend args. */
2627 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2628 data->locate.slot_offset.constant += pretend_bytes;
2629 data->locate.offset.constant += pretend_bytes;
2631 data->entry_parm = entry_parm;
2634 /* A subroutine of assign_parms. If there is actually space on the stack
2635 for this parm, count it in stack_args_size and return true. */
2637 static bool
2638 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2639 struct assign_parm_data_one *data)
2641 /* Trivially true if we've no incoming register. */
2642 if (data->entry_parm == NULL)
2644 /* Also true if we're partially in registers and partially not,
2645 since we've arranged to drop the entire argument on the stack. */
2646 else if (data->partial != 0)
2648 /* Also true if the target says that it's passed in both registers
2649 and on the stack. */
2650 else if (GET_CODE (data->entry_parm) == PARALLEL
2651 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2653 /* Also true if the target says that there's stack allocated for
2654 all register parameters. */
2655 else if (all->reg_parm_stack_space > 0)
2657 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2658 else
2659 return false;
2661 all->stack_args_size.constant += data->locate.size.constant;
2662 if (data->locate.size.var)
2663 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2665 return true;
2668 /* A subroutine of assign_parms. Given that this parameter is allocated
2669 stack space by the ABI, find it. */
2671 static void
2672 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2674 rtx offset_rtx, stack_parm;
2675 unsigned int align, boundary;
2677 /* If we're passing this arg using a reg, make its stack home the
2678 aligned stack slot. */
2679 if (data->entry_parm)
2680 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2681 else
2682 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2684 stack_parm = crtl->args.internal_arg_pointer;
2685 if (offset_rtx != const0_rtx)
2686 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2687 stack_parm = gen_rtx_MEM (data->arg.mode, stack_parm);
2689 if (!data->arg.pass_by_reference)
2691 set_mem_attributes (stack_parm, parm, 1);
2692 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2693 while promoted mode's size is needed. */
2694 if (data->arg.mode != BLKmode
2695 && data->arg.mode != DECL_MODE (parm))
2697 set_mem_size (stack_parm, GET_MODE_SIZE (data->arg.mode));
2698 if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2700 poly_int64 offset = subreg_lowpart_offset (DECL_MODE (parm),
2701 data->arg.mode);
2702 if (maybe_ne (offset, 0))
2703 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2708 boundary = data->locate.boundary;
2709 align = BITS_PER_UNIT;
2711 /* If we're padding upward, we know that the alignment of the slot
2712 is TARGET_FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2713 intentionally forcing upward padding. Otherwise we have to come
2714 up with a guess at the alignment based on OFFSET_RTX. */
2715 poly_int64 offset;
2716 if (data->locate.where_pad == PAD_NONE || data->entry_parm)
2717 align = boundary;
2718 else if (data->locate.where_pad == PAD_UPWARD)
2720 align = boundary;
2721 /* If the argument offset is actually more aligned than the nominal
2722 stack slot boundary, take advantage of that excess alignment.
2723 Don't make any assumptions if STACK_POINTER_OFFSET is in use. */
2724 if (poly_int_rtx_p (offset_rtx, &offset)
2725 && known_eq (STACK_POINTER_OFFSET, 0))
2727 unsigned int offset_align = known_alignment (offset) * BITS_PER_UNIT;
2728 if (offset_align == 0 || offset_align > STACK_BOUNDARY)
2729 offset_align = STACK_BOUNDARY;
2730 align = MAX (align, offset_align);
2733 else if (poly_int_rtx_p (offset_rtx, &offset))
2735 align = least_bit_hwi (boundary);
2736 unsigned int offset_align = known_alignment (offset) * BITS_PER_UNIT;
2737 if (offset_align != 0)
2738 align = MIN (align, offset_align);
2740 set_mem_align (stack_parm, align);
2742 if (data->entry_parm)
2743 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2745 data->stack_parm = stack_parm;
2748 /* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2749 always valid and contiguous. */
2751 static void
2752 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2754 rtx entry_parm = data->entry_parm;
2755 rtx stack_parm = data->stack_parm;
2757 /* If this parm was passed part in regs and part in memory, pretend it
2758 arrived entirely in memory by pushing the register-part onto the stack.
2759 In the special case of a DImode or DFmode that is split, we could put
2760 it together in a pseudoreg directly, but for now that's not worth
2761 bothering with. */
2762 if (data->partial != 0)
2764 /* Handle calls that pass values in multiple non-contiguous
2765 locations. The Irix 6 ABI has examples of this. */
2766 if (GET_CODE (entry_parm) == PARALLEL)
2767 emit_group_store (validize_mem (copy_rtx (stack_parm)), entry_parm,
2768 data->arg.type, int_size_in_bytes (data->arg.type));
2769 else
2771 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2772 move_block_from_reg (REGNO (entry_parm),
2773 validize_mem (copy_rtx (stack_parm)),
2774 data->partial / UNITS_PER_WORD);
2777 entry_parm = stack_parm;
2780 /* If we didn't decide this parm came in a register, by default it came
2781 on the stack. */
2782 else if (entry_parm == NULL)
2783 entry_parm = stack_parm;
2785 /* When an argument is passed in multiple locations, we can't make use
2786 of this information, but we can save some copying if the whole argument
2787 is passed in a single register. */
2788 else if (GET_CODE (entry_parm) == PARALLEL
2789 && data->nominal_mode != BLKmode
2790 && data->passed_mode != BLKmode)
2792 size_t i, len = XVECLEN (entry_parm, 0);
2794 for (i = 0; i < len; i++)
2795 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2796 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2797 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2798 == data->passed_mode)
2799 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2801 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2802 break;
2806 data->entry_parm = entry_parm;
2809 /* A subroutine of assign_parms. Reconstitute any values which were
2810 passed in multiple registers and would fit in a single register. */
2812 static void
2813 assign_parm_remove_parallels (struct assign_parm_data_one *data)
2815 rtx entry_parm = data->entry_parm;
2817 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2818 This can be done with register operations rather than on the
2819 stack, even if we will store the reconstituted parameter on the
2820 stack later. */
2821 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2823 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2824 emit_group_store (parmreg, entry_parm, data->arg.type,
2825 GET_MODE_SIZE (GET_MODE (entry_parm)));
2826 entry_parm = parmreg;
2829 data->entry_parm = entry_parm;
2832 /* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2833 always valid and properly aligned. */
2835 static void
2836 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2838 rtx stack_parm = data->stack_parm;
2840 /* If we can't trust the parm stack slot to be aligned enough for its
2841 ultimate type, don't use that slot after entry. We'll make another
2842 stack slot, if we need one. */
2843 if (stack_parm
2844 && ((GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm)
2845 && ((optab_handler (movmisalign_optab, data->nominal_mode)
2846 != CODE_FOR_nothing)
2847 || targetm.slow_unaligned_access (data->nominal_mode,
2848 MEM_ALIGN (stack_parm))))
2849 || (data->nominal_type
2850 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2851 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2852 stack_parm = NULL;
2854 /* If parm was passed in memory, and we need to convert it on entry,
2855 don't store it back in that same slot. */
2856 else if (data->entry_parm == stack_parm
2857 && data->nominal_mode != BLKmode
2858 && data->nominal_mode != data->passed_mode)
2859 stack_parm = NULL;
2861 /* If stack protection is in effect for this function, don't leave any
2862 pointers in their passed stack slots. */
2863 else if (crtl->stack_protect_guard
2864 && (flag_stack_protect == SPCT_FLAG_ALL
2865 || data->arg.pass_by_reference
2866 || POINTER_TYPE_P (data->nominal_type)))
2867 stack_parm = NULL;
2869 data->stack_parm = stack_parm;
2872 /* A subroutine of assign_parms. Return true if the current parameter
2873 should be stored as a BLKmode in the current frame. */
2875 static bool
2876 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2878 if (data->nominal_mode == BLKmode)
2879 return true;
2880 if (GET_MODE (data->entry_parm) == BLKmode)
2881 return true;
2883 #ifdef BLOCK_REG_PADDING
2884 /* Only assign_parm_setup_block knows how to deal with register arguments
2885 that are padded at the least significant end. */
2886 if (REG_P (data->entry_parm)
2887 && known_lt (GET_MODE_SIZE (data->arg.mode), UNITS_PER_WORD)
2888 && (BLOCK_REG_PADDING (data->passed_mode, data->arg.type, 1)
2889 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
2890 return true;
2891 #endif
2893 return false;
2896 /* A subroutine of assign_parms. Arrange for the parameter to be
2897 present and valid in DATA->STACK_RTL. */
2899 static void
2900 assign_parm_setup_block (struct assign_parm_data_all *all,
2901 tree parm, struct assign_parm_data_one *data)
2903 rtx entry_parm = data->entry_parm;
2904 rtx stack_parm = data->stack_parm;
2905 rtx target_reg = NULL_RTX;
2906 bool in_conversion_seq = false;
2907 HOST_WIDE_INT size;
2908 HOST_WIDE_INT size_stored;
2910 if (GET_CODE (entry_parm) == PARALLEL)
2911 entry_parm = emit_group_move_into_temps (entry_parm);
2913 /* If we want the parameter in a pseudo, don't use a stack slot. */
2914 if (is_gimple_reg (parm) && use_register_for_decl (parm))
2916 tree def = ssa_default_def (cfun, parm);
2917 gcc_assert (def);
2918 machine_mode mode = promote_ssa_mode (def, NULL);
2919 rtx reg = gen_reg_rtx (mode);
2920 if (GET_CODE (reg) != CONCAT)
2921 stack_parm = reg;
2922 else
2924 target_reg = reg;
2925 /* Avoid allocating a stack slot, if there isn't one
2926 preallocated by the ABI. It might seem like we should
2927 always prefer a pseudo, but converting between
2928 floating-point and integer modes goes through the stack
2929 on various machines, so it's better to use the reserved
2930 stack slot than to risk wasting it and allocating more
2931 for the conversion. */
2932 if (stack_parm == NULL_RTX)
2934 int save = generating_concat_p;
2935 generating_concat_p = 0;
2936 stack_parm = gen_reg_rtx (mode);
2937 generating_concat_p = save;
2940 data->stack_parm = NULL;
2943 size = int_size_in_bytes (data->arg.type);
2944 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2945 if (stack_parm == 0)
2947 HOST_WIDE_INT parm_align
2948 = (STRICT_ALIGNMENT
2949 ? MAX (DECL_ALIGN (parm), BITS_PER_WORD) : DECL_ALIGN (parm));
2951 SET_DECL_ALIGN (parm, parm_align);
2952 if (DECL_ALIGN (parm) > MAX_SUPPORTED_STACK_ALIGNMENT)
2954 rtx allocsize = gen_int_mode (size_stored, Pmode);
2955 get_dynamic_stack_size (&allocsize, 0, DECL_ALIGN (parm), NULL);
2956 stack_parm = assign_stack_local (BLKmode, UINTVAL (allocsize),
2957 MAX_SUPPORTED_STACK_ALIGNMENT);
2958 rtx addr = align_dynamic_address (XEXP (stack_parm, 0),
2959 DECL_ALIGN (parm));
2960 mark_reg_pointer (addr, DECL_ALIGN (parm));
2961 stack_parm = gen_rtx_MEM (GET_MODE (stack_parm), addr);
2962 MEM_NOTRAP_P (stack_parm) = 1;
2964 else
2965 stack_parm = assign_stack_local (BLKmode, size_stored,
2966 DECL_ALIGN (parm));
2967 if (known_eq (GET_MODE_SIZE (GET_MODE (entry_parm)), size))
2968 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2969 set_mem_attributes (stack_parm, parm, 1);
2972 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2973 calls that pass values in multiple non-contiguous locations. */
2974 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2976 rtx mem;
2978 /* Note that we will be storing an integral number of words.
2979 So we have to be careful to ensure that we allocate an
2980 integral number of words. We do this above when we call
2981 assign_stack_local if space was not allocated in the argument
2982 list. If it was, this will not work if PARM_BOUNDARY is not
2983 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2984 if it becomes a problem. Exception is when BLKmode arrives
2985 with arguments not conforming to word_mode. */
2987 if (data->stack_parm == 0)
2989 else if (GET_CODE (entry_parm) == PARALLEL)
2991 else
2992 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2994 mem = validize_mem (copy_rtx (stack_parm));
2996 /* Handle values in multiple non-contiguous locations. */
2997 if (GET_CODE (entry_parm) == PARALLEL && !MEM_P (mem))
2998 emit_group_store (mem, entry_parm, data->arg.type, size);
2999 else if (GET_CODE (entry_parm) == PARALLEL)
3001 push_to_sequence2 (all->first_conversion_insn,
3002 all->last_conversion_insn);
3003 emit_group_store (mem, entry_parm, data->arg.type, size);
3004 all->first_conversion_insn = get_insns ();
3005 all->last_conversion_insn = get_last_insn ();
3006 end_sequence ();
3007 in_conversion_seq = true;
3010 else if (size == 0)
3013 /* If SIZE is that of a mode no bigger than a word, just use
3014 that mode's store operation. */
3015 else if (size <= UNITS_PER_WORD)
3017 unsigned int bits = size * BITS_PER_UNIT;
3018 machine_mode mode = int_mode_for_size (bits, 0).else_blk ();
3020 if (mode != BLKmode
3021 #ifdef BLOCK_REG_PADDING
3022 && (size == UNITS_PER_WORD
3023 || (BLOCK_REG_PADDING (mode, data->arg.type, 1)
3024 != (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
3025 #endif
3028 rtx reg;
3030 /* We are really truncating a word_mode value containing
3031 SIZE bytes into a value of mode MODE. If such an
3032 operation requires no actual instructions, we can refer
3033 to the value directly in mode MODE, otherwise we must
3034 start with the register in word_mode and explicitly
3035 convert it. */
3036 if (mode == word_mode
3037 || TRULY_NOOP_TRUNCATION_MODES_P (mode, word_mode))
3038 reg = gen_rtx_REG (mode, REGNO (entry_parm));
3039 else
3041 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
3042 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
3045 /* We use adjust_address to get a new MEM with the mode
3046 changed. adjust_address is better than change_address
3047 for this purpose because adjust_address does not lose
3048 the MEM_EXPR associated with the MEM.
3050 If the MEM_EXPR is lost, then optimizations like DSE
3051 assume the MEM escapes and thus is not subject to DSE. */
3052 emit_move_insn (adjust_address (mem, mode, 0), reg);
3055 #ifdef BLOCK_REG_PADDING
3056 /* Storing the register in memory as a full word, as
3057 move_block_from_reg below would do, and then using the
3058 MEM in a smaller mode, has the effect of shifting right
3059 if BYTES_BIG_ENDIAN. If we're bypassing memory, the
3060 shifting must be explicit. */
3061 else if (!MEM_P (mem))
3063 rtx x;
3065 /* If the assert below fails, we should have taken the
3066 mode != BLKmode path above, unless we have downward
3067 padding of smaller-than-word arguments on a machine
3068 with little-endian bytes, which would likely require
3069 additional changes to work correctly. */
3070 gcc_checking_assert (BYTES_BIG_ENDIAN
3071 && (BLOCK_REG_PADDING (mode,
3072 data->arg.type, 1)
3073 == PAD_UPWARD));
3075 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
3077 x = gen_rtx_REG (word_mode, REGNO (entry_parm));
3078 x = expand_shift (RSHIFT_EXPR, word_mode, x, by,
3079 NULL_RTX, 1);
3080 x = force_reg (word_mode, x);
3081 x = gen_lowpart_SUBREG (GET_MODE (mem), x);
3083 emit_move_insn (mem, x);
3085 #endif
3087 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
3088 machine must be aligned to the left before storing
3089 to memory. Note that the previous test doesn't
3090 handle all cases (e.g. SIZE == 3). */
3091 else if (size != UNITS_PER_WORD
3092 #ifdef BLOCK_REG_PADDING
3093 && (BLOCK_REG_PADDING (mode, data->arg.type, 1)
3094 == PAD_DOWNWARD)
3095 #else
3096 && BYTES_BIG_ENDIAN
3097 #endif
3100 rtx tem, x;
3101 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
3102 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
3104 x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
3105 tem = change_address (mem, word_mode, 0);
3106 emit_move_insn (tem, x);
3108 else
3109 move_block_from_reg (REGNO (entry_parm), mem,
3110 size_stored / UNITS_PER_WORD);
3112 else if (!MEM_P (mem))
3114 gcc_checking_assert (size > UNITS_PER_WORD);
3115 #ifdef BLOCK_REG_PADDING
3116 gcc_checking_assert (BLOCK_REG_PADDING (GET_MODE (mem),
3117 data->arg.type, 0)
3118 == PAD_UPWARD);
3119 #endif
3120 emit_move_insn (mem, entry_parm);
3122 else
3123 move_block_from_reg (REGNO (entry_parm), mem,
3124 size_stored / UNITS_PER_WORD);
3126 else if (data->stack_parm == 0 && !TYPE_EMPTY_P (data->arg.type))
3128 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3129 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
3130 BLOCK_OP_NORMAL);
3131 all->first_conversion_insn = get_insns ();
3132 all->last_conversion_insn = get_last_insn ();
3133 end_sequence ();
3134 in_conversion_seq = true;
3137 if (target_reg)
3139 if (!in_conversion_seq)
3140 emit_move_insn (target_reg, stack_parm);
3141 else
3143 push_to_sequence2 (all->first_conversion_insn,
3144 all->last_conversion_insn);
3145 emit_move_insn (target_reg, stack_parm);
3146 all->first_conversion_insn = get_insns ();
3147 all->last_conversion_insn = get_last_insn ();
3148 end_sequence ();
3150 stack_parm = target_reg;
3153 data->stack_parm = stack_parm;
3154 set_parm_rtl (parm, stack_parm);
3157 /* A subroutine of assign_parms. Allocate a pseudo to hold the current
3158 parameter. Get it there. Perform all ABI specified conversions. */
3160 static void
3161 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
3162 struct assign_parm_data_one *data)
3164 rtx parmreg, validated_mem;
3165 rtx equiv_stack_parm;
3166 machine_mode promoted_nominal_mode;
3167 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
3168 bool did_conversion = false;
3169 bool need_conversion, moved;
3170 enum insn_code icode;
3171 rtx rtl;
3173 /* Store the parm in a pseudoregister during the function, but we may
3174 need to do it in a wider mode. Using 2 here makes the result
3175 consistent with promote_decl_mode and thus expand_expr_real_1. */
3176 promoted_nominal_mode
3177 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
3178 TREE_TYPE (current_function_decl), 2);
3180 parmreg = gen_reg_rtx (promoted_nominal_mode);
3181 if (!DECL_ARTIFICIAL (parm))
3182 mark_user_reg (parmreg);
3184 /* If this was an item that we received a pointer to,
3185 set rtl appropriately. */
3186 if (data->arg.pass_by_reference)
3188 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->arg.type)), parmreg);
3189 set_mem_attributes (rtl, parm, 1);
3191 else
3192 rtl = parmreg;
3194 assign_parm_remove_parallels (data);
3196 /* Copy the value into the register, thus bridging between
3197 assign_parm_find_data_types and expand_expr_real_1. */
3199 equiv_stack_parm = data->stack_parm;
3200 validated_mem = validize_mem (copy_rtx (data->entry_parm));
3202 need_conversion = (data->nominal_mode != data->passed_mode
3203 || promoted_nominal_mode != data->arg.mode);
3204 moved = false;
3206 if (need_conversion
3207 && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
3208 && data->nominal_mode == data->passed_mode
3209 && data->nominal_mode == GET_MODE (data->entry_parm))
3211 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
3212 mode, by the caller. We now have to convert it to
3213 NOMINAL_MODE, if different. However, PARMREG may be in
3214 a different mode than NOMINAL_MODE if it is being stored
3215 promoted.
3217 If ENTRY_PARM is a hard register, it might be in a register
3218 not valid for operating in its mode (e.g., an odd-numbered
3219 register for a DFmode). In that case, moves are the only
3220 thing valid, so we can't do a convert from there. This
3221 occurs when the calling sequence allow such misaligned
3222 usages.
3224 In addition, the conversion may involve a call, which could
3225 clobber parameters which haven't been copied to pseudo
3226 registers yet.
3228 First, we try to emit an insn which performs the necessary
3229 conversion. We verify that this insn does not clobber any
3230 hard registers. */
3232 rtx op0, op1;
3234 icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
3235 unsignedp);
3237 op0 = parmreg;
3238 op1 = validated_mem;
3239 if (icode != CODE_FOR_nothing
3240 && insn_operand_matches (icode, 0, op0)
3241 && insn_operand_matches (icode, 1, op1))
3243 enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
3244 rtx_insn *insn, *insns;
3245 rtx t = op1;
3246 HARD_REG_SET hardregs;
3248 start_sequence ();
3249 /* If op1 is a hard register that is likely spilled, first
3250 force it into a pseudo, otherwise combiner might extend
3251 its lifetime too much. */
3252 if (GET_CODE (t) == SUBREG)
3253 t = SUBREG_REG (t);
3254 if (REG_P (t)
3255 && HARD_REGISTER_P (t)
3256 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (t))
3257 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (t))))
3259 t = gen_reg_rtx (GET_MODE (op1));
3260 emit_move_insn (t, op1);
3262 else
3263 t = op1;
3264 rtx_insn *pat = gen_extend_insn (op0, t, promoted_nominal_mode,
3265 data->passed_mode, unsignedp);
3266 emit_insn (pat);
3267 insns = get_insns ();
3269 moved = true;
3270 CLEAR_HARD_REG_SET (hardregs);
3271 for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3273 if (INSN_P (insn))
3274 note_stores (insn, record_hard_reg_sets, &hardregs);
3275 if (!hard_reg_set_empty_p (hardregs))
3276 moved = false;
3279 end_sequence ();
3281 if (moved)
3283 emit_insn (insns);
3284 if (equiv_stack_parm != NULL_RTX)
3285 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3286 equiv_stack_parm);
3291 if (moved)
3292 /* Nothing to do. */
3294 else if (need_conversion)
3296 /* We did not have an insn to convert directly, or the sequence
3297 generated appeared unsafe. We must first copy the parm to a
3298 pseudo reg, and save the conversion until after all
3299 parameters have been moved. */
3301 int save_tree_used;
3302 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3304 emit_move_insn (tempreg, validated_mem);
3306 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3307 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3309 if (partial_subreg_p (tempreg)
3310 && GET_MODE (tempreg) == data->nominal_mode
3311 && REG_P (SUBREG_REG (tempreg))
3312 && data->nominal_mode == data->passed_mode
3313 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm))
3315 /* The argument is already sign/zero extended, so note it
3316 into the subreg. */
3317 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3318 SUBREG_PROMOTED_SET (tempreg, unsignedp);
3321 /* TREE_USED gets set erroneously during expand_assignment. */
3322 save_tree_used = TREE_USED (parm);
3323 SET_DECL_RTL (parm, rtl);
3324 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3325 SET_DECL_RTL (parm, NULL_RTX);
3326 TREE_USED (parm) = save_tree_used;
3327 all->first_conversion_insn = get_insns ();
3328 all->last_conversion_insn = get_last_insn ();
3329 end_sequence ();
3331 did_conversion = true;
3333 else if (MEM_P (data->entry_parm)
3334 && GET_MODE_ALIGNMENT (promoted_nominal_mode)
3335 > MEM_ALIGN (data->entry_parm)
3336 && (((icode = optab_handler (movmisalign_optab,
3337 promoted_nominal_mode))
3338 != CODE_FOR_nothing)
3339 || targetm.slow_unaligned_access (promoted_nominal_mode,
3340 MEM_ALIGN (data->entry_parm))))
3342 if (icode != CODE_FOR_nothing)
3343 emit_insn (GEN_FCN (icode) (parmreg, validated_mem));
3344 else
3345 rtl = parmreg = extract_bit_field (validated_mem,
3346 GET_MODE_BITSIZE (promoted_nominal_mode), 0,
3347 unsignedp, parmreg,
3348 promoted_nominal_mode, VOIDmode, false, NULL);
3350 else
3351 emit_move_insn (parmreg, validated_mem);
3353 /* If we were passed a pointer but the actual value can live in a register,
3354 retrieve it and use it directly. Note that we cannot use nominal_mode,
3355 because it will have been set to Pmode above, we must use the actual mode
3356 of the parameter instead. */
3357 if (data->arg.pass_by_reference && TYPE_MODE (TREE_TYPE (parm)) != BLKmode)
3359 /* Use a stack slot for debugging purposes if possible. */
3360 if (use_register_for_decl (parm))
3362 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3363 mark_user_reg (parmreg);
3365 else
3367 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3368 TYPE_MODE (TREE_TYPE (parm)),
3369 TYPE_ALIGN (TREE_TYPE (parm)));
3370 parmreg
3371 = assign_stack_local (TYPE_MODE (TREE_TYPE (parm)),
3372 GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parm))),
3373 align);
3374 set_mem_attributes (parmreg, parm, 1);
3377 /* We need to preserve an address based on VIRTUAL_STACK_VARS_REGNUM for
3378 the debug info in case it is not legitimate. */
3379 if (GET_MODE (parmreg) != GET_MODE (rtl))
3381 rtx tempreg = gen_reg_rtx (GET_MODE (rtl));
3382 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3384 push_to_sequence2 (all->first_conversion_insn,
3385 all->last_conversion_insn);
3386 emit_move_insn (tempreg, rtl);
3387 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3388 emit_move_insn (MEM_P (parmreg) ? copy_rtx (parmreg) : parmreg,
3389 tempreg);
3390 all->first_conversion_insn = get_insns ();
3391 all->last_conversion_insn = get_last_insn ();
3392 end_sequence ();
3394 did_conversion = true;
3396 else
3397 emit_move_insn (MEM_P (parmreg) ? copy_rtx (parmreg) : parmreg, rtl);
3399 rtl = parmreg;
3401 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3402 now the parm. */
3403 data->stack_parm = NULL;
3406 set_parm_rtl (parm, rtl);
3408 /* Mark the register as eliminable if we did no conversion and it was
3409 copied from memory at a fixed offset, and the arg pointer was not
3410 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3411 offset formed an invalid address, such memory-equivalences as we
3412 make here would screw up life analysis for it. */
3413 if (data->nominal_mode == data->passed_mode
3414 && !did_conversion
3415 && data->stack_parm != 0
3416 && MEM_P (data->stack_parm)
3417 && data->locate.offset.var == 0
3418 && reg_mentioned_p (virtual_incoming_args_rtx,
3419 XEXP (data->stack_parm, 0)))
3421 rtx_insn *linsn = get_last_insn ();
3422 rtx_insn *sinsn;
3423 rtx set;
3425 /* Mark complex types separately. */
3426 if (GET_CODE (parmreg) == CONCAT)
3428 scalar_mode submode = GET_MODE_INNER (GET_MODE (parmreg));
3429 int regnor = REGNO (XEXP (parmreg, 0));
3430 int regnoi = REGNO (XEXP (parmreg, 1));
3431 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3432 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3433 GET_MODE_SIZE (submode));
3435 /* Scan backwards for the set of the real and
3436 imaginary parts. */
3437 for (sinsn = linsn; sinsn != 0;
3438 sinsn = prev_nonnote_insn (sinsn))
3440 set = single_set (sinsn);
3441 if (set == 0)
3442 continue;
3444 if (SET_DEST (set) == regno_reg_rtx [regnoi])
3445 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3446 else if (SET_DEST (set) == regno_reg_rtx [regnor])
3447 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3450 else
3451 set_dst_reg_note (linsn, REG_EQUIV, equiv_stack_parm, parmreg);
3454 /* For pointer data type, suggest pointer register. */
3455 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3456 mark_reg_pointer (parmreg,
3457 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3460 /* A subroutine of assign_parms. Allocate stack space to hold the current
3461 parameter. Get it there. Perform all ABI specified conversions. */
3463 static void
3464 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3465 struct assign_parm_data_one *data)
3467 /* Value must be stored in the stack slot STACK_PARM during function
3468 execution. */
3469 bool to_conversion = false;
3471 assign_parm_remove_parallels (data);
3473 if (data->arg.mode != data->nominal_mode)
3475 /* Conversion is required. */
3476 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3478 emit_move_insn (tempreg, validize_mem (copy_rtx (data->entry_parm)));
3480 /* Some ABIs require scalar floating point modes to be passed
3481 in a wider scalar integer mode. We need to explicitly
3482 truncate to an integer mode of the correct precision before
3483 using a SUBREG to reinterpret as a floating point value. */
3484 if (SCALAR_FLOAT_MODE_P (data->nominal_mode)
3485 && SCALAR_INT_MODE_P (data->arg.mode)
3486 && known_lt (GET_MODE_SIZE (data->nominal_mode),
3487 GET_MODE_SIZE (data->arg.mode)))
3488 tempreg = convert_wider_int_to_float (data->nominal_mode,
3489 data->arg.mode, tempreg);
3491 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3492 to_conversion = true;
3494 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3495 TYPE_UNSIGNED (TREE_TYPE (parm)));
3497 if (data->stack_parm)
3499 poly_int64 offset
3500 = subreg_lowpart_offset (data->nominal_mode,
3501 GET_MODE (data->stack_parm));
3502 /* ??? This may need a big-endian conversion on sparc64. */
3503 data->stack_parm
3504 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3505 if (maybe_ne (offset, 0) && MEM_OFFSET_KNOWN_P (data->stack_parm))
3506 set_mem_offset (data->stack_parm,
3507 MEM_OFFSET (data->stack_parm) + offset);
3511 if (data->entry_parm != data->stack_parm)
3513 rtx src, dest;
3515 if (data->stack_parm == 0)
3517 int align = STACK_SLOT_ALIGNMENT (data->arg.type,
3518 GET_MODE (data->entry_parm),
3519 TYPE_ALIGN (data->arg.type));
3520 if (align < (int)GET_MODE_ALIGNMENT (GET_MODE (data->entry_parm))
3521 && ((optab_handler (movmisalign_optab,
3522 GET_MODE (data->entry_parm))
3523 != CODE_FOR_nothing)
3524 || targetm.slow_unaligned_access (GET_MODE (data->entry_parm),
3525 align)))
3526 align = GET_MODE_ALIGNMENT (GET_MODE (data->entry_parm));
3527 data->stack_parm
3528 = assign_stack_local (GET_MODE (data->entry_parm),
3529 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3530 align);
3531 align = MEM_ALIGN (data->stack_parm);
3532 set_mem_attributes (data->stack_parm, parm, 1);
3533 set_mem_align (data->stack_parm, align);
3536 dest = validize_mem (copy_rtx (data->stack_parm));
3537 src = validize_mem (copy_rtx (data->entry_parm));
3539 if (TYPE_EMPTY_P (data->arg.type))
3540 /* Empty types don't really need to be copied. */;
3541 else if (MEM_P (src))
3543 /* Use a block move to handle potentially misaligned entry_parm. */
3544 if (!to_conversion)
3545 push_to_sequence2 (all->first_conversion_insn,
3546 all->last_conversion_insn);
3547 to_conversion = true;
3549 emit_block_move (dest, src,
3550 GEN_INT (int_size_in_bytes (data->arg.type)),
3551 BLOCK_OP_NORMAL);
3553 else
3555 if (!REG_P (src))
3556 src = force_reg (GET_MODE (src), src);
3557 emit_move_insn (dest, src);
3561 if (to_conversion)
3563 all->first_conversion_insn = get_insns ();
3564 all->last_conversion_insn = get_last_insn ();
3565 end_sequence ();
3568 set_parm_rtl (parm, data->stack_parm);
3571 /* A subroutine of assign_parms. If the ABI splits complex arguments, then
3572 undo the frobbing that we did in assign_parms_augmented_arg_list. */
3574 static void
3575 assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3576 vec<tree> fnargs)
3578 tree parm;
3579 tree orig_fnargs = all->orig_fnargs;
3580 unsigned i = 0;
3582 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3584 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3585 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3587 rtx tmp, real, imag;
3588 scalar_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3590 real = DECL_RTL (fnargs[i]);
3591 imag = DECL_RTL (fnargs[i + 1]);
3592 if (inner != GET_MODE (real))
3594 real = gen_lowpart_SUBREG (inner, real);
3595 imag = gen_lowpart_SUBREG (inner, imag);
3598 if (TREE_ADDRESSABLE (parm))
3600 rtx rmem, imem;
3601 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3602 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3603 DECL_MODE (parm),
3604 TYPE_ALIGN (TREE_TYPE (parm)));
3606 /* split_complex_arg put the real and imag parts in
3607 pseudos. Move them to memory. */
3608 tmp = assign_stack_local (DECL_MODE (parm), size, align);
3609 set_mem_attributes (tmp, parm, 1);
3610 rmem = adjust_address_nv (tmp, inner, 0);
3611 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3612 push_to_sequence2 (all->first_conversion_insn,
3613 all->last_conversion_insn);
3614 emit_move_insn (rmem, real);
3615 emit_move_insn (imem, imag);
3616 all->first_conversion_insn = get_insns ();
3617 all->last_conversion_insn = get_last_insn ();
3618 end_sequence ();
3620 else
3621 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3622 set_parm_rtl (parm, tmp);
3624 real = DECL_INCOMING_RTL (fnargs[i]);
3625 imag = DECL_INCOMING_RTL (fnargs[i + 1]);
3626 if (inner != GET_MODE (real))
3628 real = gen_lowpart_SUBREG (inner, real);
3629 imag = gen_lowpart_SUBREG (inner, imag);
3631 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3632 set_decl_incoming_rtl (parm, tmp, false);
3633 i++;
3638 /* Assign RTL expressions to the function's parameters. This may involve
3639 copying them into registers and using those registers as the DECL_RTL. */
3641 static void
3642 assign_parms (tree fndecl)
3644 struct assign_parm_data_all all;
3645 tree parm;
3646 vec<tree> fnargs;
3647 unsigned i;
3649 crtl->args.internal_arg_pointer
3650 = targetm.calls.internal_arg_pointer ();
3652 assign_parms_initialize_all (&all);
3653 fnargs = assign_parms_augmented_arg_list (&all);
3655 if (TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (fndecl))
3656 && fnargs.is_empty ())
3658 struct assign_parm_data_one data = {};
3659 assign_parms_setup_varargs (&all, &data, false);
3662 FOR_EACH_VEC_ELT (fnargs, i, parm)
3664 struct assign_parm_data_one data;
3666 /* Extract the type of PARM; adjust it according to ABI. */
3667 assign_parm_find_data_types (&all, parm, &data);
3669 /* Early out for errors and void parameters. */
3670 if (data.passed_mode == VOIDmode)
3672 SET_DECL_RTL (parm, const0_rtx);
3673 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3674 continue;
3677 /* Estimate stack alignment from parameter alignment. */
3678 if (SUPPORTS_STACK_ALIGNMENT)
3680 unsigned int align
3681 = targetm.calls.function_arg_boundary (data.arg.mode,
3682 data.arg.type);
3683 align = MINIMUM_ALIGNMENT (data.arg.type, data.arg.mode, align);
3684 if (TYPE_ALIGN (data.nominal_type) > align)
3685 align = MINIMUM_ALIGNMENT (data.nominal_type,
3686 TYPE_MODE (data.nominal_type),
3687 TYPE_ALIGN (data.nominal_type));
3688 if (crtl->stack_alignment_estimated < align)
3690 gcc_assert (!crtl->stack_realign_processed);
3691 crtl->stack_alignment_estimated = align;
3695 /* Find out where the parameter arrives in this function. */
3696 assign_parm_find_entry_rtl (&all, &data);
3698 /* Find out where stack space for this parameter might be. */
3699 if (assign_parm_is_stack_parm (&all, &data))
3701 assign_parm_find_stack_rtl (parm, &data);
3702 assign_parm_adjust_entry_rtl (&data);
3703 /* For arguments that occupy no space in the parameter
3704 passing area, have non-zero size and have address taken,
3705 force creation of a stack slot so that they have distinct
3706 address from other parameters. */
3707 if (TYPE_EMPTY_P (data.arg.type)
3708 && TREE_ADDRESSABLE (parm)
3709 && data.entry_parm == data.stack_parm
3710 && MEM_P (data.entry_parm)
3711 && int_size_in_bytes (data.arg.type))
3712 data.stack_parm = NULL_RTX;
3714 /* Record permanently how this parm was passed. */
3715 if (data.arg.pass_by_reference)
3717 rtx incoming_rtl
3718 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.arg.type)),
3719 data.entry_parm);
3720 set_decl_incoming_rtl (parm, incoming_rtl, true);
3722 else
3723 set_decl_incoming_rtl (parm, data.entry_parm, false);
3725 assign_parm_adjust_stack_rtl (&data);
3727 if (assign_parm_setup_block_p (&data))
3728 assign_parm_setup_block (&all, parm, &data);
3729 else if (data.arg.pass_by_reference || use_register_for_decl (parm))
3730 assign_parm_setup_reg (&all, parm, &data);
3731 else
3732 assign_parm_setup_stack (&all, parm, &data);
3734 if (cfun->stdarg && !DECL_CHAIN (parm))
3735 assign_parms_setup_varargs (&all, &data, false);
3737 /* Update info on where next arg arrives in registers. */
3738 targetm.calls.function_arg_advance (all.args_so_far, data.arg);
3741 if (targetm.calls.split_complex_arg)
3742 assign_parms_unsplit_complex (&all, fnargs);
3744 fnargs.release ();
3746 /* Output all parameter conversion instructions (possibly including calls)
3747 now that all parameters have been copied out of hard registers. */
3748 emit_insn (all.first_conversion_insn);
3750 do_pending_stack_adjust ();
3752 /* Estimate reload stack alignment from scalar return mode. */
3753 if (SUPPORTS_STACK_ALIGNMENT)
3755 if (DECL_RESULT (fndecl))
3757 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3758 machine_mode mode = TYPE_MODE (type);
3760 if (mode != BLKmode
3761 && mode != VOIDmode
3762 && !AGGREGATE_TYPE_P (type))
3764 unsigned int align = GET_MODE_ALIGNMENT (mode);
3765 if (crtl->stack_alignment_estimated < align)
3767 gcc_assert (!crtl->stack_realign_processed);
3768 crtl->stack_alignment_estimated = align;
3774 /* If we are receiving a struct value address as the first argument, set up
3775 the RTL for the function result. As this might require code to convert
3776 the transmitted address to Pmode, we do this here to ensure that possible
3777 preliminary conversions of the address have been emitted already. */
3778 if (all.function_result_decl)
3780 tree result = DECL_RESULT (current_function_decl);
3781 rtx addr = DECL_RTL (all.function_result_decl);
3782 rtx x;
3784 if (DECL_BY_REFERENCE (result))
3786 SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3787 x = addr;
3789 else
3791 SET_DECL_VALUE_EXPR (result,
3792 build1 (INDIRECT_REF, TREE_TYPE (result),
3793 all.function_result_decl));
3794 addr = convert_memory_address (Pmode, addr);
3795 x = gen_rtx_MEM (DECL_MODE (result), addr);
3796 set_mem_attributes (x, result, 1);
3799 DECL_HAS_VALUE_EXPR_P (result) = 1;
3801 set_parm_rtl (result, x);
3804 /* We have aligned all the args, so add space for the pretend args. */
3805 crtl->args.pretend_args_size = all.pretend_args_size;
3806 all.stack_args_size.constant += all.extra_pretend_bytes;
3807 crtl->args.size = all.stack_args_size.constant;
3809 /* Adjust function incoming argument size for alignment and
3810 minimum length. */
3812 crtl->args.size = upper_bound (crtl->args.size, all.reg_parm_stack_space);
3813 crtl->args.size = aligned_upper_bound (crtl->args.size,
3814 PARM_BOUNDARY / BITS_PER_UNIT);
3816 if (ARGS_GROW_DOWNWARD)
3818 crtl->args.arg_offset_rtx
3819 = (all.stack_args_size.var == 0
3820 ? gen_int_mode (-all.stack_args_size.constant, Pmode)
3821 : expand_expr (size_diffop (all.stack_args_size.var,
3822 size_int (-all.stack_args_size.constant)),
3823 NULL_RTX, VOIDmode, EXPAND_NORMAL));
3825 else
3826 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3828 /* See how many bytes, if any, of its args a function should try to pop
3829 on return. */
3831 crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3832 TREE_TYPE (fndecl),
3833 crtl->args.size);
3835 /* For stdarg.h function, save info about
3836 regs and stack space used by the named args. */
3838 crtl->args.info = all.args_so_far_v;
3840 /* Set the rtx used for the function return value. Put this in its
3841 own variable so any optimizers that need this information don't have
3842 to include tree.h. Do this here so it gets done when an inlined
3843 function gets output. */
3845 crtl->return_rtx
3846 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3847 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3849 /* If scalar return value was computed in a pseudo-reg, or was a named
3850 return value that got dumped to the stack, copy that to the hard
3851 return register. */
3852 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3854 tree decl_result = DECL_RESULT (fndecl);
3855 rtx decl_rtl = DECL_RTL (decl_result);
3857 if (REG_P (decl_rtl)
3858 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3859 : DECL_REGISTER (decl_result))
3861 rtx real_decl_rtl;
3863 /* Unless the psABI says not to. */
3864 if (TYPE_EMPTY_P (TREE_TYPE (decl_result)))
3865 real_decl_rtl = NULL_RTX;
3866 else
3868 real_decl_rtl
3869 = targetm.calls.function_value (TREE_TYPE (decl_result),
3870 fndecl, true);
3871 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3873 /* The delay slot scheduler assumes that crtl->return_rtx
3874 holds the hard register containing the return value, not a
3875 temporary pseudo. */
3876 crtl->return_rtx = real_decl_rtl;
3881 /* Gimplify the parameter list for current_function_decl. This involves
3882 evaluating SAVE_EXPRs of variable sized parameters and generating code
3883 to implement callee-copies reference parameters. Returns a sequence of
3884 statements to add to the beginning of the function. */
3886 gimple_seq
3887 gimplify_parameters (gimple_seq *cleanup)
3889 struct assign_parm_data_all all;
3890 tree parm;
3891 gimple_seq stmts = NULL;
3892 vec<tree> fnargs;
3893 unsigned i;
3895 assign_parms_initialize_all (&all);
3896 fnargs = assign_parms_augmented_arg_list (&all);
3898 FOR_EACH_VEC_ELT (fnargs, i, parm)
3900 struct assign_parm_data_one data;
3902 /* Extract the type of PARM; adjust it according to ABI. */
3903 assign_parm_find_data_types (&all, parm, &data);
3905 /* Early out for errors and void parameters. */
3906 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3907 continue;
3909 /* Update info on where next arg arrives in registers. */
3910 targetm.calls.function_arg_advance (all.args_so_far, data.arg);
3912 /* ??? Once upon a time variable_size stuffed parameter list
3913 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3914 turned out to be less than manageable in the gimple world.
3915 Now we have to hunt them down ourselves. */
3916 gimplify_type_sizes (TREE_TYPE (parm), &stmts);
3918 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3920 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3921 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3924 if (data.arg.pass_by_reference)
3926 tree type = TREE_TYPE (data.arg.type);
3927 function_arg_info orig_arg (type, data.arg.named);
3928 if (reference_callee_copied (&all.args_so_far_v, orig_arg))
3930 tree local, t;
3932 /* For constant-sized objects, this is trivial; for
3933 variable-sized objects, we have to play games. */
3934 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3935 && !(flag_stack_check == GENERIC_STACK_CHECK
3936 && compare_tree_int (DECL_SIZE_UNIT (parm),
3937 STACK_CHECK_MAX_VAR_SIZE) > 0))
3939 local = create_tmp_var (type, get_name (parm));
3940 DECL_IGNORED_P (local) = 0;
3941 /* If PARM was addressable, move that flag over
3942 to the local copy, as its address will be taken,
3943 not the PARMs. Keep the parms address taken
3944 as we'll query that flag during gimplification. */
3945 if (TREE_ADDRESSABLE (parm))
3946 TREE_ADDRESSABLE (local) = 1;
3947 if (DECL_NOT_GIMPLE_REG_P (parm))
3948 DECL_NOT_GIMPLE_REG_P (local) = 1;
3950 if (!is_gimple_reg (local)
3951 && flag_stack_reuse != SR_NONE)
3953 tree clobber = build_clobber (type);
3954 gimple *clobber_stmt;
3955 clobber_stmt = gimple_build_assign (local, clobber);
3956 gimple_seq_add_stmt (cleanup, clobber_stmt);
3959 else
3961 tree ptr_type, addr;
3963 ptr_type = build_pointer_type (type);
3964 addr = create_tmp_reg (ptr_type, get_name (parm));
3965 DECL_IGNORED_P (addr) = 0;
3966 local = build_fold_indirect_ref (addr);
3968 t = build_alloca_call_expr (DECL_SIZE_UNIT (parm),
3969 DECL_ALIGN (parm),
3970 max_int_size_in_bytes (type));
3971 /* The call has been built for a variable-sized object. */
3972 CALL_ALLOCA_FOR_VAR_P (t) = 1;
3973 t = fold_convert (ptr_type, t);
3974 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
3975 gimplify_and_add (t, &stmts);
3978 gimplify_assign (local, parm, &stmts);
3980 SET_DECL_VALUE_EXPR (parm, local);
3981 DECL_HAS_VALUE_EXPR_P (parm) = 1;
3986 fnargs.release ();
3988 return stmts;
3991 /* Compute the size and offset from the start of the stacked arguments for a
3992 parm passed in mode PASSED_MODE and with type TYPE.
3994 INITIAL_OFFSET_PTR points to the current offset into the stacked
3995 arguments.
3997 The starting offset and size for this parm are returned in
3998 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
3999 nonzero, the offset is that of stack slot, which is returned in
4000 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
4001 padding required from the initial offset ptr to the stack slot.
4003 IN_REGS is nonzero if the argument will be passed in registers. It will
4004 never be set if REG_PARM_STACK_SPACE is not defined.
4006 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
4007 for arguments which are passed in registers.
4009 FNDECL is the function in which the argument was defined.
4011 There are two types of rounding that are done. The first, controlled by
4012 TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
4013 argument list to be aligned to the specific boundary (in bits). This
4014 rounding affects the initial and starting offsets, but not the argument
4015 size.
4017 The second, controlled by TARGET_FUNCTION_ARG_PADDING and PARM_BOUNDARY,
4018 optionally rounds the size of the parm to PARM_BOUNDARY. The
4019 initial offset is not affected by this rounding, while the size always
4020 is and the starting offset may be. */
4022 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
4023 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
4024 callers pass in the total size of args so far as
4025 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
4027 void
4028 locate_and_pad_parm (machine_mode passed_mode, tree type, int in_regs,
4029 int reg_parm_stack_space, int partial,
4030 tree fndecl ATTRIBUTE_UNUSED,
4031 struct args_size *initial_offset_ptr,
4032 struct locate_and_pad_arg_data *locate)
4034 tree sizetree;
4035 pad_direction where_pad;
4036 unsigned int boundary, round_boundary;
4037 int part_size_in_regs;
4039 /* If we have found a stack parm before we reach the end of the
4040 area reserved for registers, skip that area. */
4041 if (! in_regs)
4043 if (reg_parm_stack_space > 0)
4045 if (initial_offset_ptr->var
4046 || !ordered_p (initial_offset_ptr->constant,
4047 reg_parm_stack_space))
4049 initial_offset_ptr->var
4050 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
4051 ssize_int (reg_parm_stack_space));
4052 initial_offset_ptr->constant = 0;
4054 else
4055 initial_offset_ptr->constant
4056 = ordered_max (initial_offset_ptr->constant,
4057 reg_parm_stack_space);
4061 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
4063 sizetree = (type
4064 ? arg_size_in_bytes (type)
4065 : size_int (GET_MODE_SIZE (passed_mode)));
4066 where_pad = targetm.calls.function_arg_padding (passed_mode, type);
4067 boundary = targetm.calls.function_arg_boundary (passed_mode, type);
4068 round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
4069 type);
4070 locate->where_pad = where_pad;
4072 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
4073 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4074 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4076 locate->boundary = boundary;
4078 if (SUPPORTS_STACK_ALIGNMENT)
4080 /* stack_alignment_estimated can't change after stack has been
4081 realigned. */
4082 if (crtl->stack_alignment_estimated < boundary)
4084 if (!crtl->stack_realign_processed)
4085 crtl->stack_alignment_estimated = boundary;
4086 else
4088 /* If stack is realigned and stack alignment value
4089 hasn't been finalized, it is OK not to increase
4090 stack_alignment_estimated. The bigger alignment
4091 requirement is recorded in stack_alignment_needed
4092 below. */
4093 gcc_assert (!crtl->stack_realign_finalized
4094 && crtl->stack_realign_needed);
4099 if (ARGS_GROW_DOWNWARD)
4101 locate->slot_offset.constant = -initial_offset_ptr->constant;
4102 if (initial_offset_ptr->var)
4103 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
4104 initial_offset_ptr->var);
4107 tree s2 = sizetree;
4108 if (where_pad != PAD_NONE
4109 && (!tree_fits_uhwi_p (sizetree)
4110 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4111 s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
4112 SUB_PARM_SIZE (locate->slot_offset, s2);
4115 locate->slot_offset.constant += part_size_in_regs;
4117 if (!in_regs || reg_parm_stack_space > 0)
4118 pad_to_arg_alignment (&locate->slot_offset, boundary,
4119 &locate->alignment_pad);
4121 locate->size.constant = (-initial_offset_ptr->constant
4122 - locate->slot_offset.constant);
4123 if (initial_offset_ptr->var)
4124 locate->size.var = size_binop (MINUS_EXPR,
4125 size_binop (MINUS_EXPR,
4126 ssize_int (0),
4127 initial_offset_ptr->var),
4128 locate->slot_offset.var);
4130 /* Pad_below needs the pre-rounded size to know how much to pad
4131 below. */
4132 locate->offset = locate->slot_offset;
4133 if (where_pad == PAD_DOWNWARD)
4134 pad_below (&locate->offset, passed_mode, sizetree);
4137 else
4139 if (!in_regs || reg_parm_stack_space > 0)
4140 pad_to_arg_alignment (initial_offset_ptr, boundary,
4141 &locate->alignment_pad);
4142 locate->slot_offset = *initial_offset_ptr;
4144 #ifdef PUSH_ROUNDING
4145 if (passed_mode != BLKmode)
4146 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
4147 #endif
4149 /* Pad_below needs the pre-rounded size to know how much to pad below
4150 so this must be done before rounding up. */
4151 locate->offset = locate->slot_offset;
4152 if (where_pad == PAD_DOWNWARD)
4153 pad_below (&locate->offset, passed_mode, sizetree);
4155 if (where_pad != PAD_NONE
4156 && (!tree_fits_uhwi_p (sizetree)
4157 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4158 sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
4160 ADD_PARM_SIZE (locate->size, sizetree);
4162 locate->size.constant -= part_size_in_regs;
4165 locate->offset.constant
4166 += targetm.calls.function_arg_offset (passed_mode, type);
4169 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
4170 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
4172 static void
4173 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
4174 struct args_size *alignment_pad)
4176 tree save_var = NULL_TREE;
4177 poly_int64 save_constant = 0;
4178 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4179 poly_int64 sp_offset = STACK_POINTER_OFFSET;
4181 #ifdef SPARC_STACK_BOUNDARY_HACK
4182 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
4183 the real alignment of %sp. However, when it does this, the
4184 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
4185 if (SPARC_STACK_BOUNDARY_HACK)
4186 sp_offset = 0;
4187 #endif
4189 if (boundary > PARM_BOUNDARY)
4191 save_var = offset_ptr->var;
4192 save_constant = offset_ptr->constant;
4195 alignment_pad->var = NULL_TREE;
4196 alignment_pad->constant = 0;
4198 if (boundary > BITS_PER_UNIT)
4200 int misalign;
4201 if (offset_ptr->var
4202 || !known_misalignment (offset_ptr->constant + sp_offset,
4203 boundary_in_bytes, &misalign))
4205 tree sp_offset_tree = ssize_int (sp_offset);
4206 tree offset = size_binop (PLUS_EXPR,
4207 ARGS_SIZE_TREE (*offset_ptr),
4208 sp_offset_tree);
4209 tree rounded;
4210 if (ARGS_GROW_DOWNWARD)
4211 rounded = round_down (offset, boundary / BITS_PER_UNIT);
4212 else
4213 rounded = round_up (offset, boundary / BITS_PER_UNIT);
4215 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
4216 /* ARGS_SIZE_TREE includes constant term. */
4217 offset_ptr->constant = 0;
4218 if (boundary > PARM_BOUNDARY)
4219 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
4220 save_var);
4222 else
4224 if (ARGS_GROW_DOWNWARD)
4225 offset_ptr->constant -= misalign;
4226 else
4227 offset_ptr->constant += -misalign & (boundary_in_bytes - 1);
4229 if (boundary > PARM_BOUNDARY)
4230 alignment_pad->constant = offset_ptr->constant - save_constant;
4235 static void
4236 pad_below (struct args_size *offset_ptr, machine_mode passed_mode, tree sizetree)
4238 unsigned int align = PARM_BOUNDARY / BITS_PER_UNIT;
4239 int misalign;
4240 if (passed_mode != BLKmode
4241 && known_misalignment (GET_MODE_SIZE (passed_mode), align, &misalign))
4242 offset_ptr->constant += -misalign & (align - 1);
4243 else
4245 if (TREE_CODE (sizetree) != INTEGER_CST
4246 || (TREE_INT_CST_LOW (sizetree) & (align - 1)) != 0)
4248 /* Round the size up to multiple of PARM_BOUNDARY bits. */
4249 tree s2 = round_up (sizetree, align);
4250 /* Add it in. */
4251 ADD_PARM_SIZE (*offset_ptr, s2);
4252 SUB_PARM_SIZE (*offset_ptr, sizetree);
4258 /* True if register REGNO was alive at a place where `setjmp' was
4259 called and was set more than once or is an argument. Such regs may
4260 be clobbered by `longjmp'. */
4262 static bool
4263 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
4265 /* There appear to be cases where some local vars never reach the
4266 backend but have bogus regnos. */
4267 if (regno >= max_reg_num ())
4268 return false;
4270 return ((REG_N_SETS (regno) > 1
4271 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
4272 regno))
4273 && REGNO_REG_SET_P (setjmp_crosses, regno));
4276 /* Walk the tree of blocks describing the binding levels within a
4277 function and warn about variables the might be killed by setjmp or
4278 vfork. This is done after calling flow_analysis before register
4279 allocation since that will clobber the pseudo-regs to hard
4280 regs. */
4282 static void
4283 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
4285 tree decl, sub;
4287 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
4289 if (VAR_P (decl)
4290 && DECL_RTL_SET_P (decl)
4291 && REG_P (DECL_RTL (decl))
4292 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4293 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
4294 " %<longjmp%> or %<vfork%>", decl);
4297 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
4298 setjmp_vars_warning (setjmp_crosses, sub);
4301 /* Do the appropriate part of setjmp_vars_warning
4302 but for arguments instead of local variables. */
4304 static void
4305 setjmp_args_warning (bitmap setjmp_crosses)
4307 tree decl;
4308 for (decl = DECL_ARGUMENTS (current_function_decl);
4309 decl; decl = DECL_CHAIN (decl))
4310 if (DECL_RTL (decl) != 0
4311 && REG_P (DECL_RTL (decl))
4312 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4313 warning (OPT_Wclobbered,
4314 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
4315 decl);
4318 /* Generate warning messages for variables live across setjmp. */
4320 void
4321 generate_setjmp_warnings (void)
4323 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4325 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS
4326 || bitmap_empty_p (setjmp_crosses))
4327 return;
4329 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4330 setjmp_args_warning (setjmp_crosses);
4334 /* Reverse the order of elements in the fragment chain T of blocks,
4335 and return the new head of the chain (old last element).
4336 In addition to that clear BLOCK_SAME_RANGE flags when needed
4337 and adjust BLOCK_SUPERCONTEXT from the super fragment to
4338 its super fragment origin. */
4340 static tree
4341 block_fragments_nreverse (tree t)
4343 tree prev = 0, block, next, prev_super = 0;
4344 tree super = BLOCK_SUPERCONTEXT (t);
4345 if (BLOCK_FRAGMENT_ORIGIN (super))
4346 super = BLOCK_FRAGMENT_ORIGIN (super);
4347 for (block = t; block; block = next)
4349 next = BLOCK_FRAGMENT_CHAIN (block);
4350 BLOCK_FRAGMENT_CHAIN (block) = prev;
4351 if ((prev && !BLOCK_SAME_RANGE (prev))
4352 || (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (block))
4353 != prev_super))
4354 BLOCK_SAME_RANGE (block) = 0;
4355 prev_super = BLOCK_SUPERCONTEXT (block);
4356 BLOCK_SUPERCONTEXT (block) = super;
4357 prev = block;
4359 t = BLOCK_FRAGMENT_ORIGIN (t);
4360 if (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (t))
4361 != prev_super)
4362 BLOCK_SAME_RANGE (t) = 0;
4363 BLOCK_SUPERCONTEXT (t) = super;
4364 return prev;
4367 /* Reverse the order of elements in the chain T of blocks,
4368 and return the new head of the chain (old last element).
4369 Also do the same on subblocks and reverse the order of elements
4370 in BLOCK_FRAGMENT_CHAIN as well. */
4372 static tree
4373 blocks_nreverse_all (tree t)
4375 tree prev = 0, block, next;
4376 for (block = t; block; block = next)
4378 next = BLOCK_CHAIN (block);
4379 BLOCK_CHAIN (block) = prev;
4380 if (BLOCK_FRAGMENT_CHAIN (block)
4381 && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4383 BLOCK_FRAGMENT_CHAIN (block)
4384 = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4385 if (!BLOCK_SAME_RANGE (BLOCK_FRAGMENT_CHAIN (block)))
4386 BLOCK_SAME_RANGE (block) = 0;
4388 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4389 prev = block;
4391 return prev;
4395 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4396 and create duplicate blocks. */
4397 /* ??? Need an option to either create block fragments or to create
4398 abstract origin duplicates of a source block. It really depends
4399 on what optimization has been performed. */
4401 void
4402 reorder_blocks (void)
4404 tree block = DECL_INITIAL (current_function_decl);
4406 if (block == NULL_TREE)
4407 return;
4409 auto_vec<tree, 10> block_stack;
4411 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4412 clear_block_marks (block);
4414 /* Prune the old trees away, so that they don't get in the way. */
4415 BLOCK_SUBBLOCKS (block) = NULL_TREE;
4416 BLOCK_CHAIN (block) = NULL_TREE;
4418 /* Recreate the block tree from the note nesting. */
4419 reorder_blocks_1 (get_insns (), block, &block_stack);
4420 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4423 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
4425 void
4426 clear_block_marks (tree block)
4428 while (block)
4430 TREE_ASM_WRITTEN (block) = 0;
4431 clear_block_marks (BLOCK_SUBBLOCKS (block));
4432 block = BLOCK_CHAIN (block);
4436 static void
4437 reorder_blocks_1 (rtx_insn *insns, tree current_block,
4438 vec<tree> *p_block_stack)
4440 rtx_insn *insn;
4441 tree prev_beg = NULL_TREE, prev_end = NULL_TREE;
4443 for (insn = insns; insn; insn = NEXT_INSN (insn))
4445 if (NOTE_P (insn))
4447 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4449 tree block = NOTE_BLOCK (insn);
4450 tree origin;
4452 gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4453 origin = block;
4455 if (prev_end)
4456 BLOCK_SAME_RANGE (prev_end) = 0;
4457 prev_end = NULL_TREE;
4459 /* If we have seen this block before, that means it now
4460 spans multiple address regions. Create a new fragment. */
4461 if (TREE_ASM_WRITTEN (block))
4463 tree new_block = copy_node (block);
4465 BLOCK_SAME_RANGE (new_block) = 0;
4466 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4467 BLOCK_FRAGMENT_CHAIN (new_block)
4468 = BLOCK_FRAGMENT_CHAIN (origin);
4469 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4471 NOTE_BLOCK (insn) = new_block;
4472 block = new_block;
4475 if (prev_beg == current_block && prev_beg)
4476 BLOCK_SAME_RANGE (block) = 1;
4478 prev_beg = origin;
4480 BLOCK_SUBBLOCKS (block) = 0;
4481 TREE_ASM_WRITTEN (block) = 1;
4482 /* When there's only one block for the entire function,
4483 current_block == block and we mustn't do this, it
4484 will cause infinite recursion. */
4485 if (block != current_block)
4487 tree super;
4488 if (block != origin)
4489 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block
4490 || BLOCK_FRAGMENT_ORIGIN (BLOCK_SUPERCONTEXT
4491 (origin))
4492 == current_block);
4493 if (p_block_stack->is_empty ())
4494 super = current_block;
4495 else
4497 super = p_block_stack->last ();
4498 gcc_assert (super == current_block
4499 || BLOCK_FRAGMENT_ORIGIN (super)
4500 == current_block);
4502 BLOCK_SUPERCONTEXT (block) = super;
4503 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4504 BLOCK_SUBBLOCKS (current_block) = block;
4505 current_block = origin;
4507 p_block_stack->safe_push (block);
4509 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4511 NOTE_BLOCK (insn) = p_block_stack->pop ();
4512 current_block = BLOCK_SUPERCONTEXT (current_block);
4513 if (BLOCK_FRAGMENT_ORIGIN (current_block))
4514 current_block = BLOCK_FRAGMENT_ORIGIN (current_block);
4515 prev_beg = NULL_TREE;
4516 prev_end = BLOCK_SAME_RANGE (NOTE_BLOCK (insn))
4517 ? NOTE_BLOCK (insn) : NULL_TREE;
4520 else
4522 prev_beg = NULL_TREE;
4523 if (prev_end)
4524 BLOCK_SAME_RANGE (prev_end) = 0;
4525 prev_end = NULL_TREE;
4530 /* Reverse the order of elements in the chain T of blocks,
4531 and return the new head of the chain (old last element). */
4533 tree
4534 blocks_nreverse (tree t)
4536 tree prev = 0, block, next;
4537 for (block = t; block; block = next)
4539 next = BLOCK_CHAIN (block);
4540 BLOCK_CHAIN (block) = prev;
4541 prev = block;
4543 return prev;
4546 /* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4547 by modifying the last node in chain 1 to point to chain 2. */
4549 tree
4550 block_chainon (tree op1, tree op2)
4552 tree t1;
4554 if (!op1)
4555 return op2;
4556 if (!op2)
4557 return op1;
4559 for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4560 continue;
4561 BLOCK_CHAIN (t1) = op2;
4563 #ifdef ENABLE_TREE_CHECKING
4565 tree t2;
4566 for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4567 gcc_assert (t2 != t1);
4569 #endif
4571 return op1;
4574 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
4575 non-NULL, list them all into VECTOR, in a depth-first preorder
4576 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
4577 blocks. */
4579 static int
4580 all_blocks (tree block, tree *vector)
4582 int n_blocks = 0;
4584 while (block)
4586 TREE_ASM_WRITTEN (block) = 0;
4588 /* Record this block. */
4589 if (vector)
4590 vector[n_blocks] = block;
4592 ++n_blocks;
4594 /* Record the subblocks, and their subblocks... */
4595 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4596 vector ? vector + n_blocks : 0);
4597 block = BLOCK_CHAIN (block);
4600 return n_blocks;
4603 /* Return a vector containing all the blocks rooted at BLOCK. The
4604 number of elements in the vector is stored in N_BLOCKS_P. The
4605 vector is dynamically allocated; it is the caller's responsibility
4606 to call `free' on the pointer returned. */
4608 static tree *
4609 get_block_vector (tree block, int *n_blocks_p)
4611 tree *block_vector;
4613 *n_blocks_p = all_blocks (block, NULL);
4614 block_vector = XNEWVEC (tree, *n_blocks_p);
4615 all_blocks (block, block_vector);
4617 return block_vector;
4620 static GTY(()) int next_block_index = 2;
4622 /* Set BLOCK_NUMBER for all the blocks in FN. */
4624 void
4625 number_blocks (tree fn)
4627 int i;
4628 int n_blocks;
4629 tree *block_vector;
4631 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4633 /* The top-level BLOCK isn't numbered at all. */
4634 for (i = 1; i < n_blocks; ++i)
4635 /* We number the blocks from two. */
4636 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4638 free (block_vector);
4640 return;
4643 /* If VAR is present in a subblock of BLOCK, return the subblock. */
4645 DEBUG_FUNCTION tree
4646 debug_find_var_in_block_tree (tree var, tree block)
4648 tree t;
4650 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4651 if (t == var)
4652 return block;
4654 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4656 tree ret = debug_find_var_in_block_tree (var, t);
4657 if (ret)
4658 return ret;
4661 return NULL_TREE;
4664 /* Keep track of whether we're in a dummy function context. If we are,
4665 we don't want to invoke the set_current_function hook, because we'll
4666 get into trouble if the hook calls target_reinit () recursively or
4667 when the initial initialization is not yet complete. */
4669 static bool in_dummy_function;
4671 /* Invoke the target hook when setting cfun. Update the optimization options
4672 if the function uses different options than the default. */
4674 static void
4675 invoke_set_current_function_hook (tree fndecl)
4677 if (!in_dummy_function)
4679 tree opts = ((fndecl)
4680 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4681 : optimization_default_node);
4683 if (!opts)
4684 opts = optimization_default_node;
4686 /* Change optimization options if needed. */
4687 if (optimization_current_node != opts)
4689 optimization_current_node = opts;
4690 cl_optimization_restore (&global_options, &global_options_set,
4691 TREE_OPTIMIZATION (opts));
4694 targetm.set_current_function (fndecl);
4695 this_fn_optabs = this_target_optabs;
4697 /* Initialize global alignment variables after op. */
4698 parse_alignment_opts ();
4700 if (opts != optimization_default_node)
4702 init_tree_optimization_optabs (opts);
4703 if (TREE_OPTIMIZATION_OPTABS (opts))
4704 this_fn_optabs = (struct target_optabs *)
4705 TREE_OPTIMIZATION_OPTABS (opts);
4710 /* Set cfun to NEW_CFUN and switch to the optimization and target options
4711 associated with NEW_FNDECL.
4713 FORCE says whether we should do the switch even if NEW_CFUN is the current
4714 function, e.g. because there has been a change in optimization or target
4715 options. */
4717 static void
4718 set_function_decl (function *new_cfun, tree new_fndecl, bool force)
4720 if (cfun != new_cfun || force)
4722 cfun = new_cfun;
4723 invoke_set_current_function_hook (new_fndecl);
4724 redirect_edge_var_map_empty ();
4728 /* cfun should never be set directly; use this function. */
4730 void
4731 set_cfun (struct function *new_cfun, bool force)
4733 set_function_decl (new_cfun, new_cfun ? new_cfun->decl : NULL_TREE, force);
4736 /* Initialized with NOGC, making this poisonous to the garbage collector. */
4738 static vec<function *> cfun_stack;
4740 /* Push the current cfun onto the stack, then switch to function NEW_CFUN
4741 and FUNCTION_DECL NEW_FNDECL. FORCE is as for set_function_decl. */
4743 static void
4744 push_function_decl (function *new_cfun, tree new_fndecl, bool force)
4746 gcc_assert ((!cfun && !current_function_decl)
4747 || (cfun && current_function_decl == cfun->decl));
4748 cfun_stack.safe_push (cfun);
4749 current_function_decl = new_fndecl;
4750 set_function_decl (new_cfun, new_fndecl, force);
4753 /* Push the current cfun onto the stack and switch to function declaration
4754 NEW_FNDECL, which might or might not have a function body. FORCE is as for
4755 set_function_decl. */
4757 void
4758 push_function_decl (tree new_fndecl, bool force)
4760 force |= current_function_decl != new_fndecl;
4761 push_function_decl (DECL_STRUCT_FUNCTION (new_fndecl), new_fndecl, force);
4764 /* Push the current cfun onto the stack, and set cfun to new_cfun. Also set
4765 current_function_decl accordingly. */
4767 void
4768 push_cfun (struct function *new_cfun)
4770 push_function_decl (new_cfun, new_cfun ? new_cfun->decl : NULL_TREE, false);
4773 /* A common subroutine for pop_cfun and pop_function_decl. FORCE is as
4774 for set_function_decl. */
4776 static void
4777 pop_cfun_1 (bool force)
4779 struct function *new_cfun = cfun_stack.pop ();
4780 /* When in_dummy_function, we do have a cfun but current_function_decl is
4781 NULL. We also allow pushing NULL cfun and subsequently changing
4782 current_function_decl to something else and have both restored by
4783 pop_cfun. */
4784 gcc_checking_assert (in_dummy_function
4785 || !cfun
4786 || current_function_decl == cfun->decl);
4787 set_cfun (new_cfun, force);
4788 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4791 /* Pop cfun from the stack. Also set current_function_decl accordingly. */
4793 void
4794 pop_cfun (void)
4796 pop_cfun_1 (false);
4799 /* Undo push_function_decl. */
4801 void
4802 pop_function_decl (void)
4804 /* If the previous cfun was null, the options should be reset to the
4805 global set. Checking the current cfun against the new (popped) cfun
4806 wouldn't catch this if the current function decl has no function
4807 struct. */
4808 pop_cfun_1 (!cfun_stack.last ());
4811 /* Return value of funcdef and increase it. */
4813 get_next_funcdef_no (void)
4815 return funcdef_no++;
4818 /* Return value of funcdef. */
4820 get_last_funcdef_no (void)
4822 return funcdef_no;
4825 /* Allocate and initialize the stack usage info data structure for the
4826 current function. */
4827 static void
4828 allocate_stack_usage_info (void)
4830 gcc_assert (!cfun->su);
4831 cfun->su = ggc_cleared_alloc<stack_usage> ();
4832 cfun->su->static_stack_size = -1;
4835 /* Allocate a function structure for FNDECL and set its contents
4836 to the defaults. Set cfun to the newly-allocated object.
4837 Some of the helper functions invoked during initialization assume
4838 that cfun has already been set. Therefore, assign the new object
4839 directly into cfun and invoke the back end hook explicitly at the
4840 very end, rather than initializing a temporary and calling set_cfun
4841 on it.
4843 ABSTRACT_P is true if this is a function that will never be seen by
4844 the middle-end. Such functions are front-end concepts (like C++
4845 function templates) that do not correspond directly to functions
4846 placed in object files. */
4848 void
4849 allocate_struct_function (tree fndecl, bool abstract_p)
4851 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4853 cfun = ggc_cleared_alloc<function> ();
4855 init_eh_for_function ();
4857 if (init_machine_status)
4858 cfun->machine = (*init_machine_status) ();
4860 #ifdef OVERRIDE_ABI_FORMAT
4861 OVERRIDE_ABI_FORMAT (fndecl);
4862 #endif
4864 if (fndecl != NULL_TREE)
4866 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4867 cfun->decl = fndecl;
4868 current_function_funcdef_no = get_next_funcdef_no ();
4871 invoke_set_current_function_hook (fndecl);
4873 if (fndecl != NULL_TREE)
4875 tree result = DECL_RESULT (fndecl);
4877 if (!abstract_p)
4879 /* Now that we have activated any function-specific attributes
4880 that might affect layout, particularly vector modes, relayout
4881 each of the parameters and the result. */
4882 relayout_decl (result);
4883 for (tree parm = DECL_ARGUMENTS (fndecl); parm;
4884 parm = DECL_CHAIN (parm))
4885 relayout_decl (parm);
4887 /* Similarly relayout the function decl. */
4888 targetm.target_option.relayout_function (fndecl);
4891 if (!abstract_p && aggregate_value_p (result, fndecl))
4893 #ifdef PCC_STATIC_STRUCT_RETURN
4894 cfun->returns_pcc_struct = 1;
4895 #endif
4896 cfun->returns_struct = 1;
4899 cfun->stdarg = stdarg_p (fntype);
4901 /* Assume all registers in stdarg functions need to be saved. */
4902 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4903 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4905 /* ??? This could be set on a per-function basis by the front-end
4906 but is this worth the hassle? */
4907 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4908 cfun->can_delete_dead_exceptions = flag_delete_dead_exceptions;
4910 if (!profile_flag && !flag_instrument_function_entry_exit)
4911 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl) = 1;
4913 if (flag_callgraph_info)
4914 allocate_stack_usage_info ();
4917 /* Don't enable begin stmt markers if var-tracking at assignments is
4918 disabled. The markers make little sense without the variable
4919 binding annotations among them. */
4920 cfun->debug_nonbind_markers = lang_hooks.emits_begin_stmt
4921 && MAY_HAVE_DEBUG_MARKER_STMTS;
4924 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4925 instead of just setting it. */
4927 void
4928 push_struct_function (tree fndecl, bool abstract_p)
4930 /* When in_dummy_function we might be in the middle of a pop_cfun and
4931 current_function_decl and cfun may not match. */
4932 gcc_assert (in_dummy_function
4933 || (!cfun && !current_function_decl)
4934 || (cfun && current_function_decl == cfun->decl));
4935 cfun_stack.safe_push (cfun);
4936 current_function_decl = fndecl;
4937 allocate_struct_function (fndecl, abstract_p);
4940 /* Reset crtl and other non-struct-function variables to defaults as
4941 appropriate for emitting rtl at the start of a function. */
4943 static void
4944 prepare_function_start (void)
4946 gcc_assert (!get_last_insn ());
4948 if (in_dummy_function)
4949 crtl->abi = &default_function_abi;
4950 else
4951 crtl->abi = &fndecl_abi (cfun->decl).base_abi ();
4953 init_temp_slots ();
4954 init_emit ();
4955 init_varasm_status ();
4956 init_expr ();
4957 default_rtl_profile ();
4959 if (flag_stack_usage_info && !flag_callgraph_info)
4960 allocate_stack_usage_info ();
4962 cse_not_expected = ! optimize;
4964 /* Caller save not needed yet. */
4965 caller_save_needed = 0;
4967 /* We haven't done register allocation yet. */
4968 reg_renumber = 0;
4970 /* Indicate that we have not instantiated virtual registers yet. */
4971 virtuals_instantiated = 0;
4973 /* Indicate that we want CONCATs now. */
4974 generating_concat_p = 1;
4976 /* Indicate we have no need of a frame pointer yet. */
4977 frame_pointer_needed = 0;
4980 void
4981 push_dummy_function (bool with_decl)
4983 tree fn_decl, fn_type, fn_result_decl;
4985 gcc_assert (!in_dummy_function);
4986 in_dummy_function = true;
4988 if (with_decl)
4990 fn_type = build_function_type_list (void_type_node, NULL_TREE);
4991 fn_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
4992 fn_type);
4993 fn_result_decl = build_decl (UNKNOWN_LOCATION, RESULT_DECL,
4994 NULL_TREE, void_type_node);
4995 DECL_RESULT (fn_decl) = fn_result_decl;
4996 DECL_ARTIFICIAL (fn_decl) = 1;
4997 tree fn_name = get_identifier (" ");
4998 SET_DECL_ASSEMBLER_NAME (fn_decl, fn_name);
5000 else
5001 fn_decl = NULL_TREE;
5003 push_struct_function (fn_decl);
5006 /* Initialize the rtl expansion mechanism so that we can do simple things
5007 like generate sequences. This is used to provide a context during global
5008 initialization of some passes. You must call expand_dummy_function_end
5009 to exit this context. */
5011 void
5012 init_dummy_function_start (void)
5014 push_dummy_function (false);
5015 prepare_function_start ();
5018 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
5019 and initialize static variables for generating RTL for the statements
5020 of the function. */
5022 void
5023 init_function_start (tree subr)
5025 /* Initialize backend, if needed. */
5026 initialize_rtl ();
5028 prepare_function_start ();
5029 decide_function_section (subr);
5031 /* Warn if this value is an aggregate type,
5032 regardless of which calling convention we are using for it. */
5033 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
5034 warning_at (DECL_SOURCE_LOCATION (DECL_RESULT (subr)),
5035 OPT_Waggregate_return, "function returns an aggregate");
5038 /* Expand code to verify the stack_protect_guard. This is invoked at
5039 the end of a function to be protected. */
5041 void
5042 stack_protect_epilogue (void)
5044 tree guard_decl = crtl->stack_protect_guard_decl;
5045 rtx_code_label *label = gen_label_rtx ();
5046 rtx x, y;
5047 rtx_insn *seq = NULL;
5049 x = expand_normal (crtl->stack_protect_guard);
5051 if (targetm.have_stack_protect_combined_test () && guard_decl)
5053 gcc_assert (DECL_P (guard_decl));
5054 y = DECL_RTL (guard_decl);
5055 /* Allow the target to compute address of Y and compare it with X without
5056 leaking Y into a register. This combined address + compare pattern
5057 allows the target to prevent spilling of any intermediate results by
5058 splitting it after register allocator. */
5059 seq = targetm.gen_stack_protect_combined_test (x, y, label);
5061 else
5063 if (guard_decl)
5064 y = expand_normal (guard_decl);
5065 else
5066 y = const0_rtx;
5068 /* Allow the target to compare Y with X without leaking either into
5069 a register. */
5070 if (targetm.have_stack_protect_test ())
5071 seq = targetm.gen_stack_protect_test (x, y, label);
5074 if (seq)
5075 emit_insn (seq);
5076 else
5077 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
5079 /* The noreturn predictor has been moved to the tree level. The rtl-level
5080 predictors estimate this branch about 20%, which isn't enough to get
5081 things moved out of line. Since this is the only extant case of adding
5082 a noreturn function at the rtl level, it doesn't seem worth doing ought
5083 except adding the prediction by hand. */
5084 rtx_insn *tmp = get_last_insn ();
5085 if (JUMP_P (tmp))
5086 predict_insn_def (tmp, PRED_NORETURN, TAKEN);
5088 expand_call (targetm.stack_protect_fail (), NULL_RTX, /*ignore=*/true);
5089 free_temp_slots ();
5090 emit_label (label);
5093 /* Start the RTL for a new function, and set variables used for
5094 emitting RTL.
5095 SUBR is the FUNCTION_DECL node.
5096 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
5097 the function's parameters, which must be run at any return statement. */
5099 bool currently_expanding_function_start;
5100 void
5101 expand_function_start (tree subr)
5103 currently_expanding_function_start = true;
5105 /* Make sure volatile mem refs aren't considered
5106 valid operands of arithmetic insns. */
5107 init_recog_no_volatile ();
5109 crtl->profile
5110 = (profile_flag
5111 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
5113 crtl->limit_stack
5114 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
5116 /* Make the label for return statements to jump to. Do not special
5117 case machines with special return instructions -- they will be
5118 handled later during jump, ifcvt, or epilogue creation. */
5119 return_label = gen_label_rtx ();
5121 /* Initialize rtx used to return the value. */
5122 /* Do this before assign_parms so that we copy the struct value address
5123 before any library calls that assign parms might generate. */
5125 /* Decide whether to return the value in memory or in a register. */
5126 tree res = DECL_RESULT (subr);
5127 if (aggregate_value_p (res, subr))
5129 /* Returning something that won't go in a register. */
5130 rtx value_address = 0;
5132 #ifdef PCC_STATIC_STRUCT_RETURN
5133 if (cfun->returns_pcc_struct)
5135 int size = int_size_in_bytes (TREE_TYPE (res));
5136 value_address = assemble_static_space (size);
5138 else
5139 #endif
5141 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
5142 /* Expect to be passed the address of a place to store the value.
5143 If it is passed as an argument, assign_parms will take care of
5144 it. */
5145 if (sv)
5147 value_address = gen_reg_rtx (Pmode);
5148 emit_move_insn (value_address, sv);
5151 if (value_address)
5153 rtx x = value_address;
5154 if (!DECL_BY_REFERENCE (res))
5156 x = gen_rtx_MEM (DECL_MODE (res), x);
5157 set_mem_attributes (x, res, 1);
5159 set_parm_rtl (res, x);
5162 else if (DECL_MODE (res) == VOIDmode)
5163 /* If return mode is void, this decl rtl should not be used. */
5164 set_parm_rtl (res, NULL_RTX);
5165 else
5167 /* Compute the return values into a pseudo reg, which we will copy
5168 into the true return register after the cleanups are done. */
5169 tree return_type = TREE_TYPE (res);
5171 /* If we may coalesce this result, make sure it has the expected mode
5172 in case it was promoted. But we need not bother about BLKmode. */
5173 machine_mode promoted_mode
5174 = flag_tree_coalesce_vars && is_gimple_reg (res)
5175 ? promote_ssa_mode (ssa_default_def (cfun, res), NULL)
5176 : BLKmode;
5178 if (promoted_mode != BLKmode)
5179 set_parm_rtl (res, gen_reg_rtx (promoted_mode));
5180 else if (TYPE_MODE (return_type) != BLKmode
5181 && targetm.calls.return_in_msb (return_type))
5182 /* expand_function_end will insert the appropriate padding in
5183 this case. Use the return value's natural (unpadded) mode
5184 within the function proper. */
5185 set_parm_rtl (res, gen_reg_rtx (TYPE_MODE (return_type)));
5186 else
5188 /* In order to figure out what mode to use for the pseudo, we
5189 figure out what the mode of the eventual return register will
5190 actually be, and use that. */
5191 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
5193 /* Structures that are returned in registers are not
5194 aggregate_value_p, so we may see a PARALLEL or a REG. */
5195 if (REG_P (hard_reg))
5196 set_parm_rtl (res, gen_reg_rtx (GET_MODE (hard_reg)));
5197 else
5199 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
5200 set_parm_rtl (res, gen_group_rtx (hard_reg));
5204 /* Set DECL_REGISTER flag so that expand_function_end will copy the
5205 result to the real return register(s). */
5206 DECL_REGISTER (res) = 1;
5209 /* Initialize rtx for parameters and local variables.
5210 In some cases this requires emitting insns. */
5211 assign_parms (subr);
5213 /* If function gets a static chain arg, store it. */
5214 if (cfun->static_chain_decl)
5216 tree parm = cfun->static_chain_decl;
5217 rtx local, chain;
5218 rtx_insn *insn;
5219 int unsignedp;
5221 local = gen_reg_rtx (promote_decl_mode (parm, &unsignedp));
5222 chain = targetm.calls.static_chain (current_function_decl, true);
5224 set_decl_incoming_rtl (parm, chain, false);
5225 set_parm_rtl (parm, local);
5226 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
5228 if (GET_MODE (local) != GET_MODE (chain))
5230 convert_move (local, chain, unsignedp);
5231 insn = get_last_insn ();
5233 else
5234 insn = emit_move_insn (local, chain);
5236 /* Mark the register as eliminable, similar to parameters. */
5237 if (MEM_P (chain)
5238 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
5239 set_dst_reg_note (insn, REG_EQUIV, chain, local);
5241 /* If we aren't optimizing, save the static chain onto the stack. */
5242 if (!optimize)
5244 tree saved_static_chain_decl
5245 = build_decl (DECL_SOURCE_LOCATION (parm), VAR_DECL,
5246 DECL_NAME (parm), TREE_TYPE (parm));
5247 rtx saved_static_chain_rtx
5248 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5249 SET_DECL_RTL (saved_static_chain_decl, saved_static_chain_rtx);
5250 emit_move_insn (saved_static_chain_rtx, chain);
5251 SET_DECL_VALUE_EXPR (parm, saved_static_chain_decl);
5252 DECL_HAS_VALUE_EXPR_P (parm) = 1;
5256 /* The following was moved from init_function_start.
5257 The move was supposed to make sdb output more accurate. */
5258 /* Indicate the beginning of the function body,
5259 as opposed to parm setup. */
5260 emit_note (NOTE_INSN_FUNCTION_BEG);
5262 gcc_assert (NOTE_P (get_last_insn ()));
5264 function_beg_insn = parm_birth_insn = get_last_insn ();
5266 /* If the function receives a non-local goto, then store the
5267 bits we need to restore the frame pointer. */
5268 if (cfun->nonlocal_goto_save_area)
5270 tree t_save;
5271 rtx r_save;
5273 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
5274 gcc_assert (DECL_RTL_SET_P (var));
5276 t_save = build4 (ARRAY_REF,
5277 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
5278 cfun->nonlocal_goto_save_area,
5279 integer_zero_node, NULL_TREE, NULL_TREE);
5280 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
5281 gcc_assert (GET_MODE (r_save) == Pmode);
5283 emit_move_insn (r_save, hard_frame_pointer_rtx);
5284 update_nonlocal_goto_save_area ();
5287 if (crtl->profile)
5289 #ifdef PROFILE_HOOK
5290 PROFILE_HOOK (current_function_funcdef_no);
5291 #endif
5294 /* If we are doing generic stack checking, the probe should go here. */
5295 if (flag_stack_check == GENERIC_STACK_CHECK)
5296 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
5298 currently_expanding_function_start = false;
5301 void
5302 pop_dummy_function (void)
5304 pop_cfun ();
5305 in_dummy_function = false;
5308 /* Undo the effects of init_dummy_function_start. */
5309 void
5310 expand_dummy_function_end (void)
5312 gcc_assert (in_dummy_function);
5314 /* End any sequences that failed to be closed due to syntax errors. */
5315 while (in_sequence_p ())
5316 end_sequence ();
5318 /* Outside function body, can't compute type's actual size
5319 until next function's body starts. */
5321 free_after_parsing (cfun);
5322 free_after_compilation (cfun);
5323 pop_dummy_function ();
5326 /* Helper for diddle_return_value. */
5328 void
5329 diddle_return_value_1 (void (*doit) (rtx, void *), void *arg, rtx outgoing)
5331 if (! outgoing)
5332 return;
5334 if (REG_P (outgoing))
5335 (*doit) (outgoing, arg);
5336 else if (GET_CODE (outgoing) == PARALLEL)
5338 int i;
5340 for (i = 0; i < XVECLEN (outgoing, 0); i++)
5342 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
5344 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
5345 (*doit) (x, arg);
5350 /* Call DOIT for each hard register used as a return value from
5351 the current function. */
5353 void
5354 diddle_return_value (void (*doit) (rtx, void *), void *arg)
5356 diddle_return_value_1 (doit, arg, crtl->return_rtx);
5359 static void
5360 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5362 emit_clobber (reg);
5365 void
5366 clobber_return_register (void)
5368 diddle_return_value (do_clobber_return_reg, NULL);
5370 /* In case we do use pseudo to return value, clobber it too. */
5371 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5373 tree decl_result = DECL_RESULT (current_function_decl);
5374 rtx decl_rtl = DECL_RTL (decl_result);
5375 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
5377 do_clobber_return_reg (decl_rtl, NULL);
5382 static void
5383 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5385 emit_use (reg);
5388 static void
5389 use_return_register (void)
5391 diddle_return_value (do_use_return_reg, NULL);
5394 /* Generate RTL for the end of the current function. */
5396 void
5397 expand_function_end (void)
5399 /* If arg_pointer_save_area was referenced only from a nested
5400 function, we will not have initialized it yet. Do that now. */
5401 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
5402 get_arg_pointer_save_area ();
5404 /* If we are doing generic stack checking and this function makes calls,
5405 do a stack probe at the start of the function to ensure we have enough
5406 space for another stack frame. */
5407 if (flag_stack_check == GENERIC_STACK_CHECK)
5409 rtx_insn *insn, *seq;
5411 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5412 if (CALL_P (insn))
5414 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
5415 start_sequence ();
5416 if (STACK_CHECK_MOVING_SP)
5417 anti_adjust_stack_and_probe (max_frame_size, true);
5418 else
5419 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
5420 seq = get_insns ();
5421 end_sequence ();
5422 set_insn_locations (seq, prologue_location);
5423 emit_insn_before (seq, stack_check_probe_note);
5424 break;
5428 /* End any sequences that failed to be closed due to syntax errors. */
5429 while (in_sequence_p ())
5430 end_sequence ();
5432 clear_pending_stack_adjust ();
5433 do_pending_stack_adjust ();
5435 /* Output a linenumber for the end of the function.
5436 SDB depended on this. */
5437 set_curr_insn_location (input_location);
5439 /* Before the return label (if any), clobber the return
5440 registers so that they are not propagated live to the rest of
5441 the function. This can only happen with functions that drop
5442 through; if there had been a return statement, there would
5443 have either been a return rtx, or a jump to the return label.
5445 We delay actual code generation after the current_function_value_rtx
5446 is computed. */
5447 rtx_insn *clobber_after = get_last_insn ();
5449 /* Output the label for the actual return from the function. */
5450 emit_label (return_label);
5452 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
5454 /* Let except.cc know where it should emit the call to unregister
5455 the function context for sjlj exceptions. */
5456 if (flag_exceptions)
5457 sjlj_emit_function_exit_after (get_last_insn ());
5460 /* If this is an implementation of throw, do what's necessary to
5461 communicate between __builtin_eh_return and the epilogue. */
5462 expand_eh_return ();
5464 /* If stack protection is enabled for this function, check the guard. */
5465 if (crtl->stack_protect_guard
5466 && targetm.stack_protect_runtime_enabled_p ()
5467 && naked_return_label == NULL_RTX)
5468 stack_protect_epilogue ();
5470 /* If scalar return value was computed in a pseudo-reg, or was a named
5471 return value that got dumped to the stack, copy that to the hard
5472 return register. */
5473 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5475 tree decl_result = DECL_RESULT (current_function_decl);
5476 rtx decl_rtl = DECL_RTL (decl_result);
5478 if ((REG_P (decl_rtl)
5479 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5480 : DECL_REGISTER (decl_result))
5481 /* Unless the psABI says not to. */
5482 && !TYPE_EMPTY_P (TREE_TYPE (decl_result)))
5484 rtx real_decl_rtl = crtl->return_rtx;
5485 complex_mode cmode;
5487 /* This should be set in assign_parms. */
5488 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5490 /* If this is a BLKmode structure being returned in registers,
5491 then use the mode computed in expand_return. Note that if
5492 decl_rtl is memory, then its mode may have been changed,
5493 but that crtl->return_rtx has not. */
5494 if (GET_MODE (real_decl_rtl) == BLKmode)
5495 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
5497 /* If a non-BLKmode return value should be padded at the least
5498 significant end of the register, shift it left by the appropriate
5499 amount. BLKmode results are handled using the group load/store
5500 machinery. */
5501 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5502 && REG_P (real_decl_rtl)
5503 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5505 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5506 REGNO (real_decl_rtl)),
5507 decl_rtl);
5508 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5510 else if (GET_CODE (real_decl_rtl) == PARALLEL)
5512 /* If expand_function_start has created a PARALLEL for decl_rtl,
5513 move the result to the real return registers. Otherwise, do
5514 a group load from decl_rtl for a named return. */
5515 if (GET_CODE (decl_rtl) == PARALLEL)
5516 emit_group_move (real_decl_rtl, decl_rtl);
5517 else
5518 emit_group_load (real_decl_rtl, decl_rtl,
5519 TREE_TYPE (decl_result),
5520 int_size_in_bytes (TREE_TYPE (decl_result)));
5522 /* In the case of complex integer modes smaller than a word, we'll
5523 need to generate some non-trivial bitfield insertions. Do that
5524 on a pseudo and not the hard register. */
5525 else if (GET_CODE (decl_rtl) == CONCAT
5526 && is_complex_int_mode (GET_MODE (decl_rtl), &cmode)
5527 && GET_MODE_BITSIZE (cmode) <= BITS_PER_WORD)
5529 int old_generating_concat_p;
5530 rtx tmp;
5532 old_generating_concat_p = generating_concat_p;
5533 generating_concat_p = 0;
5534 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5535 generating_concat_p = old_generating_concat_p;
5537 emit_move_insn (tmp, decl_rtl);
5538 emit_move_insn (real_decl_rtl, tmp);
5540 /* If a named return value dumped decl_return to memory, then
5541 we may need to re-do the PROMOTE_MODE signed/unsigned
5542 extension. */
5543 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5545 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5546 promote_function_mode (TREE_TYPE (decl_result),
5547 GET_MODE (decl_rtl), &unsignedp,
5548 TREE_TYPE (current_function_decl), 1);
5550 convert_move (real_decl_rtl, decl_rtl, unsignedp);
5552 else
5553 emit_move_insn (real_decl_rtl, decl_rtl);
5557 /* If returning a structure, arrange to return the address of the value
5558 in a place where debuggers expect to find it.
5560 If returning a structure PCC style,
5561 the caller also depends on this value.
5562 And cfun->returns_pcc_struct is not necessarily set. */
5563 if ((cfun->returns_struct || cfun->returns_pcc_struct)
5564 && !targetm.calls.omit_struct_return_reg)
5566 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5567 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5568 rtx outgoing;
5570 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5571 type = TREE_TYPE (type);
5572 else
5573 value_address = XEXP (value_address, 0);
5575 outgoing = targetm.calls.function_value (build_pointer_type (type),
5576 current_function_decl, true);
5578 /* Mark this as a function return value so integrate will delete the
5579 assignment and USE below when inlining this function. */
5580 REG_FUNCTION_VALUE_P (outgoing) = 1;
5582 /* The address may be ptr_mode and OUTGOING may be Pmode. */
5583 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (outgoing));
5584 value_address = convert_memory_address (mode, value_address);
5586 emit_move_insn (outgoing, value_address);
5588 /* Show return register used to hold result (in this case the address
5589 of the result. */
5590 crtl->return_rtx = outgoing;
5593 /* Emit the actual code to clobber return register. Don't emit
5594 it if clobber_after is a barrier, then the previous basic block
5595 certainly doesn't fall thru into the exit block. */
5596 if (!BARRIER_P (clobber_after))
5598 start_sequence ();
5599 clobber_return_register ();
5600 rtx_insn *seq = get_insns ();
5601 end_sequence ();
5603 emit_insn_after (seq, clobber_after);
5606 /* Output the label for the naked return from the function. */
5607 if (naked_return_label)
5608 emit_label (naked_return_label);
5610 /* @@@ This is a kludge. We want to ensure that instructions that
5611 may trap are not moved into the epilogue by scheduling, because
5612 we don't always emit unwind information for the epilogue. */
5613 if (cfun->can_throw_non_call_exceptions
5614 && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5615 emit_insn (gen_blockage ());
5617 /* If stack protection is enabled for this function, check the guard. */
5618 if (crtl->stack_protect_guard
5619 && targetm.stack_protect_runtime_enabled_p ()
5620 && naked_return_label)
5621 stack_protect_epilogue ();
5623 /* If we had calls to alloca, and this machine needs
5624 an accurate stack pointer to exit the function,
5625 insert some code to save and restore the stack pointer. */
5626 if (! EXIT_IGNORE_STACK
5627 && cfun->calls_alloca)
5629 rtx tem = 0;
5631 start_sequence ();
5632 emit_stack_save (SAVE_FUNCTION, &tem);
5633 rtx_insn *seq = get_insns ();
5634 end_sequence ();
5635 emit_insn_before (seq, parm_birth_insn);
5637 emit_stack_restore (SAVE_FUNCTION, tem);
5640 /* ??? This should no longer be necessary since stupid is no longer with
5641 us, but there are some parts of the compiler (eg reload_combine, and
5642 sh mach_dep_reorg) that still try and compute their own lifetime info
5643 instead of using the general framework. */
5644 use_return_register ();
5648 get_arg_pointer_save_area (void)
5650 rtx ret = arg_pointer_save_area;
5652 if (! ret)
5654 ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5655 arg_pointer_save_area = ret;
5658 if (! crtl->arg_pointer_save_area_init)
5660 /* Save the arg pointer at the beginning of the function. The
5661 generated stack slot may not be a valid memory address, so we
5662 have to check it and fix it if necessary. */
5663 start_sequence ();
5664 emit_move_insn (validize_mem (copy_rtx (ret)),
5665 crtl->args.internal_arg_pointer);
5666 rtx_insn *seq = get_insns ();
5667 end_sequence ();
5669 push_topmost_sequence ();
5670 emit_insn_after (seq, entry_of_function ());
5671 pop_topmost_sequence ();
5673 crtl->arg_pointer_save_area_init = true;
5676 return ret;
5680 /* If debugging dumps are requested, dump information about how the
5681 target handled -fstack-check=clash for the prologue.
5683 PROBES describes what if any probes were emitted.
5685 RESIDUALS indicates if the prologue had any residual allocation
5686 (i.e. total allocation was not a multiple of PROBE_INTERVAL). */
5688 void
5689 dump_stack_clash_frame_info (enum stack_clash_probes probes, bool residuals)
5691 if (!dump_file)
5692 return;
5694 switch (probes)
5696 case NO_PROBE_NO_FRAME:
5697 fprintf (dump_file,
5698 "Stack clash no probe no stack adjustment in prologue.\n");
5699 break;
5700 case NO_PROBE_SMALL_FRAME:
5701 fprintf (dump_file,
5702 "Stack clash no probe small stack adjustment in prologue.\n");
5703 break;
5704 case PROBE_INLINE:
5705 fprintf (dump_file, "Stack clash inline probes in prologue.\n");
5706 break;
5707 case PROBE_LOOP:
5708 fprintf (dump_file, "Stack clash probe loop in prologue.\n");
5709 break;
5712 if (residuals)
5713 fprintf (dump_file, "Stack clash residual allocation in prologue.\n");
5714 else
5715 fprintf (dump_file, "Stack clash no residual allocation in prologue.\n");
5717 if (frame_pointer_needed)
5718 fprintf (dump_file, "Stack clash frame pointer needed.\n");
5719 else
5720 fprintf (dump_file, "Stack clash no frame pointer needed.\n");
5722 if (TREE_THIS_VOLATILE (cfun->decl))
5723 fprintf (dump_file,
5724 "Stack clash noreturn prologue, assuming no implicit"
5725 " probes in caller.\n");
5726 else
5727 fprintf (dump_file,
5728 "Stack clash not noreturn prologue.\n");
5731 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5732 for the first time. */
5734 static void
5735 record_insns (rtx_insn *insns, rtx end, hash_table<insn_cache_hasher> **hashp)
5737 rtx_insn *tmp;
5738 hash_table<insn_cache_hasher> *hash = *hashp;
5740 if (hash == NULL)
5741 *hashp = hash = hash_table<insn_cache_hasher>::create_ggc (17);
5743 for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5745 rtx *slot = hash->find_slot (tmp, INSERT);
5746 gcc_assert (*slot == NULL);
5747 *slot = tmp;
5751 /* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5752 basic block, splitting or peepholes. If INSN is a prologue or epilogue
5753 insn, then record COPY as well. */
5755 void
5756 maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5758 hash_table<insn_cache_hasher> *hash;
5759 rtx *slot;
5761 hash = epilogue_insn_hash;
5762 if (!hash || !hash->find (insn))
5764 hash = prologue_insn_hash;
5765 if (!hash || !hash->find (insn))
5766 return;
5769 slot = hash->find_slot (copy, INSERT);
5770 gcc_assert (*slot == NULL);
5771 *slot = copy;
5774 /* Determine if any INSNs in HASH are, or are part of, INSN. Because
5775 we can be running after reorg, SEQUENCE rtl is possible. */
5777 static bool
5778 contains (const rtx_insn *insn, hash_table<insn_cache_hasher> *hash)
5780 if (hash == NULL)
5781 return false;
5783 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5785 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
5786 int i;
5787 for (i = seq->len () - 1; i >= 0; i--)
5788 if (hash->find (seq->element (i)))
5789 return true;
5790 return false;
5793 return hash->find (const_cast<rtx_insn *> (insn)) != NULL;
5796 bool
5797 prologue_contains (const rtx_insn *insn)
5799 return contains (insn, prologue_insn_hash);
5802 bool
5803 epilogue_contains (const rtx_insn *insn)
5805 return contains (insn, epilogue_insn_hash);
5808 bool
5809 prologue_epilogue_contains (const rtx_insn *insn)
5811 if (contains (insn, prologue_insn_hash))
5812 return true;
5813 if (contains (insn, epilogue_insn_hash))
5814 return true;
5815 return false;
5818 void
5819 record_prologue_seq (rtx_insn *seq)
5821 record_insns (seq, NULL, &prologue_insn_hash);
5824 void
5825 record_epilogue_seq (rtx_insn *seq)
5827 record_insns (seq, NULL, &epilogue_insn_hash);
5830 /* Set JUMP_LABEL for a return insn. */
5832 void
5833 set_return_jump_label (rtx_insn *returnjump)
5835 rtx pat = PATTERN (returnjump);
5836 if (GET_CODE (pat) == PARALLEL)
5837 pat = XVECEXP (pat, 0, 0);
5838 if (ANY_RETURN_P (pat))
5839 JUMP_LABEL (returnjump) = pat;
5840 else
5841 JUMP_LABEL (returnjump) = ret_rtx;
5844 /* Return a sequence to be used as the split prologue for the current
5845 function, or NULL. */
5847 static rtx_insn *
5848 make_split_prologue_seq (void)
5850 if (!flag_split_stack
5851 || lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl)))
5852 return NULL;
5854 start_sequence ();
5855 emit_insn (targetm.gen_split_stack_prologue ());
5856 rtx_insn *seq = get_insns ();
5857 end_sequence ();
5859 record_insns (seq, NULL, &prologue_insn_hash);
5860 set_insn_locations (seq, prologue_location);
5862 return seq;
5865 /* Return a sequence to be used as the prologue for the current function,
5866 or NULL. */
5868 static rtx_insn *
5869 make_prologue_seq (void)
5871 if (!targetm.have_prologue ())
5872 return NULL;
5874 start_sequence ();
5875 rtx_insn *seq = targetm.gen_prologue ();
5876 emit_insn (seq);
5878 /* Insert an explicit USE for the frame pointer
5879 if the profiling is on and the frame pointer is required. */
5880 if (crtl->profile && frame_pointer_needed)
5881 emit_use (hard_frame_pointer_rtx);
5883 /* Retain a map of the prologue insns. */
5884 record_insns (seq, NULL, &prologue_insn_hash);
5885 emit_note (NOTE_INSN_PROLOGUE_END);
5887 /* Ensure that instructions are not moved into the prologue when
5888 profiling is on. The call to the profiling routine can be
5889 emitted within the live range of a call-clobbered register. */
5890 if (!targetm.profile_before_prologue () && crtl->profile)
5891 emit_insn (gen_blockage ());
5893 seq = get_insns ();
5894 end_sequence ();
5895 set_insn_locations (seq, prologue_location);
5897 return seq;
5900 /* Emit a sequence of insns to zero the call-used registers before RET
5901 according to ZERO_REGS_TYPE. */
5903 static void
5904 gen_call_used_regs_seq (rtx_insn *ret, unsigned int zero_regs_type)
5906 bool only_gpr = true;
5907 bool only_used = true;
5908 bool only_arg = true;
5910 /* No need to zero call-used-regs in main (). */
5911 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
5912 return;
5914 /* No need to zero call-used-regs if __builtin_eh_return is called
5915 since it isn't a normal function return. */
5916 if (crtl->calls_eh_return)
5917 return;
5919 /* If only_gpr is true, only zero call-used registers that are
5920 general-purpose registers; if only_used is true, only zero
5921 call-used registers that are used in the current function;
5922 if only_arg is true, only zero call-used registers that pass
5923 parameters defined by the flatform's calling conversion. */
5925 using namespace zero_regs_flags;
5927 only_gpr = zero_regs_type & ONLY_GPR;
5928 only_used = zero_regs_type & ONLY_USED;
5929 only_arg = zero_regs_type & ONLY_ARG;
5931 if ((zero_regs_type & LEAFY_MODE) && leaf_function_p ())
5932 only_used = true;
5934 /* For each of the hard registers, we should zero it if:
5935 1. it is a call-used register;
5936 and 2. it is not a fixed register;
5937 and 3. it is not live at the return of the routine;
5938 and 4. it is general registor if only_gpr is true;
5939 and 5. it is used in the routine if only_used is true;
5940 and 6. it is a register that passes parameter if only_arg is true. */
5942 /* First, prepare the data flow information. */
5943 basic_block bb = BLOCK_FOR_INSN (ret);
5944 auto_bitmap live_out;
5945 bitmap_copy (live_out, df_get_live_out (bb));
5946 df_simulate_initialize_backwards (bb, live_out);
5947 df_simulate_one_insn_backwards (bb, ret, live_out);
5949 HARD_REG_SET selected_hardregs;
5950 HARD_REG_SET all_call_used_regs;
5951 CLEAR_HARD_REG_SET (selected_hardregs);
5952 CLEAR_HARD_REG_SET (all_call_used_regs);
5953 for (unsigned int regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5955 if (!crtl->abi->clobbers_full_reg_p (regno))
5956 continue;
5957 if (fixed_regs[regno])
5958 continue;
5959 if (REGNO_REG_SET_P (live_out, regno))
5960 continue;
5961 #ifdef LEAF_REG_REMAP
5962 if (crtl->uses_only_leaf_regs && LEAF_REG_REMAP (regno) < 0)
5963 continue;
5964 #endif
5965 /* This is a call used register that is dead at return. */
5966 SET_HARD_REG_BIT (all_call_used_regs, regno);
5968 if (only_gpr
5969 && !TEST_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], regno))
5970 continue;
5971 if (only_used && !df_regs_ever_live_p (regno))
5972 continue;
5973 if (only_arg && !FUNCTION_ARG_REGNO_P (regno))
5974 continue;
5976 /* Now this is a register that we might want to zero. */
5977 SET_HARD_REG_BIT (selected_hardregs, regno);
5980 if (hard_reg_set_empty_p (selected_hardregs))
5981 return;
5983 /* Now that we have a hard register set that needs to be zeroed, pass it to
5984 target to generate zeroing sequence. */
5985 HARD_REG_SET zeroed_hardregs;
5986 start_sequence ();
5987 zeroed_hardregs = targetm.calls.zero_call_used_regs (selected_hardregs);
5989 /* For most targets, the returned set of registers is a subset of
5990 selected_hardregs, however, for some of the targets (for example MIPS),
5991 clearing some registers that are in selected_hardregs requires clearing
5992 other call used registers that are not in the selected_hardregs, under
5993 such situation, the returned set of registers must be a subset of
5994 all call used registers. */
5995 gcc_assert (hard_reg_set_subset_p (zeroed_hardregs, all_call_used_regs));
5997 rtx_insn *seq = get_insns ();
5998 end_sequence ();
5999 if (seq)
6001 /* Emit the memory blockage and register clobber asm volatile before
6002 the whole sequence. */
6003 start_sequence ();
6004 expand_asm_reg_clobber_mem_blockage (zeroed_hardregs);
6005 rtx_insn *seq_barrier = get_insns ();
6006 end_sequence ();
6008 emit_insn_before (seq_barrier, ret);
6009 emit_insn_before (seq, ret);
6011 /* Update the data flow information. */
6012 crtl->must_be_zero_on_return |= zeroed_hardregs;
6013 df_update_exit_block_uses ();
6018 /* Return a sequence to be used as the epilogue for the current function,
6019 or NULL. */
6021 static rtx_insn *
6022 make_epilogue_seq (void)
6024 if (!targetm.have_epilogue ())
6025 return NULL;
6027 start_sequence ();
6028 emit_note (NOTE_INSN_EPILOGUE_BEG);
6029 rtx_insn *seq = targetm.gen_epilogue ();
6030 if (seq)
6031 emit_jump_insn (seq);
6033 /* Retain a map of the epilogue insns. */
6034 record_insns (seq, NULL, &epilogue_insn_hash);
6035 set_insn_locations (seq, epilogue_location);
6037 seq = get_insns ();
6038 rtx_insn *returnjump = get_last_insn ();
6039 end_sequence ();
6041 if (JUMP_P (returnjump))
6042 set_return_jump_label (returnjump);
6044 return seq;
6048 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
6049 this into place with notes indicating where the prologue ends and where
6050 the epilogue begins. Update the basic block information when possible.
6052 Notes on epilogue placement:
6053 There are several kinds of edges to the exit block:
6054 * a single fallthru edge from LAST_BB
6055 * possibly, edges from blocks containing sibcalls
6056 * possibly, fake edges from infinite loops
6058 The epilogue is always emitted on the fallthru edge from the last basic
6059 block in the function, LAST_BB, into the exit block.
6061 If LAST_BB is empty except for a label, it is the target of every
6062 other basic block in the function that ends in a return. If a
6063 target has a return or simple_return pattern (possibly with
6064 conditional variants), these basic blocks can be changed so that a
6065 return insn is emitted into them, and their target is adjusted to
6066 the real exit block.
6068 Notes on shrink wrapping: We implement a fairly conservative
6069 version of shrink-wrapping rather than the textbook one. We only
6070 generate a single prologue and a single epilogue. This is
6071 sufficient to catch a number of interesting cases involving early
6072 exits.
6074 First, we identify the blocks that require the prologue to occur before
6075 them. These are the ones that modify a call-saved register, or reference
6076 any of the stack or frame pointer registers. To simplify things, we then
6077 mark everything reachable from these blocks as also requiring a prologue.
6078 This takes care of loops automatically, and avoids the need to examine
6079 whether MEMs reference the frame, since it is sufficient to check for
6080 occurrences of the stack or frame pointer.
6082 We then compute the set of blocks for which the need for a prologue
6083 is anticipatable (borrowing terminology from the shrink-wrapping
6084 description in Muchnick's book). These are the blocks which either
6085 require a prologue themselves, or those that have only successors
6086 where the prologue is anticipatable. The prologue needs to be
6087 inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
6088 is not. For the moment, we ensure that only one such edge exists.
6090 The epilogue is placed as described above, but we make a
6091 distinction between inserting return and simple_return patterns
6092 when modifying other blocks that end in a return. Blocks that end
6093 in a sibcall omit the sibcall_epilogue if the block is not in
6094 ANTIC. */
6096 void
6097 thread_prologue_and_epilogue_insns (void)
6099 df_analyze ();
6101 /* Can't deal with multiple successors of the entry block at the
6102 moment. Function should always have at least one entry
6103 point. */
6104 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
6106 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6107 edge orig_entry_edge = entry_edge;
6109 rtx_insn *split_prologue_seq = make_split_prologue_seq ();
6110 rtx_insn *prologue_seq = make_prologue_seq ();
6111 rtx_insn *epilogue_seq = make_epilogue_seq ();
6113 /* Try to perform a kind of shrink-wrapping, making sure the
6114 prologue/epilogue is emitted only around those parts of the
6115 function that require it. */
6116 try_shrink_wrapping (&entry_edge, prologue_seq);
6118 /* If the target can handle splitting the prologue/epilogue into separate
6119 components, try to shrink-wrap these components separately. */
6120 try_shrink_wrapping_separate (entry_edge->dest);
6122 /* If that did anything for any component we now need the generate the
6123 "main" prologue again. Because some targets require some of these
6124 to be called in a specific order (i386 requires the split prologue
6125 to be first, for example), we create all three sequences again here.
6126 If this does not work for some target, that target should not enable
6127 separate shrink-wrapping. */
6128 if (crtl->shrink_wrapped_separate)
6130 split_prologue_seq = make_split_prologue_seq ();
6131 prologue_seq = make_prologue_seq ();
6132 epilogue_seq = make_epilogue_seq ();
6135 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
6137 /* A small fib -- epilogue is not yet completed, but we wish to re-use
6138 this marker for the splits of EH_RETURN patterns, and nothing else
6139 uses the flag in the meantime. */
6140 epilogue_completed = 1;
6142 /* Find non-fallthru edges that end with EH_RETURN instructions. On
6143 some targets, these get split to a special version of the epilogue
6144 code. In order to be able to properly annotate these with unwind
6145 info, try to split them now. If we get a valid split, drop an
6146 EPILOGUE_BEG note and mark the insns as epilogue insns. */
6147 edge e;
6148 edge_iterator ei;
6149 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6151 rtx_insn *prev, *last, *trial;
6153 if (e->flags & EDGE_FALLTHRU)
6154 continue;
6155 last = BB_END (e->src);
6156 if (!eh_returnjump_p (last))
6157 continue;
6159 prev = PREV_INSN (last);
6160 trial = try_split (PATTERN (last), last, 1);
6161 if (trial == last)
6162 continue;
6164 record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
6165 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
6168 edge exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
6170 if (exit_fallthru_edge)
6172 if (epilogue_seq)
6174 insert_insn_on_edge (epilogue_seq, exit_fallthru_edge);
6175 commit_edge_insertions ();
6177 /* The epilogue insns we inserted may cause the exit edge to no longer
6178 be fallthru. */
6179 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6181 if (((e->flags & EDGE_FALLTHRU) != 0)
6182 && returnjump_p (BB_END (e->src)))
6183 e->flags &= ~EDGE_FALLTHRU;
6186 find_sub_basic_blocks (BLOCK_FOR_INSN (epilogue_seq));
6188 else if (next_active_insn (BB_END (exit_fallthru_edge->src)))
6190 /* We have a fall-through edge to the exit block, the source is not
6191 at the end of the function, and there will be an assembler epilogue
6192 at the end of the function.
6193 We can't use force_nonfallthru here, because that would try to
6194 use return. Inserting a jump 'by hand' is extremely messy, so
6195 we take advantage of cfg_layout_finalize using
6196 fixup_fallthru_exit_predecessor. */
6197 cfg_layout_initialize (0);
6198 basic_block cur_bb;
6199 FOR_EACH_BB_FN (cur_bb, cfun)
6200 if (cur_bb->index >= NUM_FIXED_BLOCKS
6201 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
6202 cur_bb->aux = cur_bb->next_bb;
6203 cfg_layout_finalize ();
6207 /* Insert the prologue. */
6209 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6211 if (split_prologue_seq || prologue_seq)
6213 rtx_insn *split_prologue_insn = split_prologue_seq;
6214 if (split_prologue_seq)
6216 while (split_prologue_insn && !NONDEBUG_INSN_P (split_prologue_insn))
6217 split_prologue_insn = NEXT_INSN (split_prologue_insn);
6218 insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
6221 rtx_insn *prologue_insn = prologue_seq;
6222 if (prologue_seq)
6224 while (prologue_insn && !NONDEBUG_INSN_P (prologue_insn))
6225 prologue_insn = NEXT_INSN (prologue_insn);
6226 insert_insn_on_edge (prologue_seq, entry_edge);
6229 commit_edge_insertions ();
6231 /* Look for basic blocks within the prologue insns. */
6232 if (split_prologue_insn
6233 && BLOCK_FOR_INSN (split_prologue_insn) == NULL)
6234 split_prologue_insn = NULL;
6235 if (prologue_insn
6236 && BLOCK_FOR_INSN (prologue_insn) == NULL)
6237 prologue_insn = NULL;
6238 if (split_prologue_insn || prologue_insn)
6240 auto_sbitmap blocks (last_basic_block_for_fn (cfun));
6241 bitmap_clear (blocks);
6242 if (split_prologue_insn)
6243 bitmap_set_bit (blocks,
6244 BLOCK_FOR_INSN (split_prologue_insn)->index);
6245 if (prologue_insn)
6246 bitmap_set_bit (blocks, BLOCK_FOR_INSN (prologue_insn)->index);
6247 find_many_sub_basic_blocks (blocks);
6251 default_rtl_profile ();
6253 /* Emit sibling epilogues before any sibling call sites. */
6254 for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
6255 (e = ei_safe_edge (ei));
6256 ei_next (&ei))
6258 /* Skip those already handled, the ones that run without prologue. */
6259 if (e->flags & EDGE_IGNORE)
6261 e->flags &= ~EDGE_IGNORE;
6262 continue;
6265 rtx_insn *insn = BB_END (e->src);
6267 if (!(CALL_P (insn) && SIBLING_CALL_P (insn)))
6268 continue;
6270 rtx_insn *ep_seq;
6271 if (targetm.emit_epilogue_for_sibcall)
6273 start_sequence ();
6274 targetm.emit_epilogue_for_sibcall (as_a<rtx_call_insn *> (insn));
6275 ep_seq = get_insns ();
6276 end_sequence ();
6278 else
6279 ep_seq = targetm.gen_sibcall_epilogue ();
6280 if (ep_seq)
6282 start_sequence ();
6283 emit_note (NOTE_INSN_EPILOGUE_BEG);
6284 emit_insn (ep_seq);
6285 rtx_insn *seq = get_insns ();
6286 end_sequence ();
6288 /* Retain a map of the epilogue insns. Used in life analysis to
6289 avoid getting rid of sibcall epilogue insns. Do this before we
6290 actually emit the sequence. */
6291 record_insns (seq, NULL, &epilogue_insn_hash);
6292 set_insn_locations (seq, epilogue_location);
6294 emit_insn_before (seq, insn);
6296 find_sub_basic_blocks (BLOCK_FOR_INSN (insn));
6300 if (epilogue_seq)
6302 rtx_insn *insn, *next;
6304 /* Similarly, move any line notes that appear after the epilogue.
6305 There is no need, however, to be quite so anal about the existence
6306 of such a note. Also possibly move
6307 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
6308 info generation. */
6309 for (insn = epilogue_seq; insn; insn = next)
6311 next = NEXT_INSN (insn);
6312 if (NOTE_P (insn)
6313 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
6314 reorder_insns (insn, insn, PREV_INSN (epilogue_seq));
6318 /* Threading the prologue and epilogue changes the artificial refs in the
6319 entry and exit blocks, and may invalidate DF info for tail calls.
6320 This is also needed for [[musttail]] conversion even when not
6321 optimizing. */
6322 if (optimize
6323 || cfun->tail_call_marked
6324 || flag_optimize_sibling_calls
6325 || flag_ipa_icf_functions
6326 || in_lto_p)
6327 df_update_entry_exit_and_calls ();
6328 else
6330 df_update_entry_block_defs ();
6331 df_update_exit_block_uses ();
6335 /* Reposition the prologue-end and epilogue-begin notes after
6336 instruction scheduling. */
6338 void
6339 reposition_prologue_and_epilogue_notes (void)
6341 if (!targetm.have_prologue ()
6342 && !targetm.have_epilogue ()
6343 && !targetm.have_sibcall_epilogue ()
6344 && !targetm.emit_epilogue_for_sibcall)
6345 return;
6347 /* Since the hash table is created on demand, the fact that it is
6348 non-null is a signal that it is non-empty. */
6349 if (prologue_insn_hash != NULL)
6351 size_t len = prologue_insn_hash->elements ();
6352 rtx_insn *insn, *last = NULL, *note = NULL;
6354 /* Scan from the beginning until we reach the last prologue insn. */
6355 /* ??? While we do have the CFG intact, there are two problems:
6356 (1) The prologue can contain loops (typically probing the stack),
6357 which means that the end of the prologue isn't in the first bb.
6358 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
6359 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6361 if (NOTE_P (insn))
6363 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
6364 note = insn;
6366 else if (contains (insn, prologue_insn_hash))
6368 last = insn;
6369 if (--len == 0)
6370 break;
6374 if (last)
6376 if (note == NULL)
6378 /* Scan forward looking for the PROLOGUE_END note. It should
6379 be right at the beginning of the block, possibly with other
6380 insn notes that got moved there. */
6381 for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
6383 if (NOTE_P (note)
6384 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
6385 break;
6389 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
6390 if (LABEL_P (last))
6391 last = NEXT_INSN (last);
6392 reorder_insns (note, note, last);
6396 if (epilogue_insn_hash != NULL)
6398 edge_iterator ei;
6399 edge e;
6401 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6403 rtx_insn *insn, *first = NULL, *note = NULL;
6404 basic_block bb = e->src;
6406 /* Scan from the beginning until we reach the first epilogue insn. */
6407 FOR_BB_INSNS (bb, insn)
6409 if (NOTE_P (insn))
6411 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
6413 note = insn;
6414 if (first != NULL)
6415 break;
6418 else if (first == NULL && contains (insn, epilogue_insn_hash))
6420 first = insn;
6421 if (note != NULL)
6422 break;
6426 if (note)
6428 /* If the function has a single basic block, and no real
6429 epilogue insns (e.g. sibcall with no cleanup), the
6430 epilogue note can get scheduled before the prologue
6431 note. If we have frame related prologue insns, having
6432 them scanned during the epilogue will result in a crash.
6433 In this case re-order the epilogue note to just before
6434 the last insn in the block. */
6435 if (first == NULL)
6436 first = BB_END (bb);
6438 if (PREV_INSN (first) != note)
6439 reorder_insns (note, note, PREV_INSN (first));
6445 /* Returns the name of function declared by FNDECL. */
6446 const char *
6447 fndecl_name (tree fndecl)
6449 if (fndecl == NULL)
6450 return "(nofn)";
6451 return lang_hooks.decl_printable_name (fndecl, 1);
6454 /* Returns the name of function FN. */
6455 const char *
6456 function_name (const function *fn)
6458 tree fndecl = (fn == NULL) ? NULL : fn->decl;
6459 return fndecl_name (fndecl);
6462 /* Returns the name of the current function. */
6463 const char *
6464 current_function_name (void)
6466 return function_name (cfun);
6470 static void
6471 rest_of_handle_check_leaf_regs (void)
6473 #ifdef LEAF_REGISTERS
6474 crtl->uses_only_leaf_regs
6475 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6476 #endif
6479 /* Insert a TYPE into the used types hash table of CFUN. */
6481 static void
6482 used_types_insert_helper (tree type, struct function *func)
6484 if (type != NULL && func != NULL)
6486 if (func->used_types_hash == NULL)
6487 func->used_types_hash = hash_set<tree>::create_ggc (37);
6489 func->used_types_hash->add (type);
6493 /* Given a type, insert it into the used hash table in cfun. */
6494 void
6495 used_types_insert (tree t)
6497 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6498 if (TYPE_NAME (t))
6499 break;
6500 else
6501 t = TREE_TYPE (t);
6502 if (TREE_CODE (t) == ERROR_MARK)
6503 return;
6504 if (TYPE_NAME (t) == NULL_TREE
6505 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6506 t = TYPE_MAIN_VARIANT (t);
6507 if (debug_info_level > DINFO_LEVEL_NONE)
6509 if (cfun)
6510 used_types_insert_helper (t, cfun);
6511 else
6513 /* So this might be a type referenced by a global variable.
6514 Record that type so that we can later decide to emit its
6515 debug information. */
6516 vec_safe_push (types_used_by_cur_var_decl, t);
6521 /* Helper to Hash a struct types_used_by_vars_entry. */
6523 static hashval_t
6524 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6526 gcc_assert (entry && entry->var_decl && entry->type);
6528 return iterative_hash_object (entry->type,
6529 iterative_hash_object (entry->var_decl, 0));
6532 /* Hash function of the types_used_by_vars_entry hash table. */
6534 hashval_t
6535 used_type_hasher::hash (types_used_by_vars_entry *entry)
6537 return hash_types_used_by_vars_entry (entry);
6540 /*Equality function of the types_used_by_vars_entry hash table. */
6542 bool
6543 used_type_hasher::equal (types_used_by_vars_entry *e1,
6544 types_used_by_vars_entry *e2)
6546 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6549 /* Inserts an entry into the types_used_by_vars_hash hash table. */
6551 void
6552 types_used_by_var_decl_insert (tree type, tree var_decl)
6554 if (type != NULL && var_decl != NULL)
6556 types_used_by_vars_entry **slot;
6557 struct types_used_by_vars_entry e;
6558 e.var_decl = var_decl;
6559 e.type = type;
6560 if (types_used_by_vars_hash == NULL)
6561 types_used_by_vars_hash
6562 = hash_table<used_type_hasher>::create_ggc (37);
6564 slot = types_used_by_vars_hash->find_slot (&e, INSERT);
6565 if (*slot == NULL)
6567 struct types_used_by_vars_entry *entry;
6568 entry = ggc_alloc<types_used_by_vars_entry> ();
6569 entry->type = type;
6570 entry->var_decl = var_decl;
6571 *slot = entry;
6576 namespace {
6578 const pass_data pass_data_leaf_regs =
6580 RTL_PASS, /* type */
6581 "*leaf_regs", /* name */
6582 OPTGROUP_NONE, /* optinfo_flags */
6583 TV_NONE, /* tv_id */
6584 0, /* properties_required */
6585 0, /* properties_provided */
6586 0, /* properties_destroyed */
6587 0, /* todo_flags_start */
6588 0, /* todo_flags_finish */
6591 class pass_leaf_regs : public rtl_opt_pass
6593 public:
6594 pass_leaf_regs (gcc::context *ctxt)
6595 : rtl_opt_pass (pass_data_leaf_regs, ctxt)
6598 /* opt_pass methods: */
6599 unsigned int execute (function *) final override
6601 rest_of_handle_check_leaf_regs ();
6602 return 0;
6605 }; // class pass_leaf_regs
6607 } // anon namespace
6609 rtl_opt_pass *
6610 make_pass_leaf_regs (gcc::context *ctxt)
6612 return new pass_leaf_regs (ctxt);
6615 static void
6616 rest_of_handle_thread_prologue_and_epilogue (function *fun)
6618 /* prepare_shrink_wrap is sensitive to the block structure of the control
6619 flow graph, so clean it up first. */
6620 if (cfun->tail_call_marked || optimize)
6621 cleanup_cfg (0);
6623 /* On some machines, the prologue and epilogue code, or parts thereof,
6624 can be represented as RTL. Doing so lets us schedule insns between
6625 it and the rest of the code and also allows delayed branch
6626 scheduling to operate in the epilogue. */
6627 thread_prologue_and_epilogue_insns ();
6629 /* Some non-cold blocks may now be only reachable from cold blocks.
6630 Fix that up. */
6631 fixup_partitions ();
6633 /* After prologue and epilogue generation, the judgement on whether
6634 one memory access onto stack frame may trap or not could change,
6635 since we get more exact stack information by now. So try to
6636 remove any EH edges here, see PR90259. */
6637 if (fun->can_throw_non_call_exceptions)
6638 purge_all_dead_edges ();
6640 /* Shrink-wrapping can result in unreachable edges in the epilogue,
6641 see PR57320. */
6642 cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
6644 /* The stack usage info is finalized during prologue expansion. */
6645 if (flag_stack_usage_info || flag_callgraph_info)
6646 output_stack_usage ();
6649 /* Record a final call to CALLEE at LOCATION. */
6651 void
6652 record_final_call (tree callee, location_t location)
6654 struct callinfo_callee datum = { location, callee };
6655 vec_safe_push (cfun->su->callees, datum);
6658 /* Record a dynamic allocation made for DECL_OR_EXP. */
6660 void
6661 record_dynamic_alloc (tree decl_or_exp)
6663 struct callinfo_dalloc datum;
6665 if (DECL_P (decl_or_exp))
6667 datum.location = DECL_SOURCE_LOCATION (decl_or_exp);
6668 const char *name = lang_hooks.decl_printable_name (decl_or_exp, 2);
6669 const char *dot = strrchr (name, '.');
6670 if (dot)
6671 name = dot + 1;
6672 datum.name = ggc_strdup (name);
6674 else
6676 datum.location = EXPR_LOCATION (decl_or_exp);
6677 datum.name = NULL;
6680 vec_safe_push (cfun->su->dallocs, datum);
6683 namespace {
6685 const pass_data pass_data_thread_prologue_and_epilogue =
6687 RTL_PASS, /* type */
6688 "pro_and_epilogue", /* name */
6689 OPTGROUP_NONE, /* optinfo_flags */
6690 TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
6691 0, /* properties_required */
6692 0, /* properties_provided */
6693 0, /* properties_destroyed */
6694 0, /* todo_flags_start */
6695 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
6698 class pass_thread_prologue_and_epilogue : public rtl_opt_pass
6700 public:
6701 pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6702 : rtl_opt_pass (pass_data_thread_prologue_and_epilogue, ctxt)
6705 /* opt_pass methods: */
6706 bool gate (function *) final override
6708 return !targetm.use_late_prologue_epilogue ();
6711 unsigned int execute (function * fun) final override
6713 rest_of_handle_thread_prologue_and_epilogue (fun);
6714 return 0;
6717 }; // class pass_thread_prologue_and_epilogue
6719 const pass_data pass_data_late_thread_prologue_and_epilogue =
6721 RTL_PASS, /* type */
6722 "late_pro_and_epilogue", /* name */
6723 OPTGROUP_NONE, /* optinfo_flags */
6724 TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
6725 0, /* properties_required */
6726 0, /* properties_provided */
6727 0, /* properties_destroyed */
6728 0, /* todo_flags_start */
6729 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
6732 class pass_late_thread_prologue_and_epilogue : public rtl_opt_pass
6734 public:
6735 pass_late_thread_prologue_and_epilogue (gcc::context *ctxt)
6736 : rtl_opt_pass (pass_data_late_thread_prologue_and_epilogue, ctxt)
6739 /* opt_pass methods: */
6740 bool gate (function *) final override
6742 return targetm.use_late_prologue_epilogue ();
6745 unsigned int execute (function *fn) final override
6747 /* It's not currently possible to have both delay slots and
6748 late prologue/epilogue, since the latter has to run before
6749 the former, and the former won't honor whatever restrictions
6750 the latter is trying to enforce. */
6751 gcc_assert (!DELAY_SLOTS);
6752 rest_of_handle_thread_prologue_and_epilogue (fn);
6753 return 0;
6755 }; // class pass_late_thread_prologue_and_epilogue
6757 } // anon namespace
6759 rtl_opt_pass *
6760 make_pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6762 return new pass_thread_prologue_and_epilogue (ctxt);
6765 rtl_opt_pass *
6766 make_pass_late_thread_prologue_and_epilogue (gcc::context *ctxt)
6768 return new pass_late_thread_prologue_and_epilogue (ctxt);
6771 namespace {
6773 const pass_data pass_data_zero_call_used_regs =
6775 RTL_PASS, /* type */
6776 "zero_call_used_regs", /* name */
6777 OPTGROUP_NONE, /* optinfo_flags */
6778 TV_NONE, /* tv_id */
6779 0, /* properties_required */
6780 0, /* properties_provided */
6781 0, /* properties_destroyed */
6782 0, /* todo_flags_start */
6783 0, /* todo_flags_finish */
6786 class pass_zero_call_used_regs: public rtl_opt_pass
6788 public:
6789 pass_zero_call_used_regs (gcc::context *ctxt)
6790 : rtl_opt_pass (pass_data_zero_call_used_regs, ctxt)
6793 /* opt_pass methods: */
6794 unsigned int execute (function *) final override;
6796 }; // class pass_zero_call_used_regs
6798 unsigned int
6799 pass_zero_call_used_regs::execute (function *fun)
6801 using namespace zero_regs_flags;
6802 unsigned int zero_regs_type = UNSET;
6804 tree attr_zero_regs = lookup_attribute ("zero_call_used_regs",
6805 DECL_ATTRIBUTES (fun->decl));
6807 /* Get the type of zero_call_used_regs from function attribute.
6808 We have filtered out invalid attribute values already at this point. */
6809 if (attr_zero_regs)
6811 /* The TREE_VALUE of an attribute is a TREE_LIST whose TREE_VALUE
6812 is the attribute argument's value. */
6813 attr_zero_regs = TREE_VALUE (attr_zero_regs);
6814 gcc_assert (TREE_CODE (attr_zero_regs) == TREE_LIST);
6815 attr_zero_regs = TREE_VALUE (attr_zero_regs);
6816 gcc_assert (TREE_CODE (attr_zero_regs) == STRING_CST);
6818 for (unsigned int i = 0; zero_call_used_regs_opts[i].name != NULL; ++i)
6819 if (strcmp (TREE_STRING_POINTER (attr_zero_regs),
6820 zero_call_used_regs_opts[i].name) == 0)
6822 zero_regs_type = zero_call_used_regs_opts[i].flag;
6823 break;
6827 if (!zero_regs_type)
6828 zero_regs_type = flag_zero_call_used_regs;
6830 /* No need to zero call-used-regs when no user request is present. */
6831 if (!(zero_regs_type & ENABLED))
6832 return 0;
6834 edge_iterator ei;
6835 edge e;
6837 /* This pass needs data flow information. */
6838 df_analyze ();
6840 /* Iterate over the function's return instructions and insert any
6841 register zeroing required by the -fzero-call-used-regs command-line
6842 option or the "zero_call_used_regs" function attribute. */
6843 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6845 rtx_insn *insn = BB_END (e->src);
6846 if (JUMP_P (insn) && ANY_RETURN_P (JUMP_LABEL (insn)))
6847 gen_call_used_regs_seq (insn, zero_regs_type);
6850 return 0;
6853 } // anon namespace
6855 rtl_opt_pass *
6856 make_pass_zero_call_used_regs (gcc::context *ctxt)
6858 return new pass_zero_call_used_regs (ctxt);
6861 /* If CONSTRAINT is a matching constraint, then return its number.
6862 Otherwise, return -1. */
6864 static int
6865 matching_constraint_num (const char *constraint)
6867 if (*constraint == '%')
6868 constraint++;
6870 if (IN_RANGE (*constraint, '0', '9'))
6871 return strtoul (constraint, NULL, 10);
6873 return -1;
6876 /* This mini-pass fixes fall-out from SSA in asm statements that have
6877 in-out constraints. Say you start with
6879 orig = inout;
6880 asm ("": "+mr" (inout));
6881 use (orig);
6883 which is transformed very early to use explicit output and match operands:
6885 orig = inout;
6886 asm ("": "=mr" (inout) : "0" (inout));
6887 use (orig);
6889 Or, after SSA and copyprop,
6891 asm ("": "=mr" (inout_2) : "0" (inout_1));
6892 use (inout_1);
6894 Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
6895 they represent two separate values, so they will get different pseudo
6896 registers during expansion. Then, since the two operands need to match
6897 per the constraints, but use different pseudo registers, reload can
6898 only register a reload for these operands. But reloads can only be
6899 satisfied by hardregs, not by memory, so we need a register for this
6900 reload, just because we are presented with non-matching operands.
6901 So, even though we allow memory for this operand, no memory can be
6902 used for it, just because the two operands don't match. This can
6903 cause reload failures on register-starved targets.
6905 So it's a symptom of reload not being able to use memory for reloads
6906 or, alternatively it's also a symptom of both operands not coming into
6907 reload as matching (in which case the pseudo could go to memory just
6908 fine, as the alternative allows it, and no reload would be necessary).
6909 We fix the latter problem here, by transforming
6911 asm ("": "=mr" (inout_2) : "0" (inout_1));
6913 back to
6915 inout_2 = inout_1;
6916 asm ("": "=mr" (inout_2) : "0" (inout_2)); */
6918 static void
6919 match_asm_constraints_1 (rtx_insn *insn, rtx *p_sets, int noutputs)
6921 int i;
6922 bool changed = false;
6923 rtx op = SET_SRC (p_sets[0]);
6924 int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
6925 rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
6926 bool *output_matched = XALLOCAVEC (bool, noutputs);
6928 memset (output_matched, 0, noutputs * sizeof (bool));
6929 for (i = 0; i < ninputs; i++)
6931 rtx input, output;
6932 rtx_insn *insns;
6933 const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
6934 int match, j;
6936 match = matching_constraint_num (constraint);
6937 if (match < 0)
6938 continue;
6940 gcc_assert (match < noutputs);
6941 output = SET_DEST (p_sets[match]);
6942 input = RTVEC_ELT (inputs, i);
6943 /* Only do the transformation for pseudos. */
6944 if (! REG_P (output)
6945 || rtx_equal_p (output, input)
6946 || !(REG_P (input) || SUBREG_P (input)
6947 || MEM_P (input) || CONSTANT_P (input))
6948 || !general_operand (input, GET_MODE (output)))
6949 continue;
6951 /* We can't do anything if the output is also used as input,
6952 as we're going to overwrite it. */
6953 for (j = 0; j < ninputs; j++)
6954 if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
6955 break;
6956 if (j != ninputs)
6957 continue;
6959 /* Avoid changing the same input several times. For
6960 asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
6961 only change it once (to out1), rather than changing it
6962 first to out1 and afterwards to out2. */
6963 if (i > 0)
6965 for (j = 0; j < noutputs; j++)
6966 if (output_matched[j] && input == SET_DEST (p_sets[j]))
6967 break;
6968 if (j != noutputs)
6969 continue;
6971 output_matched[match] = true;
6973 start_sequence ();
6974 emit_move_insn (output, copy_rtx (input));
6975 insns = get_insns ();
6976 end_sequence ();
6977 emit_insn_before (insns, insn);
6979 constraint = ASM_OPERANDS_OUTPUT_CONSTRAINT(SET_SRC(p_sets[match]));
6980 bool early_clobber_p = strchr (constraint, '&') != NULL;
6982 /* Now replace all mentions of the input with output. We can't
6983 just replace the occurrence in inputs[i], as the register might
6984 also be used in some other input (or even in an address of an
6985 output), which would mean possibly increasing the number of
6986 inputs by one (namely 'output' in addition), which might pose
6987 a too complicated problem for reload to solve. E.g. this situation:
6989 asm ("" : "=r" (output), "=m" (input) : "0" (input))
6991 Here 'input' is used in two occurrences as input (once for the
6992 input operand, once for the address in the second output operand).
6993 If we would replace only the occurrence of the input operand (to
6994 make the matching) we would be left with this:
6996 output = input
6997 asm ("" : "=r" (output), "=m" (input) : "0" (output))
6999 Now we suddenly have two different input values (containing the same
7000 value, but different pseudos) where we formerly had only one.
7001 With more complicated asms this might lead to reload failures
7002 which wouldn't have happen without this pass. So, iterate over
7003 all operands and replace all occurrences of the register used.
7005 However, if one or more of the 'input' uses have a non-matching
7006 constraint and the matched output operand is an early clobber
7007 operand, then do not replace the input operand, since by definition
7008 it conflicts with the output operand and cannot share the same
7009 register. See PR89313 for details. */
7011 for (j = 0; j < noutputs; j++)
7012 if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
7013 && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
7014 SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
7015 input, output);
7016 for (j = 0; j < ninputs; j++)
7017 if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
7019 if (!early_clobber_p
7020 || match == matching_constraint_num
7021 (ASM_OPERANDS_INPUT_CONSTRAINT (op, j)))
7022 RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
7023 input, output);
7026 changed = true;
7029 if (changed)
7030 df_insn_rescan (insn);
7033 /* Add the decl D to the local_decls list of FUN. */
7035 void
7036 add_local_decl (struct function *fun, tree d)
7038 gcc_assert (VAR_P (d));
7039 vec_safe_push (fun->local_decls, d);
7042 namespace {
7044 const pass_data pass_data_match_asm_constraints =
7046 RTL_PASS, /* type */
7047 "asmcons", /* name */
7048 OPTGROUP_NONE, /* optinfo_flags */
7049 TV_NONE, /* tv_id */
7050 0, /* properties_required */
7051 0, /* properties_provided */
7052 0, /* properties_destroyed */
7053 0, /* todo_flags_start */
7054 0, /* todo_flags_finish */
7057 class pass_match_asm_constraints : public rtl_opt_pass
7059 public:
7060 pass_match_asm_constraints (gcc::context *ctxt)
7061 : rtl_opt_pass (pass_data_match_asm_constraints, ctxt)
7064 /* opt_pass methods: */
7065 unsigned int execute (function *) final override;
7067 }; // class pass_match_asm_constraints
7069 unsigned
7070 pass_match_asm_constraints::execute (function *fun)
7072 basic_block bb;
7073 rtx_insn *insn;
7074 rtx pat, *p_sets;
7075 int noutputs;
7077 if (!crtl->has_asm_statement)
7078 return 0;
7080 df_set_flags (DF_DEFER_INSN_RESCAN);
7081 FOR_EACH_BB_FN (bb, fun)
7083 FOR_BB_INSNS (bb, insn)
7085 if (!INSN_P (insn))
7086 continue;
7088 pat = PATTERN (insn);
7089 if (GET_CODE (pat) == PARALLEL)
7090 p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
7091 else if (GET_CODE (pat) == SET)
7092 p_sets = &PATTERN (insn), noutputs = 1;
7093 else
7094 continue;
7096 if (GET_CODE (*p_sets) == SET
7097 && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
7098 match_asm_constraints_1 (insn, p_sets, noutputs);
7102 return TODO_df_finish;
7105 } // anon namespace
7107 rtl_opt_pass *
7108 make_pass_match_asm_constraints (gcc::context *ctxt)
7110 return new pass_match_asm_constraints (ctxt);
7114 #include "gt-function.h"