* tree.h (TYPE_OVERFLOW_SANITIZED): Define.
[official-gcc.git] / gcc / function.c
blobef9809159096392d04c1896cd9a6972ad9ac9145
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987-2014 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 "tm.h"
38 #include "rtl-error.h"
39 #include "tree.h"
40 #include "stor-layout.h"
41 #include "varasm.h"
42 #include "stringpool.h"
43 #include "flags.h"
44 #include "except.h"
45 #include "hashtab.h"
46 #include "hash-set.h"
47 #include "vec.h"
48 #include "machmode.h"
49 #include "hard-reg-set.h"
50 #include "input.h"
51 #include "function.h"
52 #include "expr.h"
53 #include "insn-codes.h"
54 #include "optabs.h"
55 #include "libfuncs.h"
56 #include "regs.h"
57 #include "insn-config.h"
58 #include "recog.h"
59 #include "output.h"
60 #include "tm_p.h"
61 #include "langhooks.h"
62 #include "target.h"
63 #include "common/common-target.h"
64 #include "gimple-expr.h"
65 #include "gimplify.h"
66 #include "tree-pass.h"
67 #include "predict.h"
68 #include "dominance.h"
69 #include "cfg.h"
70 #include "cfgrtl.h"
71 #include "cfganal.h"
72 #include "cfgbuild.h"
73 #include "cfgcleanup.h"
74 #include "basic-block.h"
75 #include "df.h"
76 #include "params.h"
77 #include "bb-reorder.h"
78 #include "shrink-wrap.h"
79 #include "toplev.h"
80 #include "rtl-iter.h"
81 #include "tree-chkp.h"
82 #include "rtl-chkp.h"
84 /* So we can assign to cfun in this file. */
85 #undef cfun
87 #ifndef STACK_ALIGNMENT_NEEDED
88 #define STACK_ALIGNMENT_NEEDED 1
89 #endif
91 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
93 /* Round a value to the lowest integer less than it that is a multiple of
94 the required alignment. Avoid using division in case the value is
95 negative. Assume the alignment is a power of two. */
96 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
98 /* Similar, but round to the next highest integer that meets the
99 alignment. */
100 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
102 /* Nonzero once virtual register instantiation has been done.
103 assign_stack_local uses frame_pointer_rtx when this is nonzero.
104 calls.c:emit_library_call_value_1 uses it to set up
105 post-instantiation libcalls. */
106 int virtuals_instantiated;
108 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
109 static GTY(()) int funcdef_no;
111 /* These variables hold pointers to functions to create and destroy
112 target specific, per-function data structures. */
113 struct machine_function * (*init_machine_status) (void);
115 /* The currently compiled function. */
116 struct function *cfun = 0;
118 /* These hashes record the prologue and epilogue insns. */
119 static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
120 htab_t prologue_insn_hash;
121 static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
122 htab_t epilogue_insn_hash;
125 hash_table<used_type_hasher> *types_used_by_vars_hash = NULL;
126 vec<tree, va_gc> *types_used_by_cur_var_decl;
128 /* Forward declarations. */
130 static struct temp_slot *find_temp_slot_from_address (rtx);
131 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
132 static void pad_below (struct args_size *, machine_mode, tree);
133 static void reorder_blocks_1 (rtx_insn *, tree, vec<tree> *);
134 static int all_blocks (tree, tree *);
135 static tree *get_block_vector (tree, int *);
136 extern tree debug_find_var_in_block_tree (tree, tree);
137 /* We always define `record_insns' even if it's not used so that we
138 can always export `prologue_epilogue_contains'. */
139 static void record_insns (rtx_insn *, rtx, htab_t *) ATTRIBUTE_UNUSED;
140 static bool contains (const_rtx, htab_t);
141 static void prepare_function_start (void);
142 static void do_clobber_return_reg (rtx, void *);
143 static void do_use_return_reg (rtx, void *);
145 /* Stack of nested functions. */
146 /* Keep track of the cfun stack. */
148 typedef struct function *function_p;
150 static vec<function_p> function_context_stack;
152 /* Save the current context for compilation of a nested function.
153 This is called from language-specific code. */
155 void
156 push_function_context (void)
158 if (cfun == 0)
159 allocate_struct_function (NULL, false);
161 function_context_stack.safe_push (cfun);
162 set_cfun (NULL);
165 /* Restore the last saved context, at the end of a nested function.
166 This function is called from language-specific code. */
168 void
169 pop_function_context (void)
171 struct function *p = function_context_stack.pop ();
172 set_cfun (p);
173 current_function_decl = p->decl;
175 /* Reset variables that have known state during rtx generation. */
176 virtuals_instantiated = 0;
177 generating_concat_p = 1;
180 /* Clear out all parts of the state in F that can safely be discarded
181 after the function has been parsed, but not compiled, to let
182 garbage collection reclaim the memory. */
184 void
185 free_after_parsing (struct function *f)
187 f->language = 0;
190 /* Clear out all parts of the state in F that can safely be discarded
191 after the function has been compiled, to let garbage collection
192 reclaim the memory. */
194 void
195 free_after_compilation (struct function *f)
197 prologue_insn_hash = NULL;
198 epilogue_insn_hash = NULL;
200 free (crtl->emit.regno_pointer_align);
202 memset (crtl, 0, sizeof (struct rtl_data));
203 f->eh = NULL;
204 f->machine = NULL;
205 f->cfg = NULL;
207 regno_reg_rtx = NULL;
210 /* Return size needed for stack frame based on slots so far allocated.
211 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
212 the caller may have to do that. */
214 HOST_WIDE_INT
215 get_frame_size (void)
217 if (FRAME_GROWS_DOWNWARD)
218 return -frame_offset;
219 else
220 return frame_offset;
223 /* Issue an error message and return TRUE if frame OFFSET overflows in
224 the signed target pointer arithmetics for function FUNC. Otherwise
225 return FALSE. */
227 bool
228 frame_offset_overflow (HOST_WIDE_INT offset, tree func)
230 unsigned HOST_WIDE_INT size = FRAME_GROWS_DOWNWARD ? -offset : offset;
232 if (size > ((unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (Pmode) - 1))
233 /* Leave room for the fixed part of the frame. */
234 - 64 * UNITS_PER_WORD)
236 error_at (DECL_SOURCE_LOCATION (func),
237 "total size of local objects too large");
238 return TRUE;
241 return FALSE;
244 /* Return stack slot alignment in bits for TYPE and MODE. */
246 static unsigned int
247 get_stack_local_alignment (tree type, machine_mode mode)
249 unsigned int alignment;
251 if (mode == BLKmode)
252 alignment = BIGGEST_ALIGNMENT;
253 else
254 alignment = GET_MODE_ALIGNMENT (mode);
256 /* Allow the frond-end to (possibly) increase the alignment of this
257 stack slot. */
258 if (! type)
259 type = lang_hooks.types.type_for_mode (mode, 0);
261 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
264 /* Determine whether it is possible to fit a stack slot of size SIZE and
265 alignment ALIGNMENT into an area in the stack frame that starts at
266 frame offset START and has a length of LENGTH. If so, store the frame
267 offset to be used for the stack slot in *POFFSET and return true;
268 return false otherwise. This function will extend the frame size when
269 given a start/length pair that lies at the end of the frame. */
271 static bool
272 try_fit_stack_local (HOST_WIDE_INT start, HOST_WIDE_INT length,
273 HOST_WIDE_INT size, unsigned int alignment,
274 HOST_WIDE_INT *poffset)
276 HOST_WIDE_INT this_frame_offset;
277 int frame_off, frame_alignment, frame_phase;
279 /* Calculate how many bytes the start of local variables is off from
280 stack alignment. */
281 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
282 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
283 frame_phase = frame_off ? frame_alignment - frame_off : 0;
285 /* Round the frame offset to the specified alignment. */
287 /* We must be careful here, since FRAME_OFFSET might be negative and
288 division with a negative dividend isn't as well defined as we might
289 like. So we instead assume that ALIGNMENT is a power of two and
290 use logical operations which are unambiguous. */
291 if (FRAME_GROWS_DOWNWARD)
292 this_frame_offset
293 = (FLOOR_ROUND (start + length - size - frame_phase,
294 (unsigned HOST_WIDE_INT) alignment)
295 + frame_phase);
296 else
297 this_frame_offset
298 = (CEIL_ROUND (start - frame_phase,
299 (unsigned HOST_WIDE_INT) alignment)
300 + frame_phase);
302 /* See if it fits. If this space is at the edge of the frame,
303 consider extending the frame to make it fit. Our caller relies on
304 this when allocating a new slot. */
305 if (frame_offset == start && this_frame_offset < frame_offset)
306 frame_offset = this_frame_offset;
307 else if (this_frame_offset < start)
308 return false;
309 else if (start + length == frame_offset
310 && this_frame_offset + size > start + length)
311 frame_offset = this_frame_offset + size;
312 else if (this_frame_offset + size > start + length)
313 return false;
315 *poffset = this_frame_offset;
316 return true;
319 /* Create a new frame_space structure describing free space in the stack
320 frame beginning at START and ending at END, and chain it into the
321 function's frame_space_list. */
323 static void
324 add_frame_space (HOST_WIDE_INT start, HOST_WIDE_INT end)
326 struct frame_space *space = ggc_alloc<frame_space> ();
327 space->next = crtl->frame_space_list;
328 crtl->frame_space_list = space;
329 space->start = start;
330 space->length = end - start;
333 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
334 with machine mode MODE.
336 ALIGN controls the amount of alignment for the address of the slot:
337 0 means according to MODE,
338 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
339 -2 means use BITS_PER_UNIT,
340 positive specifies alignment boundary in bits.
342 KIND has ASLK_REDUCE_ALIGN bit set if it is OK to reduce
343 alignment and ASLK_RECORD_PAD bit set if we should remember
344 extra space we allocated for alignment purposes. When we are
345 called from assign_stack_temp_for_type, it is not set so we don't
346 track the same stack slot in two independent lists.
348 We do not round to stack_boundary here. */
351 assign_stack_local_1 (machine_mode mode, HOST_WIDE_INT size,
352 int align, int kind)
354 rtx x, addr;
355 int bigend_correction = 0;
356 HOST_WIDE_INT slot_offset = 0, old_frame_offset;
357 unsigned int alignment, alignment_in_bits;
359 if (align == 0)
361 alignment = get_stack_local_alignment (NULL, mode);
362 alignment /= BITS_PER_UNIT;
364 else if (align == -1)
366 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
367 size = CEIL_ROUND (size, alignment);
369 else if (align == -2)
370 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
371 else
372 alignment = align / BITS_PER_UNIT;
374 alignment_in_bits = alignment * BITS_PER_UNIT;
376 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
377 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
379 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
380 alignment = alignment_in_bits / BITS_PER_UNIT;
383 if (SUPPORTS_STACK_ALIGNMENT)
385 if (crtl->stack_alignment_estimated < alignment_in_bits)
387 if (!crtl->stack_realign_processed)
388 crtl->stack_alignment_estimated = alignment_in_bits;
389 else
391 /* If stack is realigned and stack alignment value
392 hasn't been finalized, it is OK not to increase
393 stack_alignment_estimated. The bigger alignment
394 requirement is recorded in stack_alignment_needed
395 below. */
396 gcc_assert (!crtl->stack_realign_finalized);
397 if (!crtl->stack_realign_needed)
399 /* It is OK to reduce the alignment as long as the
400 requested size is 0 or the estimated stack
401 alignment >= mode alignment. */
402 gcc_assert ((kind & ASLK_REDUCE_ALIGN)
403 || size == 0
404 || (crtl->stack_alignment_estimated
405 >= GET_MODE_ALIGNMENT (mode)));
406 alignment_in_bits = crtl->stack_alignment_estimated;
407 alignment = alignment_in_bits / BITS_PER_UNIT;
413 if (crtl->stack_alignment_needed < alignment_in_bits)
414 crtl->stack_alignment_needed = alignment_in_bits;
415 if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
416 crtl->max_used_stack_slot_alignment = alignment_in_bits;
418 if (mode != BLKmode || size != 0)
420 if (kind & ASLK_RECORD_PAD)
422 struct frame_space **psp;
424 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
426 struct frame_space *space = *psp;
427 if (!try_fit_stack_local (space->start, space->length, size,
428 alignment, &slot_offset))
429 continue;
430 *psp = space->next;
431 if (slot_offset > space->start)
432 add_frame_space (space->start, slot_offset);
433 if (slot_offset + size < space->start + space->length)
434 add_frame_space (slot_offset + size,
435 space->start + space->length);
436 goto found_space;
440 else if (!STACK_ALIGNMENT_NEEDED)
442 slot_offset = frame_offset;
443 goto found_space;
446 old_frame_offset = frame_offset;
448 if (FRAME_GROWS_DOWNWARD)
450 frame_offset -= size;
451 try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
453 if (kind & ASLK_RECORD_PAD)
455 if (slot_offset > frame_offset)
456 add_frame_space (frame_offset, slot_offset);
457 if (slot_offset + size < old_frame_offset)
458 add_frame_space (slot_offset + size, old_frame_offset);
461 else
463 frame_offset += size;
464 try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
466 if (kind & ASLK_RECORD_PAD)
468 if (slot_offset > old_frame_offset)
469 add_frame_space (old_frame_offset, slot_offset);
470 if (slot_offset + size < frame_offset)
471 add_frame_space (slot_offset + size, frame_offset);
475 found_space:
476 /* On a big-endian machine, if we are allocating more space than we will use,
477 use the least significant bytes of those that are allocated. */
478 if (BYTES_BIG_ENDIAN && mode != BLKmode && GET_MODE_SIZE (mode) < size)
479 bigend_correction = size - GET_MODE_SIZE (mode);
481 /* If we have already instantiated virtual registers, return the actual
482 address relative to the frame pointer. */
483 if (virtuals_instantiated)
484 addr = plus_constant (Pmode, frame_pointer_rtx,
485 trunc_int_for_mode
486 (slot_offset + bigend_correction
487 + STARTING_FRAME_OFFSET, Pmode));
488 else
489 addr = plus_constant (Pmode, virtual_stack_vars_rtx,
490 trunc_int_for_mode
491 (slot_offset + bigend_correction,
492 Pmode));
494 x = gen_rtx_MEM (mode, addr);
495 set_mem_align (x, alignment_in_bits);
496 MEM_NOTRAP_P (x) = 1;
498 stack_slot_list
499 = gen_rtx_EXPR_LIST (VOIDmode, x, stack_slot_list);
501 if (frame_offset_overflow (frame_offset, current_function_decl))
502 frame_offset = 0;
504 return x;
507 /* Wrap up assign_stack_local_1 with last parameter as false. */
510 assign_stack_local (machine_mode mode, HOST_WIDE_INT size, int align)
512 return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
515 /* In order to evaluate some expressions, such as function calls returning
516 structures in memory, we need to temporarily allocate stack locations.
517 We record each allocated temporary in the following structure.
519 Associated with each temporary slot is a nesting level. When we pop up
520 one level, all temporaries associated with the previous level are freed.
521 Normally, all temporaries are freed after the execution of the statement
522 in which they were created. However, if we are inside a ({...}) grouping,
523 the result may be in a temporary and hence must be preserved. If the
524 result could be in a temporary, we preserve it if we can determine which
525 one it is in. If we cannot determine which temporary may contain the
526 result, all temporaries are preserved. A temporary is preserved by
527 pretending it was allocated at the previous nesting level. */
529 struct GTY(()) temp_slot {
530 /* Points to next temporary slot. */
531 struct temp_slot *next;
532 /* Points to previous temporary slot. */
533 struct temp_slot *prev;
534 /* The rtx to used to reference the slot. */
535 rtx slot;
536 /* The size, in units, of the slot. */
537 HOST_WIDE_INT size;
538 /* The type of the object in the slot, or zero if it doesn't correspond
539 to a type. We use this to determine whether a slot can be reused.
540 It can be reused if objects of the type of the new slot will always
541 conflict with objects of the type of the old slot. */
542 tree type;
543 /* The alignment (in bits) of the slot. */
544 unsigned int align;
545 /* Nonzero if this temporary is currently in use. */
546 char in_use;
547 /* Nesting level at which this slot is being used. */
548 int level;
549 /* The offset of the slot from the frame_pointer, including extra space
550 for alignment. This info is for combine_temp_slots. */
551 HOST_WIDE_INT base_offset;
552 /* The size of the slot, including extra space for alignment. This
553 info is for combine_temp_slots. */
554 HOST_WIDE_INT full_size;
557 /* Entry for the below hash table. */
558 struct GTY((for_user)) temp_slot_address_entry {
559 hashval_t hash;
560 rtx address;
561 struct temp_slot *temp_slot;
564 struct temp_address_hasher : ggc_hasher<temp_slot_address_entry *>
566 static hashval_t hash (temp_slot_address_entry *);
567 static bool equal (temp_slot_address_entry *, temp_slot_address_entry *);
570 /* A table of addresses that represent a stack slot. The table is a mapping
571 from address RTXen to a temp slot. */
572 static GTY(()) hash_table<temp_address_hasher> *temp_slot_address_table;
573 static size_t n_temp_slots_in_use;
575 /* Removes temporary slot TEMP from LIST. */
577 static void
578 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
580 if (temp->next)
581 temp->next->prev = temp->prev;
582 if (temp->prev)
583 temp->prev->next = temp->next;
584 else
585 *list = temp->next;
587 temp->prev = temp->next = NULL;
590 /* Inserts temporary slot TEMP to LIST. */
592 static void
593 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
595 temp->next = *list;
596 if (*list)
597 (*list)->prev = temp;
598 temp->prev = NULL;
599 *list = temp;
602 /* Returns the list of used temp slots at LEVEL. */
604 static struct temp_slot **
605 temp_slots_at_level (int level)
607 if (level >= (int) vec_safe_length (used_temp_slots))
608 vec_safe_grow_cleared (used_temp_slots, level + 1);
610 return &(*used_temp_slots)[level];
613 /* Returns the maximal temporary slot level. */
615 static int
616 max_slot_level (void)
618 if (!used_temp_slots)
619 return -1;
621 return used_temp_slots->length () - 1;
624 /* Moves temporary slot TEMP to LEVEL. */
626 static void
627 move_slot_to_level (struct temp_slot *temp, int level)
629 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
630 insert_slot_to_list (temp, temp_slots_at_level (level));
631 temp->level = level;
634 /* Make temporary slot TEMP available. */
636 static void
637 make_slot_available (struct temp_slot *temp)
639 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
640 insert_slot_to_list (temp, &avail_temp_slots);
641 temp->in_use = 0;
642 temp->level = -1;
643 n_temp_slots_in_use--;
646 /* Compute the hash value for an address -> temp slot mapping.
647 The value is cached on the mapping entry. */
648 static hashval_t
649 temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
651 int do_not_record = 0;
652 return hash_rtx (t->address, GET_MODE (t->address),
653 &do_not_record, NULL, false);
656 /* Return the hash value for an address -> temp slot mapping. */
657 hashval_t
658 temp_address_hasher::hash (temp_slot_address_entry *t)
660 return t->hash;
663 /* Compare two address -> temp slot mapping entries. */
664 bool
665 temp_address_hasher::equal (temp_slot_address_entry *t1,
666 temp_slot_address_entry *t2)
668 return exp_equiv_p (t1->address, t2->address, 0, true);
671 /* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
672 static void
673 insert_temp_slot_address (rtx address, struct temp_slot *temp_slot)
675 struct temp_slot_address_entry *t = ggc_alloc<temp_slot_address_entry> ();
676 t->address = address;
677 t->temp_slot = temp_slot;
678 t->hash = temp_slot_address_compute_hash (t);
679 *temp_slot_address_table->find_slot_with_hash (t, t->hash, INSERT) = t;
682 /* Remove an address -> temp slot mapping entry if the temp slot is
683 not in use anymore. Callback for remove_unused_temp_slot_addresses. */
685 remove_unused_temp_slot_addresses_1 (temp_slot_address_entry **slot, void *)
687 const struct temp_slot_address_entry *t = *slot;
688 if (! t->temp_slot->in_use)
689 temp_slot_address_table->clear_slot (slot);
690 return 1;
693 /* Remove all mappings of addresses to unused temp slots. */
694 static void
695 remove_unused_temp_slot_addresses (void)
697 /* Use quicker clearing if there aren't any active temp slots. */
698 if (n_temp_slots_in_use)
699 temp_slot_address_table->traverse
700 <void *, remove_unused_temp_slot_addresses_1> (NULL);
701 else
702 temp_slot_address_table->empty ();
705 /* Find the temp slot corresponding to the object at address X. */
707 static struct temp_slot *
708 find_temp_slot_from_address (rtx x)
710 struct temp_slot *p;
711 struct temp_slot_address_entry tmp, *t;
713 /* First try the easy way:
714 See if X exists in the address -> temp slot mapping. */
715 tmp.address = x;
716 tmp.temp_slot = NULL;
717 tmp.hash = temp_slot_address_compute_hash (&tmp);
718 t = temp_slot_address_table->find_with_hash (&tmp, tmp.hash);
719 if (t)
720 return t->temp_slot;
722 /* If we have a sum involving a register, see if it points to a temp
723 slot. */
724 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
725 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
726 return p;
727 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
728 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
729 return p;
731 /* Last resort: Address is a virtual stack var address. */
732 if (GET_CODE (x) == PLUS
733 && XEXP (x, 0) == virtual_stack_vars_rtx
734 && CONST_INT_P (XEXP (x, 1)))
736 int i;
737 for (i = max_slot_level (); i >= 0; i--)
738 for (p = *temp_slots_at_level (i); p; p = p->next)
740 if (INTVAL (XEXP (x, 1)) >= p->base_offset
741 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size)
742 return p;
746 return NULL;
749 /* Allocate a temporary stack slot and record it for possible later
750 reuse.
752 MODE is the machine mode to be given to the returned rtx.
754 SIZE is the size in units of the space required. We do no rounding here
755 since assign_stack_local will do any required rounding.
757 TYPE is the type that will be used for the stack slot. */
760 assign_stack_temp_for_type (machine_mode mode, HOST_WIDE_INT size,
761 tree type)
763 unsigned int align;
764 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
765 rtx slot;
767 /* If SIZE is -1 it means that somebody tried to allocate a temporary
768 of a variable size. */
769 gcc_assert (size != -1);
771 align = get_stack_local_alignment (type, mode);
773 /* Try to find an available, already-allocated temporary of the proper
774 mode which meets the size and alignment requirements. Choose the
775 smallest one with the closest alignment.
777 If assign_stack_temp is called outside of the tree->rtl expansion,
778 we cannot reuse the stack slots (that may still refer to
779 VIRTUAL_STACK_VARS_REGNUM). */
780 if (!virtuals_instantiated)
782 for (p = avail_temp_slots; p; p = p->next)
784 if (p->align >= align && p->size >= size
785 && GET_MODE (p->slot) == mode
786 && objects_must_conflict_p (p->type, type)
787 && (best_p == 0 || best_p->size > p->size
788 || (best_p->size == p->size && best_p->align > p->align)))
790 if (p->align == align && p->size == size)
792 selected = p;
793 cut_slot_from_list (selected, &avail_temp_slots);
794 best_p = 0;
795 break;
797 best_p = p;
802 /* Make our best, if any, the one to use. */
803 if (best_p)
805 selected = best_p;
806 cut_slot_from_list (selected, &avail_temp_slots);
808 /* If there are enough aligned bytes left over, make them into a new
809 temp_slot so that the extra bytes don't get wasted. Do this only
810 for BLKmode slots, so that we can be sure of the alignment. */
811 if (GET_MODE (best_p->slot) == BLKmode)
813 int alignment = best_p->align / BITS_PER_UNIT;
814 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
816 if (best_p->size - rounded_size >= alignment)
818 p = ggc_alloc<temp_slot> ();
819 p->in_use = 0;
820 p->size = best_p->size - rounded_size;
821 p->base_offset = best_p->base_offset + rounded_size;
822 p->full_size = best_p->full_size - rounded_size;
823 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
824 p->align = best_p->align;
825 p->type = best_p->type;
826 insert_slot_to_list (p, &avail_temp_slots);
828 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
829 stack_slot_list);
831 best_p->size = rounded_size;
832 best_p->full_size = rounded_size;
837 /* If we still didn't find one, make a new temporary. */
838 if (selected == 0)
840 HOST_WIDE_INT frame_offset_old = frame_offset;
842 p = ggc_alloc<temp_slot> ();
844 /* We are passing an explicit alignment request to assign_stack_local.
845 One side effect of that is assign_stack_local will not round SIZE
846 to ensure the frame offset remains suitably aligned.
848 So for requests which depended on the rounding of SIZE, we go ahead
849 and round it now. We also make sure ALIGNMENT is at least
850 BIGGEST_ALIGNMENT. */
851 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
852 p->slot = assign_stack_local_1 (mode,
853 (mode == BLKmode
854 ? CEIL_ROUND (size,
855 (int) align
856 / BITS_PER_UNIT)
857 : size),
858 align, 0);
860 p->align = align;
862 /* The following slot size computation is necessary because we don't
863 know the actual size of the temporary slot until assign_stack_local
864 has performed all the frame alignment and size rounding for the
865 requested temporary. Note that extra space added for alignment
866 can be either above or below this stack slot depending on which
867 way the frame grows. We include the extra space if and only if it
868 is above this slot. */
869 if (FRAME_GROWS_DOWNWARD)
870 p->size = frame_offset_old - frame_offset;
871 else
872 p->size = size;
874 /* Now define the fields used by combine_temp_slots. */
875 if (FRAME_GROWS_DOWNWARD)
877 p->base_offset = frame_offset;
878 p->full_size = frame_offset_old - frame_offset;
880 else
882 p->base_offset = frame_offset_old;
883 p->full_size = frame_offset - frame_offset_old;
886 selected = p;
889 p = selected;
890 p->in_use = 1;
891 p->type = type;
892 p->level = temp_slot_level;
893 n_temp_slots_in_use++;
895 pp = temp_slots_at_level (p->level);
896 insert_slot_to_list (p, pp);
897 insert_temp_slot_address (XEXP (p->slot, 0), p);
899 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
900 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
901 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
903 /* If we know the alias set for the memory that will be used, use
904 it. If there's no TYPE, then we don't know anything about the
905 alias set for the memory. */
906 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
907 set_mem_align (slot, align);
909 /* If a type is specified, set the relevant flags. */
910 if (type != 0)
911 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
912 MEM_NOTRAP_P (slot) = 1;
914 return slot;
917 /* Allocate a temporary stack slot and record it for possible later
918 reuse. First two arguments are same as in preceding function. */
921 assign_stack_temp (machine_mode mode, HOST_WIDE_INT size)
923 return assign_stack_temp_for_type (mode, size, NULL_TREE);
926 /* Assign a temporary.
927 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
928 and so that should be used in error messages. In either case, we
929 allocate of the given type.
930 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
931 it is 0 if a register is OK.
932 DONT_PROMOTE is 1 if we should not promote values in register
933 to wider modes. */
936 assign_temp (tree type_or_decl, int memory_required,
937 int dont_promote ATTRIBUTE_UNUSED)
939 tree type, decl;
940 machine_mode mode;
941 #ifdef PROMOTE_MODE
942 int unsignedp;
943 #endif
945 if (DECL_P (type_or_decl))
946 decl = type_or_decl, type = TREE_TYPE (decl);
947 else
948 decl = NULL, type = type_or_decl;
950 mode = TYPE_MODE (type);
951 #ifdef PROMOTE_MODE
952 unsignedp = TYPE_UNSIGNED (type);
953 #endif
955 if (mode == BLKmode || memory_required)
957 HOST_WIDE_INT size = int_size_in_bytes (type);
958 rtx tmp;
960 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
961 problems with allocating the stack space. */
962 if (size == 0)
963 size = 1;
965 /* Unfortunately, we don't yet know how to allocate variable-sized
966 temporaries. However, sometimes we can find a fixed upper limit on
967 the size, so try that instead. */
968 else if (size == -1)
969 size = max_int_size_in_bytes (type);
971 /* The size of the temporary may be too large to fit into an integer. */
972 /* ??? Not sure this should happen except for user silliness, so limit
973 this to things that aren't compiler-generated temporaries. The
974 rest of the time we'll die in assign_stack_temp_for_type. */
975 if (decl && size == -1
976 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
978 error ("size of variable %q+D is too large", decl);
979 size = 1;
982 tmp = assign_stack_temp_for_type (mode, size, type);
983 return tmp;
986 #ifdef PROMOTE_MODE
987 if (! dont_promote)
988 mode = promote_mode (type, mode, &unsignedp);
989 #endif
991 return gen_reg_rtx (mode);
994 /* Combine temporary stack slots which are adjacent on the stack.
996 This allows for better use of already allocated stack space. This is only
997 done for BLKmode slots because we can be sure that we won't have alignment
998 problems in this case. */
1000 static void
1001 combine_temp_slots (void)
1003 struct temp_slot *p, *q, *next, *next_q;
1004 int num_slots;
1006 /* We can't combine slots, because the information about which slot
1007 is in which alias set will be lost. */
1008 if (flag_strict_aliasing)
1009 return;
1011 /* If there are a lot of temp slots, don't do anything unless
1012 high levels of optimization. */
1013 if (! flag_expensive_optimizations)
1014 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
1015 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1016 return;
1018 for (p = avail_temp_slots; p; p = next)
1020 int delete_p = 0;
1022 next = p->next;
1024 if (GET_MODE (p->slot) != BLKmode)
1025 continue;
1027 for (q = p->next; q; q = next_q)
1029 int delete_q = 0;
1031 next_q = q->next;
1033 if (GET_MODE (q->slot) != BLKmode)
1034 continue;
1036 if (p->base_offset + p->full_size == q->base_offset)
1038 /* Q comes after P; combine Q into P. */
1039 p->size += q->size;
1040 p->full_size += q->full_size;
1041 delete_q = 1;
1043 else if (q->base_offset + q->full_size == p->base_offset)
1045 /* P comes after Q; combine P into Q. */
1046 q->size += p->size;
1047 q->full_size += p->full_size;
1048 delete_p = 1;
1049 break;
1051 if (delete_q)
1052 cut_slot_from_list (q, &avail_temp_slots);
1055 /* Either delete P or advance past it. */
1056 if (delete_p)
1057 cut_slot_from_list (p, &avail_temp_slots);
1061 /* Indicate that NEW_RTX is an alternate way of referring to the temp
1062 slot that previously was known by OLD_RTX. */
1064 void
1065 update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1067 struct temp_slot *p;
1069 if (rtx_equal_p (old_rtx, new_rtx))
1070 return;
1072 p = find_temp_slot_from_address (old_rtx);
1074 /* If we didn't find one, see if both OLD_RTX is a PLUS. If so, and
1075 NEW_RTX is a register, see if one operand of the PLUS is a
1076 temporary location. If so, NEW_RTX points into it. Otherwise,
1077 if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1078 in common between them. If so, try a recursive call on those
1079 values. */
1080 if (p == 0)
1082 if (GET_CODE (old_rtx) != PLUS)
1083 return;
1085 if (REG_P (new_rtx))
1087 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1088 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1089 return;
1091 else if (GET_CODE (new_rtx) != PLUS)
1092 return;
1094 if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1095 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1096 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1097 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1098 else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1099 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1100 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1101 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
1103 return;
1106 /* Otherwise add an alias for the temp's address. */
1107 insert_temp_slot_address (new_rtx, p);
1110 /* If X could be a reference to a temporary slot, mark that slot as
1111 belonging to the to one level higher than the current level. If X
1112 matched one of our slots, just mark that one. Otherwise, we can't
1113 easily predict which it is, so upgrade all of them.
1115 This is called when an ({...}) construct occurs and a statement
1116 returns a value in memory. */
1118 void
1119 preserve_temp_slots (rtx x)
1121 struct temp_slot *p = 0, *next;
1123 if (x == 0)
1124 return;
1126 /* If X is a register that is being used as a pointer, see if we have
1127 a temporary slot we know it points to. */
1128 if (REG_P (x) && REG_POINTER (x))
1129 p = find_temp_slot_from_address (x);
1131 /* If X is not in memory or is at a constant address, it cannot be in
1132 a temporary slot. */
1133 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1134 return;
1136 /* First see if we can find a match. */
1137 if (p == 0)
1138 p = find_temp_slot_from_address (XEXP (x, 0));
1140 if (p != 0)
1142 if (p->level == temp_slot_level)
1143 move_slot_to_level (p, temp_slot_level - 1);
1144 return;
1147 /* Otherwise, preserve all non-kept slots at this level. */
1148 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1150 next = p->next;
1151 move_slot_to_level (p, temp_slot_level - 1);
1155 /* Free all temporaries used so far. This is normally called at the
1156 end of generating code for a statement. */
1158 void
1159 free_temp_slots (void)
1161 struct temp_slot *p, *next;
1162 bool some_available = false;
1164 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1166 next = p->next;
1167 make_slot_available (p);
1168 some_available = true;
1171 if (some_available)
1173 remove_unused_temp_slot_addresses ();
1174 combine_temp_slots ();
1178 /* Push deeper into the nesting level for stack temporaries. */
1180 void
1181 push_temp_slots (void)
1183 temp_slot_level++;
1186 /* Pop a temporary nesting level. All slots in use in the current level
1187 are freed. */
1189 void
1190 pop_temp_slots (void)
1192 free_temp_slots ();
1193 temp_slot_level--;
1196 /* Initialize temporary slots. */
1198 void
1199 init_temp_slots (void)
1201 /* We have not allocated any temporaries yet. */
1202 avail_temp_slots = 0;
1203 vec_alloc (used_temp_slots, 0);
1204 temp_slot_level = 0;
1205 n_temp_slots_in_use = 0;
1207 /* Set up the table to map addresses to temp slots. */
1208 if (! temp_slot_address_table)
1209 temp_slot_address_table = hash_table<temp_address_hasher>::create_ggc (32);
1210 else
1211 temp_slot_address_table->empty ();
1214 /* Functions and data structures to keep track of the values hard regs
1215 had at the start of the function. */
1217 /* Private type used by get_hard_reg_initial_reg, get_hard_reg_initial_val,
1218 and has_hard_reg_initial_val.. */
1219 typedef struct GTY(()) initial_value_pair {
1220 rtx hard_reg;
1221 rtx pseudo;
1222 } initial_value_pair;
1223 /* ??? This could be a VEC but there is currently no way to define an
1224 opaque VEC type. This could be worked around by defining struct
1225 initial_value_pair in function.h. */
1226 typedef struct GTY(()) initial_value_struct {
1227 int num_entries;
1228 int max_entries;
1229 initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
1230 } initial_value_struct;
1232 /* If a pseudo represents an initial hard reg (or expression), return
1233 it, else return NULL_RTX. */
1236 get_hard_reg_initial_reg (rtx reg)
1238 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1239 int i;
1241 if (ivs == 0)
1242 return NULL_RTX;
1244 for (i = 0; i < ivs->num_entries; i++)
1245 if (rtx_equal_p (ivs->entries[i].pseudo, reg))
1246 return ivs->entries[i].hard_reg;
1248 return NULL_RTX;
1251 /* Make sure that there's a pseudo register of mode MODE that stores the
1252 initial value of hard register REGNO. Return an rtx for such a pseudo. */
1255 get_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1257 struct initial_value_struct *ivs;
1258 rtx rv;
1260 rv = has_hard_reg_initial_val (mode, regno);
1261 if (rv)
1262 return rv;
1264 ivs = crtl->hard_reg_initial_vals;
1265 if (ivs == 0)
1267 ivs = ggc_alloc<initial_value_struct> ();
1268 ivs->num_entries = 0;
1269 ivs->max_entries = 5;
1270 ivs->entries = ggc_vec_alloc<initial_value_pair> (5);
1271 crtl->hard_reg_initial_vals = ivs;
1274 if (ivs->num_entries >= ivs->max_entries)
1276 ivs->max_entries += 5;
1277 ivs->entries = GGC_RESIZEVEC (initial_value_pair, ivs->entries,
1278 ivs->max_entries);
1281 ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
1282 ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
1284 return ivs->entries[ivs->num_entries++].pseudo;
1287 /* See if get_hard_reg_initial_val has been used to create a pseudo
1288 for the initial value of hard register REGNO in mode MODE. Return
1289 the associated pseudo if so, otherwise return NULL. */
1292 has_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1294 struct initial_value_struct *ivs;
1295 int i;
1297 ivs = crtl->hard_reg_initial_vals;
1298 if (ivs != 0)
1299 for (i = 0; i < ivs->num_entries; i++)
1300 if (GET_MODE (ivs->entries[i].hard_reg) == mode
1301 && REGNO (ivs->entries[i].hard_reg) == regno)
1302 return ivs->entries[i].pseudo;
1304 return NULL_RTX;
1307 unsigned int
1308 emit_initial_value_sets (void)
1310 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1311 int i;
1312 rtx_insn *seq;
1314 if (ivs == 0)
1315 return 0;
1317 start_sequence ();
1318 for (i = 0; i < ivs->num_entries; i++)
1319 emit_move_insn (ivs->entries[i].pseudo, ivs->entries[i].hard_reg);
1320 seq = get_insns ();
1321 end_sequence ();
1323 emit_insn_at_entry (seq);
1324 return 0;
1327 /* Return the hardreg-pseudoreg initial values pair entry I and
1328 TRUE if I is a valid entry, or FALSE if I is not a valid entry. */
1329 bool
1330 initial_value_entry (int i, rtx *hreg, rtx *preg)
1332 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1333 if (!ivs || i >= ivs->num_entries)
1334 return false;
1336 *hreg = ivs->entries[i].hard_reg;
1337 *preg = ivs->entries[i].pseudo;
1338 return true;
1341 /* These routines are responsible for converting virtual register references
1342 to the actual hard register references once RTL generation is complete.
1344 The following four variables are used for communication between the
1345 routines. They contain the offsets of the virtual registers from their
1346 respective hard registers. */
1348 static int in_arg_offset;
1349 static int var_offset;
1350 static int dynamic_offset;
1351 static int out_arg_offset;
1352 static int cfa_offset;
1354 /* In most machines, the stack pointer register is equivalent to the bottom
1355 of the stack. */
1357 #ifndef STACK_POINTER_OFFSET
1358 #define STACK_POINTER_OFFSET 0
1359 #endif
1361 #if defined (REG_PARM_STACK_SPACE) && !defined (INCOMING_REG_PARM_STACK_SPACE)
1362 #define INCOMING_REG_PARM_STACK_SPACE REG_PARM_STACK_SPACE
1363 #endif
1365 /* If not defined, pick an appropriate default for the offset of dynamically
1366 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1367 INCOMING_REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1369 #ifndef STACK_DYNAMIC_OFFSET
1371 /* The bottom of the stack points to the actual arguments. If
1372 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1373 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1374 stack space for register parameters is not pushed by the caller, but
1375 rather part of the fixed stack areas and hence not included in
1376 `crtl->outgoing_args_size'. Nevertheless, we must allow
1377 for it when allocating stack dynamic objects. */
1379 #ifdef INCOMING_REG_PARM_STACK_SPACE
1380 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1381 ((ACCUMULATE_OUTGOING_ARGS \
1382 ? (crtl->outgoing_args_size \
1383 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1384 : INCOMING_REG_PARM_STACK_SPACE (FNDECL))) \
1385 : 0) + (STACK_POINTER_OFFSET))
1386 #else
1387 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1388 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0) \
1389 + (STACK_POINTER_OFFSET))
1390 #endif
1391 #endif
1394 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1395 is a virtual register, return the equivalent hard register and set the
1396 offset indirectly through the pointer. Otherwise, return 0. */
1398 static rtx
1399 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
1401 rtx new_rtx;
1402 HOST_WIDE_INT offset;
1404 if (x == virtual_incoming_args_rtx)
1406 if (stack_realign_drap)
1408 /* Replace virtual_incoming_args_rtx with internal arg
1409 pointer if DRAP is used to realign stack. */
1410 new_rtx = crtl->args.internal_arg_pointer;
1411 offset = 0;
1413 else
1414 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1416 else if (x == virtual_stack_vars_rtx)
1417 new_rtx = frame_pointer_rtx, offset = var_offset;
1418 else if (x == virtual_stack_dynamic_rtx)
1419 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1420 else if (x == virtual_outgoing_args_rtx)
1421 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1422 else if (x == virtual_cfa_rtx)
1424 #ifdef FRAME_POINTER_CFA_OFFSET
1425 new_rtx = frame_pointer_rtx;
1426 #else
1427 new_rtx = arg_pointer_rtx;
1428 #endif
1429 offset = cfa_offset;
1431 else if (x == virtual_preferred_stack_boundary_rtx)
1433 new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1434 offset = 0;
1436 else
1437 return NULL_RTX;
1439 *poffset = offset;
1440 return new_rtx;
1443 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1444 registers present inside of *LOC. The expression is simplified,
1445 as much as possible, but is not to be considered "valid" in any sense
1446 implied by the target. Return true if any change is made. */
1448 static bool
1449 instantiate_virtual_regs_in_rtx (rtx *loc)
1451 if (!*loc)
1452 return false;
1453 bool changed = false;
1454 subrtx_ptr_iterator::array_type array;
1455 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
1457 rtx *loc = *iter;
1458 if (rtx x = *loc)
1460 rtx new_rtx;
1461 HOST_WIDE_INT offset;
1462 switch (GET_CODE (x))
1464 case REG:
1465 new_rtx = instantiate_new_reg (x, &offset);
1466 if (new_rtx)
1468 *loc = plus_constant (GET_MODE (x), new_rtx, offset);
1469 changed = true;
1471 iter.skip_subrtxes ();
1472 break;
1474 case PLUS:
1475 new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1476 if (new_rtx)
1478 XEXP (x, 0) = new_rtx;
1479 *loc = plus_constant (GET_MODE (x), x, offset, true);
1480 changed = true;
1481 iter.skip_subrtxes ();
1482 break;
1485 /* FIXME -- from old code */
1486 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1487 we can commute the PLUS and SUBREG because pointers into the
1488 frame are well-behaved. */
1489 break;
1491 default:
1492 break;
1496 return changed;
1499 /* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1500 matches the predicate for insn CODE operand OPERAND. */
1502 static int
1503 safe_insn_predicate (int code, int operand, rtx x)
1505 return code < 0 || insn_operand_matches ((enum insn_code) code, operand, x);
1508 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1509 registers present inside of insn. The result will be a valid insn. */
1511 static void
1512 instantiate_virtual_regs_in_insn (rtx_insn *insn)
1514 HOST_WIDE_INT offset;
1515 int insn_code, i;
1516 bool any_change = false;
1517 rtx set, new_rtx, x;
1518 rtx_insn *seq;
1520 /* There are some special cases to be handled first. */
1521 set = single_set (insn);
1522 if (set)
1524 /* We're allowed to assign to a virtual register. This is interpreted
1525 to mean that the underlying register gets assigned the inverse
1526 transformation. This is used, for example, in the handling of
1527 non-local gotos. */
1528 new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1529 if (new_rtx)
1531 start_sequence ();
1533 instantiate_virtual_regs_in_rtx (&SET_SRC (set));
1534 x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
1535 gen_int_mode (-offset, GET_MODE (new_rtx)));
1536 x = force_operand (x, new_rtx);
1537 if (x != new_rtx)
1538 emit_move_insn (new_rtx, x);
1540 seq = get_insns ();
1541 end_sequence ();
1543 emit_insn_before (seq, insn);
1544 delete_insn (insn);
1545 return;
1548 /* Handle a straight copy from a virtual register by generating a
1549 new add insn. The difference between this and falling through
1550 to the generic case is avoiding a new pseudo and eliminating a
1551 move insn in the initial rtl stream. */
1552 new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1553 if (new_rtx && offset != 0
1554 && REG_P (SET_DEST (set))
1555 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1557 start_sequence ();
1559 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS, new_rtx,
1560 gen_int_mode (offset,
1561 GET_MODE (SET_DEST (set))),
1562 SET_DEST (set), 1, OPTAB_LIB_WIDEN);
1563 if (x != SET_DEST (set))
1564 emit_move_insn (SET_DEST (set), x);
1566 seq = get_insns ();
1567 end_sequence ();
1569 emit_insn_before (seq, insn);
1570 delete_insn (insn);
1571 return;
1574 extract_insn (insn);
1575 insn_code = INSN_CODE (insn);
1577 /* Handle a plus involving a virtual register by determining if the
1578 operands remain valid if they're modified in place. */
1579 if (GET_CODE (SET_SRC (set)) == PLUS
1580 && recog_data.n_operands >= 3
1581 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1582 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1583 && CONST_INT_P (recog_data.operand[2])
1584 && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
1586 offset += INTVAL (recog_data.operand[2]);
1588 /* If the sum is zero, then replace with a plain move. */
1589 if (offset == 0
1590 && REG_P (SET_DEST (set))
1591 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1593 start_sequence ();
1594 emit_move_insn (SET_DEST (set), new_rtx);
1595 seq = get_insns ();
1596 end_sequence ();
1598 emit_insn_before (seq, insn);
1599 delete_insn (insn);
1600 return;
1603 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1605 /* Using validate_change and apply_change_group here leaves
1606 recog_data in an invalid state. Since we know exactly what
1607 we want to check, do those two by hand. */
1608 if (safe_insn_predicate (insn_code, 1, new_rtx)
1609 && safe_insn_predicate (insn_code, 2, x))
1611 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1612 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1613 any_change = true;
1615 /* Fall through into the regular operand fixup loop in
1616 order to take care of operands other than 1 and 2. */
1620 else
1622 extract_insn (insn);
1623 insn_code = INSN_CODE (insn);
1626 /* In the general case, we expect virtual registers to appear only in
1627 operands, and then only as either bare registers or inside memories. */
1628 for (i = 0; i < recog_data.n_operands; ++i)
1630 x = recog_data.operand[i];
1631 switch (GET_CODE (x))
1633 case MEM:
1635 rtx addr = XEXP (x, 0);
1637 if (!instantiate_virtual_regs_in_rtx (&addr))
1638 continue;
1640 start_sequence ();
1641 x = replace_equiv_address (x, addr, true);
1642 /* It may happen that the address with the virtual reg
1643 was valid (e.g. based on the virtual stack reg, which might
1644 be acceptable to the predicates with all offsets), whereas
1645 the address now isn't anymore, for instance when the address
1646 is still offsetted, but the base reg isn't virtual-stack-reg
1647 anymore. Below we would do a force_reg on the whole operand,
1648 but this insn might actually only accept memory. Hence,
1649 before doing that last resort, try to reload the address into
1650 a register, so this operand stays a MEM. */
1651 if (!safe_insn_predicate (insn_code, i, x))
1653 addr = force_reg (GET_MODE (addr), addr);
1654 x = replace_equiv_address (x, addr, true);
1656 seq = get_insns ();
1657 end_sequence ();
1658 if (seq)
1659 emit_insn_before (seq, insn);
1661 break;
1663 case REG:
1664 new_rtx = instantiate_new_reg (x, &offset);
1665 if (new_rtx == NULL)
1666 continue;
1667 if (offset == 0)
1668 x = new_rtx;
1669 else
1671 start_sequence ();
1673 /* Careful, special mode predicates may have stuff in
1674 insn_data[insn_code].operand[i].mode that isn't useful
1675 to us for computing a new value. */
1676 /* ??? Recognize address_operand and/or "p" constraints
1677 to see if (plus new offset) is a valid before we put
1678 this through expand_simple_binop. */
1679 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1680 gen_int_mode (offset, GET_MODE (x)),
1681 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1682 seq = get_insns ();
1683 end_sequence ();
1684 emit_insn_before (seq, insn);
1686 break;
1688 case SUBREG:
1689 new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1690 if (new_rtx == NULL)
1691 continue;
1692 if (offset != 0)
1694 start_sequence ();
1695 new_rtx = expand_simple_binop
1696 (GET_MODE (new_rtx), PLUS, new_rtx,
1697 gen_int_mode (offset, GET_MODE (new_rtx)),
1698 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1699 seq = get_insns ();
1700 end_sequence ();
1701 emit_insn_before (seq, insn);
1703 x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1704 GET_MODE (new_rtx), SUBREG_BYTE (x));
1705 gcc_assert (x);
1706 break;
1708 default:
1709 continue;
1712 /* At this point, X contains the new value for the operand.
1713 Validate the new value vs the insn predicate. Note that
1714 asm insns will have insn_code -1 here. */
1715 if (!safe_insn_predicate (insn_code, i, x))
1717 start_sequence ();
1718 if (REG_P (x))
1720 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1721 x = copy_to_reg (x);
1723 else
1724 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1725 seq = get_insns ();
1726 end_sequence ();
1727 if (seq)
1728 emit_insn_before (seq, insn);
1731 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1732 any_change = true;
1735 if (any_change)
1737 /* Propagate operand changes into the duplicates. */
1738 for (i = 0; i < recog_data.n_dups; ++i)
1739 *recog_data.dup_loc[i]
1740 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1742 /* Force re-recognition of the instruction for validation. */
1743 INSN_CODE (insn) = -1;
1746 if (asm_noperands (PATTERN (insn)) >= 0)
1748 if (!check_asm_operands (PATTERN (insn)))
1750 error_for_asm (insn, "impossible constraint in %<asm%>");
1751 /* For asm goto, instead of fixing up all the edges
1752 just clear the template and clear input operands
1753 (asm goto doesn't have any output operands). */
1754 if (JUMP_P (insn))
1756 rtx asm_op = extract_asm_operands (PATTERN (insn));
1757 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1758 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1759 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1761 else
1762 delete_insn (insn);
1765 else
1767 if (recog_memoized (insn) < 0)
1768 fatal_insn_not_found (insn);
1772 /* Subroutine of instantiate_decls. Given RTL representing a decl,
1773 do any instantiation required. */
1775 void
1776 instantiate_decl_rtl (rtx x)
1778 rtx addr;
1780 if (x == 0)
1781 return;
1783 /* If this is a CONCAT, recurse for the pieces. */
1784 if (GET_CODE (x) == CONCAT)
1786 instantiate_decl_rtl (XEXP (x, 0));
1787 instantiate_decl_rtl (XEXP (x, 1));
1788 return;
1791 /* If this is not a MEM, no need to do anything. Similarly if the
1792 address is a constant or a register that is not a virtual register. */
1793 if (!MEM_P (x))
1794 return;
1796 addr = XEXP (x, 0);
1797 if (CONSTANT_P (addr)
1798 || (REG_P (addr)
1799 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1800 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1801 return;
1803 instantiate_virtual_regs_in_rtx (&XEXP (x, 0));
1806 /* Helper for instantiate_decls called via walk_tree: Process all decls
1807 in the given DECL_VALUE_EXPR. */
1809 static tree
1810 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1812 tree t = *tp;
1813 if (! EXPR_P (t))
1815 *walk_subtrees = 0;
1816 if (DECL_P (t))
1818 if (DECL_RTL_SET_P (t))
1819 instantiate_decl_rtl (DECL_RTL (t));
1820 if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1821 && DECL_INCOMING_RTL (t))
1822 instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1823 if ((TREE_CODE (t) == VAR_DECL
1824 || TREE_CODE (t) == RESULT_DECL)
1825 && DECL_HAS_VALUE_EXPR_P (t))
1827 tree v = DECL_VALUE_EXPR (t);
1828 walk_tree (&v, instantiate_expr, NULL, NULL);
1832 return NULL;
1835 /* Subroutine of instantiate_decls: Process all decls in the given
1836 BLOCK node and all its subblocks. */
1838 static void
1839 instantiate_decls_1 (tree let)
1841 tree t;
1843 for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1845 if (DECL_RTL_SET_P (t))
1846 instantiate_decl_rtl (DECL_RTL (t));
1847 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t))
1849 tree v = DECL_VALUE_EXPR (t);
1850 walk_tree (&v, instantiate_expr, NULL, NULL);
1854 /* Process all subblocks. */
1855 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1856 instantiate_decls_1 (t);
1859 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1860 all virtual registers in their DECL_RTL's. */
1862 static void
1863 instantiate_decls (tree fndecl)
1865 tree decl;
1866 unsigned ix;
1868 /* Process all parameters of the function. */
1869 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1871 instantiate_decl_rtl (DECL_RTL (decl));
1872 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1873 if (DECL_HAS_VALUE_EXPR_P (decl))
1875 tree v = DECL_VALUE_EXPR (decl);
1876 walk_tree (&v, instantiate_expr, NULL, NULL);
1880 if ((decl = DECL_RESULT (fndecl))
1881 && TREE_CODE (decl) == RESULT_DECL)
1883 if (DECL_RTL_SET_P (decl))
1884 instantiate_decl_rtl (DECL_RTL (decl));
1885 if (DECL_HAS_VALUE_EXPR_P (decl))
1887 tree v = DECL_VALUE_EXPR (decl);
1888 walk_tree (&v, instantiate_expr, NULL, NULL);
1892 /* Process the saved static chain if it exists. */
1893 decl = DECL_STRUCT_FUNCTION (fndecl)->static_chain_decl;
1894 if (decl && DECL_HAS_VALUE_EXPR_P (decl))
1895 instantiate_decl_rtl (DECL_RTL (DECL_VALUE_EXPR (decl)));
1897 /* Now process all variables defined in the function or its subblocks. */
1898 instantiate_decls_1 (DECL_INITIAL (fndecl));
1900 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1901 if (DECL_RTL_SET_P (decl))
1902 instantiate_decl_rtl (DECL_RTL (decl));
1903 vec_free (cfun->local_decls);
1906 /* Pass through the INSNS of function FNDECL and convert virtual register
1907 references to hard register references. */
1909 static unsigned int
1910 instantiate_virtual_regs (void)
1912 rtx_insn *insn;
1914 /* Compute the offsets to use for this function. */
1915 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1916 var_offset = STARTING_FRAME_OFFSET;
1917 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1918 out_arg_offset = STACK_POINTER_OFFSET;
1919 #ifdef FRAME_POINTER_CFA_OFFSET
1920 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1921 #else
1922 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1923 #endif
1925 /* Initialize recognition, indicating that volatile is OK. */
1926 init_recog ();
1928 /* Scan through all the insns, instantiating every virtual register still
1929 present. */
1930 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1931 if (INSN_P (insn))
1933 /* These patterns in the instruction stream can never be recognized.
1934 Fortunately, they shouldn't contain virtual registers either. */
1935 if (GET_CODE (PATTERN (insn)) == USE
1936 || GET_CODE (PATTERN (insn)) == CLOBBER
1937 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
1938 continue;
1939 else if (DEBUG_INSN_P (insn))
1940 instantiate_virtual_regs_in_rtx (&INSN_VAR_LOCATION (insn));
1941 else
1942 instantiate_virtual_regs_in_insn (insn);
1944 if (insn->deleted ())
1945 continue;
1947 instantiate_virtual_regs_in_rtx (&REG_NOTES (insn));
1949 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
1950 if (CALL_P (insn))
1951 instantiate_virtual_regs_in_rtx (&CALL_INSN_FUNCTION_USAGE (insn));
1954 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
1955 instantiate_decls (current_function_decl);
1957 targetm.instantiate_decls ();
1959 /* Indicate that, from now on, assign_stack_local should use
1960 frame_pointer_rtx. */
1961 virtuals_instantiated = 1;
1963 return 0;
1966 namespace {
1968 const pass_data pass_data_instantiate_virtual_regs =
1970 RTL_PASS, /* type */
1971 "vregs", /* name */
1972 OPTGROUP_NONE, /* optinfo_flags */
1973 TV_NONE, /* tv_id */
1974 0, /* properties_required */
1975 0, /* properties_provided */
1976 0, /* properties_destroyed */
1977 0, /* todo_flags_start */
1978 0, /* todo_flags_finish */
1981 class pass_instantiate_virtual_regs : public rtl_opt_pass
1983 public:
1984 pass_instantiate_virtual_regs (gcc::context *ctxt)
1985 : rtl_opt_pass (pass_data_instantiate_virtual_regs, ctxt)
1988 /* opt_pass methods: */
1989 virtual unsigned int execute (function *)
1991 return instantiate_virtual_regs ();
1994 }; // class pass_instantiate_virtual_regs
1996 } // anon namespace
1998 rtl_opt_pass *
1999 make_pass_instantiate_virtual_regs (gcc::context *ctxt)
2001 return new pass_instantiate_virtual_regs (ctxt);
2005 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
2006 This means a type for which function calls must pass an address to the
2007 function or get an address back from the function.
2008 EXP may be a type node or an expression (whose type is tested). */
2011 aggregate_value_p (const_tree exp, const_tree fntype)
2013 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
2014 int i, regno, nregs;
2015 rtx reg;
2017 if (fntype)
2018 switch (TREE_CODE (fntype))
2020 case CALL_EXPR:
2022 tree fndecl = get_callee_fndecl (fntype);
2023 if (fndecl)
2024 fntype = TREE_TYPE (fndecl);
2025 else if (CALL_EXPR_FN (fntype))
2026 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype)));
2027 else
2028 /* For internal functions, assume nothing needs to be
2029 returned in memory. */
2030 return 0;
2032 break;
2033 case FUNCTION_DECL:
2034 fntype = TREE_TYPE (fntype);
2035 break;
2036 case FUNCTION_TYPE:
2037 case METHOD_TYPE:
2038 break;
2039 case IDENTIFIER_NODE:
2040 fntype = NULL_TREE;
2041 break;
2042 default:
2043 /* We don't expect other tree types here. */
2044 gcc_unreachable ();
2047 if (VOID_TYPE_P (type))
2048 return 0;
2050 /* If a record should be passed the same as its first (and only) member
2051 don't pass it as an aggregate. */
2052 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2053 return aggregate_value_p (first_field (type), fntype);
2055 /* If the front end has decided that this needs to be passed by
2056 reference, do so. */
2057 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2058 && DECL_BY_REFERENCE (exp))
2059 return 1;
2061 /* Function types that are TREE_ADDRESSABLE force return in memory. */
2062 if (fntype && TREE_ADDRESSABLE (fntype))
2063 return 1;
2065 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2066 and thus can't be returned in registers. */
2067 if (TREE_ADDRESSABLE (type))
2068 return 1;
2070 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2071 return 1;
2073 if (targetm.calls.return_in_memory (type, fntype))
2074 return 1;
2076 /* Make sure we have suitable call-clobbered regs to return
2077 the value in; if not, we must return it in memory. */
2078 reg = hard_function_value (type, 0, fntype, 0);
2080 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2081 it is OK. */
2082 if (!REG_P (reg))
2083 return 0;
2085 regno = REGNO (reg);
2086 nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
2087 for (i = 0; i < nregs; i++)
2088 if (! call_used_regs[regno + i])
2089 return 1;
2091 return 0;
2094 /* Return true if we should assign DECL a pseudo register; false if it
2095 should live on the local stack. */
2097 bool
2098 use_register_for_decl (const_tree decl)
2100 if (!targetm.calls.allocate_stack_slots_for_args ())
2101 return true;
2103 /* Honor volatile. */
2104 if (TREE_SIDE_EFFECTS (decl))
2105 return false;
2107 /* Honor addressability. */
2108 if (TREE_ADDRESSABLE (decl))
2109 return false;
2111 /* Decl is implicitly addressible by bound stores and loads
2112 if it is an aggregate holding bounds. */
2113 if (chkp_function_instrumented_p (current_function_decl)
2114 && TREE_TYPE (decl)
2115 && !BOUNDED_P (decl)
2116 && chkp_type_has_pointer (TREE_TYPE (decl)))
2117 return false;
2119 /* Only register-like things go in registers. */
2120 if (DECL_MODE (decl) == BLKmode)
2121 return false;
2123 /* If -ffloat-store specified, don't put explicit float variables
2124 into registers. */
2125 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2126 propagates values across these stores, and it probably shouldn't. */
2127 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2128 return false;
2130 /* If we're not interested in tracking debugging information for
2131 this decl, then we can certainly put it in a register. */
2132 if (DECL_IGNORED_P (decl))
2133 return true;
2135 if (optimize)
2136 return true;
2138 if (!DECL_REGISTER (decl))
2139 return false;
2141 switch (TREE_CODE (TREE_TYPE (decl)))
2143 case RECORD_TYPE:
2144 case UNION_TYPE:
2145 case QUAL_UNION_TYPE:
2146 /* When not optimizing, disregard register keyword for variables with
2147 types containing methods, otherwise the methods won't be callable
2148 from the debugger. */
2149 if (TYPE_METHODS (TREE_TYPE (decl)))
2150 return false;
2151 break;
2152 default:
2153 break;
2156 return true;
2159 /* Return true if TYPE should be passed by invisible reference. */
2161 bool
2162 pass_by_reference (CUMULATIVE_ARGS *ca, machine_mode mode,
2163 tree type, bool named_arg)
2165 if (type)
2167 /* If this type contains non-trivial constructors, then it is
2168 forbidden for the middle-end to create any new copies. */
2169 if (TREE_ADDRESSABLE (type))
2170 return true;
2172 /* GCC post 3.4 passes *all* variable sized types by reference. */
2173 if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2174 return true;
2176 /* If a record type should be passed the same as its first (and only)
2177 member, use the type and mode of that member. */
2178 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2180 type = TREE_TYPE (first_field (type));
2181 mode = TYPE_MODE (type);
2185 return targetm.calls.pass_by_reference (pack_cumulative_args (ca), mode,
2186 type, named_arg);
2189 /* Return true if TYPE, which is passed by reference, should be callee
2190 copied instead of caller copied. */
2192 bool
2193 reference_callee_copied (CUMULATIVE_ARGS *ca, machine_mode mode,
2194 tree type, bool named_arg)
2196 if (type && TREE_ADDRESSABLE (type))
2197 return false;
2198 return targetm.calls.callee_copies (pack_cumulative_args (ca), mode, type,
2199 named_arg);
2202 /* Structures to communicate between the subroutines of assign_parms.
2203 The first holds data persistent across all parameters, the second
2204 is cleared out for each parameter. */
2206 struct assign_parm_data_all
2208 /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2209 should become a job of the target or otherwise encapsulated. */
2210 CUMULATIVE_ARGS args_so_far_v;
2211 cumulative_args_t args_so_far;
2212 struct args_size stack_args_size;
2213 tree function_result_decl;
2214 tree orig_fnargs;
2215 rtx_insn *first_conversion_insn;
2216 rtx_insn *last_conversion_insn;
2217 HOST_WIDE_INT pretend_args_size;
2218 HOST_WIDE_INT extra_pretend_bytes;
2219 int reg_parm_stack_space;
2222 struct assign_parm_data_one
2224 tree nominal_type;
2225 tree passed_type;
2226 rtx entry_parm;
2227 rtx stack_parm;
2228 machine_mode nominal_mode;
2229 machine_mode passed_mode;
2230 machine_mode promoted_mode;
2231 struct locate_and_pad_arg_data locate;
2232 int partial;
2233 BOOL_BITFIELD named_arg : 1;
2234 BOOL_BITFIELD passed_pointer : 1;
2235 BOOL_BITFIELD on_stack : 1;
2236 BOOL_BITFIELD loaded_in_reg : 1;
2239 struct bounds_parm_data
2241 assign_parm_data_one parm_data;
2242 tree bounds_parm;
2243 tree ptr_parm;
2244 rtx ptr_entry;
2245 int bound_no;
2248 /* A subroutine of assign_parms. Initialize ALL. */
2250 static void
2251 assign_parms_initialize_all (struct assign_parm_data_all *all)
2253 tree fntype ATTRIBUTE_UNUSED;
2255 memset (all, 0, sizeof (*all));
2257 fntype = TREE_TYPE (current_function_decl);
2259 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
2260 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2261 #else
2262 INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2263 current_function_decl, -1);
2264 #endif
2265 all->args_so_far = pack_cumulative_args (&all->args_so_far_v);
2267 #ifdef INCOMING_REG_PARM_STACK_SPACE
2268 all->reg_parm_stack_space
2269 = INCOMING_REG_PARM_STACK_SPACE (current_function_decl);
2270 #endif
2273 /* If ARGS contains entries with complex types, split the entry into two
2274 entries of the component type. Return a new list of substitutions are
2275 needed, else the old list. */
2277 static void
2278 split_complex_args (vec<tree> *args)
2280 unsigned i;
2281 tree p;
2283 FOR_EACH_VEC_ELT (*args, i, p)
2285 tree type = TREE_TYPE (p);
2286 if (TREE_CODE (type) == COMPLEX_TYPE
2287 && targetm.calls.split_complex_arg (type))
2289 tree decl;
2290 tree subtype = TREE_TYPE (type);
2291 bool addressable = TREE_ADDRESSABLE (p);
2293 /* Rewrite the PARM_DECL's type with its component. */
2294 p = copy_node (p);
2295 TREE_TYPE (p) = subtype;
2296 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2297 DECL_MODE (p) = VOIDmode;
2298 DECL_SIZE (p) = NULL;
2299 DECL_SIZE_UNIT (p) = NULL;
2300 /* If this arg must go in memory, put it in a pseudo here.
2301 We can't allow it to go in memory as per normal parms,
2302 because the usual place might not have the imag part
2303 adjacent to the real part. */
2304 DECL_ARTIFICIAL (p) = addressable;
2305 DECL_IGNORED_P (p) = addressable;
2306 TREE_ADDRESSABLE (p) = 0;
2307 layout_decl (p, 0);
2308 (*args)[i] = p;
2310 /* Build a second synthetic decl. */
2311 decl = build_decl (EXPR_LOCATION (p),
2312 PARM_DECL, NULL_TREE, subtype);
2313 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2314 DECL_ARTIFICIAL (decl) = addressable;
2315 DECL_IGNORED_P (decl) = addressable;
2316 layout_decl (decl, 0);
2317 args->safe_insert (++i, decl);
2322 /* A subroutine of assign_parms. Adjust the parameter list to incorporate
2323 the hidden struct return argument, and (abi willing) complex args.
2324 Return the new parameter list. */
2326 static vec<tree>
2327 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2329 tree fndecl = current_function_decl;
2330 tree fntype = TREE_TYPE (fndecl);
2331 vec<tree> fnargs = vNULL;
2332 tree arg;
2334 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2335 fnargs.safe_push (arg);
2337 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2339 /* If struct value address is treated as the first argument, make it so. */
2340 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2341 && ! cfun->returns_pcc_struct
2342 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2344 tree type = build_pointer_type (TREE_TYPE (fntype));
2345 tree decl;
2347 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2348 PARM_DECL, get_identifier (".result_ptr"), type);
2349 DECL_ARG_TYPE (decl) = type;
2350 DECL_ARTIFICIAL (decl) = 1;
2351 DECL_NAMELESS (decl) = 1;
2352 TREE_CONSTANT (decl) = 1;
2354 DECL_CHAIN (decl) = all->orig_fnargs;
2355 all->orig_fnargs = decl;
2356 fnargs.safe_insert (0, decl);
2358 all->function_result_decl = decl;
2360 /* If function is instrumented then bounds of the
2361 passed structure address is the second argument. */
2362 if (chkp_function_instrumented_p (fndecl))
2364 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2365 PARM_DECL, get_identifier (".result_bnd"),
2366 pointer_bounds_type_node);
2367 DECL_ARG_TYPE (decl) = pointer_bounds_type_node;
2368 DECL_ARTIFICIAL (decl) = 1;
2369 DECL_NAMELESS (decl) = 1;
2370 TREE_CONSTANT (decl) = 1;
2372 DECL_CHAIN (decl) = DECL_CHAIN (all->orig_fnargs);
2373 DECL_CHAIN (all->orig_fnargs) = decl;
2374 fnargs.safe_insert (1, decl);
2378 /* If the target wants to split complex arguments into scalars, do so. */
2379 if (targetm.calls.split_complex_arg)
2380 split_complex_args (&fnargs);
2382 return fnargs;
2385 /* A subroutine of assign_parms. Examine PARM and pull out type and mode
2386 data for the parameter. Incorporate ABI specifics such as pass-by-
2387 reference and type promotion. */
2389 static void
2390 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2391 struct assign_parm_data_one *data)
2393 tree nominal_type, passed_type;
2394 machine_mode nominal_mode, passed_mode, promoted_mode;
2395 int unsignedp;
2397 memset (data, 0, sizeof (*data));
2399 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
2400 if (!cfun->stdarg)
2401 data->named_arg = 1; /* No variadic parms. */
2402 else if (DECL_CHAIN (parm))
2403 data->named_arg = 1; /* Not the last non-variadic parm. */
2404 else if (targetm.calls.strict_argument_naming (all->args_so_far))
2405 data->named_arg = 1; /* Only variadic ones are unnamed. */
2406 else
2407 data->named_arg = 0; /* Treat as variadic. */
2409 nominal_type = TREE_TYPE (parm);
2410 passed_type = DECL_ARG_TYPE (parm);
2412 /* Look out for errors propagating this far. Also, if the parameter's
2413 type is void then its value doesn't matter. */
2414 if (TREE_TYPE (parm) == error_mark_node
2415 /* This can happen after weird syntax errors
2416 or if an enum type is defined among the parms. */
2417 || TREE_CODE (parm) != PARM_DECL
2418 || passed_type == NULL
2419 || VOID_TYPE_P (nominal_type))
2421 nominal_type = passed_type = void_type_node;
2422 nominal_mode = passed_mode = promoted_mode = VOIDmode;
2423 goto egress;
2426 /* Find mode of arg as it is passed, and mode of arg as it should be
2427 during execution of this function. */
2428 passed_mode = TYPE_MODE (passed_type);
2429 nominal_mode = TYPE_MODE (nominal_type);
2431 /* If the parm is to be passed as a transparent union or record, use the
2432 type of the first field for the tests below. We have already verified
2433 that the modes are the same. */
2434 if ((TREE_CODE (passed_type) == UNION_TYPE
2435 || TREE_CODE (passed_type) == RECORD_TYPE)
2436 && TYPE_TRANSPARENT_AGGR (passed_type))
2437 passed_type = TREE_TYPE (first_field (passed_type));
2439 /* See if this arg was passed by invisible reference. */
2440 if (pass_by_reference (&all->args_so_far_v, passed_mode,
2441 passed_type, data->named_arg))
2443 passed_type = nominal_type = build_pointer_type (passed_type);
2444 data->passed_pointer = true;
2445 passed_mode = nominal_mode = TYPE_MODE (nominal_type);
2448 /* Find mode as it is passed by the ABI. */
2449 unsignedp = TYPE_UNSIGNED (passed_type);
2450 promoted_mode = promote_function_mode (passed_type, passed_mode, &unsignedp,
2451 TREE_TYPE (current_function_decl), 0);
2453 egress:
2454 data->nominal_type = nominal_type;
2455 data->passed_type = passed_type;
2456 data->nominal_mode = nominal_mode;
2457 data->passed_mode = passed_mode;
2458 data->promoted_mode = promoted_mode;
2461 /* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2463 static void
2464 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2465 struct assign_parm_data_one *data, bool no_rtl)
2467 int varargs_pretend_bytes = 0;
2469 targetm.calls.setup_incoming_varargs (all->args_so_far,
2470 data->promoted_mode,
2471 data->passed_type,
2472 &varargs_pretend_bytes, no_rtl);
2474 /* If the back-end has requested extra stack space, record how much is
2475 needed. Do not change pretend_args_size otherwise since it may be
2476 nonzero from an earlier partial argument. */
2477 if (varargs_pretend_bytes > 0)
2478 all->pretend_args_size = varargs_pretend_bytes;
2481 /* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2482 the incoming location of the current parameter. */
2484 static void
2485 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2486 struct assign_parm_data_one *data)
2488 HOST_WIDE_INT pretend_bytes = 0;
2489 rtx entry_parm;
2490 bool in_regs;
2492 if (data->promoted_mode == VOIDmode)
2494 data->entry_parm = data->stack_parm = const0_rtx;
2495 return;
2498 entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2499 data->promoted_mode,
2500 data->passed_type,
2501 data->named_arg);
2503 if (entry_parm == 0)
2504 data->promoted_mode = data->passed_mode;
2506 /* Determine parm's home in the stack, in case it arrives in the stack
2507 or we should pretend it did. Compute the stack position and rtx where
2508 the argument arrives and its size.
2510 There is one complexity here: If this was a parameter that would
2511 have been passed in registers, but wasn't only because it is
2512 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2513 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2514 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2515 as it was the previous time. */
2516 in_regs = (entry_parm != 0) || POINTER_BOUNDS_TYPE_P (data->passed_type);
2517 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2518 in_regs = true;
2519 #endif
2520 if (!in_regs && !data->named_arg)
2522 if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2524 rtx tem;
2525 tem = targetm.calls.function_incoming_arg (all->args_so_far,
2526 data->promoted_mode,
2527 data->passed_type, true);
2528 in_regs = tem != NULL;
2532 /* If this parameter was passed both in registers and in the stack, use
2533 the copy on the stack. */
2534 if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2535 data->passed_type))
2536 entry_parm = 0;
2538 if (entry_parm)
2540 int partial;
2542 partial = targetm.calls.arg_partial_bytes (all->args_so_far,
2543 data->promoted_mode,
2544 data->passed_type,
2545 data->named_arg);
2546 data->partial = partial;
2548 /* The caller might already have allocated stack space for the
2549 register parameters. */
2550 if (partial != 0 && all->reg_parm_stack_space == 0)
2552 /* Part of this argument is passed in registers and part
2553 is passed on the stack. Ask the prologue code to extend
2554 the stack part so that we can recreate the full value.
2556 PRETEND_BYTES is the size of the registers we need to store.
2557 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2558 stack space that the prologue should allocate.
2560 Internally, gcc assumes that the argument pointer is aligned
2561 to STACK_BOUNDARY bits. This is used both for alignment
2562 optimizations (see init_emit) and to locate arguments that are
2563 aligned to more than PARM_BOUNDARY bits. We must preserve this
2564 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2565 a stack boundary. */
2567 /* We assume at most one partial arg, and it must be the first
2568 argument on the stack. */
2569 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2571 pretend_bytes = partial;
2572 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2574 /* We want to align relative to the actual stack pointer, so
2575 don't include this in the stack size until later. */
2576 all->extra_pretend_bytes = all->pretend_args_size;
2580 locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2581 all->reg_parm_stack_space,
2582 entry_parm ? data->partial : 0, current_function_decl,
2583 &all->stack_args_size, &data->locate);
2585 /* Update parm_stack_boundary if this parameter is passed in the
2586 stack. */
2587 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2588 crtl->parm_stack_boundary = data->locate.boundary;
2590 /* Adjust offsets to include the pretend args. */
2591 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2592 data->locate.slot_offset.constant += pretend_bytes;
2593 data->locate.offset.constant += pretend_bytes;
2595 data->entry_parm = entry_parm;
2598 /* A subroutine of assign_parms. If there is actually space on the stack
2599 for this parm, count it in stack_args_size and return true. */
2601 static bool
2602 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2603 struct assign_parm_data_one *data)
2605 /* Bounds are never passed on the stack to keep compatibility
2606 with not instrumented code. */
2607 if (POINTER_BOUNDS_TYPE_P (data->passed_type))
2608 return false;
2609 /* Trivially true if we've no incoming register. */
2610 else if (data->entry_parm == NULL)
2612 /* Also true if we're partially in registers and partially not,
2613 since we've arranged to drop the entire argument on the stack. */
2614 else if (data->partial != 0)
2616 /* Also true if the target says that it's passed in both registers
2617 and on the stack. */
2618 else if (GET_CODE (data->entry_parm) == PARALLEL
2619 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2621 /* Also true if the target says that there's stack allocated for
2622 all register parameters. */
2623 else if (all->reg_parm_stack_space > 0)
2625 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2626 else
2627 return false;
2629 all->stack_args_size.constant += data->locate.size.constant;
2630 if (data->locate.size.var)
2631 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2633 return true;
2636 /* A subroutine of assign_parms. Given that this parameter is allocated
2637 stack space by the ABI, find it. */
2639 static void
2640 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2642 rtx offset_rtx, stack_parm;
2643 unsigned int align, boundary;
2645 /* If we're passing this arg using a reg, make its stack home the
2646 aligned stack slot. */
2647 if (data->entry_parm)
2648 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2649 else
2650 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2652 stack_parm = crtl->args.internal_arg_pointer;
2653 if (offset_rtx != const0_rtx)
2654 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2655 stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2657 if (!data->passed_pointer)
2659 set_mem_attributes (stack_parm, parm, 1);
2660 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2661 while promoted mode's size is needed. */
2662 if (data->promoted_mode != BLKmode
2663 && data->promoted_mode != DECL_MODE (parm))
2665 set_mem_size (stack_parm, GET_MODE_SIZE (data->promoted_mode));
2666 if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2668 int offset = subreg_lowpart_offset (DECL_MODE (parm),
2669 data->promoted_mode);
2670 if (offset)
2671 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2676 boundary = data->locate.boundary;
2677 align = BITS_PER_UNIT;
2679 /* If we're padding upward, we know that the alignment of the slot
2680 is TARGET_FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2681 intentionally forcing upward padding. Otherwise we have to come
2682 up with a guess at the alignment based on OFFSET_RTX. */
2683 if (data->locate.where_pad != downward || data->entry_parm)
2684 align = boundary;
2685 else if (CONST_INT_P (offset_rtx))
2687 align = INTVAL (offset_rtx) * BITS_PER_UNIT | boundary;
2688 align = align & -align;
2690 set_mem_align (stack_parm, align);
2692 if (data->entry_parm)
2693 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2695 data->stack_parm = stack_parm;
2698 /* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2699 always valid and contiguous. */
2701 static void
2702 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2704 rtx entry_parm = data->entry_parm;
2705 rtx stack_parm = data->stack_parm;
2707 /* If this parm was passed part in regs and part in memory, pretend it
2708 arrived entirely in memory by pushing the register-part onto the stack.
2709 In the special case of a DImode or DFmode that is split, we could put
2710 it together in a pseudoreg directly, but for now that's not worth
2711 bothering with. */
2712 if (data->partial != 0)
2714 /* Handle calls that pass values in multiple non-contiguous
2715 locations. The Irix 6 ABI has examples of this. */
2716 if (GET_CODE (entry_parm) == PARALLEL)
2717 emit_group_store (validize_mem (copy_rtx (stack_parm)), entry_parm,
2718 data->passed_type,
2719 int_size_in_bytes (data->passed_type));
2720 else
2722 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2723 move_block_from_reg (REGNO (entry_parm),
2724 validize_mem (copy_rtx (stack_parm)),
2725 data->partial / UNITS_PER_WORD);
2728 entry_parm = stack_parm;
2731 /* If we didn't decide this parm came in a register, by default it came
2732 on the stack. */
2733 else if (entry_parm == NULL)
2734 entry_parm = stack_parm;
2736 /* When an argument is passed in multiple locations, we can't make use
2737 of this information, but we can save some copying if the whole argument
2738 is passed in a single register. */
2739 else if (GET_CODE (entry_parm) == PARALLEL
2740 && data->nominal_mode != BLKmode
2741 && data->passed_mode != BLKmode)
2743 size_t i, len = XVECLEN (entry_parm, 0);
2745 for (i = 0; i < len; i++)
2746 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2747 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2748 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2749 == data->passed_mode)
2750 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2752 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2753 break;
2757 data->entry_parm = entry_parm;
2760 /* A subroutine of assign_parms. Reconstitute any values which were
2761 passed in multiple registers and would fit in a single register. */
2763 static void
2764 assign_parm_remove_parallels (struct assign_parm_data_one *data)
2766 rtx entry_parm = data->entry_parm;
2768 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2769 This can be done with register operations rather than on the
2770 stack, even if we will store the reconstituted parameter on the
2771 stack later. */
2772 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2774 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2775 emit_group_store (parmreg, entry_parm, data->passed_type,
2776 GET_MODE_SIZE (GET_MODE (entry_parm)));
2777 entry_parm = parmreg;
2780 data->entry_parm = entry_parm;
2783 /* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2784 always valid and properly aligned. */
2786 static void
2787 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2789 rtx stack_parm = data->stack_parm;
2791 /* If we can't trust the parm stack slot to be aligned enough for its
2792 ultimate type, don't use that slot after entry. We'll make another
2793 stack slot, if we need one. */
2794 if (stack_parm
2795 && ((STRICT_ALIGNMENT
2796 && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2797 || (data->nominal_type
2798 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2799 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2800 stack_parm = NULL;
2802 /* If parm was passed in memory, and we need to convert it on entry,
2803 don't store it back in that same slot. */
2804 else if (data->entry_parm == stack_parm
2805 && data->nominal_mode != BLKmode
2806 && data->nominal_mode != data->passed_mode)
2807 stack_parm = NULL;
2809 /* If stack protection is in effect for this function, don't leave any
2810 pointers in their passed stack slots. */
2811 else if (crtl->stack_protect_guard
2812 && (flag_stack_protect == 2
2813 || data->passed_pointer
2814 || POINTER_TYPE_P (data->nominal_type)))
2815 stack_parm = NULL;
2817 data->stack_parm = stack_parm;
2820 /* A subroutine of assign_parms. Return true if the current parameter
2821 should be stored as a BLKmode in the current frame. */
2823 static bool
2824 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2826 if (data->nominal_mode == BLKmode)
2827 return true;
2828 if (GET_MODE (data->entry_parm) == BLKmode)
2829 return true;
2831 #ifdef BLOCK_REG_PADDING
2832 /* Only assign_parm_setup_block knows how to deal with register arguments
2833 that are padded at the least significant end. */
2834 if (REG_P (data->entry_parm)
2835 && GET_MODE_SIZE (data->promoted_mode) < UNITS_PER_WORD
2836 && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2837 == (BYTES_BIG_ENDIAN ? upward : downward)))
2838 return true;
2839 #endif
2841 return false;
2844 /* A subroutine of assign_parms. Arrange for the parameter to be
2845 present and valid in DATA->STACK_RTL. */
2847 static void
2848 assign_parm_setup_block (struct assign_parm_data_all *all,
2849 tree parm, struct assign_parm_data_one *data)
2851 rtx entry_parm = data->entry_parm;
2852 rtx stack_parm = data->stack_parm;
2853 HOST_WIDE_INT size;
2854 HOST_WIDE_INT size_stored;
2856 if (GET_CODE (entry_parm) == PARALLEL)
2857 entry_parm = emit_group_move_into_temps (entry_parm);
2859 size = int_size_in_bytes (data->passed_type);
2860 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2861 if (stack_parm == 0)
2863 DECL_ALIGN (parm) = MAX (DECL_ALIGN (parm), BITS_PER_WORD);
2864 stack_parm = assign_stack_local (BLKmode, size_stored,
2865 DECL_ALIGN (parm));
2866 if (GET_MODE_SIZE (GET_MODE (entry_parm)) == size)
2867 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2868 set_mem_attributes (stack_parm, parm, 1);
2871 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2872 calls that pass values in multiple non-contiguous locations. */
2873 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2875 rtx mem;
2877 /* Note that we will be storing an integral number of words.
2878 So we have to be careful to ensure that we allocate an
2879 integral number of words. We do this above when we call
2880 assign_stack_local if space was not allocated in the argument
2881 list. If it was, this will not work if PARM_BOUNDARY is not
2882 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2883 if it becomes a problem. Exception is when BLKmode arrives
2884 with arguments not conforming to word_mode. */
2886 if (data->stack_parm == 0)
2888 else if (GET_CODE (entry_parm) == PARALLEL)
2890 else
2891 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2893 mem = validize_mem (copy_rtx (stack_parm));
2895 /* Handle values in multiple non-contiguous locations. */
2896 if (GET_CODE (entry_parm) == PARALLEL)
2898 push_to_sequence2 (all->first_conversion_insn,
2899 all->last_conversion_insn);
2900 emit_group_store (mem, entry_parm, data->passed_type, size);
2901 all->first_conversion_insn = get_insns ();
2902 all->last_conversion_insn = get_last_insn ();
2903 end_sequence ();
2906 else if (size == 0)
2909 /* If SIZE is that of a mode no bigger than a word, just use
2910 that mode's store operation. */
2911 else if (size <= UNITS_PER_WORD)
2913 machine_mode mode
2914 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2916 if (mode != BLKmode
2917 #ifdef BLOCK_REG_PADDING
2918 && (size == UNITS_PER_WORD
2919 || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2920 != (BYTES_BIG_ENDIAN ? upward : downward)))
2921 #endif
2924 rtx reg;
2926 /* We are really truncating a word_mode value containing
2927 SIZE bytes into a value of mode MODE. If such an
2928 operation requires no actual instructions, we can refer
2929 to the value directly in mode MODE, otherwise we must
2930 start with the register in word_mode and explicitly
2931 convert it. */
2932 if (TRULY_NOOP_TRUNCATION (size * BITS_PER_UNIT, BITS_PER_WORD))
2933 reg = gen_rtx_REG (mode, REGNO (entry_parm));
2934 else
2936 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2937 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
2939 emit_move_insn (change_address (mem, mode, 0), reg);
2942 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
2943 machine must be aligned to the left before storing
2944 to memory. Note that the previous test doesn't
2945 handle all cases (e.g. SIZE == 3). */
2946 else if (size != UNITS_PER_WORD
2947 #ifdef BLOCK_REG_PADDING
2948 && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2949 == downward)
2950 #else
2951 && BYTES_BIG_ENDIAN
2952 #endif
2955 rtx tem, x;
2956 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2957 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2959 x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
2960 tem = change_address (mem, word_mode, 0);
2961 emit_move_insn (tem, x);
2963 else
2964 move_block_from_reg (REGNO (entry_parm), mem,
2965 size_stored / UNITS_PER_WORD);
2967 else
2968 move_block_from_reg (REGNO (entry_parm), mem,
2969 size_stored / UNITS_PER_WORD);
2971 else if (data->stack_parm == 0)
2973 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
2974 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
2975 BLOCK_OP_NORMAL);
2976 all->first_conversion_insn = get_insns ();
2977 all->last_conversion_insn = get_last_insn ();
2978 end_sequence ();
2981 data->stack_parm = stack_parm;
2982 SET_DECL_RTL (parm, stack_parm);
2985 /* A subroutine of assign_parms. Allocate a pseudo to hold the current
2986 parameter. Get it there. Perform all ABI specified conversions. */
2988 static void
2989 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
2990 struct assign_parm_data_one *data)
2992 rtx parmreg, validated_mem;
2993 rtx equiv_stack_parm;
2994 machine_mode promoted_nominal_mode;
2995 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
2996 bool did_conversion = false;
2997 bool need_conversion, moved;
2999 /* Store the parm in a pseudoregister during the function, but we may
3000 need to do it in a wider mode. Using 2 here makes the result
3001 consistent with promote_decl_mode and thus expand_expr_real_1. */
3002 promoted_nominal_mode
3003 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
3004 TREE_TYPE (current_function_decl), 2);
3006 parmreg = gen_reg_rtx (promoted_nominal_mode);
3008 if (!DECL_ARTIFICIAL (parm))
3009 mark_user_reg (parmreg);
3011 /* If this was an item that we received a pointer to,
3012 set DECL_RTL appropriately. */
3013 if (data->passed_pointer)
3015 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
3016 set_mem_attributes (x, parm, 1);
3017 SET_DECL_RTL (parm, x);
3019 else
3020 SET_DECL_RTL (parm, parmreg);
3022 assign_parm_remove_parallels (data);
3024 /* Copy the value into the register, thus bridging between
3025 assign_parm_find_data_types and expand_expr_real_1. */
3027 equiv_stack_parm = data->stack_parm;
3028 validated_mem = validize_mem (copy_rtx (data->entry_parm));
3030 need_conversion = (data->nominal_mode != data->passed_mode
3031 || promoted_nominal_mode != data->promoted_mode);
3032 moved = false;
3034 if (need_conversion
3035 && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
3036 && data->nominal_mode == data->passed_mode
3037 && data->nominal_mode == GET_MODE (data->entry_parm))
3039 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
3040 mode, by the caller. We now have to convert it to
3041 NOMINAL_MODE, if different. However, PARMREG may be in
3042 a different mode than NOMINAL_MODE if it is being stored
3043 promoted.
3045 If ENTRY_PARM is a hard register, it might be in a register
3046 not valid for operating in its mode (e.g., an odd-numbered
3047 register for a DFmode). In that case, moves are the only
3048 thing valid, so we can't do a convert from there. This
3049 occurs when the calling sequence allow such misaligned
3050 usages.
3052 In addition, the conversion may involve a call, which could
3053 clobber parameters which haven't been copied to pseudo
3054 registers yet.
3056 First, we try to emit an insn which performs the necessary
3057 conversion. We verify that this insn does not clobber any
3058 hard registers. */
3060 enum insn_code icode;
3061 rtx op0, op1;
3063 icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
3064 unsignedp);
3066 op0 = parmreg;
3067 op1 = validated_mem;
3068 if (icode != CODE_FOR_nothing
3069 && insn_operand_matches (icode, 0, op0)
3070 && insn_operand_matches (icode, 1, op1))
3072 enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
3073 rtx_insn *insn, *insns;
3074 rtx t = op1;
3075 HARD_REG_SET hardregs;
3077 start_sequence ();
3078 /* If op1 is a hard register that is likely spilled, first
3079 force it into a pseudo, otherwise combiner might extend
3080 its lifetime too much. */
3081 if (GET_CODE (t) == SUBREG)
3082 t = SUBREG_REG (t);
3083 if (REG_P (t)
3084 && HARD_REGISTER_P (t)
3085 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (t))
3086 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (t))))
3088 t = gen_reg_rtx (GET_MODE (op1));
3089 emit_move_insn (t, op1);
3091 else
3092 t = op1;
3093 rtx pat = gen_extend_insn (op0, t, promoted_nominal_mode,
3094 data->passed_mode, unsignedp);
3095 emit_insn (pat);
3096 insns = get_insns ();
3098 moved = true;
3099 CLEAR_HARD_REG_SET (hardregs);
3100 for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3102 if (INSN_P (insn))
3103 note_stores (PATTERN (insn), record_hard_reg_sets,
3104 &hardregs);
3105 if (!hard_reg_set_empty_p (hardregs))
3106 moved = false;
3109 end_sequence ();
3111 if (moved)
3113 emit_insn (insns);
3114 if (equiv_stack_parm != NULL_RTX)
3115 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3116 equiv_stack_parm);
3121 if (moved)
3122 /* Nothing to do. */
3124 else if (need_conversion)
3126 /* We did not have an insn to convert directly, or the sequence
3127 generated appeared unsafe. We must first copy the parm to a
3128 pseudo reg, and save the conversion until after all
3129 parameters have been moved. */
3131 int save_tree_used;
3132 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3134 emit_move_insn (tempreg, validated_mem);
3136 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3137 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3139 if (GET_CODE (tempreg) == SUBREG
3140 && GET_MODE (tempreg) == data->nominal_mode
3141 && REG_P (SUBREG_REG (tempreg))
3142 && data->nominal_mode == data->passed_mode
3143 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm)
3144 && GET_MODE_SIZE (GET_MODE (tempreg))
3145 < GET_MODE_SIZE (GET_MODE (data->entry_parm)))
3147 /* The argument is already sign/zero extended, so note it
3148 into the subreg. */
3149 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3150 SUBREG_PROMOTED_SET (tempreg, unsignedp);
3153 /* TREE_USED gets set erroneously during expand_assignment. */
3154 save_tree_used = TREE_USED (parm);
3155 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3156 TREE_USED (parm) = save_tree_used;
3157 all->first_conversion_insn = get_insns ();
3158 all->last_conversion_insn = get_last_insn ();
3159 end_sequence ();
3161 did_conversion = true;
3163 else
3164 emit_move_insn (parmreg, validated_mem);
3166 /* If we were passed a pointer but the actual value can safely live
3167 in a register, retrieve it and use it directly. */
3168 if (data->passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode)
3170 /* We can't use nominal_mode, because it will have been set to
3171 Pmode above. We must use the actual mode of the parm. */
3172 if (use_register_for_decl (parm))
3174 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3175 mark_user_reg (parmreg);
3177 else
3179 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3180 TYPE_MODE (TREE_TYPE (parm)),
3181 TYPE_ALIGN (TREE_TYPE (parm)));
3182 parmreg
3183 = assign_stack_local (TYPE_MODE (TREE_TYPE (parm)),
3184 GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parm))),
3185 align);
3186 set_mem_attributes (parmreg, parm, 1);
3189 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
3191 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
3192 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3194 push_to_sequence2 (all->first_conversion_insn,
3195 all->last_conversion_insn);
3196 emit_move_insn (tempreg, DECL_RTL (parm));
3197 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3198 emit_move_insn (parmreg, tempreg);
3199 all->first_conversion_insn = get_insns ();
3200 all->last_conversion_insn = get_last_insn ();
3201 end_sequence ();
3203 did_conversion = true;
3205 else
3206 emit_move_insn (parmreg, DECL_RTL (parm));
3208 SET_DECL_RTL (parm, parmreg);
3210 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3211 now the parm. */
3212 data->stack_parm = NULL;
3215 /* Mark the register as eliminable if we did no conversion and it was
3216 copied from memory at a fixed offset, and the arg pointer was not
3217 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3218 offset formed an invalid address, such memory-equivalences as we
3219 make here would screw up life analysis for it. */
3220 if (data->nominal_mode == data->passed_mode
3221 && !did_conversion
3222 && data->stack_parm != 0
3223 && MEM_P (data->stack_parm)
3224 && data->locate.offset.var == 0
3225 && reg_mentioned_p (virtual_incoming_args_rtx,
3226 XEXP (data->stack_parm, 0)))
3228 rtx_insn *linsn = get_last_insn ();
3229 rtx_insn *sinsn;
3230 rtx set;
3232 /* Mark complex types separately. */
3233 if (GET_CODE (parmreg) == CONCAT)
3235 machine_mode submode
3236 = GET_MODE_INNER (GET_MODE (parmreg));
3237 int regnor = REGNO (XEXP (parmreg, 0));
3238 int regnoi = REGNO (XEXP (parmreg, 1));
3239 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3240 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3241 GET_MODE_SIZE (submode));
3243 /* Scan backwards for the set of the real and
3244 imaginary parts. */
3245 for (sinsn = linsn; sinsn != 0;
3246 sinsn = prev_nonnote_insn (sinsn))
3248 set = single_set (sinsn);
3249 if (set == 0)
3250 continue;
3252 if (SET_DEST (set) == regno_reg_rtx [regnoi])
3253 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3254 else if (SET_DEST (set) == regno_reg_rtx [regnor])
3255 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3258 else
3259 set_dst_reg_note (linsn, REG_EQUIV, equiv_stack_parm, parmreg);
3262 /* For pointer data type, suggest pointer register. */
3263 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3264 mark_reg_pointer (parmreg,
3265 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3268 /* A subroutine of assign_parms. Allocate stack space to hold the current
3269 parameter. Get it there. Perform all ABI specified conversions. */
3271 static void
3272 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3273 struct assign_parm_data_one *data)
3275 /* Value must be stored in the stack slot STACK_PARM during function
3276 execution. */
3277 bool to_conversion = false;
3279 assign_parm_remove_parallels (data);
3281 if (data->promoted_mode != data->nominal_mode)
3283 /* Conversion is required. */
3284 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3286 emit_move_insn (tempreg, validize_mem (copy_rtx (data->entry_parm)));
3288 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3289 to_conversion = true;
3291 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3292 TYPE_UNSIGNED (TREE_TYPE (parm)));
3294 if (data->stack_parm)
3296 int offset = subreg_lowpart_offset (data->nominal_mode,
3297 GET_MODE (data->stack_parm));
3298 /* ??? This may need a big-endian conversion on sparc64. */
3299 data->stack_parm
3300 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3301 if (offset && MEM_OFFSET_KNOWN_P (data->stack_parm))
3302 set_mem_offset (data->stack_parm,
3303 MEM_OFFSET (data->stack_parm) + offset);
3307 if (data->entry_parm != data->stack_parm)
3309 rtx src, dest;
3311 if (data->stack_parm == 0)
3313 int align = STACK_SLOT_ALIGNMENT (data->passed_type,
3314 GET_MODE (data->entry_parm),
3315 TYPE_ALIGN (data->passed_type));
3316 data->stack_parm
3317 = assign_stack_local (GET_MODE (data->entry_parm),
3318 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3319 align);
3320 set_mem_attributes (data->stack_parm, parm, 1);
3323 dest = validize_mem (copy_rtx (data->stack_parm));
3324 src = validize_mem (copy_rtx (data->entry_parm));
3326 if (MEM_P (src))
3328 /* Use a block move to handle potentially misaligned entry_parm. */
3329 if (!to_conversion)
3330 push_to_sequence2 (all->first_conversion_insn,
3331 all->last_conversion_insn);
3332 to_conversion = true;
3334 emit_block_move (dest, src,
3335 GEN_INT (int_size_in_bytes (data->passed_type)),
3336 BLOCK_OP_NORMAL);
3338 else
3339 emit_move_insn (dest, src);
3342 if (to_conversion)
3344 all->first_conversion_insn = get_insns ();
3345 all->last_conversion_insn = get_last_insn ();
3346 end_sequence ();
3349 SET_DECL_RTL (parm, data->stack_parm);
3352 /* A subroutine of assign_parms. If the ABI splits complex arguments, then
3353 undo the frobbing that we did in assign_parms_augmented_arg_list. */
3355 static void
3356 assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3357 vec<tree> fnargs)
3359 tree parm;
3360 tree orig_fnargs = all->orig_fnargs;
3361 unsigned i = 0;
3363 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3365 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3366 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3368 rtx tmp, real, imag;
3369 machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3371 real = DECL_RTL (fnargs[i]);
3372 imag = DECL_RTL (fnargs[i + 1]);
3373 if (inner != GET_MODE (real))
3375 real = gen_lowpart_SUBREG (inner, real);
3376 imag = gen_lowpart_SUBREG (inner, imag);
3379 if (TREE_ADDRESSABLE (parm))
3381 rtx rmem, imem;
3382 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3383 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3384 DECL_MODE (parm),
3385 TYPE_ALIGN (TREE_TYPE (parm)));
3387 /* split_complex_arg put the real and imag parts in
3388 pseudos. Move them to memory. */
3389 tmp = assign_stack_local (DECL_MODE (parm), size, align);
3390 set_mem_attributes (tmp, parm, 1);
3391 rmem = adjust_address_nv (tmp, inner, 0);
3392 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3393 push_to_sequence2 (all->first_conversion_insn,
3394 all->last_conversion_insn);
3395 emit_move_insn (rmem, real);
3396 emit_move_insn (imem, imag);
3397 all->first_conversion_insn = get_insns ();
3398 all->last_conversion_insn = get_last_insn ();
3399 end_sequence ();
3401 else
3402 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3403 SET_DECL_RTL (parm, tmp);
3405 real = DECL_INCOMING_RTL (fnargs[i]);
3406 imag = DECL_INCOMING_RTL (fnargs[i + 1]);
3407 if (inner != GET_MODE (real))
3409 real = gen_lowpart_SUBREG (inner, real);
3410 imag = gen_lowpart_SUBREG (inner, imag);
3412 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3413 set_decl_incoming_rtl (parm, tmp, false);
3414 i++;
3419 /* Load bounds of PARM from bounds table. */
3420 static void
3421 assign_parm_load_bounds (struct assign_parm_data_one *data,
3422 tree parm,
3423 rtx entry,
3424 unsigned bound_no)
3426 bitmap_iterator bi;
3427 unsigned i, offs = 0;
3428 int bnd_no = -1;
3429 rtx slot = NULL, ptr = NULL;
3431 if (parm)
3433 bitmap slots;
3434 bitmap_obstack_initialize (NULL);
3435 slots = BITMAP_ALLOC (NULL);
3436 chkp_find_bound_slots (TREE_TYPE (parm), slots);
3437 EXECUTE_IF_SET_IN_BITMAP (slots, 0, i, bi)
3439 if (bound_no)
3440 bound_no--;
3441 else
3443 bnd_no = i;
3444 break;
3447 BITMAP_FREE (slots);
3448 bitmap_obstack_release (NULL);
3451 /* We may have bounds not associated with any pointer. */
3452 if (bnd_no != -1)
3453 offs = bnd_no * POINTER_SIZE / BITS_PER_UNIT;
3455 /* Find associated pointer. */
3456 if (bnd_no == -1)
3458 /* If bounds are not associated with any bounds,
3459 then it is passed in a register or special slot. */
3460 gcc_assert (data->entry_parm);
3461 ptr = const0_rtx;
3463 else if (MEM_P (entry))
3464 slot = adjust_address (entry, Pmode, offs);
3465 else if (REG_P (entry))
3466 ptr = gen_rtx_REG (Pmode, REGNO (entry) + bnd_no);
3467 else if (GET_CODE (entry) == PARALLEL)
3468 ptr = chkp_get_value_with_offs (entry, GEN_INT (offs));
3469 else
3470 gcc_unreachable ();
3471 data->entry_parm = targetm.calls.load_bounds_for_arg (slot, ptr,
3472 data->entry_parm);
3475 /* Assign RTL expressions to the function's bounds parameters BNDARGS. */
3477 static void
3478 assign_bounds (vec<bounds_parm_data> &bndargs,
3479 struct assign_parm_data_all &all)
3481 unsigned i, pass, handled = 0;
3482 bounds_parm_data *pbdata;
3484 if (!bndargs.exists ())
3485 return;
3487 /* We make few passes to store input bounds. Firstly handle bounds
3488 passed in registers. After that we load bounds passed in special
3489 slots. Finally we load bounds from Bounds Table. */
3490 for (pass = 0; pass < 3; pass++)
3491 FOR_EACH_VEC_ELT (bndargs, i, pbdata)
3493 /* Pass 0 => regs only. */
3494 if (pass == 0
3495 && (!pbdata->parm_data.entry_parm
3496 || GET_CODE (pbdata->parm_data.entry_parm) != REG))
3497 continue;
3498 /* Pass 1 => slots only. */
3499 else if (pass == 1
3500 && (!pbdata->parm_data.entry_parm
3501 || GET_CODE (pbdata->parm_data.entry_parm) == REG))
3502 continue;
3503 /* Pass 2 => BT only. */
3504 else if (pass == 2
3505 && pbdata->parm_data.entry_parm)
3506 continue;
3508 if (!pbdata->parm_data.entry_parm
3509 || GET_CODE (pbdata->parm_data.entry_parm) != REG)
3510 assign_parm_load_bounds (&pbdata->parm_data, pbdata->ptr_parm,
3511 pbdata->ptr_entry, pbdata->bound_no);
3513 set_decl_incoming_rtl (pbdata->bounds_parm,
3514 pbdata->parm_data.entry_parm, false);
3516 if (assign_parm_setup_block_p (&pbdata->parm_data))
3517 assign_parm_setup_block (&all, pbdata->bounds_parm,
3518 &pbdata->parm_data);
3519 else if (pbdata->parm_data.passed_pointer
3520 || use_register_for_decl (pbdata->bounds_parm))
3521 assign_parm_setup_reg (&all, pbdata->bounds_parm,
3522 &pbdata->parm_data);
3523 else
3524 assign_parm_setup_stack (&all, pbdata->bounds_parm,
3525 &pbdata->parm_data);
3527 /* Count handled bounds to make sure we miss nothing. */
3528 handled++;
3531 gcc_assert (handled == bndargs.length ());
3533 bndargs.release ();
3536 /* Assign RTL expressions to the function's parameters. This may involve
3537 copying them into registers and using those registers as the DECL_RTL. */
3539 static void
3540 assign_parms (tree fndecl)
3542 struct assign_parm_data_all all;
3543 tree parm;
3544 vec<tree> fnargs;
3545 unsigned i, bound_no = 0;
3546 tree last_arg = NULL;
3547 rtx last_arg_entry = NULL;
3548 vec<bounds_parm_data> bndargs = vNULL;
3549 bounds_parm_data bdata;
3551 crtl->args.internal_arg_pointer
3552 = targetm.calls.internal_arg_pointer ();
3554 assign_parms_initialize_all (&all);
3555 fnargs = assign_parms_augmented_arg_list (&all);
3557 FOR_EACH_VEC_ELT (fnargs, i, parm)
3559 struct assign_parm_data_one data;
3561 /* Extract the type of PARM; adjust it according to ABI. */
3562 assign_parm_find_data_types (&all, parm, &data);
3564 /* Early out for errors and void parameters. */
3565 if (data.passed_mode == VOIDmode)
3567 SET_DECL_RTL (parm, const0_rtx);
3568 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3569 continue;
3572 /* Estimate stack alignment from parameter alignment. */
3573 if (SUPPORTS_STACK_ALIGNMENT)
3575 unsigned int align
3576 = targetm.calls.function_arg_boundary (data.promoted_mode,
3577 data.passed_type);
3578 align = MINIMUM_ALIGNMENT (data.passed_type, data.promoted_mode,
3579 align);
3580 if (TYPE_ALIGN (data.nominal_type) > align)
3581 align = MINIMUM_ALIGNMENT (data.nominal_type,
3582 TYPE_MODE (data.nominal_type),
3583 TYPE_ALIGN (data.nominal_type));
3584 if (crtl->stack_alignment_estimated < align)
3586 gcc_assert (!crtl->stack_realign_processed);
3587 crtl->stack_alignment_estimated = align;
3591 /* Find out where the parameter arrives in this function. */
3592 assign_parm_find_entry_rtl (&all, &data);
3594 /* Find out where stack space for this parameter might be. */
3595 if (assign_parm_is_stack_parm (&all, &data))
3597 assign_parm_find_stack_rtl (parm, &data);
3598 assign_parm_adjust_entry_rtl (&data);
3600 if (!POINTER_BOUNDS_TYPE_P (data.passed_type))
3602 /* Remember where last non bounds arg was passed in case
3603 we have to load associated bounds for it from Bounds
3604 Table. */
3605 last_arg = parm;
3606 last_arg_entry = data.entry_parm;
3607 bound_no = 0;
3609 /* Record permanently how this parm was passed. */
3610 if (data.passed_pointer)
3612 rtx incoming_rtl
3613 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.passed_type)),
3614 data.entry_parm);
3615 set_decl_incoming_rtl (parm, incoming_rtl, true);
3617 else
3618 set_decl_incoming_rtl (parm, data.entry_parm, false);
3620 /* Boudns should be loaded in the particular order to
3621 have registers allocated correctly. Collect info about
3622 input bounds and load them later. */
3623 if (POINTER_BOUNDS_TYPE_P (data.passed_type))
3625 /* Expect bounds in instrumented functions only. */
3626 gcc_assert (chkp_function_instrumented_p (fndecl));
3628 bdata.parm_data = data;
3629 bdata.bounds_parm = parm;
3630 bdata.ptr_parm = last_arg;
3631 bdata.ptr_entry = last_arg_entry;
3632 bdata.bound_no = bound_no;
3633 bndargs.safe_push (bdata);
3635 else
3637 assign_parm_adjust_stack_rtl (&data);
3639 if (assign_parm_setup_block_p (&data))
3640 assign_parm_setup_block (&all, parm, &data);
3641 else if (data.passed_pointer || use_register_for_decl (parm))
3642 assign_parm_setup_reg (&all, parm, &data);
3643 else
3644 assign_parm_setup_stack (&all, parm, &data);
3647 if (cfun->stdarg && !DECL_CHAIN (parm))
3649 int pretend_bytes = 0;
3651 assign_parms_setup_varargs (&all, &data, false);
3653 if (chkp_function_instrumented_p (fndecl))
3655 /* We expect this is the last parm. Otherwise it is wrong
3656 to assign bounds right now. */
3657 gcc_assert (i == (fnargs.length () - 1));
3658 assign_bounds (bndargs, all);
3659 targetm.calls.setup_incoming_vararg_bounds (all.args_so_far,
3660 data.promoted_mode,
3661 data.passed_type,
3662 &pretend_bytes,
3663 false);
3667 /* Update info on where next arg arrives in registers. */
3668 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3669 data.passed_type, data.named_arg);
3671 if (POINTER_BOUNDS_TYPE_P (data.passed_type))
3672 bound_no++;
3675 assign_bounds (bndargs, all);
3677 if (targetm.calls.split_complex_arg)
3678 assign_parms_unsplit_complex (&all, fnargs);
3680 fnargs.release ();
3682 /* Initialize pic_offset_table_rtx with a pseudo register
3683 if required. */
3684 if (targetm.use_pseudo_pic_reg ())
3685 pic_offset_table_rtx = gen_reg_rtx (Pmode);
3687 /* Output all parameter conversion instructions (possibly including calls)
3688 now that all parameters have been copied out of hard registers. */
3689 emit_insn (all.first_conversion_insn);
3691 /* Estimate reload stack alignment from scalar return mode. */
3692 if (SUPPORTS_STACK_ALIGNMENT)
3694 if (DECL_RESULT (fndecl))
3696 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3697 machine_mode mode = TYPE_MODE (type);
3699 if (mode != BLKmode
3700 && mode != VOIDmode
3701 && !AGGREGATE_TYPE_P (type))
3703 unsigned int align = GET_MODE_ALIGNMENT (mode);
3704 if (crtl->stack_alignment_estimated < align)
3706 gcc_assert (!crtl->stack_realign_processed);
3707 crtl->stack_alignment_estimated = align;
3713 /* If we are receiving a struct value address as the first argument, set up
3714 the RTL for the function result. As this might require code to convert
3715 the transmitted address to Pmode, we do this here to ensure that possible
3716 preliminary conversions of the address have been emitted already. */
3717 if (all.function_result_decl)
3719 tree result = DECL_RESULT (current_function_decl);
3720 rtx addr = DECL_RTL (all.function_result_decl);
3721 rtx x;
3723 if (DECL_BY_REFERENCE (result))
3725 SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3726 x = addr;
3728 else
3730 SET_DECL_VALUE_EXPR (result,
3731 build1 (INDIRECT_REF, TREE_TYPE (result),
3732 all.function_result_decl));
3733 addr = convert_memory_address (Pmode, addr);
3734 x = gen_rtx_MEM (DECL_MODE (result), addr);
3735 set_mem_attributes (x, result, 1);
3738 DECL_HAS_VALUE_EXPR_P (result) = 1;
3740 SET_DECL_RTL (result, x);
3743 /* We have aligned all the args, so add space for the pretend args. */
3744 crtl->args.pretend_args_size = all.pretend_args_size;
3745 all.stack_args_size.constant += all.extra_pretend_bytes;
3746 crtl->args.size = all.stack_args_size.constant;
3748 /* Adjust function incoming argument size for alignment and
3749 minimum length. */
3751 crtl->args.size = MAX (crtl->args.size, all.reg_parm_stack_space);
3752 crtl->args.size = CEIL_ROUND (crtl->args.size,
3753 PARM_BOUNDARY / BITS_PER_UNIT);
3755 #ifdef ARGS_GROW_DOWNWARD
3756 crtl->args.arg_offset_rtx
3757 = (all.stack_args_size.var == 0 ? GEN_INT (-all.stack_args_size.constant)
3758 : expand_expr (size_diffop (all.stack_args_size.var,
3759 size_int (-all.stack_args_size.constant)),
3760 NULL_RTX, VOIDmode, EXPAND_NORMAL));
3761 #else
3762 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3763 #endif
3765 /* See how many bytes, if any, of its args a function should try to pop
3766 on return. */
3768 crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3769 TREE_TYPE (fndecl),
3770 crtl->args.size);
3772 /* For stdarg.h function, save info about
3773 regs and stack space used by the named args. */
3775 crtl->args.info = all.args_so_far_v;
3777 /* Set the rtx used for the function return value. Put this in its
3778 own variable so any optimizers that need this information don't have
3779 to include tree.h. Do this here so it gets done when an inlined
3780 function gets output. */
3782 crtl->return_rtx
3783 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3784 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3786 /* If scalar return value was computed in a pseudo-reg, or was a named
3787 return value that got dumped to the stack, copy that to the hard
3788 return register. */
3789 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3791 tree decl_result = DECL_RESULT (fndecl);
3792 rtx decl_rtl = DECL_RTL (decl_result);
3794 if (REG_P (decl_rtl)
3795 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3796 : DECL_REGISTER (decl_result))
3798 rtx real_decl_rtl;
3800 real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3801 fndecl, true);
3802 if (chkp_function_instrumented_p (fndecl))
3803 crtl->return_bnd
3804 = targetm.calls.chkp_function_value_bounds (TREE_TYPE (decl_result),
3805 fndecl, true);
3806 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3807 /* The delay slot scheduler assumes that crtl->return_rtx
3808 holds the hard register containing the return value, not a
3809 temporary pseudo. */
3810 crtl->return_rtx = real_decl_rtl;
3815 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3816 For all seen types, gimplify their sizes. */
3818 static tree
3819 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3821 tree t = *tp;
3823 *walk_subtrees = 0;
3824 if (TYPE_P (t))
3826 if (POINTER_TYPE_P (t))
3827 *walk_subtrees = 1;
3828 else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3829 && !TYPE_SIZES_GIMPLIFIED (t))
3831 gimplify_type_sizes (t, (gimple_seq *) data);
3832 *walk_subtrees = 1;
3836 return NULL;
3839 /* Gimplify the parameter list for current_function_decl. This involves
3840 evaluating SAVE_EXPRs of variable sized parameters and generating code
3841 to implement callee-copies reference parameters. Returns a sequence of
3842 statements to add to the beginning of the function. */
3844 gimple_seq
3845 gimplify_parameters (void)
3847 struct assign_parm_data_all all;
3848 tree parm;
3849 gimple_seq stmts = NULL;
3850 vec<tree> fnargs;
3851 unsigned i;
3853 assign_parms_initialize_all (&all);
3854 fnargs = assign_parms_augmented_arg_list (&all);
3856 FOR_EACH_VEC_ELT (fnargs, i, parm)
3858 struct assign_parm_data_one data;
3860 /* Extract the type of PARM; adjust it according to ABI. */
3861 assign_parm_find_data_types (&all, parm, &data);
3863 /* Early out for errors and void parameters. */
3864 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3865 continue;
3867 /* Update info on where next arg arrives in registers. */
3868 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3869 data.passed_type, data.named_arg);
3871 /* ??? Once upon a time variable_size stuffed parameter list
3872 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3873 turned out to be less than manageable in the gimple world.
3874 Now we have to hunt them down ourselves. */
3875 walk_tree_without_duplicates (&data.passed_type,
3876 gimplify_parm_type, &stmts);
3878 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3880 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3881 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3884 if (data.passed_pointer)
3886 tree type = TREE_TYPE (data.passed_type);
3887 if (reference_callee_copied (&all.args_so_far_v, TYPE_MODE (type),
3888 type, data.named_arg))
3890 tree local, t;
3892 /* For constant-sized objects, this is trivial; for
3893 variable-sized objects, we have to play games. */
3894 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3895 && !(flag_stack_check == GENERIC_STACK_CHECK
3896 && compare_tree_int (DECL_SIZE_UNIT (parm),
3897 STACK_CHECK_MAX_VAR_SIZE) > 0))
3899 local = create_tmp_var (type, get_name (parm));
3900 DECL_IGNORED_P (local) = 0;
3901 /* If PARM was addressable, move that flag over
3902 to the local copy, as its address will be taken,
3903 not the PARMs. Keep the parms address taken
3904 as we'll query that flag during gimplification. */
3905 if (TREE_ADDRESSABLE (parm))
3906 TREE_ADDRESSABLE (local) = 1;
3907 else if (TREE_CODE (type) == COMPLEX_TYPE
3908 || TREE_CODE (type) == VECTOR_TYPE)
3909 DECL_GIMPLE_REG_P (local) = 1;
3911 else
3913 tree ptr_type, addr;
3915 ptr_type = build_pointer_type (type);
3916 addr = create_tmp_reg (ptr_type, get_name (parm));
3917 DECL_IGNORED_P (addr) = 0;
3918 local = build_fold_indirect_ref (addr);
3920 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
3921 t = build_call_expr (t, 2, DECL_SIZE_UNIT (parm),
3922 size_int (DECL_ALIGN (parm)));
3924 /* The call has been built for a variable-sized object. */
3925 CALL_ALLOCA_FOR_VAR_P (t) = 1;
3926 t = fold_convert (ptr_type, t);
3927 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
3928 gimplify_and_add (t, &stmts);
3931 gimplify_assign (local, parm, &stmts);
3933 SET_DECL_VALUE_EXPR (parm, local);
3934 DECL_HAS_VALUE_EXPR_P (parm) = 1;
3939 fnargs.release ();
3941 return stmts;
3944 /* Compute the size and offset from the start of the stacked arguments for a
3945 parm passed in mode PASSED_MODE and with type TYPE.
3947 INITIAL_OFFSET_PTR points to the current offset into the stacked
3948 arguments.
3950 The starting offset and size for this parm are returned in
3951 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
3952 nonzero, the offset is that of stack slot, which is returned in
3953 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
3954 padding required from the initial offset ptr to the stack slot.
3956 IN_REGS is nonzero if the argument will be passed in registers. It will
3957 never be set if REG_PARM_STACK_SPACE is not defined.
3959 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
3960 for arguments which are passed in registers.
3962 FNDECL is the function in which the argument was defined.
3964 There are two types of rounding that are done. The first, controlled by
3965 TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
3966 argument list to be aligned to the specific boundary (in bits). This
3967 rounding affects the initial and starting offsets, but not the argument
3968 size.
3970 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
3971 optionally rounds the size of the parm to PARM_BOUNDARY. The
3972 initial offset is not affected by this rounding, while the size always
3973 is and the starting offset may be. */
3975 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
3976 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
3977 callers pass in the total size of args so far as
3978 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
3980 void
3981 locate_and_pad_parm (machine_mode passed_mode, tree type, int in_regs,
3982 int reg_parm_stack_space, int partial,
3983 tree fndecl ATTRIBUTE_UNUSED,
3984 struct args_size *initial_offset_ptr,
3985 struct locate_and_pad_arg_data *locate)
3987 tree sizetree;
3988 enum direction where_pad;
3989 unsigned int boundary, round_boundary;
3990 int part_size_in_regs;
3992 /* If we have found a stack parm before we reach the end of the
3993 area reserved for registers, skip that area. */
3994 if (! in_regs)
3996 if (reg_parm_stack_space > 0)
3998 if (initial_offset_ptr->var)
4000 initial_offset_ptr->var
4001 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
4002 ssize_int (reg_parm_stack_space));
4003 initial_offset_ptr->constant = 0;
4005 else if (initial_offset_ptr->constant < reg_parm_stack_space)
4006 initial_offset_ptr->constant = reg_parm_stack_space;
4010 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
4012 sizetree
4013 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
4014 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
4015 boundary = targetm.calls.function_arg_boundary (passed_mode, type);
4016 round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
4017 type);
4018 locate->where_pad = where_pad;
4020 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
4021 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4022 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4024 locate->boundary = boundary;
4026 if (SUPPORTS_STACK_ALIGNMENT)
4028 /* stack_alignment_estimated can't change after stack has been
4029 realigned. */
4030 if (crtl->stack_alignment_estimated < boundary)
4032 if (!crtl->stack_realign_processed)
4033 crtl->stack_alignment_estimated = boundary;
4034 else
4036 /* If stack is realigned and stack alignment value
4037 hasn't been finalized, it is OK not to increase
4038 stack_alignment_estimated. The bigger alignment
4039 requirement is recorded in stack_alignment_needed
4040 below. */
4041 gcc_assert (!crtl->stack_realign_finalized
4042 && crtl->stack_realign_needed);
4047 /* Remember if the outgoing parameter requires extra alignment on the
4048 calling function side. */
4049 if (crtl->stack_alignment_needed < boundary)
4050 crtl->stack_alignment_needed = boundary;
4051 if (crtl->preferred_stack_boundary < boundary)
4052 crtl->preferred_stack_boundary = boundary;
4054 #ifdef ARGS_GROW_DOWNWARD
4055 locate->slot_offset.constant = -initial_offset_ptr->constant;
4056 if (initial_offset_ptr->var)
4057 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
4058 initial_offset_ptr->var);
4061 tree s2 = sizetree;
4062 if (where_pad != none
4063 && (!tree_fits_uhwi_p (sizetree)
4064 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4065 s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
4066 SUB_PARM_SIZE (locate->slot_offset, s2);
4069 locate->slot_offset.constant += part_size_in_regs;
4071 if (!in_regs || reg_parm_stack_space > 0)
4072 pad_to_arg_alignment (&locate->slot_offset, boundary,
4073 &locate->alignment_pad);
4075 locate->size.constant = (-initial_offset_ptr->constant
4076 - locate->slot_offset.constant);
4077 if (initial_offset_ptr->var)
4078 locate->size.var = size_binop (MINUS_EXPR,
4079 size_binop (MINUS_EXPR,
4080 ssize_int (0),
4081 initial_offset_ptr->var),
4082 locate->slot_offset.var);
4084 /* Pad_below needs the pre-rounded size to know how much to pad
4085 below. */
4086 locate->offset = locate->slot_offset;
4087 if (where_pad == downward)
4088 pad_below (&locate->offset, passed_mode, sizetree);
4090 #else /* !ARGS_GROW_DOWNWARD */
4091 if (!in_regs || reg_parm_stack_space > 0)
4092 pad_to_arg_alignment (initial_offset_ptr, boundary,
4093 &locate->alignment_pad);
4094 locate->slot_offset = *initial_offset_ptr;
4096 #ifdef PUSH_ROUNDING
4097 if (passed_mode != BLKmode)
4098 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
4099 #endif
4101 /* Pad_below needs the pre-rounded size to know how much to pad below
4102 so this must be done before rounding up. */
4103 locate->offset = locate->slot_offset;
4104 if (where_pad == downward)
4105 pad_below (&locate->offset, passed_mode, sizetree);
4107 if (where_pad != none
4108 && (!tree_fits_uhwi_p (sizetree)
4109 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4110 sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
4112 ADD_PARM_SIZE (locate->size, sizetree);
4114 locate->size.constant -= part_size_in_regs;
4115 #endif /* ARGS_GROW_DOWNWARD */
4117 #ifdef FUNCTION_ARG_OFFSET
4118 locate->offset.constant += FUNCTION_ARG_OFFSET (passed_mode, type);
4119 #endif
4122 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
4123 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
4125 static void
4126 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
4127 struct args_size *alignment_pad)
4129 tree save_var = NULL_TREE;
4130 HOST_WIDE_INT save_constant = 0;
4131 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4132 HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
4134 #ifdef SPARC_STACK_BOUNDARY_HACK
4135 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
4136 the real alignment of %sp. However, when it does this, the
4137 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
4138 if (SPARC_STACK_BOUNDARY_HACK)
4139 sp_offset = 0;
4140 #endif
4142 if (boundary > PARM_BOUNDARY)
4144 save_var = offset_ptr->var;
4145 save_constant = offset_ptr->constant;
4148 alignment_pad->var = NULL_TREE;
4149 alignment_pad->constant = 0;
4151 if (boundary > BITS_PER_UNIT)
4153 if (offset_ptr->var)
4155 tree sp_offset_tree = ssize_int (sp_offset);
4156 tree offset = size_binop (PLUS_EXPR,
4157 ARGS_SIZE_TREE (*offset_ptr),
4158 sp_offset_tree);
4159 #ifdef ARGS_GROW_DOWNWARD
4160 tree rounded = round_down (offset, boundary / BITS_PER_UNIT);
4161 #else
4162 tree rounded = round_up (offset, boundary / BITS_PER_UNIT);
4163 #endif
4165 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
4166 /* ARGS_SIZE_TREE includes constant term. */
4167 offset_ptr->constant = 0;
4168 if (boundary > PARM_BOUNDARY)
4169 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
4170 save_var);
4172 else
4174 offset_ptr->constant = -sp_offset +
4175 #ifdef ARGS_GROW_DOWNWARD
4176 FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
4177 #else
4178 CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
4179 #endif
4180 if (boundary > PARM_BOUNDARY)
4181 alignment_pad->constant = offset_ptr->constant - save_constant;
4186 static void
4187 pad_below (struct args_size *offset_ptr, machine_mode passed_mode, tree sizetree)
4189 if (passed_mode != BLKmode)
4191 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
4192 offset_ptr->constant
4193 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
4194 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
4195 - GET_MODE_SIZE (passed_mode));
4197 else
4199 if (TREE_CODE (sizetree) != INTEGER_CST
4200 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
4202 /* Round the size up to multiple of PARM_BOUNDARY bits. */
4203 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4204 /* Add it in. */
4205 ADD_PARM_SIZE (*offset_ptr, s2);
4206 SUB_PARM_SIZE (*offset_ptr, sizetree);
4212 /* True if register REGNO was alive at a place where `setjmp' was
4213 called and was set more than once or is an argument. Such regs may
4214 be clobbered by `longjmp'. */
4216 static bool
4217 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
4219 /* There appear to be cases where some local vars never reach the
4220 backend but have bogus regnos. */
4221 if (regno >= max_reg_num ())
4222 return false;
4224 return ((REG_N_SETS (regno) > 1
4225 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
4226 regno))
4227 && REGNO_REG_SET_P (setjmp_crosses, regno));
4230 /* Walk the tree of blocks describing the binding levels within a
4231 function and warn about variables the might be killed by setjmp or
4232 vfork. This is done after calling flow_analysis before register
4233 allocation since that will clobber the pseudo-regs to hard
4234 regs. */
4236 static void
4237 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
4239 tree decl, sub;
4241 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
4243 if (TREE_CODE (decl) == VAR_DECL
4244 && DECL_RTL_SET_P (decl)
4245 && REG_P (DECL_RTL (decl))
4246 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4247 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
4248 " %<longjmp%> or %<vfork%>", decl);
4251 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
4252 setjmp_vars_warning (setjmp_crosses, sub);
4255 /* Do the appropriate part of setjmp_vars_warning
4256 but for arguments instead of local variables. */
4258 static void
4259 setjmp_args_warning (bitmap setjmp_crosses)
4261 tree decl;
4262 for (decl = DECL_ARGUMENTS (current_function_decl);
4263 decl; decl = DECL_CHAIN (decl))
4264 if (DECL_RTL (decl) != 0
4265 && REG_P (DECL_RTL (decl))
4266 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4267 warning (OPT_Wclobbered,
4268 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
4269 decl);
4272 /* Generate warning messages for variables live across setjmp. */
4274 void
4275 generate_setjmp_warnings (void)
4277 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4279 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS
4280 || bitmap_empty_p (setjmp_crosses))
4281 return;
4283 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4284 setjmp_args_warning (setjmp_crosses);
4288 /* Reverse the order of elements in the fragment chain T of blocks,
4289 and return the new head of the chain (old last element).
4290 In addition to that clear BLOCK_SAME_RANGE flags when needed
4291 and adjust BLOCK_SUPERCONTEXT from the super fragment to
4292 its super fragment origin. */
4294 static tree
4295 block_fragments_nreverse (tree t)
4297 tree prev = 0, block, next, prev_super = 0;
4298 tree super = BLOCK_SUPERCONTEXT (t);
4299 if (BLOCK_FRAGMENT_ORIGIN (super))
4300 super = BLOCK_FRAGMENT_ORIGIN (super);
4301 for (block = t; block; block = next)
4303 next = BLOCK_FRAGMENT_CHAIN (block);
4304 BLOCK_FRAGMENT_CHAIN (block) = prev;
4305 if ((prev && !BLOCK_SAME_RANGE (prev))
4306 || (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (block))
4307 != prev_super))
4308 BLOCK_SAME_RANGE (block) = 0;
4309 prev_super = BLOCK_SUPERCONTEXT (block);
4310 BLOCK_SUPERCONTEXT (block) = super;
4311 prev = block;
4313 t = BLOCK_FRAGMENT_ORIGIN (t);
4314 if (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (t))
4315 != prev_super)
4316 BLOCK_SAME_RANGE (t) = 0;
4317 BLOCK_SUPERCONTEXT (t) = super;
4318 return prev;
4321 /* Reverse the order of elements in the chain T of blocks,
4322 and return the new head of the chain (old last element).
4323 Also do the same on subblocks and reverse the order of elements
4324 in BLOCK_FRAGMENT_CHAIN as well. */
4326 static tree
4327 blocks_nreverse_all (tree t)
4329 tree prev = 0, block, next;
4330 for (block = t; block; block = next)
4332 next = BLOCK_CHAIN (block);
4333 BLOCK_CHAIN (block) = prev;
4334 if (BLOCK_FRAGMENT_CHAIN (block)
4335 && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4337 BLOCK_FRAGMENT_CHAIN (block)
4338 = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4339 if (!BLOCK_SAME_RANGE (BLOCK_FRAGMENT_CHAIN (block)))
4340 BLOCK_SAME_RANGE (block) = 0;
4342 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4343 prev = block;
4345 return prev;
4349 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4350 and create duplicate blocks. */
4351 /* ??? Need an option to either create block fragments or to create
4352 abstract origin duplicates of a source block. It really depends
4353 on what optimization has been performed. */
4355 void
4356 reorder_blocks (void)
4358 tree block = DECL_INITIAL (current_function_decl);
4360 if (block == NULL_TREE)
4361 return;
4363 auto_vec<tree, 10> block_stack;
4365 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4366 clear_block_marks (block);
4368 /* Prune the old trees away, so that they don't get in the way. */
4369 BLOCK_SUBBLOCKS (block) = NULL_TREE;
4370 BLOCK_CHAIN (block) = NULL_TREE;
4372 /* Recreate the block tree from the note nesting. */
4373 reorder_blocks_1 (get_insns (), block, &block_stack);
4374 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4377 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
4379 void
4380 clear_block_marks (tree block)
4382 while (block)
4384 TREE_ASM_WRITTEN (block) = 0;
4385 clear_block_marks (BLOCK_SUBBLOCKS (block));
4386 block = BLOCK_CHAIN (block);
4390 static void
4391 reorder_blocks_1 (rtx_insn *insns, tree current_block,
4392 vec<tree> *p_block_stack)
4394 rtx_insn *insn;
4395 tree prev_beg = NULL_TREE, prev_end = NULL_TREE;
4397 for (insn = insns; insn; insn = NEXT_INSN (insn))
4399 if (NOTE_P (insn))
4401 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4403 tree block = NOTE_BLOCK (insn);
4404 tree origin;
4406 gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4407 origin = block;
4409 if (prev_end)
4410 BLOCK_SAME_RANGE (prev_end) = 0;
4411 prev_end = NULL_TREE;
4413 /* If we have seen this block before, that means it now
4414 spans multiple address regions. Create a new fragment. */
4415 if (TREE_ASM_WRITTEN (block))
4417 tree new_block = copy_node (block);
4419 BLOCK_SAME_RANGE (new_block) = 0;
4420 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4421 BLOCK_FRAGMENT_CHAIN (new_block)
4422 = BLOCK_FRAGMENT_CHAIN (origin);
4423 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4425 NOTE_BLOCK (insn) = new_block;
4426 block = new_block;
4429 if (prev_beg == current_block && prev_beg)
4430 BLOCK_SAME_RANGE (block) = 1;
4432 prev_beg = origin;
4434 BLOCK_SUBBLOCKS (block) = 0;
4435 TREE_ASM_WRITTEN (block) = 1;
4436 /* When there's only one block for the entire function,
4437 current_block == block and we mustn't do this, it
4438 will cause infinite recursion. */
4439 if (block != current_block)
4441 tree super;
4442 if (block != origin)
4443 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block
4444 || BLOCK_FRAGMENT_ORIGIN (BLOCK_SUPERCONTEXT
4445 (origin))
4446 == current_block);
4447 if (p_block_stack->is_empty ())
4448 super = current_block;
4449 else
4451 super = p_block_stack->last ();
4452 gcc_assert (super == current_block
4453 || BLOCK_FRAGMENT_ORIGIN (super)
4454 == current_block);
4456 BLOCK_SUPERCONTEXT (block) = super;
4457 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4458 BLOCK_SUBBLOCKS (current_block) = block;
4459 current_block = origin;
4461 p_block_stack->safe_push (block);
4463 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4465 NOTE_BLOCK (insn) = p_block_stack->pop ();
4466 current_block = BLOCK_SUPERCONTEXT (current_block);
4467 if (BLOCK_FRAGMENT_ORIGIN (current_block))
4468 current_block = BLOCK_FRAGMENT_ORIGIN (current_block);
4469 prev_beg = NULL_TREE;
4470 prev_end = BLOCK_SAME_RANGE (NOTE_BLOCK (insn))
4471 ? NOTE_BLOCK (insn) : NULL_TREE;
4474 else
4476 prev_beg = NULL_TREE;
4477 if (prev_end)
4478 BLOCK_SAME_RANGE (prev_end) = 0;
4479 prev_end = NULL_TREE;
4484 /* Reverse the order of elements in the chain T of blocks,
4485 and return the new head of the chain (old last element). */
4487 tree
4488 blocks_nreverse (tree t)
4490 tree prev = 0, block, next;
4491 for (block = t; block; block = next)
4493 next = BLOCK_CHAIN (block);
4494 BLOCK_CHAIN (block) = prev;
4495 prev = block;
4497 return prev;
4500 /* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4501 by modifying the last node in chain 1 to point to chain 2. */
4503 tree
4504 block_chainon (tree op1, tree op2)
4506 tree t1;
4508 if (!op1)
4509 return op2;
4510 if (!op2)
4511 return op1;
4513 for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4514 continue;
4515 BLOCK_CHAIN (t1) = op2;
4517 #ifdef ENABLE_TREE_CHECKING
4519 tree t2;
4520 for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4521 gcc_assert (t2 != t1);
4523 #endif
4525 return op1;
4528 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
4529 non-NULL, list them all into VECTOR, in a depth-first preorder
4530 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
4531 blocks. */
4533 static int
4534 all_blocks (tree block, tree *vector)
4536 int n_blocks = 0;
4538 while (block)
4540 TREE_ASM_WRITTEN (block) = 0;
4542 /* Record this block. */
4543 if (vector)
4544 vector[n_blocks] = block;
4546 ++n_blocks;
4548 /* Record the subblocks, and their subblocks... */
4549 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4550 vector ? vector + n_blocks : 0);
4551 block = BLOCK_CHAIN (block);
4554 return n_blocks;
4557 /* Return a vector containing all the blocks rooted at BLOCK. The
4558 number of elements in the vector is stored in N_BLOCKS_P. The
4559 vector is dynamically allocated; it is the caller's responsibility
4560 to call `free' on the pointer returned. */
4562 static tree *
4563 get_block_vector (tree block, int *n_blocks_p)
4565 tree *block_vector;
4567 *n_blocks_p = all_blocks (block, NULL);
4568 block_vector = XNEWVEC (tree, *n_blocks_p);
4569 all_blocks (block, block_vector);
4571 return block_vector;
4574 static GTY(()) int next_block_index = 2;
4576 /* Set BLOCK_NUMBER for all the blocks in FN. */
4578 void
4579 number_blocks (tree fn)
4581 int i;
4582 int n_blocks;
4583 tree *block_vector;
4585 /* For SDB and XCOFF debugging output, we start numbering the blocks
4586 from 1 within each function, rather than keeping a running
4587 count. */
4588 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
4589 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
4590 next_block_index = 1;
4591 #endif
4593 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4595 /* The top-level BLOCK isn't numbered at all. */
4596 for (i = 1; i < n_blocks; ++i)
4597 /* We number the blocks from two. */
4598 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4600 free (block_vector);
4602 return;
4605 /* If VAR is present in a subblock of BLOCK, return the subblock. */
4607 DEBUG_FUNCTION tree
4608 debug_find_var_in_block_tree (tree var, tree block)
4610 tree t;
4612 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4613 if (t == var)
4614 return block;
4616 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4618 tree ret = debug_find_var_in_block_tree (var, t);
4619 if (ret)
4620 return ret;
4623 return NULL_TREE;
4626 /* Keep track of whether we're in a dummy function context. If we are,
4627 we don't want to invoke the set_current_function hook, because we'll
4628 get into trouble if the hook calls target_reinit () recursively or
4629 when the initial initialization is not yet complete. */
4631 static bool in_dummy_function;
4633 /* Invoke the target hook when setting cfun. Update the optimization options
4634 if the function uses different options than the default. */
4636 static void
4637 invoke_set_current_function_hook (tree fndecl)
4639 if (!in_dummy_function)
4641 tree opts = ((fndecl)
4642 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4643 : optimization_default_node);
4645 if (!opts)
4646 opts = optimization_default_node;
4648 /* Change optimization options if needed. */
4649 if (optimization_current_node != opts)
4651 optimization_current_node = opts;
4652 cl_optimization_restore (&global_options, TREE_OPTIMIZATION (opts));
4655 targetm.set_current_function (fndecl);
4656 this_fn_optabs = this_target_optabs;
4658 if (opts != optimization_default_node)
4660 init_tree_optimization_optabs (opts);
4661 if (TREE_OPTIMIZATION_OPTABS (opts))
4662 this_fn_optabs = (struct target_optabs *)
4663 TREE_OPTIMIZATION_OPTABS (opts);
4668 /* cfun should never be set directly; use this function. */
4670 void
4671 set_cfun (struct function *new_cfun)
4673 if (cfun != new_cfun)
4675 cfun = new_cfun;
4676 invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4680 /* Initialized with NOGC, making this poisonous to the garbage collector. */
4682 static vec<function_p> cfun_stack;
4684 /* Push the current cfun onto the stack, and set cfun to new_cfun. Also set
4685 current_function_decl accordingly. */
4687 void
4688 push_cfun (struct function *new_cfun)
4690 gcc_assert ((!cfun && !current_function_decl)
4691 || (cfun && current_function_decl == cfun->decl));
4692 cfun_stack.safe_push (cfun);
4693 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4694 set_cfun (new_cfun);
4697 /* Pop cfun from the stack. Also set current_function_decl accordingly. */
4699 void
4700 pop_cfun (void)
4702 struct function *new_cfun = cfun_stack.pop ();
4703 /* When in_dummy_function, we do have a cfun but current_function_decl is
4704 NULL. We also allow pushing NULL cfun and subsequently changing
4705 current_function_decl to something else and have both restored by
4706 pop_cfun. */
4707 gcc_checking_assert (in_dummy_function
4708 || !cfun
4709 || current_function_decl == cfun->decl);
4710 set_cfun (new_cfun);
4711 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4714 /* Return value of funcdef and increase it. */
4716 get_next_funcdef_no (void)
4718 return funcdef_no++;
4721 /* Return value of funcdef. */
4723 get_last_funcdef_no (void)
4725 return funcdef_no;
4728 /* Allocate a function structure for FNDECL and set its contents
4729 to the defaults. Set cfun to the newly-allocated object.
4730 Some of the helper functions invoked during initialization assume
4731 that cfun has already been set. Therefore, assign the new object
4732 directly into cfun and invoke the back end hook explicitly at the
4733 very end, rather than initializing a temporary and calling set_cfun
4734 on it.
4736 ABSTRACT_P is true if this is a function that will never be seen by
4737 the middle-end. Such functions are front-end concepts (like C++
4738 function templates) that do not correspond directly to functions
4739 placed in object files. */
4741 void
4742 allocate_struct_function (tree fndecl, bool abstract_p)
4744 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4746 cfun = ggc_cleared_alloc<function> ();
4748 init_eh_for_function ();
4750 if (init_machine_status)
4751 cfun->machine = (*init_machine_status) ();
4753 #ifdef OVERRIDE_ABI_FORMAT
4754 OVERRIDE_ABI_FORMAT (fndecl);
4755 #endif
4757 if (fndecl != NULL_TREE)
4759 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4760 cfun->decl = fndecl;
4761 current_function_funcdef_no = get_next_funcdef_no ();
4764 invoke_set_current_function_hook (fndecl);
4766 if (fndecl != NULL_TREE)
4768 tree result = DECL_RESULT (fndecl);
4769 if (!abstract_p && aggregate_value_p (result, fndecl))
4771 #ifdef PCC_STATIC_STRUCT_RETURN
4772 cfun->returns_pcc_struct = 1;
4773 #endif
4774 cfun->returns_struct = 1;
4777 cfun->stdarg = stdarg_p (fntype);
4779 /* Assume all registers in stdarg functions need to be saved. */
4780 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4781 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4783 /* ??? This could be set on a per-function basis by the front-end
4784 but is this worth the hassle? */
4785 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4786 cfun->can_delete_dead_exceptions = flag_delete_dead_exceptions;
4788 if (!profile_flag && !flag_instrument_function_entry_exit)
4789 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl) = 1;
4793 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4794 instead of just setting it. */
4796 void
4797 push_struct_function (tree fndecl)
4799 /* When in_dummy_function we might be in the middle of a pop_cfun and
4800 current_function_decl and cfun may not match. */
4801 gcc_assert (in_dummy_function
4802 || (!cfun && !current_function_decl)
4803 || (cfun && current_function_decl == cfun->decl));
4804 cfun_stack.safe_push (cfun);
4805 current_function_decl = fndecl;
4806 allocate_struct_function (fndecl, false);
4809 /* Reset crtl and other non-struct-function variables to defaults as
4810 appropriate for emitting rtl at the start of a function. */
4812 static void
4813 prepare_function_start (void)
4815 gcc_assert (!crtl->emit.x_last_insn);
4816 init_temp_slots ();
4817 init_emit ();
4818 init_varasm_status ();
4819 init_expr ();
4820 default_rtl_profile ();
4822 if (flag_stack_usage_info)
4824 cfun->su = ggc_cleared_alloc<stack_usage> ();
4825 cfun->su->static_stack_size = -1;
4828 cse_not_expected = ! optimize;
4830 /* Caller save not needed yet. */
4831 caller_save_needed = 0;
4833 /* We haven't done register allocation yet. */
4834 reg_renumber = 0;
4836 /* Indicate that we have not instantiated virtual registers yet. */
4837 virtuals_instantiated = 0;
4839 /* Indicate that we want CONCATs now. */
4840 generating_concat_p = 1;
4842 /* Indicate we have no need of a frame pointer yet. */
4843 frame_pointer_needed = 0;
4846 /* Initialize the rtl expansion mechanism so that we can do simple things
4847 like generate sequences. This is used to provide a context during global
4848 initialization of some passes. You must call expand_dummy_function_end
4849 to exit this context. */
4851 void
4852 init_dummy_function_start (void)
4854 gcc_assert (!in_dummy_function);
4855 in_dummy_function = true;
4856 push_struct_function (NULL_TREE);
4857 prepare_function_start ();
4860 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4861 and initialize static variables for generating RTL for the statements
4862 of the function. */
4864 void
4865 init_function_start (tree subr)
4867 if (subr && DECL_STRUCT_FUNCTION (subr))
4868 set_cfun (DECL_STRUCT_FUNCTION (subr));
4869 else
4870 allocate_struct_function (subr, false);
4872 /* Initialize backend, if needed. */
4873 initialize_rtl ();
4875 prepare_function_start ();
4876 decide_function_section (subr);
4878 /* Warn if this value is an aggregate type,
4879 regardless of which calling convention we are using for it. */
4880 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4881 warning (OPT_Waggregate_return, "function returns an aggregate");
4884 /* Expand code to verify the stack_protect_guard. This is invoked at
4885 the end of a function to be protected. */
4887 #ifndef HAVE_stack_protect_test
4888 # define HAVE_stack_protect_test 0
4889 # define gen_stack_protect_test(x, y, z) (gcc_unreachable (), NULL_RTX)
4890 #endif
4892 void
4893 stack_protect_epilogue (void)
4895 tree guard_decl = targetm.stack_protect_guard ();
4896 rtx_code_label *label = gen_label_rtx ();
4897 rtx x, y, tmp;
4899 x = expand_normal (crtl->stack_protect_guard);
4900 y = expand_normal (guard_decl);
4902 /* Allow the target to compare Y with X without leaking either into
4903 a register. */
4904 switch ((int) (HAVE_stack_protect_test != 0))
4906 case 1:
4907 tmp = gen_stack_protect_test (x, y, label);
4908 if (tmp)
4910 emit_insn (tmp);
4911 break;
4913 /* FALLTHRU */
4915 default:
4916 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
4917 break;
4920 /* The noreturn predictor has been moved to the tree level. The rtl-level
4921 predictors estimate this branch about 20%, which isn't enough to get
4922 things moved out of line. Since this is the only extant case of adding
4923 a noreturn function at the rtl level, it doesn't seem worth doing ought
4924 except adding the prediction by hand. */
4925 tmp = get_last_insn ();
4926 if (JUMP_P (tmp))
4927 predict_insn_def (as_a <rtx_insn *> (tmp), PRED_NORETURN, TAKEN);
4929 expand_call (targetm.stack_protect_fail (), NULL_RTX, /*ignore=*/true);
4930 free_temp_slots ();
4931 emit_label (label);
4934 /* Start the RTL for a new function, and set variables used for
4935 emitting RTL.
4936 SUBR is the FUNCTION_DECL node.
4937 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
4938 the function's parameters, which must be run at any return statement. */
4940 void
4941 expand_function_start (tree subr)
4943 /* Make sure volatile mem refs aren't considered
4944 valid operands of arithmetic insns. */
4945 init_recog_no_volatile ();
4947 crtl->profile
4948 = (profile_flag
4949 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
4951 crtl->limit_stack
4952 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
4954 /* Make the label for return statements to jump to. Do not special
4955 case machines with special return instructions -- they will be
4956 handled later during jump, ifcvt, or epilogue creation. */
4957 return_label = gen_label_rtx ();
4959 /* Initialize rtx used to return the value. */
4960 /* Do this before assign_parms so that we copy the struct value address
4961 before any library calls that assign parms might generate. */
4963 /* Decide whether to return the value in memory or in a register. */
4964 if (aggregate_value_p (DECL_RESULT (subr), subr))
4966 /* Returning something that won't go in a register. */
4967 rtx value_address = 0;
4969 #ifdef PCC_STATIC_STRUCT_RETURN
4970 if (cfun->returns_pcc_struct)
4972 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
4973 value_address = assemble_static_space (size);
4975 else
4976 #endif
4978 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
4979 /* Expect to be passed the address of a place to store the value.
4980 If it is passed as an argument, assign_parms will take care of
4981 it. */
4982 if (sv)
4984 value_address = gen_reg_rtx (Pmode);
4985 emit_move_insn (value_address, sv);
4988 if (value_address)
4990 rtx x = value_address;
4991 if (!DECL_BY_REFERENCE (DECL_RESULT (subr)))
4993 x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), x);
4994 set_mem_attributes (x, DECL_RESULT (subr), 1);
4996 SET_DECL_RTL (DECL_RESULT (subr), x);
4999 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
5000 /* If return mode is void, this decl rtl should not be used. */
5001 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
5002 else
5004 /* Compute the return values into a pseudo reg, which we will copy
5005 into the true return register after the cleanups are done. */
5006 tree return_type = TREE_TYPE (DECL_RESULT (subr));
5007 if (TYPE_MODE (return_type) != BLKmode
5008 && targetm.calls.return_in_msb (return_type))
5009 /* expand_function_end will insert the appropriate padding in
5010 this case. Use the return value's natural (unpadded) mode
5011 within the function proper. */
5012 SET_DECL_RTL (DECL_RESULT (subr),
5013 gen_reg_rtx (TYPE_MODE (return_type)));
5014 else
5016 /* In order to figure out what mode to use for the pseudo, we
5017 figure out what the mode of the eventual return register will
5018 actually be, and use that. */
5019 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
5021 /* Structures that are returned in registers are not
5022 aggregate_value_p, so we may see a PARALLEL or a REG. */
5023 if (REG_P (hard_reg))
5024 SET_DECL_RTL (DECL_RESULT (subr),
5025 gen_reg_rtx (GET_MODE (hard_reg)));
5026 else
5028 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
5029 SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
5033 /* Set DECL_REGISTER flag so that expand_function_end will copy the
5034 result to the real return register(s). */
5035 DECL_REGISTER (DECL_RESULT (subr)) = 1;
5037 if (chkp_function_instrumented_p (current_function_decl))
5039 tree return_type = TREE_TYPE (DECL_RESULT (subr));
5040 rtx bounds = targetm.calls.chkp_function_value_bounds (return_type,
5041 subr, 1);
5042 SET_DECL_BOUNDS_RTL (DECL_RESULT (subr), bounds);
5046 /* Initialize rtx for parameters and local variables.
5047 In some cases this requires emitting insns. */
5048 assign_parms (subr);
5050 /* If function gets a static chain arg, store it. */
5051 if (cfun->static_chain_decl)
5053 tree parm = cfun->static_chain_decl;
5054 rtx local, chain, insn;
5056 local = gen_reg_rtx (Pmode);
5057 chain = targetm.calls.static_chain (current_function_decl, true);
5059 set_decl_incoming_rtl (parm, chain, false);
5060 SET_DECL_RTL (parm, local);
5061 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
5063 insn = emit_move_insn (local, chain);
5065 /* Mark the register as eliminable, similar to parameters. */
5066 if (MEM_P (chain)
5067 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
5068 set_dst_reg_note (insn, REG_EQUIV, chain, local);
5070 /* If we aren't optimizing, save the static chain onto the stack. */
5071 if (!optimize)
5073 tree saved_static_chain_decl
5074 = build_decl (DECL_SOURCE_LOCATION (parm), VAR_DECL,
5075 DECL_NAME (parm), TREE_TYPE (parm));
5076 rtx saved_static_chain_rtx
5077 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5078 SET_DECL_RTL (saved_static_chain_decl, saved_static_chain_rtx);
5079 emit_move_insn (saved_static_chain_rtx, chain);
5080 SET_DECL_VALUE_EXPR (parm, saved_static_chain_decl);
5081 DECL_HAS_VALUE_EXPR_P (parm) = 1;
5085 /* If the function receives a non-local goto, then store the
5086 bits we need to restore the frame pointer. */
5087 if (cfun->nonlocal_goto_save_area)
5089 tree t_save;
5090 rtx r_save;
5092 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
5093 gcc_assert (DECL_RTL_SET_P (var));
5095 t_save = build4 (ARRAY_REF,
5096 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
5097 cfun->nonlocal_goto_save_area,
5098 integer_zero_node, NULL_TREE, NULL_TREE);
5099 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
5100 gcc_assert (GET_MODE (r_save) == Pmode);
5102 emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
5103 update_nonlocal_goto_save_area ();
5106 /* The following was moved from init_function_start.
5107 The move is supposed to make sdb output more accurate. */
5108 /* Indicate the beginning of the function body,
5109 as opposed to parm setup. */
5110 emit_note (NOTE_INSN_FUNCTION_BEG);
5112 gcc_assert (NOTE_P (get_last_insn ()));
5114 parm_birth_insn = get_last_insn ();
5116 if (crtl->profile)
5118 #ifdef PROFILE_HOOK
5119 PROFILE_HOOK (current_function_funcdef_no);
5120 #endif
5123 /* If we are doing generic stack checking, the probe should go here. */
5124 if (flag_stack_check == GENERIC_STACK_CHECK)
5125 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
5128 /* Undo the effects of init_dummy_function_start. */
5129 void
5130 expand_dummy_function_end (void)
5132 gcc_assert (in_dummy_function);
5134 /* End any sequences that failed to be closed due to syntax errors. */
5135 while (in_sequence_p ())
5136 end_sequence ();
5138 /* Outside function body, can't compute type's actual size
5139 until next function's body starts. */
5141 free_after_parsing (cfun);
5142 free_after_compilation (cfun);
5143 pop_cfun ();
5144 in_dummy_function = false;
5147 /* Helper for diddle_return_value. */
5149 void
5150 diddle_return_value_1 (void (*doit) (rtx, void *), void *arg, rtx outgoing)
5152 if (! outgoing)
5153 return;
5155 if (REG_P (outgoing))
5156 (*doit) (outgoing, arg);
5157 else if (GET_CODE (outgoing) == PARALLEL)
5159 int i;
5161 for (i = 0; i < XVECLEN (outgoing, 0); i++)
5163 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
5165 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
5166 (*doit) (x, arg);
5171 /* Call DOIT for each hard register used as a return value from
5172 the current function. */
5174 void
5175 diddle_return_value (void (*doit) (rtx, void *), void *arg)
5177 diddle_return_value_1 (doit, arg, crtl->return_rtx);
5178 diddle_return_value_1 (doit, arg, crtl->return_bnd);
5181 static void
5182 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5184 emit_clobber (reg);
5187 void
5188 clobber_return_register (void)
5190 diddle_return_value (do_clobber_return_reg, NULL);
5192 /* In case we do use pseudo to return value, clobber it too. */
5193 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5195 tree decl_result = DECL_RESULT (current_function_decl);
5196 rtx decl_rtl = DECL_RTL (decl_result);
5197 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
5199 do_clobber_return_reg (decl_rtl, NULL);
5204 static void
5205 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5207 emit_use (reg);
5210 static void
5211 use_return_register (void)
5213 diddle_return_value (do_use_return_reg, NULL);
5216 /* Possibly warn about unused parameters. */
5217 void
5218 do_warn_unused_parameter (tree fn)
5220 tree decl;
5222 for (decl = DECL_ARGUMENTS (fn);
5223 decl; decl = DECL_CHAIN (decl))
5224 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
5225 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
5226 && !TREE_NO_WARNING (decl))
5227 warning (OPT_Wunused_parameter, "unused parameter %q+D", decl);
5230 /* Set the location of the insn chain starting at INSN to LOC. */
5232 static void
5233 set_insn_locations (rtx_insn *insn, int loc)
5235 while (insn != NULL)
5237 if (INSN_P (insn))
5238 INSN_LOCATION (insn) = loc;
5239 insn = NEXT_INSN (insn);
5243 /* Generate RTL for the end of the current function. */
5245 void
5246 expand_function_end (void)
5248 rtx clobber_after;
5250 /* If arg_pointer_save_area was referenced only from a nested
5251 function, we will not have initialized it yet. Do that now. */
5252 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
5253 get_arg_pointer_save_area ();
5255 /* If we are doing generic stack checking and this function makes calls,
5256 do a stack probe at the start of the function to ensure we have enough
5257 space for another stack frame. */
5258 if (flag_stack_check == GENERIC_STACK_CHECK)
5260 rtx_insn *insn, *seq;
5262 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5263 if (CALL_P (insn))
5265 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
5266 start_sequence ();
5267 if (STACK_CHECK_MOVING_SP)
5268 anti_adjust_stack_and_probe (max_frame_size, true);
5269 else
5270 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
5271 seq = get_insns ();
5272 end_sequence ();
5273 set_insn_locations (seq, prologue_location);
5274 emit_insn_before (seq, stack_check_probe_note);
5275 break;
5279 /* End any sequences that failed to be closed due to syntax errors. */
5280 while (in_sequence_p ())
5281 end_sequence ();
5283 clear_pending_stack_adjust ();
5284 do_pending_stack_adjust ();
5286 /* Output a linenumber for the end of the function.
5287 SDB depends on this. */
5288 set_curr_insn_location (input_location);
5290 /* Before the return label (if any), clobber the return
5291 registers so that they are not propagated live to the rest of
5292 the function. This can only happen with functions that drop
5293 through; if there had been a return statement, there would
5294 have either been a return rtx, or a jump to the return label.
5296 We delay actual code generation after the current_function_value_rtx
5297 is computed. */
5298 clobber_after = get_last_insn ();
5300 /* Output the label for the actual return from the function. */
5301 emit_label (return_label);
5303 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
5305 /* Let except.c know where it should emit the call to unregister
5306 the function context for sjlj exceptions. */
5307 if (flag_exceptions)
5308 sjlj_emit_function_exit_after (get_last_insn ());
5310 else
5312 /* We want to ensure that instructions that may trap are not
5313 moved into the epilogue by scheduling, because we don't
5314 always emit unwind information for the epilogue. */
5315 if (cfun->can_throw_non_call_exceptions)
5316 emit_insn (gen_blockage ());
5319 /* If this is an implementation of throw, do what's necessary to
5320 communicate between __builtin_eh_return and the epilogue. */
5321 expand_eh_return ();
5323 /* If scalar return value was computed in a pseudo-reg, or was a named
5324 return value that got dumped to the stack, copy that to the hard
5325 return register. */
5326 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5328 tree decl_result = DECL_RESULT (current_function_decl);
5329 rtx decl_rtl = DECL_RTL (decl_result);
5331 if (REG_P (decl_rtl)
5332 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5333 : DECL_REGISTER (decl_result))
5335 rtx real_decl_rtl = crtl->return_rtx;
5337 /* This should be set in assign_parms. */
5338 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5340 /* If this is a BLKmode structure being returned in registers,
5341 then use the mode computed in expand_return. Note that if
5342 decl_rtl is memory, then its mode may have been changed,
5343 but that crtl->return_rtx has not. */
5344 if (GET_MODE (real_decl_rtl) == BLKmode)
5345 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
5347 /* If a non-BLKmode return value should be padded at the least
5348 significant end of the register, shift it left by the appropriate
5349 amount. BLKmode results are handled using the group load/store
5350 machinery. */
5351 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5352 && REG_P (real_decl_rtl)
5353 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5355 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5356 REGNO (real_decl_rtl)),
5357 decl_rtl);
5358 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5360 /* If a named return value dumped decl_return to memory, then
5361 we may need to re-do the PROMOTE_MODE signed/unsigned
5362 extension. */
5363 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5365 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5366 promote_function_mode (TREE_TYPE (decl_result),
5367 GET_MODE (decl_rtl), &unsignedp,
5368 TREE_TYPE (current_function_decl), 1);
5370 convert_move (real_decl_rtl, decl_rtl, unsignedp);
5372 else if (GET_CODE (real_decl_rtl) == PARALLEL)
5374 /* If expand_function_start has created a PARALLEL for decl_rtl,
5375 move the result to the real return registers. Otherwise, do
5376 a group load from decl_rtl for a named return. */
5377 if (GET_CODE (decl_rtl) == PARALLEL)
5378 emit_group_move (real_decl_rtl, decl_rtl);
5379 else
5380 emit_group_load (real_decl_rtl, decl_rtl,
5381 TREE_TYPE (decl_result),
5382 int_size_in_bytes (TREE_TYPE (decl_result)));
5384 /* In the case of complex integer modes smaller than a word, we'll
5385 need to generate some non-trivial bitfield insertions. Do that
5386 on a pseudo and not the hard register. */
5387 else if (GET_CODE (decl_rtl) == CONCAT
5388 && GET_MODE_CLASS (GET_MODE (decl_rtl)) == MODE_COMPLEX_INT
5389 && GET_MODE_BITSIZE (GET_MODE (decl_rtl)) <= BITS_PER_WORD)
5391 int old_generating_concat_p;
5392 rtx tmp;
5394 old_generating_concat_p = generating_concat_p;
5395 generating_concat_p = 0;
5396 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5397 generating_concat_p = old_generating_concat_p;
5399 emit_move_insn (tmp, decl_rtl);
5400 emit_move_insn (real_decl_rtl, tmp);
5402 else
5403 emit_move_insn (real_decl_rtl, decl_rtl);
5407 /* If returning a structure, arrange to return the address of the value
5408 in a place where debuggers expect to find it.
5410 If returning a structure PCC style,
5411 the caller also depends on this value.
5412 And cfun->returns_pcc_struct is not necessarily set. */
5413 if ((cfun->returns_struct || cfun->returns_pcc_struct)
5414 && !targetm.calls.omit_struct_return_reg)
5416 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5417 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5418 rtx outgoing;
5420 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5421 type = TREE_TYPE (type);
5422 else
5423 value_address = XEXP (value_address, 0);
5425 outgoing = targetm.calls.function_value (build_pointer_type (type),
5426 current_function_decl, true);
5428 /* Mark this as a function return value so integrate will delete the
5429 assignment and USE below when inlining this function. */
5430 REG_FUNCTION_VALUE_P (outgoing) = 1;
5432 /* The address may be ptr_mode and OUTGOING may be Pmode. */
5433 value_address = convert_memory_address (GET_MODE (outgoing),
5434 value_address);
5436 emit_move_insn (outgoing, value_address);
5438 /* Show return register used to hold result (in this case the address
5439 of the result. */
5440 crtl->return_rtx = outgoing;
5443 /* Emit the actual code to clobber return register. Don't emit
5444 it if clobber_after is a barrier, then the previous basic block
5445 certainly doesn't fall thru into the exit block. */
5446 if (!BARRIER_P (clobber_after))
5448 rtx seq;
5450 start_sequence ();
5451 clobber_return_register ();
5452 seq = get_insns ();
5453 end_sequence ();
5455 emit_insn_after (seq, clobber_after);
5458 /* Output the label for the naked return from the function. */
5459 if (naked_return_label)
5460 emit_label (naked_return_label);
5462 /* @@@ This is a kludge. We want to ensure that instructions that
5463 may trap are not moved into the epilogue by scheduling, because
5464 we don't always emit unwind information for the epilogue. */
5465 if (cfun->can_throw_non_call_exceptions
5466 && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5467 emit_insn (gen_blockage ());
5469 /* If stack protection is enabled for this function, check the guard. */
5470 if (crtl->stack_protect_guard)
5471 stack_protect_epilogue ();
5473 /* If we had calls to alloca, and this machine needs
5474 an accurate stack pointer to exit the function,
5475 insert some code to save and restore the stack pointer. */
5476 if (! EXIT_IGNORE_STACK
5477 && cfun->calls_alloca)
5479 rtx tem = 0, seq;
5481 start_sequence ();
5482 emit_stack_save (SAVE_FUNCTION, &tem);
5483 seq = get_insns ();
5484 end_sequence ();
5485 emit_insn_before (seq, parm_birth_insn);
5487 emit_stack_restore (SAVE_FUNCTION, tem);
5490 /* ??? This should no longer be necessary since stupid is no longer with
5491 us, but there are some parts of the compiler (eg reload_combine, and
5492 sh mach_dep_reorg) that still try and compute their own lifetime info
5493 instead of using the general framework. */
5494 use_return_register ();
5498 get_arg_pointer_save_area (void)
5500 rtx ret = arg_pointer_save_area;
5502 if (! ret)
5504 ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5505 arg_pointer_save_area = ret;
5508 if (! crtl->arg_pointer_save_area_init)
5510 rtx seq;
5512 /* Save the arg pointer at the beginning of the function. The
5513 generated stack slot may not be a valid memory address, so we
5514 have to check it and fix it if necessary. */
5515 start_sequence ();
5516 emit_move_insn (validize_mem (copy_rtx (ret)),
5517 crtl->args.internal_arg_pointer);
5518 seq = get_insns ();
5519 end_sequence ();
5521 push_topmost_sequence ();
5522 emit_insn_after (seq, entry_of_function ());
5523 pop_topmost_sequence ();
5525 crtl->arg_pointer_save_area_init = true;
5528 return ret;
5531 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5532 for the first time. */
5534 static void
5535 record_insns (rtx_insn *insns, rtx end, htab_t *hashp)
5537 rtx_insn *tmp;
5538 htab_t hash = *hashp;
5540 if (hash == NULL)
5541 *hashp = hash
5542 = htab_create_ggc (17, htab_hash_pointer, htab_eq_pointer, NULL);
5544 for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5546 void **slot = htab_find_slot (hash, tmp, INSERT);
5547 gcc_assert (*slot == NULL);
5548 *slot = tmp;
5552 /* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5553 basic block, splitting or peepholes. If INSN is a prologue or epilogue
5554 insn, then record COPY as well. */
5556 void
5557 maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5559 htab_t hash;
5560 void **slot;
5562 hash = epilogue_insn_hash;
5563 if (!hash || !htab_find (hash, insn))
5565 hash = prologue_insn_hash;
5566 if (!hash || !htab_find (hash, insn))
5567 return;
5570 slot = htab_find_slot (hash, copy, INSERT);
5571 gcc_assert (*slot == NULL);
5572 *slot = copy;
5575 /* Determine if any INSNs in HASH are, or are part of, INSN. Because
5576 we can be running after reorg, SEQUENCE rtl is possible. */
5578 static bool
5579 contains (const_rtx insn, htab_t hash)
5581 if (hash == NULL)
5582 return false;
5584 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5586 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
5587 int i;
5588 for (i = seq->len () - 1; i >= 0; i--)
5589 if (htab_find (hash, seq->element (i)))
5590 return true;
5591 return false;
5594 return htab_find (hash, insn) != NULL;
5598 prologue_epilogue_contains (const_rtx insn)
5600 if (contains (insn, prologue_insn_hash))
5601 return 1;
5602 if (contains (insn, epilogue_insn_hash))
5603 return 1;
5604 return 0;
5607 #ifdef HAVE_return
5608 /* Insert use of return register before the end of BB. */
5610 static void
5611 emit_use_return_register_into_block (basic_block bb)
5613 rtx seq, insn;
5614 start_sequence ();
5615 use_return_register ();
5616 seq = get_insns ();
5617 end_sequence ();
5618 insn = BB_END (bb);
5619 #ifdef HAVE_cc0
5620 if (reg_mentioned_p (cc0_rtx, PATTERN (insn)))
5621 insn = prev_cc0_setter (insn);
5622 #endif
5623 emit_insn_before (seq, insn);
5627 /* Create a return pattern, either simple_return or return, depending on
5628 simple_p. */
5630 static rtx
5631 gen_return_pattern (bool simple_p)
5633 #ifdef HAVE_simple_return
5634 return simple_p ? gen_simple_return () : gen_return ();
5635 #else
5636 gcc_assert (!simple_p);
5637 return gen_return ();
5638 #endif
5641 /* Insert an appropriate return pattern at the end of block BB. This
5642 also means updating block_for_insn appropriately. SIMPLE_P is
5643 the same as in gen_return_pattern and passed to it. */
5645 void
5646 emit_return_into_block (bool simple_p, basic_block bb)
5648 rtx jump, pat;
5649 jump = emit_jump_insn_after (gen_return_pattern (simple_p), BB_END (bb));
5650 pat = PATTERN (jump);
5651 if (GET_CODE (pat) == PARALLEL)
5652 pat = XVECEXP (pat, 0, 0);
5653 gcc_assert (ANY_RETURN_P (pat));
5654 JUMP_LABEL (jump) = pat;
5656 #endif
5658 /* Set JUMP_LABEL for a return insn. */
5660 void
5661 set_return_jump_label (rtx returnjump)
5663 rtx pat = PATTERN (returnjump);
5664 if (GET_CODE (pat) == PARALLEL)
5665 pat = XVECEXP (pat, 0, 0);
5666 if (ANY_RETURN_P (pat))
5667 JUMP_LABEL (returnjump) = pat;
5668 else
5669 JUMP_LABEL (returnjump) = ret_rtx;
5672 #if defined (HAVE_return) || defined (HAVE_simple_return)
5673 /* Return true if there are any active insns between HEAD and TAIL. */
5674 bool
5675 active_insn_between (rtx_insn *head, rtx_insn *tail)
5677 while (tail)
5679 if (active_insn_p (tail))
5680 return true;
5681 if (tail == head)
5682 return false;
5683 tail = PREV_INSN (tail);
5685 return false;
5688 /* LAST_BB is a block that exits, and empty of active instructions.
5689 Examine its predecessors for jumps that can be converted to
5690 (conditional) returns. */
5691 vec<edge>
5692 convert_jumps_to_returns (basic_block last_bb, bool simple_p,
5693 vec<edge> unconverted ATTRIBUTE_UNUSED)
5695 int i;
5696 basic_block bb;
5697 rtx label;
5698 edge_iterator ei;
5699 edge e;
5700 auto_vec<basic_block> src_bbs (EDGE_COUNT (last_bb->preds));
5702 FOR_EACH_EDGE (e, ei, last_bb->preds)
5703 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
5704 src_bbs.quick_push (e->src);
5706 label = BB_HEAD (last_bb);
5708 FOR_EACH_VEC_ELT (src_bbs, i, bb)
5710 rtx_insn *jump = BB_END (bb);
5712 if (!JUMP_P (jump) || JUMP_LABEL (jump) != label)
5713 continue;
5715 e = find_edge (bb, last_bb);
5717 /* If we have an unconditional jump, we can replace that
5718 with a simple return instruction. */
5719 if (simplejump_p (jump))
5721 /* The use of the return register might be present in the exit
5722 fallthru block. Either:
5723 - removing the use is safe, and we should remove the use in
5724 the exit fallthru block, or
5725 - removing the use is not safe, and we should add it here.
5726 For now, we conservatively choose the latter. Either of the
5727 2 helps in crossjumping. */
5728 emit_use_return_register_into_block (bb);
5730 emit_return_into_block (simple_p, bb);
5731 delete_insn (jump);
5734 /* If we have a conditional jump branching to the last
5735 block, we can try to replace that with a conditional
5736 return instruction. */
5737 else if (condjump_p (jump))
5739 rtx dest;
5741 if (simple_p)
5742 dest = simple_return_rtx;
5743 else
5744 dest = ret_rtx;
5745 if (!redirect_jump (jump, dest, 0))
5747 #ifdef HAVE_simple_return
5748 if (simple_p)
5750 if (dump_file)
5751 fprintf (dump_file,
5752 "Failed to redirect bb %d branch.\n", bb->index);
5753 unconverted.safe_push (e);
5755 #endif
5756 continue;
5759 /* See comment in simplejump_p case above. */
5760 emit_use_return_register_into_block (bb);
5762 /* If this block has only one successor, it both jumps
5763 and falls through to the fallthru block, so we can't
5764 delete the edge. */
5765 if (single_succ_p (bb))
5766 continue;
5768 else
5770 #ifdef HAVE_simple_return
5771 if (simple_p)
5773 if (dump_file)
5774 fprintf (dump_file,
5775 "Failed to redirect bb %d branch.\n", bb->index);
5776 unconverted.safe_push (e);
5778 #endif
5779 continue;
5782 /* Fix up the CFG for the successful change we just made. */
5783 redirect_edge_succ (e, EXIT_BLOCK_PTR_FOR_FN (cfun));
5784 e->flags &= ~EDGE_CROSSING;
5786 src_bbs.release ();
5787 return unconverted;
5790 /* Emit a return insn for the exit fallthru block. */
5791 basic_block
5792 emit_return_for_exit (edge exit_fallthru_edge, bool simple_p)
5794 basic_block last_bb = exit_fallthru_edge->src;
5796 if (JUMP_P (BB_END (last_bb)))
5798 last_bb = split_edge (exit_fallthru_edge);
5799 exit_fallthru_edge = single_succ_edge (last_bb);
5801 emit_barrier_after (BB_END (last_bb));
5802 emit_return_into_block (simple_p, last_bb);
5803 exit_fallthru_edge->flags &= ~EDGE_FALLTHRU;
5804 return last_bb;
5806 #endif
5809 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
5810 this into place with notes indicating where the prologue ends and where
5811 the epilogue begins. Update the basic block information when possible.
5813 Notes on epilogue placement:
5814 There are several kinds of edges to the exit block:
5815 * a single fallthru edge from LAST_BB
5816 * possibly, edges from blocks containing sibcalls
5817 * possibly, fake edges from infinite loops
5819 The epilogue is always emitted on the fallthru edge from the last basic
5820 block in the function, LAST_BB, into the exit block.
5822 If LAST_BB is empty except for a label, it is the target of every
5823 other basic block in the function that ends in a return. If a
5824 target has a return or simple_return pattern (possibly with
5825 conditional variants), these basic blocks can be changed so that a
5826 return insn is emitted into them, and their target is adjusted to
5827 the real exit block.
5829 Notes on shrink wrapping: We implement a fairly conservative
5830 version of shrink-wrapping rather than the textbook one. We only
5831 generate a single prologue and a single epilogue. This is
5832 sufficient to catch a number of interesting cases involving early
5833 exits.
5835 First, we identify the blocks that require the prologue to occur before
5836 them. These are the ones that modify a call-saved register, or reference
5837 any of the stack or frame pointer registers. To simplify things, we then
5838 mark everything reachable from these blocks as also requiring a prologue.
5839 This takes care of loops automatically, and avoids the need to examine
5840 whether MEMs reference the frame, since it is sufficient to check for
5841 occurrences of the stack or frame pointer.
5843 We then compute the set of blocks for which the need for a prologue
5844 is anticipatable (borrowing terminology from the shrink-wrapping
5845 description in Muchnick's book). These are the blocks which either
5846 require a prologue themselves, or those that have only successors
5847 where the prologue is anticipatable. The prologue needs to be
5848 inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
5849 is not. For the moment, we ensure that only one such edge exists.
5851 The epilogue is placed as described above, but we make a
5852 distinction between inserting return and simple_return patterns
5853 when modifying other blocks that end in a return. Blocks that end
5854 in a sibcall omit the sibcall_epilogue if the block is not in
5855 ANTIC. */
5857 void
5858 thread_prologue_and_epilogue_insns (void)
5860 bool inserted;
5861 #ifdef HAVE_simple_return
5862 vec<edge> unconverted_simple_returns = vNULL;
5863 bitmap_head bb_flags;
5864 #endif
5865 rtx_insn *returnjump;
5866 rtx_insn *epilogue_end ATTRIBUTE_UNUSED;
5867 rtx_insn *prologue_seq ATTRIBUTE_UNUSED, *split_prologue_seq ATTRIBUTE_UNUSED;
5868 edge e, entry_edge, orig_entry_edge, exit_fallthru_edge;
5869 edge_iterator ei;
5871 df_analyze ();
5873 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5875 inserted = false;
5876 epilogue_end = NULL;
5877 returnjump = NULL;
5879 /* Can't deal with multiple successors of the entry block at the
5880 moment. Function should always have at least one entry
5881 point. */
5882 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5883 entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5884 orig_entry_edge = entry_edge;
5886 split_prologue_seq = NULL;
5887 if (flag_split_stack
5888 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
5889 == NULL))
5891 #ifndef HAVE_split_stack_prologue
5892 gcc_unreachable ();
5893 #else
5894 gcc_assert (HAVE_split_stack_prologue);
5896 start_sequence ();
5897 emit_insn (gen_split_stack_prologue ());
5898 split_prologue_seq = get_insns ();
5899 end_sequence ();
5901 record_insns (split_prologue_seq, NULL, &prologue_insn_hash);
5902 set_insn_locations (split_prologue_seq, prologue_location);
5903 #endif
5906 prologue_seq = NULL;
5907 #ifdef HAVE_prologue
5908 if (HAVE_prologue)
5910 start_sequence ();
5911 rtx_insn *seq = safe_as_a <rtx_insn *> (gen_prologue ());
5912 emit_insn (seq);
5914 /* Insert an explicit USE for the frame pointer
5915 if the profiling is on and the frame pointer is required. */
5916 if (crtl->profile && frame_pointer_needed)
5917 emit_use (hard_frame_pointer_rtx);
5919 /* Retain a map of the prologue insns. */
5920 record_insns (seq, NULL, &prologue_insn_hash);
5921 emit_note (NOTE_INSN_PROLOGUE_END);
5923 /* Ensure that instructions are not moved into the prologue when
5924 profiling is on. The call to the profiling routine can be
5925 emitted within the live range of a call-clobbered register. */
5926 if (!targetm.profile_before_prologue () && crtl->profile)
5927 emit_insn (gen_blockage ());
5929 prologue_seq = get_insns ();
5930 end_sequence ();
5931 set_insn_locations (prologue_seq, prologue_location);
5933 #endif
5935 #ifdef HAVE_simple_return
5936 bitmap_initialize (&bb_flags, &bitmap_default_obstack);
5938 /* Try to perform a kind of shrink-wrapping, making sure the
5939 prologue/epilogue is emitted only around those parts of the
5940 function that require it. */
5942 try_shrink_wrapping (&entry_edge, orig_entry_edge, &bb_flags, prologue_seq);
5943 #endif
5945 if (split_prologue_seq != NULL_RTX)
5947 insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
5948 inserted = true;
5950 if (prologue_seq != NULL_RTX)
5952 insert_insn_on_edge (prologue_seq, entry_edge);
5953 inserted = true;
5956 /* If the exit block has no non-fake predecessors, we don't need
5957 an epilogue. */
5958 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5959 if ((e->flags & EDGE_FAKE) == 0)
5960 break;
5961 if (e == NULL)
5962 goto epilogue_done;
5964 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5966 exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
5968 #ifdef HAVE_simple_return
5969 if (entry_edge != orig_entry_edge)
5970 exit_fallthru_edge
5971 = get_unconverted_simple_return (exit_fallthru_edge, bb_flags,
5972 &unconverted_simple_returns,
5973 &returnjump);
5974 #endif
5975 #ifdef HAVE_return
5976 if (HAVE_return)
5978 if (exit_fallthru_edge == NULL)
5979 goto epilogue_done;
5981 if (optimize)
5983 basic_block last_bb = exit_fallthru_edge->src;
5985 if (LABEL_P (BB_HEAD (last_bb))
5986 && !active_insn_between (BB_HEAD (last_bb), BB_END (last_bb)))
5987 convert_jumps_to_returns (last_bb, false, vNULL);
5989 if (EDGE_COUNT (last_bb->preds) != 0
5990 && single_succ_p (last_bb))
5992 last_bb = emit_return_for_exit (exit_fallthru_edge, false);
5993 epilogue_end = returnjump = BB_END (last_bb);
5994 #ifdef HAVE_simple_return
5995 /* Emitting the return may add a basic block.
5996 Fix bb_flags for the added block. */
5997 if (last_bb != exit_fallthru_edge->src)
5998 bitmap_set_bit (&bb_flags, last_bb->index);
5999 #endif
6000 goto epilogue_done;
6004 #endif
6006 /* A small fib -- epilogue is not yet completed, but we wish to re-use
6007 this marker for the splits of EH_RETURN patterns, and nothing else
6008 uses the flag in the meantime. */
6009 epilogue_completed = 1;
6011 #ifdef HAVE_eh_return
6012 /* Find non-fallthru edges that end with EH_RETURN instructions. On
6013 some targets, these get split to a special version of the epilogue
6014 code. In order to be able to properly annotate these with unwind
6015 info, try to split them now. If we get a valid split, drop an
6016 EPILOGUE_BEG note and mark the insns as epilogue insns. */
6017 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6019 rtx_insn *prev, *last, *trial;
6021 if (e->flags & EDGE_FALLTHRU)
6022 continue;
6023 last = BB_END (e->src);
6024 if (!eh_returnjump_p (last))
6025 continue;
6027 prev = PREV_INSN (last);
6028 trial = try_split (PATTERN (last), last, 1);
6029 if (trial == last)
6030 continue;
6032 record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
6033 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
6035 #endif
6037 /* If nothing falls through into the exit block, we don't need an
6038 epilogue. */
6040 if (exit_fallthru_edge == NULL)
6041 goto epilogue_done;
6043 #ifdef HAVE_epilogue
6044 if (HAVE_epilogue)
6046 start_sequence ();
6047 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
6048 rtx_insn *seq = as_a <rtx_insn *> (gen_epilogue ());
6049 if (seq)
6050 emit_jump_insn (seq);
6052 /* Retain a map of the epilogue insns. */
6053 record_insns (seq, NULL, &epilogue_insn_hash);
6054 set_insn_locations (seq, epilogue_location);
6056 seq = get_insns ();
6057 returnjump = get_last_insn ();
6058 end_sequence ();
6060 insert_insn_on_edge (seq, exit_fallthru_edge);
6061 inserted = true;
6063 if (JUMP_P (returnjump))
6064 set_return_jump_label (returnjump);
6066 else
6067 #endif
6069 basic_block cur_bb;
6071 if (! next_active_insn (BB_END (exit_fallthru_edge->src)))
6072 goto epilogue_done;
6073 /* We have a fall-through edge to the exit block, the source is not
6074 at the end of the function, and there will be an assembler epilogue
6075 at the end of the function.
6076 We can't use force_nonfallthru here, because that would try to
6077 use return. Inserting a jump 'by hand' is extremely messy, so
6078 we take advantage of cfg_layout_finalize using
6079 fixup_fallthru_exit_predecessor. */
6080 cfg_layout_initialize (0);
6081 FOR_EACH_BB_FN (cur_bb, cfun)
6082 if (cur_bb->index >= NUM_FIXED_BLOCKS
6083 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
6084 cur_bb->aux = cur_bb->next_bb;
6085 cfg_layout_finalize ();
6088 epilogue_done:
6090 default_rtl_profile ();
6092 if (inserted)
6094 sbitmap blocks;
6096 commit_edge_insertions ();
6098 /* Look for basic blocks within the prologue insns. */
6099 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
6100 bitmap_clear (blocks);
6101 bitmap_set_bit (blocks, entry_edge->dest->index);
6102 bitmap_set_bit (blocks, orig_entry_edge->dest->index);
6103 find_many_sub_basic_blocks (blocks);
6104 sbitmap_free (blocks);
6106 /* The epilogue insns we inserted may cause the exit edge to no longer
6107 be fallthru. */
6108 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6110 if (((e->flags & EDGE_FALLTHRU) != 0)
6111 && returnjump_p (BB_END (e->src)))
6112 e->flags &= ~EDGE_FALLTHRU;
6116 #ifdef HAVE_simple_return
6117 convert_to_simple_return (entry_edge, orig_entry_edge, bb_flags, returnjump,
6118 unconverted_simple_returns);
6119 #endif
6121 #ifdef HAVE_sibcall_epilogue
6122 /* Emit sibling epilogues before any sibling call sites. */
6123 for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds); (e =
6124 ei_safe_edge (ei));
6127 basic_block bb = e->src;
6128 rtx_insn *insn = BB_END (bb);
6129 rtx ep_seq;
6131 if (!CALL_P (insn)
6132 || ! SIBLING_CALL_P (insn)
6133 #ifdef HAVE_simple_return
6134 || (entry_edge != orig_entry_edge
6135 && !bitmap_bit_p (&bb_flags, bb->index))
6136 #endif
6139 ei_next (&ei);
6140 continue;
6143 ep_seq = gen_sibcall_epilogue ();
6144 if (ep_seq)
6146 start_sequence ();
6147 emit_note (NOTE_INSN_EPILOGUE_BEG);
6148 emit_insn (ep_seq);
6149 rtx_insn *seq = get_insns ();
6150 end_sequence ();
6152 /* Retain a map of the epilogue insns. Used in life analysis to
6153 avoid getting rid of sibcall epilogue insns. Do this before we
6154 actually emit the sequence. */
6155 record_insns (seq, NULL, &epilogue_insn_hash);
6156 set_insn_locations (seq, epilogue_location);
6158 emit_insn_before (seq, insn);
6160 ei_next (&ei);
6162 #endif
6164 #ifdef HAVE_epilogue
6165 if (epilogue_end)
6167 rtx_insn *insn, *next;
6169 /* Similarly, move any line notes that appear after the epilogue.
6170 There is no need, however, to be quite so anal about the existence
6171 of such a note. Also possibly move
6172 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
6173 info generation. */
6174 for (insn = epilogue_end; insn; insn = next)
6176 next = NEXT_INSN (insn);
6177 if (NOTE_P (insn)
6178 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
6179 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
6182 #endif
6184 #ifdef HAVE_simple_return
6185 bitmap_clear (&bb_flags);
6186 #endif
6188 /* Threading the prologue and epilogue changes the artificial refs
6189 in the entry and exit blocks. */
6190 epilogue_completed = 1;
6191 df_update_entry_exit_and_calls ();
6194 /* Reposition the prologue-end and epilogue-begin notes after
6195 instruction scheduling. */
6197 void
6198 reposition_prologue_and_epilogue_notes (void)
6200 #if defined (HAVE_prologue) || defined (HAVE_epilogue) \
6201 || defined (HAVE_sibcall_epilogue)
6202 /* Since the hash table is created on demand, the fact that it is
6203 non-null is a signal that it is non-empty. */
6204 if (prologue_insn_hash != NULL)
6206 size_t len = htab_elements (prologue_insn_hash);
6207 rtx_insn *insn, *last = NULL, *note = NULL;
6209 /* Scan from the beginning until we reach the last prologue insn. */
6210 /* ??? While we do have the CFG intact, there are two problems:
6211 (1) The prologue can contain loops (typically probing the stack),
6212 which means that the end of the prologue isn't in the first bb.
6213 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
6214 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6216 if (NOTE_P (insn))
6218 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
6219 note = insn;
6221 else if (contains (insn, prologue_insn_hash))
6223 last = insn;
6224 if (--len == 0)
6225 break;
6229 if (last)
6231 if (note == NULL)
6233 /* Scan forward looking for the PROLOGUE_END note. It should
6234 be right at the beginning of the block, possibly with other
6235 insn notes that got moved there. */
6236 for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
6238 if (NOTE_P (note)
6239 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
6240 break;
6244 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
6245 if (LABEL_P (last))
6246 last = NEXT_INSN (last);
6247 reorder_insns (note, note, last);
6251 if (epilogue_insn_hash != NULL)
6253 edge_iterator ei;
6254 edge e;
6256 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6258 rtx_insn *insn, *first = NULL, *note = NULL;
6259 basic_block bb = e->src;
6261 /* Scan from the beginning until we reach the first epilogue insn. */
6262 FOR_BB_INSNS (bb, insn)
6264 if (NOTE_P (insn))
6266 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
6268 note = insn;
6269 if (first != NULL)
6270 break;
6273 else if (first == NULL && contains (insn, epilogue_insn_hash))
6275 first = insn;
6276 if (note != NULL)
6277 break;
6281 if (note)
6283 /* If the function has a single basic block, and no real
6284 epilogue insns (e.g. sibcall with no cleanup), the
6285 epilogue note can get scheduled before the prologue
6286 note. If we have frame related prologue insns, having
6287 them scanned during the epilogue will result in a crash.
6288 In this case re-order the epilogue note to just before
6289 the last insn in the block. */
6290 if (first == NULL)
6291 first = BB_END (bb);
6293 if (PREV_INSN (first) != note)
6294 reorder_insns (note, note, PREV_INSN (first));
6298 #endif /* HAVE_prologue or HAVE_epilogue */
6301 /* Returns the name of function declared by FNDECL. */
6302 const char *
6303 fndecl_name (tree fndecl)
6305 if (fndecl == NULL)
6306 return "(nofn)";
6307 return lang_hooks.decl_printable_name (fndecl, 2);
6310 /* Returns the name of function FN. */
6311 const char *
6312 function_name (struct function *fn)
6314 tree fndecl = (fn == NULL) ? NULL : fn->decl;
6315 return fndecl_name (fndecl);
6318 /* Returns the name of the current function. */
6319 const char *
6320 current_function_name (void)
6322 return function_name (cfun);
6326 static unsigned int
6327 rest_of_handle_check_leaf_regs (void)
6329 #ifdef LEAF_REGISTERS
6330 crtl->uses_only_leaf_regs
6331 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6332 #endif
6333 return 0;
6336 /* Insert a TYPE into the used types hash table of CFUN. */
6338 static void
6339 used_types_insert_helper (tree type, struct function *func)
6341 if (type != NULL && func != NULL)
6343 if (func->used_types_hash == NULL)
6344 func->used_types_hash = hash_set<tree>::create_ggc (37);
6346 func->used_types_hash->add (type);
6350 /* Given a type, insert it into the used hash table in cfun. */
6351 void
6352 used_types_insert (tree t)
6354 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6355 if (TYPE_NAME (t))
6356 break;
6357 else
6358 t = TREE_TYPE (t);
6359 if (TREE_CODE (t) == ERROR_MARK)
6360 return;
6361 if (TYPE_NAME (t) == NULL_TREE
6362 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6363 t = TYPE_MAIN_VARIANT (t);
6364 if (debug_info_level > DINFO_LEVEL_NONE)
6366 if (cfun)
6367 used_types_insert_helper (t, cfun);
6368 else
6370 /* So this might be a type referenced by a global variable.
6371 Record that type so that we can later decide to emit its
6372 debug information. */
6373 vec_safe_push (types_used_by_cur_var_decl, t);
6378 /* Helper to Hash a struct types_used_by_vars_entry. */
6380 static hashval_t
6381 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6383 gcc_assert (entry && entry->var_decl && entry->type);
6385 return iterative_hash_object (entry->type,
6386 iterative_hash_object (entry->var_decl, 0));
6389 /* Hash function of the types_used_by_vars_entry hash table. */
6391 hashval_t
6392 used_type_hasher::hash (types_used_by_vars_entry *entry)
6394 return hash_types_used_by_vars_entry (entry);
6397 /*Equality function of the types_used_by_vars_entry hash table. */
6399 bool
6400 used_type_hasher::equal (types_used_by_vars_entry *e1,
6401 types_used_by_vars_entry *e2)
6403 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6406 /* Inserts an entry into the types_used_by_vars_hash hash table. */
6408 void
6409 types_used_by_var_decl_insert (tree type, tree var_decl)
6411 if (type != NULL && var_decl != NULL)
6413 types_used_by_vars_entry **slot;
6414 struct types_used_by_vars_entry e;
6415 e.var_decl = var_decl;
6416 e.type = type;
6417 if (types_used_by_vars_hash == NULL)
6418 types_used_by_vars_hash
6419 = hash_table<used_type_hasher>::create_ggc (37);
6421 slot = types_used_by_vars_hash->find_slot (&e, INSERT);
6422 if (*slot == NULL)
6424 struct types_used_by_vars_entry *entry;
6425 entry = ggc_alloc<types_used_by_vars_entry> ();
6426 entry->type = type;
6427 entry->var_decl = var_decl;
6428 *slot = entry;
6433 namespace {
6435 const pass_data pass_data_leaf_regs =
6437 RTL_PASS, /* type */
6438 "*leaf_regs", /* name */
6439 OPTGROUP_NONE, /* optinfo_flags */
6440 TV_NONE, /* tv_id */
6441 0, /* properties_required */
6442 0, /* properties_provided */
6443 0, /* properties_destroyed */
6444 0, /* todo_flags_start */
6445 0, /* todo_flags_finish */
6448 class pass_leaf_regs : public rtl_opt_pass
6450 public:
6451 pass_leaf_regs (gcc::context *ctxt)
6452 : rtl_opt_pass (pass_data_leaf_regs, ctxt)
6455 /* opt_pass methods: */
6456 virtual unsigned int execute (function *)
6458 return rest_of_handle_check_leaf_regs ();
6461 }; // class pass_leaf_regs
6463 } // anon namespace
6465 rtl_opt_pass *
6466 make_pass_leaf_regs (gcc::context *ctxt)
6468 return new pass_leaf_regs (ctxt);
6471 static unsigned int
6472 rest_of_handle_thread_prologue_and_epilogue (void)
6474 if (optimize)
6475 cleanup_cfg (CLEANUP_EXPENSIVE);
6477 /* On some machines, the prologue and epilogue code, or parts thereof,
6478 can be represented as RTL. Doing so lets us schedule insns between
6479 it and the rest of the code and also allows delayed branch
6480 scheduling to operate in the epilogue. */
6481 thread_prologue_and_epilogue_insns ();
6483 /* Shrink-wrapping can result in unreachable edges in the epilogue,
6484 see PR57320. */
6485 cleanup_cfg (0);
6487 /* The stack usage info is finalized during prologue expansion. */
6488 if (flag_stack_usage_info)
6489 output_stack_usage ();
6491 return 0;
6494 namespace {
6496 const pass_data pass_data_thread_prologue_and_epilogue =
6498 RTL_PASS, /* type */
6499 "pro_and_epilogue", /* name */
6500 OPTGROUP_NONE, /* optinfo_flags */
6501 TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
6502 0, /* properties_required */
6503 0, /* properties_provided */
6504 0, /* properties_destroyed */
6505 0, /* todo_flags_start */
6506 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
6509 class pass_thread_prologue_and_epilogue : public rtl_opt_pass
6511 public:
6512 pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6513 : rtl_opt_pass (pass_data_thread_prologue_and_epilogue, ctxt)
6516 /* opt_pass methods: */
6517 virtual unsigned int execute (function *)
6519 return rest_of_handle_thread_prologue_and_epilogue ();
6522 }; // class pass_thread_prologue_and_epilogue
6524 } // anon namespace
6526 rtl_opt_pass *
6527 make_pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6529 return new pass_thread_prologue_and_epilogue (ctxt);
6533 /* This mini-pass fixes fall-out from SSA in asm statements that have
6534 in-out constraints. Say you start with
6536 orig = inout;
6537 asm ("": "+mr" (inout));
6538 use (orig);
6540 which is transformed very early to use explicit output and match operands:
6542 orig = inout;
6543 asm ("": "=mr" (inout) : "0" (inout));
6544 use (orig);
6546 Or, after SSA and copyprop,
6548 asm ("": "=mr" (inout_2) : "0" (inout_1));
6549 use (inout_1);
6551 Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
6552 they represent two separate values, so they will get different pseudo
6553 registers during expansion. Then, since the two operands need to match
6554 per the constraints, but use different pseudo registers, reload can
6555 only register a reload for these operands. But reloads can only be
6556 satisfied by hardregs, not by memory, so we need a register for this
6557 reload, just because we are presented with non-matching operands.
6558 So, even though we allow memory for this operand, no memory can be
6559 used for it, just because the two operands don't match. This can
6560 cause reload failures on register-starved targets.
6562 So it's a symptom of reload not being able to use memory for reloads
6563 or, alternatively it's also a symptom of both operands not coming into
6564 reload as matching (in which case the pseudo could go to memory just
6565 fine, as the alternative allows it, and no reload would be necessary).
6566 We fix the latter problem here, by transforming
6568 asm ("": "=mr" (inout_2) : "0" (inout_1));
6570 back to
6572 inout_2 = inout_1;
6573 asm ("": "=mr" (inout_2) : "0" (inout_2)); */
6575 static void
6576 match_asm_constraints_1 (rtx_insn *insn, rtx *p_sets, int noutputs)
6578 int i;
6579 bool changed = false;
6580 rtx op = SET_SRC (p_sets[0]);
6581 int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
6582 rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
6583 bool *output_matched = XALLOCAVEC (bool, noutputs);
6585 memset (output_matched, 0, noutputs * sizeof (bool));
6586 for (i = 0; i < ninputs; i++)
6588 rtx input, output;
6589 rtx_insn *insns;
6590 const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
6591 char *end;
6592 int match, j;
6594 if (*constraint == '%')
6595 constraint++;
6597 match = strtoul (constraint, &end, 10);
6598 if (end == constraint)
6599 continue;
6601 gcc_assert (match < noutputs);
6602 output = SET_DEST (p_sets[match]);
6603 input = RTVEC_ELT (inputs, i);
6604 /* Only do the transformation for pseudos. */
6605 if (! REG_P (output)
6606 || rtx_equal_p (output, input)
6607 || (GET_MODE (input) != VOIDmode
6608 && GET_MODE (input) != GET_MODE (output)))
6609 continue;
6611 /* We can't do anything if the output is also used as input,
6612 as we're going to overwrite it. */
6613 for (j = 0; j < ninputs; j++)
6614 if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
6615 break;
6616 if (j != ninputs)
6617 continue;
6619 /* Avoid changing the same input several times. For
6620 asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
6621 only change in once (to out1), rather than changing it
6622 first to out1 and afterwards to out2. */
6623 if (i > 0)
6625 for (j = 0; j < noutputs; j++)
6626 if (output_matched[j] && input == SET_DEST (p_sets[j]))
6627 break;
6628 if (j != noutputs)
6629 continue;
6631 output_matched[match] = true;
6633 start_sequence ();
6634 emit_move_insn (output, input);
6635 insns = get_insns ();
6636 end_sequence ();
6637 emit_insn_before (insns, insn);
6639 /* Now replace all mentions of the input with output. We can't
6640 just replace the occurrence in inputs[i], as the register might
6641 also be used in some other input (or even in an address of an
6642 output), which would mean possibly increasing the number of
6643 inputs by one (namely 'output' in addition), which might pose
6644 a too complicated problem for reload to solve. E.g. this situation:
6646 asm ("" : "=r" (output), "=m" (input) : "0" (input))
6648 Here 'input' is used in two occurrences as input (once for the
6649 input operand, once for the address in the second output operand).
6650 If we would replace only the occurrence of the input operand (to
6651 make the matching) we would be left with this:
6653 output = input
6654 asm ("" : "=r" (output), "=m" (input) : "0" (output))
6656 Now we suddenly have two different input values (containing the same
6657 value, but different pseudos) where we formerly had only one.
6658 With more complicated asms this might lead to reload failures
6659 which wouldn't have happen without this pass. So, iterate over
6660 all operands and replace all occurrences of the register used. */
6661 for (j = 0; j < noutputs; j++)
6662 if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
6663 && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
6664 SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
6665 input, output);
6666 for (j = 0; j < ninputs; j++)
6667 if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
6668 RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
6669 input, output);
6671 changed = true;
6674 if (changed)
6675 df_insn_rescan (insn);
6678 /* Add the decl D to the local_decls list of FUN. */
6680 void
6681 add_local_decl (struct function *fun, tree d)
6683 gcc_assert (TREE_CODE (d) == VAR_DECL);
6684 vec_safe_push (fun->local_decls, d);
6687 namespace {
6689 const pass_data pass_data_match_asm_constraints =
6691 RTL_PASS, /* type */
6692 "asmcons", /* name */
6693 OPTGROUP_NONE, /* optinfo_flags */
6694 TV_NONE, /* tv_id */
6695 0, /* properties_required */
6696 0, /* properties_provided */
6697 0, /* properties_destroyed */
6698 0, /* todo_flags_start */
6699 0, /* todo_flags_finish */
6702 class pass_match_asm_constraints : public rtl_opt_pass
6704 public:
6705 pass_match_asm_constraints (gcc::context *ctxt)
6706 : rtl_opt_pass (pass_data_match_asm_constraints, ctxt)
6709 /* opt_pass methods: */
6710 virtual unsigned int execute (function *);
6712 }; // class pass_match_asm_constraints
6714 unsigned
6715 pass_match_asm_constraints::execute (function *fun)
6717 basic_block bb;
6718 rtx_insn *insn;
6719 rtx pat, *p_sets;
6720 int noutputs;
6722 if (!crtl->has_asm_statement)
6723 return 0;
6725 df_set_flags (DF_DEFER_INSN_RESCAN);
6726 FOR_EACH_BB_FN (bb, fun)
6728 FOR_BB_INSNS (bb, insn)
6730 if (!INSN_P (insn))
6731 continue;
6733 pat = PATTERN (insn);
6734 if (GET_CODE (pat) == PARALLEL)
6735 p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
6736 else if (GET_CODE (pat) == SET)
6737 p_sets = &PATTERN (insn), noutputs = 1;
6738 else
6739 continue;
6741 if (GET_CODE (*p_sets) == SET
6742 && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
6743 match_asm_constraints_1 (insn, p_sets, noutputs);
6747 return TODO_df_finish;
6750 } // anon namespace
6752 rtl_opt_pass *
6753 make_pass_match_asm_constraints (gcc::context *ctxt)
6755 return new pass_match_asm_constraints (ctxt);
6759 #include "gt-function.h"