PR libstdc++/85951 for make_signed/make_unsigned for character types
[official-gcc.git] / gcc / function.c
blob6b9fd597d41e6f60f972fba096cab3bd7db1edd2
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987-2018 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 "regs.h"
50 #include "emit-rtl.h"
51 #include "recog.h"
52 #include "rtl-error.h"
53 #include "alias.h"
54 #include "fold-const.h"
55 #include "stor-layout.h"
56 #include "varasm.h"
57 #include "except.h"
58 #include "dojump.h"
59 #include "explow.h"
60 #include "calls.h"
61 #include "expr.h"
62 #include "optabs-tree.h"
63 #include "output.h"
64 #include "langhooks.h"
65 #include "common/common-target.h"
66 #include "gimplify.h"
67 #include "tree-pass.h"
68 #include "cfgrtl.h"
69 #include "cfganal.h"
70 #include "cfgbuild.h"
71 #include "cfgcleanup.h"
72 #include "cfgexpand.h"
73 #include "shrink-wrap.h"
74 #include "toplev.h"
75 #include "rtl-iter.h"
76 #include "tree-chkp.h"
77 #include "rtl-chkp.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"
85 /* So we can assign to cfun in this file. */
86 #undef cfun
88 #ifndef STACK_ALIGNMENT_NEEDED
89 #define STACK_ALIGNMENT_NEEDED 1
90 #endif
92 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
94 /* Round a value to the lowest integer less than it that is a multiple of
95 the required alignment. Avoid using division in case the value is
96 negative. Assume the alignment is a power of two. */
97 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
99 /* Similar, but round to the next highest integer that meets the
100 alignment. */
101 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
103 /* Nonzero once virtual register instantiation has been done.
104 assign_stack_local uses frame_pointer_rtx when this is nonzero.
105 calls.c:emit_library_call_value_1 uses it to set up
106 post-instantiation libcalls. */
107 int virtuals_instantiated;
109 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
110 static GTY(()) int funcdef_no;
112 /* These variables hold pointers to functions to create and destroy
113 target specific, per-function data structures. */
114 struct machine_function * (*init_machine_status) (void);
116 /* The currently compiled function. */
117 struct function *cfun = 0;
119 /* These hashes record the prologue and epilogue insns. */
121 struct insn_cache_hasher : ggc_cache_ptr_hash<rtx_def>
123 static hashval_t hash (rtx x) { return htab_hash_pointer (x); }
124 static bool equal (rtx a, rtx b) { return a == b; }
127 static GTY((cache))
128 hash_table<insn_cache_hasher> *prologue_insn_hash;
129 static GTY((cache))
130 hash_table<insn_cache_hasher> *epilogue_insn_hash;
133 hash_table<used_type_hasher> *types_used_by_vars_hash = NULL;
134 vec<tree, va_gc> *types_used_by_cur_var_decl;
136 /* Forward declarations. */
138 static struct temp_slot *find_temp_slot_from_address (rtx);
139 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
140 static void pad_below (struct args_size *, machine_mode, tree);
141 static void reorder_blocks_1 (rtx_insn *, tree, vec<tree> *);
142 static int all_blocks (tree, tree *);
143 static tree *get_block_vector (tree, int *);
144 extern tree debug_find_var_in_block_tree (tree, tree);
145 /* We always define `record_insns' even if it's not used so that we
146 can always export `prologue_epilogue_contains'. */
147 static void record_insns (rtx_insn *, rtx, hash_table<insn_cache_hasher> **)
148 ATTRIBUTE_UNUSED;
149 static bool contains (const rtx_insn *, hash_table<insn_cache_hasher> *);
150 static void prepare_function_start (void);
151 static void do_clobber_return_reg (rtx, void *);
152 static void do_use_return_reg (rtx, void *);
155 /* Stack of nested functions. */
156 /* Keep track of the cfun stack. */
158 static vec<function *> function_context_stack;
160 /* Save the current context for compilation of a nested function.
161 This is called from language-specific code. */
163 void
164 push_function_context (void)
166 if (cfun == 0)
167 allocate_struct_function (NULL, false);
169 function_context_stack.safe_push (cfun);
170 set_cfun (NULL);
173 /* Restore the last saved context, at the end of a nested function.
174 This function is called from language-specific code. */
176 void
177 pop_function_context (void)
179 struct function *p = function_context_stack.pop ();
180 set_cfun (p);
181 current_function_decl = p->decl;
183 /* Reset variables that have known state during rtx generation. */
184 virtuals_instantiated = 0;
185 generating_concat_p = 1;
188 /* Clear out all parts of the state in F that can safely be discarded
189 after the function has been parsed, but not compiled, to let
190 garbage collection reclaim the memory. */
192 void
193 free_after_parsing (struct function *f)
195 f->language = 0;
198 /* Clear out all parts of the state in F that can safely be discarded
199 after the function has been compiled, to let garbage collection
200 reclaim the memory. */
202 void
203 free_after_compilation (struct function *f)
205 prologue_insn_hash = NULL;
206 epilogue_insn_hash = NULL;
208 free (crtl->emit.regno_pointer_align);
210 memset (crtl, 0, sizeof (struct rtl_data));
211 f->eh = NULL;
212 f->machine = NULL;
213 f->cfg = NULL;
214 f->curr_properties &= ~PROP_cfg;
216 regno_reg_rtx = NULL;
219 /* Return size needed for stack frame based on slots so far allocated.
220 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
221 the caller may have to do that. */
223 poly_int64
224 get_frame_size (void)
226 if (FRAME_GROWS_DOWNWARD)
227 return -frame_offset;
228 else
229 return frame_offset;
232 /* Issue an error message and return TRUE if frame OFFSET overflows in
233 the signed target pointer arithmetics for function FUNC. Otherwise
234 return FALSE. */
236 bool
237 frame_offset_overflow (poly_int64 offset, tree func)
239 poly_uint64 size = FRAME_GROWS_DOWNWARD ? -offset : offset;
240 unsigned HOST_WIDE_INT limit
241 = ((HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (Pmode) - 1))
242 /* Leave room for the fixed part of the frame. */
243 - 64 * UNITS_PER_WORD);
245 if (!coeffs_in_range_p (size, 0U, limit))
247 error_at (DECL_SOURCE_LOCATION (func),
248 "total size of local objects too large");
249 return true;
252 return false;
255 /* Return the minimum spill slot alignment for a register of mode MODE. */
257 unsigned int
258 spill_slot_alignment (machine_mode mode ATTRIBUTE_UNUSED)
260 return STACK_SLOT_ALIGNMENT (NULL_TREE, mode, GET_MODE_ALIGNMENT (mode));
263 /* Return stack slot alignment in bits for TYPE and MODE. */
265 static unsigned int
266 get_stack_local_alignment (tree type, machine_mode mode)
268 unsigned int alignment;
270 if (mode == BLKmode)
271 alignment = BIGGEST_ALIGNMENT;
272 else
273 alignment = GET_MODE_ALIGNMENT (mode);
275 /* Allow the frond-end to (possibly) increase the alignment of this
276 stack slot. */
277 if (! type)
278 type = lang_hooks.types.type_for_mode (mode, 0);
280 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
283 /* Determine whether it is possible to fit a stack slot of size SIZE and
284 alignment ALIGNMENT into an area in the stack frame that starts at
285 frame offset START and has a length of LENGTH. If so, store the frame
286 offset to be used for the stack slot in *POFFSET and return true;
287 return false otherwise. This function will extend the frame size when
288 given a start/length pair that lies at the end of the frame. */
290 static bool
291 try_fit_stack_local (poly_int64 start, poly_int64 length,
292 poly_int64 size, unsigned int alignment,
293 poly_int64_pod *poffset)
295 poly_int64 this_frame_offset;
296 int frame_off, frame_alignment, frame_phase;
298 /* Calculate how many bytes the start of local variables is off from
299 stack alignment. */
300 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
301 frame_off = targetm.starting_frame_offset () % frame_alignment;
302 frame_phase = frame_off ? frame_alignment - frame_off : 0;
304 /* Round the frame offset to the specified alignment. */
306 if (FRAME_GROWS_DOWNWARD)
307 this_frame_offset
308 = (aligned_lower_bound (start + length - size - frame_phase, alignment)
309 + frame_phase);
310 else
311 this_frame_offset
312 = aligned_upper_bound (start - frame_phase, alignment) + frame_phase;
314 /* See if it fits. If this space is at the edge of the frame,
315 consider extending the frame to make it fit. Our caller relies on
316 this when allocating a new slot. */
317 if (maybe_lt (this_frame_offset, start))
319 if (known_eq (frame_offset, start))
320 frame_offset = this_frame_offset;
321 else
322 return false;
324 else if (maybe_gt (this_frame_offset + size, start + length))
326 if (known_eq (frame_offset, start + length))
327 frame_offset = this_frame_offset + size;
328 else
329 return false;
332 *poffset = this_frame_offset;
333 return true;
336 /* Create a new frame_space structure describing free space in the stack
337 frame beginning at START and ending at END, and chain it into the
338 function's frame_space_list. */
340 static void
341 add_frame_space (poly_int64 start, poly_int64 end)
343 struct frame_space *space = ggc_alloc<frame_space> ();
344 space->next = crtl->frame_space_list;
345 crtl->frame_space_list = space;
346 space->start = start;
347 space->length = end - start;
350 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
351 with machine mode MODE.
353 ALIGN controls the amount of alignment for the address of the slot:
354 0 means according to MODE,
355 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
356 -2 means use BITS_PER_UNIT,
357 positive specifies alignment boundary in bits.
359 KIND has ASLK_REDUCE_ALIGN bit set if it is OK to reduce
360 alignment and ASLK_RECORD_PAD bit set if we should remember
361 extra space we allocated for alignment purposes. When we are
362 called from assign_stack_temp_for_type, it is not set so we don't
363 track the same stack slot in two independent lists.
365 We do not round to stack_boundary here. */
368 assign_stack_local_1 (machine_mode mode, poly_int64 size,
369 int align, int kind)
371 rtx x, addr;
372 poly_int64 bigend_correction = 0;
373 poly_int64 slot_offset = 0, old_frame_offset;
374 unsigned int alignment, alignment_in_bits;
376 if (align == 0)
378 alignment = get_stack_local_alignment (NULL, mode);
379 alignment /= BITS_PER_UNIT;
381 else if (align == -1)
383 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
384 size = aligned_upper_bound (size, alignment);
386 else if (align == -2)
387 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
388 else
389 alignment = align / BITS_PER_UNIT;
391 alignment_in_bits = alignment * BITS_PER_UNIT;
393 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
394 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
396 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
397 alignment = alignment_in_bits / BITS_PER_UNIT;
400 if (SUPPORTS_STACK_ALIGNMENT)
402 if (crtl->stack_alignment_estimated < alignment_in_bits)
404 if (!crtl->stack_realign_processed)
405 crtl->stack_alignment_estimated = alignment_in_bits;
406 else
408 /* If stack is realigned and stack alignment value
409 hasn't been finalized, it is OK not to increase
410 stack_alignment_estimated. The bigger alignment
411 requirement is recorded in stack_alignment_needed
412 below. */
413 gcc_assert (!crtl->stack_realign_finalized);
414 if (!crtl->stack_realign_needed)
416 /* It is OK to reduce the alignment as long as the
417 requested size is 0 or the estimated stack
418 alignment >= mode alignment. */
419 gcc_assert ((kind & ASLK_REDUCE_ALIGN)
420 || known_eq (size, 0)
421 || (crtl->stack_alignment_estimated
422 >= GET_MODE_ALIGNMENT (mode)));
423 alignment_in_bits = crtl->stack_alignment_estimated;
424 alignment = alignment_in_bits / BITS_PER_UNIT;
430 if (crtl->stack_alignment_needed < alignment_in_bits)
431 crtl->stack_alignment_needed = alignment_in_bits;
432 if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
433 crtl->max_used_stack_slot_alignment = alignment_in_bits;
435 if (mode != BLKmode || maybe_ne (size, 0))
437 if (kind & ASLK_RECORD_PAD)
439 struct frame_space **psp;
441 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
443 struct frame_space *space = *psp;
444 if (!try_fit_stack_local (space->start, space->length, size,
445 alignment, &slot_offset))
446 continue;
447 *psp = space->next;
448 if (known_gt (slot_offset, space->start))
449 add_frame_space (space->start, slot_offset);
450 if (known_lt (slot_offset + size, space->start + space->length))
451 add_frame_space (slot_offset + size,
452 space->start + space->length);
453 goto found_space;
457 else if (!STACK_ALIGNMENT_NEEDED)
459 slot_offset = frame_offset;
460 goto found_space;
463 old_frame_offset = frame_offset;
465 if (FRAME_GROWS_DOWNWARD)
467 frame_offset -= size;
468 try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
470 if (kind & ASLK_RECORD_PAD)
472 if (known_gt (slot_offset, frame_offset))
473 add_frame_space (frame_offset, slot_offset);
474 if (known_lt (slot_offset + size, old_frame_offset))
475 add_frame_space (slot_offset + size, old_frame_offset);
478 else
480 frame_offset += size;
481 try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
483 if (kind & ASLK_RECORD_PAD)
485 if (known_gt (slot_offset, old_frame_offset))
486 add_frame_space (old_frame_offset, slot_offset);
487 if (known_lt (slot_offset + size, frame_offset))
488 add_frame_space (slot_offset + size, frame_offset);
492 found_space:
493 /* On a big-endian machine, if we are allocating more space than we will use,
494 use the least significant bytes of those that are allocated. */
495 if (mode != BLKmode)
497 /* The slot size can sometimes be smaller than the mode size;
498 e.g. the rs6000 port allocates slots with a vector mode
499 that have the size of only one element. However, the slot
500 size must always be ordered wrt to the mode size, in the
501 same way as for a subreg. */
502 gcc_checking_assert (ordered_p (GET_MODE_SIZE (mode), size));
503 if (BYTES_BIG_ENDIAN && maybe_lt (GET_MODE_SIZE (mode), size))
504 bigend_correction = size - GET_MODE_SIZE (mode);
507 /* If we have already instantiated virtual registers, return the actual
508 address relative to the frame pointer. */
509 if (virtuals_instantiated)
510 addr = plus_constant (Pmode, frame_pointer_rtx,
511 trunc_int_for_mode
512 (slot_offset + bigend_correction
513 + targetm.starting_frame_offset (), Pmode));
514 else
515 addr = plus_constant (Pmode, virtual_stack_vars_rtx,
516 trunc_int_for_mode
517 (slot_offset + bigend_correction,
518 Pmode));
520 x = gen_rtx_MEM (mode, addr);
521 set_mem_align (x, alignment_in_bits);
522 MEM_NOTRAP_P (x) = 1;
524 vec_safe_push (stack_slot_list, x);
526 if (frame_offset_overflow (frame_offset, current_function_decl))
527 frame_offset = 0;
529 return x;
532 /* Wrap up assign_stack_local_1 with last parameter as false. */
535 assign_stack_local (machine_mode mode, poly_int64 size, int align)
537 return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
540 /* In order to evaluate some expressions, such as function calls returning
541 structures in memory, we need to temporarily allocate stack locations.
542 We record each allocated temporary in the following structure.
544 Associated with each temporary slot is a nesting level. When we pop up
545 one level, all temporaries associated with the previous level are freed.
546 Normally, all temporaries are freed after the execution of the statement
547 in which they were created. However, if we are inside a ({...}) grouping,
548 the result may be in a temporary and hence must be preserved. If the
549 result could be in a temporary, we preserve it if we can determine which
550 one it is in. If we cannot determine which temporary may contain the
551 result, all temporaries are preserved. A temporary is preserved by
552 pretending it was allocated at the previous nesting level. */
554 struct GTY(()) temp_slot {
555 /* Points to next temporary slot. */
556 struct temp_slot *next;
557 /* Points to previous temporary slot. */
558 struct temp_slot *prev;
559 /* The rtx to used to reference the slot. */
560 rtx slot;
561 /* The size, in units, of the slot. */
562 poly_int64 size;
563 /* The type of the object in the slot, or zero if it doesn't correspond
564 to a type. We use this to determine whether a slot can be reused.
565 It can be reused if objects of the type of the new slot will always
566 conflict with objects of the type of the old slot. */
567 tree type;
568 /* The alignment (in bits) of the slot. */
569 unsigned int align;
570 /* Nonzero if this temporary is currently in use. */
571 char in_use;
572 /* Nesting level at which this slot is being used. */
573 int level;
574 /* The offset of the slot from the frame_pointer, including extra space
575 for alignment. This info is for combine_temp_slots. */
576 poly_int64 base_offset;
577 /* The size of the slot, including extra space for alignment. This
578 info is for combine_temp_slots. */
579 poly_int64 full_size;
582 /* Entry for the below hash table. */
583 struct GTY((for_user)) temp_slot_address_entry {
584 hashval_t hash;
585 rtx address;
586 struct temp_slot *temp_slot;
589 struct temp_address_hasher : ggc_ptr_hash<temp_slot_address_entry>
591 static hashval_t hash (temp_slot_address_entry *);
592 static bool equal (temp_slot_address_entry *, temp_slot_address_entry *);
595 /* A table of addresses that represent a stack slot. The table is a mapping
596 from address RTXen to a temp slot. */
597 static GTY(()) hash_table<temp_address_hasher> *temp_slot_address_table;
598 static size_t n_temp_slots_in_use;
600 /* Removes temporary slot TEMP from LIST. */
602 static void
603 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
605 if (temp->next)
606 temp->next->prev = temp->prev;
607 if (temp->prev)
608 temp->prev->next = temp->next;
609 else
610 *list = temp->next;
612 temp->prev = temp->next = NULL;
615 /* Inserts temporary slot TEMP to LIST. */
617 static void
618 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
620 temp->next = *list;
621 if (*list)
622 (*list)->prev = temp;
623 temp->prev = NULL;
624 *list = temp;
627 /* Returns the list of used temp slots at LEVEL. */
629 static struct temp_slot **
630 temp_slots_at_level (int level)
632 if (level >= (int) vec_safe_length (used_temp_slots))
633 vec_safe_grow_cleared (used_temp_slots, level + 1);
635 return &(*used_temp_slots)[level];
638 /* Returns the maximal temporary slot level. */
640 static int
641 max_slot_level (void)
643 if (!used_temp_slots)
644 return -1;
646 return used_temp_slots->length () - 1;
649 /* Moves temporary slot TEMP to LEVEL. */
651 static void
652 move_slot_to_level (struct temp_slot *temp, int level)
654 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
655 insert_slot_to_list (temp, temp_slots_at_level (level));
656 temp->level = level;
659 /* Make temporary slot TEMP available. */
661 static void
662 make_slot_available (struct temp_slot *temp)
664 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
665 insert_slot_to_list (temp, &avail_temp_slots);
666 temp->in_use = 0;
667 temp->level = -1;
668 n_temp_slots_in_use--;
671 /* Compute the hash value for an address -> temp slot mapping.
672 The value is cached on the mapping entry. */
673 static hashval_t
674 temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
676 int do_not_record = 0;
677 return hash_rtx (t->address, GET_MODE (t->address),
678 &do_not_record, NULL, false);
681 /* Return the hash value for an address -> temp slot mapping. */
682 hashval_t
683 temp_address_hasher::hash (temp_slot_address_entry *t)
685 return t->hash;
688 /* Compare two address -> temp slot mapping entries. */
689 bool
690 temp_address_hasher::equal (temp_slot_address_entry *t1,
691 temp_slot_address_entry *t2)
693 return exp_equiv_p (t1->address, t2->address, 0, true);
696 /* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
697 static void
698 insert_temp_slot_address (rtx address, struct temp_slot *temp_slot)
700 struct temp_slot_address_entry *t = ggc_alloc<temp_slot_address_entry> ();
701 t->address = address;
702 t->temp_slot = temp_slot;
703 t->hash = temp_slot_address_compute_hash (t);
704 *temp_slot_address_table->find_slot_with_hash (t, t->hash, INSERT) = t;
707 /* Remove an address -> temp slot mapping entry if the temp slot is
708 not in use anymore. Callback for remove_unused_temp_slot_addresses. */
710 remove_unused_temp_slot_addresses_1 (temp_slot_address_entry **slot, void *)
712 const struct temp_slot_address_entry *t = *slot;
713 if (! t->temp_slot->in_use)
714 temp_slot_address_table->clear_slot (slot);
715 return 1;
718 /* Remove all mappings of addresses to unused temp slots. */
719 static void
720 remove_unused_temp_slot_addresses (void)
722 /* Use quicker clearing if there aren't any active temp slots. */
723 if (n_temp_slots_in_use)
724 temp_slot_address_table->traverse
725 <void *, remove_unused_temp_slot_addresses_1> (NULL);
726 else
727 temp_slot_address_table->empty ();
730 /* Find the temp slot corresponding to the object at address X. */
732 static struct temp_slot *
733 find_temp_slot_from_address (rtx x)
735 struct temp_slot *p;
736 struct temp_slot_address_entry tmp, *t;
738 /* First try the easy way:
739 See if X exists in the address -> temp slot mapping. */
740 tmp.address = x;
741 tmp.temp_slot = NULL;
742 tmp.hash = temp_slot_address_compute_hash (&tmp);
743 t = temp_slot_address_table->find_with_hash (&tmp, tmp.hash);
744 if (t)
745 return t->temp_slot;
747 /* If we have a sum involving a register, see if it points to a temp
748 slot. */
749 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
750 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
751 return p;
752 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
753 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
754 return p;
756 /* Last resort: Address is a virtual stack var address. */
757 poly_int64 offset;
758 if (strip_offset (x, &offset) == virtual_stack_vars_rtx)
760 int i;
761 for (i = max_slot_level (); i >= 0; i--)
762 for (p = *temp_slots_at_level (i); p; p = p->next)
763 if (known_in_range_p (offset, p->base_offset, p->full_size))
764 return p;
767 return NULL;
770 /* Allocate a temporary stack slot and record it for possible later
771 reuse.
773 MODE is the machine mode to be given to the returned rtx.
775 SIZE is the size in units of the space required. We do no rounding here
776 since assign_stack_local will do any required rounding.
778 TYPE is the type that will be used for the stack slot. */
781 assign_stack_temp_for_type (machine_mode mode, poly_int64 size, tree type)
783 unsigned int align;
784 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
785 rtx slot;
787 gcc_assert (known_size_p (size));
789 align = get_stack_local_alignment (type, mode);
791 /* Try to find an available, already-allocated temporary of the proper
792 mode which meets the size and alignment requirements. Choose the
793 smallest one with the closest alignment.
795 If assign_stack_temp is called outside of the tree->rtl expansion,
796 we cannot reuse the stack slots (that may still refer to
797 VIRTUAL_STACK_VARS_REGNUM). */
798 if (!virtuals_instantiated)
800 for (p = avail_temp_slots; p; p = p->next)
802 if (p->align >= align
803 && known_ge (p->size, size)
804 && GET_MODE (p->slot) == mode
805 && objects_must_conflict_p (p->type, type)
806 && (best_p == 0
807 || (known_eq (best_p->size, p->size)
808 ? best_p->align > p->align
809 : known_ge (best_p->size, p->size))))
811 if (p->align == align && known_eq (p->size, size))
813 selected = p;
814 cut_slot_from_list (selected, &avail_temp_slots);
815 best_p = 0;
816 break;
818 best_p = p;
823 /* Make our best, if any, the one to use. */
824 if (best_p)
826 selected = best_p;
827 cut_slot_from_list (selected, &avail_temp_slots);
829 /* If there are enough aligned bytes left over, make them into a new
830 temp_slot so that the extra bytes don't get wasted. Do this only
831 for BLKmode slots, so that we can be sure of the alignment. */
832 if (GET_MODE (best_p->slot) == BLKmode)
834 int alignment = best_p->align / BITS_PER_UNIT;
835 poly_int64 rounded_size = aligned_upper_bound (size, alignment);
837 if (known_ge (best_p->size - rounded_size, alignment))
839 p = ggc_alloc<temp_slot> ();
840 p->in_use = 0;
841 p->size = best_p->size - rounded_size;
842 p->base_offset = best_p->base_offset + rounded_size;
843 p->full_size = best_p->full_size - rounded_size;
844 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
845 p->align = best_p->align;
846 p->type = best_p->type;
847 insert_slot_to_list (p, &avail_temp_slots);
849 vec_safe_push (stack_slot_list, p->slot);
851 best_p->size = rounded_size;
852 best_p->full_size = rounded_size;
857 /* If we still didn't find one, make a new temporary. */
858 if (selected == 0)
860 poly_int64 frame_offset_old = frame_offset;
862 p = ggc_alloc<temp_slot> ();
864 /* We are passing an explicit alignment request to assign_stack_local.
865 One side effect of that is assign_stack_local will not round SIZE
866 to ensure the frame offset remains suitably aligned.
868 So for requests which depended on the rounding of SIZE, we go ahead
869 and round it now. We also make sure ALIGNMENT is at least
870 BIGGEST_ALIGNMENT. */
871 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
872 p->slot = assign_stack_local_1 (mode,
873 (mode == BLKmode
874 ? aligned_upper_bound (size,
875 (int) align
876 / BITS_PER_UNIT)
877 : size),
878 align, 0);
880 p->align = align;
882 /* The following slot size computation is necessary because we don't
883 know the actual size of the temporary slot until assign_stack_local
884 has performed all the frame alignment and size rounding for the
885 requested temporary. Note that extra space added for alignment
886 can be either above or below this stack slot depending on which
887 way the frame grows. We include the extra space if and only if it
888 is above this slot. */
889 if (FRAME_GROWS_DOWNWARD)
890 p->size = frame_offset_old - frame_offset;
891 else
892 p->size = size;
894 /* Now define the fields used by combine_temp_slots. */
895 if (FRAME_GROWS_DOWNWARD)
897 p->base_offset = frame_offset;
898 p->full_size = frame_offset_old - frame_offset;
900 else
902 p->base_offset = frame_offset_old;
903 p->full_size = frame_offset - frame_offset_old;
906 selected = p;
909 p = selected;
910 p->in_use = 1;
911 p->type = type;
912 p->level = temp_slot_level;
913 n_temp_slots_in_use++;
915 pp = temp_slots_at_level (p->level);
916 insert_slot_to_list (p, pp);
917 insert_temp_slot_address (XEXP (p->slot, 0), p);
919 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
920 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
921 vec_safe_push (stack_slot_list, slot);
923 /* If we know the alias set for the memory that will be used, use
924 it. If there's no TYPE, then we don't know anything about the
925 alias set for the memory. */
926 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
927 set_mem_align (slot, align);
929 /* If a type is specified, set the relevant flags. */
930 if (type != 0)
931 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
932 MEM_NOTRAP_P (slot) = 1;
934 return slot;
937 /* Allocate a temporary stack slot and record it for possible later
938 reuse. First two arguments are same as in preceding function. */
941 assign_stack_temp (machine_mode mode, poly_int64 size)
943 return assign_stack_temp_for_type (mode, size, NULL_TREE);
946 /* Assign a temporary.
947 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
948 and so that should be used in error messages. In either case, we
949 allocate of the given type.
950 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
951 it is 0 if a register is OK.
952 DONT_PROMOTE is 1 if we should not promote values in register
953 to wider modes. */
956 assign_temp (tree type_or_decl, int memory_required,
957 int dont_promote ATTRIBUTE_UNUSED)
959 tree type, decl;
960 machine_mode mode;
961 #ifdef PROMOTE_MODE
962 int unsignedp;
963 #endif
965 if (DECL_P (type_or_decl))
966 decl = type_or_decl, type = TREE_TYPE (decl);
967 else
968 decl = NULL, type = type_or_decl;
970 mode = TYPE_MODE (type);
971 #ifdef PROMOTE_MODE
972 unsignedp = TYPE_UNSIGNED (type);
973 #endif
975 /* Allocating temporaries of TREE_ADDRESSABLE type must be done in the front
976 end. See also create_tmp_var for the gimplification-time check. */
977 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
979 if (mode == BLKmode || memory_required)
981 poly_int64 size;
982 rtx tmp;
984 /* Unfortunately, we don't yet know how to allocate variable-sized
985 temporaries. However, sometimes we can find a fixed upper limit on
986 the size, so try that instead. */
987 if (!poly_int_tree_p (TYPE_SIZE_UNIT (type), &size))
988 size = max_int_size_in_bytes (type);
990 /* Zero sized arrays are a GNU C extension. Set size to 1 to avoid
991 problems with allocating the stack space. */
992 if (known_eq (size, 0))
993 size = 1;
995 /* The size of the temporary may be too large to fit into an integer. */
996 /* ??? Not sure this should happen except for user silliness, so limit
997 this to things that aren't compiler-generated temporaries. The
998 rest of the time we'll die in assign_stack_temp_for_type. */
999 if (decl
1000 && !known_size_p (size)
1001 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
1003 error ("size of variable %q+D is too large", decl);
1004 size = 1;
1007 tmp = assign_stack_temp_for_type (mode, size, type);
1008 return tmp;
1011 #ifdef PROMOTE_MODE
1012 if (! dont_promote)
1013 mode = promote_mode (type, mode, &unsignedp);
1014 #endif
1016 return gen_reg_rtx (mode);
1019 /* Combine temporary stack slots which are adjacent on the stack.
1021 This allows for better use of already allocated stack space. This is only
1022 done for BLKmode slots because we can be sure that we won't have alignment
1023 problems in this case. */
1025 static void
1026 combine_temp_slots (void)
1028 struct temp_slot *p, *q, *next, *next_q;
1029 int num_slots;
1031 /* We can't combine slots, because the information about which slot
1032 is in which alias set will be lost. */
1033 if (flag_strict_aliasing)
1034 return;
1036 /* If there are a lot of temp slots, don't do anything unless
1037 high levels of optimization. */
1038 if (! flag_expensive_optimizations)
1039 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
1040 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1041 return;
1043 for (p = avail_temp_slots; p; p = next)
1045 int delete_p = 0;
1047 next = p->next;
1049 if (GET_MODE (p->slot) != BLKmode)
1050 continue;
1052 for (q = p->next; q; q = next_q)
1054 int delete_q = 0;
1056 next_q = q->next;
1058 if (GET_MODE (q->slot) != BLKmode)
1059 continue;
1061 if (known_eq (p->base_offset + p->full_size, q->base_offset))
1063 /* Q comes after P; combine Q into P. */
1064 p->size += q->size;
1065 p->full_size += q->full_size;
1066 delete_q = 1;
1068 else if (known_eq (q->base_offset + q->full_size, p->base_offset))
1070 /* P comes after Q; combine P into Q. */
1071 q->size += p->size;
1072 q->full_size += p->full_size;
1073 delete_p = 1;
1074 break;
1076 if (delete_q)
1077 cut_slot_from_list (q, &avail_temp_slots);
1080 /* Either delete P or advance past it. */
1081 if (delete_p)
1082 cut_slot_from_list (p, &avail_temp_slots);
1086 /* Indicate that NEW_RTX is an alternate way of referring to the temp
1087 slot that previously was known by OLD_RTX. */
1089 void
1090 update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1092 struct temp_slot *p;
1094 if (rtx_equal_p (old_rtx, new_rtx))
1095 return;
1097 p = find_temp_slot_from_address (old_rtx);
1099 /* If we didn't find one, see if both OLD_RTX is a PLUS. If so, and
1100 NEW_RTX is a register, see if one operand of the PLUS is a
1101 temporary location. If so, NEW_RTX points into it. Otherwise,
1102 if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1103 in common between them. If so, try a recursive call on those
1104 values. */
1105 if (p == 0)
1107 if (GET_CODE (old_rtx) != PLUS)
1108 return;
1110 if (REG_P (new_rtx))
1112 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1113 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1114 return;
1116 else if (GET_CODE (new_rtx) != PLUS)
1117 return;
1119 if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1120 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1121 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1122 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1123 else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1124 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1125 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1126 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
1128 return;
1131 /* Otherwise add an alias for the temp's address. */
1132 insert_temp_slot_address (new_rtx, p);
1135 /* If X could be a reference to a temporary slot, mark that slot as
1136 belonging to the to one level higher than the current level. If X
1137 matched one of our slots, just mark that one. Otherwise, we can't
1138 easily predict which it is, so upgrade all of them.
1140 This is called when an ({...}) construct occurs and a statement
1141 returns a value in memory. */
1143 void
1144 preserve_temp_slots (rtx x)
1146 struct temp_slot *p = 0, *next;
1148 if (x == 0)
1149 return;
1151 /* If X is a register that is being used as a pointer, see if we have
1152 a temporary slot we know it points to. */
1153 if (REG_P (x) && REG_POINTER (x))
1154 p = find_temp_slot_from_address (x);
1156 /* If X is not in memory or is at a constant address, it cannot be in
1157 a temporary slot. */
1158 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1159 return;
1161 /* First see if we can find a match. */
1162 if (p == 0)
1163 p = find_temp_slot_from_address (XEXP (x, 0));
1165 if (p != 0)
1167 if (p->level == temp_slot_level)
1168 move_slot_to_level (p, temp_slot_level - 1);
1169 return;
1172 /* Otherwise, preserve all non-kept slots at this level. */
1173 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1175 next = p->next;
1176 move_slot_to_level (p, temp_slot_level - 1);
1180 /* Free all temporaries used so far. This is normally called at the
1181 end of generating code for a statement. */
1183 void
1184 free_temp_slots (void)
1186 struct temp_slot *p, *next;
1187 bool some_available = false;
1189 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1191 next = p->next;
1192 make_slot_available (p);
1193 some_available = true;
1196 if (some_available)
1198 remove_unused_temp_slot_addresses ();
1199 combine_temp_slots ();
1203 /* Push deeper into the nesting level for stack temporaries. */
1205 void
1206 push_temp_slots (void)
1208 temp_slot_level++;
1211 /* Pop a temporary nesting level. All slots in use in the current level
1212 are freed. */
1214 void
1215 pop_temp_slots (void)
1217 free_temp_slots ();
1218 temp_slot_level--;
1221 /* Initialize temporary slots. */
1223 void
1224 init_temp_slots (void)
1226 /* We have not allocated any temporaries yet. */
1227 avail_temp_slots = 0;
1228 vec_alloc (used_temp_slots, 0);
1229 temp_slot_level = 0;
1230 n_temp_slots_in_use = 0;
1232 /* Set up the table to map addresses to temp slots. */
1233 if (! temp_slot_address_table)
1234 temp_slot_address_table = hash_table<temp_address_hasher>::create_ggc (32);
1235 else
1236 temp_slot_address_table->empty ();
1239 /* Functions and data structures to keep track of the values hard regs
1240 had at the start of the function. */
1242 /* Private type used by get_hard_reg_initial_reg, get_hard_reg_initial_val,
1243 and has_hard_reg_initial_val.. */
1244 struct GTY(()) initial_value_pair {
1245 rtx hard_reg;
1246 rtx pseudo;
1248 /* ??? This could be a VEC but there is currently no way to define an
1249 opaque VEC type. This could be worked around by defining struct
1250 initial_value_pair in function.h. */
1251 struct GTY(()) initial_value_struct {
1252 int num_entries;
1253 int max_entries;
1254 initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
1257 /* If a pseudo represents an initial hard reg (or expression), return
1258 it, else return NULL_RTX. */
1261 get_hard_reg_initial_reg (rtx reg)
1263 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1264 int i;
1266 if (ivs == 0)
1267 return NULL_RTX;
1269 for (i = 0; i < ivs->num_entries; i++)
1270 if (rtx_equal_p (ivs->entries[i].pseudo, reg))
1271 return ivs->entries[i].hard_reg;
1273 return NULL_RTX;
1276 /* Make sure that there's a pseudo register of mode MODE that stores the
1277 initial value of hard register REGNO. Return an rtx for such a pseudo. */
1280 get_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1282 struct initial_value_struct *ivs;
1283 rtx rv;
1285 rv = has_hard_reg_initial_val (mode, regno);
1286 if (rv)
1287 return rv;
1289 ivs = crtl->hard_reg_initial_vals;
1290 if (ivs == 0)
1292 ivs = ggc_alloc<initial_value_struct> ();
1293 ivs->num_entries = 0;
1294 ivs->max_entries = 5;
1295 ivs->entries = ggc_vec_alloc<initial_value_pair> (5);
1296 crtl->hard_reg_initial_vals = ivs;
1299 if (ivs->num_entries >= ivs->max_entries)
1301 ivs->max_entries += 5;
1302 ivs->entries = GGC_RESIZEVEC (initial_value_pair, ivs->entries,
1303 ivs->max_entries);
1306 ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
1307 ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
1309 return ivs->entries[ivs->num_entries++].pseudo;
1312 /* See if get_hard_reg_initial_val has been used to create a pseudo
1313 for the initial value of hard register REGNO in mode MODE. Return
1314 the associated pseudo if so, otherwise return NULL. */
1317 has_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1319 struct initial_value_struct *ivs;
1320 int i;
1322 ivs = crtl->hard_reg_initial_vals;
1323 if (ivs != 0)
1324 for (i = 0; i < ivs->num_entries; i++)
1325 if (GET_MODE (ivs->entries[i].hard_reg) == mode
1326 && REGNO (ivs->entries[i].hard_reg) == regno)
1327 return ivs->entries[i].pseudo;
1329 return NULL_RTX;
1332 unsigned int
1333 emit_initial_value_sets (void)
1335 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1336 int i;
1337 rtx_insn *seq;
1339 if (ivs == 0)
1340 return 0;
1342 start_sequence ();
1343 for (i = 0; i < ivs->num_entries; i++)
1344 emit_move_insn (ivs->entries[i].pseudo, ivs->entries[i].hard_reg);
1345 seq = get_insns ();
1346 end_sequence ();
1348 emit_insn_at_entry (seq);
1349 return 0;
1352 /* Return the hardreg-pseudoreg initial values pair entry I and
1353 TRUE if I is a valid entry, or FALSE if I is not a valid entry. */
1354 bool
1355 initial_value_entry (int i, rtx *hreg, rtx *preg)
1357 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1358 if (!ivs || i >= ivs->num_entries)
1359 return false;
1361 *hreg = ivs->entries[i].hard_reg;
1362 *preg = ivs->entries[i].pseudo;
1363 return true;
1366 /* These routines are responsible for converting virtual register references
1367 to the actual hard register references once RTL generation is complete.
1369 The following four variables are used for communication between the
1370 routines. They contain the offsets of the virtual registers from their
1371 respective hard registers. */
1373 static poly_int64 in_arg_offset;
1374 static poly_int64 var_offset;
1375 static poly_int64 dynamic_offset;
1376 static poly_int64 out_arg_offset;
1377 static poly_int64 cfa_offset;
1379 /* In most machines, the stack pointer register is equivalent to the bottom
1380 of the stack. */
1382 #ifndef STACK_POINTER_OFFSET
1383 #define STACK_POINTER_OFFSET 0
1384 #endif
1386 #if defined (REG_PARM_STACK_SPACE) && !defined (INCOMING_REG_PARM_STACK_SPACE)
1387 #define INCOMING_REG_PARM_STACK_SPACE REG_PARM_STACK_SPACE
1388 #endif
1390 /* If not defined, pick an appropriate default for the offset of dynamically
1391 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1392 INCOMING_REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1394 #ifndef STACK_DYNAMIC_OFFSET
1396 /* The bottom of the stack points to the actual arguments. If
1397 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1398 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1399 stack space for register parameters is not pushed by the caller, but
1400 rather part of the fixed stack areas and hence not included in
1401 `crtl->outgoing_args_size'. Nevertheless, we must allow
1402 for it when allocating stack dynamic objects. */
1404 #ifdef INCOMING_REG_PARM_STACK_SPACE
1405 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1406 ((ACCUMULATE_OUTGOING_ARGS \
1407 ? (crtl->outgoing_args_size \
1408 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1409 : INCOMING_REG_PARM_STACK_SPACE (FNDECL))) \
1410 : 0) + (STACK_POINTER_OFFSET))
1411 #else
1412 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1413 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : poly_int64 (0)) \
1414 + (STACK_POINTER_OFFSET))
1415 #endif
1416 #endif
1419 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1420 is a virtual register, return the equivalent hard register and set the
1421 offset indirectly through the pointer. Otherwise, return 0. */
1423 static rtx
1424 instantiate_new_reg (rtx x, poly_int64_pod *poffset)
1426 rtx new_rtx;
1427 poly_int64 offset;
1429 if (x == virtual_incoming_args_rtx)
1431 if (stack_realign_drap)
1433 /* Replace virtual_incoming_args_rtx with internal arg
1434 pointer if DRAP is used to realign stack. */
1435 new_rtx = crtl->args.internal_arg_pointer;
1436 offset = 0;
1438 else
1439 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1441 else if (x == virtual_stack_vars_rtx)
1442 new_rtx = frame_pointer_rtx, offset = var_offset;
1443 else if (x == virtual_stack_dynamic_rtx)
1444 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1445 else if (x == virtual_outgoing_args_rtx)
1446 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1447 else if (x == virtual_cfa_rtx)
1449 #ifdef FRAME_POINTER_CFA_OFFSET
1450 new_rtx = frame_pointer_rtx;
1451 #else
1452 new_rtx = arg_pointer_rtx;
1453 #endif
1454 offset = cfa_offset;
1456 else if (x == virtual_preferred_stack_boundary_rtx)
1458 new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1459 offset = 0;
1461 else
1462 return NULL_RTX;
1464 *poffset = offset;
1465 return new_rtx;
1468 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1469 registers present inside of *LOC. The expression is simplified,
1470 as much as possible, but is not to be considered "valid" in any sense
1471 implied by the target. Return true if any change is made. */
1473 static bool
1474 instantiate_virtual_regs_in_rtx (rtx *loc)
1476 if (!*loc)
1477 return false;
1478 bool changed = false;
1479 subrtx_ptr_iterator::array_type array;
1480 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
1482 rtx *loc = *iter;
1483 if (rtx x = *loc)
1485 rtx new_rtx;
1486 poly_int64 offset;
1487 switch (GET_CODE (x))
1489 case REG:
1490 new_rtx = instantiate_new_reg (x, &offset);
1491 if (new_rtx)
1493 *loc = plus_constant (GET_MODE (x), new_rtx, offset);
1494 changed = true;
1496 iter.skip_subrtxes ();
1497 break;
1499 case PLUS:
1500 new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1501 if (new_rtx)
1503 XEXP (x, 0) = new_rtx;
1504 *loc = plus_constant (GET_MODE (x), x, offset, true);
1505 changed = true;
1506 iter.skip_subrtxes ();
1507 break;
1510 /* FIXME -- from old code */
1511 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1512 we can commute the PLUS and SUBREG because pointers into the
1513 frame are well-behaved. */
1514 break;
1516 default:
1517 break;
1521 return changed;
1524 /* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1525 matches the predicate for insn CODE operand OPERAND. */
1527 static int
1528 safe_insn_predicate (int code, int operand, rtx x)
1530 return code < 0 || insn_operand_matches ((enum insn_code) code, operand, x);
1533 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1534 registers present inside of insn. The result will be a valid insn. */
1536 static void
1537 instantiate_virtual_regs_in_insn (rtx_insn *insn)
1539 poly_int64 offset;
1540 int insn_code, i;
1541 bool any_change = false;
1542 rtx set, new_rtx, x;
1543 rtx_insn *seq;
1545 /* There are some special cases to be handled first. */
1546 set = single_set (insn);
1547 if (set)
1549 /* We're allowed to assign to a virtual register. This is interpreted
1550 to mean that the underlying register gets assigned the inverse
1551 transformation. This is used, for example, in the handling of
1552 non-local gotos. */
1553 new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1554 if (new_rtx)
1556 start_sequence ();
1558 instantiate_virtual_regs_in_rtx (&SET_SRC (set));
1559 x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
1560 gen_int_mode (-offset, GET_MODE (new_rtx)));
1561 x = force_operand (x, new_rtx);
1562 if (x != new_rtx)
1563 emit_move_insn (new_rtx, x);
1565 seq = get_insns ();
1566 end_sequence ();
1568 emit_insn_before (seq, insn);
1569 delete_insn (insn);
1570 return;
1573 /* Handle a straight copy from a virtual register by generating a
1574 new add insn. The difference between this and falling through
1575 to the generic case is avoiding a new pseudo and eliminating a
1576 move insn in the initial rtl stream. */
1577 new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1578 if (new_rtx
1579 && maybe_ne (offset, 0)
1580 && REG_P (SET_DEST (set))
1581 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1583 start_sequence ();
1585 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS, new_rtx,
1586 gen_int_mode (offset,
1587 GET_MODE (SET_DEST (set))),
1588 SET_DEST (set), 1, OPTAB_LIB_WIDEN);
1589 if (x != SET_DEST (set))
1590 emit_move_insn (SET_DEST (set), x);
1592 seq = get_insns ();
1593 end_sequence ();
1595 emit_insn_before (seq, insn);
1596 delete_insn (insn);
1597 return;
1600 extract_insn (insn);
1601 insn_code = INSN_CODE (insn);
1603 /* Handle a plus involving a virtual register by determining if the
1604 operands remain valid if they're modified in place. */
1605 poly_int64 delta;
1606 if (GET_CODE (SET_SRC (set)) == PLUS
1607 && recog_data.n_operands >= 3
1608 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1609 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1610 && poly_int_rtx_p (recog_data.operand[2], &delta)
1611 && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
1613 offset += delta;
1615 /* If the sum is zero, then replace with a plain move. */
1616 if (known_eq (offset, 0)
1617 && REG_P (SET_DEST (set))
1618 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1620 start_sequence ();
1621 emit_move_insn (SET_DEST (set), new_rtx);
1622 seq = get_insns ();
1623 end_sequence ();
1625 emit_insn_before (seq, insn);
1626 delete_insn (insn);
1627 return;
1630 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1632 /* Using validate_change and apply_change_group here leaves
1633 recog_data in an invalid state. Since we know exactly what
1634 we want to check, do those two by hand. */
1635 if (safe_insn_predicate (insn_code, 1, new_rtx)
1636 && safe_insn_predicate (insn_code, 2, x))
1638 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1639 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1640 any_change = true;
1642 /* Fall through into the regular operand fixup loop in
1643 order to take care of operands other than 1 and 2. */
1647 else
1649 extract_insn (insn);
1650 insn_code = INSN_CODE (insn);
1653 /* In the general case, we expect virtual registers to appear only in
1654 operands, and then only as either bare registers or inside memories. */
1655 for (i = 0; i < recog_data.n_operands; ++i)
1657 x = recog_data.operand[i];
1658 switch (GET_CODE (x))
1660 case MEM:
1662 rtx addr = XEXP (x, 0);
1664 if (!instantiate_virtual_regs_in_rtx (&addr))
1665 continue;
1667 start_sequence ();
1668 x = replace_equiv_address (x, addr, true);
1669 /* It may happen that the address with the virtual reg
1670 was valid (e.g. based on the virtual stack reg, which might
1671 be acceptable to the predicates with all offsets), whereas
1672 the address now isn't anymore, for instance when the address
1673 is still offsetted, but the base reg isn't virtual-stack-reg
1674 anymore. Below we would do a force_reg on the whole operand,
1675 but this insn might actually only accept memory. Hence,
1676 before doing that last resort, try to reload the address into
1677 a register, so this operand stays a MEM. */
1678 if (!safe_insn_predicate (insn_code, i, x))
1680 addr = force_reg (GET_MODE (addr), addr);
1681 x = replace_equiv_address (x, addr, true);
1683 seq = get_insns ();
1684 end_sequence ();
1685 if (seq)
1686 emit_insn_before (seq, insn);
1688 break;
1690 case REG:
1691 new_rtx = instantiate_new_reg (x, &offset);
1692 if (new_rtx == NULL)
1693 continue;
1694 if (known_eq (offset, 0))
1695 x = new_rtx;
1696 else
1698 start_sequence ();
1700 /* Careful, special mode predicates may have stuff in
1701 insn_data[insn_code].operand[i].mode that isn't useful
1702 to us for computing a new value. */
1703 /* ??? Recognize address_operand and/or "p" constraints
1704 to see if (plus new offset) is a valid before we put
1705 this through expand_simple_binop. */
1706 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1707 gen_int_mode (offset, GET_MODE (x)),
1708 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1709 seq = get_insns ();
1710 end_sequence ();
1711 emit_insn_before (seq, insn);
1713 break;
1715 case SUBREG:
1716 new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1717 if (new_rtx == NULL)
1718 continue;
1719 if (maybe_ne (offset, 0))
1721 start_sequence ();
1722 new_rtx = expand_simple_binop
1723 (GET_MODE (new_rtx), PLUS, new_rtx,
1724 gen_int_mode (offset, GET_MODE (new_rtx)),
1725 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1726 seq = get_insns ();
1727 end_sequence ();
1728 emit_insn_before (seq, insn);
1730 x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1731 GET_MODE (new_rtx), SUBREG_BYTE (x));
1732 gcc_assert (x);
1733 break;
1735 default:
1736 continue;
1739 /* At this point, X contains the new value for the operand.
1740 Validate the new value vs the insn predicate. Note that
1741 asm insns will have insn_code -1 here. */
1742 if (!safe_insn_predicate (insn_code, i, x))
1744 start_sequence ();
1745 if (REG_P (x))
1747 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1748 x = copy_to_reg (x);
1750 else
1751 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1752 seq = get_insns ();
1753 end_sequence ();
1754 if (seq)
1755 emit_insn_before (seq, insn);
1758 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1759 any_change = true;
1762 if (any_change)
1764 /* Propagate operand changes into the duplicates. */
1765 for (i = 0; i < recog_data.n_dups; ++i)
1766 *recog_data.dup_loc[i]
1767 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1769 /* Force re-recognition of the instruction for validation. */
1770 INSN_CODE (insn) = -1;
1773 if (asm_noperands (PATTERN (insn)) >= 0)
1775 if (!check_asm_operands (PATTERN (insn)))
1777 error_for_asm (insn, "impossible constraint in %<asm%>");
1778 /* For asm goto, instead of fixing up all the edges
1779 just clear the template and clear input operands
1780 (asm goto doesn't have any output operands). */
1781 if (JUMP_P (insn))
1783 rtx asm_op = extract_asm_operands (PATTERN (insn));
1784 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1785 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1786 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1788 else
1789 delete_insn (insn);
1792 else
1794 if (recog_memoized (insn) < 0)
1795 fatal_insn_not_found (insn);
1799 /* Subroutine of instantiate_decls. Given RTL representing a decl,
1800 do any instantiation required. */
1802 void
1803 instantiate_decl_rtl (rtx x)
1805 rtx addr;
1807 if (x == 0)
1808 return;
1810 /* If this is a CONCAT, recurse for the pieces. */
1811 if (GET_CODE (x) == CONCAT)
1813 instantiate_decl_rtl (XEXP (x, 0));
1814 instantiate_decl_rtl (XEXP (x, 1));
1815 return;
1818 /* If this is not a MEM, no need to do anything. Similarly if the
1819 address is a constant or a register that is not a virtual register. */
1820 if (!MEM_P (x))
1821 return;
1823 addr = XEXP (x, 0);
1824 if (CONSTANT_P (addr)
1825 || (REG_P (addr)
1826 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1827 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1828 return;
1830 instantiate_virtual_regs_in_rtx (&XEXP (x, 0));
1833 /* Helper for instantiate_decls called via walk_tree: Process all decls
1834 in the given DECL_VALUE_EXPR. */
1836 static tree
1837 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1839 tree t = *tp;
1840 if (! EXPR_P (t))
1842 *walk_subtrees = 0;
1843 if (DECL_P (t))
1845 if (DECL_RTL_SET_P (t))
1846 instantiate_decl_rtl (DECL_RTL (t));
1847 if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1848 && DECL_INCOMING_RTL (t))
1849 instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1850 if ((VAR_P (t) || TREE_CODE (t) == RESULT_DECL)
1851 && DECL_HAS_VALUE_EXPR_P (t))
1853 tree v = DECL_VALUE_EXPR (t);
1854 walk_tree (&v, instantiate_expr, NULL, NULL);
1858 return NULL;
1861 /* Subroutine of instantiate_decls: Process all decls in the given
1862 BLOCK node and all its subblocks. */
1864 static void
1865 instantiate_decls_1 (tree let)
1867 tree t;
1869 for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1871 if (DECL_RTL_SET_P (t))
1872 instantiate_decl_rtl (DECL_RTL (t));
1873 if (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
1875 tree v = DECL_VALUE_EXPR (t);
1876 walk_tree (&v, instantiate_expr, NULL, NULL);
1880 /* Process all subblocks. */
1881 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1882 instantiate_decls_1 (t);
1885 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1886 all virtual registers in their DECL_RTL's. */
1888 static void
1889 instantiate_decls (tree fndecl)
1891 tree decl;
1892 unsigned ix;
1894 /* Process all parameters of the function. */
1895 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1897 instantiate_decl_rtl (DECL_RTL (decl));
1898 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1899 if (DECL_HAS_VALUE_EXPR_P (decl))
1901 tree v = DECL_VALUE_EXPR (decl);
1902 walk_tree (&v, instantiate_expr, NULL, NULL);
1906 if ((decl = DECL_RESULT (fndecl))
1907 && TREE_CODE (decl) == RESULT_DECL)
1909 if (DECL_RTL_SET_P (decl))
1910 instantiate_decl_rtl (DECL_RTL (decl));
1911 if (DECL_HAS_VALUE_EXPR_P (decl))
1913 tree v = DECL_VALUE_EXPR (decl);
1914 walk_tree (&v, instantiate_expr, NULL, NULL);
1918 /* Process the saved static chain if it exists. */
1919 decl = DECL_STRUCT_FUNCTION (fndecl)->static_chain_decl;
1920 if (decl && DECL_HAS_VALUE_EXPR_P (decl))
1921 instantiate_decl_rtl (DECL_RTL (DECL_VALUE_EXPR (decl)));
1923 /* Now process all variables defined in the function or its subblocks. */
1924 if (DECL_INITIAL (fndecl))
1925 instantiate_decls_1 (DECL_INITIAL (fndecl));
1927 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1928 if (DECL_RTL_SET_P (decl))
1929 instantiate_decl_rtl (DECL_RTL (decl));
1930 vec_free (cfun->local_decls);
1933 /* Pass through the INSNS of function FNDECL and convert virtual register
1934 references to hard register references. */
1936 static unsigned int
1937 instantiate_virtual_regs (void)
1939 rtx_insn *insn;
1941 /* Compute the offsets to use for this function. */
1942 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1943 var_offset = targetm.starting_frame_offset ();
1944 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1945 out_arg_offset = STACK_POINTER_OFFSET;
1946 #ifdef FRAME_POINTER_CFA_OFFSET
1947 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1948 #else
1949 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1950 #endif
1952 /* Initialize recognition, indicating that volatile is OK. */
1953 init_recog ();
1955 /* Scan through all the insns, instantiating every virtual register still
1956 present. */
1957 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1958 if (INSN_P (insn))
1960 /* These patterns in the instruction stream can never be recognized.
1961 Fortunately, they shouldn't contain virtual registers either. */
1962 if (GET_CODE (PATTERN (insn)) == USE
1963 || GET_CODE (PATTERN (insn)) == CLOBBER
1964 || GET_CODE (PATTERN (insn)) == ASM_INPUT
1965 || DEBUG_MARKER_INSN_P (insn))
1966 continue;
1967 else if (DEBUG_BIND_INSN_P (insn))
1968 instantiate_virtual_regs_in_rtx (INSN_VAR_LOCATION_PTR (insn));
1969 else
1970 instantiate_virtual_regs_in_insn (insn);
1972 if (insn->deleted ())
1973 continue;
1975 instantiate_virtual_regs_in_rtx (&REG_NOTES (insn));
1977 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
1978 if (CALL_P (insn))
1979 instantiate_virtual_regs_in_rtx (&CALL_INSN_FUNCTION_USAGE (insn));
1982 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
1983 instantiate_decls (current_function_decl);
1985 targetm.instantiate_decls ();
1987 /* Indicate that, from now on, assign_stack_local should use
1988 frame_pointer_rtx. */
1989 virtuals_instantiated = 1;
1991 return 0;
1994 namespace {
1996 const pass_data pass_data_instantiate_virtual_regs =
1998 RTL_PASS, /* type */
1999 "vregs", /* name */
2000 OPTGROUP_NONE, /* optinfo_flags */
2001 TV_NONE, /* tv_id */
2002 0, /* properties_required */
2003 0, /* properties_provided */
2004 0, /* properties_destroyed */
2005 0, /* todo_flags_start */
2006 0, /* todo_flags_finish */
2009 class pass_instantiate_virtual_regs : public rtl_opt_pass
2011 public:
2012 pass_instantiate_virtual_regs (gcc::context *ctxt)
2013 : rtl_opt_pass (pass_data_instantiate_virtual_regs, ctxt)
2016 /* opt_pass methods: */
2017 virtual unsigned int execute (function *)
2019 return instantiate_virtual_regs ();
2022 }; // class pass_instantiate_virtual_regs
2024 } // anon namespace
2026 rtl_opt_pass *
2027 make_pass_instantiate_virtual_regs (gcc::context *ctxt)
2029 return new pass_instantiate_virtual_regs (ctxt);
2033 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
2034 This means a type for which function calls must pass an address to the
2035 function or get an address back from the function.
2036 EXP may be a type node or an expression (whose type is tested). */
2039 aggregate_value_p (const_tree exp, const_tree fntype)
2041 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
2042 int i, regno, nregs;
2043 rtx reg;
2045 if (fntype)
2046 switch (TREE_CODE (fntype))
2048 case CALL_EXPR:
2050 tree fndecl = get_callee_fndecl (fntype);
2051 if (fndecl)
2052 fntype = TREE_TYPE (fndecl);
2053 else if (CALL_EXPR_FN (fntype))
2054 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype)));
2055 else
2056 /* For internal functions, assume nothing needs to be
2057 returned in memory. */
2058 return 0;
2060 break;
2061 case FUNCTION_DECL:
2062 fntype = TREE_TYPE (fntype);
2063 break;
2064 case FUNCTION_TYPE:
2065 case METHOD_TYPE:
2066 break;
2067 case IDENTIFIER_NODE:
2068 fntype = NULL_TREE;
2069 break;
2070 default:
2071 /* We don't expect other tree types here. */
2072 gcc_unreachable ();
2075 if (VOID_TYPE_P (type))
2076 return 0;
2078 /* If a record should be passed the same as its first (and only) member
2079 don't pass it as an aggregate. */
2080 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2081 return aggregate_value_p (first_field (type), fntype);
2083 /* If the front end has decided that this needs to be passed by
2084 reference, do so. */
2085 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2086 && DECL_BY_REFERENCE (exp))
2087 return 1;
2089 /* Function types that are TREE_ADDRESSABLE force return in memory. */
2090 if (fntype && TREE_ADDRESSABLE (fntype))
2091 return 1;
2093 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2094 and thus can't be returned in registers. */
2095 if (TREE_ADDRESSABLE (type))
2096 return 1;
2098 if (TYPE_EMPTY_P (type))
2099 return 0;
2101 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2102 return 1;
2104 if (targetm.calls.return_in_memory (type, fntype))
2105 return 1;
2107 /* Make sure we have suitable call-clobbered regs to return
2108 the value in; if not, we must return it in memory. */
2109 reg = hard_function_value (type, 0, fntype, 0);
2111 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2112 it is OK. */
2113 if (!REG_P (reg))
2114 return 0;
2116 regno = REGNO (reg);
2117 nregs = hard_regno_nregs (regno, TYPE_MODE (type));
2118 for (i = 0; i < nregs; i++)
2119 if (! call_used_regs[regno + i])
2120 return 1;
2122 return 0;
2125 /* Return true if we should assign DECL a pseudo register; false if it
2126 should live on the local stack. */
2128 bool
2129 use_register_for_decl (const_tree decl)
2131 if (TREE_CODE (decl) == SSA_NAME)
2133 /* We often try to use the SSA_NAME, instead of its underlying
2134 decl, to get type information and guide decisions, to avoid
2135 differences of behavior between anonymous and named
2136 variables, but in this one case we have to go for the actual
2137 variable if there is one. The main reason is that, at least
2138 at -O0, we want to place user variables on the stack, but we
2139 don't mind using pseudos for anonymous or ignored temps.
2140 Should we take the SSA_NAME, we'd conclude all SSA_NAMEs
2141 should go in pseudos, whereas their corresponding variables
2142 might have to go on the stack. So, disregarding the decl
2143 here would negatively impact debug info at -O0, enable
2144 coalescing between SSA_NAMEs that ought to get different
2145 stack/pseudo assignments, and get the incoming argument
2146 processing thoroughly confused by PARM_DECLs expected to live
2147 in stack slots but assigned to pseudos. */
2148 if (!SSA_NAME_VAR (decl))
2149 return TYPE_MODE (TREE_TYPE (decl)) != BLKmode
2150 && !(flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)));
2152 decl = SSA_NAME_VAR (decl);
2155 /* Honor volatile. */
2156 if (TREE_SIDE_EFFECTS (decl))
2157 return false;
2159 /* Honor addressability. */
2160 if (TREE_ADDRESSABLE (decl))
2161 return false;
2163 /* RESULT_DECLs are a bit special in that they're assigned without
2164 regard to use_register_for_decl, but we generally only store in
2165 them. If we coalesce their SSA NAMEs, we'd better return a
2166 result that matches the assignment in expand_function_start. */
2167 if (TREE_CODE (decl) == RESULT_DECL)
2169 /* If it's not an aggregate, we're going to use a REG or a
2170 PARALLEL containing a REG. */
2171 if (!aggregate_value_p (decl, current_function_decl))
2172 return true;
2174 /* If expand_function_start determines the return value, we'll
2175 use MEM if it's not by reference. */
2176 if (cfun->returns_pcc_struct
2177 || (targetm.calls.struct_value_rtx
2178 (TREE_TYPE (current_function_decl), 1)))
2179 return DECL_BY_REFERENCE (decl);
2181 /* Otherwise, we're taking an extra all.function_result_decl
2182 argument. It's set up in assign_parms_augmented_arg_list,
2183 under the (negated) conditions above, and then it's used to
2184 set up the RESULT_DECL rtl in assign_params, after looping
2185 over all parameters. Now, if the RESULT_DECL is not by
2186 reference, we'll use a MEM either way. */
2187 if (!DECL_BY_REFERENCE (decl))
2188 return false;
2190 /* Otherwise, if RESULT_DECL is DECL_BY_REFERENCE, it will take
2191 the function_result_decl's assignment. Since it's a pointer,
2192 we can short-circuit a number of the tests below, and we must
2193 duplicat e them because we don't have the
2194 function_result_decl to test. */
2195 if (!targetm.calls.allocate_stack_slots_for_args ())
2196 return true;
2197 /* We don't set DECL_IGNORED_P for the function_result_decl. */
2198 if (optimize)
2199 return true;
2200 /* We don't set DECL_REGISTER for the function_result_decl. */
2201 return false;
2204 /* Decl is implicitly addressible by bound stores and loads
2205 if it is an aggregate holding bounds. */
2206 if (chkp_function_instrumented_p (current_function_decl)
2207 && TREE_TYPE (decl)
2208 && !BOUNDED_P (decl)
2209 && chkp_type_has_pointer (TREE_TYPE (decl)))
2210 return false;
2212 /* Only register-like things go in registers. */
2213 if (DECL_MODE (decl) == BLKmode)
2214 return false;
2216 /* If -ffloat-store specified, don't put explicit float variables
2217 into registers. */
2218 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2219 propagates values across these stores, and it probably shouldn't. */
2220 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2221 return false;
2223 if (!targetm.calls.allocate_stack_slots_for_args ())
2224 return true;
2226 /* If we're not interested in tracking debugging information for
2227 this decl, then we can certainly put it in a register. */
2228 if (DECL_IGNORED_P (decl))
2229 return true;
2231 if (optimize)
2232 return true;
2234 if (!DECL_REGISTER (decl))
2235 return false;
2237 /* When not optimizing, disregard register keyword for types that
2238 could have methods, otherwise the methods won't be callable from
2239 the debugger. */
2240 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
2241 return false;
2243 return true;
2246 /* Structures to communicate between the subroutines of assign_parms.
2247 The first holds data persistent across all parameters, the second
2248 is cleared out for each parameter. */
2250 struct assign_parm_data_all
2252 /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2253 should become a job of the target or otherwise encapsulated. */
2254 CUMULATIVE_ARGS args_so_far_v;
2255 cumulative_args_t args_so_far;
2256 struct args_size stack_args_size;
2257 tree function_result_decl;
2258 tree orig_fnargs;
2259 rtx_insn *first_conversion_insn;
2260 rtx_insn *last_conversion_insn;
2261 HOST_WIDE_INT pretend_args_size;
2262 HOST_WIDE_INT extra_pretend_bytes;
2263 int reg_parm_stack_space;
2266 struct assign_parm_data_one
2268 tree nominal_type;
2269 tree passed_type;
2270 rtx entry_parm;
2271 rtx stack_parm;
2272 machine_mode nominal_mode;
2273 machine_mode passed_mode;
2274 machine_mode promoted_mode;
2275 struct locate_and_pad_arg_data locate;
2276 int partial;
2277 BOOL_BITFIELD named_arg : 1;
2278 BOOL_BITFIELD passed_pointer : 1;
2279 BOOL_BITFIELD on_stack : 1;
2280 BOOL_BITFIELD loaded_in_reg : 1;
2283 struct bounds_parm_data
2285 assign_parm_data_one parm_data;
2286 tree bounds_parm;
2287 tree ptr_parm;
2288 rtx ptr_entry;
2289 int bound_no;
2292 /* A subroutine of assign_parms. Initialize ALL. */
2294 static void
2295 assign_parms_initialize_all (struct assign_parm_data_all *all)
2297 tree fntype ATTRIBUTE_UNUSED;
2299 memset (all, 0, sizeof (*all));
2301 fntype = TREE_TYPE (current_function_decl);
2303 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
2304 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2305 #else
2306 INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2307 current_function_decl, -1);
2308 #endif
2309 all->args_so_far = pack_cumulative_args (&all->args_so_far_v);
2311 #ifdef INCOMING_REG_PARM_STACK_SPACE
2312 all->reg_parm_stack_space
2313 = INCOMING_REG_PARM_STACK_SPACE (current_function_decl);
2314 #endif
2317 /* If ARGS contains entries with complex types, split the entry into two
2318 entries of the component type. Return a new list of substitutions are
2319 needed, else the old list. */
2321 static void
2322 split_complex_args (vec<tree> *args)
2324 unsigned i;
2325 tree p;
2327 FOR_EACH_VEC_ELT (*args, i, p)
2329 tree type = TREE_TYPE (p);
2330 if (TREE_CODE (type) == COMPLEX_TYPE
2331 && targetm.calls.split_complex_arg (type))
2333 tree decl;
2334 tree subtype = TREE_TYPE (type);
2335 bool addressable = TREE_ADDRESSABLE (p);
2337 /* Rewrite the PARM_DECL's type with its component. */
2338 p = copy_node (p);
2339 TREE_TYPE (p) = subtype;
2340 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2341 SET_DECL_MODE (p, VOIDmode);
2342 DECL_SIZE (p) = NULL;
2343 DECL_SIZE_UNIT (p) = NULL;
2344 /* If this arg must go in memory, put it in a pseudo here.
2345 We can't allow it to go in memory as per normal parms,
2346 because the usual place might not have the imag part
2347 adjacent to the real part. */
2348 DECL_ARTIFICIAL (p) = addressable;
2349 DECL_IGNORED_P (p) = addressable;
2350 TREE_ADDRESSABLE (p) = 0;
2351 layout_decl (p, 0);
2352 (*args)[i] = p;
2354 /* Build a second synthetic decl. */
2355 decl = build_decl (EXPR_LOCATION (p),
2356 PARM_DECL, NULL_TREE, subtype);
2357 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2358 DECL_ARTIFICIAL (decl) = addressable;
2359 DECL_IGNORED_P (decl) = addressable;
2360 layout_decl (decl, 0);
2361 args->safe_insert (++i, decl);
2366 /* A subroutine of assign_parms. Adjust the parameter list to incorporate
2367 the hidden struct return argument, and (abi willing) complex args.
2368 Return the new parameter list. */
2370 static vec<tree>
2371 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2373 tree fndecl = current_function_decl;
2374 tree fntype = TREE_TYPE (fndecl);
2375 vec<tree> fnargs = vNULL;
2376 tree arg;
2378 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2379 fnargs.safe_push (arg);
2381 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2383 /* If struct value address is treated as the first argument, make it so. */
2384 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2385 && ! cfun->returns_pcc_struct
2386 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2388 tree type = build_pointer_type (TREE_TYPE (fntype));
2389 tree decl;
2391 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2392 PARM_DECL, get_identifier (".result_ptr"), type);
2393 DECL_ARG_TYPE (decl) = type;
2394 DECL_ARTIFICIAL (decl) = 1;
2395 DECL_NAMELESS (decl) = 1;
2396 TREE_CONSTANT (decl) = 1;
2397 /* We don't set DECL_IGNORED_P or DECL_REGISTER here. If this
2398 changes, the end of the RESULT_DECL handling block in
2399 use_register_for_decl must be adjusted to match. */
2401 DECL_CHAIN (decl) = all->orig_fnargs;
2402 all->orig_fnargs = decl;
2403 fnargs.safe_insert (0, decl);
2405 all->function_result_decl = decl;
2407 /* If function is instrumented then bounds of the
2408 passed structure address is the second argument. */
2409 if (chkp_function_instrumented_p (fndecl))
2411 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2412 PARM_DECL, get_identifier (".result_bnd"),
2413 pointer_bounds_type_node);
2414 DECL_ARG_TYPE (decl) = pointer_bounds_type_node;
2415 DECL_ARTIFICIAL (decl) = 1;
2416 DECL_NAMELESS (decl) = 1;
2417 TREE_CONSTANT (decl) = 1;
2419 DECL_CHAIN (decl) = DECL_CHAIN (all->orig_fnargs);
2420 DECL_CHAIN (all->orig_fnargs) = decl;
2421 fnargs.safe_insert (1, decl);
2425 /* If the target wants to split complex arguments into scalars, do so. */
2426 if (targetm.calls.split_complex_arg)
2427 split_complex_args (&fnargs);
2429 return fnargs;
2432 /* A subroutine of assign_parms. Examine PARM and pull out type and mode
2433 data for the parameter. Incorporate ABI specifics such as pass-by-
2434 reference and type promotion. */
2436 static void
2437 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2438 struct assign_parm_data_one *data)
2440 tree nominal_type, passed_type;
2441 machine_mode nominal_mode, passed_mode, promoted_mode;
2442 int unsignedp;
2444 memset (data, 0, sizeof (*data));
2446 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
2447 if (!cfun->stdarg)
2448 data->named_arg = 1; /* No variadic parms. */
2449 else if (DECL_CHAIN (parm))
2450 data->named_arg = 1; /* Not the last non-variadic parm. */
2451 else if (targetm.calls.strict_argument_naming (all->args_so_far))
2452 data->named_arg = 1; /* Only variadic ones are unnamed. */
2453 else
2454 data->named_arg = 0; /* Treat as variadic. */
2456 nominal_type = TREE_TYPE (parm);
2457 passed_type = DECL_ARG_TYPE (parm);
2459 /* Look out for errors propagating this far. Also, if the parameter's
2460 type is void then its value doesn't matter. */
2461 if (TREE_TYPE (parm) == error_mark_node
2462 /* This can happen after weird syntax errors
2463 or if an enum type is defined among the parms. */
2464 || TREE_CODE (parm) != PARM_DECL
2465 || passed_type == NULL
2466 || VOID_TYPE_P (nominal_type))
2468 nominal_type = passed_type = void_type_node;
2469 nominal_mode = passed_mode = promoted_mode = VOIDmode;
2470 goto egress;
2473 /* Find mode of arg as it is passed, and mode of arg as it should be
2474 during execution of this function. */
2475 passed_mode = TYPE_MODE (passed_type);
2476 nominal_mode = TYPE_MODE (nominal_type);
2478 /* If the parm is to be passed as a transparent union or record, use the
2479 type of the first field for the tests below. We have already verified
2480 that the modes are the same. */
2481 if ((TREE_CODE (passed_type) == UNION_TYPE
2482 || TREE_CODE (passed_type) == RECORD_TYPE)
2483 && TYPE_TRANSPARENT_AGGR (passed_type))
2484 passed_type = TREE_TYPE (first_field (passed_type));
2486 /* See if this arg was passed by invisible reference. */
2487 if (pass_by_reference (&all->args_so_far_v, passed_mode,
2488 passed_type, data->named_arg))
2490 passed_type = nominal_type = build_pointer_type (passed_type);
2491 data->passed_pointer = true;
2492 passed_mode = nominal_mode = TYPE_MODE (nominal_type);
2495 /* Find mode as it is passed by the ABI. */
2496 unsignedp = TYPE_UNSIGNED (passed_type);
2497 promoted_mode = promote_function_mode (passed_type, passed_mode, &unsignedp,
2498 TREE_TYPE (current_function_decl), 0);
2500 egress:
2501 data->nominal_type = nominal_type;
2502 data->passed_type = passed_type;
2503 data->nominal_mode = nominal_mode;
2504 data->passed_mode = passed_mode;
2505 data->promoted_mode = promoted_mode;
2508 /* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2510 static void
2511 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2512 struct assign_parm_data_one *data, bool no_rtl)
2514 int varargs_pretend_bytes = 0;
2516 targetm.calls.setup_incoming_varargs (all->args_so_far,
2517 data->promoted_mode,
2518 data->passed_type,
2519 &varargs_pretend_bytes, no_rtl);
2521 /* If the back-end has requested extra stack space, record how much is
2522 needed. Do not change pretend_args_size otherwise since it may be
2523 nonzero from an earlier partial argument. */
2524 if (varargs_pretend_bytes > 0)
2525 all->pretend_args_size = varargs_pretend_bytes;
2528 /* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2529 the incoming location of the current parameter. */
2531 static void
2532 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2533 struct assign_parm_data_one *data)
2535 HOST_WIDE_INT pretend_bytes = 0;
2536 rtx entry_parm;
2537 bool in_regs;
2539 if (data->promoted_mode == VOIDmode)
2541 data->entry_parm = data->stack_parm = const0_rtx;
2542 return;
2545 targetm.calls.warn_parameter_passing_abi (all->args_so_far,
2546 data->passed_type);
2548 entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2549 data->promoted_mode,
2550 data->passed_type,
2551 data->named_arg);
2553 if (entry_parm == 0)
2554 data->promoted_mode = data->passed_mode;
2556 /* Determine parm's home in the stack, in case it arrives in the stack
2557 or we should pretend it did. Compute the stack position and rtx where
2558 the argument arrives and its size.
2560 There is one complexity here: If this was a parameter that would
2561 have been passed in registers, but wasn't only because it is
2562 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2563 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2564 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2565 as it was the previous time. */
2566 in_regs = (entry_parm != 0) || POINTER_BOUNDS_TYPE_P (data->passed_type);
2567 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2568 in_regs = true;
2569 #endif
2570 if (!in_regs && !data->named_arg)
2572 if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2574 rtx tem;
2575 tem = targetm.calls.function_incoming_arg (all->args_so_far,
2576 data->promoted_mode,
2577 data->passed_type, true);
2578 in_regs = tem != NULL;
2582 /* If this parameter was passed both in registers and in the stack, use
2583 the copy on the stack. */
2584 if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2585 data->passed_type))
2586 entry_parm = 0;
2588 if (entry_parm)
2590 int partial;
2592 partial = targetm.calls.arg_partial_bytes (all->args_so_far,
2593 data->promoted_mode,
2594 data->passed_type,
2595 data->named_arg);
2596 data->partial = partial;
2598 /* The caller might already have allocated stack space for the
2599 register parameters. */
2600 if (partial != 0 && all->reg_parm_stack_space == 0)
2602 /* Part of this argument is passed in registers and part
2603 is passed on the stack. Ask the prologue code to extend
2604 the stack part so that we can recreate the full value.
2606 PRETEND_BYTES is the size of the registers we need to store.
2607 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2608 stack space that the prologue should allocate.
2610 Internally, gcc assumes that the argument pointer is aligned
2611 to STACK_BOUNDARY bits. This is used both for alignment
2612 optimizations (see init_emit) and to locate arguments that are
2613 aligned to more than PARM_BOUNDARY bits. We must preserve this
2614 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2615 a stack boundary. */
2617 /* We assume at most one partial arg, and it must be the first
2618 argument on the stack. */
2619 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2621 pretend_bytes = partial;
2622 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2624 /* We want to align relative to the actual stack pointer, so
2625 don't include this in the stack size until later. */
2626 all->extra_pretend_bytes = all->pretend_args_size;
2630 locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2631 all->reg_parm_stack_space,
2632 entry_parm ? data->partial : 0, current_function_decl,
2633 &all->stack_args_size, &data->locate);
2635 /* Update parm_stack_boundary if this parameter is passed in the
2636 stack. */
2637 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2638 crtl->parm_stack_boundary = data->locate.boundary;
2640 /* Adjust offsets to include the pretend args. */
2641 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2642 data->locate.slot_offset.constant += pretend_bytes;
2643 data->locate.offset.constant += pretend_bytes;
2645 data->entry_parm = entry_parm;
2648 /* A subroutine of assign_parms. If there is actually space on the stack
2649 for this parm, count it in stack_args_size and return true. */
2651 static bool
2652 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2653 struct assign_parm_data_one *data)
2655 /* Bounds are never passed on the stack to keep compatibility
2656 with not instrumented code. */
2657 if (POINTER_BOUNDS_TYPE_P (data->passed_type))
2658 return false;
2659 /* Trivially true if we've no incoming register. */
2660 else if (data->entry_parm == NULL)
2662 /* Also true if we're partially in registers and partially not,
2663 since we've arranged to drop the entire argument on the stack. */
2664 else if (data->partial != 0)
2666 /* Also true if the target says that it's passed in both registers
2667 and on the stack. */
2668 else if (GET_CODE (data->entry_parm) == PARALLEL
2669 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2671 /* Also true if the target says that there's stack allocated for
2672 all register parameters. */
2673 else if (all->reg_parm_stack_space > 0)
2675 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2676 else
2677 return false;
2679 all->stack_args_size.constant += data->locate.size.constant;
2680 if (data->locate.size.var)
2681 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2683 return true;
2686 /* A subroutine of assign_parms. Given that this parameter is allocated
2687 stack space by the ABI, find it. */
2689 static void
2690 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2692 rtx offset_rtx, stack_parm;
2693 unsigned int align, boundary;
2695 /* If we're passing this arg using a reg, make its stack home the
2696 aligned stack slot. */
2697 if (data->entry_parm)
2698 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2699 else
2700 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2702 stack_parm = crtl->args.internal_arg_pointer;
2703 if (offset_rtx != const0_rtx)
2704 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2705 stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2707 if (!data->passed_pointer)
2709 set_mem_attributes (stack_parm, parm, 1);
2710 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2711 while promoted mode's size is needed. */
2712 if (data->promoted_mode != BLKmode
2713 && data->promoted_mode != DECL_MODE (parm))
2715 set_mem_size (stack_parm, GET_MODE_SIZE (data->promoted_mode));
2716 if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2718 poly_int64 offset = subreg_lowpart_offset (DECL_MODE (parm),
2719 data->promoted_mode);
2720 if (maybe_ne (offset, 0))
2721 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2726 boundary = data->locate.boundary;
2727 align = BITS_PER_UNIT;
2729 /* If we're padding upward, we know that the alignment of the slot
2730 is TARGET_FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2731 intentionally forcing upward padding. Otherwise we have to come
2732 up with a guess at the alignment based on OFFSET_RTX. */
2733 poly_int64 offset;
2734 if (data->locate.where_pad != PAD_DOWNWARD || data->entry_parm)
2735 align = boundary;
2736 else if (poly_int_rtx_p (offset_rtx, &offset))
2738 align = least_bit_hwi (boundary);
2739 unsigned int offset_align = known_alignment (offset) * BITS_PER_UNIT;
2740 if (offset_align != 0)
2741 align = MIN (align, offset_align);
2743 set_mem_align (stack_parm, align);
2745 if (data->entry_parm)
2746 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2748 data->stack_parm = stack_parm;
2751 /* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2752 always valid and contiguous. */
2754 static void
2755 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2757 rtx entry_parm = data->entry_parm;
2758 rtx stack_parm = data->stack_parm;
2760 /* If this parm was passed part in regs and part in memory, pretend it
2761 arrived entirely in memory by pushing the register-part onto the stack.
2762 In the special case of a DImode or DFmode that is split, we could put
2763 it together in a pseudoreg directly, but for now that's not worth
2764 bothering with. */
2765 if (data->partial != 0)
2767 /* Handle calls that pass values in multiple non-contiguous
2768 locations. The Irix 6 ABI has examples of this. */
2769 if (GET_CODE (entry_parm) == PARALLEL)
2770 emit_group_store (validize_mem (copy_rtx (stack_parm)), entry_parm,
2771 data->passed_type,
2772 int_size_in_bytes (data->passed_type));
2773 else
2775 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2776 move_block_from_reg (REGNO (entry_parm),
2777 validize_mem (copy_rtx (stack_parm)),
2778 data->partial / UNITS_PER_WORD);
2781 entry_parm = stack_parm;
2784 /* If we didn't decide this parm came in a register, by default it came
2785 on the stack. */
2786 else if (entry_parm == NULL)
2787 entry_parm = stack_parm;
2789 /* When an argument is passed in multiple locations, we can't make use
2790 of this information, but we can save some copying if the whole argument
2791 is passed in a single register. */
2792 else if (GET_CODE (entry_parm) == PARALLEL
2793 && data->nominal_mode != BLKmode
2794 && data->passed_mode != BLKmode)
2796 size_t i, len = XVECLEN (entry_parm, 0);
2798 for (i = 0; i < len; i++)
2799 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2800 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2801 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2802 == data->passed_mode)
2803 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2805 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2806 break;
2810 data->entry_parm = entry_parm;
2813 /* A subroutine of assign_parms. Reconstitute any values which were
2814 passed in multiple registers and would fit in a single register. */
2816 static void
2817 assign_parm_remove_parallels (struct assign_parm_data_one *data)
2819 rtx entry_parm = data->entry_parm;
2821 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2822 This can be done with register operations rather than on the
2823 stack, even if we will store the reconstituted parameter on the
2824 stack later. */
2825 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2827 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2828 emit_group_store (parmreg, entry_parm, data->passed_type,
2829 GET_MODE_SIZE (GET_MODE (entry_parm)));
2830 entry_parm = parmreg;
2833 data->entry_parm = entry_parm;
2836 /* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2837 always valid and properly aligned. */
2839 static void
2840 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2842 rtx stack_parm = data->stack_parm;
2844 /* If we can't trust the parm stack slot to be aligned enough for its
2845 ultimate type, don't use that slot after entry. We'll make another
2846 stack slot, if we need one. */
2847 if (stack_parm
2848 && ((STRICT_ALIGNMENT
2849 && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2850 || (data->nominal_type
2851 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2852 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2853 stack_parm = NULL;
2855 /* If parm was passed in memory, and we need to convert it on entry,
2856 don't store it back in that same slot. */
2857 else if (data->entry_parm == stack_parm
2858 && data->nominal_mode != BLKmode
2859 && data->nominal_mode != data->passed_mode)
2860 stack_parm = NULL;
2862 /* If stack protection is in effect for this function, don't leave any
2863 pointers in their passed stack slots. */
2864 else if (crtl->stack_protect_guard
2865 && (flag_stack_protect == 2
2866 || data->passed_pointer
2867 || POINTER_TYPE_P (data->nominal_type)))
2868 stack_parm = NULL;
2870 data->stack_parm = stack_parm;
2873 /* A subroutine of assign_parms. Return true if the current parameter
2874 should be stored as a BLKmode in the current frame. */
2876 static bool
2877 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2879 if (data->nominal_mode == BLKmode)
2880 return true;
2881 if (GET_MODE (data->entry_parm) == BLKmode)
2882 return true;
2884 #ifdef BLOCK_REG_PADDING
2885 /* Only assign_parm_setup_block knows how to deal with register arguments
2886 that are padded at the least significant end. */
2887 if (REG_P (data->entry_parm)
2888 && known_lt (GET_MODE_SIZE (data->promoted_mode), UNITS_PER_WORD)
2889 && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2890 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
2891 return true;
2892 #endif
2894 return false;
2897 /* A subroutine of assign_parms. Arrange for the parameter to be
2898 present and valid in DATA->STACK_RTL. */
2900 static void
2901 assign_parm_setup_block (struct assign_parm_data_all *all,
2902 tree parm, struct assign_parm_data_one *data)
2904 rtx entry_parm = data->entry_parm;
2905 rtx stack_parm = data->stack_parm;
2906 rtx target_reg = NULL_RTX;
2907 bool in_conversion_seq = false;
2908 HOST_WIDE_INT size;
2909 HOST_WIDE_INT size_stored;
2911 if (GET_CODE (entry_parm) == PARALLEL)
2912 entry_parm = emit_group_move_into_temps (entry_parm);
2914 /* If we want the parameter in a pseudo, don't use a stack slot. */
2915 if (is_gimple_reg (parm) && use_register_for_decl (parm))
2917 tree def = ssa_default_def (cfun, parm);
2918 gcc_assert (def);
2919 machine_mode mode = promote_ssa_mode (def, NULL);
2920 rtx reg = gen_reg_rtx (mode);
2921 if (GET_CODE (reg) != CONCAT)
2922 stack_parm = reg;
2923 else
2925 target_reg = reg;
2926 /* Avoid allocating a stack slot, if there isn't one
2927 preallocated by the ABI. It might seem like we should
2928 always prefer a pseudo, but converting between
2929 floating-point and integer modes goes through the stack
2930 on various machines, so it's better to use the reserved
2931 stack slot than to risk wasting it and allocating more
2932 for the conversion. */
2933 if (stack_parm == NULL_RTX)
2935 int save = generating_concat_p;
2936 generating_concat_p = 0;
2937 stack_parm = gen_reg_rtx (mode);
2938 generating_concat_p = save;
2941 data->stack_parm = NULL;
2944 size = int_size_in_bytes (data->passed_type);
2945 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2946 if (stack_parm == 0)
2948 SET_DECL_ALIGN (parm, MAX (DECL_ALIGN (parm), BITS_PER_WORD));
2949 stack_parm = assign_stack_local (BLKmode, size_stored,
2950 DECL_ALIGN (parm));
2951 if (known_eq (GET_MODE_SIZE (GET_MODE (entry_parm)), size))
2952 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2953 set_mem_attributes (stack_parm, parm, 1);
2956 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2957 calls that pass values in multiple non-contiguous locations. */
2958 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2960 rtx mem;
2962 /* Note that we will be storing an integral number of words.
2963 So we have to be careful to ensure that we allocate an
2964 integral number of words. We do this above when we call
2965 assign_stack_local if space was not allocated in the argument
2966 list. If it was, this will not work if PARM_BOUNDARY is not
2967 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2968 if it becomes a problem. Exception is when BLKmode arrives
2969 with arguments not conforming to word_mode. */
2971 if (data->stack_parm == 0)
2973 else if (GET_CODE (entry_parm) == PARALLEL)
2975 else
2976 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2978 mem = validize_mem (copy_rtx (stack_parm));
2980 /* Handle values in multiple non-contiguous locations. */
2981 if (GET_CODE (entry_parm) == PARALLEL && !MEM_P (mem))
2982 emit_group_store (mem, entry_parm, data->passed_type, size);
2983 else if (GET_CODE (entry_parm) == PARALLEL)
2985 push_to_sequence2 (all->first_conversion_insn,
2986 all->last_conversion_insn);
2987 emit_group_store (mem, entry_parm, data->passed_type, size);
2988 all->first_conversion_insn = get_insns ();
2989 all->last_conversion_insn = get_last_insn ();
2990 end_sequence ();
2991 in_conversion_seq = true;
2994 else if (size == 0)
2997 /* If SIZE is that of a mode no bigger than a word, just use
2998 that mode's store operation. */
2999 else if (size <= UNITS_PER_WORD)
3001 unsigned int bits = size * BITS_PER_UNIT;
3002 machine_mode mode = int_mode_for_size (bits, 0).else_blk ();
3004 if (mode != BLKmode
3005 #ifdef BLOCK_REG_PADDING
3006 && (size == UNITS_PER_WORD
3007 || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
3008 != (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
3009 #endif
3012 rtx reg;
3014 /* We are really truncating a word_mode value containing
3015 SIZE bytes into a value of mode MODE. If such an
3016 operation requires no actual instructions, we can refer
3017 to the value directly in mode MODE, otherwise we must
3018 start with the register in word_mode and explicitly
3019 convert it. */
3020 if (targetm.truly_noop_truncation (size * BITS_PER_UNIT,
3021 BITS_PER_WORD))
3022 reg = gen_rtx_REG (mode, REGNO (entry_parm));
3023 else
3025 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
3026 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
3028 emit_move_insn (change_address (mem, mode, 0), reg);
3031 #ifdef BLOCK_REG_PADDING
3032 /* Storing the register in memory as a full word, as
3033 move_block_from_reg below would do, and then using the
3034 MEM in a smaller mode, has the effect of shifting right
3035 if BYTES_BIG_ENDIAN. If we're bypassing memory, the
3036 shifting must be explicit. */
3037 else if (!MEM_P (mem))
3039 rtx x;
3041 /* If the assert below fails, we should have taken the
3042 mode != BLKmode path above, unless we have downward
3043 padding of smaller-than-word arguments on a machine
3044 with little-endian bytes, which would likely require
3045 additional changes to work correctly. */
3046 gcc_checking_assert (BYTES_BIG_ENDIAN
3047 && (BLOCK_REG_PADDING (mode,
3048 data->passed_type, 1)
3049 == PAD_UPWARD));
3051 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
3053 x = gen_rtx_REG (word_mode, REGNO (entry_parm));
3054 x = expand_shift (RSHIFT_EXPR, word_mode, x, by,
3055 NULL_RTX, 1);
3056 x = force_reg (word_mode, x);
3057 x = gen_lowpart_SUBREG (GET_MODE (mem), x);
3059 emit_move_insn (mem, x);
3061 #endif
3063 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
3064 machine must be aligned to the left before storing
3065 to memory. Note that the previous test doesn't
3066 handle all cases (e.g. SIZE == 3). */
3067 else if (size != UNITS_PER_WORD
3068 #ifdef BLOCK_REG_PADDING
3069 && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
3070 == PAD_DOWNWARD)
3071 #else
3072 && BYTES_BIG_ENDIAN
3073 #endif
3076 rtx tem, x;
3077 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
3078 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
3080 x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
3081 tem = change_address (mem, word_mode, 0);
3082 emit_move_insn (tem, x);
3084 else
3085 move_block_from_reg (REGNO (entry_parm), mem,
3086 size_stored / UNITS_PER_WORD);
3088 else if (!MEM_P (mem))
3090 gcc_checking_assert (size > UNITS_PER_WORD);
3091 #ifdef BLOCK_REG_PADDING
3092 gcc_checking_assert (BLOCK_REG_PADDING (GET_MODE (mem),
3093 data->passed_type, 0)
3094 == PAD_UPWARD);
3095 #endif
3096 emit_move_insn (mem, entry_parm);
3098 else
3099 move_block_from_reg (REGNO (entry_parm), mem,
3100 size_stored / UNITS_PER_WORD);
3102 else if (data->stack_parm == 0)
3104 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3105 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
3106 BLOCK_OP_NORMAL);
3107 all->first_conversion_insn = get_insns ();
3108 all->last_conversion_insn = get_last_insn ();
3109 end_sequence ();
3110 in_conversion_seq = true;
3113 if (target_reg)
3115 if (!in_conversion_seq)
3116 emit_move_insn (target_reg, stack_parm);
3117 else
3119 push_to_sequence2 (all->first_conversion_insn,
3120 all->last_conversion_insn);
3121 emit_move_insn (target_reg, stack_parm);
3122 all->first_conversion_insn = get_insns ();
3123 all->last_conversion_insn = get_last_insn ();
3124 end_sequence ();
3126 stack_parm = target_reg;
3129 data->stack_parm = stack_parm;
3130 set_parm_rtl (parm, stack_parm);
3133 /* A subroutine of assign_parms. Allocate a pseudo to hold the current
3134 parameter. Get it there. Perform all ABI specified conversions. */
3136 static void
3137 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
3138 struct assign_parm_data_one *data)
3140 rtx parmreg, validated_mem;
3141 rtx equiv_stack_parm;
3142 machine_mode promoted_nominal_mode;
3143 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
3144 bool did_conversion = false;
3145 bool need_conversion, moved;
3146 rtx rtl;
3148 /* Store the parm in a pseudoregister during the function, but we may
3149 need to do it in a wider mode. Using 2 here makes the result
3150 consistent with promote_decl_mode and thus expand_expr_real_1. */
3151 promoted_nominal_mode
3152 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
3153 TREE_TYPE (current_function_decl), 2);
3155 parmreg = gen_reg_rtx (promoted_nominal_mode);
3156 if (!DECL_ARTIFICIAL (parm))
3157 mark_user_reg (parmreg);
3159 /* If this was an item that we received a pointer to,
3160 set rtl appropriately. */
3161 if (data->passed_pointer)
3163 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
3164 set_mem_attributes (rtl, parm, 1);
3166 else
3167 rtl = parmreg;
3169 assign_parm_remove_parallels (data);
3171 /* Copy the value into the register, thus bridging between
3172 assign_parm_find_data_types and expand_expr_real_1. */
3174 equiv_stack_parm = data->stack_parm;
3175 validated_mem = validize_mem (copy_rtx (data->entry_parm));
3177 need_conversion = (data->nominal_mode != data->passed_mode
3178 || promoted_nominal_mode != data->promoted_mode);
3179 moved = false;
3181 if (need_conversion
3182 && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
3183 && data->nominal_mode == data->passed_mode
3184 && data->nominal_mode == GET_MODE (data->entry_parm))
3186 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
3187 mode, by the caller. We now have to convert it to
3188 NOMINAL_MODE, if different. However, PARMREG may be in
3189 a different mode than NOMINAL_MODE if it is being stored
3190 promoted.
3192 If ENTRY_PARM is a hard register, it might be in a register
3193 not valid for operating in its mode (e.g., an odd-numbered
3194 register for a DFmode). In that case, moves are the only
3195 thing valid, so we can't do a convert from there. This
3196 occurs when the calling sequence allow such misaligned
3197 usages.
3199 In addition, the conversion may involve a call, which could
3200 clobber parameters which haven't been copied to pseudo
3201 registers yet.
3203 First, we try to emit an insn which performs the necessary
3204 conversion. We verify that this insn does not clobber any
3205 hard registers. */
3207 enum insn_code icode;
3208 rtx op0, op1;
3210 icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
3211 unsignedp);
3213 op0 = parmreg;
3214 op1 = validated_mem;
3215 if (icode != CODE_FOR_nothing
3216 && insn_operand_matches (icode, 0, op0)
3217 && insn_operand_matches (icode, 1, op1))
3219 enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
3220 rtx_insn *insn, *insns;
3221 rtx t = op1;
3222 HARD_REG_SET hardregs;
3224 start_sequence ();
3225 /* If op1 is a hard register that is likely spilled, first
3226 force it into a pseudo, otherwise combiner might extend
3227 its lifetime too much. */
3228 if (GET_CODE (t) == SUBREG)
3229 t = SUBREG_REG (t);
3230 if (REG_P (t)
3231 && HARD_REGISTER_P (t)
3232 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (t))
3233 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (t))))
3235 t = gen_reg_rtx (GET_MODE (op1));
3236 emit_move_insn (t, op1);
3238 else
3239 t = op1;
3240 rtx_insn *pat = gen_extend_insn (op0, t, promoted_nominal_mode,
3241 data->passed_mode, unsignedp);
3242 emit_insn (pat);
3243 insns = get_insns ();
3245 moved = true;
3246 CLEAR_HARD_REG_SET (hardregs);
3247 for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3249 if (INSN_P (insn))
3250 note_stores (PATTERN (insn), record_hard_reg_sets,
3251 &hardregs);
3252 if (!hard_reg_set_empty_p (hardregs))
3253 moved = false;
3256 end_sequence ();
3258 if (moved)
3260 emit_insn (insns);
3261 if (equiv_stack_parm != NULL_RTX)
3262 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3263 equiv_stack_parm);
3268 if (moved)
3269 /* Nothing to do. */
3271 else if (need_conversion)
3273 /* We did not have an insn to convert directly, or the sequence
3274 generated appeared unsafe. We must first copy the parm to a
3275 pseudo reg, and save the conversion until after all
3276 parameters have been moved. */
3278 int save_tree_used;
3279 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3281 emit_move_insn (tempreg, validated_mem);
3283 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3284 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3286 if (partial_subreg_p (tempreg)
3287 && GET_MODE (tempreg) == data->nominal_mode
3288 && REG_P (SUBREG_REG (tempreg))
3289 && data->nominal_mode == data->passed_mode
3290 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm))
3292 /* The argument is already sign/zero extended, so note it
3293 into the subreg. */
3294 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3295 SUBREG_PROMOTED_SET (tempreg, unsignedp);
3298 /* TREE_USED gets set erroneously during expand_assignment. */
3299 save_tree_used = TREE_USED (parm);
3300 SET_DECL_RTL (parm, rtl);
3301 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3302 SET_DECL_RTL (parm, NULL_RTX);
3303 TREE_USED (parm) = save_tree_used;
3304 all->first_conversion_insn = get_insns ();
3305 all->last_conversion_insn = get_last_insn ();
3306 end_sequence ();
3308 did_conversion = true;
3310 else
3311 emit_move_insn (parmreg, validated_mem);
3313 /* If we were passed a pointer but the actual value can safely live
3314 in a register, retrieve it and use it directly. */
3315 if (data->passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode)
3317 /* We can't use nominal_mode, because it will have been set to
3318 Pmode above. We must use the actual mode of the parm. */
3319 if (use_register_for_decl (parm))
3321 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3322 mark_user_reg (parmreg);
3324 else
3326 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3327 TYPE_MODE (TREE_TYPE (parm)),
3328 TYPE_ALIGN (TREE_TYPE (parm)));
3329 parmreg
3330 = assign_stack_local (TYPE_MODE (TREE_TYPE (parm)),
3331 GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parm))),
3332 align);
3333 set_mem_attributes (parmreg, parm, 1);
3336 /* We need to preserve an address based on VIRTUAL_STACK_VARS_REGNUM for
3337 the debug info in case it is not legitimate. */
3338 if (GET_MODE (parmreg) != GET_MODE (rtl))
3340 rtx tempreg = gen_reg_rtx (GET_MODE (rtl));
3341 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3343 push_to_sequence2 (all->first_conversion_insn,
3344 all->last_conversion_insn);
3345 emit_move_insn (tempreg, rtl);
3346 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3347 emit_move_insn (MEM_P (parmreg) ? copy_rtx (parmreg) : parmreg,
3348 tempreg);
3349 all->first_conversion_insn = get_insns ();
3350 all->last_conversion_insn = get_last_insn ();
3351 end_sequence ();
3353 did_conversion = true;
3355 else
3356 emit_move_insn (MEM_P (parmreg) ? copy_rtx (parmreg) : parmreg, rtl);
3358 rtl = parmreg;
3360 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3361 now the parm. */
3362 data->stack_parm = NULL;
3365 set_parm_rtl (parm, rtl);
3367 /* Mark the register as eliminable if we did no conversion and it was
3368 copied from memory at a fixed offset, and the arg pointer was not
3369 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3370 offset formed an invalid address, such memory-equivalences as we
3371 make here would screw up life analysis for it. */
3372 if (data->nominal_mode == data->passed_mode
3373 && !did_conversion
3374 && data->stack_parm != 0
3375 && MEM_P (data->stack_parm)
3376 && data->locate.offset.var == 0
3377 && reg_mentioned_p (virtual_incoming_args_rtx,
3378 XEXP (data->stack_parm, 0)))
3380 rtx_insn *linsn = get_last_insn ();
3381 rtx_insn *sinsn;
3382 rtx set;
3384 /* Mark complex types separately. */
3385 if (GET_CODE (parmreg) == CONCAT)
3387 scalar_mode submode = GET_MODE_INNER (GET_MODE (parmreg));
3388 int regnor = REGNO (XEXP (parmreg, 0));
3389 int regnoi = REGNO (XEXP (parmreg, 1));
3390 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3391 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3392 GET_MODE_SIZE (submode));
3394 /* Scan backwards for the set of the real and
3395 imaginary parts. */
3396 for (sinsn = linsn; sinsn != 0;
3397 sinsn = prev_nonnote_insn (sinsn))
3399 set = single_set (sinsn);
3400 if (set == 0)
3401 continue;
3403 if (SET_DEST (set) == regno_reg_rtx [regnoi])
3404 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3405 else if (SET_DEST (set) == regno_reg_rtx [regnor])
3406 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3409 else
3410 set_dst_reg_note (linsn, REG_EQUIV, equiv_stack_parm, parmreg);
3413 /* For pointer data type, suggest pointer register. */
3414 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3415 mark_reg_pointer (parmreg,
3416 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3419 /* A subroutine of assign_parms. Allocate stack space to hold the current
3420 parameter. Get it there. Perform all ABI specified conversions. */
3422 static void
3423 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3424 struct assign_parm_data_one *data)
3426 /* Value must be stored in the stack slot STACK_PARM during function
3427 execution. */
3428 bool to_conversion = false;
3430 assign_parm_remove_parallels (data);
3432 if (data->promoted_mode != data->nominal_mode)
3434 /* Conversion is required. */
3435 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3437 emit_move_insn (tempreg, validize_mem (copy_rtx (data->entry_parm)));
3439 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3440 to_conversion = true;
3442 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3443 TYPE_UNSIGNED (TREE_TYPE (parm)));
3445 if (data->stack_parm)
3447 poly_int64 offset
3448 = subreg_lowpart_offset (data->nominal_mode,
3449 GET_MODE (data->stack_parm));
3450 /* ??? This may need a big-endian conversion on sparc64. */
3451 data->stack_parm
3452 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3453 if (maybe_ne (offset, 0) && MEM_OFFSET_KNOWN_P (data->stack_parm))
3454 set_mem_offset (data->stack_parm,
3455 MEM_OFFSET (data->stack_parm) + offset);
3459 if (data->entry_parm != data->stack_parm)
3461 rtx src, dest;
3463 if (data->stack_parm == 0)
3465 int align = STACK_SLOT_ALIGNMENT (data->passed_type,
3466 GET_MODE (data->entry_parm),
3467 TYPE_ALIGN (data->passed_type));
3468 data->stack_parm
3469 = assign_stack_local (GET_MODE (data->entry_parm),
3470 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3471 align);
3472 set_mem_attributes (data->stack_parm, parm, 1);
3475 dest = validize_mem (copy_rtx (data->stack_parm));
3476 src = validize_mem (copy_rtx (data->entry_parm));
3478 if (MEM_P (src))
3480 /* Use a block move to handle potentially misaligned entry_parm. */
3481 if (!to_conversion)
3482 push_to_sequence2 (all->first_conversion_insn,
3483 all->last_conversion_insn);
3484 to_conversion = true;
3486 emit_block_move (dest, src,
3487 GEN_INT (int_size_in_bytes (data->passed_type)),
3488 BLOCK_OP_NORMAL);
3490 else
3492 if (!REG_P (src))
3493 src = force_reg (GET_MODE (src), src);
3494 emit_move_insn (dest, src);
3498 if (to_conversion)
3500 all->first_conversion_insn = get_insns ();
3501 all->last_conversion_insn = get_last_insn ();
3502 end_sequence ();
3505 set_parm_rtl (parm, data->stack_parm);
3508 /* A subroutine of assign_parms. If the ABI splits complex arguments, then
3509 undo the frobbing that we did in assign_parms_augmented_arg_list. */
3511 static void
3512 assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3513 vec<tree> fnargs)
3515 tree parm;
3516 tree orig_fnargs = all->orig_fnargs;
3517 unsigned i = 0;
3519 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3521 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3522 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3524 rtx tmp, real, imag;
3525 scalar_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3527 real = DECL_RTL (fnargs[i]);
3528 imag = DECL_RTL (fnargs[i + 1]);
3529 if (inner != GET_MODE (real))
3531 real = gen_lowpart_SUBREG (inner, real);
3532 imag = gen_lowpart_SUBREG (inner, imag);
3535 if (TREE_ADDRESSABLE (parm))
3537 rtx rmem, imem;
3538 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3539 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3540 DECL_MODE (parm),
3541 TYPE_ALIGN (TREE_TYPE (parm)));
3543 /* split_complex_arg put the real and imag parts in
3544 pseudos. Move them to memory. */
3545 tmp = assign_stack_local (DECL_MODE (parm), size, align);
3546 set_mem_attributes (tmp, parm, 1);
3547 rmem = adjust_address_nv (tmp, inner, 0);
3548 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3549 push_to_sequence2 (all->first_conversion_insn,
3550 all->last_conversion_insn);
3551 emit_move_insn (rmem, real);
3552 emit_move_insn (imem, imag);
3553 all->first_conversion_insn = get_insns ();
3554 all->last_conversion_insn = get_last_insn ();
3555 end_sequence ();
3557 else
3558 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3559 set_parm_rtl (parm, tmp);
3561 real = DECL_INCOMING_RTL (fnargs[i]);
3562 imag = DECL_INCOMING_RTL (fnargs[i + 1]);
3563 if (inner != GET_MODE (real))
3565 real = gen_lowpart_SUBREG (inner, real);
3566 imag = gen_lowpart_SUBREG (inner, imag);
3568 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3569 set_decl_incoming_rtl (parm, tmp, false);
3570 i++;
3575 /* Load bounds of PARM from bounds table. */
3576 static void
3577 assign_parm_load_bounds (struct assign_parm_data_one *data,
3578 tree parm,
3579 rtx entry,
3580 unsigned bound_no)
3582 bitmap_iterator bi;
3583 unsigned i, offs = 0;
3584 int bnd_no = -1;
3585 rtx slot = NULL, ptr = NULL;
3587 if (parm)
3589 bitmap slots;
3590 bitmap_obstack_initialize (NULL);
3591 slots = BITMAP_ALLOC (NULL);
3592 chkp_find_bound_slots (TREE_TYPE (parm), slots);
3593 EXECUTE_IF_SET_IN_BITMAP (slots, 0, i, bi)
3595 if (bound_no)
3596 bound_no--;
3597 else
3599 bnd_no = i;
3600 break;
3603 BITMAP_FREE (slots);
3604 bitmap_obstack_release (NULL);
3607 /* We may have bounds not associated with any pointer. */
3608 if (bnd_no != -1)
3609 offs = bnd_no * POINTER_SIZE / BITS_PER_UNIT;
3611 /* Find associated pointer. */
3612 if (bnd_no == -1)
3614 /* If bounds are not associated with any bounds,
3615 then it is passed in a register or special slot. */
3616 gcc_assert (data->entry_parm);
3617 ptr = const0_rtx;
3619 else if (MEM_P (entry))
3620 slot = adjust_address (entry, Pmode, offs);
3621 else if (REG_P (entry))
3622 ptr = gen_rtx_REG (Pmode, REGNO (entry) + bnd_no);
3623 else if (GET_CODE (entry) == PARALLEL)
3624 ptr = chkp_get_value_with_offs (entry, GEN_INT (offs));
3625 else
3626 gcc_unreachable ();
3627 data->entry_parm = targetm.calls.load_bounds_for_arg (slot, ptr,
3628 data->entry_parm);
3631 /* Assign RTL expressions to the function's bounds parameters BNDARGS. */
3633 static void
3634 assign_bounds (vec<bounds_parm_data> &bndargs,
3635 struct assign_parm_data_all &all,
3636 bool assign_regs, bool assign_special,
3637 bool assign_bt)
3639 unsigned i, pass;
3640 bounds_parm_data *pbdata;
3642 if (!bndargs.exists ())
3643 return;
3645 /* We make few passes to store input bounds. Firstly handle bounds
3646 passed in registers. After that we load bounds passed in special
3647 slots. Finally we load bounds from Bounds Table. */
3648 for (pass = 0; pass < 3; pass++)
3649 FOR_EACH_VEC_ELT (bndargs, i, pbdata)
3651 /* Pass 0 => regs only. */
3652 if (pass == 0
3653 && (!assign_regs
3654 ||(!pbdata->parm_data.entry_parm
3655 || GET_CODE (pbdata->parm_data.entry_parm) != REG)))
3656 continue;
3657 /* Pass 1 => slots only. */
3658 else if (pass == 1
3659 && (!assign_special
3660 || (!pbdata->parm_data.entry_parm
3661 || GET_CODE (pbdata->parm_data.entry_parm) == REG)))
3662 continue;
3663 /* Pass 2 => BT only. */
3664 else if (pass == 2
3665 && (!assign_bt
3666 || pbdata->parm_data.entry_parm))
3667 continue;
3669 if (!pbdata->parm_data.entry_parm
3670 || GET_CODE (pbdata->parm_data.entry_parm) != REG)
3671 assign_parm_load_bounds (&pbdata->parm_data, pbdata->ptr_parm,
3672 pbdata->ptr_entry, pbdata->bound_no);
3674 set_decl_incoming_rtl (pbdata->bounds_parm,
3675 pbdata->parm_data.entry_parm, false);
3677 if (assign_parm_setup_block_p (&pbdata->parm_data))
3678 assign_parm_setup_block (&all, pbdata->bounds_parm,
3679 &pbdata->parm_data);
3680 else if (pbdata->parm_data.passed_pointer
3681 || use_register_for_decl (pbdata->bounds_parm))
3682 assign_parm_setup_reg (&all, pbdata->bounds_parm,
3683 &pbdata->parm_data);
3684 else
3685 assign_parm_setup_stack (&all, pbdata->bounds_parm,
3686 &pbdata->parm_data);
3690 /* Assign RTL expressions to the function's parameters. This may involve
3691 copying them into registers and using those registers as the DECL_RTL. */
3693 static void
3694 assign_parms (tree fndecl)
3696 struct assign_parm_data_all all;
3697 tree parm;
3698 vec<tree> fnargs;
3699 unsigned i, bound_no = 0;
3700 tree last_arg = NULL;
3701 rtx last_arg_entry = NULL;
3702 vec<bounds_parm_data> bndargs = vNULL;
3703 bounds_parm_data bdata;
3705 crtl->args.internal_arg_pointer
3706 = targetm.calls.internal_arg_pointer ();
3708 assign_parms_initialize_all (&all);
3709 fnargs = assign_parms_augmented_arg_list (&all);
3711 FOR_EACH_VEC_ELT (fnargs, i, parm)
3713 struct assign_parm_data_one data;
3715 /* Extract the type of PARM; adjust it according to ABI. */
3716 assign_parm_find_data_types (&all, parm, &data);
3718 /* Early out for errors and void parameters. */
3719 if (data.passed_mode == VOIDmode)
3721 SET_DECL_RTL (parm, const0_rtx);
3722 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3723 continue;
3726 /* Estimate stack alignment from parameter alignment. */
3727 if (SUPPORTS_STACK_ALIGNMENT)
3729 unsigned int align
3730 = targetm.calls.function_arg_boundary (data.promoted_mode,
3731 data.passed_type);
3732 align = MINIMUM_ALIGNMENT (data.passed_type, data.promoted_mode,
3733 align);
3734 if (TYPE_ALIGN (data.nominal_type) > align)
3735 align = MINIMUM_ALIGNMENT (data.nominal_type,
3736 TYPE_MODE (data.nominal_type),
3737 TYPE_ALIGN (data.nominal_type));
3738 if (crtl->stack_alignment_estimated < align)
3740 gcc_assert (!crtl->stack_realign_processed);
3741 crtl->stack_alignment_estimated = align;
3745 /* Find out where the parameter arrives in this function. */
3746 assign_parm_find_entry_rtl (&all, &data);
3748 /* Find out where stack space for this parameter might be. */
3749 if (assign_parm_is_stack_parm (&all, &data))
3751 assign_parm_find_stack_rtl (parm, &data);
3752 assign_parm_adjust_entry_rtl (&data);
3754 if (!POINTER_BOUNDS_TYPE_P (data.passed_type))
3756 /* Remember where last non bounds arg was passed in case
3757 we have to load associated bounds for it from Bounds
3758 Table. */
3759 last_arg = parm;
3760 last_arg_entry = data.entry_parm;
3761 bound_no = 0;
3763 /* Record permanently how this parm was passed. */
3764 if (data.passed_pointer)
3766 rtx incoming_rtl
3767 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.passed_type)),
3768 data.entry_parm);
3769 set_decl_incoming_rtl (parm, incoming_rtl, true);
3771 else
3772 set_decl_incoming_rtl (parm, data.entry_parm, false);
3774 assign_parm_adjust_stack_rtl (&data);
3776 /* Bounds should be loaded in the particular order to
3777 have registers allocated correctly. Collect info about
3778 input bounds and load them later. */
3779 if (POINTER_BOUNDS_TYPE_P (data.passed_type))
3781 /* Expect bounds in instrumented functions only. */
3782 gcc_assert (chkp_function_instrumented_p (fndecl));
3784 bdata.parm_data = data;
3785 bdata.bounds_parm = parm;
3786 bdata.ptr_parm = last_arg;
3787 bdata.ptr_entry = last_arg_entry;
3788 bdata.bound_no = bound_no;
3789 bndargs.safe_push (bdata);
3791 else
3793 if (assign_parm_setup_block_p (&data))
3794 assign_parm_setup_block (&all, parm, &data);
3795 else if (data.passed_pointer || use_register_for_decl (parm))
3796 assign_parm_setup_reg (&all, parm, &data);
3797 else
3798 assign_parm_setup_stack (&all, parm, &data);
3801 if (cfun->stdarg && !DECL_CHAIN (parm))
3803 int pretend_bytes = 0;
3805 assign_parms_setup_varargs (&all, &data, false);
3807 if (chkp_function_instrumented_p (fndecl))
3809 /* We expect this is the last parm. Otherwise it is wrong
3810 to assign bounds right now. */
3811 gcc_assert (i == (fnargs.length () - 1));
3812 assign_bounds (bndargs, all, true, false, false);
3813 targetm.calls.setup_incoming_vararg_bounds (all.args_so_far,
3814 data.promoted_mode,
3815 data.passed_type,
3816 &pretend_bytes,
3817 false);
3818 assign_bounds (bndargs, all, false, true, true);
3819 bndargs.release ();
3823 /* Update info on where next arg arrives in registers. */
3824 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3825 data.passed_type, data.named_arg);
3827 if (POINTER_BOUNDS_TYPE_P (data.passed_type))
3828 bound_no++;
3831 assign_bounds (bndargs, all, true, true, true);
3832 bndargs.release ();
3834 if (targetm.calls.split_complex_arg)
3835 assign_parms_unsplit_complex (&all, fnargs);
3837 fnargs.release ();
3839 /* Output all parameter conversion instructions (possibly including calls)
3840 now that all parameters have been copied out of hard registers. */
3841 emit_insn (all.first_conversion_insn);
3843 /* Estimate reload stack alignment from scalar return mode. */
3844 if (SUPPORTS_STACK_ALIGNMENT)
3846 if (DECL_RESULT (fndecl))
3848 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3849 machine_mode mode = TYPE_MODE (type);
3851 if (mode != BLKmode
3852 && mode != VOIDmode
3853 && !AGGREGATE_TYPE_P (type))
3855 unsigned int align = GET_MODE_ALIGNMENT (mode);
3856 if (crtl->stack_alignment_estimated < align)
3858 gcc_assert (!crtl->stack_realign_processed);
3859 crtl->stack_alignment_estimated = align;
3865 /* If we are receiving a struct value address as the first argument, set up
3866 the RTL for the function result. As this might require code to convert
3867 the transmitted address to Pmode, we do this here to ensure that possible
3868 preliminary conversions of the address have been emitted already. */
3869 if (all.function_result_decl)
3871 tree result = DECL_RESULT (current_function_decl);
3872 rtx addr = DECL_RTL (all.function_result_decl);
3873 rtx x;
3875 if (DECL_BY_REFERENCE (result))
3877 SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3878 x = addr;
3880 else
3882 SET_DECL_VALUE_EXPR (result,
3883 build1 (INDIRECT_REF, TREE_TYPE (result),
3884 all.function_result_decl));
3885 addr = convert_memory_address (Pmode, addr);
3886 x = gen_rtx_MEM (DECL_MODE (result), addr);
3887 set_mem_attributes (x, result, 1);
3890 DECL_HAS_VALUE_EXPR_P (result) = 1;
3892 set_parm_rtl (result, x);
3895 /* We have aligned all the args, so add space for the pretend args. */
3896 crtl->args.pretend_args_size = all.pretend_args_size;
3897 all.stack_args_size.constant += all.extra_pretend_bytes;
3898 crtl->args.size = all.stack_args_size.constant;
3900 /* Adjust function incoming argument size for alignment and
3901 minimum length. */
3903 crtl->args.size = upper_bound (crtl->args.size, all.reg_parm_stack_space);
3904 crtl->args.size = aligned_upper_bound (crtl->args.size,
3905 PARM_BOUNDARY / BITS_PER_UNIT);
3907 if (ARGS_GROW_DOWNWARD)
3909 crtl->args.arg_offset_rtx
3910 = (all.stack_args_size.var == 0
3911 ? gen_int_mode (-all.stack_args_size.constant, Pmode)
3912 : expand_expr (size_diffop (all.stack_args_size.var,
3913 size_int (-all.stack_args_size.constant)),
3914 NULL_RTX, VOIDmode, EXPAND_NORMAL));
3916 else
3917 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3919 /* See how many bytes, if any, of its args a function should try to pop
3920 on return. */
3922 crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3923 TREE_TYPE (fndecl),
3924 crtl->args.size);
3926 /* For stdarg.h function, save info about
3927 regs and stack space used by the named args. */
3929 crtl->args.info = all.args_so_far_v;
3931 /* Set the rtx used for the function return value. Put this in its
3932 own variable so any optimizers that need this information don't have
3933 to include tree.h. Do this here so it gets done when an inlined
3934 function gets output. */
3936 crtl->return_rtx
3937 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3938 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3940 /* If scalar return value was computed in a pseudo-reg, or was a named
3941 return value that got dumped to the stack, copy that to the hard
3942 return register. */
3943 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3945 tree decl_result = DECL_RESULT (fndecl);
3946 rtx decl_rtl = DECL_RTL (decl_result);
3948 if (REG_P (decl_rtl)
3949 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3950 : DECL_REGISTER (decl_result))
3952 rtx real_decl_rtl;
3954 real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3955 fndecl, true);
3956 if (chkp_function_instrumented_p (fndecl))
3957 crtl->return_bnd
3958 = targetm.calls.chkp_function_value_bounds (TREE_TYPE (decl_result),
3959 fndecl, true);
3960 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3961 /* The delay slot scheduler assumes that crtl->return_rtx
3962 holds the hard register containing the return value, not a
3963 temporary pseudo. */
3964 crtl->return_rtx = real_decl_rtl;
3969 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3970 For all seen types, gimplify their sizes. */
3972 static tree
3973 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3975 tree t = *tp;
3977 *walk_subtrees = 0;
3978 if (TYPE_P (t))
3980 if (POINTER_TYPE_P (t))
3981 *walk_subtrees = 1;
3982 else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3983 && !TYPE_SIZES_GIMPLIFIED (t))
3985 gimplify_type_sizes (t, (gimple_seq *) data);
3986 *walk_subtrees = 1;
3990 return NULL;
3993 /* Gimplify the parameter list for current_function_decl. This involves
3994 evaluating SAVE_EXPRs of variable sized parameters and generating code
3995 to implement callee-copies reference parameters. Returns a sequence of
3996 statements to add to the beginning of the function. */
3998 gimple_seq
3999 gimplify_parameters (gimple_seq *cleanup)
4001 struct assign_parm_data_all all;
4002 tree parm;
4003 gimple_seq stmts = NULL;
4004 vec<tree> fnargs;
4005 unsigned i;
4007 assign_parms_initialize_all (&all);
4008 fnargs = assign_parms_augmented_arg_list (&all);
4010 FOR_EACH_VEC_ELT (fnargs, i, parm)
4012 struct assign_parm_data_one data;
4014 /* Extract the type of PARM; adjust it according to ABI. */
4015 assign_parm_find_data_types (&all, parm, &data);
4017 /* Early out for errors and void parameters. */
4018 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
4019 continue;
4021 /* Update info on where next arg arrives in registers. */
4022 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
4023 data.passed_type, data.named_arg);
4025 /* ??? Once upon a time variable_size stuffed parameter list
4026 SAVE_EXPRs (amongst others) onto a pending sizes list. This
4027 turned out to be less than manageable in the gimple world.
4028 Now we have to hunt them down ourselves. */
4029 walk_tree_without_duplicates (&data.passed_type,
4030 gimplify_parm_type, &stmts);
4032 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
4034 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
4035 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
4038 if (data.passed_pointer)
4040 tree type = TREE_TYPE (data.passed_type);
4041 if (reference_callee_copied (&all.args_so_far_v, TYPE_MODE (type),
4042 type, data.named_arg))
4044 tree local, t;
4046 /* For constant-sized objects, this is trivial; for
4047 variable-sized objects, we have to play games. */
4048 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
4049 && !(flag_stack_check == GENERIC_STACK_CHECK
4050 && compare_tree_int (DECL_SIZE_UNIT (parm),
4051 STACK_CHECK_MAX_VAR_SIZE) > 0))
4053 local = create_tmp_var (type, get_name (parm));
4054 DECL_IGNORED_P (local) = 0;
4055 /* If PARM was addressable, move that flag over
4056 to the local copy, as its address will be taken,
4057 not the PARMs. Keep the parms address taken
4058 as we'll query that flag during gimplification. */
4059 if (TREE_ADDRESSABLE (parm))
4060 TREE_ADDRESSABLE (local) = 1;
4061 else if (TREE_CODE (type) == COMPLEX_TYPE
4062 || TREE_CODE (type) == VECTOR_TYPE)
4063 DECL_GIMPLE_REG_P (local) = 1;
4065 if (!is_gimple_reg (local)
4066 && flag_stack_reuse != SR_NONE)
4068 tree clobber = build_constructor (type, NULL);
4069 gimple *clobber_stmt;
4070 TREE_THIS_VOLATILE (clobber) = 1;
4071 clobber_stmt = gimple_build_assign (local, clobber);
4072 gimple_seq_add_stmt (cleanup, clobber_stmt);
4075 else
4077 tree ptr_type, addr;
4079 ptr_type = build_pointer_type (type);
4080 addr = create_tmp_reg (ptr_type, get_name (parm));
4081 DECL_IGNORED_P (addr) = 0;
4082 local = build_fold_indirect_ref (addr);
4084 t = build_alloca_call_expr (DECL_SIZE_UNIT (parm),
4085 DECL_ALIGN (parm),
4086 max_int_size_in_bytes (type));
4087 /* The call has been built for a variable-sized object. */
4088 CALL_ALLOCA_FOR_VAR_P (t) = 1;
4089 t = fold_convert (ptr_type, t);
4090 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
4091 gimplify_and_add (t, &stmts);
4094 gimplify_assign (local, parm, &stmts);
4096 SET_DECL_VALUE_EXPR (parm, local);
4097 DECL_HAS_VALUE_EXPR_P (parm) = 1;
4102 fnargs.release ();
4104 return stmts;
4107 /* Compute the size and offset from the start of the stacked arguments for a
4108 parm passed in mode PASSED_MODE and with type TYPE.
4110 INITIAL_OFFSET_PTR points to the current offset into the stacked
4111 arguments.
4113 The starting offset and size for this parm are returned in
4114 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
4115 nonzero, the offset is that of stack slot, which is returned in
4116 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
4117 padding required from the initial offset ptr to the stack slot.
4119 IN_REGS is nonzero if the argument will be passed in registers. It will
4120 never be set if REG_PARM_STACK_SPACE is not defined.
4122 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
4123 for arguments which are passed in registers.
4125 FNDECL is the function in which the argument was defined.
4127 There are two types of rounding that are done. The first, controlled by
4128 TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
4129 argument list to be aligned to the specific boundary (in bits). This
4130 rounding affects the initial and starting offsets, but not the argument
4131 size.
4133 The second, controlled by TARGET_FUNCTION_ARG_PADDING and PARM_BOUNDARY,
4134 optionally rounds the size of the parm to PARM_BOUNDARY. The
4135 initial offset is not affected by this rounding, while the size always
4136 is and the starting offset may be. */
4138 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
4139 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
4140 callers pass in the total size of args so far as
4141 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
4143 void
4144 locate_and_pad_parm (machine_mode passed_mode, tree type, int in_regs,
4145 int reg_parm_stack_space, int partial,
4146 tree fndecl ATTRIBUTE_UNUSED,
4147 struct args_size *initial_offset_ptr,
4148 struct locate_and_pad_arg_data *locate)
4150 tree sizetree;
4151 pad_direction where_pad;
4152 unsigned int boundary, round_boundary;
4153 int part_size_in_regs;
4155 /* If we have found a stack parm before we reach the end of the
4156 area reserved for registers, skip that area. */
4157 if (! in_regs)
4159 if (reg_parm_stack_space > 0)
4161 if (initial_offset_ptr->var
4162 || !ordered_p (initial_offset_ptr->constant,
4163 reg_parm_stack_space))
4165 initial_offset_ptr->var
4166 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
4167 ssize_int (reg_parm_stack_space));
4168 initial_offset_ptr->constant = 0;
4170 else
4171 initial_offset_ptr->constant
4172 = ordered_max (initial_offset_ptr->constant,
4173 reg_parm_stack_space);
4177 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
4179 sizetree = (type
4180 ? arg_size_in_bytes (type)
4181 : size_int (GET_MODE_SIZE (passed_mode)));
4182 where_pad = targetm.calls.function_arg_padding (passed_mode, type);
4183 boundary = targetm.calls.function_arg_boundary (passed_mode, type);
4184 round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
4185 type);
4186 locate->where_pad = where_pad;
4188 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
4189 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4190 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4192 locate->boundary = boundary;
4194 if (SUPPORTS_STACK_ALIGNMENT)
4196 /* stack_alignment_estimated can't change after stack has been
4197 realigned. */
4198 if (crtl->stack_alignment_estimated < boundary)
4200 if (!crtl->stack_realign_processed)
4201 crtl->stack_alignment_estimated = boundary;
4202 else
4204 /* If stack is realigned and stack alignment value
4205 hasn't been finalized, it is OK not to increase
4206 stack_alignment_estimated. The bigger alignment
4207 requirement is recorded in stack_alignment_needed
4208 below. */
4209 gcc_assert (!crtl->stack_realign_finalized
4210 && crtl->stack_realign_needed);
4215 /* Remember if the outgoing parameter requires extra alignment on the
4216 calling function side. */
4217 if (crtl->stack_alignment_needed < boundary)
4218 crtl->stack_alignment_needed = boundary;
4219 if (crtl->preferred_stack_boundary < boundary)
4220 crtl->preferred_stack_boundary = boundary;
4222 if (ARGS_GROW_DOWNWARD)
4224 locate->slot_offset.constant = -initial_offset_ptr->constant;
4225 if (initial_offset_ptr->var)
4226 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
4227 initial_offset_ptr->var);
4230 tree s2 = sizetree;
4231 if (where_pad != PAD_NONE
4232 && (!tree_fits_uhwi_p (sizetree)
4233 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4234 s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
4235 SUB_PARM_SIZE (locate->slot_offset, s2);
4238 locate->slot_offset.constant += part_size_in_regs;
4240 if (!in_regs || reg_parm_stack_space > 0)
4241 pad_to_arg_alignment (&locate->slot_offset, boundary,
4242 &locate->alignment_pad);
4244 locate->size.constant = (-initial_offset_ptr->constant
4245 - locate->slot_offset.constant);
4246 if (initial_offset_ptr->var)
4247 locate->size.var = size_binop (MINUS_EXPR,
4248 size_binop (MINUS_EXPR,
4249 ssize_int (0),
4250 initial_offset_ptr->var),
4251 locate->slot_offset.var);
4253 /* Pad_below needs the pre-rounded size to know how much to pad
4254 below. */
4255 locate->offset = locate->slot_offset;
4256 if (where_pad == PAD_DOWNWARD)
4257 pad_below (&locate->offset, passed_mode, sizetree);
4260 else
4262 if (!in_regs || reg_parm_stack_space > 0)
4263 pad_to_arg_alignment (initial_offset_ptr, boundary,
4264 &locate->alignment_pad);
4265 locate->slot_offset = *initial_offset_ptr;
4267 #ifdef PUSH_ROUNDING
4268 if (passed_mode != BLKmode)
4269 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
4270 #endif
4272 /* Pad_below needs the pre-rounded size to know how much to pad below
4273 so this must be done before rounding up. */
4274 locate->offset = locate->slot_offset;
4275 if (where_pad == PAD_DOWNWARD)
4276 pad_below (&locate->offset, passed_mode, sizetree);
4278 if (where_pad != PAD_NONE
4279 && (!tree_fits_uhwi_p (sizetree)
4280 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4281 sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
4283 ADD_PARM_SIZE (locate->size, sizetree);
4285 locate->size.constant -= part_size_in_regs;
4288 locate->offset.constant
4289 += targetm.calls.function_arg_offset (passed_mode, type);
4292 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
4293 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
4295 static void
4296 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
4297 struct args_size *alignment_pad)
4299 tree save_var = NULL_TREE;
4300 poly_int64 save_constant = 0;
4301 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4302 poly_int64 sp_offset = STACK_POINTER_OFFSET;
4304 #ifdef SPARC_STACK_BOUNDARY_HACK
4305 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
4306 the real alignment of %sp. However, when it does this, the
4307 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
4308 if (SPARC_STACK_BOUNDARY_HACK)
4309 sp_offset = 0;
4310 #endif
4312 if (boundary > PARM_BOUNDARY)
4314 save_var = offset_ptr->var;
4315 save_constant = offset_ptr->constant;
4318 alignment_pad->var = NULL_TREE;
4319 alignment_pad->constant = 0;
4321 if (boundary > BITS_PER_UNIT)
4323 int misalign;
4324 if (offset_ptr->var
4325 || !known_misalignment (offset_ptr->constant + sp_offset,
4326 boundary_in_bytes, &misalign))
4328 tree sp_offset_tree = ssize_int (sp_offset);
4329 tree offset = size_binop (PLUS_EXPR,
4330 ARGS_SIZE_TREE (*offset_ptr),
4331 sp_offset_tree);
4332 tree rounded;
4333 if (ARGS_GROW_DOWNWARD)
4334 rounded = round_down (offset, boundary / BITS_PER_UNIT);
4335 else
4336 rounded = round_up (offset, boundary / BITS_PER_UNIT);
4338 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
4339 /* ARGS_SIZE_TREE includes constant term. */
4340 offset_ptr->constant = 0;
4341 if (boundary > PARM_BOUNDARY)
4342 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
4343 save_var);
4345 else
4347 if (ARGS_GROW_DOWNWARD)
4348 offset_ptr->constant -= misalign;
4349 else
4350 offset_ptr->constant += -misalign & (boundary_in_bytes - 1);
4352 if (boundary > PARM_BOUNDARY)
4353 alignment_pad->constant = offset_ptr->constant - save_constant;
4358 static void
4359 pad_below (struct args_size *offset_ptr, machine_mode passed_mode, tree sizetree)
4361 unsigned int align = PARM_BOUNDARY / BITS_PER_UNIT;
4362 int misalign;
4363 if (passed_mode != BLKmode
4364 && known_misalignment (GET_MODE_SIZE (passed_mode), align, &misalign))
4365 offset_ptr->constant += -misalign & (align - 1);
4366 else
4368 if (TREE_CODE (sizetree) != INTEGER_CST
4369 || (TREE_INT_CST_LOW (sizetree) & (align - 1)) != 0)
4371 /* Round the size up to multiple of PARM_BOUNDARY bits. */
4372 tree s2 = round_up (sizetree, align);
4373 /* Add it in. */
4374 ADD_PARM_SIZE (*offset_ptr, s2);
4375 SUB_PARM_SIZE (*offset_ptr, sizetree);
4381 /* True if register REGNO was alive at a place where `setjmp' was
4382 called and was set more than once or is an argument. Such regs may
4383 be clobbered by `longjmp'. */
4385 static bool
4386 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
4388 /* There appear to be cases where some local vars never reach the
4389 backend but have bogus regnos. */
4390 if (regno >= max_reg_num ())
4391 return false;
4393 return ((REG_N_SETS (regno) > 1
4394 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
4395 regno))
4396 && REGNO_REG_SET_P (setjmp_crosses, regno));
4399 /* Walk the tree of blocks describing the binding levels within a
4400 function and warn about variables the might be killed by setjmp or
4401 vfork. This is done after calling flow_analysis before register
4402 allocation since that will clobber the pseudo-regs to hard
4403 regs. */
4405 static void
4406 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
4408 tree decl, sub;
4410 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
4412 if (VAR_P (decl)
4413 && DECL_RTL_SET_P (decl)
4414 && REG_P (DECL_RTL (decl))
4415 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4416 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
4417 " %<longjmp%> or %<vfork%>", decl);
4420 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
4421 setjmp_vars_warning (setjmp_crosses, sub);
4424 /* Do the appropriate part of setjmp_vars_warning
4425 but for arguments instead of local variables. */
4427 static void
4428 setjmp_args_warning (bitmap setjmp_crosses)
4430 tree decl;
4431 for (decl = DECL_ARGUMENTS (current_function_decl);
4432 decl; decl = DECL_CHAIN (decl))
4433 if (DECL_RTL (decl) != 0
4434 && REG_P (DECL_RTL (decl))
4435 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4436 warning (OPT_Wclobbered,
4437 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
4438 decl);
4441 /* Generate warning messages for variables live across setjmp. */
4443 void
4444 generate_setjmp_warnings (void)
4446 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4448 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS
4449 || bitmap_empty_p (setjmp_crosses))
4450 return;
4452 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4453 setjmp_args_warning (setjmp_crosses);
4457 /* Reverse the order of elements in the fragment chain T of blocks,
4458 and return the new head of the chain (old last element).
4459 In addition to that clear BLOCK_SAME_RANGE flags when needed
4460 and adjust BLOCK_SUPERCONTEXT from the super fragment to
4461 its super fragment origin. */
4463 static tree
4464 block_fragments_nreverse (tree t)
4466 tree prev = 0, block, next, prev_super = 0;
4467 tree super = BLOCK_SUPERCONTEXT (t);
4468 if (BLOCK_FRAGMENT_ORIGIN (super))
4469 super = BLOCK_FRAGMENT_ORIGIN (super);
4470 for (block = t; block; block = next)
4472 next = BLOCK_FRAGMENT_CHAIN (block);
4473 BLOCK_FRAGMENT_CHAIN (block) = prev;
4474 if ((prev && !BLOCK_SAME_RANGE (prev))
4475 || (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (block))
4476 != prev_super))
4477 BLOCK_SAME_RANGE (block) = 0;
4478 prev_super = BLOCK_SUPERCONTEXT (block);
4479 BLOCK_SUPERCONTEXT (block) = super;
4480 prev = block;
4482 t = BLOCK_FRAGMENT_ORIGIN (t);
4483 if (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (t))
4484 != prev_super)
4485 BLOCK_SAME_RANGE (t) = 0;
4486 BLOCK_SUPERCONTEXT (t) = super;
4487 return prev;
4490 /* Reverse the order of elements in the chain T of blocks,
4491 and return the new head of the chain (old last element).
4492 Also do the same on subblocks and reverse the order of elements
4493 in BLOCK_FRAGMENT_CHAIN as well. */
4495 static tree
4496 blocks_nreverse_all (tree t)
4498 tree prev = 0, block, next;
4499 for (block = t; block; block = next)
4501 next = BLOCK_CHAIN (block);
4502 BLOCK_CHAIN (block) = prev;
4503 if (BLOCK_FRAGMENT_CHAIN (block)
4504 && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4506 BLOCK_FRAGMENT_CHAIN (block)
4507 = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4508 if (!BLOCK_SAME_RANGE (BLOCK_FRAGMENT_CHAIN (block)))
4509 BLOCK_SAME_RANGE (block) = 0;
4511 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4512 prev = block;
4514 return prev;
4518 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4519 and create duplicate blocks. */
4520 /* ??? Need an option to either create block fragments or to create
4521 abstract origin duplicates of a source block. It really depends
4522 on what optimization has been performed. */
4524 void
4525 reorder_blocks (void)
4527 tree block = DECL_INITIAL (current_function_decl);
4529 if (block == NULL_TREE)
4530 return;
4532 auto_vec<tree, 10> block_stack;
4534 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4535 clear_block_marks (block);
4537 /* Prune the old trees away, so that they don't get in the way. */
4538 BLOCK_SUBBLOCKS (block) = NULL_TREE;
4539 BLOCK_CHAIN (block) = NULL_TREE;
4541 /* Recreate the block tree from the note nesting. */
4542 reorder_blocks_1 (get_insns (), block, &block_stack);
4543 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4546 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
4548 void
4549 clear_block_marks (tree block)
4551 while (block)
4553 TREE_ASM_WRITTEN (block) = 0;
4554 clear_block_marks (BLOCK_SUBBLOCKS (block));
4555 block = BLOCK_CHAIN (block);
4559 static void
4560 reorder_blocks_1 (rtx_insn *insns, tree current_block,
4561 vec<tree> *p_block_stack)
4563 rtx_insn *insn;
4564 tree prev_beg = NULL_TREE, prev_end = NULL_TREE;
4566 for (insn = insns; insn; insn = NEXT_INSN (insn))
4568 if (NOTE_P (insn))
4570 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4572 tree block = NOTE_BLOCK (insn);
4573 tree origin;
4575 gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4576 origin = block;
4578 if (prev_end)
4579 BLOCK_SAME_RANGE (prev_end) = 0;
4580 prev_end = NULL_TREE;
4582 /* If we have seen this block before, that means it now
4583 spans multiple address regions. Create a new fragment. */
4584 if (TREE_ASM_WRITTEN (block))
4586 tree new_block = copy_node (block);
4588 BLOCK_SAME_RANGE (new_block) = 0;
4589 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4590 BLOCK_FRAGMENT_CHAIN (new_block)
4591 = BLOCK_FRAGMENT_CHAIN (origin);
4592 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4594 NOTE_BLOCK (insn) = new_block;
4595 block = new_block;
4598 if (prev_beg == current_block && prev_beg)
4599 BLOCK_SAME_RANGE (block) = 1;
4601 prev_beg = origin;
4603 BLOCK_SUBBLOCKS (block) = 0;
4604 TREE_ASM_WRITTEN (block) = 1;
4605 /* When there's only one block for the entire function,
4606 current_block == block and we mustn't do this, it
4607 will cause infinite recursion. */
4608 if (block != current_block)
4610 tree super;
4611 if (block != origin)
4612 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block
4613 || BLOCK_FRAGMENT_ORIGIN (BLOCK_SUPERCONTEXT
4614 (origin))
4615 == current_block);
4616 if (p_block_stack->is_empty ())
4617 super = current_block;
4618 else
4620 super = p_block_stack->last ();
4621 gcc_assert (super == current_block
4622 || BLOCK_FRAGMENT_ORIGIN (super)
4623 == current_block);
4625 BLOCK_SUPERCONTEXT (block) = super;
4626 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4627 BLOCK_SUBBLOCKS (current_block) = block;
4628 current_block = origin;
4630 p_block_stack->safe_push (block);
4632 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4634 NOTE_BLOCK (insn) = p_block_stack->pop ();
4635 current_block = BLOCK_SUPERCONTEXT (current_block);
4636 if (BLOCK_FRAGMENT_ORIGIN (current_block))
4637 current_block = BLOCK_FRAGMENT_ORIGIN (current_block);
4638 prev_beg = NULL_TREE;
4639 prev_end = BLOCK_SAME_RANGE (NOTE_BLOCK (insn))
4640 ? NOTE_BLOCK (insn) : NULL_TREE;
4643 else
4645 prev_beg = NULL_TREE;
4646 if (prev_end)
4647 BLOCK_SAME_RANGE (prev_end) = 0;
4648 prev_end = NULL_TREE;
4653 /* Reverse the order of elements in the chain T of blocks,
4654 and return the new head of the chain (old last element). */
4656 tree
4657 blocks_nreverse (tree t)
4659 tree prev = 0, block, next;
4660 for (block = t; block; block = next)
4662 next = BLOCK_CHAIN (block);
4663 BLOCK_CHAIN (block) = prev;
4664 prev = block;
4666 return prev;
4669 /* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4670 by modifying the last node in chain 1 to point to chain 2. */
4672 tree
4673 block_chainon (tree op1, tree op2)
4675 tree t1;
4677 if (!op1)
4678 return op2;
4679 if (!op2)
4680 return op1;
4682 for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4683 continue;
4684 BLOCK_CHAIN (t1) = op2;
4686 #ifdef ENABLE_TREE_CHECKING
4688 tree t2;
4689 for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4690 gcc_assert (t2 != t1);
4692 #endif
4694 return op1;
4697 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
4698 non-NULL, list them all into VECTOR, in a depth-first preorder
4699 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
4700 blocks. */
4702 static int
4703 all_blocks (tree block, tree *vector)
4705 int n_blocks = 0;
4707 while (block)
4709 TREE_ASM_WRITTEN (block) = 0;
4711 /* Record this block. */
4712 if (vector)
4713 vector[n_blocks] = block;
4715 ++n_blocks;
4717 /* Record the subblocks, and their subblocks... */
4718 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4719 vector ? vector + n_blocks : 0);
4720 block = BLOCK_CHAIN (block);
4723 return n_blocks;
4726 /* Return a vector containing all the blocks rooted at BLOCK. The
4727 number of elements in the vector is stored in N_BLOCKS_P. The
4728 vector is dynamically allocated; it is the caller's responsibility
4729 to call `free' on the pointer returned. */
4731 static tree *
4732 get_block_vector (tree block, int *n_blocks_p)
4734 tree *block_vector;
4736 *n_blocks_p = all_blocks (block, NULL);
4737 block_vector = XNEWVEC (tree, *n_blocks_p);
4738 all_blocks (block, block_vector);
4740 return block_vector;
4743 static GTY(()) int next_block_index = 2;
4745 /* Set BLOCK_NUMBER for all the blocks in FN. */
4747 void
4748 number_blocks (tree fn)
4750 int i;
4751 int n_blocks;
4752 tree *block_vector;
4754 /* For XCOFF debugging output, we start numbering the blocks
4755 from 1 within each function, rather than keeping a running
4756 count. */
4757 #if defined (XCOFF_DEBUGGING_INFO)
4758 if (write_symbols == XCOFF_DEBUG)
4759 next_block_index = 1;
4760 #endif
4762 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4764 /* The top-level BLOCK isn't numbered at all. */
4765 for (i = 1; i < n_blocks; ++i)
4766 /* We number the blocks from two. */
4767 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4769 free (block_vector);
4771 return;
4774 /* If VAR is present in a subblock of BLOCK, return the subblock. */
4776 DEBUG_FUNCTION tree
4777 debug_find_var_in_block_tree (tree var, tree block)
4779 tree t;
4781 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4782 if (t == var)
4783 return block;
4785 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4787 tree ret = debug_find_var_in_block_tree (var, t);
4788 if (ret)
4789 return ret;
4792 return NULL_TREE;
4795 /* Keep track of whether we're in a dummy function context. If we are,
4796 we don't want to invoke the set_current_function hook, because we'll
4797 get into trouble if the hook calls target_reinit () recursively or
4798 when the initial initialization is not yet complete. */
4800 static bool in_dummy_function;
4802 /* Invoke the target hook when setting cfun. Update the optimization options
4803 if the function uses different options than the default. */
4805 static void
4806 invoke_set_current_function_hook (tree fndecl)
4808 if (!in_dummy_function)
4810 tree opts = ((fndecl)
4811 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4812 : optimization_default_node);
4814 if (!opts)
4815 opts = optimization_default_node;
4817 /* Change optimization options if needed. */
4818 if (optimization_current_node != opts)
4820 optimization_current_node = opts;
4821 cl_optimization_restore (&global_options, TREE_OPTIMIZATION (opts));
4824 targetm.set_current_function (fndecl);
4825 this_fn_optabs = this_target_optabs;
4827 if (opts != optimization_default_node)
4829 init_tree_optimization_optabs (opts);
4830 if (TREE_OPTIMIZATION_OPTABS (opts))
4831 this_fn_optabs = (struct target_optabs *)
4832 TREE_OPTIMIZATION_OPTABS (opts);
4837 /* cfun should never be set directly; use this function. */
4839 void
4840 set_cfun (struct function *new_cfun, bool force)
4842 if (cfun != new_cfun || force)
4844 cfun = new_cfun;
4845 invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4846 redirect_edge_var_map_empty ();
4850 /* Initialized with NOGC, making this poisonous to the garbage collector. */
4852 static vec<function *> cfun_stack;
4854 /* Push the current cfun onto the stack, and set cfun to new_cfun. Also set
4855 current_function_decl accordingly. */
4857 void
4858 push_cfun (struct function *new_cfun)
4860 gcc_assert ((!cfun && !current_function_decl)
4861 || (cfun && current_function_decl == cfun->decl));
4862 cfun_stack.safe_push (cfun);
4863 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4864 set_cfun (new_cfun);
4867 /* Pop cfun from the stack. Also set current_function_decl accordingly. */
4869 void
4870 pop_cfun (void)
4872 struct function *new_cfun = cfun_stack.pop ();
4873 /* When in_dummy_function, we do have a cfun but current_function_decl is
4874 NULL. We also allow pushing NULL cfun and subsequently changing
4875 current_function_decl to something else and have both restored by
4876 pop_cfun. */
4877 gcc_checking_assert (in_dummy_function
4878 || !cfun
4879 || current_function_decl == cfun->decl);
4880 set_cfun (new_cfun);
4881 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4884 /* Return value of funcdef and increase it. */
4886 get_next_funcdef_no (void)
4888 return funcdef_no++;
4891 /* Return value of funcdef. */
4893 get_last_funcdef_no (void)
4895 return funcdef_no;
4898 /* Allocate a function structure for FNDECL and set its contents
4899 to the defaults. Set cfun to the newly-allocated object.
4900 Some of the helper functions invoked during initialization assume
4901 that cfun has already been set. Therefore, assign the new object
4902 directly into cfun and invoke the back end hook explicitly at the
4903 very end, rather than initializing a temporary and calling set_cfun
4904 on it.
4906 ABSTRACT_P is true if this is a function that will never be seen by
4907 the middle-end. Such functions are front-end concepts (like C++
4908 function templates) that do not correspond directly to functions
4909 placed in object files. */
4911 void
4912 allocate_struct_function (tree fndecl, bool abstract_p)
4914 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4916 cfun = ggc_cleared_alloc<function> ();
4918 init_eh_for_function ();
4920 if (init_machine_status)
4921 cfun->machine = (*init_machine_status) ();
4923 #ifdef OVERRIDE_ABI_FORMAT
4924 OVERRIDE_ABI_FORMAT (fndecl);
4925 #endif
4927 if (fndecl != NULL_TREE)
4929 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4930 cfun->decl = fndecl;
4931 current_function_funcdef_no = get_next_funcdef_no ();
4934 invoke_set_current_function_hook (fndecl);
4936 if (fndecl != NULL_TREE)
4938 tree result = DECL_RESULT (fndecl);
4940 if (!abstract_p)
4942 /* Now that we have activated any function-specific attributes
4943 that might affect layout, particularly vector modes, relayout
4944 each of the parameters and the result. */
4945 relayout_decl (result);
4946 for (tree parm = DECL_ARGUMENTS (fndecl); parm;
4947 parm = DECL_CHAIN (parm))
4948 relayout_decl (parm);
4950 /* Similarly relayout the function decl. */
4951 targetm.target_option.relayout_function (fndecl);
4954 if (!abstract_p && aggregate_value_p (result, fndecl))
4956 #ifdef PCC_STATIC_STRUCT_RETURN
4957 cfun->returns_pcc_struct = 1;
4958 #endif
4959 cfun->returns_struct = 1;
4962 cfun->stdarg = stdarg_p (fntype);
4964 /* Assume all registers in stdarg functions need to be saved. */
4965 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4966 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4968 /* ??? This could be set on a per-function basis by the front-end
4969 but is this worth the hassle? */
4970 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4971 cfun->can_delete_dead_exceptions = flag_delete_dead_exceptions;
4973 if (!profile_flag && !flag_instrument_function_entry_exit)
4974 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl) = 1;
4977 /* Don't enable begin stmt markers if var-tracking at assignments is
4978 disabled. The markers make little sense without the variable
4979 binding annotations among them. */
4980 cfun->debug_nonbind_markers = lang_hooks.emits_begin_stmt
4981 && MAY_HAVE_DEBUG_MARKER_STMTS;
4984 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4985 instead of just setting it. */
4987 void
4988 push_struct_function (tree fndecl)
4990 /* When in_dummy_function we might be in the middle of a pop_cfun and
4991 current_function_decl and cfun may not match. */
4992 gcc_assert (in_dummy_function
4993 || (!cfun && !current_function_decl)
4994 || (cfun && current_function_decl == cfun->decl));
4995 cfun_stack.safe_push (cfun);
4996 current_function_decl = fndecl;
4997 allocate_struct_function (fndecl, false);
5000 /* Reset crtl and other non-struct-function variables to defaults as
5001 appropriate for emitting rtl at the start of a function. */
5003 static void
5004 prepare_function_start (void)
5006 gcc_assert (!get_last_insn ());
5007 init_temp_slots ();
5008 init_emit ();
5009 init_varasm_status ();
5010 init_expr ();
5011 default_rtl_profile ();
5013 if (flag_stack_usage_info)
5015 cfun->su = ggc_cleared_alloc<stack_usage> ();
5016 cfun->su->static_stack_size = -1;
5019 cse_not_expected = ! optimize;
5021 /* Caller save not needed yet. */
5022 caller_save_needed = 0;
5024 /* We haven't done register allocation yet. */
5025 reg_renumber = 0;
5027 /* Indicate that we have not instantiated virtual registers yet. */
5028 virtuals_instantiated = 0;
5030 /* Indicate that we want CONCATs now. */
5031 generating_concat_p = 1;
5033 /* Indicate we have no need of a frame pointer yet. */
5034 frame_pointer_needed = 0;
5037 void
5038 push_dummy_function (bool with_decl)
5040 tree fn_decl, fn_type, fn_result_decl;
5042 gcc_assert (!in_dummy_function);
5043 in_dummy_function = true;
5045 if (with_decl)
5047 fn_type = build_function_type_list (void_type_node, NULL_TREE);
5048 fn_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
5049 fn_type);
5050 fn_result_decl = build_decl (UNKNOWN_LOCATION, RESULT_DECL,
5051 NULL_TREE, void_type_node);
5052 DECL_RESULT (fn_decl) = fn_result_decl;
5054 else
5055 fn_decl = NULL_TREE;
5057 push_struct_function (fn_decl);
5060 /* Initialize the rtl expansion mechanism so that we can do simple things
5061 like generate sequences. This is used to provide a context during global
5062 initialization of some passes. You must call expand_dummy_function_end
5063 to exit this context. */
5065 void
5066 init_dummy_function_start (void)
5068 push_dummy_function (false);
5069 prepare_function_start ();
5072 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
5073 and initialize static variables for generating RTL for the statements
5074 of the function. */
5076 void
5077 init_function_start (tree subr)
5079 /* Initialize backend, if needed. */
5080 initialize_rtl ();
5082 prepare_function_start ();
5083 decide_function_section (subr);
5085 /* Warn if this value is an aggregate type,
5086 regardless of which calling convention we are using for it. */
5087 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
5088 warning (OPT_Waggregate_return, "function returns an aggregate");
5091 /* Expand code to verify the stack_protect_guard. This is invoked at
5092 the end of a function to be protected. */
5094 void
5095 stack_protect_epilogue (void)
5097 tree guard_decl = targetm.stack_protect_guard ();
5098 rtx_code_label *label = gen_label_rtx ();
5099 rtx x, y;
5100 rtx_insn *seq;
5102 x = expand_normal (crtl->stack_protect_guard);
5103 if (guard_decl)
5104 y = expand_normal (guard_decl);
5105 else
5106 y = const0_rtx;
5108 /* Allow the target to compare Y with X without leaking either into
5109 a register. */
5110 if (targetm.have_stack_protect_test ()
5111 && ((seq = targetm.gen_stack_protect_test (x, y, label)) != NULL_RTX))
5112 emit_insn (seq);
5113 else
5114 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
5116 /* The noreturn predictor has been moved to the tree level. The rtl-level
5117 predictors estimate this branch about 20%, which isn't enough to get
5118 things moved out of line. Since this is the only extant case of adding
5119 a noreturn function at the rtl level, it doesn't seem worth doing ought
5120 except adding the prediction by hand. */
5121 rtx_insn *tmp = get_last_insn ();
5122 if (JUMP_P (tmp))
5123 predict_insn_def (tmp, PRED_NORETURN, TAKEN);
5125 expand_call (targetm.stack_protect_fail (), NULL_RTX, /*ignore=*/true);
5126 free_temp_slots ();
5127 emit_label (label);
5130 /* Start the RTL for a new function, and set variables used for
5131 emitting RTL.
5132 SUBR is the FUNCTION_DECL node.
5133 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
5134 the function's parameters, which must be run at any return statement. */
5136 void
5137 expand_function_start (tree subr)
5139 /* Make sure volatile mem refs aren't considered
5140 valid operands of arithmetic insns. */
5141 init_recog_no_volatile ();
5143 crtl->profile
5144 = (profile_flag
5145 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
5147 crtl->limit_stack
5148 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
5150 /* Make the label for return statements to jump to. Do not special
5151 case machines with special return instructions -- they will be
5152 handled later during jump, ifcvt, or epilogue creation. */
5153 return_label = gen_label_rtx ();
5155 /* Initialize rtx used to return the value. */
5156 /* Do this before assign_parms so that we copy the struct value address
5157 before any library calls that assign parms might generate. */
5159 /* Decide whether to return the value in memory or in a register. */
5160 tree res = DECL_RESULT (subr);
5161 if (aggregate_value_p (res, subr))
5163 /* Returning something that won't go in a register. */
5164 rtx value_address = 0;
5166 #ifdef PCC_STATIC_STRUCT_RETURN
5167 if (cfun->returns_pcc_struct)
5169 int size = int_size_in_bytes (TREE_TYPE (res));
5170 value_address = assemble_static_space (size);
5172 else
5173 #endif
5175 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
5176 /* Expect to be passed the address of a place to store the value.
5177 If it is passed as an argument, assign_parms will take care of
5178 it. */
5179 if (sv)
5181 value_address = gen_reg_rtx (Pmode);
5182 emit_move_insn (value_address, sv);
5185 if (value_address)
5187 rtx x = value_address;
5188 if (!DECL_BY_REFERENCE (res))
5190 x = gen_rtx_MEM (DECL_MODE (res), x);
5191 set_mem_attributes (x, res, 1);
5193 set_parm_rtl (res, x);
5196 else if (DECL_MODE (res) == VOIDmode)
5197 /* If return mode is void, this decl rtl should not be used. */
5198 set_parm_rtl (res, NULL_RTX);
5199 else
5201 /* Compute the return values into a pseudo reg, which we will copy
5202 into the true return register after the cleanups are done. */
5203 tree return_type = TREE_TYPE (res);
5205 /* If we may coalesce this result, make sure it has the expected mode
5206 in case it was promoted. But we need not bother about BLKmode. */
5207 machine_mode promoted_mode
5208 = flag_tree_coalesce_vars && is_gimple_reg (res)
5209 ? promote_ssa_mode (ssa_default_def (cfun, res), NULL)
5210 : BLKmode;
5212 if (promoted_mode != BLKmode)
5213 set_parm_rtl (res, gen_reg_rtx (promoted_mode));
5214 else if (TYPE_MODE (return_type) != BLKmode
5215 && targetm.calls.return_in_msb (return_type))
5216 /* expand_function_end will insert the appropriate padding in
5217 this case. Use the return value's natural (unpadded) mode
5218 within the function proper. */
5219 set_parm_rtl (res, gen_reg_rtx (TYPE_MODE (return_type)));
5220 else
5222 /* In order to figure out what mode to use for the pseudo, we
5223 figure out what the mode of the eventual return register will
5224 actually be, and use that. */
5225 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
5227 /* Structures that are returned in registers are not
5228 aggregate_value_p, so we may see a PARALLEL or a REG. */
5229 if (REG_P (hard_reg))
5230 set_parm_rtl (res, gen_reg_rtx (GET_MODE (hard_reg)));
5231 else
5233 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
5234 set_parm_rtl (res, gen_group_rtx (hard_reg));
5238 /* Set DECL_REGISTER flag so that expand_function_end will copy the
5239 result to the real return register(s). */
5240 DECL_REGISTER (res) = 1;
5242 if (chkp_function_instrumented_p (current_function_decl))
5244 tree return_type = TREE_TYPE (res);
5245 rtx bounds = targetm.calls.chkp_function_value_bounds (return_type,
5246 subr, 1);
5247 SET_DECL_BOUNDS_RTL (res, bounds);
5251 /* Initialize rtx for parameters and local variables.
5252 In some cases this requires emitting insns. */
5253 assign_parms (subr);
5255 /* If function gets a static chain arg, store it. */
5256 if (cfun->static_chain_decl)
5258 tree parm = cfun->static_chain_decl;
5259 rtx local, chain;
5260 rtx_insn *insn;
5261 int unsignedp;
5263 local = gen_reg_rtx (promote_decl_mode (parm, &unsignedp));
5264 chain = targetm.calls.static_chain (current_function_decl, true);
5266 set_decl_incoming_rtl (parm, chain, false);
5267 set_parm_rtl (parm, local);
5268 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
5270 if (GET_MODE (local) != GET_MODE (chain))
5272 convert_move (local, chain, unsignedp);
5273 insn = get_last_insn ();
5275 else
5276 insn = emit_move_insn (local, chain);
5278 /* Mark the register as eliminable, similar to parameters. */
5279 if (MEM_P (chain)
5280 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
5281 set_dst_reg_note (insn, REG_EQUIV, chain, local);
5283 /* If we aren't optimizing, save the static chain onto the stack. */
5284 if (!optimize)
5286 tree saved_static_chain_decl
5287 = build_decl (DECL_SOURCE_LOCATION (parm), VAR_DECL,
5288 DECL_NAME (parm), TREE_TYPE (parm));
5289 rtx saved_static_chain_rtx
5290 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5291 SET_DECL_RTL (saved_static_chain_decl, saved_static_chain_rtx);
5292 emit_move_insn (saved_static_chain_rtx, chain);
5293 SET_DECL_VALUE_EXPR (parm, saved_static_chain_decl);
5294 DECL_HAS_VALUE_EXPR_P (parm) = 1;
5298 /* The following was moved from init_function_start.
5299 The move was supposed to make sdb output more accurate. */
5300 /* Indicate the beginning of the function body,
5301 as opposed to parm setup. */
5302 emit_note (NOTE_INSN_FUNCTION_BEG);
5304 gcc_assert (NOTE_P (get_last_insn ()));
5306 parm_birth_insn = get_last_insn ();
5308 /* If the function receives a non-local goto, then store the
5309 bits we need to restore the frame pointer. */
5310 if (cfun->nonlocal_goto_save_area)
5312 tree t_save;
5313 rtx r_save;
5315 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
5316 gcc_assert (DECL_RTL_SET_P (var));
5318 t_save = build4 (ARRAY_REF,
5319 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
5320 cfun->nonlocal_goto_save_area,
5321 integer_zero_node, NULL_TREE, NULL_TREE);
5322 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
5323 gcc_assert (GET_MODE (r_save) == Pmode);
5325 emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
5326 update_nonlocal_goto_save_area ();
5329 if (crtl->profile)
5331 #ifdef PROFILE_HOOK
5332 PROFILE_HOOK (current_function_funcdef_no);
5333 #endif
5336 /* If we are doing generic stack checking, the probe should go here. */
5337 if (flag_stack_check == GENERIC_STACK_CHECK)
5338 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
5341 void
5342 pop_dummy_function (void)
5344 pop_cfun ();
5345 in_dummy_function = false;
5348 /* Undo the effects of init_dummy_function_start. */
5349 void
5350 expand_dummy_function_end (void)
5352 gcc_assert (in_dummy_function);
5354 /* End any sequences that failed to be closed due to syntax errors. */
5355 while (in_sequence_p ())
5356 end_sequence ();
5358 /* Outside function body, can't compute type's actual size
5359 until next function's body starts. */
5361 free_after_parsing (cfun);
5362 free_after_compilation (cfun);
5363 pop_dummy_function ();
5366 /* Helper for diddle_return_value. */
5368 void
5369 diddle_return_value_1 (void (*doit) (rtx, void *), void *arg, rtx outgoing)
5371 if (! outgoing)
5372 return;
5374 if (REG_P (outgoing))
5375 (*doit) (outgoing, arg);
5376 else if (GET_CODE (outgoing) == PARALLEL)
5378 int i;
5380 for (i = 0; i < XVECLEN (outgoing, 0); i++)
5382 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
5384 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
5385 (*doit) (x, arg);
5390 /* Call DOIT for each hard register used as a return value from
5391 the current function. */
5393 void
5394 diddle_return_value (void (*doit) (rtx, void *), void *arg)
5396 diddle_return_value_1 (doit, arg, crtl->return_bnd);
5397 diddle_return_value_1 (doit, arg, crtl->return_rtx);
5400 static void
5401 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5403 emit_clobber (reg);
5406 void
5407 clobber_return_register (void)
5409 diddle_return_value (do_clobber_return_reg, NULL);
5411 /* In case we do use pseudo to return value, clobber it too. */
5412 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5414 tree decl_result = DECL_RESULT (current_function_decl);
5415 rtx decl_rtl = DECL_RTL (decl_result);
5416 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
5418 do_clobber_return_reg (decl_rtl, NULL);
5423 static void
5424 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5426 emit_use (reg);
5429 static void
5430 use_return_register (void)
5432 diddle_return_value (do_use_return_reg, NULL);
5435 /* Set the location of the insn chain starting at INSN to LOC. */
5437 static void
5438 set_insn_locations (rtx_insn *insn, int loc)
5440 while (insn != NULL)
5442 if (INSN_P (insn))
5443 INSN_LOCATION (insn) = loc;
5444 insn = NEXT_INSN (insn);
5448 /* Generate RTL for the end of the current function. */
5450 void
5451 expand_function_end (void)
5453 /* If arg_pointer_save_area was referenced only from a nested
5454 function, we will not have initialized it yet. Do that now. */
5455 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
5456 get_arg_pointer_save_area ();
5458 /* If we are doing generic stack checking and this function makes calls,
5459 do a stack probe at the start of the function to ensure we have enough
5460 space for another stack frame. */
5461 if (flag_stack_check == GENERIC_STACK_CHECK)
5463 rtx_insn *insn, *seq;
5465 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5466 if (CALL_P (insn))
5468 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
5469 start_sequence ();
5470 if (STACK_CHECK_MOVING_SP)
5471 anti_adjust_stack_and_probe (max_frame_size, true);
5472 else
5473 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
5474 seq = get_insns ();
5475 end_sequence ();
5476 set_insn_locations (seq, prologue_location);
5477 emit_insn_before (seq, stack_check_probe_note);
5478 break;
5482 /* End any sequences that failed to be closed due to syntax errors. */
5483 while (in_sequence_p ())
5484 end_sequence ();
5486 clear_pending_stack_adjust ();
5487 do_pending_stack_adjust ();
5489 /* Output a linenumber for the end of the function.
5490 SDB depended on this. */
5491 set_curr_insn_location (input_location);
5493 /* Before the return label (if any), clobber the return
5494 registers so that they are not propagated live to the rest of
5495 the function. This can only happen with functions that drop
5496 through; if there had been a return statement, there would
5497 have either been a return rtx, or a jump to the return label.
5499 We delay actual code generation after the current_function_value_rtx
5500 is computed. */
5501 rtx_insn *clobber_after = get_last_insn ();
5503 /* Output the label for the actual return from the function. */
5504 emit_label (return_label);
5506 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
5508 /* Let except.c know where it should emit the call to unregister
5509 the function context for sjlj exceptions. */
5510 if (flag_exceptions)
5511 sjlj_emit_function_exit_after (get_last_insn ());
5513 else
5515 /* We want to ensure that instructions that may trap are not
5516 moved into the epilogue by scheduling, because we don't
5517 always emit unwind information for the epilogue. */
5518 if (cfun->can_throw_non_call_exceptions)
5519 emit_insn (gen_blockage ());
5522 /* If this is an implementation of throw, do what's necessary to
5523 communicate between __builtin_eh_return and the epilogue. */
5524 expand_eh_return ();
5526 /* If scalar return value was computed in a pseudo-reg, or was a named
5527 return value that got dumped to the stack, copy that to the hard
5528 return register. */
5529 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5531 tree decl_result = DECL_RESULT (current_function_decl);
5532 rtx decl_rtl = DECL_RTL (decl_result);
5534 if (REG_P (decl_rtl)
5535 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5536 : DECL_REGISTER (decl_result))
5538 rtx real_decl_rtl = crtl->return_rtx;
5539 complex_mode cmode;
5541 /* This should be set in assign_parms. */
5542 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5544 /* If this is a BLKmode structure being returned in registers,
5545 then use the mode computed in expand_return. Note that if
5546 decl_rtl is memory, then its mode may have been changed,
5547 but that crtl->return_rtx has not. */
5548 if (GET_MODE (real_decl_rtl) == BLKmode)
5549 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
5551 /* If a non-BLKmode return value should be padded at the least
5552 significant end of the register, shift it left by the appropriate
5553 amount. BLKmode results are handled using the group load/store
5554 machinery. */
5555 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5556 && REG_P (real_decl_rtl)
5557 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5559 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5560 REGNO (real_decl_rtl)),
5561 decl_rtl);
5562 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5564 else if (GET_CODE (real_decl_rtl) == PARALLEL)
5566 /* If expand_function_start has created a PARALLEL for decl_rtl,
5567 move the result to the real return registers. Otherwise, do
5568 a group load from decl_rtl for a named return. */
5569 if (GET_CODE (decl_rtl) == PARALLEL)
5570 emit_group_move (real_decl_rtl, decl_rtl);
5571 else
5572 emit_group_load (real_decl_rtl, decl_rtl,
5573 TREE_TYPE (decl_result),
5574 int_size_in_bytes (TREE_TYPE (decl_result)));
5576 /* In the case of complex integer modes smaller than a word, we'll
5577 need to generate some non-trivial bitfield insertions. Do that
5578 on a pseudo and not the hard register. */
5579 else if (GET_CODE (decl_rtl) == CONCAT
5580 && is_complex_int_mode (GET_MODE (decl_rtl), &cmode)
5581 && GET_MODE_BITSIZE (cmode) <= BITS_PER_WORD)
5583 int old_generating_concat_p;
5584 rtx tmp;
5586 old_generating_concat_p = generating_concat_p;
5587 generating_concat_p = 0;
5588 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5589 generating_concat_p = old_generating_concat_p;
5591 emit_move_insn (tmp, decl_rtl);
5592 emit_move_insn (real_decl_rtl, tmp);
5594 /* If a named return value dumped decl_return to memory, then
5595 we may need to re-do the PROMOTE_MODE signed/unsigned
5596 extension. */
5597 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5599 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5600 promote_function_mode (TREE_TYPE (decl_result),
5601 GET_MODE (decl_rtl), &unsignedp,
5602 TREE_TYPE (current_function_decl), 1);
5604 convert_move (real_decl_rtl, decl_rtl, unsignedp);
5606 else
5607 emit_move_insn (real_decl_rtl, decl_rtl);
5611 /* If returning a structure, arrange to return the address of the value
5612 in a place where debuggers expect to find it.
5614 If returning a structure PCC style,
5615 the caller also depends on this value.
5616 And cfun->returns_pcc_struct is not necessarily set. */
5617 if ((cfun->returns_struct || cfun->returns_pcc_struct)
5618 && !targetm.calls.omit_struct_return_reg)
5620 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5621 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5622 rtx outgoing;
5624 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5625 type = TREE_TYPE (type);
5626 else
5627 value_address = XEXP (value_address, 0);
5629 outgoing = targetm.calls.function_value (build_pointer_type (type),
5630 current_function_decl, true);
5632 /* Mark this as a function return value so integrate will delete the
5633 assignment and USE below when inlining this function. */
5634 REG_FUNCTION_VALUE_P (outgoing) = 1;
5636 /* The address may be ptr_mode and OUTGOING may be Pmode. */
5637 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (outgoing));
5638 value_address = convert_memory_address (mode, value_address);
5640 emit_move_insn (outgoing, value_address);
5642 /* Show return register used to hold result (in this case the address
5643 of the result. */
5644 crtl->return_rtx = outgoing;
5647 /* Emit the actual code to clobber return register. Don't emit
5648 it if clobber_after is a barrier, then the previous basic block
5649 certainly doesn't fall thru into the exit block. */
5650 if (!BARRIER_P (clobber_after))
5652 start_sequence ();
5653 clobber_return_register ();
5654 rtx_insn *seq = get_insns ();
5655 end_sequence ();
5657 emit_insn_after (seq, clobber_after);
5660 /* Output the label for the naked return from the function. */
5661 if (naked_return_label)
5662 emit_label (naked_return_label);
5664 /* @@@ This is a kludge. We want to ensure that instructions that
5665 may trap are not moved into the epilogue by scheduling, because
5666 we don't always emit unwind information for the epilogue. */
5667 if (cfun->can_throw_non_call_exceptions
5668 && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5669 emit_insn (gen_blockage ());
5671 /* If stack protection is enabled for this function, check the guard. */
5672 if (crtl->stack_protect_guard && targetm.stack_protect_runtime_enabled_p ())
5673 stack_protect_epilogue ();
5675 /* If we had calls to alloca, and this machine needs
5676 an accurate stack pointer to exit the function,
5677 insert some code to save and restore the stack pointer. */
5678 if (! EXIT_IGNORE_STACK
5679 && cfun->calls_alloca)
5681 rtx tem = 0;
5683 start_sequence ();
5684 emit_stack_save (SAVE_FUNCTION, &tem);
5685 rtx_insn *seq = get_insns ();
5686 end_sequence ();
5687 emit_insn_before (seq, parm_birth_insn);
5689 emit_stack_restore (SAVE_FUNCTION, tem);
5692 /* ??? This should no longer be necessary since stupid is no longer with
5693 us, but there are some parts of the compiler (eg reload_combine, and
5694 sh mach_dep_reorg) that still try and compute their own lifetime info
5695 instead of using the general framework. */
5696 use_return_register ();
5700 get_arg_pointer_save_area (void)
5702 rtx ret = arg_pointer_save_area;
5704 if (! ret)
5706 ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5707 arg_pointer_save_area = ret;
5710 if (! crtl->arg_pointer_save_area_init)
5712 /* Save the arg pointer at the beginning of the function. The
5713 generated stack slot may not be a valid memory address, so we
5714 have to check it and fix it if necessary. */
5715 start_sequence ();
5716 emit_move_insn (validize_mem (copy_rtx (ret)),
5717 crtl->args.internal_arg_pointer);
5718 rtx_insn *seq = get_insns ();
5719 end_sequence ();
5721 push_topmost_sequence ();
5722 emit_insn_after (seq, entry_of_function ());
5723 pop_topmost_sequence ();
5725 crtl->arg_pointer_save_area_init = true;
5728 return ret;
5732 /* If debugging dumps are requested, dump information about how the
5733 target handled -fstack-check=clash for the prologue.
5735 PROBES describes what if any probes were emitted.
5737 RESIDUALS indicates if the prologue had any residual allocation
5738 (i.e. total allocation was not a multiple of PROBE_INTERVAL). */
5740 void
5741 dump_stack_clash_frame_info (enum stack_clash_probes probes, bool residuals)
5743 if (!dump_file)
5744 return;
5746 switch (probes)
5748 case NO_PROBE_NO_FRAME:
5749 fprintf (dump_file,
5750 "Stack clash no probe no stack adjustment in prologue.\n");
5751 break;
5752 case NO_PROBE_SMALL_FRAME:
5753 fprintf (dump_file,
5754 "Stack clash no probe small stack adjustment in prologue.\n");
5755 break;
5756 case PROBE_INLINE:
5757 fprintf (dump_file, "Stack clash inline probes in prologue.\n");
5758 break;
5759 case PROBE_LOOP:
5760 fprintf (dump_file, "Stack clash probe loop in prologue.\n");
5761 break;
5764 if (residuals)
5765 fprintf (dump_file, "Stack clash residual allocation in prologue.\n");
5766 else
5767 fprintf (dump_file, "Stack clash no residual allocation in prologue.\n");
5769 if (frame_pointer_needed)
5770 fprintf (dump_file, "Stack clash frame pointer needed.\n");
5771 else
5772 fprintf (dump_file, "Stack clash no frame pointer needed.\n");
5774 if (TREE_THIS_VOLATILE (cfun->decl))
5775 fprintf (dump_file,
5776 "Stack clash noreturn prologue, assuming no implicit"
5777 " probes in caller.\n");
5778 else
5779 fprintf (dump_file,
5780 "Stack clash not noreturn prologue.\n");
5783 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5784 for the first time. */
5786 static void
5787 record_insns (rtx_insn *insns, rtx end, hash_table<insn_cache_hasher> **hashp)
5789 rtx_insn *tmp;
5790 hash_table<insn_cache_hasher> *hash = *hashp;
5792 if (hash == NULL)
5793 *hashp = hash = hash_table<insn_cache_hasher>::create_ggc (17);
5795 for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5797 rtx *slot = hash->find_slot (tmp, INSERT);
5798 gcc_assert (*slot == NULL);
5799 *slot = tmp;
5803 /* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5804 basic block, splitting or peepholes. If INSN is a prologue or epilogue
5805 insn, then record COPY as well. */
5807 void
5808 maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5810 hash_table<insn_cache_hasher> *hash;
5811 rtx *slot;
5813 hash = epilogue_insn_hash;
5814 if (!hash || !hash->find (insn))
5816 hash = prologue_insn_hash;
5817 if (!hash || !hash->find (insn))
5818 return;
5821 slot = hash->find_slot (copy, INSERT);
5822 gcc_assert (*slot == NULL);
5823 *slot = copy;
5826 /* Determine if any INSNs in HASH are, or are part of, INSN. Because
5827 we can be running after reorg, SEQUENCE rtl is possible. */
5829 static bool
5830 contains (const rtx_insn *insn, hash_table<insn_cache_hasher> *hash)
5832 if (hash == NULL)
5833 return false;
5835 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5837 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
5838 int i;
5839 for (i = seq->len () - 1; i >= 0; i--)
5840 if (hash->find (seq->element (i)))
5841 return true;
5842 return false;
5845 return hash->find (const_cast<rtx_insn *> (insn)) != NULL;
5849 prologue_contains (const rtx_insn *insn)
5851 return contains (insn, prologue_insn_hash);
5855 epilogue_contains (const rtx_insn *insn)
5857 return contains (insn, epilogue_insn_hash);
5861 prologue_epilogue_contains (const rtx_insn *insn)
5863 if (contains (insn, prologue_insn_hash))
5864 return 1;
5865 if (contains (insn, epilogue_insn_hash))
5866 return 1;
5867 return 0;
5870 void
5871 record_prologue_seq (rtx_insn *seq)
5873 record_insns (seq, NULL, &prologue_insn_hash);
5876 void
5877 record_epilogue_seq (rtx_insn *seq)
5879 record_insns (seq, NULL, &epilogue_insn_hash);
5882 /* Set JUMP_LABEL for a return insn. */
5884 void
5885 set_return_jump_label (rtx_insn *returnjump)
5887 rtx pat = PATTERN (returnjump);
5888 if (GET_CODE (pat) == PARALLEL)
5889 pat = XVECEXP (pat, 0, 0);
5890 if (ANY_RETURN_P (pat))
5891 JUMP_LABEL (returnjump) = pat;
5892 else
5893 JUMP_LABEL (returnjump) = ret_rtx;
5896 /* Return a sequence to be used as the split prologue for the current
5897 function, or NULL. */
5899 static rtx_insn *
5900 make_split_prologue_seq (void)
5902 if (!flag_split_stack
5903 || lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl)))
5904 return NULL;
5906 start_sequence ();
5907 emit_insn (targetm.gen_split_stack_prologue ());
5908 rtx_insn *seq = get_insns ();
5909 end_sequence ();
5911 record_insns (seq, NULL, &prologue_insn_hash);
5912 set_insn_locations (seq, prologue_location);
5914 return seq;
5917 /* Return a sequence to be used as the prologue for the current function,
5918 or NULL. */
5920 static rtx_insn *
5921 make_prologue_seq (void)
5923 if (!targetm.have_prologue ())
5924 return NULL;
5926 start_sequence ();
5927 rtx_insn *seq = targetm.gen_prologue ();
5928 emit_insn (seq);
5930 /* Insert an explicit USE for the frame pointer
5931 if the profiling is on and the frame pointer is required. */
5932 if (crtl->profile && frame_pointer_needed)
5933 emit_use (hard_frame_pointer_rtx);
5935 /* Retain a map of the prologue insns. */
5936 record_insns (seq, NULL, &prologue_insn_hash);
5937 emit_note (NOTE_INSN_PROLOGUE_END);
5939 /* Ensure that instructions are not moved into the prologue when
5940 profiling is on. The call to the profiling routine can be
5941 emitted within the live range of a call-clobbered register. */
5942 if (!targetm.profile_before_prologue () && crtl->profile)
5943 emit_insn (gen_blockage ());
5945 seq = get_insns ();
5946 end_sequence ();
5947 set_insn_locations (seq, prologue_location);
5949 return seq;
5952 /* Return a sequence to be used as the epilogue for the current function,
5953 or NULL. */
5955 static rtx_insn *
5956 make_epilogue_seq (void)
5958 if (!targetm.have_epilogue ())
5959 return NULL;
5961 start_sequence ();
5962 emit_note (NOTE_INSN_EPILOGUE_BEG);
5963 rtx_insn *seq = targetm.gen_epilogue ();
5964 if (seq)
5965 emit_jump_insn (seq);
5967 /* Retain a map of the epilogue insns. */
5968 record_insns (seq, NULL, &epilogue_insn_hash);
5969 set_insn_locations (seq, epilogue_location);
5971 seq = get_insns ();
5972 rtx_insn *returnjump = get_last_insn ();
5973 end_sequence ();
5975 if (JUMP_P (returnjump))
5976 set_return_jump_label (returnjump);
5978 return seq;
5982 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
5983 this into place with notes indicating where the prologue ends and where
5984 the epilogue begins. Update the basic block information when possible.
5986 Notes on epilogue placement:
5987 There are several kinds of edges to the exit block:
5988 * a single fallthru edge from LAST_BB
5989 * possibly, edges from blocks containing sibcalls
5990 * possibly, fake edges from infinite loops
5992 The epilogue is always emitted on the fallthru edge from the last basic
5993 block in the function, LAST_BB, into the exit block.
5995 If LAST_BB is empty except for a label, it is the target of every
5996 other basic block in the function that ends in a return. If a
5997 target has a return or simple_return pattern (possibly with
5998 conditional variants), these basic blocks can be changed so that a
5999 return insn is emitted into them, and their target is adjusted to
6000 the real exit block.
6002 Notes on shrink wrapping: We implement a fairly conservative
6003 version of shrink-wrapping rather than the textbook one. We only
6004 generate a single prologue and a single epilogue. This is
6005 sufficient to catch a number of interesting cases involving early
6006 exits.
6008 First, we identify the blocks that require the prologue to occur before
6009 them. These are the ones that modify a call-saved register, or reference
6010 any of the stack or frame pointer registers. To simplify things, we then
6011 mark everything reachable from these blocks as also requiring a prologue.
6012 This takes care of loops automatically, and avoids the need to examine
6013 whether MEMs reference the frame, since it is sufficient to check for
6014 occurrences of the stack or frame pointer.
6016 We then compute the set of blocks for which the need for a prologue
6017 is anticipatable (borrowing terminology from the shrink-wrapping
6018 description in Muchnick's book). These are the blocks which either
6019 require a prologue themselves, or those that have only successors
6020 where the prologue is anticipatable. The prologue needs to be
6021 inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
6022 is not. For the moment, we ensure that only one such edge exists.
6024 The epilogue is placed as described above, but we make a
6025 distinction between inserting return and simple_return patterns
6026 when modifying other blocks that end in a return. Blocks that end
6027 in a sibcall omit the sibcall_epilogue if the block is not in
6028 ANTIC. */
6030 void
6031 thread_prologue_and_epilogue_insns (void)
6033 df_analyze ();
6035 /* Can't deal with multiple successors of the entry block at the
6036 moment. Function should always have at least one entry
6037 point. */
6038 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
6040 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6041 edge orig_entry_edge = entry_edge;
6043 rtx_insn *split_prologue_seq = make_split_prologue_seq ();
6044 rtx_insn *prologue_seq = make_prologue_seq ();
6045 rtx_insn *epilogue_seq = make_epilogue_seq ();
6047 /* Try to perform a kind of shrink-wrapping, making sure the
6048 prologue/epilogue is emitted only around those parts of the
6049 function that require it. */
6050 try_shrink_wrapping (&entry_edge, prologue_seq);
6052 /* If the target can handle splitting the prologue/epilogue into separate
6053 components, try to shrink-wrap these components separately. */
6054 try_shrink_wrapping_separate (entry_edge->dest);
6056 /* If that did anything for any component we now need the generate the
6057 "main" prologue again. Because some targets require some of these
6058 to be called in a specific order (i386 requires the split prologue
6059 to be first, for example), we create all three sequences again here.
6060 If this does not work for some target, that target should not enable
6061 separate shrink-wrapping. */
6062 if (crtl->shrink_wrapped_separate)
6064 split_prologue_seq = make_split_prologue_seq ();
6065 prologue_seq = make_prologue_seq ();
6066 epilogue_seq = make_epilogue_seq ();
6069 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
6071 /* A small fib -- epilogue is not yet completed, but we wish to re-use
6072 this marker for the splits of EH_RETURN patterns, and nothing else
6073 uses the flag in the meantime. */
6074 epilogue_completed = 1;
6076 /* Find non-fallthru edges that end with EH_RETURN instructions. On
6077 some targets, these get split to a special version of the epilogue
6078 code. In order to be able to properly annotate these with unwind
6079 info, try to split them now. If we get a valid split, drop an
6080 EPILOGUE_BEG note and mark the insns as epilogue insns. */
6081 edge e;
6082 edge_iterator ei;
6083 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6085 rtx_insn *prev, *last, *trial;
6087 if (e->flags & EDGE_FALLTHRU)
6088 continue;
6089 last = BB_END (e->src);
6090 if (!eh_returnjump_p (last))
6091 continue;
6093 prev = PREV_INSN (last);
6094 trial = try_split (PATTERN (last), last, 1);
6095 if (trial == last)
6096 continue;
6098 record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
6099 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
6102 edge exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
6104 if (exit_fallthru_edge)
6106 if (epilogue_seq)
6108 insert_insn_on_edge (epilogue_seq, exit_fallthru_edge);
6109 commit_edge_insertions ();
6111 /* The epilogue insns we inserted may cause the exit edge to no longer
6112 be fallthru. */
6113 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6115 if (((e->flags & EDGE_FALLTHRU) != 0)
6116 && returnjump_p (BB_END (e->src)))
6117 e->flags &= ~EDGE_FALLTHRU;
6120 else if (next_active_insn (BB_END (exit_fallthru_edge->src)))
6122 /* We have a fall-through edge to the exit block, the source is not
6123 at the end of the function, and there will be an assembler epilogue
6124 at the end of the function.
6125 We can't use force_nonfallthru here, because that would try to
6126 use return. Inserting a jump 'by hand' is extremely messy, so
6127 we take advantage of cfg_layout_finalize using
6128 fixup_fallthru_exit_predecessor. */
6129 cfg_layout_initialize (0);
6130 basic_block cur_bb;
6131 FOR_EACH_BB_FN (cur_bb, cfun)
6132 if (cur_bb->index >= NUM_FIXED_BLOCKS
6133 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
6134 cur_bb->aux = cur_bb->next_bb;
6135 cfg_layout_finalize ();
6139 /* Insert the prologue. */
6141 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6143 if (split_prologue_seq || prologue_seq)
6145 rtx_insn *split_prologue_insn = split_prologue_seq;
6146 if (split_prologue_seq)
6148 while (split_prologue_insn && !NONDEBUG_INSN_P (split_prologue_insn))
6149 split_prologue_insn = NEXT_INSN (split_prologue_insn);
6150 insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
6153 rtx_insn *prologue_insn = prologue_seq;
6154 if (prologue_seq)
6156 while (prologue_insn && !NONDEBUG_INSN_P (prologue_insn))
6157 prologue_insn = NEXT_INSN (prologue_insn);
6158 insert_insn_on_edge (prologue_seq, entry_edge);
6161 commit_edge_insertions ();
6163 /* Look for basic blocks within the prologue insns. */
6164 if (split_prologue_insn
6165 && BLOCK_FOR_INSN (split_prologue_insn) == NULL)
6166 split_prologue_insn = NULL;
6167 if (prologue_insn
6168 && BLOCK_FOR_INSN (prologue_insn) == NULL)
6169 prologue_insn = NULL;
6170 if (split_prologue_insn || prologue_insn)
6172 auto_sbitmap blocks (last_basic_block_for_fn (cfun));
6173 bitmap_clear (blocks);
6174 if (split_prologue_insn)
6175 bitmap_set_bit (blocks,
6176 BLOCK_FOR_INSN (split_prologue_insn)->index);
6177 if (prologue_insn)
6178 bitmap_set_bit (blocks, BLOCK_FOR_INSN (prologue_insn)->index);
6179 find_many_sub_basic_blocks (blocks);
6183 default_rtl_profile ();
6185 /* Emit sibling epilogues before any sibling call sites. */
6186 for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
6187 (e = ei_safe_edge (ei));
6188 ei_next (&ei))
6190 /* Skip those already handled, the ones that run without prologue. */
6191 if (e->flags & EDGE_IGNORE)
6193 e->flags &= ~EDGE_IGNORE;
6194 continue;
6197 rtx_insn *insn = BB_END (e->src);
6199 if (!(CALL_P (insn) && SIBLING_CALL_P (insn)))
6200 continue;
6202 if (rtx_insn *ep_seq = targetm.gen_sibcall_epilogue ())
6204 start_sequence ();
6205 emit_note (NOTE_INSN_EPILOGUE_BEG);
6206 emit_insn (ep_seq);
6207 rtx_insn *seq = get_insns ();
6208 end_sequence ();
6210 /* Retain a map of the epilogue insns. Used in life analysis to
6211 avoid getting rid of sibcall epilogue insns. Do this before we
6212 actually emit the sequence. */
6213 record_insns (seq, NULL, &epilogue_insn_hash);
6214 set_insn_locations (seq, epilogue_location);
6216 emit_insn_before (seq, insn);
6220 if (epilogue_seq)
6222 rtx_insn *insn, *next;
6224 /* Similarly, move any line notes that appear after the epilogue.
6225 There is no need, however, to be quite so anal about the existence
6226 of such a note. Also possibly move
6227 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
6228 info generation. */
6229 for (insn = epilogue_seq; insn; insn = next)
6231 next = NEXT_INSN (insn);
6232 if (NOTE_P (insn)
6233 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
6234 reorder_insns (insn, insn, PREV_INSN (epilogue_seq));
6238 /* Threading the prologue and epilogue changes the artificial refs
6239 in the entry and exit blocks. */
6240 epilogue_completed = 1;
6241 df_update_entry_exit_and_calls ();
6244 /* Reposition the prologue-end and epilogue-begin notes after
6245 instruction scheduling. */
6247 void
6248 reposition_prologue_and_epilogue_notes (void)
6250 if (!targetm.have_prologue ()
6251 && !targetm.have_epilogue ()
6252 && !targetm.have_sibcall_epilogue ())
6253 return;
6255 /* Since the hash table is created on demand, the fact that it is
6256 non-null is a signal that it is non-empty. */
6257 if (prologue_insn_hash != NULL)
6259 size_t len = prologue_insn_hash->elements ();
6260 rtx_insn *insn, *last = NULL, *note = NULL;
6262 /* Scan from the beginning until we reach the last prologue insn. */
6263 /* ??? While we do have the CFG intact, there are two problems:
6264 (1) The prologue can contain loops (typically probing the stack),
6265 which means that the end of the prologue isn't in the first bb.
6266 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
6267 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6269 if (NOTE_P (insn))
6271 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
6272 note = insn;
6274 else if (contains (insn, prologue_insn_hash))
6276 last = insn;
6277 if (--len == 0)
6278 break;
6282 if (last)
6284 if (note == NULL)
6286 /* Scan forward looking for the PROLOGUE_END note. It should
6287 be right at the beginning of the block, possibly with other
6288 insn notes that got moved there. */
6289 for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
6291 if (NOTE_P (note)
6292 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
6293 break;
6297 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
6298 if (LABEL_P (last))
6299 last = NEXT_INSN (last);
6300 reorder_insns (note, note, last);
6304 if (epilogue_insn_hash != NULL)
6306 edge_iterator ei;
6307 edge e;
6309 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6311 rtx_insn *insn, *first = NULL, *note = NULL;
6312 basic_block bb = e->src;
6314 /* Scan from the beginning until we reach the first epilogue insn. */
6315 FOR_BB_INSNS (bb, insn)
6317 if (NOTE_P (insn))
6319 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
6321 note = insn;
6322 if (first != NULL)
6323 break;
6326 else if (first == NULL && contains (insn, epilogue_insn_hash))
6328 first = insn;
6329 if (note != NULL)
6330 break;
6334 if (note)
6336 /* If the function has a single basic block, and no real
6337 epilogue insns (e.g. sibcall with no cleanup), the
6338 epilogue note can get scheduled before the prologue
6339 note. If we have frame related prologue insns, having
6340 them scanned during the epilogue will result in a crash.
6341 In this case re-order the epilogue note to just before
6342 the last insn in the block. */
6343 if (first == NULL)
6344 first = BB_END (bb);
6346 if (PREV_INSN (first) != note)
6347 reorder_insns (note, note, PREV_INSN (first));
6353 /* Returns the name of function declared by FNDECL. */
6354 const char *
6355 fndecl_name (tree fndecl)
6357 if (fndecl == NULL)
6358 return "(nofn)";
6359 return lang_hooks.decl_printable_name (fndecl, 1);
6362 /* Returns the name of function FN. */
6363 const char *
6364 function_name (struct function *fn)
6366 tree fndecl = (fn == NULL) ? NULL : fn->decl;
6367 return fndecl_name (fndecl);
6370 /* Returns the name of the current function. */
6371 const char *
6372 current_function_name (void)
6374 return function_name (cfun);
6378 static unsigned int
6379 rest_of_handle_check_leaf_regs (void)
6381 #ifdef LEAF_REGISTERS
6382 crtl->uses_only_leaf_regs
6383 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6384 #endif
6385 return 0;
6388 /* Insert a TYPE into the used types hash table of CFUN. */
6390 static void
6391 used_types_insert_helper (tree type, struct function *func)
6393 if (type != NULL && func != NULL)
6395 if (func->used_types_hash == NULL)
6396 func->used_types_hash = hash_set<tree>::create_ggc (37);
6398 func->used_types_hash->add (type);
6402 /* Given a type, insert it into the used hash table in cfun. */
6403 void
6404 used_types_insert (tree t)
6406 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6407 if (TYPE_NAME (t))
6408 break;
6409 else
6410 t = TREE_TYPE (t);
6411 if (TREE_CODE (t) == ERROR_MARK)
6412 return;
6413 if (TYPE_NAME (t) == NULL_TREE
6414 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6415 t = TYPE_MAIN_VARIANT (t);
6416 if (debug_info_level > DINFO_LEVEL_NONE)
6418 if (cfun)
6419 used_types_insert_helper (t, cfun);
6420 else
6422 /* So this might be a type referenced by a global variable.
6423 Record that type so that we can later decide to emit its
6424 debug information. */
6425 vec_safe_push (types_used_by_cur_var_decl, t);
6430 /* Helper to Hash a struct types_used_by_vars_entry. */
6432 static hashval_t
6433 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6435 gcc_assert (entry && entry->var_decl && entry->type);
6437 return iterative_hash_object (entry->type,
6438 iterative_hash_object (entry->var_decl, 0));
6441 /* Hash function of the types_used_by_vars_entry hash table. */
6443 hashval_t
6444 used_type_hasher::hash (types_used_by_vars_entry *entry)
6446 return hash_types_used_by_vars_entry (entry);
6449 /*Equality function of the types_used_by_vars_entry hash table. */
6451 bool
6452 used_type_hasher::equal (types_used_by_vars_entry *e1,
6453 types_used_by_vars_entry *e2)
6455 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6458 /* Inserts an entry into the types_used_by_vars_hash hash table. */
6460 void
6461 types_used_by_var_decl_insert (tree type, tree var_decl)
6463 if (type != NULL && var_decl != NULL)
6465 types_used_by_vars_entry **slot;
6466 struct types_used_by_vars_entry e;
6467 e.var_decl = var_decl;
6468 e.type = type;
6469 if (types_used_by_vars_hash == NULL)
6470 types_used_by_vars_hash
6471 = hash_table<used_type_hasher>::create_ggc (37);
6473 slot = types_used_by_vars_hash->find_slot (&e, INSERT);
6474 if (*slot == NULL)
6476 struct types_used_by_vars_entry *entry;
6477 entry = ggc_alloc<types_used_by_vars_entry> ();
6478 entry->type = type;
6479 entry->var_decl = var_decl;
6480 *slot = entry;
6485 namespace {
6487 const pass_data pass_data_leaf_regs =
6489 RTL_PASS, /* type */
6490 "*leaf_regs", /* name */
6491 OPTGROUP_NONE, /* optinfo_flags */
6492 TV_NONE, /* tv_id */
6493 0, /* properties_required */
6494 0, /* properties_provided */
6495 0, /* properties_destroyed */
6496 0, /* todo_flags_start */
6497 0, /* todo_flags_finish */
6500 class pass_leaf_regs : public rtl_opt_pass
6502 public:
6503 pass_leaf_regs (gcc::context *ctxt)
6504 : rtl_opt_pass (pass_data_leaf_regs, ctxt)
6507 /* opt_pass methods: */
6508 virtual unsigned int execute (function *)
6510 return rest_of_handle_check_leaf_regs ();
6513 }; // class pass_leaf_regs
6515 } // anon namespace
6517 rtl_opt_pass *
6518 make_pass_leaf_regs (gcc::context *ctxt)
6520 return new pass_leaf_regs (ctxt);
6523 static unsigned int
6524 rest_of_handle_thread_prologue_and_epilogue (void)
6526 /* prepare_shrink_wrap is sensitive to the block structure of the control
6527 flow graph, so clean it up first. */
6528 if (optimize)
6529 cleanup_cfg (0);
6531 /* On some machines, the prologue and epilogue code, or parts thereof,
6532 can be represented as RTL. Doing so lets us schedule insns between
6533 it and the rest of the code and also allows delayed branch
6534 scheduling to operate in the epilogue. */
6535 thread_prologue_and_epilogue_insns ();
6537 /* Some non-cold blocks may now be only reachable from cold blocks.
6538 Fix that up. */
6539 fixup_partitions ();
6541 /* Shrink-wrapping can result in unreachable edges in the epilogue,
6542 see PR57320. */
6543 cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
6545 /* The stack usage info is finalized during prologue expansion. */
6546 if (flag_stack_usage_info)
6547 output_stack_usage ();
6549 return 0;
6552 namespace {
6554 const pass_data pass_data_thread_prologue_and_epilogue =
6556 RTL_PASS, /* type */
6557 "pro_and_epilogue", /* name */
6558 OPTGROUP_NONE, /* optinfo_flags */
6559 TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
6560 0, /* properties_required */
6561 0, /* properties_provided */
6562 0, /* properties_destroyed */
6563 0, /* todo_flags_start */
6564 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
6567 class pass_thread_prologue_and_epilogue : public rtl_opt_pass
6569 public:
6570 pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6571 : rtl_opt_pass (pass_data_thread_prologue_and_epilogue, ctxt)
6574 /* opt_pass methods: */
6575 virtual unsigned int execute (function *)
6577 return rest_of_handle_thread_prologue_and_epilogue ();
6580 }; // class pass_thread_prologue_and_epilogue
6582 } // anon namespace
6584 rtl_opt_pass *
6585 make_pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6587 return new pass_thread_prologue_and_epilogue (ctxt);
6591 /* This mini-pass fixes fall-out from SSA in asm statements that have
6592 in-out constraints. Say you start with
6594 orig = inout;
6595 asm ("": "+mr" (inout));
6596 use (orig);
6598 which is transformed very early to use explicit output and match operands:
6600 orig = inout;
6601 asm ("": "=mr" (inout) : "0" (inout));
6602 use (orig);
6604 Or, after SSA and copyprop,
6606 asm ("": "=mr" (inout_2) : "0" (inout_1));
6607 use (inout_1);
6609 Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
6610 they represent two separate values, so they will get different pseudo
6611 registers during expansion. Then, since the two operands need to match
6612 per the constraints, but use different pseudo registers, reload can
6613 only register a reload for these operands. But reloads can only be
6614 satisfied by hardregs, not by memory, so we need a register for this
6615 reload, just because we are presented with non-matching operands.
6616 So, even though we allow memory for this operand, no memory can be
6617 used for it, just because the two operands don't match. This can
6618 cause reload failures on register-starved targets.
6620 So it's a symptom of reload not being able to use memory for reloads
6621 or, alternatively it's also a symptom of both operands not coming into
6622 reload as matching (in which case the pseudo could go to memory just
6623 fine, as the alternative allows it, and no reload would be necessary).
6624 We fix the latter problem here, by transforming
6626 asm ("": "=mr" (inout_2) : "0" (inout_1));
6628 back to
6630 inout_2 = inout_1;
6631 asm ("": "=mr" (inout_2) : "0" (inout_2)); */
6633 static void
6634 match_asm_constraints_1 (rtx_insn *insn, rtx *p_sets, int noutputs)
6636 int i;
6637 bool changed = false;
6638 rtx op = SET_SRC (p_sets[0]);
6639 int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
6640 rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
6641 bool *output_matched = XALLOCAVEC (bool, noutputs);
6643 memset (output_matched, 0, noutputs * sizeof (bool));
6644 for (i = 0; i < ninputs; i++)
6646 rtx input, output;
6647 rtx_insn *insns;
6648 const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
6649 char *end;
6650 int match, j;
6652 if (*constraint == '%')
6653 constraint++;
6655 match = strtoul (constraint, &end, 10);
6656 if (end == constraint)
6657 continue;
6659 gcc_assert (match < noutputs);
6660 output = SET_DEST (p_sets[match]);
6661 input = RTVEC_ELT (inputs, i);
6662 /* Only do the transformation for pseudos. */
6663 if (! REG_P (output)
6664 || rtx_equal_p (output, input)
6665 || !(REG_P (input) || SUBREG_P (input)
6666 || MEM_P (input) || CONSTANT_P (input))
6667 || !general_operand (input, GET_MODE (output)))
6668 continue;
6670 /* We can't do anything if the output is also used as input,
6671 as we're going to overwrite it. */
6672 for (j = 0; j < ninputs; j++)
6673 if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
6674 break;
6675 if (j != ninputs)
6676 continue;
6678 /* Avoid changing the same input several times. For
6679 asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
6680 only change in once (to out1), rather than changing it
6681 first to out1 and afterwards to out2. */
6682 if (i > 0)
6684 for (j = 0; j < noutputs; j++)
6685 if (output_matched[j] && input == SET_DEST (p_sets[j]))
6686 break;
6687 if (j != noutputs)
6688 continue;
6690 output_matched[match] = true;
6692 start_sequence ();
6693 emit_move_insn (output, input);
6694 insns = get_insns ();
6695 end_sequence ();
6696 emit_insn_before (insns, insn);
6698 /* Now replace all mentions of the input with output. We can't
6699 just replace the occurrence in inputs[i], as the register might
6700 also be used in some other input (or even in an address of an
6701 output), which would mean possibly increasing the number of
6702 inputs by one (namely 'output' in addition), which might pose
6703 a too complicated problem for reload to solve. E.g. this situation:
6705 asm ("" : "=r" (output), "=m" (input) : "0" (input))
6707 Here 'input' is used in two occurrences as input (once for the
6708 input operand, once for the address in the second output operand).
6709 If we would replace only the occurrence of the input operand (to
6710 make the matching) we would be left with this:
6712 output = input
6713 asm ("" : "=r" (output), "=m" (input) : "0" (output))
6715 Now we suddenly have two different input values (containing the same
6716 value, but different pseudos) where we formerly had only one.
6717 With more complicated asms this might lead to reload failures
6718 which wouldn't have happen without this pass. So, iterate over
6719 all operands and replace all occurrences of the register used. */
6720 for (j = 0; j < noutputs; j++)
6721 if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
6722 && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
6723 SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
6724 input, output);
6725 for (j = 0; j < ninputs; j++)
6726 if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
6727 RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
6728 input, output);
6730 changed = true;
6733 if (changed)
6734 df_insn_rescan (insn);
6737 /* Add the decl D to the local_decls list of FUN. */
6739 void
6740 add_local_decl (struct function *fun, tree d)
6742 gcc_assert (VAR_P (d));
6743 vec_safe_push (fun->local_decls, d);
6746 namespace {
6748 const pass_data pass_data_match_asm_constraints =
6750 RTL_PASS, /* type */
6751 "asmcons", /* name */
6752 OPTGROUP_NONE, /* optinfo_flags */
6753 TV_NONE, /* tv_id */
6754 0, /* properties_required */
6755 0, /* properties_provided */
6756 0, /* properties_destroyed */
6757 0, /* todo_flags_start */
6758 0, /* todo_flags_finish */
6761 class pass_match_asm_constraints : public rtl_opt_pass
6763 public:
6764 pass_match_asm_constraints (gcc::context *ctxt)
6765 : rtl_opt_pass (pass_data_match_asm_constraints, ctxt)
6768 /* opt_pass methods: */
6769 virtual unsigned int execute (function *);
6771 }; // class pass_match_asm_constraints
6773 unsigned
6774 pass_match_asm_constraints::execute (function *fun)
6776 basic_block bb;
6777 rtx_insn *insn;
6778 rtx pat, *p_sets;
6779 int noutputs;
6781 if (!crtl->has_asm_statement)
6782 return 0;
6784 df_set_flags (DF_DEFER_INSN_RESCAN);
6785 FOR_EACH_BB_FN (bb, fun)
6787 FOR_BB_INSNS (bb, insn)
6789 if (!INSN_P (insn))
6790 continue;
6792 pat = PATTERN (insn);
6793 if (GET_CODE (pat) == PARALLEL)
6794 p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
6795 else if (GET_CODE (pat) == SET)
6796 p_sets = &PATTERN (insn), noutputs = 1;
6797 else
6798 continue;
6800 if (GET_CODE (*p_sets) == SET
6801 && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
6802 match_asm_constraints_1 (insn, p_sets, noutputs);
6806 return TODO_df_finish;
6809 } // anon namespace
6811 rtl_opt_pass *
6812 make_pass_match_asm_constraints (gcc::context *ctxt)
6814 return new pass_match_asm_constraints (ctxt);
6818 #include "gt-function.h"