* doc/extend.texi (Size of an asm): Really move node to its position.
[official-gcc.git] / gcc / function.c
blobec2ea26628b8d2fce34456a626c66f29bd64809b
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file handles the generation of rtl code from tree structure
21 at the level of the function as a whole.
22 It creates the rtl expressions for parameters and auto variables
23 and has full responsibility for allocating stack slots.
25 `expand_function_start' is called at the beginning of a function,
26 before the function body is parsed, and `expand_function_end' is
27 called after parsing the body.
29 Call `assign_stack_local' to allocate a stack slot for a local variable.
30 This is usually done during the RTL generation for the function body,
31 but it can also be done in the reload pass when a pseudo-register does
32 not get a hard register. */
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "tm.h"
38 #include "rtl-error.h"
39 #include "tree.h"
40 #include "stor-layout.h"
41 #include "varasm.h"
42 #include "stringpool.h"
43 #include "flags.h"
44 #include "except.h"
45 #include "function.h"
46 #include "expr.h"
47 #include "optabs.h"
48 #include "libfuncs.h"
49 #include "regs.h"
50 #include "hard-reg-set.h"
51 #include "insn-config.h"
52 #include "recog.h"
53 #include "output.h"
54 #include "hashtab.h"
55 #include "tm_p.h"
56 #include "langhooks.h"
57 #include "target.h"
58 #include "common/common-target.h"
59 #include "gimple-expr.h"
60 #include "gimplify.h"
61 #include "tree-pass.h"
62 #include "predict.h"
63 #include "df.h"
64 #include "params.h"
65 #include "bb-reorder.h"
66 #include "shrink-wrap.h"
68 /* So we can assign to cfun in this file. */
69 #undef cfun
71 #ifndef STACK_ALIGNMENT_NEEDED
72 #define STACK_ALIGNMENT_NEEDED 1
73 #endif
75 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
77 /* Round a value to the lowest integer less than it that is a multiple of
78 the required alignment. Avoid using division in case the value is
79 negative. Assume the alignment is a power of two. */
80 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
82 /* Similar, but round to the next highest integer that meets the
83 alignment. */
84 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
86 /* Nonzero once virtual register instantiation has been done.
87 assign_stack_local uses frame_pointer_rtx when this is nonzero.
88 calls.c:emit_library_call_value_1 uses it to set up
89 post-instantiation libcalls. */
90 int virtuals_instantiated;
92 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
93 static GTY(()) int funcdef_no;
95 /* These variables hold pointers to functions to create and destroy
96 target specific, per-function data structures. */
97 struct machine_function * (*init_machine_status) (void);
99 /* The currently compiled function. */
100 struct function *cfun = 0;
102 /* These hashes record the prologue and epilogue insns. */
103 static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
104 htab_t prologue_insn_hash;
105 static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
106 htab_t epilogue_insn_hash;
109 htab_t types_used_by_vars_hash = NULL;
110 vec<tree, va_gc> *types_used_by_cur_var_decl;
112 /* Forward declarations. */
114 static struct temp_slot *find_temp_slot_from_address (rtx);
115 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
116 static void pad_below (struct args_size *, enum machine_mode, tree);
117 static void reorder_blocks_1 (rtx, tree, vec<tree> *);
118 static int all_blocks (tree, tree *);
119 static tree *get_block_vector (tree, int *);
120 extern tree debug_find_var_in_block_tree (tree, tree);
121 /* We always define `record_insns' even if it's not used so that we
122 can always export `prologue_epilogue_contains'. */
123 static void record_insns (rtx, rtx, htab_t *) ATTRIBUTE_UNUSED;
124 static bool contains (const_rtx, htab_t);
125 static void prepare_function_start (void);
126 static void do_clobber_return_reg (rtx, void *);
127 static void do_use_return_reg (rtx, void *);
129 /* Stack of nested functions. */
130 /* Keep track of the cfun stack. */
132 typedef struct function *function_p;
134 static vec<function_p> function_context_stack;
136 /* Save the current context for compilation of a nested function.
137 This is called from language-specific code. */
139 void
140 push_function_context (void)
142 if (cfun == 0)
143 allocate_struct_function (NULL, false);
145 function_context_stack.safe_push (cfun);
146 set_cfun (NULL);
149 /* Restore the last saved context, at the end of a nested function.
150 This function is called from language-specific code. */
152 void
153 pop_function_context (void)
155 struct function *p = function_context_stack.pop ();
156 set_cfun (p);
157 current_function_decl = p->decl;
159 /* Reset variables that have known state during rtx generation. */
160 virtuals_instantiated = 0;
161 generating_concat_p = 1;
164 /* Clear out all parts of the state in F that can safely be discarded
165 after the function has been parsed, but not compiled, to let
166 garbage collection reclaim the memory. */
168 void
169 free_after_parsing (struct function *f)
171 f->language = 0;
174 /* Clear out all parts of the state in F that can safely be discarded
175 after the function has been compiled, to let garbage collection
176 reclaim the memory. */
178 void
179 free_after_compilation (struct function *f)
181 prologue_insn_hash = NULL;
182 epilogue_insn_hash = NULL;
184 free (crtl->emit.regno_pointer_align);
186 memset (crtl, 0, sizeof (struct rtl_data));
187 f->eh = NULL;
188 f->machine = NULL;
189 f->cfg = NULL;
191 regno_reg_rtx = NULL;
194 /* Return size needed for stack frame based on slots so far allocated.
195 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
196 the caller may have to do that. */
198 HOST_WIDE_INT
199 get_frame_size (void)
201 if (FRAME_GROWS_DOWNWARD)
202 return -frame_offset;
203 else
204 return frame_offset;
207 /* Issue an error message and return TRUE if frame OFFSET overflows in
208 the signed target pointer arithmetics for function FUNC. Otherwise
209 return FALSE. */
211 bool
212 frame_offset_overflow (HOST_WIDE_INT offset, tree func)
214 unsigned HOST_WIDE_INT size = FRAME_GROWS_DOWNWARD ? -offset : offset;
216 if (size > ((unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (Pmode) - 1))
217 /* Leave room for the fixed part of the frame. */
218 - 64 * UNITS_PER_WORD)
220 error_at (DECL_SOURCE_LOCATION (func),
221 "total size of local objects too large");
222 return TRUE;
225 return FALSE;
228 /* Return stack slot alignment in bits for TYPE and MODE. */
230 static unsigned int
231 get_stack_local_alignment (tree type, enum machine_mode mode)
233 unsigned int alignment;
235 if (mode == BLKmode)
236 alignment = BIGGEST_ALIGNMENT;
237 else
238 alignment = GET_MODE_ALIGNMENT (mode);
240 /* Allow the frond-end to (possibly) increase the alignment of this
241 stack slot. */
242 if (! type)
243 type = lang_hooks.types.type_for_mode (mode, 0);
245 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
248 /* Determine whether it is possible to fit a stack slot of size SIZE and
249 alignment ALIGNMENT into an area in the stack frame that starts at
250 frame offset START and has a length of LENGTH. If so, store the frame
251 offset to be used for the stack slot in *POFFSET and return true;
252 return false otherwise. This function will extend the frame size when
253 given a start/length pair that lies at the end of the frame. */
255 static bool
256 try_fit_stack_local (HOST_WIDE_INT start, HOST_WIDE_INT length,
257 HOST_WIDE_INT size, unsigned int alignment,
258 HOST_WIDE_INT *poffset)
260 HOST_WIDE_INT this_frame_offset;
261 int frame_off, frame_alignment, frame_phase;
263 /* Calculate how many bytes the start of local variables is off from
264 stack alignment. */
265 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
266 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
267 frame_phase = frame_off ? frame_alignment - frame_off : 0;
269 /* Round the frame offset to the specified alignment. */
271 /* We must be careful here, since FRAME_OFFSET might be negative and
272 division with a negative dividend isn't as well defined as we might
273 like. So we instead assume that ALIGNMENT is a power of two and
274 use logical operations which are unambiguous. */
275 if (FRAME_GROWS_DOWNWARD)
276 this_frame_offset
277 = (FLOOR_ROUND (start + length - size - frame_phase,
278 (unsigned HOST_WIDE_INT) alignment)
279 + frame_phase);
280 else
281 this_frame_offset
282 = (CEIL_ROUND (start - frame_phase,
283 (unsigned HOST_WIDE_INT) alignment)
284 + frame_phase);
286 /* See if it fits. If this space is at the edge of the frame,
287 consider extending the frame to make it fit. Our caller relies on
288 this when allocating a new slot. */
289 if (frame_offset == start && this_frame_offset < frame_offset)
290 frame_offset = this_frame_offset;
291 else if (this_frame_offset < start)
292 return false;
293 else if (start + length == frame_offset
294 && this_frame_offset + size > start + length)
295 frame_offset = this_frame_offset + size;
296 else if (this_frame_offset + size > start + length)
297 return false;
299 *poffset = this_frame_offset;
300 return true;
303 /* Create a new frame_space structure describing free space in the stack
304 frame beginning at START and ending at END, and chain it into the
305 function's frame_space_list. */
307 static void
308 add_frame_space (HOST_WIDE_INT start, HOST_WIDE_INT end)
310 struct frame_space *space = ggc_alloc<frame_space> ();
311 space->next = crtl->frame_space_list;
312 crtl->frame_space_list = space;
313 space->start = start;
314 space->length = end - start;
317 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
318 with machine mode MODE.
320 ALIGN controls the amount of alignment for the address of the slot:
321 0 means according to MODE,
322 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
323 -2 means use BITS_PER_UNIT,
324 positive specifies alignment boundary in bits.
326 KIND has ASLK_REDUCE_ALIGN bit set if it is OK to reduce
327 alignment and ASLK_RECORD_PAD bit set if we should remember
328 extra space we allocated for alignment purposes. When we are
329 called from assign_stack_temp_for_type, it is not set so we don't
330 track the same stack slot in two independent lists.
332 We do not round to stack_boundary here. */
335 assign_stack_local_1 (enum machine_mode mode, HOST_WIDE_INT size,
336 int align, int kind)
338 rtx x, addr;
339 int bigend_correction = 0;
340 HOST_WIDE_INT slot_offset = 0, old_frame_offset;
341 unsigned int alignment, alignment_in_bits;
343 if (align == 0)
345 alignment = get_stack_local_alignment (NULL, mode);
346 alignment /= BITS_PER_UNIT;
348 else if (align == -1)
350 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
351 size = CEIL_ROUND (size, alignment);
353 else if (align == -2)
354 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
355 else
356 alignment = align / BITS_PER_UNIT;
358 alignment_in_bits = alignment * BITS_PER_UNIT;
360 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
361 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
363 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
364 alignment = alignment_in_bits / BITS_PER_UNIT;
367 if (SUPPORTS_STACK_ALIGNMENT)
369 if (crtl->stack_alignment_estimated < alignment_in_bits)
371 if (!crtl->stack_realign_processed)
372 crtl->stack_alignment_estimated = alignment_in_bits;
373 else
375 /* If stack is realigned and stack alignment value
376 hasn't been finalized, it is OK not to increase
377 stack_alignment_estimated. The bigger alignment
378 requirement is recorded in stack_alignment_needed
379 below. */
380 gcc_assert (!crtl->stack_realign_finalized);
381 if (!crtl->stack_realign_needed)
383 /* It is OK to reduce the alignment as long as the
384 requested size is 0 or the estimated stack
385 alignment >= mode alignment. */
386 gcc_assert ((kind & ASLK_REDUCE_ALIGN)
387 || size == 0
388 || (crtl->stack_alignment_estimated
389 >= GET_MODE_ALIGNMENT (mode)));
390 alignment_in_bits = crtl->stack_alignment_estimated;
391 alignment = alignment_in_bits / BITS_PER_UNIT;
397 if (crtl->stack_alignment_needed < alignment_in_bits)
398 crtl->stack_alignment_needed = alignment_in_bits;
399 if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
400 crtl->max_used_stack_slot_alignment = alignment_in_bits;
402 if (mode != BLKmode || size != 0)
404 if (kind & ASLK_RECORD_PAD)
406 struct frame_space **psp;
408 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
410 struct frame_space *space = *psp;
411 if (!try_fit_stack_local (space->start, space->length, size,
412 alignment, &slot_offset))
413 continue;
414 *psp = space->next;
415 if (slot_offset > space->start)
416 add_frame_space (space->start, slot_offset);
417 if (slot_offset + size < space->start + space->length)
418 add_frame_space (slot_offset + size,
419 space->start + space->length);
420 goto found_space;
424 else if (!STACK_ALIGNMENT_NEEDED)
426 slot_offset = frame_offset;
427 goto found_space;
430 old_frame_offset = frame_offset;
432 if (FRAME_GROWS_DOWNWARD)
434 frame_offset -= size;
435 try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
437 if (kind & ASLK_RECORD_PAD)
439 if (slot_offset > frame_offset)
440 add_frame_space (frame_offset, slot_offset);
441 if (slot_offset + size < old_frame_offset)
442 add_frame_space (slot_offset + size, old_frame_offset);
445 else
447 frame_offset += size;
448 try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
450 if (kind & ASLK_RECORD_PAD)
452 if (slot_offset > old_frame_offset)
453 add_frame_space (old_frame_offset, slot_offset);
454 if (slot_offset + size < frame_offset)
455 add_frame_space (slot_offset + size, frame_offset);
459 found_space:
460 /* On a big-endian machine, if we are allocating more space than we will use,
461 use the least significant bytes of those that are allocated. */
462 if (BYTES_BIG_ENDIAN && mode != BLKmode && GET_MODE_SIZE (mode) < size)
463 bigend_correction = size - GET_MODE_SIZE (mode);
465 /* If we have already instantiated virtual registers, return the actual
466 address relative to the frame pointer. */
467 if (virtuals_instantiated)
468 addr = plus_constant (Pmode, frame_pointer_rtx,
469 trunc_int_for_mode
470 (slot_offset + bigend_correction
471 + STARTING_FRAME_OFFSET, Pmode));
472 else
473 addr = plus_constant (Pmode, virtual_stack_vars_rtx,
474 trunc_int_for_mode
475 (slot_offset + bigend_correction,
476 Pmode));
478 x = gen_rtx_MEM (mode, addr);
479 set_mem_align (x, alignment_in_bits);
480 MEM_NOTRAP_P (x) = 1;
482 stack_slot_list
483 = gen_rtx_EXPR_LIST (VOIDmode, x, stack_slot_list);
485 if (frame_offset_overflow (frame_offset, current_function_decl))
486 frame_offset = 0;
488 return x;
491 /* Wrap up assign_stack_local_1 with last parameter as false. */
494 assign_stack_local (enum machine_mode mode, HOST_WIDE_INT size, int align)
496 return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
499 /* In order to evaluate some expressions, such as function calls returning
500 structures in memory, we need to temporarily allocate stack locations.
501 We record each allocated temporary in the following structure.
503 Associated with each temporary slot is a nesting level. When we pop up
504 one level, all temporaries associated with the previous level are freed.
505 Normally, all temporaries are freed after the execution of the statement
506 in which they were created. However, if we are inside a ({...}) grouping,
507 the result may be in a temporary and hence must be preserved. If the
508 result could be in a temporary, we preserve it if we can determine which
509 one it is in. If we cannot determine which temporary may contain the
510 result, all temporaries are preserved. A temporary is preserved by
511 pretending it was allocated at the previous nesting level. */
513 struct GTY(()) temp_slot {
514 /* Points to next temporary slot. */
515 struct temp_slot *next;
516 /* Points to previous temporary slot. */
517 struct temp_slot *prev;
518 /* The rtx to used to reference the slot. */
519 rtx slot;
520 /* The size, in units, of the slot. */
521 HOST_WIDE_INT size;
522 /* The type of the object in the slot, or zero if it doesn't correspond
523 to a type. We use this to determine whether a slot can be reused.
524 It can be reused if objects of the type of the new slot will always
525 conflict with objects of the type of the old slot. */
526 tree type;
527 /* The alignment (in bits) of the slot. */
528 unsigned int align;
529 /* Nonzero if this temporary is currently in use. */
530 char in_use;
531 /* Nesting level at which this slot is being used. */
532 int level;
533 /* The offset of the slot from the frame_pointer, including extra space
534 for alignment. This info is for combine_temp_slots. */
535 HOST_WIDE_INT base_offset;
536 /* The size of the slot, including extra space for alignment. This
537 info is for combine_temp_slots. */
538 HOST_WIDE_INT full_size;
541 /* A table of addresses that represent a stack slot. The table is a mapping
542 from address RTXen to a temp slot. */
543 static GTY((param_is(struct temp_slot_address_entry))) htab_t temp_slot_address_table;
544 static size_t n_temp_slots_in_use;
546 /* Entry for the above hash table. */
547 struct GTY(()) temp_slot_address_entry {
548 hashval_t hash;
549 rtx address;
550 struct temp_slot *temp_slot;
553 /* Removes temporary slot TEMP from LIST. */
555 static void
556 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
558 if (temp->next)
559 temp->next->prev = temp->prev;
560 if (temp->prev)
561 temp->prev->next = temp->next;
562 else
563 *list = temp->next;
565 temp->prev = temp->next = NULL;
568 /* Inserts temporary slot TEMP to LIST. */
570 static void
571 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
573 temp->next = *list;
574 if (*list)
575 (*list)->prev = temp;
576 temp->prev = NULL;
577 *list = temp;
580 /* Returns the list of used temp slots at LEVEL. */
582 static struct temp_slot **
583 temp_slots_at_level (int level)
585 if (level >= (int) vec_safe_length (used_temp_slots))
586 vec_safe_grow_cleared (used_temp_slots, level + 1);
588 return &(*used_temp_slots)[level];
591 /* Returns the maximal temporary slot level. */
593 static int
594 max_slot_level (void)
596 if (!used_temp_slots)
597 return -1;
599 return used_temp_slots->length () - 1;
602 /* Moves temporary slot TEMP to LEVEL. */
604 static void
605 move_slot_to_level (struct temp_slot *temp, int level)
607 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
608 insert_slot_to_list (temp, temp_slots_at_level (level));
609 temp->level = level;
612 /* Make temporary slot TEMP available. */
614 static void
615 make_slot_available (struct temp_slot *temp)
617 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
618 insert_slot_to_list (temp, &avail_temp_slots);
619 temp->in_use = 0;
620 temp->level = -1;
621 n_temp_slots_in_use--;
624 /* Compute the hash value for an address -> temp slot mapping.
625 The value is cached on the mapping entry. */
626 static hashval_t
627 temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
629 int do_not_record = 0;
630 return hash_rtx (t->address, GET_MODE (t->address),
631 &do_not_record, NULL, false);
634 /* Return the hash value for an address -> temp slot mapping. */
635 static hashval_t
636 temp_slot_address_hash (const void *p)
638 const struct temp_slot_address_entry *t;
639 t = (const struct temp_slot_address_entry *) p;
640 return t->hash;
643 /* Compare two address -> temp slot mapping entries. */
644 static int
645 temp_slot_address_eq (const void *p1, const void *p2)
647 const struct temp_slot_address_entry *t1, *t2;
648 t1 = (const struct temp_slot_address_entry *) p1;
649 t2 = (const struct temp_slot_address_entry *) p2;
650 return exp_equiv_p (t1->address, t2->address, 0, true);
653 /* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
654 static void
655 insert_temp_slot_address (rtx address, struct temp_slot *temp_slot)
657 void **slot;
658 struct temp_slot_address_entry *t = ggc_alloc<temp_slot_address_entry> ();
659 t->address = address;
660 t->temp_slot = temp_slot;
661 t->hash = temp_slot_address_compute_hash (t);
662 slot = htab_find_slot_with_hash (temp_slot_address_table, t, t->hash, INSERT);
663 *slot = t;
666 /* Remove an address -> temp slot mapping entry if the temp slot is
667 not in use anymore. Callback for remove_unused_temp_slot_addresses. */
668 static int
669 remove_unused_temp_slot_addresses_1 (void **slot, void *data ATTRIBUTE_UNUSED)
671 const struct temp_slot_address_entry *t;
672 t = (const struct temp_slot_address_entry *) *slot;
673 if (! t->temp_slot->in_use)
674 htab_clear_slot (temp_slot_address_table, slot);
675 return 1;
678 /* Remove all mappings of addresses to unused temp slots. */
679 static void
680 remove_unused_temp_slot_addresses (void)
682 /* Use quicker clearing if there aren't any active temp slots. */
683 if (n_temp_slots_in_use)
684 htab_traverse (temp_slot_address_table,
685 remove_unused_temp_slot_addresses_1,
686 NULL);
687 else
688 htab_empty (temp_slot_address_table);
691 /* Find the temp slot corresponding to the object at address X. */
693 static struct temp_slot *
694 find_temp_slot_from_address (rtx x)
696 struct temp_slot *p;
697 struct temp_slot_address_entry tmp, *t;
699 /* First try the easy way:
700 See if X exists in the address -> temp slot mapping. */
701 tmp.address = x;
702 tmp.temp_slot = NULL;
703 tmp.hash = temp_slot_address_compute_hash (&tmp);
704 t = (struct temp_slot_address_entry *)
705 htab_find_with_hash (temp_slot_address_table, &tmp, tmp.hash);
706 if (t)
707 return t->temp_slot;
709 /* If we have a sum involving a register, see if it points to a temp
710 slot. */
711 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
712 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
713 return p;
714 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
715 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
716 return p;
718 /* Last resort: Address is a virtual stack var address. */
719 if (GET_CODE (x) == PLUS
720 && XEXP (x, 0) == virtual_stack_vars_rtx
721 && CONST_INT_P (XEXP (x, 1)))
723 int i;
724 for (i = max_slot_level (); i >= 0; i--)
725 for (p = *temp_slots_at_level (i); p; p = p->next)
727 if (INTVAL (XEXP (x, 1)) >= p->base_offset
728 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size)
729 return p;
733 return NULL;
736 /* Allocate a temporary stack slot and record it for possible later
737 reuse.
739 MODE is the machine mode to be given to the returned rtx.
741 SIZE is the size in units of the space required. We do no rounding here
742 since assign_stack_local will do any required rounding.
744 TYPE is the type that will be used for the stack slot. */
747 assign_stack_temp_for_type (enum machine_mode mode, HOST_WIDE_INT size,
748 tree type)
750 unsigned int align;
751 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
752 rtx slot;
754 /* If SIZE is -1 it means that somebody tried to allocate a temporary
755 of a variable size. */
756 gcc_assert (size != -1);
758 align = get_stack_local_alignment (type, mode);
760 /* Try to find an available, already-allocated temporary of the proper
761 mode which meets the size and alignment requirements. Choose the
762 smallest one with the closest alignment.
764 If assign_stack_temp is called outside of the tree->rtl expansion,
765 we cannot reuse the stack slots (that may still refer to
766 VIRTUAL_STACK_VARS_REGNUM). */
767 if (!virtuals_instantiated)
769 for (p = avail_temp_slots; p; p = p->next)
771 if (p->align >= align && p->size >= size
772 && GET_MODE (p->slot) == mode
773 && objects_must_conflict_p (p->type, type)
774 && (best_p == 0 || best_p->size > p->size
775 || (best_p->size == p->size && best_p->align > p->align)))
777 if (p->align == align && p->size == size)
779 selected = p;
780 cut_slot_from_list (selected, &avail_temp_slots);
781 best_p = 0;
782 break;
784 best_p = p;
789 /* Make our best, if any, the one to use. */
790 if (best_p)
792 selected = best_p;
793 cut_slot_from_list (selected, &avail_temp_slots);
795 /* If there are enough aligned bytes left over, make them into a new
796 temp_slot so that the extra bytes don't get wasted. Do this only
797 for BLKmode slots, so that we can be sure of the alignment. */
798 if (GET_MODE (best_p->slot) == BLKmode)
800 int alignment = best_p->align / BITS_PER_UNIT;
801 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
803 if (best_p->size - rounded_size >= alignment)
805 p = ggc_alloc<temp_slot> ();
806 p->in_use = 0;
807 p->size = best_p->size - rounded_size;
808 p->base_offset = best_p->base_offset + rounded_size;
809 p->full_size = best_p->full_size - rounded_size;
810 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
811 p->align = best_p->align;
812 p->type = best_p->type;
813 insert_slot_to_list (p, &avail_temp_slots);
815 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
816 stack_slot_list);
818 best_p->size = rounded_size;
819 best_p->full_size = rounded_size;
824 /* If we still didn't find one, make a new temporary. */
825 if (selected == 0)
827 HOST_WIDE_INT frame_offset_old = frame_offset;
829 p = ggc_alloc<temp_slot> ();
831 /* We are passing an explicit alignment request to assign_stack_local.
832 One side effect of that is assign_stack_local will not round SIZE
833 to ensure the frame offset remains suitably aligned.
835 So for requests which depended on the rounding of SIZE, we go ahead
836 and round it now. We also make sure ALIGNMENT is at least
837 BIGGEST_ALIGNMENT. */
838 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
839 p->slot = assign_stack_local_1 (mode,
840 (mode == BLKmode
841 ? CEIL_ROUND (size,
842 (int) align
843 / BITS_PER_UNIT)
844 : size),
845 align, 0);
847 p->align = align;
849 /* The following slot size computation is necessary because we don't
850 know the actual size of the temporary slot until assign_stack_local
851 has performed all the frame alignment and size rounding for the
852 requested temporary. Note that extra space added for alignment
853 can be either above or below this stack slot depending on which
854 way the frame grows. We include the extra space if and only if it
855 is above this slot. */
856 if (FRAME_GROWS_DOWNWARD)
857 p->size = frame_offset_old - frame_offset;
858 else
859 p->size = size;
861 /* Now define the fields used by combine_temp_slots. */
862 if (FRAME_GROWS_DOWNWARD)
864 p->base_offset = frame_offset;
865 p->full_size = frame_offset_old - frame_offset;
867 else
869 p->base_offset = frame_offset_old;
870 p->full_size = frame_offset - frame_offset_old;
873 selected = p;
876 p = selected;
877 p->in_use = 1;
878 p->type = type;
879 p->level = temp_slot_level;
880 n_temp_slots_in_use++;
882 pp = temp_slots_at_level (p->level);
883 insert_slot_to_list (p, pp);
884 insert_temp_slot_address (XEXP (p->slot, 0), p);
886 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
887 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
888 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
890 /* If we know the alias set for the memory that will be used, use
891 it. If there's no TYPE, then we don't know anything about the
892 alias set for the memory. */
893 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
894 set_mem_align (slot, align);
896 /* If a type is specified, set the relevant flags. */
897 if (type != 0)
898 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
899 MEM_NOTRAP_P (slot) = 1;
901 return slot;
904 /* Allocate a temporary stack slot and record it for possible later
905 reuse. First two arguments are same as in preceding function. */
908 assign_stack_temp (enum machine_mode mode, HOST_WIDE_INT size)
910 return assign_stack_temp_for_type (mode, size, NULL_TREE);
913 /* Assign a temporary.
914 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
915 and so that should be used in error messages. In either case, we
916 allocate of the given type.
917 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
918 it is 0 if a register is OK.
919 DONT_PROMOTE is 1 if we should not promote values in register
920 to wider modes. */
923 assign_temp (tree type_or_decl, int memory_required,
924 int dont_promote ATTRIBUTE_UNUSED)
926 tree type, decl;
927 enum machine_mode mode;
928 #ifdef PROMOTE_MODE
929 int unsignedp;
930 #endif
932 if (DECL_P (type_or_decl))
933 decl = type_or_decl, type = TREE_TYPE (decl);
934 else
935 decl = NULL, type = type_or_decl;
937 mode = TYPE_MODE (type);
938 #ifdef PROMOTE_MODE
939 unsignedp = TYPE_UNSIGNED (type);
940 #endif
942 if (mode == BLKmode || memory_required)
944 HOST_WIDE_INT size = int_size_in_bytes (type);
945 rtx tmp;
947 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
948 problems with allocating the stack space. */
949 if (size == 0)
950 size = 1;
952 /* Unfortunately, we don't yet know how to allocate variable-sized
953 temporaries. However, sometimes we can find a fixed upper limit on
954 the size, so try that instead. */
955 else if (size == -1)
956 size = max_int_size_in_bytes (type);
958 /* The size of the temporary may be too large to fit into an integer. */
959 /* ??? Not sure this should happen except for user silliness, so limit
960 this to things that aren't compiler-generated temporaries. The
961 rest of the time we'll die in assign_stack_temp_for_type. */
962 if (decl && size == -1
963 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
965 error ("size of variable %q+D is too large", decl);
966 size = 1;
969 tmp = assign_stack_temp_for_type (mode, size, type);
970 return tmp;
973 #ifdef PROMOTE_MODE
974 if (! dont_promote)
975 mode = promote_mode (type, mode, &unsignedp);
976 #endif
978 return gen_reg_rtx (mode);
981 /* Combine temporary stack slots which are adjacent on the stack.
983 This allows for better use of already allocated stack space. This is only
984 done for BLKmode slots because we can be sure that we won't have alignment
985 problems in this case. */
987 static void
988 combine_temp_slots (void)
990 struct temp_slot *p, *q, *next, *next_q;
991 int num_slots;
993 /* We can't combine slots, because the information about which slot
994 is in which alias set will be lost. */
995 if (flag_strict_aliasing)
996 return;
998 /* If there are a lot of temp slots, don't do anything unless
999 high levels of optimization. */
1000 if (! flag_expensive_optimizations)
1001 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
1002 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1003 return;
1005 for (p = avail_temp_slots; p; p = next)
1007 int delete_p = 0;
1009 next = p->next;
1011 if (GET_MODE (p->slot) != BLKmode)
1012 continue;
1014 for (q = p->next; q; q = next_q)
1016 int delete_q = 0;
1018 next_q = q->next;
1020 if (GET_MODE (q->slot) != BLKmode)
1021 continue;
1023 if (p->base_offset + p->full_size == q->base_offset)
1025 /* Q comes after P; combine Q into P. */
1026 p->size += q->size;
1027 p->full_size += q->full_size;
1028 delete_q = 1;
1030 else if (q->base_offset + q->full_size == p->base_offset)
1032 /* P comes after Q; combine P into Q. */
1033 q->size += p->size;
1034 q->full_size += p->full_size;
1035 delete_p = 1;
1036 break;
1038 if (delete_q)
1039 cut_slot_from_list (q, &avail_temp_slots);
1042 /* Either delete P or advance past it. */
1043 if (delete_p)
1044 cut_slot_from_list (p, &avail_temp_slots);
1048 /* Indicate that NEW_RTX is an alternate way of referring to the temp
1049 slot that previously was known by OLD_RTX. */
1051 void
1052 update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1054 struct temp_slot *p;
1056 if (rtx_equal_p (old_rtx, new_rtx))
1057 return;
1059 p = find_temp_slot_from_address (old_rtx);
1061 /* If we didn't find one, see if both OLD_RTX is a PLUS. If so, and
1062 NEW_RTX is a register, see if one operand of the PLUS is a
1063 temporary location. If so, NEW_RTX points into it. Otherwise,
1064 if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1065 in common between them. If so, try a recursive call on those
1066 values. */
1067 if (p == 0)
1069 if (GET_CODE (old_rtx) != PLUS)
1070 return;
1072 if (REG_P (new_rtx))
1074 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1075 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1076 return;
1078 else if (GET_CODE (new_rtx) != PLUS)
1079 return;
1081 if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1082 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1083 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1084 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1085 else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1086 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1087 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1088 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
1090 return;
1093 /* Otherwise add an alias for the temp's address. */
1094 insert_temp_slot_address (new_rtx, p);
1097 /* If X could be a reference to a temporary slot, mark that slot as
1098 belonging to the to one level higher than the current level. If X
1099 matched one of our slots, just mark that one. Otherwise, we can't
1100 easily predict which it is, so upgrade all of them.
1102 This is called when an ({...}) construct occurs and a statement
1103 returns a value in memory. */
1105 void
1106 preserve_temp_slots (rtx x)
1108 struct temp_slot *p = 0, *next;
1110 if (x == 0)
1111 return;
1113 /* If X is a register that is being used as a pointer, see if we have
1114 a temporary slot we know it points to. */
1115 if (REG_P (x) && REG_POINTER (x))
1116 p = find_temp_slot_from_address (x);
1118 /* If X is not in memory or is at a constant address, it cannot be in
1119 a temporary slot. */
1120 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1121 return;
1123 /* First see if we can find a match. */
1124 if (p == 0)
1125 p = find_temp_slot_from_address (XEXP (x, 0));
1127 if (p != 0)
1129 if (p->level == temp_slot_level)
1130 move_slot_to_level (p, temp_slot_level - 1);
1131 return;
1134 /* Otherwise, preserve all non-kept slots at this level. */
1135 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1137 next = p->next;
1138 move_slot_to_level (p, temp_slot_level - 1);
1142 /* Free all temporaries used so far. This is normally called at the
1143 end of generating code for a statement. */
1145 void
1146 free_temp_slots (void)
1148 struct temp_slot *p, *next;
1149 bool some_available = false;
1151 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1153 next = p->next;
1154 make_slot_available (p);
1155 some_available = true;
1158 if (some_available)
1160 remove_unused_temp_slot_addresses ();
1161 combine_temp_slots ();
1165 /* Push deeper into the nesting level for stack temporaries. */
1167 void
1168 push_temp_slots (void)
1170 temp_slot_level++;
1173 /* Pop a temporary nesting level. All slots in use in the current level
1174 are freed. */
1176 void
1177 pop_temp_slots (void)
1179 free_temp_slots ();
1180 temp_slot_level--;
1183 /* Initialize temporary slots. */
1185 void
1186 init_temp_slots (void)
1188 /* We have not allocated any temporaries yet. */
1189 avail_temp_slots = 0;
1190 vec_alloc (used_temp_slots, 0);
1191 temp_slot_level = 0;
1192 n_temp_slots_in_use = 0;
1194 /* Set up the table to map addresses to temp slots. */
1195 if (! temp_slot_address_table)
1196 temp_slot_address_table = htab_create_ggc (32,
1197 temp_slot_address_hash,
1198 temp_slot_address_eq,
1199 NULL);
1200 else
1201 htab_empty (temp_slot_address_table);
1204 /* Functions and data structures to keep track of the values hard regs
1205 had at the start of the function. */
1207 /* Private type used by get_hard_reg_initial_reg, get_hard_reg_initial_val,
1208 and has_hard_reg_initial_val.. */
1209 typedef struct GTY(()) initial_value_pair {
1210 rtx hard_reg;
1211 rtx pseudo;
1212 } initial_value_pair;
1213 /* ??? This could be a VEC but there is currently no way to define an
1214 opaque VEC type. This could be worked around by defining struct
1215 initial_value_pair in function.h. */
1216 typedef struct GTY(()) initial_value_struct {
1217 int num_entries;
1218 int max_entries;
1219 initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
1220 } initial_value_struct;
1222 /* If a pseudo represents an initial hard reg (or expression), return
1223 it, else return NULL_RTX. */
1226 get_hard_reg_initial_reg (rtx reg)
1228 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1229 int i;
1231 if (ivs == 0)
1232 return NULL_RTX;
1234 for (i = 0; i < ivs->num_entries; i++)
1235 if (rtx_equal_p (ivs->entries[i].pseudo, reg))
1236 return ivs->entries[i].hard_reg;
1238 return NULL_RTX;
1241 /* Make sure that there's a pseudo register of mode MODE that stores the
1242 initial value of hard register REGNO. Return an rtx for such a pseudo. */
1245 get_hard_reg_initial_val (enum machine_mode mode, unsigned int regno)
1247 struct initial_value_struct *ivs;
1248 rtx rv;
1250 rv = has_hard_reg_initial_val (mode, regno);
1251 if (rv)
1252 return rv;
1254 ivs = crtl->hard_reg_initial_vals;
1255 if (ivs == 0)
1257 ivs = ggc_alloc<initial_value_struct> ();
1258 ivs->num_entries = 0;
1259 ivs->max_entries = 5;
1260 ivs->entries = ggc_vec_alloc<initial_value_pair> (5);
1261 crtl->hard_reg_initial_vals = ivs;
1264 if (ivs->num_entries >= ivs->max_entries)
1266 ivs->max_entries += 5;
1267 ivs->entries = GGC_RESIZEVEC (initial_value_pair, ivs->entries,
1268 ivs->max_entries);
1271 ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
1272 ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
1274 return ivs->entries[ivs->num_entries++].pseudo;
1277 /* See if get_hard_reg_initial_val has been used to create a pseudo
1278 for the initial value of hard register REGNO in mode MODE. Return
1279 the associated pseudo if so, otherwise return NULL. */
1282 has_hard_reg_initial_val (enum machine_mode mode, unsigned int regno)
1284 struct initial_value_struct *ivs;
1285 int i;
1287 ivs = crtl->hard_reg_initial_vals;
1288 if (ivs != 0)
1289 for (i = 0; i < ivs->num_entries; i++)
1290 if (GET_MODE (ivs->entries[i].hard_reg) == mode
1291 && REGNO (ivs->entries[i].hard_reg) == regno)
1292 return ivs->entries[i].pseudo;
1294 return NULL_RTX;
1297 unsigned int
1298 emit_initial_value_sets (void)
1300 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1301 int i;
1302 rtx seq;
1304 if (ivs == 0)
1305 return 0;
1307 start_sequence ();
1308 for (i = 0; i < ivs->num_entries; i++)
1309 emit_move_insn (ivs->entries[i].pseudo, ivs->entries[i].hard_reg);
1310 seq = get_insns ();
1311 end_sequence ();
1313 emit_insn_at_entry (seq);
1314 return 0;
1317 /* Return the hardreg-pseudoreg initial values pair entry I and
1318 TRUE if I is a valid entry, or FALSE if I is not a valid entry. */
1319 bool
1320 initial_value_entry (int i, rtx *hreg, rtx *preg)
1322 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1323 if (!ivs || i >= ivs->num_entries)
1324 return false;
1326 *hreg = ivs->entries[i].hard_reg;
1327 *preg = ivs->entries[i].pseudo;
1328 return true;
1331 /* These routines are responsible for converting virtual register references
1332 to the actual hard register references once RTL generation is complete.
1334 The following four variables are used for communication between the
1335 routines. They contain the offsets of the virtual registers from their
1336 respective hard registers. */
1338 static int in_arg_offset;
1339 static int var_offset;
1340 static int dynamic_offset;
1341 static int out_arg_offset;
1342 static int cfa_offset;
1344 /* In most machines, the stack pointer register is equivalent to the bottom
1345 of the stack. */
1347 #ifndef STACK_POINTER_OFFSET
1348 #define STACK_POINTER_OFFSET 0
1349 #endif
1351 /* If not defined, pick an appropriate default for the offset of dynamically
1352 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1353 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1355 #ifndef STACK_DYNAMIC_OFFSET
1357 /* The bottom of the stack points to the actual arguments. If
1358 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1359 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1360 stack space for register parameters is not pushed by the caller, but
1361 rather part of the fixed stack areas and hence not included in
1362 `crtl->outgoing_args_size'. Nevertheless, we must allow
1363 for it when allocating stack dynamic objects. */
1365 #if defined(REG_PARM_STACK_SPACE)
1366 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1367 ((ACCUMULATE_OUTGOING_ARGS \
1368 ? (crtl->outgoing_args_size \
1369 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1370 : REG_PARM_STACK_SPACE (FNDECL))) \
1371 : 0) + (STACK_POINTER_OFFSET))
1372 #else
1373 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1374 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0) \
1375 + (STACK_POINTER_OFFSET))
1376 #endif
1377 #endif
1380 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1381 is a virtual register, return the equivalent hard register and set the
1382 offset indirectly through the pointer. Otherwise, return 0. */
1384 static rtx
1385 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
1387 rtx new_rtx;
1388 HOST_WIDE_INT offset;
1390 if (x == virtual_incoming_args_rtx)
1392 if (stack_realign_drap)
1394 /* Replace virtual_incoming_args_rtx with internal arg
1395 pointer if DRAP is used to realign stack. */
1396 new_rtx = crtl->args.internal_arg_pointer;
1397 offset = 0;
1399 else
1400 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1402 else if (x == virtual_stack_vars_rtx)
1403 new_rtx = frame_pointer_rtx, offset = var_offset;
1404 else if (x == virtual_stack_dynamic_rtx)
1405 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1406 else if (x == virtual_outgoing_args_rtx)
1407 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1408 else if (x == virtual_cfa_rtx)
1410 #ifdef FRAME_POINTER_CFA_OFFSET
1411 new_rtx = frame_pointer_rtx;
1412 #else
1413 new_rtx = arg_pointer_rtx;
1414 #endif
1415 offset = cfa_offset;
1417 else if (x == virtual_preferred_stack_boundary_rtx)
1419 new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1420 offset = 0;
1422 else
1423 return NULL_RTX;
1425 *poffset = offset;
1426 return new_rtx;
1429 /* A subroutine of instantiate_virtual_regs, called via for_each_rtx.
1430 Instantiate any virtual registers present inside of *LOC. The expression
1431 is simplified, as much as possible, but is not to be considered "valid"
1432 in any sense implied by the target. If any change is made, set CHANGED
1433 to true. */
1435 static int
1436 instantiate_virtual_regs_in_rtx (rtx *loc, void *data)
1438 HOST_WIDE_INT offset;
1439 bool *changed = (bool *) data;
1440 rtx x, new_rtx;
1442 x = *loc;
1443 if (x == 0)
1444 return 0;
1446 switch (GET_CODE (x))
1448 case REG:
1449 new_rtx = instantiate_new_reg (x, &offset);
1450 if (new_rtx)
1452 *loc = plus_constant (GET_MODE (x), new_rtx, offset);
1453 if (changed)
1454 *changed = true;
1456 return -1;
1458 case PLUS:
1459 new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1460 if (new_rtx)
1462 XEXP (x, 0) = new_rtx;
1463 *loc = plus_constant (GET_MODE (x), x, offset, true);
1464 if (changed)
1465 *changed = true;
1466 return -1;
1469 /* FIXME -- from old code */
1470 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1471 we can commute the PLUS and SUBREG because pointers into the
1472 frame are well-behaved. */
1473 break;
1475 default:
1476 break;
1479 return 0;
1482 /* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1483 matches the predicate for insn CODE operand OPERAND. */
1485 static int
1486 safe_insn_predicate (int code, int operand, rtx x)
1488 return code < 0 || insn_operand_matches ((enum insn_code) code, operand, x);
1491 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1492 registers present inside of insn. The result will be a valid insn. */
1494 static void
1495 instantiate_virtual_regs_in_insn (rtx insn)
1497 HOST_WIDE_INT offset;
1498 int insn_code, i;
1499 bool any_change = false;
1500 rtx set, new_rtx, x, seq;
1502 /* There are some special cases to be handled first. */
1503 set = single_set (insn);
1504 if (set)
1506 /* We're allowed to assign to a virtual register. This is interpreted
1507 to mean that the underlying register gets assigned the inverse
1508 transformation. This is used, for example, in the handling of
1509 non-local gotos. */
1510 new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1511 if (new_rtx)
1513 start_sequence ();
1515 for_each_rtx (&SET_SRC (set), instantiate_virtual_regs_in_rtx, NULL);
1516 x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
1517 gen_int_mode (-offset, GET_MODE (new_rtx)));
1518 x = force_operand (x, new_rtx);
1519 if (x != new_rtx)
1520 emit_move_insn (new_rtx, x);
1522 seq = get_insns ();
1523 end_sequence ();
1525 emit_insn_before (seq, insn);
1526 delete_insn (insn);
1527 return;
1530 /* Handle a straight copy from a virtual register by generating a
1531 new add insn. The difference between this and falling through
1532 to the generic case is avoiding a new pseudo and eliminating a
1533 move insn in the initial rtl stream. */
1534 new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1535 if (new_rtx && offset != 0
1536 && REG_P (SET_DEST (set))
1537 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1539 start_sequence ();
1541 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS, new_rtx,
1542 gen_int_mode (offset,
1543 GET_MODE (SET_DEST (set))),
1544 SET_DEST (set), 1, OPTAB_LIB_WIDEN);
1545 if (x != SET_DEST (set))
1546 emit_move_insn (SET_DEST (set), x);
1548 seq = get_insns ();
1549 end_sequence ();
1551 emit_insn_before (seq, insn);
1552 delete_insn (insn);
1553 return;
1556 extract_insn (insn);
1557 insn_code = INSN_CODE (insn);
1559 /* Handle a plus involving a virtual register by determining if the
1560 operands remain valid if they're modified in place. */
1561 if (GET_CODE (SET_SRC (set)) == PLUS
1562 && recog_data.n_operands >= 3
1563 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1564 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1565 && CONST_INT_P (recog_data.operand[2])
1566 && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
1568 offset += INTVAL (recog_data.operand[2]);
1570 /* If the sum is zero, then replace with a plain move. */
1571 if (offset == 0
1572 && REG_P (SET_DEST (set))
1573 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1575 start_sequence ();
1576 emit_move_insn (SET_DEST (set), new_rtx);
1577 seq = get_insns ();
1578 end_sequence ();
1580 emit_insn_before (seq, insn);
1581 delete_insn (insn);
1582 return;
1585 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1587 /* Using validate_change and apply_change_group here leaves
1588 recog_data in an invalid state. Since we know exactly what
1589 we want to check, do those two by hand. */
1590 if (safe_insn_predicate (insn_code, 1, new_rtx)
1591 && safe_insn_predicate (insn_code, 2, x))
1593 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1594 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1595 any_change = true;
1597 /* Fall through into the regular operand fixup loop in
1598 order to take care of operands other than 1 and 2. */
1602 else
1604 extract_insn (insn);
1605 insn_code = INSN_CODE (insn);
1608 /* In the general case, we expect virtual registers to appear only in
1609 operands, and then only as either bare registers or inside memories. */
1610 for (i = 0; i < recog_data.n_operands; ++i)
1612 x = recog_data.operand[i];
1613 switch (GET_CODE (x))
1615 case MEM:
1617 rtx addr = XEXP (x, 0);
1618 bool changed = false;
1620 for_each_rtx (&addr, instantiate_virtual_regs_in_rtx, &changed);
1621 if (!changed)
1622 continue;
1624 start_sequence ();
1625 x = replace_equiv_address (x, addr, true);
1626 /* It may happen that the address with the virtual reg
1627 was valid (e.g. based on the virtual stack reg, which might
1628 be acceptable to the predicates with all offsets), whereas
1629 the address now isn't anymore, for instance when the address
1630 is still offsetted, but the base reg isn't virtual-stack-reg
1631 anymore. Below we would do a force_reg on the whole operand,
1632 but this insn might actually only accept memory. Hence,
1633 before doing that last resort, try to reload the address into
1634 a register, so this operand stays a MEM. */
1635 if (!safe_insn_predicate (insn_code, i, x))
1637 addr = force_reg (GET_MODE (addr), addr);
1638 x = replace_equiv_address (x, addr, true);
1640 seq = get_insns ();
1641 end_sequence ();
1642 if (seq)
1643 emit_insn_before (seq, insn);
1645 break;
1647 case REG:
1648 new_rtx = instantiate_new_reg (x, &offset);
1649 if (new_rtx == NULL)
1650 continue;
1651 if (offset == 0)
1652 x = new_rtx;
1653 else
1655 start_sequence ();
1657 /* Careful, special mode predicates may have stuff in
1658 insn_data[insn_code].operand[i].mode that isn't useful
1659 to us for computing a new value. */
1660 /* ??? Recognize address_operand and/or "p" constraints
1661 to see if (plus new offset) is a valid before we put
1662 this through expand_simple_binop. */
1663 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1664 gen_int_mode (offset, GET_MODE (x)),
1665 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1666 seq = get_insns ();
1667 end_sequence ();
1668 emit_insn_before (seq, insn);
1670 break;
1672 case SUBREG:
1673 new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1674 if (new_rtx == NULL)
1675 continue;
1676 if (offset != 0)
1678 start_sequence ();
1679 new_rtx = expand_simple_binop
1680 (GET_MODE (new_rtx), PLUS, new_rtx,
1681 gen_int_mode (offset, GET_MODE (new_rtx)),
1682 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1683 seq = get_insns ();
1684 end_sequence ();
1685 emit_insn_before (seq, insn);
1687 x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1688 GET_MODE (new_rtx), SUBREG_BYTE (x));
1689 gcc_assert (x);
1690 break;
1692 default:
1693 continue;
1696 /* At this point, X contains the new value for the operand.
1697 Validate the new value vs the insn predicate. Note that
1698 asm insns will have insn_code -1 here. */
1699 if (!safe_insn_predicate (insn_code, i, x))
1701 start_sequence ();
1702 if (REG_P (x))
1704 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1705 x = copy_to_reg (x);
1707 else
1708 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1709 seq = get_insns ();
1710 end_sequence ();
1711 if (seq)
1712 emit_insn_before (seq, insn);
1715 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1716 any_change = true;
1719 if (any_change)
1721 /* Propagate operand changes into the duplicates. */
1722 for (i = 0; i < recog_data.n_dups; ++i)
1723 *recog_data.dup_loc[i]
1724 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1726 /* Force re-recognition of the instruction for validation. */
1727 INSN_CODE (insn) = -1;
1730 if (asm_noperands (PATTERN (insn)) >= 0)
1732 if (!check_asm_operands (PATTERN (insn)))
1734 error_for_asm (insn, "impossible constraint in %<asm%>");
1735 /* For asm goto, instead of fixing up all the edges
1736 just clear the template and clear input operands
1737 (asm goto doesn't have any output operands). */
1738 if (JUMP_P (insn))
1740 rtx asm_op = extract_asm_operands (PATTERN (insn));
1741 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1742 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1743 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1745 else
1746 delete_insn (insn);
1749 else
1751 if (recog_memoized (insn) < 0)
1752 fatal_insn_not_found (insn);
1756 /* Subroutine of instantiate_decls. Given RTL representing a decl,
1757 do any instantiation required. */
1759 void
1760 instantiate_decl_rtl (rtx x)
1762 rtx addr;
1764 if (x == 0)
1765 return;
1767 /* If this is a CONCAT, recurse for the pieces. */
1768 if (GET_CODE (x) == CONCAT)
1770 instantiate_decl_rtl (XEXP (x, 0));
1771 instantiate_decl_rtl (XEXP (x, 1));
1772 return;
1775 /* If this is not a MEM, no need to do anything. Similarly if the
1776 address is a constant or a register that is not a virtual register. */
1777 if (!MEM_P (x))
1778 return;
1780 addr = XEXP (x, 0);
1781 if (CONSTANT_P (addr)
1782 || (REG_P (addr)
1783 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1784 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1785 return;
1787 for_each_rtx (&XEXP (x, 0), instantiate_virtual_regs_in_rtx, NULL);
1790 /* Helper for instantiate_decls called via walk_tree: Process all decls
1791 in the given DECL_VALUE_EXPR. */
1793 static tree
1794 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1796 tree t = *tp;
1797 if (! EXPR_P (t))
1799 *walk_subtrees = 0;
1800 if (DECL_P (t))
1802 if (DECL_RTL_SET_P (t))
1803 instantiate_decl_rtl (DECL_RTL (t));
1804 if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1805 && DECL_INCOMING_RTL (t))
1806 instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1807 if ((TREE_CODE (t) == VAR_DECL
1808 || TREE_CODE (t) == RESULT_DECL)
1809 && DECL_HAS_VALUE_EXPR_P (t))
1811 tree v = DECL_VALUE_EXPR (t);
1812 walk_tree (&v, instantiate_expr, NULL, NULL);
1816 return NULL;
1819 /* Subroutine of instantiate_decls: Process all decls in the given
1820 BLOCK node and all its subblocks. */
1822 static void
1823 instantiate_decls_1 (tree let)
1825 tree t;
1827 for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1829 if (DECL_RTL_SET_P (t))
1830 instantiate_decl_rtl (DECL_RTL (t));
1831 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t))
1833 tree v = DECL_VALUE_EXPR (t);
1834 walk_tree (&v, instantiate_expr, NULL, NULL);
1838 /* Process all subblocks. */
1839 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1840 instantiate_decls_1 (t);
1843 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1844 all virtual registers in their DECL_RTL's. */
1846 static void
1847 instantiate_decls (tree fndecl)
1849 tree decl;
1850 unsigned ix;
1852 /* Process all parameters of the function. */
1853 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1855 instantiate_decl_rtl (DECL_RTL (decl));
1856 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1857 if (DECL_HAS_VALUE_EXPR_P (decl))
1859 tree v = DECL_VALUE_EXPR (decl);
1860 walk_tree (&v, instantiate_expr, NULL, NULL);
1864 if ((decl = DECL_RESULT (fndecl))
1865 && TREE_CODE (decl) == RESULT_DECL)
1867 if (DECL_RTL_SET_P (decl))
1868 instantiate_decl_rtl (DECL_RTL (decl));
1869 if (DECL_HAS_VALUE_EXPR_P (decl))
1871 tree v = DECL_VALUE_EXPR (decl);
1872 walk_tree (&v, instantiate_expr, NULL, NULL);
1876 /* Now process all variables defined in the function or its subblocks. */
1877 instantiate_decls_1 (DECL_INITIAL (fndecl));
1879 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1880 if (DECL_RTL_SET_P (decl))
1881 instantiate_decl_rtl (DECL_RTL (decl));
1882 vec_free (cfun->local_decls);
1885 /* Pass through the INSNS of function FNDECL and convert virtual register
1886 references to hard register references. */
1888 static unsigned int
1889 instantiate_virtual_regs (void)
1891 rtx insn;
1893 /* Compute the offsets to use for this function. */
1894 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1895 var_offset = STARTING_FRAME_OFFSET;
1896 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1897 out_arg_offset = STACK_POINTER_OFFSET;
1898 #ifdef FRAME_POINTER_CFA_OFFSET
1899 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1900 #else
1901 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1902 #endif
1904 /* Initialize recognition, indicating that volatile is OK. */
1905 init_recog ();
1907 /* Scan through all the insns, instantiating every virtual register still
1908 present. */
1909 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1910 if (INSN_P (insn))
1912 /* These patterns in the instruction stream can never be recognized.
1913 Fortunately, they shouldn't contain virtual registers either. */
1914 if (GET_CODE (PATTERN (insn)) == USE
1915 || GET_CODE (PATTERN (insn)) == CLOBBER
1916 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
1917 continue;
1918 else if (DEBUG_INSN_P (insn))
1919 for_each_rtx (&INSN_VAR_LOCATION (insn),
1920 instantiate_virtual_regs_in_rtx, NULL);
1921 else
1922 instantiate_virtual_regs_in_insn (insn);
1924 if (INSN_DELETED_P (insn))
1925 continue;
1927 for_each_rtx (&REG_NOTES (insn), instantiate_virtual_regs_in_rtx, NULL);
1929 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
1930 if (CALL_P (insn))
1931 for_each_rtx (&CALL_INSN_FUNCTION_USAGE (insn),
1932 instantiate_virtual_regs_in_rtx, NULL);
1935 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
1936 instantiate_decls (current_function_decl);
1938 targetm.instantiate_decls ();
1940 /* Indicate that, from now on, assign_stack_local should use
1941 frame_pointer_rtx. */
1942 virtuals_instantiated = 1;
1944 return 0;
1947 namespace {
1949 const pass_data pass_data_instantiate_virtual_regs =
1951 RTL_PASS, /* type */
1952 "vregs", /* name */
1953 OPTGROUP_NONE, /* optinfo_flags */
1954 true, /* has_execute */
1955 TV_NONE, /* tv_id */
1956 0, /* properties_required */
1957 0, /* properties_provided */
1958 0, /* properties_destroyed */
1959 0, /* todo_flags_start */
1960 0, /* todo_flags_finish */
1963 class pass_instantiate_virtual_regs : public rtl_opt_pass
1965 public:
1966 pass_instantiate_virtual_regs (gcc::context *ctxt)
1967 : rtl_opt_pass (pass_data_instantiate_virtual_regs, ctxt)
1970 /* opt_pass methods: */
1971 virtual unsigned int execute (function *)
1973 return instantiate_virtual_regs ();
1976 }; // class pass_instantiate_virtual_regs
1978 } // anon namespace
1980 rtl_opt_pass *
1981 make_pass_instantiate_virtual_regs (gcc::context *ctxt)
1983 return new pass_instantiate_virtual_regs (ctxt);
1987 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
1988 This means a type for which function calls must pass an address to the
1989 function or get an address back from the function.
1990 EXP may be a type node or an expression (whose type is tested). */
1993 aggregate_value_p (const_tree exp, const_tree fntype)
1995 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
1996 int i, regno, nregs;
1997 rtx reg;
1999 if (fntype)
2000 switch (TREE_CODE (fntype))
2002 case CALL_EXPR:
2004 tree fndecl = get_callee_fndecl (fntype);
2005 fntype = (fndecl
2006 ? TREE_TYPE (fndecl)
2007 : TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype))));
2009 break;
2010 case FUNCTION_DECL:
2011 fntype = TREE_TYPE (fntype);
2012 break;
2013 case FUNCTION_TYPE:
2014 case METHOD_TYPE:
2015 break;
2016 case IDENTIFIER_NODE:
2017 fntype = NULL_TREE;
2018 break;
2019 default:
2020 /* We don't expect other tree types here. */
2021 gcc_unreachable ();
2024 if (VOID_TYPE_P (type))
2025 return 0;
2027 /* If a record should be passed the same as its first (and only) member
2028 don't pass it as an aggregate. */
2029 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2030 return aggregate_value_p (first_field (type), fntype);
2032 /* If the front end has decided that this needs to be passed by
2033 reference, do so. */
2034 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2035 && DECL_BY_REFERENCE (exp))
2036 return 1;
2038 /* Function types that are TREE_ADDRESSABLE force return in memory. */
2039 if (fntype && TREE_ADDRESSABLE (fntype))
2040 return 1;
2042 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2043 and thus can't be returned in registers. */
2044 if (TREE_ADDRESSABLE (type))
2045 return 1;
2047 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2048 return 1;
2050 if (targetm.calls.return_in_memory (type, fntype))
2051 return 1;
2053 /* Make sure we have suitable call-clobbered regs to return
2054 the value in; if not, we must return it in memory. */
2055 reg = hard_function_value (type, 0, fntype, 0);
2057 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2058 it is OK. */
2059 if (!REG_P (reg))
2060 return 0;
2062 regno = REGNO (reg);
2063 nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
2064 for (i = 0; i < nregs; i++)
2065 if (! call_used_regs[regno + i])
2066 return 1;
2068 return 0;
2071 /* Return true if we should assign DECL a pseudo register; false if it
2072 should live on the local stack. */
2074 bool
2075 use_register_for_decl (const_tree decl)
2077 if (!targetm.calls.allocate_stack_slots_for_args ())
2078 return true;
2080 /* Honor volatile. */
2081 if (TREE_SIDE_EFFECTS (decl))
2082 return false;
2084 /* Honor addressability. */
2085 if (TREE_ADDRESSABLE (decl))
2086 return false;
2088 /* Only register-like things go in registers. */
2089 if (DECL_MODE (decl) == BLKmode)
2090 return false;
2092 /* If -ffloat-store specified, don't put explicit float variables
2093 into registers. */
2094 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2095 propagates values across these stores, and it probably shouldn't. */
2096 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2097 return false;
2099 /* If we're not interested in tracking debugging information for
2100 this decl, then we can certainly put it in a register. */
2101 if (DECL_IGNORED_P (decl))
2102 return true;
2104 if (optimize)
2105 return true;
2107 if (!DECL_REGISTER (decl))
2108 return false;
2110 switch (TREE_CODE (TREE_TYPE (decl)))
2112 case RECORD_TYPE:
2113 case UNION_TYPE:
2114 case QUAL_UNION_TYPE:
2115 /* When not optimizing, disregard register keyword for variables with
2116 types containing methods, otherwise the methods won't be callable
2117 from the debugger. */
2118 if (TYPE_METHODS (TREE_TYPE (decl)))
2119 return false;
2120 break;
2121 default:
2122 break;
2125 return true;
2128 /* Return true if TYPE should be passed by invisible reference. */
2130 bool
2131 pass_by_reference (CUMULATIVE_ARGS *ca, enum machine_mode mode,
2132 tree type, bool named_arg)
2134 if (type)
2136 /* If this type contains non-trivial constructors, then it is
2137 forbidden for the middle-end to create any new copies. */
2138 if (TREE_ADDRESSABLE (type))
2139 return true;
2141 /* GCC post 3.4 passes *all* variable sized types by reference. */
2142 if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2143 return true;
2145 /* If a record type should be passed the same as its first (and only)
2146 member, use the type and mode of that member. */
2147 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2149 type = TREE_TYPE (first_field (type));
2150 mode = TYPE_MODE (type);
2154 return targetm.calls.pass_by_reference (pack_cumulative_args (ca), mode,
2155 type, named_arg);
2158 /* Return true if TYPE, which is passed by reference, should be callee
2159 copied instead of caller copied. */
2161 bool
2162 reference_callee_copied (CUMULATIVE_ARGS *ca, enum machine_mode mode,
2163 tree type, bool named_arg)
2165 if (type && TREE_ADDRESSABLE (type))
2166 return false;
2167 return targetm.calls.callee_copies (pack_cumulative_args (ca), mode, type,
2168 named_arg);
2171 /* Structures to communicate between the subroutines of assign_parms.
2172 The first holds data persistent across all parameters, the second
2173 is cleared out for each parameter. */
2175 struct assign_parm_data_all
2177 /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2178 should become a job of the target or otherwise encapsulated. */
2179 CUMULATIVE_ARGS args_so_far_v;
2180 cumulative_args_t args_so_far;
2181 struct args_size stack_args_size;
2182 tree function_result_decl;
2183 tree orig_fnargs;
2184 rtx first_conversion_insn;
2185 rtx last_conversion_insn;
2186 HOST_WIDE_INT pretend_args_size;
2187 HOST_WIDE_INT extra_pretend_bytes;
2188 int reg_parm_stack_space;
2191 struct assign_parm_data_one
2193 tree nominal_type;
2194 tree passed_type;
2195 rtx entry_parm;
2196 rtx stack_parm;
2197 enum machine_mode nominal_mode;
2198 enum machine_mode passed_mode;
2199 enum machine_mode promoted_mode;
2200 struct locate_and_pad_arg_data locate;
2201 int partial;
2202 BOOL_BITFIELD named_arg : 1;
2203 BOOL_BITFIELD passed_pointer : 1;
2204 BOOL_BITFIELD on_stack : 1;
2205 BOOL_BITFIELD loaded_in_reg : 1;
2208 /* A subroutine of assign_parms. Initialize ALL. */
2210 static void
2211 assign_parms_initialize_all (struct assign_parm_data_all *all)
2213 tree fntype ATTRIBUTE_UNUSED;
2215 memset (all, 0, sizeof (*all));
2217 fntype = TREE_TYPE (current_function_decl);
2219 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
2220 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2221 #else
2222 INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2223 current_function_decl, -1);
2224 #endif
2225 all->args_so_far = pack_cumulative_args (&all->args_so_far_v);
2227 #ifdef REG_PARM_STACK_SPACE
2228 all->reg_parm_stack_space = REG_PARM_STACK_SPACE (current_function_decl);
2229 #endif
2232 /* If ARGS contains entries with complex types, split the entry into two
2233 entries of the component type. Return a new list of substitutions are
2234 needed, else the old list. */
2236 static void
2237 split_complex_args (vec<tree> *args)
2239 unsigned i;
2240 tree p;
2242 FOR_EACH_VEC_ELT (*args, i, p)
2244 tree type = TREE_TYPE (p);
2245 if (TREE_CODE (type) == COMPLEX_TYPE
2246 && targetm.calls.split_complex_arg (type))
2248 tree decl;
2249 tree subtype = TREE_TYPE (type);
2250 bool addressable = TREE_ADDRESSABLE (p);
2252 /* Rewrite the PARM_DECL's type with its component. */
2253 p = copy_node (p);
2254 TREE_TYPE (p) = subtype;
2255 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2256 DECL_MODE (p) = VOIDmode;
2257 DECL_SIZE (p) = NULL;
2258 DECL_SIZE_UNIT (p) = NULL;
2259 /* If this arg must go in memory, put it in a pseudo here.
2260 We can't allow it to go in memory as per normal parms,
2261 because the usual place might not have the imag part
2262 adjacent to the real part. */
2263 DECL_ARTIFICIAL (p) = addressable;
2264 DECL_IGNORED_P (p) = addressable;
2265 TREE_ADDRESSABLE (p) = 0;
2266 layout_decl (p, 0);
2267 (*args)[i] = p;
2269 /* Build a second synthetic decl. */
2270 decl = build_decl (EXPR_LOCATION (p),
2271 PARM_DECL, NULL_TREE, subtype);
2272 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2273 DECL_ARTIFICIAL (decl) = addressable;
2274 DECL_IGNORED_P (decl) = addressable;
2275 layout_decl (decl, 0);
2276 args->safe_insert (++i, decl);
2281 /* A subroutine of assign_parms. Adjust the parameter list to incorporate
2282 the hidden struct return argument, and (abi willing) complex args.
2283 Return the new parameter list. */
2285 static vec<tree>
2286 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2288 tree fndecl = current_function_decl;
2289 tree fntype = TREE_TYPE (fndecl);
2290 vec<tree> fnargs = vNULL;
2291 tree arg;
2293 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2294 fnargs.safe_push (arg);
2296 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2298 /* If struct value address is treated as the first argument, make it so. */
2299 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2300 && ! cfun->returns_pcc_struct
2301 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2303 tree type = build_pointer_type (TREE_TYPE (fntype));
2304 tree decl;
2306 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2307 PARM_DECL, get_identifier (".result_ptr"), type);
2308 DECL_ARG_TYPE (decl) = type;
2309 DECL_ARTIFICIAL (decl) = 1;
2310 DECL_NAMELESS (decl) = 1;
2311 TREE_CONSTANT (decl) = 1;
2313 DECL_CHAIN (decl) = all->orig_fnargs;
2314 all->orig_fnargs = decl;
2315 fnargs.safe_insert (0, decl);
2317 all->function_result_decl = decl;
2320 /* If the target wants to split complex arguments into scalars, do so. */
2321 if (targetm.calls.split_complex_arg)
2322 split_complex_args (&fnargs);
2324 return fnargs;
2327 /* A subroutine of assign_parms. Examine PARM and pull out type and mode
2328 data for the parameter. Incorporate ABI specifics such as pass-by-
2329 reference and type promotion. */
2331 static void
2332 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2333 struct assign_parm_data_one *data)
2335 tree nominal_type, passed_type;
2336 enum machine_mode nominal_mode, passed_mode, promoted_mode;
2337 int unsignedp;
2339 memset (data, 0, sizeof (*data));
2341 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
2342 if (!cfun->stdarg)
2343 data->named_arg = 1; /* No variadic parms. */
2344 else if (DECL_CHAIN (parm))
2345 data->named_arg = 1; /* Not the last non-variadic parm. */
2346 else if (targetm.calls.strict_argument_naming (all->args_so_far))
2347 data->named_arg = 1; /* Only variadic ones are unnamed. */
2348 else
2349 data->named_arg = 0; /* Treat as variadic. */
2351 nominal_type = TREE_TYPE (parm);
2352 passed_type = DECL_ARG_TYPE (parm);
2354 /* Look out for errors propagating this far. Also, if the parameter's
2355 type is void then its value doesn't matter. */
2356 if (TREE_TYPE (parm) == error_mark_node
2357 /* This can happen after weird syntax errors
2358 or if an enum type is defined among the parms. */
2359 || TREE_CODE (parm) != PARM_DECL
2360 || passed_type == NULL
2361 || VOID_TYPE_P (nominal_type))
2363 nominal_type = passed_type = void_type_node;
2364 nominal_mode = passed_mode = promoted_mode = VOIDmode;
2365 goto egress;
2368 /* Find mode of arg as it is passed, and mode of arg as it should be
2369 during execution of this function. */
2370 passed_mode = TYPE_MODE (passed_type);
2371 nominal_mode = TYPE_MODE (nominal_type);
2373 /* If the parm is to be passed as a transparent union or record, use the
2374 type of the first field for the tests below. We have already verified
2375 that the modes are the same. */
2376 if ((TREE_CODE (passed_type) == UNION_TYPE
2377 || TREE_CODE (passed_type) == RECORD_TYPE)
2378 && TYPE_TRANSPARENT_AGGR (passed_type))
2379 passed_type = TREE_TYPE (first_field (passed_type));
2381 /* See if this arg was passed by invisible reference. */
2382 if (pass_by_reference (&all->args_so_far_v, passed_mode,
2383 passed_type, data->named_arg))
2385 passed_type = nominal_type = build_pointer_type (passed_type);
2386 data->passed_pointer = true;
2387 passed_mode = nominal_mode = TYPE_MODE (nominal_type);
2390 /* Find mode as it is passed by the ABI. */
2391 unsignedp = TYPE_UNSIGNED (passed_type);
2392 promoted_mode = promote_function_mode (passed_type, passed_mode, &unsignedp,
2393 TREE_TYPE (current_function_decl), 0);
2395 egress:
2396 data->nominal_type = nominal_type;
2397 data->passed_type = passed_type;
2398 data->nominal_mode = nominal_mode;
2399 data->passed_mode = passed_mode;
2400 data->promoted_mode = promoted_mode;
2403 /* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2405 static void
2406 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2407 struct assign_parm_data_one *data, bool no_rtl)
2409 int varargs_pretend_bytes = 0;
2411 targetm.calls.setup_incoming_varargs (all->args_so_far,
2412 data->promoted_mode,
2413 data->passed_type,
2414 &varargs_pretend_bytes, no_rtl);
2416 /* If the back-end has requested extra stack space, record how much is
2417 needed. Do not change pretend_args_size otherwise since it may be
2418 nonzero from an earlier partial argument. */
2419 if (varargs_pretend_bytes > 0)
2420 all->pretend_args_size = varargs_pretend_bytes;
2423 /* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2424 the incoming location of the current parameter. */
2426 static void
2427 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2428 struct assign_parm_data_one *data)
2430 HOST_WIDE_INT pretend_bytes = 0;
2431 rtx entry_parm;
2432 bool in_regs;
2434 if (data->promoted_mode == VOIDmode)
2436 data->entry_parm = data->stack_parm = const0_rtx;
2437 return;
2440 entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2441 data->promoted_mode,
2442 data->passed_type,
2443 data->named_arg);
2445 if (entry_parm == 0)
2446 data->promoted_mode = data->passed_mode;
2448 /* Determine parm's home in the stack, in case it arrives in the stack
2449 or we should pretend it did. Compute the stack position and rtx where
2450 the argument arrives and its size.
2452 There is one complexity here: If this was a parameter that would
2453 have been passed in registers, but wasn't only because it is
2454 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2455 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2456 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2457 as it was the previous time. */
2458 in_regs = entry_parm != 0;
2459 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2460 in_regs = true;
2461 #endif
2462 if (!in_regs && !data->named_arg)
2464 if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2466 rtx tem;
2467 tem = targetm.calls.function_incoming_arg (all->args_so_far,
2468 data->promoted_mode,
2469 data->passed_type, true);
2470 in_regs = tem != NULL;
2474 /* If this parameter was passed both in registers and in the stack, use
2475 the copy on the stack. */
2476 if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2477 data->passed_type))
2478 entry_parm = 0;
2480 if (entry_parm)
2482 int partial;
2484 partial = targetm.calls.arg_partial_bytes (all->args_so_far,
2485 data->promoted_mode,
2486 data->passed_type,
2487 data->named_arg);
2488 data->partial = partial;
2490 /* The caller might already have allocated stack space for the
2491 register parameters. */
2492 if (partial != 0 && all->reg_parm_stack_space == 0)
2494 /* Part of this argument is passed in registers and part
2495 is passed on the stack. Ask the prologue code to extend
2496 the stack part so that we can recreate the full value.
2498 PRETEND_BYTES is the size of the registers we need to store.
2499 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2500 stack space that the prologue should allocate.
2502 Internally, gcc assumes that the argument pointer is aligned
2503 to STACK_BOUNDARY bits. This is used both for alignment
2504 optimizations (see init_emit) and to locate arguments that are
2505 aligned to more than PARM_BOUNDARY bits. We must preserve this
2506 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2507 a stack boundary. */
2509 /* We assume at most one partial arg, and it must be the first
2510 argument on the stack. */
2511 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2513 pretend_bytes = partial;
2514 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2516 /* We want to align relative to the actual stack pointer, so
2517 don't include this in the stack size until later. */
2518 all->extra_pretend_bytes = all->pretend_args_size;
2522 locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2523 all->reg_parm_stack_space,
2524 entry_parm ? data->partial : 0, current_function_decl,
2525 &all->stack_args_size, &data->locate);
2527 /* Update parm_stack_boundary if this parameter is passed in the
2528 stack. */
2529 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2530 crtl->parm_stack_boundary = data->locate.boundary;
2532 /* Adjust offsets to include the pretend args. */
2533 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2534 data->locate.slot_offset.constant += pretend_bytes;
2535 data->locate.offset.constant += pretend_bytes;
2537 data->entry_parm = entry_parm;
2540 /* A subroutine of assign_parms. If there is actually space on the stack
2541 for this parm, count it in stack_args_size and return true. */
2543 static bool
2544 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2545 struct assign_parm_data_one *data)
2547 /* Trivially true if we've no incoming register. */
2548 if (data->entry_parm == NULL)
2550 /* Also true if we're partially in registers and partially not,
2551 since we've arranged to drop the entire argument on the stack. */
2552 else if (data->partial != 0)
2554 /* Also true if the target says that it's passed in both registers
2555 and on the stack. */
2556 else if (GET_CODE (data->entry_parm) == PARALLEL
2557 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2559 /* Also true if the target says that there's stack allocated for
2560 all register parameters. */
2561 else if (all->reg_parm_stack_space > 0)
2563 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2564 else
2565 return false;
2567 all->stack_args_size.constant += data->locate.size.constant;
2568 if (data->locate.size.var)
2569 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2571 return true;
2574 /* A subroutine of assign_parms. Given that this parameter is allocated
2575 stack space by the ABI, find it. */
2577 static void
2578 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2580 rtx offset_rtx, stack_parm;
2581 unsigned int align, boundary;
2583 /* If we're passing this arg using a reg, make its stack home the
2584 aligned stack slot. */
2585 if (data->entry_parm)
2586 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2587 else
2588 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2590 stack_parm = crtl->args.internal_arg_pointer;
2591 if (offset_rtx != const0_rtx)
2592 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2593 stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2595 if (!data->passed_pointer)
2597 set_mem_attributes (stack_parm, parm, 1);
2598 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2599 while promoted mode's size is needed. */
2600 if (data->promoted_mode != BLKmode
2601 && data->promoted_mode != DECL_MODE (parm))
2603 set_mem_size (stack_parm, GET_MODE_SIZE (data->promoted_mode));
2604 if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2606 int offset = subreg_lowpart_offset (DECL_MODE (parm),
2607 data->promoted_mode);
2608 if (offset)
2609 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2614 boundary = data->locate.boundary;
2615 align = BITS_PER_UNIT;
2617 /* If we're padding upward, we know that the alignment of the slot
2618 is TARGET_FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2619 intentionally forcing upward padding. Otherwise we have to come
2620 up with a guess at the alignment based on OFFSET_RTX. */
2621 if (data->locate.where_pad != downward || data->entry_parm)
2622 align = boundary;
2623 else if (CONST_INT_P (offset_rtx))
2625 align = INTVAL (offset_rtx) * BITS_PER_UNIT | boundary;
2626 align = align & -align;
2628 set_mem_align (stack_parm, align);
2630 if (data->entry_parm)
2631 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2633 data->stack_parm = stack_parm;
2636 /* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2637 always valid and contiguous. */
2639 static void
2640 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2642 rtx entry_parm = data->entry_parm;
2643 rtx stack_parm = data->stack_parm;
2645 /* If this parm was passed part in regs and part in memory, pretend it
2646 arrived entirely in memory by pushing the register-part onto the stack.
2647 In the special case of a DImode or DFmode that is split, we could put
2648 it together in a pseudoreg directly, but for now that's not worth
2649 bothering with. */
2650 if (data->partial != 0)
2652 /* Handle calls that pass values in multiple non-contiguous
2653 locations. The Irix 6 ABI has examples of this. */
2654 if (GET_CODE (entry_parm) == PARALLEL)
2655 emit_group_store (validize_mem (stack_parm), entry_parm,
2656 data->passed_type,
2657 int_size_in_bytes (data->passed_type));
2658 else
2660 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2661 move_block_from_reg (REGNO (entry_parm), validize_mem (stack_parm),
2662 data->partial / UNITS_PER_WORD);
2665 entry_parm = stack_parm;
2668 /* If we didn't decide this parm came in a register, by default it came
2669 on the stack. */
2670 else if (entry_parm == NULL)
2671 entry_parm = stack_parm;
2673 /* When an argument is passed in multiple locations, we can't make use
2674 of this information, but we can save some copying if the whole argument
2675 is passed in a single register. */
2676 else if (GET_CODE (entry_parm) == PARALLEL
2677 && data->nominal_mode != BLKmode
2678 && data->passed_mode != BLKmode)
2680 size_t i, len = XVECLEN (entry_parm, 0);
2682 for (i = 0; i < len; i++)
2683 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2684 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2685 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2686 == data->passed_mode)
2687 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2689 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2690 break;
2694 data->entry_parm = entry_parm;
2697 /* A subroutine of assign_parms. Reconstitute any values which were
2698 passed in multiple registers and would fit in a single register. */
2700 static void
2701 assign_parm_remove_parallels (struct assign_parm_data_one *data)
2703 rtx entry_parm = data->entry_parm;
2705 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2706 This can be done with register operations rather than on the
2707 stack, even if we will store the reconstituted parameter on the
2708 stack later. */
2709 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2711 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2712 emit_group_store (parmreg, entry_parm, data->passed_type,
2713 GET_MODE_SIZE (GET_MODE (entry_parm)));
2714 entry_parm = parmreg;
2717 data->entry_parm = entry_parm;
2720 /* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2721 always valid and properly aligned. */
2723 static void
2724 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2726 rtx stack_parm = data->stack_parm;
2728 /* If we can't trust the parm stack slot to be aligned enough for its
2729 ultimate type, don't use that slot after entry. We'll make another
2730 stack slot, if we need one. */
2731 if (stack_parm
2732 && ((STRICT_ALIGNMENT
2733 && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2734 || (data->nominal_type
2735 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2736 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2737 stack_parm = NULL;
2739 /* If parm was passed in memory, and we need to convert it on entry,
2740 don't store it back in that same slot. */
2741 else if (data->entry_parm == stack_parm
2742 && data->nominal_mode != BLKmode
2743 && data->nominal_mode != data->passed_mode)
2744 stack_parm = NULL;
2746 /* If stack protection is in effect for this function, don't leave any
2747 pointers in their passed stack slots. */
2748 else if (crtl->stack_protect_guard
2749 && (flag_stack_protect == 2
2750 || data->passed_pointer
2751 || POINTER_TYPE_P (data->nominal_type)))
2752 stack_parm = NULL;
2754 data->stack_parm = stack_parm;
2757 /* A subroutine of assign_parms. Return true if the current parameter
2758 should be stored as a BLKmode in the current frame. */
2760 static bool
2761 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2763 if (data->nominal_mode == BLKmode)
2764 return true;
2765 if (GET_MODE (data->entry_parm) == BLKmode)
2766 return true;
2768 #ifdef BLOCK_REG_PADDING
2769 /* Only assign_parm_setup_block knows how to deal with register arguments
2770 that are padded at the least significant end. */
2771 if (REG_P (data->entry_parm)
2772 && GET_MODE_SIZE (data->promoted_mode) < UNITS_PER_WORD
2773 && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2774 == (BYTES_BIG_ENDIAN ? upward : downward)))
2775 return true;
2776 #endif
2778 return false;
2781 /* A subroutine of assign_parms. Arrange for the parameter to be
2782 present and valid in DATA->STACK_RTL. */
2784 static void
2785 assign_parm_setup_block (struct assign_parm_data_all *all,
2786 tree parm, struct assign_parm_data_one *data)
2788 rtx entry_parm = data->entry_parm;
2789 rtx stack_parm = data->stack_parm;
2790 HOST_WIDE_INT size;
2791 HOST_WIDE_INT size_stored;
2793 if (GET_CODE (entry_parm) == PARALLEL)
2794 entry_parm = emit_group_move_into_temps (entry_parm);
2796 size = int_size_in_bytes (data->passed_type);
2797 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2798 if (stack_parm == 0)
2800 DECL_ALIGN (parm) = MAX (DECL_ALIGN (parm), BITS_PER_WORD);
2801 stack_parm = assign_stack_local (BLKmode, size_stored,
2802 DECL_ALIGN (parm));
2803 if (GET_MODE_SIZE (GET_MODE (entry_parm)) == size)
2804 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2805 set_mem_attributes (stack_parm, parm, 1);
2808 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2809 calls that pass values in multiple non-contiguous locations. */
2810 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2812 rtx mem;
2814 /* Note that we will be storing an integral number of words.
2815 So we have to be careful to ensure that we allocate an
2816 integral number of words. We do this above when we call
2817 assign_stack_local if space was not allocated in the argument
2818 list. If it was, this will not work if PARM_BOUNDARY is not
2819 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2820 if it becomes a problem. Exception is when BLKmode arrives
2821 with arguments not conforming to word_mode. */
2823 if (data->stack_parm == 0)
2825 else if (GET_CODE (entry_parm) == PARALLEL)
2827 else
2828 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2830 mem = validize_mem (stack_parm);
2832 /* Handle values in multiple non-contiguous locations. */
2833 if (GET_CODE (entry_parm) == PARALLEL)
2835 push_to_sequence2 (all->first_conversion_insn,
2836 all->last_conversion_insn);
2837 emit_group_store (mem, entry_parm, data->passed_type, size);
2838 all->first_conversion_insn = get_insns ();
2839 all->last_conversion_insn = get_last_insn ();
2840 end_sequence ();
2843 else if (size == 0)
2846 /* If SIZE is that of a mode no bigger than a word, just use
2847 that mode's store operation. */
2848 else if (size <= UNITS_PER_WORD)
2850 enum machine_mode mode
2851 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2853 if (mode != BLKmode
2854 #ifdef BLOCK_REG_PADDING
2855 && (size == UNITS_PER_WORD
2856 || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2857 != (BYTES_BIG_ENDIAN ? upward : downward)))
2858 #endif
2861 rtx reg;
2863 /* We are really truncating a word_mode value containing
2864 SIZE bytes into a value of mode MODE. If such an
2865 operation requires no actual instructions, we can refer
2866 to the value directly in mode MODE, otherwise we must
2867 start with the register in word_mode and explicitly
2868 convert it. */
2869 if (TRULY_NOOP_TRUNCATION (size * BITS_PER_UNIT, BITS_PER_WORD))
2870 reg = gen_rtx_REG (mode, REGNO (entry_parm));
2871 else
2873 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2874 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
2876 emit_move_insn (change_address (mem, mode, 0), reg);
2879 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
2880 machine must be aligned to the left before storing
2881 to memory. Note that the previous test doesn't
2882 handle all cases (e.g. SIZE == 3). */
2883 else if (size != UNITS_PER_WORD
2884 #ifdef BLOCK_REG_PADDING
2885 && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2886 == downward)
2887 #else
2888 && BYTES_BIG_ENDIAN
2889 #endif
2892 rtx tem, x;
2893 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2894 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2896 x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
2897 tem = change_address (mem, word_mode, 0);
2898 emit_move_insn (tem, x);
2900 else
2901 move_block_from_reg (REGNO (entry_parm), mem,
2902 size_stored / UNITS_PER_WORD);
2904 else
2905 move_block_from_reg (REGNO (entry_parm), mem,
2906 size_stored / UNITS_PER_WORD);
2908 else if (data->stack_parm == 0)
2910 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
2911 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
2912 BLOCK_OP_NORMAL);
2913 all->first_conversion_insn = get_insns ();
2914 all->last_conversion_insn = get_last_insn ();
2915 end_sequence ();
2918 data->stack_parm = stack_parm;
2919 SET_DECL_RTL (parm, stack_parm);
2922 /* A subroutine of assign_parms. Allocate a pseudo to hold the current
2923 parameter. Get it there. Perform all ABI specified conversions. */
2925 static void
2926 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
2927 struct assign_parm_data_one *data)
2929 rtx parmreg, validated_mem;
2930 rtx equiv_stack_parm;
2931 enum machine_mode promoted_nominal_mode;
2932 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
2933 bool did_conversion = false;
2934 bool need_conversion, moved;
2936 /* Store the parm in a pseudoregister during the function, but we may
2937 need to do it in a wider mode. Using 2 here makes the result
2938 consistent with promote_decl_mode and thus expand_expr_real_1. */
2939 promoted_nominal_mode
2940 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
2941 TREE_TYPE (current_function_decl), 2);
2943 parmreg = gen_reg_rtx (promoted_nominal_mode);
2945 if (!DECL_ARTIFICIAL (parm))
2946 mark_user_reg (parmreg);
2948 /* If this was an item that we received a pointer to,
2949 set DECL_RTL appropriately. */
2950 if (data->passed_pointer)
2952 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
2953 set_mem_attributes (x, parm, 1);
2954 SET_DECL_RTL (parm, x);
2956 else
2957 SET_DECL_RTL (parm, parmreg);
2959 assign_parm_remove_parallels (data);
2961 /* Copy the value into the register, thus bridging between
2962 assign_parm_find_data_types and expand_expr_real_1. */
2964 equiv_stack_parm = data->stack_parm;
2965 validated_mem = validize_mem (data->entry_parm);
2967 need_conversion = (data->nominal_mode != data->passed_mode
2968 || promoted_nominal_mode != data->promoted_mode);
2969 moved = false;
2971 if (need_conversion
2972 && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
2973 && data->nominal_mode == data->passed_mode
2974 && data->nominal_mode == GET_MODE (data->entry_parm))
2976 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
2977 mode, by the caller. We now have to convert it to
2978 NOMINAL_MODE, if different. However, PARMREG may be in
2979 a different mode than NOMINAL_MODE if it is being stored
2980 promoted.
2982 If ENTRY_PARM is a hard register, it might be in a register
2983 not valid for operating in its mode (e.g., an odd-numbered
2984 register for a DFmode). In that case, moves are the only
2985 thing valid, so we can't do a convert from there. This
2986 occurs when the calling sequence allow such misaligned
2987 usages.
2989 In addition, the conversion may involve a call, which could
2990 clobber parameters which haven't been copied to pseudo
2991 registers yet.
2993 First, we try to emit an insn which performs the necessary
2994 conversion. We verify that this insn does not clobber any
2995 hard registers. */
2997 enum insn_code icode;
2998 rtx op0, op1;
3000 icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
3001 unsignedp);
3003 op0 = parmreg;
3004 op1 = validated_mem;
3005 if (icode != CODE_FOR_nothing
3006 && insn_operand_matches (icode, 0, op0)
3007 && insn_operand_matches (icode, 1, op1))
3009 enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
3010 rtx insn, insns, t = op1;
3011 HARD_REG_SET hardregs;
3013 start_sequence ();
3014 /* If op1 is a hard register that is likely spilled, first
3015 force it into a pseudo, otherwise combiner might extend
3016 its lifetime too much. */
3017 if (GET_CODE (t) == SUBREG)
3018 t = SUBREG_REG (t);
3019 if (REG_P (t)
3020 && HARD_REGISTER_P (t)
3021 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (t))
3022 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (t))))
3024 t = gen_reg_rtx (GET_MODE (op1));
3025 emit_move_insn (t, op1);
3027 else
3028 t = op1;
3029 insn = gen_extend_insn (op0, t, promoted_nominal_mode,
3030 data->passed_mode, unsignedp);
3031 emit_insn (insn);
3032 insns = get_insns ();
3034 moved = true;
3035 CLEAR_HARD_REG_SET (hardregs);
3036 for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3038 if (INSN_P (insn))
3039 note_stores (PATTERN (insn), record_hard_reg_sets,
3040 &hardregs);
3041 if (!hard_reg_set_empty_p (hardregs))
3042 moved = false;
3045 end_sequence ();
3047 if (moved)
3049 emit_insn (insns);
3050 if (equiv_stack_parm != NULL_RTX)
3051 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3052 equiv_stack_parm);
3057 if (moved)
3058 /* Nothing to do. */
3060 else if (need_conversion)
3062 /* We did not have an insn to convert directly, or the sequence
3063 generated appeared unsafe. We must first copy the parm to a
3064 pseudo reg, and save the conversion until after all
3065 parameters have been moved. */
3067 int save_tree_used;
3068 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3070 emit_move_insn (tempreg, validated_mem);
3072 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3073 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3075 if (GET_CODE (tempreg) == SUBREG
3076 && GET_MODE (tempreg) == data->nominal_mode
3077 && REG_P (SUBREG_REG (tempreg))
3078 && data->nominal_mode == data->passed_mode
3079 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm)
3080 && GET_MODE_SIZE (GET_MODE (tempreg))
3081 < GET_MODE_SIZE (GET_MODE (data->entry_parm)))
3083 /* The argument is already sign/zero extended, so note it
3084 into the subreg. */
3085 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3086 SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
3089 /* TREE_USED gets set erroneously during expand_assignment. */
3090 save_tree_used = TREE_USED (parm);
3091 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3092 TREE_USED (parm) = save_tree_used;
3093 all->first_conversion_insn = get_insns ();
3094 all->last_conversion_insn = get_last_insn ();
3095 end_sequence ();
3097 did_conversion = true;
3099 else
3100 emit_move_insn (parmreg, validated_mem);
3102 /* If we were passed a pointer but the actual value can safely live
3103 in a register, retrieve it and use it directly. */
3104 if (data->passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode)
3106 /* We can't use nominal_mode, because it will have been set to
3107 Pmode above. We must use the actual mode of the parm. */
3108 if (use_register_for_decl (parm))
3110 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3111 mark_user_reg (parmreg);
3113 else
3115 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3116 TYPE_MODE (TREE_TYPE (parm)),
3117 TYPE_ALIGN (TREE_TYPE (parm)));
3118 parmreg
3119 = assign_stack_local (TYPE_MODE (TREE_TYPE (parm)),
3120 GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parm))),
3121 align);
3122 set_mem_attributes (parmreg, parm, 1);
3125 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
3127 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
3128 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3130 push_to_sequence2 (all->first_conversion_insn,
3131 all->last_conversion_insn);
3132 emit_move_insn (tempreg, DECL_RTL (parm));
3133 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3134 emit_move_insn (parmreg, tempreg);
3135 all->first_conversion_insn = get_insns ();
3136 all->last_conversion_insn = get_last_insn ();
3137 end_sequence ();
3139 did_conversion = true;
3141 else
3142 emit_move_insn (parmreg, DECL_RTL (parm));
3144 SET_DECL_RTL (parm, parmreg);
3146 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3147 now the parm. */
3148 data->stack_parm = NULL;
3151 /* Mark the register as eliminable if we did no conversion and it was
3152 copied from memory at a fixed offset, and the arg pointer was not
3153 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3154 offset formed an invalid address, such memory-equivalences as we
3155 make here would screw up life analysis for it. */
3156 if (data->nominal_mode == data->passed_mode
3157 && !did_conversion
3158 && data->stack_parm != 0
3159 && MEM_P (data->stack_parm)
3160 && data->locate.offset.var == 0
3161 && reg_mentioned_p (virtual_incoming_args_rtx,
3162 XEXP (data->stack_parm, 0)))
3164 rtx linsn = get_last_insn ();
3165 rtx sinsn, set;
3167 /* Mark complex types separately. */
3168 if (GET_CODE (parmreg) == CONCAT)
3170 enum machine_mode submode
3171 = GET_MODE_INNER (GET_MODE (parmreg));
3172 int regnor = REGNO (XEXP (parmreg, 0));
3173 int regnoi = REGNO (XEXP (parmreg, 1));
3174 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3175 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3176 GET_MODE_SIZE (submode));
3178 /* Scan backwards for the set of the real and
3179 imaginary parts. */
3180 for (sinsn = linsn; sinsn != 0;
3181 sinsn = prev_nonnote_insn (sinsn))
3183 set = single_set (sinsn);
3184 if (set == 0)
3185 continue;
3187 if (SET_DEST (set) == regno_reg_rtx [regnoi])
3188 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3189 else if (SET_DEST (set) == regno_reg_rtx [regnor])
3190 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3193 else
3194 set_dst_reg_note (linsn, REG_EQUIV, equiv_stack_parm, parmreg);
3197 /* For pointer data type, suggest pointer register. */
3198 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3199 mark_reg_pointer (parmreg,
3200 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3203 /* A subroutine of assign_parms. Allocate stack space to hold the current
3204 parameter. Get it there. Perform all ABI specified conversions. */
3206 static void
3207 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3208 struct assign_parm_data_one *data)
3210 /* Value must be stored in the stack slot STACK_PARM during function
3211 execution. */
3212 bool to_conversion = false;
3214 assign_parm_remove_parallels (data);
3216 if (data->promoted_mode != data->nominal_mode)
3218 /* Conversion is required. */
3219 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3221 emit_move_insn (tempreg, validize_mem (data->entry_parm));
3223 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3224 to_conversion = true;
3226 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3227 TYPE_UNSIGNED (TREE_TYPE (parm)));
3229 if (data->stack_parm)
3231 int offset = subreg_lowpart_offset (data->nominal_mode,
3232 GET_MODE (data->stack_parm));
3233 /* ??? This may need a big-endian conversion on sparc64. */
3234 data->stack_parm
3235 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3236 if (offset && MEM_OFFSET_KNOWN_P (data->stack_parm))
3237 set_mem_offset (data->stack_parm,
3238 MEM_OFFSET (data->stack_parm) + offset);
3242 if (data->entry_parm != data->stack_parm)
3244 rtx src, dest;
3246 if (data->stack_parm == 0)
3248 int align = STACK_SLOT_ALIGNMENT (data->passed_type,
3249 GET_MODE (data->entry_parm),
3250 TYPE_ALIGN (data->passed_type));
3251 data->stack_parm
3252 = assign_stack_local (GET_MODE (data->entry_parm),
3253 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3254 align);
3255 set_mem_attributes (data->stack_parm, parm, 1);
3258 dest = validize_mem (data->stack_parm);
3259 src = validize_mem (data->entry_parm);
3261 if (MEM_P (src))
3263 /* Use a block move to handle potentially misaligned entry_parm. */
3264 if (!to_conversion)
3265 push_to_sequence2 (all->first_conversion_insn,
3266 all->last_conversion_insn);
3267 to_conversion = true;
3269 emit_block_move (dest, src,
3270 GEN_INT (int_size_in_bytes (data->passed_type)),
3271 BLOCK_OP_NORMAL);
3273 else
3274 emit_move_insn (dest, src);
3277 if (to_conversion)
3279 all->first_conversion_insn = get_insns ();
3280 all->last_conversion_insn = get_last_insn ();
3281 end_sequence ();
3284 SET_DECL_RTL (parm, data->stack_parm);
3287 /* A subroutine of assign_parms. If the ABI splits complex arguments, then
3288 undo the frobbing that we did in assign_parms_augmented_arg_list. */
3290 static void
3291 assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3292 vec<tree> fnargs)
3294 tree parm;
3295 tree orig_fnargs = all->orig_fnargs;
3296 unsigned i = 0;
3298 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3300 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3301 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3303 rtx tmp, real, imag;
3304 enum machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3306 real = DECL_RTL (fnargs[i]);
3307 imag = DECL_RTL (fnargs[i + 1]);
3308 if (inner != GET_MODE (real))
3310 real = gen_lowpart_SUBREG (inner, real);
3311 imag = gen_lowpart_SUBREG (inner, imag);
3314 if (TREE_ADDRESSABLE (parm))
3316 rtx rmem, imem;
3317 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3318 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3319 DECL_MODE (parm),
3320 TYPE_ALIGN (TREE_TYPE (parm)));
3322 /* split_complex_arg put the real and imag parts in
3323 pseudos. Move them to memory. */
3324 tmp = assign_stack_local (DECL_MODE (parm), size, align);
3325 set_mem_attributes (tmp, parm, 1);
3326 rmem = adjust_address_nv (tmp, inner, 0);
3327 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3328 push_to_sequence2 (all->first_conversion_insn,
3329 all->last_conversion_insn);
3330 emit_move_insn (rmem, real);
3331 emit_move_insn (imem, imag);
3332 all->first_conversion_insn = get_insns ();
3333 all->last_conversion_insn = get_last_insn ();
3334 end_sequence ();
3336 else
3337 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3338 SET_DECL_RTL (parm, tmp);
3340 real = DECL_INCOMING_RTL (fnargs[i]);
3341 imag = DECL_INCOMING_RTL (fnargs[i + 1]);
3342 if (inner != GET_MODE (real))
3344 real = gen_lowpart_SUBREG (inner, real);
3345 imag = gen_lowpart_SUBREG (inner, imag);
3347 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3348 set_decl_incoming_rtl (parm, tmp, false);
3349 i++;
3354 /* Assign RTL expressions to the function's parameters. This may involve
3355 copying them into registers and using those registers as the DECL_RTL. */
3357 static void
3358 assign_parms (tree fndecl)
3360 struct assign_parm_data_all all;
3361 tree parm;
3362 vec<tree> fnargs;
3363 unsigned i;
3365 crtl->args.internal_arg_pointer
3366 = targetm.calls.internal_arg_pointer ();
3368 assign_parms_initialize_all (&all);
3369 fnargs = assign_parms_augmented_arg_list (&all);
3371 FOR_EACH_VEC_ELT (fnargs, i, parm)
3373 struct assign_parm_data_one data;
3375 /* Extract the type of PARM; adjust it according to ABI. */
3376 assign_parm_find_data_types (&all, parm, &data);
3378 /* Early out for errors and void parameters. */
3379 if (data.passed_mode == VOIDmode)
3381 SET_DECL_RTL (parm, const0_rtx);
3382 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3383 continue;
3386 /* Estimate stack alignment from parameter alignment. */
3387 if (SUPPORTS_STACK_ALIGNMENT)
3389 unsigned int align
3390 = targetm.calls.function_arg_boundary (data.promoted_mode,
3391 data.passed_type);
3392 align = MINIMUM_ALIGNMENT (data.passed_type, data.promoted_mode,
3393 align);
3394 if (TYPE_ALIGN (data.nominal_type) > align)
3395 align = MINIMUM_ALIGNMENT (data.nominal_type,
3396 TYPE_MODE (data.nominal_type),
3397 TYPE_ALIGN (data.nominal_type));
3398 if (crtl->stack_alignment_estimated < align)
3400 gcc_assert (!crtl->stack_realign_processed);
3401 crtl->stack_alignment_estimated = align;
3405 if (cfun->stdarg && !DECL_CHAIN (parm))
3406 assign_parms_setup_varargs (&all, &data, false);
3408 /* Find out where the parameter arrives in this function. */
3409 assign_parm_find_entry_rtl (&all, &data);
3411 /* Find out where stack space for this parameter might be. */
3412 if (assign_parm_is_stack_parm (&all, &data))
3414 assign_parm_find_stack_rtl (parm, &data);
3415 assign_parm_adjust_entry_rtl (&data);
3418 /* Record permanently how this parm was passed. */
3419 if (data.passed_pointer)
3421 rtx incoming_rtl
3422 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.passed_type)),
3423 data.entry_parm);
3424 set_decl_incoming_rtl (parm, incoming_rtl, true);
3426 else
3427 set_decl_incoming_rtl (parm, data.entry_parm, false);
3429 /* Update info on where next arg arrives in registers. */
3430 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3431 data.passed_type, data.named_arg);
3433 assign_parm_adjust_stack_rtl (&data);
3435 if (assign_parm_setup_block_p (&data))
3436 assign_parm_setup_block (&all, parm, &data);
3437 else if (data.passed_pointer || use_register_for_decl (parm))
3438 assign_parm_setup_reg (&all, parm, &data);
3439 else
3440 assign_parm_setup_stack (&all, parm, &data);
3443 if (targetm.calls.split_complex_arg)
3444 assign_parms_unsplit_complex (&all, fnargs);
3446 fnargs.release ();
3448 /* Output all parameter conversion instructions (possibly including calls)
3449 now that all parameters have been copied out of hard registers. */
3450 emit_insn (all.first_conversion_insn);
3452 /* Estimate reload stack alignment from scalar return mode. */
3453 if (SUPPORTS_STACK_ALIGNMENT)
3455 if (DECL_RESULT (fndecl))
3457 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3458 enum machine_mode mode = TYPE_MODE (type);
3460 if (mode != BLKmode
3461 && mode != VOIDmode
3462 && !AGGREGATE_TYPE_P (type))
3464 unsigned int align = GET_MODE_ALIGNMENT (mode);
3465 if (crtl->stack_alignment_estimated < align)
3467 gcc_assert (!crtl->stack_realign_processed);
3468 crtl->stack_alignment_estimated = align;
3474 /* If we are receiving a struct value address as the first argument, set up
3475 the RTL for the function result. As this might require code to convert
3476 the transmitted address to Pmode, we do this here to ensure that possible
3477 preliminary conversions of the address have been emitted already. */
3478 if (all.function_result_decl)
3480 tree result = DECL_RESULT (current_function_decl);
3481 rtx addr = DECL_RTL (all.function_result_decl);
3482 rtx x;
3484 if (DECL_BY_REFERENCE (result))
3486 SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3487 x = addr;
3489 else
3491 SET_DECL_VALUE_EXPR (result,
3492 build1 (INDIRECT_REF, TREE_TYPE (result),
3493 all.function_result_decl));
3494 addr = convert_memory_address (Pmode, addr);
3495 x = gen_rtx_MEM (DECL_MODE (result), addr);
3496 set_mem_attributes (x, result, 1);
3499 DECL_HAS_VALUE_EXPR_P (result) = 1;
3501 SET_DECL_RTL (result, x);
3504 /* We have aligned all the args, so add space for the pretend args. */
3505 crtl->args.pretend_args_size = all.pretend_args_size;
3506 all.stack_args_size.constant += all.extra_pretend_bytes;
3507 crtl->args.size = all.stack_args_size.constant;
3509 /* Adjust function incoming argument size for alignment and
3510 minimum length. */
3512 crtl->args.size = MAX (crtl->args.size, all.reg_parm_stack_space);
3513 crtl->args.size = CEIL_ROUND (crtl->args.size,
3514 PARM_BOUNDARY / BITS_PER_UNIT);
3516 #ifdef ARGS_GROW_DOWNWARD
3517 crtl->args.arg_offset_rtx
3518 = (all.stack_args_size.var == 0 ? GEN_INT (-all.stack_args_size.constant)
3519 : expand_expr (size_diffop (all.stack_args_size.var,
3520 size_int (-all.stack_args_size.constant)),
3521 NULL_RTX, VOIDmode, EXPAND_NORMAL));
3522 #else
3523 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3524 #endif
3526 /* See how many bytes, if any, of its args a function should try to pop
3527 on return. */
3529 crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3530 TREE_TYPE (fndecl),
3531 crtl->args.size);
3533 /* For stdarg.h function, save info about
3534 regs and stack space used by the named args. */
3536 crtl->args.info = all.args_so_far_v;
3538 /* Set the rtx used for the function return value. Put this in its
3539 own variable so any optimizers that need this information don't have
3540 to include tree.h. Do this here so it gets done when an inlined
3541 function gets output. */
3543 crtl->return_rtx
3544 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3545 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3547 /* If scalar return value was computed in a pseudo-reg, or was a named
3548 return value that got dumped to the stack, copy that to the hard
3549 return register. */
3550 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3552 tree decl_result = DECL_RESULT (fndecl);
3553 rtx decl_rtl = DECL_RTL (decl_result);
3555 if (REG_P (decl_rtl)
3556 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3557 : DECL_REGISTER (decl_result))
3559 rtx real_decl_rtl;
3561 real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3562 fndecl, true);
3563 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3564 /* The delay slot scheduler assumes that crtl->return_rtx
3565 holds the hard register containing the return value, not a
3566 temporary pseudo. */
3567 crtl->return_rtx = real_decl_rtl;
3572 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3573 For all seen types, gimplify their sizes. */
3575 static tree
3576 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3578 tree t = *tp;
3580 *walk_subtrees = 0;
3581 if (TYPE_P (t))
3583 if (POINTER_TYPE_P (t))
3584 *walk_subtrees = 1;
3585 else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3586 && !TYPE_SIZES_GIMPLIFIED (t))
3588 gimplify_type_sizes (t, (gimple_seq *) data);
3589 *walk_subtrees = 1;
3593 return NULL;
3596 /* Gimplify the parameter list for current_function_decl. This involves
3597 evaluating SAVE_EXPRs of variable sized parameters and generating code
3598 to implement callee-copies reference parameters. Returns a sequence of
3599 statements to add to the beginning of the function. */
3601 gimple_seq
3602 gimplify_parameters (void)
3604 struct assign_parm_data_all all;
3605 tree parm;
3606 gimple_seq stmts = NULL;
3607 vec<tree> fnargs;
3608 unsigned i;
3610 assign_parms_initialize_all (&all);
3611 fnargs = assign_parms_augmented_arg_list (&all);
3613 FOR_EACH_VEC_ELT (fnargs, i, parm)
3615 struct assign_parm_data_one data;
3617 /* Extract the type of PARM; adjust it according to ABI. */
3618 assign_parm_find_data_types (&all, parm, &data);
3620 /* Early out for errors and void parameters. */
3621 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3622 continue;
3624 /* Update info on where next arg arrives in registers. */
3625 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3626 data.passed_type, data.named_arg);
3628 /* ??? Once upon a time variable_size stuffed parameter list
3629 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3630 turned out to be less than manageable in the gimple world.
3631 Now we have to hunt them down ourselves. */
3632 walk_tree_without_duplicates (&data.passed_type,
3633 gimplify_parm_type, &stmts);
3635 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3637 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3638 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3641 if (data.passed_pointer)
3643 tree type = TREE_TYPE (data.passed_type);
3644 if (reference_callee_copied (&all.args_so_far_v, TYPE_MODE (type),
3645 type, data.named_arg))
3647 tree local, t;
3649 /* For constant-sized objects, this is trivial; for
3650 variable-sized objects, we have to play games. */
3651 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3652 && !(flag_stack_check == GENERIC_STACK_CHECK
3653 && compare_tree_int (DECL_SIZE_UNIT (parm),
3654 STACK_CHECK_MAX_VAR_SIZE) > 0))
3656 local = create_tmp_var (type, get_name (parm));
3657 DECL_IGNORED_P (local) = 0;
3658 /* If PARM was addressable, move that flag over
3659 to the local copy, as its address will be taken,
3660 not the PARMs. Keep the parms address taken
3661 as we'll query that flag during gimplification. */
3662 if (TREE_ADDRESSABLE (parm))
3663 TREE_ADDRESSABLE (local) = 1;
3664 else if (TREE_CODE (type) == COMPLEX_TYPE
3665 || TREE_CODE (type) == VECTOR_TYPE)
3666 DECL_GIMPLE_REG_P (local) = 1;
3668 else
3670 tree ptr_type, addr;
3672 ptr_type = build_pointer_type (type);
3673 addr = create_tmp_reg (ptr_type, get_name (parm));
3674 DECL_IGNORED_P (addr) = 0;
3675 local = build_fold_indirect_ref (addr);
3677 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
3678 t = build_call_expr (t, 2, DECL_SIZE_UNIT (parm),
3679 size_int (DECL_ALIGN (parm)));
3681 /* The call has been built for a variable-sized object. */
3682 CALL_ALLOCA_FOR_VAR_P (t) = 1;
3683 t = fold_convert (ptr_type, t);
3684 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
3685 gimplify_and_add (t, &stmts);
3688 gimplify_assign (local, parm, &stmts);
3690 SET_DECL_VALUE_EXPR (parm, local);
3691 DECL_HAS_VALUE_EXPR_P (parm) = 1;
3696 fnargs.release ();
3698 return stmts;
3701 /* Compute the size and offset from the start of the stacked arguments for a
3702 parm passed in mode PASSED_MODE and with type TYPE.
3704 INITIAL_OFFSET_PTR points to the current offset into the stacked
3705 arguments.
3707 The starting offset and size for this parm are returned in
3708 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
3709 nonzero, the offset is that of stack slot, which is returned in
3710 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
3711 padding required from the initial offset ptr to the stack slot.
3713 IN_REGS is nonzero if the argument will be passed in registers. It will
3714 never be set if REG_PARM_STACK_SPACE is not defined.
3716 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
3717 for arguments which are passed in registers.
3719 FNDECL is the function in which the argument was defined.
3721 There are two types of rounding that are done. The first, controlled by
3722 TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
3723 argument list to be aligned to the specific boundary (in bits). This
3724 rounding affects the initial and starting offsets, but not the argument
3725 size.
3727 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
3728 optionally rounds the size of the parm to PARM_BOUNDARY. The
3729 initial offset is not affected by this rounding, while the size always
3730 is and the starting offset may be. */
3732 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
3733 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
3734 callers pass in the total size of args so far as
3735 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
3737 void
3738 locate_and_pad_parm (enum machine_mode passed_mode, tree type, int in_regs,
3739 int reg_parm_stack_space, int partial,
3740 tree fndecl ATTRIBUTE_UNUSED,
3741 struct args_size *initial_offset_ptr,
3742 struct locate_and_pad_arg_data *locate)
3744 tree sizetree;
3745 enum direction where_pad;
3746 unsigned int boundary, round_boundary;
3747 int part_size_in_regs;
3749 /* If we have found a stack parm before we reach the end of the
3750 area reserved for registers, skip that area. */
3751 if (! in_regs)
3753 if (reg_parm_stack_space > 0)
3755 if (initial_offset_ptr->var)
3757 initial_offset_ptr->var
3758 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
3759 ssize_int (reg_parm_stack_space));
3760 initial_offset_ptr->constant = 0;
3762 else if (initial_offset_ptr->constant < reg_parm_stack_space)
3763 initial_offset_ptr->constant = reg_parm_stack_space;
3767 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
3769 sizetree
3770 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
3771 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
3772 boundary = targetm.calls.function_arg_boundary (passed_mode, type);
3773 round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
3774 type);
3775 locate->where_pad = where_pad;
3777 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
3778 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
3779 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
3781 locate->boundary = boundary;
3783 if (SUPPORTS_STACK_ALIGNMENT)
3785 /* stack_alignment_estimated can't change after stack has been
3786 realigned. */
3787 if (crtl->stack_alignment_estimated < boundary)
3789 if (!crtl->stack_realign_processed)
3790 crtl->stack_alignment_estimated = boundary;
3791 else
3793 /* If stack is realigned and stack alignment value
3794 hasn't been finalized, it is OK not to increase
3795 stack_alignment_estimated. The bigger alignment
3796 requirement is recorded in stack_alignment_needed
3797 below. */
3798 gcc_assert (!crtl->stack_realign_finalized
3799 && crtl->stack_realign_needed);
3804 /* Remember if the outgoing parameter requires extra alignment on the
3805 calling function side. */
3806 if (crtl->stack_alignment_needed < boundary)
3807 crtl->stack_alignment_needed = boundary;
3808 if (crtl->preferred_stack_boundary < boundary)
3809 crtl->preferred_stack_boundary = boundary;
3811 #ifdef ARGS_GROW_DOWNWARD
3812 locate->slot_offset.constant = -initial_offset_ptr->constant;
3813 if (initial_offset_ptr->var)
3814 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
3815 initial_offset_ptr->var);
3818 tree s2 = sizetree;
3819 if (where_pad != none
3820 && (!tree_fits_uhwi_p (sizetree)
3821 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
3822 s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
3823 SUB_PARM_SIZE (locate->slot_offset, s2);
3826 locate->slot_offset.constant += part_size_in_regs;
3828 if (!in_regs || reg_parm_stack_space > 0)
3829 pad_to_arg_alignment (&locate->slot_offset, boundary,
3830 &locate->alignment_pad);
3832 locate->size.constant = (-initial_offset_ptr->constant
3833 - locate->slot_offset.constant);
3834 if (initial_offset_ptr->var)
3835 locate->size.var = size_binop (MINUS_EXPR,
3836 size_binop (MINUS_EXPR,
3837 ssize_int (0),
3838 initial_offset_ptr->var),
3839 locate->slot_offset.var);
3841 /* Pad_below needs the pre-rounded size to know how much to pad
3842 below. */
3843 locate->offset = locate->slot_offset;
3844 if (where_pad == downward)
3845 pad_below (&locate->offset, passed_mode, sizetree);
3847 #else /* !ARGS_GROW_DOWNWARD */
3848 if (!in_regs || reg_parm_stack_space > 0)
3849 pad_to_arg_alignment (initial_offset_ptr, boundary,
3850 &locate->alignment_pad);
3851 locate->slot_offset = *initial_offset_ptr;
3853 #ifdef PUSH_ROUNDING
3854 if (passed_mode != BLKmode)
3855 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
3856 #endif
3858 /* Pad_below needs the pre-rounded size to know how much to pad below
3859 so this must be done before rounding up. */
3860 locate->offset = locate->slot_offset;
3861 if (where_pad == downward)
3862 pad_below (&locate->offset, passed_mode, sizetree);
3864 if (where_pad != none
3865 && (!tree_fits_uhwi_p (sizetree)
3866 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
3867 sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
3869 ADD_PARM_SIZE (locate->size, sizetree);
3871 locate->size.constant -= part_size_in_regs;
3872 #endif /* ARGS_GROW_DOWNWARD */
3874 #ifdef FUNCTION_ARG_OFFSET
3875 locate->offset.constant += FUNCTION_ARG_OFFSET (passed_mode, type);
3876 #endif
3879 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
3880 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
3882 static void
3883 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
3884 struct args_size *alignment_pad)
3886 tree save_var = NULL_TREE;
3887 HOST_WIDE_INT save_constant = 0;
3888 int boundary_in_bytes = boundary / BITS_PER_UNIT;
3889 HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
3891 #ifdef SPARC_STACK_BOUNDARY_HACK
3892 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
3893 the real alignment of %sp. However, when it does this, the
3894 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
3895 if (SPARC_STACK_BOUNDARY_HACK)
3896 sp_offset = 0;
3897 #endif
3899 if (boundary > PARM_BOUNDARY)
3901 save_var = offset_ptr->var;
3902 save_constant = offset_ptr->constant;
3905 alignment_pad->var = NULL_TREE;
3906 alignment_pad->constant = 0;
3908 if (boundary > BITS_PER_UNIT)
3910 if (offset_ptr->var)
3912 tree sp_offset_tree = ssize_int (sp_offset);
3913 tree offset = size_binop (PLUS_EXPR,
3914 ARGS_SIZE_TREE (*offset_ptr),
3915 sp_offset_tree);
3916 #ifdef ARGS_GROW_DOWNWARD
3917 tree rounded = round_down (offset, boundary / BITS_PER_UNIT);
3918 #else
3919 tree rounded = round_up (offset, boundary / BITS_PER_UNIT);
3920 #endif
3922 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
3923 /* ARGS_SIZE_TREE includes constant term. */
3924 offset_ptr->constant = 0;
3925 if (boundary > PARM_BOUNDARY)
3926 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
3927 save_var);
3929 else
3931 offset_ptr->constant = -sp_offset +
3932 #ifdef ARGS_GROW_DOWNWARD
3933 FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3934 #else
3935 CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
3936 #endif
3937 if (boundary > PARM_BOUNDARY)
3938 alignment_pad->constant = offset_ptr->constant - save_constant;
3943 static void
3944 pad_below (struct args_size *offset_ptr, enum machine_mode passed_mode, tree sizetree)
3946 if (passed_mode != BLKmode)
3948 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
3949 offset_ptr->constant
3950 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
3951 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
3952 - GET_MODE_SIZE (passed_mode));
3954 else
3956 if (TREE_CODE (sizetree) != INTEGER_CST
3957 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
3959 /* Round the size up to multiple of PARM_BOUNDARY bits. */
3960 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
3961 /* Add it in. */
3962 ADD_PARM_SIZE (*offset_ptr, s2);
3963 SUB_PARM_SIZE (*offset_ptr, sizetree);
3969 /* True if register REGNO was alive at a place where `setjmp' was
3970 called and was set more than once or is an argument. Such regs may
3971 be clobbered by `longjmp'. */
3973 static bool
3974 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
3976 /* There appear to be cases where some local vars never reach the
3977 backend but have bogus regnos. */
3978 if (regno >= max_reg_num ())
3979 return false;
3981 return ((REG_N_SETS (regno) > 1
3982 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3983 regno))
3984 && REGNO_REG_SET_P (setjmp_crosses, regno));
3987 /* Walk the tree of blocks describing the binding levels within a
3988 function and warn about variables the might be killed by setjmp or
3989 vfork. This is done after calling flow_analysis before register
3990 allocation since that will clobber the pseudo-regs to hard
3991 regs. */
3993 static void
3994 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
3996 tree decl, sub;
3998 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
4000 if (TREE_CODE (decl) == VAR_DECL
4001 && DECL_RTL_SET_P (decl)
4002 && REG_P (DECL_RTL (decl))
4003 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4004 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
4005 " %<longjmp%> or %<vfork%>", decl);
4008 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
4009 setjmp_vars_warning (setjmp_crosses, sub);
4012 /* Do the appropriate part of setjmp_vars_warning
4013 but for arguments instead of local variables. */
4015 static void
4016 setjmp_args_warning (bitmap setjmp_crosses)
4018 tree decl;
4019 for (decl = DECL_ARGUMENTS (current_function_decl);
4020 decl; decl = DECL_CHAIN (decl))
4021 if (DECL_RTL (decl) != 0
4022 && REG_P (DECL_RTL (decl))
4023 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4024 warning (OPT_Wclobbered,
4025 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
4026 decl);
4029 /* Generate warning messages for variables live across setjmp. */
4031 void
4032 generate_setjmp_warnings (void)
4034 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4036 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS
4037 || bitmap_empty_p (setjmp_crosses))
4038 return;
4040 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4041 setjmp_args_warning (setjmp_crosses);
4045 /* Reverse the order of elements in the fragment chain T of blocks,
4046 and return the new head of the chain (old last element).
4047 In addition to that clear BLOCK_SAME_RANGE flags when needed
4048 and adjust BLOCK_SUPERCONTEXT from the super fragment to
4049 its super fragment origin. */
4051 static tree
4052 block_fragments_nreverse (tree t)
4054 tree prev = 0, block, next, prev_super = 0;
4055 tree super = BLOCK_SUPERCONTEXT (t);
4056 if (BLOCK_FRAGMENT_ORIGIN (super))
4057 super = BLOCK_FRAGMENT_ORIGIN (super);
4058 for (block = t; block; block = next)
4060 next = BLOCK_FRAGMENT_CHAIN (block);
4061 BLOCK_FRAGMENT_CHAIN (block) = prev;
4062 if ((prev && !BLOCK_SAME_RANGE (prev))
4063 || (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (block))
4064 != prev_super))
4065 BLOCK_SAME_RANGE (block) = 0;
4066 prev_super = BLOCK_SUPERCONTEXT (block);
4067 BLOCK_SUPERCONTEXT (block) = super;
4068 prev = block;
4070 t = BLOCK_FRAGMENT_ORIGIN (t);
4071 if (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (t))
4072 != prev_super)
4073 BLOCK_SAME_RANGE (t) = 0;
4074 BLOCK_SUPERCONTEXT (t) = super;
4075 return prev;
4078 /* Reverse the order of elements in the chain T of blocks,
4079 and return the new head of the chain (old last element).
4080 Also do the same on subblocks and reverse the order of elements
4081 in BLOCK_FRAGMENT_CHAIN as well. */
4083 static tree
4084 blocks_nreverse_all (tree t)
4086 tree prev = 0, block, next;
4087 for (block = t; block; block = next)
4089 next = BLOCK_CHAIN (block);
4090 BLOCK_CHAIN (block) = prev;
4091 if (BLOCK_FRAGMENT_CHAIN (block)
4092 && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4094 BLOCK_FRAGMENT_CHAIN (block)
4095 = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4096 if (!BLOCK_SAME_RANGE (BLOCK_FRAGMENT_CHAIN (block)))
4097 BLOCK_SAME_RANGE (block) = 0;
4099 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4100 prev = block;
4102 return prev;
4106 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4107 and create duplicate blocks. */
4108 /* ??? Need an option to either create block fragments or to create
4109 abstract origin duplicates of a source block. It really depends
4110 on what optimization has been performed. */
4112 void
4113 reorder_blocks (void)
4115 tree block = DECL_INITIAL (current_function_decl);
4117 if (block == NULL_TREE)
4118 return;
4120 auto_vec<tree, 10> block_stack;
4122 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4123 clear_block_marks (block);
4125 /* Prune the old trees away, so that they don't get in the way. */
4126 BLOCK_SUBBLOCKS (block) = NULL_TREE;
4127 BLOCK_CHAIN (block) = NULL_TREE;
4129 /* Recreate the block tree from the note nesting. */
4130 reorder_blocks_1 (get_insns (), block, &block_stack);
4131 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4134 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
4136 void
4137 clear_block_marks (tree block)
4139 while (block)
4141 TREE_ASM_WRITTEN (block) = 0;
4142 clear_block_marks (BLOCK_SUBBLOCKS (block));
4143 block = BLOCK_CHAIN (block);
4147 static void
4148 reorder_blocks_1 (rtx insns, tree current_block, vec<tree> *p_block_stack)
4150 rtx insn;
4151 tree prev_beg = NULL_TREE, prev_end = NULL_TREE;
4153 for (insn = insns; insn; insn = NEXT_INSN (insn))
4155 if (NOTE_P (insn))
4157 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4159 tree block = NOTE_BLOCK (insn);
4160 tree origin;
4162 gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4163 origin = block;
4165 if (prev_end)
4166 BLOCK_SAME_RANGE (prev_end) = 0;
4167 prev_end = NULL_TREE;
4169 /* If we have seen this block before, that means it now
4170 spans multiple address regions. Create a new fragment. */
4171 if (TREE_ASM_WRITTEN (block))
4173 tree new_block = copy_node (block);
4175 BLOCK_SAME_RANGE (new_block) = 0;
4176 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4177 BLOCK_FRAGMENT_CHAIN (new_block)
4178 = BLOCK_FRAGMENT_CHAIN (origin);
4179 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4181 NOTE_BLOCK (insn) = new_block;
4182 block = new_block;
4185 if (prev_beg == current_block && prev_beg)
4186 BLOCK_SAME_RANGE (block) = 1;
4188 prev_beg = origin;
4190 BLOCK_SUBBLOCKS (block) = 0;
4191 TREE_ASM_WRITTEN (block) = 1;
4192 /* When there's only one block for the entire function,
4193 current_block == block and we mustn't do this, it
4194 will cause infinite recursion. */
4195 if (block != current_block)
4197 tree super;
4198 if (block != origin)
4199 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block
4200 || BLOCK_FRAGMENT_ORIGIN (BLOCK_SUPERCONTEXT
4201 (origin))
4202 == current_block);
4203 if (p_block_stack->is_empty ())
4204 super = current_block;
4205 else
4207 super = p_block_stack->last ();
4208 gcc_assert (super == current_block
4209 || BLOCK_FRAGMENT_ORIGIN (super)
4210 == current_block);
4212 BLOCK_SUPERCONTEXT (block) = super;
4213 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4214 BLOCK_SUBBLOCKS (current_block) = block;
4215 current_block = origin;
4217 p_block_stack->safe_push (block);
4219 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4221 NOTE_BLOCK (insn) = p_block_stack->pop ();
4222 current_block = BLOCK_SUPERCONTEXT (current_block);
4223 if (BLOCK_FRAGMENT_ORIGIN (current_block))
4224 current_block = BLOCK_FRAGMENT_ORIGIN (current_block);
4225 prev_beg = NULL_TREE;
4226 prev_end = BLOCK_SAME_RANGE (NOTE_BLOCK (insn))
4227 ? NOTE_BLOCK (insn) : NULL_TREE;
4230 else
4232 prev_beg = NULL_TREE;
4233 if (prev_end)
4234 BLOCK_SAME_RANGE (prev_end) = 0;
4235 prev_end = NULL_TREE;
4240 /* Reverse the order of elements in the chain T of blocks,
4241 and return the new head of the chain (old last element). */
4243 tree
4244 blocks_nreverse (tree t)
4246 tree prev = 0, block, next;
4247 for (block = t; block; block = next)
4249 next = BLOCK_CHAIN (block);
4250 BLOCK_CHAIN (block) = prev;
4251 prev = block;
4253 return prev;
4256 /* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4257 by modifying the last node in chain 1 to point to chain 2. */
4259 tree
4260 block_chainon (tree op1, tree op2)
4262 tree t1;
4264 if (!op1)
4265 return op2;
4266 if (!op2)
4267 return op1;
4269 for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4270 continue;
4271 BLOCK_CHAIN (t1) = op2;
4273 #ifdef ENABLE_TREE_CHECKING
4275 tree t2;
4276 for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4277 gcc_assert (t2 != t1);
4279 #endif
4281 return op1;
4284 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
4285 non-NULL, list them all into VECTOR, in a depth-first preorder
4286 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
4287 blocks. */
4289 static int
4290 all_blocks (tree block, tree *vector)
4292 int n_blocks = 0;
4294 while (block)
4296 TREE_ASM_WRITTEN (block) = 0;
4298 /* Record this block. */
4299 if (vector)
4300 vector[n_blocks] = block;
4302 ++n_blocks;
4304 /* Record the subblocks, and their subblocks... */
4305 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4306 vector ? vector + n_blocks : 0);
4307 block = BLOCK_CHAIN (block);
4310 return n_blocks;
4313 /* Return a vector containing all the blocks rooted at BLOCK. The
4314 number of elements in the vector is stored in N_BLOCKS_P. The
4315 vector is dynamically allocated; it is the caller's responsibility
4316 to call `free' on the pointer returned. */
4318 static tree *
4319 get_block_vector (tree block, int *n_blocks_p)
4321 tree *block_vector;
4323 *n_blocks_p = all_blocks (block, NULL);
4324 block_vector = XNEWVEC (tree, *n_blocks_p);
4325 all_blocks (block, block_vector);
4327 return block_vector;
4330 static GTY(()) int next_block_index = 2;
4332 /* Set BLOCK_NUMBER for all the blocks in FN. */
4334 void
4335 number_blocks (tree fn)
4337 int i;
4338 int n_blocks;
4339 tree *block_vector;
4341 /* For SDB and XCOFF debugging output, we start numbering the blocks
4342 from 1 within each function, rather than keeping a running
4343 count. */
4344 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
4345 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
4346 next_block_index = 1;
4347 #endif
4349 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4351 /* The top-level BLOCK isn't numbered at all. */
4352 for (i = 1; i < n_blocks; ++i)
4353 /* We number the blocks from two. */
4354 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4356 free (block_vector);
4358 return;
4361 /* If VAR is present in a subblock of BLOCK, return the subblock. */
4363 DEBUG_FUNCTION tree
4364 debug_find_var_in_block_tree (tree var, tree block)
4366 tree t;
4368 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4369 if (t == var)
4370 return block;
4372 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4374 tree ret = debug_find_var_in_block_tree (var, t);
4375 if (ret)
4376 return ret;
4379 return NULL_TREE;
4382 /* Keep track of whether we're in a dummy function context. If we are,
4383 we don't want to invoke the set_current_function hook, because we'll
4384 get into trouble if the hook calls target_reinit () recursively or
4385 when the initial initialization is not yet complete. */
4387 static bool in_dummy_function;
4389 /* Invoke the target hook when setting cfun. Update the optimization options
4390 if the function uses different options than the default. */
4392 static void
4393 invoke_set_current_function_hook (tree fndecl)
4395 if (!in_dummy_function)
4397 tree opts = ((fndecl)
4398 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4399 : optimization_default_node);
4401 if (!opts)
4402 opts = optimization_default_node;
4404 /* Change optimization options if needed. */
4405 if (optimization_current_node != opts)
4407 optimization_current_node = opts;
4408 cl_optimization_restore (&global_options, TREE_OPTIMIZATION (opts));
4411 targetm.set_current_function (fndecl);
4412 this_fn_optabs = this_target_optabs;
4414 if (opts != optimization_default_node)
4416 init_tree_optimization_optabs (opts);
4417 if (TREE_OPTIMIZATION_OPTABS (opts))
4418 this_fn_optabs = (struct target_optabs *)
4419 TREE_OPTIMIZATION_OPTABS (opts);
4424 /* cfun should never be set directly; use this function. */
4426 void
4427 set_cfun (struct function *new_cfun)
4429 if (cfun != new_cfun)
4431 cfun = new_cfun;
4432 invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4436 /* Initialized with NOGC, making this poisonous to the garbage collector. */
4438 static vec<function_p> cfun_stack;
4440 /* Push the current cfun onto the stack, and set cfun to new_cfun. Also set
4441 current_function_decl accordingly. */
4443 void
4444 push_cfun (struct function *new_cfun)
4446 gcc_assert ((!cfun && !current_function_decl)
4447 || (cfun && current_function_decl == cfun->decl));
4448 cfun_stack.safe_push (cfun);
4449 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4450 set_cfun (new_cfun);
4453 /* Pop cfun from the stack. Also set current_function_decl accordingly. */
4455 void
4456 pop_cfun (void)
4458 struct function *new_cfun = cfun_stack.pop ();
4459 /* When in_dummy_function, we do have a cfun but current_function_decl is
4460 NULL. We also allow pushing NULL cfun and subsequently changing
4461 current_function_decl to something else and have both restored by
4462 pop_cfun. */
4463 gcc_checking_assert (in_dummy_function
4464 || !cfun
4465 || current_function_decl == cfun->decl);
4466 set_cfun (new_cfun);
4467 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4470 /* Return value of funcdef and increase it. */
4472 get_next_funcdef_no (void)
4474 return funcdef_no++;
4477 /* Return value of funcdef. */
4479 get_last_funcdef_no (void)
4481 return funcdef_no;
4484 /* Allocate a function structure for FNDECL and set its contents
4485 to the defaults. Set cfun to the newly-allocated object.
4486 Some of the helper functions invoked during initialization assume
4487 that cfun has already been set. Therefore, assign the new object
4488 directly into cfun and invoke the back end hook explicitly at the
4489 very end, rather than initializing a temporary and calling set_cfun
4490 on it.
4492 ABSTRACT_P is true if this is a function that will never be seen by
4493 the middle-end. Such functions are front-end concepts (like C++
4494 function templates) that do not correspond directly to functions
4495 placed in object files. */
4497 void
4498 allocate_struct_function (tree fndecl, bool abstract_p)
4500 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4502 cfun = ggc_cleared_alloc<function> ();
4504 init_eh_for_function ();
4506 if (init_machine_status)
4507 cfun->machine = (*init_machine_status) ();
4509 #ifdef OVERRIDE_ABI_FORMAT
4510 OVERRIDE_ABI_FORMAT (fndecl);
4511 #endif
4513 if (fndecl != NULL_TREE)
4515 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4516 cfun->decl = fndecl;
4517 current_function_funcdef_no = get_next_funcdef_no ();
4520 invoke_set_current_function_hook (fndecl);
4522 if (fndecl != NULL_TREE)
4524 tree result = DECL_RESULT (fndecl);
4525 if (!abstract_p && aggregate_value_p (result, fndecl))
4527 #ifdef PCC_STATIC_STRUCT_RETURN
4528 cfun->returns_pcc_struct = 1;
4529 #endif
4530 cfun->returns_struct = 1;
4533 cfun->stdarg = stdarg_p (fntype);
4535 /* Assume all registers in stdarg functions need to be saved. */
4536 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4537 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4539 /* ??? This could be set on a per-function basis by the front-end
4540 but is this worth the hassle? */
4541 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4545 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4546 instead of just setting it. */
4548 void
4549 push_struct_function (tree fndecl)
4551 /* When in_dummy_function we might be in the middle of a pop_cfun and
4552 current_function_decl and cfun may not match. */
4553 gcc_assert (in_dummy_function
4554 || (!cfun && !current_function_decl)
4555 || (cfun && current_function_decl == cfun->decl));
4556 cfun_stack.safe_push (cfun);
4557 current_function_decl = fndecl;
4558 allocate_struct_function (fndecl, false);
4561 /* Reset crtl and other non-struct-function variables to defaults as
4562 appropriate for emitting rtl at the start of a function. */
4564 static void
4565 prepare_function_start (void)
4567 gcc_assert (!crtl->emit.x_last_insn);
4568 init_temp_slots ();
4569 init_emit ();
4570 init_varasm_status ();
4571 init_expr ();
4572 default_rtl_profile ();
4574 if (flag_stack_usage_info)
4576 cfun->su = ggc_cleared_alloc<stack_usage> ();
4577 cfun->su->static_stack_size = -1;
4580 cse_not_expected = ! optimize;
4582 /* Caller save not needed yet. */
4583 caller_save_needed = 0;
4585 /* We haven't done register allocation yet. */
4586 reg_renumber = 0;
4588 /* Indicate that we have not instantiated virtual registers yet. */
4589 virtuals_instantiated = 0;
4591 /* Indicate that we want CONCATs now. */
4592 generating_concat_p = 1;
4594 /* Indicate we have no need of a frame pointer yet. */
4595 frame_pointer_needed = 0;
4598 /* Initialize the rtl expansion mechanism so that we can do simple things
4599 like generate sequences. This is used to provide a context during global
4600 initialization of some passes. You must call expand_dummy_function_end
4601 to exit this context. */
4603 void
4604 init_dummy_function_start (void)
4606 gcc_assert (!in_dummy_function);
4607 in_dummy_function = true;
4608 push_struct_function (NULL_TREE);
4609 prepare_function_start ();
4612 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4613 and initialize static variables for generating RTL for the statements
4614 of the function. */
4616 void
4617 init_function_start (tree subr)
4619 if (subr && DECL_STRUCT_FUNCTION (subr))
4620 set_cfun (DECL_STRUCT_FUNCTION (subr));
4621 else
4622 allocate_struct_function (subr, false);
4623 prepare_function_start ();
4624 decide_function_section (subr);
4626 /* Warn if this value is an aggregate type,
4627 regardless of which calling convention we are using for it. */
4628 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4629 warning (OPT_Waggregate_return, "function returns an aggregate");
4632 /* Expand code to verify the stack_protect_guard. This is invoked at
4633 the end of a function to be protected. */
4635 #ifndef HAVE_stack_protect_test
4636 # define HAVE_stack_protect_test 0
4637 # define gen_stack_protect_test(x, y, z) (gcc_unreachable (), NULL_RTX)
4638 #endif
4640 void
4641 stack_protect_epilogue (void)
4643 tree guard_decl = targetm.stack_protect_guard ();
4644 rtx label = gen_label_rtx ();
4645 rtx x, y, tmp;
4647 x = expand_normal (crtl->stack_protect_guard);
4648 y = expand_normal (guard_decl);
4650 /* Allow the target to compare Y with X without leaking either into
4651 a register. */
4652 switch (HAVE_stack_protect_test != 0)
4654 case 1:
4655 tmp = gen_stack_protect_test (x, y, label);
4656 if (tmp)
4658 emit_insn (tmp);
4659 break;
4661 /* FALLTHRU */
4663 default:
4664 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
4665 break;
4668 /* The noreturn predictor has been moved to the tree level. The rtl-level
4669 predictors estimate this branch about 20%, which isn't enough to get
4670 things moved out of line. Since this is the only extant case of adding
4671 a noreturn function at the rtl level, it doesn't seem worth doing ought
4672 except adding the prediction by hand. */
4673 tmp = get_last_insn ();
4674 if (JUMP_P (tmp))
4675 predict_insn_def (tmp, PRED_NORETURN, TAKEN);
4677 expand_call (targetm.stack_protect_fail (), NULL_RTX, /*ignore=*/true);
4678 free_temp_slots ();
4679 emit_label (label);
4682 /* Start the RTL for a new function, and set variables used for
4683 emitting RTL.
4684 SUBR is the FUNCTION_DECL node.
4685 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
4686 the function's parameters, which must be run at any return statement. */
4688 void
4689 expand_function_start (tree subr)
4691 /* Make sure volatile mem refs aren't considered
4692 valid operands of arithmetic insns. */
4693 init_recog_no_volatile ();
4695 crtl->profile
4696 = (profile_flag
4697 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
4699 crtl->limit_stack
4700 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
4702 /* Make the label for return statements to jump to. Do not special
4703 case machines with special return instructions -- they will be
4704 handled later during jump, ifcvt, or epilogue creation. */
4705 return_label = gen_label_rtx ();
4707 /* Initialize rtx used to return the value. */
4708 /* Do this before assign_parms so that we copy the struct value address
4709 before any library calls that assign parms might generate. */
4711 /* Decide whether to return the value in memory or in a register. */
4712 if (aggregate_value_p (DECL_RESULT (subr), subr))
4714 /* Returning something that won't go in a register. */
4715 rtx value_address = 0;
4717 #ifdef PCC_STATIC_STRUCT_RETURN
4718 if (cfun->returns_pcc_struct)
4720 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
4721 value_address = assemble_static_space (size);
4723 else
4724 #endif
4726 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
4727 /* Expect to be passed the address of a place to store the value.
4728 If it is passed as an argument, assign_parms will take care of
4729 it. */
4730 if (sv)
4732 value_address = gen_reg_rtx (Pmode);
4733 emit_move_insn (value_address, sv);
4736 if (value_address)
4738 rtx x = value_address;
4739 if (!DECL_BY_REFERENCE (DECL_RESULT (subr)))
4741 x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), x);
4742 set_mem_attributes (x, DECL_RESULT (subr), 1);
4744 SET_DECL_RTL (DECL_RESULT (subr), x);
4747 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
4748 /* If return mode is void, this decl rtl should not be used. */
4749 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
4750 else
4752 /* Compute the return values into a pseudo reg, which we will copy
4753 into the true return register after the cleanups are done. */
4754 tree return_type = TREE_TYPE (DECL_RESULT (subr));
4755 if (TYPE_MODE (return_type) != BLKmode
4756 && targetm.calls.return_in_msb (return_type))
4757 /* expand_function_end will insert the appropriate padding in
4758 this case. Use the return value's natural (unpadded) mode
4759 within the function proper. */
4760 SET_DECL_RTL (DECL_RESULT (subr),
4761 gen_reg_rtx (TYPE_MODE (return_type)));
4762 else
4764 /* In order to figure out what mode to use for the pseudo, we
4765 figure out what the mode of the eventual return register will
4766 actually be, and use that. */
4767 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
4769 /* Structures that are returned in registers are not
4770 aggregate_value_p, so we may see a PARALLEL or a REG. */
4771 if (REG_P (hard_reg))
4772 SET_DECL_RTL (DECL_RESULT (subr),
4773 gen_reg_rtx (GET_MODE (hard_reg)));
4774 else
4776 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
4777 SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
4781 /* Set DECL_REGISTER flag so that expand_function_end will copy the
4782 result to the real return register(s). */
4783 DECL_REGISTER (DECL_RESULT (subr)) = 1;
4786 /* Initialize rtx for parameters and local variables.
4787 In some cases this requires emitting insns. */
4788 assign_parms (subr);
4790 /* If function gets a static chain arg, store it. */
4791 if (cfun->static_chain_decl)
4793 tree parm = cfun->static_chain_decl;
4794 rtx local, chain, insn;
4796 local = gen_reg_rtx (Pmode);
4797 chain = targetm.calls.static_chain (current_function_decl, true);
4799 set_decl_incoming_rtl (parm, chain, false);
4800 SET_DECL_RTL (parm, local);
4801 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4803 insn = emit_move_insn (local, chain);
4805 /* Mark the register as eliminable, similar to parameters. */
4806 if (MEM_P (chain)
4807 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
4808 set_dst_reg_note (insn, REG_EQUIV, chain, local);
4811 /* If the function receives a non-local goto, then store the
4812 bits we need to restore the frame pointer. */
4813 if (cfun->nonlocal_goto_save_area)
4815 tree t_save;
4816 rtx r_save;
4818 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
4819 gcc_assert (DECL_RTL_SET_P (var));
4821 t_save = build4 (ARRAY_REF,
4822 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
4823 cfun->nonlocal_goto_save_area,
4824 integer_zero_node, NULL_TREE, NULL_TREE);
4825 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
4826 gcc_assert (GET_MODE (r_save) == Pmode);
4828 emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
4829 update_nonlocal_goto_save_area ();
4832 /* The following was moved from init_function_start.
4833 The move is supposed to make sdb output more accurate. */
4834 /* Indicate the beginning of the function body,
4835 as opposed to parm setup. */
4836 emit_note (NOTE_INSN_FUNCTION_BEG);
4838 gcc_assert (NOTE_P (get_last_insn ()));
4840 parm_birth_insn = get_last_insn ();
4842 if (crtl->profile)
4844 #ifdef PROFILE_HOOK
4845 PROFILE_HOOK (current_function_funcdef_no);
4846 #endif
4849 /* If we are doing generic stack checking, the probe should go here. */
4850 if (flag_stack_check == GENERIC_STACK_CHECK)
4851 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
4854 /* Undo the effects of init_dummy_function_start. */
4855 void
4856 expand_dummy_function_end (void)
4858 gcc_assert (in_dummy_function);
4860 /* End any sequences that failed to be closed due to syntax errors. */
4861 while (in_sequence_p ())
4862 end_sequence ();
4864 /* Outside function body, can't compute type's actual size
4865 until next function's body starts. */
4867 free_after_parsing (cfun);
4868 free_after_compilation (cfun);
4869 pop_cfun ();
4870 in_dummy_function = false;
4873 /* Call DOIT for each hard register used as a return value from
4874 the current function. */
4876 void
4877 diddle_return_value (void (*doit) (rtx, void *), void *arg)
4879 rtx outgoing = crtl->return_rtx;
4881 if (! outgoing)
4882 return;
4884 if (REG_P (outgoing))
4885 (*doit) (outgoing, arg);
4886 else if (GET_CODE (outgoing) == PARALLEL)
4888 int i;
4890 for (i = 0; i < XVECLEN (outgoing, 0); i++)
4892 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
4894 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
4895 (*doit) (x, arg);
4900 static void
4901 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4903 emit_clobber (reg);
4906 void
4907 clobber_return_register (void)
4909 diddle_return_value (do_clobber_return_reg, NULL);
4911 /* In case we do use pseudo to return value, clobber it too. */
4912 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
4914 tree decl_result = DECL_RESULT (current_function_decl);
4915 rtx decl_rtl = DECL_RTL (decl_result);
4916 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
4918 do_clobber_return_reg (decl_rtl, NULL);
4923 static void
4924 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
4926 emit_use (reg);
4929 static void
4930 use_return_register (void)
4932 diddle_return_value (do_use_return_reg, NULL);
4935 /* Possibly warn about unused parameters. */
4936 void
4937 do_warn_unused_parameter (tree fn)
4939 tree decl;
4941 for (decl = DECL_ARGUMENTS (fn);
4942 decl; decl = DECL_CHAIN (decl))
4943 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
4944 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
4945 && !TREE_NO_WARNING (decl))
4946 warning (OPT_Wunused_parameter, "unused parameter %q+D", decl);
4949 /* Set the location of the insn chain starting at INSN to LOC. */
4951 static void
4952 set_insn_locations (rtx insn, int loc)
4954 while (insn != NULL_RTX)
4956 if (INSN_P (insn))
4957 INSN_LOCATION (insn) = loc;
4958 insn = NEXT_INSN (insn);
4962 /* Generate RTL for the end of the current function. */
4964 void
4965 expand_function_end (void)
4967 rtx clobber_after;
4969 /* If arg_pointer_save_area was referenced only from a nested
4970 function, we will not have initialized it yet. Do that now. */
4971 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
4972 get_arg_pointer_save_area ();
4974 /* If we are doing generic stack checking and this function makes calls,
4975 do a stack probe at the start of the function to ensure we have enough
4976 space for another stack frame. */
4977 if (flag_stack_check == GENERIC_STACK_CHECK)
4979 rtx insn, seq;
4981 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4982 if (CALL_P (insn))
4984 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
4985 start_sequence ();
4986 if (STACK_CHECK_MOVING_SP)
4987 anti_adjust_stack_and_probe (max_frame_size, true);
4988 else
4989 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
4990 seq = get_insns ();
4991 end_sequence ();
4992 set_insn_locations (seq, prologue_location);
4993 emit_insn_before (seq, stack_check_probe_note);
4994 break;
4998 /* End any sequences that failed to be closed due to syntax errors. */
4999 while (in_sequence_p ())
5000 end_sequence ();
5002 clear_pending_stack_adjust ();
5003 do_pending_stack_adjust ();
5005 /* Output a linenumber for the end of the function.
5006 SDB depends on this. */
5007 set_curr_insn_location (input_location);
5009 /* Before the return label (if any), clobber the return
5010 registers so that they are not propagated live to the rest of
5011 the function. This can only happen with functions that drop
5012 through; if there had been a return statement, there would
5013 have either been a return rtx, or a jump to the return label.
5015 We delay actual code generation after the current_function_value_rtx
5016 is computed. */
5017 clobber_after = get_last_insn ();
5019 /* Output the label for the actual return from the function. */
5020 emit_label (return_label);
5022 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
5024 /* Let except.c know where it should emit the call to unregister
5025 the function context for sjlj exceptions. */
5026 if (flag_exceptions)
5027 sjlj_emit_function_exit_after (get_last_insn ());
5029 else
5031 /* We want to ensure that instructions that may trap are not
5032 moved into the epilogue by scheduling, because we don't
5033 always emit unwind information for the epilogue. */
5034 if (cfun->can_throw_non_call_exceptions)
5035 emit_insn (gen_blockage ());
5038 /* If this is an implementation of throw, do what's necessary to
5039 communicate between __builtin_eh_return and the epilogue. */
5040 expand_eh_return ();
5042 /* If scalar return value was computed in a pseudo-reg, or was a named
5043 return value that got dumped to the stack, copy that to the hard
5044 return register. */
5045 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5047 tree decl_result = DECL_RESULT (current_function_decl);
5048 rtx decl_rtl = DECL_RTL (decl_result);
5050 if (REG_P (decl_rtl)
5051 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5052 : DECL_REGISTER (decl_result))
5054 rtx real_decl_rtl = crtl->return_rtx;
5056 /* This should be set in assign_parms. */
5057 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5059 /* If this is a BLKmode structure being returned in registers,
5060 then use the mode computed in expand_return. Note that if
5061 decl_rtl is memory, then its mode may have been changed,
5062 but that crtl->return_rtx has not. */
5063 if (GET_MODE (real_decl_rtl) == BLKmode)
5064 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
5066 /* If a non-BLKmode return value should be padded at the least
5067 significant end of the register, shift it left by the appropriate
5068 amount. BLKmode results are handled using the group load/store
5069 machinery. */
5070 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5071 && REG_P (real_decl_rtl)
5072 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5074 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5075 REGNO (real_decl_rtl)),
5076 decl_rtl);
5077 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5079 /* If a named return value dumped decl_return to memory, then
5080 we may need to re-do the PROMOTE_MODE signed/unsigned
5081 extension. */
5082 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5084 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5085 promote_function_mode (TREE_TYPE (decl_result),
5086 GET_MODE (decl_rtl), &unsignedp,
5087 TREE_TYPE (current_function_decl), 1);
5089 convert_move (real_decl_rtl, decl_rtl, unsignedp);
5091 else if (GET_CODE (real_decl_rtl) == PARALLEL)
5093 /* If expand_function_start has created a PARALLEL for decl_rtl,
5094 move the result to the real return registers. Otherwise, do
5095 a group load from decl_rtl for a named return. */
5096 if (GET_CODE (decl_rtl) == PARALLEL)
5097 emit_group_move (real_decl_rtl, decl_rtl);
5098 else
5099 emit_group_load (real_decl_rtl, decl_rtl,
5100 TREE_TYPE (decl_result),
5101 int_size_in_bytes (TREE_TYPE (decl_result)));
5103 /* In the case of complex integer modes smaller than a word, we'll
5104 need to generate some non-trivial bitfield insertions. Do that
5105 on a pseudo and not the hard register. */
5106 else if (GET_CODE (decl_rtl) == CONCAT
5107 && GET_MODE_CLASS (GET_MODE (decl_rtl)) == MODE_COMPLEX_INT
5108 && GET_MODE_BITSIZE (GET_MODE (decl_rtl)) <= BITS_PER_WORD)
5110 int old_generating_concat_p;
5111 rtx tmp;
5113 old_generating_concat_p = generating_concat_p;
5114 generating_concat_p = 0;
5115 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5116 generating_concat_p = old_generating_concat_p;
5118 emit_move_insn (tmp, decl_rtl);
5119 emit_move_insn (real_decl_rtl, tmp);
5121 else
5122 emit_move_insn (real_decl_rtl, decl_rtl);
5126 /* If returning a structure, arrange to return the address of the value
5127 in a place where debuggers expect to find it.
5129 If returning a structure PCC style,
5130 the caller also depends on this value.
5131 And cfun->returns_pcc_struct is not necessarily set. */
5132 if (cfun->returns_struct
5133 || cfun->returns_pcc_struct)
5135 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5136 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5137 rtx outgoing;
5139 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5140 type = TREE_TYPE (type);
5141 else
5142 value_address = XEXP (value_address, 0);
5144 outgoing = targetm.calls.function_value (build_pointer_type (type),
5145 current_function_decl, true);
5147 /* Mark this as a function return value so integrate will delete the
5148 assignment and USE below when inlining this function. */
5149 REG_FUNCTION_VALUE_P (outgoing) = 1;
5151 /* The address may be ptr_mode and OUTGOING may be Pmode. */
5152 value_address = convert_memory_address (GET_MODE (outgoing),
5153 value_address);
5155 emit_move_insn (outgoing, value_address);
5157 /* Show return register used to hold result (in this case the address
5158 of the result. */
5159 crtl->return_rtx = outgoing;
5162 /* Emit the actual code to clobber return register. Don't emit
5163 it if clobber_after is a barrier, then the previous basic block
5164 certainly doesn't fall thru into the exit block. */
5165 if (!BARRIER_P (clobber_after))
5167 rtx seq;
5169 start_sequence ();
5170 clobber_return_register ();
5171 seq = get_insns ();
5172 end_sequence ();
5174 emit_insn_after (seq, clobber_after);
5177 /* Output the label for the naked return from the function. */
5178 if (naked_return_label)
5179 emit_label (naked_return_label);
5181 /* @@@ This is a kludge. We want to ensure that instructions that
5182 may trap are not moved into the epilogue by scheduling, because
5183 we don't always emit unwind information for the epilogue. */
5184 if (cfun->can_throw_non_call_exceptions
5185 && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5186 emit_insn (gen_blockage ());
5188 /* If stack protection is enabled for this function, check the guard. */
5189 if (crtl->stack_protect_guard)
5190 stack_protect_epilogue ();
5192 /* If we had calls to alloca, and this machine needs
5193 an accurate stack pointer to exit the function,
5194 insert some code to save and restore the stack pointer. */
5195 if (! EXIT_IGNORE_STACK
5196 && cfun->calls_alloca)
5198 rtx tem = 0, seq;
5200 start_sequence ();
5201 emit_stack_save (SAVE_FUNCTION, &tem);
5202 seq = get_insns ();
5203 end_sequence ();
5204 emit_insn_before (seq, parm_birth_insn);
5206 emit_stack_restore (SAVE_FUNCTION, tem);
5209 /* ??? This should no longer be necessary since stupid is no longer with
5210 us, but there are some parts of the compiler (eg reload_combine, and
5211 sh mach_dep_reorg) that still try and compute their own lifetime info
5212 instead of using the general framework. */
5213 use_return_register ();
5217 get_arg_pointer_save_area (void)
5219 rtx ret = arg_pointer_save_area;
5221 if (! ret)
5223 ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5224 arg_pointer_save_area = ret;
5227 if (! crtl->arg_pointer_save_area_init)
5229 rtx seq;
5231 /* Save the arg pointer at the beginning of the function. The
5232 generated stack slot may not be a valid memory address, so we
5233 have to check it and fix it if necessary. */
5234 start_sequence ();
5235 emit_move_insn (validize_mem (ret),
5236 crtl->args.internal_arg_pointer);
5237 seq = get_insns ();
5238 end_sequence ();
5240 push_topmost_sequence ();
5241 emit_insn_after (seq, entry_of_function ());
5242 pop_topmost_sequence ();
5244 crtl->arg_pointer_save_area_init = true;
5247 return ret;
5250 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5251 for the first time. */
5253 static void
5254 record_insns (rtx insns, rtx end, htab_t *hashp)
5256 rtx tmp;
5257 htab_t hash = *hashp;
5259 if (hash == NULL)
5260 *hashp = hash
5261 = htab_create_ggc (17, htab_hash_pointer, htab_eq_pointer, NULL);
5263 for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5265 void **slot = htab_find_slot (hash, tmp, INSERT);
5266 gcc_assert (*slot == NULL);
5267 *slot = tmp;
5271 /* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5272 basic block, splitting or peepholes. If INSN is a prologue or epilogue
5273 insn, then record COPY as well. */
5275 void
5276 maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5278 htab_t hash;
5279 void **slot;
5281 hash = epilogue_insn_hash;
5282 if (!hash || !htab_find (hash, insn))
5284 hash = prologue_insn_hash;
5285 if (!hash || !htab_find (hash, insn))
5286 return;
5289 slot = htab_find_slot (hash, copy, INSERT);
5290 gcc_assert (*slot == NULL);
5291 *slot = copy;
5294 /* Determine if any INSNs in HASH are, or are part of, INSN. Because
5295 we can be running after reorg, SEQUENCE rtl is possible. */
5297 static bool
5298 contains (const_rtx insn, htab_t hash)
5300 if (hash == NULL)
5301 return false;
5303 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5305 int i;
5306 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
5307 if (htab_find (hash, XVECEXP (PATTERN (insn), 0, i)))
5308 return true;
5309 return false;
5312 return htab_find (hash, insn) != NULL;
5316 prologue_epilogue_contains (const_rtx insn)
5318 if (contains (insn, prologue_insn_hash))
5319 return 1;
5320 if (contains (insn, epilogue_insn_hash))
5321 return 1;
5322 return 0;
5325 #ifdef HAVE_return
5326 /* Insert use of return register before the end of BB. */
5328 static void
5329 emit_use_return_register_into_block (basic_block bb)
5331 rtx seq, insn;
5332 start_sequence ();
5333 use_return_register ();
5334 seq = get_insns ();
5335 end_sequence ();
5336 insn = BB_END (bb);
5337 #ifdef HAVE_cc0
5338 if (reg_mentioned_p (cc0_rtx, PATTERN (insn)))
5339 insn = prev_cc0_setter (insn);
5340 #endif
5341 emit_insn_before (seq, insn);
5345 /* Create a return pattern, either simple_return or return, depending on
5346 simple_p. */
5348 static rtx
5349 gen_return_pattern (bool simple_p)
5351 #ifdef HAVE_simple_return
5352 return simple_p ? gen_simple_return () : gen_return ();
5353 #else
5354 gcc_assert (!simple_p);
5355 return gen_return ();
5356 #endif
5359 /* Insert an appropriate return pattern at the end of block BB. This
5360 also means updating block_for_insn appropriately. SIMPLE_P is
5361 the same as in gen_return_pattern and passed to it. */
5363 void
5364 emit_return_into_block (bool simple_p, basic_block bb)
5366 rtx jump, pat;
5367 jump = emit_jump_insn_after (gen_return_pattern (simple_p), BB_END (bb));
5368 pat = PATTERN (jump);
5369 if (GET_CODE (pat) == PARALLEL)
5370 pat = XVECEXP (pat, 0, 0);
5371 gcc_assert (ANY_RETURN_P (pat));
5372 JUMP_LABEL (jump) = pat;
5374 #endif
5376 /* Set JUMP_LABEL for a return insn. */
5378 void
5379 set_return_jump_label (rtx returnjump)
5381 rtx pat = PATTERN (returnjump);
5382 if (GET_CODE (pat) == PARALLEL)
5383 pat = XVECEXP (pat, 0, 0);
5384 if (ANY_RETURN_P (pat))
5385 JUMP_LABEL (returnjump) = pat;
5386 else
5387 JUMP_LABEL (returnjump) = ret_rtx;
5390 #if defined (HAVE_return) || defined (HAVE_simple_return)
5391 /* Return true if there are any active insns between HEAD and TAIL. */
5392 bool
5393 active_insn_between (rtx head, rtx tail)
5395 while (tail)
5397 if (active_insn_p (tail))
5398 return true;
5399 if (tail == head)
5400 return false;
5401 tail = PREV_INSN (tail);
5403 return false;
5406 /* LAST_BB is a block that exits, and empty of active instructions.
5407 Examine its predecessors for jumps that can be converted to
5408 (conditional) returns. */
5409 vec<edge>
5410 convert_jumps_to_returns (basic_block last_bb, bool simple_p,
5411 vec<edge> unconverted ATTRIBUTE_UNUSED)
5413 int i;
5414 basic_block bb;
5415 rtx label;
5416 edge_iterator ei;
5417 edge e;
5418 auto_vec<basic_block> src_bbs (EDGE_COUNT (last_bb->preds));
5420 FOR_EACH_EDGE (e, ei, last_bb->preds)
5421 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
5422 src_bbs.quick_push (e->src);
5424 label = BB_HEAD (last_bb);
5426 FOR_EACH_VEC_ELT (src_bbs, i, bb)
5428 rtx jump = BB_END (bb);
5430 if (!JUMP_P (jump) || JUMP_LABEL (jump) != label)
5431 continue;
5433 e = find_edge (bb, last_bb);
5435 /* If we have an unconditional jump, we can replace that
5436 with a simple return instruction. */
5437 if (simplejump_p (jump))
5439 /* The use of the return register might be present in the exit
5440 fallthru block. Either:
5441 - removing the use is safe, and we should remove the use in
5442 the exit fallthru block, or
5443 - removing the use is not safe, and we should add it here.
5444 For now, we conservatively choose the latter. Either of the
5445 2 helps in crossjumping. */
5446 emit_use_return_register_into_block (bb);
5448 emit_return_into_block (simple_p, bb);
5449 delete_insn (jump);
5452 /* If we have a conditional jump branching to the last
5453 block, we can try to replace that with a conditional
5454 return instruction. */
5455 else if (condjump_p (jump))
5457 rtx dest;
5459 if (simple_p)
5460 dest = simple_return_rtx;
5461 else
5462 dest = ret_rtx;
5463 if (!redirect_jump (jump, dest, 0))
5465 #ifdef HAVE_simple_return
5466 if (simple_p)
5468 if (dump_file)
5469 fprintf (dump_file,
5470 "Failed to redirect bb %d branch.\n", bb->index);
5471 unconverted.safe_push (e);
5473 #endif
5474 continue;
5477 /* See comment in simplejump_p case above. */
5478 emit_use_return_register_into_block (bb);
5480 /* If this block has only one successor, it both jumps
5481 and falls through to the fallthru block, so we can't
5482 delete the edge. */
5483 if (single_succ_p (bb))
5484 continue;
5486 else
5488 #ifdef HAVE_simple_return
5489 if (simple_p)
5491 if (dump_file)
5492 fprintf (dump_file,
5493 "Failed to redirect bb %d branch.\n", bb->index);
5494 unconverted.safe_push (e);
5496 #endif
5497 continue;
5500 /* Fix up the CFG for the successful change we just made. */
5501 redirect_edge_succ (e, EXIT_BLOCK_PTR_FOR_FN (cfun));
5502 e->flags &= ~EDGE_CROSSING;
5504 src_bbs.release ();
5505 return unconverted;
5508 /* Emit a return insn for the exit fallthru block. */
5509 basic_block
5510 emit_return_for_exit (edge exit_fallthru_edge, bool simple_p)
5512 basic_block last_bb = exit_fallthru_edge->src;
5514 if (JUMP_P (BB_END (last_bb)))
5516 last_bb = split_edge (exit_fallthru_edge);
5517 exit_fallthru_edge = single_succ_edge (last_bb);
5519 emit_barrier_after (BB_END (last_bb));
5520 emit_return_into_block (simple_p, last_bb);
5521 exit_fallthru_edge->flags &= ~EDGE_FALLTHRU;
5522 return last_bb;
5524 #endif
5527 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
5528 this into place with notes indicating where the prologue ends and where
5529 the epilogue begins. Update the basic block information when possible.
5531 Notes on epilogue placement:
5532 There are several kinds of edges to the exit block:
5533 * a single fallthru edge from LAST_BB
5534 * possibly, edges from blocks containing sibcalls
5535 * possibly, fake edges from infinite loops
5537 The epilogue is always emitted on the fallthru edge from the last basic
5538 block in the function, LAST_BB, into the exit block.
5540 If LAST_BB is empty except for a label, it is the target of every
5541 other basic block in the function that ends in a return. If a
5542 target has a return or simple_return pattern (possibly with
5543 conditional variants), these basic blocks can be changed so that a
5544 return insn is emitted into them, and their target is adjusted to
5545 the real exit block.
5547 Notes on shrink wrapping: We implement a fairly conservative
5548 version of shrink-wrapping rather than the textbook one. We only
5549 generate a single prologue and a single epilogue. This is
5550 sufficient to catch a number of interesting cases involving early
5551 exits.
5553 First, we identify the blocks that require the prologue to occur before
5554 them. These are the ones that modify a call-saved register, or reference
5555 any of the stack or frame pointer registers. To simplify things, we then
5556 mark everything reachable from these blocks as also requiring a prologue.
5557 This takes care of loops automatically, and avoids the need to examine
5558 whether MEMs reference the frame, since it is sufficient to check for
5559 occurrences of the stack or frame pointer.
5561 We then compute the set of blocks for which the need for a prologue
5562 is anticipatable (borrowing terminology from the shrink-wrapping
5563 description in Muchnick's book). These are the blocks which either
5564 require a prologue themselves, or those that have only successors
5565 where the prologue is anticipatable. The prologue needs to be
5566 inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
5567 is not. For the moment, we ensure that only one such edge exists.
5569 The epilogue is placed as described above, but we make a
5570 distinction between inserting return and simple_return patterns
5571 when modifying other blocks that end in a return. Blocks that end
5572 in a sibcall omit the sibcall_epilogue if the block is not in
5573 ANTIC. */
5575 static void
5576 thread_prologue_and_epilogue_insns (void)
5578 bool inserted;
5579 #ifdef HAVE_simple_return
5580 vec<edge> unconverted_simple_returns = vNULL;
5581 bitmap_head bb_flags;
5582 #endif
5583 rtx returnjump;
5584 rtx seq ATTRIBUTE_UNUSED, epilogue_end ATTRIBUTE_UNUSED;
5585 rtx prologue_seq ATTRIBUTE_UNUSED, split_prologue_seq ATTRIBUTE_UNUSED;
5586 edge e, entry_edge, orig_entry_edge, exit_fallthru_edge;
5587 edge_iterator ei;
5589 df_analyze ();
5591 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5593 inserted = false;
5594 seq = NULL_RTX;
5595 epilogue_end = NULL_RTX;
5596 returnjump = NULL_RTX;
5598 /* Can't deal with multiple successors of the entry block at the
5599 moment. Function should always have at least one entry
5600 point. */
5601 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5602 entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5603 orig_entry_edge = entry_edge;
5605 split_prologue_seq = NULL_RTX;
5606 if (flag_split_stack
5607 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
5608 == NULL))
5610 #ifndef HAVE_split_stack_prologue
5611 gcc_unreachable ();
5612 #else
5613 gcc_assert (HAVE_split_stack_prologue);
5615 start_sequence ();
5616 emit_insn (gen_split_stack_prologue ());
5617 split_prologue_seq = get_insns ();
5618 end_sequence ();
5620 record_insns (split_prologue_seq, NULL, &prologue_insn_hash);
5621 set_insn_locations (split_prologue_seq, prologue_location);
5622 #endif
5625 prologue_seq = NULL_RTX;
5626 #ifdef HAVE_prologue
5627 if (HAVE_prologue)
5629 start_sequence ();
5630 seq = gen_prologue ();
5631 emit_insn (seq);
5633 /* Insert an explicit USE for the frame pointer
5634 if the profiling is on and the frame pointer is required. */
5635 if (crtl->profile && frame_pointer_needed)
5636 emit_use (hard_frame_pointer_rtx);
5638 /* Retain a map of the prologue insns. */
5639 record_insns (seq, NULL, &prologue_insn_hash);
5640 emit_note (NOTE_INSN_PROLOGUE_END);
5642 /* Ensure that instructions are not moved into the prologue when
5643 profiling is on. The call to the profiling routine can be
5644 emitted within the live range of a call-clobbered register. */
5645 if (!targetm.profile_before_prologue () && crtl->profile)
5646 emit_insn (gen_blockage ());
5648 prologue_seq = get_insns ();
5649 end_sequence ();
5650 set_insn_locations (prologue_seq, prologue_location);
5652 #endif
5654 #ifdef HAVE_simple_return
5655 bitmap_initialize (&bb_flags, &bitmap_default_obstack);
5657 /* Try to perform a kind of shrink-wrapping, making sure the
5658 prologue/epilogue is emitted only around those parts of the
5659 function that require it. */
5661 try_shrink_wrapping (&entry_edge, orig_entry_edge, &bb_flags, prologue_seq);
5662 #endif
5664 if (split_prologue_seq != NULL_RTX)
5666 insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
5667 inserted = true;
5669 if (prologue_seq != NULL_RTX)
5671 insert_insn_on_edge (prologue_seq, entry_edge);
5672 inserted = true;
5675 /* If the exit block has no non-fake predecessors, we don't need
5676 an epilogue. */
5677 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5678 if ((e->flags & EDGE_FAKE) == 0)
5679 break;
5680 if (e == NULL)
5681 goto epilogue_done;
5683 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5685 exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
5687 #ifdef HAVE_simple_return
5688 if (entry_edge != orig_entry_edge)
5689 exit_fallthru_edge
5690 = get_unconverted_simple_return (exit_fallthru_edge, bb_flags,
5691 &unconverted_simple_returns,
5692 &returnjump);
5693 #endif
5694 #ifdef HAVE_return
5695 if (HAVE_return)
5697 if (exit_fallthru_edge == NULL)
5698 goto epilogue_done;
5700 if (optimize)
5702 basic_block last_bb = exit_fallthru_edge->src;
5704 if (LABEL_P (BB_HEAD (last_bb))
5705 && !active_insn_between (BB_HEAD (last_bb), BB_END (last_bb)))
5706 convert_jumps_to_returns (last_bb, false, vNULL);
5708 if (EDGE_COUNT (last_bb->preds) != 0
5709 && single_succ_p (last_bb))
5711 last_bb = emit_return_for_exit (exit_fallthru_edge, false);
5712 epilogue_end = returnjump = BB_END (last_bb);
5713 #ifdef HAVE_simple_return
5714 /* Emitting the return may add a basic block.
5715 Fix bb_flags for the added block. */
5716 if (last_bb != exit_fallthru_edge->src)
5717 bitmap_set_bit (&bb_flags, last_bb->index);
5718 #endif
5719 goto epilogue_done;
5723 #endif
5725 /* A small fib -- epilogue is not yet completed, but we wish to re-use
5726 this marker for the splits of EH_RETURN patterns, and nothing else
5727 uses the flag in the meantime. */
5728 epilogue_completed = 1;
5730 #ifdef HAVE_eh_return
5731 /* Find non-fallthru edges that end with EH_RETURN instructions. On
5732 some targets, these get split to a special version of the epilogue
5733 code. In order to be able to properly annotate these with unwind
5734 info, try to split them now. If we get a valid split, drop an
5735 EPILOGUE_BEG note and mark the insns as epilogue insns. */
5736 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5738 rtx prev, last, trial;
5740 if (e->flags & EDGE_FALLTHRU)
5741 continue;
5742 last = BB_END (e->src);
5743 if (!eh_returnjump_p (last))
5744 continue;
5746 prev = PREV_INSN (last);
5747 trial = try_split (PATTERN (last), last, 1);
5748 if (trial == last)
5749 continue;
5751 record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
5752 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
5754 #endif
5756 /* If nothing falls through into the exit block, we don't need an
5757 epilogue. */
5759 if (exit_fallthru_edge == NULL)
5760 goto epilogue_done;
5762 #ifdef HAVE_epilogue
5763 if (HAVE_epilogue)
5765 start_sequence ();
5766 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
5767 seq = gen_epilogue ();
5768 if (seq)
5769 emit_jump_insn (seq);
5771 /* Retain a map of the epilogue insns. */
5772 record_insns (seq, NULL, &epilogue_insn_hash);
5773 set_insn_locations (seq, epilogue_location);
5775 seq = get_insns ();
5776 returnjump = get_last_insn ();
5777 end_sequence ();
5779 insert_insn_on_edge (seq, exit_fallthru_edge);
5780 inserted = true;
5782 if (JUMP_P (returnjump))
5783 set_return_jump_label (returnjump);
5785 else
5786 #endif
5788 basic_block cur_bb;
5790 if (! next_active_insn (BB_END (exit_fallthru_edge->src)))
5791 goto epilogue_done;
5792 /* We have a fall-through edge to the exit block, the source is not
5793 at the end of the function, and there will be an assembler epilogue
5794 at the end of the function.
5795 We can't use force_nonfallthru here, because that would try to
5796 use return. Inserting a jump 'by hand' is extremely messy, so
5797 we take advantage of cfg_layout_finalize using
5798 fixup_fallthru_exit_predecessor. */
5799 cfg_layout_initialize (0);
5800 FOR_EACH_BB_FN (cur_bb, cfun)
5801 if (cur_bb->index >= NUM_FIXED_BLOCKS
5802 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
5803 cur_bb->aux = cur_bb->next_bb;
5804 cfg_layout_finalize ();
5807 epilogue_done:
5809 default_rtl_profile ();
5811 if (inserted)
5813 sbitmap blocks;
5815 commit_edge_insertions ();
5817 /* Look for basic blocks within the prologue insns. */
5818 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
5819 bitmap_clear (blocks);
5820 bitmap_set_bit (blocks, entry_edge->dest->index);
5821 bitmap_set_bit (blocks, orig_entry_edge->dest->index);
5822 find_many_sub_basic_blocks (blocks);
5823 sbitmap_free (blocks);
5825 /* The epilogue insns we inserted may cause the exit edge to no longer
5826 be fallthru. */
5827 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5829 if (((e->flags & EDGE_FALLTHRU) != 0)
5830 && returnjump_p (BB_END (e->src)))
5831 e->flags &= ~EDGE_FALLTHRU;
5835 #ifdef HAVE_simple_return
5836 convert_to_simple_return (entry_edge, orig_entry_edge, bb_flags, returnjump,
5837 unconverted_simple_returns);
5838 #endif
5840 #ifdef HAVE_sibcall_epilogue
5841 /* Emit sibling epilogues before any sibling call sites. */
5842 for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds); (e =
5843 ei_safe_edge (ei));
5846 basic_block bb = e->src;
5847 rtx insn = BB_END (bb);
5848 rtx ep_seq;
5850 if (!CALL_P (insn)
5851 || ! SIBLING_CALL_P (insn)
5852 #ifdef HAVE_simple_return
5853 || (entry_edge != orig_entry_edge
5854 && !bitmap_bit_p (&bb_flags, bb->index))
5855 #endif
5858 ei_next (&ei);
5859 continue;
5862 ep_seq = gen_sibcall_epilogue ();
5863 if (ep_seq)
5865 start_sequence ();
5866 emit_note (NOTE_INSN_EPILOGUE_BEG);
5867 emit_insn (ep_seq);
5868 seq = get_insns ();
5869 end_sequence ();
5871 /* Retain a map of the epilogue insns. Used in life analysis to
5872 avoid getting rid of sibcall epilogue insns. Do this before we
5873 actually emit the sequence. */
5874 record_insns (seq, NULL, &epilogue_insn_hash);
5875 set_insn_locations (seq, epilogue_location);
5877 emit_insn_before (seq, insn);
5879 ei_next (&ei);
5881 #endif
5883 #ifdef HAVE_epilogue
5884 if (epilogue_end)
5886 rtx insn, next;
5888 /* Similarly, move any line notes that appear after the epilogue.
5889 There is no need, however, to be quite so anal about the existence
5890 of such a note. Also possibly move
5891 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
5892 info generation. */
5893 for (insn = epilogue_end; insn; insn = next)
5895 next = NEXT_INSN (insn);
5896 if (NOTE_P (insn)
5897 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
5898 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
5901 #endif
5903 #ifdef HAVE_simple_return
5904 bitmap_clear (&bb_flags);
5905 #endif
5907 /* Threading the prologue and epilogue changes the artificial refs
5908 in the entry and exit blocks. */
5909 epilogue_completed = 1;
5910 df_update_entry_exit_and_calls ();
5913 /* Reposition the prologue-end and epilogue-begin notes after
5914 instruction scheduling. */
5916 void
5917 reposition_prologue_and_epilogue_notes (void)
5919 #if defined (HAVE_prologue) || defined (HAVE_epilogue) \
5920 || defined (HAVE_sibcall_epilogue)
5921 /* Since the hash table is created on demand, the fact that it is
5922 non-null is a signal that it is non-empty. */
5923 if (prologue_insn_hash != NULL)
5925 size_t len = htab_elements (prologue_insn_hash);
5926 rtx insn, last = NULL, note = NULL;
5928 /* Scan from the beginning until we reach the last prologue insn. */
5929 /* ??? While we do have the CFG intact, there are two problems:
5930 (1) The prologue can contain loops (typically probing the stack),
5931 which means that the end of the prologue isn't in the first bb.
5932 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
5933 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5935 if (NOTE_P (insn))
5937 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
5938 note = insn;
5940 else if (contains (insn, prologue_insn_hash))
5942 last = insn;
5943 if (--len == 0)
5944 break;
5948 if (last)
5950 if (note == NULL)
5952 /* Scan forward looking for the PROLOGUE_END note. It should
5953 be right at the beginning of the block, possibly with other
5954 insn notes that got moved there. */
5955 for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
5957 if (NOTE_P (note)
5958 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
5959 break;
5963 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
5964 if (LABEL_P (last))
5965 last = NEXT_INSN (last);
5966 reorder_insns (note, note, last);
5970 if (epilogue_insn_hash != NULL)
5972 edge_iterator ei;
5973 edge e;
5975 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5977 rtx insn, first = NULL, note = NULL;
5978 basic_block bb = e->src;
5980 /* Scan from the beginning until we reach the first epilogue insn. */
5981 FOR_BB_INSNS (bb, insn)
5983 if (NOTE_P (insn))
5985 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
5987 note = insn;
5988 if (first != NULL)
5989 break;
5992 else if (first == NULL && contains (insn, epilogue_insn_hash))
5994 first = insn;
5995 if (note != NULL)
5996 break;
6000 if (note)
6002 /* If the function has a single basic block, and no real
6003 epilogue insns (e.g. sibcall with no cleanup), the
6004 epilogue note can get scheduled before the prologue
6005 note. If we have frame related prologue insns, having
6006 them scanned during the epilogue will result in a crash.
6007 In this case re-order the epilogue note to just before
6008 the last insn in the block. */
6009 if (first == NULL)
6010 first = BB_END (bb);
6012 if (PREV_INSN (first) != note)
6013 reorder_insns (note, note, PREV_INSN (first));
6017 #endif /* HAVE_prologue or HAVE_epilogue */
6020 /* Returns the name of function declared by FNDECL. */
6021 const char *
6022 fndecl_name (tree fndecl)
6024 if (fndecl == NULL)
6025 return "(nofn)";
6026 return lang_hooks.decl_printable_name (fndecl, 2);
6029 /* Returns the name of function FN. */
6030 const char *
6031 function_name (struct function *fn)
6033 tree fndecl = (fn == NULL) ? NULL : fn->decl;
6034 return fndecl_name (fndecl);
6037 /* Returns the name of the current function. */
6038 const char *
6039 current_function_name (void)
6041 return function_name (cfun);
6045 static unsigned int
6046 rest_of_handle_check_leaf_regs (void)
6048 #ifdef LEAF_REGISTERS
6049 crtl->uses_only_leaf_regs
6050 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6051 #endif
6052 return 0;
6055 /* Insert a TYPE into the used types hash table of CFUN. */
6057 static void
6058 used_types_insert_helper (tree type, struct function *func)
6060 if (type != NULL && func != NULL)
6062 void **slot;
6064 if (func->used_types_hash == NULL)
6065 func->used_types_hash = htab_create_ggc (37, htab_hash_pointer,
6066 htab_eq_pointer, NULL);
6067 slot = htab_find_slot (func->used_types_hash, type, INSERT);
6068 if (*slot == NULL)
6069 *slot = type;
6073 /* Given a type, insert it into the used hash table in cfun. */
6074 void
6075 used_types_insert (tree t)
6077 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6078 if (TYPE_NAME (t))
6079 break;
6080 else
6081 t = TREE_TYPE (t);
6082 if (TREE_CODE (t) == ERROR_MARK)
6083 return;
6084 if (TYPE_NAME (t) == NULL_TREE
6085 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6086 t = TYPE_MAIN_VARIANT (t);
6087 if (debug_info_level > DINFO_LEVEL_NONE)
6089 if (cfun)
6090 used_types_insert_helper (t, cfun);
6091 else
6093 /* So this might be a type referenced by a global variable.
6094 Record that type so that we can later decide to emit its
6095 debug information. */
6096 vec_safe_push (types_used_by_cur_var_decl, t);
6101 /* Helper to Hash a struct types_used_by_vars_entry. */
6103 static hashval_t
6104 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6106 gcc_assert (entry && entry->var_decl && entry->type);
6108 return iterative_hash_object (entry->type,
6109 iterative_hash_object (entry->var_decl, 0));
6112 /* Hash function of the types_used_by_vars_entry hash table. */
6114 hashval_t
6115 types_used_by_vars_do_hash (const void *x)
6117 const struct types_used_by_vars_entry *entry =
6118 (const struct types_used_by_vars_entry *) x;
6120 return hash_types_used_by_vars_entry (entry);
6123 /*Equality function of the types_used_by_vars_entry hash table. */
6126 types_used_by_vars_eq (const void *x1, const void *x2)
6128 const struct types_used_by_vars_entry *e1 =
6129 (const struct types_used_by_vars_entry *) x1;
6130 const struct types_used_by_vars_entry *e2 =
6131 (const struct types_used_by_vars_entry *)x2;
6133 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6136 /* Inserts an entry into the types_used_by_vars_hash hash table. */
6138 void
6139 types_used_by_var_decl_insert (tree type, tree var_decl)
6141 if (type != NULL && var_decl != NULL)
6143 void **slot;
6144 struct types_used_by_vars_entry e;
6145 e.var_decl = var_decl;
6146 e.type = type;
6147 if (types_used_by_vars_hash == NULL)
6148 types_used_by_vars_hash =
6149 htab_create_ggc (37, types_used_by_vars_do_hash,
6150 types_used_by_vars_eq, NULL);
6151 slot = htab_find_slot_with_hash (types_used_by_vars_hash, &e,
6152 hash_types_used_by_vars_entry (&e), INSERT);
6153 if (*slot == NULL)
6155 struct types_used_by_vars_entry *entry;
6156 entry = ggc_alloc<types_used_by_vars_entry> ();
6157 entry->type = type;
6158 entry->var_decl = var_decl;
6159 *slot = entry;
6164 namespace {
6166 const pass_data pass_data_leaf_regs =
6168 RTL_PASS, /* type */
6169 "*leaf_regs", /* name */
6170 OPTGROUP_NONE, /* optinfo_flags */
6171 true, /* has_execute */
6172 TV_NONE, /* tv_id */
6173 0, /* properties_required */
6174 0, /* properties_provided */
6175 0, /* properties_destroyed */
6176 0, /* todo_flags_start */
6177 0, /* todo_flags_finish */
6180 class pass_leaf_regs : public rtl_opt_pass
6182 public:
6183 pass_leaf_regs (gcc::context *ctxt)
6184 : rtl_opt_pass (pass_data_leaf_regs, ctxt)
6187 /* opt_pass methods: */
6188 virtual unsigned int execute (function *)
6190 return rest_of_handle_check_leaf_regs ();
6193 }; // class pass_leaf_regs
6195 } // anon namespace
6197 rtl_opt_pass *
6198 make_pass_leaf_regs (gcc::context *ctxt)
6200 return new pass_leaf_regs (ctxt);
6203 static unsigned int
6204 rest_of_handle_thread_prologue_and_epilogue (void)
6206 if (optimize)
6207 cleanup_cfg (CLEANUP_EXPENSIVE);
6209 /* On some machines, the prologue and epilogue code, or parts thereof,
6210 can be represented as RTL. Doing so lets us schedule insns between
6211 it and the rest of the code and also allows delayed branch
6212 scheduling to operate in the epilogue. */
6213 thread_prologue_and_epilogue_insns ();
6215 /* Shrink-wrapping can result in unreachable edges in the epilogue,
6216 see PR57320. */
6217 cleanup_cfg (0);
6219 /* The stack usage info is finalized during prologue expansion. */
6220 if (flag_stack_usage_info)
6221 output_stack_usage ();
6223 return 0;
6226 namespace {
6228 const pass_data pass_data_thread_prologue_and_epilogue =
6230 RTL_PASS, /* type */
6231 "pro_and_epilogue", /* name */
6232 OPTGROUP_NONE, /* optinfo_flags */
6233 true, /* has_execute */
6234 TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
6235 0, /* properties_required */
6236 0, /* properties_provided */
6237 0, /* properties_destroyed */
6238 0, /* todo_flags_start */
6239 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
6242 class pass_thread_prologue_and_epilogue : public rtl_opt_pass
6244 public:
6245 pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6246 : rtl_opt_pass (pass_data_thread_prologue_and_epilogue, ctxt)
6249 /* opt_pass methods: */
6250 virtual unsigned int execute (function *)
6252 return rest_of_handle_thread_prologue_and_epilogue ();
6255 }; // class pass_thread_prologue_and_epilogue
6257 } // anon namespace
6259 rtl_opt_pass *
6260 make_pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6262 return new pass_thread_prologue_and_epilogue (ctxt);
6266 /* This mini-pass fixes fall-out from SSA in asm statements that have
6267 in-out constraints. Say you start with
6269 orig = inout;
6270 asm ("": "+mr" (inout));
6271 use (orig);
6273 which is transformed very early to use explicit output and match operands:
6275 orig = inout;
6276 asm ("": "=mr" (inout) : "0" (inout));
6277 use (orig);
6279 Or, after SSA and copyprop,
6281 asm ("": "=mr" (inout_2) : "0" (inout_1));
6282 use (inout_1);
6284 Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
6285 they represent two separate values, so they will get different pseudo
6286 registers during expansion. Then, since the two operands need to match
6287 per the constraints, but use different pseudo registers, reload can
6288 only register a reload for these operands. But reloads can only be
6289 satisfied by hardregs, not by memory, so we need a register for this
6290 reload, just because we are presented with non-matching operands.
6291 So, even though we allow memory for this operand, no memory can be
6292 used for it, just because the two operands don't match. This can
6293 cause reload failures on register-starved targets.
6295 So it's a symptom of reload not being able to use memory for reloads
6296 or, alternatively it's also a symptom of both operands not coming into
6297 reload as matching (in which case the pseudo could go to memory just
6298 fine, as the alternative allows it, and no reload would be necessary).
6299 We fix the latter problem here, by transforming
6301 asm ("": "=mr" (inout_2) : "0" (inout_1));
6303 back to
6305 inout_2 = inout_1;
6306 asm ("": "=mr" (inout_2) : "0" (inout_2)); */
6308 static void
6309 match_asm_constraints_1 (rtx insn, rtx *p_sets, int noutputs)
6311 int i;
6312 bool changed = false;
6313 rtx op = SET_SRC (p_sets[0]);
6314 int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
6315 rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
6316 bool *output_matched = XALLOCAVEC (bool, noutputs);
6318 memset (output_matched, 0, noutputs * sizeof (bool));
6319 for (i = 0; i < ninputs; i++)
6321 rtx input, output, insns;
6322 const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
6323 char *end;
6324 int match, j;
6326 if (*constraint == '%')
6327 constraint++;
6329 match = strtoul (constraint, &end, 10);
6330 if (end == constraint)
6331 continue;
6333 gcc_assert (match < noutputs);
6334 output = SET_DEST (p_sets[match]);
6335 input = RTVEC_ELT (inputs, i);
6336 /* Only do the transformation for pseudos. */
6337 if (! REG_P (output)
6338 || rtx_equal_p (output, input)
6339 || (GET_MODE (input) != VOIDmode
6340 && GET_MODE (input) != GET_MODE (output)))
6341 continue;
6343 /* We can't do anything if the output is also used as input,
6344 as we're going to overwrite it. */
6345 for (j = 0; j < ninputs; j++)
6346 if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
6347 break;
6348 if (j != ninputs)
6349 continue;
6351 /* Avoid changing the same input several times. For
6352 asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
6353 only change in once (to out1), rather than changing it
6354 first to out1 and afterwards to out2. */
6355 if (i > 0)
6357 for (j = 0; j < noutputs; j++)
6358 if (output_matched[j] && input == SET_DEST (p_sets[j]))
6359 break;
6360 if (j != noutputs)
6361 continue;
6363 output_matched[match] = true;
6365 start_sequence ();
6366 emit_move_insn (output, input);
6367 insns = get_insns ();
6368 end_sequence ();
6369 emit_insn_before (insns, insn);
6371 /* Now replace all mentions of the input with output. We can't
6372 just replace the occurrence in inputs[i], as the register might
6373 also be used in some other input (or even in an address of an
6374 output), which would mean possibly increasing the number of
6375 inputs by one (namely 'output' in addition), which might pose
6376 a too complicated problem for reload to solve. E.g. this situation:
6378 asm ("" : "=r" (output), "=m" (input) : "0" (input))
6380 Here 'input' is used in two occurrences as input (once for the
6381 input operand, once for the address in the second output operand).
6382 If we would replace only the occurrence of the input operand (to
6383 make the matching) we would be left with this:
6385 output = input
6386 asm ("" : "=r" (output), "=m" (input) : "0" (output))
6388 Now we suddenly have two different input values (containing the same
6389 value, but different pseudos) where we formerly had only one.
6390 With more complicated asms this might lead to reload failures
6391 which wouldn't have happen without this pass. So, iterate over
6392 all operands and replace all occurrences of the register used. */
6393 for (j = 0; j < noutputs; j++)
6394 if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
6395 && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
6396 SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
6397 input, output);
6398 for (j = 0; j < ninputs; j++)
6399 if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
6400 RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
6401 input, output);
6403 changed = true;
6406 if (changed)
6407 df_insn_rescan (insn);
6410 namespace {
6412 const pass_data pass_data_match_asm_constraints =
6414 RTL_PASS, /* type */
6415 "asmcons", /* name */
6416 OPTGROUP_NONE, /* optinfo_flags */
6417 true, /* has_execute */
6418 TV_NONE, /* tv_id */
6419 0, /* properties_required */
6420 0, /* properties_provided */
6421 0, /* properties_destroyed */
6422 0, /* todo_flags_start */
6423 0, /* todo_flags_finish */
6426 class pass_match_asm_constraints : public rtl_opt_pass
6428 public:
6429 pass_match_asm_constraints (gcc::context *ctxt)
6430 : rtl_opt_pass (pass_data_match_asm_constraints, ctxt)
6433 /* opt_pass methods: */
6434 virtual unsigned int execute (function *);
6436 }; // class pass_match_asm_constraints
6438 unsigned
6439 pass_match_asm_constraints::execute (function *fun)
6441 basic_block bb;
6442 rtx insn, pat, *p_sets;
6443 int noutputs;
6445 if (!crtl->has_asm_statement)
6446 return 0;
6448 df_set_flags (DF_DEFER_INSN_RESCAN);
6449 FOR_EACH_BB_FN (bb, fun)
6451 FOR_BB_INSNS (bb, insn)
6453 if (!INSN_P (insn))
6454 continue;
6456 pat = PATTERN (insn);
6457 if (GET_CODE (pat) == PARALLEL)
6458 p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
6459 else if (GET_CODE (pat) == SET)
6460 p_sets = &PATTERN (insn), noutputs = 1;
6461 else
6462 continue;
6464 if (GET_CODE (*p_sets) == SET
6465 && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
6466 match_asm_constraints_1 (insn, p_sets, noutputs);
6470 return TODO_df_finish;
6473 } // anon namespace
6475 rtl_opt_pass *
6476 make_pass_match_asm_constraints (gcc::context *ctxt)
6478 return new pass_match_asm_constraints (ctxt);
6482 #include "gt-function.h"