2013-11-13 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / function.c
blob620554d55f3306d8bc60649ac9cc2202104f38a2
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987-2013 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 "flags.h"
41 #include "except.h"
42 #include "function.h"
43 #include "expr.h"
44 #include "optabs.h"
45 #include "libfuncs.h"
46 #include "regs.h"
47 #include "hard-reg-set.h"
48 #include "insn-config.h"
49 #include "recog.h"
50 #include "output.h"
51 #include "basic-block.h"
52 #include "hashtab.h"
53 #include "ggc.h"
54 #include "tm_p.h"
55 #include "langhooks.h"
56 #include "target.h"
57 #include "common/common-target.h"
58 #include "gimplify.h"
59 #include "tree-pass.h"
60 #include "predict.h"
61 #include "df.h"
62 #include "params.h"
63 #include "bb-reorder.h"
65 /* So we can assign to cfun in this file. */
66 #undef cfun
68 #ifndef STACK_ALIGNMENT_NEEDED
69 #define STACK_ALIGNMENT_NEEDED 1
70 #endif
72 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
74 /* Round a value to the lowest integer less than it that is a multiple of
75 the required alignment. Avoid using division in case the value is
76 negative. Assume the alignment is a power of two. */
77 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
79 /* Similar, but round to the next highest integer that meets the
80 alignment. */
81 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
83 /* Nonzero once virtual register instantiation has been done.
84 assign_stack_local uses frame_pointer_rtx when this is nonzero.
85 calls.c:emit_library_call_value_1 uses it to set up
86 post-instantiation libcalls. */
87 int virtuals_instantiated;
89 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
90 static GTY(()) int funcdef_no;
92 /* These variables hold pointers to functions to create and destroy
93 target specific, per-function data structures. */
94 struct machine_function * (*init_machine_status) (void);
96 /* The currently compiled function. */
97 struct function *cfun = 0;
99 /* These hashes record the prologue and epilogue insns. */
100 static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
101 htab_t prologue_insn_hash;
102 static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
103 htab_t epilogue_insn_hash;
106 htab_t types_used_by_vars_hash = NULL;
107 vec<tree, va_gc> *types_used_by_cur_var_decl;
109 /* Forward declarations. */
111 static struct temp_slot *find_temp_slot_from_address (rtx);
112 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
113 static void pad_below (struct args_size *, enum machine_mode, tree);
114 static void reorder_blocks_1 (rtx, tree, vec<tree> *);
115 static int all_blocks (tree, tree *);
116 static tree *get_block_vector (tree, int *);
117 extern tree debug_find_var_in_block_tree (tree, tree);
118 /* We always define `record_insns' even if it's not used so that we
119 can always export `prologue_epilogue_contains'. */
120 static void record_insns (rtx, rtx, htab_t *) ATTRIBUTE_UNUSED;
121 static bool contains (const_rtx, htab_t);
122 static void prepare_function_start (void);
123 static void do_clobber_return_reg (rtx, void *);
124 static void do_use_return_reg (rtx, void *);
126 /* Stack of nested functions. */
127 /* Keep track of the cfun stack. */
129 typedef struct function *function_p;
131 static vec<function_p> function_context_stack;
133 /* Save the current context for compilation of a nested function.
134 This is called from language-specific code. */
136 void
137 push_function_context (void)
139 if (cfun == 0)
140 allocate_struct_function (NULL, false);
142 function_context_stack.safe_push (cfun);
143 set_cfun (NULL);
146 /* Restore the last saved context, at the end of a nested function.
147 This function is called from language-specific code. */
149 void
150 pop_function_context (void)
152 struct function *p = function_context_stack.pop ();
153 set_cfun (p);
154 current_function_decl = p->decl;
156 /* Reset variables that have known state during rtx generation. */
157 virtuals_instantiated = 0;
158 generating_concat_p = 1;
161 /* Clear out all parts of the state in F that can safely be discarded
162 after the function has been parsed, but not compiled, to let
163 garbage collection reclaim the memory. */
165 void
166 free_after_parsing (struct function *f)
168 f->language = 0;
171 /* Clear out all parts of the state in F that can safely be discarded
172 after the function has been compiled, to let garbage collection
173 reclaim the memory. */
175 void
176 free_after_compilation (struct function *f)
178 prologue_insn_hash = NULL;
179 epilogue_insn_hash = NULL;
181 free (crtl->emit.regno_pointer_align);
183 memset (crtl, 0, sizeof (struct rtl_data));
184 f->eh = NULL;
185 f->machine = NULL;
186 f->cfg = NULL;
188 regno_reg_rtx = NULL;
191 /* Return size needed for stack frame based on slots so far allocated.
192 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
193 the caller may have to do that. */
195 HOST_WIDE_INT
196 get_frame_size (void)
198 if (FRAME_GROWS_DOWNWARD)
199 return -frame_offset;
200 else
201 return frame_offset;
204 /* Issue an error message and return TRUE if frame OFFSET overflows in
205 the signed target pointer arithmetics for function FUNC. Otherwise
206 return FALSE. */
208 bool
209 frame_offset_overflow (HOST_WIDE_INT offset, tree func)
211 unsigned HOST_WIDE_INT size = FRAME_GROWS_DOWNWARD ? -offset : offset;
213 if (size > ((unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (Pmode) - 1))
214 /* Leave room for the fixed part of the frame. */
215 - 64 * UNITS_PER_WORD)
217 error_at (DECL_SOURCE_LOCATION (func),
218 "total size of local objects too large");
219 return TRUE;
222 return FALSE;
225 /* Return stack slot alignment in bits for TYPE and MODE. */
227 static unsigned int
228 get_stack_local_alignment (tree type, enum machine_mode mode)
230 unsigned int alignment;
232 if (mode == BLKmode)
233 alignment = BIGGEST_ALIGNMENT;
234 else
235 alignment = GET_MODE_ALIGNMENT (mode);
237 /* Allow the frond-end to (possibly) increase the alignment of this
238 stack slot. */
239 if (! type)
240 type = lang_hooks.types.type_for_mode (mode, 0);
242 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
245 /* Determine whether it is possible to fit a stack slot of size SIZE and
246 alignment ALIGNMENT into an area in the stack frame that starts at
247 frame offset START and has a length of LENGTH. If so, store the frame
248 offset to be used for the stack slot in *POFFSET and return true;
249 return false otherwise. This function will extend the frame size when
250 given a start/length pair that lies at the end of the frame. */
252 static bool
253 try_fit_stack_local (HOST_WIDE_INT start, HOST_WIDE_INT length,
254 HOST_WIDE_INT size, unsigned int alignment,
255 HOST_WIDE_INT *poffset)
257 HOST_WIDE_INT this_frame_offset;
258 int frame_off, frame_alignment, frame_phase;
260 /* Calculate how many bytes the start of local variables is off from
261 stack alignment. */
262 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
263 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
264 frame_phase = frame_off ? frame_alignment - frame_off : 0;
266 /* Round the frame offset to the specified alignment. */
268 /* We must be careful here, since FRAME_OFFSET might be negative and
269 division with a negative dividend isn't as well defined as we might
270 like. So we instead assume that ALIGNMENT is a power of two and
271 use logical operations which are unambiguous. */
272 if (FRAME_GROWS_DOWNWARD)
273 this_frame_offset
274 = (FLOOR_ROUND (start + length - size - frame_phase,
275 (unsigned HOST_WIDE_INT) alignment)
276 + frame_phase);
277 else
278 this_frame_offset
279 = (CEIL_ROUND (start - frame_phase,
280 (unsigned HOST_WIDE_INT) alignment)
281 + frame_phase);
283 /* See if it fits. If this space is at the edge of the frame,
284 consider extending the frame to make it fit. Our caller relies on
285 this when allocating a new slot. */
286 if (frame_offset == start && this_frame_offset < frame_offset)
287 frame_offset = this_frame_offset;
288 else if (this_frame_offset < start)
289 return false;
290 else if (start + length == frame_offset
291 && this_frame_offset + size > start + length)
292 frame_offset = this_frame_offset + size;
293 else if (this_frame_offset + size > start + length)
294 return false;
296 *poffset = this_frame_offset;
297 return true;
300 /* Create a new frame_space structure describing free space in the stack
301 frame beginning at START and ending at END, and chain it into the
302 function's frame_space_list. */
304 static void
305 add_frame_space (HOST_WIDE_INT start, HOST_WIDE_INT end)
307 struct frame_space *space = ggc_alloc_frame_space ();
308 space->next = crtl->frame_space_list;
309 crtl->frame_space_list = space;
310 space->start = start;
311 space->length = end - start;
314 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
315 with machine mode MODE.
317 ALIGN controls the amount of alignment for the address of the slot:
318 0 means according to MODE,
319 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
320 -2 means use BITS_PER_UNIT,
321 positive specifies alignment boundary in bits.
323 KIND has ASLK_REDUCE_ALIGN bit set if it is OK to reduce
324 alignment and ASLK_RECORD_PAD bit set if we should remember
325 extra space we allocated for alignment purposes. When we are
326 called from assign_stack_temp_for_type, it is not set so we don't
327 track the same stack slot in two independent lists.
329 We do not round to stack_boundary here. */
332 assign_stack_local_1 (enum machine_mode mode, HOST_WIDE_INT size,
333 int align, int kind)
335 rtx x, addr;
336 int bigend_correction = 0;
337 HOST_WIDE_INT slot_offset = 0, old_frame_offset;
338 unsigned int alignment, alignment_in_bits;
340 if (align == 0)
342 alignment = get_stack_local_alignment (NULL, mode);
343 alignment /= BITS_PER_UNIT;
345 else if (align == -1)
347 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
348 size = CEIL_ROUND (size, alignment);
350 else if (align == -2)
351 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
352 else
353 alignment = align / BITS_PER_UNIT;
355 alignment_in_bits = alignment * BITS_PER_UNIT;
357 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
358 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
360 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
361 alignment = alignment_in_bits / BITS_PER_UNIT;
364 if (SUPPORTS_STACK_ALIGNMENT)
366 if (crtl->stack_alignment_estimated < alignment_in_bits)
368 if (!crtl->stack_realign_processed)
369 crtl->stack_alignment_estimated = alignment_in_bits;
370 else
372 /* If stack is realigned and stack alignment value
373 hasn't been finalized, it is OK not to increase
374 stack_alignment_estimated. The bigger alignment
375 requirement is recorded in stack_alignment_needed
376 below. */
377 gcc_assert (!crtl->stack_realign_finalized);
378 if (!crtl->stack_realign_needed)
380 /* It is OK to reduce the alignment as long as the
381 requested size is 0 or the estimated stack
382 alignment >= mode alignment. */
383 gcc_assert ((kind & ASLK_REDUCE_ALIGN)
384 || size == 0
385 || (crtl->stack_alignment_estimated
386 >= GET_MODE_ALIGNMENT (mode)));
387 alignment_in_bits = crtl->stack_alignment_estimated;
388 alignment = alignment_in_bits / BITS_PER_UNIT;
394 if (crtl->stack_alignment_needed < alignment_in_bits)
395 crtl->stack_alignment_needed = alignment_in_bits;
396 if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
397 crtl->max_used_stack_slot_alignment = alignment_in_bits;
399 if (mode != BLKmode || size != 0)
401 if (kind & ASLK_RECORD_PAD)
403 struct frame_space **psp;
405 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
407 struct frame_space *space = *psp;
408 if (!try_fit_stack_local (space->start, space->length, size,
409 alignment, &slot_offset))
410 continue;
411 *psp = space->next;
412 if (slot_offset > space->start)
413 add_frame_space (space->start, slot_offset);
414 if (slot_offset + size < space->start + space->length)
415 add_frame_space (slot_offset + size,
416 space->start + space->length);
417 goto found_space;
421 else if (!STACK_ALIGNMENT_NEEDED)
423 slot_offset = frame_offset;
424 goto found_space;
427 old_frame_offset = frame_offset;
429 if (FRAME_GROWS_DOWNWARD)
431 frame_offset -= size;
432 try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
434 if (kind & ASLK_RECORD_PAD)
436 if (slot_offset > frame_offset)
437 add_frame_space (frame_offset, slot_offset);
438 if (slot_offset + size < old_frame_offset)
439 add_frame_space (slot_offset + size, old_frame_offset);
442 else
444 frame_offset += size;
445 try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
447 if (kind & ASLK_RECORD_PAD)
449 if (slot_offset > old_frame_offset)
450 add_frame_space (old_frame_offset, slot_offset);
451 if (slot_offset + size < frame_offset)
452 add_frame_space (slot_offset + size, frame_offset);
456 found_space:
457 /* On a big-endian machine, if we are allocating more space than we will use,
458 use the least significant bytes of those that are allocated. */
459 if (BYTES_BIG_ENDIAN && mode != BLKmode && GET_MODE_SIZE (mode) < size)
460 bigend_correction = size - GET_MODE_SIZE (mode);
462 /* If we have already instantiated virtual registers, return the actual
463 address relative to the frame pointer. */
464 if (virtuals_instantiated)
465 addr = plus_constant (Pmode, frame_pointer_rtx,
466 trunc_int_for_mode
467 (slot_offset + bigend_correction
468 + STARTING_FRAME_OFFSET, Pmode));
469 else
470 addr = plus_constant (Pmode, virtual_stack_vars_rtx,
471 trunc_int_for_mode
472 (slot_offset + bigend_correction,
473 Pmode));
475 x = gen_rtx_MEM (mode, addr);
476 set_mem_align (x, alignment_in_bits);
477 MEM_NOTRAP_P (x) = 1;
479 stack_slot_list
480 = gen_rtx_EXPR_LIST (VOIDmode, x, stack_slot_list);
482 if (frame_offset_overflow (frame_offset, current_function_decl))
483 frame_offset = 0;
485 return x;
488 /* Wrap up assign_stack_local_1 with last parameter as false. */
491 assign_stack_local (enum machine_mode mode, HOST_WIDE_INT size, int align)
493 return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
496 /* In order to evaluate some expressions, such as function calls returning
497 structures in memory, we need to temporarily allocate stack locations.
498 We record each allocated temporary in the following structure.
500 Associated with each temporary slot is a nesting level. When we pop up
501 one level, all temporaries associated with the previous level are freed.
502 Normally, all temporaries are freed after the execution of the statement
503 in which they were created. However, if we are inside a ({...}) grouping,
504 the result may be in a temporary and hence must be preserved. If the
505 result could be in a temporary, we preserve it if we can determine which
506 one it is in. If we cannot determine which temporary may contain the
507 result, all temporaries are preserved. A temporary is preserved by
508 pretending it was allocated at the previous nesting level. */
510 struct GTY(()) temp_slot {
511 /* Points to next temporary slot. */
512 struct temp_slot *next;
513 /* Points to previous temporary slot. */
514 struct temp_slot *prev;
515 /* The rtx to used to reference the slot. */
516 rtx slot;
517 /* The size, in units, of the slot. */
518 HOST_WIDE_INT size;
519 /* The type of the object in the slot, or zero if it doesn't correspond
520 to a type. We use this to determine whether a slot can be reused.
521 It can be reused if objects of the type of the new slot will always
522 conflict with objects of the type of the old slot. */
523 tree type;
524 /* The alignment (in bits) of the slot. */
525 unsigned int align;
526 /* Nonzero if this temporary is currently in use. */
527 char in_use;
528 /* Nesting level at which this slot is being used. */
529 int level;
530 /* The offset of the slot from the frame_pointer, including extra space
531 for alignment. This info is for combine_temp_slots. */
532 HOST_WIDE_INT base_offset;
533 /* The size of the slot, including extra space for alignment. This
534 info is for combine_temp_slots. */
535 HOST_WIDE_INT full_size;
538 /* A table of addresses that represent a stack slot. The table is a mapping
539 from address RTXen to a temp slot. */
540 static GTY((param_is(struct temp_slot_address_entry))) htab_t temp_slot_address_table;
541 static size_t n_temp_slots_in_use;
543 /* Entry for the above hash table. */
544 struct GTY(()) temp_slot_address_entry {
545 hashval_t hash;
546 rtx address;
547 struct temp_slot *temp_slot;
550 /* Removes temporary slot TEMP from LIST. */
552 static void
553 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
555 if (temp->next)
556 temp->next->prev = temp->prev;
557 if (temp->prev)
558 temp->prev->next = temp->next;
559 else
560 *list = temp->next;
562 temp->prev = temp->next = NULL;
565 /* Inserts temporary slot TEMP to LIST. */
567 static void
568 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
570 temp->next = *list;
571 if (*list)
572 (*list)->prev = temp;
573 temp->prev = NULL;
574 *list = temp;
577 /* Returns the list of used temp slots at LEVEL. */
579 static struct temp_slot **
580 temp_slots_at_level (int level)
582 if (level >= (int) vec_safe_length (used_temp_slots))
583 vec_safe_grow_cleared (used_temp_slots, level + 1);
585 return &(*used_temp_slots)[level];
588 /* Returns the maximal temporary slot level. */
590 static int
591 max_slot_level (void)
593 if (!used_temp_slots)
594 return -1;
596 return used_temp_slots->length () - 1;
599 /* Moves temporary slot TEMP to LEVEL. */
601 static void
602 move_slot_to_level (struct temp_slot *temp, int level)
604 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
605 insert_slot_to_list (temp, temp_slots_at_level (level));
606 temp->level = level;
609 /* Make temporary slot TEMP available. */
611 static void
612 make_slot_available (struct temp_slot *temp)
614 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
615 insert_slot_to_list (temp, &avail_temp_slots);
616 temp->in_use = 0;
617 temp->level = -1;
618 n_temp_slots_in_use--;
621 /* Compute the hash value for an address -> temp slot mapping.
622 The value is cached on the mapping entry. */
623 static hashval_t
624 temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
626 int do_not_record = 0;
627 return hash_rtx (t->address, GET_MODE (t->address),
628 &do_not_record, NULL, false);
631 /* Return the hash value for an address -> temp slot mapping. */
632 static hashval_t
633 temp_slot_address_hash (const void *p)
635 const struct temp_slot_address_entry *t;
636 t = (const struct temp_slot_address_entry *) p;
637 return t->hash;
640 /* Compare two address -> temp slot mapping entries. */
641 static int
642 temp_slot_address_eq (const void *p1, const void *p2)
644 const struct temp_slot_address_entry *t1, *t2;
645 t1 = (const struct temp_slot_address_entry *) p1;
646 t2 = (const struct temp_slot_address_entry *) p2;
647 return exp_equiv_p (t1->address, t2->address, 0, true);
650 /* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
651 static void
652 insert_temp_slot_address (rtx address, struct temp_slot *temp_slot)
654 void **slot;
655 struct temp_slot_address_entry *t = ggc_alloc_temp_slot_address_entry ();
656 t->address = address;
657 t->temp_slot = temp_slot;
658 t->hash = temp_slot_address_compute_hash (t);
659 slot = htab_find_slot_with_hash (temp_slot_address_table, t, t->hash, INSERT);
660 *slot = t;
663 /* Remove an address -> temp slot mapping entry if the temp slot is
664 not in use anymore. Callback for remove_unused_temp_slot_addresses. */
665 static int
666 remove_unused_temp_slot_addresses_1 (void **slot, void *data ATTRIBUTE_UNUSED)
668 const struct temp_slot_address_entry *t;
669 t = (const struct temp_slot_address_entry *) *slot;
670 if (! t->temp_slot->in_use)
671 htab_clear_slot (temp_slot_address_table, slot);
672 return 1;
675 /* Remove all mappings of addresses to unused temp slots. */
676 static void
677 remove_unused_temp_slot_addresses (void)
679 /* Use quicker clearing if there aren't any active temp slots. */
680 if (n_temp_slots_in_use)
681 htab_traverse (temp_slot_address_table,
682 remove_unused_temp_slot_addresses_1,
683 NULL);
684 else
685 htab_empty (temp_slot_address_table);
688 /* Find the temp slot corresponding to the object at address X. */
690 static struct temp_slot *
691 find_temp_slot_from_address (rtx x)
693 struct temp_slot *p;
694 struct temp_slot_address_entry tmp, *t;
696 /* First try the easy way:
697 See if X exists in the address -> temp slot mapping. */
698 tmp.address = x;
699 tmp.temp_slot = NULL;
700 tmp.hash = temp_slot_address_compute_hash (&tmp);
701 t = (struct temp_slot_address_entry *)
702 htab_find_with_hash (temp_slot_address_table, &tmp, tmp.hash);
703 if (t)
704 return t->temp_slot;
706 /* If we have a sum involving a register, see if it points to a temp
707 slot. */
708 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
709 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
710 return p;
711 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
712 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
713 return p;
715 /* Last resort: Address is a virtual stack var address. */
716 if (GET_CODE (x) == PLUS
717 && XEXP (x, 0) == virtual_stack_vars_rtx
718 && CONST_INT_P (XEXP (x, 1)))
720 int i;
721 for (i = max_slot_level (); i >= 0; i--)
722 for (p = *temp_slots_at_level (i); p; p = p->next)
724 if (INTVAL (XEXP (x, 1)) >= p->base_offset
725 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size)
726 return p;
730 return NULL;
733 /* Allocate a temporary stack slot and record it for possible later
734 reuse.
736 MODE is the machine mode to be given to the returned rtx.
738 SIZE is the size in units of the space required. We do no rounding here
739 since assign_stack_local will do any required rounding.
741 TYPE is the type that will be used for the stack slot. */
744 assign_stack_temp_for_type (enum machine_mode mode, HOST_WIDE_INT size,
745 tree type)
747 unsigned int align;
748 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
749 rtx slot;
751 /* If SIZE is -1 it means that somebody tried to allocate a temporary
752 of a variable size. */
753 gcc_assert (size != -1);
755 align = get_stack_local_alignment (type, mode);
757 /* Try to find an available, already-allocated temporary of the proper
758 mode which meets the size and alignment requirements. Choose the
759 smallest one with the closest alignment.
761 If assign_stack_temp is called outside of the tree->rtl expansion,
762 we cannot reuse the stack slots (that may still refer to
763 VIRTUAL_STACK_VARS_REGNUM). */
764 if (!virtuals_instantiated)
766 for (p = avail_temp_slots; p; p = p->next)
768 if (p->align >= align && p->size >= size
769 && GET_MODE (p->slot) == mode
770 && objects_must_conflict_p (p->type, type)
771 && (best_p == 0 || best_p->size > p->size
772 || (best_p->size == p->size && best_p->align > p->align)))
774 if (p->align == align && p->size == size)
776 selected = p;
777 cut_slot_from_list (selected, &avail_temp_slots);
778 best_p = 0;
779 break;
781 best_p = p;
786 /* Make our best, if any, the one to use. */
787 if (best_p)
789 selected = best_p;
790 cut_slot_from_list (selected, &avail_temp_slots);
792 /* If there are enough aligned bytes left over, make them into a new
793 temp_slot so that the extra bytes don't get wasted. Do this only
794 for BLKmode slots, so that we can be sure of the alignment. */
795 if (GET_MODE (best_p->slot) == BLKmode)
797 int alignment = best_p->align / BITS_PER_UNIT;
798 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
800 if (best_p->size - rounded_size >= alignment)
802 p = ggc_alloc_temp_slot ();
803 p->in_use = 0;
804 p->size = best_p->size - rounded_size;
805 p->base_offset = best_p->base_offset + rounded_size;
806 p->full_size = best_p->full_size - rounded_size;
807 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
808 p->align = best_p->align;
809 p->type = best_p->type;
810 insert_slot_to_list (p, &avail_temp_slots);
812 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
813 stack_slot_list);
815 best_p->size = rounded_size;
816 best_p->full_size = rounded_size;
821 /* If we still didn't find one, make a new temporary. */
822 if (selected == 0)
824 HOST_WIDE_INT frame_offset_old = frame_offset;
826 p = ggc_alloc_temp_slot ();
828 /* We are passing an explicit alignment request to assign_stack_local.
829 One side effect of that is assign_stack_local will not round SIZE
830 to ensure the frame offset remains suitably aligned.
832 So for requests which depended on the rounding of SIZE, we go ahead
833 and round it now. We also make sure ALIGNMENT is at least
834 BIGGEST_ALIGNMENT. */
835 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
836 p->slot = assign_stack_local_1 (mode,
837 (mode == BLKmode
838 ? CEIL_ROUND (size,
839 (int) align
840 / BITS_PER_UNIT)
841 : size),
842 align, 0);
844 p->align = align;
846 /* The following slot size computation is necessary because we don't
847 know the actual size of the temporary slot until assign_stack_local
848 has performed all the frame alignment and size rounding for the
849 requested temporary. Note that extra space added for alignment
850 can be either above or below this stack slot depending on which
851 way the frame grows. We include the extra space if and only if it
852 is above this slot. */
853 if (FRAME_GROWS_DOWNWARD)
854 p->size = frame_offset_old - frame_offset;
855 else
856 p->size = size;
858 /* Now define the fields used by combine_temp_slots. */
859 if (FRAME_GROWS_DOWNWARD)
861 p->base_offset = frame_offset;
862 p->full_size = frame_offset_old - frame_offset;
864 else
866 p->base_offset = frame_offset_old;
867 p->full_size = frame_offset - frame_offset_old;
870 selected = p;
873 p = selected;
874 p->in_use = 1;
875 p->type = type;
876 p->level = temp_slot_level;
877 n_temp_slots_in_use++;
879 pp = temp_slots_at_level (p->level);
880 insert_slot_to_list (p, pp);
881 insert_temp_slot_address (XEXP (p->slot, 0), p);
883 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
884 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
885 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
887 /* If we know the alias set for the memory that will be used, use
888 it. If there's no TYPE, then we don't know anything about the
889 alias set for the memory. */
890 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
891 set_mem_align (slot, align);
893 /* If a type is specified, set the relevant flags. */
894 if (type != 0)
895 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
896 MEM_NOTRAP_P (slot) = 1;
898 return slot;
901 /* Allocate a temporary stack slot and record it for possible later
902 reuse. First two arguments are same as in preceding function. */
905 assign_stack_temp (enum machine_mode mode, HOST_WIDE_INT size)
907 return assign_stack_temp_for_type (mode, size, NULL_TREE);
910 /* Assign a temporary.
911 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
912 and so that should be used in error messages. In either case, we
913 allocate of the given type.
914 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
915 it is 0 if a register is OK.
916 DONT_PROMOTE is 1 if we should not promote values in register
917 to wider modes. */
920 assign_temp (tree type_or_decl, int memory_required,
921 int dont_promote ATTRIBUTE_UNUSED)
923 tree type, decl;
924 enum machine_mode mode;
925 #ifdef PROMOTE_MODE
926 int unsignedp;
927 #endif
929 if (DECL_P (type_or_decl))
930 decl = type_or_decl, type = TREE_TYPE (decl);
931 else
932 decl = NULL, type = type_or_decl;
934 mode = TYPE_MODE (type);
935 #ifdef PROMOTE_MODE
936 unsignedp = TYPE_UNSIGNED (type);
937 #endif
939 if (mode == BLKmode || memory_required)
941 HOST_WIDE_INT size = int_size_in_bytes (type);
942 rtx tmp;
944 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
945 problems with allocating the stack space. */
946 if (size == 0)
947 size = 1;
949 /* Unfortunately, we don't yet know how to allocate variable-sized
950 temporaries. However, sometimes we can find a fixed upper limit on
951 the size, so try that instead. */
952 else if (size == -1)
953 size = max_int_size_in_bytes (type);
955 /* The size of the temporary may be too large to fit into an integer. */
956 /* ??? Not sure this should happen except for user silliness, so limit
957 this to things that aren't compiler-generated temporaries. The
958 rest of the time we'll die in assign_stack_temp_for_type. */
959 if (decl && size == -1
960 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
962 error ("size of variable %q+D is too large", decl);
963 size = 1;
966 tmp = assign_stack_temp_for_type (mode, size, type);
967 return tmp;
970 #ifdef PROMOTE_MODE
971 if (! dont_promote)
972 mode = promote_mode (type, mode, &unsignedp);
973 #endif
975 return gen_reg_rtx (mode);
978 /* Combine temporary stack slots which are adjacent on the stack.
980 This allows for better use of already allocated stack space. This is only
981 done for BLKmode slots because we can be sure that we won't have alignment
982 problems in this case. */
984 static void
985 combine_temp_slots (void)
987 struct temp_slot *p, *q, *next, *next_q;
988 int num_slots;
990 /* We can't combine slots, because the information about which slot
991 is in which alias set will be lost. */
992 if (flag_strict_aliasing)
993 return;
995 /* If there are a lot of temp slots, don't do anything unless
996 high levels of optimization. */
997 if (! flag_expensive_optimizations)
998 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
999 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1000 return;
1002 for (p = avail_temp_slots; p; p = next)
1004 int delete_p = 0;
1006 next = p->next;
1008 if (GET_MODE (p->slot) != BLKmode)
1009 continue;
1011 for (q = p->next; q; q = next_q)
1013 int delete_q = 0;
1015 next_q = q->next;
1017 if (GET_MODE (q->slot) != BLKmode)
1018 continue;
1020 if (p->base_offset + p->full_size == q->base_offset)
1022 /* Q comes after P; combine Q into P. */
1023 p->size += q->size;
1024 p->full_size += q->full_size;
1025 delete_q = 1;
1027 else if (q->base_offset + q->full_size == p->base_offset)
1029 /* P comes after Q; combine P into Q. */
1030 q->size += p->size;
1031 q->full_size += p->full_size;
1032 delete_p = 1;
1033 break;
1035 if (delete_q)
1036 cut_slot_from_list (q, &avail_temp_slots);
1039 /* Either delete P or advance past it. */
1040 if (delete_p)
1041 cut_slot_from_list (p, &avail_temp_slots);
1045 /* Indicate that NEW_RTX is an alternate way of referring to the temp
1046 slot that previously was known by OLD_RTX. */
1048 void
1049 update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1051 struct temp_slot *p;
1053 if (rtx_equal_p (old_rtx, new_rtx))
1054 return;
1056 p = find_temp_slot_from_address (old_rtx);
1058 /* If we didn't find one, see if both OLD_RTX is a PLUS. If so, and
1059 NEW_RTX is a register, see if one operand of the PLUS is a
1060 temporary location. If so, NEW_RTX points into it. Otherwise,
1061 if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1062 in common between them. If so, try a recursive call on those
1063 values. */
1064 if (p == 0)
1066 if (GET_CODE (old_rtx) != PLUS)
1067 return;
1069 if (REG_P (new_rtx))
1071 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1072 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1073 return;
1075 else if (GET_CODE (new_rtx) != PLUS)
1076 return;
1078 if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1079 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1080 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1081 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1082 else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1083 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1084 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1085 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
1087 return;
1090 /* Otherwise add an alias for the temp's address. */
1091 insert_temp_slot_address (new_rtx, p);
1094 /* If X could be a reference to a temporary slot, mark that slot as
1095 belonging to the to one level higher than the current level. If X
1096 matched one of our slots, just mark that one. Otherwise, we can't
1097 easily predict which it is, so upgrade all of them.
1099 This is called when an ({...}) construct occurs and a statement
1100 returns a value in memory. */
1102 void
1103 preserve_temp_slots (rtx x)
1105 struct temp_slot *p = 0, *next;
1107 if (x == 0)
1108 return;
1110 /* If X is a register that is being used as a pointer, see if we have
1111 a temporary slot we know it points to. */
1112 if (REG_P (x) && REG_POINTER (x))
1113 p = find_temp_slot_from_address (x);
1115 /* If X is not in memory or is at a constant address, it cannot be in
1116 a temporary slot. */
1117 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1118 return;
1120 /* First see if we can find a match. */
1121 if (p == 0)
1122 p = find_temp_slot_from_address (XEXP (x, 0));
1124 if (p != 0)
1126 if (p->level == temp_slot_level)
1127 move_slot_to_level (p, temp_slot_level - 1);
1128 return;
1131 /* Otherwise, preserve all non-kept slots at this level. */
1132 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1134 next = p->next;
1135 move_slot_to_level (p, temp_slot_level - 1);
1139 /* Free all temporaries used so far. This is normally called at the
1140 end of generating code for a statement. */
1142 void
1143 free_temp_slots (void)
1145 struct temp_slot *p, *next;
1146 bool some_available = false;
1148 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1150 next = p->next;
1151 make_slot_available (p);
1152 some_available = true;
1155 if (some_available)
1157 remove_unused_temp_slot_addresses ();
1158 combine_temp_slots ();
1162 /* Push deeper into the nesting level for stack temporaries. */
1164 void
1165 push_temp_slots (void)
1167 temp_slot_level++;
1170 /* Pop a temporary nesting level. All slots in use in the current level
1171 are freed. */
1173 void
1174 pop_temp_slots (void)
1176 free_temp_slots ();
1177 temp_slot_level--;
1180 /* Initialize temporary slots. */
1182 void
1183 init_temp_slots (void)
1185 /* We have not allocated any temporaries yet. */
1186 avail_temp_slots = 0;
1187 vec_alloc (used_temp_slots, 0);
1188 temp_slot_level = 0;
1189 n_temp_slots_in_use = 0;
1191 /* Set up the table to map addresses to temp slots. */
1192 if (! temp_slot_address_table)
1193 temp_slot_address_table = htab_create_ggc (32,
1194 temp_slot_address_hash,
1195 temp_slot_address_eq,
1196 NULL);
1197 else
1198 htab_empty (temp_slot_address_table);
1201 /* Functions and data structures to keep track of the values hard regs
1202 had at the start of the function. */
1204 /* Private type used by get_hard_reg_initial_reg, get_hard_reg_initial_val,
1205 and has_hard_reg_initial_val.. */
1206 typedef struct GTY(()) initial_value_pair {
1207 rtx hard_reg;
1208 rtx pseudo;
1209 } initial_value_pair;
1210 /* ??? This could be a VEC but there is currently no way to define an
1211 opaque VEC type. This could be worked around by defining struct
1212 initial_value_pair in function.h. */
1213 typedef struct GTY(()) initial_value_struct {
1214 int num_entries;
1215 int max_entries;
1216 initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
1217 } initial_value_struct;
1219 /* If a pseudo represents an initial hard reg (or expression), return
1220 it, else return NULL_RTX. */
1223 get_hard_reg_initial_reg (rtx reg)
1225 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1226 int i;
1228 if (ivs == 0)
1229 return NULL_RTX;
1231 for (i = 0; i < ivs->num_entries; i++)
1232 if (rtx_equal_p (ivs->entries[i].pseudo, reg))
1233 return ivs->entries[i].hard_reg;
1235 return NULL_RTX;
1238 /* Make sure that there's a pseudo register of mode MODE that stores the
1239 initial value of hard register REGNO. Return an rtx for such a pseudo. */
1242 get_hard_reg_initial_val (enum machine_mode mode, unsigned int regno)
1244 struct initial_value_struct *ivs;
1245 rtx rv;
1247 rv = has_hard_reg_initial_val (mode, regno);
1248 if (rv)
1249 return rv;
1251 ivs = crtl->hard_reg_initial_vals;
1252 if (ivs == 0)
1254 ivs = ggc_alloc_initial_value_struct ();
1255 ivs->num_entries = 0;
1256 ivs->max_entries = 5;
1257 ivs->entries = ggc_alloc_vec_initial_value_pair (5);
1258 crtl->hard_reg_initial_vals = ivs;
1261 if (ivs->num_entries >= ivs->max_entries)
1263 ivs->max_entries += 5;
1264 ivs->entries = GGC_RESIZEVEC (initial_value_pair, ivs->entries,
1265 ivs->max_entries);
1268 ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
1269 ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
1271 return ivs->entries[ivs->num_entries++].pseudo;
1274 /* See if get_hard_reg_initial_val has been used to create a pseudo
1275 for the initial value of hard register REGNO in mode MODE. Return
1276 the associated pseudo if so, otherwise return NULL. */
1279 has_hard_reg_initial_val (enum machine_mode mode, unsigned int regno)
1281 struct initial_value_struct *ivs;
1282 int i;
1284 ivs = crtl->hard_reg_initial_vals;
1285 if (ivs != 0)
1286 for (i = 0; i < ivs->num_entries; i++)
1287 if (GET_MODE (ivs->entries[i].hard_reg) == mode
1288 && REGNO (ivs->entries[i].hard_reg) == regno)
1289 return ivs->entries[i].pseudo;
1291 return NULL_RTX;
1294 unsigned int
1295 emit_initial_value_sets (void)
1297 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1298 int i;
1299 rtx seq;
1301 if (ivs == 0)
1302 return 0;
1304 start_sequence ();
1305 for (i = 0; i < ivs->num_entries; i++)
1306 emit_move_insn (ivs->entries[i].pseudo, ivs->entries[i].hard_reg);
1307 seq = get_insns ();
1308 end_sequence ();
1310 emit_insn_at_entry (seq);
1311 return 0;
1314 /* Return the hardreg-pseudoreg initial values pair entry I and
1315 TRUE if I is a valid entry, or FALSE if I is not a valid entry. */
1316 bool
1317 initial_value_entry (int i, rtx *hreg, rtx *preg)
1319 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1320 if (!ivs || i >= ivs->num_entries)
1321 return false;
1323 *hreg = ivs->entries[i].hard_reg;
1324 *preg = ivs->entries[i].pseudo;
1325 return true;
1328 /* These routines are responsible for converting virtual register references
1329 to the actual hard register references once RTL generation is complete.
1331 The following four variables are used for communication between the
1332 routines. They contain the offsets of the virtual registers from their
1333 respective hard registers. */
1335 static int in_arg_offset;
1336 static int var_offset;
1337 static int dynamic_offset;
1338 static int out_arg_offset;
1339 static int cfa_offset;
1341 /* In most machines, the stack pointer register is equivalent to the bottom
1342 of the stack. */
1344 #ifndef STACK_POINTER_OFFSET
1345 #define STACK_POINTER_OFFSET 0
1346 #endif
1348 /* If not defined, pick an appropriate default for the offset of dynamically
1349 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1350 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1352 #ifndef STACK_DYNAMIC_OFFSET
1354 /* The bottom of the stack points to the actual arguments. If
1355 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1356 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1357 stack space for register parameters is not pushed by the caller, but
1358 rather part of the fixed stack areas and hence not included in
1359 `crtl->outgoing_args_size'. Nevertheless, we must allow
1360 for it when allocating stack dynamic objects. */
1362 #if defined(REG_PARM_STACK_SPACE)
1363 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1364 ((ACCUMULATE_OUTGOING_ARGS \
1365 ? (crtl->outgoing_args_size \
1366 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1367 : REG_PARM_STACK_SPACE (FNDECL))) \
1368 : 0) + (STACK_POINTER_OFFSET))
1369 #else
1370 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1371 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0) \
1372 + (STACK_POINTER_OFFSET))
1373 #endif
1374 #endif
1377 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1378 is a virtual register, return the equivalent hard register and set the
1379 offset indirectly through the pointer. Otherwise, return 0. */
1381 static rtx
1382 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
1384 rtx new_rtx;
1385 HOST_WIDE_INT offset;
1387 if (x == virtual_incoming_args_rtx)
1389 if (stack_realign_drap)
1391 /* Replace virtual_incoming_args_rtx with internal arg
1392 pointer if DRAP is used to realign stack. */
1393 new_rtx = crtl->args.internal_arg_pointer;
1394 offset = 0;
1396 else
1397 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1399 else if (x == virtual_stack_vars_rtx)
1400 new_rtx = frame_pointer_rtx, offset = var_offset;
1401 else if (x == virtual_stack_dynamic_rtx)
1402 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1403 else if (x == virtual_outgoing_args_rtx)
1404 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1405 else if (x == virtual_cfa_rtx)
1407 #ifdef FRAME_POINTER_CFA_OFFSET
1408 new_rtx = frame_pointer_rtx;
1409 #else
1410 new_rtx = arg_pointer_rtx;
1411 #endif
1412 offset = cfa_offset;
1414 else if (x == virtual_preferred_stack_boundary_rtx)
1416 new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1417 offset = 0;
1419 else
1420 return NULL_RTX;
1422 *poffset = offset;
1423 return new_rtx;
1426 /* A subroutine of instantiate_virtual_regs, called via for_each_rtx.
1427 Instantiate any virtual registers present inside of *LOC. The expression
1428 is simplified, as much as possible, but is not to be considered "valid"
1429 in any sense implied by the target. If any change is made, set CHANGED
1430 to true. */
1432 static int
1433 instantiate_virtual_regs_in_rtx (rtx *loc, void *data)
1435 HOST_WIDE_INT offset;
1436 bool *changed = (bool *) data;
1437 rtx x, new_rtx;
1439 x = *loc;
1440 if (x == 0)
1441 return 0;
1443 switch (GET_CODE (x))
1445 case REG:
1446 new_rtx = instantiate_new_reg (x, &offset);
1447 if (new_rtx)
1449 *loc = plus_constant (GET_MODE (x), new_rtx, offset);
1450 if (changed)
1451 *changed = true;
1453 return -1;
1455 case PLUS:
1456 new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1457 if (new_rtx)
1459 new_rtx = plus_constant (GET_MODE (x), new_rtx, offset);
1460 *loc = simplify_gen_binary (PLUS, GET_MODE (x), new_rtx, XEXP (x, 1));
1461 if (changed)
1462 *changed = true;
1463 return -1;
1466 /* FIXME -- from old code */
1467 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1468 we can commute the PLUS and SUBREG because pointers into the
1469 frame are well-behaved. */
1470 break;
1472 default:
1473 break;
1476 return 0;
1479 /* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1480 matches the predicate for insn CODE operand OPERAND. */
1482 static int
1483 safe_insn_predicate (int code, int operand, rtx x)
1485 return code < 0 || insn_operand_matches ((enum insn_code) code, operand, x);
1488 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1489 registers present inside of insn. The result will be a valid insn. */
1491 static void
1492 instantiate_virtual_regs_in_insn (rtx insn)
1494 HOST_WIDE_INT offset;
1495 int insn_code, i;
1496 bool any_change = false;
1497 rtx set, new_rtx, x, seq;
1499 /* There are some special cases to be handled first. */
1500 set = single_set (insn);
1501 if (set)
1503 /* We're allowed to assign to a virtual register. This is interpreted
1504 to mean that the underlying register gets assigned the inverse
1505 transformation. This is used, for example, in the handling of
1506 non-local gotos. */
1507 new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1508 if (new_rtx)
1510 start_sequence ();
1512 for_each_rtx (&SET_SRC (set), instantiate_virtual_regs_in_rtx, NULL);
1513 x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
1514 gen_int_mode (-offset, GET_MODE (new_rtx)));
1515 x = force_operand (x, new_rtx);
1516 if (x != new_rtx)
1517 emit_move_insn (new_rtx, x);
1519 seq = get_insns ();
1520 end_sequence ();
1522 emit_insn_before (seq, insn);
1523 delete_insn (insn);
1524 return;
1527 /* Handle a straight copy from a virtual register by generating a
1528 new add insn. The difference between this and falling through
1529 to the generic case is avoiding a new pseudo and eliminating a
1530 move insn in the initial rtl stream. */
1531 new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1532 if (new_rtx && offset != 0
1533 && REG_P (SET_DEST (set))
1534 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1536 start_sequence ();
1538 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS, new_rtx,
1539 gen_int_mode (offset,
1540 GET_MODE (SET_DEST (set))),
1541 SET_DEST (set), 1, OPTAB_LIB_WIDEN);
1542 if (x != SET_DEST (set))
1543 emit_move_insn (SET_DEST (set), x);
1545 seq = get_insns ();
1546 end_sequence ();
1548 emit_insn_before (seq, insn);
1549 delete_insn (insn);
1550 return;
1553 extract_insn (insn);
1554 insn_code = INSN_CODE (insn);
1556 /* Handle a plus involving a virtual register by determining if the
1557 operands remain valid if they're modified in place. */
1558 if (GET_CODE (SET_SRC (set)) == PLUS
1559 && recog_data.n_operands >= 3
1560 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1561 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1562 && CONST_INT_P (recog_data.operand[2])
1563 && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
1565 offset += INTVAL (recog_data.operand[2]);
1567 /* If the sum is zero, then replace with a plain move. */
1568 if (offset == 0
1569 && REG_P (SET_DEST (set))
1570 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1572 start_sequence ();
1573 emit_move_insn (SET_DEST (set), new_rtx);
1574 seq = get_insns ();
1575 end_sequence ();
1577 emit_insn_before (seq, insn);
1578 delete_insn (insn);
1579 return;
1582 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1584 /* Using validate_change and apply_change_group here leaves
1585 recog_data in an invalid state. Since we know exactly what
1586 we want to check, do those two by hand. */
1587 if (safe_insn_predicate (insn_code, 1, new_rtx)
1588 && safe_insn_predicate (insn_code, 2, x))
1590 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1591 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1592 any_change = true;
1594 /* Fall through into the regular operand fixup loop in
1595 order to take care of operands other than 1 and 2. */
1599 else
1601 extract_insn (insn);
1602 insn_code = INSN_CODE (insn);
1605 /* In the general case, we expect virtual registers to appear only in
1606 operands, and then only as either bare registers or inside memories. */
1607 for (i = 0; i < recog_data.n_operands; ++i)
1609 x = recog_data.operand[i];
1610 switch (GET_CODE (x))
1612 case MEM:
1614 rtx addr = XEXP (x, 0);
1615 bool changed = false;
1617 for_each_rtx (&addr, instantiate_virtual_regs_in_rtx, &changed);
1618 if (!changed)
1619 continue;
1621 start_sequence ();
1622 x = replace_equiv_address (x, addr);
1623 /* It may happen that the address with the virtual reg
1624 was valid (e.g. based on the virtual stack reg, which might
1625 be acceptable to the predicates with all offsets), whereas
1626 the address now isn't anymore, for instance when the address
1627 is still offsetted, but the base reg isn't virtual-stack-reg
1628 anymore. Below we would do a force_reg on the whole operand,
1629 but this insn might actually only accept memory. Hence,
1630 before doing that last resort, try to reload the address into
1631 a register, so this operand stays a MEM. */
1632 if (!safe_insn_predicate (insn_code, i, x))
1634 addr = force_reg (GET_MODE (addr), addr);
1635 x = replace_equiv_address (x, addr);
1637 seq = get_insns ();
1638 end_sequence ();
1639 if (seq)
1640 emit_insn_before (seq, insn);
1642 break;
1644 case REG:
1645 new_rtx = instantiate_new_reg (x, &offset);
1646 if (new_rtx == NULL)
1647 continue;
1648 if (offset == 0)
1649 x = new_rtx;
1650 else
1652 start_sequence ();
1654 /* Careful, special mode predicates may have stuff in
1655 insn_data[insn_code].operand[i].mode that isn't useful
1656 to us for computing a new value. */
1657 /* ??? Recognize address_operand and/or "p" constraints
1658 to see if (plus new offset) is a valid before we put
1659 this through expand_simple_binop. */
1660 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1661 gen_int_mode (offset, GET_MODE (x)),
1662 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1663 seq = get_insns ();
1664 end_sequence ();
1665 emit_insn_before (seq, insn);
1667 break;
1669 case SUBREG:
1670 new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1671 if (new_rtx == NULL)
1672 continue;
1673 if (offset != 0)
1675 start_sequence ();
1676 new_rtx = expand_simple_binop
1677 (GET_MODE (new_rtx), PLUS, new_rtx,
1678 gen_int_mode (offset, GET_MODE (new_rtx)),
1679 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1680 seq = get_insns ();
1681 end_sequence ();
1682 emit_insn_before (seq, insn);
1684 x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1685 GET_MODE (new_rtx), SUBREG_BYTE (x));
1686 gcc_assert (x);
1687 break;
1689 default:
1690 continue;
1693 /* At this point, X contains the new value for the operand.
1694 Validate the new value vs the insn predicate. Note that
1695 asm insns will have insn_code -1 here. */
1696 if (!safe_insn_predicate (insn_code, i, x))
1698 start_sequence ();
1699 if (REG_P (x))
1701 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1702 x = copy_to_reg (x);
1704 else
1705 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1706 seq = get_insns ();
1707 end_sequence ();
1708 if (seq)
1709 emit_insn_before (seq, insn);
1712 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1713 any_change = true;
1716 if (any_change)
1718 /* Propagate operand changes into the duplicates. */
1719 for (i = 0; i < recog_data.n_dups; ++i)
1720 *recog_data.dup_loc[i]
1721 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1723 /* Force re-recognition of the instruction for validation. */
1724 INSN_CODE (insn) = -1;
1727 if (asm_noperands (PATTERN (insn)) >= 0)
1729 if (!check_asm_operands (PATTERN (insn)))
1731 error_for_asm (insn, "impossible constraint in %<asm%>");
1732 /* For asm goto, instead of fixing up all the edges
1733 just clear the template and clear input operands
1734 (asm goto doesn't have any output operands). */
1735 if (JUMP_P (insn))
1737 rtx asm_op = extract_asm_operands (PATTERN (insn));
1738 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1739 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1740 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1742 else
1743 delete_insn (insn);
1746 else
1748 if (recog_memoized (insn) < 0)
1749 fatal_insn_not_found (insn);
1753 /* Subroutine of instantiate_decls. Given RTL representing a decl,
1754 do any instantiation required. */
1756 void
1757 instantiate_decl_rtl (rtx x)
1759 rtx addr;
1761 if (x == 0)
1762 return;
1764 /* If this is a CONCAT, recurse for the pieces. */
1765 if (GET_CODE (x) == CONCAT)
1767 instantiate_decl_rtl (XEXP (x, 0));
1768 instantiate_decl_rtl (XEXP (x, 1));
1769 return;
1772 /* If this is not a MEM, no need to do anything. Similarly if the
1773 address is a constant or a register that is not a virtual register. */
1774 if (!MEM_P (x))
1775 return;
1777 addr = XEXP (x, 0);
1778 if (CONSTANT_P (addr)
1779 || (REG_P (addr)
1780 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1781 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1782 return;
1784 for_each_rtx (&XEXP (x, 0), instantiate_virtual_regs_in_rtx, NULL);
1787 /* Helper for instantiate_decls called via walk_tree: Process all decls
1788 in the given DECL_VALUE_EXPR. */
1790 static tree
1791 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1793 tree t = *tp;
1794 if (! EXPR_P (t))
1796 *walk_subtrees = 0;
1797 if (DECL_P (t))
1799 if (DECL_RTL_SET_P (t))
1800 instantiate_decl_rtl (DECL_RTL (t));
1801 if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1802 && DECL_INCOMING_RTL (t))
1803 instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1804 if ((TREE_CODE (t) == VAR_DECL
1805 || TREE_CODE (t) == RESULT_DECL)
1806 && DECL_HAS_VALUE_EXPR_P (t))
1808 tree v = DECL_VALUE_EXPR (t);
1809 walk_tree (&v, instantiate_expr, NULL, NULL);
1813 return NULL;
1816 /* Subroutine of instantiate_decls: Process all decls in the given
1817 BLOCK node and all its subblocks. */
1819 static void
1820 instantiate_decls_1 (tree let)
1822 tree t;
1824 for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1826 if (DECL_RTL_SET_P (t))
1827 instantiate_decl_rtl (DECL_RTL (t));
1828 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t))
1830 tree v = DECL_VALUE_EXPR (t);
1831 walk_tree (&v, instantiate_expr, NULL, NULL);
1835 /* Process all subblocks. */
1836 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1837 instantiate_decls_1 (t);
1840 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1841 all virtual registers in their DECL_RTL's. */
1843 static void
1844 instantiate_decls (tree fndecl)
1846 tree decl;
1847 unsigned ix;
1849 /* Process all parameters of the function. */
1850 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1852 instantiate_decl_rtl (DECL_RTL (decl));
1853 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1854 if (DECL_HAS_VALUE_EXPR_P (decl))
1856 tree v = DECL_VALUE_EXPR (decl);
1857 walk_tree (&v, instantiate_expr, NULL, NULL);
1861 if ((decl = DECL_RESULT (fndecl))
1862 && TREE_CODE (decl) == RESULT_DECL)
1864 if (DECL_RTL_SET_P (decl))
1865 instantiate_decl_rtl (DECL_RTL (decl));
1866 if (DECL_HAS_VALUE_EXPR_P (decl))
1868 tree v = DECL_VALUE_EXPR (decl);
1869 walk_tree (&v, instantiate_expr, NULL, NULL);
1873 /* Now process all variables defined in the function or its subblocks. */
1874 instantiate_decls_1 (DECL_INITIAL (fndecl));
1876 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1877 if (DECL_RTL_SET_P (decl))
1878 instantiate_decl_rtl (DECL_RTL (decl));
1879 vec_free (cfun->local_decls);
1882 /* Pass through the INSNS of function FNDECL and convert virtual register
1883 references to hard register references. */
1885 static unsigned int
1886 instantiate_virtual_regs (void)
1888 rtx insn;
1890 /* Compute the offsets to use for this function. */
1891 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1892 var_offset = STARTING_FRAME_OFFSET;
1893 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1894 out_arg_offset = STACK_POINTER_OFFSET;
1895 #ifdef FRAME_POINTER_CFA_OFFSET
1896 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1897 #else
1898 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1899 #endif
1901 /* Initialize recognition, indicating that volatile is OK. */
1902 init_recog ();
1904 /* Scan through all the insns, instantiating every virtual register still
1905 present. */
1906 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1907 if (INSN_P (insn))
1909 /* These patterns in the instruction stream can never be recognized.
1910 Fortunately, they shouldn't contain virtual registers either. */
1911 if (GET_CODE (PATTERN (insn)) == USE
1912 || GET_CODE (PATTERN (insn)) == CLOBBER
1913 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
1914 continue;
1915 else if (DEBUG_INSN_P (insn))
1916 for_each_rtx (&INSN_VAR_LOCATION (insn),
1917 instantiate_virtual_regs_in_rtx, NULL);
1918 else
1919 instantiate_virtual_regs_in_insn (insn);
1921 if (INSN_DELETED_P (insn))
1922 continue;
1924 for_each_rtx (&REG_NOTES (insn), instantiate_virtual_regs_in_rtx, NULL);
1926 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
1927 if (CALL_P (insn))
1928 for_each_rtx (&CALL_INSN_FUNCTION_USAGE (insn),
1929 instantiate_virtual_regs_in_rtx, NULL);
1932 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
1933 instantiate_decls (current_function_decl);
1935 targetm.instantiate_decls ();
1937 /* Indicate that, from now on, assign_stack_local should use
1938 frame_pointer_rtx. */
1939 virtuals_instantiated = 1;
1941 return 0;
1944 namespace {
1946 const pass_data pass_data_instantiate_virtual_regs =
1948 RTL_PASS, /* type */
1949 "vregs", /* name */
1950 OPTGROUP_NONE, /* optinfo_flags */
1951 false, /* has_gate */
1952 true, /* has_execute */
1953 TV_NONE, /* tv_id */
1954 0, /* properties_required */
1955 0, /* properties_provided */
1956 0, /* properties_destroyed */
1957 0, /* todo_flags_start */
1958 0, /* todo_flags_finish */
1961 class pass_instantiate_virtual_regs : public rtl_opt_pass
1963 public:
1964 pass_instantiate_virtual_regs (gcc::context *ctxt)
1965 : rtl_opt_pass (pass_data_instantiate_virtual_regs, ctxt)
1968 /* opt_pass methods: */
1969 unsigned int execute () { return instantiate_virtual_regs (); }
1971 }; // class pass_instantiate_virtual_regs
1973 } // anon namespace
1975 rtl_opt_pass *
1976 make_pass_instantiate_virtual_regs (gcc::context *ctxt)
1978 return new pass_instantiate_virtual_regs (ctxt);
1982 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
1983 This means a type for which function calls must pass an address to the
1984 function or get an address back from the function.
1985 EXP may be a type node or an expression (whose type is tested). */
1988 aggregate_value_p (const_tree exp, const_tree fntype)
1990 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
1991 int i, regno, nregs;
1992 rtx reg;
1994 if (fntype)
1995 switch (TREE_CODE (fntype))
1997 case CALL_EXPR:
1999 tree fndecl = get_callee_fndecl (fntype);
2000 fntype = (fndecl
2001 ? TREE_TYPE (fndecl)
2002 : TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype))));
2004 break;
2005 case FUNCTION_DECL:
2006 fntype = TREE_TYPE (fntype);
2007 break;
2008 case FUNCTION_TYPE:
2009 case METHOD_TYPE:
2010 break;
2011 case IDENTIFIER_NODE:
2012 fntype = NULL_TREE;
2013 break;
2014 default:
2015 /* We don't expect other tree types here. */
2016 gcc_unreachable ();
2019 if (VOID_TYPE_P (type))
2020 return 0;
2022 /* If a record should be passed the same as its first (and only) member
2023 don't pass it as an aggregate. */
2024 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2025 return aggregate_value_p (first_field (type), fntype);
2027 /* If the front end has decided that this needs to be passed by
2028 reference, do so. */
2029 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2030 && DECL_BY_REFERENCE (exp))
2031 return 1;
2033 /* Function types that are TREE_ADDRESSABLE force return in memory. */
2034 if (fntype && TREE_ADDRESSABLE (fntype))
2035 return 1;
2037 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2038 and thus can't be returned in registers. */
2039 if (TREE_ADDRESSABLE (type))
2040 return 1;
2042 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2043 return 1;
2045 if (targetm.calls.return_in_memory (type, fntype))
2046 return 1;
2048 /* Make sure we have suitable call-clobbered regs to return
2049 the value in; if not, we must return it in memory. */
2050 reg = hard_function_value (type, 0, fntype, 0);
2052 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2053 it is OK. */
2054 if (!REG_P (reg))
2055 return 0;
2057 regno = REGNO (reg);
2058 nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
2059 for (i = 0; i < nregs; i++)
2060 if (! call_used_regs[regno + i])
2061 return 1;
2063 return 0;
2066 /* Return true if we should assign DECL a pseudo register; false if it
2067 should live on the local stack. */
2069 bool
2070 use_register_for_decl (const_tree decl)
2072 if (!targetm.calls.allocate_stack_slots_for_args ())
2073 return true;
2075 /* Honor volatile. */
2076 if (TREE_SIDE_EFFECTS (decl))
2077 return false;
2079 /* Honor addressability. */
2080 if (TREE_ADDRESSABLE (decl))
2081 return false;
2083 /* Only register-like things go in registers. */
2084 if (DECL_MODE (decl) == BLKmode)
2085 return false;
2087 /* If -ffloat-store specified, don't put explicit float variables
2088 into registers. */
2089 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2090 propagates values across these stores, and it probably shouldn't. */
2091 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2092 return false;
2094 /* If we're not interested in tracking debugging information for
2095 this decl, then we can certainly put it in a register. */
2096 if (DECL_IGNORED_P (decl))
2097 return true;
2099 if (optimize)
2100 return true;
2102 if (!DECL_REGISTER (decl))
2103 return false;
2105 switch (TREE_CODE (TREE_TYPE (decl)))
2107 case RECORD_TYPE:
2108 case UNION_TYPE:
2109 case QUAL_UNION_TYPE:
2110 /* When not optimizing, disregard register keyword for variables with
2111 types containing methods, otherwise the methods won't be callable
2112 from the debugger. */
2113 if (TYPE_METHODS (TREE_TYPE (decl)))
2114 return false;
2115 break;
2116 default:
2117 break;
2120 return true;
2123 /* Return true if TYPE should be passed by invisible reference. */
2125 bool
2126 pass_by_reference (CUMULATIVE_ARGS *ca, enum machine_mode mode,
2127 tree type, bool named_arg)
2129 if (type)
2131 /* If this type contains non-trivial constructors, then it is
2132 forbidden for the middle-end to create any new copies. */
2133 if (TREE_ADDRESSABLE (type))
2134 return true;
2136 /* GCC post 3.4 passes *all* variable sized types by reference. */
2137 if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2138 return true;
2140 /* If a record type should be passed the same as its first (and only)
2141 member, use the type and mode of that member. */
2142 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2144 type = TREE_TYPE (first_field (type));
2145 mode = TYPE_MODE (type);
2149 return targetm.calls.pass_by_reference (pack_cumulative_args (ca), mode,
2150 type, named_arg);
2153 /* Return true if TYPE, which is passed by reference, should be callee
2154 copied instead of caller copied. */
2156 bool
2157 reference_callee_copied (CUMULATIVE_ARGS *ca, enum machine_mode mode,
2158 tree type, bool named_arg)
2160 if (type && TREE_ADDRESSABLE (type))
2161 return false;
2162 return targetm.calls.callee_copies (pack_cumulative_args (ca), mode, type,
2163 named_arg);
2166 /* Structures to communicate between the subroutines of assign_parms.
2167 The first holds data persistent across all parameters, the second
2168 is cleared out for each parameter. */
2170 struct assign_parm_data_all
2172 /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2173 should become a job of the target or otherwise encapsulated. */
2174 CUMULATIVE_ARGS args_so_far_v;
2175 cumulative_args_t args_so_far;
2176 struct args_size stack_args_size;
2177 tree function_result_decl;
2178 tree orig_fnargs;
2179 rtx first_conversion_insn;
2180 rtx last_conversion_insn;
2181 HOST_WIDE_INT pretend_args_size;
2182 HOST_WIDE_INT extra_pretend_bytes;
2183 int reg_parm_stack_space;
2186 struct assign_parm_data_one
2188 tree nominal_type;
2189 tree passed_type;
2190 rtx entry_parm;
2191 rtx stack_parm;
2192 enum machine_mode nominal_mode;
2193 enum machine_mode passed_mode;
2194 enum machine_mode promoted_mode;
2195 struct locate_and_pad_arg_data locate;
2196 int partial;
2197 BOOL_BITFIELD named_arg : 1;
2198 BOOL_BITFIELD passed_pointer : 1;
2199 BOOL_BITFIELD on_stack : 1;
2200 BOOL_BITFIELD loaded_in_reg : 1;
2203 /* A subroutine of assign_parms. Initialize ALL. */
2205 static void
2206 assign_parms_initialize_all (struct assign_parm_data_all *all)
2208 tree fntype ATTRIBUTE_UNUSED;
2210 memset (all, 0, sizeof (*all));
2212 fntype = TREE_TYPE (current_function_decl);
2214 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
2215 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2216 #else
2217 INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2218 current_function_decl, -1);
2219 #endif
2220 all->args_so_far = pack_cumulative_args (&all->args_so_far_v);
2222 #ifdef REG_PARM_STACK_SPACE
2223 all->reg_parm_stack_space = REG_PARM_STACK_SPACE (current_function_decl);
2224 #endif
2227 /* If ARGS contains entries with complex types, split the entry into two
2228 entries of the component type. Return a new list of substitutions are
2229 needed, else the old list. */
2231 static void
2232 split_complex_args (vec<tree> *args)
2234 unsigned i;
2235 tree p;
2237 FOR_EACH_VEC_ELT (*args, i, p)
2239 tree type = TREE_TYPE (p);
2240 if (TREE_CODE (type) == COMPLEX_TYPE
2241 && targetm.calls.split_complex_arg (type))
2243 tree decl;
2244 tree subtype = TREE_TYPE (type);
2245 bool addressable = TREE_ADDRESSABLE (p);
2247 /* Rewrite the PARM_DECL's type with its component. */
2248 p = copy_node (p);
2249 TREE_TYPE (p) = subtype;
2250 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2251 DECL_MODE (p) = VOIDmode;
2252 DECL_SIZE (p) = NULL;
2253 DECL_SIZE_UNIT (p) = NULL;
2254 /* If this arg must go in memory, put it in a pseudo here.
2255 We can't allow it to go in memory as per normal parms,
2256 because the usual place might not have the imag part
2257 adjacent to the real part. */
2258 DECL_ARTIFICIAL (p) = addressable;
2259 DECL_IGNORED_P (p) = addressable;
2260 TREE_ADDRESSABLE (p) = 0;
2261 layout_decl (p, 0);
2262 (*args)[i] = p;
2264 /* Build a second synthetic decl. */
2265 decl = build_decl (EXPR_LOCATION (p),
2266 PARM_DECL, NULL_TREE, subtype);
2267 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2268 DECL_ARTIFICIAL (decl) = addressable;
2269 DECL_IGNORED_P (decl) = addressable;
2270 layout_decl (decl, 0);
2271 args->safe_insert (++i, decl);
2276 /* A subroutine of assign_parms. Adjust the parameter list to incorporate
2277 the hidden struct return argument, and (abi willing) complex args.
2278 Return the new parameter list. */
2280 static vec<tree>
2281 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2283 tree fndecl = current_function_decl;
2284 tree fntype = TREE_TYPE (fndecl);
2285 vec<tree> fnargs = vNULL;
2286 tree arg;
2288 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2289 fnargs.safe_push (arg);
2291 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2293 /* If struct value address is treated as the first argument, make it so. */
2294 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2295 && ! cfun->returns_pcc_struct
2296 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2298 tree type = build_pointer_type (TREE_TYPE (fntype));
2299 tree decl;
2301 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2302 PARM_DECL, get_identifier (".result_ptr"), type);
2303 DECL_ARG_TYPE (decl) = type;
2304 DECL_ARTIFICIAL (decl) = 1;
2305 DECL_NAMELESS (decl) = 1;
2306 TREE_CONSTANT (decl) = 1;
2308 DECL_CHAIN (decl) = all->orig_fnargs;
2309 all->orig_fnargs = decl;
2310 fnargs.safe_insert (0, decl);
2312 all->function_result_decl = decl;
2315 /* If the target wants to split complex arguments into scalars, do so. */
2316 if (targetm.calls.split_complex_arg)
2317 split_complex_args (&fnargs);
2319 return fnargs;
2322 /* A subroutine of assign_parms. Examine PARM and pull out type and mode
2323 data for the parameter. Incorporate ABI specifics such as pass-by-
2324 reference and type promotion. */
2326 static void
2327 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2328 struct assign_parm_data_one *data)
2330 tree nominal_type, passed_type;
2331 enum machine_mode nominal_mode, passed_mode, promoted_mode;
2332 int unsignedp;
2334 memset (data, 0, sizeof (*data));
2336 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
2337 if (!cfun->stdarg)
2338 data->named_arg = 1; /* No variadic parms. */
2339 else if (DECL_CHAIN (parm))
2340 data->named_arg = 1; /* Not the last non-variadic parm. */
2341 else if (targetm.calls.strict_argument_naming (all->args_so_far))
2342 data->named_arg = 1; /* Only variadic ones are unnamed. */
2343 else
2344 data->named_arg = 0; /* Treat as variadic. */
2346 nominal_type = TREE_TYPE (parm);
2347 passed_type = DECL_ARG_TYPE (parm);
2349 /* Look out for errors propagating this far. Also, if the parameter's
2350 type is void then its value doesn't matter. */
2351 if (TREE_TYPE (parm) == error_mark_node
2352 /* This can happen after weird syntax errors
2353 or if an enum type is defined among the parms. */
2354 || TREE_CODE (parm) != PARM_DECL
2355 || passed_type == NULL
2356 || VOID_TYPE_P (nominal_type))
2358 nominal_type = passed_type = void_type_node;
2359 nominal_mode = passed_mode = promoted_mode = VOIDmode;
2360 goto egress;
2363 /* Find mode of arg as it is passed, and mode of arg as it should be
2364 during execution of this function. */
2365 passed_mode = TYPE_MODE (passed_type);
2366 nominal_mode = TYPE_MODE (nominal_type);
2368 /* If the parm is to be passed as a transparent union or record, use the
2369 type of the first field for the tests below. We have already verified
2370 that the modes are the same. */
2371 if ((TREE_CODE (passed_type) == UNION_TYPE
2372 || TREE_CODE (passed_type) == RECORD_TYPE)
2373 && TYPE_TRANSPARENT_AGGR (passed_type))
2374 passed_type = TREE_TYPE (first_field (passed_type));
2376 /* See if this arg was passed by invisible reference. */
2377 if (pass_by_reference (&all->args_so_far_v, passed_mode,
2378 passed_type, data->named_arg))
2380 passed_type = nominal_type = build_pointer_type (passed_type);
2381 data->passed_pointer = true;
2382 passed_mode = nominal_mode = TYPE_MODE (nominal_type);
2385 /* Find mode as it is passed by the ABI. */
2386 unsignedp = TYPE_UNSIGNED (passed_type);
2387 promoted_mode = promote_function_mode (passed_type, passed_mode, &unsignedp,
2388 TREE_TYPE (current_function_decl), 0);
2390 egress:
2391 data->nominal_type = nominal_type;
2392 data->passed_type = passed_type;
2393 data->nominal_mode = nominal_mode;
2394 data->passed_mode = passed_mode;
2395 data->promoted_mode = promoted_mode;
2398 /* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2400 static void
2401 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2402 struct assign_parm_data_one *data, bool no_rtl)
2404 int varargs_pretend_bytes = 0;
2406 targetm.calls.setup_incoming_varargs (all->args_so_far,
2407 data->promoted_mode,
2408 data->passed_type,
2409 &varargs_pretend_bytes, no_rtl);
2411 /* If the back-end has requested extra stack space, record how much is
2412 needed. Do not change pretend_args_size otherwise since it may be
2413 nonzero from an earlier partial argument. */
2414 if (varargs_pretend_bytes > 0)
2415 all->pretend_args_size = varargs_pretend_bytes;
2418 /* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2419 the incoming location of the current parameter. */
2421 static void
2422 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2423 struct assign_parm_data_one *data)
2425 HOST_WIDE_INT pretend_bytes = 0;
2426 rtx entry_parm;
2427 bool in_regs;
2429 if (data->promoted_mode == VOIDmode)
2431 data->entry_parm = data->stack_parm = const0_rtx;
2432 return;
2435 entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2436 data->promoted_mode,
2437 data->passed_type,
2438 data->named_arg);
2440 if (entry_parm == 0)
2441 data->promoted_mode = data->passed_mode;
2443 /* Determine parm's home in the stack, in case it arrives in the stack
2444 or we should pretend it did. Compute the stack position and rtx where
2445 the argument arrives and its size.
2447 There is one complexity here: If this was a parameter that would
2448 have been passed in registers, but wasn't only because it is
2449 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2450 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2451 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2452 as it was the previous time. */
2453 in_regs = entry_parm != 0;
2454 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2455 in_regs = true;
2456 #endif
2457 if (!in_regs && !data->named_arg)
2459 if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2461 rtx tem;
2462 tem = targetm.calls.function_incoming_arg (all->args_so_far,
2463 data->promoted_mode,
2464 data->passed_type, true);
2465 in_regs = tem != NULL;
2469 /* If this parameter was passed both in registers and in the stack, use
2470 the copy on the stack. */
2471 if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2472 data->passed_type))
2473 entry_parm = 0;
2475 if (entry_parm)
2477 int partial;
2479 partial = targetm.calls.arg_partial_bytes (all->args_so_far,
2480 data->promoted_mode,
2481 data->passed_type,
2482 data->named_arg);
2483 data->partial = partial;
2485 /* The caller might already have allocated stack space for the
2486 register parameters. */
2487 if (partial != 0 && all->reg_parm_stack_space == 0)
2489 /* Part of this argument is passed in registers and part
2490 is passed on the stack. Ask the prologue code to extend
2491 the stack part so that we can recreate the full value.
2493 PRETEND_BYTES is the size of the registers we need to store.
2494 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2495 stack space that the prologue should allocate.
2497 Internally, gcc assumes that the argument pointer is aligned
2498 to STACK_BOUNDARY bits. This is used both for alignment
2499 optimizations (see init_emit) and to locate arguments that are
2500 aligned to more than PARM_BOUNDARY bits. We must preserve this
2501 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2502 a stack boundary. */
2504 /* We assume at most one partial arg, and it must be the first
2505 argument on the stack. */
2506 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2508 pretend_bytes = partial;
2509 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2511 /* We want to align relative to the actual stack pointer, so
2512 don't include this in the stack size until later. */
2513 all->extra_pretend_bytes = all->pretend_args_size;
2517 locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2518 entry_parm ? data->partial : 0, current_function_decl,
2519 &all->stack_args_size, &data->locate);
2521 /* Update parm_stack_boundary if this parameter is passed in the
2522 stack. */
2523 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2524 crtl->parm_stack_boundary = data->locate.boundary;
2526 /* Adjust offsets to include the pretend args. */
2527 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2528 data->locate.slot_offset.constant += pretend_bytes;
2529 data->locate.offset.constant += pretend_bytes;
2531 data->entry_parm = entry_parm;
2534 /* A subroutine of assign_parms. If there is actually space on the stack
2535 for this parm, count it in stack_args_size and return true. */
2537 static bool
2538 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2539 struct assign_parm_data_one *data)
2541 /* Trivially true if we've no incoming register. */
2542 if (data->entry_parm == NULL)
2544 /* Also true if we're partially in registers and partially not,
2545 since we've arranged to drop the entire argument on the stack. */
2546 else if (data->partial != 0)
2548 /* Also true if the target says that it's passed in both registers
2549 and on the stack. */
2550 else if (GET_CODE (data->entry_parm) == PARALLEL
2551 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2553 /* Also true if the target says that there's stack allocated for
2554 all register parameters. */
2555 else if (all->reg_parm_stack_space > 0)
2557 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2558 else
2559 return false;
2561 all->stack_args_size.constant += data->locate.size.constant;
2562 if (data->locate.size.var)
2563 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2565 return true;
2568 /* A subroutine of assign_parms. Given that this parameter is allocated
2569 stack space by the ABI, find it. */
2571 static void
2572 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2574 rtx offset_rtx, stack_parm;
2575 unsigned int align, boundary;
2577 /* If we're passing this arg using a reg, make its stack home the
2578 aligned stack slot. */
2579 if (data->entry_parm)
2580 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2581 else
2582 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2584 stack_parm = crtl->args.internal_arg_pointer;
2585 if (offset_rtx != const0_rtx)
2586 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2587 stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2589 if (!data->passed_pointer)
2591 set_mem_attributes (stack_parm, parm, 1);
2592 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2593 while promoted mode's size is needed. */
2594 if (data->promoted_mode != BLKmode
2595 && data->promoted_mode != DECL_MODE (parm))
2597 set_mem_size (stack_parm, GET_MODE_SIZE (data->promoted_mode));
2598 if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2600 int offset = subreg_lowpart_offset (DECL_MODE (parm),
2601 data->promoted_mode);
2602 if (offset)
2603 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2608 boundary = data->locate.boundary;
2609 align = BITS_PER_UNIT;
2611 /* If we're padding upward, we know that the alignment of the slot
2612 is TARGET_FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2613 intentionally forcing upward padding. Otherwise we have to come
2614 up with a guess at the alignment based on OFFSET_RTX. */
2615 if (data->locate.where_pad != downward || data->entry_parm)
2616 align = boundary;
2617 else if (CONST_INT_P (offset_rtx))
2619 align = INTVAL (offset_rtx) * BITS_PER_UNIT | boundary;
2620 align = align & -align;
2622 set_mem_align (stack_parm, align);
2624 if (data->entry_parm)
2625 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2627 data->stack_parm = stack_parm;
2630 /* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2631 always valid and contiguous. */
2633 static void
2634 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2636 rtx entry_parm = data->entry_parm;
2637 rtx stack_parm = data->stack_parm;
2639 /* If this parm was passed part in regs and part in memory, pretend it
2640 arrived entirely in memory by pushing the register-part onto the stack.
2641 In the special case of a DImode or DFmode that is split, we could put
2642 it together in a pseudoreg directly, but for now that's not worth
2643 bothering with. */
2644 if (data->partial != 0)
2646 /* Handle calls that pass values in multiple non-contiguous
2647 locations. The Irix 6 ABI has examples of this. */
2648 if (GET_CODE (entry_parm) == PARALLEL)
2649 emit_group_store (validize_mem (stack_parm), entry_parm,
2650 data->passed_type,
2651 int_size_in_bytes (data->passed_type));
2652 else
2654 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2655 move_block_from_reg (REGNO (entry_parm), validize_mem (stack_parm),
2656 data->partial / UNITS_PER_WORD);
2659 entry_parm = stack_parm;
2662 /* If we didn't decide this parm came in a register, by default it came
2663 on the stack. */
2664 else if (entry_parm == NULL)
2665 entry_parm = stack_parm;
2667 /* When an argument is passed in multiple locations, we can't make use
2668 of this information, but we can save some copying if the whole argument
2669 is passed in a single register. */
2670 else if (GET_CODE (entry_parm) == PARALLEL
2671 && data->nominal_mode != BLKmode
2672 && data->passed_mode != BLKmode)
2674 size_t i, len = XVECLEN (entry_parm, 0);
2676 for (i = 0; i < len; i++)
2677 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2678 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2679 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2680 == data->passed_mode)
2681 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2683 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2684 break;
2688 data->entry_parm = entry_parm;
2691 /* A subroutine of assign_parms. Reconstitute any values which were
2692 passed in multiple registers and would fit in a single register. */
2694 static void
2695 assign_parm_remove_parallels (struct assign_parm_data_one *data)
2697 rtx entry_parm = data->entry_parm;
2699 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2700 This can be done with register operations rather than on the
2701 stack, even if we will store the reconstituted parameter on the
2702 stack later. */
2703 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2705 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2706 emit_group_store (parmreg, entry_parm, data->passed_type,
2707 GET_MODE_SIZE (GET_MODE (entry_parm)));
2708 entry_parm = parmreg;
2711 data->entry_parm = entry_parm;
2714 /* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2715 always valid and properly aligned. */
2717 static void
2718 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2720 rtx stack_parm = data->stack_parm;
2722 /* If we can't trust the parm stack slot to be aligned enough for its
2723 ultimate type, don't use that slot after entry. We'll make another
2724 stack slot, if we need one. */
2725 if (stack_parm
2726 && ((STRICT_ALIGNMENT
2727 && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2728 || (data->nominal_type
2729 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2730 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2731 stack_parm = NULL;
2733 /* If parm was passed in memory, and we need to convert it on entry,
2734 don't store it back in that same slot. */
2735 else if (data->entry_parm == stack_parm
2736 && data->nominal_mode != BLKmode
2737 && data->nominal_mode != data->passed_mode)
2738 stack_parm = NULL;
2740 /* If stack protection is in effect for this function, don't leave any
2741 pointers in their passed stack slots. */
2742 else if (crtl->stack_protect_guard
2743 && (flag_stack_protect == 2
2744 || data->passed_pointer
2745 || POINTER_TYPE_P (data->nominal_type)))
2746 stack_parm = NULL;
2748 data->stack_parm = stack_parm;
2751 /* A subroutine of assign_parms. Return true if the current parameter
2752 should be stored as a BLKmode in the current frame. */
2754 static bool
2755 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2757 if (data->nominal_mode == BLKmode)
2758 return true;
2759 if (GET_MODE (data->entry_parm) == BLKmode)
2760 return true;
2762 #ifdef BLOCK_REG_PADDING
2763 /* Only assign_parm_setup_block knows how to deal with register arguments
2764 that are padded at the least significant end. */
2765 if (REG_P (data->entry_parm)
2766 && GET_MODE_SIZE (data->promoted_mode) < UNITS_PER_WORD
2767 && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2768 == (BYTES_BIG_ENDIAN ? upward : downward)))
2769 return true;
2770 #endif
2772 return false;
2775 /* A subroutine of assign_parms. Arrange for the parameter to be
2776 present and valid in DATA->STACK_RTL. */
2778 static void
2779 assign_parm_setup_block (struct assign_parm_data_all *all,
2780 tree parm, struct assign_parm_data_one *data)
2782 rtx entry_parm = data->entry_parm;
2783 rtx stack_parm = data->stack_parm;
2784 HOST_WIDE_INT size;
2785 HOST_WIDE_INT size_stored;
2787 if (GET_CODE (entry_parm) == PARALLEL)
2788 entry_parm = emit_group_move_into_temps (entry_parm);
2790 size = int_size_in_bytes (data->passed_type);
2791 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2792 if (stack_parm == 0)
2794 DECL_ALIGN (parm) = MAX (DECL_ALIGN (parm), BITS_PER_WORD);
2795 stack_parm = assign_stack_local (BLKmode, size_stored,
2796 DECL_ALIGN (parm));
2797 if (GET_MODE_SIZE (GET_MODE (entry_parm)) == size)
2798 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2799 set_mem_attributes (stack_parm, parm, 1);
2802 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2803 calls that pass values in multiple non-contiguous locations. */
2804 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2806 rtx mem;
2808 /* Note that we will be storing an integral number of words.
2809 So we have to be careful to ensure that we allocate an
2810 integral number of words. We do this above when we call
2811 assign_stack_local if space was not allocated in the argument
2812 list. If it was, this will not work if PARM_BOUNDARY is not
2813 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2814 if it becomes a problem. Exception is when BLKmode arrives
2815 with arguments not conforming to word_mode. */
2817 if (data->stack_parm == 0)
2819 else if (GET_CODE (entry_parm) == PARALLEL)
2821 else
2822 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2824 mem = validize_mem (stack_parm);
2826 /* Handle values in multiple non-contiguous locations. */
2827 if (GET_CODE (entry_parm) == PARALLEL)
2829 push_to_sequence2 (all->first_conversion_insn,
2830 all->last_conversion_insn);
2831 emit_group_store (mem, entry_parm, data->passed_type, size);
2832 all->first_conversion_insn = get_insns ();
2833 all->last_conversion_insn = get_last_insn ();
2834 end_sequence ();
2837 else if (size == 0)
2840 /* If SIZE is that of a mode no bigger than a word, just use
2841 that mode's store operation. */
2842 else if (size <= UNITS_PER_WORD)
2844 enum machine_mode mode
2845 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2847 if (mode != BLKmode
2848 #ifdef BLOCK_REG_PADDING
2849 && (size == UNITS_PER_WORD
2850 || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2851 != (BYTES_BIG_ENDIAN ? upward : downward)))
2852 #endif
2855 rtx reg;
2857 /* We are really truncating a word_mode value containing
2858 SIZE bytes into a value of mode MODE. If such an
2859 operation requires no actual instructions, we can refer
2860 to the value directly in mode MODE, otherwise we must
2861 start with the register in word_mode and explicitly
2862 convert it. */
2863 if (TRULY_NOOP_TRUNCATION (size * BITS_PER_UNIT, BITS_PER_WORD))
2864 reg = gen_rtx_REG (mode, REGNO (entry_parm));
2865 else
2867 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2868 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
2870 emit_move_insn (change_address (mem, mode, 0), reg);
2873 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
2874 machine must be aligned to the left before storing
2875 to memory. Note that the previous test doesn't
2876 handle all cases (e.g. SIZE == 3). */
2877 else if (size != UNITS_PER_WORD
2878 #ifdef BLOCK_REG_PADDING
2879 && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2880 == downward)
2881 #else
2882 && BYTES_BIG_ENDIAN
2883 #endif
2886 rtx tem, x;
2887 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2888 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2890 x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
2891 tem = change_address (mem, word_mode, 0);
2892 emit_move_insn (tem, x);
2894 else
2895 move_block_from_reg (REGNO (entry_parm), mem,
2896 size_stored / UNITS_PER_WORD);
2898 else
2899 move_block_from_reg (REGNO (entry_parm), mem,
2900 size_stored / UNITS_PER_WORD);
2902 else if (data->stack_parm == 0)
2904 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
2905 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
2906 BLOCK_OP_NORMAL);
2907 all->first_conversion_insn = get_insns ();
2908 all->last_conversion_insn = get_last_insn ();
2909 end_sequence ();
2912 data->stack_parm = stack_parm;
2913 SET_DECL_RTL (parm, stack_parm);
2916 /* A subroutine of assign_parms. Allocate a pseudo to hold the current
2917 parameter. Get it there. Perform all ABI specified conversions. */
2919 static void
2920 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
2921 struct assign_parm_data_one *data)
2923 rtx parmreg, validated_mem;
2924 rtx equiv_stack_parm;
2925 enum machine_mode promoted_nominal_mode;
2926 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
2927 bool did_conversion = false;
2928 bool need_conversion, moved;
2930 /* Store the parm in a pseudoregister during the function, but we may
2931 need to do it in a wider mode. Using 2 here makes the result
2932 consistent with promote_decl_mode and thus expand_expr_real_1. */
2933 promoted_nominal_mode
2934 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
2935 TREE_TYPE (current_function_decl), 2);
2937 parmreg = gen_reg_rtx (promoted_nominal_mode);
2939 if (!DECL_ARTIFICIAL (parm))
2940 mark_user_reg (parmreg);
2942 /* If this was an item that we received a pointer to,
2943 set DECL_RTL appropriately. */
2944 if (data->passed_pointer)
2946 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
2947 set_mem_attributes (x, parm, 1);
2948 SET_DECL_RTL (parm, x);
2950 else
2951 SET_DECL_RTL (parm, parmreg);
2953 assign_parm_remove_parallels (data);
2955 /* Copy the value into the register, thus bridging between
2956 assign_parm_find_data_types and expand_expr_real_1. */
2958 equiv_stack_parm = data->stack_parm;
2959 validated_mem = validize_mem (data->entry_parm);
2961 need_conversion = (data->nominal_mode != data->passed_mode
2962 || promoted_nominal_mode != data->promoted_mode);
2963 moved = false;
2965 if (need_conversion
2966 && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
2967 && data->nominal_mode == data->passed_mode
2968 && data->nominal_mode == GET_MODE (data->entry_parm))
2970 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
2971 mode, by the caller. We now have to convert it to
2972 NOMINAL_MODE, if different. However, PARMREG may be in
2973 a different mode than NOMINAL_MODE if it is being stored
2974 promoted.
2976 If ENTRY_PARM is a hard register, it might be in a register
2977 not valid for operating in its mode (e.g., an odd-numbered
2978 register for a DFmode). In that case, moves are the only
2979 thing valid, so we can't do a convert from there. This
2980 occurs when the calling sequence allow such misaligned
2981 usages.
2983 In addition, the conversion may involve a call, which could
2984 clobber parameters which haven't been copied to pseudo
2985 registers yet.
2987 First, we try to emit an insn which performs the necessary
2988 conversion. We verify that this insn does not clobber any
2989 hard registers. */
2991 enum insn_code icode;
2992 rtx op0, op1;
2994 icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
2995 unsignedp);
2997 op0 = parmreg;
2998 op1 = validated_mem;
2999 if (icode != CODE_FOR_nothing
3000 && insn_operand_matches (icode, 0, op0)
3001 && insn_operand_matches (icode, 1, op1))
3003 enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
3004 rtx insn, insns, t = op1;
3005 HARD_REG_SET hardregs;
3007 start_sequence ();
3008 /* If op1 is a hard register that is likely spilled, first
3009 force it into a pseudo, otherwise combiner might extend
3010 its lifetime too much. */
3011 if (GET_CODE (t) == SUBREG)
3012 t = SUBREG_REG (t);
3013 if (REG_P (t)
3014 && HARD_REGISTER_P (t)
3015 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (t))
3016 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (t))))
3018 t = gen_reg_rtx (GET_MODE (op1));
3019 emit_move_insn (t, op1);
3021 else
3022 t = op1;
3023 insn = gen_extend_insn (op0, t, promoted_nominal_mode,
3024 data->passed_mode, unsignedp);
3025 emit_insn (insn);
3026 insns = get_insns ();
3028 moved = true;
3029 CLEAR_HARD_REG_SET (hardregs);
3030 for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3032 if (INSN_P (insn))
3033 note_stores (PATTERN (insn), record_hard_reg_sets,
3034 &hardregs);
3035 if (!hard_reg_set_empty_p (hardregs))
3036 moved = false;
3039 end_sequence ();
3041 if (moved)
3043 emit_insn (insns);
3044 if (equiv_stack_parm != NULL_RTX)
3045 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3046 equiv_stack_parm);
3051 if (moved)
3052 /* Nothing to do. */
3054 else if (need_conversion)
3056 /* We did not have an insn to convert directly, or the sequence
3057 generated appeared unsafe. We must first copy the parm to a
3058 pseudo reg, and save the conversion until after all
3059 parameters have been moved. */
3061 int save_tree_used;
3062 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3064 emit_move_insn (tempreg, validated_mem);
3066 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3067 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3069 if (GET_CODE (tempreg) == SUBREG
3070 && GET_MODE (tempreg) == data->nominal_mode
3071 && REG_P (SUBREG_REG (tempreg))
3072 && data->nominal_mode == data->passed_mode
3073 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm)
3074 && GET_MODE_SIZE (GET_MODE (tempreg))
3075 < GET_MODE_SIZE (GET_MODE (data->entry_parm)))
3077 /* The argument is already sign/zero extended, so note it
3078 into the subreg. */
3079 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3080 SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
3083 /* TREE_USED gets set erroneously during expand_assignment. */
3084 save_tree_used = TREE_USED (parm);
3085 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3086 TREE_USED (parm) = save_tree_used;
3087 all->first_conversion_insn = get_insns ();
3088 all->last_conversion_insn = get_last_insn ();
3089 end_sequence ();
3091 did_conversion = true;
3093 else
3094 emit_move_insn (parmreg, validated_mem);
3096 /* If we were passed a pointer but the actual value can safely live
3097 in a register, retrieve it and use it directly. */
3098 if (data->passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode)
3100 /* We can't use nominal_mode, because it will have been set to
3101 Pmode above. We must use the actual mode of the parm. */
3102 if (use_register_for_decl (parm))
3104 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3105 mark_user_reg (parmreg);
3107 else
3109 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3110 TYPE_MODE (TREE_TYPE (parm)),
3111 TYPE_ALIGN (TREE_TYPE (parm)));
3112 parmreg
3113 = assign_stack_local (TYPE_MODE (TREE_TYPE (parm)),
3114 GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parm))),
3115 align);
3116 set_mem_attributes (parmreg, parm, 1);
3119 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
3121 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
3122 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3124 push_to_sequence2 (all->first_conversion_insn,
3125 all->last_conversion_insn);
3126 emit_move_insn (tempreg, DECL_RTL (parm));
3127 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3128 emit_move_insn (parmreg, tempreg);
3129 all->first_conversion_insn = get_insns ();
3130 all->last_conversion_insn = get_last_insn ();
3131 end_sequence ();
3133 did_conversion = true;
3135 else
3136 emit_move_insn (parmreg, DECL_RTL (parm));
3138 SET_DECL_RTL (parm, parmreg);
3140 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3141 now the parm. */
3142 data->stack_parm = NULL;
3145 /* Mark the register as eliminable if we did no conversion and it was
3146 copied from memory at a fixed offset, and the arg pointer was not
3147 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3148 offset formed an invalid address, such memory-equivalences as we
3149 make here would screw up life analysis for it. */
3150 if (data->nominal_mode == data->passed_mode
3151 && !did_conversion
3152 && data->stack_parm != 0
3153 && MEM_P (data->stack_parm)
3154 && data->locate.offset.var == 0
3155 && reg_mentioned_p (virtual_incoming_args_rtx,
3156 XEXP (data->stack_parm, 0)))
3158 rtx linsn = get_last_insn ();
3159 rtx sinsn, set;
3161 /* Mark complex types separately. */
3162 if (GET_CODE (parmreg) == CONCAT)
3164 enum machine_mode submode
3165 = GET_MODE_INNER (GET_MODE (parmreg));
3166 int regnor = REGNO (XEXP (parmreg, 0));
3167 int regnoi = REGNO (XEXP (parmreg, 1));
3168 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3169 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3170 GET_MODE_SIZE (submode));
3172 /* Scan backwards for the set of the real and
3173 imaginary parts. */
3174 for (sinsn = linsn; sinsn != 0;
3175 sinsn = prev_nonnote_insn (sinsn))
3177 set = single_set (sinsn);
3178 if (set == 0)
3179 continue;
3181 if (SET_DEST (set) == regno_reg_rtx [regnoi])
3182 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3183 else if (SET_DEST (set) == regno_reg_rtx [regnor])
3184 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3187 else
3188 set_dst_reg_note (linsn, REG_EQUIV, equiv_stack_parm, parmreg);
3191 /* For pointer data type, suggest pointer register. */
3192 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3193 mark_reg_pointer (parmreg,
3194 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3197 /* A subroutine of assign_parms. Allocate stack space to hold the current
3198 parameter. Get it there. Perform all ABI specified conversions. */
3200 static void
3201 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3202 struct assign_parm_data_one *data)
3204 /* Value must be stored in the stack slot STACK_PARM during function
3205 execution. */
3206 bool to_conversion = false;
3208 assign_parm_remove_parallels (data);
3210 if (data->promoted_mode != data->nominal_mode)
3212 /* Conversion is required. */
3213 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3215 emit_move_insn (tempreg, validize_mem (data->entry_parm));
3217 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3218 to_conversion = true;
3220 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3221 TYPE_UNSIGNED (TREE_TYPE (parm)));
3223 if (data->stack_parm)
3225 int offset = subreg_lowpart_offset (data->nominal_mode,
3226 GET_MODE (data->stack_parm));
3227 /* ??? This may need a big-endian conversion on sparc64. */
3228 data->stack_parm
3229 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3230 if (offset && MEM_OFFSET_KNOWN_P (data->stack_parm))
3231 set_mem_offset (data->stack_parm,
3232 MEM_OFFSET (data->stack_parm) + offset);
3236 if (data->entry_parm != data->stack_parm)
3238 rtx src, dest;
3240 if (data->stack_parm == 0)
3242 int align = STACK_SLOT_ALIGNMENT (data->passed_type,
3243 GET_MODE (data->entry_parm),
3244 TYPE_ALIGN (data->passed_type));
3245 data->stack_parm
3246 = assign_stack_local (GET_MODE (data->entry_parm),
3247 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3248 align);
3249 set_mem_attributes (data->stack_parm, parm, 1);
3252 dest = validize_mem (data->stack_parm);
3253 src = validize_mem (data->entry_parm);
3255 if (MEM_P (src))
3257 /* Use a block move to handle potentially misaligned entry_parm. */
3258 if (!to_conversion)
3259 push_to_sequence2 (all->first_conversion_insn,
3260 all->last_conversion_insn);
3261 to_conversion = true;
3263 emit_block_move (dest, src,
3264 GEN_INT (int_size_in_bytes (data->passed_type)),
3265 BLOCK_OP_NORMAL);
3267 else
3268 emit_move_insn (dest, src);
3271 if (to_conversion)
3273 all->first_conversion_insn = get_insns ();
3274 all->last_conversion_insn = get_last_insn ();
3275 end_sequence ();
3278 SET_DECL_RTL (parm, data->stack_parm);
3281 /* A subroutine of assign_parms. If the ABI splits complex arguments, then
3282 undo the frobbing that we did in assign_parms_augmented_arg_list. */
3284 static void
3285 assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3286 vec<tree> fnargs)
3288 tree parm;
3289 tree orig_fnargs = all->orig_fnargs;
3290 unsigned i = 0;
3292 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3294 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3295 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3297 rtx tmp, real, imag;
3298 enum machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3300 real = DECL_RTL (fnargs[i]);
3301 imag = DECL_RTL (fnargs[i + 1]);
3302 if (inner != GET_MODE (real))
3304 real = gen_lowpart_SUBREG (inner, real);
3305 imag = gen_lowpart_SUBREG (inner, imag);
3308 if (TREE_ADDRESSABLE (parm))
3310 rtx rmem, imem;
3311 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3312 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3313 DECL_MODE (parm),
3314 TYPE_ALIGN (TREE_TYPE (parm)));
3316 /* split_complex_arg put the real and imag parts in
3317 pseudos. Move them to memory. */
3318 tmp = assign_stack_local (DECL_MODE (parm), size, align);
3319 set_mem_attributes (tmp, parm, 1);
3320 rmem = adjust_address_nv (tmp, inner, 0);
3321 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3322 push_to_sequence2 (all->first_conversion_insn,
3323 all->last_conversion_insn);
3324 emit_move_insn (rmem, real);
3325 emit_move_insn (imem, imag);
3326 all->first_conversion_insn = get_insns ();
3327 all->last_conversion_insn = get_last_insn ();
3328 end_sequence ();
3330 else
3331 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3332 SET_DECL_RTL (parm, tmp);
3334 real = DECL_INCOMING_RTL (fnargs[i]);
3335 imag = DECL_INCOMING_RTL (fnargs[i + 1]);
3336 if (inner != GET_MODE (real))
3338 real = gen_lowpart_SUBREG (inner, real);
3339 imag = gen_lowpart_SUBREG (inner, imag);
3341 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3342 set_decl_incoming_rtl (parm, tmp, false);
3343 i++;
3348 /* Assign RTL expressions to the function's parameters. This may involve
3349 copying them into registers and using those registers as the DECL_RTL. */
3351 static void
3352 assign_parms (tree fndecl)
3354 struct assign_parm_data_all all;
3355 tree parm;
3356 vec<tree> fnargs;
3357 unsigned i;
3359 crtl->args.internal_arg_pointer
3360 = targetm.calls.internal_arg_pointer ();
3362 assign_parms_initialize_all (&all);
3363 fnargs = assign_parms_augmented_arg_list (&all);
3365 FOR_EACH_VEC_ELT (fnargs, i, parm)
3367 struct assign_parm_data_one data;
3369 /* Extract the type of PARM; adjust it according to ABI. */
3370 assign_parm_find_data_types (&all, parm, &data);
3372 /* Early out for errors and void parameters. */
3373 if (data.passed_mode == VOIDmode)
3375 SET_DECL_RTL (parm, const0_rtx);
3376 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3377 continue;
3380 /* Estimate stack alignment from parameter alignment. */
3381 if (SUPPORTS_STACK_ALIGNMENT)
3383 unsigned int align
3384 = targetm.calls.function_arg_boundary (data.promoted_mode,
3385 data.passed_type);
3386 align = MINIMUM_ALIGNMENT (data.passed_type, data.promoted_mode,
3387 align);
3388 if (TYPE_ALIGN (data.nominal_type) > align)
3389 align = MINIMUM_ALIGNMENT (data.nominal_type,
3390 TYPE_MODE (data.nominal_type),
3391 TYPE_ALIGN (data.nominal_type));
3392 if (crtl->stack_alignment_estimated < align)
3394 gcc_assert (!crtl->stack_realign_processed);
3395 crtl->stack_alignment_estimated = align;
3399 if (cfun->stdarg && !DECL_CHAIN (parm))
3400 assign_parms_setup_varargs (&all, &data, false);
3402 /* Find out where the parameter arrives in this function. */
3403 assign_parm_find_entry_rtl (&all, &data);
3405 /* Find out where stack space for this parameter might be. */
3406 if (assign_parm_is_stack_parm (&all, &data))
3408 assign_parm_find_stack_rtl (parm, &data);
3409 assign_parm_adjust_entry_rtl (&data);
3412 /* Record permanently how this parm was passed. */
3413 if (data.passed_pointer)
3415 rtx incoming_rtl
3416 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.passed_type)),
3417 data.entry_parm);
3418 set_decl_incoming_rtl (parm, incoming_rtl, true);
3420 else
3421 set_decl_incoming_rtl (parm, data.entry_parm, false);
3423 /* Update info on where next arg arrives in registers. */
3424 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3425 data.passed_type, data.named_arg);
3427 assign_parm_adjust_stack_rtl (&data);
3429 if (assign_parm_setup_block_p (&data))
3430 assign_parm_setup_block (&all, parm, &data);
3431 else if (data.passed_pointer || use_register_for_decl (parm))
3432 assign_parm_setup_reg (&all, parm, &data);
3433 else
3434 assign_parm_setup_stack (&all, parm, &data);
3437 if (targetm.calls.split_complex_arg)
3438 assign_parms_unsplit_complex (&all, fnargs);
3440 fnargs.release ();
3442 /* Output all parameter conversion instructions (possibly including calls)
3443 now that all parameters have been copied out of hard registers. */
3444 emit_insn (all.first_conversion_insn);
3446 /* Estimate reload stack alignment from scalar return mode. */
3447 if (SUPPORTS_STACK_ALIGNMENT)
3449 if (DECL_RESULT (fndecl))
3451 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3452 enum machine_mode mode = TYPE_MODE (type);
3454 if (mode != BLKmode
3455 && mode != VOIDmode
3456 && !AGGREGATE_TYPE_P (type))
3458 unsigned int align = GET_MODE_ALIGNMENT (mode);
3459 if (crtl->stack_alignment_estimated < align)
3461 gcc_assert (!crtl->stack_realign_processed);
3462 crtl->stack_alignment_estimated = align;
3468 /* If we are receiving a struct value address as the first argument, set up
3469 the RTL for the function result. As this might require code to convert
3470 the transmitted address to Pmode, we do this here to ensure that possible
3471 preliminary conversions of the address have been emitted already. */
3472 if (all.function_result_decl)
3474 tree result = DECL_RESULT (current_function_decl);
3475 rtx addr = DECL_RTL (all.function_result_decl);
3476 rtx x;
3478 if (DECL_BY_REFERENCE (result))
3480 SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3481 x = addr;
3483 else
3485 SET_DECL_VALUE_EXPR (result,
3486 build1 (INDIRECT_REF, TREE_TYPE (result),
3487 all.function_result_decl));
3488 addr = convert_memory_address (Pmode, addr);
3489 x = gen_rtx_MEM (DECL_MODE (result), addr);
3490 set_mem_attributes (x, result, 1);
3493 DECL_HAS_VALUE_EXPR_P (result) = 1;
3495 SET_DECL_RTL (result, x);
3498 /* We have aligned all the args, so add space for the pretend args. */
3499 crtl->args.pretend_args_size = all.pretend_args_size;
3500 all.stack_args_size.constant += all.extra_pretend_bytes;
3501 crtl->args.size = all.stack_args_size.constant;
3503 /* Adjust function incoming argument size for alignment and
3504 minimum length. */
3506 #ifdef REG_PARM_STACK_SPACE
3507 crtl->args.size = MAX (crtl->args.size,
3508 REG_PARM_STACK_SPACE (fndecl));
3509 #endif
3511 crtl->args.size = CEIL_ROUND (crtl->args.size,
3512 PARM_BOUNDARY / BITS_PER_UNIT);
3514 #ifdef ARGS_GROW_DOWNWARD
3515 crtl->args.arg_offset_rtx
3516 = (all.stack_args_size.var == 0 ? GEN_INT (-all.stack_args_size.constant)
3517 : expand_expr (size_diffop (all.stack_args_size.var,
3518 size_int (-all.stack_args_size.constant)),
3519 NULL_RTX, VOIDmode, EXPAND_NORMAL));
3520 #else
3521 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3522 #endif
3524 /* See how many bytes, if any, of its args a function should try to pop
3525 on return. */
3527 crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3528 TREE_TYPE (fndecl),
3529 crtl->args.size);
3531 /* For stdarg.h function, save info about
3532 regs and stack space used by the named args. */
3534 crtl->args.info = all.args_so_far_v;
3536 /* Set the rtx used for the function return value. Put this in its
3537 own variable so any optimizers that need this information don't have
3538 to include tree.h. Do this here so it gets done when an inlined
3539 function gets output. */
3541 crtl->return_rtx
3542 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3543 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3545 /* If scalar return value was computed in a pseudo-reg, or was a named
3546 return value that got dumped to the stack, copy that to the hard
3547 return register. */
3548 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3550 tree decl_result = DECL_RESULT (fndecl);
3551 rtx decl_rtl = DECL_RTL (decl_result);
3553 if (REG_P (decl_rtl)
3554 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3555 : DECL_REGISTER (decl_result))
3557 rtx real_decl_rtl;
3559 real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3560 fndecl, true);
3561 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3562 /* The delay slot scheduler assumes that crtl->return_rtx
3563 holds the hard register containing the return value, not a
3564 temporary pseudo. */
3565 crtl->return_rtx = real_decl_rtl;
3570 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3571 For all seen types, gimplify their sizes. */
3573 static tree
3574 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3576 tree t = *tp;
3578 *walk_subtrees = 0;
3579 if (TYPE_P (t))
3581 if (POINTER_TYPE_P (t))
3582 *walk_subtrees = 1;
3583 else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3584 && !TYPE_SIZES_GIMPLIFIED (t))
3586 gimplify_type_sizes (t, (gimple_seq *) data);
3587 *walk_subtrees = 1;
3591 return NULL;
3594 /* Gimplify the parameter list for current_function_decl. This involves
3595 evaluating SAVE_EXPRs of variable sized parameters and generating code
3596 to implement callee-copies reference parameters. Returns a sequence of
3597 statements to add to the beginning of the function. */
3599 gimple_seq
3600 gimplify_parameters (void)
3602 struct assign_parm_data_all all;
3603 tree parm;
3604 gimple_seq stmts = NULL;
3605 vec<tree> fnargs;
3606 unsigned i;
3608 assign_parms_initialize_all (&all);
3609 fnargs = assign_parms_augmented_arg_list (&all);
3611 FOR_EACH_VEC_ELT (fnargs, i, parm)
3613 struct assign_parm_data_one data;
3615 /* Extract the type of PARM; adjust it according to ABI. */
3616 assign_parm_find_data_types (&all, parm, &data);
3618 /* Early out for errors and void parameters. */
3619 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3620 continue;
3622 /* Update info on where next arg arrives in registers. */
3623 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3624 data.passed_type, data.named_arg);
3626 /* ??? Once upon a time variable_size stuffed parameter list
3627 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3628 turned out to be less than manageable in the gimple world.
3629 Now we have to hunt them down ourselves. */
3630 walk_tree_without_duplicates (&data.passed_type,
3631 gimplify_parm_type, &stmts);
3633 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3635 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3636 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3639 if (data.passed_pointer)
3641 tree type = TREE_TYPE (data.passed_type);
3642 if (reference_callee_copied (&all.args_so_far_v, TYPE_MODE (type),
3643 type, data.named_arg))
3645 tree local, t;
3647 /* For constant-sized objects, this is trivial; for
3648 variable-sized objects, we have to play games. */
3649 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3650 && !(flag_stack_check == GENERIC_STACK_CHECK
3651 && compare_tree_int (DECL_SIZE_UNIT (parm),
3652 STACK_CHECK_MAX_VAR_SIZE) > 0))
3654 local = create_tmp_var (type, get_name (parm));
3655 DECL_IGNORED_P (local) = 0;
3656 /* If PARM was addressable, move that flag over
3657 to the local copy, as its address will be taken,
3658 not the PARMs. Keep the parms address taken
3659 as we'll query that flag during gimplification. */
3660 if (TREE_ADDRESSABLE (parm))
3661 TREE_ADDRESSABLE (local) = 1;
3662 else if (TREE_CODE (type) == COMPLEX_TYPE
3663 || TREE_CODE (type) == VECTOR_TYPE)
3664 DECL_GIMPLE_REG_P (local) = 1;
3666 else
3668 tree ptr_type, addr;
3670 ptr_type = build_pointer_type (type);
3671 addr = create_tmp_reg (ptr_type, get_name (parm));
3672 DECL_IGNORED_P (addr) = 0;
3673 local = build_fold_indirect_ref (addr);
3675 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
3676 t = build_call_expr (t, 2, DECL_SIZE_UNIT (parm),
3677 size_int (DECL_ALIGN (parm)));
3679 /* The call has been built for a variable-sized object. */
3680 CALL_ALLOCA_FOR_VAR_P (t) = 1;
3681 t = fold_convert (ptr_type, t);
3682 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
3683 gimplify_and_add (t, &stmts);
3686 gimplify_assign (local, parm, &stmts);
3688 SET_DECL_VALUE_EXPR (parm, local);
3689 DECL_HAS_VALUE_EXPR_P (parm) = 1;
3694 fnargs.release ();
3696 return stmts;
3699 /* Compute the size and offset from the start of the stacked arguments for a
3700 parm passed in mode PASSED_MODE and with type TYPE.
3702 INITIAL_OFFSET_PTR points to the current offset into the stacked
3703 arguments.
3705 The starting offset and size for this parm are returned in
3706 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
3707 nonzero, the offset is that of stack slot, which is returned in
3708 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
3709 padding required from the initial offset ptr to the stack slot.
3711 IN_REGS is nonzero if the argument will be passed in registers. It will
3712 never be set if REG_PARM_STACK_SPACE is not defined.
3714 FNDECL is the function in which the argument was defined.
3716 There are two types of rounding that are done. The first, controlled by
3717 TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
3718 argument list to be aligned to the specific boundary (in bits). This
3719 rounding affects the initial and starting offsets, but not the argument
3720 size.
3722 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
3723 optionally rounds the size of the parm to PARM_BOUNDARY. The
3724 initial offset is not affected by this rounding, while the size always
3725 is and the starting offset may be. */
3727 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
3728 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
3729 callers pass in the total size of args so far as
3730 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
3732 void
3733 locate_and_pad_parm (enum machine_mode passed_mode, tree type, int in_regs,
3734 int partial, tree fndecl ATTRIBUTE_UNUSED,
3735 struct args_size *initial_offset_ptr,
3736 struct locate_and_pad_arg_data *locate)
3738 tree sizetree;
3739 enum direction where_pad;
3740 unsigned int boundary, round_boundary;
3741 int reg_parm_stack_space = 0;
3742 int part_size_in_regs;
3744 #ifdef REG_PARM_STACK_SPACE
3745 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
3747 /* If we have found a stack parm before we reach the end of the
3748 area reserved for registers, skip that area. */
3749 if (! in_regs)
3751 if (reg_parm_stack_space > 0)
3753 if (initial_offset_ptr->var)
3755 initial_offset_ptr->var
3756 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
3757 ssize_int (reg_parm_stack_space));
3758 initial_offset_ptr->constant = 0;
3760 else if (initial_offset_ptr->constant < reg_parm_stack_space)
3761 initial_offset_ptr->constant = reg_parm_stack_space;
3764 #endif /* REG_PARM_STACK_SPACE */
3766 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
3768 sizetree
3769 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
3770 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
3771 boundary = targetm.calls.function_arg_boundary (passed_mode, type);
3772 round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
3773 type);
3774 locate->where_pad = where_pad;
3776 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
3777 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
3778 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
3780 locate->boundary = boundary;
3782 if (SUPPORTS_STACK_ALIGNMENT)
3784 /* stack_alignment_estimated can't change after stack has been
3785 realigned. */
3786 if (crtl->stack_alignment_estimated < boundary)
3788 if (!crtl->stack_realign_processed)
3789 crtl->stack_alignment_estimated = boundary;
3790 else
3792 /* If stack is realigned and stack alignment value
3793 hasn't been finalized, it is OK not to increase
3794 stack_alignment_estimated. The bigger alignment
3795 requirement is recorded in stack_alignment_needed
3796 below. */
3797 gcc_assert (!crtl->stack_realign_finalized
3798 && crtl->stack_realign_needed);
3803 /* Remember if the outgoing parameter requires extra alignment on the
3804 calling function side. */
3805 if (crtl->stack_alignment_needed < boundary)
3806 crtl->stack_alignment_needed = boundary;
3807 if (crtl->preferred_stack_boundary < boundary)
3808 crtl->preferred_stack_boundary = boundary;
3810 #ifdef ARGS_GROW_DOWNWARD
3811 locate->slot_offset.constant = -initial_offset_ptr->constant;
3812 if (initial_offset_ptr->var)
3813 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
3814 initial_offset_ptr->var);
3817 tree s2 = sizetree;
3818 if (where_pad != none
3819 && (!host_integerp (sizetree, 1)
3820 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % round_boundary))
3821 s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
3822 SUB_PARM_SIZE (locate->slot_offset, s2);
3825 locate->slot_offset.constant += part_size_in_regs;
3827 if (!in_regs
3828 #ifdef REG_PARM_STACK_SPACE
3829 || REG_PARM_STACK_SPACE (fndecl) > 0
3830 #endif
3832 pad_to_arg_alignment (&locate->slot_offset, boundary,
3833 &locate->alignment_pad);
3835 locate->size.constant = (-initial_offset_ptr->constant
3836 - locate->slot_offset.constant);
3837 if (initial_offset_ptr->var)
3838 locate->size.var = size_binop (MINUS_EXPR,
3839 size_binop (MINUS_EXPR,
3840 ssize_int (0),
3841 initial_offset_ptr->var),
3842 locate->slot_offset.var);
3844 /* Pad_below needs the pre-rounded size to know how much to pad
3845 below. */
3846 locate->offset = locate->slot_offset;
3847 if (where_pad == downward)
3848 pad_below (&locate->offset, passed_mode, sizetree);
3850 #else /* !ARGS_GROW_DOWNWARD */
3851 if (!in_regs
3852 #ifdef REG_PARM_STACK_SPACE
3853 || REG_PARM_STACK_SPACE (fndecl) > 0
3854 #endif
3856 pad_to_arg_alignment (initial_offset_ptr, boundary,
3857 &locate->alignment_pad);
3858 locate->slot_offset = *initial_offset_ptr;
3860 #ifdef PUSH_ROUNDING
3861 if (passed_mode != BLKmode)
3862 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
3863 #endif
3865 /* Pad_below needs the pre-rounded size to know how much to pad below
3866 so this must be done before rounding up. */
3867 locate->offset = locate->slot_offset;
3868 if (where_pad == downward)
3869 pad_below (&locate->offset, passed_mode, sizetree);
3871 if (where_pad != none
3872 && (!host_integerp (sizetree, 1)
3873 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % round_boundary))
3874 sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
3876 ADD_PARM_SIZE (locate->size, sizetree);
3878 locate->size.constant -= part_size_in_regs;
3879 #endif /* ARGS_GROW_DOWNWARD */
3881 #ifdef FUNCTION_ARG_OFFSET
3882 locate->offset.constant += FUNCTION_ARG_OFFSET (passed_mode, type);
3883 #endif
3886 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
3887 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
3889 static void
3890 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
3891 struct args_size *alignment_pad)
3893 tree save_var = NULL_TREE;
3894 HOST_WIDE_INT save_constant = 0;
3895 int boundary_in_bytes = boundary / BITS_PER_UNIT;
3896 HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
3898 #ifdef SPARC_STACK_BOUNDARY_HACK
3899 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
3900 the real alignment of %sp. However, when it does this, the
3901 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
3902 if (SPARC_STACK_BOUNDARY_HACK)
3903 sp_offset = 0;
3904 #endif
3906 if (boundary > PARM_BOUNDARY)
3908 save_var = offset_ptr->var;
3909 save_constant = offset_ptr->constant;
3912 alignment_pad->var = NULL_TREE;
3913 alignment_pad->constant = 0;
3915 if (boundary > BITS_PER_UNIT)
3917 if (offset_ptr->var)
3919 tree sp_offset_tree = ssize_int (sp_offset);
3920 tree offset = size_binop (PLUS_EXPR,
3921 ARGS_SIZE_TREE (*offset_ptr),
3922 sp_offset_tree);
3923 #ifdef ARGS_GROW_DOWNWARD
3924 tree rounded = round_down (offset, boundary / BITS_PER_UNIT);
3925 #else
3926 tree rounded = round_up (offset, boundary / BITS_PER_UNIT);
3927 #endif
3929 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
3930 /* ARGS_SIZE_TREE includes constant term. */
3931 offset_ptr->constant = 0;
3932 if (boundary > PARM_BOUNDARY)
3933 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
3934 save_var);
3936 else
3938 offset_ptr->constant = -sp_offset +
3939 #ifdef ARGS_GROW_DOWNWARD
3940 FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3941 #else
3942 CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3943 #endif
3944 if (boundary > PARM_BOUNDARY)
3945 alignment_pad->constant = offset_ptr->constant - save_constant;
3950 static void
3951 pad_below (struct args_size *offset_ptr, enum machine_mode passed_mode, tree sizetree)
3953 if (passed_mode != BLKmode)
3955 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
3956 offset_ptr->constant
3957 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
3958 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
3959 - GET_MODE_SIZE (passed_mode));
3961 else
3963 if (TREE_CODE (sizetree) != INTEGER_CST
3964 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
3966 /* Round the size up to multiple of PARM_BOUNDARY bits. */
3967 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
3968 /* Add it in. */
3969 ADD_PARM_SIZE (*offset_ptr, s2);
3970 SUB_PARM_SIZE (*offset_ptr, sizetree);
3976 /* True if register REGNO was alive at a place where `setjmp' was
3977 called and was set more than once or is an argument. Such regs may
3978 be clobbered by `longjmp'. */
3980 static bool
3981 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
3983 /* There appear to be cases where some local vars never reach the
3984 backend but have bogus regnos. */
3985 if (regno >= max_reg_num ())
3986 return false;
3988 return ((REG_N_SETS (regno) > 1
3989 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), regno))
3990 && REGNO_REG_SET_P (setjmp_crosses, regno));
3993 /* Walk the tree of blocks describing the binding levels within a
3994 function and warn about variables the might be killed by setjmp or
3995 vfork. This is done after calling flow_analysis before register
3996 allocation since that will clobber the pseudo-regs to hard
3997 regs. */
3999 static void
4000 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
4002 tree decl, sub;
4004 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
4006 if (TREE_CODE (decl) == VAR_DECL
4007 && DECL_RTL_SET_P (decl)
4008 && REG_P (DECL_RTL (decl))
4009 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4010 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
4011 " %<longjmp%> or %<vfork%>", decl);
4014 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
4015 setjmp_vars_warning (setjmp_crosses, sub);
4018 /* Do the appropriate part of setjmp_vars_warning
4019 but for arguments instead of local variables. */
4021 static void
4022 setjmp_args_warning (bitmap setjmp_crosses)
4024 tree decl;
4025 for (decl = DECL_ARGUMENTS (current_function_decl);
4026 decl; decl = DECL_CHAIN (decl))
4027 if (DECL_RTL (decl) != 0
4028 && REG_P (DECL_RTL (decl))
4029 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4030 warning (OPT_Wclobbered,
4031 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
4032 decl);
4035 /* Generate warning messages for variables live across setjmp. */
4037 void
4038 generate_setjmp_warnings (void)
4040 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4042 if (n_basic_blocks == NUM_FIXED_BLOCKS
4043 || bitmap_empty_p (setjmp_crosses))
4044 return;
4046 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4047 setjmp_args_warning (setjmp_crosses);
4051 /* Reverse the order of elements in the fragment chain T of blocks,
4052 and return the new head of the chain (old last element).
4053 In addition to that clear BLOCK_SAME_RANGE flags when needed
4054 and adjust BLOCK_SUPERCONTEXT from the super fragment to
4055 its super fragment origin. */
4057 static tree
4058 block_fragments_nreverse (tree t)
4060 tree prev = 0, block, next, prev_super = 0;
4061 tree super = BLOCK_SUPERCONTEXT (t);
4062 if (BLOCK_FRAGMENT_ORIGIN (super))
4063 super = BLOCK_FRAGMENT_ORIGIN (super);
4064 for (block = t; block; block = next)
4066 next = BLOCK_FRAGMENT_CHAIN (block);
4067 BLOCK_FRAGMENT_CHAIN (block) = prev;
4068 if ((prev && !BLOCK_SAME_RANGE (prev))
4069 || (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (block))
4070 != prev_super))
4071 BLOCK_SAME_RANGE (block) = 0;
4072 prev_super = BLOCK_SUPERCONTEXT (block);
4073 BLOCK_SUPERCONTEXT (block) = super;
4074 prev = block;
4076 t = BLOCK_FRAGMENT_ORIGIN (t);
4077 if (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (t))
4078 != prev_super)
4079 BLOCK_SAME_RANGE (t) = 0;
4080 BLOCK_SUPERCONTEXT (t) = super;
4081 return prev;
4084 /* Reverse the order of elements in the chain T of blocks,
4085 and return the new head of the chain (old last element).
4086 Also do the same on subblocks and reverse the order of elements
4087 in BLOCK_FRAGMENT_CHAIN as well. */
4089 static tree
4090 blocks_nreverse_all (tree t)
4092 tree prev = 0, block, next;
4093 for (block = t; block; block = next)
4095 next = BLOCK_CHAIN (block);
4096 BLOCK_CHAIN (block) = prev;
4097 if (BLOCK_FRAGMENT_CHAIN (block)
4098 && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4100 BLOCK_FRAGMENT_CHAIN (block)
4101 = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4102 if (!BLOCK_SAME_RANGE (BLOCK_FRAGMENT_CHAIN (block)))
4103 BLOCK_SAME_RANGE (block) = 0;
4105 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4106 prev = block;
4108 return prev;
4112 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4113 and create duplicate blocks. */
4114 /* ??? Need an option to either create block fragments or to create
4115 abstract origin duplicates of a source block. It really depends
4116 on what optimization has been performed. */
4118 void
4119 reorder_blocks (void)
4121 tree block = DECL_INITIAL (current_function_decl);
4123 if (block == NULL_TREE)
4124 return;
4126 stack_vec<tree, 10> block_stack;
4128 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4129 clear_block_marks (block);
4131 /* Prune the old trees away, so that they don't get in the way. */
4132 BLOCK_SUBBLOCKS (block) = NULL_TREE;
4133 BLOCK_CHAIN (block) = NULL_TREE;
4135 /* Recreate the block tree from the note nesting. */
4136 reorder_blocks_1 (get_insns (), block, &block_stack);
4137 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4140 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
4142 void
4143 clear_block_marks (tree block)
4145 while (block)
4147 TREE_ASM_WRITTEN (block) = 0;
4148 clear_block_marks (BLOCK_SUBBLOCKS (block));
4149 block = BLOCK_CHAIN (block);
4153 static void
4154 reorder_blocks_1 (rtx insns, tree current_block, vec<tree> *p_block_stack)
4156 rtx insn;
4157 tree prev_beg = NULL_TREE, prev_end = NULL_TREE;
4159 for (insn = insns; insn; insn = NEXT_INSN (insn))
4161 if (NOTE_P (insn))
4163 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4165 tree block = NOTE_BLOCK (insn);
4166 tree origin;
4168 gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4169 origin = block;
4171 if (prev_end)
4172 BLOCK_SAME_RANGE (prev_end) = 0;
4173 prev_end = NULL_TREE;
4175 /* If we have seen this block before, that means it now
4176 spans multiple address regions. Create a new fragment. */
4177 if (TREE_ASM_WRITTEN (block))
4179 tree new_block = copy_node (block);
4181 BLOCK_SAME_RANGE (new_block) = 0;
4182 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4183 BLOCK_FRAGMENT_CHAIN (new_block)
4184 = BLOCK_FRAGMENT_CHAIN (origin);
4185 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4187 NOTE_BLOCK (insn) = new_block;
4188 block = new_block;
4191 if (prev_beg == current_block && prev_beg)
4192 BLOCK_SAME_RANGE (block) = 1;
4194 prev_beg = origin;
4196 BLOCK_SUBBLOCKS (block) = 0;
4197 TREE_ASM_WRITTEN (block) = 1;
4198 /* When there's only one block for the entire function,
4199 current_block == block and we mustn't do this, it
4200 will cause infinite recursion. */
4201 if (block != current_block)
4203 tree super;
4204 if (block != origin)
4205 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block
4206 || BLOCK_FRAGMENT_ORIGIN (BLOCK_SUPERCONTEXT
4207 (origin))
4208 == current_block);
4209 if (p_block_stack->is_empty ())
4210 super = current_block;
4211 else
4213 super = p_block_stack->last ();
4214 gcc_assert (super == current_block
4215 || BLOCK_FRAGMENT_ORIGIN (super)
4216 == current_block);
4218 BLOCK_SUPERCONTEXT (block) = super;
4219 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4220 BLOCK_SUBBLOCKS (current_block) = block;
4221 current_block = origin;
4223 p_block_stack->safe_push (block);
4225 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4227 NOTE_BLOCK (insn) = p_block_stack->pop ();
4228 current_block = BLOCK_SUPERCONTEXT (current_block);
4229 if (BLOCK_FRAGMENT_ORIGIN (current_block))
4230 current_block = BLOCK_FRAGMENT_ORIGIN (current_block);
4231 prev_beg = NULL_TREE;
4232 prev_end = BLOCK_SAME_RANGE (NOTE_BLOCK (insn))
4233 ? NOTE_BLOCK (insn) : NULL_TREE;
4236 else
4238 prev_beg = NULL_TREE;
4239 if (prev_end)
4240 BLOCK_SAME_RANGE (prev_end) = 0;
4241 prev_end = NULL_TREE;
4246 /* Reverse the order of elements in the chain T of blocks,
4247 and return the new head of the chain (old last element). */
4249 tree
4250 blocks_nreverse (tree t)
4252 tree prev = 0, block, next;
4253 for (block = t; block; block = next)
4255 next = BLOCK_CHAIN (block);
4256 BLOCK_CHAIN (block) = prev;
4257 prev = block;
4259 return prev;
4262 /* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4263 by modifying the last node in chain 1 to point to chain 2. */
4265 tree
4266 block_chainon (tree op1, tree op2)
4268 tree t1;
4270 if (!op1)
4271 return op2;
4272 if (!op2)
4273 return op1;
4275 for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4276 continue;
4277 BLOCK_CHAIN (t1) = op2;
4279 #ifdef ENABLE_TREE_CHECKING
4281 tree t2;
4282 for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4283 gcc_assert (t2 != t1);
4285 #endif
4287 return op1;
4290 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
4291 non-NULL, list them all into VECTOR, in a depth-first preorder
4292 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
4293 blocks. */
4295 static int
4296 all_blocks (tree block, tree *vector)
4298 int n_blocks = 0;
4300 while (block)
4302 TREE_ASM_WRITTEN (block) = 0;
4304 /* Record this block. */
4305 if (vector)
4306 vector[n_blocks] = block;
4308 ++n_blocks;
4310 /* Record the subblocks, and their subblocks... */
4311 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4312 vector ? vector + n_blocks : 0);
4313 block = BLOCK_CHAIN (block);
4316 return n_blocks;
4319 /* Return a vector containing all the blocks rooted at BLOCK. The
4320 number of elements in the vector is stored in N_BLOCKS_P. The
4321 vector is dynamically allocated; it is the caller's responsibility
4322 to call `free' on the pointer returned. */
4324 static tree *
4325 get_block_vector (tree block, int *n_blocks_p)
4327 tree *block_vector;
4329 *n_blocks_p = all_blocks (block, NULL);
4330 block_vector = XNEWVEC (tree, *n_blocks_p);
4331 all_blocks (block, block_vector);
4333 return block_vector;
4336 static GTY(()) int next_block_index = 2;
4338 /* Set BLOCK_NUMBER for all the blocks in FN. */
4340 void
4341 number_blocks (tree fn)
4343 int i;
4344 int n_blocks;
4345 tree *block_vector;
4347 /* For SDB and XCOFF debugging output, we start numbering the blocks
4348 from 1 within each function, rather than keeping a running
4349 count. */
4350 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
4351 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
4352 next_block_index = 1;
4353 #endif
4355 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4357 /* The top-level BLOCK isn't numbered at all. */
4358 for (i = 1; i < n_blocks; ++i)
4359 /* We number the blocks from two. */
4360 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4362 free (block_vector);
4364 return;
4367 /* If VAR is present in a subblock of BLOCK, return the subblock. */
4369 DEBUG_FUNCTION tree
4370 debug_find_var_in_block_tree (tree var, tree block)
4372 tree t;
4374 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4375 if (t == var)
4376 return block;
4378 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4380 tree ret = debug_find_var_in_block_tree (var, t);
4381 if (ret)
4382 return ret;
4385 return NULL_TREE;
4388 /* Keep track of whether we're in a dummy function context. If we are,
4389 we don't want to invoke the set_current_function hook, because we'll
4390 get into trouble if the hook calls target_reinit () recursively or
4391 when the initial initialization is not yet complete. */
4393 static bool in_dummy_function;
4395 /* Invoke the target hook when setting cfun. Update the optimization options
4396 if the function uses different options than the default. */
4398 static void
4399 invoke_set_current_function_hook (tree fndecl)
4401 if (!in_dummy_function)
4403 tree opts = ((fndecl)
4404 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4405 : optimization_default_node);
4407 if (!opts)
4408 opts = optimization_default_node;
4410 /* Change optimization options if needed. */
4411 if (optimization_current_node != opts)
4413 optimization_current_node = opts;
4414 cl_optimization_restore (&global_options, TREE_OPTIMIZATION (opts));
4417 targetm.set_current_function (fndecl);
4418 this_fn_optabs = this_target_optabs;
4420 if (opts != optimization_default_node)
4422 init_tree_optimization_optabs (opts);
4423 if (TREE_OPTIMIZATION_OPTABS (opts))
4424 this_fn_optabs = (struct target_optabs *)
4425 TREE_OPTIMIZATION_OPTABS (opts);
4430 /* cfun should never be set directly; use this function. */
4432 void
4433 set_cfun (struct function *new_cfun)
4435 if (cfun != new_cfun)
4437 cfun = new_cfun;
4438 invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4442 /* Initialized with NOGC, making this poisonous to the garbage collector. */
4444 static vec<function_p> cfun_stack;
4446 /* Push the current cfun onto the stack, and set cfun to new_cfun. Also set
4447 current_function_decl accordingly. */
4449 void
4450 push_cfun (struct function *new_cfun)
4452 gcc_assert ((!cfun && !current_function_decl)
4453 || (cfun && current_function_decl == cfun->decl));
4454 cfun_stack.safe_push (cfun);
4455 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4456 set_cfun (new_cfun);
4459 /* Pop cfun from the stack. Also set current_function_decl accordingly. */
4461 void
4462 pop_cfun (void)
4464 struct function *new_cfun = cfun_stack.pop ();
4465 /* When in_dummy_function, we do have a cfun but current_function_decl is
4466 NULL. We also allow pushing NULL cfun and subsequently changing
4467 current_function_decl to something else and have both restored by
4468 pop_cfun. */
4469 gcc_checking_assert (in_dummy_function
4470 || !cfun
4471 || current_function_decl == cfun->decl);
4472 set_cfun (new_cfun);
4473 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4476 /* Return value of funcdef and increase it. */
4478 get_next_funcdef_no (void)
4480 return funcdef_no++;
4483 /* Return value of funcdef. */
4485 get_last_funcdef_no (void)
4487 return funcdef_no;
4490 /* Allocate a function structure for FNDECL and set its contents
4491 to the defaults. Set cfun to the newly-allocated object.
4492 Some of the helper functions invoked during initialization assume
4493 that cfun has already been set. Therefore, assign the new object
4494 directly into cfun and invoke the back end hook explicitly at the
4495 very end, rather than initializing a temporary and calling set_cfun
4496 on it.
4498 ABSTRACT_P is true if this is a function that will never be seen by
4499 the middle-end. Such functions are front-end concepts (like C++
4500 function templates) that do not correspond directly to functions
4501 placed in object files. */
4503 void
4504 allocate_struct_function (tree fndecl, bool abstract_p)
4506 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4508 cfun = ggc_alloc_cleared_function ();
4510 init_eh_for_function ();
4512 if (init_machine_status)
4513 cfun->machine = (*init_machine_status) ();
4515 #ifdef OVERRIDE_ABI_FORMAT
4516 OVERRIDE_ABI_FORMAT (fndecl);
4517 #endif
4519 if (fndecl != NULL_TREE)
4521 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4522 cfun->decl = fndecl;
4523 current_function_funcdef_no = get_next_funcdef_no ();
4526 invoke_set_current_function_hook (fndecl);
4528 if (fndecl != NULL_TREE)
4530 tree result = DECL_RESULT (fndecl);
4531 if (!abstract_p && aggregate_value_p (result, fndecl))
4533 #ifdef PCC_STATIC_STRUCT_RETURN
4534 cfun->returns_pcc_struct = 1;
4535 #endif
4536 cfun->returns_struct = 1;
4539 cfun->stdarg = stdarg_p (fntype);
4541 /* Assume all registers in stdarg functions need to be saved. */
4542 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4543 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4545 /* ??? This could be set on a per-function basis by the front-end
4546 but is this worth the hassle? */
4547 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4551 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4552 instead of just setting it. */
4554 void
4555 push_struct_function (tree fndecl)
4557 /* When in_dummy_function we might be in the middle of a pop_cfun and
4558 current_function_decl and cfun may not match. */
4559 gcc_assert (in_dummy_function
4560 || (!cfun && !current_function_decl)
4561 || (cfun && current_function_decl == cfun->decl));
4562 cfun_stack.safe_push (cfun);
4563 current_function_decl = fndecl;
4564 allocate_struct_function (fndecl, false);
4567 /* Reset crtl and other non-struct-function variables to defaults as
4568 appropriate for emitting rtl at the start of a function. */
4570 static void
4571 prepare_function_start (void)
4573 gcc_assert (!crtl->emit.x_last_insn);
4574 init_temp_slots ();
4575 init_emit ();
4576 init_varasm_status ();
4577 init_expr ();
4578 default_rtl_profile ();
4580 if (flag_stack_usage_info)
4582 cfun->su = ggc_alloc_cleared_stack_usage ();
4583 cfun->su->static_stack_size = -1;
4586 cse_not_expected = ! optimize;
4588 /* Caller save not needed yet. */
4589 caller_save_needed = 0;
4591 /* We haven't done register allocation yet. */
4592 reg_renumber = 0;
4594 /* Indicate that we have not instantiated virtual registers yet. */
4595 virtuals_instantiated = 0;
4597 /* Indicate that we want CONCATs now. */
4598 generating_concat_p = 1;
4600 /* Indicate we have no need of a frame pointer yet. */
4601 frame_pointer_needed = 0;
4604 /* Initialize the rtl expansion mechanism so that we can do simple things
4605 like generate sequences. This is used to provide a context during global
4606 initialization of some passes. You must call expand_dummy_function_end
4607 to exit this context. */
4609 void
4610 init_dummy_function_start (void)
4612 gcc_assert (!in_dummy_function);
4613 in_dummy_function = true;
4614 push_struct_function (NULL_TREE);
4615 prepare_function_start ();
4618 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4619 and initialize static variables for generating RTL for the statements
4620 of the function. */
4622 void
4623 init_function_start (tree subr)
4625 if (subr && DECL_STRUCT_FUNCTION (subr))
4626 set_cfun (DECL_STRUCT_FUNCTION (subr));
4627 else
4628 allocate_struct_function (subr, false);
4629 prepare_function_start ();
4630 decide_function_section (subr);
4632 /* Warn if this value is an aggregate type,
4633 regardless of which calling convention we are using for it. */
4634 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4635 warning (OPT_Waggregate_return, "function returns an aggregate");
4638 /* Expand code to verify the stack_protect_guard. This is invoked at
4639 the end of a function to be protected. */
4641 #ifndef HAVE_stack_protect_test
4642 # define HAVE_stack_protect_test 0
4643 # define gen_stack_protect_test(x, y, z) (gcc_unreachable (), NULL_RTX)
4644 #endif
4646 void
4647 stack_protect_epilogue (void)
4649 tree guard_decl = targetm.stack_protect_guard ();
4650 rtx label = gen_label_rtx ();
4651 rtx x, y, tmp;
4653 x = expand_normal (crtl->stack_protect_guard);
4654 y = expand_normal (guard_decl);
4656 /* Allow the target to compare Y with X without leaking either into
4657 a register. */
4658 switch (HAVE_stack_protect_test != 0)
4660 case 1:
4661 tmp = gen_stack_protect_test (x, y, label);
4662 if (tmp)
4664 emit_insn (tmp);
4665 break;
4667 /* FALLTHRU */
4669 default:
4670 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
4671 break;
4674 /* The noreturn predictor has been moved to the tree level. The rtl-level
4675 predictors estimate this branch about 20%, which isn't enough to get
4676 things moved out of line. Since this is the only extant case of adding
4677 a noreturn function at the rtl level, it doesn't seem worth doing ought
4678 except adding the prediction by hand. */
4679 tmp = get_last_insn ();
4680 if (JUMP_P (tmp))
4681 predict_insn_def (tmp, PRED_NORETURN, TAKEN);
4683 expand_call (targetm.stack_protect_fail (), NULL_RTX, /*ignore=*/true);
4684 free_temp_slots ();
4685 emit_label (label);
4688 /* Start the RTL for a new function, and set variables used for
4689 emitting RTL.
4690 SUBR is the FUNCTION_DECL node.
4691 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
4692 the function's parameters, which must be run at any return statement. */
4694 void
4695 expand_function_start (tree subr)
4697 /* Make sure volatile mem refs aren't considered
4698 valid operands of arithmetic insns. */
4699 init_recog_no_volatile ();
4701 crtl->profile
4702 = (profile_flag
4703 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
4705 crtl->limit_stack
4706 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
4708 /* Make the label for return statements to jump to. Do not special
4709 case machines with special return instructions -- they will be
4710 handled later during jump, ifcvt, or epilogue creation. */
4711 return_label = gen_label_rtx ();
4713 /* Initialize rtx used to return the value. */
4714 /* Do this before assign_parms so that we copy the struct value address
4715 before any library calls that assign parms might generate. */
4717 /* Decide whether to return the value in memory or in a register. */
4718 if (aggregate_value_p (DECL_RESULT (subr), subr))
4720 /* Returning something that won't go in a register. */
4721 rtx value_address = 0;
4723 #ifdef PCC_STATIC_STRUCT_RETURN
4724 if (cfun->returns_pcc_struct)
4726 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
4727 value_address = assemble_static_space (size);
4729 else
4730 #endif
4732 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
4733 /* Expect to be passed the address of a place to store the value.
4734 If it is passed as an argument, assign_parms will take care of
4735 it. */
4736 if (sv)
4738 value_address = gen_reg_rtx (Pmode);
4739 emit_move_insn (value_address, sv);
4742 if (value_address)
4744 rtx x = value_address;
4745 if (!DECL_BY_REFERENCE (DECL_RESULT (subr)))
4747 x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), x);
4748 set_mem_attributes (x, DECL_RESULT (subr), 1);
4750 SET_DECL_RTL (DECL_RESULT (subr), x);
4753 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
4754 /* If return mode is void, this decl rtl should not be used. */
4755 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
4756 else
4758 /* Compute the return values into a pseudo reg, which we will copy
4759 into the true return register after the cleanups are done. */
4760 tree return_type = TREE_TYPE (DECL_RESULT (subr));
4761 if (TYPE_MODE (return_type) != BLKmode
4762 && targetm.calls.return_in_msb (return_type))
4763 /* expand_function_end will insert the appropriate padding in
4764 this case. Use the return value's natural (unpadded) mode
4765 within the function proper. */
4766 SET_DECL_RTL (DECL_RESULT (subr),
4767 gen_reg_rtx (TYPE_MODE (return_type)));
4768 else
4770 /* In order to figure out what mode to use for the pseudo, we
4771 figure out what the mode of the eventual return register will
4772 actually be, and use that. */
4773 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
4775 /* Structures that are returned in registers are not
4776 aggregate_value_p, so we may see a PARALLEL or a REG. */
4777 if (REG_P (hard_reg))
4778 SET_DECL_RTL (DECL_RESULT (subr),
4779 gen_reg_rtx (GET_MODE (hard_reg)));
4780 else
4782 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
4783 SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
4787 /* Set DECL_REGISTER flag so that expand_function_end will copy the
4788 result to the real return register(s). */
4789 DECL_REGISTER (DECL_RESULT (subr)) = 1;
4792 /* Initialize rtx for parameters and local variables.
4793 In some cases this requires emitting insns. */
4794 assign_parms (subr);
4796 /* If function gets a static chain arg, store it. */
4797 if (cfun->static_chain_decl)
4799 tree parm = cfun->static_chain_decl;
4800 rtx local, chain, insn;
4802 local = gen_reg_rtx (Pmode);
4803 chain = targetm.calls.static_chain (current_function_decl, true);
4805 set_decl_incoming_rtl (parm, chain, false);
4806 SET_DECL_RTL (parm, local);
4807 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4809 insn = emit_move_insn (local, chain);
4811 /* Mark the register as eliminable, similar to parameters. */
4812 if (MEM_P (chain)
4813 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
4814 set_dst_reg_note (insn, REG_EQUIV, chain, local);
4817 /* If the function receives a non-local goto, then store the
4818 bits we need to restore the frame pointer. */
4819 if (cfun->nonlocal_goto_save_area)
4821 tree t_save;
4822 rtx r_save;
4824 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
4825 gcc_assert (DECL_RTL_SET_P (var));
4827 t_save = build4 (ARRAY_REF,
4828 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
4829 cfun->nonlocal_goto_save_area,
4830 integer_zero_node, NULL_TREE, NULL_TREE);
4831 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
4832 gcc_assert (GET_MODE (r_save) == Pmode);
4834 emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
4835 update_nonlocal_goto_save_area ();
4838 /* The following was moved from init_function_start.
4839 The move is supposed to make sdb output more accurate. */
4840 /* Indicate the beginning of the function body,
4841 as opposed to parm setup. */
4842 emit_note (NOTE_INSN_FUNCTION_BEG);
4844 gcc_assert (NOTE_P (get_last_insn ()));
4846 parm_birth_insn = get_last_insn ();
4848 if (crtl->profile)
4850 #ifdef PROFILE_HOOK
4851 PROFILE_HOOK (current_function_funcdef_no);
4852 #endif
4855 /* If we are doing generic stack checking, the probe should go here. */
4856 if (flag_stack_check == GENERIC_STACK_CHECK)
4857 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
4860 /* Undo the effects of init_dummy_function_start. */
4861 void
4862 expand_dummy_function_end (void)
4864 gcc_assert (in_dummy_function);
4866 /* End any sequences that failed to be closed due to syntax errors. */
4867 while (in_sequence_p ())
4868 end_sequence ();
4870 /* Outside function body, can't compute type's actual size
4871 until next function's body starts. */
4873 free_after_parsing (cfun);
4874 free_after_compilation (cfun);
4875 pop_cfun ();
4876 in_dummy_function = false;
4879 /* Call DOIT for each hard register used as a return value from
4880 the current function. */
4882 void
4883 diddle_return_value (void (*doit) (rtx, void *), void *arg)
4885 rtx outgoing = crtl->return_rtx;
4887 if (! outgoing)
4888 return;
4890 if (REG_P (outgoing))
4891 (*doit) (outgoing, arg);
4892 else if (GET_CODE (outgoing) == PARALLEL)
4894 int i;
4896 for (i = 0; i < XVECLEN (outgoing, 0); i++)
4898 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
4900 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
4901 (*doit) (x, arg);
4906 static void
4907 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4909 emit_clobber (reg);
4912 void
4913 clobber_return_register (void)
4915 diddle_return_value (do_clobber_return_reg, NULL);
4917 /* In case we do use pseudo to return value, clobber it too. */
4918 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
4920 tree decl_result = DECL_RESULT (current_function_decl);
4921 rtx decl_rtl = DECL_RTL (decl_result);
4922 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
4924 do_clobber_return_reg (decl_rtl, NULL);
4929 static void
4930 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4932 emit_use (reg);
4935 static void
4936 use_return_register (void)
4938 diddle_return_value (do_use_return_reg, NULL);
4941 /* Possibly warn about unused parameters. */
4942 void
4943 do_warn_unused_parameter (tree fn)
4945 tree decl;
4947 for (decl = DECL_ARGUMENTS (fn);
4948 decl; decl = DECL_CHAIN (decl))
4949 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
4950 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
4951 && !TREE_NO_WARNING (decl))
4952 warning (OPT_Wunused_parameter, "unused parameter %q+D", decl);
4955 /* Set the location of the insn chain starting at INSN to LOC. */
4957 static void
4958 set_insn_locations (rtx insn, int loc)
4960 while (insn != NULL_RTX)
4962 if (INSN_P (insn))
4963 INSN_LOCATION (insn) = loc;
4964 insn = NEXT_INSN (insn);
4968 /* Generate RTL for the end of the current function. */
4970 void
4971 expand_function_end (void)
4973 rtx clobber_after;
4975 /* If arg_pointer_save_area was referenced only from a nested
4976 function, we will not have initialized it yet. Do that now. */
4977 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
4978 get_arg_pointer_save_area ();
4980 /* If we are doing generic stack checking and this function makes calls,
4981 do a stack probe at the start of the function to ensure we have enough
4982 space for another stack frame. */
4983 if (flag_stack_check == GENERIC_STACK_CHECK)
4985 rtx insn, seq;
4987 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4988 if (CALL_P (insn))
4990 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
4991 start_sequence ();
4992 if (STACK_CHECK_MOVING_SP)
4993 anti_adjust_stack_and_probe (max_frame_size, true);
4994 else
4995 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
4996 seq = get_insns ();
4997 end_sequence ();
4998 set_insn_locations (seq, prologue_location);
4999 emit_insn_before (seq, stack_check_probe_note);
5000 break;
5004 /* End any sequences that failed to be closed due to syntax errors. */
5005 while (in_sequence_p ())
5006 end_sequence ();
5008 clear_pending_stack_adjust ();
5009 do_pending_stack_adjust ();
5011 /* Output a linenumber for the end of the function.
5012 SDB depends on this. */
5013 set_curr_insn_location (input_location);
5015 /* Before the return label (if any), clobber the return
5016 registers so that they are not propagated live to the rest of
5017 the function. This can only happen with functions that drop
5018 through; if there had been a return statement, there would
5019 have either been a return rtx, or a jump to the return label.
5021 We delay actual code generation after the current_function_value_rtx
5022 is computed. */
5023 clobber_after = get_last_insn ();
5025 /* Output the label for the actual return from the function. */
5026 emit_label (return_label);
5028 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
5030 /* Let except.c know where it should emit the call to unregister
5031 the function context for sjlj exceptions. */
5032 if (flag_exceptions)
5033 sjlj_emit_function_exit_after (get_last_insn ());
5035 else
5037 /* We want to ensure that instructions that may trap are not
5038 moved into the epilogue by scheduling, because we don't
5039 always emit unwind information for the epilogue. */
5040 if (cfun->can_throw_non_call_exceptions)
5041 emit_insn (gen_blockage ());
5044 /* If this is an implementation of throw, do what's necessary to
5045 communicate between __builtin_eh_return and the epilogue. */
5046 expand_eh_return ();
5048 /* If scalar return value was computed in a pseudo-reg, or was a named
5049 return value that got dumped to the stack, copy that to the hard
5050 return register. */
5051 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5053 tree decl_result = DECL_RESULT (current_function_decl);
5054 rtx decl_rtl = DECL_RTL (decl_result);
5056 if (REG_P (decl_rtl)
5057 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5058 : DECL_REGISTER (decl_result))
5060 rtx real_decl_rtl = crtl->return_rtx;
5062 /* This should be set in assign_parms. */
5063 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5065 /* If this is a BLKmode structure being returned in registers,
5066 then use the mode computed in expand_return. Note that if
5067 decl_rtl is memory, then its mode may have been changed,
5068 but that crtl->return_rtx has not. */
5069 if (GET_MODE (real_decl_rtl) == BLKmode)
5070 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
5072 /* If a non-BLKmode return value should be padded at the least
5073 significant end of the register, shift it left by the appropriate
5074 amount. BLKmode results are handled using the group load/store
5075 machinery. */
5076 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5077 && REG_P (real_decl_rtl)
5078 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5080 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5081 REGNO (real_decl_rtl)),
5082 decl_rtl);
5083 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5085 /* If a named return value dumped decl_return to memory, then
5086 we may need to re-do the PROMOTE_MODE signed/unsigned
5087 extension. */
5088 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5090 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5091 promote_function_mode (TREE_TYPE (decl_result),
5092 GET_MODE (decl_rtl), &unsignedp,
5093 TREE_TYPE (current_function_decl), 1);
5095 convert_move (real_decl_rtl, decl_rtl, unsignedp);
5097 else if (GET_CODE (real_decl_rtl) == PARALLEL)
5099 /* If expand_function_start has created a PARALLEL for decl_rtl,
5100 move the result to the real return registers. Otherwise, do
5101 a group load from decl_rtl for a named return. */
5102 if (GET_CODE (decl_rtl) == PARALLEL)
5103 emit_group_move (real_decl_rtl, decl_rtl);
5104 else
5105 emit_group_load (real_decl_rtl, decl_rtl,
5106 TREE_TYPE (decl_result),
5107 int_size_in_bytes (TREE_TYPE (decl_result)));
5109 /* In the case of complex integer modes smaller than a word, we'll
5110 need to generate some non-trivial bitfield insertions. Do that
5111 on a pseudo and not the hard register. */
5112 else if (GET_CODE (decl_rtl) == CONCAT
5113 && GET_MODE_CLASS (GET_MODE (decl_rtl)) == MODE_COMPLEX_INT
5114 && GET_MODE_BITSIZE (GET_MODE (decl_rtl)) <= BITS_PER_WORD)
5116 int old_generating_concat_p;
5117 rtx tmp;
5119 old_generating_concat_p = generating_concat_p;
5120 generating_concat_p = 0;
5121 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5122 generating_concat_p = old_generating_concat_p;
5124 emit_move_insn (tmp, decl_rtl);
5125 emit_move_insn (real_decl_rtl, tmp);
5127 else
5128 emit_move_insn (real_decl_rtl, decl_rtl);
5132 /* If returning a structure, arrange to return the address of the value
5133 in a place where debuggers expect to find it.
5135 If returning a structure PCC style,
5136 the caller also depends on this value.
5137 And cfun->returns_pcc_struct is not necessarily set. */
5138 if (cfun->returns_struct
5139 || cfun->returns_pcc_struct)
5141 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5142 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5143 rtx outgoing;
5145 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5146 type = TREE_TYPE (type);
5147 else
5148 value_address = XEXP (value_address, 0);
5150 outgoing = targetm.calls.function_value (build_pointer_type (type),
5151 current_function_decl, true);
5153 /* Mark this as a function return value so integrate will delete the
5154 assignment and USE below when inlining this function. */
5155 REG_FUNCTION_VALUE_P (outgoing) = 1;
5157 /* The address may be ptr_mode and OUTGOING may be Pmode. */
5158 value_address = convert_memory_address (GET_MODE (outgoing),
5159 value_address);
5161 emit_move_insn (outgoing, value_address);
5163 /* Show return register used to hold result (in this case the address
5164 of the result. */
5165 crtl->return_rtx = outgoing;
5168 /* Emit the actual code to clobber return register. */
5170 rtx seq;
5172 start_sequence ();
5173 clobber_return_register ();
5174 seq = get_insns ();
5175 end_sequence ();
5177 emit_insn_after (seq, clobber_after);
5180 /* Output the label for the naked return from the function. */
5181 if (naked_return_label)
5182 emit_label (naked_return_label);
5184 /* @@@ This is a kludge. We want to ensure that instructions that
5185 may trap are not moved into the epilogue by scheduling, because
5186 we don't always emit unwind information for the epilogue. */
5187 if (cfun->can_throw_non_call_exceptions
5188 && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5189 emit_insn (gen_blockage ());
5191 /* If stack protection is enabled for this function, check the guard. */
5192 if (crtl->stack_protect_guard)
5193 stack_protect_epilogue ();
5195 /* If we had calls to alloca, and this machine needs
5196 an accurate stack pointer to exit the function,
5197 insert some code to save and restore the stack pointer. */
5198 if (! EXIT_IGNORE_STACK
5199 && cfun->calls_alloca)
5201 rtx tem = 0, seq;
5203 start_sequence ();
5204 emit_stack_save (SAVE_FUNCTION, &tem);
5205 seq = get_insns ();
5206 end_sequence ();
5207 emit_insn_before (seq, parm_birth_insn);
5209 emit_stack_restore (SAVE_FUNCTION, tem);
5212 /* ??? This should no longer be necessary since stupid is no longer with
5213 us, but there are some parts of the compiler (eg reload_combine, and
5214 sh mach_dep_reorg) that still try and compute their own lifetime info
5215 instead of using the general framework. */
5216 use_return_register ();
5220 get_arg_pointer_save_area (void)
5222 rtx ret = arg_pointer_save_area;
5224 if (! ret)
5226 ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5227 arg_pointer_save_area = ret;
5230 if (! crtl->arg_pointer_save_area_init)
5232 rtx seq;
5234 /* Save the arg pointer at the beginning of the function. The
5235 generated stack slot may not be a valid memory address, so we
5236 have to check it and fix it if necessary. */
5237 start_sequence ();
5238 emit_move_insn (validize_mem (ret),
5239 crtl->args.internal_arg_pointer);
5240 seq = get_insns ();
5241 end_sequence ();
5243 push_topmost_sequence ();
5244 emit_insn_after (seq, entry_of_function ());
5245 pop_topmost_sequence ();
5247 crtl->arg_pointer_save_area_init = true;
5250 return ret;
5253 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5254 for the first time. */
5256 static void
5257 record_insns (rtx insns, rtx end, htab_t *hashp)
5259 rtx tmp;
5260 htab_t hash = *hashp;
5262 if (hash == NULL)
5263 *hashp = hash
5264 = htab_create_ggc (17, htab_hash_pointer, htab_eq_pointer, NULL);
5266 for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5268 void **slot = htab_find_slot (hash, tmp, INSERT);
5269 gcc_assert (*slot == NULL);
5270 *slot = tmp;
5274 /* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5275 basic block, splitting or peepholes. If INSN is a prologue or epilogue
5276 insn, then record COPY as well. */
5278 void
5279 maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5281 htab_t hash;
5282 void **slot;
5284 hash = epilogue_insn_hash;
5285 if (!hash || !htab_find (hash, insn))
5287 hash = prologue_insn_hash;
5288 if (!hash || !htab_find (hash, insn))
5289 return;
5292 slot = htab_find_slot (hash, copy, INSERT);
5293 gcc_assert (*slot == NULL);
5294 *slot = copy;
5297 /* Determine if any INSNs in HASH are, or are part of, INSN. Because
5298 we can be running after reorg, SEQUENCE rtl is possible. */
5300 static bool
5301 contains (const_rtx insn, htab_t hash)
5303 if (hash == NULL)
5304 return false;
5306 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5308 int i;
5309 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
5310 if (htab_find (hash, XVECEXP (PATTERN (insn), 0, i)))
5311 return true;
5312 return false;
5315 return htab_find (hash, insn) != NULL;
5319 prologue_epilogue_contains (const_rtx insn)
5321 if (contains (insn, prologue_insn_hash))
5322 return 1;
5323 if (contains (insn, epilogue_insn_hash))
5324 return 1;
5325 return 0;
5328 #ifdef HAVE_simple_return
5330 /* Return true if INSN requires the stack frame to be set up.
5331 PROLOGUE_USED contains the hard registers used in the function
5332 prologue. SET_UP_BY_PROLOGUE is the set of registers we expect the
5333 prologue to set up for the function. */
5334 bool
5335 requires_stack_frame_p (rtx insn, HARD_REG_SET prologue_used,
5336 HARD_REG_SET set_up_by_prologue)
5338 df_ref *df_rec;
5339 HARD_REG_SET hardregs;
5340 unsigned regno;
5342 if (CALL_P (insn))
5343 return !SIBLING_CALL_P (insn);
5345 /* We need a frame to get the unique CFA expected by the unwinder. */
5346 if (cfun->can_throw_non_call_exceptions && can_throw_internal (insn))
5347 return true;
5349 CLEAR_HARD_REG_SET (hardregs);
5350 for (df_rec = DF_INSN_DEFS (insn); *df_rec; df_rec++)
5352 rtx dreg = DF_REF_REG (*df_rec);
5354 if (!REG_P (dreg))
5355 continue;
5357 add_to_hard_reg_set (&hardregs, GET_MODE (dreg),
5358 REGNO (dreg));
5360 if (hard_reg_set_intersect_p (hardregs, prologue_used))
5361 return true;
5362 AND_COMPL_HARD_REG_SET (hardregs, call_used_reg_set);
5363 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5364 if (TEST_HARD_REG_BIT (hardregs, regno)
5365 && df_regs_ever_live_p (regno))
5366 return true;
5368 for (df_rec = DF_INSN_USES (insn); *df_rec; df_rec++)
5370 rtx reg = DF_REF_REG (*df_rec);
5372 if (!REG_P (reg))
5373 continue;
5375 add_to_hard_reg_set (&hardregs, GET_MODE (reg),
5376 REGNO (reg));
5378 if (hard_reg_set_intersect_p (hardregs, set_up_by_prologue))
5379 return true;
5381 return false;
5384 /* See whether BB has a single successor that uses [REGNO, END_REGNO),
5385 and if BB is its only predecessor. Return that block if so,
5386 otherwise return null. */
5388 static basic_block
5389 next_block_for_reg (basic_block bb, int regno, int end_regno)
5391 edge e, live_edge;
5392 edge_iterator ei;
5393 bitmap live;
5394 int i;
5396 live_edge = NULL;
5397 FOR_EACH_EDGE (e, ei, bb->succs)
5399 live = df_get_live_in (e->dest);
5400 for (i = regno; i < end_regno; i++)
5401 if (REGNO_REG_SET_P (live, i))
5403 if (live_edge && live_edge != e)
5404 return NULL;
5405 live_edge = e;
5409 /* We can sometimes encounter dead code. Don't try to move it
5410 into the exit block. */
5411 if (!live_edge || live_edge->dest == EXIT_BLOCK_PTR)
5412 return NULL;
5414 /* Reject targets of abnormal edges. This is needed for correctness
5415 on ports like Alpha and MIPS, whose pic_offset_table_rtx can die on
5416 exception edges even though it is generally treated as call-saved
5417 for the majority of the compilation. Moving across abnormal edges
5418 isn't going to be interesting for shrink-wrap usage anyway. */
5419 if (live_edge->flags & EDGE_ABNORMAL)
5420 return NULL;
5422 if (EDGE_COUNT (live_edge->dest->preds) > 1)
5423 return NULL;
5425 return live_edge->dest;
5428 /* Try to move INSN from BB to a successor. Return true on success.
5429 USES and DEFS are the set of registers that are used and defined
5430 after INSN in BB. */
5432 static bool
5433 move_insn_for_shrink_wrap (basic_block bb, rtx insn,
5434 const HARD_REG_SET uses,
5435 const HARD_REG_SET defs)
5437 rtx set, src, dest;
5438 bitmap live_out, live_in, bb_uses, bb_defs;
5439 unsigned int i, dregno, end_dregno, sregno, end_sregno;
5440 basic_block next_block;
5442 /* Look for a simple register copy. */
5443 set = single_set (insn);
5444 if (!set)
5445 return false;
5446 src = SET_SRC (set);
5447 dest = SET_DEST (set);
5448 if (!REG_P (dest) || !REG_P (src))
5449 return false;
5451 /* Make sure that the source register isn't defined later in BB. */
5452 sregno = REGNO (src);
5453 end_sregno = END_REGNO (src);
5454 if (overlaps_hard_reg_set_p (defs, GET_MODE (src), sregno))
5455 return false;
5457 /* Make sure that the destination register isn't referenced later in BB. */
5458 dregno = REGNO (dest);
5459 end_dregno = END_REGNO (dest);
5460 if (overlaps_hard_reg_set_p (uses, GET_MODE (dest), dregno)
5461 || overlaps_hard_reg_set_p (defs, GET_MODE (dest), dregno))
5462 return false;
5464 /* See whether there is a successor block to which we could move INSN. */
5465 next_block = next_block_for_reg (bb, dregno, end_dregno);
5466 if (!next_block)
5467 return false;
5469 /* At this point we are committed to moving INSN, but let's try to
5470 move it as far as we can. */
5473 live_out = df_get_live_out (bb);
5474 live_in = df_get_live_in (next_block);
5475 bb = next_block;
5477 /* Check whether BB uses DEST or clobbers DEST. We need to add
5478 INSN to BB if so. Either way, DEST is no longer live on entry,
5479 except for any part that overlaps SRC (next loop). */
5480 bb_uses = &DF_LR_BB_INFO (bb)->use;
5481 bb_defs = &DF_LR_BB_INFO (bb)->def;
5482 if (df_live)
5484 for (i = dregno; i < end_dregno; i++)
5486 if (REGNO_REG_SET_P (bb_uses, i) || REGNO_REG_SET_P (bb_defs, i)
5487 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
5488 next_block = NULL;
5489 CLEAR_REGNO_REG_SET (live_out, i);
5490 CLEAR_REGNO_REG_SET (live_in, i);
5493 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
5494 Either way, SRC is now live on entry. */
5495 for (i = sregno; i < end_sregno; i++)
5497 if (REGNO_REG_SET_P (bb_defs, i)
5498 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
5499 next_block = NULL;
5500 SET_REGNO_REG_SET (live_out, i);
5501 SET_REGNO_REG_SET (live_in, i);
5504 else
5506 /* DF_LR_BB_INFO (bb)->def does not comprise the DF_REF_PARTIAL and
5507 DF_REF_CONDITIONAL defs. So if DF_LIVE doesn't exist, i.e.
5508 at -O1, just give up searching NEXT_BLOCK. */
5509 next_block = NULL;
5510 for (i = dregno; i < end_dregno; i++)
5512 CLEAR_REGNO_REG_SET (live_out, i);
5513 CLEAR_REGNO_REG_SET (live_in, i);
5516 for (i = sregno; i < end_sregno; i++)
5518 SET_REGNO_REG_SET (live_out, i);
5519 SET_REGNO_REG_SET (live_in, i);
5523 /* If we don't need to add the move to BB, look for a single
5524 successor block. */
5525 if (next_block)
5526 next_block = next_block_for_reg (next_block, dregno, end_dregno);
5528 while (next_block);
5530 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
5531 (next loop). */
5532 for (i = dregno; i < end_dregno; i++)
5534 CLEAR_REGNO_REG_SET (bb_uses, i);
5535 SET_REGNO_REG_SET (bb_defs, i);
5538 /* BB now uses SRC. */
5539 for (i = sregno; i < end_sregno; i++)
5540 SET_REGNO_REG_SET (bb_uses, i);
5542 emit_insn_after (PATTERN (insn), bb_note (bb));
5543 delete_insn (insn);
5544 return true;
5547 /* Look for register copies in the first block of the function, and move
5548 them down into successor blocks if the register is used only on one
5549 path. This exposes more opportunities for shrink-wrapping. These
5550 kinds of sets often occur when incoming argument registers are moved
5551 to call-saved registers because their values are live across one or
5552 more calls during the function. */
5554 static void
5555 prepare_shrink_wrap (basic_block entry_block)
5557 rtx insn, curr, x;
5558 HARD_REG_SET uses, defs;
5559 df_ref *ref;
5561 CLEAR_HARD_REG_SET (uses);
5562 CLEAR_HARD_REG_SET (defs);
5563 FOR_BB_INSNS_REVERSE_SAFE (entry_block, insn, curr)
5564 if (NONDEBUG_INSN_P (insn)
5565 && !move_insn_for_shrink_wrap (entry_block, insn, uses, defs))
5567 /* Add all defined registers to DEFs. */
5568 for (ref = DF_INSN_DEFS (insn); *ref; ref++)
5570 x = DF_REF_REG (*ref);
5571 if (REG_P (x) && HARD_REGISTER_P (x))
5572 SET_HARD_REG_BIT (defs, REGNO (x));
5575 /* Add all used registers to USESs. */
5576 for (ref = DF_INSN_USES (insn); *ref; ref++)
5578 x = DF_REF_REG (*ref);
5579 if (REG_P (x) && HARD_REGISTER_P (x))
5580 SET_HARD_REG_BIT (uses, REGNO (x));
5585 #endif
5587 #ifdef HAVE_return
5588 /* Insert use of return register before the end of BB. */
5590 static void
5591 emit_use_return_register_into_block (basic_block bb)
5593 rtx seq, insn;
5594 start_sequence ();
5595 use_return_register ();
5596 seq = get_insns ();
5597 end_sequence ();
5598 insn = BB_END (bb);
5599 #ifdef HAVE_cc0
5600 if (reg_mentioned_p (cc0_rtx, PATTERN (insn)))
5601 insn = prev_cc0_setter (insn);
5602 #endif
5603 emit_insn_before (seq, insn);
5607 /* Create a return pattern, either simple_return or return, depending on
5608 simple_p. */
5610 static rtx
5611 gen_return_pattern (bool simple_p)
5613 #ifdef HAVE_simple_return
5614 return simple_p ? gen_simple_return () : gen_return ();
5615 #else
5616 gcc_assert (!simple_p);
5617 return gen_return ();
5618 #endif
5621 /* Insert an appropriate return pattern at the end of block BB. This
5622 also means updating block_for_insn appropriately. SIMPLE_P is
5623 the same as in gen_return_pattern and passed to it. */
5625 static void
5626 emit_return_into_block (bool simple_p, basic_block bb)
5628 rtx jump, pat;
5629 jump = emit_jump_insn_after (gen_return_pattern (simple_p), BB_END (bb));
5630 pat = PATTERN (jump);
5631 if (GET_CODE (pat) == PARALLEL)
5632 pat = XVECEXP (pat, 0, 0);
5633 gcc_assert (ANY_RETURN_P (pat));
5634 JUMP_LABEL (jump) = pat;
5636 #endif
5638 /* Set JUMP_LABEL for a return insn. */
5640 void
5641 set_return_jump_label (rtx returnjump)
5643 rtx pat = PATTERN (returnjump);
5644 if (GET_CODE (pat) == PARALLEL)
5645 pat = XVECEXP (pat, 0, 0);
5646 if (ANY_RETURN_P (pat))
5647 JUMP_LABEL (returnjump) = pat;
5648 else
5649 JUMP_LABEL (returnjump) = ret_rtx;
5652 #ifdef HAVE_simple_return
5653 /* Create a copy of BB instructions and insert at BEFORE. Redirect
5654 preds of BB to COPY_BB if they don't appear in NEED_PROLOGUE. */
5655 static void
5656 dup_block_and_redirect (basic_block bb, basic_block copy_bb, rtx before,
5657 bitmap_head *need_prologue)
5659 edge_iterator ei;
5660 edge e;
5661 rtx insn = BB_END (bb);
5663 /* We know BB has a single successor, so there is no need to copy a
5664 simple jump at the end of BB. */
5665 if (simplejump_p (insn))
5666 insn = PREV_INSN (insn);
5668 start_sequence ();
5669 duplicate_insn_chain (BB_HEAD (bb), insn);
5670 if (dump_file)
5672 unsigned count = 0;
5673 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5674 if (active_insn_p (insn))
5675 ++count;
5676 fprintf (dump_file, "Duplicating bb %d to bb %d, %u active insns.\n",
5677 bb->index, copy_bb->index, count);
5679 insn = get_insns ();
5680 end_sequence ();
5681 emit_insn_before (insn, before);
5683 /* Redirect all the paths that need no prologue into copy_bb. */
5684 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
5685 if (!bitmap_bit_p (need_prologue, e->src->index))
5687 int freq = EDGE_FREQUENCY (e);
5688 copy_bb->count += e->count;
5689 copy_bb->frequency += EDGE_FREQUENCY (e);
5690 e->dest->count -= e->count;
5691 if (e->dest->count < 0)
5692 e->dest->count = 0;
5693 e->dest->frequency -= freq;
5694 if (e->dest->frequency < 0)
5695 e->dest->frequency = 0;
5696 redirect_edge_and_branch_force (e, copy_bb);
5697 continue;
5699 else
5700 ei_next (&ei);
5702 #endif
5704 #if defined (HAVE_return) || defined (HAVE_simple_return)
5705 /* Return true if there are any active insns between HEAD and TAIL. */
5706 static bool
5707 active_insn_between (rtx head, rtx tail)
5709 while (tail)
5711 if (active_insn_p (tail))
5712 return true;
5713 if (tail == head)
5714 return false;
5715 tail = PREV_INSN (tail);
5717 return false;
5720 /* LAST_BB is a block that exits, and empty of active instructions.
5721 Examine its predecessors for jumps that can be converted to
5722 (conditional) returns. */
5723 static vec<edge>
5724 convert_jumps_to_returns (basic_block last_bb, bool simple_p,
5725 vec<edge> unconverted ATTRIBUTE_UNUSED)
5727 int i;
5728 basic_block bb;
5729 rtx label;
5730 edge_iterator ei;
5731 edge e;
5732 vec<basic_block> src_bbs;
5734 src_bbs.create (EDGE_COUNT (last_bb->preds));
5735 FOR_EACH_EDGE (e, ei, last_bb->preds)
5736 if (e->src != ENTRY_BLOCK_PTR)
5737 src_bbs.quick_push (e->src);
5739 label = BB_HEAD (last_bb);
5741 FOR_EACH_VEC_ELT (src_bbs, i, bb)
5743 rtx jump = BB_END (bb);
5745 if (!JUMP_P (jump) || JUMP_LABEL (jump) != label)
5746 continue;
5748 e = find_edge (bb, last_bb);
5750 /* If we have an unconditional jump, we can replace that
5751 with a simple return instruction. */
5752 if (simplejump_p (jump))
5754 /* The use of the return register might be present in the exit
5755 fallthru block. Either:
5756 - removing the use is safe, and we should remove the use in
5757 the exit fallthru block, or
5758 - removing the use is not safe, and we should add it here.
5759 For now, we conservatively choose the latter. Either of the
5760 2 helps in crossjumping. */
5761 emit_use_return_register_into_block (bb);
5763 emit_return_into_block (simple_p, bb);
5764 delete_insn (jump);
5767 /* If we have a conditional jump branching to the last
5768 block, we can try to replace that with a conditional
5769 return instruction. */
5770 else if (condjump_p (jump))
5772 rtx dest;
5774 if (simple_p)
5775 dest = simple_return_rtx;
5776 else
5777 dest = ret_rtx;
5778 if (!redirect_jump (jump, dest, 0))
5780 #ifdef HAVE_simple_return
5781 if (simple_p)
5783 if (dump_file)
5784 fprintf (dump_file,
5785 "Failed to redirect bb %d branch.\n", bb->index);
5786 unconverted.safe_push (e);
5788 #endif
5789 continue;
5792 /* See comment in simplejump_p case above. */
5793 emit_use_return_register_into_block (bb);
5795 /* If this block has only one successor, it both jumps
5796 and falls through to the fallthru block, so we can't
5797 delete the edge. */
5798 if (single_succ_p (bb))
5799 continue;
5801 else
5803 #ifdef HAVE_simple_return
5804 if (simple_p)
5806 if (dump_file)
5807 fprintf (dump_file,
5808 "Failed to redirect bb %d branch.\n", bb->index);
5809 unconverted.safe_push (e);
5811 #endif
5812 continue;
5815 /* Fix up the CFG for the successful change we just made. */
5816 redirect_edge_succ (e, EXIT_BLOCK_PTR);
5817 e->flags &= ~EDGE_CROSSING;
5819 src_bbs.release ();
5820 return unconverted;
5823 /* Emit a return insn for the exit fallthru block. */
5824 static basic_block
5825 emit_return_for_exit (edge exit_fallthru_edge, bool simple_p)
5827 basic_block last_bb = exit_fallthru_edge->src;
5829 if (JUMP_P (BB_END (last_bb)))
5831 last_bb = split_edge (exit_fallthru_edge);
5832 exit_fallthru_edge = single_succ_edge (last_bb);
5834 emit_barrier_after (BB_END (last_bb));
5835 emit_return_into_block (simple_p, last_bb);
5836 exit_fallthru_edge->flags &= ~EDGE_FALLTHRU;
5837 return last_bb;
5839 #endif
5842 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
5843 this into place with notes indicating where the prologue ends and where
5844 the epilogue begins. Update the basic block information when possible.
5846 Notes on epilogue placement:
5847 There are several kinds of edges to the exit block:
5848 * a single fallthru edge from LAST_BB
5849 * possibly, edges from blocks containing sibcalls
5850 * possibly, fake edges from infinite loops
5852 The epilogue is always emitted on the fallthru edge from the last basic
5853 block in the function, LAST_BB, into the exit block.
5855 If LAST_BB is empty except for a label, it is the target of every
5856 other basic block in the function that ends in a return. If a
5857 target has a return or simple_return pattern (possibly with
5858 conditional variants), these basic blocks can be changed so that a
5859 return insn is emitted into them, and their target is adjusted to
5860 the real exit block.
5862 Notes on shrink wrapping: We implement a fairly conservative
5863 version of shrink-wrapping rather than the textbook one. We only
5864 generate a single prologue and a single epilogue. This is
5865 sufficient to catch a number of interesting cases involving early
5866 exits.
5868 First, we identify the blocks that require the prologue to occur before
5869 them. These are the ones that modify a call-saved register, or reference
5870 any of the stack or frame pointer registers. To simplify things, we then
5871 mark everything reachable from these blocks as also requiring a prologue.
5872 This takes care of loops automatically, and avoids the need to examine
5873 whether MEMs reference the frame, since it is sufficient to check for
5874 occurrences of the stack or frame pointer.
5876 We then compute the set of blocks for which the need for a prologue
5877 is anticipatable (borrowing terminology from the shrink-wrapping
5878 description in Muchnick's book). These are the blocks which either
5879 require a prologue themselves, or those that have only successors
5880 where the prologue is anticipatable. The prologue needs to be
5881 inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
5882 is not. For the moment, we ensure that only one such edge exists.
5884 The epilogue is placed as described above, but we make a
5885 distinction between inserting return and simple_return patterns
5886 when modifying other blocks that end in a return. Blocks that end
5887 in a sibcall omit the sibcall_epilogue if the block is not in
5888 ANTIC. */
5890 static void
5891 thread_prologue_and_epilogue_insns (void)
5893 bool inserted;
5894 #ifdef HAVE_simple_return
5895 vec<edge> unconverted_simple_returns = vNULL;
5896 bool nonempty_prologue;
5897 bitmap_head bb_flags;
5898 unsigned max_grow_size;
5899 #endif
5900 rtx returnjump;
5901 rtx seq ATTRIBUTE_UNUSED, epilogue_end ATTRIBUTE_UNUSED;
5902 rtx prologue_seq ATTRIBUTE_UNUSED, split_prologue_seq ATTRIBUTE_UNUSED;
5903 edge e, entry_edge, orig_entry_edge, exit_fallthru_edge;
5904 edge_iterator ei;
5906 df_analyze ();
5908 rtl_profile_for_bb (ENTRY_BLOCK_PTR);
5910 inserted = false;
5911 seq = NULL_RTX;
5912 epilogue_end = NULL_RTX;
5913 returnjump = NULL_RTX;
5915 /* Can't deal with multiple successors of the entry block at the
5916 moment. Function should always have at least one entry
5917 point. */
5918 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR));
5919 entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
5920 orig_entry_edge = entry_edge;
5922 split_prologue_seq = NULL_RTX;
5923 if (flag_split_stack
5924 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
5925 == NULL))
5927 #ifndef HAVE_split_stack_prologue
5928 gcc_unreachable ();
5929 #else
5930 gcc_assert (HAVE_split_stack_prologue);
5932 start_sequence ();
5933 emit_insn (gen_split_stack_prologue ());
5934 split_prologue_seq = get_insns ();
5935 end_sequence ();
5937 record_insns (split_prologue_seq, NULL, &prologue_insn_hash);
5938 set_insn_locations (split_prologue_seq, prologue_location);
5939 #endif
5942 prologue_seq = NULL_RTX;
5943 #ifdef HAVE_prologue
5944 if (HAVE_prologue)
5946 start_sequence ();
5947 seq = gen_prologue ();
5948 emit_insn (seq);
5950 /* Insert an explicit USE for the frame pointer
5951 if the profiling is on and the frame pointer is required. */
5952 if (crtl->profile && frame_pointer_needed)
5953 emit_use (hard_frame_pointer_rtx);
5955 /* Retain a map of the prologue insns. */
5956 record_insns (seq, NULL, &prologue_insn_hash);
5957 emit_note (NOTE_INSN_PROLOGUE_END);
5959 /* Ensure that instructions are not moved into the prologue when
5960 profiling is on. The call to the profiling routine can be
5961 emitted within the live range of a call-clobbered register. */
5962 if (!targetm.profile_before_prologue () && crtl->profile)
5963 emit_insn (gen_blockage ());
5965 prologue_seq = get_insns ();
5966 end_sequence ();
5967 set_insn_locations (prologue_seq, prologue_location);
5969 #endif
5971 #ifdef HAVE_simple_return
5972 bitmap_initialize (&bb_flags, &bitmap_default_obstack);
5974 /* Try to perform a kind of shrink-wrapping, making sure the
5975 prologue/epilogue is emitted only around those parts of the
5976 function that require it. */
5978 nonempty_prologue = false;
5979 for (seq = prologue_seq; seq; seq = NEXT_INSN (seq))
5980 if (!NOTE_P (seq) || NOTE_KIND (seq) != NOTE_INSN_PROLOGUE_END)
5982 nonempty_prologue = true;
5983 break;
5986 if (flag_shrink_wrap && HAVE_simple_return
5987 && (targetm.profile_before_prologue () || !crtl->profile)
5988 && nonempty_prologue && !crtl->calls_eh_return)
5990 HARD_REG_SET prologue_clobbered, prologue_used, live_on_edge;
5991 struct hard_reg_set_container set_up_by_prologue;
5992 rtx p_insn;
5993 vec<basic_block> vec;
5994 basic_block bb;
5995 bitmap_head bb_antic_flags;
5996 bitmap_head bb_on_list;
5997 bitmap_head bb_tail;
5999 if (dump_file)
6000 fprintf (dump_file, "Attempting shrink-wrapping optimization.\n");
6002 /* Compute the registers set and used in the prologue. */
6003 CLEAR_HARD_REG_SET (prologue_clobbered);
6004 CLEAR_HARD_REG_SET (prologue_used);
6005 for (p_insn = prologue_seq; p_insn; p_insn = NEXT_INSN (p_insn))
6007 HARD_REG_SET this_used;
6008 if (!NONDEBUG_INSN_P (p_insn))
6009 continue;
6011 CLEAR_HARD_REG_SET (this_used);
6012 note_uses (&PATTERN (p_insn), record_hard_reg_uses,
6013 &this_used);
6014 AND_COMPL_HARD_REG_SET (this_used, prologue_clobbered);
6015 IOR_HARD_REG_SET (prologue_used, this_used);
6016 note_stores (PATTERN (p_insn), record_hard_reg_sets,
6017 &prologue_clobbered);
6020 prepare_shrink_wrap (entry_edge->dest);
6022 bitmap_initialize (&bb_antic_flags, &bitmap_default_obstack);
6023 bitmap_initialize (&bb_on_list, &bitmap_default_obstack);
6024 bitmap_initialize (&bb_tail, &bitmap_default_obstack);
6026 /* Find the set of basic blocks that require a stack frame,
6027 and blocks that are too big to be duplicated. */
6029 vec.create (n_basic_blocks);
6031 CLEAR_HARD_REG_SET (set_up_by_prologue.set);
6032 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
6033 STACK_POINTER_REGNUM);
6034 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode, ARG_POINTER_REGNUM);
6035 if (frame_pointer_needed)
6036 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
6037 HARD_FRAME_POINTER_REGNUM);
6038 if (pic_offset_table_rtx)
6039 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
6040 PIC_OFFSET_TABLE_REGNUM);
6041 if (crtl->drap_reg)
6042 add_to_hard_reg_set (&set_up_by_prologue.set,
6043 GET_MODE (crtl->drap_reg),
6044 REGNO (crtl->drap_reg));
6045 if (targetm.set_up_by_prologue)
6046 targetm.set_up_by_prologue (&set_up_by_prologue);
6048 /* We don't use a different max size depending on
6049 optimize_bb_for_speed_p because increasing shrink-wrapping
6050 opportunities by duplicating tail blocks can actually result
6051 in an overall decrease in code size. */
6052 max_grow_size = get_uncond_jump_length ();
6053 max_grow_size *= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS);
6055 FOR_EACH_BB (bb)
6057 rtx insn;
6058 unsigned size = 0;
6060 FOR_BB_INSNS (bb, insn)
6061 if (NONDEBUG_INSN_P (insn))
6063 if (requires_stack_frame_p (insn, prologue_used,
6064 set_up_by_prologue.set))
6066 if (bb == entry_edge->dest)
6067 goto fail_shrinkwrap;
6068 bitmap_set_bit (&bb_flags, bb->index);
6069 vec.quick_push (bb);
6070 break;
6072 else if (size <= max_grow_size)
6074 size += get_attr_min_length (insn);
6075 if (size > max_grow_size)
6076 bitmap_set_bit (&bb_on_list, bb->index);
6081 /* Blocks that really need a prologue, or are too big for tails. */
6082 bitmap_ior_into (&bb_on_list, &bb_flags);
6084 /* For every basic block that needs a prologue, mark all blocks
6085 reachable from it, so as to ensure they are also seen as
6086 requiring a prologue. */
6087 while (!vec.is_empty ())
6089 basic_block tmp_bb = vec.pop ();
6091 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
6092 if (e->dest != EXIT_BLOCK_PTR
6093 && bitmap_set_bit (&bb_flags, e->dest->index))
6094 vec.quick_push (e->dest);
6097 /* Find the set of basic blocks that need no prologue, have a
6098 single successor, can be duplicated, meet a max size
6099 requirement, and go to the exit via like blocks. */
6100 vec.quick_push (EXIT_BLOCK_PTR);
6101 while (!vec.is_empty ())
6103 basic_block tmp_bb = vec.pop ();
6105 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
6106 if (single_succ_p (e->src)
6107 && !bitmap_bit_p (&bb_on_list, e->src->index)
6108 && can_duplicate_block_p (e->src))
6110 edge pe;
6111 edge_iterator pei;
6113 /* If there is predecessor of e->src which doesn't
6114 need prologue and the edge is complex,
6115 we might not be able to redirect the branch
6116 to a copy of e->src. */
6117 FOR_EACH_EDGE (pe, pei, e->src->preds)
6118 if ((pe->flags & EDGE_COMPLEX) != 0
6119 && !bitmap_bit_p (&bb_flags, pe->src->index))
6120 break;
6121 if (pe == NULL && bitmap_set_bit (&bb_tail, e->src->index))
6122 vec.quick_push (e->src);
6126 /* Now walk backwards from every block that is marked as needing
6127 a prologue to compute the bb_antic_flags bitmap. Exclude
6128 tail blocks; They can be duplicated to be used on paths not
6129 needing a prologue. */
6130 bitmap_clear (&bb_on_list);
6131 bitmap_and_compl (&bb_antic_flags, &bb_flags, &bb_tail);
6132 FOR_EACH_BB (bb)
6134 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
6135 continue;
6136 FOR_EACH_EDGE (e, ei, bb->preds)
6137 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
6138 && bitmap_set_bit (&bb_on_list, e->src->index))
6139 vec.quick_push (e->src);
6141 while (!vec.is_empty ())
6143 basic_block tmp_bb = vec.pop ();
6144 bool all_set = true;
6146 bitmap_clear_bit (&bb_on_list, tmp_bb->index);
6147 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
6148 if (!bitmap_bit_p (&bb_antic_flags, e->dest->index))
6150 all_set = false;
6151 break;
6154 if (all_set)
6156 bitmap_set_bit (&bb_antic_flags, tmp_bb->index);
6157 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
6158 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
6159 && bitmap_set_bit (&bb_on_list, e->src->index))
6160 vec.quick_push (e->src);
6163 /* Find exactly one edge that leads to a block in ANTIC from
6164 a block that isn't. */
6165 if (!bitmap_bit_p (&bb_antic_flags, entry_edge->dest->index))
6166 FOR_EACH_BB (bb)
6168 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
6169 continue;
6170 FOR_EACH_EDGE (e, ei, bb->preds)
6171 if (!bitmap_bit_p (&bb_antic_flags, e->src->index))
6173 if (entry_edge != orig_entry_edge)
6175 entry_edge = orig_entry_edge;
6176 if (dump_file)
6177 fprintf (dump_file, "More than one candidate edge.\n");
6178 goto fail_shrinkwrap;
6180 if (dump_file)
6181 fprintf (dump_file, "Found candidate edge for "
6182 "shrink-wrapping, %d->%d.\n", e->src->index,
6183 e->dest->index);
6184 entry_edge = e;
6188 if (entry_edge != orig_entry_edge)
6190 /* Test whether the prologue is known to clobber any register
6191 (other than FP or SP) which are live on the edge. */
6192 CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
6193 if (frame_pointer_needed)
6194 CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
6195 REG_SET_TO_HARD_REG_SET (live_on_edge,
6196 df_get_live_in (entry_edge->dest));
6197 if (hard_reg_set_intersect_p (live_on_edge, prologue_clobbered))
6199 entry_edge = orig_entry_edge;
6200 if (dump_file)
6201 fprintf (dump_file,
6202 "Shrink-wrapping aborted due to clobber.\n");
6205 if (entry_edge != orig_entry_edge)
6207 crtl->shrink_wrapped = true;
6208 if (dump_file)
6209 fprintf (dump_file, "Performing shrink-wrapping.\n");
6211 /* Find tail blocks reachable from both blocks needing a
6212 prologue and blocks not needing a prologue. */
6213 if (!bitmap_empty_p (&bb_tail))
6214 FOR_EACH_BB (bb)
6216 bool some_pro, some_no_pro;
6217 if (!bitmap_bit_p (&bb_tail, bb->index))
6218 continue;
6219 some_pro = some_no_pro = false;
6220 FOR_EACH_EDGE (e, ei, bb->preds)
6222 if (bitmap_bit_p (&bb_flags, e->src->index))
6223 some_pro = true;
6224 else
6225 some_no_pro = true;
6227 if (some_pro && some_no_pro)
6228 vec.quick_push (bb);
6229 else
6230 bitmap_clear_bit (&bb_tail, bb->index);
6232 /* Find the head of each tail. */
6233 while (!vec.is_empty ())
6235 basic_block tbb = vec.pop ();
6237 if (!bitmap_bit_p (&bb_tail, tbb->index))
6238 continue;
6240 while (single_succ_p (tbb))
6242 tbb = single_succ (tbb);
6243 bitmap_clear_bit (&bb_tail, tbb->index);
6246 /* Now duplicate the tails. */
6247 if (!bitmap_empty_p (&bb_tail))
6248 FOR_EACH_BB_REVERSE (bb)
6250 basic_block copy_bb, tbb;
6251 rtx insert_point;
6252 int eflags;
6254 if (!bitmap_clear_bit (&bb_tail, bb->index))
6255 continue;
6257 /* Create a copy of BB, instructions and all, for
6258 use on paths that don't need a prologue.
6259 Ideal placement of the copy is on a fall-thru edge
6260 or after a block that would jump to the copy. */
6261 FOR_EACH_EDGE (e, ei, bb->preds)
6262 if (!bitmap_bit_p (&bb_flags, e->src->index)
6263 && single_succ_p (e->src))
6264 break;
6265 if (e)
6267 /* Make sure we insert after any barriers. */
6268 rtx end = get_last_bb_insn (e->src);
6269 copy_bb = create_basic_block (NEXT_INSN (end),
6270 NULL_RTX, e->src);
6271 BB_COPY_PARTITION (copy_bb, e->src);
6273 else
6275 /* Otherwise put the copy at the end of the function. */
6276 copy_bb = create_basic_block (NULL_RTX, NULL_RTX,
6277 EXIT_BLOCK_PTR->prev_bb);
6278 BB_COPY_PARTITION (copy_bb, bb);
6281 insert_point = emit_note_after (NOTE_INSN_DELETED,
6282 BB_END (copy_bb));
6283 emit_barrier_after (BB_END (copy_bb));
6285 tbb = bb;
6286 while (1)
6288 dup_block_and_redirect (tbb, copy_bb, insert_point,
6289 &bb_flags);
6290 tbb = single_succ (tbb);
6291 if (tbb == EXIT_BLOCK_PTR)
6292 break;
6293 e = split_block (copy_bb, PREV_INSN (insert_point));
6294 copy_bb = e->dest;
6297 /* Quiet verify_flow_info by (ab)using EDGE_FAKE.
6298 We have yet to add a simple_return to the tails,
6299 as we'd like to first convert_jumps_to_returns in
6300 case the block is no longer used after that. */
6301 eflags = EDGE_FAKE;
6302 if (CALL_P (PREV_INSN (insert_point))
6303 && SIBLING_CALL_P (PREV_INSN (insert_point)))
6304 eflags = EDGE_SIBCALL | EDGE_ABNORMAL;
6305 make_single_succ_edge (copy_bb, EXIT_BLOCK_PTR, eflags);
6307 /* verify_flow_info doesn't like a note after a
6308 sibling call. */
6309 delete_insn (insert_point);
6310 if (bitmap_empty_p (&bb_tail))
6311 break;
6315 fail_shrinkwrap:
6316 bitmap_clear (&bb_tail);
6317 bitmap_clear (&bb_antic_flags);
6318 bitmap_clear (&bb_on_list);
6319 vec.release ();
6321 #endif
6323 if (split_prologue_seq != NULL_RTX)
6325 insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
6326 inserted = true;
6328 if (prologue_seq != NULL_RTX)
6330 insert_insn_on_edge (prologue_seq, entry_edge);
6331 inserted = true;
6334 /* If the exit block has no non-fake predecessors, we don't need
6335 an epilogue. */
6336 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6337 if ((e->flags & EDGE_FAKE) == 0)
6338 break;
6339 if (e == NULL)
6340 goto epilogue_done;
6342 rtl_profile_for_bb (EXIT_BLOCK_PTR);
6344 exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR->preds);
6346 /* If we're allowed to generate a simple return instruction, then by
6347 definition we don't need a full epilogue. If the last basic
6348 block before the exit block does not contain active instructions,
6349 examine its predecessors and try to emit (conditional) return
6350 instructions. */
6351 #ifdef HAVE_simple_return
6352 if (entry_edge != orig_entry_edge)
6354 if (optimize)
6356 unsigned i, last;
6358 /* convert_jumps_to_returns may add to EXIT_BLOCK_PTR->preds
6359 (but won't remove). Stop at end of current preds. */
6360 last = EDGE_COUNT (EXIT_BLOCK_PTR->preds);
6361 for (i = 0; i < last; i++)
6363 e = EDGE_I (EXIT_BLOCK_PTR->preds, i);
6364 if (LABEL_P (BB_HEAD (e->src))
6365 && !bitmap_bit_p (&bb_flags, e->src->index)
6366 && !active_insn_between (BB_HEAD (e->src), BB_END (e->src)))
6367 unconverted_simple_returns
6368 = convert_jumps_to_returns (e->src, true,
6369 unconverted_simple_returns);
6373 if (exit_fallthru_edge != NULL
6374 && EDGE_COUNT (exit_fallthru_edge->src->preds) != 0
6375 && !bitmap_bit_p (&bb_flags, exit_fallthru_edge->src->index))
6377 basic_block last_bb;
6379 last_bb = emit_return_for_exit (exit_fallthru_edge, true);
6380 returnjump = BB_END (last_bb);
6381 exit_fallthru_edge = NULL;
6384 #endif
6385 #ifdef HAVE_return
6386 if (HAVE_return)
6388 if (exit_fallthru_edge == NULL)
6389 goto epilogue_done;
6391 if (optimize)
6393 basic_block last_bb = exit_fallthru_edge->src;
6395 if (LABEL_P (BB_HEAD (last_bb))
6396 && !active_insn_between (BB_HEAD (last_bb), BB_END (last_bb)))
6397 convert_jumps_to_returns (last_bb, false, vNULL);
6399 if (EDGE_COUNT (last_bb->preds) != 0
6400 && single_succ_p (last_bb))
6402 last_bb = emit_return_for_exit (exit_fallthru_edge, false);
6403 epilogue_end = returnjump = BB_END (last_bb);
6404 #ifdef HAVE_simple_return
6405 /* Emitting the return may add a basic block.
6406 Fix bb_flags for the added block. */
6407 if (last_bb != exit_fallthru_edge->src)
6408 bitmap_set_bit (&bb_flags, last_bb->index);
6409 #endif
6410 goto epilogue_done;
6414 #endif
6416 /* A small fib -- epilogue is not yet completed, but we wish to re-use
6417 this marker for the splits of EH_RETURN patterns, and nothing else
6418 uses the flag in the meantime. */
6419 epilogue_completed = 1;
6421 #ifdef HAVE_eh_return
6422 /* Find non-fallthru edges that end with EH_RETURN instructions. On
6423 some targets, these get split to a special version of the epilogue
6424 code. In order to be able to properly annotate these with unwind
6425 info, try to split them now. If we get a valid split, drop an
6426 EPILOGUE_BEG note and mark the insns as epilogue insns. */
6427 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6429 rtx prev, last, trial;
6431 if (e->flags & EDGE_FALLTHRU)
6432 continue;
6433 last = BB_END (e->src);
6434 if (!eh_returnjump_p (last))
6435 continue;
6437 prev = PREV_INSN (last);
6438 trial = try_split (PATTERN (last), last, 1);
6439 if (trial == last)
6440 continue;
6442 record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
6443 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
6445 #endif
6447 /* If nothing falls through into the exit block, we don't need an
6448 epilogue. */
6450 if (exit_fallthru_edge == NULL)
6451 goto epilogue_done;
6453 #ifdef HAVE_epilogue
6454 if (HAVE_epilogue)
6456 start_sequence ();
6457 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
6458 seq = gen_epilogue ();
6459 if (seq)
6460 emit_jump_insn (seq);
6462 /* Retain a map of the epilogue insns. */
6463 record_insns (seq, NULL, &epilogue_insn_hash);
6464 set_insn_locations (seq, epilogue_location);
6466 seq = get_insns ();
6467 returnjump = get_last_insn ();
6468 end_sequence ();
6470 insert_insn_on_edge (seq, exit_fallthru_edge);
6471 inserted = true;
6473 if (JUMP_P (returnjump))
6474 set_return_jump_label (returnjump);
6476 else
6477 #endif
6479 basic_block cur_bb;
6481 if (! next_active_insn (BB_END (exit_fallthru_edge->src)))
6482 goto epilogue_done;
6483 /* We have a fall-through edge to the exit block, the source is not
6484 at the end of the function, and there will be an assembler epilogue
6485 at the end of the function.
6486 We can't use force_nonfallthru here, because that would try to
6487 use return. Inserting a jump 'by hand' is extremely messy, so
6488 we take advantage of cfg_layout_finalize using
6489 fixup_fallthru_exit_predecessor. */
6490 cfg_layout_initialize (0);
6491 FOR_EACH_BB (cur_bb)
6492 if (cur_bb->index >= NUM_FIXED_BLOCKS
6493 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
6494 cur_bb->aux = cur_bb->next_bb;
6495 cfg_layout_finalize ();
6498 epilogue_done:
6500 default_rtl_profile ();
6502 if (inserted)
6504 sbitmap blocks;
6506 commit_edge_insertions ();
6508 /* Look for basic blocks within the prologue insns. */
6509 blocks = sbitmap_alloc (last_basic_block);
6510 bitmap_clear (blocks);
6511 bitmap_set_bit (blocks, entry_edge->dest->index);
6512 bitmap_set_bit (blocks, orig_entry_edge->dest->index);
6513 find_many_sub_basic_blocks (blocks);
6514 sbitmap_free (blocks);
6516 /* The epilogue insns we inserted may cause the exit edge to no longer
6517 be fallthru. */
6518 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6520 if (((e->flags & EDGE_FALLTHRU) != 0)
6521 && returnjump_p (BB_END (e->src)))
6522 e->flags &= ~EDGE_FALLTHRU;
6526 #ifdef HAVE_simple_return
6527 /* If there were branches to an empty LAST_BB which we tried to
6528 convert to conditional simple_returns, but couldn't for some
6529 reason, create a block to hold a simple_return insn and redirect
6530 those remaining edges. */
6531 if (!unconverted_simple_returns.is_empty ())
6533 basic_block simple_return_block_hot = NULL;
6534 basic_block simple_return_block_cold = NULL;
6535 edge pending_edge_hot = NULL;
6536 edge pending_edge_cold = NULL;
6537 basic_block exit_pred;
6538 int i;
6540 gcc_assert (entry_edge != orig_entry_edge);
6542 /* See if we can reuse the last insn that was emitted for the
6543 epilogue. */
6544 if (returnjump != NULL_RTX
6545 && JUMP_LABEL (returnjump) == simple_return_rtx)
6547 e = split_block (BLOCK_FOR_INSN (returnjump), PREV_INSN (returnjump));
6548 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
6549 simple_return_block_hot = e->dest;
6550 else
6551 simple_return_block_cold = e->dest;
6554 /* Also check returns we might need to add to tail blocks. */
6555 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6556 if (EDGE_COUNT (e->src->preds) != 0
6557 && (e->flags & EDGE_FAKE) != 0
6558 && !bitmap_bit_p (&bb_flags, e->src->index))
6560 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
6561 pending_edge_hot = e;
6562 else
6563 pending_edge_cold = e;
6566 /* Save a pointer to the exit's predecessor BB for use in
6567 inserting new BBs at the end of the function. Do this
6568 after the call to split_block above which may split
6569 the original exit pred. */
6570 exit_pred = EXIT_BLOCK_PTR->prev_bb;
6572 FOR_EACH_VEC_ELT (unconverted_simple_returns, i, e)
6574 basic_block *pdest_bb;
6575 edge pending;
6577 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
6579 pdest_bb = &simple_return_block_hot;
6580 pending = pending_edge_hot;
6582 else
6584 pdest_bb = &simple_return_block_cold;
6585 pending = pending_edge_cold;
6588 if (*pdest_bb == NULL && pending != NULL)
6590 emit_return_into_block (true, pending->src);
6591 pending->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
6592 *pdest_bb = pending->src;
6594 else if (*pdest_bb == NULL)
6596 basic_block bb;
6597 rtx start;
6599 bb = create_basic_block (NULL, NULL, exit_pred);
6600 BB_COPY_PARTITION (bb, e->src);
6601 start = emit_jump_insn_after (gen_simple_return (),
6602 BB_END (bb));
6603 JUMP_LABEL (start) = simple_return_rtx;
6604 emit_barrier_after (start);
6606 *pdest_bb = bb;
6607 make_edge (bb, EXIT_BLOCK_PTR, 0);
6609 redirect_edge_and_branch_force (e, *pdest_bb);
6611 unconverted_simple_returns.release ();
6614 if (entry_edge != orig_entry_edge)
6616 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6617 if (EDGE_COUNT (e->src->preds) != 0
6618 && (e->flags & EDGE_FAKE) != 0
6619 && !bitmap_bit_p (&bb_flags, e->src->index))
6621 emit_return_into_block (true, e->src);
6622 e->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
6625 #endif
6627 #ifdef HAVE_sibcall_epilogue
6628 /* Emit sibling epilogues before any sibling call sites. */
6629 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
6631 basic_block bb = e->src;
6632 rtx insn = BB_END (bb);
6633 rtx ep_seq;
6635 if (!CALL_P (insn)
6636 || ! SIBLING_CALL_P (insn)
6637 #ifdef HAVE_simple_return
6638 || (entry_edge != orig_entry_edge
6639 && !bitmap_bit_p (&bb_flags, bb->index))
6640 #endif
6643 ei_next (&ei);
6644 continue;
6647 ep_seq = gen_sibcall_epilogue ();
6648 if (ep_seq)
6650 start_sequence ();
6651 emit_note (NOTE_INSN_EPILOGUE_BEG);
6652 emit_insn (ep_seq);
6653 seq = get_insns ();
6654 end_sequence ();
6656 /* Retain a map of the epilogue insns. Used in life analysis to
6657 avoid getting rid of sibcall epilogue insns. Do this before we
6658 actually emit the sequence. */
6659 record_insns (seq, NULL, &epilogue_insn_hash);
6660 set_insn_locations (seq, epilogue_location);
6662 emit_insn_before (seq, insn);
6664 ei_next (&ei);
6666 #endif
6668 #ifdef HAVE_epilogue
6669 if (epilogue_end)
6671 rtx insn, next;
6673 /* Similarly, move any line notes that appear after the epilogue.
6674 There is no need, however, to be quite so anal about the existence
6675 of such a note. Also possibly move
6676 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
6677 info generation. */
6678 for (insn = epilogue_end; insn; insn = next)
6680 next = NEXT_INSN (insn);
6681 if (NOTE_P (insn)
6682 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
6683 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
6686 #endif
6688 #ifdef HAVE_simple_return
6689 bitmap_clear (&bb_flags);
6690 #endif
6692 /* Threading the prologue and epilogue changes the artificial refs
6693 in the entry and exit blocks. */
6694 epilogue_completed = 1;
6695 df_update_entry_exit_and_calls ();
6698 /* Reposition the prologue-end and epilogue-begin notes after
6699 instruction scheduling. */
6701 void
6702 reposition_prologue_and_epilogue_notes (void)
6704 #if defined (HAVE_prologue) || defined (HAVE_epilogue) \
6705 || defined (HAVE_sibcall_epilogue)
6706 /* Since the hash table is created on demand, the fact that it is
6707 non-null is a signal that it is non-empty. */
6708 if (prologue_insn_hash != NULL)
6710 size_t len = htab_elements (prologue_insn_hash);
6711 rtx insn, last = NULL, note = NULL;
6713 /* Scan from the beginning until we reach the last prologue insn. */
6714 /* ??? While we do have the CFG intact, there are two problems:
6715 (1) The prologue can contain loops (typically probing the stack),
6716 which means that the end of the prologue isn't in the first bb.
6717 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
6718 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6720 if (NOTE_P (insn))
6722 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
6723 note = insn;
6725 else if (contains (insn, prologue_insn_hash))
6727 last = insn;
6728 if (--len == 0)
6729 break;
6733 if (last)
6735 if (note == NULL)
6737 /* Scan forward looking for the PROLOGUE_END note. It should
6738 be right at the beginning of the block, possibly with other
6739 insn notes that got moved there. */
6740 for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
6742 if (NOTE_P (note)
6743 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
6744 break;
6748 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
6749 if (LABEL_P (last))
6750 last = NEXT_INSN (last);
6751 reorder_insns (note, note, last);
6755 if (epilogue_insn_hash != NULL)
6757 edge_iterator ei;
6758 edge e;
6760 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
6762 rtx insn, first = NULL, note = NULL;
6763 basic_block bb = e->src;
6765 /* Scan from the beginning until we reach the first epilogue insn. */
6766 FOR_BB_INSNS (bb, insn)
6768 if (NOTE_P (insn))
6770 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
6772 note = insn;
6773 if (first != NULL)
6774 break;
6777 else if (first == NULL && contains (insn, epilogue_insn_hash))
6779 first = insn;
6780 if (note != NULL)
6781 break;
6785 if (note)
6787 /* If the function has a single basic block, and no real
6788 epilogue insns (e.g. sibcall with no cleanup), the
6789 epilogue note can get scheduled before the prologue
6790 note. If we have frame related prologue insns, having
6791 them scanned during the epilogue will result in a crash.
6792 In this case re-order the epilogue note to just before
6793 the last insn in the block. */
6794 if (first == NULL)
6795 first = BB_END (bb);
6797 if (PREV_INSN (first) != note)
6798 reorder_insns (note, note, PREV_INSN (first));
6802 #endif /* HAVE_prologue or HAVE_epilogue */
6805 /* Returns the name of function declared by FNDECL. */
6806 const char *
6807 fndecl_name (tree fndecl)
6809 if (fndecl == NULL)
6810 return "(nofn)";
6811 return lang_hooks.decl_printable_name (fndecl, 2);
6814 /* Returns the name of function FN. */
6815 const char *
6816 function_name (struct function *fn)
6818 tree fndecl = (fn == NULL) ? NULL : fn->decl;
6819 return fndecl_name (fndecl);
6822 /* Returns the name of the current function. */
6823 const char *
6824 current_function_name (void)
6826 return function_name (cfun);
6830 static unsigned int
6831 rest_of_handle_check_leaf_regs (void)
6833 #ifdef LEAF_REGISTERS
6834 crtl->uses_only_leaf_regs
6835 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6836 #endif
6837 return 0;
6840 /* Insert a TYPE into the used types hash table of CFUN. */
6842 static void
6843 used_types_insert_helper (tree type, struct function *func)
6845 if (type != NULL && func != NULL)
6847 void **slot;
6849 if (func->used_types_hash == NULL)
6850 func->used_types_hash = htab_create_ggc (37, htab_hash_pointer,
6851 htab_eq_pointer, NULL);
6852 slot = htab_find_slot (func->used_types_hash, type, INSERT);
6853 if (*slot == NULL)
6854 *slot = type;
6858 /* Given a type, insert it into the used hash table in cfun. */
6859 void
6860 used_types_insert (tree t)
6862 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6863 if (TYPE_NAME (t))
6864 break;
6865 else
6866 t = TREE_TYPE (t);
6867 if (TREE_CODE (t) == ERROR_MARK)
6868 return;
6869 if (TYPE_NAME (t) == NULL_TREE
6870 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6871 t = TYPE_MAIN_VARIANT (t);
6872 if (debug_info_level > DINFO_LEVEL_NONE)
6874 if (cfun)
6875 used_types_insert_helper (t, cfun);
6876 else
6878 /* So this might be a type referenced by a global variable.
6879 Record that type so that we can later decide to emit its
6880 debug information. */
6881 vec_safe_push (types_used_by_cur_var_decl, t);
6886 /* Helper to Hash a struct types_used_by_vars_entry. */
6888 static hashval_t
6889 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6891 gcc_assert (entry && entry->var_decl && entry->type);
6893 return iterative_hash_object (entry->type,
6894 iterative_hash_object (entry->var_decl, 0));
6897 /* Hash function of the types_used_by_vars_entry hash table. */
6899 hashval_t
6900 types_used_by_vars_do_hash (const void *x)
6902 const struct types_used_by_vars_entry *entry =
6903 (const struct types_used_by_vars_entry *) x;
6905 return hash_types_used_by_vars_entry (entry);
6908 /*Equality function of the types_used_by_vars_entry hash table. */
6911 types_used_by_vars_eq (const void *x1, const void *x2)
6913 const struct types_used_by_vars_entry *e1 =
6914 (const struct types_used_by_vars_entry *) x1;
6915 const struct types_used_by_vars_entry *e2 =
6916 (const struct types_used_by_vars_entry *)x2;
6918 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6921 /* Inserts an entry into the types_used_by_vars_hash hash table. */
6923 void
6924 types_used_by_var_decl_insert (tree type, tree var_decl)
6926 if (type != NULL && var_decl != NULL)
6928 void **slot;
6929 struct types_used_by_vars_entry e;
6930 e.var_decl = var_decl;
6931 e.type = type;
6932 if (types_used_by_vars_hash == NULL)
6933 types_used_by_vars_hash =
6934 htab_create_ggc (37, types_used_by_vars_do_hash,
6935 types_used_by_vars_eq, NULL);
6936 slot = htab_find_slot_with_hash (types_used_by_vars_hash, &e,
6937 hash_types_used_by_vars_entry (&e), INSERT);
6938 if (*slot == NULL)
6940 struct types_used_by_vars_entry *entry;
6941 entry = ggc_alloc_types_used_by_vars_entry ();
6942 entry->type = type;
6943 entry->var_decl = var_decl;
6944 *slot = entry;
6949 namespace {
6951 const pass_data pass_data_leaf_regs =
6953 RTL_PASS, /* type */
6954 "*leaf_regs", /* name */
6955 OPTGROUP_NONE, /* optinfo_flags */
6956 false, /* has_gate */
6957 true, /* has_execute */
6958 TV_NONE, /* tv_id */
6959 0, /* properties_required */
6960 0, /* properties_provided */
6961 0, /* properties_destroyed */
6962 0, /* todo_flags_start */
6963 0, /* todo_flags_finish */
6966 class pass_leaf_regs : public rtl_opt_pass
6968 public:
6969 pass_leaf_regs (gcc::context *ctxt)
6970 : rtl_opt_pass (pass_data_leaf_regs, ctxt)
6973 /* opt_pass methods: */
6974 unsigned int execute () { return rest_of_handle_check_leaf_regs (); }
6976 }; // class pass_leaf_regs
6978 } // anon namespace
6980 rtl_opt_pass *
6981 make_pass_leaf_regs (gcc::context *ctxt)
6983 return new pass_leaf_regs (ctxt);
6986 static unsigned int
6987 rest_of_handle_thread_prologue_and_epilogue (void)
6989 if (optimize)
6990 cleanup_cfg (CLEANUP_EXPENSIVE);
6992 /* On some machines, the prologue and epilogue code, or parts thereof,
6993 can be represented as RTL. Doing so lets us schedule insns between
6994 it and the rest of the code and also allows delayed branch
6995 scheduling to operate in the epilogue. */
6996 thread_prologue_and_epilogue_insns ();
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 (bb)
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"