* gcc.target/i386/funcspec-2.c, gcc.target/i386/funcspec-3.c,
[official-gcc.git] / gcc / function.c
bloba61e4757c7a2ea93318e92f89253b9ac665a9426
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file handles the generation of rtl code from tree structure
21 at the level of the function as a whole.
22 It creates the rtl expressions for parameters and auto variables
23 and has full responsibility for allocating stack slots.
25 `expand_function_start' is called at the beginning of a function,
26 before the function body is parsed, and `expand_function_end' is
27 called after parsing the body.
29 Call `assign_stack_local' to allocate a stack slot for a local variable.
30 This is usually done during the RTL generation for the function body,
31 but it can also be done in the reload pass when a pseudo-register does
32 not get a hard register. */
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "tm.h"
38 #include "rtl-error.h"
39 #include "tree.h"
40 #include "stor-layout.h"
41 #include "varasm.h"
42 #include "stringpool.h"
43 #include "flags.h"
44 #include "except.h"
45 #include "function.h"
46 #include "expr.h"
47 #include "optabs.h"
48 #include "libfuncs.h"
49 #include "regs.h"
50 #include "hard-reg-set.h"
51 #include "insn-config.h"
52 #include "recog.h"
53 #include "output.h"
54 #include "hashtab.h"
55 #include "tm_p.h"
56 #include "langhooks.h"
57 #include "target.h"
58 #include "common/common-target.h"
59 #include "gimple-expr.h"
60 #include "gimplify.h"
61 #include "tree-pass.h"
62 #include "predict.h"
63 #include "df.h"
64 #include "params.h"
65 #include "bb-reorder.h"
67 /* So we can assign to cfun in this file. */
68 #undef cfun
70 #ifndef STACK_ALIGNMENT_NEEDED
71 #define STACK_ALIGNMENT_NEEDED 1
72 #endif
74 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
76 /* Round a value to the lowest integer less than it that is a multiple of
77 the required alignment. Avoid using division in case the value is
78 negative. Assume the alignment is a power of two. */
79 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
81 /* Similar, but round to the next highest integer that meets the
82 alignment. */
83 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
85 /* Nonzero once virtual register instantiation has been done.
86 assign_stack_local uses frame_pointer_rtx when this is nonzero.
87 calls.c:emit_library_call_value_1 uses it to set up
88 post-instantiation libcalls. */
89 int virtuals_instantiated;
91 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
92 static GTY(()) int funcdef_no;
94 /* These variables hold pointers to functions to create and destroy
95 target specific, per-function data structures. */
96 struct machine_function * (*init_machine_status) (void);
98 /* The currently compiled function. */
99 struct function *cfun = 0;
101 /* These hashes record the prologue and epilogue insns. */
102 static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
103 htab_t prologue_insn_hash;
104 static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
105 htab_t epilogue_insn_hash;
108 htab_t types_used_by_vars_hash = NULL;
109 vec<tree, va_gc> *types_used_by_cur_var_decl;
111 /* Forward declarations. */
113 static struct temp_slot *find_temp_slot_from_address (rtx);
114 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
115 static void pad_below (struct args_size *, enum machine_mode, tree);
116 static void reorder_blocks_1 (rtx, tree, vec<tree> *);
117 static int all_blocks (tree, tree *);
118 static tree *get_block_vector (tree, int *);
119 extern tree debug_find_var_in_block_tree (tree, tree);
120 /* We always define `record_insns' even if it's not used so that we
121 can always export `prologue_epilogue_contains'. */
122 static void record_insns (rtx, rtx, htab_t *) ATTRIBUTE_UNUSED;
123 static bool contains (const_rtx, htab_t);
124 static void prepare_function_start (void);
125 static void do_clobber_return_reg (rtx, void *);
126 static void do_use_return_reg (rtx, void *);
128 /* Stack of nested functions. */
129 /* Keep track of the cfun stack. */
131 typedef struct function *function_p;
133 static vec<function_p> function_context_stack;
135 /* Save the current context for compilation of a nested function.
136 This is called from language-specific code. */
138 void
139 push_function_context (void)
141 if (cfun == 0)
142 allocate_struct_function (NULL, false);
144 function_context_stack.safe_push (cfun);
145 set_cfun (NULL);
148 /* Restore the last saved context, at the end of a nested function.
149 This function is called from language-specific code. */
151 void
152 pop_function_context (void)
154 struct function *p = function_context_stack.pop ();
155 set_cfun (p);
156 current_function_decl = p->decl;
158 /* Reset variables that have known state during rtx generation. */
159 virtuals_instantiated = 0;
160 generating_concat_p = 1;
163 /* Clear out all parts of the state in F that can safely be discarded
164 after the function has been parsed, but not compiled, to let
165 garbage collection reclaim the memory. */
167 void
168 free_after_parsing (struct function *f)
170 f->language = 0;
173 /* Clear out all parts of the state in F that can safely be discarded
174 after the function has been compiled, to let garbage collection
175 reclaim the memory. */
177 void
178 free_after_compilation (struct function *f)
180 prologue_insn_hash = NULL;
181 epilogue_insn_hash = NULL;
183 free (crtl->emit.regno_pointer_align);
185 memset (crtl, 0, sizeof (struct rtl_data));
186 f->eh = NULL;
187 f->machine = NULL;
188 f->cfg = NULL;
190 regno_reg_rtx = NULL;
193 /* Return size needed for stack frame based on slots so far allocated.
194 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
195 the caller may have to do that. */
197 HOST_WIDE_INT
198 get_frame_size (void)
200 if (FRAME_GROWS_DOWNWARD)
201 return -frame_offset;
202 else
203 return frame_offset;
206 /* Issue an error message and return TRUE if frame OFFSET overflows in
207 the signed target pointer arithmetics for function FUNC. Otherwise
208 return FALSE. */
210 bool
211 frame_offset_overflow (HOST_WIDE_INT offset, tree func)
213 unsigned HOST_WIDE_INT size = FRAME_GROWS_DOWNWARD ? -offset : offset;
215 if (size > ((unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (Pmode) - 1))
216 /* Leave room for the fixed part of the frame. */
217 - 64 * UNITS_PER_WORD)
219 error_at (DECL_SOURCE_LOCATION (func),
220 "total size of local objects too large");
221 return TRUE;
224 return FALSE;
227 /* Return stack slot alignment in bits for TYPE and MODE. */
229 static unsigned int
230 get_stack_local_alignment (tree type, enum machine_mode mode)
232 unsigned int alignment;
234 if (mode == BLKmode)
235 alignment = BIGGEST_ALIGNMENT;
236 else
237 alignment = GET_MODE_ALIGNMENT (mode);
239 /* Allow the frond-end to (possibly) increase the alignment of this
240 stack slot. */
241 if (! type)
242 type = lang_hooks.types.type_for_mode (mode, 0);
244 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
247 /* Determine whether it is possible to fit a stack slot of size SIZE and
248 alignment ALIGNMENT into an area in the stack frame that starts at
249 frame offset START and has a length of LENGTH. If so, store the frame
250 offset to be used for the stack slot in *POFFSET and return true;
251 return false otherwise. This function will extend the frame size when
252 given a start/length pair that lies at the end of the frame. */
254 static bool
255 try_fit_stack_local (HOST_WIDE_INT start, HOST_WIDE_INT length,
256 HOST_WIDE_INT size, unsigned int alignment,
257 HOST_WIDE_INT *poffset)
259 HOST_WIDE_INT this_frame_offset;
260 int frame_off, frame_alignment, frame_phase;
262 /* Calculate how many bytes the start of local variables is off from
263 stack alignment. */
264 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
265 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
266 frame_phase = frame_off ? frame_alignment - frame_off : 0;
268 /* Round the frame offset to the specified alignment. */
270 /* We must be careful here, since FRAME_OFFSET might be negative and
271 division with a negative dividend isn't as well defined as we might
272 like. So we instead assume that ALIGNMENT is a power of two and
273 use logical operations which are unambiguous. */
274 if (FRAME_GROWS_DOWNWARD)
275 this_frame_offset
276 = (FLOOR_ROUND (start + length - size - frame_phase,
277 (unsigned HOST_WIDE_INT) alignment)
278 + frame_phase);
279 else
280 this_frame_offset
281 = (CEIL_ROUND (start - frame_phase,
282 (unsigned HOST_WIDE_INT) alignment)
283 + frame_phase);
285 /* See if it fits. If this space is at the edge of the frame,
286 consider extending the frame to make it fit. Our caller relies on
287 this when allocating a new slot. */
288 if (frame_offset == start && this_frame_offset < frame_offset)
289 frame_offset = this_frame_offset;
290 else if (this_frame_offset < start)
291 return false;
292 else if (start + length == frame_offset
293 && this_frame_offset + size > start + length)
294 frame_offset = this_frame_offset + size;
295 else if (this_frame_offset + size > start + length)
296 return false;
298 *poffset = this_frame_offset;
299 return true;
302 /* Create a new frame_space structure describing free space in the stack
303 frame beginning at START and ending at END, and chain it into the
304 function's frame_space_list. */
306 static void
307 add_frame_space (HOST_WIDE_INT start, HOST_WIDE_INT end)
309 struct frame_space *space = ggc_alloc_frame_space ();
310 space->next = crtl->frame_space_list;
311 crtl->frame_space_list = space;
312 space->start = start;
313 space->length = end - start;
316 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
317 with machine mode MODE.
319 ALIGN controls the amount of alignment for the address of the slot:
320 0 means according to MODE,
321 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
322 -2 means use BITS_PER_UNIT,
323 positive specifies alignment boundary in bits.
325 KIND has ASLK_REDUCE_ALIGN bit set if it is OK to reduce
326 alignment and ASLK_RECORD_PAD bit set if we should remember
327 extra space we allocated for alignment purposes. When we are
328 called from assign_stack_temp_for_type, it is not set so we don't
329 track the same stack slot in two independent lists.
331 We do not round to stack_boundary here. */
334 assign_stack_local_1 (enum machine_mode mode, HOST_WIDE_INT size,
335 int align, int kind)
337 rtx x, addr;
338 int bigend_correction = 0;
339 HOST_WIDE_INT slot_offset = 0, old_frame_offset;
340 unsigned int alignment, alignment_in_bits;
342 if (align == 0)
344 alignment = get_stack_local_alignment (NULL, mode);
345 alignment /= BITS_PER_UNIT;
347 else if (align == -1)
349 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
350 size = CEIL_ROUND (size, alignment);
352 else if (align == -2)
353 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
354 else
355 alignment = align / BITS_PER_UNIT;
357 alignment_in_bits = alignment * BITS_PER_UNIT;
359 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
360 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
362 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
363 alignment = alignment_in_bits / BITS_PER_UNIT;
366 if (SUPPORTS_STACK_ALIGNMENT)
368 if (crtl->stack_alignment_estimated < alignment_in_bits)
370 if (!crtl->stack_realign_processed)
371 crtl->stack_alignment_estimated = alignment_in_bits;
372 else
374 /* If stack is realigned and stack alignment value
375 hasn't been finalized, it is OK not to increase
376 stack_alignment_estimated. The bigger alignment
377 requirement is recorded in stack_alignment_needed
378 below. */
379 gcc_assert (!crtl->stack_realign_finalized);
380 if (!crtl->stack_realign_needed)
382 /* It is OK to reduce the alignment as long as the
383 requested size is 0 or the estimated stack
384 alignment >= mode alignment. */
385 gcc_assert ((kind & ASLK_REDUCE_ALIGN)
386 || size == 0
387 || (crtl->stack_alignment_estimated
388 >= GET_MODE_ALIGNMENT (mode)));
389 alignment_in_bits = crtl->stack_alignment_estimated;
390 alignment = alignment_in_bits / BITS_PER_UNIT;
396 if (crtl->stack_alignment_needed < alignment_in_bits)
397 crtl->stack_alignment_needed = alignment_in_bits;
398 if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
399 crtl->max_used_stack_slot_alignment = alignment_in_bits;
401 if (mode != BLKmode || size != 0)
403 if (kind & ASLK_RECORD_PAD)
405 struct frame_space **psp;
407 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
409 struct frame_space *space = *psp;
410 if (!try_fit_stack_local (space->start, space->length, size,
411 alignment, &slot_offset))
412 continue;
413 *psp = space->next;
414 if (slot_offset > space->start)
415 add_frame_space (space->start, slot_offset);
416 if (slot_offset + size < space->start + space->length)
417 add_frame_space (slot_offset + size,
418 space->start + space->length);
419 goto found_space;
423 else if (!STACK_ALIGNMENT_NEEDED)
425 slot_offset = frame_offset;
426 goto found_space;
429 old_frame_offset = frame_offset;
431 if (FRAME_GROWS_DOWNWARD)
433 frame_offset -= size;
434 try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
436 if (kind & ASLK_RECORD_PAD)
438 if (slot_offset > frame_offset)
439 add_frame_space (frame_offset, slot_offset);
440 if (slot_offset + size < old_frame_offset)
441 add_frame_space (slot_offset + size, old_frame_offset);
444 else
446 frame_offset += size;
447 try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
449 if (kind & ASLK_RECORD_PAD)
451 if (slot_offset > old_frame_offset)
452 add_frame_space (old_frame_offset, slot_offset);
453 if (slot_offset + size < frame_offset)
454 add_frame_space (slot_offset + size, frame_offset);
458 found_space:
459 /* On a big-endian machine, if we are allocating more space than we will use,
460 use the least significant bytes of those that are allocated. */
461 if (BYTES_BIG_ENDIAN && mode != BLKmode && GET_MODE_SIZE (mode) < size)
462 bigend_correction = size - GET_MODE_SIZE (mode);
464 /* If we have already instantiated virtual registers, return the actual
465 address relative to the frame pointer. */
466 if (virtuals_instantiated)
467 addr = plus_constant (Pmode, frame_pointer_rtx,
468 trunc_int_for_mode
469 (slot_offset + bigend_correction
470 + STARTING_FRAME_OFFSET, Pmode));
471 else
472 addr = plus_constant (Pmode, virtual_stack_vars_rtx,
473 trunc_int_for_mode
474 (slot_offset + bigend_correction,
475 Pmode));
477 x = gen_rtx_MEM (mode, addr);
478 set_mem_align (x, alignment_in_bits);
479 MEM_NOTRAP_P (x) = 1;
481 stack_slot_list
482 = gen_rtx_EXPR_LIST (VOIDmode, x, stack_slot_list);
484 if (frame_offset_overflow (frame_offset, current_function_decl))
485 frame_offset = 0;
487 return x;
490 /* Wrap up assign_stack_local_1 with last parameter as false. */
493 assign_stack_local (enum machine_mode mode, HOST_WIDE_INT size, int align)
495 return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
498 /* In order to evaluate some expressions, such as function calls returning
499 structures in memory, we need to temporarily allocate stack locations.
500 We record each allocated temporary in the following structure.
502 Associated with each temporary slot is a nesting level. When we pop up
503 one level, all temporaries associated with the previous level are freed.
504 Normally, all temporaries are freed after the execution of the statement
505 in which they were created. However, if we are inside a ({...}) grouping,
506 the result may be in a temporary and hence must be preserved. If the
507 result could be in a temporary, we preserve it if we can determine which
508 one it is in. If we cannot determine which temporary may contain the
509 result, all temporaries are preserved. A temporary is preserved by
510 pretending it was allocated at the previous nesting level. */
512 struct GTY(()) temp_slot {
513 /* Points to next temporary slot. */
514 struct temp_slot *next;
515 /* Points to previous temporary slot. */
516 struct temp_slot *prev;
517 /* The rtx to used to reference the slot. */
518 rtx slot;
519 /* The size, in units, of the slot. */
520 HOST_WIDE_INT size;
521 /* The type of the object in the slot, or zero if it doesn't correspond
522 to a type. We use this to determine whether a slot can be reused.
523 It can be reused if objects of the type of the new slot will always
524 conflict with objects of the type of the old slot. */
525 tree type;
526 /* The alignment (in bits) of the slot. */
527 unsigned int align;
528 /* Nonzero if this temporary is currently in use. */
529 char in_use;
530 /* Nesting level at which this slot is being used. */
531 int level;
532 /* The offset of the slot from the frame_pointer, including extra space
533 for alignment. This info is for combine_temp_slots. */
534 HOST_WIDE_INT base_offset;
535 /* The size of the slot, including extra space for alignment. This
536 info is for combine_temp_slots. */
537 HOST_WIDE_INT full_size;
540 /* A table of addresses that represent a stack slot. The table is a mapping
541 from address RTXen to a temp slot. */
542 static GTY((param_is(struct temp_slot_address_entry))) htab_t temp_slot_address_table;
543 static size_t n_temp_slots_in_use;
545 /* Entry for the above hash table. */
546 struct GTY(()) temp_slot_address_entry {
547 hashval_t hash;
548 rtx address;
549 struct temp_slot *temp_slot;
552 /* Removes temporary slot TEMP from LIST. */
554 static void
555 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
557 if (temp->next)
558 temp->next->prev = temp->prev;
559 if (temp->prev)
560 temp->prev->next = temp->next;
561 else
562 *list = temp->next;
564 temp->prev = temp->next = NULL;
567 /* Inserts temporary slot TEMP to LIST. */
569 static void
570 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
572 temp->next = *list;
573 if (*list)
574 (*list)->prev = temp;
575 temp->prev = NULL;
576 *list = temp;
579 /* Returns the list of used temp slots at LEVEL. */
581 static struct temp_slot **
582 temp_slots_at_level (int level)
584 if (level >= (int) vec_safe_length (used_temp_slots))
585 vec_safe_grow_cleared (used_temp_slots, level + 1);
587 return &(*used_temp_slots)[level];
590 /* Returns the maximal temporary slot level. */
592 static int
593 max_slot_level (void)
595 if (!used_temp_slots)
596 return -1;
598 return used_temp_slots->length () - 1;
601 /* Moves temporary slot TEMP to LEVEL. */
603 static void
604 move_slot_to_level (struct temp_slot *temp, int level)
606 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
607 insert_slot_to_list (temp, temp_slots_at_level (level));
608 temp->level = level;
611 /* Make temporary slot TEMP available. */
613 static void
614 make_slot_available (struct temp_slot *temp)
616 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
617 insert_slot_to_list (temp, &avail_temp_slots);
618 temp->in_use = 0;
619 temp->level = -1;
620 n_temp_slots_in_use--;
623 /* Compute the hash value for an address -> temp slot mapping.
624 The value is cached on the mapping entry. */
625 static hashval_t
626 temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
628 int do_not_record = 0;
629 return hash_rtx (t->address, GET_MODE (t->address),
630 &do_not_record, NULL, false);
633 /* Return the hash value for an address -> temp slot mapping. */
634 static hashval_t
635 temp_slot_address_hash (const void *p)
637 const struct temp_slot_address_entry *t;
638 t = (const struct temp_slot_address_entry *) p;
639 return t->hash;
642 /* Compare two address -> temp slot mapping entries. */
643 static int
644 temp_slot_address_eq (const void *p1, const void *p2)
646 const struct temp_slot_address_entry *t1, *t2;
647 t1 = (const struct temp_slot_address_entry *) p1;
648 t2 = (const struct temp_slot_address_entry *) p2;
649 return exp_equiv_p (t1->address, t2->address, 0, true);
652 /* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
653 static void
654 insert_temp_slot_address (rtx address, struct temp_slot *temp_slot)
656 void **slot;
657 struct temp_slot_address_entry *t = ggc_alloc_temp_slot_address_entry ();
658 t->address = address;
659 t->temp_slot = temp_slot;
660 t->hash = temp_slot_address_compute_hash (t);
661 slot = htab_find_slot_with_hash (temp_slot_address_table, t, t->hash, INSERT);
662 *slot = t;
665 /* Remove an address -> temp slot mapping entry if the temp slot is
666 not in use anymore. Callback for remove_unused_temp_slot_addresses. */
667 static int
668 remove_unused_temp_slot_addresses_1 (void **slot, void *data ATTRIBUTE_UNUSED)
670 const struct temp_slot_address_entry *t;
671 t = (const struct temp_slot_address_entry *) *slot;
672 if (! t->temp_slot->in_use)
673 htab_clear_slot (temp_slot_address_table, slot);
674 return 1;
677 /* Remove all mappings of addresses to unused temp slots. */
678 static void
679 remove_unused_temp_slot_addresses (void)
681 /* Use quicker clearing if there aren't any active temp slots. */
682 if (n_temp_slots_in_use)
683 htab_traverse (temp_slot_address_table,
684 remove_unused_temp_slot_addresses_1,
685 NULL);
686 else
687 htab_empty (temp_slot_address_table);
690 /* Find the temp slot corresponding to the object at address X. */
692 static struct temp_slot *
693 find_temp_slot_from_address (rtx x)
695 struct temp_slot *p;
696 struct temp_slot_address_entry tmp, *t;
698 /* First try the easy way:
699 See if X exists in the address -> temp slot mapping. */
700 tmp.address = x;
701 tmp.temp_slot = NULL;
702 tmp.hash = temp_slot_address_compute_hash (&tmp);
703 t = (struct temp_slot_address_entry *)
704 htab_find_with_hash (temp_slot_address_table, &tmp, tmp.hash);
705 if (t)
706 return t->temp_slot;
708 /* If we have a sum involving a register, see if it points to a temp
709 slot. */
710 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
711 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
712 return p;
713 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
714 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
715 return p;
717 /* Last resort: Address is a virtual stack var address. */
718 if (GET_CODE (x) == PLUS
719 && XEXP (x, 0) == virtual_stack_vars_rtx
720 && CONST_INT_P (XEXP (x, 1)))
722 int i;
723 for (i = max_slot_level (); i >= 0; i--)
724 for (p = *temp_slots_at_level (i); p; p = p->next)
726 if (INTVAL (XEXP (x, 1)) >= p->base_offset
727 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size)
728 return p;
732 return NULL;
735 /* Allocate a temporary stack slot and record it for possible later
736 reuse.
738 MODE is the machine mode to be given to the returned rtx.
740 SIZE is the size in units of the space required. We do no rounding here
741 since assign_stack_local will do any required rounding.
743 TYPE is the type that will be used for the stack slot. */
746 assign_stack_temp_for_type (enum machine_mode mode, HOST_WIDE_INT size,
747 tree type)
749 unsigned int align;
750 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
751 rtx slot;
753 /* If SIZE is -1 it means that somebody tried to allocate a temporary
754 of a variable size. */
755 gcc_assert (size != -1);
757 align = get_stack_local_alignment (type, mode);
759 /* Try to find an available, already-allocated temporary of the proper
760 mode which meets the size and alignment requirements. Choose the
761 smallest one with the closest alignment.
763 If assign_stack_temp is called outside of the tree->rtl expansion,
764 we cannot reuse the stack slots (that may still refer to
765 VIRTUAL_STACK_VARS_REGNUM). */
766 if (!virtuals_instantiated)
768 for (p = avail_temp_slots; p; p = p->next)
770 if (p->align >= align && p->size >= size
771 && GET_MODE (p->slot) == mode
772 && objects_must_conflict_p (p->type, type)
773 && (best_p == 0 || best_p->size > p->size
774 || (best_p->size == p->size && best_p->align > p->align)))
776 if (p->align == align && p->size == size)
778 selected = p;
779 cut_slot_from_list (selected, &avail_temp_slots);
780 best_p = 0;
781 break;
783 best_p = p;
788 /* Make our best, if any, the one to use. */
789 if (best_p)
791 selected = best_p;
792 cut_slot_from_list (selected, &avail_temp_slots);
794 /* If there are enough aligned bytes left over, make them into a new
795 temp_slot so that the extra bytes don't get wasted. Do this only
796 for BLKmode slots, so that we can be sure of the alignment. */
797 if (GET_MODE (best_p->slot) == BLKmode)
799 int alignment = best_p->align / BITS_PER_UNIT;
800 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
802 if (best_p->size - rounded_size >= alignment)
804 p = ggc_alloc_temp_slot ();
805 p->in_use = 0;
806 p->size = best_p->size - rounded_size;
807 p->base_offset = best_p->base_offset + rounded_size;
808 p->full_size = best_p->full_size - rounded_size;
809 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
810 p->align = best_p->align;
811 p->type = best_p->type;
812 insert_slot_to_list (p, &avail_temp_slots);
814 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
815 stack_slot_list);
817 best_p->size = rounded_size;
818 best_p->full_size = rounded_size;
823 /* If we still didn't find one, make a new temporary. */
824 if (selected == 0)
826 HOST_WIDE_INT frame_offset_old = frame_offset;
828 p = ggc_alloc_temp_slot ();
830 /* We are passing an explicit alignment request to assign_stack_local.
831 One side effect of that is assign_stack_local will not round SIZE
832 to ensure the frame offset remains suitably aligned.
834 So for requests which depended on the rounding of SIZE, we go ahead
835 and round it now. We also make sure ALIGNMENT is at least
836 BIGGEST_ALIGNMENT. */
837 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
838 p->slot = assign_stack_local_1 (mode,
839 (mode == BLKmode
840 ? CEIL_ROUND (size,
841 (int) align
842 / BITS_PER_UNIT)
843 : size),
844 align, 0);
846 p->align = align;
848 /* The following slot size computation is necessary because we don't
849 know the actual size of the temporary slot until assign_stack_local
850 has performed all the frame alignment and size rounding for the
851 requested temporary. Note that extra space added for alignment
852 can be either above or below this stack slot depending on which
853 way the frame grows. We include the extra space if and only if it
854 is above this slot. */
855 if (FRAME_GROWS_DOWNWARD)
856 p->size = frame_offset_old - frame_offset;
857 else
858 p->size = size;
860 /* Now define the fields used by combine_temp_slots. */
861 if (FRAME_GROWS_DOWNWARD)
863 p->base_offset = frame_offset;
864 p->full_size = frame_offset_old - frame_offset;
866 else
868 p->base_offset = frame_offset_old;
869 p->full_size = frame_offset - frame_offset_old;
872 selected = p;
875 p = selected;
876 p->in_use = 1;
877 p->type = type;
878 p->level = temp_slot_level;
879 n_temp_slots_in_use++;
881 pp = temp_slots_at_level (p->level);
882 insert_slot_to_list (p, pp);
883 insert_temp_slot_address (XEXP (p->slot, 0), p);
885 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
886 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
887 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
889 /* If we know the alias set for the memory that will be used, use
890 it. If there's no TYPE, then we don't know anything about the
891 alias set for the memory. */
892 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
893 set_mem_align (slot, align);
895 /* If a type is specified, set the relevant flags. */
896 if (type != 0)
897 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
898 MEM_NOTRAP_P (slot) = 1;
900 return slot;
903 /* Allocate a temporary stack slot and record it for possible later
904 reuse. First two arguments are same as in preceding function. */
907 assign_stack_temp (enum machine_mode mode, HOST_WIDE_INT size)
909 return assign_stack_temp_for_type (mode, size, NULL_TREE);
912 /* Assign a temporary.
913 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
914 and so that should be used in error messages. In either case, we
915 allocate of the given type.
916 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
917 it is 0 if a register is OK.
918 DONT_PROMOTE is 1 if we should not promote values in register
919 to wider modes. */
922 assign_temp (tree type_or_decl, int memory_required,
923 int dont_promote ATTRIBUTE_UNUSED)
925 tree type, decl;
926 enum machine_mode mode;
927 #ifdef PROMOTE_MODE
928 int unsignedp;
929 #endif
931 if (DECL_P (type_or_decl))
932 decl = type_or_decl, type = TREE_TYPE (decl);
933 else
934 decl = NULL, type = type_or_decl;
936 mode = TYPE_MODE (type);
937 #ifdef PROMOTE_MODE
938 unsignedp = TYPE_UNSIGNED (type);
939 #endif
941 if (mode == BLKmode || memory_required)
943 HOST_WIDE_INT size = int_size_in_bytes (type);
944 rtx tmp;
946 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
947 problems with allocating the stack space. */
948 if (size == 0)
949 size = 1;
951 /* Unfortunately, we don't yet know how to allocate variable-sized
952 temporaries. However, sometimes we can find a fixed upper limit on
953 the size, so try that instead. */
954 else if (size == -1)
955 size = max_int_size_in_bytes (type);
957 /* The size of the temporary may be too large to fit into an integer. */
958 /* ??? Not sure this should happen except for user silliness, so limit
959 this to things that aren't compiler-generated temporaries. The
960 rest of the time we'll die in assign_stack_temp_for_type. */
961 if (decl && size == -1
962 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
964 error ("size of variable %q+D is too large", decl);
965 size = 1;
968 tmp = assign_stack_temp_for_type (mode, size, type);
969 return tmp;
972 #ifdef PROMOTE_MODE
973 if (! dont_promote)
974 mode = promote_mode (type, mode, &unsignedp);
975 #endif
977 return gen_reg_rtx (mode);
980 /* Combine temporary stack slots which are adjacent on the stack.
982 This allows for better use of already allocated stack space. This is only
983 done for BLKmode slots because we can be sure that we won't have alignment
984 problems in this case. */
986 static void
987 combine_temp_slots (void)
989 struct temp_slot *p, *q, *next, *next_q;
990 int num_slots;
992 /* We can't combine slots, because the information about which slot
993 is in which alias set will be lost. */
994 if (flag_strict_aliasing)
995 return;
997 /* If there are a lot of temp slots, don't do anything unless
998 high levels of optimization. */
999 if (! flag_expensive_optimizations)
1000 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
1001 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1002 return;
1004 for (p = avail_temp_slots; p; p = next)
1006 int delete_p = 0;
1008 next = p->next;
1010 if (GET_MODE (p->slot) != BLKmode)
1011 continue;
1013 for (q = p->next; q; q = next_q)
1015 int delete_q = 0;
1017 next_q = q->next;
1019 if (GET_MODE (q->slot) != BLKmode)
1020 continue;
1022 if (p->base_offset + p->full_size == q->base_offset)
1024 /* Q comes after P; combine Q into P. */
1025 p->size += q->size;
1026 p->full_size += q->full_size;
1027 delete_q = 1;
1029 else if (q->base_offset + q->full_size == p->base_offset)
1031 /* P comes after Q; combine P into Q. */
1032 q->size += p->size;
1033 q->full_size += p->full_size;
1034 delete_p = 1;
1035 break;
1037 if (delete_q)
1038 cut_slot_from_list (q, &avail_temp_slots);
1041 /* Either delete P or advance past it. */
1042 if (delete_p)
1043 cut_slot_from_list (p, &avail_temp_slots);
1047 /* Indicate that NEW_RTX is an alternate way of referring to the temp
1048 slot that previously was known by OLD_RTX. */
1050 void
1051 update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1053 struct temp_slot *p;
1055 if (rtx_equal_p (old_rtx, new_rtx))
1056 return;
1058 p = find_temp_slot_from_address (old_rtx);
1060 /* If we didn't find one, see if both OLD_RTX is a PLUS. If so, and
1061 NEW_RTX is a register, see if one operand of the PLUS is a
1062 temporary location. If so, NEW_RTX points into it. Otherwise,
1063 if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1064 in common between them. If so, try a recursive call on those
1065 values. */
1066 if (p == 0)
1068 if (GET_CODE (old_rtx) != PLUS)
1069 return;
1071 if (REG_P (new_rtx))
1073 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1074 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1075 return;
1077 else if (GET_CODE (new_rtx) != PLUS)
1078 return;
1080 if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1081 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1082 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1083 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1084 else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1085 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1086 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1087 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
1089 return;
1092 /* Otherwise add an alias for the temp's address. */
1093 insert_temp_slot_address (new_rtx, p);
1096 /* If X could be a reference to a temporary slot, mark that slot as
1097 belonging to the to one level higher than the current level. If X
1098 matched one of our slots, just mark that one. Otherwise, we can't
1099 easily predict which it is, so upgrade all of them.
1101 This is called when an ({...}) construct occurs and a statement
1102 returns a value in memory. */
1104 void
1105 preserve_temp_slots (rtx x)
1107 struct temp_slot *p = 0, *next;
1109 if (x == 0)
1110 return;
1112 /* If X is a register that is being used as a pointer, see if we have
1113 a temporary slot we know it points to. */
1114 if (REG_P (x) && REG_POINTER (x))
1115 p = find_temp_slot_from_address (x);
1117 /* If X is not in memory or is at a constant address, it cannot be in
1118 a temporary slot. */
1119 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1120 return;
1122 /* First see if we can find a match. */
1123 if (p == 0)
1124 p = find_temp_slot_from_address (XEXP (x, 0));
1126 if (p != 0)
1128 if (p->level == temp_slot_level)
1129 move_slot_to_level (p, temp_slot_level - 1);
1130 return;
1133 /* Otherwise, preserve all non-kept slots at this level. */
1134 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1136 next = p->next;
1137 move_slot_to_level (p, temp_slot_level - 1);
1141 /* Free all temporaries used so far. This is normally called at the
1142 end of generating code for a statement. */
1144 void
1145 free_temp_slots (void)
1147 struct temp_slot *p, *next;
1148 bool some_available = false;
1150 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1152 next = p->next;
1153 make_slot_available (p);
1154 some_available = true;
1157 if (some_available)
1159 remove_unused_temp_slot_addresses ();
1160 combine_temp_slots ();
1164 /* Push deeper into the nesting level for stack temporaries. */
1166 void
1167 push_temp_slots (void)
1169 temp_slot_level++;
1172 /* Pop a temporary nesting level. All slots in use in the current level
1173 are freed. */
1175 void
1176 pop_temp_slots (void)
1178 free_temp_slots ();
1179 temp_slot_level--;
1182 /* Initialize temporary slots. */
1184 void
1185 init_temp_slots (void)
1187 /* We have not allocated any temporaries yet. */
1188 avail_temp_slots = 0;
1189 vec_alloc (used_temp_slots, 0);
1190 temp_slot_level = 0;
1191 n_temp_slots_in_use = 0;
1193 /* Set up the table to map addresses to temp slots. */
1194 if (! temp_slot_address_table)
1195 temp_slot_address_table = htab_create_ggc (32,
1196 temp_slot_address_hash,
1197 temp_slot_address_eq,
1198 NULL);
1199 else
1200 htab_empty (temp_slot_address_table);
1203 /* Functions and data structures to keep track of the values hard regs
1204 had at the start of the function. */
1206 /* Private type used by get_hard_reg_initial_reg, get_hard_reg_initial_val,
1207 and has_hard_reg_initial_val.. */
1208 typedef struct GTY(()) initial_value_pair {
1209 rtx hard_reg;
1210 rtx pseudo;
1211 } initial_value_pair;
1212 /* ??? This could be a VEC but there is currently no way to define an
1213 opaque VEC type. This could be worked around by defining struct
1214 initial_value_pair in function.h. */
1215 typedef struct GTY(()) initial_value_struct {
1216 int num_entries;
1217 int max_entries;
1218 initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
1219 } initial_value_struct;
1221 /* If a pseudo represents an initial hard reg (or expression), return
1222 it, else return NULL_RTX. */
1225 get_hard_reg_initial_reg (rtx reg)
1227 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1228 int i;
1230 if (ivs == 0)
1231 return NULL_RTX;
1233 for (i = 0; i < ivs->num_entries; i++)
1234 if (rtx_equal_p (ivs->entries[i].pseudo, reg))
1235 return ivs->entries[i].hard_reg;
1237 return NULL_RTX;
1240 /* Make sure that there's a pseudo register of mode MODE that stores the
1241 initial value of hard register REGNO. Return an rtx for such a pseudo. */
1244 get_hard_reg_initial_val (enum machine_mode mode, unsigned int regno)
1246 struct initial_value_struct *ivs;
1247 rtx rv;
1249 rv = has_hard_reg_initial_val (mode, regno);
1250 if (rv)
1251 return rv;
1253 ivs = crtl->hard_reg_initial_vals;
1254 if (ivs == 0)
1256 ivs = ggc_alloc_initial_value_struct ();
1257 ivs->num_entries = 0;
1258 ivs->max_entries = 5;
1259 ivs->entries = ggc_alloc_vec_initial_value_pair (5);
1260 crtl->hard_reg_initial_vals = ivs;
1263 if (ivs->num_entries >= ivs->max_entries)
1265 ivs->max_entries += 5;
1266 ivs->entries = GGC_RESIZEVEC (initial_value_pair, ivs->entries,
1267 ivs->max_entries);
1270 ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
1271 ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
1273 return ivs->entries[ivs->num_entries++].pseudo;
1276 /* See if get_hard_reg_initial_val has been used to create a pseudo
1277 for the initial value of hard register REGNO in mode MODE. Return
1278 the associated pseudo if so, otherwise return NULL. */
1281 has_hard_reg_initial_val (enum machine_mode mode, unsigned int regno)
1283 struct initial_value_struct *ivs;
1284 int i;
1286 ivs = crtl->hard_reg_initial_vals;
1287 if (ivs != 0)
1288 for (i = 0; i < ivs->num_entries; i++)
1289 if (GET_MODE (ivs->entries[i].hard_reg) == mode
1290 && REGNO (ivs->entries[i].hard_reg) == regno)
1291 return ivs->entries[i].pseudo;
1293 return NULL_RTX;
1296 unsigned int
1297 emit_initial_value_sets (void)
1299 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1300 int i;
1301 rtx seq;
1303 if (ivs == 0)
1304 return 0;
1306 start_sequence ();
1307 for (i = 0; i < ivs->num_entries; i++)
1308 emit_move_insn (ivs->entries[i].pseudo, ivs->entries[i].hard_reg);
1309 seq = get_insns ();
1310 end_sequence ();
1312 emit_insn_at_entry (seq);
1313 return 0;
1316 /* Return the hardreg-pseudoreg initial values pair entry I and
1317 TRUE if I is a valid entry, or FALSE if I is not a valid entry. */
1318 bool
1319 initial_value_entry (int i, rtx *hreg, rtx *preg)
1321 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1322 if (!ivs || i >= ivs->num_entries)
1323 return false;
1325 *hreg = ivs->entries[i].hard_reg;
1326 *preg = ivs->entries[i].pseudo;
1327 return true;
1330 /* These routines are responsible for converting virtual register references
1331 to the actual hard register references once RTL generation is complete.
1333 The following four variables are used for communication between the
1334 routines. They contain the offsets of the virtual registers from their
1335 respective hard registers. */
1337 static int in_arg_offset;
1338 static int var_offset;
1339 static int dynamic_offset;
1340 static int out_arg_offset;
1341 static int cfa_offset;
1343 /* In most machines, the stack pointer register is equivalent to the bottom
1344 of the stack. */
1346 #ifndef STACK_POINTER_OFFSET
1347 #define STACK_POINTER_OFFSET 0
1348 #endif
1350 /* If not defined, pick an appropriate default for the offset of dynamically
1351 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1352 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1354 #ifndef STACK_DYNAMIC_OFFSET
1356 /* The bottom of the stack points to the actual arguments. If
1357 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1358 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1359 stack space for register parameters is not pushed by the caller, but
1360 rather part of the fixed stack areas and hence not included in
1361 `crtl->outgoing_args_size'. Nevertheless, we must allow
1362 for it when allocating stack dynamic objects. */
1364 #if defined(REG_PARM_STACK_SPACE)
1365 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1366 ((ACCUMULATE_OUTGOING_ARGS \
1367 ? (crtl->outgoing_args_size \
1368 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1369 : REG_PARM_STACK_SPACE (FNDECL))) \
1370 : 0) + (STACK_POINTER_OFFSET))
1371 #else
1372 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1373 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0) \
1374 + (STACK_POINTER_OFFSET))
1375 #endif
1376 #endif
1379 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1380 is a virtual register, return the equivalent hard register and set the
1381 offset indirectly through the pointer. Otherwise, return 0. */
1383 static rtx
1384 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
1386 rtx new_rtx;
1387 HOST_WIDE_INT offset;
1389 if (x == virtual_incoming_args_rtx)
1391 if (stack_realign_drap)
1393 /* Replace virtual_incoming_args_rtx with internal arg
1394 pointer if DRAP is used to realign stack. */
1395 new_rtx = crtl->args.internal_arg_pointer;
1396 offset = 0;
1398 else
1399 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1401 else if (x == virtual_stack_vars_rtx)
1402 new_rtx = frame_pointer_rtx, offset = var_offset;
1403 else if (x == virtual_stack_dynamic_rtx)
1404 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1405 else if (x == virtual_outgoing_args_rtx)
1406 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1407 else if (x == virtual_cfa_rtx)
1409 #ifdef FRAME_POINTER_CFA_OFFSET
1410 new_rtx = frame_pointer_rtx;
1411 #else
1412 new_rtx = arg_pointer_rtx;
1413 #endif
1414 offset = cfa_offset;
1416 else if (x == virtual_preferred_stack_boundary_rtx)
1418 new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1419 offset = 0;
1421 else
1422 return NULL_RTX;
1424 *poffset = offset;
1425 return new_rtx;
1428 /* A subroutine of instantiate_virtual_regs, called via for_each_rtx.
1429 Instantiate any virtual registers present inside of *LOC. The expression
1430 is simplified, as much as possible, but is not to be considered "valid"
1431 in any sense implied by the target. If any change is made, set CHANGED
1432 to true. */
1434 static int
1435 instantiate_virtual_regs_in_rtx (rtx *loc, void *data)
1437 HOST_WIDE_INT offset;
1438 bool *changed = (bool *) data;
1439 rtx x, new_rtx;
1441 x = *loc;
1442 if (x == 0)
1443 return 0;
1445 switch (GET_CODE (x))
1447 case REG:
1448 new_rtx = instantiate_new_reg (x, &offset);
1449 if (new_rtx)
1451 *loc = plus_constant (GET_MODE (x), new_rtx, offset);
1452 if (changed)
1453 *changed = true;
1455 return -1;
1457 case PLUS:
1458 new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1459 if (new_rtx)
1461 new_rtx = plus_constant (GET_MODE (x), new_rtx, offset);
1462 *loc = simplify_gen_binary (PLUS, GET_MODE (x), new_rtx, XEXP (x, 1));
1463 if (changed)
1464 *changed = true;
1465 return -1;
1468 /* FIXME -- from old code */
1469 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1470 we can commute the PLUS and SUBREG because pointers into the
1471 frame are well-behaved. */
1472 break;
1474 default:
1475 break;
1478 return 0;
1481 /* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1482 matches the predicate for insn CODE operand OPERAND. */
1484 static int
1485 safe_insn_predicate (int code, int operand, rtx x)
1487 return code < 0 || insn_operand_matches ((enum insn_code) code, operand, x);
1490 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1491 registers present inside of insn. The result will be a valid insn. */
1493 static void
1494 instantiate_virtual_regs_in_insn (rtx insn)
1496 HOST_WIDE_INT offset;
1497 int insn_code, i;
1498 bool any_change = false;
1499 rtx set, new_rtx, x, seq;
1501 /* There are some special cases to be handled first. */
1502 set = single_set (insn);
1503 if (set)
1505 /* We're allowed to assign to a virtual register. This is interpreted
1506 to mean that the underlying register gets assigned the inverse
1507 transformation. This is used, for example, in the handling of
1508 non-local gotos. */
1509 new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1510 if (new_rtx)
1512 start_sequence ();
1514 for_each_rtx (&SET_SRC (set), instantiate_virtual_regs_in_rtx, NULL);
1515 x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
1516 gen_int_mode (-offset, GET_MODE (new_rtx)));
1517 x = force_operand (x, new_rtx);
1518 if (x != new_rtx)
1519 emit_move_insn (new_rtx, x);
1521 seq = get_insns ();
1522 end_sequence ();
1524 emit_insn_before (seq, insn);
1525 delete_insn (insn);
1526 return;
1529 /* Handle a straight copy from a virtual register by generating a
1530 new add insn. The difference between this and falling through
1531 to the generic case is avoiding a new pseudo and eliminating a
1532 move insn in the initial rtl stream. */
1533 new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1534 if (new_rtx && offset != 0
1535 && REG_P (SET_DEST (set))
1536 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1538 start_sequence ();
1540 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS, new_rtx,
1541 gen_int_mode (offset,
1542 GET_MODE (SET_DEST (set))),
1543 SET_DEST (set), 1, OPTAB_LIB_WIDEN);
1544 if (x != SET_DEST (set))
1545 emit_move_insn (SET_DEST (set), x);
1547 seq = get_insns ();
1548 end_sequence ();
1550 emit_insn_before (seq, insn);
1551 delete_insn (insn);
1552 return;
1555 extract_insn (insn);
1556 insn_code = INSN_CODE (insn);
1558 /* Handle a plus involving a virtual register by determining if the
1559 operands remain valid if they're modified in place. */
1560 if (GET_CODE (SET_SRC (set)) == PLUS
1561 && recog_data.n_operands >= 3
1562 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1563 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1564 && CONST_INT_P (recog_data.operand[2])
1565 && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
1567 offset += INTVAL (recog_data.operand[2]);
1569 /* If the sum is zero, then replace with a plain move. */
1570 if (offset == 0
1571 && REG_P (SET_DEST (set))
1572 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1574 start_sequence ();
1575 emit_move_insn (SET_DEST (set), new_rtx);
1576 seq = get_insns ();
1577 end_sequence ();
1579 emit_insn_before (seq, insn);
1580 delete_insn (insn);
1581 return;
1584 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1586 /* Using validate_change and apply_change_group here leaves
1587 recog_data in an invalid state. Since we know exactly what
1588 we want to check, do those two by hand. */
1589 if (safe_insn_predicate (insn_code, 1, new_rtx)
1590 && safe_insn_predicate (insn_code, 2, x))
1592 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1593 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1594 any_change = true;
1596 /* Fall through into the regular operand fixup loop in
1597 order to take care of operands other than 1 and 2. */
1601 else
1603 extract_insn (insn);
1604 insn_code = INSN_CODE (insn);
1607 /* In the general case, we expect virtual registers to appear only in
1608 operands, and then only as either bare registers or inside memories. */
1609 for (i = 0; i < recog_data.n_operands; ++i)
1611 x = recog_data.operand[i];
1612 switch (GET_CODE (x))
1614 case MEM:
1616 rtx addr = XEXP (x, 0);
1617 bool changed = false;
1619 for_each_rtx (&addr, instantiate_virtual_regs_in_rtx, &changed);
1620 if (!changed)
1621 continue;
1623 start_sequence ();
1624 x = replace_equiv_address (x, addr);
1625 /* It may happen that the address with the virtual reg
1626 was valid (e.g. based on the virtual stack reg, which might
1627 be acceptable to the predicates with all offsets), whereas
1628 the address now isn't anymore, for instance when the address
1629 is still offsetted, but the base reg isn't virtual-stack-reg
1630 anymore. Below we would do a force_reg on the whole operand,
1631 but this insn might actually only accept memory. Hence,
1632 before doing that last resort, try to reload the address into
1633 a register, so this operand stays a MEM. */
1634 if (!safe_insn_predicate (insn_code, i, x))
1636 addr = force_reg (GET_MODE (addr), addr);
1637 x = replace_equiv_address (x, addr);
1639 seq = get_insns ();
1640 end_sequence ();
1641 if (seq)
1642 emit_insn_before (seq, insn);
1644 break;
1646 case REG:
1647 new_rtx = instantiate_new_reg (x, &offset);
1648 if (new_rtx == NULL)
1649 continue;
1650 if (offset == 0)
1651 x = new_rtx;
1652 else
1654 start_sequence ();
1656 /* Careful, special mode predicates may have stuff in
1657 insn_data[insn_code].operand[i].mode that isn't useful
1658 to us for computing a new value. */
1659 /* ??? Recognize address_operand and/or "p" constraints
1660 to see if (plus new offset) is a valid before we put
1661 this through expand_simple_binop. */
1662 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1663 gen_int_mode (offset, GET_MODE (x)),
1664 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1665 seq = get_insns ();
1666 end_sequence ();
1667 emit_insn_before (seq, insn);
1669 break;
1671 case SUBREG:
1672 new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1673 if (new_rtx == NULL)
1674 continue;
1675 if (offset != 0)
1677 start_sequence ();
1678 new_rtx = expand_simple_binop
1679 (GET_MODE (new_rtx), PLUS, new_rtx,
1680 gen_int_mode (offset, GET_MODE (new_rtx)),
1681 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1682 seq = get_insns ();
1683 end_sequence ();
1684 emit_insn_before (seq, insn);
1686 x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1687 GET_MODE (new_rtx), SUBREG_BYTE (x));
1688 gcc_assert (x);
1689 break;
1691 default:
1692 continue;
1695 /* At this point, X contains the new value for the operand.
1696 Validate the new value vs the insn predicate. Note that
1697 asm insns will have insn_code -1 here. */
1698 if (!safe_insn_predicate (insn_code, i, x))
1700 start_sequence ();
1701 if (REG_P (x))
1703 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1704 x = copy_to_reg (x);
1706 else
1707 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1708 seq = get_insns ();
1709 end_sequence ();
1710 if (seq)
1711 emit_insn_before (seq, insn);
1714 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1715 any_change = true;
1718 if (any_change)
1720 /* Propagate operand changes into the duplicates. */
1721 for (i = 0; i < recog_data.n_dups; ++i)
1722 *recog_data.dup_loc[i]
1723 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1725 /* Force re-recognition of the instruction for validation. */
1726 INSN_CODE (insn) = -1;
1729 if (asm_noperands (PATTERN (insn)) >= 0)
1731 if (!check_asm_operands (PATTERN (insn)))
1733 error_for_asm (insn, "impossible constraint in %<asm%>");
1734 /* For asm goto, instead of fixing up all the edges
1735 just clear the template and clear input operands
1736 (asm goto doesn't have any output operands). */
1737 if (JUMP_P (insn))
1739 rtx asm_op = extract_asm_operands (PATTERN (insn));
1740 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1741 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1742 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1744 else
1745 delete_insn (insn);
1748 else
1750 if (recog_memoized (insn) < 0)
1751 fatal_insn_not_found (insn);
1755 /* Subroutine of instantiate_decls. Given RTL representing a decl,
1756 do any instantiation required. */
1758 void
1759 instantiate_decl_rtl (rtx x)
1761 rtx addr;
1763 if (x == 0)
1764 return;
1766 /* If this is a CONCAT, recurse for the pieces. */
1767 if (GET_CODE (x) == CONCAT)
1769 instantiate_decl_rtl (XEXP (x, 0));
1770 instantiate_decl_rtl (XEXP (x, 1));
1771 return;
1774 /* If this is not a MEM, no need to do anything. Similarly if the
1775 address is a constant or a register that is not a virtual register. */
1776 if (!MEM_P (x))
1777 return;
1779 addr = XEXP (x, 0);
1780 if (CONSTANT_P (addr)
1781 || (REG_P (addr)
1782 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1783 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1784 return;
1786 for_each_rtx (&XEXP (x, 0), instantiate_virtual_regs_in_rtx, NULL);
1789 /* Helper for instantiate_decls called via walk_tree: Process all decls
1790 in the given DECL_VALUE_EXPR. */
1792 static tree
1793 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1795 tree t = *tp;
1796 if (! EXPR_P (t))
1798 *walk_subtrees = 0;
1799 if (DECL_P (t))
1801 if (DECL_RTL_SET_P (t))
1802 instantiate_decl_rtl (DECL_RTL (t));
1803 if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1804 && DECL_INCOMING_RTL (t))
1805 instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1806 if ((TREE_CODE (t) == VAR_DECL
1807 || TREE_CODE (t) == RESULT_DECL)
1808 && DECL_HAS_VALUE_EXPR_P (t))
1810 tree v = DECL_VALUE_EXPR (t);
1811 walk_tree (&v, instantiate_expr, NULL, NULL);
1815 return NULL;
1818 /* Subroutine of instantiate_decls: Process all decls in the given
1819 BLOCK node and all its subblocks. */
1821 static void
1822 instantiate_decls_1 (tree let)
1824 tree t;
1826 for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1828 if (DECL_RTL_SET_P (t))
1829 instantiate_decl_rtl (DECL_RTL (t));
1830 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t))
1832 tree v = DECL_VALUE_EXPR (t);
1833 walk_tree (&v, instantiate_expr, NULL, NULL);
1837 /* Process all subblocks. */
1838 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1839 instantiate_decls_1 (t);
1842 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1843 all virtual registers in their DECL_RTL's. */
1845 static void
1846 instantiate_decls (tree fndecl)
1848 tree decl;
1849 unsigned ix;
1851 /* Process all parameters of the function. */
1852 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1854 instantiate_decl_rtl (DECL_RTL (decl));
1855 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1856 if (DECL_HAS_VALUE_EXPR_P (decl))
1858 tree v = DECL_VALUE_EXPR (decl);
1859 walk_tree (&v, instantiate_expr, NULL, NULL);
1863 if ((decl = DECL_RESULT (fndecl))
1864 && TREE_CODE (decl) == RESULT_DECL)
1866 if (DECL_RTL_SET_P (decl))
1867 instantiate_decl_rtl (DECL_RTL (decl));
1868 if (DECL_HAS_VALUE_EXPR_P (decl))
1870 tree v = DECL_VALUE_EXPR (decl);
1871 walk_tree (&v, instantiate_expr, NULL, NULL);
1875 /* Now process all variables defined in the function or its subblocks. */
1876 instantiate_decls_1 (DECL_INITIAL (fndecl));
1878 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1879 if (DECL_RTL_SET_P (decl))
1880 instantiate_decl_rtl (DECL_RTL (decl));
1881 vec_free (cfun->local_decls);
1884 /* Pass through the INSNS of function FNDECL and convert virtual register
1885 references to hard register references. */
1887 static unsigned int
1888 instantiate_virtual_regs (void)
1890 rtx insn;
1892 /* Compute the offsets to use for this function. */
1893 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1894 var_offset = STARTING_FRAME_OFFSET;
1895 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1896 out_arg_offset = STACK_POINTER_OFFSET;
1897 #ifdef FRAME_POINTER_CFA_OFFSET
1898 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1899 #else
1900 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1901 #endif
1903 /* Initialize recognition, indicating that volatile is OK. */
1904 init_recog ();
1906 /* Scan through all the insns, instantiating every virtual register still
1907 present. */
1908 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1909 if (INSN_P (insn))
1911 /* These patterns in the instruction stream can never be recognized.
1912 Fortunately, they shouldn't contain virtual registers either. */
1913 if (GET_CODE (PATTERN (insn)) == USE
1914 || GET_CODE (PATTERN (insn)) == CLOBBER
1915 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
1916 continue;
1917 else if (DEBUG_INSN_P (insn))
1918 for_each_rtx (&INSN_VAR_LOCATION (insn),
1919 instantiate_virtual_regs_in_rtx, NULL);
1920 else
1921 instantiate_virtual_regs_in_insn (insn);
1923 if (INSN_DELETED_P (insn))
1924 continue;
1926 for_each_rtx (&REG_NOTES (insn), instantiate_virtual_regs_in_rtx, NULL);
1928 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
1929 if (CALL_P (insn))
1930 for_each_rtx (&CALL_INSN_FUNCTION_USAGE (insn),
1931 instantiate_virtual_regs_in_rtx, NULL);
1934 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
1935 instantiate_decls (current_function_decl);
1937 targetm.instantiate_decls ();
1939 /* Indicate that, from now on, assign_stack_local should use
1940 frame_pointer_rtx. */
1941 virtuals_instantiated = 1;
1943 return 0;
1946 namespace {
1948 const pass_data pass_data_instantiate_virtual_regs =
1950 RTL_PASS, /* type */
1951 "vregs", /* name */
1952 OPTGROUP_NONE, /* optinfo_flags */
1953 false, /* has_gate */
1954 true, /* has_execute */
1955 TV_NONE, /* tv_id */
1956 0, /* properties_required */
1957 0, /* properties_provided */
1958 0, /* properties_destroyed */
1959 0, /* todo_flags_start */
1960 0, /* todo_flags_finish */
1963 class pass_instantiate_virtual_regs : public rtl_opt_pass
1965 public:
1966 pass_instantiate_virtual_regs (gcc::context *ctxt)
1967 : rtl_opt_pass (pass_data_instantiate_virtual_regs, ctxt)
1970 /* opt_pass methods: */
1971 unsigned int execute () { return instantiate_virtual_regs (); }
1973 }; // class pass_instantiate_virtual_regs
1975 } // anon namespace
1977 rtl_opt_pass *
1978 make_pass_instantiate_virtual_regs (gcc::context *ctxt)
1980 return new pass_instantiate_virtual_regs (ctxt);
1984 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
1985 This means a type for which function calls must pass an address to the
1986 function or get an address back from the function.
1987 EXP may be a type node or an expression (whose type is tested). */
1990 aggregate_value_p (const_tree exp, const_tree fntype)
1992 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
1993 int i, regno, nregs;
1994 rtx reg;
1996 if (fntype)
1997 switch (TREE_CODE (fntype))
1999 case CALL_EXPR:
2001 tree fndecl = get_callee_fndecl (fntype);
2002 fntype = (fndecl
2003 ? TREE_TYPE (fndecl)
2004 : TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype))));
2006 break;
2007 case FUNCTION_DECL:
2008 fntype = TREE_TYPE (fntype);
2009 break;
2010 case FUNCTION_TYPE:
2011 case METHOD_TYPE:
2012 break;
2013 case IDENTIFIER_NODE:
2014 fntype = NULL_TREE;
2015 break;
2016 default:
2017 /* We don't expect other tree types here. */
2018 gcc_unreachable ();
2021 if (VOID_TYPE_P (type))
2022 return 0;
2024 /* If a record should be passed the same as its first (and only) member
2025 don't pass it as an aggregate. */
2026 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2027 return aggregate_value_p (first_field (type), fntype);
2029 /* If the front end has decided that this needs to be passed by
2030 reference, do so. */
2031 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2032 && DECL_BY_REFERENCE (exp))
2033 return 1;
2035 /* Function types that are TREE_ADDRESSABLE force return in memory. */
2036 if (fntype && TREE_ADDRESSABLE (fntype))
2037 return 1;
2039 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2040 and thus can't be returned in registers. */
2041 if (TREE_ADDRESSABLE (type))
2042 return 1;
2044 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2045 return 1;
2047 if (targetm.calls.return_in_memory (type, fntype))
2048 return 1;
2050 /* Make sure we have suitable call-clobbered regs to return
2051 the value in; if not, we must return it in memory. */
2052 reg = hard_function_value (type, 0, fntype, 0);
2054 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2055 it is OK. */
2056 if (!REG_P (reg))
2057 return 0;
2059 regno = REGNO (reg);
2060 nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
2061 for (i = 0; i < nregs; i++)
2062 if (! call_used_regs[regno + i])
2063 return 1;
2065 return 0;
2068 /* Return true if we should assign DECL a pseudo register; false if it
2069 should live on the local stack. */
2071 bool
2072 use_register_for_decl (const_tree decl)
2074 if (!targetm.calls.allocate_stack_slots_for_args ())
2075 return true;
2077 /* Honor volatile. */
2078 if (TREE_SIDE_EFFECTS (decl))
2079 return false;
2081 /* Honor addressability. */
2082 if (TREE_ADDRESSABLE (decl))
2083 return false;
2085 /* Only register-like things go in registers. */
2086 if (DECL_MODE (decl) == BLKmode)
2087 return false;
2089 /* If -ffloat-store specified, don't put explicit float variables
2090 into registers. */
2091 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2092 propagates values across these stores, and it probably shouldn't. */
2093 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2094 return false;
2096 /* If we're not interested in tracking debugging information for
2097 this decl, then we can certainly put it in a register. */
2098 if (DECL_IGNORED_P (decl))
2099 return true;
2101 if (optimize)
2102 return true;
2104 if (!DECL_REGISTER (decl))
2105 return false;
2107 switch (TREE_CODE (TREE_TYPE (decl)))
2109 case RECORD_TYPE:
2110 case UNION_TYPE:
2111 case QUAL_UNION_TYPE:
2112 /* When not optimizing, disregard register keyword for variables with
2113 types containing methods, otherwise the methods won't be callable
2114 from the debugger. */
2115 if (TYPE_METHODS (TREE_TYPE (decl)))
2116 return false;
2117 break;
2118 default:
2119 break;
2122 return true;
2125 /* Return true if TYPE should be passed by invisible reference. */
2127 bool
2128 pass_by_reference (CUMULATIVE_ARGS *ca, enum machine_mode mode,
2129 tree type, bool named_arg)
2131 if (type)
2133 /* If this type contains non-trivial constructors, then it is
2134 forbidden for the middle-end to create any new copies. */
2135 if (TREE_ADDRESSABLE (type))
2136 return true;
2138 /* GCC post 3.4 passes *all* variable sized types by reference. */
2139 if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2140 return true;
2142 /* If a record type should be passed the same as its first (and only)
2143 member, use the type and mode of that member. */
2144 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2146 type = TREE_TYPE (first_field (type));
2147 mode = TYPE_MODE (type);
2151 return targetm.calls.pass_by_reference (pack_cumulative_args (ca), mode,
2152 type, named_arg);
2155 /* Return true if TYPE, which is passed by reference, should be callee
2156 copied instead of caller copied. */
2158 bool
2159 reference_callee_copied (CUMULATIVE_ARGS *ca, enum machine_mode mode,
2160 tree type, bool named_arg)
2162 if (type && TREE_ADDRESSABLE (type))
2163 return false;
2164 return targetm.calls.callee_copies (pack_cumulative_args (ca), mode, type,
2165 named_arg);
2168 /* Structures to communicate between the subroutines of assign_parms.
2169 The first holds data persistent across all parameters, the second
2170 is cleared out for each parameter. */
2172 struct assign_parm_data_all
2174 /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2175 should become a job of the target or otherwise encapsulated. */
2176 CUMULATIVE_ARGS args_so_far_v;
2177 cumulative_args_t args_so_far;
2178 struct args_size stack_args_size;
2179 tree function_result_decl;
2180 tree orig_fnargs;
2181 rtx first_conversion_insn;
2182 rtx last_conversion_insn;
2183 HOST_WIDE_INT pretend_args_size;
2184 HOST_WIDE_INT extra_pretend_bytes;
2185 int reg_parm_stack_space;
2188 struct assign_parm_data_one
2190 tree nominal_type;
2191 tree passed_type;
2192 rtx entry_parm;
2193 rtx stack_parm;
2194 enum machine_mode nominal_mode;
2195 enum machine_mode passed_mode;
2196 enum machine_mode promoted_mode;
2197 struct locate_and_pad_arg_data locate;
2198 int partial;
2199 BOOL_BITFIELD named_arg : 1;
2200 BOOL_BITFIELD passed_pointer : 1;
2201 BOOL_BITFIELD on_stack : 1;
2202 BOOL_BITFIELD loaded_in_reg : 1;
2205 /* A subroutine of assign_parms. Initialize ALL. */
2207 static void
2208 assign_parms_initialize_all (struct assign_parm_data_all *all)
2210 tree fntype ATTRIBUTE_UNUSED;
2212 memset (all, 0, sizeof (*all));
2214 fntype = TREE_TYPE (current_function_decl);
2216 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
2217 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2218 #else
2219 INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2220 current_function_decl, -1);
2221 #endif
2222 all->args_so_far = pack_cumulative_args (&all->args_so_far_v);
2224 #ifdef REG_PARM_STACK_SPACE
2225 all->reg_parm_stack_space = REG_PARM_STACK_SPACE (current_function_decl);
2226 #endif
2229 /* If ARGS contains entries with complex types, split the entry into two
2230 entries of the component type. Return a new list of substitutions are
2231 needed, else the old list. */
2233 static void
2234 split_complex_args (vec<tree> *args)
2236 unsigned i;
2237 tree p;
2239 FOR_EACH_VEC_ELT (*args, i, p)
2241 tree type = TREE_TYPE (p);
2242 if (TREE_CODE (type) == COMPLEX_TYPE
2243 && targetm.calls.split_complex_arg (type))
2245 tree decl;
2246 tree subtype = TREE_TYPE (type);
2247 bool addressable = TREE_ADDRESSABLE (p);
2249 /* Rewrite the PARM_DECL's type with its component. */
2250 p = copy_node (p);
2251 TREE_TYPE (p) = subtype;
2252 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2253 DECL_MODE (p) = VOIDmode;
2254 DECL_SIZE (p) = NULL;
2255 DECL_SIZE_UNIT (p) = NULL;
2256 /* If this arg must go in memory, put it in a pseudo here.
2257 We can't allow it to go in memory as per normal parms,
2258 because the usual place might not have the imag part
2259 adjacent to the real part. */
2260 DECL_ARTIFICIAL (p) = addressable;
2261 DECL_IGNORED_P (p) = addressable;
2262 TREE_ADDRESSABLE (p) = 0;
2263 layout_decl (p, 0);
2264 (*args)[i] = p;
2266 /* Build a second synthetic decl. */
2267 decl = build_decl (EXPR_LOCATION (p),
2268 PARM_DECL, NULL_TREE, subtype);
2269 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2270 DECL_ARTIFICIAL (decl) = addressable;
2271 DECL_IGNORED_P (decl) = addressable;
2272 layout_decl (decl, 0);
2273 args->safe_insert (++i, decl);
2278 /* A subroutine of assign_parms. Adjust the parameter list to incorporate
2279 the hidden struct return argument, and (abi willing) complex args.
2280 Return the new parameter list. */
2282 static vec<tree>
2283 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2285 tree fndecl = current_function_decl;
2286 tree fntype = TREE_TYPE (fndecl);
2287 vec<tree> fnargs = vNULL;
2288 tree arg;
2290 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2291 fnargs.safe_push (arg);
2293 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2295 /* If struct value address is treated as the first argument, make it so. */
2296 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2297 && ! cfun->returns_pcc_struct
2298 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2300 tree type = build_pointer_type (TREE_TYPE (fntype));
2301 tree decl;
2303 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2304 PARM_DECL, get_identifier (".result_ptr"), type);
2305 DECL_ARG_TYPE (decl) = type;
2306 DECL_ARTIFICIAL (decl) = 1;
2307 DECL_NAMELESS (decl) = 1;
2308 TREE_CONSTANT (decl) = 1;
2310 DECL_CHAIN (decl) = all->orig_fnargs;
2311 all->orig_fnargs = decl;
2312 fnargs.safe_insert (0, decl);
2314 all->function_result_decl = decl;
2317 /* If the target wants to split complex arguments into scalars, do so. */
2318 if (targetm.calls.split_complex_arg)
2319 split_complex_args (&fnargs);
2321 return fnargs;
2324 /* A subroutine of assign_parms. Examine PARM and pull out type and mode
2325 data for the parameter. Incorporate ABI specifics such as pass-by-
2326 reference and type promotion. */
2328 static void
2329 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2330 struct assign_parm_data_one *data)
2332 tree nominal_type, passed_type;
2333 enum machine_mode nominal_mode, passed_mode, promoted_mode;
2334 int unsignedp;
2336 memset (data, 0, sizeof (*data));
2338 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
2339 if (!cfun->stdarg)
2340 data->named_arg = 1; /* No variadic parms. */
2341 else if (DECL_CHAIN (parm))
2342 data->named_arg = 1; /* Not the last non-variadic parm. */
2343 else if (targetm.calls.strict_argument_naming (all->args_so_far))
2344 data->named_arg = 1; /* Only variadic ones are unnamed. */
2345 else
2346 data->named_arg = 0; /* Treat as variadic. */
2348 nominal_type = TREE_TYPE (parm);
2349 passed_type = DECL_ARG_TYPE (parm);
2351 /* Look out for errors propagating this far. Also, if the parameter's
2352 type is void then its value doesn't matter. */
2353 if (TREE_TYPE (parm) == error_mark_node
2354 /* This can happen after weird syntax errors
2355 or if an enum type is defined among the parms. */
2356 || TREE_CODE (parm) != PARM_DECL
2357 || passed_type == NULL
2358 || VOID_TYPE_P (nominal_type))
2360 nominal_type = passed_type = void_type_node;
2361 nominal_mode = passed_mode = promoted_mode = VOIDmode;
2362 goto egress;
2365 /* Find mode of arg as it is passed, and mode of arg as it should be
2366 during execution of this function. */
2367 passed_mode = TYPE_MODE (passed_type);
2368 nominal_mode = TYPE_MODE (nominal_type);
2370 /* If the parm is to be passed as a transparent union or record, use the
2371 type of the first field for the tests below. We have already verified
2372 that the modes are the same. */
2373 if ((TREE_CODE (passed_type) == UNION_TYPE
2374 || TREE_CODE (passed_type) == RECORD_TYPE)
2375 && TYPE_TRANSPARENT_AGGR (passed_type))
2376 passed_type = TREE_TYPE (first_field (passed_type));
2378 /* See if this arg was passed by invisible reference. */
2379 if (pass_by_reference (&all->args_so_far_v, passed_mode,
2380 passed_type, data->named_arg))
2382 passed_type = nominal_type = build_pointer_type (passed_type);
2383 data->passed_pointer = true;
2384 passed_mode = nominal_mode = TYPE_MODE (nominal_type);
2387 /* Find mode as it is passed by the ABI. */
2388 unsignedp = TYPE_UNSIGNED (passed_type);
2389 promoted_mode = promote_function_mode (passed_type, passed_mode, &unsignedp,
2390 TREE_TYPE (current_function_decl), 0);
2392 egress:
2393 data->nominal_type = nominal_type;
2394 data->passed_type = passed_type;
2395 data->nominal_mode = nominal_mode;
2396 data->passed_mode = passed_mode;
2397 data->promoted_mode = promoted_mode;
2400 /* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2402 static void
2403 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2404 struct assign_parm_data_one *data, bool no_rtl)
2406 int varargs_pretend_bytes = 0;
2408 targetm.calls.setup_incoming_varargs (all->args_so_far,
2409 data->promoted_mode,
2410 data->passed_type,
2411 &varargs_pretend_bytes, no_rtl);
2413 /* If the back-end has requested extra stack space, record how much is
2414 needed. Do not change pretend_args_size otherwise since it may be
2415 nonzero from an earlier partial argument. */
2416 if (varargs_pretend_bytes > 0)
2417 all->pretend_args_size = varargs_pretend_bytes;
2420 /* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2421 the incoming location of the current parameter. */
2423 static void
2424 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2425 struct assign_parm_data_one *data)
2427 HOST_WIDE_INT pretend_bytes = 0;
2428 rtx entry_parm;
2429 bool in_regs;
2431 if (data->promoted_mode == VOIDmode)
2433 data->entry_parm = data->stack_parm = const0_rtx;
2434 return;
2437 entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2438 data->promoted_mode,
2439 data->passed_type,
2440 data->named_arg);
2442 if (entry_parm == 0)
2443 data->promoted_mode = data->passed_mode;
2445 /* Determine parm's home in the stack, in case it arrives in the stack
2446 or we should pretend it did. Compute the stack position and rtx where
2447 the argument arrives and its size.
2449 There is one complexity here: If this was a parameter that would
2450 have been passed in registers, but wasn't only because it is
2451 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2452 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2453 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2454 as it was the previous time. */
2455 in_regs = entry_parm != 0;
2456 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2457 in_regs = true;
2458 #endif
2459 if (!in_regs && !data->named_arg)
2461 if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2463 rtx tem;
2464 tem = targetm.calls.function_incoming_arg (all->args_so_far,
2465 data->promoted_mode,
2466 data->passed_type, true);
2467 in_regs = tem != NULL;
2471 /* If this parameter was passed both in registers and in the stack, use
2472 the copy on the stack. */
2473 if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2474 data->passed_type))
2475 entry_parm = 0;
2477 if (entry_parm)
2479 int partial;
2481 partial = targetm.calls.arg_partial_bytes (all->args_so_far,
2482 data->promoted_mode,
2483 data->passed_type,
2484 data->named_arg);
2485 data->partial = partial;
2487 /* The caller might already have allocated stack space for the
2488 register parameters. */
2489 if (partial != 0 && all->reg_parm_stack_space == 0)
2491 /* Part of this argument is passed in registers and part
2492 is passed on the stack. Ask the prologue code to extend
2493 the stack part so that we can recreate the full value.
2495 PRETEND_BYTES is the size of the registers we need to store.
2496 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2497 stack space that the prologue should allocate.
2499 Internally, gcc assumes that the argument pointer is aligned
2500 to STACK_BOUNDARY bits. This is used both for alignment
2501 optimizations (see init_emit) and to locate arguments that are
2502 aligned to more than PARM_BOUNDARY bits. We must preserve this
2503 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2504 a stack boundary. */
2506 /* We assume at most one partial arg, and it must be the first
2507 argument on the stack. */
2508 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2510 pretend_bytes = partial;
2511 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2513 /* We want to align relative to the actual stack pointer, so
2514 don't include this in the stack size until later. */
2515 all->extra_pretend_bytes = all->pretend_args_size;
2519 locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2520 all->reg_parm_stack_space,
2521 entry_parm ? data->partial : 0, current_function_decl,
2522 &all->stack_args_size, &data->locate);
2524 /* Update parm_stack_boundary if this parameter is passed in the
2525 stack. */
2526 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2527 crtl->parm_stack_boundary = data->locate.boundary;
2529 /* Adjust offsets to include the pretend args. */
2530 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2531 data->locate.slot_offset.constant += pretend_bytes;
2532 data->locate.offset.constant += pretend_bytes;
2534 data->entry_parm = entry_parm;
2537 /* A subroutine of assign_parms. If there is actually space on the stack
2538 for this parm, count it in stack_args_size and return true. */
2540 static bool
2541 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2542 struct assign_parm_data_one *data)
2544 /* Trivially true if we've no incoming register. */
2545 if (data->entry_parm == NULL)
2547 /* Also true if we're partially in registers and partially not,
2548 since we've arranged to drop the entire argument on the stack. */
2549 else if (data->partial != 0)
2551 /* Also true if the target says that it's passed in both registers
2552 and on the stack. */
2553 else if (GET_CODE (data->entry_parm) == PARALLEL
2554 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2556 /* Also true if the target says that there's stack allocated for
2557 all register parameters. */
2558 else if (all->reg_parm_stack_space > 0)
2560 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2561 else
2562 return false;
2564 all->stack_args_size.constant += data->locate.size.constant;
2565 if (data->locate.size.var)
2566 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2568 return true;
2571 /* A subroutine of assign_parms. Given that this parameter is allocated
2572 stack space by the ABI, find it. */
2574 static void
2575 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2577 rtx offset_rtx, stack_parm;
2578 unsigned int align, boundary;
2580 /* If we're passing this arg using a reg, make its stack home the
2581 aligned stack slot. */
2582 if (data->entry_parm)
2583 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2584 else
2585 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2587 stack_parm = crtl->args.internal_arg_pointer;
2588 if (offset_rtx != const0_rtx)
2589 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2590 stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2592 if (!data->passed_pointer)
2594 set_mem_attributes (stack_parm, parm, 1);
2595 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2596 while promoted mode's size is needed. */
2597 if (data->promoted_mode != BLKmode
2598 && data->promoted_mode != DECL_MODE (parm))
2600 set_mem_size (stack_parm, GET_MODE_SIZE (data->promoted_mode));
2601 if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2603 int offset = subreg_lowpart_offset (DECL_MODE (parm),
2604 data->promoted_mode);
2605 if (offset)
2606 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2611 boundary = data->locate.boundary;
2612 align = BITS_PER_UNIT;
2614 /* If we're padding upward, we know that the alignment of the slot
2615 is TARGET_FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2616 intentionally forcing upward padding. Otherwise we have to come
2617 up with a guess at the alignment based on OFFSET_RTX. */
2618 if (data->locate.where_pad != downward || data->entry_parm)
2619 align = boundary;
2620 else if (CONST_INT_P (offset_rtx))
2622 align = INTVAL (offset_rtx) * BITS_PER_UNIT | boundary;
2623 align = align & -align;
2625 set_mem_align (stack_parm, align);
2627 if (data->entry_parm)
2628 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2630 data->stack_parm = stack_parm;
2633 /* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2634 always valid and contiguous. */
2636 static void
2637 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2639 rtx entry_parm = data->entry_parm;
2640 rtx stack_parm = data->stack_parm;
2642 /* If this parm was passed part in regs and part in memory, pretend it
2643 arrived entirely in memory by pushing the register-part onto the stack.
2644 In the special case of a DImode or DFmode that is split, we could put
2645 it together in a pseudoreg directly, but for now that's not worth
2646 bothering with. */
2647 if (data->partial != 0)
2649 /* Handle calls that pass values in multiple non-contiguous
2650 locations. The Irix 6 ABI has examples of this. */
2651 if (GET_CODE (entry_parm) == PARALLEL)
2652 emit_group_store (validize_mem (stack_parm), entry_parm,
2653 data->passed_type,
2654 int_size_in_bytes (data->passed_type));
2655 else
2657 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2658 move_block_from_reg (REGNO (entry_parm), validize_mem (stack_parm),
2659 data->partial / UNITS_PER_WORD);
2662 entry_parm = stack_parm;
2665 /* If we didn't decide this parm came in a register, by default it came
2666 on the stack. */
2667 else if (entry_parm == NULL)
2668 entry_parm = stack_parm;
2670 /* When an argument is passed in multiple locations, we can't make use
2671 of this information, but we can save some copying if the whole argument
2672 is passed in a single register. */
2673 else if (GET_CODE (entry_parm) == PARALLEL
2674 && data->nominal_mode != BLKmode
2675 && data->passed_mode != BLKmode)
2677 size_t i, len = XVECLEN (entry_parm, 0);
2679 for (i = 0; i < len; i++)
2680 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2681 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2682 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2683 == data->passed_mode)
2684 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2686 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2687 break;
2691 data->entry_parm = entry_parm;
2694 /* A subroutine of assign_parms. Reconstitute any values which were
2695 passed in multiple registers and would fit in a single register. */
2697 static void
2698 assign_parm_remove_parallels (struct assign_parm_data_one *data)
2700 rtx entry_parm = data->entry_parm;
2702 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2703 This can be done with register operations rather than on the
2704 stack, even if we will store the reconstituted parameter on the
2705 stack later. */
2706 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2708 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2709 emit_group_store (parmreg, entry_parm, data->passed_type,
2710 GET_MODE_SIZE (GET_MODE (entry_parm)));
2711 entry_parm = parmreg;
2714 data->entry_parm = entry_parm;
2717 /* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2718 always valid and properly aligned. */
2720 static void
2721 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2723 rtx stack_parm = data->stack_parm;
2725 /* If we can't trust the parm stack slot to be aligned enough for its
2726 ultimate type, don't use that slot after entry. We'll make another
2727 stack slot, if we need one. */
2728 if (stack_parm
2729 && ((STRICT_ALIGNMENT
2730 && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2731 || (data->nominal_type
2732 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2733 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2734 stack_parm = NULL;
2736 /* If parm was passed in memory, and we need to convert it on entry,
2737 don't store it back in that same slot. */
2738 else if (data->entry_parm == stack_parm
2739 && data->nominal_mode != BLKmode
2740 && data->nominal_mode != data->passed_mode)
2741 stack_parm = NULL;
2743 /* If stack protection is in effect for this function, don't leave any
2744 pointers in their passed stack slots. */
2745 else if (crtl->stack_protect_guard
2746 && (flag_stack_protect == 2
2747 || data->passed_pointer
2748 || POINTER_TYPE_P (data->nominal_type)))
2749 stack_parm = NULL;
2751 data->stack_parm = stack_parm;
2754 /* A subroutine of assign_parms. Return true if the current parameter
2755 should be stored as a BLKmode in the current frame. */
2757 static bool
2758 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2760 if (data->nominal_mode == BLKmode)
2761 return true;
2762 if (GET_MODE (data->entry_parm) == BLKmode)
2763 return true;
2765 #ifdef BLOCK_REG_PADDING
2766 /* Only assign_parm_setup_block knows how to deal with register arguments
2767 that are padded at the least significant end. */
2768 if (REG_P (data->entry_parm)
2769 && GET_MODE_SIZE (data->promoted_mode) < UNITS_PER_WORD
2770 && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2771 == (BYTES_BIG_ENDIAN ? upward : downward)))
2772 return true;
2773 #endif
2775 return false;
2778 /* A subroutine of assign_parms. Arrange for the parameter to be
2779 present and valid in DATA->STACK_RTL. */
2781 static void
2782 assign_parm_setup_block (struct assign_parm_data_all *all,
2783 tree parm, struct assign_parm_data_one *data)
2785 rtx entry_parm = data->entry_parm;
2786 rtx stack_parm = data->stack_parm;
2787 HOST_WIDE_INT size;
2788 HOST_WIDE_INT size_stored;
2790 if (GET_CODE (entry_parm) == PARALLEL)
2791 entry_parm = emit_group_move_into_temps (entry_parm);
2793 size = int_size_in_bytes (data->passed_type);
2794 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2795 if (stack_parm == 0)
2797 DECL_ALIGN (parm) = MAX (DECL_ALIGN (parm), BITS_PER_WORD);
2798 stack_parm = assign_stack_local (BLKmode, size_stored,
2799 DECL_ALIGN (parm));
2800 if (GET_MODE_SIZE (GET_MODE (entry_parm)) == size)
2801 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2802 set_mem_attributes (stack_parm, parm, 1);
2805 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2806 calls that pass values in multiple non-contiguous locations. */
2807 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2809 rtx mem;
2811 /* Note that we will be storing an integral number of words.
2812 So we have to be careful to ensure that we allocate an
2813 integral number of words. We do this above when we call
2814 assign_stack_local if space was not allocated in the argument
2815 list. If it was, this will not work if PARM_BOUNDARY is not
2816 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2817 if it becomes a problem. Exception is when BLKmode arrives
2818 with arguments not conforming to word_mode. */
2820 if (data->stack_parm == 0)
2822 else if (GET_CODE (entry_parm) == PARALLEL)
2824 else
2825 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2827 mem = validize_mem (stack_parm);
2829 /* Handle values in multiple non-contiguous locations. */
2830 if (GET_CODE (entry_parm) == PARALLEL)
2832 push_to_sequence2 (all->first_conversion_insn,
2833 all->last_conversion_insn);
2834 emit_group_store (mem, entry_parm, data->passed_type, size);
2835 all->first_conversion_insn = get_insns ();
2836 all->last_conversion_insn = get_last_insn ();
2837 end_sequence ();
2840 else if (size == 0)
2843 /* If SIZE is that of a mode no bigger than a word, just use
2844 that mode's store operation. */
2845 else if (size <= UNITS_PER_WORD)
2847 enum machine_mode mode
2848 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2850 if (mode != BLKmode
2851 #ifdef BLOCK_REG_PADDING
2852 && (size == UNITS_PER_WORD
2853 || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2854 != (BYTES_BIG_ENDIAN ? upward : downward)))
2855 #endif
2858 rtx reg;
2860 /* We are really truncating a word_mode value containing
2861 SIZE bytes into a value of mode MODE. If such an
2862 operation requires no actual instructions, we can refer
2863 to the value directly in mode MODE, otherwise we must
2864 start with the register in word_mode and explicitly
2865 convert it. */
2866 if (TRULY_NOOP_TRUNCATION (size * BITS_PER_UNIT, BITS_PER_WORD))
2867 reg = gen_rtx_REG (mode, REGNO (entry_parm));
2868 else
2870 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2871 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
2873 emit_move_insn (change_address (mem, mode, 0), reg);
2876 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
2877 machine must be aligned to the left before storing
2878 to memory. Note that the previous test doesn't
2879 handle all cases (e.g. SIZE == 3). */
2880 else if (size != UNITS_PER_WORD
2881 #ifdef BLOCK_REG_PADDING
2882 && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2883 == downward)
2884 #else
2885 && BYTES_BIG_ENDIAN
2886 #endif
2889 rtx tem, x;
2890 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2891 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2893 x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
2894 tem = change_address (mem, word_mode, 0);
2895 emit_move_insn (tem, x);
2897 else
2898 move_block_from_reg (REGNO (entry_parm), mem,
2899 size_stored / UNITS_PER_WORD);
2901 else
2902 move_block_from_reg (REGNO (entry_parm), mem,
2903 size_stored / UNITS_PER_WORD);
2905 else if (data->stack_parm == 0)
2907 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
2908 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
2909 BLOCK_OP_NORMAL);
2910 all->first_conversion_insn = get_insns ();
2911 all->last_conversion_insn = get_last_insn ();
2912 end_sequence ();
2915 data->stack_parm = stack_parm;
2916 SET_DECL_RTL (parm, stack_parm);
2919 /* A subroutine of assign_parms. Allocate a pseudo to hold the current
2920 parameter. Get it there. Perform all ABI specified conversions. */
2922 static void
2923 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
2924 struct assign_parm_data_one *data)
2926 rtx parmreg, validated_mem;
2927 rtx equiv_stack_parm;
2928 enum machine_mode promoted_nominal_mode;
2929 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
2930 bool did_conversion = false;
2931 bool need_conversion, moved;
2933 /* Store the parm in a pseudoregister during the function, but we may
2934 need to do it in a wider mode. Using 2 here makes the result
2935 consistent with promote_decl_mode and thus expand_expr_real_1. */
2936 promoted_nominal_mode
2937 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
2938 TREE_TYPE (current_function_decl), 2);
2940 parmreg = gen_reg_rtx (promoted_nominal_mode);
2942 if (!DECL_ARTIFICIAL (parm))
2943 mark_user_reg (parmreg);
2945 /* If this was an item that we received a pointer to,
2946 set DECL_RTL appropriately. */
2947 if (data->passed_pointer)
2949 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
2950 set_mem_attributes (x, parm, 1);
2951 SET_DECL_RTL (parm, x);
2953 else
2954 SET_DECL_RTL (parm, parmreg);
2956 assign_parm_remove_parallels (data);
2958 /* Copy the value into the register, thus bridging between
2959 assign_parm_find_data_types and expand_expr_real_1. */
2961 equiv_stack_parm = data->stack_parm;
2962 validated_mem = validize_mem (data->entry_parm);
2964 need_conversion = (data->nominal_mode != data->passed_mode
2965 || promoted_nominal_mode != data->promoted_mode);
2966 moved = false;
2968 if (need_conversion
2969 && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
2970 && data->nominal_mode == data->passed_mode
2971 && data->nominal_mode == GET_MODE (data->entry_parm))
2973 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
2974 mode, by the caller. We now have to convert it to
2975 NOMINAL_MODE, if different. However, PARMREG may be in
2976 a different mode than NOMINAL_MODE if it is being stored
2977 promoted.
2979 If ENTRY_PARM is a hard register, it might be in a register
2980 not valid for operating in its mode (e.g., an odd-numbered
2981 register for a DFmode). In that case, moves are the only
2982 thing valid, so we can't do a convert from there. This
2983 occurs when the calling sequence allow such misaligned
2984 usages.
2986 In addition, the conversion may involve a call, which could
2987 clobber parameters which haven't been copied to pseudo
2988 registers yet.
2990 First, we try to emit an insn which performs the necessary
2991 conversion. We verify that this insn does not clobber any
2992 hard registers. */
2994 enum insn_code icode;
2995 rtx op0, op1;
2997 icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
2998 unsignedp);
3000 op0 = parmreg;
3001 op1 = validated_mem;
3002 if (icode != CODE_FOR_nothing
3003 && insn_operand_matches (icode, 0, op0)
3004 && insn_operand_matches (icode, 1, op1))
3006 enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
3007 rtx insn, insns, t = op1;
3008 HARD_REG_SET hardregs;
3010 start_sequence ();
3011 /* If op1 is a hard register that is likely spilled, first
3012 force it into a pseudo, otherwise combiner might extend
3013 its lifetime too much. */
3014 if (GET_CODE (t) == SUBREG)
3015 t = SUBREG_REG (t);
3016 if (REG_P (t)
3017 && HARD_REGISTER_P (t)
3018 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (t))
3019 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (t))))
3021 t = gen_reg_rtx (GET_MODE (op1));
3022 emit_move_insn (t, op1);
3024 else
3025 t = op1;
3026 insn = gen_extend_insn (op0, t, promoted_nominal_mode,
3027 data->passed_mode, unsignedp);
3028 emit_insn (insn);
3029 insns = get_insns ();
3031 moved = true;
3032 CLEAR_HARD_REG_SET (hardregs);
3033 for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3035 if (INSN_P (insn))
3036 note_stores (PATTERN (insn), record_hard_reg_sets,
3037 &hardregs);
3038 if (!hard_reg_set_empty_p (hardregs))
3039 moved = false;
3042 end_sequence ();
3044 if (moved)
3046 emit_insn (insns);
3047 if (equiv_stack_parm != NULL_RTX)
3048 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3049 equiv_stack_parm);
3054 if (moved)
3055 /* Nothing to do. */
3057 else if (need_conversion)
3059 /* We did not have an insn to convert directly, or the sequence
3060 generated appeared unsafe. We must first copy the parm to a
3061 pseudo reg, and save the conversion until after all
3062 parameters have been moved. */
3064 int save_tree_used;
3065 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3067 emit_move_insn (tempreg, validated_mem);
3069 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3070 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3072 if (GET_CODE (tempreg) == SUBREG
3073 && GET_MODE (tempreg) == data->nominal_mode
3074 && REG_P (SUBREG_REG (tempreg))
3075 && data->nominal_mode == data->passed_mode
3076 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm)
3077 && GET_MODE_SIZE (GET_MODE (tempreg))
3078 < GET_MODE_SIZE (GET_MODE (data->entry_parm)))
3080 /* The argument is already sign/zero extended, so note it
3081 into the subreg. */
3082 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3083 SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
3086 /* TREE_USED gets set erroneously during expand_assignment. */
3087 save_tree_used = TREE_USED (parm);
3088 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3089 TREE_USED (parm) = save_tree_used;
3090 all->first_conversion_insn = get_insns ();
3091 all->last_conversion_insn = get_last_insn ();
3092 end_sequence ();
3094 did_conversion = true;
3096 else
3097 emit_move_insn (parmreg, validated_mem);
3099 /* If we were passed a pointer but the actual value can safely live
3100 in a register, retrieve it and use it directly. */
3101 if (data->passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode)
3103 /* We can't use nominal_mode, because it will have been set to
3104 Pmode above. We must use the actual mode of the parm. */
3105 if (use_register_for_decl (parm))
3107 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3108 mark_user_reg (parmreg);
3110 else
3112 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3113 TYPE_MODE (TREE_TYPE (parm)),
3114 TYPE_ALIGN (TREE_TYPE (parm)));
3115 parmreg
3116 = assign_stack_local (TYPE_MODE (TREE_TYPE (parm)),
3117 GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parm))),
3118 align);
3119 set_mem_attributes (parmreg, parm, 1);
3122 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
3124 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
3125 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3127 push_to_sequence2 (all->first_conversion_insn,
3128 all->last_conversion_insn);
3129 emit_move_insn (tempreg, DECL_RTL (parm));
3130 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3131 emit_move_insn (parmreg, tempreg);
3132 all->first_conversion_insn = get_insns ();
3133 all->last_conversion_insn = get_last_insn ();
3134 end_sequence ();
3136 did_conversion = true;
3138 else
3139 emit_move_insn (parmreg, DECL_RTL (parm));
3141 SET_DECL_RTL (parm, parmreg);
3143 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3144 now the parm. */
3145 data->stack_parm = NULL;
3148 /* Mark the register as eliminable if we did no conversion and it was
3149 copied from memory at a fixed offset, and the arg pointer was not
3150 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3151 offset formed an invalid address, such memory-equivalences as we
3152 make here would screw up life analysis for it. */
3153 if (data->nominal_mode == data->passed_mode
3154 && !did_conversion
3155 && data->stack_parm != 0
3156 && MEM_P (data->stack_parm)
3157 && data->locate.offset.var == 0
3158 && reg_mentioned_p (virtual_incoming_args_rtx,
3159 XEXP (data->stack_parm, 0)))
3161 rtx linsn = get_last_insn ();
3162 rtx sinsn, set;
3164 /* Mark complex types separately. */
3165 if (GET_CODE (parmreg) == CONCAT)
3167 enum machine_mode submode
3168 = GET_MODE_INNER (GET_MODE (parmreg));
3169 int regnor = REGNO (XEXP (parmreg, 0));
3170 int regnoi = REGNO (XEXP (parmreg, 1));
3171 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3172 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3173 GET_MODE_SIZE (submode));
3175 /* Scan backwards for the set of the real and
3176 imaginary parts. */
3177 for (sinsn = linsn; sinsn != 0;
3178 sinsn = prev_nonnote_insn (sinsn))
3180 set = single_set (sinsn);
3181 if (set == 0)
3182 continue;
3184 if (SET_DEST (set) == regno_reg_rtx [regnoi])
3185 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3186 else if (SET_DEST (set) == regno_reg_rtx [regnor])
3187 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3190 else
3191 set_dst_reg_note (linsn, REG_EQUIV, equiv_stack_parm, parmreg);
3194 /* For pointer data type, suggest pointer register. */
3195 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3196 mark_reg_pointer (parmreg,
3197 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3200 /* A subroutine of assign_parms. Allocate stack space to hold the current
3201 parameter. Get it there. Perform all ABI specified conversions. */
3203 static void
3204 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3205 struct assign_parm_data_one *data)
3207 /* Value must be stored in the stack slot STACK_PARM during function
3208 execution. */
3209 bool to_conversion = false;
3211 assign_parm_remove_parallels (data);
3213 if (data->promoted_mode != data->nominal_mode)
3215 /* Conversion is required. */
3216 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3218 emit_move_insn (tempreg, validize_mem (data->entry_parm));
3220 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3221 to_conversion = true;
3223 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3224 TYPE_UNSIGNED (TREE_TYPE (parm)));
3226 if (data->stack_parm)
3228 int offset = subreg_lowpart_offset (data->nominal_mode,
3229 GET_MODE (data->stack_parm));
3230 /* ??? This may need a big-endian conversion on sparc64. */
3231 data->stack_parm
3232 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3233 if (offset && MEM_OFFSET_KNOWN_P (data->stack_parm))
3234 set_mem_offset (data->stack_parm,
3235 MEM_OFFSET (data->stack_parm) + offset);
3239 if (data->entry_parm != data->stack_parm)
3241 rtx src, dest;
3243 if (data->stack_parm == 0)
3245 int align = STACK_SLOT_ALIGNMENT (data->passed_type,
3246 GET_MODE (data->entry_parm),
3247 TYPE_ALIGN (data->passed_type));
3248 data->stack_parm
3249 = assign_stack_local (GET_MODE (data->entry_parm),
3250 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3251 align);
3252 set_mem_attributes (data->stack_parm, parm, 1);
3255 dest = validize_mem (data->stack_parm);
3256 src = validize_mem (data->entry_parm);
3258 if (MEM_P (src))
3260 /* Use a block move to handle potentially misaligned entry_parm. */
3261 if (!to_conversion)
3262 push_to_sequence2 (all->first_conversion_insn,
3263 all->last_conversion_insn);
3264 to_conversion = true;
3266 emit_block_move (dest, src,
3267 GEN_INT (int_size_in_bytes (data->passed_type)),
3268 BLOCK_OP_NORMAL);
3270 else
3271 emit_move_insn (dest, src);
3274 if (to_conversion)
3276 all->first_conversion_insn = get_insns ();
3277 all->last_conversion_insn = get_last_insn ();
3278 end_sequence ();
3281 SET_DECL_RTL (parm, data->stack_parm);
3284 /* A subroutine of assign_parms. If the ABI splits complex arguments, then
3285 undo the frobbing that we did in assign_parms_augmented_arg_list. */
3287 static void
3288 assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3289 vec<tree> fnargs)
3291 tree parm;
3292 tree orig_fnargs = all->orig_fnargs;
3293 unsigned i = 0;
3295 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3297 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3298 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3300 rtx tmp, real, imag;
3301 enum machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3303 real = DECL_RTL (fnargs[i]);
3304 imag = DECL_RTL (fnargs[i + 1]);
3305 if (inner != GET_MODE (real))
3307 real = gen_lowpart_SUBREG (inner, real);
3308 imag = gen_lowpart_SUBREG (inner, imag);
3311 if (TREE_ADDRESSABLE (parm))
3313 rtx rmem, imem;
3314 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3315 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3316 DECL_MODE (parm),
3317 TYPE_ALIGN (TREE_TYPE (parm)));
3319 /* split_complex_arg put the real and imag parts in
3320 pseudos. Move them to memory. */
3321 tmp = assign_stack_local (DECL_MODE (parm), size, align);
3322 set_mem_attributes (tmp, parm, 1);
3323 rmem = adjust_address_nv (tmp, inner, 0);
3324 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3325 push_to_sequence2 (all->first_conversion_insn,
3326 all->last_conversion_insn);
3327 emit_move_insn (rmem, real);
3328 emit_move_insn (imem, imag);
3329 all->first_conversion_insn = get_insns ();
3330 all->last_conversion_insn = get_last_insn ();
3331 end_sequence ();
3333 else
3334 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3335 SET_DECL_RTL (parm, tmp);
3337 real = DECL_INCOMING_RTL (fnargs[i]);
3338 imag = DECL_INCOMING_RTL (fnargs[i + 1]);
3339 if (inner != GET_MODE (real))
3341 real = gen_lowpart_SUBREG (inner, real);
3342 imag = gen_lowpart_SUBREG (inner, imag);
3344 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3345 set_decl_incoming_rtl (parm, tmp, false);
3346 i++;
3351 /* Assign RTL expressions to the function's parameters. This may involve
3352 copying them into registers and using those registers as the DECL_RTL. */
3354 static void
3355 assign_parms (tree fndecl)
3357 struct assign_parm_data_all all;
3358 tree parm;
3359 vec<tree> fnargs;
3360 unsigned i;
3362 crtl->args.internal_arg_pointer
3363 = targetm.calls.internal_arg_pointer ();
3365 assign_parms_initialize_all (&all);
3366 fnargs = assign_parms_augmented_arg_list (&all);
3368 FOR_EACH_VEC_ELT (fnargs, i, parm)
3370 struct assign_parm_data_one data;
3372 /* Extract the type of PARM; adjust it according to ABI. */
3373 assign_parm_find_data_types (&all, parm, &data);
3375 /* Early out for errors and void parameters. */
3376 if (data.passed_mode == VOIDmode)
3378 SET_DECL_RTL (parm, const0_rtx);
3379 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3380 continue;
3383 /* Estimate stack alignment from parameter alignment. */
3384 if (SUPPORTS_STACK_ALIGNMENT)
3386 unsigned int align
3387 = targetm.calls.function_arg_boundary (data.promoted_mode,
3388 data.passed_type);
3389 align = MINIMUM_ALIGNMENT (data.passed_type, data.promoted_mode,
3390 align);
3391 if (TYPE_ALIGN (data.nominal_type) > align)
3392 align = MINIMUM_ALIGNMENT (data.nominal_type,
3393 TYPE_MODE (data.nominal_type),
3394 TYPE_ALIGN (data.nominal_type));
3395 if (crtl->stack_alignment_estimated < align)
3397 gcc_assert (!crtl->stack_realign_processed);
3398 crtl->stack_alignment_estimated = align;
3402 if (cfun->stdarg && !DECL_CHAIN (parm))
3403 assign_parms_setup_varargs (&all, &data, false);
3405 /* Find out where the parameter arrives in this function. */
3406 assign_parm_find_entry_rtl (&all, &data);
3408 /* Find out where stack space for this parameter might be. */
3409 if (assign_parm_is_stack_parm (&all, &data))
3411 assign_parm_find_stack_rtl (parm, &data);
3412 assign_parm_adjust_entry_rtl (&data);
3415 /* Record permanently how this parm was passed. */
3416 if (data.passed_pointer)
3418 rtx incoming_rtl
3419 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.passed_type)),
3420 data.entry_parm);
3421 set_decl_incoming_rtl (parm, incoming_rtl, true);
3423 else
3424 set_decl_incoming_rtl (parm, data.entry_parm, false);
3426 /* Update info on where next arg arrives in registers. */
3427 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3428 data.passed_type, data.named_arg);
3430 assign_parm_adjust_stack_rtl (&data);
3432 if (assign_parm_setup_block_p (&data))
3433 assign_parm_setup_block (&all, parm, &data);
3434 else if (data.passed_pointer || use_register_for_decl (parm))
3435 assign_parm_setup_reg (&all, parm, &data);
3436 else
3437 assign_parm_setup_stack (&all, parm, &data);
3440 if (targetm.calls.split_complex_arg)
3441 assign_parms_unsplit_complex (&all, fnargs);
3443 fnargs.release ();
3445 /* Output all parameter conversion instructions (possibly including calls)
3446 now that all parameters have been copied out of hard registers. */
3447 emit_insn (all.first_conversion_insn);
3449 /* Estimate reload stack alignment from scalar return mode. */
3450 if (SUPPORTS_STACK_ALIGNMENT)
3452 if (DECL_RESULT (fndecl))
3454 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3455 enum machine_mode mode = TYPE_MODE (type);
3457 if (mode != BLKmode
3458 && mode != VOIDmode
3459 && !AGGREGATE_TYPE_P (type))
3461 unsigned int align = GET_MODE_ALIGNMENT (mode);
3462 if (crtl->stack_alignment_estimated < align)
3464 gcc_assert (!crtl->stack_realign_processed);
3465 crtl->stack_alignment_estimated = align;
3471 /* If we are receiving a struct value address as the first argument, set up
3472 the RTL for the function result. As this might require code to convert
3473 the transmitted address to Pmode, we do this here to ensure that possible
3474 preliminary conversions of the address have been emitted already. */
3475 if (all.function_result_decl)
3477 tree result = DECL_RESULT (current_function_decl);
3478 rtx addr = DECL_RTL (all.function_result_decl);
3479 rtx x;
3481 if (DECL_BY_REFERENCE (result))
3483 SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3484 x = addr;
3486 else
3488 SET_DECL_VALUE_EXPR (result,
3489 build1 (INDIRECT_REF, TREE_TYPE (result),
3490 all.function_result_decl));
3491 addr = convert_memory_address (Pmode, addr);
3492 x = gen_rtx_MEM (DECL_MODE (result), addr);
3493 set_mem_attributes (x, result, 1);
3496 DECL_HAS_VALUE_EXPR_P (result) = 1;
3498 SET_DECL_RTL (result, x);
3501 /* We have aligned all the args, so add space for the pretend args. */
3502 crtl->args.pretend_args_size = all.pretend_args_size;
3503 all.stack_args_size.constant += all.extra_pretend_bytes;
3504 crtl->args.size = all.stack_args_size.constant;
3506 /* Adjust function incoming argument size for alignment and
3507 minimum length. */
3509 crtl->args.size = MAX (crtl->args.size, all.reg_parm_stack_space);
3510 crtl->args.size = CEIL_ROUND (crtl->args.size,
3511 PARM_BOUNDARY / BITS_PER_UNIT);
3513 #ifdef ARGS_GROW_DOWNWARD
3514 crtl->args.arg_offset_rtx
3515 = (all.stack_args_size.var == 0 ? GEN_INT (-all.stack_args_size.constant)
3516 : expand_expr (size_diffop (all.stack_args_size.var,
3517 size_int (-all.stack_args_size.constant)),
3518 NULL_RTX, VOIDmode, EXPAND_NORMAL));
3519 #else
3520 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3521 #endif
3523 /* See how many bytes, if any, of its args a function should try to pop
3524 on return. */
3526 crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3527 TREE_TYPE (fndecl),
3528 crtl->args.size);
3530 /* For stdarg.h function, save info about
3531 regs and stack space used by the named args. */
3533 crtl->args.info = all.args_so_far_v;
3535 /* Set the rtx used for the function return value. Put this in its
3536 own variable so any optimizers that need this information don't have
3537 to include tree.h. Do this here so it gets done when an inlined
3538 function gets output. */
3540 crtl->return_rtx
3541 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3542 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3544 /* If scalar return value was computed in a pseudo-reg, or was a named
3545 return value that got dumped to the stack, copy that to the hard
3546 return register. */
3547 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3549 tree decl_result = DECL_RESULT (fndecl);
3550 rtx decl_rtl = DECL_RTL (decl_result);
3552 if (REG_P (decl_rtl)
3553 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3554 : DECL_REGISTER (decl_result))
3556 rtx real_decl_rtl;
3558 real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3559 fndecl, true);
3560 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3561 /* The delay slot scheduler assumes that crtl->return_rtx
3562 holds the hard register containing the return value, not a
3563 temporary pseudo. */
3564 crtl->return_rtx = real_decl_rtl;
3569 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3570 For all seen types, gimplify their sizes. */
3572 static tree
3573 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3575 tree t = *tp;
3577 *walk_subtrees = 0;
3578 if (TYPE_P (t))
3580 if (POINTER_TYPE_P (t))
3581 *walk_subtrees = 1;
3582 else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3583 && !TYPE_SIZES_GIMPLIFIED (t))
3585 gimplify_type_sizes (t, (gimple_seq *) data);
3586 *walk_subtrees = 1;
3590 return NULL;
3593 /* Gimplify the parameter list for current_function_decl. This involves
3594 evaluating SAVE_EXPRs of variable sized parameters and generating code
3595 to implement callee-copies reference parameters. Returns a sequence of
3596 statements to add to the beginning of the function. */
3598 gimple_seq
3599 gimplify_parameters (void)
3601 struct assign_parm_data_all all;
3602 tree parm;
3603 gimple_seq stmts = NULL;
3604 vec<tree> fnargs;
3605 unsigned i;
3607 assign_parms_initialize_all (&all);
3608 fnargs = assign_parms_augmented_arg_list (&all);
3610 FOR_EACH_VEC_ELT (fnargs, i, parm)
3612 struct assign_parm_data_one data;
3614 /* Extract the type of PARM; adjust it according to ABI. */
3615 assign_parm_find_data_types (&all, parm, &data);
3617 /* Early out for errors and void parameters. */
3618 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3619 continue;
3621 /* Update info on where next arg arrives in registers. */
3622 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3623 data.passed_type, data.named_arg);
3625 /* ??? Once upon a time variable_size stuffed parameter list
3626 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3627 turned out to be less than manageable in the gimple world.
3628 Now we have to hunt them down ourselves. */
3629 walk_tree_without_duplicates (&data.passed_type,
3630 gimplify_parm_type, &stmts);
3632 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3634 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3635 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3638 if (data.passed_pointer)
3640 tree type = TREE_TYPE (data.passed_type);
3641 if (reference_callee_copied (&all.args_so_far_v, TYPE_MODE (type),
3642 type, data.named_arg))
3644 tree local, t;
3646 /* For constant-sized objects, this is trivial; for
3647 variable-sized objects, we have to play games. */
3648 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3649 && !(flag_stack_check == GENERIC_STACK_CHECK
3650 && compare_tree_int (DECL_SIZE_UNIT (parm),
3651 STACK_CHECK_MAX_VAR_SIZE) > 0))
3653 local = create_tmp_var (type, get_name (parm));
3654 DECL_IGNORED_P (local) = 0;
3655 /* If PARM was addressable, move that flag over
3656 to the local copy, as its address will be taken,
3657 not the PARMs. Keep the parms address taken
3658 as we'll query that flag during gimplification. */
3659 if (TREE_ADDRESSABLE (parm))
3660 TREE_ADDRESSABLE (local) = 1;
3661 else if (TREE_CODE (type) == COMPLEX_TYPE
3662 || TREE_CODE (type) == VECTOR_TYPE)
3663 DECL_GIMPLE_REG_P (local) = 1;
3665 else
3667 tree ptr_type, addr;
3669 ptr_type = build_pointer_type (type);
3670 addr = create_tmp_reg (ptr_type, get_name (parm));
3671 DECL_IGNORED_P (addr) = 0;
3672 local = build_fold_indirect_ref (addr);
3674 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
3675 t = build_call_expr (t, 2, DECL_SIZE_UNIT (parm),
3676 size_int (DECL_ALIGN (parm)));
3678 /* The call has been built for a variable-sized object. */
3679 CALL_ALLOCA_FOR_VAR_P (t) = 1;
3680 t = fold_convert (ptr_type, t);
3681 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
3682 gimplify_and_add (t, &stmts);
3685 gimplify_assign (local, parm, &stmts);
3687 SET_DECL_VALUE_EXPR (parm, local);
3688 DECL_HAS_VALUE_EXPR_P (parm) = 1;
3693 fnargs.release ();
3695 return stmts;
3698 /* Compute the size and offset from the start of the stacked arguments for a
3699 parm passed in mode PASSED_MODE and with type TYPE.
3701 INITIAL_OFFSET_PTR points to the current offset into the stacked
3702 arguments.
3704 The starting offset and size for this parm are returned in
3705 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
3706 nonzero, the offset is that of stack slot, which is returned in
3707 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
3708 padding required from the initial offset ptr to the stack slot.
3710 IN_REGS is nonzero if the argument will be passed in registers. It will
3711 never be set if REG_PARM_STACK_SPACE is not defined.
3713 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
3714 for arguments which are passed in registers.
3716 FNDECL is the function in which the argument was defined.
3718 There are two types of rounding that are done. The first, controlled by
3719 TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
3720 argument list to be aligned to the specific boundary (in bits). This
3721 rounding affects the initial and starting offsets, but not the argument
3722 size.
3724 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
3725 optionally rounds the size of the parm to PARM_BOUNDARY. The
3726 initial offset is not affected by this rounding, while the size always
3727 is and the starting offset may be. */
3729 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
3730 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
3731 callers pass in the total size of args so far as
3732 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
3734 void
3735 locate_and_pad_parm (enum machine_mode passed_mode, tree type, int in_regs,
3736 int reg_parm_stack_space, int partial,
3737 tree fndecl ATTRIBUTE_UNUSED,
3738 struct args_size *initial_offset_ptr,
3739 struct locate_and_pad_arg_data *locate)
3741 tree sizetree;
3742 enum direction where_pad;
3743 unsigned int boundary, round_boundary;
3744 int part_size_in_regs;
3746 /* If we have found a stack parm before we reach the end of the
3747 area reserved for registers, skip that area. */
3748 if (! in_regs)
3750 if (reg_parm_stack_space > 0)
3752 if (initial_offset_ptr->var)
3754 initial_offset_ptr->var
3755 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
3756 ssize_int (reg_parm_stack_space));
3757 initial_offset_ptr->constant = 0;
3759 else if (initial_offset_ptr->constant < reg_parm_stack_space)
3760 initial_offset_ptr->constant = reg_parm_stack_space;
3764 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
3766 sizetree
3767 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
3768 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
3769 boundary = targetm.calls.function_arg_boundary (passed_mode, type);
3770 round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
3771 type);
3772 locate->where_pad = where_pad;
3774 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
3775 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
3776 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
3778 locate->boundary = boundary;
3780 if (SUPPORTS_STACK_ALIGNMENT)
3782 /* stack_alignment_estimated can't change after stack has been
3783 realigned. */
3784 if (crtl->stack_alignment_estimated < boundary)
3786 if (!crtl->stack_realign_processed)
3787 crtl->stack_alignment_estimated = boundary;
3788 else
3790 /* If stack is realigned and stack alignment value
3791 hasn't been finalized, it is OK not to increase
3792 stack_alignment_estimated. The bigger alignment
3793 requirement is recorded in stack_alignment_needed
3794 below. */
3795 gcc_assert (!crtl->stack_realign_finalized
3796 && crtl->stack_realign_needed);
3801 /* Remember if the outgoing parameter requires extra alignment on the
3802 calling function side. */
3803 if (crtl->stack_alignment_needed < boundary)
3804 crtl->stack_alignment_needed = boundary;
3805 if (crtl->preferred_stack_boundary < boundary)
3806 crtl->preferred_stack_boundary = boundary;
3808 #ifdef ARGS_GROW_DOWNWARD
3809 locate->slot_offset.constant = -initial_offset_ptr->constant;
3810 if (initial_offset_ptr->var)
3811 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
3812 initial_offset_ptr->var);
3815 tree s2 = sizetree;
3816 if (where_pad != none
3817 && (!tree_fits_uhwi_p (sizetree)
3818 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
3819 s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
3820 SUB_PARM_SIZE (locate->slot_offset, s2);
3823 locate->slot_offset.constant += part_size_in_regs;
3825 if (!in_regs || reg_parm_stack_space > 0)
3826 pad_to_arg_alignment (&locate->slot_offset, boundary,
3827 &locate->alignment_pad);
3829 locate->size.constant = (-initial_offset_ptr->constant
3830 - locate->slot_offset.constant);
3831 if (initial_offset_ptr->var)
3832 locate->size.var = size_binop (MINUS_EXPR,
3833 size_binop (MINUS_EXPR,
3834 ssize_int (0),
3835 initial_offset_ptr->var),
3836 locate->slot_offset.var);
3838 /* Pad_below needs the pre-rounded size to know how much to pad
3839 below. */
3840 locate->offset = locate->slot_offset;
3841 if (where_pad == downward)
3842 pad_below (&locate->offset, passed_mode, sizetree);
3844 #else /* !ARGS_GROW_DOWNWARD */
3845 if (!in_regs || reg_parm_stack_space > 0)
3846 pad_to_arg_alignment (initial_offset_ptr, boundary,
3847 &locate->alignment_pad);
3848 locate->slot_offset = *initial_offset_ptr;
3850 #ifdef PUSH_ROUNDING
3851 if (passed_mode != BLKmode)
3852 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
3853 #endif
3855 /* Pad_below needs the pre-rounded size to know how much to pad below
3856 so this must be done before rounding up. */
3857 locate->offset = locate->slot_offset;
3858 if (where_pad == downward)
3859 pad_below (&locate->offset, passed_mode, sizetree);
3861 if (where_pad != none
3862 && (!tree_fits_uhwi_p (sizetree)
3863 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
3864 sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
3866 ADD_PARM_SIZE (locate->size, sizetree);
3868 locate->size.constant -= part_size_in_regs;
3869 #endif /* ARGS_GROW_DOWNWARD */
3871 #ifdef FUNCTION_ARG_OFFSET
3872 locate->offset.constant += FUNCTION_ARG_OFFSET (passed_mode, type);
3873 #endif
3876 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
3877 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
3879 static void
3880 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
3881 struct args_size *alignment_pad)
3883 tree save_var = NULL_TREE;
3884 HOST_WIDE_INT save_constant = 0;
3885 int boundary_in_bytes = boundary / BITS_PER_UNIT;
3886 HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
3888 #ifdef SPARC_STACK_BOUNDARY_HACK
3889 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
3890 the real alignment of %sp. However, when it does this, the
3891 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
3892 if (SPARC_STACK_BOUNDARY_HACK)
3893 sp_offset = 0;
3894 #endif
3896 if (boundary > PARM_BOUNDARY)
3898 save_var = offset_ptr->var;
3899 save_constant = offset_ptr->constant;
3902 alignment_pad->var = NULL_TREE;
3903 alignment_pad->constant = 0;
3905 if (boundary > BITS_PER_UNIT)
3907 if (offset_ptr->var)
3909 tree sp_offset_tree = ssize_int (sp_offset);
3910 tree offset = size_binop (PLUS_EXPR,
3911 ARGS_SIZE_TREE (*offset_ptr),
3912 sp_offset_tree);
3913 #ifdef ARGS_GROW_DOWNWARD
3914 tree rounded = round_down (offset, boundary / BITS_PER_UNIT);
3915 #else
3916 tree rounded = round_up (offset, boundary / BITS_PER_UNIT);
3917 #endif
3919 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
3920 /* ARGS_SIZE_TREE includes constant term. */
3921 offset_ptr->constant = 0;
3922 if (boundary > PARM_BOUNDARY)
3923 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
3924 save_var);
3926 else
3928 offset_ptr->constant = -sp_offset +
3929 #ifdef ARGS_GROW_DOWNWARD
3930 FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3931 #else
3932 CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3933 #endif
3934 if (boundary > PARM_BOUNDARY)
3935 alignment_pad->constant = offset_ptr->constant - save_constant;
3940 static void
3941 pad_below (struct args_size *offset_ptr, enum machine_mode passed_mode, tree sizetree)
3943 if (passed_mode != BLKmode)
3945 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
3946 offset_ptr->constant
3947 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
3948 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
3949 - GET_MODE_SIZE (passed_mode));
3951 else
3953 if (TREE_CODE (sizetree) != INTEGER_CST
3954 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
3956 /* Round the size up to multiple of PARM_BOUNDARY bits. */
3957 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
3958 /* Add it in. */
3959 ADD_PARM_SIZE (*offset_ptr, s2);
3960 SUB_PARM_SIZE (*offset_ptr, sizetree);
3966 /* True if register REGNO was alive at a place where `setjmp' was
3967 called and was set more than once or is an argument. Such regs may
3968 be clobbered by `longjmp'. */
3970 static bool
3971 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
3973 /* There appear to be cases where some local vars never reach the
3974 backend but have bogus regnos. */
3975 if (regno >= max_reg_num ())
3976 return false;
3978 return ((REG_N_SETS (regno) > 1
3979 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3980 regno))
3981 && REGNO_REG_SET_P (setjmp_crosses, regno));
3984 /* Walk the tree of blocks describing the binding levels within a
3985 function and warn about variables the might be killed by setjmp or
3986 vfork. This is done after calling flow_analysis before register
3987 allocation since that will clobber the pseudo-regs to hard
3988 regs. */
3990 static void
3991 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
3993 tree decl, sub;
3995 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
3997 if (TREE_CODE (decl) == VAR_DECL
3998 && DECL_RTL_SET_P (decl)
3999 && REG_P (DECL_RTL (decl))
4000 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4001 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
4002 " %<longjmp%> or %<vfork%>", decl);
4005 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
4006 setjmp_vars_warning (setjmp_crosses, sub);
4009 /* Do the appropriate part of setjmp_vars_warning
4010 but for arguments instead of local variables. */
4012 static void
4013 setjmp_args_warning (bitmap setjmp_crosses)
4015 tree decl;
4016 for (decl = DECL_ARGUMENTS (current_function_decl);
4017 decl; decl = DECL_CHAIN (decl))
4018 if (DECL_RTL (decl) != 0
4019 && REG_P (DECL_RTL (decl))
4020 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4021 warning (OPT_Wclobbered,
4022 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
4023 decl);
4026 /* Generate warning messages for variables live across setjmp. */
4028 void
4029 generate_setjmp_warnings (void)
4031 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4033 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS
4034 || bitmap_empty_p (setjmp_crosses))
4035 return;
4037 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4038 setjmp_args_warning (setjmp_crosses);
4042 /* Reverse the order of elements in the fragment chain T of blocks,
4043 and return the new head of the chain (old last element).
4044 In addition to that clear BLOCK_SAME_RANGE flags when needed
4045 and adjust BLOCK_SUPERCONTEXT from the super fragment to
4046 its super fragment origin. */
4048 static tree
4049 block_fragments_nreverse (tree t)
4051 tree prev = 0, block, next, prev_super = 0;
4052 tree super = BLOCK_SUPERCONTEXT (t);
4053 if (BLOCK_FRAGMENT_ORIGIN (super))
4054 super = BLOCK_FRAGMENT_ORIGIN (super);
4055 for (block = t; block; block = next)
4057 next = BLOCK_FRAGMENT_CHAIN (block);
4058 BLOCK_FRAGMENT_CHAIN (block) = prev;
4059 if ((prev && !BLOCK_SAME_RANGE (prev))
4060 || (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (block))
4061 != prev_super))
4062 BLOCK_SAME_RANGE (block) = 0;
4063 prev_super = BLOCK_SUPERCONTEXT (block);
4064 BLOCK_SUPERCONTEXT (block) = super;
4065 prev = block;
4067 t = BLOCK_FRAGMENT_ORIGIN (t);
4068 if (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (t))
4069 != prev_super)
4070 BLOCK_SAME_RANGE (t) = 0;
4071 BLOCK_SUPERCONTEXT (t) = super;
4072 return prev;
4075 /* Reverse the order of elements in the chain T of blocks,
4076 and return the new head of the chain (old last element).
4077 Also do the same on subblocks and reverse the order of elements
4078 in BLOCK_FRAGMENT_CHAIN as well. */
4080 static tree
4081 blocks_nreverse_all (tree t)
4083 tree prev = 0, block, next;
4084 for (block = t; block; block = next)
4086 next = BLOCK_CHAIN (block);
4087 BLOCK_CHAIN (block) = prev;
4088 if (BLOCK_FRAGMENT_CHAIN (block)
4089 && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4091 BLOCK_FRAGMENT_CHAIN (block)
4092 = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4093 if (!BLOCK_SAME_RANGE (BLOCK_FRAGMENT_CHAIN (block)))
4094 BLOCK_SAME_RANGE (block) = 0;
4096 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4097 prev = block;
4099 return prev;
4103 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4104 and create duplicate blocks. */
4105 /* ??? Need an option to either create block fragments or to create
4106 abstract origin duplicates of a source block. It really depends
4107 on what optimization has been performed. */
4109 void
4110 reorder_blocks (void)
4112 tree block = DECL_INITIAL (current_function_decl);
4114 if (block == NULL_TREE)
4115 return;
4117 auto_vec<tree, 10> block_stack;
4119 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4120 clear_block_marks (block);
4122 /* Prune the old trees away, so that they don't get in the way. */
4123 BLOCK_SUBBLOCKS (block) = NULL_TREE;
4124 BLOCK_CHAIN (block) = NULL_TREE;
4126 /* Recreate the block tree from the note nesting. */
4127 reorder_blocks_1 (get_insns (), block, &block_stack);
4128 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4131 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
4133 void
4134 clear_block_marks (tree block)
4136 while (block)
4138 TREE_ASM_WRITTEN (block) = 0;
4139 clear_block_marks (BLOCK_SUBBLOCKS (block));
4140 block = BLOCK_CHAIN (block);
4144 static void
4145 reorder_blocks_1 (rtx insns, tree current_block, vec<tree> *p_block_stack)
4147 rtx insn;
4148 tree prev_beg = NULL_TREE, prev_end = NULL_TREE;
4150 for (insn = insns; insn; insn = NEXT_INSN (insn))
4152 if (NOTE_P (insn))
4154 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4156 tree block = NOTE_BLOCK (insn);
4157 tree origin;
4159 gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4160 origin = block;
4162 if (prev_end)
4163 BLOCK_SAME_RANGE (prev_end) = 0;
4164 prev_end = NULL_TREE;
4166 /* If we have seen this block before, that means it now
4167 spans multiple address regions. Create a new fragment. */
4168 if (TREE_ASM_WRITTEN (block))
4170 tree new_block = copy_node (block);
4172 BLOCK_SAME_RANGE (new_block) = 0;
4173 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4174 BLOCK_FRAGMENT_CHAIN (new_block)
4175 = BLOCK_FRAGMENT_CHAIN (origin);
4176 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4178 NOTE_BLOCK (insn) = new_block;
4179 block = new_block;
4182 if (prev_beg == current_block && prev_beg)
4183 BLOCK_SAME_RANGE (block) = 1;
4185 prev_beg = origin;
4187 BLOCK_SUBBLOCKS (block) = 0;
4188 TREE_ASM_WRITTEN (block) = 1;
4189 /* When there's only one block for the entire function,
4190 current_block == block and we mustn't do this, it
4191 will cause infinite recursion. */
4192 if (block != current_block)
4194 tree super;
4195 if (block != origin)
4196 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block
4197 || BLOCK_FRAGMENT_ORIGIN (BLOCK_SUPERCONTEXT
4198 (origin))
4199 == current_block);
4200 if (p_block_stack->is_empty ())
4201 super = current_block;
4202 else
4204 super = p_block_stack->last ();
4205 gcc_assert (super == current_block
4206 || BLOCK_FRAGMENT_ORIGIN (super)
4207 == current_block);
4209 BLOCK_SUPERCONTEXT (block) = super;
4210 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4211 BLOCK_SUBBLOCKS (current_block) = block;
4212 current_block = origin;
4214 p_block_stack->safe_push (block);
4216 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4218 NOTE_BLOCK (insn) = p_block_stack->pop ();
4219 current_block = BLOCK_SUPERCONTEXT (current_block);
4220 if (BLOCK_FRAGMENT_ORIGIN (current_block))
4221 current_block = BLOCK_FRAGMENT_ORIGIN (current_block);
4222 prev_beg = NULL_TREE;
4223 prev_end = BLOCK_SAME_RANGE (NOTE_BLOCK (insn))
4224 ? NOTE_BLOCK (insn) : NULL_TREE;
4227 else
4229 prev_beg = NULL_TREE;
4230 if (prev_end)
4231 BLOCK_SAME_RANGE (prev_end) = 0;
4232 prev_end = NULL_TREE;
4237 /* Reverse the order of elements in the chain T of blocks,
4238 and return the new head of the chain (old last element). */
4240 tree
4241 blocks_nreverse (tree t)
4243 tree prev = 0, block, next;
4244 for (block = t; block; block = next)
4246 next = BLOCK_CHAIN (block);
4247 BLOCK_CHAIN (block) = prev;
4248 prev = block;
4250 return prev;
4253 /* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4254 by modifying the last node in chain 1 to point to chain 2. */
4256 tree
4257 block_chainon (tree op1, tree op2)
4259 tree t1;
4261 if (!op1)
4262 return op2;
4263 if (!op2)
4264 return op1;
4266 for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4267 continue;
4268 BLOCK_CHAIN (t1) = op2;
4270 #ifdef ENABLE_TREE_CHECKING
4272 tree t2;
4273 for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4274 gcc_assert (t2 != t1);
4276 #endif
4278 return op1;
4281 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
4282 non-NULL, list them all into VECTOR, in a depth-first preorder
4283 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
4284 blocks. */
4286 static int
4287 all_blocks (tree block, tree *vector)
4289 int n_blocks = 0;
4291 while (block)
4293 TREE_ASM_WRITTEN (block) = 0;
4295 /* Record this block. */
4296 if (vector)
4297 vector[n_blocks] = block;
4299 ++n_blocks;
4301 /* Record the subblocks, and their subblocks... */
4302 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4303 vector ? vector + n_blocks : 0);
4304 block = BLOCK_CHAIN (block);
4307 return n_blocks;
4310 /* Return a vector containing all the blocks rooted at BLOCK. The
4311 number of elements in the vector is stored in N_BLOCKS_P. The
4312 vector is dynamically allocated; it is the caller's responsibility
4313 to call `free' on the pointer returned. */
4315 static tree *
4316 get_block_vector (tree block, int *n_blocks_p)
4318 tree *block_vector;
4320 *n_blocks_p = all_blocks (block, NULL);
4321 block_vector = XNEWVEC (tree, *n_blocks_p);
4322 all_blocks (block, block_vector);
4324 return block_vector;
4327 static GTY(()) int next_block_index = 2;
4329 /* Set BLOCK_NUMBER for all the blocks in FN. */
4331 void
4332 number_blocks (tree fn)
4334 int i;
4335 int n_blocks;
4336 tree *block_vector;
4338 /* For SDB and XCOFF debugging output, we start numbering the blocks
4339 from 1 within each function, rather than keeping a running
4340 count. */
4341 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
4342 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
4343 next_block_index = 1;
4344 #endif
4346 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4348 /* The top-level BLOCK isn't numbered at all. */
4349 for (i = 1; i < n_blocks; ++i)
4350 /* We number the blocks from two. */
4351 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4353 free (block_vector);
4355 return;
4358 /* If VAR is present in a subblock of BLOCK, return the subblock. */
4360 DEBUG_FUNCTION tree
4361 debug_find_var_in_block_tree (tree var, tree block)
4363 tree t;
4365 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4366 if (t == var)
4367 return block;
4369 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4371 tree ret = debug_find_var_in_block_tree (var, t);
4372 if (ret)
4373 return ret;
4376 return NULL_TREE;
4379 /* Keep track of whether we're in a dummy function context. If we are,
4380 we don't want to invoke the set_current_function hook, because we'll
4381 get into trouble if the hook calls target_reinit () recursively or
4382 when the initial initialization is not yet complete. */
4384 static bool in_dummy_function;
4386 /* Invoke the target hook when setting cfun. Update the optimization options
4387 if the function uses different options than the default. */
4389 static void
4390 invoke_set_current_function_hook (tree fndecl)
4392 if (!in_dummy_function)
4394 tree opts = ((fndecl)
4395 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4396 : optimization_default_node);
4398 if (!opts)
4399 opts = optimization_default_node;
4401 /* Change optimization options if needed. */
4402 if (optimization_current_node != opts)
4404 optimization_current_node = opts;
4405 cl_optimization_restore (&global_options, TREE_OPTIMIZATION (opts));
4408 targetm.set_current_function (fndecl);
4409 this_fn_optabs = this_target_optabs;
4411 if (opts != optimization_default_node)
4413 init_tree_optimization_optabs (opts);
4414 if (TREE_OPTIMIZATION_OPTABS (opts))
4415 this_fn_optabs = (struct target_optabs *)
4416 TREE_OPTIMIZATION_OPTABS (opts);
4421 /* cfun should never be set directly; use this function. */
4423 void
4424 set_cfun (struct function *new_cfun)
4426 if (cfun != new_cfun)
4428 cfun = new_cfun;
4429 invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4433 /* Initialized with NOGC, making this poisonous to the garbage collector. */
4435 static vec<function_p> cfun_stack;
4437 /* Push the current cfun onto the stack, and set cfun to new_cfun. Also set
4438 current_function_decl accordingly. */
4440 void
4441 push_cfun (struct function *new_cfun)
4443 gcc_assert ((!cfun && !current_function_decl)
4444 || (cfun && current_function_decl == cfun->decl));
4445 cfun_stack.safe_push (cfun);
4446 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4447 set_cfun (new_cfun);
4450 /* Pop cfun from the stack. Also set current_function_decl accordingly. */
4452 void
4453 pop_cfun (void)
4455 struct function *new_cfun = cfun_stack.pop ();
4456 /* When in_dummy_function, we do have a cfun but current_function_decl is
4457 NULL. We also allow pushing NULL cfun and subsequently changing
4458 current_function_decl to something else and have both restored by
4459 pop_cfun. */
4460 gcc_checking_assert (in_dummy_function
4461 || !cfun
4462 || current_function_decl == cfun->decl);
4463 set_cfun (new_cfun);
4464 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4467 /* Return value of funcdef and increase it. */
4469 get_next_funcdef_no (void)
4471 return funcdef_no++;
4474 /* Return value of funcdef. */
4476 get_last_funcdef_no (void)
4478 return funcdef_no;
4481 /* Allocate a function structure for FNDECL and set its contents
4482 to the defaults. Set cfun to the newly-allocated object.
4483 Some of the helper functions invoked during initialization assume
4484 that cfun has already been set. Therefore, assign the new object
4485 directly into cfun and invoke the back end hook explicitly at the
4486 very end, rather than initializing a temporary and calling set_cfun
4487 on it.
4489 ABSTRACT_P is true if this is a function that will never be seen by
4490 the middle-end. Such functions are front-end concepts (like C++
4491 function templates) that do not correspond directly to functions
4492 placed in object files. */
4494 void
4495 allocate_struct_function (tree fndecl, bool abstract_p)
4497 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4499 cfun = ggc_alloc_cleared_function ();
4501 init_eh_for_function ();
4503 if (init_machine_status)
4504 cfun->machine = (*init_machine_status) ();
4506 #ifdef OVERRIDE_ABI_FORMAT
4507 OVERRIDE_ABI_FORMAT (fndecl);
4508 #endif
4510 if (fndecl != NULL_TREE)
4512 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4513 cfun->decl = fndecl;
4514 current_function_funcdef_no = get_next_funcdef_no ();
4517 invoke_set_current_function_hook (fndecl);
4519 if (fndecl != NULL_TREE)
4521 tree result = DECL_RESULT (fndecl);
4522 if (!abstract_p && aggregate_value_p (result, fndecl))
4524 #ifdef PCC_STATIC_STRUCT_RETURN
4525 cfun->returns_pcc_struct = 1;
4526 #endif
4527 cfun->returns_struct = 1;
4530 cfun->stdarg = stdarg_p (fntype);
4532 /* Assume all registers in stdarg functions need to be saved. */
4533 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4534 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4536 /* ??? This could be set on a per-function basis by the front-end
4537 but is this worth the hassle? */
4538 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4542 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4543 instead of just setting it. */
4545 void
4546 push_struct_function (tree fndecl)
4548 /* When in_dummy_function we might be in the middle of a pop_cfun and
4549 current_function_decl and cfun may not match. */
4550 gcc_assert (in_dummy_function
4551 || (!cfun && !current_function_decl)
4552 || (cfun && current_function_decl == cfun->decl));
4553 cfun_stack.safe_push (cfun);
4554 current_function_decl = fndecl;
4555 allocate_struct_function (fndecl, false);
4558 /* Reset crtl and other non-struct-function variables to defaults as
4559 appropriate for emitting rtl at the start of a function. */
4561 static void
4562 prepare_function_start (void)
4564 gcc_assert (!crtl->emit.x_last_insn);
4565 init_temp_slots ();
4566 init_emit ();
4567 init_varasm_status ();
4568 init_expr ();
4569 default_rtl_profile ();
4571 if (flag_stack_usage_info)
4573 cfun->su = ggc_alloc_cleared_stack_usage ();
4574 cfun->su->static_stack_size = -1;
4577 cse_not_expected = ! optimize;
4579 /* Caller save not needed yet. */
4580 caller_save_needed = 0;
4582 /* We haven't done register allocation yet. */
4583 reg_renumber = 0;
4585 /* Indicate that we have not instantiated virtual registers yet. */
4586 virtuals_instantiated = 0;
4588 /* Indicate that we want CONCATs now. */
4589 generating_concat_p = 1;
4591 /* Indicate we have no need of a frame pointer yet. */
4592 frame_pointer_needed = 0;
4595 /* Initialize the rtl expansion mechanism so that we can do simple things
4596 like generate sequences. This is used to provide a context during global
4597 initialization of some passes. You must call expand_dummy_function_end
4598 to exit this context. */
4600 void
4601 init_dummy_function_start (void)
4603 gcc_assert (!in_dummy_function);
4604 in_dummy_function = true;
4605 push_struct_function (NULL_TREE);
4606 prepare_function_start ();
4609 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4610 and initialize static variables for generating RTL for the statements
4611 of the function. */
4613 void
4614 init_function_start (tree subr)
4616 if (subr && DECL_STRUCT_FUNCTION (subr))
4617 set_cfun (DECL_STRUCT_FUNCTION (subr));
4618 else
4619 allocate_struct_function (subr, false);
4620 prepare_function_start ();
4621 decide_function_section (subr);
4623 /* Warn if this value is an aggregate type,
4624 regardless of which calling convention we are using for it. */
4625 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4626 warning (OPT_Waggregate_return, "function returns an aggregate");
4629 /* Expand code to verify the stack_protect_guard. This is invoked at
4630 the end of a function to be protected. */
4632 #ifndef HAVE_stack_protect_test
4633 # define HAVE_stack_protect_test 0
4634 # define gen_stack_protect_test(x, y, z) (gcc_unreachable (), NULL_RTX)
4635 #endif
4637 void
4638 stack_protect_epilogue (void)
4640 tree guard_decl = targetm.stack_protect_guard ();
4641 rtx label = gen_label_rtx ();
4642 rtx x, y, tmp;
4644 x = expand_normal (crtl->stack_protect_guard);
4645 y = expand_normal (guard_decl);
4647 /* Allow the target to compare Y with X without leaking either into
4648 a register. */
4649 switch (HAVE_stack_protect_test != 0)
4651 case 1:
4652 tmp = gen_stack_protect_test (x, y, label);
4653 if (tmp)
4655 emit_insn (tmp);
4656 break;
4658 /* FALLTHRU */
4660 default:
4661 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
4662 break;
4665 /* The noreturn predictor has been moved to the tree level. The rtl-level
4666 predictors estimate this branch about 20%, which isn't enough to get
4667 things moved out of line. Since this is the only extant case of adding
4668 a noreturn function at the rtl level, it doesn't seem worth doing ought
4669 except adding the prediction by hand. */
4670 tmp = get_last_insn ();
4671 if (JUMP_P (tmp))
4672 predict_insn_def (tmp, PRED_NORETURN, TAKEN);
4674 expand_call (targetm.stack_protect_fail (), NULL_RTX, /*ignore=*/true);
4675 free_temp_slots ();
4676 emit_label (label);
4679 /* Start the RTL for a new function, and set variables used for
4680 emitting RTL.
4681 SUBR is the FUNCTION_DECL node.
4682 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
4683 the function's parameters, which must be run at any return statement. */
4685 void
4686 expand_function_start (tree subr)
4688 /* Make sure volatile mem refs aren't considered
4689 valid operands of arithmetic insns. */
4690 init_recog_no_volatile ();
4692 crtl->profile
4693 = (profile_flag
4694 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
4696 crtl->limit_stack
4697 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
4699 /* Make the label for return statements to jump to. Do not special
4700 case machines with special return instructions -- they will be
4701 handled later during jump, ifcvt, or epilogue creation. */
4702 return_label = gen_label_rtx ();
4704 /* Initialize rtx used to return the value. */
4705 /* Do this before assign_parms so that we copy the struct value address
4706 before any library calls that assign parms might generate. */
4708 /* Decide whether to return the value in memory or in a register. */
4709 if (aggregate_value_p (DECL_RESULT (subr), subr))
4711 /* Returning something that won't go in a register. */
4712 rtx value_address = 0;
4714 #ifdef PCC_STATIC_STRUCT_RETURN
4715 if (cfun->returns_pcc_struct)
4717 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
4718 value_address = assemble_static_space (size);
4720 else
4721 #endif
4723 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
4724 /* Expect to be passed the address of a place to store the value.
4725 If it is passed as an argument, assign_parms will take care of
4726 it. */
4727 if (sv)
4729 value_address = gen_reg_rtx (Pmode);
4730 emit_move_insn (value_address, sv);
4733 if (value_address)
4735 rtx x = value_address;
4736 if (!DECL_BY_REFERENCE (DECL_RESULT (subr)))
4738 x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), x);
4739 set_mem_attributes (x, DECL_RESULT (subr), 1);
4741 SET_DECL_RTL (DECL_RESULT (subr), x);
4744 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
4745 /* If return mode is void, this decl rtl should not be used. */
4746 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
4747 else
4749 /* Compute the return values into a pseudo reg, which we will copy
4750 into the true return register after the cleanups are done. */
4751 tree return_type = TREE_TYPE (DECL_RESULT (subr));
4752 if (TYPE_MODE (return_type) != BLKmode
4753 && targetm.calls.return_in_msb (return_type))
4754 /* expand_function_end will insert the appropriate padding in
4755 this case. Use the return value's natural (unpadded) mode
4756 within the function proper. */
4757 SET_DECL_RTL (DECL_RESULT (subr),
4758 gen_reg_rtx (TYPE_MODE (return_type)));
4759 else
4761 /* In order to figure out what mode to use for the pseudo, we
4762 figure out what the mode of the eventual return register will
4763 actually be, and use that. */
4764 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
4766 /* Structures that are returned in registers are not
4767 aggregate_value_p, so we may see a PARALLEL or a REG. */
4768 if (REG_P (hard_reg))
4769 SET_DECL_RTL (DECL_RESULT (subr),
4770 gen_reg_rtx (GET_MODE (hard_reg)));
4771 else
4773 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
4774 SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
4778 /* Set DECL_REGISTER flag so that expand_function_end will copy the
4779 result to the real return register(s). */
4780 DECL_REGISTER (DECL_RESULT (subr)) = 1;
4783 /* Initialize rtx for parameters and local variables.
4784 In some cases this requires emitting insns. */
4785 assign_parms (subr);
4787 /* If function gets a static chain arg, store it. */
4788 if (cfun->static_chain_decl)
4790 tree parm = cfun->static_chain_decl;
4791 rtx local, chain, insn;
4793 local = gen_reg_rtx (Pmode);
4794 chain = targetm.calls.static_chain (current_function_decl, true);
4796 set_decl_incoming_rtl (parm, chain, false);
4797 SET_DECL_RTL (parm, local);
4798 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4800 insn = emit_move_insn (local, chain);
4802 /* Mark the register as eliminable, similar to parameters. */
4803 if (MEM_P (chain)
4804 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
4805 set_dst_reg_note (insn, REG_EQUIV, chain, local);
4808 /* If the function receives a non-local goto, then store the
4809 bits we need to restore the frame pointer. */
4810 if (cfun->nonlocal_goto_save_area)
4812 tree t_save;
4813 rtx r_save;
4815 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
4816 gcc_assert (DECL_RTL_SET_P (var));
4818 t_save = build4 (ARRAY_REF,
4819 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
4820 cfun->nonlocal_goto_save_area,
4821 integer_zero_node, NULL_TREE, NULL_TREE);
4822 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
4823 gcc_assert (GET_MODE (r_save) == Pmode);
4825 emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
4826 update_nonlocal_goto_save_area ();
4829 /* The following was moved from init_function_start.
4830 The move is supposed to make sdb output more accurate. */
4831 /* Indicate the beginning of the function body,
4832 as opposed to parm setup. */
4833 emit_note (NOTE_INSN_FUNCTION_BEG);
4835 gcc_assert (NOTE_P (get_last_insn ()));
4837 parm_birth_insn = get_last_insn ();
4839 if (crtl->profile)
4841 #ifdef PROFILE_HOOK
4842 PROFILE_HOOK (current_function_funcdef_no);
4843 #endif
4846 /* If we are doing generic stack checking, the probe should go here. */
4847 if (flag_stack_check == GENERIC_STACK_CHECK)
4848 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
4851 /* Undo the effects of init_dummy_function_start. */
4852 void
4853 expand_dummy_function_end (void)
4855 gcc_assert (in_dummy_function);
4857 /* End any sequences that failed to be closed due to syntax errors. */
4858 while (in_sequence_p ())
4859 end_sequence ();
4861 /* Outside function body, can't compute type's actual size
4862 until next function's body starts. */
4864 free_after_parsing (cfun);
4865 free_after_compilation (cfun);
4866 pop_cfun ();
4867 in_dummy_function = false;
4870 /* Call DOIT for each hard register used as a return value from
4871 the current function. */
4873 void
4874 diddle_return_value (void (*doit) (rtx, void *), void *arg)
4876 rtx outgoing = crtl->return_rtx;
4878 if (! outgoing)
4879 return;
4881 if (REG_P (outgoing))
4882 (*doit) (outgoing, arg);
4883 else if (GET_CODE (outgoing) == PARALLEL)
4885 int i;
4887 for (i = 0; i < XVECLEN (outgoing, 0); i++)
4889 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
4891 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
4892 (*doit) (x, arg);
4897 static void
4898 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4900 emit_clobber (reg);
4903 void
4904 clobber_return_register (void)
4906 diddle_return_value (do_clobber_return_reg, NULL);
4908 /* In case we do use pseudo to return value, clobber it too. */
4909 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
4911 tree decl_result = DECL_RESULT (current_function_decl);
4912 rtx decl_rtl = DECL_RTL (decl_result);
4913 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
4915 do_clobber_return_reg (decl_rtl, NULL);
4920 static void
4921 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4923 emit_use (reg);
4926 static void
4927 use_return_register (void)
4929 diddle_return_value (do_use_return_reg, NULL);
4932 /* Possibly warn about unused parameters. */
4933 void
4934 do_warn_unused_parameter (tree fn)
4936 tree decl;
4938 for (decl = DECL_ARGUMENTS (fn);
4939 decl; decl = DECL_CHAIN (decl))
4940 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
4941 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
4942 && !TREE_NO_WARNING (decl))
4943 warning (OPT_Wunused_parameter, "unused parameter %q+D", decl);
4946 /* Set the location of the insn chain starting at INSN to LOC. */
4948 static void
4949 set_insn_locations (rtx insn, int loc)
4951 while (insn != NULL_RTX)
4953 if (INSN_P (insn))
4954 INSN_LOCATION (insn) = loc;
4955 insn = NEXT_INSN (insn);
4959 /* Generate RTL for the end of the current function. */
4961 void
4962 expand_function_end (void)
4964 rtx clobber_after;
4966 /* If arg_pointer_save_area was referenced only from a nested
4967 function, we will not have initialized it yet. Do that now. */
4968 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
4969 get_arg_pointer_save_area ();
4971 /* If we are doing generic stack checking and this function makes calls,
4972 do a stack probe at the start of the function to ensure we have enough
4973 space for another stack frame. */
4974 if (flag_stack_check == GENERIC_STACK_CHECK)
4976 rtx insn, seq;
4978 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4979 if (CALL_P (insn))
4981 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
4982 start_sequence ();
4983 if (STACK_CHECK_MOVING_SP)
4984 anti_adjust_stack_and_probe (max_frame_size, true);
4985 else
4986 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
4987 seq = get_insns ();
4988 end_sequence ();
4989 set_insn_locations (seq, prologue_location);
4990 emit_insn_before (seq, stack_check_probe_note);
4991 break;
4995 /* End any sequences that failed to be closed due to syntax errors. */
4996 while (in_sequence_p ())
4997 end_sequence ();
4999 clear_pending_stack_adjust ();
5000 do_pending_stack_adjust ();
5002 /* Output a linenumber for the end of the function.
5003 SDB depends on this. */
5004 set_curr_insn_location (input_location);
5006 /* Before the return label (if any), clobber the return
5007 registers so that they are not propagated live to the rest of
5008 the function. This can only happen with functions that drop
5009 through; if there had been a return statement, there would
5010 have either been a return rtx, or a jump to the return label.
5012 We delay actual code generation after the current_function_value_rtx
5013 is computed. */
5014 clobber_after = get_last_insn ();
5016 /* Output the label for the actual return from the function. */
5017 emit_label (return_label);
5019 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
5021 /* Let except.c know where it should emit the call to unregister
5022 the function context for sjlj exceptions. */
5023 if (flag_exceptions)
5024 sjlj_emit_function_exit_after (get_last_insn ());
5026 else
5028 /* We want to ensure that instructions that may trap are not
5029 moved into the epilogue by scheduling, because we don't
5030 always emit unwind information for the epilogue. */
5031 if (cfun->can_throw_non_call_exceptions)
5032 emit_insn (gen_blockage ());
5035 /* If this is an implementation of throw, do what's necessary to
5036 communicate between __builtin_eh_return and the epilogue. */
5037 expand_eh_return ();
5039 /* If scalar return value was computed in a pseudo-reg, or was a named
5040 return value that got dumped to the stack, copy that to the hard
5041 return register. */
5042 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5044 tree decl_result = DECL_RESULT (current_function_decl);
5045 rtx decl_rtl = DECL_RTL (decl_result);
5047 if (REG_P (decl_rtl)
5048 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5049 : DECL_REGISTER (decl_result))
5051 rtx real_decl_rtl = crtl->return_rtx;
5053 /* This should be set in assign_parms. */
5054 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5056 /* If this is a BLKmode structure being returned in registers,
5057 then use the mode computed in expand_return. Note that if
5058 decl_rtl is memory, then its mode may have been changed,
5059 but that crtl->return_rtx has not. */
5060 if (GET_MODE (real_decl_rtl) == BLKmode)
5061 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
5063 /* If a non-BLKmode return value should be padded at the least
5064 significant end of the register, shift it left by the appropriate
5065 amount. BLKmode results are handled using the group load/store
5066 machinery. */
5067 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5068 && REG_P (real_decl_rtl)
5069 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5071 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5072 REGNO (real_decl_rtl)),
5073 decl_rtl);
5074 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5076 /* If a named return value dumped decl_return to memory, then
5077 we may need to re-do the PROMOTE_MODE signed/unsigned
5078 extension. */
5079 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5081 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5082 promote_function_mode (TREE_TYPE (decl_result),
5083 GET_MODE (decl_rtl), &unsignedp,
5084 TREE_TYPE (current_function_decl), 1);
5086 convert_move (real_decl_rtl, decl_rtl, unsignedp);
5088 else if (GET_CODE (real_decl_rtl) == PARALLEL)
5090 /* If expand_function_start has created a PARALLEL for decl_rtl,
5091 move the result to the real return registers. Otherwise, do
5092 a group load from decl_rtl for a named return. */
5093 if (GET_CODE (decl_rtl) == PARALLEL)
5094 emit_group_move (real_decl_rtl, decl_rtl);
5095 else
5096 emit_group_load (real_decl_rtl, decl_rtl,
5097 TREE_TYPE (decl_result),
5098 int_size_in_bytes (TREE_TYPE (decl_result)));
5100 /* In the case of complex integer modes smaller than a word, we'll
5101 need to generate some non-trivial bitfield insertions. Do that
5102 on a pseudo and not the hard register. */
5103 else if (GET_CODE (decl_rtl) == CONCAT
5104 && GET_MODE_CLASS (GET_MODE (decl_rtl)) == MODE_COMPLEX_INT
5105 && GET_MODE_BITSIZE (GET_MODE (decl_rtl)) <= BITS_PER_WORD)
5107 int old_generating_concat_p;
5108 rtx tmp;
5110 old_generating_concat_p = generating_concat_p;
5111 generating_concat_p = 0;
5112 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5113 generating_concat_p = old_generating_concat_p;
5115 emit_move_insn (tmp, decl_rtl);
5116 emit_move_insn (real_decl_rtl, tmp);
5118 else
5119 emit_move_insn (real_decl_rtl, decl_rtl);
5123 /* If returning a structure, arrange to return the address of the value
5124 in a place where debuggers expect to find it.
5126 If returning a structure PCC style,
5127 the caller also depends on this value.
5128 And cfun->returns_pcc_struct is not necessarily set. */
5129 if (cfun->returns_struct
5130 || cfun->returns_pcc_struct)
5132 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5133 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5134 rtx outgoing;
5136 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5137 type = TREE_TYPE (type);
5138 else
5139 value_address = XEXP (value_address, 0);
5141 outgoing = targetm.calls.function_value (build_pointer_type (type),
5142 current_function_decl, true);
5144 /* Mark this as a function return value so integrate will delete the
5145 assignment and USE below when inlining this function. */
5146 REG_FUNCTION_VALUE_P (outgoing) = 1;
5148 /* The address may be ptr_mode and OUTGOING may be Pmode. */
5149 value_address = convert_memory_address (GET_MODE (outgoing),
5150 value_address);
5152 emit_move_insn (outgoing, value_address);
5154 /* Show return register used to hold result (in this case the address
5155 of the result. */
5156 crtl->return_rtx = outgoing;
5159 /* Emit the actual code to clobber return register. Don't emit
5160 it if clobber_after is a barrier, then the previous basic block
5161 certainly doesn't fall thru into the exit block. */
5162 if (!BARRIER_P (clobber_after))
5164 rtx seq;
5166 start_sequence ();
5167 clobber_return_register ();
5168 seq = get_insns ();
5169 end_sequence ();
5171 emit_insn_after (seq, clobber_after);
5174 /* Output the label for the naked return from the function. */
5175 if (naked_return_label)
5176 emit_label (naked_return_label);
5178 /* @@@ This is a kludge. We want to ensure that instructions that
5179 may trap are not moved into the epilogue by scheduling, because
5180 we don't always emit unwind information for the epilogue. */
5181 if (cfun->can_throw_non_call_exceptions
5182 && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5183 emit_insn (gen_blockage ());
5185 /* If stack protection is enabled for this function, check the guard. */
5186 if (crtl->stack_protect_guard)
5187 stack_protect_epilogue ();
5189 /* If we had calls to alloca, and this machine needs
5190 an accurate stack pointer to exit the function,
5191 insert some code to save and restore the stack pointer. */
5192 if (! EXIT_IGNORE_STACK
5193 && cfun->calls_alloca)
5195 rtx tem = 0, seq;
5197 start_sequence ();
5198 emit_stack_save (SAVE_FUNCTION, &tem);
5199 seq = get_insns ();
5200 end_sequence ();
5201 emit_insn_before (seq, parm_birth_insn);
5203 emit_stack_restore (SAVE_FUNCTION, tem);
5206 /* ??? This should no longer be necessary since stupid is no longer with
5207 us, but there are some parts of the compiler (eg reload_combine, and
5208 sh mach_dep_reorg) that still try and compute their own lifetime info
5209 instead of using the general framework. */
5210 use_return_register ();
5214 get_arg_pointer_save_area (void)
5216 rtx ret = arg_pointer_save_area;
5218 if (! ret)
5220 ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5221 arg_pointer_save_area = ret;
5224 if (! crtl->arg_pointer_save_area_init)
5226 rtx seq;
5228 /* Save the arg pointer at the beginning of the function. The
5229 generated stack slot may not be a valid memory address, so we
5230 have to check it and fix it if necessary. */
5231 start_sequence ();
5232 emit_move_insn (validize_mem (ret),
5233 crtl->args.internal_arg_pointer);
5234 seq = get_insns ();
5235 end_sequence ();
5237 push_topmost_sequence ();
5238 emit_insn_after (seq, entry_of_function ());
5239 pop_topmost_sequence ();
5241 crtl->arg_pointer_save_area_init = true;
5244 return ret;
5247 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5248 for the first time. */
5250 static void
5251 record_insns (rtx insns, rtx end, htab_t *hashp)
5253 rtx tmp;
5254 htab_t hash = *hashp;
5256 if (hash == NULL)
5257 *hashp = hash
5258 = htab_create_ggc (17, htab_hash_pointer, htab_eq_pointer, NULL);
5260 for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5262 void **slot = htab_find_slot (hash, tmp, INSERT);
5263 gcc_assert (*slot == NULL);
5264 *slot = tmp;
5268 /* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5269 basic block, splitting or peepholes. If INSN is a prologue or epilogue
5270 insn, then record COPY as well. */
5272 void
5273 maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5275 htab_t hash;
5276 void **slot;
5278 hash = epilogue_insn_hash;
5279 if (!hash || !htab_find (hash, insn))
5281 hash = prologue_insn_hash;
5282 if (!hash || !htab_find (hash, insn))
5283 return;
5286 slot = htab_find_slot (hash, copy, INSERT);
5287 gcc_assert (*slot == NULL);
5288 *slot = copy;
5291 /* Determine if any INSNs in HASH are, or are part of, INSN. Because
5292 we can be running after reorg, SEQUENCE rtl is possible. */
5294 static bool
5295 contains (const_rtx insn, htab_t hash)
5297 if (hash == NULL)
5298 return false;
5300 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5302 int i;
5303 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
5304 if (htab_find (hash, XVECEXP (PATTERN (insn), 0, i)))
5305 return true;
5306 return false;
5309 return htab_find (hash, insn) != NULL;
5313 prologue_epilogue_contains (const_rtx insn)
5315 if (contains (insn, prologue_insn_hash))
5316 return 1;
5317 if (contains (insn, epilogue_insn_hash))
5318 return 1;
5319 return 0;
5322 #ifdef HAVE_simple_return
5324 /* Return true if INSN requires the stack frame to be set up.
5325 PROLOGUE_USED contains the hard registers used in the function
5326 prologue. SET_UP_BY_PROLOGUE is the set of registers we expect the
5327 prologue to set up for the function. */
5328 bool
5329 requires_stack_frame_p (rtx insn, HARD_REG_SET prologue_used,
5330 HARD_REG_SET set_up_by_prologue)
5332 df_ref *df_rec;
5333 HARD_REG_SET hardregs;
5334 unsigned regno;
5336 if (CALL_P (insn))
5337 return !SIBLING_CALL_P (insn);
5339 /* We need a frame to get the unique CFA expected by the unwinder. */
5340 if (cfun->can_throw_non_call_exceptions && can_throw_internal (insn))
5341 return true;
5343 CLEAR_HARD_REG_SET (hardregs);
5344 for (df_rec = DF_INSN_DEFS (insn); *df_rec; df_rec++)
5346 rtx dreg = DF_REF_REG (*df_rec);
5348 if (!REG_P (dreg))
5349 continue;
5351 add_to_hard_reg_set (&hardregs, GET_MODE (dreg),
5352 REGNO (dreg));
5354 if (hard_reg_set_intersect_p (hardregs, prologue_used))
5355 return true;
5356 AND_COMPL_HARD_REG_SET (hardregs, call_used_reg_set);
5357 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5358 if (TEST_HARD_REG_BIT (hardregs, regno)
5359 && df_regs_ever_live_p (regno))
5360 return true;
5362 for (df_rec = DF_INSN_USES (insn); *df_rec; df_rec++)
5364 rtx reg = DF_REF_REG (*df_rec);
5366 if (!REG_P (reg))
5367 continue;
5369 add_to_hard_reg_set (&hardregs, GET_MODE (reg),
5370 REGNO (reg));
5372 if (hard_reg_set_intersect_p (hardregs, set_up_by_prologue))
5373 return true;
5375 return false;
5378 /* See whether BB has a single successor that uses [REGNO, END_REGNO),
5379 and if BB is its only predecessor. Return that block if so,
5380 otherwise return null. */
5382 static basic_block
5383 next_block_for_reg (basic_block bb, int regno, int end_regno)
5385 edge e, live_edge;
5386 edge_iterator ei;
5387 bitmap live;
5388 int i;
5390 live_edge = NULL;
5391 FOR_EACH_EDGE (e, ei, bb->succs)
5393 live = df_get_live_in (e->dest);
5394 for (i = regno; i < end_regno; i++)
5395 if (REGNO_REG_SET_P (live, i))
5397 if (live_edge && live_edge != e)
5398 return NULL;
5399 live_edge = e;
5403 /* We can sometimes encounter dead code. Don't try to move it
5404 into the exit block. */
5405 if (!live_edge || live_edge->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
5406 return NULL;
5408 /* Reject targets of abnormal edges. This is needed for correctness
5409 on ports like Alpha and MIPS, whose pic_offset_table_rtx can die on
5410 exception edges even though it is generally treated as call-saved
5411 for the majority of the compilation. Moving across abnormal edges
5412 isn't going to be interesting for shrink-wrap usage anyway. */
5413 if (live_edge->flags & EDGE_ABNORMAL)
5414 return NULL;
5416 if (EDGE_COUNT (live_edge->dest->preds) > 1)
5417 return NULL;
5419 return live_edge->dest;
5422 /* Try to move INSN from BB to a successor. Return true on success.
5423 USES and DEFS are the set of registers that are used and defined
5424 after INSN in BB. */
5426 static bool
5427 move_insn_for_shrink_wrap (basic_block bb, rtx insn,
5428 const HARD_REG_SET uses,
5429 const HARD_REG_SET defs)
5431 rtx set, src, dest;
5432 bitmap live_out, live_in, bb_uses, bb_defs;
5433 unsigned int i, dregno, end_dregno, sregno, end_sregno;
5434 basic_block next_block;
5436 /* Look for a simple register copy. */
5437 set = single_set (insn);
5438 if (!set)
5439 return false;
5440 src = SET_SRC (set);
5441 dest = SET_DEST (set);
5442 if (!REG_P (dest) || !REG_P (src))
5443 return false;
5445 /* Make sure that the source register isn't defined later in BB. */
5446 sregno = REGNO (src);
5447 end_sregno = END_REGNO (src);
5448 if (overlaps_hard_reg_set_p (defs, GET_MODE (src), sregno))
5449 return false;
5451 /* Make sure that the destination register isn't referenced later in BB. */
5452 dregno = REGNO (dest);
5453 end_dregno = END_REGNO (dest);
5454 if (overlaps_hard_reg_set_p (uses, GET_MODE (dest), dregno)
5455 || overlaps_hard_reg_set_p (defs, GET_MODE (dest), dregno))
5456 return false;
5458 /* See whether there is a successor block to which we could move INSN. */
5459 next_block = next_block_for_reg (bb, dregno, end_dregno);
5460 if (!next_block)
5461 return false;
5463 /* At this point we are committed to moving INSN, but let's try to
5464 move it as far as we can. */
5467 live_out = df_get_live_out (bb);
5468 live_in = df_get_live_in (next_block);
5469 bb = next_block;
5471 /* Check whether BB uses DEST or clobbers DEST. We need to add
5472 INSN to BB if so. Either way, DEST is no longer live on entry,
5473 except for any part that overlaps SRC (next loop). */
5474 bb_uses = &DF_LR_BB_INFO (bb)->use;
5475 bb_defs = &DF_LR_BB_INFO (bb)->def;
5476 if (df_live)
5478 for (i = dregno; i < end_dregno; i++)
5480 if (REGNO_REG_SET_P (bb_uses, i) || REGNO_REG_SET_P (bb_defs, i)
5481 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
5482 next_block = NULL;
5483 CLEAR_REGNO_REG_SET (live_out, i);
5484 CLEAR_REGNO_REG_SET (live_in, i);
5487 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
5488 Either way, SRC is now live on entry. */
5489 for (i = sregno; i < end_sregno; i++)
5491 if (REGNO_REG_SET_P (bb_defs, i)
5492 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
5493 next_block = NULL;
5494 SET_REGNO_REG_SET (live_out, i);
5495 SET_REGNO_REG_SET (live_in, i);
5498 else
5500 /* DF_LR_BB_INFO (bb)->def does not comprise the DF_REF_PARTIAL and
5501 DF_REF_CONDITIONAL defs. So if DF_LIVE doesn't exist, i.e.
5502 at -O1, just give up searching NEXT_BLOCK. */
5503 next_block = NULL;
5504 for (i = dregno; i < end_dregno; i++)
5506 CLEAR_REGNO_REG_SET (live_out, i);
5507 CLEAR_REGNO_REG_SET (live_in, i);
5510 for (i = sregno; i < end_sregno; i++)
5512 SET_REGNO_REG_SET (live_out, i);
5513 SET_REGNO_REG_SET (live_in, i);
5517 /* If we don't need to add the move to BB, look for a single
5518 successor block. */
5519 if (next_block)
5520 next_block = next_block_for_reg (next_block, dregno, end_dregno);
5522 while (next_block);
5524 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
5525 (next loop). */
5526 for (i = dregno; i < end_dregno; i++)
5528 CLEAR_REGNO_REG_SET (bb_uses, i);
5529 SET_REGNO_REG_SET (bb_defs, i);
5532 /* BB now uses SRC. */
5533 for (i = sregno; i < end_sregno; i++)
5534 SET_REGNO_REG_SET (bb_uses, i);
5536 emit_insn_after (PATTERN (insn), bb_note (bb));
5537 delete_insn (insn);
5538 return true;
5541 /* Look for register copies in the first block of the function, and move
5542 them down into successor blocks if the register is used only on one
5543 path. This exposes more opportunities for shrink-wrapping. These
5544 kinds of sets often occur when incoming argument registers are moved
5545 to call-saved registers because their values are live across one or
5546 more calls during the function. */
5548 static void
5549 prepare_shrink_wrap (basic_block entry_block)
5551 rtx insn, curr, x;
5552 HARD_REG_SET uses, defs;
5553 df_ref *ref;
5555 CLEAR_HARD_REG_SET (uses);
5556 CLEAR_HARD_REG_SET (defs);
5557 FOR_BB_INSNS_REVERSE_SAFE (entry_block, insn, curr)
5558 if (NONDEBUG_INSN_P (insn)
5559 && !move_insn_for_shrink_wrap (entry_block, insn, uses, defs))
5561 /* Add all defined registers to DEFs. */
5562 for (ref = DF_INSN_DEFS (insn); *ref; ref++)
5564 x = DF_REF_REG (*ref);
5565 if (REG_P (x) && HARD_REGISTER_P (x))
5566 SET_HARD_REG_BIT (defs, REGNO (x));
5569 /* Add all used registers to USESs. */
5570 for (ref = DF_INSN_USES (insn); *ref; ref++)
5572 x = DF_REF_REG (*ref);
5573 if (REG_P (x) && HARD_REGISTER_P (x))
5574 SET_HARD_REG_BIT (uses, REGNO (x));
5579 #endif
5581 #ifdef HAVE_return
5582 /* Insert use of return register before the end of BB. */
5584 static void
5585 emit_use_return_register_into_block (basic_block bb)
5587 rtx seq, insn;
5588 start_sequence ();
5589 use_return_register ();
5590 seq = get_insns ();
5591 end_sequence ();
5592 insn = BB_END (bb);
5593 #ifdef HAVE_cc0
5594 if (reg_mentioned_p (cc0_rtx, PATTERN (insn)))
5595 insn = prev_cc0_setter (insn);
5596 #endif
5597 emit_insn_before (seq, insn);
5601 /* Create a return pattern, either simple_return or return, depending on
5602 simple_p. */
5604 static rtx
5605 gen_return_pattern (bool simple_p)
5607 #ifdef HAVE_simple_return
5608 return simple_p ? gen_simple_return () : gen_return ();
5609 #else
5610 gcc_assert (!simple_p);
5611 return gen_return ();
5612 #endif
5615 /* Insert an appropriate return pattern at the end of block BB. This
5616 also means updating block_for_insn appropriately. SIMPLE_P is
5617 the same as in gen_return_pattern and passed to it. */
5619 static void
5620 emit_return_into_block (bool simple_p, basic_block bb)
5622 rtx jump, pat;
5623 jump = emit_jump_insn_after (gen_return_pattern (simple_p), BB_END (bb));
5624 pat = PATTERN (jump);
5625 if (GET_CODE (pat) == PARALLEL)
5626 pat = XVECEXP (pat, 0, 0);
5627 gcc_assert (ANY_RETURN_P (pat));
5628 JUMP_LABEL (jump) = pat;
5630 #endif
5632 /* Set JUMP_LABEL for a return insn. */
5634 void
5635 set_return_jump_label (rtx returnjump)
5637 rtx pat = PATTERN (returnjump);
5638 if (GET_CODE (pat) == PARALLEL)
5639 pat = XVECEXP (pat, 0, 0);
5640 if (ANY_RETURN_P (pat))
5641 JUMP_LABEL (returnjump) = pat;
5642 else
5643 JUMP_LABEL (returnjump) = ret_rtx;
5646 #ifdef HAVE_simple_return
5647 /* Create a copy of BB instructions and insert at BEFORE. Redirect
5648 preds of BB to COPY_BB if they don't appear in NEED_PROLOGUE. */
5649 static void
5650 dup_block_and_redirect (basic_block bb, basic_block copy_bb, rtx before,
5651 bitmap_head *need_prologue)
5653 edge_iterator ei;
5654 edge e;
5655 rtx insn = BB_END (bb);
5657 /* We know BB has a single successor, so there is no need to copy a
5658 simple jump at the end of BB. */
5659 if (simplejump_p (insn))
5660 insn = PREV_INSN (insn);
5662 start_sequence ();
5663 duplicate_insn_chain (BB_HEAD (bb), insn);
5664 if (dump_file)
5666 unsigned count = 0;
5667 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5668 if (active_insn_p (insn))
5669 ++count;
5670 fprintf (dump_file, "Duplicating bb %d to bb %d, %u active insns.\n",
5671 bb->index, copy_bb->index, count);
5673 insn = get_insns ();
5674 end_sequence ();
5675 emit_insn_before (insn, before);
5677 /* Redirect all the paths that need no prologue into copy_bb. */
5678 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
5679 if (!bitmap_bit_p (need_prologue, e->src->index))
5681 int freq = EDGE_FREQUENCY (e);
5682 copy_bb->count += e->count;
5683 copy_bb->frequency += EDGE_FREQUENCY (e);
5684 e->dest->count -= e->count;
5685 if (e->dest->count < 0)
5686 e->dest->count = 0;
5687 e->dest->frequency -= freq;
5688 if (e->dest->frequency < 0)
5689 e->dest->frequency = 0;
5690 redirect_edge_and_branch_force (e, copy_bb);
5691 continue;
5693 else
5694 ei_next (&ei);
5696 #endif
5698 #if defined (HAVE_return) || defined (HAVE_simple_return)
5699 /* Return true if there are any active insns between HEAD and TAIL. */
5700 static bool
5701 active_insn_between (rtx head, rtx tail)
5703 while (tail)
5705 if (active_insn_p (tail))
5706 return true;
5707 if (tail == head)
5708 return false;
5709 tail = PREV_INSN (tail);
5711 return false;
5714 /* LAST_BB is a block that exits, and empty of active instructions.
5715 Examine its predecessors for jumps that can be converted to
5716 (conditional) returns. */
5717 static vec<edge>
5718 convert_jumps_to_returns (basic_block last_bb, bool simple_p,
5719 vec<edge> unconverted ATTRIBUTE_UNUSED)
5721 int i;
5722 basic_block bb;
5723 rtx label;
5724 edge_iterator ei;
5725 edge e;
5726 auto_vec<basic_block> src_bbs (EDGE_COUNT (last_bb->preds));
5728 FOR_EACH_EDGE (e, ei, last_bb->preds)
5729 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
5730 src_bbs.quick_push (e->src);
5732 label = BB_HEAD (last_bb);
5734 FOR_EACH_VEC_ELT (src_bbs, i, bb)
5736 rtx jump = BB_END (bb);
5738 if (!JUMP_P (jump) || JUMP_LABEL (jump) != label)
5739 continue;
5741 e = find_edge (bb, last_bb);
5743 /* If we have an unconditional jump, we can replace that
5744 with a simple return instruction. */
5745 if (simplejump_p (jump))
5747 /* The use of the return register might be present in the exit
5748 fallthru block. Either:
5749 - removing the use is safe, and we should remove the use in
5750 the exit fallthru block, or
5751 - removing the use is not safe, and we should add it here.
5752 For now, we conservatively choose the latter. Either of the
5753 2 helps in crossjumping. */
5754 emit_use_return_register_into_block (bb);
5756 emit_return_into_block (simple_p, bb);
5757 delete_insn (jump);
5760 /* If we have a conditional jump branching to the last
5761 block, we can try to replace that with a conditional
5762 return instruction. */
5763 else if (condjump_p (jump))
5765 rtx dest;
5767 if (simple_p)
5768 dest = simple_return_rtx;
5769 else
5770 dest = ret_rtx;
5771 if (!redirect_jump (jump, dest, 0))
5773 #ifdef HAVE_simple_return
5774 if (simple_p)
5776 if (dump_file)
5777 fprintf (dump_file,
5778 "Failed to redirect bb %d branch.\n", bb->index);
5779 unconverted.safe_push (e);
5781 #endif
5782 continue;
5785 /* See comment in simplejump_p case above. */
5786 emit_use_return_register_into_block (bb);
5788 /* If this block has only one successor, it both jumps
5789 and falls through to the fallthru block, so we can't
5790 delete the edge. */
5791 if (single_succ_p (bb))
5792 continue;
5794 else
5796 #ifdef HAVE_simple_return
5797 if (simple_p)
5799 if (dump_file)
5800 fprintf (dump_file,
5801 "Failed to redirect bb %d branch.\n", bb->index);
5802 unconverted.safe_push (e);
5804 #endif
5805 continue;
5808 /* Fix up the CFG for the successful change we just made. */
5809 redirect_edge_succ (e, EXIT_BLOCK_PTR_FOR_FN (cfun));
5810 e->flags &= ~EDGE_CROSSING;
5812 src_bbs.release ();
5813 return unconverted;
5816 /* Emit a return insn for the exit fallthru block. */
5817 static basic_block
5818 emit_return_for_exit (edge exit_fallthru_edge, bool simple_p)
5820 basic_block last_bb = exit_fallthru_edge->src;
5822 if (JUMP_P (BB_END (last_bb)))
5824 last_bb = split_edge (exit_fallthru_edge);
5825 exit_fallthru_edge = single_succ_edge (last_bb);
5827 emit_barrier_after (BB_END (last_bb));
5828 emit_return_into_block (simple_p, last_bb);
5829 exit_fallthru_edge->flags &= ~EDGE_FALLTHRU;
5830 return last_bb;
5832 #endif
5835 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
5836 this into place with notes indicating where the prologue ends and where
5837 the epilogue begins. Update the basic block information when possible.
5839 Notes on epilogue placement:
5840 There are several kinds of edges to the exit block:
5841 * a single fallthru edge from LAST_BB
5842 * possibly, edges from blocks containing sibcalls
5843 * possibly, fake edges from infinite loops
5845 The epilogue is always emitted on the fallthru edge from the last basic
5846 block in the function, LAST_BB, into the exit block.
5848 If LAST_BB is empty except for a label, it is the target of every
5849 other basic block in the function that ends in a return. If a
5850 target has a return or simple_return pattern (possibly with
5851 conditional variants), these basic blocks can be changed so that a
5852 return insn is emitted into them, and their target is adjusted to
5853 the real exit block.
5855 Notes on shrink wrapping: We implement a fairly conservative
5856 version of shrink-wrapping rather than the textbook one. We only
5857 generate a single prologue and a single epilogue. This is
5858 sufficient to catch a number of interesting cases involving early
5859 exits.
5861 First, we identify the blocks that require the prologue to occur before
5862 them. These are the ones that modify a call-saved register, or reference
5863 any of the stack or frame pointer registers. To simplify things, we then
5864 mark everything reachable from these blocks as also requiring a prologue.
5865 This takes care of loops automatically, and avoids the need to examine
5866 whether MEMs reference the frame, since it is sufficient to check for
5867 occurrences of the stack or frame pointer.
5869 We then compute the set of blocks for which the need for a prologue
5870 is anticipatable (borrowing terminology from the shrink-wrapping
5871 description in Muchnick's book). These are the blocks which either
5872 require a prologue themselves, or those that have only successors
5873 where the prologue is anticipatable. The prologue needs to be
5874 inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
5875 is not. For the moment, we ensure that only one such edge exists.
5877 The epilogue is placed as described above, but we make a
5878 distinction between inserting return and simple_return patterns
5879 when modifying other blocks that end in a return. Blocks that end
5880 in a sibcall omit the sibcall_epilogue if the block is not in
5881 ANTIC. */
5883 static void
5884 thread_prologue_and_epilogue_insns (void)
5886 bool inserted;
5887 #ifdef HAVE_simple_return
5888 vec<edge> unconverted_simple_returns = vNULL;
5889 bool nonempty_prologue;
5890 bitmap_head bb_flags;
5891 unsigned max_grow_size;
5892 #endif
5893 rtx returnjump;
5894 rtx seq ATTRIBUTE_UNUSED, epilogue_end ATTRIBUTE_UNUSED;
5895 rtx prologue_seq ATTRIBUTE_UNUSED, split_prologue_seq ATTRIBUTE_UNUSED;
5896 edge e, entry_edge, orig_entry_edge, exit_fallthru_edge;
5897 edge_iterator ei;
5899 df_analyze ();
5901 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5903 inserted = false;
5904 seq = NULL_RTX;
5905 epilogue_end = NULL_RTX;
5906 returnjump = NULL_RTX;
5908 /* Can't deal with multiple successors of the entry block at the
5909 moment. Function should always have at least one entry
5910 point. */
5911 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5912 entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5913 orig_entry_edge = entry_edge;
5915 split_prologue_seq = NULL_RTX;
5916 if (flag_split_stack
5917 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
5918 == NULL))
5920 #ifndef HAVE_split_stack_prologue
5921 gcc_unreachable ();
5922 #else
5923 gcc_assert (HAVE_split_stack_prologue);
5925 start_sequence ();
5926 emit_insn (gen_split_stack_prologue ());
5927 split_prologue_seq = get_insns ();
5928 end_sequence ();
5930 record_insns (split_prologue_seq, NULL, &prologue_insn_hash);
5931 set_insn_locations (split_prologue_seq, prologue_location);
5932 #endif
5935 prologue_seq = NULL_RTX;
5936 #ifdef HAVE_prologue
5937 if (HAVE_prologue)
5939 start_sequence ();
5940 seq = gen_prologue ();
5941 emit_insn (seq);
5943 /* Insert an explicit USE for the frame pointer
5944 if the profiling is on and the frame pointer is required. */
5945 if (crtl->profile && frame_pointer_needed)
5946 emit_use (hard_frame_pointer_rtx);
5948 /* Retain a map of the prologue insns. */
5949 record_insns (seq, NULL, &prologue_insn_hash);
5950 emit_note (NOTE_INSN_PROLOGUE_END);
5952 /* Ensure that instructions are not moved into the prologue when
5953 profiling is on. The call to the profiling routine can be
5954 emitted within the live range of a call-clobbered register. */
5955 if (!targetm.profile_before_prologue () && crtl->profile)
5956 emit_insn (gen_blockage ());
5958 prologue_seq = get_insns ();
5959 end_sequence ();
5960 set_insn_locations (prologue_seq, prologue_location);
5962 #endif
5964 #ifdef HAVE_simple_return
5965 bitmap_initialize (&bb_flags, &bitmap_default_obstack);
5967 /* Try to perform a kind of shrink-wrapping, making sure the
5968 prologue/epilogue is emitted only around those parts of the
5969 function that require it. */
5971 nonempty_prologue = false;
5972 for (seq = prologue_seq; seq; seq = NEXT_INSN (seq))
5973 if (!NOTE_P (seq) || NOTE_KIND (seq) != NOTE_INSN_PROLOGUE_END)
5975 nonempty_prologue = true;
5976 break;
5979 if (flag_shrink_wrap && HAVE_simple_return
5980 && (targetm.profile_before_prologue () || !crtl->profile)
5981 && nonempty_prologue && !crtl->calls_eh_return)
5983 HARD_REG_SET prologue_clobbered, prologue_used, live_on_edge;
5984 struct hard_reg_set_container set_up_by_prologue;
5985 rtx p_insn;
5986 vec<basic_block> vec;
5987 basic_block bb;
5988 bitmap_head bb_antic_flags;
5989 bitmap_head bb_on_list;
5990 bitmap_head bb_tail;
5992 if (dump_file)
5993 fprintf (dump_file, "Attempting shrink-wrapping optimization.\n");
5995 /* Compute the registers set and used in the prologue. */
5996 CLEAR_HARD_REG_SET (prologue_clobbered);
5997 CLEAR_HARD_REG_SET (prologue_used);
5998 for (p_insn = prologue_seq; p_insn; p_insn = NEXT_INSN (p_insn))
6000 HARD_REG_SET this_used;
6001 if (!NONDEBUG_INSN_P (p_insn))
6002 continue;
6004 CLEAR_HARD_REG_SET (this_used);
6005 note_uses (&PATTERN (p_insn), record_hard_reg_uses,
6006 &this_used);
6007 AND_COMPL_HARD_REG_SET (this_used, prologue_clobbered);
6008 IOR_HARD_REG_SET (prologue_used, this_used);
6009 note_stores (PATTERN (p_insn), record_hard_reg_sets,
6010 &prologue_clobbered);
6013 prepare_shrink_wrap (entry_edge->dest);
6015 bitmap_initialize (&bb_antic_flags, &bitmap_default_obstack);
6016 bitmap_initialize (&bb_on_list, &bitmap_default_obstack);
6017 bitmap_initialize (&bb_tail, &bitmap_default_obstack);
6019 /* Find the set of basic blocks that require a stack frame,
6020 and blocks that are too big to be duplicated. */
6022 vec.create (n_basic_blocks_for_fn (cfun));
6024 CLEAR_HARD_REG_SET (set_up_by_prologue.set);
6025 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
6026 STACK_POINTER_REGNUM);
6027 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode, ARG_POINTER_REGNUM);
6028 if (frame_pointer_needed)
6029 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
6030 HARD_FRAME_POINTER_REGNUM);
6031 if (pic_offset_table_rtx)
6032 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
6033 PIC_OFFSET_TABLE_REGNUM);
6034 if (crtl->drap_reg)
6035 add_to_hard_reg_set (&set_up_by_prologue.set,
6036 GET_MODE (crtl->drap_reg),
6037 REGNO (crtl->drap_reg));
6038 if (targetm.set_up_by_prologue)
6039 targetm.set_up_by_prologue (&set_up_by_prologue);
6041 /* We don't use a different max size depending on
6042 optimize_bb_for_speed_p because increasing shrink-wrapping
6043 opportunities by duplicating tail blocks can actually result
6044 in an overall decrease in code size. */
6045 max_grow_size = get_uncond_jump_length ();
6046 max_grow_size *= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS);
6048 FOR_EACH_BB_FN (bb, cfun)
6050 rtx insn;
6051 unsigned size = 0;
6053 FOR_BB_INSNS (bb, insn)
6054 if (NONDEBUG_INSN_P (insn))
6056 if (requires_stack_frame_p (insn, prologue_used,
6057 set_up_by_prologue.set))
6059 if (bb == entry_edge->dest)
6060 goto fail_shrinkwrap;
6061 bitmap_set_bit (&bb_flags, bb->index);
6062 vec.quick_push (bb);
6063 break;
6065 else if (size <= max_grow_size)
6067 size += get_attr_min_length (insn);
6068 if (size > max_grow_size)
6069 bitmap_set_bit (&bb_on_list, bb->index);
6074 /* Blocks that really need a prologue, or are too big for tails. */
6075 bitmap_ior_into (&bb_on_list, &bb_flags);
6077 /* For every basic block that needs a prologue, mark all blocks
6078 reachable from it, so as to ensure they are also seen as
6079 requiring a prologue. */
6080 while (!vec.is_empty ())
6082 basic_block tmp_bb = vec.pop ();
6084 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
6085 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
6086 && bitmap_set_bit (&bb_flags, e->dest->index))
6087 vec.quick_push (e->dest);
6090 /* Find the set of basic blocks that need no prologue, have a
6091 single successor, can be duplicated, meet a max size
6092 requirement, and go to the exit via like blocks. */
6093 vec.quick_push (EXIT_BLOCK_PTR_FOR_FN (cfun));
6094 while (!vec.is_empty ())
6096 basic_block tmp_bb = vec.pop ();
6098 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
6099 if (single_succ_p (e->src)
6100 && !bitmap_bit_p (&bb_on_list, e->src->index)
6101 && can_duplicate_block_p (e->src))
6103 edge pe;
6104 edge_iterator pei;
6106 /* If there is predecessor of e->src which doesn't
6107 need prologue and the edge is complex,
6108 we might not be able to redirect the branch
6109 to a copy of e->src. */
6110 FOR_EACH_EDGE (pe, pei, e->src->preds)
6111 if ((pe->flags & EDGE_COMPLEX) != 0
6112 && !bitmap_bit_p (&bb_flags, pe->src->index))
6113 break;
6114 if (pe == NULL && bitmap_set_bit (&bb_tail, e->src->index))
6115 vec.quick_push (e->src);
6119 /* Now walk backwards from every block that is marked as needing
6120 a prologue to compute the bb_antic_flags bitmap. Exclude
6121 tail blocks; They can be duplicated to be used on paths not
6122 needing a prologue. */
6123 bitmap_clear (&bb_on_list);
6124 bitmap_and_compl (&bb_antic_flags, &bb_flags, &bb_tail);
6125 FOR_EACH_BB_FN (bb, cfun)
6127 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
6128 continue;
6129 FOR_EACH_EDGE (e, ei, bb->preds)
6130 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
6131 && bitmap_set_bit (&bb_on_list, e->src->index))
6132 vec.quick_push (e->src);
6134 while (!vec.is_empty ())
6136 basic_block tmp_bb = vec.pop ();
6137 bool all_set = true;
6139 bitmap_clear_bit (&bb_on_list, tmp_bb->index);
6140 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
6141 if (!bitmap_bit_p (&bb_antic_flags, e->dest->index))
6143 all_set = false;
6144 break;
6147 if (all_set)
6149 bitmap_set_bit (&bb_antic_flags, tmp_bb->index);
6150 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
6151 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
6152 && bitmap_set_bit (&bb_on_list, e->src->index))
6153 vec.quick_push (e->src);
6156 /* Find exactly one edge that leads to a block in ANTIC from
6157 a block that isn't. */
6158 if (!bitmap_bit_p (&bb_antic_flags, entry_edge->dest->index))
6159 FOR_EACH_BB_FN (bb, cfun)
6161 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
6162 continue;
6163 FOR_EACH_EDGE (e, ei, bb->preds)
6164 if (!bitmap_bit_p (&bb_antic_flags, e->src->index))
6166 if (entry_edge != orig_entry_edge)
6168 entry_edge = orig_entry_edge;
6169 if (dump_file)
6170 fprintf (dump_file, "More than one candidate edge.\n");
6171 goto fail_shrinkwrap;
6173 if (dump_file)
6174 fprintf (dump_file, "Found candidate edge for "
6175 "shrink-wrapping, %d->%d.\n", e->src->index,
6176 e->dest->index);
6177 entry_edge = e;
6181 if (entry_edge != orig_entry_edge)
6183 /* Test whether the prologue is known to clobber any register
6184 (other than FP or SP) which are live on the edge. */
6185 CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
6186 if (frame_pointer_needed)
6187 CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
6188 REG_SET_TO_HARD_REG_SET (live_on_edge,
6189 df_get_live_in (entry_edge->dest));
6190 if (hard_reg_set_intersect_p (live_on_edge, prologue_clobbered))
6192 entry_edge = orig_entry_edge;
6193 if (dump_file)
6194 fprintf (dump_file,
6195 "Shrink-wrapping aborted due to clobber.\n");
6198 if (entry_edge != orig_entry_edge)
6200 crtl->shrink_wrapped = true;
6201 if (dump_file)
6202 fprintf (dump_file, "Performing shrink-wrapping.\n");
6204 /* Find tail blocks reachable from both blocks needing a
6205 prologue and blocks not needing a prologue. */
6206 if (!bitmap_empty_p (&bb_tail))
6207 FOR_EACH_BB_FN (bb, cfun)
6209 bool some_pro, some_no_pro;
6210 if (!bitmap_bit_p (&bb_tail, bb->index))
6211 continue;
6212 some_pro = some_no_pro = false;
6213 FOR_EACH_EDGE (e, ei, bb->preds)
6215 if (bitmap_bit_p (&bb_flags, e->src->index))
6216 some_pro = true;
6217 else
6218 some_no_pro = true;
6220 if (some_pro && some_no_pro)
6221 vec.quick_push (bb);
6222 else
6223 bitmap_clear_bit (&bb_tail, bb->index);
6225 /* Find the head of each tail. */
6226 while (!vec.is_empty ())
6228 basic_block tbb = vec.pop ();
6230 if (!bitmap_bit_p (&bb_tail, tbb->index))
6231 continue;
6233 while (single_succ_p (tbb))
6235 tbb = single_succ (tbb);
6236 bitmap_clear_bit (&bb_tail, tbb->index);
6239 /* Now duplicate the tails. */
6240 if (!bitmap_empty_p (&bb_tail))
6241 FOR_EACH_BB_REVERSE_FN (bb, cfun)
6243 basic_block copy_bb, tbb;
6244 rtx insert_point;
6245 int eflags;
6247 if (!bitmap_clear_bit (&bb_tail, bb->index))
6248 continue;
6250 /* Create a copy of BB, instructions and all, for
6251 use on paths that don't need a prologue.
6252 Ideal placement of the copy is on a fall-thru edge
6253 or after a block that would jump to the copy. */
6254 FOR_EACH_EDGE (e, ei, bb->preds)
6255 if (!bitmap_bit_p (&bb_flags, e->src->index)
6256 && single_succ_p (e->src))
6257 break;
6258 if (e)
6260 /* Make sure we insert after any barriers. */
6261 rtx end = get_last_bb_insn (e->src);
6262 copy_bb = create_basic_block (NEXT_INSN (end),
6263 NULL_RTX, e->src);
6264 BB_COPY_PARTITION (copy_bb, e->src);
6266 else
6268 /* Otherwise put the copy at the end of the function. */
6269 copy_bb = create_basic_block (NULL_RTX, NULL_RTX,
6270 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
6271 BB_COPY_PARTITION (copy_bb, bb);
6274 insert_point = emit_note_after (NOTE_INSN_DELETED,
6275 BB_END (copy_bb));
6276 emit_barrier_after (BB_END (copy_bb));
6278 tbb = bb;
6279 while (1)
6281 dup_block_and_redirect (tbb, copy_bb, insert_point,
6282 &bb_flags);
6283 tbb = single_succ (tbb);
6284 if (tbb == EXIT_BLOCK_PTR_FOR_FN (cfun))
6285 break;
6286 e = split_block (copy_bb, PREV_INSN (insert_point));
6287 copy_bb = e->dest;
6290 /* Quiet verify_flow_info by (ab)using EDGE_FAKE.
6291 We have yet to add a simple_return to the tails,
6292 as we'd like to first convert_jumps_to_returns in
6293 case the block is no longer used after that. */
6294 eflags = EDGE_FAKE;
6295 if (CALL_P (PREV_INSN (insert_point))
6296 && SIBLING_CALL_P (PREV_INSN (insert_point)))
6297 eflags = EDGE_SIBCALL | EDGE_ABNORMAL;
6298 make_single_succ_edge (copy_bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
6299 eflags);
6301 /* verify_flow_info doesn't like a note after a
6302 sibling call. */
6303 delete_insn (insert_point);
6304 if (bitmap_empty_p (&bb_tail))
6305 break;
6309 fail_shrinkwrap:
6310 bitmap_clear (&bb_tail);
6311 bitmap_clear (&bb_antic_flags);
6312 bitmap_clear (&bb_on_list);
6313 vec.release ();
6315 #endif
6317 if (split_prologue_seq != NULL_RTX)
6319 insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
6320 inserted = true;
6322 if (prologue_seq != NULL_RTX)
6324 insert_insn_on_edge (prologue_seq, entry_edge);
6325 inserted = true;
6328 /* If the exit block has no non-fake predecessors, we don't need
6329 an epilogue. */
6330 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6331 if ((e->flags & EDGE_FAKE) == 0)
6332 break;
6333 if (e == NULL)
6334 goto epilogue_done;
6336 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
6338 exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
6340 /* If we're allowed to generate a simple return instruction, then by
6341 definition we don't need a full epilogue. If the last basic
6342 block before the exit block does not contain active instructions,
6343 examine its predecessors and try to emit (conditional) return
6344 instructions. */
6345 #ifdef HAVE_simple_return
6346 if (entry_edge != orig_entry_edge)
6348 if (optimize)
6350 unsigned i, last;
6352 /* convert_jumps_to_returns may add to preds of the exit block
6353 (but won't remove). Stop at end of current preds. */
6354 last = EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
6355 for (i = 0; i < last; i++)
6357 e = EDGE_I (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds, i);
6358 if (LABEL_P (BB_HEAD (e->src))
6359 && !bitmap_bit_p (&bb_flags, e->src->index)
6360 && !active_insn_between (BB_HEAD (e->src), BB_END (e->src)))
6361 unconverted_simple_returns
6362 = convert_jumps_to_returns (e->src, true,
6363 unconverted_simple_returns);
6367 if (exit_fallthru_edge != NULL
6368 && EDGE_COUNT (exit_fallthru_edge->src->preds) != 0
6369 && !bitmap_bit_p (&bb_flags, exit_fallthru_edge->src->index))
6371 basic_block last_bb;
6373 last_bb = emit_return_for_exit (exit_fallthru_edge, true);
6374 returnjump = BB_END (last_bb);
6375 exit_fallthru_edge = NULL;
6378 #endif
6379 #ifdef HAVE_return
6380 if (HAVE_return)
6382 if (exit_fallthru_edge == NULL)
6383 goto epilogue_done;
6385 if (optimize)
6387 basic_block last_bb = exit_fallthru_edge->src;
6389 if (LABEL_P (BB_HEAD (last_bb))
6390 && !active_insn_between (BB_HEAD (last_bb), BB_END (last_bb)))
6391 convert_jumps_to_returns (last_bb, false, vNULL);
6393 if (EDGE_COUNT (last_bb->preds) != 0
6394 && single_succ_p (last_bb))
6396 last_bb = emit_return_for_exit (exit_fallthru_edge, false);
6397 epilogue_end = returnjump = BB_END (last_bb);
6398 #ifdef HAVE_simple_return
6399 /* Emitting the return may add a basic block.
6400 Fix bb_flags for the added block. */
6401 if (last_bb != exit_fallthru_edge->src)
6402 bitmap_set_bit (&bb_flags, last_bb->index);
6403 #endif
6404 goto epilogue_done;
6408 #endif
6410 /* A small fib -- epilogue is not yet completed, but we wish to re-use
6411 this marker for the splits of EH_RETURN patterns, and nothing else
6412 uses the flag in the meantime. */
6413 epilogue_completed = 1;
6415 #ifdef HAVE_eh_return
6416 /* Find non-fallthru edges that end with EH_RETURN instructions. On
6417 some targets, these get split to a special version of the epilogue
6418 code. In order to be able to properly annotate these with unwind
6419 info, try to split them now. If we get a valid split, drop an
6420 EPILOGUE_BEG note and mark the insns as epilogue insns. */
6421 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6423 rtx prev, last, trial;
6425 if (e->flags & EDGE_FALLTHRU)
6426 continue;
6427 last = BB_END (e->src);
6428 if (!eh_returnjump_p (last))
6429 continue;
6431 prev = PREV_INSN (last);
6432 trial = try_split (PATTERN (last), last, 1);
6433 if (trial == last)
6434 continue;
6436 record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
6437 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
6439 #endif
6441 /* If nothing falls through into the exit block, we don't need an
6442 epilogue. */
6444 if (exit_fallthru_edge == NULL)
6445 goto epilogue_done;
6447 #ifdef HAVE_epilogue
6448 if (HAVE_epilogue)
6450 start_sequence ();
6451 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
6452 seq = gen_epilogue ();
6453 if (seq)
6454 emit_jump_insn (seq);
6456 /* Retain a map of the epilogue insns. */
6457 record_insns (seq, NULL, &epilogue_insn_hash);
6458 set_insn_locations (seq, epilogue_location);
6460 seq = get_insns ();
6461 returnjump = get_last_insn ();
6462 end_sequence ();
6464 insert_insn_on_edge (seq, exit_fallthru_edge);
6465 inserted = true;
6467 if (JUMP_P (returnjump))
6468 set_return_jump_label (returnjump);
6470 else
6471 #endif
6473 basic_block cur_bb;
6475 if (! next_active_insn (BB_END (exit_fallthru_edge->src)))
6476 goto epilogue_done;
6477 /* We have a fall-through edge to the exit block, the source is not
6478 at the end of the function, and there will be an assembler epilogue
6479 at the end of the function.
6480 We can't use force_nonfallthru here, because that would try to
6481 use return. Inserting a jump 'by hand' is extremely messy, so
6482 we take advantage of cfg_layout_finalize using
6483 fixup_fallthru_exit_predecessor. */
6484 cfg_layout_initialize (0);
6485 FOR_EACH_BB_FN (cur_bb, cfun)
6486 if (cur_bb->index >= NUM_FIXED_BLOCKS
6487 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
6488 cur_bb->aux = cur_bb->next_bb;
6489 cfg_layout_finalize ();
6492 epilogue_done:
6494 default_rtl_profile ();
6496 if (inserted)
6498 sbitmap blocks;
6500 commit_edge_insertions ();
6502 /* Look for basic blocks within the prologue insns. */
6503 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
6504 bitmap_clear (blocks);
6505 bitmap_set_bit (blocks, entry_edge->dest->index);
6506 bitmap_set_bit (blocks, orig_entry_edge->dest->index);
6507 find_many_sub_basic_blocks (blocks);
6508 sbitmap_free (blocks);
6510 /* The epilogue insns we inserted may cause the exit edge to no longer
6511 be fallthru. */
6512 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6514 if (((e->flags & EDGE_FALLTHRU) != 0)
6515 && returnjump_p (BB_END (e->src)))
6516 e->flags &= ~EDGE_FALLTHRU;
6520 #ifdef HAVE_simple_return
6521 /* If there were branches to an empty LAST_BB which we tried to
6522 convert to conditional simple_returns, but couldn't for some
6523 reason, create a block to hold a simple_return insn and redirect
6524 those remaining edges. */
6525 if (!unconverted_simple_returns.is_empty ())
6527 basic_block simple_return_block_hot = NULL;
6528 basic_block simple_return_block_cold = NULL;
6529 edge pending_edge_hot = NULL;
6530 edge pending_edge_cold = NULL;
6531 basic_block exit_pred;
6532 int i;
6534 gcc_assert (entry_edge != orig_entry_edge);
6536 /* See if we can reuse the last insn that was emitted for the
6537 epilogue. */
6538 if (returnjump != NULL_RTX
6539 && JUMP_LABEL (returnjump) == simple_return_rtx)
6541 e = split_block (BLOCK_FOR_INSN (returnjump), PREV_INSN (returnjump));
6542 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
6543 simple_return_block_hot = e->dest;
6544 else
6545 simple_return_block_cold = e->dest;
6548 /* Also check returns we might need to add to tail blocks. */
6549 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6550 if (EDGE_COUNT (e->src->preds) != 0
6551 && (e->flags & EDGE_FAKE) != 0
6552 && !bitmap_bit_p (&bb_flags, e->src->index))
6554 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
6555 pending_edge_hot = e;
6556 else
6557 pending_edge_cold = e;
6560 /* Save a pointer to the exit's predecessor BB for use in
6561 inserting new BBs at the end of the function. Do this
6562 after the call to split_block above which may split
6563 the original exit pred. */
6564 exit_pred = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
6566 FOR_EACH_VEC_ELT (unconverted_simple_returns, i, e)
6568 basic_block *pdest_bb;
6569 edge pending;
6571 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
6573 pdest_bb = &simple_return_block_hot;
6574 pending = pending_edge_hot;
6576 else
6578 pdest_bb = &simple_return_block_cold;
6579 pending = pending_edge_cold;
6582 if (*pdest_bb == NULL && pending != NULL)
6584 emit_return_into_block (true, pending->src);
6585 pending->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
6586 *pdest_bb = pending->src;
6588 else if (*pdest_bb == NULL)
6590 basic_block bb;
6591 rtx start;
6593 bb = create_basic_block (NULL, NULL, exit_pred);
6594 BB_COPY_PARTITION (bb, e->src);
6595 start = emit_jump_insn_after (gen_simple_return (),
6596 BB_END (bb));
6597 JUMP_LABEL (start) = simple_return_rtx;
6598 emit_barrier_after (start);
6600 *pdest_bb = bb;
6601 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
6603 redirect_edge_and_branch_force (e, *pdest_bb);
6605 unconverted_simple_returns.release ();
6608 if (entry_edge != orig_entry_edge)
6610 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6611 if (EDGE_COUNT (e->src->preds) != 0
6612 && (e->flags & EDGE_FAKE) != 0
6613 && !bitmap_bit_p (&bb_flags, e->src->index))
6615 emit_return_into_block (true, e->src);
6616 e->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
6619 #endif
6621 #ifdef HAVE_sibcall_epilogue
6622 /* Emit sibling epilogues before any sibling call sites. */
6623 for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds); (e =
6624 ei_safe_edge (ei));
6627 basic_block bb = e->src;
6628 rtx insn = BB_END (bb);
6629 rtx ep_seq;
6631 if (!CALL_P (insn)
6632 || ! SIBLING_CALL_P (insn)
6633 #ifdef HAVE_simple_return
6634 || (entry_edge != orig_entry_edge
6635 && !bitmap_bit_p (&bb_flags, bb->index))
6636 #endif
6639 ei_next (&ei);
6640 continue;
6643 ep_seq = gen_sibcall_epilogue ();
6644 if (ep_seq)
6646 start_sequence ();
6647 emit_note (NOTE_INSN_EPILOGUE_BEG);
6648 emit_insn (ep_seq);
6649 seq = get_insns ();
6650 end_sequence ();
6652 /* Retain a map of the epilogue insns. Used in life analysis to
6653 avoid getting rid of sibcall epilogue insns. Do this before we
6654 actually emit the sequence. */
6655 record_insns (seq, NULL, &epilogue_insn_hash);
6656 set_insn_locations (seq, epilogue_location);
6658 emit_insn_before (seq, insn);
6660 ei_next (&ei);
6662 #endif
6664 #ifdef HAVE_epilogue
6665 if (epilogue_end)
6667 rtx insn, next;
6669 /* Similarly, move any line notes that appear after the epilogue.
6670 There is no need, however, to be quite so anal about the existence
6671 of such a note. Also possibly move
6672 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
6673 info generation. */
6674 for (insn = epilogue_end; insn; insn = next)
6676 next = NEXT_INSN (insn);
6677 if (NOTE_P (insn)
6678 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
6679 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
6682 #endif
6684 #ifdef HAVE_simple_return
6685 bitmap_clear (&bb_flags);
6686 #endif
6688 /* Threading the prologue and epilogue changes the artificial refs
6689 in the entry and exit blocks. */
6690 epilogue_completed = 1;
6691 df_update_entry_exit_and_calls ();
6694 /* Reposition the prologue-end and epilogue-begin notes after
6695 instruction scheduling. */
6697 void
6698 reposition_prologue_and_epilogue_notes (void)
6700 #if defined (HAVE_prologue) || defined (HAVE_epilogue) \
6701 || defined (HAVE_sibcall_epilogue)
6702 /* Since the hash table is created on demand, the fact that it is
6703 non-null is a signal that it is non-empty. */
6704 if (prologue_insn_hash != NULL)
6706 size_t len = htab_elements (prologue_insn_hash);
6707 rtx insn, last = NULL, note = NULL;
6709 /* Scan from the beginning until we reach the last prologue insn. */
6710 /* ??? While we do have the CFG intact, there are two problems:
6711 (1) The prologue can contain loops (typically probing the stack),
6712 which means that the end of the prologue isn't in the first bb.
6713 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
6714 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6716 if (NOTE_P (insn))
6718 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
6719 note = insn;
6721 else if (contains (insn, prologue_insn_hash))
6723 last = insn;
6724 if (--len == 0)
6725 break;
6729 if (last)
6731 if (note == NULL)
6733 /* Scan forward looking for the PROLOGUE_END note. It should
6734 be right at the beginning of the block, possibly with other
6735 insn notes that got moved there. */
6736 for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
6738 if (NOTE_P (note)
6739 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
6740 break;
6744 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
6745 if (LABEL_P (last))
6746 last = NEXT_INSN (last);
6747 reorder_insns (note, note, last);
6751 if (epilogue_insn_hash != NULL)
6753 edge_iterator ei;
6754 edge e;
6756 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6758 rtx insn, first = NULL, note = NULL;
6759 basic_block bb = e->src;
6761 /* Scan from the beginning until we reach the first epilogue insn. */
6762 FOR_BB_INSNS (bb, insn)
6764 if (NOTE_P (insn))
6766 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
6768 note = insn;
6769 if (first != NULL)
6770 break;
6773 else if (first == NULL && contains (insn, epilogue_insn_hash))
6775 first = insn;
6776 if (note != NULL)
6777 break;
6781 if (note)
6783 /* If the function has a single basic block, and no real
6784 epilogue insns (e.g. sibcall with no cleanup), the
6785 epilogue note can get scheduled before the prologue
6786 note. If we have frame related prologue insns, having
6787 them scanned during the epilogue will result in a crash.
6788 In this case re-order the epilogue note to just before
6789 the last insn in the block. */
6790 if (first == NULL)
6791 first = BB_END (bb);
6793 if (PREV_INSN (first) != note)
6794 reorder_insns (note, note, PREV_INSN (first));
6798 #endif /* HAVE_prologue or HAVE_epilogue */
6801 /* Returns the name of function declared by FNDECL. */
6802 const char *
6803 fndecl_name (tree fndecl)
6805 if (fndecl == NULL)
6806 return "(nofn)";
6807 return lang_hooks.decl_printable_name (fndecl, 2);
6810 /* Returns the name of function FN. */
6811 const char *
6812 function_name (struct function *fn)
6814 tree fndecl = (fn == NULL) ? NULL : fn->decl;
6815 return fndecl_name (fndecl);
6818 /* Returns the name of the current function. */
6819 const char *
6820 current_function_name (void)
6822 return function_name (cfun);
6826 static unsigned int
6827 rest_of_handle_check_leaf_regs (void)
6829 #ifdef LEAF_REGISTERS
6830 crtl->uses_only_leaf_regs
6831 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6832 #endif
6833 return 0;
6836 /* Insert a TYPE into the used types hash table of CFUN. */
6838 static void
6839 used_types_insert_helper (tree type, struct function *func)
6841 if (type != NULL && func != NULL)
6843 void **slot;
6845 if (func->used_types_hash == NULL)
6846 func->used_types_hash = htab_create_ggc (37, htab_hash_pointer,
6847 htab_eq_pointer, NULL);
6848 slot = htab_find_slot (func->used_types_hash, type, INSERT);
6849 if (*slot == NULL)
6850 *slot = type;
6854 /* Given a type, insert it into the used hash table in cfun. */
6855 void
6856 used_types_insert (tree t)
6858 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6859 if (TYPE_NAME (t))
6860 break;
6861 else
6862 t = TREE_TYPE (t);
6863 if (TREE_CODE (t) == ERROR_MARK)
6864 return;
6865 if (TYPE_NAME (t) == NULL_TREE
6866 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6867 t = TYPE_MAIN_VARIANT (t);
6868 if (debug_info_level > DINFO_LEVEL_NONE)
6870 if (cfun)
6871 used_types_insert_helper (t, cfun);
6872 else
6874 /* So this might be a type referenced by a global variable.
6875 Record that type so that we can later decide to emit its
6876 debug information. */
6877 vec_safe_push (types_used_by_cur_var_decl, t);
6882 /* Helper to Hash a struct types_used_by_vars_entry. */
6884 static hashval_t
6885 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6887 gcc_assert (entry && entry->var_decl && entry->type);
6889 return iterative_hash_object (entry->type,
6890 iterative_hash_object (entry->var_decl, 0));
6893 /* Hash function of the types_used_by_vars_entry hash table. */
6895 hashval_t
6896 types_used_by_vars_do_hash (const void *x)
6898 const struct types_used_by_vars_entry *entry =
6899 (const struct types_used_by_vars_entry *) x;
6901 return hash_types_used_by_vars_entry (entry);
6904 /*Equality function of the types_used_by_vars_entry hash table. */
6907 types_used_by_vars_eq (const void *x1, const void *x2)
6909 const struct types_used_by_vars_entry *e1 =
6910 (const struct types_used_by_vars_entry *) x1;
6911 const struct types_used_by_vars_entry *e2 =
6912 (const struct types_used_by_vars_entry *)x2;
6914 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6917 /* Inserts an entry into the types_used_by_vars_hash hash table. */
6919 void
6920 types_used_by_var_decl_insert (tree type, tree var_decl)
6922 if (type != NULL && var_decl != NULL)
6924 void **slot;
6925 struct types_used_by_vars_entry e;
6926 e.var_decl = var_decl;
6927 e.type = type;
6928 if (types_used_by_vars_hash == NULL)
6929 types_used_by_vars_hash =
6930 htab_create_ggc (37, types_used_by_vars_do_hash,
6931 types_used_by_vars_eq, NULL);
6932 slot = htab_find_slot_with_hash (types_used_by_vars_hash, &e,
6933 hash_types_used_by_vars_entry (&e), INSERT);
6934 if (*slot == NULL)
6936 struct types_used_by_vars_entry *entry;
6937 entry = ggc_alloc_types_used_by_vars_entry ();
6938 entry->type = type;
6939 entry->var_decl = var_decl;
6940 *slot = entry;
6945 namespace {
6947 const pass_data pass_data_leaf_regs =
6949 RTL_PASS, /* type */
6950 "*leaf_regs", /* name */
6951 OPTGROUP_NONE, /* optinfo_flags */
6952 false, /* has_gate */
6953 true, /* has_execute */
6954 TV_NONE, /* tv_id */
6955 0, /* properties_required */
6956 0, /* properties_provided */
6957 0, /* properties_destroyed */
6958 0, /* todo_flags_start */
6959 0, /* todo_flags_finish */
6962 class pass_leaf_regs : public rtl_opt_pass
6964 public:
6965 pass_leaf_regs (gcc::context *ctxt)
6966 : rtl_opt_pass (pass_data_leaf_regs, ctxt)
6969 /* opt_pass methods: */
6970 unsigned int execute () { return rest_of_handle_check_leaf_regs (); }
6972 }; // class pass_leaf_regs
6974 } // anon namespace
6976 rtl_opt_pass *
6977 make_pass_leaf_regs (gcc::context *ctxt)
6979 return new pass_leaf_regs (ctxt);
6982 static unsigned int
6983 rest_of_handle_thread_prologue_and_epilogue (void)
6985 if (optimize)
6986 cleanup_cfg (CLEANUP_EXPENSIVE);
6988 /* On some machines, the prologue and epilogue code, or parts thereof,
6989 can be represented as RTL. Doing so lets us schedule insns between
6990 it and the rest of the code and also allows delayed branch
6991 scheduling to operate in the epilogue. */
6992 thread_prologue_and_epilogue_insns ();
6994 /* Shrink-wrapping can result in unreachable edges in the epilogue,
6995 see PR57320. */
6996 cleanup_cfg (0);
6998 /* The stack usage info is finalized during prologue expansion. */
6999 if (flag_stack_usage_info)
7000 output_stack_usage ();
7002 return 0;
7005 namespace {
7007 const pass_data pass_data_thread_prologue_and_epilogue =
7009 RTL_PASS, /* type */
7010 "pro_and_epilogue", /* name */
7011 OPTGROUP_NONE, /* optinfo_flags */
7012 false, /* has_gate */
7013 true, /* has_execute */
7014 TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
7015 0, /* properties_required */
7016 0, /* properties_provided */
7017 0, /* properties_destroyed */
7018 TODO_verify_flow, /* todo_flags_start */
7019 ( TODO_df_verify | TODO_df_finish
7020 | TODO_verify_rtl_sharing ), /* todo_flags_finish */
7023 class pass_thread_prologue_and_epilogue : public rtl_opt_pass
7025 public:
7026 pass_thread_prologue_and_epilogue (gcc::context *ctxt)
7027 : rtl_opt_pass (pass_data_thread_prologue_and_epilogue, ctxt)
7030 /* opt_pass methods: */
7031 unsigned int execute () {
7032 return rest_of_handle_thread_prologue_and_epilogue ();
7035 }; // class pass_thread_prologue_and_epilogue
7037 } // anon namespace
7039 rtl_opt_pass *
7040 make_pass_thread_prologue_and_epilogue (gcc::context *ctxt)
7042 return new pass_thread_prologue_and_epilogue (ctxt);
7046 /* This mini-pass fixes fall-out from SSA in asm statements that have
7047 in-out constraints. Say you start with
7049 orig = inout;
7050 asm ("": "+mr" (inout));
7051 use (orig);
7053 which is transformed very early to use explicit output and match operands:
7055 orig = inout;
7056 asm ("": "=mr" (inout) : "0" (inout));
7057 use (orig);
7059 Or, after SSA and copyprop,
7061 asm ("": "=mr" (inout_2) : "0" (inout_1));
7062 use (inout_1);
7064 Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
7065 they represent two separate values, so they will get different pseudo
7066 registers during expansion. Then, since the two operands need to match
7067 per the constraints, but use different pseudo registers, reload can
7068 only register a reload for these operands. But reloads can only be
7069 satisfied by hardregs, not by memory, so we need a register for this
7070 reload, just because we are presented with non-matching operands.
7071 So, even though we allow memory for this operand, no memory can be
7072 used for it, just because the two operands don't match. This can
7073 cause reload failures on register-starved targets.
7075 So it's a symptom of reload not being able to use memory for reloads
7076 or, alternatively it's also a symptom of both operands not coming into
7077 reload as matching (in which case the pseudo could go to memory just
7078 fine, as the alternative allows it, and no reload would be necessary).
7079 We fix the latter problem here, by transforming
7081 asm ("": "=mr" (inout_2) : "0" (inout_1));
7083 back to
7085 inout_2 = inout_1;
7086 asm ("": "=mr" (inout_2) : "0" (inout_2)); */
7088 static void
7089 match_asm_constraints_1 (rtx insn, rtx *p_sets, int noutputs)
7091 int i;
7092 bool changed = false;
7093 rtx op = SET_SRC (p_sets[0]);
7094 int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
7095 rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
7096 bool *output_matched = XALLOCAVEC (bool, noutputs);
7098 memset (output_matched, 0, noutputs * sizeof (bool));
7099 for (i = 0; i < ninputs; i++)
7101 rtx input, output, insns;
7102 const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
7103 char *end;
7104 int match, j;
7106 if (*constraint == '%')
7107 constraint++;
7109 match = strtoul (constraint, &end, 10);
7110 if (end == constraint)
7111 continue;
7113 gcc_assert (match < noutputs);
7114 output = SET_DEST (p_sets[match]);
7115 input = RTVEC_ELT (inputs, i);
7116 /* Only do the transformation for pseudos. */
7117 if (! REG_P (output)
7118 || rtx_equal_p (output, input)
7119 || (GET_MODE (input) != VOIDmode
7120 && GET_MODE (input) != GET_MODE (output)))
7121 continue;
7123 /* We can't do anything if the output is also used as input,
7124 as we're going to overwrite it. */
7125 for (j = 0; j < ninputs; j++)
7126 if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
7127 break;
7128 if (j != ninputs)
7129 continue;
7131 /* Avoid changing the same input several times. For
7132 asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
7133 only change in once (to out1), rather than changing it
7134 first to out1 and afterwards to out2. */
7135 if (i > 0)
7137 for (j = 0; j < noutputs; j++)
7138 if (output_matched[j] && input == SET_DEST (p_sets[j]))
7139 break;
7140 if (j != noutputs)
7141 continue;
7143 output_matched[match] = true;
7145 start_sequence ();
7146 emit_move_insn (output, input);
7147 insns = get_insns ();
7148 end_sequence ();
7149 emit_insn_before (insns, insn);
7151 /* Now replace all mentions of the input with output. We can't
7152 just replace the occurrence in inputs[i], as the register might
7153 also be used in some other input (or even in an address of an
7154 output), which would mean possibly increasing the number of
7155 inputs by one (namely 'output' in addition), which might pose
7156 a too complicated problem for reload to solve. E.g. this situation:
7158 asm ("" : "=r" (output), "=m" (input) : "0" (input))
7160 Here 'input' is used in two occurrences as input (once for the
7161 input operand, once for the address in the second output operand).
7162 If we would replace only the occurrence of the input operand (to
7163 make the matching) we would be left with this:
7165 output = input
7166 asm ("" : "=r" (output), "=m" (input) : "0" (output))
7168 Now we suddenly have two different input values (containing the same
7169 value, but different pseudos) where we formerly had only one.
7170 With more complicated asms this might lead to reload failures
7171 which wouldn't have happen without this pass. So, iterate over
7172 all operands and replace all occurrences of the register used. */
7173 for (j = 0; j < noutputs; j++)
7174 if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
7175 && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
7176 SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
7177 input, output);
7178 for (j = 0; j < ninputs; j++)
7179 if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
7180 RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
7181 input, output);
7183 changed = true;
7186 if (changed)
7187 df_insn_rescan (insn);
7190 static unsigned
7191 rest_of_match_asm_constraints (void)
7193 basic_block bb;
7194 rtx insn, pat, *p_sets;
7195 int noutputs;
7197 if (!crtl->has_asm_statement)
7198 return 0;
7200 df_set_flags (DF_DEFER_INSN_RESCAN);
7201 FOR_EACH_BB_FN (bb, cfun)
7203 FOR_BB_INSNS (bb, insn)
7205 if (!INSN_P (insn))
7206 continue;
7208 pat = PATTERN (insn);
7209 if (GET_CODE (pat) == PARALLEL)
7210 p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
7211 else if (GET_CODE (pat) == SET)
7212 p_sets = &PATTERN (insn), noutputs = 1;
7213 else
7214 continue;
7216 if (GET_CODE (*p_sets) == SET
7217 && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
7218 match_asm_constraints_1 (insn, p_sets, noutputs);
7222 return TODO_df_finish;
7225 namespace {
7227 const pass_data pass_data_match_asm_constraints =
7229 RTL_PASS, /* type */
7230 "asmcons", /* name */
7231 OPTGROUP_NONE, /* optinfo_flags */
7232 false, /* has_gate */
7233 true, /* has_execute */
7234 TV_NONE, /* tv_id */
7235 0, /* properties_required */
7236 0, /* properties_provided */
7237 0, /* properties_destroyed */
7238 0, /* todo_flags_start */
7239 0, /* todo_flags_finish */
7242 class pass_match_asm_constraints : public rtl_opt_pass
7244 public:
7245 pass_match_asm_constraints (gcc::context *ctxt)
7246 : rtl_opt_pass (pass_data_match_asm_constraints, ctxt)
7249 /* opt_pass methods: */
7250 unsigned int execute () { return rest_of_match_asm_constraints (); }
7252 }; // class pass_match_asm_constraints
7254 } // anon namespace
7256 rtl_opt_pass *
7257 make_pass_match_asm_constraints (gcc::context *ctxt)
7259 return new pass_match_asm_constraints (ctxt);
7263 #include "gt-function.h"