jit: API change to gcc_jit_context_new_global
[official-gcc.git] / gcc / function.c
blob9589e64df2ecaef666ff206a5e31450e902af355
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file handles the generation of rtl code from tree structure
21 at the level of the function as a whole.
22 It creates the rtl expressions for parameters and auto variables
23 and has full responsibility for allocating stack slots.
25 `expand_function_start' is called at the beginning of a function,
26 before the function body is parsed, and `expand_function_end' is
27 called after parsing the body.
29 Call `assign_stack_local' to allocate a stack slot for a local variable.
30 This is usually done during the RTL generation for the function body,
31 but it can also be done in the reload pass when a pseudo-register does
32 not get a hard register. */
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "tm.h"
38 #include "rtl-error.h"
39 #include "hash-set.h"
40 #include "machmode.h"
41 #include "vec.h"
42 #include "double-int.h"
43 #include "input.h"
44 #include "alias.h"
45 #include "symtab.h"
46 #include "wide-int.h"
47 #include "inchash.h"
48 #include "tree.h"
49 #include "fold-const.h"
50 #include "stor-layout.h"
51 #include "varasm.h"
52 #include "stringpool.h"
53 #include "flags.h"
54 #include "except.h"
55 #include "hashtab.h"
56 #include "hash-set.h"
57 #include "vec.h"
58 #include "machmode.h"
59 #include "hard-reg-set.h"
60 #include "input.h"
61 #include "function.h"
62 #include "expr.h"
63 #include "insn-codes.h"
64 #include "optabs.h"
65 #include "libfuncs.h"
66 #include "regs.h"
67 #include "insn-config.h"
68 #include "recog.h"
69 #include "output.h"
70 #include "tm_p.h"
71 #include "langhooks.h"
72 #include "target.h"
73 #include "common/common-target.h"
74 #include "gimple-expr.h"
75 #include "gimplify.h"
76 #include "tree-pass.h"
77 #include "predict.h"
78 #include "dominance.h"
79 #include "cfg.h"
80 #include "cfgrtl.h"
81 #include "cfganal.h"
82 #include "cfgbuild.h"
83 #include "cfgcleanup.h"
84 #include "basic-block.h"
85 #include "df.h"
86 #include "params.h"
87 #include "bb-reorder.h"
88 #include "shrink-wrap.h"
89 #include "toplev.h"
90 #include "rtl-iter.h"
91 #include "tree-chkp.h"
92 #include "rtl-chkp.h"
94 /* So we can assign to cfun in this file. */
95 #undef cfun
97 #ifndef STACK_ALIGNMENT_NEEDED
98 #define STACK_ALIGNMENT_NEEDED 1
99 #endif
101 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
103 /* Round a value to the lowest integer less than it that is a multiple of
104 the required alignment. Avoid using division in case the value is
105 negative. Assume the alignment is a power of two. */
106 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
108 /* Similar, but round to the next highest integer that meets the
109 alignment. */
110 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
112 /* Nonzero once virtual register instantiation has been done.
113 assign_stack_local uses frame_pointer_rtx when this is nonzero.
114 calls.c:emit_library_call_value_1 uses it to set up
115 post-instantiation libcalls. */
116 int virtuals_instantiated;
118 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
119 static GTY(()) int funcdef_no;
121 /* These variables hold pointers to functions to create and destroy
122 target specific, per-function data structures. */
123 struct machine_function * (*init_machine_status) (void);
125 /* The currently compiled function. */
126 struct function *cfun = 0;
128 /* These hashes record the prologue and epilogue insns. */
130 struct insn_cache_hasher : ggc_cache_hasher<rtx>
132 static hashval_t hash (rtx x) { return htab_hash_pointer (x); }
133 static bool equal (rtx a, rtx b) { return a == b; }
136 static GTY((cache))
137 hash_table<insn_cache_hasher> *prologue_insn_hash;
138 static GTY((cache))
139 hash_table<insn_cache_hasher> *epilogue_insn_hash;
142 hash_table<used_type_hasher> *types_used_by_vars_hash = NULL;
143 vec<tree, va_gc> *types_used_by_cur_var_decl;
145 /* Forward declarations. */
147 static struct temp_slot *find_temp_slot_from_address (rtx);
148 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
149 static void pad_below (struct args_size *, machine_mode, tree);
150 static void reorder_blocks_1 (rtx_insn *, tree, vec<tree> *);
151 static int all_blocks (tree, tree *);
152 static tree *get_block_vector (tree, int *);
153 extern tree debug_find_var_in_block_tree (tree, tree);
154 /* We always define `record_insns' even if it's not used so that we
155 can always export `prologue_epilogue_contains'. */
156 static void record_insns (rtx_insn *, rtx, hash_table<insn_cache_hasher> **)
157 ATTRIBUTE_UNUSED;
158 static bool contains (const_rtx, hash_table<insn_cache_hasher> *);
159 static void prepare_function_start (void);
160 static void do_clobber_return_reg (rtx, void *);
161 static void do_use_return_reg (rtx, void *);
163 /* Stack of nested functions. */
164 /* Keep track of the cfun stack. */
166 typedef struct function *function_p;
168 static vec<function_p> function_context_stack;
170 /* Save the current context for compilation of a nested function.
171 This is called from language-specific code. */
173 void
174 push_function_context (void)
176 if (cfun == 0)
177 allocate_struct_function (NULL, false);
179 function_context_stack.safe_push (cfun);
180 set_cfun (NULL);
183 /* Restore the last saved context, at the end of a nested function.
184 This function is called from language-specific code. */
186 void
187 pop_function_context (void)
189 struct function *p = function_context_stack.pop ();
190 set_cfun (p);
191 current_function_decl = p->decl;
193 /* Reset variables that have known state during rtx generation. */
194 virtuals_instantiated = 0;
195 generating_concat_p = 1;
198 /* Clear out all parts of the state in F that can safely be discarded
199 after the function has been parsed, but not compiled, to let
200 garbage collection reclaim the memory. */
202 void
203 free_after_parsing (struct function *f)
205 f->language = 0;
208 /* Clear out all parts of the state in F that can safely be discarded
209 after the function has been compiled, to let garbage collection
210 reclaim the memory. */
212 void
213 free_after_compilation (struct function *f)
215 prologue_insn_hash = NULL;
216 epilogue_insn_hash = NULL;
218 free (crtl->emit.regno_pointer_align);
220 memset (crtl, 0, sizeof (struct rtl_data));
221 f->eh = NULL;
222 f->machine = NULL;
223 f->cfg = NULL;
225 regno_reg_rtx = NULL;
228 /* Return size needed for stack frame based on slots so far allocated.
229 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
230 the caller may have to do that. */
232 HOST_WIDE_INT
233 get_frame_size (void)
235 if (FRAME_GROWS_DOWNWARD)
236 return -frame_offset;
237 else
238 return frame_offset;
241 /* Issue an error message and return TRUE if frame OFFSET overflows in
242 the signed target pointer arithmetics for function FUNC. Otherwise
243 return FALSE. */
245 bool
246 frame_offset_overflow (HOST_WIDE_INT offset, tree func)
248 unsigned HOST_WIDE_INT size = FRAME_GROWS_DOWNWARD ? -offset : offset;
250 if (size > ((unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (Pmode) - 1))
251 /* Leave room for the fixed part of the frame. */
252 - 64 * UNITS_PER_WORD)
254 error_at (DECL_SOURCE_LOCATION (func),
255 "total size of local objects too large");
256 return TRUE;
259 return FALSE;
262 /* Return stack slot alignment in bits for TYPE and MODE. */
264 static unsigned int
265 get_stack_local_alignment (tree type, machine_mode mode)
267 unsigned int alignment;
269 if (mode == BLKmode)
270 alignment = BIGGEST_ALIGNMENT;
271 else
272 alignment = GET_MODE_ALIGNMENT (mode);
274 /* Allow the frond-end to (possibly) increase the alignment of this
275 stack slot. */
276 if (! type)
277 type = lang_hooks.types.type_for_mode (mode, 0);
279 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
282 /* Determine whether it is possible to fit a stack slot of size SIZE and
283 alignment ALIGNMENT into an area in the stack frame that starts at
284 frame offset START and has a length of LENGTH. If so, store the frame
285 offset to be used for the stack slot in *POFFSET and return true;
286 return false otherwise. This function will extend the frame size when
287 given a start/length pair that lies at the end of the frame. */
289 static bool
290 try_fit_stack_local (HOST_WIDE_INT start, HOST_WIDE_INT length,
291 HOST_WIDE_INT size, unsigned int alignment,
292 HOST_WIDE_INT *poffset)
294 HOST_WIDE_INT this_frame_offset;
295 int frame_off, frame_alignment, frame_phase;
297 /* Calculate how many bytes the start of local variables is off from
298 stack alignment. */
299 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
300 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
301 frame_phase = frame_off ? frame_alignment - frame_off : 0;
303 /* Round the frame offset to the specified alignment. */
305 /* We must be careful here, since FRAME_OFFSET might be negative and
306 division with a negative dividend isn't as well defined as we might
307 like. So we instead assume that ALIGNMENT is a power of two and
308 use logical operations which are unambiguous. */
309 if (FRAME_GROWS_DOWNWARD)
310 this_frame_offset
311 = (FLOOR_ROUND (start + length - size - frame_phase,
312 (unsigned HOST_WIDE_INT) alignment)
313 + frame_phase);
314 else
315 this_frame_offset
316 = (CEIL_ROUND (start - frame_phase,
317 (unsigned HOST_WIDE_INT) alignment)
318 + frame_phase);
320 /* See if it fits. If this space is at the edge of the frame,
321 consider extending the frame to make it fit. Our caller relies on
322 this when allocating a new slot. */
323 if (frame_offset == start && this_frame_offset < frame_offset)
324 frame_offset = this_frame_offset;
325 else if (this_frame_offset < start)
326 return false;
327 else if (start + length == frame_offset
328 && this_frame_offset + size > start + length)
329 frame_offset = this_frame_offset + size;
330 else if (this_frame_offset + size > start + length)
331 return false;
333 *poffset = this_frame_offset;
334 return true;
337 /* Create a new frame_space structure describing free space in the stack
338 frame beginning at START and ending at END, and chain it into the
339 function's frame_space_list. */
341 static void
342 add_frame_space (HOST_WIDE_INT start, HOST_WIDE_INT end)
344 struct frame_space *space = ggc_alloc<frame_space> ();
345 space->next = crtl->frame_space_list;
346 crtl->frame_space_list = space;
347 space->start = start;
348 space->length = end - start;
351 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
352 with machine mode MODE.
354 ALIGN controls the amount of alignment for the address of the slot:
355 0 means according to MODE,
356 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
357 -2 means use BITS_PER_UNIT,
358 positive specifies alignment boundary in bits.
360 KIND has ASLK_REDUCE_ALIGN bit set if it is OK to reduce
361 alignment and ASLK_RECORD_PAD bit set if we should remember
362 extra space we allocated for alignment purposes. When we are
363 called from assign_stack_temp_for_type, it is not set so we don't
364 track the same stack slot in two independent lists.
366 We do not round to stack_boundary here. */
369 assign_stack_local_1 (machine_mode mode, HOST_WIDE_INT size,
370 int align, int kind)
372 rtx x, addr;
373 int bigend_correction = 0;
374 HOST_WIDE_INT slot_offset = 0, old_frame_offset;
375 unsigned int alignment, alignment_in_bits;
377 if (align == 0)
379 alignment = get_stack_local_alignment (NULL, mode);
380 alignment /= BITS_PER_UNIT;
382 else if (align == -1)
384 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
385 size = CEIL_ROUND (size, alignment);
387 else if (align == -2)
388 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
389 else
390 alignment = align / BITS_PER_UNIT;
392 alignment_in_bits = alignment * BITS_PER_UNIT;
394 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
395 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
397 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
398 alignment = alignment_in_bits / BITS_PER_UNIT;
401 if (SUPPORTS_STACK_ALIGNMENT)
403 if (crtl->stack_alignment_estimated < alignment_in_bits)
405 if (!crtl->stack_realign_processed)
406 crtl->stack_alignment_estimated = alignment_in_bits;
407 else
409 /* If stack is realigned and stack alignment value
410 hasn't been finalized, it is OK not to increase
411 stack_alignment_estimated. The bigger alignment
412 requirement is recorded in stack_alignment_needed
413 below. */
414 gcc_assert (!crtl->stack_realign_finalized);
415 if (!crtl->stack_realign_needed)
417 /* It is OK to reduce the alignment as long as the
418 requested size is 0 or the estimated stack
419 alignment >= mode alignment. */
420 gcc_assert ((kind & ASLK_REDUCE_ALIGN)
421 || size == 0
422 || (crtl->stack_alignment_estimated
423 >= GET_MODE_ALIGNMENT (mode)));
424 alignment_in_bits = crtl->stack_alignment_estimated;
425 alignment = alignment_in_bits / BITS_PER_UNIT;
431 if (crtl->stack_alignment_needed < alignment_in_bits)
432 crtl->stack_alignment_needed = alignment_in_bits;
433 if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
434 crtl->max_used_stack_slot_alignment = alignment_in_bits;
436 if (mode != BLKmode || size != 0)
438 if (kind & ASLK_RECORD_PAD)
440 struct frame_space **psp;
442 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
444 struct frame_space *space = *psp;
445 if (!try_fit_stack_local (space->start, space->length, size,
446 alignment, &slot_offset))
447 continue;
448 *psp = space->next;
449 if (slot_offset > space->start)
450 add_frame_space (space->start, slot_offset);
451 if (slot_offset + size < space->start + space->length)
452 add_frame_space (slot_offset + size,
453 space->start + space->length);
454 goto found_space;
458 else if (!STACK_ALIGNMENT_NEEDED)
460 slot_offset = frame_offset;
461 goto found_space;
464 old_frame_offset = frame_offset;
466 if (FRAME_GROWS_DOWNWARD)
468 frame_offset -= size;
469 try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
471 if (kind & ASLK_RECORD_PAD)
473 if (slot_offset > frame_offset)
474 add_frame_space (frame_offset, slot_offset);
475 if (slot_offset + size < old_frame_offset)
476 add_frame_space (slot_offset + size, old_frame_offset);
479 else
481 frame_offset += size;
482 try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
484 if (kind & ASLK_RECORD_PAD)
486 if (slot_offset > old_frame_offset)
487 add_frame_space (old_frame_offset, slot_offset);
488 if (slot_offset + size < frame_offset)
489 add_frame_space (slot_offset + size, frame_offset);
493 found_space:
494 /* On a big-endian machine, if we are allocating more space than we will use,
495 use the least significant bytes of those that are allocated. */
496 if (BYTES_BIG_ENDIAN && mode != BLKmode && GET_MODE_SIZE (mode) < size)
497 bigend_correction = size - GET_MODE_SIZE (mode);
499 /* If we have already instantiated virtual registers, return the actual
500 address relative to the frame pointer. */
501 if (virtuals_instantiated)
502 addr = plus_constant (Pmode, frame_pointer_rtx,
503 trunc_int_for_mode
504 (slot_offset + bigend_correction
505 + STARTING_FRAME_OFFSET, Pmode));
506 else
507 addr = plus_constant (Pmode, virtual_stack_vars_rtx,
508 trunc_int_for_mode
509 (slot_offset + bigend_correction,
510 Pmode));
512 x = gen_rtx_MEM (mode, addr);
513 set_mem_align (x, alignment_in_bits);
514 MEM_NOTRAP_P (x) = 1;
516 stack_slot_list
517 = gen_rtx_EXPR_LIST (VOIDmode, x, stack_slot_list);
519 if (frame_offset_overflow (frame_offset, current_function_decl))
520 frame_offset = 0;
522 return x;
525 /* Wrap up assign_stack_local_1 with last parameter as false. */
528 assign_stack_local (machine_mode mode, HOST_WIDE_INT size, int align)
530 return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
533 /* In order to evaluate some expressions, such as function calls returning
534 structures in memory, we need to temporarily allocate stack locations.
535 We record each allocated temporary in the following structure.
537 Associated with each temporary slot is a nesting level. When we pop up
538 one level, all temporaries associated with the previous level are freed.
539 Normally, all temporaries are freed after the execution of the statement
540 in which they were created. However, if we are inside a ({...}) grouping,
541 the result may be in a temporary and hence must be preserved. If the
542 result could be in a temporary, we preserve it if we can determine which
543 one it is in. If we cannot determine which temporary may contain the
544 result, all temporaries are preserved. A temporary is preserved by
545 pretending it was allocated at the previous nesting level. */
547 struct GTY(()) temp_slot {
548 /* Points to next temporary slot. */
549 struct temp_slot *next;
550 /* Points to previous temporary slot. */
551 struct temp_slot *prev;
552 /* The rtx to used to reference the slot. */
553 rtx slot;
554 /* The size, in units, of the slot. */
555 HOST_WIDE_INT size;
556 /* The type of the object in the slot, or zero if it doesn't correspond
557 to a type. We use this to determine whether a slot can be reused.
558 It can be reused if objects of the type of the new slot will always
559 conflict with objects of the type of the old slot. */
560 tree type;
561 /* The alignment (in bits) of the slot. */
562 unsigned int align;
563 /* Nonzero if this temporary is currently in use. */
564 char in_use;
565 /* Nesting level at which this slot is being used. */
566 int level;
567 /* The offset of the slot from the frame_pointer, including extra space
568 for alignment. This info is for combine_temp_slots. */
569 HOST_WIDE_INT base_offset;
570 /* The size of the slot, including extra space for alignment. This
571 info is for combine_temp_slots. */
572 HOST_WIDE_INT full_size;
575 /* Entry for the below hash table. */
576 struct GTY((for_user)) temp_slot_address_entry {
577 hashval_t hash;
578 rtx address;
579 struct temp_slot *temp_slot;
582 struct temp_address_hasher : ggc_hasher<temp_slot_address_entry *>
584 static hashval_t hash (temp_slot_address_entry *);
585 static bool equal (temp_slot_address_entry *, temp_slot_address_entry *);
588 /* A table of addresses that represent a stack slot. The table is a mapping
589 from address RTXen to a temp slot. */
590 static GTY(()) hash_table<temp_address_hasher> *temp_slot_address_table;
591 static size_t n_temp_slots_in_use;
593 /* Removes temporary slot TEMP from LIST. */
595 static void
596 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
598 if (temp->next)
599 temp->next->prev = temp->prev;
600 if (temp->prev)
601 temp->prev->next = temp->next;
602 else
603 *list = temp->next;
605 temp->prev = temp->next = NULL;
608 /* Inserts temporary slot TEMP to LIST. */
610 static void
611 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
613 temp->next = *list;
614 if (*list)
615 (*list)->prev = temp;
616 temp->prev = NULL;
617 *list = temp;
620 /* Returns the list of used temp slots at LEVEL. */
622 static struct temp_slot **
623 temp_slots_at_level (int level)
625 if (level >= (int) vec_safe_length (used_temp_slots))
626 vec_safe_grow_cleared (used_temp_slots, level + 1);
628 return &(*used_temp_slots)[level];
631 /* Returns the maximal temporary slot level. */
633 static int
634 max_slot_level (void)
636 if (!used_temp_slots)
637 return -1;
639 return used_temp_slots->length () - 1;
642 /* Moves temporary slot TEMP to LEVEL. */
644 static void
645 move_slot_to_level (struct temp_slot *temp, int level)
647 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
648 insert_slot_to_list (temp, temp_slots_at_level (level));
649 temp->level = level;
652 /* Make temporary slot TEMP available. */
654 static void
655 make_slot_available (struct temp_slot *temp)
657 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
658 insert_slot_to_list (temp, &avail_temp_slots);
659 temp->in_use = 0;
660 temp->level = -1;
661 n_temp_slots_in_use--;
664 /* Compute the hash value for an address -> temp slot mapping.
665 The value is cached on the mapping entry. */
666 static hashval_t
667 temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
669 int do_not_record = 0;
670 return hash_rtx (t->address, GET_MODE (t->address),
671 &do_not_record, NULL, false);
674 /* Return the hash value for an address -> temp slot mapping. */
675 hashval_t
676 temp_address_hasher::hash (temp_slot_address_entry *t)
678 return t->hash;
681 /* Compare two address -> temp slot mapping entries. */
682 bool
683 temp_address_hasher::equal (temp_slot_address_entry *t1,
684 temp_slot_address_entry *t2)
686 return exp_equiv_p (t1->address, t2->address, 0, true);
689 /* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
690 static void
691 insert_temp_slot_address (rtx address, struct temp_slot *temp_slot)
693 struct temp_slot_address_entry *t = ggc_alloc<temp_slot_address_entry> ();
694 t->address = address;
695 t->temp_slot = temp_slot;
696 t->hash = temp_slot_address_compute_hash (t);
697 *temp_slot_address_table->find_slot_with_hash (t, t->hash, INSERT) = t;
700 /* Remove an address -> temp slot mapping entry if the temp slot is
701 not in use anymore. Callback for remove_unused_temp_slot_addresses. */
703 remove_unused_temp_slot_addresses_1 (temp_slot_address_entry **slot, void *)
705 const struct temp_slot_address_entry *t = *slot;
706 if (! t->temp_slot->in_use)
707 temp_slot_address_table->clear_slot (slot);
708 return 1;
711 /* Remove all mappings of addresses to unused temp slots. */
712 static void
713 remove_unused_temp_slot_addresses (void)
715 /* Use quicker clearing if there aren't any active temp slots. */
716 if (n_temp_slots_in_use)
717 temp_slot_address_table->traverse
718 <void *, remove_unused_temp_slot_addresses_1> (NULL);
719 else
720 temp_slot_address_table->empty ();
723 /* Find the temp slot corresponding to the object at address X. */
725 static struct temp_slot *
726 find_temp_slot_from_address (rtx x)
728 struct temp_slot *p;
729 struct temp_slot_address_entry tmp, *t;
731 /* First try the easy way:
732 See if X exists in the address -> temp slot mapping. */
733 tmp.address = x;
734 tmp.temp_slot = NULL;
735 tmp.hash = temp_slot_address_compute_hash (&tmp);
736 t = temp_slot_address_table->find_with_hash (&tmp, tmp.hash);
737 if (t)
738 return t->temp_slot;
740 /* If we have a sum involving a register, see if it points to a temp
741 slot. */
742 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
743 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
744 return p;
745 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
746 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
747 return p;
749 /* Last resort: Address is a virtual stack var address. */
750 if (GET_CODE (x) == PLUS
751 && XEXP (x, 0) == virtual_stack_vars_rtx
752 && CONST_INT_P (XEXP (x, 1)))
754 int i;
755 for (i = max_slot_level (); i >= 0; i--)
756 for (p = *temp_slots_at_level (i); p; p = p->next)
758 if (INTVAL (XEXP (x, 1)) >= p->base_offset
759 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size)
760 return p;
764 return NULL;
767 /* Allocate a temporary stack slot and record it for possible later
768 reuse.
770 MODE is the machine mode to be given to the returned rtx.
772 SIZE is the size in units of the space required. We do no rounding here
773 since assign_stack_local will do any required rounding.
775 TYPE is the type that will be used for the stack slot. */
778 assign_stack_temp_for_type (machine_mode mode, HOST_WIDE_INT size,
779 tree type)
781 unsigned int align;
782 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
783 rtx slot;
785 /* If SIZE is -1 it means that somebody tried to allocate a temporary
786 of a variable size. */
787 gcc_assert (size != -1);
789 align = get_stack_local_alignment (type, mode);
791 /* Try to find an available, already-allocated temporary of the proper
792 mode which meets the size and alignment requirements. Choose the
793 smallest one with the closest alignment.
795 If assign_stack_temp is called outside of the tree->rtl expansion,
796 we cannot reuse the stack slots (that may still refer to
797 VIRTUAL_STACK_VARS_REGNUM). */
798 if (!virtuals_instantiated)
800 for (p = avail_temp_slots; p; p = p->next)
802 if (p->align >= align && p->size >= size
803 && GET_MODE (p->slot) == mode
804 && objects_must_conflict_p (p->type, type)
805 && (best_p == 0 || best_p->size > p->size
806 || (best_p->size == p->size && best_p->align > p->align)))
808 if (p->align == align && p->size == size)
810 selected = p;
811 cut_slot_from_list (selected, &avail_temp_slots);
812 best_p = 0;
813 break;
815 best_p = p;
820 /* Make our best, if any, the one to use. */
821 if (best_p)
823 selected = best_p;
824 cut_slot_from_list (selected, &avail_temp_slots);
826 /* If there are enough aligned bytes left over, make them into a new
827 temp_slot so that the extra bytes don't get wasted. Do this only
828 for BLKmode slots, so that we can be sure of the alignment. */
829 if (GET_MODE (best_p->slot) == BLKmode)
831 int alignment = best_p->align / BITS_PER_UNIT;
832 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
834 if (best_p->size - rounded_size >= alignment)
836 p = ggc_alloc<temp_slot> ();
837 p->in_use = 0;
838 p->size = best_p->size - rounded_size;
839 p->base_offset = best_p->base_offset + rounded_size;
840 p->full_size = best_p->full_size - rounded_size;
841 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
842 p->align = best_p->align;
843 p->type = best_p->type;
844 insert_slot_to_list (p, &avail_temp_slots);
846 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
847 stack_slot_list);
849 best_p->size = rounded_size;
850 best_p->full_size = rounded_size;
855 /* If we still didn't find one, make a new temporary. */
856 if (selected == 0)
858 HOST_WIDE_INT frame_offset_old = frame_offset;
860 p = ggc_alloc<temp_slot> ();
862 /* We are passing an explicit alignment request to assign_stack_local.
863 One side effect of that is assign_stack_local will not round SIZE
864 to ensure the frame offset remains suitably aligned.
866 So for requests which depended on the rounding of SIZE, we go ahead
867 and round it now. We also make sure ALIGNMENT is at least
868 BIGGEST_ALIGNMENT. */
869 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
870 p->slot = assign_stack_local_1 (mode,
871 (mode == BLKmode
872 ? CEIL_ROUND (size,
873 (int) align
874 / BITS_PER_UNIT)
875 : size),
876 align, 0);
878 p->align = align;
880 /* The following slot size computation is necessary because we don't
881 know the actual size of the temporary slot until assign_stack_local
882 has performed all the frame alignment and size rounding for the
883 requested temporary. Note that extra space added for alignment
884 can be either above or below this stack slot depending on which
885 way the frame grows. We include the extra space if and only if it
886 is above this slot. */
887 if (FRAME_GROWS_DOWNWARD)
888 p->size = frame_offset_old - frame_offset;
889 else
890 p->size = size;
892 /* Now define the fields used by combine_temp_slots. */
893 if (FRAME_GROWS_DOWNWARD)
895 p->base_offset = frame_offset;
896 p->full_size = frame_offset_old - frame_offset;
898 else
900 p->base_offset = frame_offset_old;
901 p->full_size = frame_offset - frame_offset_old;
904 selected = p;
907 p = selected;
908 p->in_use = 1;
909 p->type = type;
910 p->level = temp_slot_level;
911 n_temp_slots_in_use++;
913 pp = temp_slots_at_level (p->level);
914 insert_slot_to_list (p, pp);
915 insert_temp_slot_address (XEXP (p->slot, 0), p);
917 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
918 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
919 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
921 /* If we know the alias set for the memory that will be used, use
922 it. If there's no TYPE, then we don't know anything about the
923 alias set for the memory. */
924 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
925 set_mem_align (slot, align);
927 /* If a type is specified, set the relevant flags. */
928 if (type != 0)
929 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
930 MEM_NOTRAP_P (slot) = 1;
932 return slot;
935 /* Allocate a temporary stack slot and record it for possible later
936 reuse. First two arguments are same as in preceding function. */
939 assign_stack_temp (machine_mode mode, HOST_WIDE_INT size)
941 return assign_stack_temp_for_type (mode, size, NULL_TREE);
944 /* Assign a temporary.
945 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
946 and so that should be used in error messages. In either case, we
947 allocate of the given type.
948 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
949 it is 0 if a register is OK.
950 DONT_PROMOTE is 1 if we should not promote values in register
951 to wider modes. */
954 assign_temp (tree type_or_decl, int memory_required,
955 int dont_promote ATTRIBUTE_UNUSED)
957 tree type, decl;
958 machine_mode mode;
959 #ifdef PROMOTE_MODE
960 int unsignedp;
961 #endif
963 if (DECL_P (type_or_decl))
964 decl = type_or_decl, type = TREE_TYPE (decl);
965 else
966 decl = NULL, type = type_or_decl;
968 mode = TYPE_MODE (type);
969 #ifdef PROMOTE_MODE
970 unsignedp = TYPE_UNSIGNED (type);
971 #endif
973 if (mode == BLKmode || memory_required)
975 HOST_WIDE_INT size = int_size_in_bytes (type);
976 rtx tmp;
978 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
979 problems with allocating the stack space. */
980 if (size == 0)
981 size = 1;
983 /* Unfortunately, we don't yet know how to allocate variable-sized
984 temporaries. However, sometimes we can find a fixed upper limit on
985 the size, so try that instead. */
986 else if (size == -1)
987 size = max_int_size_in_bytes (type);
989 /* The size of the temporary may be too large to fit into an integer. */
990 /* ??? Not sure this should happen except for user silliness, so limit
991 this to things that aren't compiler-generated temporaries. The
992 rest of the time we'll die in assign_stack_temp_for_type. */
993 if (decl && size == -1
994 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
996 error ("size of variable %q+D is too large", decl);
997 size = 1;
1000 tmp = assign_stack_temp_for_type (mode, size, type);
1001 return tmp;
1004 #ifdef PROMOTE_MODE
1005 if (! dont_promote)
1006 mode = promote_mode (type, mode, &unsignedp);
1007 #endif
1009 return gen_reg_rtx (mode);
1012 /* Combine temporary stack slots which are adjacent on the stack.
1014 This allows for better use of already allocated stack space. This is only
1015 done for BLKmode slots because we can be sure that we won't have alignment
1016 problems in this case. */
1018 static void
1019 combine_temp_slots (void)
1021 struct temp_slot *p, *q, *next, *next_q;
1022 int num_slots;
1024 /* We can't combine slots, because the information about which slot
1025 is in which alias set will be lost. */
1026 if (flag_strict_aliasing)
1027 return;
1029 /* If there are a lot of temp slots, don't do anything unless
1030 high levels of optimization. */
1031 if (! flag_expensive_optimizations)
1032 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
1033 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1034 return;
1036 for (p = avail_temp_slots; p; p = next)
1038 int delete_p = 0;
1040 next = p->next;
1042 if (GET_MODE (p->slot) != BLKmode)
1043 continue;
1045 for (q = p->next; q; q = next_q)
1047 int delete_q = 0;
1049 next_q = q->next;
1051 if (GET_MODE (q->slot) != BLKmode)
1052 continue;
1054 if (p->base_offset + p->full_size == q->base_offset)
1056 /* Q comes after P; combine Q into P. */
1057 p->size += q->size;
1058 p->full_size += q->full_size;
1059 delete_q = 1;
1061 else if (q->base_offset + q->full_size == p->base_offset)
1063 /* P comes after Q; combine P into Q. */
1064 q->size += p->size;
1065 q->full_size += p->full_size;
1066 delete_p = 1;
1067 break;
1069 if (delete_q)
1070 cut_slot_from_list (q, &avail_temp_slots);
1073 /* Either delete P or advance past it. */
1074 if (delete_p)
1075 cut_slot_from_list (p, &avail_temp_slots);
1079 /* Indicate that NEW_RTX is an alternate way of referring to the temp
1080 slot that previously was known by OLD_RTX. */
1082 void
1083 update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1085 struct temp_slot *p;
1087 if (rtx_equal_p (old_rtx, new_rtx))
1088 return;
1090 p = find_temp_slot_from_address (old_rtx);
1092 /* If we didn't find one, see if both OLD_RTX is a PLUS. If so, and
1093 NEW_RTX is a register, see if one operand of the PLUS is a
1094 temporary location. If so, NEW_RTX points into it. Otherwise,
1095 if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1096 in common between them. If so, try a recursive call on those
1097 values. */
1098 if (p == 0)
1100 if (GET_CODE (old_rtx) != PLUS)
1101 return;
1103 if (REG_P (new_rtx))
1105 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1106 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1107 return;
1109 else if (GET_CODE (new_rtx) != PLUS)
1110 return;
1112 if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1113 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1114 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1115 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1116 else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1117 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1118 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1119 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
1121 return;
1124 /* Otherwise add an alias for the temp's address. */
1125 insert_temp_slot_address (new_rtx, p);
1128 /* If X could be a reference to a temporary slot, mark that slot as
1129 belonging to the to one level higher than the current level. If X
1130 matched one of our slots, just mark that one. Otherwise, we can't
1131 easily predict which it is, so upgrade all of them.
1133 This is called when an ({...}) construct occurs and a statement
1134 returns a value in memory. */
1136 void
1137 preserve_temp_slots (rtx x)
1139 struct temp_slot *p = 0, *next;
1141 if (x == 0)
1142 return;
1144 /* If X is a register that is being used as a pointer, see if we have
1145 a temporary slot we know it points to. */
1146 if (REG_P (x) && REG_POINTER (x))
1147 p = find_temp_slot_from_address (x);
1149 /* If X is not in memory or is at a constant address, it cannot be in
1150 a temporary slot. */
1151 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1152 return;
1154 /* First see if we can find a match. */
1155 if (p == 0)
1156 p = find_temp_slot_from_address (XEXP (x, 0));
1158 if (p != 0)
1160 if (p->level == temp_slot_level)
1161 move_slot_to_level (p, temp_slot_level - 1);
1162 return;
1165 /* Otherwise, preserve all non-kept slots at this level. */
1166 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1168 next = p->next;
1169 move_slot_to_level (p, temp_slot_level - 1);
1173 /* Free all temporaries used so far. This is normally called at the
1174 end of generating code for a statement. */
1176 void
1177 free_temp_slots (void)
1179 struct temp_slot *p, *next;
1180 bool some_available = false;
1182 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1184 next = p->next;
1185 make_slot_available (p);
1186 some_available = true;
1189 if (some_available)
1191 remove_unused_temp_slot_addresses ();
1192 combine_temp_slots ();
1196 /* Push deeper into the nesting level for stack temporaries. */
1198 void
1199 push_temp_slots (void)
1201 temp_slot_level++;
1204 /* Pop a temporary nesting level. All slots in use in the current level
1205 are freed. */
1207 void
1208 pop_temp_slots (void)
1210 free_temp_slots ();
1211 temp_slot_level--;
1214 /* Initialize temporary slots. */
1216 void
1217 init_temp_slots (void)
1219 /* We have not allocated any temporaries yet. */
1220 avail_temp_slots = 0;
1221 vec_alloc (used_temp_slots, 0);
1222 temp_slot_level = 0;
1223 n_temp_slots_in_use = 0;
1225 /* Set up the table to map addresses to temp slots. */
1226 if (! temp_slot_address_table)
1227 temp_slot_address_table = hash_table<temp_address_hasher>::create_ggc (32);
1228 else
1229 temp_slot_address_table->empty ();
1232 /* Functions and data structures to keep track of the values hard regs
1233 had at the start of the function. */
1235 /* Private type used by get_hard_reg_initial_reg, get_hard_reg_initial_val,
1236 and has_hard_reg_initial_val.. */
1237 typedef struct GTY(()) initial_value_pair {
1238 rtx hard_reg;
1239 rtx pseudo;
1240 } initial_value_pair;
1241 /* ??? This could be a VEC but there is currently no way to define an
1242 opaque VEC type. This could be worked around by defining struct
1243 initial_value_pair in function.h. */
1244 typedef struct GTY(()) initial_value_struct {
1245 int num_entries;
1246 int max_entries;
1247 initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
1248 } initial_value_struct;
1250 /* If a pseudo represents an initial hard reg (or expression), return
1251 it, else return NULL_RTX. */
1254 get_hard_reg_initial_reg (rtx reg)
1256 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1257 int i;
1259 if (ivs == 0)
1260 return NULL_RTX;
1262 for (i = 0; i < ivs->num_entries; i++)
1263 if (rtx_equal_p (ivs->entries[i].pseudo, reg))
1264 return ivs->entries[i].hard_reg;
1266 return NULL_RTX;
1269 /* Make sure that there's a pseudo register of mode MODE that stores the
1270 initial value of hard register REGNO. Return an rtx for such a pseudo. */
1273 get_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1275 struct initial_value_struct *ivs;
1276 rtx rv;
1278 rv = has_hard_reg_initial_val (mode, regno);
1279 if (rv)
1280 return rv;
1282 ivs = crtl->hard_reg_initial_vals;
1283 if (ivs == 0)
1285 ivs = ggc_alloc<initial_value_struct> ();
1286 ivs->num_entries = 0;
1287 ivs->max_entries = 5;
1288 ivs->entries = ggc_vec_alloc<initial_value_pair> (5);
1289 crtl->hard_reg_initial_vals = ivs;
1292 if (ivs->num_entries >= ivs->max_entries)
1294 ivs->max_entries += 5;
1295 ivs->entries = GGC_RESIZEVEC (initial_value_pair, ivs->entries,
1296 ivs->max_entries);
1299 ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
1300 ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
1302 return ivs->entries[ivs->num_entries++].pseudo;
1305 /* See if get_hard_reg_initial_val has been used to create a pseudo
1306 for the initial value of hard register REGNO in mode MODE. Return
1307 the associated pseudo if so, otherwise return NULL. */
1310 has_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1312 struct initial_value_struct *ivs;
1313 int i;
1315 ivs = crtl->hard_reg_initial_vals;
1316 if (ivs != 0)
1317 for (i = 0; i < ivs->num_entries; i++)
1318 if (GET_MODE (ivs->entries[i].hard_reg) == mode
1319 && REGNO (ivs->entries[i].hard_reg) == regno)
1320 return ivs->entries[i].pseudo;
1322 return NULL_RTX;
1325 unsigned int
1326 emit_initial_value_sets (void)
1328 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1329 int i;
1330 rtx_insn *seq;
1332 if (ivs == 0)
1333 return 0;
1335 start_sequence ();
1336 for (i = 0; i < ivs->num_entries; i++)
1337 emit_move_insn (ivs->entries[i].pseudo, ivs->entries[i].hard_reg);
1338 seq = get_insns ();
1339 end_sequence ();
1341 emit_insn_at_entry (seq);
1342 return 0;
1345 /* Return the hardreg-pseudoreg initial values pair entry I and
1346 TRUE if I is a valid entry, or FALSE if I is not a valid entry. */
1347 bool
1348 initial_value_entry (int i, rtx *hreg, rtx *preg)
1350 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1351 if (!ivs || i >= ivs->num_entries)
1352 return false;
1354 *hreg = ivs->entries[i].hard_reg;
1355 *preg = ivs->entries[i].pseudo;
1356 return true;
1359 /* These routines are responsible for converting virtual register references
1360 to the actual hard register references once RTL generation is complete.
1362 The following four variables are used for communication between the
1363 routines. They contain the offsets of the virtual registers from their
1364 respective hard registers. */
1366 static int in_arg_offset;
1367 static int var_offset;
1368 static int dynamic_offset;
1369 static int out_arg_offset;
1370 static int cfa_offset;
1372 /* In most machines, the stack pointer register is equivalent to the bottom
1373 of the stack. */
1375 #ifndef STACK_POINTER_OFFSET
1376 #define STACK_POINTER_OFFSET 0
1377 #endif
1379 #if defined (REG_PARM_STACK_SPACE) && !defined (INCOMING_REG_PARM_STACK_SPACE)
1380 #define INCOMING_REG_PARM_STACK_SPACE REG_PARM_STACK_SPACE
1381 #endif
1383 /* If not defined, pick an appropriate default for the offset of dynamically
1384 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1385 INCOMING_REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1387 #ifndef STACK_DYNAMIC_OFFSET
1389 /* The bottom of the stack points to the actual arguments. If
1390 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1391 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1392 stack space for register parameters is not pushed by the caller, but
1393 rather part of the fixed stack areas and hence not included in
1394 `crtl->outgoing_args_size'. Nevertheless, we must allow
1395 for it when allocating stack dynamic objects. */
1397 #ifdef INCOMING_REG_PARM_STACK_SPACE
1398 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1399 ((ACCUMULATE_OUTGOING_ARGS \
1400 ? (crtl->outgoing_args_size \
1401 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1402 : INCOMING_REG_PARM_STACK_SPACE (FNDECL))) \
1403 : 0) + (STACK_POINTER_OFFSET))
1404 #else
1405 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1406 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0) \
1407 + (STACK_POINTER_OFFSET))
1408 #endif
1409 #endif
1412 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1413 is a virtual register, return the equivalent hard register and set the
1414 offset indirectly through the pointer. Otherwise, return 0. */
1416 static rtx
1417 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
1419 rtx new_rtx;
1420 HOST_WIDE_INT offset;
1422 if (x == virtual_incoming_args_rtx)
1424 if (stack_realign_drap)
1426 /* Replace virtual_incoming_args_rtx with internal arg
1427 pointer if DRAP is used to realign stack. */
1428 new_rtx = crtl->args.internal_arg_pointer;
1429 offset = 0;
1431 else
1432 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1434 else if (x == virtual_stack_vars_rtx)
1435 new_rtx = frame_pointer_rtx, offset = var_offset;
1436 else if (x == virtual_stack_dynamic_rtx)
1437 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1438 else if (x == virtual_outgoing_args_rtx)
1439 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1440 else if (x == virtual_cfa_rtx)
1442 #ifdef FRAME_POINTER_CFA_OFFSET
1443 new_rtx = frame_pointer_rtx;
1444 #else
1445 new_rtx = arg_pointer_rtx;
1446 #endif
1447 offset = cfa_offset;
1449 else if (x == virtual_preferred_stack_boundary_rtx)
1451 new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1452 offset = 0;
1454 else
1455 return NULL_RTX;
1457 *poffset = offset;
1458 return new_rtx;
1461 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1462 registers present inside of *LOC. The expression is simplified,
1463 as much as possible, but is not to be considered "valid" in any sense
1464 implied by the target. Return true if any change is made. */
1466 static bool
1467 instantiate_virtual_regs_in_rtx (rtx *loc)
1469 if (!*loc)
1470 return false;
1471 bool changed = false;
1472 subrtx_ptr_iterator::array_type array;
1473 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
1475 rtx *loc = *iter;
1476 if (rtx x = *loc)
1478 rtx new_rtx;
1479 HOST_WIDE_INT offset;
1480 switch (GET_CODE (x))
1482 case REG:
1483 new_rtx = instantiate_new_reg (x, &offset);
1484 if (new_rtx)
1486 *loc = plus_constant (GET_MODE (x), new_rtx, offset);
1487 changed = true;
1489 iter.skip_subrtxes ();
1490 break;
1492 case PLUS:
1493 new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1494 if (new_rtx)
1496 XEXP (x, 0) = new_rtx;
1497 *loc = plus_constant (GET_MODE (x), x, offset, true);
1498 changed = true;
1499 iter.skip_subrtxes ();
1500 break;
1503 /* FIXME -- from old code */
1504 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1505 we can commute the PLUS and SUBREG because pointers into the
1506 frame are well-behaved. */
1507 break;
1509 default:
1510 break;
1514 return changed;
1517 /* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1518 matches the predicate for insn CODE operand OPERAND. */
1520 static int
1521 safe_insn_predicate (int code, int operand, rtx x)
1523 return code < 0 || insn_operand_matches ((enum insn_code) code, operand, x);
1526 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1527 registers present inside of insn. The result will be a valid insn. */
1529 static void
1530 instantiate_virtual_regs_in_insn (rtx_insn *insn)
1532 HOST_WIDE_INT offset;
1533 int insn_code, i;
1534 bool any_change = false;
1535 rtx set, new_rtx, x;
1536 rtx_insn *seq;
1538 /* There are some special cases to be handled first. */
1539 set = single_set (insn);
1540 if (set)
1542 /* We're allowed to assign to a virtual register. This is interpreted
1543 to mean that the underlying register gets assigned the inverse
1544 transformation. This is used, for example, in the handling of
1545 non-local gotos. */
1546 new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1547 if (new_rtx)
1549 start_sequence ();
1551 instantiate_virtual_regs_in_rtx (&SET_SRC (set));
1552 x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
1553 gen_int_mode (-offset, GET_MODE (new_rtx)));
1554 x = force_operand (x, new_rtx);
1555 if (x != new_rtx)
1556 emit_move_insn (new_rtx, x);
1558 seq = get_insns ();
1559 end_sequence ();
1561 emit_insn_before (seq, insn);
1562 delete_insn (insn);
1563 return;
1566 /* Handle a straight copy from a virtual register by generating a
1567 new add insn. The difference between this and falling through
1568 to the generic case is avoiding a new pseudo and eliminating a
1569 move insn in the initial rtl stream. */
1570 new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1571 if (new_rtx && offset != 0
1572 && REG_P (SET_DEST (set))
1573 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1575 start_sequence ();
1577 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS, new_rtx,
1578 gen_int_mode (offset,
1579 GET_MODE (SET_DEST (set))),
1580 SET_DEST (set), 1, OPTAB_LIB_WIDEN);
1581 if (x != SET_DEST (set))
1582 emit_move_insn (SET_DEST (set), x);
1584 seq = get_insns ();
1585 end_sequence ();
1587 emit_insn_before (seq, insn);
1588 delete_insn (insn);
1589 return;
1592 extract_insn (insn);
1593 insn_code = INSN_CODE (insn);
1595 /* Handle a plus involving a virtual register by determining if the
1596 operands remain valid if they're modified in place. */
1597 if (GET_CODE (SET_SRC (set)) == PLUS
1598 && recog_data.n_operands >= 3
1599 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1600 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1601 && CONST_INT_P (recog_data.operand[2])
1602 && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
1604 offset += INTVAL (recog_data.operand[2]);
1606 /* If the sum is zero, then replace with a plain move. */
1607 if (offset == 0
1608 && REG_P (SET_DEST (set))
1609 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1611 start_sequence ();
1612 emit_move_insn (SET_DEST (set), new_rtx);
1613 seq = get_insns ();
1614 end_sequence ();
1616 emit_insn_before (seq, insn);
1617 delete_insn (insn);
1618 return;
1621 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1623 /* Using validate_change and apply_change_group here leaves
1624 recog_data in an invalid state. Since we know exactly what
1625 we want to check, do those two by hand. */
1626 if (safe_insn_predicate (insn_code, 1, new_rtx)
1627 && safe_insn_predicate (insn_code, 2, x))
1629 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1630 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1631 any_change = true;
1633 /* Fall through into the regular operand fixup loop in
1634 order to take care of operands other than 1 and 2. */
1638 else
1640 extract_insn (insn);
1641 insn_code = INSN_CODE (insn);
1644 /* In the general case, we expect virtual registers to appear only in
1645 operands, and then only as either bare registers or inside memories. */
1646 for (i = 0; i < recog_data.n_operands; ++i)
1648 x = recog_data.operand[i];
1649 switch (GET_CODE (x))
1651 case MEM:
1653 rtx addr = XEXP (x, 0);
1655 if (!instantiate_virtual_regs_in_rtx (&addr))
1656 continue;
1658 start_sequence ();
1659 x = replace_equiv_address (x, addr, true);
1660 /* It may happen that the address with the virtual reg
1661 was valid (e.g. based on the virtual stack reg, which might
1662 be acceptable to the predicates with all offsets), whereas
1663 the address now isn't anymore, for instance when the address
1664 is still offsetted, but the base reg isn't virtual-stack-reg
1665 anymore. Below we would do a force_reg on the whole operand,
1666 but this insn might actually only accept memory. Hence,
1667 before doing that last resort, try to reload the address into
1668 a register, so this operand stays a MEM. */
1669 if (!safe_insn_predicate (insn_code, i, x))
1671 addr = force_reg (GET_MODE (addr), addr);
1672 x = replace_equiv_address (x, addr, true);
1674 seq = get_insns ();
1675 end_sequence ();
1676 if (seq)
1677 emit_insn_before (seq, insn);
1679 break;
1681 case REG:
1682 new_rtx = instantiate_new_reg (x, &offset);
1683 if (new_rtx == NULL)
1684 continue;
1685 if (offset == 0)
1686 x = new_rtx;
1687 else
1689 start_sequence ();
1691 /* Careful, special mode predicates may have stuff in
1692 insn_data[insn_code].operand[i].mode that isn't useful
1693 to us for computing a new value. */
1694 /* ??? Recognize address_operand and/or "p" constraints
1695 to see if (plus new offset) is a valid before we put
1696 this through expand_simple_binop. */
1697 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1698 gen_int_mode (offset, GET_MODE (x)),
1699 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1700 seq = get_insns ();
1701 end_sequence ();
1702 emit_insn_before (seq, insn);
1704 break;
1706 case SUBREG:
1707 new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1708 if (new_rtx == NULL)
1709 continue;
1710 if (offset != 0)
1712 start_sequence ();
1713 new_rtx = expand_simple_binop
1714 (GET_MODE (new_rtx), PLUS, new_rtx,
1715 gen_int_mode (offset, GET_MODE (new_rtx)),
1716 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1717 seq = get_insns ();
1718 end_sequence ();
1719 emit_insn_before (seq, insn);
1721 x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1722 GET_MODE (new_rtx), SUBREG_BYTE (x));
1723 gcc_assert (x);
1724 break;
1726 default:
1727 continue;
1730 /* At this point, X contains the new value for the operand.
1731 Validate the new value vs the insn predicate. Note that
1732 asm insns will have insn_code -1 here. */
1733 if (!safe_insn_predicate (insn_code, i, x))
1735 start_sequence ();
1736 if (REG_P (x))
1738 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1739 x = copy_to_reg (x);
1741 else
1742 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1743 seq = get_insns ();
1744 end_sequence ();
1745 if (seq)
1746 emit_insn_before (seq, insn);
1749 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1750 any_change = true;
1753 if (any_change)
1755 /* Propagate operand changes into the duplicates. */
1756 for (i = 0; i < recog_data.n_dups; ++i)
1757 *recog_data.dup_loc[i]
1758 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1760 /* Force re-recognition of the instruction for validation. */
1761 INSN_CODE (insn) = -1;
1764 if (asm_noperands (PATTERN (insn)) >= 0)
1766 if (!check_asm_operands (PATTERN (insn)))
1768 error_for_asm (insn, "impossible constraint in %<asm%>");
1769 /* For asm goto, instead of fixing up all the edges
1770 just clear the template and clear input operands
1771 (asm goto doesn't have any output operands). */
1772 if (JUMP_P (insn))
1774 rtx asm_op = extract_asm_operands (PATTERN (insn));
1775 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1776 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1777 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1779 else
1780 delete_insn (insn);
1783 else
1785 if (recog_memoized (insn) < 0)
1786 fatal_insn_not_found (insn);
1790 /* Subroutine of instantiate_decls. Given RTL representing a decl,
1791 do any instantiation required. */
1793 void
1794 instantiate_decl_rtl (rtx x)
1796 rtx addr;
1798 if (x == 0)
1799 return;
1801 /* If this is a CONCAT, recurse for the pieces. */
1802 if (GET_CODE (x) == CONCAT)
1804 instantiate_decl_rtl (XEXP (x, 0));
1805 instantiate_decl_rtl (XEXP (x, 1));
1806 return;
1809 /* If this is not a MEM, no need to do anything. Similarly if the
1810 address is a constant or a register that is not a virtual register. */
1811 if (!MEM_P (x))
1812 return;
1814 addr = XEXP (x, 0);
1815 if (CONSTANT_P (addr)
1816 || (REG_P (addr)
1817 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1818 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1819 return;
1821 instantiate_virtual_regs_in_rtx (&XEXP (x, 0));
1824 /* Helper for instantiate_decls called via walk_tree: Process all decls
1825 in the given DECL_VALUE_EXPR. */
1827 static tree
1828 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1830 tree t = *tp;
1831 if (! EXPR_P (t))
1833 *walk_subtrees = 0;
1834 if (DECL_P (t))
1836 if (DECL_RTL_SET_P (t))
1837 instantiate_decl_rtl (DECL_RTL (t));
1838 if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1839 && DECL_INCOMING_RTL (t))
1840 instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1841 if ((TREE_CODE (t) == VAR_DECL
1842 || TREE_CODE (t) == RESULT_DECL)
1843 && DECL_HAS_VALUE_EXPR_P (t))
1845 tree v = DECL_VALUE_EXPR (t);
1846 walk_tree (&v, instantiate_expr, NULL, NULL);
1850 return NULL;
1853 /* Subroutine of instantiate_decls: Process all decls in the given
1854 BLOCK node and all its subblocks. */
1856 static void
1857 instantiate_decls_1 (tree let)
1859 tree t;
1861 for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1863 if (DECL_RTL_SET_P (t))
1864 instantiate_decl_rtl (DECL_RTL (t));
1865 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t))
1867 tree v = DECL_VALUE_EXPR (t);
1868 walk_tree (&v, instantiate_expr, NULL, NULL);
1872 /* Process all subblocks. */
1873 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1874 instantiate_decls_1 (t);
1877 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1878 all virtual registers in their DECL_RTL's. */
1880 static void
1881 instantiate_decls (tree fndecl)
1883 tree decl;
1884 unsigned ix;
1886 /* Process all parameters of the function. */
1887 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1889 instantiate_decl_rtl (DECL_RTL (decl));
1890 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1891 if (DECL_HAS_VALUE_EXPR_P (decl))
1893 tree v = DECL_VALUE_EXPR (decl);
1894 walk_tree (&v, instantiate_expr, NULL, NULL);
1898 if ((decl = DECL_RESULT (fndecl))
1899 && TREE_CODE (decl) == RESULT_DECL)
1901 if (DECL_RTL_SET_P (decl))
1902 instantiate_decl_rtl (DECL_RTL (decl));
1903 if (DECL_HAS_VALUE_EXPR_P (decl))
1905 tree v = DECL_VALUE_EXPR (decl);
1906 walk_tree (&v, instantiate_expr, NULL, NULL);
1910 /* Process the saved static chain if it exists. */
1911 decl = DECL_STRUCT_FUNCTION (fndecl)->static_chain_decl;
1912 if (decl && DECL_HAS_VALUE_EXPR_P (decl))
1913 instantiate_decl_rtl (DECL_RTL (DECL_VALUE_EXPR (decl)));
1915 /* Now process all variables defined in the function or its subblocks. */
1916 instantiate_decls_1 (DECL_INITIAL (fndecl));
1918 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1919 if (DECL_RTL_SET_P (decl))
1920 instantiate_decl_rtl (DECL_RTL (decl));
1921 vec_free (cfun->local_decls);
1924 /* Pass through the INSNS of function FNDECL and convert virtual register
1925 references to hard register references. */
1927 static unsigned int
1928 instantiate_virtual_regs (void)
1930 rtx_insn *insn;
1932 /* Compute the offsets to use for this function. */
1933 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1934 var_offset = STARTING_FRAME_OFFSET;
1935 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1936 out_arg_offset = STACK_POINTER_OFFSET;
1937 #ifdef FRAME_POINTER_CFA_OFFSET
1938 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1939 #else
1940 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1941 #endif
1943 /* Initialize recognition, indicating that volatile is OK. */
1944 init_recog ();
1946 /* Scan through all the insns, instantiating every virtual register still
1947 present. */
1948 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1949 if (INSN_P (insn))
1951 /* These patterns in the instruction stream can never be recognized.
1952 Fortunately, they shouldn't contain virtual registers either. */
1953 if (GET_CODE (PATTERN (insn)) == USE
1954 || GET_CODE (PATTERN (insn)) == CLOBBER
1955 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
1956 continue;
1957 else if (DEBUG_INSN_P (insn))
1958 instantiate_virtual_regs_in_rtx (&INSN_VAR_LOCATION (insn));
1959 else
1960 instantiate_virtual_regs_in_insn (insn);
1962 if (insn->deleted ())
1963 continue;
1965 instantiate_virtual_regs_in_rtx (&REG_NOTES (insn));
1967 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
1968 if (CALL_P (insn))
1969 instantiate_virtual_regs_in_rtx (&CALL_INSN_FUNCTION_USAGE (insn));
1972 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
1973 instantiate_decls (current_function_decl);
1975 targetm.instantiate_decls ();
1977 /* Indicate that, from now on, assign_stack_local should use
1978 frame_pointer_rtx. */
1979 virtuals_instantiated = 1;
1981 return 0;
1984 namespace {
1986 const pass_data pass_data_instantiate_virtual_regs =
1988 RTL_PASS, /* type */
1989 "vregs", /* name */
1990 OPTGROUP_NONE, /* optinfo_flags */
1991 TV_NONE, /* tv_id */
1992 0, /* properties_required */
1993 0, /* properties_provided */
1994 0, /* properties_destroyed */
1995 0, /* todo_flags_start */
1996 0, /* todo_flags_finish */
1999 class pass_instantiate_virtual_regs : public rtl_opt_pass
2001 public:
2002 pass_instantiate_virtual_regs (gcc::context *ctxt)
2003 : rtl_opt_pass (pass_data_instantiate_virtual_regs, ctxt)
2006 /* opt_pass methods: */
2007 virtual unsigned int execute (function *)
2009 return instantiate_virtual_regs ();
2012 }; // class pass_instantiate_virtual_regs
2014 } // anon namespace
2016 rtl_opt_pass *
2017 make_pass_instantiate_virtual_regs (gcc::context *ctxt)
2019 return new pass_instantiate_virtual_regs (ctxt);
2023 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
2024 This means a type for which function calls must pass an address to the
2025 function or get an address back from the function.
2026 EXP may be a type node or an expression (whose type is tested). */
2029 aggregate_value_p (const_tree exp, const_tree fntype)
2031 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
2032 int i, regno, nregs;
2033 rtx reg;
2035 if (fntype)
2036 switch (TREE_CODE (fntype))
2038 case CALL_EXPR:
2040 tree fndecl = get_callee_fndecl (fntype);
2041 if (fndecl)
2042 fntype = TREE_TYPE (fndecl);
2043 else if (CALL_EXPR_FN (fntype))
2044 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype)));
2045 else
2046 /* For internal functions, assume nothing needs to be
2047 returned in memory. */
2048 return 0;
2050 break;
2051 case FUNCTION_DECL:
2052 fntype = TREE_TYPE (fntype);
2053 break;
2054 case FUNCTION_TYPE:
2055 case METHOD_TYPE:
2056 break;
2057 case IDENTIFIER_NODE:
2058 fntype = NULL_TREE;
2059 break;
2060 default:
2061 /* We don't expect other tree types here. */
2062 gcc_unreachable ();
2065 if (VOID_TYPE_P (type))
2066 return 0;
2068 /* If a record should be passed the same as its first (and only) member
2069 don't pass it as an aggregate. */
2070 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2071 return aggregate_value_p (first_field (type), fntype);
2073 /* If the front end has decided that this needs to be passed by
2074 reference, do so. */
2075 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2076 && DECL_BY_REFERENCE (exp))
2077 return 1;
2079 /* Function types that are TREE_ADDRESSABLE force return in memory. */
2080 if (fntype && TREE_ADDRESSABLE (fntype))
2081 return 1;
2083 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2084 and thus can't be returned in registers. */
2085 if (TREE_ADDRESSABLE (type))
2086 return 1;
2088 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2089 return 1;
2091 if (targetm.calls.return_in_memory (type, fntype))
2092 return 1;
2094 /* Make sure we have suitable call-clobbered regs to return
2095 the value in; if not, we must return it in memory. */
2096 reg = hard_function_value (type, 0, fntype, 0);
2098 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2099 it is OK. */
2100 if (!REG_P (reg))
2101 return 0;
2103 regno = REGNO (reg);
2104 nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
2105 for (i = 0; i < nregs; i++)
2106 if (! call_used_regs[regno + i])
2107 return 1;
2109 return 0;
2112 /* Return true if we should assign DECL a pseudo register; false if it
2113 should live on the local stack. */
2115 bool
2116 use_register_for_decl (const_tree decl)
2118 if (!targetm.calls.allocate_stack_slots_for_args ())
2119 return true;
2121 /* Honor volatile. */
2122 if (TREE_SIDE_EFFECTS (decl))
2123 return false;
2125 /* Honor addressability. */
2126 if (TREE_ADDRESSABLE (decl))
2127 return false;
2129 /* Decl is implicitly addressible by bound stores and loads
2130 if it is an aggregate holding bounds. */
2131 if (chkp_function_instrumented_p (current_function_decl)
2132 && TREE_TYPE (decl)
2133 && !BOUNDED_P (decl)
2134 && chkp_type_has_pointer (TREE_TYPE (decl)))
2135 return false;
2137 /* Only register-like things go in registers. */
2138 if (DECL_MODE (decl) == BLKmode)
2139 return false;
2141 /* If -ffloat-store specified, don't put explicit float variables
2142 into registers. */
2143 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2144 propagates values across these stores, and it probably shouldn't. */
2145 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2146 return false;
2148 /* If we're not interested in tracking debugging information for
2149 this decl, then we can certainly put it in a register. */
2150 if (DECL_IGNORED_P (decl))
2151 return true;
2153 if (optimize)
2154 return true;
2156 if (!DECL_REGISTER (decl))
2157 return false;
2159 switch (TREE_CODE (TREE_TYPE (decl)))
2161 case RECORD_TYPE:
2162 case UNION_TYPE:
2163 case QUAL_UNION_TYPE:
2164 /* When not optimizing, disregard register keyword for variables with
2165 types containing methods, otherwise the methods won't be callable
2166 from the debugger. */
2167 if (TYPE_METHODS (TREE_TYPE (decl)))
2168 return false;
2169 break;
2170 default:
2171 break;
2174 return true;
2177 /* Return true if TYPE should be passed by invisible reference. */
2179 bool
2180 pass_by_reference (CUMULATIVE_ARGS *ca, machine_mode mode,
2181 tree type, bool named_arg)
2183 if (type)
2185 /* If this type contains non-trivial constructors, then it is
2186 forbidden for the middle-end to create any new copies. */
2187 if (TREE_ADDRESSABLE (type))
2188 return true;
2190 /* GCC post 3.4 passes *all* variable sized types by reference. */
2191 if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2192 return true;
2194 /* If a record type should be passed the same as its first (and only)
2195 member, use the type and mode of that member. */
2196 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2198 type = TREE_TYPE (first_field (type));
2199 mode = TYPE_MODE (type);
2203 return targetm.calls.pass_by_reference (pack_cumulative_args (ca), mode,
2204 type, named_arg);
2207 /* Return true if TYPE, which is passed by reference, should be callee
2208 copied instead of caller copied. */
2210 bool
2211 reference_callee_copied (CUMULATIVE_ARGS *ca, machine_mode mode,
2212 tree type, bool named_arg)
2214 if (type && TREE_ADDRESSABLE (type))
2215 return false;
2216 return targetm.calls.callee_copies (pack_cumulative_args (ca), mode, type,
2217 named_arg);
2220 /* Structures to communicate between the subroutines of assign_parms.
2221 The first holds data persistent across all parameters, the second
2222 is cleared out for each parameter. */
2224 struct assign_parm_data_all
2226 /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2227 should become a job of the target or otherwise encapsulated. */
2228 CUMULATIVE_ARGS args_so_far_v;
2229 cumulative_args_t args_so_far;
2230 struct args_size stack_args_size;
2231 tree function_result_decl;
2232 tree orig_fnargs;
2233 rtx_insn *first_conversion_insn;
2234 rtx_insn *last_conversion_insn;
2235 HOST_WIDE_INT pretend_args_size;
2236 HOST_WIDE_INT extra_pretend_bytes;
2237 int reg_parm_stack_space;
2240 struct assign_parm_data_one
2242 tree nominal_type;
2243 tree passed_type;
2244 rtx entry_parm;
2245 rtx stack_parm;
2246 machine_mode nominal_mode;
2247 machine_mode passed_mode;
2248 machine_mode promoted_mode;
2249 struct locate_and_pad_arg_data locate;
2250 int partial;
2251 BOOL_BITFIELD named_arg : 1;
2252 BOOL_BITFIELD passed_pointer : 1;
2253 BOOL_BITFIELD on_stack : 1;
2254 BOOL_BITFIELD loaded_in_reg : 1;
2257 struct bounds_parm_data
2259 assign_parm_data_one parm_data;
2260 tree bounds_parm;
2261 tree ptr_parm;
2262 rtx ptr_entry;
2263 int bound_no;
2266 /* A subroutine of assign_parms. Initialize ALL. */
2268 static void
2269 assign_parms_initialize_all (struct assign_parm_data_all *all)
2271 tree fntype ATTRIBUTE_UNUSED;
2273 memset (all, 0, sizeof (*all));
2275 fntype = TREE_TYPE (current_function_decl);
2277 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
2278 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2279 #else
2280 INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2281 current_function_decl, -1);
2282 #endif
2283 all->args_so_far = pack_cumulative_args (&all->args_so_far_v);
2285 #ifdef INCOMING_REG_PARM_STACK_SPACE
2286 all->reg_parm_stack_space
2287 = INCOMING_REG_PARM_STACK_SPACE (current_function_decl);
2288 #endif
2291 /* If ARGS contains entries with complex types, split the entry into two
2292 entries of the component type. Return a new list of substitutions are
2293 needed, else the old list. */
2295 static void
2296 split_complex_args (vec<tree> *args)
2298 unsigned i;
2299 tree p;
2301 FOR_EACH_VEC_ELT (*args, i, p)
2303 tree type = TREE_TYPE (p);
2304 if (TREE_CODE (type) == COMPLEX_TYPE
2305 && targetm.calls.split_complex_arg (type))
2307 tree decl;
2308 tree subtype = TREE_TYPE (type);
2309 bool addressable = TREE_ADDRESSABLE (p);
2311 /* Rewrite the PARM_DECL's type with its component. */
2312 p = copy_node (p);
2313 TREE_TYPE (p) = subtype;
2314 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2315 DECL_MODE (p) = VOIDmode;
2316 DECL_SIZE (p) = NULL;
2317 DECL_SIZE_UNIT (p) = NULL;
2318 /* If this arg must go in memory, put it in a pseudo here.
2319 We can't allow it to go in memory as per normal parms,
2320 because the usual place might not have the imag part
2321 adjacent to the real part. */
2322 DECL_ARTIFICIAL (p) = addressable;
2323 DECL_IGNORED_P (p) = addressable;
2324 TREE_ADDRESSABLE (p) = 0;
2325 layout_decl (p, 0);
2326 (*args)[i] = p;
2328 /* Build a second synthetic decl. */
2329 decl = build_decl (EXPR_LOCATION (p),
2330 PARM_DECL, NULL_TREE, subtype);
2331 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2332 DECL_ARTIFICIAL (decl) = addressable;
2333 DECL_IGNORED_P (decl) = addressable;
2334 layout_decl (decl, 0);
2335 args->safe_insert (++i, decl);
2340 /* A subroutine of assign_parms. Adjust the parameter list to incorporate
2341 the hidden struct return argument, and (abi willing) complex args.
2342 Return the new parameter list. */
2344 static vec<tree>
2345 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2347 tree fndecl = current_function_decl;
2348 tree fntype = TREE_TYPE (fndecl);
2349 vec<tree> fnargs = vNULL;
2350 tree arg;
2352 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2353 fnargs.safe_push (arg);
2355 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2357 /* If struct value address is treated as the first argument, make it so. */
2358 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2359 && ! cfun->returns_pcc_struct
2360 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2362 tree type = build_pointer_type (TREE_TYPE (fntype));
2363 tree decl;
2365 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2366 PARM_DECL, get_identifier (".result_ptr"), type);
2367 DECL_ARG_TYPE (decl) = type;
2368 DECL_ARTIFICIAL (decl) = 1;
2369 DECL_NAMELESS (decl) = 1;
2370 TREE_CONSTANT (decl) = 1;
2372 DECL_CHAIN (decl) = all->orig_fnargs;
2373 all->orig_fnargs = decl;
2374 fnargs.safe_insert (0, decl);
2376 all->function_result_decl = decl;
2378 /* If function is instrumented then bounds of the
2379 passed structure address is the second argument. */
2380 if (chkp_function_instrumented_p (fndecl))
2382 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2383 PARM_DECL, get_identifier (".result_bnd"),
2384 pointer_bounds_type_node);
2385 DECL_ARG_TYPE (decl) = pointer_bounds_type_node;
2386 DECL_ARTIFICIAL (decl) = 1;
2387 DECL_NAMELESS (decl) = 1;
2388 TREE_CONSTANT (decl) = 1;
2390 DECL_CHAIN (decl) = DECL_CHAIN (all->orig_fnargs);
2391 DECL_CHAIN (all->orig_fnargs) = decl;
2392 fnargs.safe_insert (1, decl);
2396 /* If the target wants to split complex arguments into scalars, do so. */
2397 if (targetm.calls.split_complex_arg)
2398 split_complex_args (&fnargs);
2400 return fnargs;
2403 /* A subroutine of assign_parms. Examine PARM and pull out type and mode
2404 data for the parameter. Incorporate ABI specifics such as pass-by-
2405 reference and type promotion. */
2407 static void
2408 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2409 struct assign_parm_data_one *data)
2411 tree nominal_type, passed_type;
2412 machine_mode nominal_mode, passed_mode, promoted_mode;
2413 int unsignedp;
2415 memset (data, 0, sizeof (*data));
2417 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
2418 if (!cfun->stdarg)
2419 data->named_arg = 1; /* No variadic parms. */
2420 else if (DECL_CHAIN (parm))
2421 data->named_arg = 1; /* Not the last non-variadic parm. */
2422 else if (targetm.calls.strict_argument_naming (all->args_so_far))
2423 data->named_arg = 1; /* Only variadic ones are unnamed. */
2424 else
2425 data->named_arg = 0; /* Treat as variadic. */
2427 nominal_type = TREE_TYPE (parm);
2428 passed_type = DECL_ARG_TYPE (parm);
2430 /* Look out for errors propagating this far. Also, if the parameter's
2431 type is void then its value doesn't matter. */
2432 if (TREE_TYPE (parm) == error_mark_node
2433 /* This can happen after weird syntax errors
2434 or if an enum type is defined among the parms. */
2435 || TREE_CODE (parm) != PARM_DECL
2436 || passed_type == NULL
2437 || VOID_TYPE_P (nominal_type))
2439 nominal_type = passed_type = void_type_node;
2440 nominal_mode = passed_mode = promoted_mode = VOIDmode;
2441 goto egress;
2444 /* Find mode of arg as it is passed, and mode of arg as it should be
2445 during execution of this function. */
2446 passed_mode = TYPE_MODE (passed_type);
2447 nominal_mode = TYPE_MODE (nominal_type);
2449 /* If the parm is to be passed as a transparent union or record, use the
2450 type of the first field for the tests below. We have already verified
2451 that the modes are the same. */
2452 if ((TREE_CODE (passed_type) == UNION_TYPE
2453 || TREE_CODE (passed_type) == RECORD_TYPE)
2454 && TYPE_TRANSPARENT_AGGR (passed_type))
2455 passed_type = TREE_TYPE (first_field (passed_type));
2457 /* See if this arg was passed by invisible reference. */
2458 if (pass_by_reference (&all->args_so_far_v, passed_mode,
2459 passed_type, data->named_arg))
2461 passed_type = nominal_type = build_pointer_type (passed_type);
2462 data->passed_pointer = true;
2463 passed_mode = nominal_mode = TYPE_MODE (nominal_type);
2466 /* Find mode as it is passed by the ABI. */
2467 unsignedp = TYPE_UNSIGNED (passed_type);
2468 promoted_mode = promote_function_mode (passed_type, passed_mode, &unsignedp,
2469 TREE_TYPE (current_function_decl), 0);
2471 egress:
2472 data->nominal_type = nominal_type;
2473 data->passed_type = passed_type;
2474 data->nominal_mode = nominal_mode;
2475 data->passed_mode = passed_mode;
2476 data->promoted_mode = promoted_mode;
2479 /* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2481 static void
2482 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2483 struct assign_parm_data_one *data, bool no_rtl)
2485 int varargs_pretend_bytes = 0;
2487 targetm.calls.setup_incoming_varargs (all->args_so_far,
2488 data->promoted_mode,
2489 data->passed_type,
2490 &varargs_pretend_bytes, no_rtl);
2492 /* If the back-end has requested extra stack space, record how much is
2493 needed. Do not change pretend_args_size otherwise since it may be
2494 nonzero from an earlier partial argument. */
2495 if (varargs_pretend_bytes > 0)
2496 all->pretend_args_size = varargs_pretend_bytes;
2499 /* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2500 the incoming location of the current parameter. */
2502 static void
2503 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2504 struct assign_parm_data_one *data)
2506 HOST_WIDE_INT pretend_bytes = 0;
2507 rtx entry_parm;
2508 bool in_regs;
2510 if (data->promoted_mode == VOIDmode)
2512 data->entry_parm = data->stack_parm = const0_rtx;
2513 return;
2516 entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2517 data->promoted_mode,
2518 data->passed_type,
2519 data->named_arg);
2521 if (entry_parm == 0)
2522 data->promoted_mode = data->passed_mode;
2524 /* Determine parm's home in the stack, in case it arrives in the stack
2525 or we should pretend it did. Compute the stack position and rtx where
2526 the argument arrives and its size.
2528 There is one complexity here: If this was a parameter that would
2529 have been passed in registers, but wasn't only because it is
2530 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2531 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2532 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2533 as it was the previous time. */
2534 in_regs = (entry_parm != 0) || POINTER_BOUNDS_TYPE_P (data->passed_type);
2535 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2536 in_regs = true;
2537 #endif
2538 if (!in_regs && !data->named_arg)
2540 if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2542 rtx tem;
2543 tem = targetm.calls.function_incoming_arg (all->args_so_far,
2544 data->promoted_mode,
2545 data->passed_type, true);
2546 in_regs = tem != NULL;
2550 /* If this parameter was passed both in registers and in the stack, use
2551 the copy on the stack. */
2552 if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2553 data->passed_type))
2554 entry_parm = 0;
2556 if (entry_parm)
2558 int partial;
2560 partial = targetm.calls.arg_partial_bytes (all->args_so_far,
2561 data->promoted_mode,
2562 data->passed_type,
2563 data->named_arg);
2564 data->partial = partial;
2566 /* The caller might already have allocated stack space for the
2567 register parameters. */
2568 if (partial != 0 && all->reg_parm_stack_space == 0)
2570 /* Part of this argument is passed in registers and part
2571 is passed on the stack. Ask the prologue code to extend
2572 the stack part so that we can recreate the full value.
2574 PRETEND_BYTES is the size of the registers we need to store.
2575 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2576 stack space that the prologue should allocate.
2578 Internally, gcc assumes that the argument pointer is aligned
2579 to STACK_BOUNDARY bits. This is used both for alignment
2580 optimizations (see init_emit) and to locate arguments that are
2581 aligned to more than PARM_BOUNDARY bits. We must preserve this
2582 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2583 a stack boundary. */
2585 /* We assume at most one partial arg, and it must be the first
2586 argument on the stack. */
2587 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2589 pretend_bytes = partial;
2590 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2592 /* We want to align relative to the actual stack pointer, so
2593 don't include this in the stack size until later. */
2594 all->extra_pretend_bytes = all->pretend_args_size;
2598 locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2599 all->reg_parm_stack_space,
2600 entry_parm ? data->partial : 0, current_function_decl,
2601 &all->stack_args_size, &data->locate);
2603 /* Update parm_stack_boundary if this parameter is passed in the
2604 stack. */
2605 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2606 crtl->parm_stack_boundary = data->locate.boundary;
2608 /* Adjust offsets to include the pretend args. */
2609 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2610 data->locate.slot_offset.constant += pretend_bytes;
2611 data->locate.offset.constant += pretend_bytes;
2613 data->entry_parm = entry_parm;
2616 /* A subroutine of assign_parms. If there is actually space on the stack
2617 for this parm, count it in stack_args_size and return true. */
2619 static bool
2620 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2621 struct assign_parm_data_one *data)
2623 /* Bounds are never passed on the stack to keep compatibility
2624 with not instrumented code. */
2625 if (POINTER_BOUNDS_TYPE_P (data->passed_type))
2626 return false;
2627 /* Trivially true if we've no incoming register. */
2628 else if (data->entry_parm == NULL)
2630 /* Also true if we're partially in registers and partially not,
2631 since we've arranged to drop the entire argument on the stack. */
2632 else if (data->partial != 0)
2634 /* Also true if the target says that it's passed in both registers
2635 and on the stack. */
2636 else if (GET_CODE (data->entry_parm) == PARALLEL
2637 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2639 /* Also true if the target says that there's stack allocated for
2640 all register parameters. */
2641 else if (all->reg_parm_stack_space > 0)
2643 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2644 else
2645 return false;
2647 all->stack_args_size.constant += data->locate.size.constant;
2648 if (data->locate.size.var)
2649 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2651 return true;
2654 /* A subroutine of assign_parms. Given that this parameter is allocated
2655 stack space by the ABI, find it. */
2657 static void
2658 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2660 rtx offset_rtx, stack_parm;
2661 unsigned int align, boundary;
2663 /* If we're passing this arg using a reg, make its stack home the
2664 aligned stack slot. */
2665 if (data->entry_parm)
2666 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2667 else
2668 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2670 stack_parm = crtl->args.internal_arg_pointer;
2671 if (offset_rtx != const0_rtx)
2672 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2673 stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2675 if (!data->passed_pointer)
2677 set_mem_attributes (stack_parm, parm, 1);
2678 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2679 while promoted mode's size is needed. */
2680 if (data->promoted_mode != BLKmode
2681 && data->promoted_mode != DECL_MODE (parm))
2683 set_mem_size (stack_parm, GET_MODE_SIZE (data->promoted_mode));
2684 if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2686 int offset = subreg_lowpart_offset (DECL_MODE (parm),
2687 data->promoted_mode);
2688 if (offset)
2689 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2694 boundary = data->locate.boundary;
2695 align = BITS_PER_UNIT;
2697 /* If we're padding upward, we know that the alignment of the slot
2698 is TARGET_FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2699 intentionally forcing upward padding. Otherwise we have to come
2700 up with a guess at the alignment based on OFFSET_RTX. */
2701 if (data->locate.where_pad != downward || data->entry_parm)
2702 align = boundary;
2703 else if (CONST_INT_P (offset_rtx))
2705 align = INTVAL (offset_rtx) * BITS_PER_UNIT | boundary;
2706 align = align & -align;
2708 set_mem_align (stack_parm, align);
2710 if (data->entry_parm)
2711 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2713 data->stack_parm = stack_parm;
2716 /* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2717 always valid and contiguous. */
2719 static void
2720 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2722 rtx entry_parm = data->entry_parm;
2723 rtx stack_parm = data->stack_parm;
2725 /* If this parm was passed part in regs and part in memory, pretend it
2726 arrived entirely in memory by pushing the register-part onto the stack.
2727 In the special case of a DImode or DFmode that is split, we could put
2728 it together in a pseudoreg directly, but for now that's not worth
2729 bothering with. */
2730 if (data->partial != 0)
2732 /* Handle calls that pass values in multiple non-contiguous
2733 locations. The Irix 6 ABI has examples of this. */
2734 if (GET_CODE (entry_parm) == PARALLEL)
2735 emit_group_store (validize_mem (copy_rtx (stack_parm)), entry_parm,
2736 data->passed_type,
2737 int_size_in_bytes (data->passed_type));
2738 else
2740 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2741 move_block_from_reg (REGNO (entry_parm),
2742 validize_mem (copy_rtx (stack_parm)),
2743 data->partial / UNITS_PER_WORD);
2746 entry_parm = stack_parm;
2749 /* If we didn't decide this parm came in a register, by default it came
2750 on the stack. */
2751 else if (entry_parm == NULL)
2752 entry_parm = stack_parm;
2754 /* When an argument is passed in multiple locations, we can't make use
2755 of this information, but we can save some copying if the whole argument
2756 is passed in a single register. */
2757 else if (GET_CODE (entry_parm) == PARALLEL
2758 && data->nominal_mode != BLKmode
2759 && data->passed_mode != BLKmode)
2761 size_t i, len = XVECLEN (entry_parm, 0);
2763 for (i = 0; i < len; i++)
2764 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2765 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2766 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2767 == data->passed_mode)
2768 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2770 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2771 break;
2775 data->entry_parm = entry_parm;
2778 /* A subroutine of assign_parms. Reconstitute any values which were
2779 passed in multiple registers and would fit in a single register. */
2781 static void
2782 assign_parm_remove_parallels (struct assign_parm_data_one *data)
2784 rtx entry_parm = data->entry_parm;
2786 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2787 This can be done with register operations rather than on the
2788 stack, even if we will store the reconstituted parameter on the
2789 stack later. */
2790 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2792 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2793 emit_group_store (parmreg, entry_parm, data->passed_type,
2794 GET_MODE_SIZE (GET_MODE (entry_parm)));
2795 entry_parm = parmreg;
2798 data->entry_parm = entry_parm;
2801 /* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2802 always valid and properly aligned. */
2804 static void
2805 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2807 rtx stack_parm = data->stack_parm;
2809 /* If we can't trust the parm stack slot to be aligned enough for its
2810 ultimate type, don't use that slot after entry. We'll make another
2811 stack slot, if we need one. */
2812 if (stack_parm
2813 && ((STRICT_ALIGNMENT
2814 && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2815 || (data->nominal_type
2816 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2817 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2818 stack_parm = NULL;
2820 /* If parm was passed in memory, and we need to convert it on entry,
2821 don't store it back in that same slot. */
2822 else if (data->entry_parm == stack_parm
2823 && data->nominal_mode != BLKmode
2824 && data->nominal_mode != data->passed_mode)
2825 stack_parm = NULL;
2827 /* If stack protection is in effect for this function, don't leave any
2828 pointers in their passed stack slots. */
2829 else if (crtl->stack_protect_guard
2830 && (flag_stack_protect == 2
2831 || data->passed_pointer
2832 || POINTER_TYPE_P (data->nominal_type)))
2833 stack_parm = NULL;
2835 data->stack_parm = stack_parm;
2838 /* A subroutine of assign_parms. Return true if the current parameter
2839 should be stored as a BLKmode in the current frame. */
2841 static bool
2842 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2844 if (data->nominal_mode == BLKmode)
2845 return true;
2846 if (GET_MODE (data->entry_parm) == BLKmode)
2847 return true;
2849 #ifdef BLOCK_REG_PADDING
2850 /* Only assign_parm_setup_block knows how to deal with register arguments
2851 that are padded at the least significant end. */
2852 if (REG_P (data->entry_parm)
2853 && GET_MODE_SIZE (data->promoted_mode) < UNITS_PER_WORD
2854 && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2855 == (BYTES_BIG_ENDIAN ? upward : downward)))
2856 return true;
2857 #endif
2859 return false;
2862 /* A subroutine of assign_parms. Arrange for the parameter to be
2863 present and valid in DATA->STACK_RTL. */
2865 static void
2866 assign_parm_setup_block (struct assign_parm_data_all *all,
2867 tree parm, struct assign_parm_data_one *data)
2869 rtx entry_parm = data->entry_parm;
2870 rtx stack_parm = data->stack_parm;
2871 HOST_WIDE_INT size;
2872 HOST_WIDE_INT size_stored;
2874 if (GET_CODE (entry_parm) == PARALLEL)
2875 entry_parm = emit_group_move_into_temps (entry_parm);
2877 size = int_size_in_bytes (data->passed_type);
2878 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2879 if (stack_parm == 0)
2881 DECL_ALIGN (parm) = MAX (DECL_ALIGN (parm), BITS_PER_WORD);
2882 stack_parm = assign_stack_local (BLKmode, size_stored,
2883 DECL_ALIGN (parm));
2884 if (GET_MODE_SIZE (GET_MODE (entry_parm)) == size)
2885 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2886 set_mem_attributes (stack_parm, parm, 1);
2889 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2890 calls that pass values in multiple non-contiguous locations. */
2891 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2893 rtx mem;
2895 /* Note that we will be storing an integral number of words.
2896 So we have to be careful to ensure that we allocate an
2897 integral number of words. We do this above when we call
2898 assign_stack_local if space was not allocated in the argument
2899 list. If it was, this will not work if PARM_BOUNDARY is not
2900 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2901 if it becomes a problem. Exception is when BLKmode arrives
2902 with arguments not conforming to word_mode. */
2904 if (data->stack_parm == 0)
2906 else if (GET_CODE (entry_parm) == PARALLEL)
2908 else
2909 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2911 mem = validize_mem (copy_rtx (stack_parm));
2913 /* Handle values in multiple non-contiguous locations. */
2914 if (GET_CODE (entry_parm) == PARALLEL)
2916 push_to_sequence2 (all->first_conversion_insn,
2917 all->last_conversion_insn);
2918 emit_group_store (mem, entry_parm, data->passed_type, size);
2919 all->first_conversion_insn = get_insns ();
2920 all->last_conversion_insn = get_last_insn ();
2921 end_sequence ();
2924 else if (size == 0)
2927 /* If SIZE is that of a mode no bigger than a word, just use
2928 that mode's store operation. */
2929 else if (size <= UNITS_PER_WORD)
2931 machine_mode mode
2932 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2934 if (mode != BLKmode
2935 #ifdef BLOCK_REG_PADDING
2936 && (size == UNITS_PER_WORD
2937 || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2938 != (BYTES_BIG_ENDIAN ? upward : downward)))
2939 #endif
2942 rtx reg;
2944 /* We are really truncating a word_mode value containing
2945 SIZE bytes into a value of mode MODE. If such an
2946 operation requires no actual instructions, we can refer
2947 to the value directly in mode MODE, otherwise we must
2948 start with the register in word_mode and explicitly
2949 convert it. */
2950 if (TRULY_NOOP_TRUNCATION (size * BITS_PER_UNIT, BITS_PER_WORD))
2951 reg = gen_rtx_REG (mode, REGNO (entry_parm));
2952 else
2954 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2955 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
2957 emit_move_insn (change_address (mem, mode, 0), reg);
2960 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
2961 machine must be aligned to the left before storing
2962 to memory. Note that the previous test doesn't
2963 handle all cases (e.g. SIZE == 3). */
2964 else if (size != UNITS_PER_WORD
2965 #ifdef BLOCK_REG_PADDING
2966 && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2967 == downward)
2968 #else
2969 && BYTES_BIG_ENDIAN
2970 #endif
2973 rtx tem, x;
2974 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2975 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2977 x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
2978 tem = change_address (mem, word_mode, 0);
2979 emit_move_insn (tem, x);
2981 else
2982 move_block_from_reg (REGNO (entry_parm), mem,
2983 size_stored / UNITS_PER_WORD);
2985 else
2986 move_block_from_reg (REGNO (entry_parm), mem,
2987 size_stored / UNITS_PER_WORD);
2989 else if (data->stack_parm == 0)
2991 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
2992 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
2993 BLOCK_OP_NORMAL);
2994 all->first_conversion_insn = get_insns ();
2995 all->last_conversion_insn = get_last_insn ();
2996 end_sequence ();
2999 data->stack_parm = stack_parm;
3000 SET_DECL_RTL (parm, stack_parm);
3003 /* A subroutine of assign_parms. Allocate a pseudo to hold the current
3004 parameter. Get it there. Perform all ABI specified conversions. */
3006 static void
3007 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
3008 struct assign_parm_data_one *data)
3010 rtx parmreg, validated_mem;
3011 rtx equiv_stack_parm;
3012 machine_mode promoted_nominal_mode;
3013 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
3014 bool did_conversion = false;
3015 bool need_conversion, moved;
3017 /* Store the parm in a pseudoregister during the function, but we may
3018 need to do it in a wider mode. Using 2 here makes the result
3019 consistent with promote_decl_mode and thus expand_expr_real_1. */
3020 promoted_nominal_mode
3021 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
3022 TREE_TYPE (current_function_decl), 2);
3024 parmreg = gen_reg_rtx (promoted_nominal_mode);
3026 if (!DECL_ARTIFICIAL (parm))
3027 mark_user_reg (parmreg);
3029 /* If this was an item that we received a pointer to,
3030 set DECL_RTL appropriately. */
3031 if (data->passed_pointer)
3033 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
3034 set_mem_attributes (x, parm, 1);
3035 SET_DECL_RTL (parm, x);
3037 else
3038 SET_DECL_RTL (parm, parmreg);
3040 assign_parm_remove_parallels (data);
3042 /* Copy the value into the register, thus bridging between
3043 assign_parm_find_data_types and expand_expr_real_1. */
3045 equiv_stack_parm = data->stack_parm;
3046 validated_mem = validize_mem (copy_rtx (data->entry_parm));
3048 need_conversion = (data->nominal_mode != data->passed_mode
3049 || promoted_nominal_mode != data->promoted_mode);
3050 moved = false;
3052 if (need_conversion
3053 && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
3054 && data->nominal_mode == data->passed_mode
3055 && data->nominal_mode == GET_MODE (data->entry_parm))
3057 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
3058 mode, by the caller. We now have to convert it to
3059 NOMINAL_MODE, if different. However, PARMREG may be in
3060 a different mode than NOMINAL_MODE if it is being stored
3061 promoted.
3063 If ENTRY_PARM is a hard register, it might be in a register
3064 not valid for operating in its mode (e.g., an odd-numbered
3065 register for a DFmode). In that case, moves are the only
3066 thing valid, so we can't do a convert from there. This
3067 occurs when the calling sequence allow such misaligned
3068 usages.
3070 In addition, the conversion may involve a call, which could
3071 clobber parameters which haven't been copied to pseudo
3072 registers yet.
3074 First, we try to emit an insn which performs the necessary
3075 conversion. We verify that this insn does not clobber any
3076 hard registers. */
3078 enum insn_code icode;
3079 rtx op0, op1;
3081 icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
3082 unsignedp);
3084 op0 = parmreg;
3085 op1 = validated_mem;
3086 if (icode != CODE_FOR_nothing
3087 && insn_operand_matches (icode, 0, op0)
3088 && insn_operand_matches (icode, 1, op1))
3090 enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
3091 rtx_insn *insn, *insns;
3092 rtx t = op1;
3093 HARD_REG_SET hardregs;
3095 start_sequence ();
3096 /* If op1 is a hard register that is likely spilled, first
3097 force it into a pseudo, otherwise combiner might extend
3098 its lifetime too much. */
3099 if (GET_CODE (t) == SUBREG)
3100 t = SUBREG_REG (t);
3101 if (REG_P (t)
3102 && HARD_REGISTER_P (t)
3103 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (t))
3104 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (t))))
3106 t = gen_reg_rtx (GET_MODE (op1));
3107 emit_move_insn (t, op1);
3109 else
3110 t = op1;
3111 rtx pat = gen_extend_insn (op0, t, promoted_nominal_mode,
3112 data->passed_mode, unsignedp);
3113 emit_insn (pat);
3114 insns = get_insns ();
3116 moved = true;
3117 CLEAR_HARD_REG_SET (hardregs);
3118 for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3120 if (INSN_P (insn))
3121 note_stores (PATTERN (insn), record_hard_reg_sets,
3122 &hardregs);
3123 if (!hard_reg_set_empty_p (hardregs))
3124 moved = false;
3127 end_sequence ();
3129 if (moved)
3131 emit_insn (insns);
3132 if (equiv_stack_parm != NULL_RTX)
3133 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3134 equiv_stack_parm);
3139 if (moved)
3140 /* Nothing to do. */
3142 else if (need_conversion)
3144 /* We did not have an insn to convert directly, or the sequence
3145 generated appeared unsafe. We must first copy the parm to a
3146 pseudo reg, and save the conversion until after all
3147 parameters have been moved. */
3149 int save_tree_used;
3150 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3152 emit_move_insn (tempreg, validated_mem);
3154 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3155 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3157 if (GET_CODE (tempreg) == SUBREG
3158 && GET_MODE (tempreg) == data->nominal_mode
3159 && REG_P (SUBREG_REG (tempreg))
3160 && data->nominal_mode == data->passed_mode
3161 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm)
3162 && GET_MODE_SIZE (GET_MODE (tempreg))
3163 < GET_MODE_SIZE (GET_MODE (data->entry_parm)))
3165 /* The argument is already sign/zero extended, so note it
3166 into the subreg. */
3167 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3168 SUBREG_PROMOTED_SET (tempreg, unsignedp);
3171 /* TREE_USED gets set erroneously during expand_assignment. */
3172 save_tree_used = TREE_USED (parm);
3173 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3174 TREE_USED (parm) = save_tree_used;
3175 all->first_conversion_insn = get_insns ();
3176 all->last_conversion_insn = get_last_insn ();
3177 end_sequence ();
3179 did_conversion = true;
3181 else
3182 emit_move_insn (parmreg, validated_mem);
3184 /* If we were passed a pointer but the actual value can safely live
3185 in a register, retrieve it and use it directly. */
3186 if (data->passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode)
3188 /* We can't use nominal_mode, because it will have been set to
3189 Pmode above. We must use the actual mode of the parm. */
3190 if (use_register_for_decl (parm))
3192 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3193 mark_user_reg (parmreg);
3195 else
3197 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3198 TYPE_MODE (TREE_TYPE (parm)),
3199 TYPE_ALIGN (TREE_TYPE (parm)));
3200 parmreg
3201 = assign_stack_local (TYPE_MODE (TREE_TYPE (parm)),
3202 GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parm))),
3203 align);
3204 set_mem_attributes (parmreg, parm, 1);
3207 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
3209 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
3210 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3212 push_to_sequence2 (all->first_conversion_insn,
3213 all->last_conversion_insn);
3214 emit_move_insn (tempreg, DECL_RTL (parm));
3215 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3216 emit_move_insn (parmreg, tempreg);
3217 all->first_conversion_insn = get_insns ();
3218 all->last_conversion_insn = get_last_insn ();
3219 end_sequence ();
3221 did_conversion = true;
3223 else
3224 emit_move_insn (parmreg, DECL_RTL (parm));
3226 SET_DECL_RTL (parm, parmreg);
3228 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3229 now the parm. */
3230 data->stack_parm = NULL;
3233 /* Mark the register as eliminable if we did no conversion and it was
3234 copied from memory at a fixed offset, and the arg pointer was not
3235 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3236 offset formed an invalid address, such memory-equivalences as we
3237 make here would screw up life analysis for it. */
3238 if (data->nominal_mode == data->passed_mode
3239 && !did_conversion
3240 && data->stack_parm != 0
3241 && MEM_P (data->stack_parm)
3242 && data->locate.offset.var == 0
3243 && reg_mentioned_p (virtual_incoming_args_rtx,
3244 XEXP (data->stack_parm, 0)))
3246 rtx_insn *linsn = get_last_insn ();
3247 rtx_insn *sinsn;
3248 rtx set;
3250 /* Mark complex types separately. */
3251 if (GET_CODE (parmreg) == CONCAT)
3253 machine_mode submode
3254 = GET_MODE_INNER (GET_MODE (parmreg));
3255 int regnor = REGNO (XEXP (parmreg, 0));
3256 int regnoi = REGNO (XEXP (parmreg, 1));
3257 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3258 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3259 GET_MODE_SIZE (submode));
3261 /* Scan backwards for the set of the real and
3262 imaginary parts. */
3263 for (sinsn = linsn; sinsn != 0;
3264 sinsn = prev_nonnote_insn (sinsn))
3266 set = single_set (sinsn);
3267 if (set == 0)
3268 continue;
3270 if (SET_DEST (set) == regno_reg_rtx [regnoi])
3271 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3272 else if (SET_DEST (set) == regno_reg_rtx [regnor])
3273 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3276 else
3277 set_dst_reg_note (linsn, REG_EQUIV, equiv_stack_parm, parmreg);
3280 /* For pointer data type, suggest pointer register. */
3281 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3282 mark_reg_pointer (parmreg,
3283 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3286 /* A subroutine of assign_parms. Allocate stack space to hold the current
3287 parameter. Get it there. Perform all ABI specified conversions. */
3289 static void
3290 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3291 struct assign_parm_data_one *data)
3293 /* Value must be stored in the stack slot STACK_PARM during function
3294 execution. */
3295 bool to_conversion = false;
3297 assign_parm_remove_parallels (data);
3299 if (data->promoted_mode != data->nominal_mode)
3301 /* Conversion is required. */
3302 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3304 emit_move_insn (tempreg, validize_mem (copy_rtx (data->entry_parm)));
3306 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3307 to_conversion = true;
3309 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3310 TYPE_UNSIGNED (TREE_TYPE (parm)));
3312 if (data->stack_parm)
3314 int offset = subreg_lowpart_offset (data->nominal_mode,
3315 GET_MODE (data->stack_parm));
3316 /* ??? This may need a big-endian conversion on sparc64. */
3317 data->stack_parm
3318 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3319 if (offset && MEM_OFFSET_KNOWN_P (data->stack_parm))
3320 set_mem_offset (data->stack_parm,
3321 MEM_OFFSET (data->stack_parm) + offset);
3325 if (data->entry_parm != data->stack_parm)
3327 rtx src, dest;
3329 if (data->stack_parm == 0)
3331 int align = STACK_SLOT_ALIGNMENT (data->passed_type,
3332 GET_MODE (data->entry_parm),
3333 TYPE_ALIGN (data->passed_type));
3334 data->stack_parm
3335 = assign_stack_local (GET_MODE (data->entry_parm),
3336 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3337 align);
3338 set_mem_attributes (data->stack_parm, parm, 1);
3341 dest = validize_mem (copy_rtx (data->stack_parm));
3342 src = validize_mem (copy_rtx (data->entry_parm));
3344 if (MEM_P (src))
3346 /* Use a block move to handle potentially misaligned entry_parm. */
3347 if (!to_conversion)
3348 push_to_sequence2 (all->first_conversion_insn,
3349 all->last_conversion_insn);
3350 to_conversion = true;
3352 emit_block_move (dest, src,
3353 GEN_INT (int_size_in_bytes (data->passed_type)),
3354 BLOCK_OP_NORMAL);
3356 else
3357 emit_move_insn (dest, src);
3360 if (to_conversion)
3362 all->first_conversion_insn = get_insns ();
3363 all->last_conversion_insn = get_last_insn ();
3364 end_sequence ();
3367 SET_DECL_RTL (parm, data->stack_parm);
3370 /* A subroutine of assign_parms. If the ABI splits complex arguments, then
3371 undo the frobbing that we did in assign_parms_augmented_arg_list. */
3373 static void
3374 assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3375 vec<tree> fnargs)
3377 tree parm;
3378 tree orig_fnargs = all->orig_fnargs;
3379 unsigned i = 0;
3381 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3383 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3384 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3386 rtx tmp, real, imag;
3387 machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3389 real = DECL_RTL (fnargs[i]);
3390 imag = DECL_RTL (fnargs[i + 1]);
3391 if (inner != GET_MODE (real))
3393 real = gen_lowpart_SUBREG (inner, real);
3394 imag = gen_lowpart_SUBREG (inner, imag);
3397 if (TREE_ADDRESSABLE (parm))
3399 rtx rmem, imem;
3400 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3401 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3402 DECL_MODE (parm),
3403 TYPE_ALIGN (TREE_TYPE (parm)));
3405 /* split_complex_arg put the real and imag parts in
3406 pseudos. Move them to memory. */
3407 tmp = assign_stack_local (DECL_MODE (parm), size, align);
3408 set_mem_attributes (tmp, parm, 1);
3409 rmem = adjust_address_nv (tmp, inner, 0);
3410 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3411 push_to_sequence2 (all->first_conversion_insn,
3412 all->last_conversion_insn);
3413 emit_move_insn (rmem, real);
3414 emit_move_insn (imem, imag);
3415 all->first_conversion_insn = get_insns ();
3416 all->last_conversion_insn = get_last_insn ();
3417 end_sequence ();
3419 else
3420 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3421 SET_DECL_RTL (parm, tmp);
3423 real = DECL_INCOMING_RTL (fnargs[i]);
3424 imag = DECL_INCOMING_RTL (fnargs[i + 1]);
3425 if (inner != GET_MODE (real))
3427 real = gen_lowpart_SUBREG (inner, real);
3428 imag = gen_lowpart_SUBREG (inner, imag);
3430 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3431 set_decl_incoming_rtl (parm, tmp, false);
3432 i++;
3437 /* Load bounds of PARM from bounds table. */
3438 static void
3439 assign_parm_load_bounds (struct assign_parm_data_one *data,
3440 tree parm,
3441 rtx entry,
3442 unsigned bound_no)
3444 bitmap_iterator bi;
3445 unsigned i, offs = 0;
3446 int bnd_no = -1;
3447 rtx slot = NULL, ptr = NULL;
3449 if (parm)
3451 bitmap slots;
3452 bitmap_obstack_initialize (NULL);
3453 slots = BITMAP_ALLOC (NULL);
3454 chkp_find_bound_slots (TREE_TYPE (parm), slots);
3455 EXECUTE_IF_SET_IN_BITMAP (slots, 0, i, bi)
3457 if (bound_no)
3458 bound_no--;
3459 else
3461 bnd_no = i;
3462 break;
3465 BITMAP_FREE (slots);
3466 bitmap_obstack_release (NULL);
3469 /* We may have bounds not associated with any pointer. */
3470 if (bnd_no != -1)
3471 offs = bnd_no * POINTER_SIZE / BITS_PER_UNIT;
3473 /* Find associated pointer. */
3474 if (bnd_no == -1)
3476 /* If bounds are not associated with any bounds,
3477 then it is passed in a register or special slot. */
3478 gcc_assert (data->entry_parm);
3479 ptr = const0_rtx;
3481 else if (MEM_P (entry))
3482 slot = adjust_address (entry, Pmode, offs);
3483 else if (REG_P (entry))
3484 ptr = gen_rtx_REG (Pmode, REGNO (entry) + bnd_no);
3485 else if (GET_CODE (entry) == PARALLEL)
3486 ptr = chkp_get_value_with_offs (entry, GEN_INT (offs));
3487 else
3488 gcc_unreachable ();
3489 data->entry_parm = targetm.calls.load_bounds_for_arg (slot, ptr,
3490 data->entry_parm);
3493 /* Assign RTL expressions to the function's bounds parameters BNDARGS. */
3495 static void
3496 assign_bounds (vec<bounds_parm_data> &bndargs,
3497 struct assign_parm_data_all &all)
3499 unsigned i, pass, handled = 0;
3500 bounds_parm_data *pbdata;
3502 if (!bndargs.exists ())
3503 return;
3505 /* We make few passes to store input bounds. Firstly handle bounds
3506 passed in registers. After that we load bounds passed in special
3507 slots. Finally we load bounds from Bounds Table. */
3508 for (pass = 0; pass < 3; pass++)
3509 FOR_EACH_VEC_ELT (bndargs, i, pbdata)
3511 /* Pass 0 => regs only. */
3512 if (pass == 0
3513 && (!pbdata->parm_data.entry_parm
3514 || GET_CODE (pbdata->parm_data.entry_parm) != REG))
3515 continue;
3516 /* Pass 1 => slots only. */
3517 else if (pass == 1
3518 && (!pbdata->parm_data.entry_parm
3519 || GET_CODE (pbdata->parm_data.entry_parm) == REG))
3520 continue;
3521 /* Pass 2 => BT only. */
3522 else if (pass == 2
3523 && pbdata->parm_data.entry_parm)
3524 continue;
3526 if (!pbdata->parm_data.entry_parm
3527 || GET_CODE (pbdata->parm_data.entry_parm) != REG)
3528 assign_parm_load_bounds (&pbdata->parm_data, pbdata->ptr_parm,
3529 pbdata->ptr_entry, pbdata->bound_no);
3531 set_decl_incoming_rtl (pbdata->bounds_parm,
3532 pbdata->parm_data.entry_parm, false);
3534 if (assign_parm_setup_block_p (&pbdata->parm_data))
3535 assign_parm_setup_block (&all, pbdata->bounds_parm,
3536 &pbdata->parm_data);
3537 else if (pbdata->parm_data.passed_pointer
3538 || use_register_for_decl (pbdata->bounds_parm))
3539 assign_parm_setup_reg (&all, pbdata->bounds_parm,
3540 &pbdata->parm_data);
3541 else
3542 assign_parm_setup_stack (&all, pbdata->bounds_parm,
3543 &pbdata->parm_data);
3545 /* Count handled bounds to make sure we miss nothing. */
3546 handled++;
3549 gcc_assert (handled == bndargs.length ());
3551 bndargs.release ();
3554 /* Assign RTL expressions to the function's parameters. This may involve
3555 copying them into registers and using those registers as the DECL_RTL. */
3557 static void
3558 assign_parms (tree fndecl)
3560 struct assign_parm_data_all all;
3561 tree parm;
3562 vec<tree> fnargs;
3563 unsigned i, bound_no = 0;
3564 tree last_arg = NULL;
3565 rtx last_arg_entry = NULL;
3566 vec<bounds_parm_data> bndargs = vNULL;
3567 bounds_parm_data bdata;
3569 crtl->args.internal_arg_pointer
3570 = targetm.calls.internal_arg_pointer ();
3572 assign_parms_initialize_all (&all);
3573 fnargs = assign_parms_augmented_arg_list (&all);
3575 FOR_EACH_VEC_ELT (fnargs, i, parm)
3577 struct assign_parm_data_one data;
3579 /* Extract the type of PARM; adjust it according to ABI. */
3580 assign_parm_find_data_types (&all, parm, &data);
3582 /* Early out for errors and void parameters. */
3583 if (data.passed_mode == VOIDmode)
3585 SET_DECL_RTL (parm, const0_rtx);
3586 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3587 continue;
3590 /* Estimate stack alignment from parameter alignment. */
3591 if (SUPPORTS_STACK_ALIGNMENT)
3593 unsigned int align
3594 = targetm.calls.function_arg_boundary (data.promoted_mode,
3595 data.passed_type);
3596 align = MINIMUM_ALIGNMENT (data.passed_type, data.promoted_mode,
3597 align);
3598 if (TYPE_ALIGN (data.nominal_type) > align)
3599 align = MINIMUM_ALIGNMENT (data.nominal_type,
3600 TYPE_MODE (data.nominal_type),
3601 TYPE_ALIGN (data.nominal_type));
3602 if (crtl->stack_alignment_estimated < align)
3604 gcc_assert (!crtl->stack_realign_processed);
3605 crtl->stack_alignment_estimated = align;
3609 /* Find out where the parameter arrives in this function. */
3610 assign_parm_find_entry_rtl (&all, &data);
3612 /* Find out where stack space for this parameter might be. */
3613 if (assign_parm_is_stack_parm (&all, &data))
3615 assign_parm_find_stack_rtl (parm, &data);
3616 assign_parm_adjust_entry_rtl (&data);
3618 if (!POINTER_BOUNDS_TYPE_P (data.passed_type))
3620 /* Remember where last non bounds arg was passed in case
3621 we have to load associated bounds for it from Bounds
3622 Table. */
3623 last_arg = parm;
3624 last_arg_entry = data.entry_parm;
3625 bound_no = 0;
3627 /* Record permanently how this parm was passed. */
3628 if (data.passed_pointer)
3630 rtx incoming_rtl
3631 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.passed_type)),
3632 data.entry_parm);
3633 set_decl_incoming_rtl (parm, incoming_rtl, true);
3635 else
3636 set_decl_incoming_rtl (parm, data.entry_parm, false);
3638 /* Boudns should be loaded in the particular order to
3639 have registers allocated correctly. Collect info about
3640 input bounds and load them later. */
3641 if (POINTER_BOUNDS_TYPE_P (data.passed_type))
3643 /* Expect bounds in instrumented functions only. */
3644 gcc_assert (chkp_function_instrumented_p (fndecl));
3646 bdata.parm_data = data;
3647 bdata.bounds_parm = parm;
3648 bdata.ptr_parm = last_arg;
3649 bdata.ptr_entry = last_arg_entry;
3650 bdata.bound_no = bound_no;
3651 bndargs.safe_push (bdata);
3653 else
3655 assign_parm_adjust_stack_rtl (&data);
3657 if (assign_parm_setup_block_p (&data))
3658 assign_parm_setup_block (&all, parm, &data);
3659 else if (data.passed_pointer || use_register_for_decl (parm))
3660 assign_parm_setup_reg (&all, parm, &data);
3661 else
3662 assign_parm_setup_stack (&all, parm, &data);
3665 if (cfun->stdarg && !DECL_CHAIN (parm))
3667 int pretend_bytes = 0;
3669 assign_parms_setup_varargs (&all, &data, false);
3671 if (chkp_function_instrumented_p (fndecl))
3673 /* We expect this is the last parm. Otherwise it is wrong
3674 to assign bounds right now. */
3675 gcc_assert (i == (fnargs.length () - 1));
3676 assign_bounds (bndargs, all);
3677 targetm.calls.setup_incoming_vararg_bounds (all.args_so_far,
3678 data.promoted_mode,
3679 data.passed_type,
3680 &pretend_bytes,
3681 false);
3685 /* Update info on where next arg arrives in registers. */
3686 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3687 data.passed_type, data.named_arg);
3689 if (POINTER_BOUNDS_TYPE_P (data.passed_type))
3690 bound_no++;
3693 assign_bounds (bndargs, all);
3695 if (targetm.calls.split_complex_arg)
3696 assign_parms_unsplit_complex (&all, fnargs);
3698 fnargs.release ();
3700 /* Output all parameter conversion instructions (possibly including calls)
3701 now that all parameters have been copied out of hard registers. */
3702 emit_insn (all.first_conversion_insn);
3704 /* Estimate reload stack alignment from scalar return mode. */
3705 if (SUPPORTS_STACK_ALIGNMENT)
3707 if (DECL_RESULT (fndecl))
3709 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3710 machine_mode mode = TYPE_MODE (type);
3712 if (mode != BLKmode
3713 && mode != VOIDmode
3714 && !AGGREGATE_TYPE_P (type))
3716 unsigned int align = GET_MODE_ALIGNMENT (mode);
3717 if (crtl->stack_alignment_estimated < align)
3719 gcc_assert (!crtl->stack_realign_processed);
3720 crtl->stack_alignment_estimated = align;
3726 /* If we are receiving a struct value address as the first argument, set up
3727 the RTL for the function result. As this might require code to convert
3728 the transmitted address to Pmode, we do this here to ensure that possible
3729 preliminary conversions of the address have been emitted already. */
3730 if (all.function_result_decl)
3732 tree result = DECL_RESULT (current_function_decl);
3733 rtx addr = DECL_RTL (all.function_result_decl);
3734 rtx x;
3736 if (DECL_BY_REFERENCE (result))
3738 SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3739 x = addr;
3741 else
3743 SET_DECL_VALUE_EXPR (result,
3744 build1 (INDIRECT_REF, TREE_TYPE (result),
3745 all.function_result_decl));
3746 addr = convert_memory_address (Pmode, addr);
3747 x = gen_rtx_MEM (DECL_MODE (result), addr);
3748 set_mem_attributes (x, result, 1);
3751 DECL_HAS_VALUE_EXPR_P (result) = 1;
3753 SET_DECL_RTL (result, x);
3756 /* We have aligned all the args, so add space for the pretend args. */
3757 crtl->args.pretend_args_size = all.pretend_args_size;
3758 all.stack_args_size.constant += all.extra_pretend_bytes;
3759 crtl->args.size = all.stack_args_size.constant;
3761 /* Adjust function incoming argument size for alignment and
3762 minimum length. */
3764 crtl->args.size = MAX (crtl->args.size, all.reg_parm_stack_space);
3765 crtl->args.size = CEIL_ROUND (crtl->args.size,
3766 PARM_BOUNDARY / BITS_PER_UNIT);
3768 #ifdef ARGS_GROW_DOWNWARD
3769 crtl->args.arg_offset_rtx
3770 = (all.stack_args_size.var == 0 ? GEN_INT (-all.stack_args_size.constant)
3771 : expand_expr (size_diffop (all.stack_args_size.var,
3772 size_int (-all.stack_args_size.constant)),
3773 NULL_RTX, VOIDmode, EXPAND_NORMAL));
3774 #else
3775 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3776 #endif
3778 /* See how many bytes, if any, of its args a function should try to pop
3779 on return. */
3781 crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3782 TREE_TYPE (fndecl),
3783 crtl->args.size);
3785 /* For stdarg.h function, save info about
3786 regs and stack space used by the named args. */
3788 crtl->args.info = all.args_so_far_v;
3790 /* Set the rtx used for the function return value. Put this in its
3791 own variable so any optimizers that need this information don't have
3792 to include tree.h. Do this here so it gets done when an inlined
3793 function gets output. */
3795 crtl->return_rtx
3796 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3797 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3799 /* If scalar return value was computed in a pseudo-reg, or was a named
3800 return value that got dumped to the stack, copy that to the hard
3801 return register. */
3802 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3804 tree decl_result = DECL_RESULT (fndecl);
3805 rtx decl_rtl = DECL_RTL (decl_result);
3807 if (REG_P (decl_rtl)
3808 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3809 : DECL_REGISTER (decl_result))
3811 rtx real_decl_rtl;
3813 real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3814 fndecl, true);
3815 if (chkp_function_instrumented_p (fndecl))
3816 crtl->return_bnd
3817 = targetm.calls.chkp_function_value_bounds (TREE_TYPE (decl_result),
3818 fndecl, true);
3819 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3820 /* The delay slot scheduler assumes that crtl->return_rtx
3821 holds the hard register containing the return value, not a
3822 temporary pseudo. */
3823 crtl->return_rtx = real_decl_rtl;
3828 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3829 For all seen types, gimplify their sizes. */
3831 static tree
3832 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3834 tree t = *tp;
3836 *walk_subtrees = 0;
3837 if (TYPE_P (t))
3839 if (POINTER_TYPE_P (t))
3840 *walk_subtrees = 1;
3841 else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3842 && !TYPE_SIZES_GIMPLIFIED (t))
3844 gimplify_type_sizes (t, (gimple_seq *) data);
3845 *walk_subtrees = 1;
3849 return NULL;
3852 /* Gimplify the parameter list for current_function_decl. This involves
3853 evaluating SAVE_EXPRs of variable sized parameters and generating code
3854 to implement callee-copies reference parameters. Returns a sequence of
3855 statements to add to the beginning of the function. */
3857 gimple_seq
3858 gimplify_parameters (void)
3860 struct assign_parm_data_all all;
3861 tree parm;
3862 gimple_seq stmts = NULL;
3863 vec<tree> fnargs;
3864 unsigned i;
3866 assign_parms_initialize_all (&all);
3867 fnargs = assign_parms_augmented_arg_list (&all);
3869 FOR_EACH_VEC_ELT (fnargs, i, parm)
3871 struct assign_parm_data_one data;
3873 /* Extract the type of PARM; adjust it according to ABI. */
3874 assign_parm_find_data_types (&all, parm, &data);
3876 /* Early out for errors and void parameters. */
3877 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3878 continue;
3880 /* Update info on where next arg arrives in registers. */
3881 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3882 data.passed_type, data.named_arg);
3884 /* ??? Once upon a time variable_size stuffed parameter list
3885 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3886 turned out to be less than manageable in the gimple world.
3887 Now we have to hunt them down ourselves. */
3888 walk_tree_without_duplicates (&data.passed_type,
3889 gimplify_parm_type, &stmts);
3891 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3893 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3894 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3897 if (data.passed_pointer)
3899 tree type = TREE_TYPE (data.passed_type);
3900 if (reference_callee_copied (&all.args_so_far_v, TYPE_MODE (type),
3901 type, data.named_arg))
3903 tree local, t;
3905 /* For constant-sized objects, this is trivial; for
3906 variable-sized objects, we have to play games. */
3907 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3908 && !(flag_stack_check == GENERIC_STACK_CHECK
3909 && compare_tree_int (DECL_SIZE_UNIT (parm),
3910 STACK_CHECK_MAX_VAR_SIZE) > 0))
3912 local = create_tmp_var (type, get_name (parm));
3913 DECL_IGNORED_P (local) = 0;
3914 /* If PARM was addressable, move that flag over
3915 to the local copy, as its address will be taken,
3916 not the PARMs. Keep the parms address taken
3917 as we'll query that flag during gimplification. */
3918 if (TREE_ADDRESSABLE (parm))
3919 TREE_ADDRESSABLE (local) = 1;
3920 else if (TREE_CODE (type) == COMPLEX_TYPE
3921 || TREE_CODE (type) == VECTOR_TYPE)
3922 DECL_GIMPLE_REG_P (local) = 1;
3924 else
3926 tree ptr_type, addr;
3928 ptr_type = build_pointer_type (type);
3929 addr = create_tmp_reg (ptr_type, get_name (parm));
3930 DECL_IGNORED_P (addr) = 0;
3931 local = build_fold_indirect_ref (addr);
3933 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
3934 t = build_call_expr (t, 2, DECL_SIZE_UNIT (parm),
3935 size_int (DECL_ALIGN (parm)));
3937 /* The call has been built for a variable-sized object. */
3938 CALL_ALLOCA_FOR_VAR_P (t) = 1;
3939 t = fold_convert (ptr_type, t);
3940 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
3941 gimplify_and_add (t, &stmts);
3944 gimplify_assign (local, parm, &stmts);
3946 SET_DECL_VALUE_EXPR (parm, local);
3947 DECL_HAS_VALUE_EXPR_P (parm) = 1;
3952 fnargs.release ();
3954 return stmts;
3957 /* Compute the size and offset from the start of the stacked arguments for a
3958 parm passed in mode PASSED_MODE and with type TYPE.
3960 INITIAL_OFFSET_PTR points to the current offset into the stacked
3961 arguments.
3963 The starting offset and size for this parm are returned in
3964 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
3965 nonzero, the offset is that of stack slot, which is returned in
3966 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
3967 padding required from the initial offset ptr to the stack slot.
3969 IN_REGS is nonzero if the argument will be passed in registers. It will
3970 never be set if REG_PARM_STACK_SPACE is not defined.
3972 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
3973 for arguments which are passed in registers.
3975 FNDECL is the function in which the argument was defined.
3977 There are two types of rounding that are done. The first, controlled by
3978 TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
3979 argument list to be aligned to the specific boundary (in bits). This
3980 rounding affects the initial and starting offsets, but not the argument
3981 size.
3983 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
3984 optionally rounds the size of the parm to PARM_BOUNDARY. The
3985 initial offset is not affected by this rounding, while the size always
3986 is and the starting offset may be. */
3988 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
3989 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
3990 callers pass in the total size of args so far as
3991 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
3993 void
3994 locate_and_pad_parm (machine_mode passed_mode, tree type, int in_regs,
3995 int reg_parm_stack_space, int partial,
3996 tree fndecl ATTRIBUTE_UNUSED,
3997 struct args_size *initial_offset_ptr,
3998 struct locate_and_pad_arg_data *locate)
4000 tree sizetree;
4001 enum direction where_pad;
4002 unsigned int boundary, round_boundary;
4003 int part_size_in_regs;
4005 /* If we have found a stack parm before we reach the end of the
4006 area reserved for registers, skip that area. */
4007 if (! in_regs)
4009 if (reg_parm_stack_space > 0)
4011 if (initial_offset_ptr->var)
4013 initial_offset_ptr->var
4014 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
4015 ssize_int (reg_parm_stack_space));
4016 initial_offset_ptr->constant = 0;
4018 else if (initial_offset_ptr->constant < reg_parm_stack_space)
4019 initial_offset_ptr->constant = reg_parm_stack_space;
4023 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
4025 sizetree
4026 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
4027 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
4028 boundary = targetm.calls.function_arg_boundary (passed_mode, type);
4029 round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
4030 type);
4031 locate->where_pad = where_pad;
4033 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
4034 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4035 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4037 locate->boundary = boundary;
4039 if (SUPPORTS_STACK_ALIGNMENT)
4041 /* stack_alignment_estimated can't change after stack has been
4042 realigned. */
4043 if (crtl->stack_alignment_estimated < boundary)
4045 if (!crtl->stack_realign_processed)
4046 crtl->stack_alignment_estimated = boundary;
4047 else
4049 /* If stack is realigned and stack alignment value
4050 hasn't been finalized, it is OK not to increase
4051 stack_alignment_estimated. The bigger alignment
4052 requirement is recorded in stack_alignment_needed
4053 below. */
4054 gcc_assert (!crtl->stack_realign_finalized
4055 && crtl->stack_realign_needed);
4060 /* Remember if the outgoing parameter requires extra alignment on the
4061 calling function side. */
4062 if (crtl->stack_alignment_needed < boundary)
4063 crtl->stack_alignment_needed = boundary;
4064 if (crtl->preferred_stack_boundary < boundary)
4065 crtl->preferred_stack_boundary = boundary;
4067 #ifdef ARGS_GROW_DOWNWARD
4068 locate->slot_offset.constant = -initial_offset_ptr->constant;
4069 if (initial_offset_ptr->var)
4070 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
4071 initial_offset_ptr->var);
4074 tree s2 = sizetree;
4075 if (where_pad != none
4076 && (!tree_fits_uhwi_p (sizetree)
4077 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4078 s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
4079 SUB_PARM_SIZE (locate->slot_offset, s2);
4082 locate->slot_offset.constant += part_size_in_regs;
4084 if (!in_regs || reg_parm_stack_space > 0)
4085 pad_to_arg_alignment (&locate->slot_offset, boundary,
4086 &locate->alignment_pad);
4088 locate->size.constant = (-initial_offset_ptr->constant
4089 - locate->slot_offset.constant);
4090 if (initial_offset_ptr->var)
4091 locate->size.var = size_binop (MINUS_EXPR,
4092 size_binop (MINUS_EXPR,
4093 ssize_int (0),
4094 initial_offset_ptr->var),
4095 locate->slot_offset.var);
4097 /* Pad_below needs the pre-rounded size to know how much to pad
4098 below. */
4099 locate->offset = locate->slot_offset;
4100 if (where_pad == downward)
4101 pad_below (&locate->offset, passed_mode, sizetree);
4103 #else /* !ARGS_GROW_DOWNWARD */
4104 if (!in_regs || reg_parm_stack_space > 0)
4105 pad_to_arg_alignment (initial_offset_ptr, boundary,
4106 &locate->alignment_pad);
4107 locate->slot_offset = *initial_offset_ptr;
4109 #ifdef PUSH_ROUNDING
4110 if (passed_mode != BLKmode)
4111 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
4112 #endif
4114 /* Pad_below needs the pre-rounded size to know how much to pad below
4115 so this must be done before rounding up. */
4116 locate->offset = locate->slot_offset;
4117 if (where_pad == downward)
4118 pad_below (&locate->offset, passed_mode, sizetree);
4120 if (where_pad != none
4121 && (!tree_fits_uhwi_p (sizetree)
4122 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4123 sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
4125 ADD_PARM_SIZE (locate->size, sizetree);
4127 locate->size.constant -= part_size_in_regs;
4128 #endif /* ARGS_GROW_DOWNWARD */
4130 #ifdef FUNCTION_ARG_OFFSET
4131 locate->offset.constant += FUNCTION_ARG_OFFSET (passed_mode, type);
4132 #endif
4135 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
4136 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
4138 static void
4139 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
4140 struct args_size *alignment_pad)
4142 tree save_var = NULL_TREE;
4143 HOST_WIDE_INT save_constant = 0;
4144 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4145 HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
4147 #ifdef SPARC_STACK_BOUNDARY_HACK
4148 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
4149 the real alignment of %sp. However, when it does this, the
4150 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
4151 if (SPARC_STACK_BOUNDARY_HACK)
4152 sp_offset = 0;
4153 #endif
4155 if (boundary > PARM_BOUNDARY)
4157 save_var = offset_ptr->var;
4158 save_constant = offset_ptr->constant;
4161 alignment_pad->var = NULL_TREE;
4162 alignment_pad->constant = 0;
4164 if (boundary > BITS_PER_UNIT)
4166 if (offset_ptr->var)
4168 tree sp_offset_tree = ssize_int (sp_offset);
4169 tree offset = size_binop (PLUS_EXPR,
4170 ARGS_SIZE_TREE (*offset_ptr),
4171 sp_offset_tree);
4172 #ifdef ARGS_GROW_DOWNWARD
4173 tree rounded = round_down (offset, boundary / BITS_PER_UNIT);
4174 #else
4175 tree rounded = round_up (offset, boundary / BITS_PER_UNIT);
4176 #endif
4178 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
4179 /* ARGS_SIZE_TREE includes constant term. */
4180 offset_ptr->constant = 0;
4181 if (boundary > PARM_BOUNDARY)
4182 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
4183 save_var);
4185 else
4187 offset_ptr->constant = -sp_offset +
4188 #ifdef ARGS_GROW_DOWNWARD
4189 FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
4190 #else
4191 CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
4192 #endif
4193 if (boundary > PARM_BOUNDARY)
4194 alignment_pad->constant = offset_ptr->constant - save_constant;
4199 static void
4200 pad_below (struct args_size *offset_ptr, machine_mode passed_mode, tree sizetree)
4202 if (passed_mode != BLKmode)
4204 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
4205 offset_ptr->constant
4206 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
4207 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
4208 - GET_MODE_SIZE (passed_mode));
4210 else
4212 if (TREE_CODE (sizetree) != INTEGER_CST
4213 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
4215 /* Round the size up to multiple of PARM_BOUNDARY bits. */
4216 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4217 /* Add it in. */
4218 ADD_PARM_SIZE (*offset_ptr, s2);
4219 SUB_PARM_SIZE (*offset_ptr, sizetree);
4225 /* True if register REGNO was alive at a place where `setjmp' was
4226 called and was set more than once or is an argument. Such regs may
4227 be clobbered by `longjmp'. */
4229 static bool
4230 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
4232 /* There appear to be cases where some local vars never reach the
4233 backend but have bogus regnos. */
4234 if (regno >= max_reg_num ())
4235 return false;
4237 return ((REG_N_SETS (regno) > 1
4238 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
4239 regno))
4240 && REGNO_REG_SET_P (setjmp_crosses, regno));
4243 /* Walk the tree of blocks describing the binding levels within a
4244 function and warn about variables the might be killed by setjmp or
4245 vfork. This is done after calling flow_analysis before register
4246 allocation since that will clobber the pseudo-regs to hard
4247 regs. */
4249 static void
4250 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
4252 tree decl, sub;
4254 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
4256 if (TREE_CODE (decl) == VAR_DECL
4257 && DECL_RTL_SET_P (decl)
4258 && REG_P (DECL_RTL (decl))
4259 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4260 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
4261 " %<longjmp%> or %<vfork%>", decl);
4264 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
4265 setjmp_vars_warning (setjmp_crosses, sub);
4268 /* Do the appropriate part of setjmp_vars_warning
4269 but for arguments instead of local variables. */
4271 static void
4272 setjmp_args_warning (bitmap setjmp_crosses)
4274 tree decl;
4275 for (decl = DECL_ARGUMENTS (current_function_decl);
4276 decl; decl = DECL_CHAIN (decl))
4277 if (DECL_RTL (decl) != 0
4278 && REG_P (DECL_RTL (decl))
4279 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4280 warning (OPT_Wclobbered,
4281 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
4282 decl);
4285 /* Generate warning messages for variables live across setjmp. */
4287 void
4288 generate_setjmp_warnings (void)
4290 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4292 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS
4293 || bitmap_empty_p (setjmp_crosses))
4294 return;
4296 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4297 setjmp_args_warning (setjmp_crosses);
4301 /* Reverse the order of elements in the fragment chain T of blocks,
4302 and return the new head of the chain (old last element).
4303 In addition to that clear BLOCK_SAME_RANGE flags when needed
4304 and adjust BLOCK_SUPERCONTEXT from the super fragment to
4305 its super fragment origin. */
4307 static tree
4308 block_fragments_nreverse (tree t)
4310 tree prev = 0, block, next, prev_super = 0;
4311 tree super = BLOCK_SUPERCONTEXT (t);
4312 if (BLOCK_FRAGMENT_ORIGIN (super))
4313 super = BLOCK_FRAGMENT_ORIGIN (super);
4314 for (block = t; block; block = next)
4316 next = BLOCK_FRAGMENT_CHAIN (block);
4317 BLOCK_FRAGMENT_CHAIN (block) = prev;
4318 if ((prev && !BLOCK_SAME_RANGE (prev))
4319 || (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (block))
4320 != prev_super))
4321 BLOCK_SAME_RANGE (block) = 0;
4322 prev_super = BLOCK_SUPERCONTEXT (block);
4323 BLOCK_SUPERCONTEXT (block) = super;
4324 prev = block;
4326 t = BLOCK_FRAGMENT_ORIGIN (t);
4327 if (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (t))
4328 != prev_super)
4329 BLOCK_SAME_RANGE (t) = 0;
4330 BLOCK_SUPERCONTEXT (t) = super;
4331 return prev;
4334 /* Reverse the order of elements in the chain T of blocks,
4335 and return the new head of the chain (old last element).
4336 Also do the same on subblocks and reverse the order of elements
4337 in BLOCK_FRAGMENT_CHAIN as well. */
4339 static tree
4340 blocks_nreverse_all (tree t)
4342 tree prev = 0, block, next;
4343 for (block = t; block; block = next)
4345 next = BLOCK_CHAIN (block);
4346 BLOCK_CHAIN (block) = prev;
4347 if (BLOCK_FRAGMENT_CHAIN (block)
4348 && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4350 BLOCK_FRAGMENT_CHAIN (block)
4351 = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4352 if (!BLOCK_SAME_RANGE (BLOCK_FRAGMENT_CHAIN (block)))
4353 BLOCK_SAME_RANGE (block) = 0;
4355 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4356 prev = block;
4358 return prev;
4362 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4363 and create duplicate blocks. */
4364 /* ??? Need an option to either create block fragments or to create
4365 abstract origin duplicates of a source block. It really depends
4366 on what optimization has been performed. */
4368 void
4369 reorder_blocks (void)
4371 tree block = DECL_INITIAL (current_function_decl);
4373 if (block == NULL_TREE)
4374 return;
4376 auto_vec<tree, 10> block_stack;
4378 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4379 clear_block_marks (block);
4381 /* Prune the old trees away, so that they don't get in the way. */
4382 BLOCK_SUBBLOCKS (block) = NULL_TREE;
4383 BLOCK_CHAIN (block) = NULL_TREE;
4385 /* Recreate the block tree from the note nesting. */
4386 reorder_blocks_1 (get_insns (), block, &block_stack);
4387 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4390 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
4392 void
4393 clear_block_marks (tree block)
4395 while (block)
4397 TREE_ASM_WRITTEN (block) = 0;
4398 clear_block_marks (BLOCK_SUBBLOCKS (block));
4399 block = BLOCK_CHAIN (block);
4403 static void
4404 reorder_blocks_1 (rtx_insn *insns, tree current_block,
4405 vec<tree> *p_block_stack)
4407 rtx_insn *insn;
4408 tree prev_beg = NULL_TREE, prev_end = NULL_TREE;
4410 for (insn = insns; insn; insn = NEXT_INSN (insn))
4412 if (NOTE_P (insn))
4414 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4416 tree block = NOTE_BLOCK (insn);
4417 tree origin;
4419 gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4420 origin = block;
4422 if (prev_end)
4423 BLOCK_SAME_RANGE (prev_end) = 0;
4424 prev_end = NULL_TREE;
4426 /* If we have seen this block before, that means it now
4427 spans multiple address regions. Create a new fragment. */
4428 if (TREE_ASM_WRITTEN (block))
4430 tree new_block = copy_node (block);
4432 BLOCK_SAME_RANGE (new_block) = 0;
4433 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4434 BLOCK_FRAGMENT_CHAIN (new_block)
4435 = BLOCK_FRAGMENT_CHAIN (origin);
4436 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4438 NOTE_BLOCK (insn) = new_block;
4439 block = new_block;
4442 if (prev_beg == current_block && prev_beg)
4443 BLOCK_SAME_RANGE (block) = 1;
4445 prev_beg = origin;
4447 BLOCK_SUBBLOCKS (block) = 0;
4448 TREE_ASM_WRITTEN (block) = 1;
4449 /* When there's only one block for the entire function,
4450 current_block == block and we mustn't do this, it
4451 will cause infinite recursion. */
4452 if (block != current_block)
4454 tree super;
4455 if (block != origin)
4456 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block
4457 || BLOCK_FRAGMENT_ORIGIN (BLOCK_SUPERCONTEXT
4458 (origin))
4459 == current_block);
4460 if (p_block_stack->is_empty ())
4461 super = current_block;
4462 else
4464 super = p_block_stack->last ();
4465 gcc_assert (super == current_block
4466 || BLOCK_FRAGMENT_ORIGIN (super)
4467 == current_block);
4469 BLOCK_SUPERCONTEXT (block) = super;
4470 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4471 BLOCK_SUBBLOCKS (current_block) = block;
4472 current_block = origin;
4474 p_block_stack->safe_push (block);
4476 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4478 NOTE_BLOCK (insn) = p_block_stack->pop ();
4479 current_block = BLOCK_SUPERCONTEXT (current_block);
4480 if (BLOCK_FRAGMENT_ORIGIN (current_block))
4481 current_block = BLOCK_FRAGMENT_ORIGIN (current_block);
4482 prev_beg = NULL_TREE;
4483 prev_end = BLOCK_SAME_RANGE (NOTE_BLOCK (insn))
4484 ? NOTE_BLOCK (insn) : NULL_TREE;
4487 else
4489 prev_beg = NULL_TREE;
4490 if (prev_end)
4491 BLOCK_SAME_RANGE (prev_end) = 0;
4492 prev_end = NULL_TREE;
4497 /* Reverse the order of elements in the chain T of blocks,
4498 and return the new head of the chain (old last element). */
4500 tree
4501 blocks_nreverse (tree t)
4503 tree prev = 0, block, next;
4504 for (block = t; block; block = next)
4506 next = BLOCK_CHAIN (block);
4507 BLOCK_CHAIN (block) = prev;
4508 prev = block;
4510 return prev;
4513 /* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4514 by modifying the last node in chain 1 to point to chain 2. */
4516 tree
4517 block_chainon (tree op1, tree op2)
4519 tree t1;
4521 if (!op1)
4522 return op2;
4523 if (!op2)
4524 return op1;
4526 for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4527 continue;
4528 BLOCK_CHAIN (t1) = op2;
4530 #ifdef ENABLE_TREE_CHECKING
4532 tree t2;
4533 for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4534 gcc_assert (t2 != t1);
4536 #endif
4538 return op1;
4541 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
4542 non-NULL, list them all into VECTOR, in a depth-first preorder
4543 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
4544 blocks. */
4546 static int
4547 all_blocks (tree block, tree *vector)
4549 int n_blocks = 0;
4551 while (block)
4553 TREE_ASM_WRITTEN (block) = 0;
4555 /* Record this block. */
4556 if (vector)
4557 vector[n_blocks] = block;
4559 ++n_blocks;
4561 /* Record the subblocks, and their subblocks... */
4562 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4563 vector ? vector + n_blocks : 0);
4564 block = BLOCK_CHAIN (block);
4567 return n_blocks;
4570 /* Return a vector containing all the blocks rooted at BLOCK. The
4571 number of elements in the vector is stored in N_BLOCKS_P. The
4572 vector is dynamically allocated; it is the caller's responsibility
4573 to call `free' on the pointer returned. */
4575 static tree *
4576 get_block_vector (tree block, int *n_blocks_p)
4578 tree *block_vector;
4580 *n_blocks_p = all_blocks (block, NULL);
4581 block_vector = XNEWVEC (tree, *n_blocks_p);
4582 all_blocks (block, block_vector);
4584 return block_vector;
4587 static GTY(()) int next_block_index = 2;
4589 /* Set BLOCK_NUMBER for all the blocks in FN. */
4591 void
4592 number_blocks (tree fn)
4594 int i;
4595 int n_blocks;
4596 tree *block_vector;
4598 /* For SDB and XCOFF debugging output, we start numbering the blocks
4599 from 1 within each function, rather than keeping a running
4600 count. */
4601 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
4602 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
4603 next_block_index = 1;
4604 #endif
4606 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4608 /* The top-level BLOCK isn't numbered at all. */
4609 for (i = 1; i < n_blocks; ++i)
4610 /* We number the blocks from two. */
4611 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4613 free (block_vector);
4615 return;
4618 /* If VAR is present in a subblock of BLOCK, return the subblock. */
4620 DEBUG_FUNCTION tree
4621 debug_find_var_in_block_tree (tree var, tree block)
4623 tree t;
4625 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4626 if (t == var)
4627 return block;
4629 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4631 tree ret = debug_find_var_in_block_tree (var, t);
4632 if (ret)
4633 return ret;
4636 return NULL_TREE;
4639 /* Keep track of whether we're in a dummy function context. If we are,
4640 we don't want to invoke the set_current_function hook, because we'll
4641 get into trouble if the hook calls target_reinit () recursively or
4642 when the initial initialization is not yet complete. */
4644 static bool in_dummy_function;
4646 /* Invoke the target hook when setting cfun. Update the optimization options
4647 if the function uses different options than the default. */
4649 static void
4650 invoke_set_current_function_hook (tree fndecl)
4652 if (!in_dummy_function)
4654 tree opts = ((fndecl)
4655 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4656 : optimization_default_node);
4658 if (!opts)
4659 opts = optimization_default_node;
4661 /* Change optimization options if needed. */
4662 if (optimization_current_node != opts)
4664 optimization_current_node = opts;
4665 cl_optimization_restore (&global_options, TREE_OPTIMIZATION (opts));
4668 targetm.set_current_function (fndecl);
4669 this_fn_optabs = this_target_optabs;
4671 if (opts != optimization_default_node)
4673 init_tree_optimization_optabs (opts);
4674 if (TREE_OPTIMIZATION_OPTABS (opts))
4675 this_fn_optabs = (struct target_optabs *)
4676 TREE_OPTIMIZATION_OPTABS (opts);
4681 /* cfun should never be set directly; use this function. */
4683 void
4684 set_cfun (struct function *new_cfun)
4686 if (cfun != new_cfun)
4688 cfun = new_cfun;
4689 invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4693 /* Initialized with NOGC, making this poisonous to the garbage collector. */
4695 static vec<function_p> cfun_stack;
4697 /* Push the current cfun onto the stack, and set cfun to new_cfun. Also set
4698 current_function_decl accordingly. */
4700 void
4701 push_cfun (struct function *new_cfun)
4703 gcc_assert ((!cfun && !current_function_decl)
4704 || (cfun && current_function_decl == cfun->decl));
4705 cfun_stack.safe_push (cfun);
4706 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4707 set_cfun (new_cfun);
4710 /* Pop cfun from the stack. Also set current_function_decl accordingly. */
4712 void
4713 pop_cfun (void)
4715 struct function *new_cfun = cfun_stack.pop ();
4716 /* When in_dummy_function, we do have a cfun but current_function_decl is
4717 NULL. We also allow pushing NULL cfun and subsequently changing
4718 current_function_decl to something else and have both restored by
4719 pop_cfun. */
4720 gcc_checking_assert (in_dummy_function
4721 || !cfun
4722 || current_function_decl == cfun->decl);
4723 set_cfun (new_cfun);
4724 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4727 /* Return value of funcdef and increase it. */
4729 get_next_funcdef_no (void)
4731 return funcdef_no++;
4734 /* Return value of funcdef. */
4736 get_last_funcdef_no (void)
4738 return funcdef_no;
4741 /* Allocate a function structure for FNDECL and set its contents
4742 to the defaults. Set cfun to the newly-allocated object.
4743 Some of the helper functions invoked during initialization assume
4744 that cfun has already been set. Therefore, assign the new object
4745 directly into cfun and invoke the back end hook explicitly at the
4746 very end, rather than initializing a temporary and calling set_cfun
4747 on it.
4749 ABSTRACT_P is true if this is a function that will never be seen by
4750 the middle-end. Such functions are front-end concepts (like C++
4751 function templates) that do not correspond directly to functions
4752 placed in object files. */
4754 void
4755 allocate_struct_function (tree fndecl, bool abstract_p)
4757 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4759 cfun = ggc_cleared_alloc<function> ();
4761 init_eh_for_function ();
4763 if (init_machine_status)
4764 cfun->machine = (*init_machine_status) ();
4766 #ifdef OVERRIDE_ABI_FORMAT
4767 OVERRIDE_ABI_FORMAT (fndecl);
4768 #endif
4770 if (fndecl != NULL_TREE)
4772 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4773 cfun->decl = fndecl;
4774 current_function_funcdef_no = get_next_funcdef_no ();
4777 invoke_set_current_function_hook (fndecl);
4779 if (fndecl != NULL_TREE)
4781 tree result = DECL_RESULT (fndecl);
4782 if (!abstract_p && aggregate_value_p (result, fndecl))
4784 #ifdef PCC_STATIC_STRUCT_RETURN
4785 cfun->returns_pcc_struct = 1;
4786 #endif
4787 cfun->returns_struct = 1;
4790 cfun->stdarg = stdarg_p (fntype);
4792 /* Assume all registers in stdarg functions need to be saved. */
4793 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4794 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4796 /* ??? This could be set on a per-function basis by the front-end
4797 but is this worth the hassle? */
4798 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4799 cfun->can_delete_dead_exceptions = flag_delete_dead_exceptions;
4801 if (!profile_flag && !flag_instrument_function_entry_exit)
4802 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl) = 1;
4806 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4807 instead of just setting it. */
4809 void
4810 push_struct_function (tree fndecl)
4812 /* When in_dummy_function we might be in the middle of a pop_cfun and
4813 current_function_decl and cfun may not match. */
4814 gcc_assert (in_dummy_function
4815 || (!cfun && !current_function_decl)
4816 || (cfun && current_function_decl == cfun->decl));
4817 cfun_stack.safe_push (cfun);
4818 current_function_decl = fndecl;
4819 allocate_struct_function (fndecl, false);
4822 /* Reset crtl and other non-struct-function variables to defaults as
4823 appropriate for emitting rtl at the start of a function. */
4825 static void
4826 prepare_function_start (void)
4828 gcc_assert (!crtl->emit.x_last_insn);
4829 init_temp_slots ();
4830 init_emit ();
4831 init_varasm_status ();
4832 init_expr ();
4833 default_rtl_profile ();
4835 if (flag_stack_usage_info)
4837 cfun->su = ggc_cleared_alloc<stack_usage> ();
4838 cfun->su->static_stack_size = -1;
4841 cse_not_expected = ! optimize;
4843 /* Caller save not needed yet. */
4844 caller_save_needed = 0;
4846 /* We haven't done register allocation yet. */
4847 reg_renumber = 0;
4849 /* Indicate that we have not instantiated virtual registers yet. */
4850 virtuals_instantiated = 0;
4852 /* Indicate that we want CONCATs now. */
4853 generating_concat_p = 1;
4855 /* Indicate we have no need of a frame pointer yet. */
4856 frame_pointer_needed = 0;
4859 /* Initialize the rtl expansion mechanism so that we can do simple things
4860 like generate sequences. This is used to provide a context during global
4861 initialization of some passes. You must call expand_dummy_function_end
4862 to exit this context. */
4864 void
4865 init_dummy_function_start (void)
4867 gcc_assert (!in_dummy_function);
4868 in_dummy_function = true;
4869 push_struct_function (NULL_TREE);
4870 prepare_function_start ();
4873 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4874 and initialize static variables for generating RTL for the statements
4875 of the function. */
4877 void
4878 init_function_start (tree subr)
4880 if (subr && DECL_STRUCT_FUNCTION (subr))
4881 set_cfun (DECL_STRUCT_FUNCTION (subr));
4882 else
4883 allocate_struct_function (subr, false);
4885 /* Initialize backend, if needed. */
4886 initialize_rtl ();
4888 prepare_function_start ();
4889 decide_function_section (subr);
4891 /* Warn if this value is an aggregate type,
4892 regardless of which calling convention we are using for it. */
4893 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4894 warning (OPT_Waggregate_return, "function returns an aggregate");
4897 /* Expand code to verify the stack_protect_guard. This is invoked at
4898 the end of a function to be protected. */
4900 #ifndef HAVE_stack_protect_test
4901 # define HAVE_stack_protect_test 0
4902 # define gen_stack_protect_test(x, y, z) (gcc_unreachable (), NULL_RTX)
4903 #endif
4905 void
4906 stack_protect_epilogue (void)
4908 tree guard_decl = targetm.stack_protect_guard ();
4909 rtx_code_label *label = gen_label_rtx ();
4910 rtx x, y, tmp;
4912 x = expand_normal (crtl->stack_protect_guard);
4913 y = expand_normal (guard_decl);
4915 /* Allow the target to compare Y with X without leaking either into
4916 a register. */
4917 switch ((int) (HAVE_stack_protect_test != 0))
4919 case 1:
4920 tmp = gen_stack_protect_test (x, y, label);
4921 if (tmp)
4923 emit_insn (tmp);
4924 break;
4926 /* FALLTHRU */
4928 default:
4929 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
4930 break;
4933 /* The noreturn predictor has been moved to the tree level. The rtl-level
4934 predictors estimate this branch about 20%, which isn't enough to get
4935 things moved out of line. Since this is the only extant case of adding
4936 a noreturn function at the rtl level, it doesn't seem worth doing ought
4937 except adding the prediction by hand. */
4938 tmp = get_last_insn ();
4939 if (JUMP_P (tmp))
4940 predict_insn_def (as_a <rtx_insn *> (tmp), PRED_NORETURN, TAKEN);
4942 expand_call (targetm.stack_protect_fail (), NULL_RTX, /*ignore=*/true);
4943 free_temp_slots ();
4944 emit_label (label);
4947 /* Start the RTL for a new function, and set variables used for
4948 emitting RTL.
4949 SUBR is the FUNCTION_DECL node.
4950 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
4951 the function's parameters, which must be run at any return statement. */
4953 void
4954 expand_function_start (tree subr)
4956 /* Make sure volatile mem refs aren't considered
4957 valid operands of arithmetic insns. */
4958 init_recog_no_volatile ();
4960 crtl->profile
4961 = (profile_flag
4962 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
4964 crtl->limit_stack
4965 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
4967 /* Make the label for return statements to jump to. Do not special
4968 case machines with special return instructions -- they will be
4969 handled later during jump, ifcvt, or epilogue creation. */
4970 return_label = gen_label_rtx ();
4972 /* Initialize rtx used to return the value. */
4973 /* Do this before assign_parms so that we copy the struct value address
4974 before any library calls that assign parms might generate. */
4976 /* Decide whether to return the value in memory or in a register. */
4977 if (aggregate_value_p (DECL_RESULT (subr), subr))
4979 /* Returning something that won't go in a register. */
4980 rtx value_address = 0;
4982 #ifdef PCC_STATIC_STRUCT_RETURN
4983 if (cfun->returns_pcc_struct)
4985 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
4986 value_address = assemble_static_space (size);
4988 else
4989 #endif
4991 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
4992 /* Expect to be passed the address of a place to store the value.
4993 If it is passed as an argument, assign_parms will take care of
4994 it. */
4995 if (sv)
4997 value_address = gen_reg_rtx (Pmode);
4998 emit_move_insn (value_address, sv);
5001 if (value_address)
5003 rtx x = value_address;
5004 if (!DECL_BY_REFERENCE (DECL_RESULT (subr)))
5006 x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), x);
5007 set_mem_attributes (x, DECL_RESULT (subr), 1);
5009 SET_DECL_RTL (DECL_RESULT (subr), x);
5012 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
5013 /* If return mode is void, this decl rtl should not be used. */
5014 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
5015 else
5017 /* Compute the return values into a pseudo reg, which we will copy
5018 into the true return register after the cleanups are done. */
5019 tree return_type = TREE_TYPE (DECL_RESULT (subr));
5020 if (TYPE_MODE (return_type) != BLKmode
5021 && targetm.calls.return_in_msb (return_type))
5022 /* expand_function_end will insert the appropriate padding in
5023 this case. Use the return value's natural (unpadded) mode
5024 within the function proper. */
5025 SET_DECL_RTL (DECL_RESULT (subr),
5026 gen_reg_rtx (TYPE_MODE (return_type)));
5027 else
5029 /* In order to figure out what mode to use for the pseudo, we
5030 figure out what the mode of the eventual return register will
5031 actually be, and use that. */
5032 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
5034 /* Structures that are returned in registers are not
5035 aggregate_value_p, so we may see a PARALLEL or a REG. */
5036 if (REG_P (hard_reg))
5037 SET_DECL_RTL (DECL_RESULT (subr),
5038 gen_reg_rtx (GET_MODE (hard_reg)));
5039 else
5041 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
5042 SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
5046 /* Set DECL_REGISTER flag so that expand_function_end will copy the
5047 result to the real return register(s). */
5048 DECL_REGISTER (DECL_RESULT (subr)) = 1;
5050 if (chkp_function_instrumented_p (current_function_decl))
5052 tree return_type = TREE_TYPE (DECL_RESULT (subr));
5053 rtx bounds = targetm.calls.chkp_function_value_bounds (return_type,
5054 subr, 1);
5055 SET_DECL_BOUNDS_RTL (DECL_RESULT (subr), bounds);
5059 /* Initialize rtx for parameters and local variables.
5060 In some cases this requires emitting insns. */
5061 assign_parms (subr);
5063 /* If function gets a static chain arg, store it. */
5064 if (cfun->static_chain_decl)
5066 tree parm = cfun->static_chain_decl;
5067 rtx local, chain, insn;
5069 local = gen_reg_rtx (Pmode);
5070 chain = targetm.calls.static_chain (current_function_decl, true);
5072 set_decl_incoming_rtl (parm, chain, false);
5073 SET_DECL_RTL (parm, local);
5074 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
5076 insn = emit_move_insn (local, chain);
5078 /* Mark the register as eliminable, similar to parameters. */
5079 if (MEM_P (chain)
5080 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
5081 set_dst_reg_note (insn, REG_EQUIV, chain, local);
5083 /* If we aren't optimizing, save the static chain onto the stack. */
5084 if (!optimize)
5086 tree saved_static_chain_decl
5087 = build_decl (DECL_SOURCE_LOCATION (parm), VAR_DECL,
5088 DECL_NAME (parm), TREE_TYPE (parm));
5089 rtx saved_static_chain_rtx
5090 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5091 SET_DECL_RTL (saved_static_chain_decl, saved_static_chain_rtx);
5092 emit_move_insn (saved_static_chain_rtx, chain);
5093 SET_DECL_VALUE_EXPR (parm, saved_static_chain_decl);
5094 DECL_HAS_VALUE_EXPR_P (parm) = 1;
5098 /* If the function receives a non-local goto, then store the
5099 bits we need to restore the frame pointer. */
5100 if (cfun->nonlocal_goto_save_area)
5102 tree t_save;
5103 rtx r_save;
5105 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
5106 gcc_assert (DECL_RTL_SET_P (var));
5108 t_save = build4 (ARRAY_REF,
5109 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
5110 cfun->nonlocal_goto_save_area,
5111 integer_zero_node, NULL_TREE, NULL_TREE);
5112 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
5113 gcc_assert (GET_MODE (r_save) == Pmode);
5115 emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
5116 update_nonlocal_goto_save_area ();
5119 /* The following was moved from init_function_start.
5120 The move is supposed to make sdb output more accurate. */
5121 /* Indicate the beginning of the function body,
5122 as opposed to parm setup. */
5123 emit_note (NOTE_INSN_FUNCTION_BEG);
5125 gcc_assert (NOTE_P (get_last_insn ()));
5127 parm_birth_insn = get_last_insn ();
5129 if (crtl->profile)
5131 #ifdef PROFILE_HOOK
5132 PROFILE_HOOK (current_function_funcdef_no);
5133 #endif
5136 /* If we are doing generic stack checking, the probe should go here. */
5137 if (flag_stack_check == GENERIC_STACK_CHECK)
5138 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
5141 /* Undo the effects of init_dummy_function_start. */
5142 void
5143 expand_dummy_function_end (void)
5145 gcc_assert (in_dummy_function);
5147 /* End any sequences that failed to be closed due to syntax errors. */
5148 while (in_sequence_p ())
5149 end_sequence ();
5151 /* Outside function body, can't compute type's actual size
5152 until next function's body starts. */
5154 free_after_parsing (cfun);
5155 free_after_compilation (cfun);
5156 pop_cfun ();
5157 in_dummy_function = false;
5160 /* Helper for diddle_return_value. */
5162 void
5163 diddle_return_value_1 (void (*doit) (rtx, void *), void *arg, rtx outgoing)
5165 if (! outgoing)
5166 return;
5168 if (REG_P (outgoing))
5169 (*doit) (outgoing, arg);
5170 else if (GET_CODE (outgoing) == PARALLEL)
5172 int i;
5174 for (i = 0; i < XVECLEN (outgoing, 0); i++)
5176 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
5178 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
5179 (*doit) (x, arg);
5184 /* Call DOIT for each hard register used as a return value from
5185 the current function. */
5187 void
5188 diddle_return_value (void (*doit) (rtx, void *), void *arg)
5190 diddle_return_value_1 (doit, arg, crtl->return_rtx);
5191 diddle_return_value_1 (doit, arg, crtl->return_bnd);
5194 static void
5195 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5197 emit_clobber (reg);
5200 void
5201 clobber_return_register (void)
5203 diddle_return_value (do_clobber_return_reg, NULL);
5205 /* In case we do use pseudo to return value, clobber it too. */
5206 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5208 tree decl_result = DECL_RESULT (current_function_decl);
5209 rtx decl_rtl = DECL_RTL (decl_result);
5210 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
5212 do_clobber_return_reg (decl_rtl, NULL);
5217 static void
5218 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5220 emit_use (reg);
5223 static void
5224 use_return_register (void)
5226 diddle_return_value (do_use_return_reg, NULL);
5229 /* Possibly warn about unused parameters. */
5230 void
5231 do_warn_unused_parameter (tree fn)
5233 tree decl;
5235 for (decl = DECL_ARGUMENTS (fn);
5236 decl; decl = DECL_CHAIN (decl))
5237 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
5238 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
5239 && !TREE_NO_WARNING (decl))
5240 warning (OPT_Wunused_parameter, "unused parameter %q+D", decl);
5243 /* Set the location of the insn chain starting at INSN to LOC. */
5245 static void
5246 set_insn_locations (rtx_insn *insn, int loc)
5248 while (insn != NULL)
5250 if (INSN_P (insn))
5251 INSN_LOCATION (insn) = loc;
5252 insn = NEXT_INSN (insn);
5256 /* Generate RTL for the end of the current function. */
5258 void
5259 expand_function_end (void)
5261 rtx clobber_after;
5263 /* If arg_pointer_save_area was referenced only from a nested
5264 function, we will not have initialized it yet. Do that now. */
5265 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
5266 get_arg_pointer_save_area ();
5268 /* If we are doing generic stack checking and this function makes calls,
5269 do a stack probe at the start of the function to ensure we have enough
5270 space for another stack frame. */
5271 if (flag_stack_check == GENERIC_STACK_CHECK)
5273 rtx_insn *insn, *seq;
5275 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5276 if (CALL_P (insn))
5278 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
5279 start_sequence ();
5280 if (STACK_CHECK_MOVING_SP)
5281 anti_adjust_stack_and_probe (max_frame_size, true);
5282 else
5283 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
5284 seq = get_insns ();
5285 end_sequence ();
5286 set_insn_locations (seq, prologue_location);
5287 emit_insn_before (seq, stack_check_probe_note);
5288 break;
5292 /* End any sequences that failed to be closed due to syntax errors. */
5293 while (in_sequence_p ())
5294 end_sequence ();
5296 clear_pending_stack_adjust ();
5297 do_pending_stack_adjust ();
5299 /* Output a linenumber for the end of the function.
5300 SDB depends on this. */
5301 set_curr_insn_location (input_location);
5303 /* Before the return label (if any), clobber the return
5304 registers so that they are not propagated live to the rest of
5305 the function. This can only happen with functions that drop
5306 through; if there had been a return statement, there would
5307 have either been a return rtx, or a jump to the return label.
5309 We delay actual code generation after the current_function_value_rtx
5310 is computed. */
5311 clobber_after = get_last_insn ();
5313 /* Output the label for the actual return from the function. */
5314 emit_label (return_label);
5316 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
5318 /* Let except.c know where it should emit the call to unregister
5319 the function context for sjlj exceptions. */
5320 if (flag_exceptions)
5321 sjlj_emit_function_exit_after (get_last_insn ());
5323 else
5325 /* We want to ensure that instructions that may trap are not
5326 moved into the epilogue by scheduling, because we don't
5327 always emit unwind information for the epilogue. */
5328 if (cfun->can_throw_non_call_exceptions)
5329 emit_insn (gen_blockage ());
5332 /* If this is an implementation of throw, do what's necessary to
5333 communicate between __builtin_eh_return and the epilogue. */
5334 expand_eh_return ();
5336 /* If scalar return value was computed in a pseudo-reg, or was a named
5337 return value that got dumped to the stack, copy that to the hard
5338 return register. */
5339 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5341 tree decl_result = DECL_RESULT (current_function_decl);
5342 rtx decl_rtl = DECL_RTL (decl_result);
5344 if (REG_P (decl_rtl)
5345 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5346 : DECL_REGISTER (decl_result))
5348 rtx real_decl_rtl = crtl->return_rtx;
5350 /* This should be set in assign_parms. */
5351 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5353 /* If this is a BLKmode structure being returned in registers,
5354 then use the mode computed in expand_return. Note that if
5355 decl_rtl is memory, then its mode may have been changed,
5356 but that crtl->return_rtx has not. */
5357 if (GET_MODE (real_decl_rtl) == BLKmode)
5358 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
5360 /* If a non-BLKmode return value should be padded at the least
5361 significant end of the register, shift it left by the appropriate
5362 amount. BLKmode results are handled using the group load/store
5363 machinery. */
5364 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5365 && REG_P (real_decl_rtl)
5366 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5368 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5369 REGNO (real_decl_rtl)),
5370 decl_rtl);
5371 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5373 /* If a named return value dumped decl_return to memory, then
5374 we may need to re-do the PROMOTE_MODE signed/unsigned
5375 extension. */
5376 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5378 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5379 promote_function_mode (TREE_TYPE (decl_result),
5380 GET_MODE (decl_rtl), &unsignedp,
5381 TREE_TYPE (current_function_decl), 1);
5383 convert_move (real_decl_rtl, decl_rtl, unsignedp);
5385 else if (GET_CODE (real_decl_rtl) == PARALLEL)
5387 /* If expand_function_start has created a PARALLEL for decl_rtl,
5388 move the result to the real return registers. Otherwise, do
5389 a group load from decl_rtl for a named return. */
5390 if (GET_CODE (decl_rtl) == PARALLEL)
5391 emit_group_move (real_decl_rtl, decl_rtl);
5392 else
5393 emit_group_load (real_decl_rtl, decl_rtl,
5394 TREE_TYPE (decl_result),
5395 int_size_in_bytes (TREE_TYPE (decl_result)));
5397 /* In the case of complex integer modes smaller than a word, we'll
5398 need to generate some non-trivial bitfield insertions. Do that
5399 on a pseudo and not the hard register. */
5400 else if (GET_CODE (decl_rtl) == CONCAT
5401 && GET_MODE_CLASS (GET_MODE (decl_rtl)) == MODE_COMPLEX_INT
5402 && GET_MODE_BITSIZE (GET_MODE (decl_rtl)) <= BITS_PER_WORD)
5404 int old_generating_concat_p;
5405 rtx tmp;
5407 old_generating_concat_p = generating_concat_p;
5408 generating_concat_p = 0;
5409 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5410 generating_concat_p = old_generating_concat_p;
5412 emit_move_insn (tmp, decl_rtl);
5413 emit_move_insn (real_decl_rtl, tmp);
5415 else
5416 emit_move_insn (real_decl_rtl, decl_rtl);
5420 /* If returning a structure, arrange to return the address of the value
5421 in a place where debuggers expect to find it.
5423 If returning a structure PCC style,
5424 the caller also depends on this value.
5425 And cfun->returns_pcc_struct is not necessarily set. */
5426 if ((cfun->returns_struct || cfun->returns_pcc_struct)
5427 && !targetm.calls.omit_struct_return_reg)
5429 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5430 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5431 rtx outgoing;
5433 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5434 type = TREE_TYPE (type);
5435 else
5436 value_address = XEXP (value_address, 0);
5438 outgoing = targetm.calls.function_value (build_pointer_type (type),
5439 current_function_decl, true);
5441 /* Mark this as a function return value so integrate will delete the
5442 assignment and USE below when inlining this function. */
5443 REG_FUNCTION_VALUE_P (outgoing) = 1;
5445 /* The address may be ptr_mode and OUTGOING may be Pmode. */
5446 value_address = convert_memory_address (GET_MODE (outgoing),
5447 value_address);
5449 emit_move_insn (outgoing, value_address);
5451 /* Show return register used to hold result (in this case the address
5452 of the result. */
5453 crtl->return_rtx = outgoing;
5456 /* Emit the actual code to clobber return register. Don't emit
5457 it if clobber_after is a barrier, then the previous basic block
5458 certainly doesn't fall thru into the exit block. */
5459 if (!BARRIER_P (clobber_after))
5461 rtx seq;
5463 start_sequence ();
5464 clobber_return_register ();
5465 seq = get_insns ();
5466 end_sequence ();
5468 emit_insn_after (seq, clobber_after);
5471 /* Output the label for the naked return from the function. */
5472 if (naked_return_label)
5473 emit_label (naked_return_label);
5475 /* @@@ This is a kludge. We want to ensure that instructions that
5476 may trap are not moved into the epilogue by scheduling, because
5477 we don't always emit unwind information for the epilogue. */
5478 if (cfun->can_throw_non_call_exceptions
5479 && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5480 emit_insn (gen_blockage ());
5482 /* If stack protection is enabled for this function, check the guard. */
5483 if (crtl->stack_protect_guard)
5484 stack_protect_epilogue ();
5486 /* If we had calls to alloca, and this machine needs
5487 an accurate stack pointer to exit the function,
5488 insert some code to save and restore the stack pointer. */
5489 if (! EXIT_IGNORE_STACK
5490 && cfun->calls_alloca)
5492 rtx tem = 0, seq;
5494 start_sequence ();
5495 emit_stack_save (SAVE_FUNCTION, &tem);
5496 seq = get_insns ();
5497 end_sequence ();
5498 emit_insn_before (seq, parm_birth_insn);
5500 emit_stack_restore (SAVE_FUNCTION, tem);
5503 /* ??? This should no longer be necessary since stupid is no longer with
5504 us, but there are some parts of the compiler (eg reload_combine, and
5505 sh mach_dep_reorg) that still try and compute their own lifetime info
5506 instead of using the general framework. */
5507 use_return_register ();
5511 get_arg_pointer_save_area (void)
5513 rtx ret = arg_pointer_save_area;
5515 if (! ret)
5517 ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5518 arg_pointer_save_area = ret;
5521 if (! crtl->arg_pointer_save_area_init)
5523 rtx seq;
5525 /* Save the arg pointer at the beginning of the function. The
5526 generated stack slot may not be a valid memory address, so we
5527 have to check it and fix it if necessary. */
5528 start_sequence ();
5529 emit_move_insn (validize_mem (copy_rtx (ret)),
5530 crtl->args.internal_arg_pointer);
5531 seq = get_insns ();
5532 end_sequence ();
5534 push_topmost_sequence ();
5535 emit_insn_after (seq, entry_of_function ());
5536 pop_topmost_sequence ();
5538 crtl->arg_pointer_save_area_init = true;
5541 return ret;
5544 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5545 for the first time. */
5547 static void
5548 record_insns (rtx_insn *insns, rtx end, hash_table<insn_cache_hasher> **hashp)
5550 rtx_insn *tmp;
5551 hash_table<insn_cache_hasher> *hash = *hashp;
5553 if (hash == NULL)
5554 *hashp = hash = hash_table<insn_cache_hasher>::create_ggc (17);
5556 for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5558 rtx *slot = hash->find_slot (tmp, INSERT);
5559 gcc_assert (*slot == NULL);
5560 *slot = tmp;
5564 /* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5565 basic block, splitting or peepholes. If INSN is a prologue or epilogue
5566 insn, then record COPY as well. */
5568 void
5569 maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5571 hash_table<insn_cache_hasher> *hash;
5572 rtx *slot;
5574 hash = epilogue_insn_hash;
5575 if (!hash || !hash->find (insn))
5577 hash = prologue_insn_hash;
5578 if (!hash || !hash->find (insn))
5579 return;
5582 slot = hash->find_slot (copy, INSERT);
5583 gcc_assert (*slot == NULL);
5584 *slot = copy;
5587 /* Determine if any INSNs in HASH are, or are part of, INSN. Because
5588 we can be running after reorg, SEQUENCE rtl is possible. */
5590 static bool
5591 contains (const_rtx insn, hash_table<insn_cache_hasher> *hash)
5593 if (hash == NULL)
5594 return false;
5596 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5598 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
5599 int i;
5600 for (i = seq->len () - 1; i >= 0; i--)
5601 if (hash->find (seq->element (i)))
5602 return true;
5603 return false;
5606 return hash->find (const_cast<rtx> (insn)) != NULL;
5610 prologue_epilogue_contains (const_rtx insn)
5612 if (contains (insn, prologue_insn_hash))
5613 return 1;
5614 if (contains (insn, epilogue_insn_hash))
5615 return 1;
5616 return 0;
5619 #ifdef HAVE_return
5620 /* Insert use of return register before the end of BB. */
5622 static void
5623 emit_use_return_register_into_block (basic_block bb)
5625 rtx seq, insn;
5626 start_sequence ();
5627 use_return_register ();
5628 seq = get_insns ();
5629 end_sequence ();
5630 insn = BB_END (bb);
5631 #ifdef HAVE_cc0
5632 if (reg_mentioned_p (cc0_rtx, PATTERN (insn)))
5633 insn = prev_cc0_setter (insn);
5634 #endif
5635 emit_insn_before (seq, insn);
5639 /* Create a return pattern, either simple_return or return, depending on
5640 simple_p. */
5642 static rtx
5643 gen_return_pattern (bool simple_p)
5645 #ifdef HAVE_simple_return
5646 return simple_p ? gen_simple_return () : gen_return ();
5647 #else
5648 gcc_assert (!simple_p);
5649 return gen_return ();
5650 #endif
5653 /* Insert an appropriate return pattern at the end of block BB. This
5654 also means updating block_for_insn appropriately. SIMPLE_P is
5655 the same as in gen_return_pattern and passed to it. */
5657 void
5658 emit_return_into_block (bool simple_p, basic_block bb)
5660 rtx jump, pat;
5661 jump = emit_jump_insn_after (gen_return_pattern (simple_p), BB_END (bb));
5662 pat = PATTERN (jump);
5663 if (GET_CODE (pat) == PARALLEL)
5664 pat = XVECEXP (pat, 0, 0);
5665 gcc_assert (ANY_RETURN_P (pat));
5666 JUMP_LABEL (jump) = pat;
5668 #endif
5670 /* Set JUMP_LABEL for a return insn. */
5672 void
5673 set_return_jump_label (rtx returnjump)
5675 rtx pat = PATTERN (returnjump);
5676 if (GET_CODE (pat) == PARALLEL)
5677 pat = XVECEXP (pat, 0, 0);
5678 if (ANY_RETURN_P (pat))
5679 JUMP_LABEL (returnjump) = pat;
5680 else
5681 JUMP_LABEL (returnjump) = ret_rtx;
5684 #if defined (HAVE_return) || defined (HAVE_simple_return)
5685 /* Return true if there are any active insns between HEAD and TAIL. */
5686 bool
5687 active_insn_between (rtx_insn *head, rtx_insn *tail)
5689 while (tail)
5691 if (active_insn_p (tail))
5692 return true;
5693 if (tail == head)
5694 return false;
5695 tail = PREV_INSN (tail);
5697 return false;
5700 /* LAST_BB is a block that exits, and empty of active instructions.
5701 Examine its predecessors for jumps that can be converted to
5702 (conditional) returns. */
5703 vec<edge>
5704 convert_jumps_to_returns (basic_block last_bb, bool simple_p,
5705 vec<edge> unconverted ATTRIBUTE_UNUSED)
5707 int i;
5708 basic_block bb;
5709 rtx label;
5710 edge_iterator ei;
5711 edge e;
5712 auto_vec<basic_block> src_bbs (EDGE_COUNT (last_bb->preds));
5714 FOR_EACH_EDGE (e, ei, last_bb->preds)
5715 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
5716 src_bbs.quick_push (e->src);
5718 label = BB_HEAD (last_bb);
5720 FOR_EACH_VEC_ELT (src_bbs, i, bb)
5722 rtx_insn *jump = BB_END (bb);
5724 if (!JUMP_P (jump) || JUMP_LABEL (jump) != label)
5725 continue;
5727 e = find_edge (bb, last_bb);
5729 /* If we have an unconditional jump, we can replace that
5730 with a simple return instruction. */
5731 if (simplejump_p (jump))
5733 /* The use of the return register might be present in the exit
5734 fallthru block. Either:
5735 - removing the use is safe, and we should remove the use in
5736 the exit fallthru block, or
5737 - removing the use is not safe, and we should add it here.
5738 For now, we conservatively choose the latter. Either of the
5739 2 helps in crossjumping. */
5740 emit_use_return_register_into_block (bb);
5742 emit_return_into_block (simple_p, bb);
5743 delete_insn (jump);
5746 /* If we have a conditional jump branching to the last
5747 block, we can try to replace that with a conditional
5748 return instruction. */
5749 else if (condjump_p (jump))
5751 rtx dest;
5753 if (simple_p)
5754 dest = simple_return_rtx;
5755 else
5756 dest = ret_rtx;
5757 if (!redirect_jump (jump, dest, 0))
5759 #ifdef HAVE_simple_return
5760 if (simple_p)
5762 if (dump_file)
5763 fprintf (dump_file,
5764 "Failed to redirect bb %d branch.\n", bb->index);
5765 unconverted.safe_push (e);
5767 #endif
5768 continue;
5771 /* See comment in simplejump_p case above. */
5772 emit_use_return_register_into_block (bb);
5774 /* If this block has only one successor, it both jumps
5775 and falls through to the fallthru block, so we can't
5776 delete the edge. */
5777 if (single_succ_p (bb))
5778 continue;
5780 else
5782 #ifdef HAVE_simple_return
5783 if (simple_p)
5785 if (dump_file)
5786 fprintf (dump_file,
5787 "Failed to redirect bb %d branch.\n", bb->index);
5788 unconverted.safe_push (e);
5790 #endif
5791 continue;
5794 /* Fix up the CFG for the successful change we just made. */
5795 redirect_edge_succ (e, EXIT_BLOCK_PTR_FOR_FN (cfun));
5796 e->flags &= ~EDGE_CROSSING;
5798 src_bbs.release ();
5799 return unconverted;
5802 /* Emit a return insn for the exit fallthru block. */
5803 basic_block
5804 emit_return_for_exit (edge exit_fallthru_edge, bool simple_p)
5806 basic_block last_bb = exit_fallthru_edge->src;
5808 if (JUMP_P (BB_END (last_bb)))
5810 last_bb = split_edge (exit_fallthru_edge);
5811 exit_fallthru_edge = single_succ_edge (last_bb);
5813 emit_barrier_after (BB_END (last_bb));
5814 emit_return_into_block (simple_p, last_bb);
5815 exit_fallthru_edge->flags &= ~EDGE_FALLTHRU;
5816 return last_bb;
5818 #endif
5821 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
5822 this into place with notes indicating where the prologue ends and where
5823 the epilogue begins. Update the basic block information when possible.
5825 Notes on epilogue placement:
5826 There are several kinds of edges to the exit block:
5827 * a single fallthru edge from LAST_BB
5828 * possibly, edges from blocks containing sibcalls
5829 * possibly, fake edges from infinite loops
5831 The epilogue is always emitted on the fallthru edge from the last basic
5832 block in the function, LAST_BB, into the exit block.
5834 If LAST_BB is empty except for a label, it is the target of every
5835 other basic block in the function that ends in a return. If a
5836 target has a return or simple_return pattern (possibly with
5837 conditional variants), these basic blocks can be changed so that a
5838 return insn is emitted into them, and their target is adjusted to
5839 the real exit block.
5841 Notes on shrink wrapping: We implement a fairly conservative
5842 version of shrink-wrapping rather than the textbook one. We only
5843 generate a single prologue and a single epilogue. This is
5844 sufficient to catch a number of interesting cases involving early
5845 exits.
5847 First, we identify the blocks that require the prologue to occur before
5848 them. These are the ones that modify a call-saved register, or reference
5849 any of the stack or frame pointer registers. To simplify things, we then
5850 mark everything reachable from these blocks as also requiring a prologue.
5851 This takes care of loops automatically, and avoids the need to examine
5852 whether MEMs reference the frame, since it is sufficient to check for
5853 occurrences of the stack or frame pointer.
5855 We then compute the set of blocks for which the need for a prologue
5856 is anticipatable (borrowing terminology from the shrink-wrapping
5857 description in Muchnick's book). These are the blocks which either
5858 require a prologue themselves, or those that have only successors
5859 where the prologue is anticipatable. The prologue needs to be
5860 inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
5861 is not. For the moment, we ensure that only one such edge exists.
5863 The epilogue is placed as described above, but we make a
5864 distinction between inserting return and simple_return patterns
5865 when modifying other blocks that end in a return. Blocks that end
5866 in a sibcall omit the sibcall_epilogue if the block is not in
5867 ANTIC. */
5869 void
5870 thread_prologue_and_epilogue_insns (void)
5872 bool inserted;
5873 #ifdef HAVE_simple_return
5874 vec<edge> unconverted_simple_returns = vNULL;
5875 bitmap_head bb_flags;
5876 #endif
5877 rtx_insn *returnjump;
5878 rtx_insn *epilogue_end ATTRIBUTE_UNUSED;
5879 rtx_insn *prologue_seq ATTRIBUTE_UNUSED, *split_prologue_seq ATTRIBUTE_UNUSED;
5880 edge e, entry_edge, orig_entry_edge, exit_fallthru_edge;
5881 edge_iterator ei;
5883 df_analyze ();
5885 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5887 inserted = false;
5888 epilogue_end = NULL;
5889 returnjump = NULL;
5891 /* Can't deal with multiple successors of the entry block at the
5892 moment. Function should always have at least one entry
5893 point. */
5894 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5895 entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5896 orig_entry_edge = entry_edge;
5898 split_prologue_seq = NULL;
5899 if (flag_split_stack
5900 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
5901 == NULL))
5903 #ifndef HAVE_split_stack_prologue
5904 gcc_unreachable ();
5905 #else
5906 gcc_assert (HAVE_split_stack_prologue);
5908 start_sequence ();
5909 emit_insn (gen_split_stack_prologue ());
5910 split_prologue_seq = get_insns ();
5911 end_sequence ();
5913 record_insns (split_prologue_seq, NULL, &prologue_insn_hash);
5914 set_insn_locations (split_prologue_seq, prologue_location);
5915 #endif
5918 prologue_seq = NULL;
5919 #ifdef HAVE_prologue
5920 if (HAVE_prologue)
5922 start_sequence ();
5923 rtx_insn *seq = safe_as_a <rtx_insn *> (gen_prologue ());
5924 emit_insn (seq);
5926 /* Insert an explicit USE for the frame pointer
5927 if the profiling is on and the frame pointer is required. */
5928 if (crtl->profile && frame_pointer_needed)
5929 emit_use (hard_frame_pointer_rtx);
5931 /* Retain a map of the prologue insns. */
5932 record_insns (seq, NULL, &prologue_insn_hash);
5933 emit_note (NOTE_INSN_PROLOGUE_END);
5935 /* Ensure that instructions are not moved into the prologue when
5936 profiling is on. The call to the profiling routine can be
5937 emitted within the live range of a call-clobbered register. */
5938 if (!targetm.profile_before_prologue () && crtl->profile)
5939 emit_insn (gen_blockage ());
5941 prologue_seq = get_insns ();
5942 end_sequence ();
5943 set_insn_locations (prologue_seq, prologue_location);
5945 #endif
5947 #ifdef HAVE_simple_return
5948 bitmap_initialize (&bb_flags, &bitmap_default_obstack);
5950 /* Try to perform a kind of shrink-wrapping, making sure the
5951 prologue/epilogue is emitted only around those parts of the
5952 function that require it. */
5954 try_shrink_wrapping (&entry_edge, orig_entry_edge, &bb_flags, prologue_seq);
5955 #endif
5957 if (split_prologue_seq != NULL_RTX)
5959 insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
5960 inserted = true;
5962 if (prologue_seq != NULL_RTX)
5964 insert_insn_on_edge (prologue_seq, entry_edge);
5965 inserted = true;
5968 /* If the exit block has no non-fake predecessors, we don't need
5969 an epilogue. */
5970 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5971 if ((e->flags & EDGE_FAKE) == 0)
5972 break;
5973 if (e == NULL)
5974 goto epilogue_done;
5976 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5978 exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
5980 #ifdef HAVE_simple_return
5981 if (entry_edge != orig_entry_edge)
5982 exit_fallthru_edge
5983 = get_unconverted_simple_return (exit_fallthru_edge, bb_flags,
5984 &unconverted_simple_returns,
5985 &returnjump);
5986 #endif
5987 #ifdef HAVE_return
5988 if (HAVE_return)
5990 if (exit_fallthru_edge == NULL)
5991 goto epilogue_done;
5993 if (optimize)
5995 basic_block last_bb = exit_fallthru_edge->src;
5997 if (LABEL_P (BB_HEAD (last_bb))
5998 && !active_insn_between (BB_HEAD (last_bb), BB_END (last_bb)))
5999 convert_jumps_to_returns (last_bb, false, vNULL);
6001 if (EDGE_COUNT (last_bb->preds) != 0
6002 && single_succ_p (last_bb))
6004 last_bb = emit_return_for_exit (exit_fallthru_edge, false);
6005 epilogue_end = returnjump = BB_END (last_bb);
6006 #ifdef HAVE_simple_return
6007 /* Emitting the return may add a basic block.
6008 Fix bb_flags for the added block. */
6009 if (last_bb != exit_fallthru_edge->src)
6010 bitmap_set_bit (&bb_flags, last_bb->index);
6011 #endif
6012 goto epilogue_done;
6016 #endif
6018 /* A small fib -- epilogue is not yet completed, but we wish to re-use
6019 this marker for the splits of EH_RETURN patterns, and nothing else
6020 uses the flag in the meantime. */
6021 epilogue_completed = 1;
6023 #ifdef HAVE_eh_return
6024 /* Find non-fallthru edges that end with EH_RETURN instructions. On
6025 some targets, these get split to a special version of the epilogue
6026 code. In order to be able to properly annotate these with unwind
6027 info, try to split them now. If we get a valid split, drop an
6028 EPILOGUE_BEG note and mark the insns as epilogue insns. */
6029 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6031 rtx_insn *prev, *last, *trial;
6033 if (e->flags & EDGE_FALLTHRU)
6034 continue;
6035 last = BB_END (e->src);
6036 if (!eh_returnjump_p (last))
6037 continue;
6039 prev = PREV_INSN (last);
6040 trial = try_split (PATTERN (last), last, 1);
6041 if (trial == last)
6042 continue;
6044 record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
6045 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
6047 #endif
6049 /* If nothing falls through into the exit block, we don't need an
6050 epilogue. */
6052 if (exit_fallthru_edge == NULL)
6053 goto epilogue_done;
6055 #ifdef HAVE_epilogue
6056 if (HAVE_epilogue)
6058 start_sequence ();
6059 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
6060 rtx_insn *seq = as_a <rtx_insn *> (gen_epilogue ());
6061 if (seq)
6062 emit_jump_insn (seq);
6064 /* Retain a map of the epilogue insns. */
6065 record_insns (seq, NULL, &epilogue_insn_hash);
6066 set_insn_locations (seq, epilogue_location);
6068 seq = get_insns ();
6069 returnjump = get_last_insn ();
6070 end_sequence ();
6072 insert_insn_on_edge (seq, exit_fallthru_edge);
6073 inserted = true;
6075 if (JUMP_P (returnjump))
6076 set_return_jump_label (returnjump);
6078 else
6079 #endif
6081 basic_block cur_bb;
6083 if (! next_active_insn (BB_END (exit_fallthru_edge->src)))
6084 goto epilogue_done;
6085 /* We have a fall-through edge to the exit block, the source is not
6086 at the end of the function, and there will be an assembler epilogue
6087 at the end of the function.
6088 We can't use force_nonfallthru here, because that would try to
6089 use return. Inserting a jump 'by hand' is extremely messy, so
6090 we take advantage of cfg_layout_finalize using
6091 fixup_fallthru_exit_predecessor. */
6092 cfg_layout_initialize (0);
6093 FOR_EACH_BB_FN (cur_bb, cfun)
6094 if (cur_bb->index >= NUM_FIXED_BLOCKS
6095 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
6096 cur_bb->aux = cur_bb->next_bb;
6097 cfg_layout_finalize ();
6100 epilogue_done:
6102 default_rtl_profile ();
6104 if (inserted)
6106 sbitmap blocks;
6108 commit_edge_insertions ();
6110 /* Look for basic blocks within the prologue insns. */
6111 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
6112 bitmap_clear (blocks);
6113 bitmap_set_bit (blocks, entry_edge->dest->index);
6114 bitmap_set_bit (blocks, orig_entry_edge->dest->index);
6115 find_many_sub_basic_blocks (blocks);
6116 sbitmap_free (blocks);
6118 /* The epilogue insns we inserted may cause the exit edge to no longer
6119 be fallthru. */
6120 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6122 if (((e->flags & EDGE_FALLTHRU) != 0)
6123 && returnjump_p (BB_END (e->src)))
6124 e->flags &= ~EDGE_FALLTHRU;
6128 #ifdef HAVE_simple_return
6129 convert_to_simple_return (entry_edge, orig_entry_edge, bb_flags, returnjump,
6130 unconverted_simple_returns);
6131 #endif
6133 #ifdef HAVE_sibcall_epilogue
6134 /* Emit sibling epilogues before any sibling call sites. */
6135 for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds); (e =
6136 ei_safe_edge (ei));
6139 basic_block bb = e->src;
6140 rtx_insn *insn = BB_END (bb);
6141 rtx ep_seq;
6143 if (!CALL_P (insn)
6144 || ! SIBLING_CALL_P (insn)
6145 #ifdef HAVE_simple_return
6146 || (entry_edge != orig_entry_edge
6147 && !bitmap_bit_p (&bb_flags, bb->index))
6148 #endif
6151 ei_next (&ei);
6152 continue;
6155 ep_seq = gen_sibcall_epilogue ();
6156 if (ep_seq)
6158 start_sequence ();
6159 emit_note (NOTE_INSN_EPILOGUE_BEG);
6160 emit_insn (ep_seq);
6161 rtx_insn *seq = get_insns ();
6162 end_sequence ();
6164 /* Retain a map of the epilogue insns. Used in life analysis to
6165 avoid getting rid of sibcall epilogue insns. Do this before we
6166 actually emit the sequence. */
6167 record_insns (seq, NULL, &epilogue_insn_hash);
6168 set_insn_locations (seq, epilogue_location);
6170 emit_insn_before (seq, insn);
6172 ei_next (&ei);
6174 #endif
6176 #ifdef HAVE_epilogue
6177 if (epilogue_end)
6179 rtx_insn *insn, *next;
6181 /* Similarly, move any line notes that appear after the epilogue.
6182 There is no need, however, to be quite so anal about the existence
6183 of such a note. Also possibly move
6184 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
6185 info generation. */
6186 for (insn = epilogue_end; insn; insn = next)
6188 next = NEXT_INSN (insn);
6189 if (NOTE_P (insn)
6190 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
6191 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
6194 #endif
6196 #ifdef HAVE_simple_return
6197 bitmap_clear (&bb_flags);
6198 #endif
6200 /* Threading the prologue and epilogue changes the artificial refs
6201 in the entry and exit blocks. */
6202 epilogue_completed = 1;
6203 df_update_entry_exit_and_calls ();
6206 /* Reposition the prologue-end and epilogue-begin notes after
6207 instruction scheduling. */
6209 void
6210 reposition_prologue_and_epilogue_notes (void)
6212 #if defined (HAVE_prologue) || defined (HAVE_epilogue) \
6213 || defined (HAVE_sibcall_epilogue)
6214 /* Since the hash table is created on demand, the fact that it is
6215 non-null is a signal that it is non-empty. */
6216 if (prologue_insn_hash != NULL)
6218 size_t len = prologue_insn_hash->elements ();
6219 rtx_insn *insn, *last = NULL, *note = NULL;
6221 /* Scan from the beginning until we reach the last prologue insn. */
6222 /* ??? While we do have the CFG intact, there are two problems:
6223 (1) The prologue can contain loops (typically probing the stack),
6224 which means that the end of the prologue isn't in the first bb.
6225 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
6226 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6228 if (NOTE_P (insn))
6230 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
6231 note = insn;
6233 else if (contains (insn, prologue_insn_hash))
6235 last = insn;
6236 if (--len == 0)
6237 break;
6241 if (last)
6243 if (note == NULL)
6245 /* Scan forward looking for the PROLOGUE_END note. It should
6246 be right at the beginning of the block, possibly with other
6247 insn notes that got moved there. */
6248 for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
6250 if (NOTE_P (note)
6251 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
6252 break;
6256 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
6257 if (LABEL_P (last))
6258 last = NEXT_INSN (last);
6259 reorder_insns (note, note, last);
6263 if (epilogue_insn_hash != NULL)
6265 edge_iterator ei;
6266 edge e;
6268 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6270 rtx_insn *insn, *first = NULL, *note = NULL;
6271 basic_block bb = e->src;
6273 /* Scan from the beginning until we reach the first epilogue insn. */
6274 FOR_BB_INSNS (bb, insn)
6276 if (NOTE_P (insn))
6278 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
6280 note = insn;
6281 if (first != NULL)
6282 break;
6285 else if (first == NULL && contains (insn, epilogue_insn_hash))
6287 first = insn;
6288 if (note != NULL)
6289 break;
6293 if (note)
6295 /* If the function has a single basic block, and no real
6296 epilogue insns (e.g. sibcall with no cleanup), the
6297 epilogue note can get scheduled before the prologue
6298 note. If we have frame related prologue insns, having
6299 them scanned during the epilogue will result in a crash.
6300 In this case re-order the epilogue note to just before
6301 the last insn in the block. */
6302 if (first == NULL)
6303 first = BB_END (bb);
6305 if (PREV_INSN (first) != note)
6306 reorder_insns (note, note, PREV_INSN (first));
6310 #endif /* HAVE_prologue or HAVE_epilogue */
6313 /* Returns the name of function declared by FNDECL. */
6314 const char *
6315 fndecl_name (tree fndecl)
6317 if (fndecl == NULL)
6318 return "(nofn)";
6319 return lang_hooks.decl_printable_name (fndecl, 2);
6322 /* Returns the name of function FN. */
6323 const char *
6324 function_name (struct function *fn)
6326 tree fndecl = (fn == NULL) ? NULL : fn->decl;
6327 return fndecl_name (fndecl);
6330 /* Returns the name of the current function. */
6331 const char *
6332 current_function_name (void)
6334 return function_name (cfun);
6338 static unsigned int
6339 rest_of_handle_check_leaf_regs (void)
6341 #ifdef LEAF_REGISTERS
6342 crtl->uses_only_leaf_regs
6343 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6344 #endif
6345 return 0;
6348 /* Insert a TYPE into the used types hash table of CFUN. */
6350 static void
6351 used_types_insert_helper (tree type, struct function *func)
6353 if (type != NULL && func != NULL)
6355 if (func->used_types_hash == NULL)
6356 func->used_types_hash = hash_set<tree>::create_ggc (37);
6358 func->used_types_hash->add (type);
6362 /* Given a type, insert it into the used hash table in cfun. */
6363 void
6364 used_types_insert (tree t)
6366 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6367 if (TYPE_NAME (t))
6368 break;
6369 else
6370 t = TREE_TYPE (t);
6371 if (TREE_CODE (t) == ERROR_MARK)
6372 return;
6373 if (TYPE_NAME (t) == NULL_TREE
6374 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6375 t = TYPE_MAIN_VARIANT (t);
6376 if (debug_info_level > DINFO_LEVEL_NONE)
6378 if (cfun)
6379 used_types_insert_helper (t, cfun);
6380 else
6382 /* So this might be a type referenced by a global variable.
6383 Record that type so that we can later decide to emit its
6384 debug information. */
6385 vec_safe_push (types_used_by_cur_var_decl, t);
6390 /* Helper to Hash a struct types_used_by_vars_entry. */
6392 static hashval_t
6393 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6395 gcc_assert (entry && entry->var_decl && entry->type);
6397 return iterative_hash_object (entry->type,
6398 iterative_hash_object (entry->var_decl, 0));
6401 /* Hash function of the types_used_by_vars_entry hash table. */
6403 hashval_t
6404 used_type_hasher::hash (types_used_by_vars_entry *entry)
6406 return hash_types_used_by_vars_entry (entry);
6409 /*Equality function of the types_used_by_vars_entry hash table. */
6411 bool
6412 used_type_hasher::equal (types_used_by_vars_entry *e1,
6413 types_used_by_vars_entry *e2)
6415 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6418 /* Inserts an entry into the types_used_by_vars_hash hash table. */
6420 void
6421 types_used_by_var_decl_insert (tree type, tree var_decl)
6423 if (type != NULL && var_decl != NULL)
6425 types_used_by_vars_entry **slot;
6426 struct types_used_by_vars_entry e;
6427 e.var_decl = var_decl;
6428 e.type = type;
6429 if (types_used_by_vars_hash == NULL)
6430 types_used_by_vars_hash
6431 = hash_table<used_type_hasher>::create_ggc (37);
6433 slot = types_used_by_vars_hash->find_slot (&e, INSERT);
6434 if (*slot == NULL)
6436 struct types_used_by_vars_entry *entry;
6437 entry = ggc_alloc<types_used_by_vars_entry> ();
6438 entry->type = type;
6439 entry->var_decl = var_decl;
6440 *slot = entry;
6445 namespace {
6447 const pass_data pass_data_leaf_regs =
6449 RTL_PASS, /* type */
6450 "*leaf_regs", /* name */
6451 OPTGROUP_NONE, /* optinfo_flags */
6452 TV_NONE, /* tv_id */
6453 0, /* properties_required */
6454 0, /* properties_provided */
6455 0, /* properties_destroyed */
6456 0, /* todo_flags_start */
6457 0, /* todo_flags_finish */
6460 class pass_leaf_regs : public rtl_opt_pass
6462 public:
6463 pass_leaf_regs (gcc::context *ctxt)
6464 : rtl_opt_pass (pass_data_leaf_regs, ctxt)
6467 /* opt_pass methods: */
6468 virtual unsigned int execute (function *)
6470 return rest_of_handle_check_leaf_regs ();
6473 }; // class pass_leaf_regs
6475 } // anon namespace
6477 rtl_opt_pass *
6478 make_pass_leaf_regs (gcc::context *ctxt)
6480 return new pass_leaf_regs (ctxt);
6483 static unsigned int
6484 rest_of_handle_thread_prologue_and_epilogue (void)
6486 if (optimize)
6487 cleanup_cfg (CLEANUP_EXPENSIVE);
6489 /* On some machines, the prologue and epilogue code, or parts thereof,
6490 can be represented as RTL. Doing so lets us schedule insns between
6491 it and the rest of the code and also allows delayed branch
6492 scheduling to operate in the epilogue. */
6493 thread_prologue_and_epilogue_insns ();
6495 /* Shrink-wrapping can result in unreachable edges in the epilogue,
6496 see PR57320. */
6497 cleanup_cfg (0);
6499 /* The stack usage info is finalized during prologue expansion. */
6500 if (flag_stack_usage_info)
6501 output_stack_usage ();
6503 return 0;
6506 namespace {
6508 const pass_data pass_data_thread_prologue_and_epilogue =
6510 RTL_PASS, /* type */
6511 "pro_and_epilogue", /* name */
6512 OPTGROUP_NONE, /* optinfo_flags */
6513 TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
6514 0, /* properties_required */
6515 0, /* properties_provided */
6516 0, /* properties_destroyed */
6517 0, /* todo_flags_start */
6518 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
6521 class pass_thread_prologue_and_epilogue : public rtl_opt_pass
6523 public:
6524 pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6525 : rtl_opt_pass (pass_data_thread_prologue_and_epilogue, ctxt)
6528 /* opt_pass methods: */
6529 virtual unsigned int execute (function *)
6531 return rest_of_handle_thread_prologue_and_epilogue ();
6534 }; // class pass_thread_prologue_and_epilogue
6536 } // anon namespace
6538 rtl_opt_pass *
6539 make_pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6541 return new pass_thread_prologue_and_epilogue (ctxt);
6545 /* This mini-pass fixes fall-out from SSA in asm statements that have
6546 in-out constraints. Say you start with
6548 orig = inout;
6549 asm ("": "+mr" (inout));
6550 use (orig);
6552 which is transformed very early to use explicit output and match operands:
6554 orig = inout;
6555 asm ("": "=mr" (inout) : "0" (inout));
6556 use (orig);
6558 Or, after SSA and copyprop,
6560 asm ("": "=mr" (inout_2) : "0" (inout_1));
6561 use (inout_1);
6563 Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
6564 they represent two separate values, so they will get different pseudo
6565 registers during expansion. Then, since the two operands need to match
6566 per the constraints, but use different pseudo registers, reload can
6567 only register a reload for these operands. But reloads can only be
6568 satisfied by hardregs, not by memory, so we need a register for this
6569 reload, just because we are presented with non-matching operands.
6570 So, even though we allow memory for this operand, no memory can be
6571 used for it, just because the two operands don't match. This can
6572 cause reload failures on register-starved targets.
6574 So it's a symptom of reload not being able to use memory for reloads
6575 or, alternatively it's also a symptom of both operands not coming into
6576 reload as matching (in which case the pseudo could go to memory just
6577 fine, as the alternative allows it, and no reload would be necessary).
6578 We fix the latter problem here, by transforming
6580 asm ("": "=mr" (inout_2) : "0" (inout_1));
6582 back to
6584 inout_2 = inout_1;
6585 asm ("": "=mr" (inout_2) : "0" (inout_2)); */
6587 static void
6588 match_asm_constraints_1 (rtx_insn *insn, rtx *p_sets, int noutputs)
6590 int i;
6591 bool changed = false;
6592 rtx op = SET_SRC (p_sets[0]);
6593 int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
6594 rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
6595 bool *output_matched = XALLOCAVEC (bool, noutputs);
6597 memset (output_matched, 0, noutputs * sizeof (bool));
6598 for (i = 0; i < ninputs; i++)
6600 rtx input, output;
6601 rtx_insn *insns;
6602 const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
6603 char *end;
6604 int match, j;
6606 if (*constraint == '%')
6607 constraint++;
6609 match = strtoul (constraint, &end, 10);
6610 if (end == constraint)
6611 continue;
6613 gcc_assert (match < noutputs);
6614 output = SET_DEST (p_sets[match]);
6615 input = RTVEC_ELT (inputs, i);
6616 /* Only do the transformation for pseudos. */
6617 if (! REG_P (output)
6618 || rtx_equal_p (output, input)
6619 || (GET_MODE (input) != VOIDmode
6620 && GET_MODE (input) != GET_MODE (output)))
6621 continue;
6623 /* We can't do anything if the output is also used as input,
6624 as we're going to overwrite it. */
6625 for (j = 0; j < ninputs; j++)
6626 if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
6627 break;
6628 if (j != ninputs)
6629 continue;
6631 /* Avoid changing the same input several times. For
6632 asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
6633 only change in once (to out1), rather than changing it
6634 first to out1 and afterwards to out2. */
6635 if (i > 0)
6637 for (j = 0; j < noutputs; j++)
6638 if (output_matched[j] && input == SET_DEST (p_sets[j]))
6639 break;
6640 if (j != noutputs)
6641 continue;
6643 output_matched[match] = true;
6645 start_sequence ();
6646 emit_move_insn (output, input);
6647 insns = get_insns ();
6648 end_sequence ();
6649 emit_insn_before (insns, insn);
6651 /* Now replace all mentions of the input with output. We can't
6652 just replace the occurrence in inputs[i], as the register might
6653 also be used in some other input (or even in an address of an
6654 output), which would mean possibly increasing the number of
6655 inputs by one (namely 'output' in addition), which might pose
6656 a too complicated problem for reload to solve. E.g. this situation:
6658 asm ("" : "=r" (output), "=m" (input) : "0" (input))
6660 Here 'input' is used in two occurrences as input (once for the
6661 input operand, once for the address in the second output operand).
6662 If we would replace only the occurrence of the input operand (to
6663 make the matching) we would be left with this:
6665 output = input
6666 asm ("" : "=r" (output), "=m" (input) : "0" (output))
6668 Now we suddenly have two different input values (containing the same
6669 value, but different pseudos) where we formerly had only one.
6670 With more complicated asms this might lead to reload failures
6671 which wouldn't have happen without this pass. So, iterate over
6672 all operands and replace all occurrences of the register used. */
6673 for (j = 0; j < noutputs; j++)
6674 if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
6675 && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
6676 SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
6677 input, output);
6678 for (j = 0; j < ninputs; j++)
6679 if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
6680 RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
6681 input, output);
6683 changed = true;
6686 if (changed)
6687 df_insn_rescan (insn);
6690 /* Add the decl D to the local_decls list of FUN. */
6692 void
6693 add_local_decl (struct function *fun, tree d)
6695 gcc_assert (TREE_CODE (d) == VAR_DECL);
6696 vec_safe_push (fun->local_decls, d);
6699 namespace {
6701 const pass_data pass_data_match_asm_constraints =
6703 RTL_PASS, /* type */
6704 "asmcons", /* name */
6705 OPTGROUP_NONE, /* optinfo_flags */
6706 TV_NONE, /* tv_id */
6707 0, /* properties_required */
6708 0, /* properties_provided */
6709 0, /* properties_destroyed */
6710 0, /* todo_flags_start */
6711 0, /* todo_flags_finish */
6714 class pass_match_asm_constraints : public rtl_opt_pass
6716 public:
6717 pass_match_asm_constraints (gcc::context *ctxt)
6718 : rtl_opt_pass (pass_data_match_asm_constraints, ctxt)
6721 /* opt_pass methods: */
6722 virtual unsigned int execute (function *);
6724 }; // class pass_match_asm_constraints
6726 unsigned
6727 pass_match_asm_constraints::execute (function *fun)
6729 basic_block bb;
6730 rtx_insn *insn;
6731 rtx pat, *p_sets;
6732 int noutputs;
6734 if (!crtl->has_asm_statement)
6735 return 0;
6737 df_set_flags (DF_DEFER_INSN_RESCAN);
6738 FOR_EACH_BB_FN (bb, fun)
6740 FOR_BB_INSNS (bb, insn)
6742 if (!INSN_P (insn))
6743 continue;
6745 pat = PATTERN (insn);
6746 if (GET_CODE (pat) == PARALLEL)
6747 p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
6748 else if (GET_CODE (pat) == SET)
6749 p_sets = &PATTERN (insn), noutputs = 1;
6750 else
6751 continue;
6753 if (GET_CODE (*p_sets) == SET
6754 && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
6755 match_asm_constraints_1 (insn, p_sets, noutputs);
6759 return TODO_df_finish;
6762 } // anon namespace
6764 rtl_opt_pass *
6765 make_pass_match_asm_constraints (gcc::context *ctxt)
6767 return new pass_match_asm_constraints (ctxt);
6771 #include "gt-function.h"