[PR67766] reorder handling of parallels, concats and promoted values in return
[official-gcc.git] / gcc / function.c
blobd16d6d88818f569abfd95045aed3198c1e7b2582
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987-2015 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 "cfghooks.h"
39 #include "tree.h"
40 #include "rtl.h"
41 #include "df.h"
42 #include "rtl-error.h"
43 #include "alias.h"
44 #include "fold-const.h"
45 #include "stor-layout.h"
46 #include "varasm.h"
47 #include "stringpool.h"
48 #include "flags.h"
49 #include "except.h"
50 #include "insn-config.h"
51 #include "expmed.h"
52 #include "dojump.h"
53 #include "explow.h"
54 #include "calls.h"
55 #include "emit-rtl.h"
56 #include "stmt.h"
57 #include "expr.h"
58 #include "insn-codes.h"
59 #include "optabs-tree.h"
60 #include "optabs.h"
61 #include "libfuncs.h"
62 #include "regs.h"
63 #include "recog.h"
64 #include "output.h"
65 #include "tm_p.h"
66 #include "langhooks.h"
67 #include "target.h"
68 #include "common/common-target.h"
69 #include "gimple-expr.h"
70 #include "gimplify.h"
71 #include "tree-pass.h"
72 #include "cfgrtl.h"
73 #include "cfganal.h"
74 #include "cfgbuild.h"
75 #include "cfgcleanup.h"
76 #include "cfgexpand.h"
77 #include "params.h"
78 #include "bb-reorder.h"
79 #include "shrink-wrap.h"
80 #include "toplev.h"
81 #include "rtl-iter.h"
82 #include "tree-chkp.h"
83 #include "rtl-chkp.h"
84 #include "tree-dfa.h"
86 /* So we can assign to cfun in this file. */
87 #undef cfun
89 #ifndef STACK_ALIGNMENT_NEEDED
90 #define STACK_ALIGNMENT_NEEDED 1
91 #endif
93 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
95 /* Round a value to the lowest integer less than it that is a multiple of
96 the required alignment. Avoid using division in case the value is
97 negative. Assume the alignment is a power of two. */
98 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
100 /* Similar, but round to the next highest integer that meets the
101 alignment. */
102 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
104 /* Nonzero once virtual register instantiation has been done.
105 assign_stack_local uses frame_pointer_rtx when this is nonzero.
106 calls.c:emit_library_call_value_1 uses it to set up
107 post-instantiation libcalls. */
108 int virtuals_instantiated;
110 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
111 static GTY(()) int funcdef_no;
113 /* These variables hold pointers to functions to create and destroy
114 target specific, per-function data structures. */
115 struct machine_function * (*init_machine_status) (void);
117 /* The currently compiled function. */
118 struct function *cfun = 0;
120 /* These hashes record the prologue and epilogue insns. */
122 struct insn_cache_hasher : ggc_cache_ptr_hash<rtx_def>
124 static hashval_t hash (rtx x) { return htab_hash_pointer (x); }
125 static bool equal (rtx a, rtx b) { return a == b; }
128 static GTY((cache))
129 hash_table<insn_cache_hasher> *prologue_insn_hash;
130 static GTY((cache))
131 hash_table<insn_cache_hasher> *epilogue_insn_hash;
134 hash_table<used_type_hasher> *types_used_by_vars_hash = NULL;
135 vec<tree, va_gc> *types_used_by_cur_var_decl;
137 /* Forward declarations. */
139 static struct temp_slot *find_temp_slot_from_address (rtx);
140 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
141 static void pad_below (struct args_size *, machine_mode, tree);
142 static void reorder_blocks_1 (rtx_insn *, tree, vec<tree> *);
143 static int all_blocks (tree, tree *);
144 static tree *get_block_vector (tree, int *);
145 extern tree debug_find_var_in_block_tree (tree, tree);
146 /* We always define `record_insns' even if it's not used so that we
147 can always export `prologue_epilogue_contains'. */
148 static void record_insns (rtx_insn *, rtx, hash_table<insn_cache_hasher> **)
149 ATTRIBUTE_UNUSED;
150 static bool contains (const_rtx, hash_table<insn_cache_hasher> *);
151 static void prepare_function_start (void);
152 static void do_clobber_return_reg (rtx, void *);
153 static void do_use_return_reg (rtx, void *);
156 /* Stack of nested functions. */
157 /* Keep track of the cfun stack. */
159 static vec<function *> function_context_stack;
161 /* Save the current context for compilation of a nested function.
162 This is called from language-specific code. */
164 void
165 push_function_context (void)
167 if (cfun == 0)
168 allocate_struct_function (NULL, false);
170 function_context_stack.safe_push (cfun);
171 set_cfun (NULL);
174 /* Restore the last saved context, at the end of a nested function.
175 This function is called from language-specific code. */
177 void
178 pop_function_context (void)
180 struct function *p = function_context_stack.pop ();
181 set_cfun (p);
182 current_function_decl = p->decl;
184 /* Reset variables that have known state during rtx generation. */
185 virtuals_instantiated = 0;
186 generating_concat_p = 1;
189 /* Clear out all parts of the state in F that can safely be discarded
190 after the function has been parsed, but not compiled, to let
191 garbage collection reclaim the memory. */
193 void
194 free_after_parsing (struct function *f)
196 f->language = 0;
199 /* Clear out all parts of the state in F that can safely be discarded
200 after the function has been compiled, to let garbage collection
201 reclaim the memory. */
203 void
204 free_after_compilation (struct function *f)
206 prologue_insn_hash = NULL;
207 epilogue_insn_hash = NULL;
209 free (crtl->emit.regno_pointer_align);
211 memset (crtl, 0, sizeof (struct rtl_data));
212 f->eh = NULL;
213 f->machine = NULL;
214 f->cfg = NULL;
215 f->curr_properties &= ~PROP_cfg;
217 regno_reg_rtx = NULL;
220 /* Return size needed for stack frame based on slots so far allocated.
221 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
222 the caller may have to do that. */
224 HOST_WIDE_INT
225 get_frame_size (void)
227 if (FRAME_GROWS_DOWNWARD)
228 return -frame_offset;
229 else
230 return frame_offset;
233 /* Issue an error message and return TRUE if frame OFFSET overflows in
234 the signed target pointer arithmetics for function FUNC. Otherwise
235 return FALSE. */
237 bool
238 frame_offset_overflow (HOST_WIDE_INT offset, tree func)
240 unsigned HOST_WIDE_INT size = FRAME_GROWS_DOWNWARD ? -offset : offset;
242 if (size > ((unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (Pmode) - 1))
243 /* Leave room for the fixed part of the frame. */
244 - 64 * UNITS_PER_WORD)
246 error_at (DECL_SOURCE_LOCATION (func),
247 "total size of local objects too large");
248 return TRUE;
251 return FALSE;
254 /* Return stack slot alignment in bits for TYPE and MODE. */
256 static unsigned int
257 get_stack_local_alignment (tree type, machine_mode mode)
259 unsigned int alignment;
261 if (mode == BLKmode)
262 alignment = BIGGEST_ALIGNMENT;
263 else
264 alignment = GET_MODE_ALIGNMENT (mode);
266 /* Allow the frond-end to (possibly) increase the alignment of this
267 stack slot. */
268 if (! type)
269 type = lang_hooks.types.type_for_mode (mode, 0);
271 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
274 /* Determine whether it is possible to fit a stack slot of size SIZE and
275 alignment ALIGNMENT into an area in the stack frame that starts at
276 frame offset START and has a length of LENGTH. If so, store the frame
277 offset to be used for the stack slot in *POFFSET and return true;
278 return false otherwise. This function will extend the frame size when
279 given a start/length pair that lies at the end of the frame. */
281 static bool
282 try_fit_stack_local (HOST_WIDE_INT start, HOST_WIDE_INT length,
283 HOST_WIDE_INT size, unsigned int alignment,
284 HOST_WIDE_INT *poffset)
286 HOST_WIDE_INT this_frame_offset;
287 int frame_off, frame_alignment, frame_phase;
289 /* Calculate how many bytes the start of local variables is off from
290 stack alignment. */
291 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
292 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
293 frame_phase = frame_off ? frame_alignment - frame_off : 0;
295 /* Round the frame offset to the specified alignment. */
297 /* We must be careful here, since FRAME_OFFSET might be negative and
298 division with a negative dividend isn't as well defined as we might
299 like. So we instead assume that ALIGNMENT is a power of two and
300 use logical operations which are unambiguous. */
301 if (FRAME_GROWS_DOWNWARD)
302 this_frame_offset
303 = (FLOOR_ROUND (start + length - size - frame_phase,
304 (unsigned HOST_WIDE_INT) alignment)
305 + frame_phase);
306 else
307 this_frame_offset
308 = (CEIL_ROUND (start - frame_phase,
309 (unsigned HOST_WIDE_INT) alignment)
310 + frame_phase);
312 /* See if it fits. If this space is at the edge of the frame,
313 consider extending the frame to make it fit. Our caller relies on
314 this when allocating a new slot. */
315 if (frame_offset == start && this_frame_offset < frame_offset)
316 frame_offset = this_frame_offset;
317 else if (this_frame_offset < start)
318 return false;
319 else if (start + length == frame_offset
320 && this_frame_offset + size > start + length)
321 frame_offset = this_frame_offset + size;
322 else if (this_frame_offset + size > start + length)
323 return false;
325 *poffset = this_frame_offset;
326 return true;
329 /* Create a new frame_space structure describing free space in the stack
330 frame beginning at START and ending at END, and chain it into the
331 function's frame_space_list. */
333 static void
334 add_frame_space (HOST_WIDE_INT start, HOST_WIDE_INT end)
336 struct frame_space *space = ggc_alloc<frame_space> ();
337 space->next = crtl->frame_space_list;
338 crtl->frame_space_list = space;
339 space->start = start;
340 space->length = end - start;
343 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
344 with machine mode MODE.
346 ALIGN controls the amount of alignment for the address of the slot:
347 0 means according to MODE,
348 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
349 -2 means use BITS_PER_UNIT,
350 positive specifies alignment boundary in bits.
352 KIND has ASLK_REDUCE_ALIGN bit set if it is OK to reduce
353 alignment and ASLK_RECORD_PAD bit set if we should remember
354 extra space we allocated for alignment purposes. When we are
355 called from assign_stack_temp_for_type, it is not set so we don't
356 track the same stack slot in two independent lists.
358 We do not round to stack_boundary here. */
361 assign_stack_local_1 (machine_mode mode, HOST_WIDE_INT size,
362 int align, int kind)
364 rtx x, addr;
365 int bigend_correction = 0;
366 HOST_WIDE_INT slot_offset = 0, old_frame_offset;
367 unsigned int alignment, alignment_in_bits;
369 if (align == 0)
371 alignment = get_stack_local_alignment (NULL, mode);
372 alignment /= BITS_PER_UNIT;
374 else if (align == -1)
376 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
377 size = CEIL_ROUND (size, alignment);
379 else if (align == -2)
380 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
381 else
382 alignment = align / BITS_PER_UNIT;
384 alignment_in_bits = alignment * BITS_PER_UNIT;
386 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
387 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
389 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
390 alignment = alignment_in_bits / BITS_PER_UNIT;
393 if (SUPPORTS_STACK_ALIGNMENT)
395 if (crtl->stack_alignment_estimated < alignment_in_bits)
397 if (!crtl->stack_realign_processed)
398 crtl->stack_alignment_estimated = alignment_in_bits;
399 else
401 /* If stack is realigned and stack alignment value
402 hasn't been finalized, it is OK not to increase
403 stack_alignment_estimated. The bigger alignment
404 requirement is recorded in stack_alignment_needed
405 below. */
406 gcc_assert (!crtl->stack_realign_finalized);
407 if (!crtl->stack_realign_needed)
409 /* It is OK to reduce the alignment as long as the
410 requested size is 0 or the estimated stack
411 alignment >= mode alignment. */
412 gcc_assert ((kind & ASLK_REDUCE_ALIGN)
413 || size == 0
414 || (crtl->stack_alignment_estimated
415 >= GET_MODE_ALIGNMENT (mode)));
416 alignment_in_bits = crtl->stack_alignment_estimated;
417 alignment = alignment_in_bits / BITS_PER_UNIT;
423 if (crtl->stack_alignment_needed < alignment_in_bits)
424 crtl->stack_alignment_needed = alignment_in_bits;
425 if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
426 crtl->max_used_stack_slot_alignment = alignment_in_bits;
428 if (mode != BLKmode || size != 0)
430 if (kind & ASLK_RECORD_PAD)
432 struct frame_space **psp;
434 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
436 struct frame_space *space = *psp;
437 if (!try_fit_stack_local (space->start, space->length, size,
438 alignment, &slot_offset))
439 continue;
440 *psp = space->next;
441 if (slot_offset > space->start)
442 add_frame_space (space->start, slot_offset);
443 if (slot_offset + size < space->start + space->length)
444 add_frame_space (slot_offset + size,
445 space->start + space->length);
446 goto found_space;
450 else if (!STACK_ALIGNMENT_NEEDED)
452 slot_offset = frame_offset;
453 goto found_space;
456 old_frame_offset = frame_offset;
458 if (FRAME_GROWS_DOWNWARD)
460 frame_offset -= size;
461 try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
463 if (kind & ASLK_RECORD_PAD)
465 if (slot_offset > frame_offset)
466 add_frame_space (frame_offset, slot_offset);
467 if (slot_offset + size < old_frame_offset)
468 add_frame_space (slot_offset + size, old_frame_offset);
471 else
473 frame_offset += size;
474 try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
476 if (kind & ASLK_RECORD_PAD)
478 if (slot_offset > old_frame_offset)
479 add_frame_space (old_frame_offset, slot_offset);
480 if (slot_offset + size < frame_offset)
481 add_frame_space (slot_offset + size, frame_offset);
485 found_space:
486 /* On a big-endian machine, if we are allocating more space than we will use,
487 use the least significant bytes of those that are allocated. */
488 if (BYTES_BIG_ENDIAN && mode != BLKmode && GET_MODE_SIZE (mode) < size)
489 bigend_correction = size - GET_MODE_SIZE (mode);
491 /* If we have already instantiated virtual registers, return the actual
492 address relative to the frame pointer. */
493 if (virtuals_instantiated)
494 addr = plus_constant (Pmode, frame_pointer_rtx,
495 trunc_int_for_mode
496 (slot_offset + bigend_correction
497 + STARTING_FRAME_OFFSET, Pmode));
498 else
499 addr = plus_constant (Pmode, virtual_stack_vars_rtx,
500 trunc_int_for_mode
501 (slot_offset + bigend_correction,
502 Pmode));
504 x = gen_rtx_MEM (mode, addr);
505 set_mem_align (x, alignment_in_bits);
506 MEM_NOTRAP_P (x) = 1;
508 stack_slot_list
509 = gen_rtx_EXPR_LIST (VOIDmode, x, stack_slot_list);
511 if (frame_offset_overflow (frame_offset, current_function_decl))
512 frame_offset = 0;
514 return x;
517 /* Wrap up assign_stack_local_1 with last parameter as false. */
520 assign_stack_local (machine_mode mode, HOST_WIDE_INT size, int align)
522 return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
525 /* In order to evaluate some expressions, such as function calls returning
526 structures in memory, we need to temporarily allocate stack locations.
527 We record each allocated temporary in the following structure.
529 Associated with each temporary slot is a nesting level. When we pop up
530 one level, all temporaries associated with the previous level are freed.
531 Normally, all temporaries are freed after the execution of the statement
532 in which they were created. However, if we are inside a ({...}) grouping,
533 the result may be in a temporary and hence must be preserved. If the
534 result could be in a temporary, we preserve it if we can determine which
535 one it is in. If we cannot determine which temporary may contain the
536 result, all temporaries are preserved. A temporary is preserved by
537 pretending it was allocated at the previous nesting level. */
539 struct GTY(()) temp_slot {
540 /* Points to next temporary slot. */
541 struct temp_slot *next;
542 /* Points to previous temporary slot. */
543 struct temp_slot *prev;
544 /* The rtx to used to reference the slot. */
545 rtx slot;
546 /* The size, in units, of the slot. */
547 HOST_WIDE_INT size;
548 /* The type of the object in the slot, or zero if it doesn't correspond
549 to a type. We use this to determine whether a slot can be reused.
550 It can be reused if objects of the type of the new slot will always
551 conflict with objects of the type of the old slot. */
552 tree type;
553 /* The alignment (in bits) of the slot. */
554 unsigned int align;
555 /* Nonzero if this temporary is currently in use. */
556 char in_use;
557 /* Nesting level at which this slot is being used. */
558 int level;
559 /* The offset of the slot from the frame_pointer, including extra space
560 for alignment. This info is for combine_temp_slots. */
561 HOST_WIDE_INT base_offset;
562 /* The size of the slot, including extra space for alignment. This
563 info is for combine_temp_slots. */
564 HOST_WIDE_INT full_size;
567 /* Entry for the below hash table. */
568 struct GTY((for_user)) temp_slot_address_entry {
569 hashval_t hash;
570 rtx address;
571 struct temp_slot *temp_slot;
574 struct temp_address_hasher : ggc_ptr_hash<temp_slot_address_entry>
576 static hashval_t hash (temp_slot_address_entry *);
577 static bool equal (temp_slot_address_entry *, temp_slot_address_entry *);
580 /* A table of addresses that represent a stack slot. The table is a mapping
581 from address RTXen to a temp slot. */
582 static GTY(()) hash_table<temp_address_hasher> *temp_slot_address_table;
583 static size_t n_temp_slots_in_use;
585 /* Removes temporary slot TEMP from LIST. */
587 static void
588 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
590 if (temp->next)
591 temp->next->prev = temp->prev;
592 if (temp->prev)
593 temp->prev->next = temp->next;
594 else
595 *list = temp->next;
597 temp->prev = temp->next = NULL;
600 /* Inserts temporary slot TEMP to LIST. */
602 static void
603 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
605 temp->next = *list;
606 if (*list)
607 (*list)->prev = temp;
608 temp->prev = NULL;
609 *list = temp;
612 /* Returns the list of used temp slots at LEVEL. */
614 static struct temp_slot **
615 temp_slots_at_level (int level)
617 if (level >= (int) vec_safe_length (used_temp_slots))
618 vec_safe_grow_cleared (used_temp_slots, level + 1);
620 return &(*used_temp_slots)[level];
623 /* Returns the maximal temporary slot level. */
625 static int
626 max_slot_level (void)
628 if (!used_temp_slots)
629 return -1;
631 return used_temp_slots->length () - 1;
634 /* Moves temporary slot TEMP to LEVEL. */
636 static void
637 move_slot_to_level (struct temp_slot *temp, int level)
639 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
640 insert_slot_to_list (temp, temp_slots_at_level (level));
641 temp->level = level;
644 /* Make temporary slot TEMP available. */
646 static void
647 make_slot_available (struct temp_slot *temp)
649 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
650 insert_slot_to_list (temp, &avail_temp_slots);
651 temp->in_use = 0;
652 temp->level = -1;
653 n_temp_slots_in_use--;
656 /* Compute the hash value for an address -> temp slot mapping.
657 The value is cached on the mapping entry. */
658 static hashval_t
659 temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
661 int do_not_record = 0;
662 return hash_rtx (t->address, GET_MODE (t->address),
663 &do_not_record, NULL, false);
666 /* Return the hash value for an address -> temp slot mapping. */
667 hashval_t
668 temp_address_hasher::hash (temp_slot_address_entry *t)
670 return t->hash;
673 /* Compare two address -> temp slot mapping entries. */
674 bool
675 temp_address_hasher::equal (temp_slot_address_entry *t1,
676 temp_slot_address_entry *t2)
678 return exp_equiv_p (t1->address, t2->address, 0, true);
681 /* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
682 static void
683 insert_temp_slot_address (rtx address, struct temp_slot *temp_slot)
685 struct temp_slot_address_entry *t = ggc_alloc<temp_slot_address_entry> ();
686 t->address = address;
687 t->temp_slot = temp_slot;
688 t->hash = temp_slot_address_compute_hash (t);
689 *temp_slot_address_table->find_slot_with_hash (t, t->hash, INSERT) = t;
692 /* Remove an address -> temp slot mapping entry if the temp slot is
693 not in use anymore. Callback for remove_unused_temp_slot_addresses. */
695 remove_unused_temp_slot_addresses_1 (temp_slot_address_entry **slot, void *)
697 const struct temp_slot_address_entry *t = *slot;
698 if (! t->temp_slot->in_use)
699 temp_slot_address_table->clear_slot (slot);
700 return 1;
703 /* Remove all mappings of addresses to unused temp slots. */
704 static void
705 remove_unused_temp_slot_addresses (void)
707 /* Use quicker clearing if there aren't any active temp slots. */
708 if (n_temp_slots_in_use)
709 temp_slot_address_table->traverse
710 <void *, remove_unused_temp_slot_addresses_1> (NULL);
711 else
712 temp_slot_address_table->empty ();
715 /* Find the temp slot corresponding to the object at address X. */
717 static struct temp_slot *
718 find_temp_slot_from_address (rtx x)
720 struct temp_slot *p;
721 struct temp_slot_address_entry tmp, *t;
723 /* First try the easy way:
724 See if X exists in the address -> temp slot mapping. */
725 tmp.address = x;
726 tmp.temp_slot = NULL;
727 tmp.hash = temp_slot_address_compute_hash (&tmp);
728 t = temp_slot_address_table->find_with_hash (&tmp, tmp.hash);
729 if (t)
730 return t->temp_slot;
732 /* If we have a sum involving a register, see if it points to a temp
733 slot. */
734 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
735 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
736 return p;
737 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
738 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
739 return p;
741 /* Last resort: Address is a virtual stack var address. */
742 if (GET_CODE (x) == PLUS
743 && XEXP (x, 0) == virtual_stack_vars_rtx
744 && CONST_INT_P (XEXP (x, 1)))
746 int i;
747 for (i = max_slot_level (); i >= 0; i--)
748 for (p = *temp_slots_at_level (i); p; p = p->next)
750 if (INTVAL (XEXP (x, 1)) >= p->base_offset
751 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size)
752 return p;
756 return NULL;
759 /* Allocate a temporary stack slot and record it for possible later
760 reuse.
762 MODE is the machine mode to be given to the returned rtx.
764 SIZE is the size in units of the space required. We do no rounding here
765 since assign_stack_local will do any required rounding.
767 TYPE is the type that will be used for the stack slot. */
770 assign_stack_temp_for_type (machine_mode mode, HOST_WIDE_INT size,
771 tree type)
773 unsigned int align;
774 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
775 rtx slot;
777 /* If SIZE is -1 it means that somebody tried to allocate a temporary
778 of a variable size. */
779 gcc_assert (size != -1);
781 align = get_stack_local_alignment (type, mode);
783 /* Try to find an available, already-allocated temporary of the proper
784 mode which meets the size and alignment requirements. Choose the
785 smallest one with the closest alignment.
787 If assign_stack_temp is called outside of the tree->rtl expansion,
788 we cannot reuse the stack slots (that may still refer to
789 VIRTUAL_STACK_VARS_REGNUM). */
790 if (!virtuals_instantiated)
792 for (p = avail_temp_slots; p; p = p->next)
794 if (p->align >= align && p->size >= size
795 && GET_MODE (p->slot) == mode
796 && objects_must_conflict_p (p->type, type)
797 && (best_p == 0 || best_p->size > p->size
798 || (best_p->size == p->size && best_p->align > p->align)))
800 if (p->align == align && p->size == size)
802 selected = p;
803 cut_slot_from_list (selected, &avail_temp_slots);
804 best_p = 0;
805 break;
807 best_p = p;
812 /* Make our best, if any, the one to use. */
813 if (best_p)
815 selected = best_p;
816 cut_slot_from_list (selected, &avail_temp_slots);
818 /* If there are enough aligned bytes left over, make them into a new
819 temp_slot so that the extra bytes don't get wasted. Do this only
820 for BLKmode slots, so that we can be sure of the alignment. */
821 if (GET_MODE (best_p->slot) == BLKmode)
823 int alignment = best_p->align / BITS_PER_UNIT;
824 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
826 if (best_p->size - rounded_size >= alignment)
828 p = ggc_alloc<temp_slot> ();
829 p->in_use = 0;
830 p->size = best_p->size - rounded_size;
831 p->base_offset = best_p->base_offset + rounded_size;
832 p->full_size = best_p->full_size - rounded_size;
833 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
834 p->align = best_p->align;
835 p->type = best_p->type;
836 insert_slot_to_list (p, &avail_temp_slots);
838 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
839 stack_slot_list);
841 best_p->size = rounded_size;
842 best_p->full_size = rounded_size;
847 /* If we still didn't find one, make a new temporary. */
848 if (selected == 0)
850 HOST_WIDE_INT frame_offset_old = frame_offset;
852 p = ggc_alloc<temp_slot> ();
854 /* We are passing an explicit alignment request to assign_stack_local.
855 One side effect of that is assign_stack_local will not round SIZE
856 to ensure the frame offset remains suitably aligned.
858 So for requests which depended on the rounding of SIZE, we go ahead
859 and round it now. We also make sure ALIGNMENT is at least
860 BIGGEST_ALIGNMENT. */
861 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
862 p->slot = assign_stack_local_1 (mode,
863 (mode == BLKmode
864 ? CEIL_ROUND (size,
865 (int) align
866 / BITS_PER_UNIT)
867 : size),
868 align, 0);
870 p->align = align;
872 /* The following slot size computation is necessary because we don't
873 know the actual size of the temporary slot until assign_stack_local
874 has performed all the frame alignment and size rounding for the
875 requested temporary. Note that extra space added for alignment
876 can be either above or below this stack slot depending on which
877 way the frame grows. We include the extra space if and only if it
878 is above this slot. */
879 if (FRAME_GROWS_DOWNWARD)
880 p->size = frame_offset_old - frame_offset;
881 else
882 p->size = size;
884 /* Now define the fields used by combine_temp_slots. */
885 if (FRAME_GROWS_DOWNWARD)
887 p->base_offset = frame_offset;
888 p->full_size = frame_offset_old - frame_offset;
890 else
892 p->base_offset = frame_offset_old;
893 p->full_size = frame_offset - frame_offset_old;
896 selected = p;
899 p = selected;
900 p->in_use = 1;
901 p->type = type;
902 p->level = temp_slot_level;
903 n_temp_slots_in_use++;
905 pp = temp_slots_at_level (p->level);
906 insert_slot_to_list (p, pp);
907 insert_temp_slot_address (XEXP (p->slot, 0), p);
909 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
910 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
911 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
913 /* If we know the alias set for the memory that will be used, use
914 it. If there's no TYPE, then we don't know anything about the
915 alias set for the memory. */
916 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
917 set_mem_align (slot, align);
919 /* If a type is specified, set the relevant flags. */
920 if (type != 0)
921 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
922 MEM_NOTRAP_P (slot) = 1;
924 return slot;
927 /* Allocate a temporary stack slot and record it for possible later
928 reuse. First two arguments are same as in preceding function. */
931 assign_stack_temp (machine_mode mode, HOST_WIDE_INT size)
933 return assign_stack_temp_for_type (mode, size, NULL_TREE);
936 /* Assign a temporary.
937 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
938 and so that should be used in error messages. In either case, we
939 allocate of the given type.
940 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
941 it is 0 if a register is OK.
942 DONT_PROMOTE is 1 if we should not promote values in register
943 to wider modes. */
946 assign_temp (tree type_or_decl, int memory_required,
947 int dont_promote ATTRIBUTE_UNUSED)
949 tree type, decl;
950 machine_mode mode;
951 #ifdef PROMOTE_MODE
952 int unsignedp;
953 #endif
955 if (DECL_P (type_or_decl))
956 decl = type_or_decl, type = TREE_TYPE (decl);
957 else
958 decl = NULL, type = type_or_decl;
960 mode = TYPE_MODE (type);
961 #ifdef PROMOTE_MODE
962 unsignedp = TYPE_UNSIGNED (type);
963 #endif
965 if (mode == BLKmode || memory_required)
967 HOST_WIDE_INT size = int_size_in_bytes (type);
968 rtx tmp;
970 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
971 problems with allocating the stack space. */
972 if (size == 0)
973 size = 1;
975 /* Unfortunately, we don't yet know how to allocate variable-sized
976 temporaries. However, sometimes we can find a fixed upper limit on
977 the size, so try that instead. */
978 else if (size == -1)
979 size = max_int_size_in_bytes (type);
981 /* The size of the temporary may be too large to fit into an integer. */
982 /* ??? Not sure this should happen except for user silliness, so limit
983 this to things that aren't compiler-generated temporaries. The
984 rest of the time we'll die in assign_stack_temp_for_type. */
985 if (decl && size == -1
986 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
988 error ("size of variable %q+D is too large", decl);
989 size = 1;
992 tmp = assign_stack_temp_for_type (mode, size, type);
993 return tmp;
996 #ifdef PROMOTE_MODE
997 if (! dont_promote)
998 mode = promote_mode (type, mode, &unsignedp);
999 #endif
1001 return gen_reg_rtx (mode);
1004 /* Combine temporary stack slots which are adjacent on the stack.
1006 This allows for better use of already allocated stack space. This is only
1007 done for BLKmode slots because we can be sure that we won't have alignment
1008 problems in this case. */
1010 static void
1011 combine_temp_slots (void)
1013 struct temp_slot *p, *q, *next, *next_q;
1014 int num_slots;
1016 /* We can't combine slots, because the information about which slot
1017 is in which alias set will be lost. */
1018 if (flag_strict_aliasing)
1019 return;
1021 /* If there are a lot of temp slots, don't do anything unless
1022 high levels of optimization. */
1023 if (! flag_expensive_optimizations)
1024 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
1025 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1026 return;
1028 for (p = avail_temp_slots; p; p = next)
1030 int delete_p = 0;
1032 next = p->next;
1034 if (GET_MODE (p->slot) != BLKmode)
1035 continue;
1037 for (q = p->next; q; q = next_q)
1039 int delete_q = 0;
1041 next_q = q->next;
1043 if (GET_MODE (q->slot) != BLKmode)
1044 continue;
1046 if (p->base_offset + p->full_size == q->base_offset)
1048 /* Q comes after P; combine Q into P. */
1049 p->size += q->size;
1050 p->full_size += q->full_size;
1051 delete_q = 1;
1053 else if (q->base_offset + q->full_size == p->base_offset)
1055 /* P comes after Q; combine P into Q. */
1056 q->size += p->size;
1057 q->full_size += p->full_size;
1058 delete_p = 1;
1059 break;
1061 if (delete_q)
1062 cut_slot_from_list (q, &avail_temp_slots);
1065 /* Either delete P or advance past it. */
1066 if (delete_p)
1067 cut_slot_from_list (p, &avail_temp_slots);
1071 /* Indicate that NEW_RTX is an alternate way of referring to the temp
1072 slot that previously was known by OLD_RTX. */
1074 void
1075 update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1077 struct temp_slot *p;
1079 if (rtx_equal_p (old_rtx, new_rtx))
1080 return;
1082 p = find_temp_slot_from_address (old_rtx);
1084 /* If we didn't find one, see if both OLD_RTX is a PLUS. If so, and
1085 NEW_RTX is a register, see if one operand of the PLUS is a
1086 temporary location. If so, NEW_RTX points into it. Otherwise,
1087 if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1088 in common between them. If so, try a recursive call on those
1089 values. */
1090 if (p == 0)
1092 if (GET_CODE (old_rtx) != PLUS)
1093 return;
1095 if (REG_P (new_rtx))
1097 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1098 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1099 return;
1101 else if (GET_CODE (new_rtx) != PLUS)
1102 return;
1104 if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1105 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1106 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1107 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1108 else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1109 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1110 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1111 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
1113 return;
1116 /* Otherwise add an alias for the temp's address. */
1117 insert_temp_slot_address (new_rtx, p);
1120 /* If X could be a reference to a temporary slot, mark that slot as
1121 belonging to the to one level higher than the current level. If X
1122 matched one of our slots, just mark that one. Otherwise, we can't
1123 easily predict which it is, so upgrade all of them.
1125 This is called when an ({...}) construct occurs and a statement
1126 returns a value in memory. */
1128 void
1129 preserve_temp_slots (rtx x)
1131 struct temp_slot *p = 0, *next;
1133 if (x == 0)
1134 return;
1136 /* If X is a register that is being used as a pointer, see if we have
1137 a temporary slot we know it points to. */
1138 if (REG_P (x) && REG_POINTER (x))
1139 p = find_temp_slot_from_address (x);
1141 /* If X is not in memory or is at a constant address, it cannot be in
1142 a temporary slot. */
1143 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1144 return;
1146 /* First see if we can find a match. */
1147 if (p == 0)
1148 p = find_temp_slot_from_address (XEXP (x, 0));
1150 if (p != 0)
1152 if (p->level == temp_slot_level)
1153 move_slot_to_level (p, temp_slot_level - 1);
1154 return;
1157 /* Otherwise, preserve all non-kept slots at this level. */
1158 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1160 next = p->next;
1161 move_slot_to_level (p, temp_slot_level - 1);
1165 /* Free all temporaries used so far. This is normally called at the
1166 end of generating code for a statement. */
1168 void
1169 free_temp_slots (void)
1171 struct temp_slot *p, *next;
1172 bool some_available = false;
1174 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1176 next = p->next;
1177 make_slot_available (p);
1178 some_available = true;
1181 if (some_available)
1183 remove_unused_temp_slot_addresses ();
1184 combine_temp_slots ();
1188 /* Push deeper into the nesting level for stack temporaries. */
1190 void
1191 push_temp_slots (void)
1193 temp_slot_level++;
1196 /* Pop a temporary nesting level. All slots in use in the current level
1197 are freed. */
1199 void
1200 pop_temp_slots (void)
1202 free_temp_slots ();
1203 temp_slot_level--;
1206 /* Initialize temporary slots. */
1208 void
1209 init_temp_slots (void)
1211 /* We have not allocated any temporaries yet. */
1212 avail_temp_slots = 0;
1213 vec_alloc (used_temp_slots, 0);
1214 temp_slot_level = 0;
1215 n_temp_slots_in_use = 0;
1217 /* Set up the table to map addresses to temp slots. */
1218 if (! temp_slot_address_table)
1219 temp_slot_address_table = hash_table<temp_address_hasher>::create_ggc (32);
1220 else
1221 temp_slot_address_table->empty ();
1224 /* Functions and data structures to keep track of the values hard regs
1225 had at the start of the function. */
1227 /* Private type used by get_hard_reg_initial_reg, get_hard_reg_initial_val,
1228 and has_hard_reg_initial_val.. */
1229 struct GTY(()) initial_value_pair {
1230 rtx hard_reg;
1231 rtx pseudo;
1233 /* ??? This could be a VEC but there is currently no way to define an
1234 opaque VEC type. This could be worked around by defining struct
1235 initial_value_pair in function.h. */
1236 struct GTY(()) initial_value_struct {
1237 int num_entries;
1238 int max_entries;
1239 initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
1242 /* If a pseudo represents an initial hard reg (or expression), return
1243 it, else return NULL_RTX. */
1246 get_hard_reg_initial_reg (rtx reg)
1248 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1249 int i;
1251 if (ivs == 0)
1252 return NULL_RTX;
1254 for (i = 0; i < ivs->num_entries; i++)
1255 if (rtx_equal_p (ivs->entries[i].pseudo, reg))
1256 return ivs->entries[i].hard_reg;
1258 return NULL_RTX;
1261 /* Make sure that there's a pseudo register of mode MODE that stores the
1262 initial value of hard register REGNO. Return an rtx for such a pseudo. */
1265 get_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1267 struct initial_value_struct *ivs;
1268 rtx rv;
1270 rv = has_hard_reg_initial_val (mode, regno);
1271 if (rv)
1272 return rv;
1274 ivs = crtl->hard_reg_initial_vals;
1275 if (ivs == 0)
1277 ivs = ggc_alloc<initial_value_struct> ();
1278 ivs->num_entries = 0;
1279 ivs->max_entries = 5;
1280 ivs->entries = ggc_vec_alloc<initial_value_pair> (5);
1281 crtl->hard_reg_initial_vals = ivs;
1284 if (ivs->num_entries >= ivs->max_entries)
1286 ivs->max_entries += 5;
1287 ivs->entries = GGC_RESIZEVEC (initial_value_pair, ivs->entries,
1288 ivs->max_entries);
1291 ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
1292 ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
1294 return ivs->entries[ivs->num_entries++].pseudo;
1297 /* See if get_hard_reg_initial_val has been used to create a pseudo
1298 for the initial value of hard register REGNO in mode MODE. Return
1299 the associated pseudo if so, otherwise return NULL. */
1302 has_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1304 struct initial_value_struct *ivs;
1305 int i;
1307 ivs = crtl->hard_reg_initial_vals;
1308 if (ivs != 0)
1309 for (i = 0; i < ivs->num_entries; i++)
1310 if (GET_MODE (ivs->entries[i].hard_reg) == mode
1311 && REGNO (ivs->entries[i].hard_reg) == regno)
1312 return ivs->entries[i].pseudo;
1314 return NULL_RTX;
1317 unsigned int
1318 emit_initial_value_sets (void)
1320 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1321 int i;
1322 rtx_insn *seq;
1324 if (ivs == 0)
1325 return 0;
1327 start_sequence ();
1328 for (i = 0; i < ivs->num_entries; i++)
1329 emit_move_insn (ivs->entries[i].pseudo, ivs->entries[i].hard_reg);
1330 seq = get_insns ();
1331 end_sequence ();
1333 emit_insn_at_entry (seq);
1334 return 0;
1337 /* Return the hardreg-pseudoreg initial values pair entry I and
1338 TRUE if I is a valid entry, or FALSE if I is not a valid entry. */
1339 bool
1340 initial_value_entry (int i, rtx *hreg, rtx *preg)
1342 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1343 if (!ivs || i >= ivs->num_entries)
1344 return false;
1346 *hreg = ivs->entries[i].hard_reg;
1347 *preg = ivs->entries[i].pseudo;
1348 return true;
1351 /* These routines are responsible for converting virtual register references
1352 to the actual hard register references once RTL generation is complete.
1354 The following four variables are used for communication between the
1355 routines. They contain the offsets of the virtual registers from their
1356 respective hard registers. */
1358 static int in_arg_offset;
1359 static int var_offset;
1360 static int dynamic_offset;
1361 static int out_arg_offset;
1362 static int cfa_offset;
1364 /* In most machines, the stack pointer register is equivalent to the bottom
1365 of the stack. */
1367 #ifndef STACK_POINTER_OFFSET
1368 #define STACK_POINTER_OFFSET 0
1369 #endif
1371 #if defined (REG_PARM_STACK_SPACE) && !defined (INCOMING_REG_PARM_STACK_SPACE)
1372 #define INCOMING_REG_PARM_STACK_SPACE REG_PARM_STACK_SPACE
1373 #endif
1375 /* If not defined, pick an appropriate default for the offset of dynamically
1376 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1377 INCOMING_REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1379 #ifndef STACK_DYNAMIC_OFFSET
1381 /* The bottom of the stack points to the actual arguments. If
1382 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1383 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1384 stack space for register parameters is not pushed by the caller, but
1385 rather part of the fixed stack areas and hence not included in
1386 `crtl->outgoing_args_size'. Nevertheless, we must allow
1387 for it when allocating stack dynamic objects. */
1389 #ifdef INCOMING_REG_PARM_STACK_SPACE
1390 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1391 ((ACCUMULATE_OUTGOING_ARGS \
1392 ? (crtl->outgoing_args_size \
1393 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1394 : INCOMING_REG_PARM_STACK_SPACE (FNDECL))) \
1395 : 0) + (STACK_POINTER_OFFSET))
1396 #else
1397 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1398 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0) \
1399 + (STACK_POINTER_OFFSET))
1400 #endif
1401 #endif
1404 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1405 is a virtual register, return the equivalent hard register and set the
1406 offset indirectly through the pointer. Otherwise, return 0. */
1408 static rtx
1409 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
1411 rtx new_rtx;
1412 HOST_WIDE_INT offset;
1414 if (x == virtual_incoming_args_rtx)
1416 if (stack_realign_drap)
1418 /* Replace virtual_incoming_args_rtx with internal arg
1419 pointer if DRAP is used to realign stack. */
1420 new_rtx = crtl->args.internal_arg_pointer;
1421 offset = 0;
1423 else
1424 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1426 else if (x == virtual_stack_vars_rtx)
1427 new_rtx = frame_pointer_rtx, offset = var_offset;
1428 else if (x == virtual_stack_dynamic_rtx)
1429 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1430 else if (x == virtual_outgoing_args_rtx)
1431 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1432 else if (x == virtual_cfa_rtx)
1434 #ifdef FRAME_POINTER_CFA_OFFSET
1435 new_rtx = frame_pointer_rtx;
1436 #else
1437 new_rtx = arg_pointer_rtx;
1438 #endif
1439 offset = cfa_offset;
1441 else if (x == virtual_preferred_stack_boundary_rtx)
1443 new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1444 offset = 0;
1446 else
1447 return NULL_RTX;
1449 *poffset = offset;
1450 return new_rtx;
1453 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1454 registers present inside of *LOC. The expression is simplified,
1455 as much as possible, but is not to be considered "valid" in any sense
1456 implied by the target. Return true if any change is made. */
1458 static bool
1459 instantiate_virtual_regs_in_rtx (rtx *loc)
1461 if (!*loc)
1462 return false;
1463 bool changed = false;
1464 subrtx_ptr_iterator::array_type array;
1465 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
1467 rtx *loc = *iter;
1468 if (rtx x = *loc)
1470 rtx new_rtx;
1471 HOST_WIDE_INT offset;
1472 switch (GET_CODE (x))
1474 case REG:
1475 new_rtx = instantiate_new_reg (x, &offset);
1476 if (new_rtx)
1478 *loc = plus_constant (GET_MODE (x), new_rtx, offset);
1479 changed = true;
1481 iter.skip_subrtxes ();
1482 break;
1484 case PLUS:
1485 new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1486 if (new_rtx)
1488 XEXP (x, 0) = new_rtx;
1489 *loc = plus_constant (GET_MODE (x), x, offset, true);
1490 changed = true;
1491 iter.skip_subrtxes ();
1492 break;
1495 /* FIXME -- from old code */
1496 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1497 we can commute the PLUS and SUBREG because pointers into the
1498 frame are well-behaved. */
1499 break;
1501 default:
1502 break;
1506 return changed;
1509 /* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1510 matches the predicate for insn CODE operand OPERAND. */
1512 static int
1513 safe_insn_predicate (int code, int operand, rtx x)
1515 return code < 0 || insn_operand_matches ((enum insn_code) code, operand, x);
1518 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1519 registers present inside of insn. The result will be a valid insn. */
1521 static void
1522 instantiate_virtual_regs_in_insn (rtx_insn *insn)
1524 HOST_WIDE_INT offset;
1525 int insn_code, i;
1526 bool any_change = false;
1527 rtx set, new_rtx, x;
1528 rtx_insn *seq;
1530 /* There are some special cases to be handled first. */
1531 set = single_set (insn);
1532 if (set)
1534 /* We're allowed to assign to a virtual register. This is interpreted
1535 to mean that the underlying register gets assigned the inverse
1536 transformation. This is used, for example, in the handling of
1537 non-local gotos. */
1538 new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1539 if (new_rtx)
1541 start_sequence ();
1543 instantiate_virtual_regs_in_rtx (&SET_SRC (set));
1544 x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
1545 gen_int_mode (-offset, GET_MODE (new_rtx)));
1546 x = force_operand (x, new_rtx);
1547 if (x != new_rtx)
1548 emit_move_insn (new_rtx, x);
1550 seq = get_insns ();
1551 end_sequence ();
1553 emit_insn_before (seq, insn);
1554 delete_insn (insn);
1555 return;
1558 /* Handle a straight copy from a virtual register by generating a
1559 new add insn. The difference between this and falling through
1560 to the generic case is avoiding a new pseudo and eliminating a
1561 move insn in the initial rtl stream. */
1562 new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1563 if (new_rtx && offset != 0
1564 && REG_P (SET_DEST (set))
1565 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1567 start_sequence ();
1569 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS, new_rtx,
1570 gen_int_mode (offset,
1571 GET_MODE (SET_DEST (set))),
1572 SET_DEST (set), 1, OPTAB_LIB_WIDEN);
1573 if (x != SET_DEST (set))
1574 emit_move_insn (SET_DEST (set), x);
1576 seq = get_insns ();
1577 end_sequence ();
1579 emit_insn_before (seq, insn);
1580 delete_insn (insn);
1581 return;
1584 extract_insn (insn);
1585 insn_code = INSN_CODE (insn);
1587 /* Handle a plus involving a virtual register by determining if the
1588 operands remain valid if they're modified in place. */
1589 if (GET_CODE (SET_SRC (set)) == PLUS
1590 && recog_data.n_operands >= 3
1591 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1592 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1593 && CONST_INT_P (recog_data.operand[2])
1594 && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
1596 offset += INTVAL (recog_data.operand[2]);
1598 /* If the sum is zero, then replace with a plain move. */
1599 if (offset == 0
1600 && REG_P (SET_DEST (set))
1601 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1603 start_sequence ();
1604 emit_move_insn (SET_DEST (set), new_rtx);
1605 seq = get_insns ();
1606 end_sequence ();
1608 emit_insn_before (seq, insn);
1609 delete_insn (insn);
1610 return;
1613 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1615 /* Using validate_change and apply_change_group here leaves
1616 recog_data in an invalid state. Since we know exactly what
1617 we want to check, do those two by hand. */
1618 if (safe_insn_predicate (insn_code, 1, new_rtx)
1619 && safe_insn_predicate (insn_code, 2, x))
1621 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1622 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1623 any_change = true;
1625 /* Fall through into the regular operand fixup loop in
1626 order to take care of operands other than 1 and 2. */
1630 else
1632 extract_insn (insn);
1633 insn_code = INSN_CODE (insn);
1636 /* In the general case, we expect virtual registers to appear only in
1637 operands, and then only as either bare registers or inside memories. */
1638 for (i = 0; i < recog_data.n_operands; ++i)
1640 x = recog_data.operand[i];
1641 switch (GET_CODE (x))
1643 case MEM:
1645 rtx addr = XEXP (x, 0);
1647 if (!instantiate_virtual_regs_in_rtx (&addr))
1648 continue;
1650 start_sequence ();
1651 x = replace_equiv_address (x, addr, true);
1652 /* It may happen that the address with the virtual reg
1653 was valid (e.g. based on the virtual stack reg, which might
1654 be acceptable to the predicates with all offsets), whereas
1655 the address now isn't anymore, for instance when the address
1656 is still offsetted, but the base reg isn't virtual-stack-reg
1657 anymore. Below we would do a force_reg on the whole operand,
1658 but this insn might actually only accept memory. Hence,
1659 before doing that last resort, try to reload the address into
1660 a register, so this operand stays a MEM. */
1661 if (!safe_insn_predicate (insn_code, i, x))
1663 addr = force_reg (GET_MODE (addr), addr);
1664 x = replace_equiv_address (x, addr, true);
1666 seq = get_insns ();
1667 end_sequence ();
1668 if (seq)
1669 emit_insn_before (seq, insn);
1671 break;
1673 case REG:
1674 new_rtx = instantiate_new_reg (x, &offset);
1675 if (new_rtx == NULL)
1676 continue;
1677 if (offset == 0)
1678 x = new_rtx;
1679 else
1681 start_sequence ();
1683 /* Careful, special mode predicates may have stuff in
1684 insn_data[insn_code].operand[i].mode that isn't useful
1685 to us for computing a new value. */
1686 /* ??? Recognize address_operand and/or "p" constraints
1687 to see if (plus new offset) is a valid before we put
1688 this through expand_simple_binop. */
1689 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1690 gen_int_mode (offset, GET_MODE (x)),
1691 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1692 seq = get_insns ();
1693 end_sequence ();
1694 emit_insn_before (seq, insn);
1696 break;
1698 case SUBREG:
1699 new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1700 if (new_rtx == NULL)
1701 continue;
1702 if (offset != 0)
1704 start_sequence ();
1705 new_rtx = expand_simple_binop
1706 (GET_MODE (new_rtx), PLUS, new_rtx,
1707 gen_int_mode (offset, GET_MODE (new_rtx)),
1708 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1709 seq = get_insns ();
1710 end_sequence ();
1711 emit_insn_before (seq, insn);
1713 x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1714 GET_MODE (new_rtx), SUBREG_BYTE (x));
1715 gcc_assert (x);
1716 break;
1718 default:
1719 continue;
1722 /* At this point, X contains the new value for the operand.
1723 Validate the new value vs the insn predicate. Note that
1724 asm insns will have insn_code -1 here. */
1725 if (!safe_insn_predicate (insn_code, i, x))
1727 start_sequence ();
1728 if (REG_P (x))
1730 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1731 x = copy_to_reg (x);
1733 else
1734 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1735 seq = get_insns ();
1736 end_sequence ();
1737 if (seq)
1738 emit_insn_before (seq, insn);
1741 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1742 any_change = true;
1745 if (any_change)
1747 /* Propagate operand changes into the duplicates. */
1748 for (i = 0; i < recog_data.n_dups; ++i)
1749 *recog_data.dup_loc[i]
1750 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1752 /* Force re-recognition of the instruction for validation. */
1753 INSN_CODE (insn) = -1;
1756 if (asm_noperands (PATTERN (insn)) >= 0)
1758 if (!check_asm_operands (PATTERN (insn)))
1760 error_for_asm (insn, "impossible constraint in %<asm%>");
1761 /* For asm goto, instead of fixing up all the edges
1762 just clear the template and clear input operands
1763 (asm goto doesn't have any output operands). */
1764 if (JUMP_P (insn))
1766 rtx asm_op = extract_asm_operands (PATTERN (insn));
1767 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1768 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1769 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1771 else
1772 delete_insn (insn);
1775 else
1777 if (recog_memoized (insn) < 0)
1778 fatal_insn_not_found (insn);
1782 /* Subroutine of instantiate_decls. Given RTL representing a decl,
1783 do any instantiation required. */
1785 void
1786 instantiate_decl_rtl (rtx x)
1788 rtx addr;
1790 if (x == 0)
1791 return;
1793 /* If this is a CONCAT, recurse for the pieces. */
1794 if (GET_CODE (x) == CONCAT)
1796 instantiate_decl_rtl (XEXP (x, 0));
1797 instantiate_decl_rtl (XEXP (x, 1));
1798 return;
1801 /* If this is not a MEM, no need to do anything. Similarly if the
1802 address is a constant or a register that is not a virtual register. */
1803 if (!MEM_P (x))
1804 return;
1806 addr = XEXP (x, 0);
1807 if (CONSTANT_P (addr)
1808 || (REG_P (addr)
1809 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1810 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1811 return;
1813 instantiate_virtual_regs_in_rtx (&XEXP (x, 0));
1816 /* Helper for instantiate_decls called via walk_tree: Process all decls
1817 in the given DECL_VALUE_EXPR. */
1819 static tree
1820 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1822 tree t = *tp;
1823 if (! EXPR_P (t))
1825 *walk_subtrees = 0;
1826 if (DECL_P (t))
1828 if (DECL_RTL_SET_P (t))
1829 instantiate_decl_rtl (DECL_RTL (t));
1830 if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1831 && DECL_INCOMING_RTL (t))
1832 instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1833 if ((TREE_CODE (t) == VAR_DECL
1834 || TREE_CODE (t) == RESULT_DECL)
1835 && DECL_HAS_VALUE_EXPR_P (t))
1837 tree v = DECL_VALUE_EXPR (t);
1838 walk_tree (&v, instantiate_expr, NULL, NULL);
1842 return NULL;
1845 /* Subroutine of instantiate_decls: Process all decls in the given
1846 BLOCK node and all its subblocks. */
1848 static void
1849 instantiate_decls_1 (tree let)
1851 tree t;
1853 for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1855 if (DECL_RTL_SET_P (t))
1856 instantiate_decl_rtl (DECL_RTL (t));
1857 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t))
1859 tree v = DECL_VALUE_EXPR (t);
1860 walk_tree (&v, instantiate_expr, NULL, NULL);
1864 /* Process all subblocks. */
1865 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1866 instantiate_decls_1 (t);
1869 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1870 all virtual registers in their DECL_RTL's. */
1872 static void
1873 instantiate_decls (tree fndecl)
1875 tree decl;
1876 unsigned ix;
1878 /* Process all parameters of the function. */
1879 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1881 instantiate_decl_rtl (DECL_RTL (decl));
1882 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1883 if (DECL_HAS_VALUE_EXPR_P (decl))
1885 tree v = DECL_VALUE_EXPR (decl);
1886 walk_tree (&v, instantiate_expr, NULL, NULL);
1890 if ((decl = DECL_RESULT (fndecl))
1891 && TREE_CODE (decl) == RESULT_DECL)
1893 if (DECL_RTL_SET_P (decl))
1894 instantiate_decl_rtl (DECL_RTL (decl));
1895 if (DECL_HAS_VALUE_EXPR_P (decl))
1897 tree v = DECL_VALUE_EXPR (decl);
1898 walk_tree (&v, instantiate_expr, NULL, NULL);
1902 /* Process the saved static chain if it exists. */
1903 decl = DECL_STRUCT_FUNCTION (fndecl)->static_chain_decl;
1904 if (decl && DECL_HAS_VALUE_EXPR_P (decl))
1905 instantiate_decl_rtl (DECL_RTL (DECL_VALUE_EXPR (decl)));
1907 /* Now process all variables defined in the function or its subblocks. */
1908 instantiate_decls_1 (DECL_INITIAL (fndecl));
1910 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1911 if (DECL_RTL_SET_P (decl))
1912 instantiate_decl_rtl (DECL_RTL (decl));
1913 vec_free (cfun->local_decls);
1916 /* Pass through the INSNS of function FNDECL and convert virtual register
1917 references to hard register references. */
1919 static unsigned int
1920 instantiate_virtual_regs (void)
1922 rtx_insn *insn;
1924 /* Compute the offsets to use for this function. */
1925 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1926 var_offset = STARTING_FRAME_OFFSET;
1927 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1928 out_arg_offset = STACK_POINTER_OFFSET;
1929 #ifdef FRAME_POINTER_CFA_OFFSET
1930 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1931 #else
1932 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1933 #endif
1935 /* Initialize recognition, indicating that volatile is OK. */
1936 init_recog ();
1938 /* Scan through all the insns, instantiating every virtual register still
1939 present. */
1940 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1941 if (INSN_P (insn))
1943 /* These patterns in the instruction stream can never be recognized.
1944 Fortunately, they shouldn't contain virtual registers either. */
1945 if (GET_CODE (PATTERN (insn)) == USE
1946 || GET_CODE (PATTERN (insn)) == CLOBBER
1947 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
1948 continue;
1949 else if (DEBUG_INSN_P (insn))
1950 instantiate_virtual_regs_in_rtx (&INSN_VAR_LOCATION (insn));
1951 else
1952 instantiate_virtual_regs_in_insn (insn);
1954 if (insn->deleted ())
1955 continue;
1957 instantiate_virtual_regs_in_rtx (&REG_NOTES (insn));
1959 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
1960 if (CALL_P (insn))
1961 instantiate_virtual_regs_in_rtx (&CALL_INSN_FUNCTION_USAGE (insn));
1964 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
1965 instantiate_decls (current_function_decl);
1967 targetm.instantiate_decls ();
1969 /* Indicate that, from now on, assign_stack_local should use
1970 frame_pointer_rtx. */
1971 virtuals_instantiated = 1;
1973 return 0;
1976 namespace {
1978 const pass_data pass_data_instantiate_virtual_regs =
1980 RTL_PASS, /* type */
1981 "vregs", /* name */
1982 OPTGROUP_NONE, /* optinfo_flags */
1983 TV_NONE, /* tv_id */
1984 0, /* properties_required */
1985 0, /* properties_provided */
1986 0, /* properties_destroyed */
1987 0, /* todo_flags_start */
1988 0, /* todo_flags_finish */
1991 class pass_instantiate_virtual_regs : public rtl_opt_pass
1993 public:
1994 pass_instantiate_virtual_regs (gcc::context *ctxt)
1995 : rtl_opt_pass (pass_data_instantiate_virtual_regs, ctxt)
1998 /* opt_pass methods: */
1999 virtual unsigned int execute (function *)
2001 return instantiate_virtual_regs ();
2004 }; // class pass_instantiate_virtual_regs
2006 } // anon namespace
2008 rtl_opt_pass *
2009 make_pass_instantiate_virtual_regs (gcc::context *ctxt)
2011 return new pass_instantiate_virtual_regs (ctxt);
2015 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
2016 This means a type for which function calls must pass an address to the
2017 function or get an address back from the function.
2018 EXP may be a type node or an expression (whose type is tested). */
2021 aggregate_value_p (const_tree exp, const_tree fntype)
2023 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
2024 int i, regno, nregs;
2025 rtx reg;
2027 if (fntype)
2028 switch (TREE_CODE (fntype))
2030 case CALL_EXPR:
2032 tree fndecl = get_callee_fndecl (fntype);
2033 if (fndecl)
2034 fntype = TREE_TYPE (fndecl);
2035 else if (CALL_EXPR_FN (fntype))
2036 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype)));
2037 else
2038 /* For internal functions, assume nothing needs to be
2039 returned in memory. */
2040 return 0;
2042 break;
2043 case FUNCTION_DECL:
2044 fntype = TREE_TYPE (fntype);
2045 break;
2046 case FUNCTION_TYPE:
2047 case METHOD_TYPE:
2048 break;
2049 case IDENTIFIER_NODE:
2050 fntype = NULL_TREE;
2051 break;
2052 default:
2053 /* We don't expect other tree types here. */
2054 gcc_unreachable ();
2057 if (VOID_TYPE_P (type))
2058 return 0;
2060 /* If a record should be passed the same as its first (and only) member
2061 don't pass it as an aggregate. */
2062 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2063 return aggregate_value_p (first_field (type), fntype);
2065 /* If the front end has decided that this needs to be passed by
2066 reference, do so. */
2067 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2068 && DECL_BY_REFERENCE (exp))
2069 return 1;
2071 /* Function types that are TREE_ADDRESSABLE force return in memory. */
2072 if (fntype && TREE_ADDRESSABLE (fntype))
2073 return 1;
2075 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2076 and thus can't be returned in registers. */
2077 if (TREE_ADDRESSABLE (type))
2078 return 1;
2080 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2081 return 1;
2083 if (targetm.calls.return_in_memory (type, fntype))
2084 return 1;
2086 /* Make sure we have suitable call-clobbered regs to return
2087 the value in; if not, we must return it in memory. */
2088 reg = hard_function_value (type, 0, fntype, 0);
2090 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2091 it is OK. */
2092 if (!REG_P (reg))
2093 return 0;
2095 regno = REGNO (reg);
2096 nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
2097 for (i = 0; i < nregs; i++)
2098 if (! call_used_regs[regno + i])
2099 return 1;
2101 return 0;
2104 /* Return true if we should assign DECL a pseudo register; false if it
2105 should live on the local stack. */
2107 bool
2108 use_register_for_decl (const_tree decl)
2110 if (TREE_CODE (decl) == SSA_NAME)
2112 /* We often try to use the SSA_NAME, instead of its underlying
2113 decl, to get type information and guide decisions, to avoid
2114 differences of behavior between anonymous and named
2115 variables, but in this one case we have to go for the actual
2116 variable if there is one. The main reason is that, at least
2117 at -O0, we want to place user variables on the stack, but we
2118 don't mind using pseudos for anonymous or ignored temps.
2119 Should we take the SSA_NAME, we'd conclude all SSA_NAMEs
2120 should go in pseudos, whereas their corresponding variables
2121 might have to go on the stack. So, disregarding the decl
2122 here would negatively impact debug info at -O0, enable
2123 coalescing between SSA_NAMEs that ought to get different
2124 stack/pseudo assignments, and get the incoming argument
2125 processing thoroughly confused by PARM_DECLs expected to live
2126 in stack slots but assigned to pseudos. */
2127 if (!SSA_NAME_VAR (decl))
2128 return TYPE_MODE (TREE_TYPE (decl)) != BLKmode
2129 && !(flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)));
2131 decl = SSA_NAME_VAR (decl);
2134 /* Honor volatile. */
2135 if (TREE_SIDE_EFFECTS (decl))
2136 return false;
2138 /* Honor addressability. */
2139 if (TREE_ADDRESSABLE (decl))
2140 return false;
2142 /* RESULT_DECLs are a bit special in that they're assigned without
2143 regard to use_register_for_decl, but we generally only store in
2144 them. If we coalesce their SSA NAMEs, we'd better return a
2145 result that matches the assignment in expand_function_start. */
2146 if (TREE_CODE (decl) == RESULT_DECL)
2148 /* If it's not an aggregate, we're going to use a REG or a
2149 PARALLEL containing a REG. */
2150 if (!aggregate_value_p (decl, current_function_decl))
2151 return true;
2153 /* If expand_function_start determines the return value, we'll
2154 use MEM if it's not by reference. */
2155 if (cfun->returns_pcc_struct
2156 || (targetm.calls.struct_value_rtx
2157 (TREE_TYPE (current_function_decl), 1)))
2158 return DECL_BY_REFERENCE (decl);
2160 /* Otherwise, we're taking an extra all.function_result_decl
2161 argument. It's set up in assign_parms_augmented_arg_list,
2162 under the (negated) conditions above, and then it's used to
2163 set up the RESULT_DECL rtl in assign_params, after looping
2164 over all parameters. Now, if the RESULT_DECL is not by
2165 reference, we'll use a MEM either way. */
2166 if (!DECL_BY_REFERENCE (decl))
2167 return false;
2169 /* Otherwise, if RESULT_DECL is DECL_BY_REFERENCE, it will take
2170 the function_result_decl's assignment. Since it's a pointer,
2171 we can short-circuit a number of the tests below, and we must
2172 duplicat e them because we don't have the
2173 function_result_decl to test. */
2174 if (!targetm.calls.allocate_stack_slots_for_args ())
2175 return true;
2176 /* We don't set DECL_IGNORED_P for the function_result_decl. */
2177 if (optimize)
2178 return true;
2179 /* We don't set DECL_REGISTER for the function_result_decl. */
2180 return false;
2183 /* Decl is implicitly addressible by bound stores and loads
2184 if it is an aggregate holding bounds. */
2185 if (chkp_function_instrumented_p (current_function_decl)
2186 && TREE_TYPE (decl)
2187 && !BOUNDED_P (decl)
2188 && chkp_type_has_pointer (TREE_TYPE (decl)))
2189 return false;
2191 /* Only register-like things go in registers. */
2192 if (DECL_MODE (decl) == BLKmode)
2193 return false;
2195 /* If -ffloat-store specified, don't put explicit float variables
2196 into registers. */
2197 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2198 propagates values across these stores, and it probably shouldn't. */
2199 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2200 return false;
2202 if (!targetm.calls.allocate_stack_slots_for_args ())
2203 return true;
2205 /* If we're not interested in tracking debugging information for
2206 this decl, then we can certainly put it in a register. */
2207 if (DECL_IGNORED_P (decl))
2208 return true;
2210 if (optimize)
2211 return true;
2213 if (!DECL_REGISTER (decl))
2214 return false;
2216 switch (TREE_CODE (TREE_TYPE (decl)))
2218 case RECORD_TYPE:
2219 case UNION_TYPE:
2220 case QUAL_UNION_TYPE:
2221 /* When not optimizing, disregard register keyword for variables with
2222 types containing methods, otherwise the methods won't be callable
2223 from the debugger. */
2224 if (TYPE_METHODS (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
2225 return false;
2226 break;
2227 default:
2228 break;
2231 return true;
2234 /* Structures to communicate between the subroutines of assign_parms.
2235 The first holds data persistent across all parameters, the second
2236 is cleared out for each parameter. */
2238 struct assign_parm_data_all
2240 /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2241 should become a job of the target or otherwise encapsulated. */
2242 CUMULATIVE_ARGS args_so_far_v;
2243 cumulative_args_t args_so_far;
2244 struct args_size stack_args_size;
2245 tree function_result_decl;
2246 tree orig_fnargs;
2247 rtx_insn *first_conversion_insn;
2248 rtx_insn *last_conversion_insn;
2249 HOST_WIDE_INT pretend_args_size;
2250 HOST_WIDE_INT extra_pretend_bytes;
2251 int reg_parm_stack_space;
2254 struct assign_parm_data_one
2256 tree nominal_type;
2257 tree passed_type;
2258 rtx entry_parm;
2259 rtx stack_parm;
2260 machine_mode nominal_mode;
2261 machine_mode passed_mode;
2262 machine_mode promoted_mode;
2263 struct locate_and_pad_arg_data locate;
2264 int partial;
2265 BOOL_BITFIELD named_arg : 1;
2266 BOOL_BITFIELD passed_pointer : 1;
2267 BOOL_BITFIELD on_stack : 1;
2268 BOOL_BITFIELD loaded_in_reg : 1;
2271 struct bounds_parm_data
2273 assign_parm_data_one parm_data;
2274 tree bounds_parm;
2275 tree ptr_parm;
2276 rtx ptr_entry;
2277 int bound_no;
2280 /* A subroutine of assign_parms. Initialize ALL. */
2282 static void
2283 assign_parms_initialize_all (struct assign_parm_data_all *all)
2285 tree fntype ATTRIBUTE_UNUSED;
2287 memset (all, 0, sizeof (*all));
2289 fntype = TREE_TYPE (current_function_decl);
2291 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
2292 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2293 #else
2294 INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2295 current_function_decl, -1);
2296 #endif
2297 all->args_so_far = pack_cumulative_args (&all->args_so_far_v);
2299 #ifdef INCOMING_REG_PARM_STACK_SPACE
2300 all->reg_parm_stack_space
2301 = INCOMING_REG_PARM_STACK_SPACE (current_function_decl);
2302 #endif
2305 /* If ARGS contains entries with complex types, split the entry into two
2306 entries of the component type. Return a new list of substitutions are
2307 needed, else the old list. */
2309 static void
2310 split_complex_args (vec<tree> *args)
2312 unsigned i;
2313 tree p;
2315 FOR_EACH_VEC_ELT (*args, i, p)
2317 tree type = TREE_TYPE (p);
2318 if (TREE_CODE (type) == COMPLEX_TYPE
2319 && targetm.calls.split_complex_arg (type))
2321 tree decl;
2322 tree subtype = TREE_TYPE (type);
2323 bool addressable = TREE_ADDRESSABLE (p);
2325 /* Rewrite the PARM_DECL's type with its component. */
2326 p = copy_node (p);
2327 TREE_TYPE (p) = subtype;
2328 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2329 DECL_MODE (p) = VOIDmode;
2330 DECL_SIZE (p) = NULL;
2331 DECL_SIZE_UNIT (p) = NULL;
2332 /* If this arg must go in memory, put it in a pseudo here.
2333 We can't allow it to go in memory as per normal parms,
2334 because the usual place might not have the imag part
2335 adjacent to the real part. */
2336 DECL_ARTIFICIAL (p) = addressable;
2337 DECL_IGNORED_P (p) = addressable;
2338 TREE_ADDRESSABLE (p) = 0;
2339 layout_decl (p, 0);
2340 (*args)[i] = p;
2342 /* Build a second synthetic decl. */
2343 decl = build_decl (EXPR_LOCATION (p),
2344 PARM_DECL, NULL_TREE, subtype);
2345 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2346 DECL_ARTIFICIAL (decl) = addressable;
2347 DECL_IGNORED_P (decl) = addressable;
2348 layout_decl (decl, 0);
2349 args->safe_insert (++i, decl);
2354 /* A subroutine of assign_parms. Adjust the parameter list to incorporate
2355 the hidden struct return argument, and (abi willing) complex args.
2356 Return the new parameter list. */
2358 static vec<tree>
2359 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2361 tree fndecl = current_function_decl;
2362 tree fntype = TREE_TYPE (fndecl);
2363 vec<tree> fnargs = vNULL;
2364 tree arg;
2366 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2367 fnargs.safe_push (arg);
2369 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2371 /* If struct value address is treated as the first argument, make it so. */
2372 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2373 && ! cfun->returns_pcc_struct
2374 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2376 tree type = build_pointer_type (TREE_TYPE (fntype));
2377 tree decl;
2379 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2380 PARM_DECL, get_identifier (".result_ptr"), type);
2381 DECL_ARG_TYPE (decl) = type;
2382 DECL_ARTIFICIAL (decl) = 1;
2383 DECL_NAMELESS (decl) = 1;
2384 TREE_CONSTANT (decl) = 1;
2385 /* We don't set DECL_IGNORED_P or DECL_REGISTER here. If this
2386 changes, the end of the RESULT_DECL handling block in
2387 use_register_for_decl must be adjusted to match. */
2389 DECL_CHAIN (decl) = all->orig_fnargs;
2390 all->orig_fnargs = decl;
2391 fnargs.safe_insert (0, decl);
2393 all->function_result_decl = decl;
2395 /* If function is instrumented then bounds of the
2396 passed structure address is the second argument. */
2397 if (chkp_function_instrumented_p (fndecl))
2399 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2400 PARM_DECL, get_identifier (".result_bnd"),
2401 pointer_bounds_type_node);
2402 DECL_ARG_TYPE (decl) = pointer_bounds_type_node;
2403 DECL_ARTIFICIAL (decl) = 1;
2404 DECL_NAMELESS (decl) = 1;
2405 TREE_CONSTANT (decl) = 1;
2407 DECL_CHAIN (decl) = DECL_CHAIN (all->orig_fnargs);
2408 DECL_CHAIN (all->orig_fnargs) = decl;
2409 fnargs.safe_insert (1, decl);
2413 /* If the target wants to split complex arguments into scalars, do so. */
2414 if (targetm.calls.split_complex_arg)
2415 split_complex_args (&fnargs);
2417 return fnargs;
2420 /* A subroutine of assign_parms. Examine PARM and pull out type and mode
2421 data for the parameter. Incorporate ABI specifics such as pass-by-
2422 reference and type promotion. */
2424 static void
2425 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2426 struct assign_parm_data_one *data)
2428 tree nominal_type, passed_type;
2429 machine_mode nominal_mode, passed_mode, promoted_mode;
2430 int unsignedp;
2432 memset (data, 0, sizeof (*data));
2434 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
2435 if (!cfun->stdarg)
2436 data->named_arg = 1; /* No variadic parms. */
2437 else if (DECL_CHAIN (parm))
2438 data->named_arg = 1; /* Not the last non-variadic parm. */
2439 else if (targetm.calls.strict_argument_naming (all->args_so_far))
2440 data->named_arg = 1; /* Only variadic ones are unnamed. */
2441 else
2442 data->named_arg = 0; /* Treat as variadic. */
2444 nominal_type = TREE_TYPE (parm);
2445 passed_type = DECL_ARG_TYPE (parm);
2447 /* Look out for errors propagating this far. Also, if the parameter's
2448 type is void then its value doesn't matter. */
2449 if (TREE_TYPE (parm) == error_mark_node
2450 /* This can happen after weird syntax errors
2451 or if an enum type is defined among the parms. */
2452 || TREE_CODE (parm) != PARM_DECL
2453 || passed_type == NULL
2454 || VOID_TYPE_P (nominal_type))
2456 nominal_type = passed_type = void_type_node;
2457 nominal_mode = passed_mode = promoted_mode = VOIDmode;
2458 goto egress;
2461 /* Find mode of arg as it is passed, and mode of arg as it should be
2462 during execution of this function. */
2463 passed_mode = TYPE_MODE (passed_type);
2464 nominal_mode = TYPE_MODE (nominal_type);
2466 /* If the parm is to be passed as a transparent union or record, use the
2467 type of the first field for the tests below. We have already verified
2468 that the modes are the same. */
2469 if ((TREE_CODE (passed_type) == UNION_TYPE
2470 || TREE_CODE (passed_type) == RECORD_TYPE)
2471 && TYPE_TRANSPARENT_AGGR (passed_type))
2472 passed_type = TREE_TYPE (first_field (passed_type));
2474 /* See if this arg was passed by invisible reference. */
2475 if (pass_by_reference (&all->args_so_far_v, passed_mode,
2476 passed_type, data->named_arg))
2478 passed_type = nominal_type = build_pointer_type (passed_type);
2479 data->passed_pointer = true;
2480 passed_mode = nominal_mode = TYPE_MODE (nominal_type);
2483 /* Find mode as it is passed by the ABI. */
2484 unsignedp = TYPE_UNSIGNED (passed_type);
2485 promoted_mode = promote_function_mode (passed_type, passed_mode, &unsignedp,
2486 TREE_TYPE (current_function_decl), 0);
2488 egress:
2489 data->nominal_type = nominal_type;
2490 data->passed_type = passed_type;
2491 data->nominal_mode = nominal_mode;
2492 data->passed_mode = passed_mode;
2493 data->promoted_mode = promoted_mode;
2496 /* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2498 static void
2499 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2500 struct assign_parm_data_one *data, bool no_rtl)
2502 int varargs_pretend_bytes = 0;
2504 targetm.calls.setup_incoming_varargs (all->args_so_far,
2505 data->promoted_mode,
2506 data->passed_type,
2507 &varargs_pretend_bytes, no_rtl);
2509 /* If the back-end has requested extra stack space, record how much is
2510 needed. Do not change pretend_args_size otherwise since it may be
2511 nonzero from an earlier partial argument. */
2512 if (varargs_pretend_bytes > 0)
2513 all->pretend_args_size = varargs_pretend_bytes;
2516 /* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2517 the incoming location of the current parameter. */
2519 static void
2520 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2521 struct assign_parm_data_one *data)
2523 HOST_WIDE_INT pretend_bytes = 0;
2524 rtx entry_parm;
2525 bool in_regs;
2527 if (data->promoted_mode == VOIDmode)
2529 data->entry_parm = data->stack_parm = const0_rtx;
2530 return;
2533 entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2534 data->promoted_mode,
2535 data->passed_type,
2536 data->named_arg);
2538 if (entry_parm == 0)
2539 data->promoted_mode = data->passed_mode;
2541 /* Determine parm's home in the stack, in case it arrives in the stack
2542 or we should pretend it did. Compute the stack position and rtx where
2543 the argument arrives and its size.
2545 There is one complexity here: If this was a parameter that would
2546 have been passed in registers, but wasn't only because it is
2547 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2548 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2549 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2550 as it was the previous time. */
2551 in_regs = (entry_parm != 0) || POINTER_BOUNDS_TYPE_P (data->passed_type);
2552 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2553 in_regs = true;
2554 #endif
2555 if (!in_regs && !data->named_arg)
2557 if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2559 rtx tem;
2560 tem = targetm.calls.function_incoming_arg (all->args_so_far,
2561 data->promoted_mode,
2562 data->passed_type, true);
2563 in_regs = tem != NULL;
2567 /* If this parameter was passed both in registers and in the stack, use
2568 the copy on the stack. */
2569 if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2570 data->passed_type))
2571 entry_parm = 0;
2573 if (entry_parm)
2575 int partial;
2577 partial = targetm.calls.arg_partial_bytes (all->args_so_far,
2578 data->promoted_mode,
2579 data->passed_type,
2580 data->named_arg);
2581 data->partial = partial;
2583 /* The caller might already have allocated stack space for the
2584 register parameters. */
2585 if (partial != 0 && all->reg_parm_stack_space == 0)
2587 /* Part of this argument is passed in registers and part
2588 is passed on the stack. Ask the prologue code to extend
2589 the stack part so that we can recreate the full value.
2591 PRETEND_BYTES is the size of the registers we need to store.
2592 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2593 stack space that the prologue should allocate.
2595 Internally, gcc assumes that the argument pointer is aligned
2596 to STACK_BOUNDARY bits. This is used both for alignment
2597 optimizations (see init_emit) and to locate arguments that are
2598 aligned to more than PARM_BOUNDARY bits. We must preserve this
2599 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2600 a stack boundary. */
2602 /* We assume at most one partial arg, and it must be the first
2603 argument on the stack. */
2604 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2606 pretend_bytes = partial;
2607 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2609 /* We want to align relative to the actual stack pointer, so
2610 don't include this in the stack size until later. */
2611 all->extra_pretend_bytes = all->pretend_args_size;
2615 locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2616 all->reg_parm_stack_space,
2617 entry_parm ? data->partial : 0, current_function_decl,
2618 &all->stack_args_size, &data->locate);
2620 /* Update parm_stack_boundary if this parameter is passed in the
2621 stack. */
2622 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2623 crtl->parm_stack_boundary = data->locate.boundary;
2625 /* Adjust offsets to include the pretend args. */
2626 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2627 data->locate.slot_offset.constant += pretend_bytes;
2628 data->locate.offset.constant += pretend_bytes;
2630 data->entry_parm = entry_parm;
2633 /* A subroutine of assign_parms. If there is actually space on the stack
2634 for this parm, count it in stack_args_size and return true. */
2636 static bool
2637 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2638 struct assign_parm_data_one *data)
2640 /* Bounds are never passed on the stack to keep compatibility
2641 with not instrumented code. */
2642 if (POINTER_BOUNDS_TYPE_P (data->passed_type))
2643 return false;
2644 /* Trivially true if we've no incoming register. */
2645 else if (data->entry_parm == NULL)
2647 /* Also true if we're partially in registers and partially not,
2648 since we've arranged to drop the entire argument on the stack. */
2649 else if (data->partial != 0)
2651 /* Also true if the target says that it's passed in both registers
2652 and on the stack. */
2653 else if (GET_CODE (data->entry_parm) == PARALLEL
2654 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2656 /* Also true if the target says that there's stack allocated for
2657 all register parameters. */
2658 else if (all->reg_parm_stack_space > 0)
2660 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2661 else
2662 return false;
2664 all->stack_args_size.constant += data->locate.size.constant;
2665 if (data->locate.size.var)
2666 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2668 return true;
2671 /* A subroutine of assign_parms. Given that this parameter is allocated
2672 stack space by the ABI, find it. */
2674 static void
2675 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2677 rtx offset_rtx, stack_parm;
2678 unsigned int align, boundary;
2680 /* If we're passing this arg using a reg, make its stack home the
2681 aligned stack slot. */
2682 if (data->entry_parm)
2683 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2684 else
2685 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2687 stack_parm = crtl->args.internal_arg_pointer;
2688 if (offset_rtx != const0_rtx)
2689 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2690 stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2692 if (!data->passed_pointer)
2694 set_mem_attributes (stack_parm, parm, 1);
2695 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2696 while promoted mode's size is needed. */
2697 if (data->promoted_mode != BLKmode
2698 && data->promoted_mode != DECL_MODE (parm))
2700 set_mem_size (stack_parm, GET_MODE_SIZE (data->promoted_mode));
2701 if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2703 int offset = subreg_lowpart_offset (DECL_MODE (parm),
2704 data->promoted_mode);
2705 if (offset)
2706 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2711 boundary = data->locate.boundary;
2712 align = BITS_PER_UNIT;
2714 /* If we're padding upward, we know that the alignment of the slot
2715 is TARGET_FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2716 intentionally forcing upward padding. Otherwise we have to come
2717 up with a guess at the alignment based on OFFSET_RTX. */
2718 if (data->locate.where_pad != downward || data->entry_parm)
2719 align = boundary;
2720 else if (CONST_INT_P (offset_rtx))
2722 align = INTVAL (offset_rtx) * BITS_PER_UNIT | boundary;
2723 align = align & -align;
2725 set_mem_align (stack_parm, align);
2727 if (data->entry_parm)
2728 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2730 data->stack_parm = stack_parm;
2733 /* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2734 always valid and contiguous. */
2736 static void
2737 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2739 rtx entry_parm = data->entry_parm;
2740 rtx stack_parm = data->stack_parm;
2742 /* If this parm was passed part in regs and part in memory, pretend it
2743 arrived entirely in memory by pushing the register-part onto the stack.
2744 In the special case of a DImode or DFmode that is split, we could put
2745 it together in a pseudoreg directly, but for now that's not worth
2746 bothering with. */
2747 if (data->partial != 0)
2749 /* Handle calls that pass values in multiple non-contiguous
2750 locations. The Irix 6 ABI has examples of this. */
2751 if (GET_CODE (entry_parm) == PARALLEL)
2752 emit_group_store (validize_mem (copy_rtx (stack_parm)), entry_parm,
2753 data->passed_type,
2754 int_size_in_bytes (data->passed_type));
2755 else
2757 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2758 move_block_from_reg (REGNO (entry_parm),
2759 validize_mem (copy_rtx (stack_parm)),
2760 data->partial / UNITS_PER_WORD);
2763 entry_parm = stack_parm;
2766 /* If we didn't decide this parm came in a register, by default it came
2767 on the stack. */
2768 else if (entry_parm == NULL)
2769 entry_parm = stack_parm;
2771 /* When an argument is passed in multiple locations, we can't make use
2772 of this information, but we can save some copying if the whole argument
2773 is passed in a single register. */
2774 else if (GET_CODE (entry_parm) == PARALLEL
2775 && data->nominal_mode != BLKmode
2776 && data->passed_mode != BLKmode)
2778 size_t i, len = XVECLEN (entry_parm, 0);
2780 for (i = 0; i < len; i++)
2781 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2782 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2783 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2784 == data->passed_mode)
2785 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2787 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2788 break;
2792 data->entry_parm = entry_parm;
2795 /* A subroutine of assign_parms. Reconstitute any values which were
2796 passed in multiple registers and would fit in a single register. */
2798 static void
2799 assign_parm_remove_parallels (struct assign_parm_data_one *data)
2801 rtx entry_parm = data->entry_parm;
2803 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2804 This can be done with register operations rather than on the
2805 stack, even if we will store the reconstituted parameter on the
2806 stack later. */
2807 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2809 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2810 emit_group_store (parmreg, entry_parm, data->passed_type,
2811 GET_MODE_SIZE (GET_MODE (entry_parm)));
2812 entry_parm = parmreg;
2815 data->entry_parm = entry_parm;
2818 /* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2819 always valid and properly aligned. */
2821 static void
2822 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2824 rtx stack_parm = data->stack_parm;
2826 /* If we can't trust the parm stack slot to be aligned enough for its
2827 ultimate type, don't use that slot after entry. We'll make another
2828 stack slot, if we need one. */
2829 if (stack_parm
2830 && ((STRICT_ALIGNMENT
2831 && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2832 || (data->nominal_type
2833 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2834 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2835 stack_parm = NULL;
2837 /* If parm was passed in memory, and we need to convert it on entry,
2838 don't store it back in that same slot. */
2839 else if (data->entry_parm == stack_parm
2840 && data->nominal_mode != BLKmode
2841 && data->nominal_mode != data->passed_mode)
2842 stack_parm = NULL;
2844 /* If stack protection is in effect for this function, don't leave any
2845 pointers in their passed stack slots. */
2846 else if (crtl->stack_protect_guard
2847 && (flag_stack_protect == 2
2848 || data->passed_pointer
2849 || POINTER_TYPE_P (data->nominal_type)))
2850 stack_parm = NULL;
2852 data->stack_parm = stack_parm;
2855 /* A subroutine of assign_parms. Return true if the current parameter
2856 should be stored as a BLKmode in the current frame. */
2858 static bool
2859 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2861 if (data->nominal_mode == BLKmode)
2862 return true;
2863 if (GET_MODE (data->entry_parm) == BLKmode)
2864 return true;
2866 #ifdef BLOCK_REG_PADDING
2867 /* Only assign_parm_setup_block knows how to deal with register arguments
2868 that are padded at the least significant end. */
2869 if (REG_P (data->entry_parm)
2870 && GET_MODE_SIZE (data->promoted_mode) < UNITS_PER_WORD
2871 && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2872 == (BYTES_BIG_ENDIAN ? upward : downward)))
2873 return true;
2874 #endif
2876 return false;
2879 /* A subroutine of assign_parms. Arrange for the parameter to be
2880 present and valid in DATA->STACK_RTL. */
2882 static void
2883 assign_parm_setup_block (struct assign_parm_data_all *all,
2884 tree parm, struct assign_parm_data_one *data)
2886 rtx entry_parm = data->entry_parm;
2887 rtx stack_parm = data->stack_parm;
2888 rtx target_reg = NULL_RTX;
2889 HOST_WIDE_INT size;
2890 HOST_WIDE_INT size_stored;
2892 if (GET_CODE (entry_parm) == PARALLEL)
2893 entry_parm = emit_group_move_into_temps (entry_parm);
2895 /* If we want the parameter in a pseudo, don't use a stack slot. */
2896 if (is_gimple_reg (parm) && use_register_for_decl (parm))
2898 tree def = ssa_default_def (cfun, parm);
2899 gcc_assert (def);
2900 machine_mode mode = promote_ssa_mode (def, NULL);
2901 rtx reg = gen_reg_rtx (mode);
2902 if (GET_CODE (reg) != CONCAT)
2903 stack_parm = reg;
2904 else
2905 /* This will use or allocate a stack slot that we'd rather
2906 avoid. FIXME: Could we avoid it in more cases? */
2907 target_reg = reg;
2908 data->stack_parm = NULL;
2911 size = int_size_in_bytes (data->passed_type);
2912 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2913 if (stack_parm == 0)
2915 DECL_ALIGN (parm) = MAX (DECL_ALIGN (parm), BITS_PER_WORD);
2916 stack_parm = assign_stack_local (BLKmode, size_stored,
2917 DECL_ALIGN (parm));
2918 if (GET_MODE_SIZE (GET_MODE (entry_parm)) == size)
2919 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2920 set_mem_attributes (stack_parm, parm, 1);
2923 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2924 calls that pass values in multiple non-contiguous locations. */
2925 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2927 rtx mem;
2929 /* Note that we will be storing an integral number of words.
2930 So we have to be careful to ensure that we allocate an
2931 integral number of words. We do this above when we call
2932 assign_stack_local if space was not allocated in the argument
2933 list. If it was, this will not work if PARM_BOUNDARY is not
2934 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2935 if it becomes a problem. Exception is when BLKmode arrives
2936 with arguments not conforming to word_mode. */
2938 if (data->stack_parm == 0)
2940 else if (GET_CODE (entry_parm) == PARALLEL)
2942 else
2943 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2945 mem = validize_mem (copy_rtx (stack_parm));
2947 /* Handle values in multiple non-contiguous locations. */
2948 if (GET_CODE (entry_parm) == PARALLEL)
2950 push_to_sequence2 (all->first_conversion_insn,
2951 all->last_conversion_insn);
2952 emit_group_store (mem, entry_parm, data->passed_type, size);
2953 all->first_conversion_insn = get_insns ();
2954 all->last_conversion_insn = get_last_insn ();
2955 end_sequence ();
2958 else if (size == 0)
2961 /* If SIZE is that of a mode no bigger than a word, just use
2962 that mode's store operation. */
2963 else if (size <= UNITS_PER_WORD)
2965 machine_mode mode
2966 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2968 if (mode != BLKmode
2969 #ifdef BLOCK_REG_PADDING
2970 && (size == UNITS_PER_WORD
2971 || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2972 != (BYTES_BIG_ENDIAN ? upward : downward)))
2973 #endif
2976 rtx reg;
2978 /* We are really truncating a word_mode value containing
2979 SIZE bytes into a value of mode MODE. If such an
2980 operation requires no actual instructions, we can refer
2981 to the value directly in mode MODE, otherwise we must
2982 start with the register in word_mode and explicitly
2983 convert it. */
2984 if (TRULY_NOOP_TRUNCATION (size * BITS_PER_UNIT, BITS_PER_WORD))
2985 reg = gen_rtx_REG (mode, REGNO (entry_parm));
2986 else
2988 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2989 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
2991 emit_move_insn (change_address (mem, mode, 0), reg);
2994 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
2995 machine must be aligned to the left before storing
2996 to memory. Note that the previous test doesn't
2997 handle all cases (e.g. SIZE == 3). */
2998 else if (size != UNITS_PER_WORD
2999 #ifdef BLOCK_REG_PADDING
3000 && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
3001 == downward)
3002 #else
3003 && BYTES_BIG_ENDIAN
3004 #endif
3007 rtx tem, x;
3008 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
3009 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
3011 x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
3012 tem = change_address (mem, word_mode, 0);
3013 emit_move_insn (tem, x);
3015 else if (!MEM_P (mem))
3016 emit_move_insn (mem, entry_parm);
3017 else
3018 move_block_from_reg (REGNO (entry_parm), mem,
3019 size_stored / UNITS_PER_WORD);
3021 else if (!MEM_P (mem))
3022 emit_move_insn (mem, entry_parm);
3023 else
3024 move_block_from_reg (REGNO (entry_parm), mem,
3025 size_stored / UNITS_PER_WORD);
3027 else if (data->stack_parm == 0)
3029 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3030 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
3031 BLOCK_OP_NORMAL);
3032 all->first_conversion_insn = get_insns ();
3033 all->last_conversion_insn = get_last_insn ();
3034 end_sequence ();
3037 if (target_reg)
3039 emit_move_insn (target_reg, stack_parm);
3040 stack_parm = target_reg;
3043 data->stack_parm = stack_parm;
3044 set_parm_rtl (parm, stack_parm);
3047 /* A subroutine of assign_parms. Allocate a pseudo to hold the current
3048 parameter. Get it there. Perform all ABI specified conversions. */
3050 static void
3051 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
3052 struct assign_parm_data_one *data)
3054 rtx parmreg, validated_mem;
3055 rtx equiv_stack_parm;
3056 machine_mode promoted_nominal_mode;
3057 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
3058 bool did_conversion = false;
3059 bool need_conversion, moved;
3060 rtx rtl;
3062 /* Store the parm in a pseudoregister during the function, but we may
3063 need to do it in a wider mode. Using 2 here makes the result
3064 consistent with promote_decl_mode and thus expand_expr_real_1. */
3065 promoted_nominal_mode
3066 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
3067 TREE_TYPE (current_function_decl), 2);
3069 parmreg = gen_reg_rtx (promoted_nominal_mode);
3070 if (!DECL_ARTIFICIAL (parm))
3071 mark_user_reg (parmreg);
3073 /* If this was an item that we received a pointer to,
3074 set rtl appropriately. */
3075 if (data->passed_pointer)
3077 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
3078 set_mem_attributes (rtl, parm, 1);
3080 else
3081 rtl = parmreg;
3083 assign_parm_remove_parallels (data);
3085 /* Copy the value into the register, thus bridging between
3086 assign_parm_find_data_types and expand_expr_real_1. */
3088 equiv_stack_parm = data->stack_parm;
3089 validated_mem = validize_mem (copy_rtx (data->entry_parm));
3091 need_conversion = (data->nominal_mode != data->passed_mode
3092 || promoted_nominal_mode != data->promoted_mode);
3093 moved = false;
3095 if (need_conversion
3096 && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
3097 && data->nominal_mode == data->passed_mode
3098 && data->nominal_mode == GET_MODE (data->entry_parm))
3100 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
3101 mode, by the caller. We now have to convert it to
3102 NOMINAL_MODE, if different. However, PARMREG may be in
3103 a different mode than NOMINAL_MODE if it is being stored
3104 promoted.
3106 If ENTRY_PARM is a hard register, it might be in a register
3107 not valid for operating in its mode (e.g., an odd-numbered
3108 register for a DFmode). In that case, moves are the only
3109 thing valid, so we can't do a convert from there. This
3110 occurs when the calling sequence allow such misaligned
3111 usages.
3113 In addition, the conversion may involve a call, which could
3114 clobber parameters which haven't been copied to pseudo
3115 registers yet.
3117 First, we try to emit an insn which performs the necessary
3118 conversion. We verify that this insn does not clobber any
3119 hard registers. */
3121 enum insn_code icode;
3122 rtx op0, op1;
3124 icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
3125 unsignedp);
3127 op0 = parmreg;
3128 op1 = validated_mem;
3129 if (icode != CODE_FOR_nothing
3130 && insn_operand_matches (icode, 0, op0)
3131 && insn_operand_matches (icode, 1, op1))
3133 enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
3134 rtx_insn *insn, *insns;
3135 rtx t = op1;
3136 HARD_REG_SET hardregs;
3138 start_sequence ();
3139 /* If op1 is a hard register that is likely spilled, first
3140 force it into a pseudo, otherwise combiner might extend
3141 its lifetime too much. */
3142 if (GET_CODE (t) == SUBREG)
3143 t = SUBREG_REG (t);
3144 if (REG_P (t)
3145 && HARD_REGISTER_P (t)
3146 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (t))
3147 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (t))))
3149 t = gen_reg_rtx (GET_MODE (op1));
3150 emit_move_insn (t, op1);
3152 else
3153 t = op1;
3154 rtx_insn *pat = gen_extend_insn (op0, t, promoted_nominal_mode,
3155 data->passed_mode, unsignedp);
3156 emit_insn (pat);
3157 insns = get_insns ();
3159 moved = true;
3160 CLEAR_HARD_REG_SET (hardregs);
3161 for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3163 if (INSN_P (insn))
3164 note_stores (PATTERN (insn), record_hard_reg_sets,
3165 &hardregs);
3166 if (!hard_reg_set_empty_p (hardregs))
3167 moved = false;
3170 end_sequence ();
3172 if (moved)
3174 emit_insn (insns);
3175 if (equiv_stack_parm != NULL_RTX)
3176 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3177 equiv_stack_parm);
3182 if (moved)
3183 /* Nothing to do. */
3185 else if (need_conversion)
3187 /* We did not have an insn to convert directly, or the sequence
3188 generated appeared unsafe. We must first copy the parm to a
3189 pseudo reg, and save the conversion until after all
3190 parameters have been moved. */
3192 int save_tree_used;
3193 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3195 emit_move_insn (tempreg, validated_mem);
3197 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3198 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3200 if (GET_CODE (tempreg) == SUBREG
3201 && GET_MODE (tempreg) == data->nominal_mode
3202 && REG_P (SUBREG_REG (tempreg))
3203 && data->nominal_mode == data->passed_mode
3204 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm)
3205 && GET_MODE_SIZE (GET_MODE (tempreg))
3206 < GET_MODE_SIZE (GET_MODE (data->entry_parm)))
3208 /* The argument is already sign/zero extended, so note it
3209 into the subreg. */
3210 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3211 SUBREG_PROMOTED_SET (tempreg, unsignedp);
3214 /* TREE_USED gets set erroneously during expand_assignment. */
3215 save_tree_used = TREE_USED (parm);
3216 SET_DECL_RTL (parm, rtl);
3217 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3218 SET_DECL_RTL (parm, NULL_RTX);
3219 TREE_USED (parm) = save_tree_used;
3220 all->first_conversion_insn = get_insns ();
3221 all->last_conversion_insn = get_last_insn ();
3222 end_sequence ();
3224 did_conversion = true;
3226 else
3227 emit_move_insn (parmreg, validated_mem);
3229 /* If we were passed a pointer but the actual value can safely live
3230 in a register, retrieve it and use it directly. */
3231 if (data->passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode)
3233 /* We can't use nominal_mode, because it will have been set to
3234 Pmode above. We must use the actual mode of the parm. */
3235 if (use_register_for_decl (parm))
3237 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3238 mark_user_reg (parmreg);
3240 else
3242 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3243 TYPE_MODE (TREE_TYPE (parm)),
3244 TYPE_ALIGN (TREE_TYPE (parm)));
3245 parmreg
3246 = assign_stack_local (TYPE_MODE (TREE_TYPE (parm)),
3247 GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parm))),
3248 align);
3249 set_mem_attributes (parmreg, parm, 1);
3252 if (GET_MODE (parmreg) != GET_MODE (rtl))
3254 rtx tempreg = gen_reg_rtx (GET_MODE (rtl));
3255 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3257 push_to_sequence2 (all->first_conversion_insn,
3258 all->last_conversion_insn);
3259 emit_move_insn (tempreg, rtl);
3260 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3261 emit_move_insn (parmreg, tempreg);
3262 all->first_conversion_insn = get_insns ();
3263 all->last_conversion_insn = get_last_insn ();
3264 end_sequence ();
3266 did_conversion = true;
3268 else
3269 emit_move_insn (parmreg, rtl);
3271 rtl = parmreg;
3273 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3274 now the parm. */
3275 data->stack_parm = NULL;
3278 set_parm_rtl (parm, rtl);
3280 /* Mark the register as eliminable if we did no conversion and it was
3281 copied from memory at a fixed offset, and the arg pointer was not
3282 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3283 offset formed an invalid address, such memory-equivalences as we
3284 make here would screw up life analysis for it. */
3285 if (data->nominal_mode == data->passed_mode
3286 && !did_conversion
3287 && data->stack_parm != 0
3288 && MEM_P (data->stack_parm)
3289 && data->locate.offset.var == 0
3290 && reg_mentioned_p (virtual_incoming_args_rtx,
3291 XEXP (data->stack_parm, 0)))
3293 rtx_insn *linsn = get_last_insn ();
3294 rtx_insn *sinsn;
3295 rtx set;
3297 /* Mark complex types separately. */
3298 if (GET_CODE (parmreg) == CONCAT)
3300 machine_mode submode
3301 = GET_MODE_INNER (GET_MODE (parmreg));
3302 int regnor = REGNO (XEXP (parmreg, 0));
3303 int regnoi = REGNO (XEXP (parmreg, 1));
3304 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3305 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3306 GET_MODE_SIZE (submode));
3308 /* Scan backwards for the set of the real and
3309 imaginary parts. */
3310 for (sinsn = linsn; sinsn != 0;
3311 sinsn = prev_nonnote_insn (sinsn))
3313 set = single_set (sinsn);
3314 if (set == 0)
3315 continue;
3317 if (SET_DEST (set) == regno_reg_rtx [regnoi])
3318 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3319 else if (SET_DEST (set) == regno_reg_rtx [regnor])
3320 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3323 else
3324 set_dst_reg_note (linsn, REG_EQUIV, equiv_stack_parm, parmreg);
3327 /* For pointer data type, suggest pointer register. */
3328 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3329 mark_reg_pointer (parmreg,
3330 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3333 /* A subroutine of assign_parms. Allocate stack space to hold the current
3334 parameter. Get it there. Perform all ABI specified conversions. */
3336 static void
3337 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3338 struct assign_parm_data_one *data)
3340 /* Value must be stored in the stack slot STACK_PARM during function
3341 execution. */
3342 bool to_conversion = false;
3344 assign_parm_remove_parallels (data);
3346 if (data->promoted_mode != data->nominal_mode)
3348 /* Conversion is required. */
3349 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3351 emit_move_insn (tempreg, validize_mem (copy_rtx (data->entry_parm)));
3353 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3354 to_conversion = true;
3356 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3357 TYPE_UNSIGNED (TREE_TYPE (parm)));
3359 if (data->stack_parm)
3361 int offset = subreg_lowpart_offset (data->nominal_mode,
3362 GET_MODE (data->stack_parm));
3363 /* ??? This may need a big-endian conversion on sparc64. */
3364 data->stack_parm
3365 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3366 if (offset && MEM_OFFSET_KNOWN_P (data->stack_parm))
3367 set_mem_offset (data->stack_parm,
3368 MEM_OFFSET (data->stack_parm) + offset);
3372 if (data->entry_parm != data->stack_parm)
3374 rtx src, dest;
3376 if (data->stack_parm == 0)
3378 int align = STACK_SLOT_ALIGNMENT (data->passed_type,
3379 GET_MODE (data->entry_parm),
3380 TYPE_ALIGN (data->passed_type));
3381 data->stack_parm
3382 = assign_stack_local (GET_MODE (data->entry_parm),
3383 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3384 align);
3385 set_mem_attributes (data->stack_parm, parm, 1);
3388 dest = validize_mem (copy_rtx (data->stack_parm));
3389 src = validize_mem (copy_rtx (data->entry_parm));
3391 if (MEM_P (src))
3393 /* Use a block move to handle potentially misaligned entry_parm. */
3394 if (!to_conversion)
3395 push_to_sequence2 (all->first_conversion_insn,
3396 all->last_conversion_insn);
3397 to_conversion = true;
3399 emit_block_move (dest, src,
3400 GEN_INT (int_size_in_bytes (data->passed_type)),
3401 BLOCK_OP_NORMAL);
3403 else
3404 emit_move_insn (dest, src);
3407 if (to_conversion)
3409 all->first_conversion_insn = get_insns ();
3410 all->last_conversion_insn = get_last_insn ();
3411 end_sequence ();
3414 set_parm_rtl (parm, data->stack_parm);
3417 /* A subroutine of assign_parms. If the ABI splits complex arguments, then
3418 undo the frobbing that we did in assign_parms_augmented_arg_list. */
3420 static void
3421 assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3422 vec<tree> fnargs)
3424 tree parm;
3425 tree orig_fnargs = all->orig_fnargs;
3426 unsigned i = 0;
3428 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3430 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3431 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3433 rtx tmp, real, imag;
3434 machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3436 real = DECL_RTL (fnargs[i]);
3437 imag = DECL_RTL (fnargs[i + 1]);
3438 if (inner != GET_MODE (real))
3440 real = gen_lowpart_SUBREG (inner, real);
3441 imag = gen_lowpart_SUBREG (inner, imag);
3444 if (TREE_ADDRESSABLE (parm))
3446 rtx rmem, imem;
3447 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3448 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3449 DECL_MODE (parm),
3450 TYPE_ALIGN (TREE_TYPE (parm)));
3452 /* split_complex_arg put the real and imag parts in
3453 pseudos. Move them to memory. */
3454 tmp = assign_stack_local (DECL_MODE (parm), size, align);
3455 set_mem_attributes (tmp, parm, 1);
3456 rmem = adjust_address_nv (tmp, inner, 0);
3457 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3458 push_to_sequence2 (all->first_conversion_insn,
3459 all->last_conversion_insn);
3460 emit_move_insn (rmem, real);
3461 emit_move_insn (imem, imag);
3462 all->first_conversion_insn = get_insns ();
3463 all->last_conversion_insn = get_last_insn ();
3464 end_sequence ();
3466 else
3467 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3468 set_parm_rtl (parm, tmp);
3470 real = DECL_INCOMING_RTL (fnargs[i]);
3471 imag = DECL_INCOMING_RTL (fnargs[i + 1]);
3472 if (inner != GET_MODE (real))
3474 real = gen_lowpart_SUBREG (inner, real);
3475 imag = gen_lowpart_SUBREG (inner, imag);
3477 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3478 set_decl_incoming_rtl (parm, tmp, false);
3479 i++;
3484 /* Load bounds of PARM from bounds table. */
3485 static void
3486 assign_parm_load_bounds (struct assign_parm_data_one *data,
3487 tree parm,
3488 rtx entry,
3489 unsigned bound_no)
3491 bitmap_iterator bi;
3492 unsigned i, offs = 0;
3493 int bnd_no = -1;
3494 rtx slot = NULL, ptr = NULL;
3496 if (parm)
3498 bitmap slots;
3499 bitmap_obstack_initialize (NULL);
3500 slots = BITMAP_ALLOC (NULL);
3501 chkp_find_bound_slots (TREE_TYPE (parm), slots);
3502 EXECUTE_IF_SET_IN_BITMAP (slots, 0, i, bi)
3504 if (bound_no)
3505 bound_no--;
3506 else
3508 bnd_no = i;
3509 break;
3512 BITMAP_FREE (slots);
3513 bitmap_obstack_release (NULL);
3516 /* We may have bounds not associated with any pointer. */
3517 if (bnd_no != -1)
3518 offs = bnd_no * POINTER_SIZE / BITS_PER_UNIT;
3520 /* Find associated pointer. */
3521 if (bnd_no == -1)
3523 /* If bounds are not associated with any bounds,
3524 then it is passed in a register or special slot. */
3525 gcc_assert (data->entry_parm);
3526 ptr = const0_rtx;
3528 else if (MEM_P (entry))
3529 slot = adjust_address (entry, Pmode, offs);
3530 else if (REG_P (entry))
3531 ptr = gen_rtx_REG (Pmode, REGNO (entry) + bnd_no);
3532 else if (GET_CODE (entry) == PARALLEL)
3533 ptr = chkp_get_value_with_offs (entry, GEN_INT (offs));
3534 else
3535 gcc_unreachable ();
3536 data->entry_parm = targetm.calls.load_bounds_for_arg (slot, ptr,
3537 data->entry_parm);
3540 /* Assign RTL expressions to the function's bounds parameters BNDARGS. */
3542 static void
3543 assign_bounds (vec<bounds_parm_data> &bndargs,
3544 struct assign_parm_data_all &all,
3545 bool assign_regs, bool assign_special,
3546 bool assign_bt)
3548 unsigned i, pass;
3549 bounds_parm_data *pbdata;
3551 if (!bndargs.exists ())
3552 return;
3554 /* We make few passes to store input bounds. Firstly handle bounds
3555 passed in registers. After that we load bounds passed in special
3556 slots. Finally we load bounds from Bounds Table. */
3557 for (pass = 0; pass < 3; pass++)
3558 FOR_EACH_VEC_ELT (bndargs, i, pbdata)
3560 /* Pass 0 => regs only. */
3561 if (pass == 0
3562 && (!assign_regs
3563 ||(!pbdata->parm_data.entry_parm
3564 || GET_CODE (pbdata->parm_data.entry_parm) != REG)))
3565 continue;
3566 /* Pass 1 => slots only. */
3567 else if (pass == 1
3568 && (!assign_special
3569 || (!pbdata->parm_data.entry_parm
3570 || GET_CODE (pbdata->parm_data.entry_parm) == REG)))
3571 continue;
3572 /* Pass 2 => BT only. */
3573 else if (pass == 2
3574 && (!assign_bt
3575 || pbdata->parm_data.entry_parm))
3576 continue;
3578 if (!pbdata->parm_data.entry_parm
3579 || GET_CODE (pbdata->parm_data.entry_parm) != REG)
3580 assign_parm_load_bounds (&pbdata->parm_data, pbdata->ptr_parm,
3581 pbdata->ptr_entry, pbdata->bound_no);
3583 set_decl_incoming_rtl (pbdata->bounds_parm,
3584 pbdata->parm_data.entry_parm, false);
3586 if (assign_parm_setup_block_p (&pbdata->parm_data))
3587 assign_parm_setup_block (&all, pbdata->bounds_parm,
3588 &pbdata->parm_data);
3589 else if (pbdata->parm_data.passed_pointer
3590 || use_register_for_decl (pbdata->bounds_parm))
3591 assign_parm_setup_reg (&all, pbdata->bounds_parm,
3592 &pbdata->parm_data);
3593 else
3594 assign_parm_setup_stack (&all, pbdata->bounds_parm,
3595 &pbdata->parm_data);
3599 /* Assign RTL expressions to the function's parameters. This may involve
3600 copying them into registers and using those registers as the DECL_RTL. */
3602 static void
3603 assign_parms (tree fndecl)
3605 struct assign_parm_data_all all;
3606 tree parm;
3607 vec<tree> fnargs;
3608 unsigned i, bound_no = 0;
3609 tree last_arg = NULL;
3610 rtx last_arg_entry = NULL;
3611 vec<bounds_parm_data> bndargs = vNULL;
3612 bounds_parm_data bdata;
3614 crtl->args.internal_arg_pointer
3615 = targetm.calls.internal_arg_pointer ();
3617 assign_parms_initialize_all (&all);
3618 fnargs = assign_parms_augmented_arg_list (&all);
3620 FOR_EACH_VEC_ELT (fnargs, i, parm)
3622 struct assign_parm_data_one data;
3624 /* Extract the type of PARM; adjust it according to ABI. */
3625 assign_parm_find_data_types (&all, parm, &data);
3627 /* Early out for errors and void parameters. */
3628 if (data.passed_mode == VOIDmode)
3630 SET_DECL_RTL (parm, const0_rtx);
3631 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3632 continue;
3635 /* Estimate stack alignment from parameter alignment. */
3636 if (SUPPORTS_STACK_ALIGNMENT)
3638 unsigned int align
3639 = targetm.calls.function_arg_boundary (data.promoted_mode,
3640 data.passed_type);
3641 align = MINIMUM_ALIGNMENT (data.passed_type, data.promoted_mode,
3642 align);
3643 if (TYPE_ALIGN (data.nominal_type) > align)
3644 align = MINIMUM_ALIGNMENT (data.nominal_type,
3645 TYPE_MODE (data.nominal_type),
3646 TYPE_ALIGN (data.nominal_type));
3647 if (crtl->stack_alignment_estimated < align)
3649 gcc_assert (!crtl->stack_realign_processed);
3650 crtl->stack_alignment_estimated = align;
3654 /* Find out where the parameter arrives in this function. */
3655 assign_parm_find_entry_rtl (&all, &data);
3657 /* Find out where stack space for this parameter might be. */
3658 if (assign_parm_is_stack_parm (&all, &data))
3660 assign_parm_find_stack_rtl (parm, &data);
3661 assign_parm_adjust_entry_rtl (&data);
3663 if (!POINTER_BOUNDS_TYPE_P (data.passed_type))
3665 /* Remember where last non bounds arg was passed in case
3666 we have to load associated bounds for it from Bounds
3667 Table. */
3668 last_arg = parm;
3669 last_arg_entry = data.entry_parm;
3670 bound_no = 0;
3672 /* Record permanently how this parm was passed. */
3673 if (data.passed_pointer)
3675 rtx incoming_rtl
3676 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.passed_type)),
3677 data.entry_parm);
3678 set_decl_incoming_rtl (parm, incoming_rtl, true);
3680 else
3681 set_decl_incoming_rtl (parm, data.entry_parm, false);
3683 assign_parm_adjust_stack_rtl (&data);
3685 /* Bounds should be loaded in the particular order to
3686 have registers allocated correctly. Collect info about
3687 input bounds and load them later. */
3688 if (POINTER_BOUNDS_TYPE_P (data.passed_type))
3690 /* Expect bounds in instrumented functions only. */
3691 gcc_assert (chkp_function_instrumented_p (fndecl));
3693 bdata.parm_data = data;
3694 bdata.bounds_parm = parm;
3695 bdata.ptr_parm = last_arg;
3696 bdata.ptr_entry = last_arg_entry;
3697 bdata.bound_no = bound_no;
3698 bndargs.safe_push (bdata);
3700 else
3702 if (assign_parm_setup_block_p (&data))
3703 assign_parm_setup_block (&all, parm, &data);
3704 else if (data.passed_pointer || use_register_for_decl (parm))
3705 assign_parm_setup_reg (&all, parm, &data);
3706 else
3707 assign_parm_setup_stack (&all, parm, &data);
3710 if (cfun->stdarg && !DECL_CHAIN (parm))
3712 int pretend_bytes = 0;
3714 assign_parms_setup_varargs (&all, &data, false);
3716 if (chkp_function_instrumented_p (fndecl))
3718 /* We expect this is the last parm. Otherwise it is wrong
3719 to assign bounds right now. */
3720 gcc_assert (i == (fnargs.length () - 1));
3721 assign_bounds (bndargs, all, true, false, false);
3722 targetm.calls.setup_incoming_vararg_bounds (all.args_so_far,
3723 data.promoted_mode,
3724 data.passed_type,
3725 &pretend_bytes,
3726 false);
3727 assign_bounds (bndargs, all, false, true, true);
3728 bndargs.release ();
3732 /* Update info on where next arg arrives in registers. */
3733 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3734 data.passed_type, data.named_arg);
3736 if (POINTER_BOUNDS_TYPE_P (data.passed_type))
3737 bound_no++;
3740 assign_bounds (bndargs, all, true, true, true);
3741 bndargs.release ();
3743 if (targetm.calls.split_complex_arg)
3744 assign_parms_unsplit_complex (&all, fnargs);
3746 fnargs.release ();
3748 /* Output all parameter conversion instructions (possibly including calls)
3749 now that all parameters have been copied out of hard registers. */
3750 emit_insn (all.first_conversion_insn);
3752 /* Estimate reload stack alignment from scalar return mode. */
3753 if (SUPPORTS_STACK_ALIGNMENT)
3755 if (DECL_RESULT (fndecl))
3757 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3758 machine_mode mode = TYPE_MODE (type);
3760 if (mode != BLKmode
3761 && mode != VOIDmode
3762 && !AGGREGATE_TYPE_P (type))
3764 unsigned int align = GET_MODE_ALIGNMENT (mode);
3765 if (crtl->stack_alignment_estimated < align)
3767 gcc_assert (!crtl->stack_realign_processed);
3768 crtl->stack_alignment_estimated = align;
3774 /* If we are receiving a struct value address as the first argument, set up
3775 the RTL for the function result. As this might require code to convert
3776 the transmitted address to Pmode, we do this here to ensure that possible
3777 preliminary conversions of the address have been emitted already. */
3778 if (all.function_result_decl)
3780 tree result = DECL_RESULT (current_function_decl);
3781 rtx addr = DECL_RTL (all.function_result_decl);
3782 rtx x;
3784 if (DECL_BY_REFERENCE (result))
3786 SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3787 x = addr;
3789 else
3791 SET_DECL_VALUE_EXPR (result,
3792 build1 (INDIRECT_REF, TREE_TYPE (result),
3793 all.function_result_decl));
3794 addr = convert_memory_address (Pmode, addr);
3795 x = gen_rtx_MEM (DECL_MODE (result), addr);
3796 set_mem_attributes (x, result, 1);
3799 DECL_HAS_VALUE_EXPR_P (result) = 1;
3801 set_parm_rtl (result, x);
3804 /* We have aligned all the args, so add space for the pretend args. */
3805 crtl->args.pretend_args_size = all.pretend_args_size;
3806 all.stack_args_size.constant += all.extra_pretend_bytes;
3807 crtl->args.size = all.stack_args_size.constant;
3809 /* Adjust function incoming argument size for alignment and
3810 minimum length. */
3812 crtl->args.size = MAX (crtl->args.size, all.reg_parm_stack_space);
3813 crtl->args.size = CEIL_ROUND (crtl->args.size,
3814 PARM_BOUNDARY / BITS_PER_UNIT);
3816 if (ARGS_GROW_DOWNWARD)
3818 crtl->args.arg_offset_rtx
3819 = (all.stack_args_size.var == 0 ? GEN_INT (-all.stack_args_size.constant)
3820 : expand_expr (size_diffop (all.stack_args_size.var,
3821 size_int (-all.stack_args_size.constant)),
3822 NULL_RTX, VOIDmode, EXPAND_NORMAL));
3824 else
3825 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3827 /* See how many bytes, if any, of its args a function should try to pop
3828 on return. */
3830 crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3831 TREE_TYPE (fndecl),
3832 crtl->args.size);
3834 /* For stdarg.h function, save info about
3835 regs and stack space used by the named args. */
3837 crtl->args.info = all.args_so_far_v;
3839 /* Set the rtx used for the function return value. Put this in its
3840 own variable so any optimizers that need this information don't have
3841 to include tree.h. Do this here so it gets done when an inlined
3842 function gets output. */
3844 crtl->return_rtx
3845 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3846 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3848 /* If scalar return value was computed in a pseudo-reg, or was a named
3849 return value that got dumped to the stack, copy that to the hard
3850 return register. */
3851 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3853 tree decl_result = DECL_RESULT (fndecl);
3854 rtx decl_rtl = DECL_RTL (decl_result);
3856 if (REG_P (decl_rtl)
3857 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3858 : DECL_REGISTER (decl_result))
3860 rtx real_decl_rtl;
3862 real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3863 fndecl, true);
3864 if (chkp_function_instrumented_p (fndecl))
3865 crtl->return_bnd
3866 = targetm.calls.chkp_function_value_bounds (TREE_TYPE (decl_result),
3867 fndecl, true);
3868 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3869 /* The delay slot scheduler assumes that crtl->return_rtx
3870 holds the hard register containing the return value, not a
3871 temporary pseudo. */
3872 crtl->return_rtx = real_decl_rtl;
3877 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3878 For all seen types, gimplify their sizes. */
3880 static tree
3881 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3883 tree t = *tp;
3885 *walk_subtrees = 0;
3886 if (TYPE_P (t))
3888 if (POINTER_TYPE_P (t))
3889 *walk_subtrees = 1;
3890 else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3891 && !TYPE_SIZES_GIMPLIFIED (t))
3893 gimplify_type_sizes (t, (gimple_seq *) data);
3894 *walk_subtrees = 1;
3898 return NULL;
3901 /* Gimplify the parameter list for current_function_decl. This involves
3902 evaluating SAVE_EXPRs of variable sized parameters and generating code
3903 to implement callee-copies reference parameters. Returns a sequence of
3904 statements to add to the beginning of the function. */
3906 gimple_seq
3907 gimplify_parameters (void)
3909 struct assign_parm_data_all all;
3910 tree parm;
3911 gimple_seq stmts = NULL;
3912 vec<tree> fnargs;
3913 unsigned i;
3915 assign_parms_initialize_all (&all);
3916 fnargs = assign_parms_augmented_arg_list (&all);
3918 FOR_EACH_VEC_ELT (fnargs, i, parm)
3920 struct assign_parm_data_one data;
3922 /* Extract the type of PARM; adjust it according to ABI. */
3923 assign_parm_find_data_types (&all, parm, &data);
3925 /* Early out for errors and void parameters. */
3926 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3927 continue;
3929 /* Update info on where next arg arrives in registers. */
3930 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3931 data.passed_type, data.named_arg);
3933 /* ??? Once upon a time variable_size stuffed parameter list
3934 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3935 turned out to be less than manageable in the gimple world.
3936 Now we have to hunt them down ourselves. */
3937 walk_tree_without_duplicates (&data.passed_type,
3938 gimplify_parm_type, &stmts);
3940 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3942 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3943 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3946 if (data.passed_pointer)
3948 tree type = TREE_TYPE (data.passed_type);
3949 if (reference_callee_copied (&all.args_so_far_v, TYPE_MODE (type),
3950 type, data.named_arg))
3952 tree local, t;
3954 /* For constant-sized objects, this is trivial; for
3955 variable-sized objects, we have to play games. */
3956 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3957 && !(flag_stack_check == GENERIC_STACK_CHECK
3958 && compare_tree_int (DECL_SIZE_UNIT (parm),
3959 STACK_CHECK_MAX_VAR_SIZE) > 0))
3961 local = create_tmp_var (type, get_name (parm));
3962 DECL_IGNORED_P (local) = 0;
3963 /* If PARM was addressable, move that flag over
3964 to the local copy, as its address will be taken,
3965 not the PARMs. Keep the parms address taken
3966 as we'll query that flag during gimplification. */
3967 if (TREE_ADDRESSABLE (parm))
3968 TREE_ADDRESSABLE (local) = 1;
3969 else if (TREE_CODE (type) == COMPLEX_TYPE
3970 || TREE_CODE (type) == VECTOR_TYPE)
3971 DECL_GIMPLE_REG_P (local) = 1;
3973 else
3975 tree ptr_type, addr;
3977 ptr_type = build_pointer_type (type);
3978 addr = create_tmp_reg (ptr_type, get_name (parm));
3979 DECL_IGNORED_P (addr) = 0;
3980 local = build_fold_indirect_ref (addr);
3982 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
3983 t = build_call_expr (t, 2, DECL_SIZE_UNIT (parm),
3984 size_int (DECL_ALIGN (parm)));
3986 /* The call has been built for a variable-sized object. */
3987 CALL_ALLOCA_FOR_VAR_P (t) = 1;
3988 t = fold_convert (ptr_type, t);
3989 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
3990 gimplify_and_add (t, &stmts);
3993 gimplify_assign (local, parm, &stmts);
3995 SET_DECL_VALUE_EXPR (parm, local);
3996 DECL_HAS_VALUE_EXPR_P (parm) = 1;
4001 fnargs.release ();
4003 return stmts;
4006 /* Compute the size and offset from the start of the stacked arguments for a
4007 parm passed in mode PASSED_MODE and with type TYPE.
4009 INITIAL_OFFSET_PTR points to the current offset into the stacked
4010 arguments.
4012 The starting offset and size for this parm are returned in
4013 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
4014 nonzero, the offset is that of stack slot, which is returned in
4015 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
4016 padding required from the initial offset ptr to the stack slot.
4018 IN_REGS is nonzero if the argument will be passed in registers. It will
4019 never be set if REG_PARM_STACK_SPACE is not defined.
4021 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
4022 for arguments which are passed in registers.
4024 FNDECL is the function in which the argument was defined.
4026 There are two types of rounding that are done. The first, controlled by
4027 TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
4028 argument list to be aligned to the specific boundary (in bits). This
4029 rounding affects the initial and starting offsets, but not the argument
4030 size.
4032 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
4033 optionally rounds the size of the parm to PARM_BOUNDARY. The
4034 initial offset is not affected by this rounding, while the size always
4035 is and the starting offset may be. */
4037 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
4038 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
4039 callers pass in the total size of args so far as
4040 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
4042 void
4043 locate_and_pad_parm (machine_mode passed_mode, tree type, int in_regs,
4044 int reg_parm_stack_space, int partial,
4045 tree fndecl ATTRIBUTE_UNUSED,
4046 struct args_size *initial_offset_ptr,
4047 struct locate_and_pad_arg_data *locate)
4049 tree sizetree;
4050 enum direction where_pad;
4051 unsigned int boundary, round_boundary;
4052 int part_size_in_regs;
4054 /* If we have found a stack parm before we reach the end of the
4055 area reserved for registers, skip that area. */
4056 if (! in_regs)
4058 if (reg_parm_stack_space > 0)
4060 if (initial_offset_ptr->var)
4062 initial_offset_ptr->var
4063 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
4064 ssize_int (reg_parm_stack_space));
4065 initial_offset_ptr->constant = 0;
4067 else if (initial_offset_ptr->constant < reg_parm_stack_space)
4068 initial_offset_ptr->constant = reg_parm_stack_space;
4072 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
4074 sizetree
4075 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
4076 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
4077 boundary = targetm.calls.function_arg_boundary (passed_mode, type);
4078 round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
4079 type);
4080 locate->where_pad = where_pad;
4082 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
4083 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4084 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4086 locate->boundary = boundary;
4088 if (SUPPORTS_STACK_ALIGNMENT)
4090 /* stack_alignment_estimated can't change after stack has been
4091 realigned. */
4092 if (crtl->stack_alignment_estimated < boundary)
4094 if (!crtl->stack_realign_processed)
4095 crtl->stack_alignment_estimated = boundary;
4096 else
4098 /* If stack is realigned and stack alignment value
4099 hasn't been finalized, it is OK not to increase
4100 stack_alignment_estimated. The bigger alignment
4101 requirement is recorded in stack_alignment_needed
4102 below. */
4103 gcc_assert (!crtl->stack_realign_finalized
4104 && crtl->stack_realign_needed);
4109 /* Remember if the outgoing parameter requires extra alignment on the
4110 calling function side. */
4111 if (crtl->stack_alignment_needed < boundary)
4112 crtl->stack_alignment_needed = boundary;
4113 if (crtl->preferred_stack_boundary < boundary)
4114 crtl->preferred_stack_boundary = boundary;
4116 if (ARGS_GROW_DOWNWARD)
4118 locate->slot_offset.constant = -initial_offset_ptr->constant;
4119 if (initial_offset_ptr->var)
4120 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
4121 initial_offset_ptr->var);
4124 tree s2 = sizetree;
4125 if (where_pad != none
4126 && (!tree_fits_uhwi_p (sizetree)
4127 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4128 s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
4129 SUB_PARM_SIZE (locate->slot_offset, s2);
4132 locate->slot_offset.constant += part_size_in_regs;
4134 if (!in_regs || reg_parm_stack_space > 0)
4135 pad_to_arg_alignment (&locate->slot_offset, boundary,
4136 &locate->alignment_pad);
4138 locate->size.constant = (-initial_offset_ptr->constant
4139 - locate->slot_offset.constant);
4140 if (initial_offset_ptr->var)
4141 locate->size.var = size_binop (MINUS_EXPR,
4142 size_binop (MINUS_EXPR,
4143 ssize_int (0),
4144 initial_offset_ptr->var),
4145 locate->slot_offset.var);
4147 /* Pad_below needs the pre-rounded size to know how much to pad
4148 below. */
4149 locate->offset = locate->slot_offset;
4150 if (where_pad == downward)
4151 pad_below (&locate->offset, passed_mode, sizetree);
4154 else
4156 if (!in_regs || reg_parm_stack_space > 0)
4157 pad_to_arg_alignment (initial_offset_ptr, boundary,
4158 &locate->alignment_pad);
4159 locate->slot_offset = *initial_offset_ptr;
4161 #ifdef PUSH_ROUNDING
4162 if (passed_mode != BLKmode)
4163 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
4164 #endif
4166 /* Pad_below needs the pre-rounded size to know how much to pad below
4167 so this must be done before rounding up. */
4168 locate->offset = locate->slot_offset;
4169 if (where_pad == downward)
4170 pad_below (&locate->offset, passed_mode, sizetree);
4172 if (where_pad != none
4173 && (!tree_fits_uhwi_p (sizetree)
4174 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4175 sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
4177 ADD_PARM_SIZE (locate->size, sizetree);
4179 locate->size.constant -= part_size_in_regs;
4182 #ifdef FUNCTION_ARG_OFFSET
4183 locate->offset.constant += FUNCTION_ARG_OFFSET (passed_mode, type);
4184 #endif
4187 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
4188 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
4190 static void
4191 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
4192 struct args_size *alignment_pad)
4194 tree save_var = NULL_TREE;
4195 HOST_WIDE_INT save_constant = 0;
4196 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4197 HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
4199 #ifdef SPARC_STACK_BOUNDARY_HACK
4200 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
4201 the real alignment of %sp. However, when it does this, the
4202 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
4203 if (SPARC_STACK_BOUNDARY_HACK)
4204 sp_offset = 0;
4205 #endif
4207 if (boundary > PARM_BOUNDARY)
4209 save_var = offset_ptr->var;
4210 save_constant = offset_ptr->constant;
4213 alignment_pad->var = NULL_TREE;
4214 alignment_pad->constant = 0;
4216 if (boundary > BITS_PER_UNIT)
4218 if (offset_ptr->var)
4220 tree sp_offset_tree = ssize_int (sp_offset);
4221 tree offset = size_binop (PLUS_EXPR,
4222 ARGS_SIZE_TREE (*offset_ptr),
4223 sp_offset_tree);
4224 tree rounded;
4225 if (ARGS_GROW_DOWNWARD)
4226 rounded = round_down (offset, boundary / BITS_PER_UNIT);
4227 else
4228 rounded = round_up (offset, boundary / BITS_PER_UNIT);
4230 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
4231 /* ARGS_SIZE_TREE includes constant term. */
4232 offset_ptr->constant = 0;
4233 if (boundary > PARM_BOUNDARY)
4234 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
4235 save_var);
4237 else
4239 offset_ptr->constant = -sp_offset +
4240 (ARGS_GROW_DOWNWARD
4241 ? FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes)
4242 : CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes));
4244 if (boundary > PARM_BOUNDARY)
4245 alignment_pad->constant = offset_ptr->constant - save_constant;
4250 static void
4251 pad_below (struct args_size *offset_ptr, machine_mode passed_mode, tree sizetree)
4253 if (passed_mode != BLKmode)
4255 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
4256 offset_ptr->constant
4257 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
4258 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
4259 - GET_MODE_SIZE (passed_mode));
4261 else
4263 if (TREE_CODE (sizetree) != INTEGER_CST
4264 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
4266 /* Round the size up to multiple of PARM_BOUNDARY bits. */
4267 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4268 /* Add it in. */
4269 ADD_PARM_SIZE (*offset_ptr, s2);
4270 SUB_PARM_SIZE (*offset_ptr, sizetree);
4276 /* True if register REGNO was alive at a place where `setjmp' was
4277 called and was set more than once or is an argument. Such regs may
4278 be clobbered by `longjmp'. */
4280 static bool
4281 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
4283 /* There appear to be cases where some local vars never reach the
4284 backend but have bogus regnos. */
4285 if (regno >= max_reg_num ())
4286 return false;
4288 return ((REG_N_SETS (regno) > 1
4289 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
4290 regno))
4291 && REGNO_REG_SET_P (setjmp_crosses, regno));
4294 /* Walk the tree of blocks describing the binding levels within a
4295 function and warn about variables the might be killed by setjmp or
4296 vfork. This is done after calling flow_analysis before register
4297 allocation since that will clobber the pseudo-regs to hard
4298 regs. */
4300 static void
4301 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
4303 tree decl, sub;
4305 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
4307 if (TREE_CODE (decl) == VAR_DECL
4308 && DECL_RTL_SET_P (decl)
4309 && REG_P (DECL_RTL (decl))
4310 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4311 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
4312 " %<longjmp%> or %<vfork%>", decl);
4315 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
4316 setjmp_vars_warning (setjmp_crosses, sub);
4319 /* Do the appropriate part of setjmp_vars_warning
4320 but for arguments instead of local variables. */
4322 static void
4323 setjmp_args_warning (bitmap setjmp_crosses)
4325 tree decl;
4326 for (decl = DECL_ARGUMENTS (current_function_decl);
4327 decl; decl = DECL_CHAIN (decl))
4328 if (DECL_RTL (decl) != 0
4329 && REG_P (DECL_RTL (decl))
4330 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4331 warning (OPT_Wclobbered,
4332 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
4333 decl);
4336 /* Generate warning messages for variables live across setjmp. */
4338 void
4339 generate_setjmp_warnings (void)
4341 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4343 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS
4344 || bitmap_empty_p (setjmp_crosses))
4345 return;
4347 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4348 setjmp_args_warning (setjmp_crosses);
4352 /* Reverse the order of elements in the fragment chain T of blocks,
4353 and return the new head of the chain (old last element).
4354 In addition to that clear BLOCK_SAME_RANGE flags when needed
4355 and adjust BLOCK_SUPERCONTEXT from the super fragment to
4356 its super fragment origin. */
4358 static tree
4359 block_fragments_nreverse (tree t)
4361 tree prev = 0, block, next, prev_super = 0;
4362 tree super = BLOCK_SUPERCONTEXT (t);
4363 if (BLOCK_FRAGMENT_ORIGIN (super))
4364 super = BLOCK_FRAGMENT_ORIGIN (super);
4365 for (block = t; block; block = next)
4367 next = BLOCK_FRAGMENT_CHAIN (block);
4368 BLOCK_FRAGMENT_CHAIN (block) = prev;
4369 if ((prev && !BLOCK_SAME_RANGE (prev))
4370 || (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (block))
4371 != prev_super))
4372 BLOCK_SAME_RANGE (block) = 0;
4373 prev_super = BLOCK_SUPERCONTEXT (block);
4374 BLOCK_SUPERCONTEXT (block) = super;
4375 prev = block;
4377 t = BLOCK_FRAGMENT_ORIGIN (t);
4378 if (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (t))
4379 != prev_super)
4380 BLOCK_SAME_RANGE (t) = 0;
4381 BLOCK_SUPERCONTEXT (t) = super;
4382 return prev;
4385 /* Reverse the order of elements in the chain T of blocks,
4386 and return the new head of the chain (old last element).
4387 Also do the same on subblocks and reverse the order of elements
4388 in BLOCK_FRAGMENT_CHAIN as well. */
4390 static tree
4391 blocks_nreverse_all (tree t)
4393 tree prev = 0, block, next;
4394 for (block = t; block; block = next)
4396 next = BLOCK_CHAIN (block);
4397 BLOCK_CHAIN (block) = prev;
4398 if (BLOCK_FRAGMENT_CHAIN (block)
4399 && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4401 BLOCK_FRAGMENT_CHAIN (block)
4402 = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4403 if (!BLOCK_SAME_RANGE (BLOCK_FRAGMENT_CHAIN (block)))
4404 BLOCK_SAME_RANGE (block) = 0;
4406 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4407 prev = block;
4409 return prev;
4413 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4414 and create duplicate blocks. */
4415 /* ??? Need an option to either create block fragments or to create
4416 abstract origin duplicates of a source block. It really depends
4417 on what optimization has been performed. */
4419 void
4420 reorder_blocks (void)
4422 tree block = DECL_INITIAL (current_function_decl);
4424 if (block == NULL_TREE)
4425 return;
4427 auto_vec<tree, 10> block_stack;
4429 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4430 clear_block_marks (block);
4432 /* Prune the old trees away, so that they don't get in the way. */
4433 BLOCK_SUBBLOCKS (block) = NULL_TREE;
4434 BLOCK_CHAIN (block) = NULL_TREE;
4436 /* Recreate the block tree from the note nesting. */
4437 reorder_blocks_1 (get_insns (), block, &block_stack);
4438 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4441 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
4443 void
4444 clear_block_marks (tree block)
4446 while (block)
4448 TREE_ASM_WRITTEN (block) = 0;
4449 clear_block_marks (BLOCK_SUBBLOCKS (block));
4450 block = BLOCK_CHAIN (block);
4454 static void
4455 reorder_blocks_1 (rtx_insn *insns, tree current_block,
4456 vec<tree> *p_block_stack)
4458 rtx_insn *insn;
4459 tree prev_beg = NULL_TREE, prev_end = NULL_TREE;
4461 for (insn = insns; insn; insn = NEXT_INSN (insn))
4463 if (NOTE_P (insn))
4465 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4467 tree block = NOTE_BLOCK (insn);
4468 tree origin;
4470 gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4471 origin = block;
4473 if (prev_end)
4474 BLOCK_SAME_RANGE (prev_end) = 0;
4475 prev_end = NULL_TREE;
4477 /* If we have seen this block before, that means it now
4478 spans multiple address regions. Create a new fragment. */
4479 if (TREE_ASM_WRITTEN (block))
4481 tree new_block = copy_node (block);
4483 BLOCK_SAME_RANGE (new_block) = 0;
4484 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4485 BLOCK_FRAGMENT_CHAIN (new_block)
4486 = BLOCK_FRAGMENT_CHAIN (origin);
4487 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4489 NOTE_BLOCK (insn) = new_block;
4490 block = new_block;
4493 if (prev_beg == current_block && prev_beg)
4494 BLOCK_SAME_RANGE (block) = 1;
4496 prev_beg = origin;
4498 BLOCK_SUBBLOCKS (block) = 0;
4499 TREE_ASM_WRITTEN (block) = 1;
4500 /* When there's only one block for the entire function,
4501 current_block == block and we mustn't do this, it
4502 will cause infinite recursion. */
4503 if (block != current_block)
4505 tree super;
4506 if (block != origin)
4507 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block
4508 || BLOCK_FRAGMENT_ORIGIN (BLOCK_SUPERCONTEXT
4509 (origin))
4510 == current_block);
4511 if (p_block_stack->is_empty ())
4512 super = current_block;
4513 else
4515 super = p_block_stack->last ();
4516 gcc_assert (super == current_block
4517 || BLOCK_FRAGMENT_ORIGIN (super)
4518 == current_block);
4520 BLOCK_SUPERCONTEXT (block) = super;
4521 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4522 BLOCK_SUBBLOCKS (current_block) = block;
4523 current_block = origin;
4525 p_block_stack->safe_push (block);
4527 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4529 NOTE_BLOCK (insn) = p_block_stack->pop ();
4530 current_block = BLOCK_SUPERCONTEXT (current_block);
4531 if (BLOCK_FRAGMENT_ORIGIN (current_block))
4532 current_block = BLOCK_FRAGMENT_ORIGIN (current_block);
4533 prev_beg = NULL_TREE;
4534 prev_end = BLOCK_SAME_RANGE (NOTE_BLOCK (insn))
4535 ? NOTE_BLOCK (insn) : NULL_TREE;
4538 else
4540 prev_beg = NULL_TREE;
4541 if (prev_end)
4542 BLOCK_SAME_RANGE (prev_end) = 0;
4543 prev_end = NULL_TREE;
4548 /* Reverse the order of elements in the chain T of blocks,
4549 and return the new head of the chain (old last element). */
4551 tree
4552 blocks_nreverse (tree t)
4554 tree prev = 0, block, next;
4555 for (block = t; block; block = next)
4557 next = BLOCK_CHAIN (block);
4558 BLOCK_CHAIN (block) = prev;
4559 prev = block;
4561 return prev;
4564 /* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4565 by modifying the last node in chain 1 to point to chain 2. */
4567 tree
4568 block_chainon (tree op1, tree op2)
4570 tree t1;
4572 if (!op1)
4573 return op2;
4574 if (!op2)
4575 return op1;
4577 for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4578 continue;
4579 BLOCK_CHAIN (t1) = op2;
4581 #ifdef ENABLE_TREE_CHECKING
4583 tree t2;
4584 for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4585 gcc_assert (t2 != t1);
4587 #endif
4589 return op1;
4592 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
4593 non-NULL, list them all into VECTOR, in a depth-first preorder
4594 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
4595 blocks. */
4597 static int
4598 all_blocks (tree block, tree *vector)
4600 int n_blocks = 0;
4602 while (block)
4604 TREE_ASM_WRITTEN (block) = 0;
4606 /* Record this block. */
4607 if (vector)
4608 vector[n_blocks] = block;
4610 ++n_blocks;
4612 /* Record the subblocks, and their subblocks... */
4613 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4614 vector ? vector + n_blocks : 0);
4615 block = BLOCK_CHAIN (block);
4618 return n_blocks;
4621 /* Return a vector containing all the blocks rooted at BLOCK. The
4622 number of elements in the vector is stored in N_BLOCKS_P. The
4623 vector is dynamically allocated; it is the caller's responsibility
4624 to call `free' on the pointer returned. */
4626 static tree *
4627 get_block_vector (tree block, int *n_blocks_p)
4629 tree *block_vector;
4631 *n_blocks_p = all_blocks (block, NULL);
4632 block_vector = XNEWVEC (tree, *n_blocks_p);
4633 all_blocks (block, block_vector);
4635 return block_vector;
4638 static GTY(()) int next_block_index = 2;
4640 /* Set BLOCK_NUMBER for all the blocks in FN. */
4642 void
4643 number_blocks (tree fn)
4645 int i;
4646 int n_blocks;
4647 tree *block_vector;
4649 /* For SDB and XCOFF debugging output, we start numbering the blocks
4650 from 1 within each function, rather than keeping a running
4651 count. */
4652 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
4653 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
4654 next_block_index = 1;
4655 #endif
4657 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4659 /* The top-level BLOCK isn't numbered at all. */
4660 for (i = 1; i < n_blocks; ++i)
4661 /* We number the blocks from two. */
4662 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4664 free (block_vector);
4666 return;
4669 /* If VAR is present in a subblock of BLOCK, return the subblock. */
4671 DEBUG_FUNCTION tree
4672 debug_find_var_in_block_tree (tree var, tree block)
4674 tree t;
4676 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4677 if (t == var)
4678 return block;
4680 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4682 tree ret = debug_find_var_in_block_tree (var, t);
4683 if (ret)
4684 return ret;
4687 return NULL_TREE;
4690 /* Keep track of whether we're in a dummy function context. If we are,
4691 we don't want to invoke the set_current_function hook, because we'll
4692 get into trouble if the hook calls target_reinit () recursively or
4693 when the initial initialization is not yet complete. */
4695 static bool in_dummy_function;
4697 /* Invoke the target hook when setting cfun. Update the optimization options
4698 if the function uses different options than the default. */
4700 static void
4701 invoke_set_current_function_hook (tree fndecl)
4703 if (!in_dummy_function)
4705 tree opts = ((fndecl)
4706 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4707 : optimization_default_node);
4709 if (!opts)
4710 opts = optimization_default_node;
4712 /* Change optimization options if needed. */
4713 if (optimization_current_node != opts)
4715 optimization_current_node = opts;
4716 cl_optimization_restore (&global_options, TREE_OPTIMIZATION (opts));
4719 targetm.set_current_function (fndecl);
4720 this_fn_optabs = this_target_optabs;
4722 if (opts != optimization_default_node)
4724 init_tree_optimization_optabs (opts);
4725 if (TREE_OPTIMIZATION_OPTABS (opts))
4726 this_fn_optabs = (struct target_optabs *)
4727 TREE_OPTIMIZATION_OPTABS (opts);
4732 /* cfun should never be set directly; use this function. */
4734 void
4735 set_cfun (struct function *new_cfun)
4737 if (cfun != new_cfun)
4739 cfun = new_cfun;
4740 invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4744 /* Initialized with NOGC, making this poisonous to the garbage collector. */
4746 static vec<function *> cfun_stack;
4748 /* Push the current cfun onto the stack, and set cfun to new_cfun. Also set
4749 current_function_decl accordingly. */
4751 void
4752 push_cfun (struct function *new_cfun)
4754 gcc_assert ((!cfun && !current_function_decl)
4755 || (cfun && current_function_decl == cfun->decl));
4756 cfun_stack.safe_push (cfun);
4757 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4758 set_cfun (new_cfun);
4761 /* Pop cfun from the stack. Also set current_function_decl accordingly. */
4763 void
4764 pop_cfun (void)
4766 struct function *new_cfun = cfun_stack.pop ();
4767 /* When in_dummy_function, we do have a cfun but current_function_decl is
4768 NULL. We also allow pushing NULL cfun and subsequently changing
4769 current_function_decl to something else and have both restored by
4770 pop_cfun. */
4771 gcc_checking_assert (in_dummy_function
4772 || !cfun
4773 || current_function_decl == cfun->decl);
4774 set_cfun (new_cfun);
4775 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4778 /* Return value of funcdef and increase it. */
4780 get_next_funcdef_no (void)
4782 return funcdef_no++;
4785 /* Return value of funcdef. */
4787 get_last_funcdef_no (void)
4789 return funcdef_no;
4792 /* Allocate a function structure for FNDECL and set its contents
4793 to the defaults. Set cfun to the newly-allocated object.
4794 Some of the helper functions invoked during initialization assume
4795 that cfun has already been set. Therefore, assign the new object
4796 directly into cfun and invoke the back end hook explicitly at the
4797 very end, rather than initializing a temporary and calling set_cfun
4798 on it.
4800 ABSTRACT_P is true if this is a function that will never be seen by
4801 the middle-end. Such functions are front-end concepts (like C++
4802 function templates) that do not correspond directly to functions
4803 placed in object files. */
4805 void
4806 allocate_struct_function (tree fndecl, bool abstract_p)
4808 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4810 cfun = ggc_cleared_alloc<function> ();
4812 init_eh_for_function ();
4814 if (init_machine_status)
4815 cfun->machine = (*init_machine_status) ();
4817 #ifdef OVERRIDE_ABI_FORMAT
4818 OVERRIDE_ABI_FORMAT (fndecl);
4819 #endif
4821 if (fndecl != NULL_TREE)
4823 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4824 cfun->decl = fndecl;
4825 current_function_funcdef_no = get_next_funcdef_no ();
4828 invoke_set_current_function_hook (fndecl);
4830 if (fndecl != NULL_TREE)
4832 tree result = DECL_RESULT (fndecl);
4834 if (!abstract_p)
4836 /* Now that we have activated any function-specific attributes
4837 that might affect layout, particularly vector modes, relayout
4838 each of the parameters and the result. */
4839 relayout_decl (result);
4840 for (tree parm = DECL_ARGUMENTS (fndecl); parm;
4841 parm = DECL_CHAIN (parm))
4842 relayout_decl (parm);
4845 if (!abstract_p && aggregate_value_p (result, fndecl))
4847 #ifdef PCC_STATIC_STRUCT_RETURN
4848 cfun->returns_pcc_struct = 1;
4849 #endif
4850 cfun->returns_struct = 1;
4853 cfun->stdarg = stdarg_p (fntype);
4855 /* Assume all registers in stdarg functions need to be saved. */
4856 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4857 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4859 /* ??? This could be set on a per-function basis by the front-end
4860 but is this worth the hassle? */
4861 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4862 cfun->can_delete_dead_exceptions = flag_delete_dead_exceptions;
4864 if (!profile_flag && !flag_instrument_function_entry_exit)
4865 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl) = 1;
4869 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4870 instead of just setting it. */
4872 void
4873 push_struct_function (tree fndecl)
4875 /* When in_dummy_function we might be in the middle of a pop_cfun and
4876 current_function_decl and cfun may not match. */
4877 gcc_assert (in_dummy_function
4878 || (!cfun && !current_function_decl)
4879 || (cfun && current_function_decl == cfun->decl));
4880 cfun_stack.safe_push (cfun);
4881 current_function_decl = fndecl;
4882 allocate_struct_function (fndecl, false);
4885 /* Reset crtl and other non-struct-function variables to defaults as
4886 appropriate for emitting rtl at the start of a function. */
4888 static void
4889 prepare_function_start (void)
4891 gcc_assert (!get_last_insn ());
4892 init_temp_slots ();
4893 init_emit ();
4894 init_varasm_status ();
4895 init_expr ();
4896 default_rtl_profile ();
4898 if (flag_stack_usage_info)
4900 cfun->su = ggc_cleared_alloc<stack_usage> ();
4901 cfun->su->static_stack_size = -1;
4904 cse_not_expected = ! optimize;
4906 /* Caller save not needed yet. */
4907 caller_save_needed = 0;
4909 /* We haven't done register allocation yet. */
4910 reg_renumber = 0;
4912 /* Indicate that we have not instantiated virtual registers yet. */
4913 virtuals_instantiated = 0;
4915 /* Indicate that we want CONCATs now. */
4916 generating_concat_p = 1;
4918 /* Indicate we have no need of a frame pointer yet. */
4919 frame_pointer_needed = 0;
4922 void
4923 push_dummy_function (bool with_decl)
4925 tree fn_decl, fn_type, fn_result_decl;
4927 gcc_assert (!in_dummy_function);
4928 in_dummy_function = true;
4930 if (with_decl)
4932 fn_type = build_function_type_list (void_type_node, NULL_TREE);
4933 fn_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
4934 fn_type);
4935 fn_result_decl = build_decl (UNKNOWN_LOCATION, RESULT_DECL,
4936 NULL_TREE, void_type_node);
4937 DECL_RESULT (fn_decl) = fn_result_decl;
4939 else
4940 fn_decl = NULL_TREE;
4942 push_struct_function (fn_decl);
4945 /* Initialize the rtl expansion mechanism so that we can do simple things
4946 like generate sequences. This is used to provide a context during global
4947 initialization of some passes. You must call expand_dummy_function_end
4948 to exit this context. */
4950 void
4951 init_dummy_function_start (void)
4953 push_dummy_function (false);
4954 prepare_function_start ();
4957 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4958 and initialize static variables for generating RTL for the statements
4959 of the function. */
4961 void
4962 init_function_start (tree subr)
4964 if (subr && DECL_STRUCT_FUNCTION (subr))
4965 set_cfun (DECL_STRUCT_FUNCTION (subr));
4966 else
4967 allocate_struct_function (subr, false);
4969 /* Initialize backend, if needed. */
4970 initialize_rtl ();
4972 prepare_function_start ();
4973 decide_function_section (subr);
4975 /* Warn if this value is an aggregate type,
4976 regardless of which calling convention we are using for it. */
4977 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4978 warning (OPT_Waggregate_return, "function returns an aggregate");
4981 /* Expand code to verify the stack_protect_guard. This is invoked at
4982 the end of a function to be protected. */
4984 void
4985 stack_protect_epilogue (void)
4987 tree guard_decl = targetm.stack_protect_guard ();
4988 rtx_code_label *label = gen_label_rtx ();
4989 rtx x, y, tmp;
4990 rtx_insn *seq;
4992 x = expand_normal (crtl->stack_protect_guard);
4993 y = expand_normal (guard_decl);
4995 /* Allow the target to compare Y with X without leaking either into
4996 a register. */
4997 if (targetm.have_stack_protect_test ()
4998 && ((seq = targetm.gen_stack_protect_test (x, y, label)) != NULL_RTX))
4999 emit_insn (seq);
5000 else
5001 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
5003 /* The noreturn predictor has been moved to the tree level. The rtl-level
5004 predictors estimate this branch about 20%, which isn't enough to get
5005 things moved out of line. Since this is the only extant case of adding
5006 a noreturn function at the rtl level, it doesn't seem worth doing ought
5007 except adding the prediction by hand. */
5008 tmp = get_last_insn ();
5009 if (JUMP_P (tmp))
5010 predict_insn_def (as_a <rtx_insn *> (tmp), PRED_NORETURN, TAKEN);
5012 expand_call (targetm.stack_protect_fail (), NULL_RTX, /*ignore=*/true);
5013 free_temp_slots ();
5014 emit_label (label);
5017 /* Start the RTL for a new function, and set variables used for
5018 emitting RTL.
5019 SUBR is the FUNCTION_DECL node.
5020 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
5021 the function's parameters, which must be run at any return statement. */
5023 void
5024 expand_function_start (tree subr)
5026 /* Make sure volatile mem refs aren't considered
5027 valid operands of arithmetic insns. */
5028 init_recog_no_volatile ();
5030 crtl->profile
5031 = (profile_flag
5032 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
5034 crtl->limit_stack
5035 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
5037 /* Make the label for return statements to jump to. Do not special
5038 case machines with special return instructions -- they will be
5039 handled later during jump, ifcvt, or epilogue creation. */
5040 return_label = gen_label_rtx ();
5042 /* Initialize rtx used to return the value. */
5043 /* Do this before assign_parms so that we copy the struct value address
5044 before any library calls that assign parms might generate. */
5046 /* Decide whether to return the value in memory or in a register. */
5047 tree res = DECL_RESULT (subr);
5048 if (aggregate_value_p (res, subr))
5050 /* Returning something that won't go in a register. */
5051 rtx value_address = 0;
5053 #ifdef PCC_STATIC_STRUCT_RETURN
5054 if (cfun->returns_pcc_struct)
5056 int size = int_size_in_bytes (TREE_TYPE (res));
5057 value_address = assemble_static_space (size);
5059 else
5060 #endif
5062 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
5063 /* Expect to be passed the address of a place to store the value.
5064 If it is passed as an argument, assign_parms will take care of
5065 it. */
5066 if (sv)
5068 value_address = gen_reg_rtx (Pmode);
5069 emit_move_insn (value_address, sv);
5072 if (value_address)
5074 rtx x = value_address;
5075 if (!DECL_BY_REFERENCE (res))
5077 x = gen_rtx_MEM (DECL_MODE (res), x);
5078 set_mem_attributes (x, res, 1);
5080 set_parm_rtl (res, x);
5083 else if (DECL_MODE (res) == VOIDmode)
5084 /* If return mode is void, this decl rtl should not be used. */
5085 set_parm_rtl (res, NULL_RTX);
5086 else
5088 /* Compute the return values into a pseudo reg, which we will copy
5089 into the true return register after the cleanups are done. */
5090 tree return_type = TREE_TYPE (res);
5091 /* If we may coalesce this result, make sure it has the expected
5092 mode. */
5093 if (flag_tree_coalesce_vars && is_gimple_reg (res))
5095 tree def = ssa_default_def (cfun, res);
5096 gcc_assert (def);
5097 machine_mode mode = promote_ssa_mode (def, NULL);
5098 set_parm_rtl (res, gen_reg_rtx (mode));
5100 else if (TYPE_MODE (return_type) != BLKmode
5101 && targetm.calls.return_in_msb (return_type))
5102 /* expand_function_end will insert the appropriate padding in
5103 this case. Use the return value's natural (unpadded) mode
5104 within the function proper. */
5105 set_parm_rtl (res, gen_reg_rtx (TYPE_MODE (return_type)));
5106 else
5108 /* In order to figure out what mode to use for the pseudo, we
5109 figure out what the mode of the eventual return register will
5110 actually be, and use that. */
5111 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
5113 /* Structures that are returned in registers are not
5114 aggregate_value_p, so we may see a PARALLEL or a REG. */
5115 if (REG_P (hard_reg))
5116 set_parm_rtl (res, gen_reg_rtx (GET_MODE (hard_reg)));
5117 else
5119 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
5120 set_parm_rtl (res, gen_group_rtx (hard_reg));
5124 /* Set DECL_REGISTER flag so that expand_function_end will copy the
5125 result to the real return register(s). */
5126 DECL_REGISTER (res) = 1;
5128 if (chkp_function_instrumented_p (current_function_decl))
5130 tree return_type = TREE_TYPE (res);
5131 rtx bounds = targetm.calls.chkp_function_value_bounds (return_type,
5132 subr, 1);
5133 SET_DECL_BOUNDS_RTL (res, bounds);
5137 /* Initialize rtx for parameters and local variables.
5138 In some cases this requires emitting insns. */
5139 assign_parms (subr);
5141 /* If function gets a static chain arg, store it. */
5142 if (cfun->static_chain_decl)
5144 tree parm = cfun->static_chain_decl;
5145 rtx local, chain;
5146 rtx_insn *insn;
5147 int unsignedp;
5149 local = gen_reg_rtx (promote_decl_mode (parm, &unsignedp));
5150 chain = targetm.calls.static_chain (current_function_decl, true);
5152 set_decl_incoming_rtl (parm, chain, false);
5153 set_parm_rtl (parm, local);
5154 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
5156 if (GET_MODE (local) != GET_MODE (chain))
5158 convert_move (local, chain, unsignedp);
5159 insn = get_last_insn ();
5161 else
5162 insn = emit_move_insn (local, chain);
5164 /* Mark the register as eliminable, similar to parameters. */
5165 if (MEM_P (chain)
5166 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
5167 set_dst_reg_note (insn, REG_EQUIV, chain, local);
5169 /* If we aren't optimizing, save the static chain onto the stack. */
5170 if (!optimize)
5172 tree saved_static_chain_decl
5173 = build_decl (DECL_SOURCE_LOCATION (parm), VAR_DECL,
5174 DECL_NAME (parm), TREE_TYPE (parm));
5175 rtx saved_static_chain_rtx
5176 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5177 SET_DECL_RTL (saved_static_chain_decl, saved_static_chain_rtx);
5178 emit_move_insn (saved_static_chain_rtx, chain);
5179 SET_DECL_VALUE_EXPR (parm, saved_static_chain_decl);
5180 DECL_HAS_VALUE_EXPR_P (parm) = 1;
5184 /* If the function receives a non-local goto, then store the
5185 bits we need to restore the frame pointer. */
5186 if (cfun->nonlocal_goto_save_area)
5188 tree t_save;
5189 rtx r_save;
5191 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
5192 gcc_assert (DECL_RTL_SET_P (var));
5194 t_save = build4 (ARRAY_REF,
5195 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
5196 cfun->nonlocal_goto_save_area,
5197 integer_zero_node, NULL_TREE, NULL_TREE);
5198 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
5199 gcc_assert (GET_MODE (r_save) == Pmode);
5201 emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
5202 update_nonlocal_goto_save_area ();
5205 /* The following was moved from init_function_start.
5206 The move is supposed to make sdb output more accurate. */
5207 /* Indicate the beginning of the function body,
5208 as opposed to parm setup. */
5209 emit_note (NOTE_INSN_FUNCTION_BEG);
5211 gcc_assert (NOTE_P (get_last_insn ()));
5213 parm_birth_insn = get_last_insn ();
5215 if (crtl->profile)
5217 #ifdef PROFILE_HOOK
5218 PROFILE_HOOK (current_function_funcdef_no);
5219 #endif
5222 /* If we are doing generic stack checking, the probe should go here. */
5223 if (flag_stack_check == GENERIC_STACK_CHECK)
5224 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
5227 void
5228 pop_dummy_function (void)
5230 pop_cfun ();
5231 in_dummy_function = false;
5234 /* Undo the effects of init_dummy_function_start. */
5235 void
5236 expand_dummy_function_end (void)
5238 gcc_assert (in_dummy_function);
5240 /* End any sequences that failed to be closed due to syntax errors. */
5241 while (in_sequence_p ())
5242 end_sequence ();
5244 /* Outside function body, can't compute type's actual size
5245 until next function's body starts. */
5247 free_after_parsing (cfun);
5248 free_after_compilation (cfun);
5249 pop_dummy_function ();
5252 /* Helper for diddle_return_value. */
5254 void
5255 diddle_return_value_1 (void (*doit) (rtx, void *), void *arg, rtx outgoing)
5257 if (! outgoing)
5258 return;
5260 if (REG_P (outgoing))
5261 (*doit) (outgoing, arg);
5262 else if (GET_CODE (outgoing) == PARALLEL)
5264 int i;
5266 for (i = 0; i < XVECLEN (outgoing, 0); i++)
5268 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
5270 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
5271 (*doit) (x, arg);
5276 /* Call DOIT for each hard register used as a return value from
5277 the current function. */
5279 void
5280 diddle_return_value (void (*doit) (rtx, void *), void *arg)
5282 diddle_return_value_1 (doit, arg, crtl->return_bnd);
5283 diddle_return_value_1 (doit, arg, crtl->return_rtx);
5286 static void
5287 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5289 emit_clobber (reg);
5292 void
5293 clobber_return_register (void)
5295 diddle_return_value (do_clobber_return_reg, NULL);
5297 /* In case we do use pseudo to return value, clobber it too. */
5298 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5300 tree decl_result = DECL_RESULT (current_function_decl);
5301 rtx decl_rtl = DECL_RTL (decl_result);
5302 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
5304 do_clobber_return_reg (decl_rtl, NULL);
5309 static void
5310 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5312 emit_use (reg);
5315 static void
5316 use_return_register (void)
5318 diddle_return_value (do_use_return_reg, NULL);
5321 /* Set the location of the insn chain starting at INSN to LOC. */
5323 static void
5324 set_insn_locations (rtx_insn *insn, int loc)
5326 while (insn != NULL)
5328 if (INSN_P (insn))
5329 INSN_LOCATION (insn) = loc;
5330 insn = NEXT_INSN (insn);
5334 /* Generate RTL for the end of the current function. */
5336 void
5337 expand_function_end (void)
5339 /* If arg_pointer_save_area was referenced only from a nested
5340 function, we will not have initialized it yet. Do that now. */
5341 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
5342 get_arg_pointer_save_area ();
5344 /* If we are doing generic stack checking and this function makes calls,
5345 do a stack probe at the start of the function to ensure we have enough
5346 space for another stack frame. */
5347 if (flag_stack_check == GENERIC_STACK_CHECK)
5349 rtx_insn *insn, *seq;
5351 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5352 if (CALL_P (insn))
5354 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
5355 start_sequence ();
5356 if (STACK_CHECK_MOVING_SP)
5357 anti_adjust_stack_and_probe (max_frame_size, true);
5358 else
5359 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
5360 seq = get_insns ();
5361 end_sequence ();
5362 set_insn_locations (seq, prologue_location);
5363 emit_insn_before (seq, stack_check_probe_note);
5364 break;
5368 /* End any sequences that failed to be closed due to syntax errors. */
5369 while (in_sequence_p ())
5370 end_sequence ();
5372 clear_pending_stack_adjust ();
5373 do_pending_stack_adjust ();
5375 /* Output a linenumber for the end of the function.
5376 SDB depends on this. */
5377 set_curr_insn_location (input_location);
5379 /* Before the return label (if any), clobber the return
5380 registers so that they are not propagated live to the rest of
5381 the function. This can only happen with functions that drop
5382 through; if there had been a return statement, there would
5383 have either been a return rtx, or a jump to the return label.
5385 We delay actual code generation after the current_function_value_rtx
5386 is computed. */
5387 rtx_insn *clobber_after = get_last_insn ();
5389 /* Output the label for the actual return from the function. */
5390 emit_label (return_label);
5392 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
5394 /* Let except.c know where it should emit the call to unregister
5395 the function context for sjlj exceptions. */
5396 if (flag_exceptions)
5397 sjlj_emit_function_exit_after (get_last_insn ());
5399 else
5401 /* We want to ensure that instructions that may trap are not
5402 moved into the epilogue by scheduling, because we don't
5403 always emit unwind information for the epilogue. */
5404 if (cfun->can_throw_non_call_exceptions)
5405 emit_insn (gen_blockage ());
5408 /* If this is an implementation of throw, do what's necessary to
5409 communicate between __builtin_eh_return and the epilogue. */
5410 expand_eh_return ();
5412 /* If scalar return value was computed in a pseudo-reg, or was a named
5413 return value that got dumped to the stack, copy that to the hard
5414 return register. */
5415 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5417 tree decl_result = DECL_RESULT (current_function_decl);
5418 rtx decl_rtl = DECL_RTL (decl_result);
5420 if (REG_P (decl_rtl)
5421 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5422 : DECL_REGISTER (decl_result))
5424 rtx real_decl_rtl = crtl->return_rtx;
5426 /* This should be set in assign_parms. */
5427 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5429 /* If this is a BLKmode structure being returned in registers,
5430 then use the mode computed in expand_return. Note that if
5431 decl_rtl is memory, then its mode may have been changed,
5432 but that crtl->return_rtx has not. */
5433 if (GET_MODE (real_decl_rtl) == BLKmode)
5434 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
5436 /* If a non-BLKmode return value should be padded at the least
5437 significant end of the register, shift it left by the appropriate
5438 amount. BLKmode results are handled using the group load/store
5439 machinery. */
5440 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5441 && REG_P (real_decl_rtl)
5442 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5444 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5445 REGNO (real_decl_rtl)),
5446 decl_rtl);
5447 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5449 else if (GET_CODE (real_decl_rtl) == PARALLEL)
5451 /* If expand_function_start has created a PARALLEL for decl_rtl,
5452 move the result to the real return registers. Otherwise, do
5453 a group load from decl_rtl for a named return. */
5454 if (GET_CODE (decl_rtl) == PARALLEL)
5455 emit_group_move (real_decl_rtl, decl_rtl);
5456 else
5457 emit_group_load (real_decl_rtl, decl_rtl,
5458 TREE_TYPE (decl_result),
5459 int_size_in_bytes (TREE_TYPE (decl_result)));
5461 /* In the case of complex integer modes smaller than a word, we'll
5462 need to generate some non-trivial bitfield insertions. Do that
5463 on a pseudo and not the hard register. */
5464 else if (GET_CODE (decl_rtl) == CONCAT
5465 && GET_MODE_CLASS (GET_MODE (decl_rtl)) == MODE_COMPLEX_INT
5466 && GET_MODE_BITSIZE (GET_MODE (decl_rtl)) <= BITS_PER_WORD)
5468 int old_generating_concat_p;
5469 rtx tmp;
5471 old_generating_concat_p = generating_concat_p;
5472 generating_concat_p = 0;
5473 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5474 generating_concat_p = old_generating_concat_p;
5476 emit_move_insn (tmp, decl_rtl);
5477 emit_move_insn (real_decl_rtl, tmp);
5479 /* If a named return value dumped decl_return to memory, then
5480 we may need to re-do the PROMOTE_MODE signed/unsigned
5481 extension. */
5482 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5484 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5485 promote_function_mode (TREE_TYPE (decl_result),
5486 GET_MODE (decl_rtl), &unsignedp,
5487 TREE_TYPE (current_function_decl), 1);
5489 convert_move (real_decl_rtl, decl_rtl, unsignedp);
5491 else
5492 emit_move_insn (real_decl_rtl, decl_rtl);
5496 /* If returning a structure, arrange to return the address of the value
5497 in a place where debuggers expect to find it.
5499 If returning a structure PCC style,
5500 the caller also depends on this value.
5501 And cfun->returns_pcc_struct is not necessarily set. */
5502 if ((cfun->returns_struct || cfun->returns_pcc_struct)
5503 && !targetm.calls.omit_struct_return_reg)
5505 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5506 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5507 rtx outgoing;
5509 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5510 type = TREE_TYPE (type);
5511 else
5512 value_address = XEXP (value_address, 0);
5514 outgoing = targetm.calls.function_value (build_pointer_type (type),
5515 current_function_decl, true);
5517 /* Mark this as a function return value so integrate will delete the
5518 assignment and USE below when inlining this function. */
5519 REG_FUNCTION_VALUE_P (outgoing) = 1;
5521 /* The address may be ptr_mode and OUTGOING may be Pmode. */
5522 value_address = convert_memory_address (GET_MODE (outgoing),
5523 value_address);
5525 emit_move_insn (outgoing, value_address);
5527 /* Show return register used to hold result (in this case the address
5528 of the result. */
5529 crtl->return_rtx = outgoing;
5532 /* Emit the actual code to clobber return register. Don't emit
5533 it if clobber_after is a barrier, then the previous basic block
5534 certainly doesn't fall thru into the exit block. */
5535 if (!BARRIER_P (clobber_after))
5537 start_sequence ();
5538 clobber_return_register ();
5539 rtx_insn *seq = get_insns ();
5540 end_sequence ();
5542 emit_insn_after (seq, clobber_after);
5545 /* Output the label for the naked return from the function. */
5546 if (naked_return_label)
5547 emit_label (naked_return_label);
5549 /* @@@ This is a kludge. We want to ensure that instructions that
5550 may trap are not moved into the epilogue by scheduling, because
5551 we don't always emit unwind information for the epilogue. */
5552 if (cfun->can_throw_non_call_exceptions
5553 && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5554 emit_insn (gen_blockage ());
5556 /* If stack protection is enabled for this function, check the guard. */
5557 if (crtl->stack_protect_guard)
5558 stack_protect_epilogue ();
5560 /* If we had calls to alloca, and this machine needs
5561 an accurate stack pointer to exit the function,
5562 insert some code to save and restore the stack pointer. */
5563 if (! EXIT_IGNORE_STACK
5564 && cfun->calls_alloca)
5566 rtx tem = 0;
5568 start_sequence ();
5569 emit_stack_save (SAVE_FUNCTION, &tem);
5570 rtx_insn *seq = get_insns ();
5571 end_sequence ();
5572 emit_insn_before (seq, parm_birth_insn);
5574 emit_stack_restore (SAVE_FUNCTION, tem);
5577 /* ??? This should no longer be necessary since stupid is no longer with
5578 us, but there are some parts of the compiler (eg reload_combine, and
5579 sh mach_dep_reorg) that still try and compute their own lifetime info
5580 instead of using the general framework. */
5581 use_return_register ();
5585 get_arg_pointer_save_area (void)
5587 rtx ret = arg_pointer_save_area;
5589 if (! ret)
5591 ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5592 arg_pointer_save_area = ret;
5595 if (! crtl->arg_pointer_save_area_init)
5597 /* Save the arg pointer at the beginning of the function. The
5598 generated stack slot may not be a valid memory address, so we
5599 have to check it and fix it if necessary. */
5600 start_sequence ();
5601 emit_move_insn (validize_mem (copy_rtx (ret)),
5602 crtl->args.internal_arg_pointer);
5603 rtx_insn *seq = get_insns ();
5604 end_sequence ();
5606 push_topmost_sequence ();
5607 emit_insn_after (seq, entry_of_function ());
5608 pop_topmost_sequence ();
5610 crtl->arg_pointer_save_area_init = true;
5613 return ret;
5616 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5617 for the first time. */
5619 static void
5620 record_insns (rtx_insn *insns, rtx end, hash_table<insn_cache_hasher> **hashp)
5622 rtx_insn *tmp;
5623 hash_table<insn_cache_hasher> *hash = *hashp;
5625 if (hash == NULL)
5626 *hashp = hash = hash_table<insn_cache_hasher>::create_ggc (17);
5628 for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5630 rtx *slot = hash->find_slot (tmp, INSERT);
5631 gcc_assert (*slot == NULL);
5632 *slot = tmp;
5636 /* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5637 basic block, splitting or peepholes. If INSN is a prologue or epilogue
5638 insn, then record COPY as well. */
5640 void
5641 maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5643 hash_table<insn_cache_hasher> *hash;
5644 rtx *slot;
5646 hash = epilogue_insn_hash;
5647 if (!hash || !hash->find (insn))
5649 hash = prologue_insn_hash;
5650 if (!hash || !hash->find (insn))
5651 return;
5654 slot = hash->find_slot (copy, INSERT);
5655 gcc_assert (*slot == NULL);
5656 *slot = copy;
5659 /* Determine if any INSNs in HASH are, or are part of, INSN. Because
5660 we can be running after reorg, SEQUENCE rtl is possible. */
5662 static bool
5663 contains (const_rtx insn, hash_table<insn_cache_hasher> *hash)
5665 if (hash == NULL)
5666 return false;
5668 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5670 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
5671 int i;
5672 for (i = seq->len () - 1; i >= 0; i--)
5673 if (hash->find (seq->element (i)))
5674 return true;
5675 return false;
5678 return hash->find (const_cast<rtx> (insn)) != NULL;
5682 prologue_epilogue_contains (const_rtx insn)
5684 if (contains (insn, prologue_insn_hash))
5685 return 1;
5686 if (contains (insn, epilogue_insn_hash))
5687 return 1;
5688 return 0;
5691 /* Insert use of return register before the end of BB. */
5693 static void
5694 emit_use_return_register_into_block (basic_block bb)
5696 start_sequence ();
5697 use_return_register ();
5698 rtx_insn *seq = get_insns ();
5699 end_sequence ();
5700 rtx_insn *insn = BB_END (bb);
5701 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
5702 insn = prev_cc0_setter (insn);
5704 emit_insn_before (seq, insn);
5708 /* Create a return pattern, either simple_return or return, depending on
5709 simple_p. */
5711 static rtx_insn *
5712 gen_return_pattern (bool simple_p)
5714 return (simple_p
5715 ? targetm.gen_simple_return ()
5716 : targetm.gen_return ());
5719 /* Insert an appropriate return pattern at the end of block BB. This
5720 also means updating block_for_insn appropriately. SIMPLE_P is
5721 the same as in gen_return_pattern and passed to it. */
5723 void
5724 emit_return_into_block (bool simple_p, basic_block bb)
5726 rtx_jump_insn *jump = emit_jump_insn_after (gen_return_pattern (simple_p),
5727 BB_END (bb));
5728 rtx pat = PATTERN (jump);
5729 if (GET_CODE (pat) == PARALLEL)
5730 pat = XVECEXP (pat, 0, 0);
5731 gcc_assert (ANY_RETURN_P (pat));
5732 JUMP_LABEL (jump) = pat;
5735 /* Set JUMP_LABEL for a return insn. */
5737 void
5738 set_return_jump_label (rtx_insn *returnjump)
5740 rtx pat = PATTERN (returnjump);
5741 if (GET_CODE (pat) == PARALLEL)
5742 pat = XVECEXP (pat, 0, 0);
5743 if (ANY_RETURN_P (pat))
5744 JUMP_LABEL (returnjump) = pat;
5745 else
5746 JUMP_LABEL (returnjump) = ret_rtx;
5749 /* Return true if there are any active insns between HEAD and TAIL. */
5750 bool
5751 active_insn_between (rtx_insn *head, rtx_insn *tail)
5753 while (tail)
5755 if (active_insn_p (tail))
5756 return true;
5757 if (tail == head)
5758 return false;
5759 tail = PREV_INSN (tail);
5761 return false;
5764 /* LAST_BB is a block that exits, and empty of active instructions.
5765 Examine its predecessors for jumps that can be converted to
5766 (conditional) returns. */
5767 vec<edge>
5768 convert_jumps_to_returns (basic_block last_bb, bool simple_p,
5769 vec<edge> unconverted ATTRIBUTE_UNUSED)
5771 int i;
5772 basic_block bb;
5773 edge_iterator ei;
5774 edge e;
5775 auto_vec<basic_block> src_bbs (EDGE_COUNT (last_bb->preds));
5777 FOR_EACH_EDGE (e, ei, last_bb->preds)
5778 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
5779 src_bbs.quick_push (e->src);
5781 rtx_insn *label = BB_HEAD (last_bb);
5783 FOR_EACH_VEC_ELT (src_bbs, i, bb)
5785 rtx_insn *jump = BB_END (bb);
5787 if (!JUMP_P (jump) || JUMP_LABEL (jump) != label)
5788 continue;
5790 e = find_edge (bb, last_bb);
5792 /* If we have an unconditional jump, we can replace that
5793 with a simple return instruction. */
5794 if (simplejump_p (jump))
5796 /* The use of the return register might be present in the exit
5797 fallthru block. Either:
5798 - removing the use is safe, and we should remove the use in
5799 the exit fallthru block, or
5800 - removing the use is not safe, and we should add it here.
5801 For now, we conservatively choose the latter. Either of the
5802 2 helps in crossjumping. */
5803 emit_use_return_register_into_block (bb);
5805 emit_return_into_block (simple_p, bb);
5806 delete_insn (jump);
5809 /* If we have a conditional jump branching to the last
5810 block, we can try to replace that with a conditional
5811 return instruction. */
5812 else if (condjump_p (jump))
5814 rtx dest;
5816 if (simple_p)
5817 dest = simple_return_rtx;
5818 else
5819 dest = ret_rtx;
5820 if (!redirect_jump (as_a <rtx_jump_insn *> (jump), dest, 0))
5822 if (targetm.have_simple_return () && simple_p)
5824 if (dump_file)
5825 fprintf (dump_file,
5826 "Failed to redirect bb %d branch.\n", bb->index);
5827 unconverted.safe_push (e);
5829 continue;
5832 /* See comment in simplejump_p case above. */
5833 emit_use_return_register_into_block (bb);
5835 /* If this block has only one successor, it both jumps
5836 and falls through to the fallthru block, so we can't
5837 delete the edge. */
5838 if (single_succ_p (bb))
5839 continue;
5841 else
5843 if (targetm.have_simple_return () && simple_p)
5845 if (dump_file)
5846 fprintf (dump_file,
5847 "Failed to redirect bb %d branch.\n", bb->index);
5848 unconverted.safe_push (e);
5850 continue;
5853 /* Fix up the CFG for the successful change we just made. */
5854 redirect_edge_succ (e, EXIT_BLOCK_PTR_FOR_FN (cfun));
5855 e->flags &= ~EDGE_CROSSING;
5857 src_bbs.release ();
5858 return unconverted;
5861 /* Emit a return insn for the exit fallthru block. */
5862 basic_block
5863 emit_return_for_exit (edge exit_fallthru_edge, bool simple_p)
5865 basic_block last_bb = exit_fallthru_edge->src;
5867 if (JUMP_P (BB_END (last_bb)))
5869 last_bb = split_edge (exit_fallthru_edge);
5870 exit_fallthru_edge = single_succ_edge (last_bb);
5872 emit_barrier_after (BB_END (last_bb));
5873 emit_return_into_block (simple_p, last_bb);
5874 exit_fallthru_edge->flags &= ~EDGE_FALLTHRU;
5875 return last_bb;
5879 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
5880 this into place with notes indicating where the prologue ends and where
5881 the epilogue begins. Update the basic block information when possible.
5883 Notes on epilogue placement:
5884 There are several kinds of edges to the exit block:
5885 * a single fallthru edge from LAST_BB
5886 * possibly, edges from blocks containing sibcalls
5887 * possibly, fake edges from infinite loops
5889 The epilogue is always emitted on the fallthru edge from the last basic
5890 block in the function, LAST_BB, into the exit block.
5892 If LAST_BB is empty except for a label, it is the target of every
5893 other basic block in the function that ends in a return. If a
5894 target has a return or simple_return pattern (possibly with
5895 conditional variants), these basic blocks can be changed so that a
5896 return insn is emitted into them, and their target is adjusted to
5897 the real exit block.
5899 Notes on shrink wrapping: We implement a fairly conservative
5900 version of shrink-wrapping rather than the textbook one. We only
5901 generate a single prologue and a single epilogue. This is
5902 sufficient to catch a number of interesting cases involving early
5903 exits.
5905 First, we identify the blocks that require the prologue to occur before
5906 them. These are the ones that modify a call-saved register, or reference
5907 any of the stack or frame pointer registers. To simplify things, we then
5908 mark everything reachable from these blocks as also requiring a prologue.
5909 This takes care of loops automatically, and avoids the need to examine
5910 whether MEMs reference the frame, since it is sufficient to check for
5911 occurrences of the stack or frame pointer.
5913 We then compute the set of blocks for which the need for a prologue
5914 is anticipatable (borrowing terminology from the shrink-wrapping
5915 description in Muchnick's book). These are the blocks which either
5916 require a prologue themselves, or those that have only successors
5917 where the prologue is anticipatable. The prologue needs to be
5918 inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
5919 is not. For the moment, we ensure that only one such edge exists.
5921 The epilogue is placed as described above, but we make a
5922 distinction between inserting return and simple_return patterns
5923 when modifying other blocks that end in a return. Blocks that end
5924 in a sibcall omit the sibcall_epilogue if the block is not in
5925 ANTIC. */
5927 void
5928 thread_prologue_and_epilogue_insns (void)
5930 bool inserted;
5931 vec<edge> unconverted_simple_returns = vNULL;
5932 bitmap_head bb_flags;
5933 rtx_insn *returnjump;
5934 rtx_insn *epilogue_end ATTRIBUTE_UNUSED;
5935 rtx_insn *prologue_seq ATTRIBUTE_UNUSED, *split_prologue_seq ATTRIBUTE_UNUSED;
5936 edge e, entry_edge, orig_entry_edge, exit_fallthru_edge;
5937 edge_iterator ei;
5939 df_analyze ();
5941 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5943 inserted = false;
5944 epilogue_end = NULL;
5945 returnjump = NULL;
5947 /* Can't deal with multiple successors of the entry block at the
5948 moment. Function should always have at least one entry
5949 point. */
5950 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5951 entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5952 orig_entry_edge = entry_edge;
5954 split_prologue_seq = NULL;
5955 if (flag_split_stack
5956 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
5957 == NULL))
5959 start_sequence ();
5960 emit_insn (targetm.gen_split_stack_prologue ());
5961 split_prologue_seq = get_insns ();
5962 end_sequence ();
5964 record_insns (split_prologue_seq, NULL, &prologue_insn_hash);
5965 set_insn_locations (split_prologue_seq, prologue_location);
5968 prologue_seq = NULL;
5969 if (targetm.have_prologue ())
5971 start_sequence ();
5972 rtx_insn *seq = targetm.gen_prologue ();
5973 emit_insn (seq);
5975 /* Insert an explicit USE for the frame pointer
5976 if the profiling is on and the frame pointer is required. */
5977 if (crtl->profile && frame_pointer_needed)
5978 emit_use (hard_frame_pointer_rtx);
5980 /* Retain a map of the prologue insns. */
5981 record_insns (seq, NULL, &prologue_insn_hash);
5982 emit_note (NOTE_INSN_PROLOGUE_END);
5984 /* Ensure that instructions are not moved into the prologue when
5985 profiling is on. The call to the profiling routine can be
5986 emitted within the live range of a call-clobbered register. */
5987 if (!targetm.profile_before_prologue () && crtl->profile)
5988 emit_insn (gen_blockage ());
5990 prologue_seq = get_insns ();
5991 end_sequence ();
5992 set_insn_locations (prologue_seq, prologue_location);
5995 bitmap_initialize (&bb_flags, &bitmap_default_obstack);
5997 /* Try to perform a kind of shrink-wrapping, making sure the
5998 prologue/epilogue is emitted only around those parts of the
5999 function that require it. */
6001 try_shrink_wrapping (&entry_edge, &bb_flags, prologue_seq);
6003 if (split_prologue_seq != NULL_RTX)
6005 insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
6006 inserted = true;
6008 if (prologue_seq != NULL_RTX)
6010 insert_insn_on_edge (prologue_seq, entry_edge);
6011 inserted = true;
6014 /* If the exit block has no non-fake predecessors, we don't need
6015 an epilogue. */
6016 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6017 if ((e->flags & EDGE_FAKE) == 0)
6018 break;
6019 if (e == NULL)
6020 goto epilogue_done;
6022 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
6024 exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
6026 if (targetm.have_simple_return () && entry_edge != orig_entry_edge)
6027 exit_fallthru_edge
6028 = get_unconverted_simple_return (exit_fallthru_edge, bb_flags,
6029 &unconverted_simple_returns,
6030 &returnjump);
6031 if (targetm.have_return ())
6033 if (exit_fallthru_edge == NULL)
6034 goto epilogue_done;
6036 if (optimize)
6038 basic_block last_bb = exit_fallthru_edge->src;
6040 if (LABEL_P (BB_HEAD (last_bb))
6041 && !active_insn_between (BB_HEAD (last_bb), BB_END (last_bb)))
6042 convert_jumps_to_returns (last_bb, false, vNULL);
6044 if (EDGE_COUNT (last_bb->preds) != 0
6045 && single_succ_p (last_bb))
6047 last_bb = emit_return_for_exit (exit_fallthru_edge, false);
6048 epilogue_end = returnjump = BB_END (last_bb);
6050 /* Emitting the return may add a basic block.
6051 Fix bb_flags for the added block. */
6052 if (targetm.have_simple_return ()
6053 && last_bb != exit_fallthru_edge->src)
6054 bitmap_set_bit (&bb_flags, last_bb->index);
6056 goto epilogue_done;
6061 /* A small fib -- epilogue is not yet completed, but we wish to re-use
6062 this marker for the splits of EH_RETURN patterns, and nothing else
6063 uses the flag in the meantime. */
6064 epilogue_completed = 1;
6066 /* Find non-fallthru edges that end with EH_RETURN instructions. On
6067 some targets, these get split to a special version of the epilogue
6068 code. In order to be able to properly annotate these with unwind
6069 info, try to split them now. If we get a valid split, drop an
6070 EPILOGUE_BEG note and mark the insns as epilogue insns. */
6071 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6073 rtx_insn *prev, *last, *trial;
6075 if (e->flags & EDGE_FALLTHRU)
6076 continue;
6077 last = BB_END (e->src);
6078 if (!eh_returnjump_p (last))
6079 continue;
6081 prev = PREV_INSN (last);
6082 trial = try_split (PATTERN (last), last, 1);
6083 if (trial == last)
6084 continue;
6086 record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
6087 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
6090 /* If nothing falls through into the exit block, we don't need an
6091 epilogue. */
6093 if (exit_fallthru_edge == NULL)
6094 goto epilogue_done;
6096 if (targetm.have_epilogue ())
6098 start_sequence ();
6099 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
6100 rtx_insn *seq = targetm.gen_epilogue ();
6101 if (seq)
6102 emit_jump_insn (seq);
6104 /* Retain a map of the epilogue insns. */
6105 record_insns (seq, NULL, &epilogue_insn_hash);
6106 set_insn_locations (seq, epilogue_location);
6108 seq = get_insns ();
6109 returnjump = get_last_insn ();
6110 end_sequence ();
6112 insert_insn_on_edge (seq, exit_fallthru_edge);
6113 inserted = true;
6115 if (JUMP_P (returnjump))
6116 set_return_jump_label (returnjump);
6118 else
6120 basic_block cur_bb;
6122 if (! next_active_insn (BB_END (exit_fallthru_edge->src)))
6123 goto epilogue_done;
6124 /* We have a fall-through edge to the exit block, the source is not
6125 at the end of the function, and there will be an assembler epilogue
6126 at the end of the function.
6127 We can't use force_nonfallthru here, because that would try to
6128 use return. Inserting a jump 'by hand' is extremely messy, so
6129 we take advantage of cfg_layout_finalize using
6130 fixup_fallthru_exit_predecessor. */
6131 cfg_layout_initialize (0);
6132 FOR_EACH_BB_FN (cur_bb, cfun)
6133 if (cur_bb->index >= NUM_FIXED_BLOCKS
6134 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
6135 cur_bb->aux = cur_bb->next_bb;
6136 cfg_layout_finalize ();
6139 epilogue_done:
6141 default_rtl_profile ();
6143 if (inserted)
6145 sbitmap blocks;
6147 commit_edge_insertions ();
6149 /* Look for basic blocks within the prologue insns. */
6150 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
6151 bitmap_clear (blocks);
6152 bitmap_set_bit (blocks, entry_edge->dest->index);
6153 bitmap_set_bit (blocks, orig_entry_edge->dest->index);
6154 find_many_sub_basic_blocks (blocks);
6155 sbitmap_free (blocks);
6157 /* The epilogue insns we inserted may cause the exit edge to no longer
6158 be fallthru. */
6159 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6161 if (((e->flags & EDGE_FALLTHRU) != 0)
6162 && returnjump_p (BB_END (e->src)))
6163 e->flags &= ~EDGE_FALLTHRU;
6167 if (targetm.have_simple_return ())
6168 convert_to_simple_return (entry_edge, orig_entry_edge, bb_flags,
6169 returnjump, unconverted_simple_returns);
6171 /* Emit sibling epilogues before any sibling call sites. */
6172 for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds); (e =
6173 ei_safe_edge (ei));
6176 basic_block bb = e->src;
6177 rtx_insn *insn = BB_END (bb);
6179 if (!CALL_P (insn)
6180 || ! SIBLING_CALL_P (insn)
6181 || (targetm.have_simple_return ()
6182 && entry_edge != orig_entry_edge
6183 && !bitmap_bit_p (&bb_flags, bb->index)))
6185 ei_next (&ei);
6186 continue;
6189 if (rtx_insn *ep_seq = targetm.gen_sibcall_epilogue ())
6191 start_sequence ();
6192 emit_note (NOTE_INSN_EPILOGUE_BEG);
6193 emit_insn (ep_seq);
6194 rtx_insn *seq = get_insns ();
6195 end_sequence ();
6197 /* Retain a map of the epilogue insns. Used in life analysis to
6198 avoid getting rid of sibcall epilogue insns. Do this before we
6199 actually emit the sequence. */
6200 record_insns (seq, NULL, &epilogue_insn_hash);
6201 set_insn_locations (seq, epilogue_location);
6203 emit_insn_before (seq, insn);
6205 ei_next (&ei);
6208 if (epilogue_end)
6210 rtx_insn *insn, *next;
6212 /* Similarly, move any line notes that appear after the epilogue.
6213 There is no need, however, to be quite so anal about the existence
6214 of such a note. Also possibly move
6215 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
6216 info generation. */
6217 for (insn = epilogue_end; insn; insn = next)
6219 next = NEXT_INSN (insn);
6220 if (NOTE_P (insn)
6221 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
6222 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
6226 bitmap_clear (&bb_flags);
6228 /* Threading the prologue and epilogue changes the artificial refs
6229 in the entry and exit blocks. */
6230 epilogue_completed = 1;
6231 df_update_entry_exit_and_calls ();
6234 /* Reposition the prologue-end and epilogue-begin notes after
6235 instruction scheduling. */
6237 void
6238 reposition_prologue_and_epilogue_notes (void)
6240 if (!targetm.have_prologue ()
6241 && !targetm.have_epilogue ()
6242 && !targetm.have_sibcall_epilogue ())
6243 return;
6245 /* Since the hash table is created on demand, the fact that it is
6246 non-null is a signal that it is non-empty. */
6247 if (prologue_insn_hash != NULL)
6249 size_t len = prologue_insn_hash->elements ();
6250 rtx_insn *insn, *last = NULL, *note = NULL;
6252 /* Scan from the beginning until we reach the last prologue insn. */
6253 /* ??? While we do have the CFG intact, there are two problems:
6254 (1) The prologue can contain loops (typically probing the stack),
6255 which means that the end of the prologue isn't in the first bb.
6256 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
6257 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6259 if (NOTE_P (insn))
6261 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
6262 note = insn;
6264 else if (contains (insn, prologue_insn_hash))
6266 last = insn;
6267 if (--len == 0)
6268 break;
6272 if (last)
6274 if (note == NULL)
6276 /* Scan forward looking for the PROLOGUE_END note. It should
6277 be right at the beginning of the block, possibly with other
6278 insn notes that got moved there. */
6279 for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
6281 if (NOTE_P (note)
6282 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
6283 break;
6287 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
6288 if (LABEL_P (last))
6289 last = NEXT_INSN (last);
6290 reorder_insns (note, note, last);
6294 if (epilogue_insn_hash != NULL)
6296 edge_iterator ei;
6297 edge e;
6299 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6301 rtx_insn *insn, *first = NULL, *note = NULL;
6302 basic_block bb = e->src;
6304 /* Scan from the beginning until we reach the first epilogue insn. */
6305 FOR_BB_INSNS (bb, insn)
6307 if (NOTE_P (insn))
6309 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
6311 note = insn;
6312 if (first != NULL)
6313 break;
6316 else if (first == NULL && contains (insn, epilogue_insn_hash))
6318 first = insn;
6319 if (note != NULL)
6320 break;
6324 if (note)
6326 /* If the function has a single basic block, and no real
6327 epilogue insns (e.g. sibcall with no cleanup), the
6328 epilogue note can get scheduled before the prologue
6329 note. If we have frame related prologue insns, having
6330 them scanned during the epilogue will result in a crash.
6331 In this case re-order the epilogue note to just before
6332 the last insn in the block. */
6333 if (first == NULL)
6334 first = BB_END (bb);
6336 if (PREV_INSN (first) != note)
6337 reorder_insns (note, note, PREV_INSN (first));
6343 /* Returns the name of function declared by FNDECL. */
6344 const char *
6345 fndecl_name (tree fndecl)
6347 if (fndecl == NULL)
6348 return "(nofn)";
6349 return lang_hooks.decl_printable_name (fndecl, 2);
6352 /* Returns the name of function FN. */
6353 const char *
6354 function_name (struct function *fn)
6356 tree fndecl = (fn == NULL) ? NULL : fn->decl;
6357 return fndecl_name (fndecl);
6360 /* Returns the name of the current function. */
6361 const char *
6362 current_function_name (void)
6364 return function_name (cfun);
6368 static unsigned int
6369 rest_of_handle_check_leaf_regs (void)
6371 #ifdef LEAF_REGISTERS
6372 crtl->uses_only_leaf_regs
6373 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6374 #endif
6375 return 0;
6378 /* Insert a TYPE into the used types hash table of CFUN. */
6380 static void
6381 used_types_insert_helper (tree type, struct function *func)
6383 if (type != NULL && func != NULL)
6385 if (func->used_types_hash == NULL)
6386 func->used_types_hash = hash_set<tree>::create_ggc (37);
6388 func->used_types_hash->add (type);
6392 /* Given a type, insert it into the used hash table in cfun. */
6393 void
6394 used_types_insert (tree t)
6396 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6397 if (TYPE_NAME (t))
6398 break;
6399 else
6400 t = TREE_TYPE (t);
6401 if (TREE_CODE (t) == ERROR_MARK)
6402 return;
6403 if (TYPE_NAME (t) == NULL_TREE
6404 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6405 t = TYPE_MAIN_VARIANT (t);
6406 if (debug_info_level > DINFO_LEVEL_NONE)
6408 if (cfun)
6409 used_types_insert_helper (t, cfun);
6410 else
6412 /* So this might be a type referenced by a global variable.
6413 Record that type so that we can later decide to emit its
6414 debug information. */
6415 vec_safe_push (types_used_by_cur_var_decl, t);
6420 /* Helper to Hash a struct types_used_by_vars_entry. */
6422 static hashval_t
6423 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6425 gcc_assert (entry && entry->var_decl && entry->type);
6427 return iterative_hash_object (entry->type,
6428 iterative_hash_object (entry->var_decl, 0));
6431 /* Hash function of the types_used_by_vars_entry hash table. */
6433 hashval_t
6434 used_type_hasher::hash (types_used_by_vars_entry *entry)
6436 return hash_types_used_by_vars_entry (entry);
6439 /*Equality function of the types_used_by_vars_entry hash table. */
6441 bool
6442 used_type_hasher::equal (types_used_by_vars_entry *e1,
6443 types_used_by_vars_entry *e2)
6445 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6448 /* Inserts an entry into the types_used_by_vars_hash hash table. */
6450 void
6451 types_used_by_var_decl_insert (tree type, tree var_decl)
6453 if (type != NULL && var_decl != NULL)
6455 types_used_by_vars_entry **slot;
6456 struct types_used_by_vars_entry e;
6457 e.var_decl = var_decl;
6458 e.type = type;
6459 if (types_used_by_vars_hash == NULL)
6460 types_used_by_vars_hash
6461 = hash_table<used_type_hasher>::create_ggc (37);
6463 slot = types_used_by_vars_hash->find_slot (&e, INSERT);
6464 if (*slot == NULL)
6466 struct types_used_by_vars_entry *entry;
6467 entry = ggc_alloc<types_used_by_vars_entry> ();
6468 entry->type = type;
6469 entry->var_decl = var_decl;
6470 *slot = entry;
6475 namespace {
6477 const pass_data pass_data_leaf_regs =
6479 RTL_PASS, /* type */
6480 "*leaf_regs", /* name */
6481 OPTGROUP_NONE, /* optinfo_flags */
6482 TV_NONE, /* tv_id */
6483 0, /* properties_required */
6484 0, /* properties_provided */
6485 0, /* properties_destroyed */
6486 0, /* todo_flags_start */
6487 0, /* todo_flags_finish */
6490 class pass_leaf_regs : public rtl_opt_pass
6492 public:
6493 pass_leaf_regs (gcc::context *ctxt)
6494 : rtl_opt_pass (pass_data_leaf_regs, ctxt)
6497 /* opt_pass methods: */
6498 virtual unsigned int execute (function *)
6500 return rest_of_handle_check_leaf_regs ();
6503 }; // class pass_leaf_regs
6505 } // anon namespace
6507 rtl_opt_pass *
6508 make_pass_leaf_regs (gcc::context *ctxt)
6510 return new pass_leaf_regs (ctxt);
6513 static unsigned int
6514 rest_of_handle_thread_prologue_and_epilogue (void)
6516 if (optimize)
6517 cleanup_cfg (CLEANUP_EXPENSIVE);
6519 /* On some machines, the prologue and epilogue code, or parts thereof,
6520 can be represented as RTL. Doing so lets us schedule insns between
6521 it and the rest of the code and also allows delayed branch
6522 scheduling to operate in the epilogue. */
6523 thread_prologue_and_epilogue_insns ();
6525 /* Some non-cold blocks may now be only reachable from cold blocks.
6526 Fix that up. */
6527 fixup_partitions ();
6529 /* Shrink-wrapping can result in unreachable edges in the epilogue,
6530 see PR57320. */
6531 cleanup_cfg (0);
6533 /* The stack usage info is finalized during prologue expansion. */
6534 if (flag_stack_usage_info)
6535 output_stack_usage ();
6537 return 0;
6540 namespace {
6542 const pass_data pass_data_thread_prologue_and_epilogue =
6544 RTL_PASS, /* type */
6545 "pro_and_epilogue", /* name */
6546 OPTGROUP_NONE, /* optinfo_flags */
6547 TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
6548 0, /* properties_required */
6549 0, /* properties_provided */
6550 0, /* properties_destroyed */
6551 0, /* todo_flags_start */
6552 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
6555 class pass_thread_prologue_and_epilogue : public rtl_opt_pass
6557 public:
6558 pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6559 : rtl_opt_pass (pass_data_thread_prologue_and_epilogue, ctxt)
6562 /* opt_pass methods: */
6563 virtual unsigned int execute (function *)
6565 return rest_of_handle_thread_prologue_and_epilogue ();
6568 }; // class pass_thread_prologue_and_epilogue
6570 } // anon namespace
6572 rtl_opt_pass *
6573 make_pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6575 return new pass_thread_prologue_and_epilogue (ctxt);
6579 /* This mini-pass fixes fall-out from SSA in asm statements that have
6580 in-out constraints. Say you start with
6582 orig = inout;
6583 asm ("": "+mr" (inout));
6584 use (orig);
6586 which is transformed very early to use explicit output and match operands:
6588 orig = inout;
6589 asm ("": "=mr" (inout) : "0" (inout));
6590 use (orig);
6592 Or, after SSA and copyprop,
6594 asm ("": "=mr" (inout_2) : "0" (inout_1));
6595 use (inout_1);
6597 Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
6598 they represent two separate values, so they will get different pseudo
6599 registers during expansion. Then, since the two operands need to match
6600 per the constraints, but use different pseudo registers, reload can
6601 only register a reload for these operands. But reloads can only be
6602 satisfied by hardregs, not by memory, so we need a register for this
6603 reload, just because we are presented with non-matching operands.
6604 So, even though we allow memory for this operand, no memory can be
6605 used for it, just because the two operands don't match. This can
6606 cause reload failures on register-starved targets.
6608 So it's a symptom of reload not being able to use memory for reloads
6609 or, alternatively it's also a symptom of both operands not coming into
6610 reload as matching (in which case the pseudo could go to memory just
6611 fine, as the alternative allows it, and no reload would be necessary).
6612 We fix the latter problem here, by transforming
6614 asm ("": "=mr" (inout_2) : "0" (inout_1));
6616 back to
6618 inout_2 = inout_1;
6619 asm ("": "=mr" (inout_2) : "0" (inout_2)); */
6621 static void
6622 match_asm_constraints_1 (rtx_insn *insn, rtx *p_sets, int noutputs)
6624 int i;
6625 bool changed = false;
6626 rtx op = SET_SRC (p_sets[0]);
6627 int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
6628 rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
6629 bool *output_matched = XALLOCAVEC (bool, noutputs);
6631 memset (output_matched, 0, noutputs * sizeof (bool));
6632 for (i = 0; i < ninputs; i++)
6634 rtx input, output;
6635 rtx_insn *insns;
6636 const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
6637 char *end;
6638 int match, j;
6640 if (*constraint == '%')
6641 constraint++;
6643 match = strtoul (constraint, &end, 10);
6644 if (end == constraint)
6645 continue;
6647 gcc_assert (match < noutputs);
6648 output = SET_DEST (p_sets[match]);
6649 input = RTVEC_ELT (inputs, i);
6650 /* Only do the transformation for pseudos. */
6651 if (! REG_P (output)
6652 || rtx_equal_p (output, input)
6653 || (GET_MODE (input) != VOIDmode
6654 && GET_MODE (input) != GET_MODE (output)))
6655 continue;
6657 /* We can't do anything if the output is also used as input,
6658 as we're going to overwrite it. */
6659 for (j = 0; j < ninputs; j++)
6660 if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
6661 break;
6662 if (j != ninputs)
6663 continue;
6665 /* Avoid changing the same input several times. For
6666 asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
6667 only change in once (to out1), rather than changing it
6668 first to out1 and afterwards to out2. */
6669 if (i > 0)
6671 for (j = 0; j < noutputs; j++)
6672 if (output_matched[j] && input == SET_DEST (p_sets[j]))
6673 break;
6674 if (j != noutputs)
6675 continue;
6677 output_matched[match] = true;
6679 start_sequence ();
6680 emit_move_insn (output, input);
6681 insns = get_insns ();
6682 end_sequence ();
6683 emit_insn_before (insns, insn);
6685 /* Now replace all mentions of the input with output. We can't
6686 just replace the occurrence in inputs[i], as the register might
6687 also be used in some other input (or even in an address of an
6688 output), which would mean possibly increasing the number of
6689 inputs by one (namely 'output' in addition), which might pose
6690 a too complicated problem for reload to solve. E.g. this situation:
6692 asm ("" : "=r" (output), "=m" (input) : "0" (input))
6694 Here 'input' is used in two occurrences as input (once for the
6695 input operand, once for the address in the second output operand).
6696 If we would replace only the occurrence of the input operand (to
6697 make the matching) we would be left with this:
6699 output = input
6700 asm ("" : "=r" (output), "=m" (input) : "0" (output))
6702 Now we suddenly have two different input values (containing the same
6703 value, but different pseudos) where we formerly had only one.
6704 With more complicated asms this might lead to reload failures
6705 which wouldn't have happen without this pass. So, iterate over
6706 all operands and replace all occurrences of the register used. */
6707 for (j = 0; j < noutputs; j++)
6708 if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
6709 && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
6710 SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
6711 input, output);
6712 for (j = 0; j < ninputs; j++)
6713 if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
6714 RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
6715 input, output);
6717 changed = true;
6720 if (changed)
6721 df_insn_rescan (insn);
6724 /* Add the decl D to the local_decls list of FUN. */
6726 void
6727 add_local_decl (struct function *fun, tree d)
6729 gcc_assert (TREE_CODE (d) == VAR_DECL);
6730 vec_safe_push (fun->local_decls, d);
6733 namespace {
6735 const pass_data pass_data_match_asm_constraints =
6737 RTL_PASS, /* type */
6738 "asmcons", /* name */
6739 OPTGROUP_NONE, /* optinfo_flags */
6740 TV_NONE, /* tv_id */
6741 0, /* properties_required */
6742 0, /* properties_provided */
6743 0, /* properties_destroyed */
6744 0, /* todo_flags_start */
6745 0, /* todo_flags_finish */
6748 class pass_match_asm_constraints : public rtl_opt_pass
6750 public:
6751 pass_match_asm_constraints (gcc::context *ctxt)
6752 : rtl_opt_pass (pass_data_match_asm_constraints, ctxt)
6755 /* opt_pass methods: */
6756 virtual unsigned int execute (function *);
6758 }; // class pass_match_asm_constraints
6760 unsigned
6761 pass_match_asm_constraints::execute (function *fun)
6763 basic_block bb;
6764 rtx_insn *insn;
6765 rtx pat, *p_sets;
6766 int noutputs;
6768 if (!crtl->has_asm_statement)
6769 return 0;
6771 df_set_flags (DF_DEFER_INSN_RESCAN);
6772 FOR_EACH_BB_FN (bb, fun)
6774 FOR_BB_INSNS (bb, insn)
6776 if (!INSN_P (insn))
6777 continue;
6779 pat = PATTERN (insn);
6780 if (GET_CODE (pat) == PARALLEL)
6781 p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
6782 else if (GET_CODE (pat) == SET)
6783 p_sets = &PATTERN (insn), noutputs = 1;
6784 else
6785 continue;
6787 if (GET_CODE (*p_sets) == SET
6788 && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
6789 match_asm_constraints_1 (insn, p_sets, noutputs);
6793 return TODO_df_finish;
6796 } // anon namespace
6798 rtl_opt_pass *
6799 make_pass_match_asm_constraints (gcc::context *ctxt)
6801 return new pass_match_asm_constraints (ctxt);
6805 #include "gt-function.h"