]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/function.c
* tree-vrp.c (debug_value_range, debug_all_value_ranges,
[thirdparty/gcc.git] / gcc / function.c
CommitLineData
bccafa26 1/* Expands front end tree to back end RTL for GCC.
9daf6266 2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
bd564c29 3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010 Free Software Foundation, Inc.
897b77d6 5
f12b58b3 6This file is part of GCC.
897b77d6 7
f12b58b3 8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
8c4c00c1 10Software Foundation; either version 3, or (at your option) any later
f12b58b3 11version.
897b77d6 12
f12b58b3 13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
897b77d6 17
18You should have received a copy of the GNU General Public License
8c4c00c1 19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
897b77d6 21
897b77d6 22/* This file handles the generation of rtl code from tree structure
23 at the level of the function as a whole.
24 It creates the rtl expressions for parameters and auto variables
25 and has full responsibility for allocating stack slots.
26
27 `expand_function_start' is called at the beginning of a function,
28 before the function body is parsed, and `expand_function_end' is
29 called after parsing the body.
30
31 Call `assign_stack_local' to allocate a stack slot for a local variable.
32 This is usually done during the RTL generation for the function body,
33 but it can also be done in the reload pass when a pseudo-register does
e8825bb0 34 not get a hard register. */
897b77d6 35
36#include "config.h"
405711de 37#include "system.h"
805e22b2 38#include "coretypes.h"
39#include "tm.h"
897b77d6 40#include "rtl.h"
41#include "tree.h"
42#include "flags.h"
dcabb90e 43#include "except.h"
897b77d6 44#include "function.h"
897b77d6 45#include "expr.h"
530178a9 46#include "optabs.h"
d8fc4d0b 47#include "libfuncs.h"
897b77d6 48#include "regs.h"
49#include "hard-reg-set.h"
50#include "insn-config.h"
51#include "recog.h"
52#include "output.h"
b2c5602e 53#include "basic-block.h"
12874aaf 54#include "toplev.h"
1f3233d1 55#include "hashtab.h"
a7b0c170 56#include "ggc.h"
075136a2 57#include "tm_p.h"
fb0c0ea7 58#include "integrate.h"
96554925 59#include "langhooks.h"
45550790 60#include "target.h"
9bb8a4af 61#include "cfglayout.h"
75a70cf9 62#include "gimple.h"
77fce4cd 63#include "tree-pass.h"
f1a0edff 64#include "predict.h"
3072d30e 65#include "df.h"
66#include "timevar.h"
05f3df98 67#include "vecprim.h"
f1a0edff 68
c8a152f6 69/* So we can assign to cfun in this file. */
70#undef cfun
71
256f9b65 72#ifndef STACK_ALIGNMENT_NEEDED
73#define STACK_ALIGNMENT_NEEDED 1
74#endif
75
1cd50c9a 76#define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
77
90ed9d61 78/* Some systems use __main in a way incompatible with its use in gcc, in these
79 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
80 give the same symbol without quotes for an alternative entry point. You
a92771b8 81 must define both, or neither. */
90ed9d61 82#ifndef NAME__MAIN
83#define NAME__MAIN "__main"
90ed9d61 84#endif
85
897b77d6 86/* Round a value to the lowest integer less than it that is a multiple of
87 the required alignment. Avoid using division in case the value is
88 negative. Assume the alignment is a power of two. */
89#define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
90
91/* Similar, but round to the next highest integer that meets the
92 alignment. */
93#define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
94
b3b67b7c 95/* Nonzero if function being compiled doesn't contain any calls
96 (ignoring the prologue and epilogue). This is set prior to
97 local register allocation and is valid for the remaining
06ebc183 98 compiler passes. */
b3b67b7c 99int current_function_is_leaf;
100
22c8984a 101/* Nonzero if function being compiled doesn't modify the stack pointer
102 (ignoring the prologue and epilogue). This is only valid after
3072d30e 103 pass_stack_ptr_mod has run. */
22c8984a 104int current_function_sp_is_unchanging;
105
b3b67b7c 106/* Nonzero if the function being compiled is a leaf function which only
107 uses leaf registers. This is valid after reload (specifically after
108 sched2) and is useful only if the port defines LEAF_REGISTERS. */
b3b67b7c 109int current_function_uses_only_leaf_regs;
110
897b77d6 111/* Nonzero once virtual register instantiation has been done.
9c0a756f 112 assign_stack_local uses frame_pointer_rtx when this is nonzero.
113 calls.c:emit_library_call_value_1 uses it to set up
114 post-instantiation libcalls. */
115int virtuals_instantiated;
897b77d6 116
4781f9b9 117/* Assign unique numbers to labels generated for profiling, debugging, etc. */
573aba85 118static GTY(()) int funcdef_no;
b8a21949 119
ab5beff9 120/* These variables hold pointers to functions to create and destroy
121 target specific, per-function data structures. */
de1b648b 122struct machine_function * (*init_machine_status) (void);
adc2961c 123
304c5bf1 124/* The currently compiled function. */
08513b52 125struct function *cfun = 0;
304c5bf1 126
25e880b1 127/* These hashes record the prologue and epilogue insns. */
128static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
129 htab_t prologue_insn_hash;
130static GTY((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
131 htab_t epilogue_insn_hash;
897b77d6 132\f
1a4c44c5 133
134htab_t types_used_by_vars_hash = NULL;
135tree types_used_by_cur_var_decl = NULL;
136
209a68cc 137/* Forward declarations. */
138
de1b648b 139static struct temp_slot *find_temp_slot_from_address (rtx);
de1b648b 140static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
141static void pad_below (struct args_size *, enum machine_mode, tree);
573a97ad 142static void reorder_blocks_1 (rtx, tree, VEC(tree,heap) **);
de1b648b 143static int all_blocks (tree, tree *);
144static tree *get_block_vector (tree, int *);
145extern tree debug_find_var_in_block_tree (tree, tree);
4885b286 146/* We always define `record_insns' even if it's not used so that we
2dc40d2d 147 can always export `prologue_epilogue_contains'. */
25e880b1 148static void record_insns (rtx, rtx, htab_t *) ATTRIBUTE_UNUSED;
149static bool contains (const_rtx, htab_t);
2bb09e1e 150#ifdef HAVE_return
f2aa2401 151static void emit_return_into_block (basic_block);
2bb09e1e 152#endif
87d4aa85 153static void prepare_function_start (void);
de1b648b 154static void do_clobber_return_reg (rtx, void *);
155static void do_use_return_reg (rtx, void *);
76a2507e 156static void set_insn_locators (rtx, int) ATTRIBUTE_UNUSED;
8e4c05da 157\f
5737913a 158/* Stack of nested functions. */
159/* Keep track of the cfun stack. */
997d68fe 160
5737913a 161typedef struct function *function_p;
997d68fe 162
5737913a 163DEF_VEC_P(function_p);
164DEF_VEC_ALLOC_P(function_p,heap);
165static VEC(function_p,heap) *function_context_stack;
897b77d6 166
167/* Save the current context for compilation of a nested function.
d2764e2d 168 This is called from language-specific code. */
897b77d6 169
170void
d2764e2d 171push_function_context (void)
897b77d6 172{
08513b52 173 if (cfun == 0)
80f2ef47 174 allocate_struct_function (NULL, false);
304c5bf1 175
5737913a 176 VEC_safe_push (function_p, heap, function_context_stack, cfun);
87d4aa85 177 set_cfun (NULL);
897b77d6 178}
179
180/* Restore the last saved context, at the end of a nested function.
181 This function is called from language-specific code. */
182
183void
d2764e2d 184pop_function_context (void)
897b77d6 185{
5737913a 186 struct function *p = VEC_pop (function_p, function_context_stack);
87d4aa85 187 set_cfun (p);
897b77d6 188 current_function_decl = p->decl;
897b77d6 189
897b77d6 190 /* Reset variables that have known state during rtx generation. */
897b77d6 191 virtuals_instantiated = 0;
316bc009 192 generating_concat_p = 1;
897b77d6 193}
2a228d52 194
3c3bb268 195/* Clear out all parts of the state in F that can safely be discarded
196 after the function has been parsed, but not compiled, to let
197 garbage collection reclaim the memory. */
198
199void
de1b648b 200free_after_parsing (struct function *f)
3c3bb268 201{
b75409ba 202 f->language = 0;
3c3bb268 203}
204
26df1c5e 205/* Clear out all parts of the state in F that can safely be discarded
206 after the function has been compiled, to let garbage collection
a57bcb3b 207 reclaim the memory. */
c788feb1 208
26df1c5e 209void
de1b648b 210free_after_compilation (struct function *f)
26df1c5e 211{
25e880b1 212 prologue_insn_hash = NULL;
213 epilogue_insn_hash = NULL;
214
fd6ffb7c 215 if (crtl->emit.regno_pointer_align)
216 free (crtl->emit.regno_pointer_align);
a4a0e8fd 217
fd6ffb7c 218 memset (crtl, 0, sizeof (struct rtl_data));
1f3233d1 219 f->eh = NULL;
1f3233d1 220 f->machine = NULL;
7a22afab 221 f->cfg = NULL;
3c3bb268 222
a9f6414b 223 regno_reg_rtx = NULL;
8b3949ab 224 insn_locators_free ();
26df1c5e 225}
897b77d6 226\f
0a893c29 227/* Return size needed for stack frame based on slots so far allocated.
228 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
229 the caller may have to do that. */
26d04e5f 230
0a893c29 231HOST_WIDE_INT
de1b648b 232get_frame_size (void)
0a893c29 233{
b079a207 234 if (FRAME_GROWS_DOWNWARD)
235 return -frame_offset;
236 else
237 return frame_offset;
0a893c29 238}
239
26d04e5f 240/* Issue an error message and return TRUE if frame OFFSET overflows in
241 the signed target pointer arithmetics for function FUNC. Otherwise
242 return FALSE. */
243
244bool
245frame_offset_overflow (HOST_WIDE_INT offset, tree func)
48e1416a 246{
26d04e5f 247 unsigned HOST_WIDE_INT size = FRAME_GROWS_DOWNWARD ? -offset : offset;
248
249 if (size > ((unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (Pmode) - 1))
250 /* Leave room for the fixed part of the frame. */
251 - 64 * UNITS_PER_WORD)
252 {
712d2297 253 error_at (DECL_SOURCE_LOCATION (func),
254 "total size of local objects too large");
26d04e5f 255 return TRUE;
256 }
257
258 return FALSE;
259}
260
ad33891d 261/* Return stack slot alignment in bits for TYPE and MODE. */
262
263static unsigned int
264get_stack_local_alignment (tree type, enum machine_mode mode)
265{
266 unsigned int alignment;
267
268 if (mode == BLKmode)
269 alignment = BIGGEST_ALIGNMENT;
270 else
271 alignment = GET_MODE_ALIGNMENT (mode);
272
273 /* Allow the frond-end to (possibly) increase the alignment of this
274 stack slot. */
275 if (! type)
276 type = lang_hooks.types.type_for_mode (mode, 0);
277
278 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
279}
280
43165fe4 281/* Determine whether it is possible to fit a stack slot of size SIZE and
282 alignment ALIGNMENT into an area in the stack frame that starts at
283 frame offset START and has a length of LENGTH. If so, store the frame
284 offset to be used for the stack slot in *POFFSET and return true;
285 return false otherwise. This function will extend the frame size when
286 given a start/length pair that lies at the end of the frame. */
287
288static bool
289try_fit_stack_local (HOST_WIDE_INT start, HOST_WIDE_INT length,
290 HOST_WIDE_INT size, unsigned int alignment,
291 HOST_WIDE_INT *poffset)
292{
293 HOST_WIDE_INT this_frame_offset;
294 int frame_off, frame_alignment, frame_phase;
295
296 /* Calculate how many bytes the start of local variables is off from
297 stack alignment. */
298 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
299 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
300 frame_phase = frame_off ? frame_alignment - frame_off : 0;
301
302 /* Round the frame offset to the specified alignment. */
303
304 /* We must be careful here, since FRAME_OFFSET might be negative and
305 division with a negative dividend isn't as well defined as we might
306 like. So we instead assume that ALIGNMENT is a power of two and
307 use logical operations which are unambiguous. */
308 if (FRAME_GROWS_DOWNWARD)
309 this_frame_offset
310 = (FLOOR_ROUND (start + length - size - frame_phase,
311 (unsigned HOST_WIDE_INT) alignment)
312 + frame_phase);
313 else
314 this_frame_offset
315 = (CEIL_ROUND (start - frame_phase,
316 (unsigned HOST_WIDE_INT) alignment)
317 + frame_phase);
318
319 /* See if it fits. If this space is at the edge of the frame,
320 consider extending the frame to make it fit. Our caller relies on
321 this when allocating a new slot. */
322 if (frame_offset == start && this_frame_offset < frame_offset)
323 frame_offset = this_frame_offset;
324 else if (this_frame_offset < start)
325 return false;
326 else if (start + length == frame_offset
327 && this_frame_offset + size > start + length)
328 frame_offset = this_frame_offset + size;
329 else if (this_frame_offset + size > start + length)
330 return false;
331
332 *poffset = this_frame_offset;
333 return true;
334}
335
336/* Create a new frame_space structure describing free space in the stack
337 frame beginning at START and ending at END, and chain it into the
338 function's frame_space_list. */
339
340static void
341add_frame_space (HOST_WIDE_INT start, HOST_WIDE_INT end)
342{
343 struct frame_space *space = GGC_NEW (struct frame_space);
344 space->next = crtl->frame_space_list;
345 crtl->frame_space_list = space;
346 space->start = start;
347 space->length = end - start;
348}
349
897b77d6 350/* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
351 with machine mode MODE.
06ebc183 352
897b77d6 353 ALIGN controls the amount of alignment for the address of the slot:
354 0 means according to MODE,
355 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
c20b6803 356 -2 means use BITS_PER_UNIT,
897b77d6 357 positive specifies alignment boundary in bits.
358
27a7a23a 359 If REDUCE_ALIGNMENT_OK is true, it is OK to reduce alignment.
360
b079a207 361 We do not round to stack_boundary here. */
897b77d6 362
b079a207 363rtx
27a7a23a 364assign_stack_local_1 (enum machine_mode mode, HOST_WIDE_INT size,
365 int align,
366 bool reduce_alignment_ok ATTRIBUTE_UNUSED)
897b77d6 367{
19cb6b50 368 rtx x, addr;
897b77d6 369 int bigend_correction = 0;
286887d9 370 HOST_WIDE_INT slot_offset = 0, old_frame_offset;
ad33891d 371 unsigned int alignment, alignment_in_bits;
897b77d6 372
373 if (align == 0)
374 {
ad33891d 375 alignment = get_stack_local_alignment (NULL, mode);
9bd87fd2 376 alignment /= BITS_PER_UNIT;
897b77d6 377 }
378 else if (align == -1)
379 {
380 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
381 size = CEIL_ROUND (size, alignment);
382 }
c20b6803 383 else if (align == -2)
384 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
897b77d6 385 else
386 alignment = align / BITS_PER_UNIT;
387
27a7a23a 388 alignment_in_bits = alignment * BITS_PER_UNIT;
389
27a7a23a 390 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
391 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
392 {
393 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
394 alignment = alignment_in_bits / BITS_PER_UNIT;
395 }
a79cb8e9 396
27a7a23a 397 if (SUPPORTS_STACK_ALIGNMENT)
398 {
399 if (crtl->stack_alignment_estimated < alignment_in_bits)
400 {
401 if (!crtl->stack_realign_processed)
402 crtl->stack_alignment_estimated = alignment_in_bits;
403 else
404 {
405 /* If stack is realigned and stack alignment value
406 hasn't been finalized, it is OK not to increase
407 stack_alignment_estimated. The bigger alignment
408 requirement is recorded in stack_alignment_needed
409 below. */
410 gcc_assert (!crtl->stack_realign_finalized);
411 if (!crtl->stack_realign_needed)
412 {
413 /* It is OK to reduce the alignment as long as the
414 requested size is 0 or the estimated stack
415 alignment >= mode alignment. */
416 gcc_assert (reduce_alignment_ok
417 || size == 0
418 || (crtl->stack_alignment_estimated
419 >= GET_MODE_ALIGNMENT (mode)));
420 alignment_in_bits = crtl->stack_alignment_estimated;
421 alignment = alignment_in_bits / BITS_PER_UNIT;
422 }
423 }
424 }
425 }
ad33891d 426
427 if (crtl->stack_alignment_needed < alignment_in_bits)
428 crtl->stack_alignment_needed = alignment_in_bits;
bd9c33a8 429 if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
430 crtl->max_used_stack_slot_alignment = alignment_in_bits;
a79cb8e9 431
43165fe4 432 if (mode != BLKmode || size != 0)
433 {
434 struct frame_space **psp;
435
436 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
437 {
438 struct frame_space *space = *psp;
439 if (!try_fit_stack_local (space->start, space->length, size,
440 alignment, &slot_offset))
441 continue;
442 *psp = space->next;
443 if (slot_offset > space->start)
444 add_frame_space (space->start, slot_offset);
445 if (slot_offset + size < space->start + space->length)
446 add_frame_space (slot_offset + size,
447 space->start + space->length);
448 goto found_space;
449 }
450 }
451 else if (!STACK_ALIGNMENT_NEEDED)
452 {
453 slot_offset = frame_offset;
454 goto found_space;
455 }
456
457 old_frame_offset = frame_offset;
458
459 if (FRAME_GROWS_DOWNWARD)
460 {
461 frame_offset -= size;
462 try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
9f843b44 463
43165fe4 464 if (slot_offset > frame_offset)
465 add_frame_space (frame_offset, slot_offset);
466 if (slot_offset + size < old_frame_offset)
467 add_frame_space (slot_offset + size, old_frame_offset);
468 }
469 else
256f9b65 470 {
43165fe4 471 frame_offset += size;
472 try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
473
474 if (slot_offset > old_frame_offset)
475 add_frame_space (old_frame_offset, slot_offset);
476 if (slot_offset + size < frame_offset)
477 add_frame_space (slot_offset + size, frame_offset);
256f9b65 478 }
897b77d6 479
43165fe4 480 found_space:
897b77d6 481 /* On a big-endian machine, if we are allocating more space than we will use,
482 use the least significant bytes of those that are allocated. */
1c088911 483 if (BYTES_BIG_ENDIAN && mode != BLKmode && GET_MODE_SIZE (mode) < size)
897b77d6 484 bigend_correction = size - GET_MODE_SIZE (mode);
897b77d6 485
897b77d6 486 /* If we have already instantiated virtual registers, return the actual
487 address relative to the frame pointer. */
b079a207 488 if (virtuals_instantiated)
897b77d6 489 addr = plus_constant (frame_pointer_rtx,
eb21abb2 490 trunc_int_for_mode
43165fe4 491 (slot_offset + bigend_correction
eb21abb2 492 + STARTING_FRAME_OFFSET, Pmode));
897b77d6 493 else
494 addr = plus_constant (virtual_stack_vars_rtx,
eb21abb2 495 trunc_int_for_mode
43165fe4 496 (slot_offset + bigend_correction,
eb21abb2 497 Pmode));
897b77d6 498
941522d6 499 x = gen_rtx_MEM (mode, addr);
ad33891d 500 set_mem_align (x, alignment_in_bits);
43283c91 501 MEM_NOTRAP_P (x) = 1;
897b77d6 502
b079a207 503 stack_slot_list
504 = gen_rtx_EXPR_LIST (VOIDmode, x, stack_slot_list);
26df1c5e 505
b079a207 506 if (frame_offset_overflow (frame_offset, current_function_decl))
507 frame_offset = 0;
55abba5b 508
897b77d6 509 return x;
510}
27a7a23a 511
512/* Wrap up assign_stack_local_1 with last parameter as false. */
513
514rtx
515assign_stack_local (enum machine_mode mode, HOST_WIDE_INT size, int align)
516{
517 return assign_stack_local_1 (mode, size, align, false);
518}
a6629703 519\f
fef299ce 520\f
521/* In order to evaluate some expressions, such as function calls returning
522 structures in memory, we need to temporarily allocate stack locations.
523 We record each allocated temporary in the following structure.
524
525 Associated with each temporary slot is a nesting level. When we pop up
526 one level, all temporaries associated with the previous level are freed.
527 Normally, all temporaries are freed after the execution of the statement
528 in which they were created. However, if we are inside a ({...}) grouping,
529 the result may be in a temporary and hence must be preserved. If the
530 result could be in a temporary, we preserve it if we can determine which
531 one it is in. If we cannot determine which temporary may contain the
532 result, all temporaries are preserved. A temporary is preserved by
533 pretending it was allocated at the previous nesting level.
534
535 Automatic variables are also assigned temporary slots, at the nesting
536 level where they are defined. They are marked a "kept" so that
537 free_temp_slots will not free them. */
538
fb1e4f4a 539struct GTY(()) temp_slot {
fef299ce 540 /* Points to next temporary slot. */
541 struct temp_slot *next;
542 /* Points to previous temporary slot. */
543 struct temp_slot *prev;
544 /* The rtx to used to reference the slot. */
545 rtx slot;
fef299ce 546 /* The size, in units, of the slot. */
547 HOST_WIDE_INT size;
548 /* The type of the object in the slot, or zero if it doesn't correspond
549 to a type. We use this to determine whether a slot can be reused.
550 It can be reused if objects of the type of the new slot will always
551 conflict with objects of the type of the old slot. */
552 tree type;
0ac758f7 553 /* The alignment (in bits) of the slot. */
554 unsigned int align;
fef299ce 555 /* Nonzero if this temporary is currently in use. */
556 char in_use;
557 /* Nonzero if this temporary has its address taken. */
558 char addr_taken;
559 /* Nesting level at which this slot is being used. */
560 int level;
561 /* Nonzero if this should survive a call to free_temp_slots. */
562 int keep;
563 /* The offset of the slot from the frame_pointer, including extra space
564 for alignment. This info is for combine_temp_slots. */
565 HOST_WIDE_INT base_offset;
566 /* The size of the slot, including extra space for alignment. This
567 info is for combine_temp_slots. */
568 HOST_WIDE_INT full_size;
569};
570
571/* A table of addresses that represent a stack slot. The table is a mapping
572 from address RTXen to a temp slot. */
573static GTY((param_is(struct temp_slot_address_entry))) htab_t temp_slot_address_table;
574
575/* Entry for the above hash table. */
fb1e4f4a 576struct GTY(()) temp_slot_address_entry {
fef299ce 577 hashval_t hash;
578 rtx address;
579 struct temp_slot *temp_slot;
580};
581
a6629703 582/* Removes temporary slot TEMP from LIST. */
583
584static void
585cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
586{
587 if (temp->next)
588 temp->next->prev = temp->prev;
589 if (temp->prev)
590 temp->prev->next = temp->next;
591 else
592 *list = temp->next;
593
594 temp->prev = temp->next = NULL;
595}
596
597/* Inserts temporary slot TEMP to LIST. */
598
599static void
600insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
601{
602 temp->next = *list;
603 if (*list)
604 (*list)->prev = temp;
605 temp->prev = NULL;
606 *list = temp;
607}
608
609/* Returns the list of used temp slots at LEVEL. */
610
611static struct temp_slot **
612temp_slots_at_level (int level)
613{
84aacb40 614 if (level >= (int) VEC_length (temp_slot_p, used_temp_slots))
e85c2c2d 615 VEC_safe_grow_cleared (temp_slot_p, gc, used_temp_slots, level + 1);
a6629703 616
84aacb40 617 return &(VEC_address (temp_slot_p, used_temp_slots)[level]);
a6629703 618}
619
620/* Returns the maximal temporary slot level. */
621
622static int
623max_slot_level (void)
624{
625 if (!used_temp_slots)
626 return -1;
627
84aacb40 628 return VEC_length (temp_slot_p, used_temp_slots) - 1;
a6629703 629}
630
631/* Moves temporary slot TEMP to LEVEL. */
632
633static void
634move_slot_to_level (struct temp_slot *temp, int level)
635{
636 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
637 insert_slot_to_list (temp, temp_slots_at_level (level));
638 temp->level = level;
639}
640
641/* Make temporary slot TEMP available. */
642
643static void
644make_slot_available (struct temp_slot *temp)
645{
646 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
647 insert_slot_to_list (temp, &avail_temp_slots);
648 temp->in_use = 0;
649 temp->level = -1;
650}
fef299ce 651
652/* Compute the hash value for an address -> temp slot mapping.
653 The value is cached on the mapping entry. */
654static hashval_t
655temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
656{
657 int do_not_record = 0;
658 return hash_rtx (t->address, GET_MODE (t->address),
659 &do_not_record, NULL, false);
660}
661
662/* Return the hash value for an address -> temp slot mapping. */
663static hashval_t
664temp_slot_address_hash (const void *p)
665{
666 const struct temp_slot_address_entry *t;
667 t = (const struct temp_slot_address_entry *) p;
668 return t->hash;
669}
670
671/* Compare two address -> temp slot mapping entries. */
672static int
673temp_slot_address_eq (const void *p1, const void *p2)
674{
675 const struct temp_slot_address_entry *t1, *t2;
676 t1 = (const struct temp_slot_address_entry *) p1;
677 t2 = (const struct temp_slot_address_entry *) p2;
678 return exp_equiv_p (t1->address, t2->address, 0, true);
679}
680
681/* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
682static void
683insert_temp_slot_address (rtx address, struct temp_slot *temp_slot)
684{
685 void **slot;
686 struct temp_slot_address_entry *t = GGC_NEW (struct temp_slot_address_entry);
687 t->address = address;
688 t->temp_slot = temp_slot;
689 t->hash = temp_slot_address_compute_hash (t);
690 slot = htab_find_slot_with_hash (temp_slot_address_table, t, t->hash, INSERT);
691 *slot = t;
692}
693
694/* Remove an address -> temp slot mapping entry if the temp slot is
695 not in use anymore. Callback for remove_unused_temp_slot_addresses. */
696static int
697remove_unused_temp_slot_addresses_1 (void **slot, void *data ATTRIBUTE_UNUSED)
698{
699 const struct temp_slot_address_entry *t;
700 t = (const struct temp_slot_address_entry *) *slot;
701 if (! t->temp_slot->in_use)
702 *slot = NULL;
703 return 1;
704}
705
706/* Remove all mappings of addresses to unused temp slots. */
707static void
708remove_unused_temp_slot_addresses (void)
709{
710 htab_traverse (temp_slot_address_table,
711 remove_unused_temp_slot_addresses_1,
712 NULL);
713}
714
715/* Find the temp slot corresponding to the object at address X. */
716
717static struct temp_slot *
718find_temp_slot_from_address (rtx x)
719{
720 struct temp_slot *p;
721 struct temp_slot_address_entry tmp, *t;
722
723 /* First try the easy way:
724 See if X exists in the address -> temp slot mapping. */
725 tmp.address = x;
726 tmp.temp_slot = NULL;
727 tmp.hash = temp_slot_address_compute_hash (&tmp);
728 t = (struct temp_slot_address_entry *)
729 htab_find_with_hash (temp_slot_address_table, &tmp, tmp.hash);
730 if (t)
731 return t->temp_slot;
732
733 /* If we have a sum involving a register, see if it points to a temp
734 slot. */
735 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
736 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
737 return p;
738 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
739 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
740 return p;
741
742 /* Last resort: Address is a virtual stack var address. */
743 if (GET_CODE (x) == PLUS
744 && XEXP (x, 0) == virtual_stack_vars_rtx
971ba038 745 && CONST_INT_P (XEXP (x, 1)))
fef299ce 746 {
747 int i;
748 for (i = max_slot_level (); i >= 0; i--)
749 for (p = *temp_slots_at_level (i); p; p = p->next)
750 {
751 if (INTVAL (XEXP (x, 1)) >= p->base_offset
752 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size)
753 return p;
754 }
755 }
756
757 return NULL;
758}
897b77d6 759\f
760/* Allocate a temporary stack slot and record it for possible later
761 reuse.
762
763 MODE is the machine mode to be given to the returned rtx.
764
765 SIZE is the size in units of the space required. We do no rounding here
766 since assign_stack_local will do any required rounding.
767
6fd6341f 768 KEEP is 1 if this slot is to be retained after a call to
769 free_temp_slots. Automatic variables for a block are allocated
fcb807f8 770 with this flag. KEEP values of 2 or 3 were needed respectively
771 for variables whose lifetime is controlled by CLEANUP_POINT_EXPRs
89f18f73 772 or for SAVE_EXPRs, but they are now unused.
59241190 773
774 TYPE is the type that will be used for the stack slot. */
897b77d6 775
2b96c5f6 776rtx
89f18f73 777assign_stack_temp_for_type (enum machine_mode mode, HOST_WIDE_INT size,
778 int keep, tree type)
897b77d6 779{
d3e10bed 780 unsigned int align;
a6629703 781 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
84be287d 782 rtx slot;
897b77d6 783
babc13fa 784 /* If SIZE is -1 it means that somebody tried to allocate a temporary
785 of a variable size. */
fdada98f 786 gcc_assert (size != -1);
babc13fa 787
fcb807f8 788 /* These are now unused. */
fdada98f 789 gcc_assert (keep <= 1);
fcb807f8 790
ad33891d 791 align = get_stack_local_alignment (type, mode);
9bd87fd2 792
793 /* Try to find an available, already-allocated temporary of the proper
794 mode which meets the size and alignment requirements. Choose the
867eb367 795 smallest one with the closest alignment.
48e1416a 796
867eb367 797 If assign_stack_temp is called outside of the tree->rtl expansion,
798 we cannot reuse the stack slots (that may still refer to
799 VIRTUAL_STACK_VARS_REGNUM). */
800 if (!virtuals_instantiated)
a6629703 801 {
867eb367 802 for (p = avail_temp_slots; p; p = p->next)
a6629703 803 {
867eb367 804 if (p->align >= align && p->size >= size
805 && GET_MODE (p->slot) == mode
806 && objects_must_conflict_p (p->type, type)
807 && (best_p == 0 || best_p->size > p->size
808 || (best_p->size == p->size && best_p->align > p->align)))
a6629703 809 {
867eb367 810 if (p->align == align && p->size == size)
811 {
812 selected = p;
813 cut_slot_from_list (selected, &avail_temp_slots);
814 best_p = 0;
815 break;
816 }
817 best_p = p;
a6629703 818 }
a6629703 819 }
820 }
897b77d6 821
822 /* Make our best, if any, the one to use. */
823 if (best_p)
49d3d726 824 {
a6629703 825 selected = best_p;
826 cut_slot_from_list (selected, &avail_temp_slots);
827
49d3d726 828 /* If there are enough aligned bytes left over, make them into a new
829 temp_slot so that the extra bytes don't get wasted. Do this only
830 for BLKmode slots, so that we can be sure of the alignment. */
f7c44134 831 if (GET_MODE (best_p->slot) == BLKmode)
49d3d726 832 {
9bd87fd2 833 int alignment = best_p->align / BITS_PER_UNIT;
997d68fe 834 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
49d3d726 835
836 if (best_p->size - rounded_size >= alignment)
837 {
2457c754 838 p = GGC_NEW (struct temp_slot);
ce2403c3 839 p->in_use = p->addr_taken = 0;
49d3d726 840 p->size = best_p->size - rounded_size;
e8a637a3 841 p->base_offset = best_p->base_offset + rounded_size;
842 p->full_size = best_p->full_size - rounded_size;
43283c91 843 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
9bd87fd2 844 p->align = best_p->align;
387bc205 845 p->type = best_p->type;
a6629703 846 insert_slot_to_list (p, &avail_temp_slots);
49d3d726 847
941522d6 848 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
849 stack_slot_list);
49d3d726 850
851 best_p->size = rounded_size;
5ea3c815 852 best_p->full_size = rounded_size;
49d3d726 853 }
854 }
49d3d726 855 }
06ebc183 856
897b77d6 857 /* If we still didn't find one, make a new temporary. */
a6629703 858 if (selected == 0)
897b77d6 859 {
997d68fe 860 HOST_WIDE_INT frame_offset_old = frame_offset;
861
2457c754 862 p = GGC_NEW (struct temp_slot);
997d68fe 863
d61726bc 864 /* We are passing an explicit alignment request to assign_stack_local.
865 One side effect of that is assign_stack_local will not round SIZE
866 to ensure the frame offset remains suitably aligned.
867
868 So for requests which depended on the rounding of SIZE, we go ahead
869 and round it now. We also make sure ALIGNMENT is at least
870 BIGGEST_ALIGNMENT. */
fdada98f 871 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
ec7b5e18 872 p->slot = assign_stack_local (mode,
1d693c20 873 (mode == BLKmode
3473aefe 874 ? CEIL_ROUND (size, (int) align / BITS_PER_UNIT)
1d693c20 875 : size),
ec7b5e18 876 align);
9bd87fd2 877
878 p->align = align;
997d68fe 879
ef4d68c5 880 /* The following slot size computation is necessary because we don't
881 know the actual size of the temporary slot until assign_stack_local
882 has performed all the frame alignment and size rounding for the
d53be447 883 requested temporary. Note that extra space added for alignment
884 can be either above or below this stack slot depending on which
885 way the frame grows. We include the extra space if and only if it
886 is above this slot. */
d28d5017 887 if (FRAME_GROWS_DOWNWARD)
888 p->size = frame_offset_old - frame_offset;
889 else
890 p->size = size;
997d68fe 891
d53be447 892 /* Now define the fields used by combine_temp_slots. */
d28d5017 893 if (FRAME_GROWS_DOWNWARD)
894 {
895 p->base_offset = frame_offset;
896 p->full_size = frame_offset_old - frame_offset;
897 }
898 else
899 {
900 p->base_offset = frame_offset_old;
901 p->full_size = frame_offset - frame_offset_old;
902 }
a6629703 903
904 selected = p;
897b77d6 905 }
906
a6629703 907 p = selected;
897b77d6 908 p->in_use = 1;
ce2403c3 909 p->addr_taken = 0;
387bc205 910 p->type = type;
fcb807f8 911 p->level = temp_slot_level;
912 p->keep = keep;
21c867df 913
a6629703 914 pp = temp_slots_at_level (p->level);
915 insert_slot_to_list (p, pp);
fef299ce 916 insert_temp_slot_address (XEXP (p->slot, 0), p);
84be287d 917
918 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
919 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
920 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
f7c44134 921
387bc205 922 /* If we know the alias set for the memory that will be used, use
923 it. If there's no TYPE, then we don't know anything about the
924 alias set for the memory. */
84be287d 925 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
926 set_mem_align (slot, align);
387bc205 927
6312a35e 928 /* If a type is specified, set the relevant flags. */
f7c44134 929 if (type != 0)
387bc205 930 {
84be287d 931 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
cfefc966 932 MEM_SET_IN_STRUCT_P (slot, (AGGREGATE_TYPE_P (type)
933 || TREE_CODE (type) == COMPLEX_TYPE));
387bc205 934 }
43283c91 935 MEM_NOTRAP_P (slot) = 1;
f7c44134 936
84be287d 937 return slot;
897b77d6 938}
9bd87fd2 939
940/* Allocate a temporary stack slot and record it for possible later
941 reuse. First three arguments are same as in preceding function. */
942
943rtx
de1b648b 944assign_stack_temp (enum machine_mode mode, HOST_WIDE_INT size, int keep)
9bd87fd2 945{
946 return assign_stack_temp_for_type (mode, size, keep, NULL_TREE);
947}
ad6d0e80 948\f
567c22a9 949/* Assign a temporary.
950 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
951 and so that should be used in error messages. In either case, we
952 allocate of the given type.
9c457457 953 KEEP is as for assign_stack_temp.
954 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
30dd806d 955 it is 0 if a register is OK.
956 DONT_PROMOTE is 1 if we should not promote values in register
957 to wider modes. */
9c457457 958
959rtx
de1b648b 960assign_temp (tree type_or_decl, int keep, int memory_required,
961 int dont_promote ATTRIBUTE_UNUSED)
9c457457 962{
567c22a9 963 tree type, decl;
964 enum machine_mode mode;
7752d341 965#ifdef PROMOTE_MODE
567c22a9 966 int unsignedp;
967#endif
968
969 if (DECL_P (type_or_decl))
970 decl = type_or_decl, type = TREE_TYPE (decl);
971 else
972 decl = NULL, type = type_or_decl;
973
974 mode = TYPE_MODE (type);
7752d341 975#ifdef PROMOTE_MODE
78a8ed03 976 unsignedp = TYPE_UNSIGNED (type);
aeb6d7ef 977#endif
ad6d0e80 978
9c457457 979 if (mode == BLKmode || memory_required)
980 {
997d68fe 981 HOST_WIDE_INT size = int_size_in_bytes (type);
9c457457 982 rtx tmp;
983
779a20c8 984 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
985 problems with allocating the stack space. */
986 if (size == 0)
987 size = 1;
988
9c457457 989 /* Unfortunately, we don't yet know how to allocate variable-sized
150edb07 990 temporaries. However, sometimes we can find a fixed upper limit on
991 the size, so try that instead. */
992 else if (size == -1)
993 size = max_int_size_in_bytes (type);
8c3216ae 994
567c22a9 995 /* The size of the temporary may be too large to fit into an integer. */
996 /* ??? Not sure this should happen except for user silliness, so limit
60d903f5 997 this to things that aren't compiler-generated temporaries. The
89f18f73 998 rest of the time we'll die in assign_stack_temp_for_type. */
567c22a9 999 if (decl && size == -1
1000 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
1001 {
3cf8b391 1002 error ("size of variable %q+D is too large", decl);
567c22a9 1003 size = 1;
1004 }
1005
9bd87fd2 1006 tmp = assign_stack_temp_for_type (mode, size, keep, type);
9c457457 1007 return tmp;
1008 }
ad6d0e80 1009
7752d341 1010#ifdef PROMOTE_MODE
30dd806d 1011 if (! dont_promote)
3b2411a8 1012 mode = promote_mode (type, mode, &unsignedp);
9c457457 1013#endif
ad6d0e80 1014
9c457457 1015 return gen_reg_rtx (mode);
1016}
ad6d0e80 1017\f
49d3d726 1018/* Combine temporary stack slots which are adjacent on the stack.
1019
1020 This allows for better use of already allocated stack space. This is only
1021 done for BLKmode slots because we can be sure that we won't have alignment
1022 problems in this case. */
1023
3f0895d3 1024static void
de1b648b 1025combine_temp_slots (void)
49d3d726 1026{
a6629703 1027 struct temp_slot *p, *q, *next, *next_q;
997d68fe 1028 int num_slots;
1029
59241190 1030 /* We can't combine slots, because the information about which slot
1031 is in which alias set will be lost. */
1032 if (flag_strict_aliasing)
1033 return;
1034
06ebc183 1035 /* If there are a lot of temp slots, don't do anything unless
cb0ccc1e 1036 high levels of optimization. */
997d68fe 1037 if (! flag_expensive_optimizations)
a6629703 1038 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
997d68fe 1039 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1040 return;
49d3d726 1041
a6629703 1042 for (p = avail_temp_slots; p; p = next)
ccf0a5eb 1043 {
1044 int delete_p = 0;
997d68fe 1045
a6629703 1046 next = p->next;
1047
1048 if (GET_MODE (p->slot) != BLKmode)
1049 continue;
1050
1051 for (q = p->next; q; q = next_q)
ccf0a5eb 1052 {
a6629703 1053 int delete_q = 0;
1054
1055 next_q = q->next;
1056
1057 if (GET_MODE (q->slot) != BLKmode)
1058 continue;
1059
1060 if (p->base_offset + p->full_size == q->base_offset)
1061 {
1062 /* Q comes after P; combine Q into P. */
1063 p->size += q->size;
1064 p->full_size += q->full_size;
1065 delete_q = 1;
1066 }
1067 else if (q->base_offset + q->full_size == p->base_offset)
1068 {
1069 /* P comes after Q; combine P into Q. */
1070 q->size += p->size;
1071 q->full_size += p->full_size;
1072 delete_p = 1;
1073 break;
1074 }
1075 if (delete_q)
1076 cut_slot_from_list (q, &avail_temp_slots);
ccf0a5eb 1077 }
a6629703 1078
1079 /* Either delete P or advance past it. */
1080 if (delete_p)
1081 cut_slot_from_list (p, &avail_temp_slots);
ccf0a5eb 1082 }
49d3d726 1083}
897b77d6 1084\f
f4e36c33 1085/* Indicate that NEW_RTX is an alternate way of referring to the temp
1086 slot that previously was known by OLD_RTX. */
64e90dae 1087
1088void
f4e36c33 1089update_temp_slot_address (rtx old_rtx, rtx new_rtx)
64e90dae 1090{
155b05dc 1091 struct temp_slot *p;
64e90dae 1092
f4e36c33 1093 if (rtx_equal_p (old_rtx, new_rtx))
64e90dae 1094 return;
155b05dc 1095
f4e36c33 1096 p = find_temp_slot_from_address (old_rtx);
155b05dc 1097
f4e36c33 1098 /* If we didn't find one, see if both OLD_RTX is a PLUS. If so, and
1099 NEW_RTX is a register, see if one operand of the PLUS is a
1100 temporary location. If so, NEW_RTX points into it. Otherwise,
1101 if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1102 in common between them. If so, try a recursive call on those
1103 values. */
155b05dc 1104 if (p == 0)
1105 {
f4e36c33 1106 if (GET_CODE (old_rtx) != PLUS)
8911b943 1107 return;
1108
f4e36c33 1109 if (REG_P (new_rtx))
8911b943 1110 {
f4e36c33 1111 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1112 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
8911b943 1113 return;
1114 }
f4e36c33 1115 else if (GET_CODE (new_rtx) != PLUS)
155b05dc 1116 return;
1117
f4e36c33 1118 if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1119 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1120 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1121 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1122 else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1123 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1124 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1125 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
155b05dc 1126
1127 return;
1128 }
1129
06ebc183 1130 /* Otherwise add an alias for the temp's address. */
fef299ce 1131 insert_temp_slot_address (new_rtx, p);
64e90dae 1132}
1133
ce2403c3 1134/* If X could be a reference to a temporary slot, mark the fact that its
c3418f42 1135 address was taken. */
ce2403c3 1136
1137void
de1b648b 1138mark_temp_addr_taken (rtx x)
ce2403c3 1139{
1140 struct temp_slot *p;
1141
1142 if (x == 0)
1143 return;
1144
1145 /* If X is not in memory or is at a constant address, it cannot be in
1146 a temporary slot. */
e16ceb8e 1147 if (!MEM_P (x) || CONSTANT_P (XEXP (x, 0)))
ce2403c3 1148 return;
1149
1150 p = find_temp_slot_from_address (XEXP (x, 0));
1151 if (p != 0)
1152 p->addr_taken = 1;
1153}
1154
30f413ae 1155/* If X could be a reference to a temporary slot, mark that slot as
1156 belonging to the to one level higher than the current level. If X
1157 matched one of our slots, just mark that one. Otherwise, we can't
1158 easily predict which it is, so upgrade all of them. Kept slots
1159 need not be touched.
897b77d6 1160
1161 This is called when an ({...}) construct occurs and a statement
1162 returns a value in memory. */
1163
1164void
de1b648b 1165preserve_temp_slots (rtx x)
897b77d6 1166{
a6629703 1167 struct temp_slot *p = 0, *next;
897b77d6 1168
839e204f 1169 /* If there is no result, we still might have some objects whose address
1170 were taken, so we need to make sure they stay around. */
c7c7590a 1171 if (x == 0)
839e204f 1172 {
a6629703 1173 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1174 {
1175 next = p->next;
1176
1177 if (p->addr_taken)
1178 move_slot_to_level (p, temp_slot_level - 1);
1179 }
839e204f 1180
e8825bb0 1181 return;
1182 }
41969bd3 1183
e8825bb0 1184 /* If X is a register that is being used as a pointer, see if we have
1185 a temporary slot we know it points to. To be consistent with
1186 the code below, we really should preserve all non-kept slots
1187 if we can't find a match, but that seems to be much too costly. */
1188 if (REG_P (x) && REG_POINTER (x))
1189 p = find_temp_slot_from_address (x);
41969bd3 1190
e8825bb0 1191 /* If X is not in memory or is at a constant address, it cannot be in
1192 a temporary slot, but it can contain something whose address was
1193 taken. */
1194 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1195 {
1196 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1197 {
1198 next = p->next;
a9c66733 1199
e8825bb0 1200 if (p->addr_taken)
1201 move_slot_to_level (p, temp_slot_level - 1);
0dbd1c74 1202 }
7014838c 1203
e8825bb0 1204 return;
1205 }
1206
1207 /* First see if we can find a match. */
1208 if (p == 0)
1209 p = find_temp_slot_from_address (XEXP (x, 0));
1210
1211 if (p != 0)
1212 {
1213 /* Move everything at our level whose address was taken to our new
1214 level in case we used its address. */
1215 struct temp_slot *q;
1216
1217 if (p->level == temp_slot_level)
2d6d2426 1218 {
e8825bb0 1219 for (q = *temp_slots_at_level (temp_slot_level); q; q = next)
cb87449c 1220 {
e8825bb0 1221 next = q->next;
cb87449c 1222
e8825bb0 1223 if (p != q && q->addr_taken)
1224 move_slot_to_level (q, temp_slot_level - 1);
cb87449c 1225 }
e8825bb0 1226
1227 move_slot_to_level (p, temp_slot_level - 1);
1228 p->addr_taken = 0;
2d6d2426 1229 }
e8825bb0 1230 return;
41969bd3 1231 }
0dbd1c74 1232
e8825bb0 1233 /* Otherwise, preserve all non-kept slots at this level. */
1234 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
0dbd1c74 1235 {
e8825bb0 1236 next = p->next;
c925694c 1237
e8825bb0 1238 if (!p->keep)
1239 move_slot_to_level (p, temp_slot_level - 1);
1240 }
c925694c 1241}
1242
e8825bb0 1243/* Free all temporaries used so far. This is normally called at the
1244 end of generating code for a statement. */
c925694c 1245
e8825bb0 1246void
1247free_temp_slots (void)
c925694c 1248{
e8825bb0 1249 struct temp_slot *p, *next;
a4da9a83 1250 bool some_available = false;
c925694c 1251
e8825bb0 1252 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1253 {
1254 next = p->next;
c925694c 1255
e8825bb0 1256 if (!p->keep)
a4da9a83 1257 {
1258 make_slot_available (p);
1259 some_available = true;
1260 }
e8825bb0 1261 }
c925694c 1262
a4da9a83 1263 if (some_available)
1264 {
1265 remove_unused_temp_slot_addresses ();
1266 combine_temp_slots ();
1267 }
e8825bb0 1268}
c925694c 1269
e8825bb0 1270/* Push deeper into the nesting level for stack temporaries. */
c925694c 1271
e8825bb0 1272void
1273push_temp_slots (void)
c925694c 1274{
e8825bb0 1275 temp_slot_level++;
c925694c 1276}
1277
e8825bb0 1278/* Pop a temporary nesting level. All slots in use in the current level
1279 are freed. */
c925694c 1280
e8825bb0 1281void
1282pop_temp_slots (void)
c925694c 1283{
e8825bb0 1284 struct temp_slot *p, *next;
a4da9a83 1285 bool some_available = false;
c925694c 1286
e8825bb0 1287 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1288 {
1289 next = p->next;
1290 make_slot_available (p);
a4da9a83 1291 some_available = true;
e8825bb0 1292 }
0dbd1c74 1293
a4da9a83 1294 if (some_available)
1295 {
1296 remove_unused_temp_slot_addresses ();
1297 combine_temp_slots ();
1298 }
5ccb94d7 1299
e8825bb0 1300 temp_slot_level--;
bf5a43e2 1301}
1302
e8825bb0 1303/* Initialize temporary slots. */
0dbd1c74 1304
1305void
e8825bb0 1306init_temp_slots (void)
0dbd1c74 1307{
e8825bb0 1308 /* We have not allocated any temporaries yet. */
1309 avail_temp_slots = 0;
1310 used_temp_slots = 0;
1311 temp_slot_level = 0;
fef299ce 1312
1313 /* Set up the table to map addresses to temp slots. */
1314 if (! temp_slot_address_table)
1315 temp_slot_address_table = htab_create_ggc (32,
1316 temp_slot_address_hash,
1317 temp_slot_address_eq,
1318 NULL);
1319 else
1320 htab_empty (temp_slot_address_table);
e8825bb0 1321}
1322\f
1323/* These routines are responsible for converting virtual register references
1324 to the actual hard register references once RTL generation is complete.
06ebc183 1325
e8825bb0 1326 The following four variables are used for communication between the
1327 routines. They contain the offsets of the virtual registers from their
1328 respective hard registers. */
c925694c 1329
e8825bb0 1330static int in_arg_offset;
1331static int var_offset;
1332static int dynamic_offset;
1333static int out_arg_offset;
1334static int cfa_offset;
a8636638 1335
e8825bb0 1336/* In most machines, the stack pointer register is equivalent to the bottom
1337 of the stack. */
06ebc183 1338
e8825bb0 1339#ifndef STACK_POINTER_OFFSET
1340#define STACK_POINTER_OFFSET 0
1341#endif
bf5a43e2 1342
e8825bb0 1343/* If not defined, pick an appropriate default for the offset of dynamically
1344 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1345 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
c925694c 1346
e8825bb0 1347#ifndef STACK_DYNAMIC_OFFSET
a8636638 1348
e8825bb0 1349/* The bottom of the stack points to the actual arguments. If
1350 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1351 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1352 stack space for register parameters is not pushed by the caller, but
1353 rather part of the fixed stack areas and hence not included in
abe32cce 1354 `crtl->outgoing_args_size'. Nevertheless, we must allow
e8825bb0 1355 for it when allocating stack dynamic objects. */
a8636638 1356
63c68695 1357#if defined(REG_PARM_STACK_SPACE)
e8825bb0 1358#define STACK_DYNAMIC_OFFSET(FNDECL) \
1359((ACCUMULATE_OUTGOING_ARGS \
abe32cce 1360 ? (crtl->outgoing_args_size \
22c61100 1361 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1362 : REG_PARM_STACK_SPACE (FNDECL))) \
63c68695 1363 : 0) + (STACK_POINTER_OFFSET))
e8825bb0 1364#else
1365#define STACK_DYNAMIC_OFFSET(FNDECL) \
abe32cce 1366((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0) \
e8825bb0 1367 + (STACK_POINTER_OFFSET))
1368#endif
1369#endif
f678883b 1370
e3d5af87 1371\f
f15c4004 1372/* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1373 is a virtual register, return the equivalent hard register and set the
1374 offset indirectly through the pointer. Otherwise, return 0. */
897b77d6 1375
f15c4004 1376static rtx
1377instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
897b77d6 1378{
f4e36c33 1379 rtx new_rtx;
f15c4004 1380 HOST_WIDE_INT offset;
897b77d6 1381
f15c4004 1382 if (x == virtual_incoming_args_rtx)
27a7a23a 1383 {
f6754469 1384 if (stack_realign_drap)
27a7a23a 1385 {
f6754469 1386 /* Replace virtual_incoming_args_rtx with internal arg
1387 pointer if DRAP is used to realign stack. */
f4e36c33 1388 new_rtx = crtl->args.internal_arg_pointer;
27a7a23a 1389 offset = 0;
1390 }
1391 else
f4e36c33 1392 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
27a7a23a 1393 }
f15c4004 1394 else if (x == virtual_stack_vars_rtx)
f4e36c33 1395 new_rtx = frame_pointer_rtx, offset = var_offset;
f15c4004 1396 else if (x == virtual_stack_dynamic_rtx)
f4e36c33 1397 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
f15c4004 1398 else if (x == virtual_outgoing_args_rtx)
f4e36c33 1399 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
f15c4004 1400 else if (x == virtual_cfa_rtx)
da72c083 1401 {
1402#ifdef FRAME_POINTER_CFA_OFFSET
f4e36c33 1403 new_rtx = frame_pointer_rtx;
da72c083 1404#else
f4e36c33 1405 new_rtx = arg_pointer_rtx;
da72c083 1406#endif
1407 offset = cfa_offset;
1408 }
f15c4004 1409 else
1410 return NULL_RTX;
897b77d6 1411
f15c4004 1412 *poffset = offset;
f4e36c33 1413 return new_rtx;
897b77d6 1414}
1415
f15c4004 1416/* A subroutine of instantiate_virtual_regs, called via for_each_rtx.
1417 Instantiate any virtual registers present inside of *LOC. The expression
1418 is simplified, as much as possible, but is not to be considered "valid"
1419 in any sense implied by the target. If any change is made, set CHANGED
1420 to true. */
897b77d6 1421
f15c4004 1422static int
1423instantiate_virtual_regs_in_rtx (rtx *loc, void *data)
897b77d6 1424{
f15c4004 1425 HOST_WIDE_INT offset;
1426 bool *changed = (bool *) data;
f4e36c33 1427 rtx x, new_rtx;
897b77d6 1428
f15c4004 1429 x = *loc;
1430 if (x == 0)
1431 return 0;
1432
1433 switch (GET_CODE (x))
897b77d6 1434 {
f15c4004 1435 case REG:
f4e36c33 1436 new_rtx = instantiate_new_reg (x, &offset);
1437 if (new_rtx)
f15c4004 1438 {
f4e36c33 1439 *loc = plus_constant (new_rtx, offset);
f15c4004 1440 if (changed)
1441 *changed = true;
1442 }
1443 return -1;
1444
1445 case PLUS:
f4e36c33 1446 new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1447 if (new_rtx)
f15c4004 1448 {
f4e36c33 1449 new_rtx = plus_constant (new_rtx, offset);
1450 *loc = simplify_gen_binary (PLUS, GET_MODE (x), new_rtx, XEXP (x, 1));
f15c4004 1451 if (changed)
1452 *changed = true;
1453 return -1;
1454 }
997d68fe 1455
f15c4004 1456 /* FIXME -- from old code */
1457 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1458 we can commute the PLUS and SUBREG because pointers into the
1459 frame are well-behaved. */
1460 break;
5970b26a 1461
f15c4004 1462 default:
1463 break;
897b77d6 1464 }
1465
f15c4004 1466 return 0;
897b77d6 1467}
1468
f15c4004 1469/* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1470 matches the predicate for insn CODE operand OPERAND. */
897b77d6 1471
f15c4004 1472static int
1473safe_insn_predicate (int code, int operand, rtx x)
897b77d6 1474{
f15c4004 1475 const struct insn_operand_data *op_data;
897b77d6 1476
f15c4004 1477 if (code < 0)
1478 return true;
897b77d6 1479
f15c4004 1480 op_data = &insn_data[code].operand[operand];
1481 if (op_data->predicate == NULL)
1482 return true;
6d0423b8 1483
f15c4004 1484 return op_data->predicate (x, op_data->mode);
1485}
6d0423b8 1486
f15c4004 1487/* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1488 registers present inside of insn. The result will be a valid insn. */
6d0423b8 1489
1490static void
f15c4004 1491instantiate_virtual_regs_in_insn (rtx insn)
6d0423b8 1492{
f15c4004 1493 HOST_WIDE_INT offset;
1494 int insn_code, i;
27ca6129 1495 bool any_change = false;
f4e36c33 1496 rtx set, new_rtx, x, seq;
00dfb616 1497
f15c4004 1498 /* There are some special cases to be handled first. */
1499 set = single_set (insn);
1500 if (set)
00dfb616 1501 {
f15c4004 1502 /* We're allowed to assign to a virtual register. This is interpreted
1503 to mean that the underlying register gets assigned the inverse
1504 transformation. This is used, for example, in the handling of
1505 non-local gotos. */
f4e36c33 1506 new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1507 if (new_rtx)
f15c4004 1508 {
1509 start_sequence ();
00dfb616 1510
f15c4004 1511 for_each_rtx (&SET_SRC (set), instantiate_virtual_regs_in_rtx, NULL);
f4e36c33 1512 x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
f15c4004 1513 GEN_INT (-offset));
f4e36c33 1514 x = force_operand (x, new_rtx);
1515 if (x != new_rtx)
1516 emit_move_insn (new_rtx, x);
6d0423b8 1517
f15c4004 1518 seq = get_insns ();
1519 end_sequence ();
6d0423b8 1520
f15c4004 1521 emit_insn_before (seq, insn);
1522 delete_insn (insn);
1523 return;
1524 }
6d0423b8 1525
f15c4004 1526 /* Handle a straight copy from a virtual register by generating a
1527 new add insn. The difference between this and falling through
1528 to the generic case is avoiding a new pseudo and eliminating a
1529 move insn in the initial rtl stream. */
f4e36c33 1530 new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1531 if (new_rtx && offset != 0
f15c4004 1532 && REG_P (SET_DEST (set))
1533 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1534 {
1535 start_sequence ();
6d0423b8 1536
f15c4004 1537 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS,
f4e36c33 1538 new_rtx, GEN_INT (offset), SET_DEST (set),
f15c4004 1539 1, OPTAB_LIB_WIDEN);
1540 if (x != SET_DEST (set))
1541 emit_move_insn (SET_DEST (set), x);
02e7a332 1542
f15c4004 1543 seq = get_insns ();
1544 end_sequence ();
e3f529ab 1545
f15c4004 1546 emit_insn_before (seq, insn);
1547 delete_insn (insn);
e3f529ab 1548 return;
f15c4004 1549 }
6d0423b8 1550
f15c4004 1551 extract_insn (insn);
27ca6129 1552 insn_code = INSN_CODE (insn);
6d0423b8 1553
f15c4004 1554 /* Handle a plus involving a virtual register by determining if the
1555 operands remain valid if they're modified in place. */
1556 if (GET_CODE (SET_SRC (set)) == PLUS
1557 && recog_data.n_operands >= 3
1558 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1559 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
971ba038 1560 && CONST_INT_P (recog_data.operand[2])
f4e36c33 1561 && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
f15c4004 1562 {
1563 offset += INTVAL (recog_data.operand[2]);
6d0423b8 1564
f15c4004 1565 /* If the sum is zero, then replace with a plain move. */
27ca6129 1566 if (offset == 0
1567 && REG_P (SET_DEST (set))
1568 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
f15c4004 1569 {
1570 start_sequence ();
f4e36c33 1571 emit_move_insn (SET_DEST (set), new_rtx);
f15c4004 1572 seq = get_insns ();
1573 end_sequence ();
bc17f7a4 1574
f15c4004 1575 emit_insn_before (seq, insn);
1576 delete_insn (insn);
1577 return;
1578 }
bc17f7a4 1579
f15c4004 1580 x = gen_int_mode (offset, recog_data.operand_mode[2]);
f15c4004 1581
1582 /* Using validate_change and apply_change_group here leaves
1583 recog_data in an invalid state. Since we know exactly what
1584 we want to check, do those two by hand. */
f4e36c33 1585 if (safe_insn_predicate (insn_code, 1, new_rtx)
f15c4004 1586 && safe_insn_predicate (insn_code, 2, x))
1587 {
f4e36c33 1588 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
f15c4004 1589 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1590 any_change = true;
27ca6129 1591
1592 /* Fall through into the regular operand fixup loop in
1593 order to take care of operands other than 1 and 2. */
f15c4004 1594 }
1595 }
1596 }
bc17f7a4 1597 else
27ca6129 1598 {
1599 extract_insn (insn);
1600 insn_code = INSN_CODE (insn);
1601 }
dd79abfb 1602
f15c4004 1603 /* In the general case, we expect virtual registers to appear only in
1604 operands, and then only as either bare registers or inside memories. */
1605 for (i = 0; i < recog_data.n_operands; ++i)
1606 {
1607 x = recog_data.operand[i];
1608 switch (GET_CODE (x))
1609 {
1610 case MEM:
1611 {
1612 rtx addr = XEXP (x, 0);
1613 bool changed = false;
1614
1615 for_each_rtx (&addr, instantiate_virtual_regs_in_rtx, &changed);
1616 if (!changed)
1617 continue;
1618
1619 start_sequence ();
1620 x = replace_equiv_address (x, addr);
7e507322 1621 /* It may happen that the address with the virtual reg
1622 was valid (e.g. based on the virtual stack reg, which might
1623 be acceptable to the predicates with all offsets), whereas
1624 the address now isn't anymore, for instance when the address
1625 is still offsetted, but the base reg isn't virtual-stack-reg
1626 anymore. Below we would do a force_reg on the whole operand,
1627 but this insn might actually only accept memory. Hence,
1628 before doing that last resort, try to reload the address into
1629 a register, so this operand stays a MEM. */
1630 if (!safe_insn_predicate (insn_code, i, x))
1631 {
1632 addr = force_reg (GET_MODE (addr), addr);
1633 x = replace_equiv_address (x, addr);
1634 }
f15c4004 1635 seq = get_insns ();
1636 end_sequence ();
1637 if (seq)
1638 emit_insn_before (seq, insn);
1639 }
1640 break;
1641
1642 case REG:
f4e36c33 1643 new_rtx = instantiate_new_reg (x, &offset);
1644 if (new_rtx == NULL)
f15c4004 1645 continue;
1646 if (offset == 0)
f4e36c33 1647 x = new_rtx;
f15c4004 1648 else
1649 {
1650 start_sequence ();
897b77d6 1651
f15c4004 1652 /* Careful, special mode predicates may have stuff in
1653 insn_data[insn_code].operand[i].mode that isn't useful
1654 to us for computing a new value. */
1655 /* ??? Recognize address_operand and/or "p" constraints
1656 to see if (plus new offset) is a valid before we put
1657 this through expand_simple_binop. */
f4e36c33 1658 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
f15c4004 1659 GEN_INT (offset), NULL_RTX,
1660 1, OPTAB_LIB_WIDEN);
1661 seq = get_insns ();
1662 end_sequence ();
1663 emit_insn_before (seq, insn);
1664 }
1665 break;
897b77d6 1666
f15c4004 1667 case SUBREG:
f4e36c33 1668 new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1669 if (new_rtx == NULL)
f15c4004 1670 continue;
1671 if (offset != 0)
1672 {
1673 start_sequence ();
f4e36c33 1674 new_rtx = expand_simple_binop (GET_MODE (new_rtx), PLUS, new_rtx,
f15c4004 1675 GEN_INT (offset), NULL_RTX,
1676 1, OPTAB_LIB_WIDEN);
1677 seq = get_insns ();
1678 end_sequence ();
1679 emit_insn_before (seq, insn);
1680 }
f4e36c33 1681 x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1682 GET_MODE (new_rtx), SUBREG_BYTE (x));
024f0a8a 1683 gcc_assert (x);
f15c4004 1684 break;
897b77d6 1685
f15c4004 1686 default:
1687 continue;
1688 }
897b77d6 1689
f15c4004 1690 /* At this point, X contains the new value for the operand.
1691 Validate the new value vs the insn predicate. Note that
1692 asm insns will have insn_code -1 here. */
1693 if (!safe_insn_predicate (insn_code, i, x))
c5159852 1694 {
1695 start_sequence ();
83b6c9db 1696 if (REG_P (x))
1697 {
1698 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1699 x = copy_to_reg (x);
1700 }
1701 else
1702 x = force_reg (insn_data[insn_code].operand[i].mode, x);
c5159852 1703 seq = get_insns ();
1704 end_sequence ();
1705 if (seq)
1706 emit_insn_before (seq, insn);
1707 }
897b77d6 1708
f15c4004 1709 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1710 any_change = true;
1711 }
897b77d6 1712
f15c4004 1713 if (any_change)
1714 {
1715 /* Propagate operand changes into the duplicates. */
1716 for (i = 0; i < recog_data.n_dups; ++i)
1717 *recog_data.dup_loc[i]
cdf37bc1 1718 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
dd79abfb 1719
f15c4004 1720 /* Force re-recognition of the instruction for validation. */
1721 INSN_CODE (insn) = -1;
1722 }
897b77d6 1723
f15c4004 1724 if (asm_noperands (PATTERN (insn)) >= 0)
897b77d6 1725 {
f15c4004 1726 if (!check_asm_operands (PATTERN (insn)))
897b77d6 1727 {
f15c4004 1728 error_for_asm (insn, "impossible constraint in %<asm%>");
1729 delete_insn (insn);
1730 }
1731 }
1732 else
1733 {
1734 if (recog_memoized (insn) < 0)
1735 fatal_insn_not_found (insn);
1736 }
1737}
155b05dc 1738
f15c4004 1739/* Subroutine of instantiate_decls. Given RTL representing a decl,
1740 do any instantiation required. */
155b05dc 1741
bc5e6ea1 1742void
1743instantiate_decl_rtl (rtx x)
f15c4004 1744{
1745 rtx addr;
897b77d6 1746
f15c4004 1747 if (x == 0)
1748 return;
897b77d6 1749
f15c4004 1750 /* If this is a CONCAT, recurse for the pieces. */
1751 if (GET_CODE (x) == CONCAT)
1752 {
bc5e6ea1 1753 instantiate_decl_rtl (XEXP (x, 0));
1754 instantiate_decl_rtl (XEXP (x, 1));
f15c4004 1755 return;
1756 }
897b77d6 1757
f15c4004 1758 /* If this is not a MEM, no need to do anything. Similarly if the
1759 address is a constant or a register that is not a virtual register. */
1760 if (!MEM_P (x))
1761 return;
897b77d6 1762
f15c4004 1763 addr = XEXP (x, 0);
1764 if (CONSTANT_P (addr)
1765 || (REG_P (addr)
1766 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1767 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1768 return;
897b77d6 1769
f15c4004 1770 for_each_rtx (&XEXP (x, 0), instantiate_virtual_regs_in_rtx, NULL);
1771}
897b77d6 1772
9338678e 1773/* Helper for instantiate_decls called via walk_tree: Process all decls
1774 in the given DECL_VALUE_EXPR. */
1775
1776static tree
1777instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1778{
1779 tree t = *tp;
75a70cf9 1780 if (! EXPR_P (t))
9338678e 1781 {
1782 *walk_subtrees = 0;
1783 if (DECL_P (t) && DECL_RTL_SET_P (t))
bc5e6ea1 1784 instantiate_decl_rtl (DECL_RTL (t));
9338678e 1785 }
1786 return NULL;
1787}
1788
f15c4004 1789/* Subroutine of instantiate_decls: Process all decls in the given
1790 BLOCK node and all its subblocks. */
897b77d6 1791
f15c4004 1792static void
1793instantiate_decls_1 (tree let)
1794{
1795 tree t;
897b77d6 1796
f15c4004 1797 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
9338678e 1798 {
1799 if (DECL_RTL_SET_P (t))
bc5e6ea1 1800 instantiate_decl_rtl (DECL_RTL (t));
9338678e 1801 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t))
1802 {
1803 tree v = DECL_VALUE_EXPR (t);
1804 walk_tree (&v, instantiate_expr, NULL, NULL);
1805 }
1806 }
897b77d6 1807
f15c4004 1808 /* Process all subblocks. */
93110716 1809 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
f15c4004 1810 instantiate_decls_1 (t);
1811}
897b77d6 1812
f15c4004 1813/* Scan all decls in FNDECL (both variables and parameters) and instantiate
1814 all virtual registers in their DECL_RTL's. */
897b77d6 1815
f15c4004 1816static void
1817instantiate_decls (tree fndecl)
1818{
78fa9ba7 1819 tree decl, t, next;
897b77d6 1820
f15c4004 1821 /* Process all parameters of the function. */
1822 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
1823 {
bc5e6ea1 1824 instantiate_decl_rtl (DECL_RTL (decl));
1825 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
9338678e 1826 if (DECL_HAS_VALUE_EXPR_P (decl))
1827 {
1828 tree v = DECL_VALUE_EXPR (decl);
1829 walk_tree (&v, instantiate_expr, NULL, NULL);
1830 }
f15c4004 1831 }
a51c8974 1832
f15c4004 1833 /* Now process all variables defined in the function or its subblocks. */
1834 instantiate_decls_1 (DECL_INITIAL (fndecl));
78fa9ba7 1835
1836 t = cfun->local_decls;
1837 cfun->local_decls = NULL_TREE;
1838 for (; t; t = next)
1839 {
1840 next = TREE_CHAIN (t);
1841 decl = TREE_VALUE (t);
1842 if (DECL_RTL_SET_P (decl))
1843 instantiate_decl_rtl (DECL_RTL (decl));
1844 ggc_free (t);
1845 }
f15c4004 1846}
897b77d6 1847
f15c4004 1848/* Pass through the INSNS of function FNDECL and convert virtual register
1849 references to hard register references. */
897b77d6 1850
2a1990e9 1851static unsigned int
f15c4004 1852instantiate_virtual_regs (void)
1853{
201f6961 1854 rtx insn;
897b77d6 1855
f15c4004 1856 /* Compute the offsets to use for this function. */
1857 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1858 var_offset = STARTING_FRAME_OFFSET;
1859 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1860 out_arg_offset = STACK_POINTER_OFFSET;
da72c083 1861#ifdef FRAME_POINTER_CFA_OFFSET
1862 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1863#else
f15c4004 1864 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
da72c083 1865#endif
0dbd1c74 1866
f15c4004 1867 /* Initialize recognition, indicating that volatile is OK. */
1868 init_recog ();
897b77d6 1869
f15c4004 1870 /* Scan through all the insns, instantiating every virtual register still
1871 present. */
201f6961 1872 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1873 if (INSN_P (insn))
1874 {
1875 /* These patterns in the instruction stream can never be recognized.
1876 Fortunately, they shouldn't contain virtual registers either. */
1877 if (GET_CODE (PATTERN (insn)) == USE
1878 || GET_CODE (PATTERN (insn)) == CLOBBER
1879 || GET_CODE (PATTERN (insn)) == ADDR_VEC
1880 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
1881 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
1882 continue;
9845d120 1883 else if (DEBUG_INSN_P (insn))
1884 for_each_rtx (&INSN_VAR_LOCATION (insn),
1885 instantiate_virtual_regs_in_rtx, NULL);
1886 else
1887 instantiate_virtual_regs_in_insn (insn);
201f6961 1888
1889 if (INSN_DELETED_P (insn))
1890 continue;
1891
1892 for_each_rtx (&REG_NOTES (insn), instantiate_virtual_regs_in_rtx, NULL);
1893
1894 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
971ba038 1895 if (CALL_P (insn))
201f6961 1896 for_each_rtx (&CALL_INSN_FUNCTION_USAGE (insn),
1897 instantiate_virtual_regs_in_rtx, NULL);
1898 }
897b77d6 1899
f15c4004 1900 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
1901 instantiate_decls (current_function_decl);
1902
bc5e6ea1 1903 targetm.instantiate_decls ();
1904
f15c4004 1905 /* Indicate that, from now on, assign_stack_local should use
1906 frame_pointer_rtx. */
1907 virtuals_instantiated = 1;
2a1990e9 1908 return 0;
897b77d6 1909}
77fce4cd 1910
20099e35 1911struct rtl_opt_pass pass_instantiate_virtual_regs =
77fce4cd 1912{
20099e35 1913 {
1914 RTL_PASS,
228967a9 1915 "vregs", /* name */
77fce4cd 1916 NULL, /* gate */
1917 instantiate_virtual_regs, /* execute */
1918 NULL, /* sub */
1919 NULL, /* next */
1920 0, /* static_pass_number */
0b1615c1 1921 TV_NONE, /* tv_id */
77fce4cd 1922 0, /* properties_required */
1923 0, /* properties_provided */
1924 0, /* properties_destroyed */
1925 0, /* todo_flags_start */
20099e35 1926 TODO_dump_func /* todo_flags_finish */
1927 }
77fce4cd 1928};
1929
897b77d6 1930\f
8f48fc81 1931/* Return 1 if EXP is an aggregate type (or a value with aggregate type).
1932 This means a type for which function calls must pass an address to the
1933 function or get an address back from the function.
1934 EXP may be a type node or an expression (whose type is tested). */
897b77d6 1935
1936int
fb80456a 1937aggregate_value_p (const_tree exp, const_tree fntype)
897b77d6 1938{
4cd5bb61 1939 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
d5c7cfd2 1940 int i, regno, nregs;
1941 rtx reg;
9308e976 1942
45550790 1943 if (fntype)
1944 switch (TREE_CODE (fntype))
1945 {
1946 case CALL_EXPR:
4cd5bb61 1947 {
1948 tree fndecl = get_callee_fndecl (fntype);
1949 fntype = (fndecl
1950 ? TREE_TYPE (fndecl)
1951 : TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype))));
1952 }
45550790 1953 break;
1954 case FUNCTION_DECL:
4cd5bb61 1955 fntype = TREE_TYPE (fntype);
45550790 1956 break;
1957 case FUNCTION_TYPE:
1958 case METHOD_TYPE:
1959 break;
1960 case IDENTIFIER_NODE:
4cd5bb61 1961 fntype = NULL_TREE;
45550790 1962 break;
1963 default:
4cd5bb61 1964 /* We don't expect other tree types here. */
fdada98f 1965 gcc_unreachable ();
45550790 1966 }
1967
4cd5bb61 1968 if (VOID_TYPE_P (type))
2c8db4fe 1969 return 0;
6f18455e 1970
8df5a43d 1971 /* If a record should be passed the same as its first (and only) member
1972 don't pass it as an aggregate. */
1973 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
1974 return aggregate_value_p (first_field (type), fntype);
1975
806e4c12 1976 /* If the front end has decided that this needs to be passed by
1977 reference, do so. */
1978 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
1979 && DECL_BY_REFERENCE (exp))
1980 return 1;
6f18455e 1981
4cd5bb61 1982 /* Function types that are TREE_ADDRESSABLE force return in memory. */
1983 if (fntype && TREE_ADDRESSABLE (fntype))
6f18455e 1984 return 1;
48e1416a 1985
ad87de1e 1986 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
79f1a380 1987 and thus can't be returned in registers. */
1988 if (TREE_ADDRESSABLE (type))
1989 return 1;
4cd5bb61 1990
727a13df 1991 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
897b77d6 1992 return 1;
4cd5bb61 1993
1994 if (targetm.calls.return_in_memory (type, fntype))
1995 return 1;
1996
d5c7cfd2 1997 /* Make sure we have suitable call-clobbered regs to return
1998 the value in; if not, we must return it in memory. */
46b3ff29 1999 reg = hard_function_value (type, 0, fntype, 0);
84d69b33 2000
2001 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2002 it is OK. */
8ad4c111 2003 if (!REG_P (reg))
84d69b33 2004 return 0;
2005
d5c7cfd2 2006 regno = REGNO (reg);
67d6c12b 2007 nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
d5c7cfd2 2008 for (i = 0; i < nregs; i++)
2009 if (! call_used_regs[regno + i])
2010 return 1;
4cd5bb61 2011
897b77d6 2012 return 0;
2013}
2014\f
e8825bb0 2015/* Return true if we should assign DECL a pseudo register; false if it
2016 should live on the local stack. */
2017
2018bool
b7bf20db 2019use_register_for_decl (const_tree decl)
e8825bb0 2020{
658e203c 2021 if (!targetm.calls.allocate_stack_slots_for_args())
2022 return true;
48e1416a 2023
e8825bb0 2024 /* Honor volatile. */
2025 if (TREE_SIDE_EFFECTS (decl))
2026 return false;
2027
2028 /* Honor addressability. */
2029 if (TREE_ADDRESSABLE (decl))
2030 return false;
2031
2032 /* Only register-like things go in registers. */
2033 if (DECL_MODE (decl) == BLKmode)
2034 return false;
2035
2036 /* If -ffloat-store specified, don't put explicit float variables
2037 into registers. */
2038 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2039 propagates values across these stores, and it probably shouldn't. */
2040 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2041 return false;
2042
553acd9c 2043 /* If we're not interested in tracking debugging information for
2044 this decl, then we can certainly put it in a register. */
2045 if (DECL_IGNORED_P (decl))
e8825bb0 2046 return true;
2047
f24ccada 2048 if (optimize)
2049 return true;
2050
2051 if (!DECL_REGISTER (decl))
2052 return false;
2053
2054 switch (TREE_CODE (TREE_TYPE (decl)))
2055 {
2056 case RECORD_TYPE:
2057 case UNION_TYPE:
2058 case QUAL_UNION_TYPE:
2059 /* When not optimizing, disregard register keyword for variables with
2060 types containing methods, otherwise the methods won't be callable
2061 from the debugger. */
2062 if (TYPE_METHODS (TREE_TYPE (decl)))
2063 return false;
2064 break;
2065 default:
2066 break;
2067 }
2068
2069 return true;
e8825bb0 2070}
2071
cc9b8628 2072/* Return true if TYPE should be passed by invisible reference. */
2073
2074bool
b981d932 2075pass_by_reference (CUMULATIVE_ARGS *ca, enum machine_mode mode,
2076 tree type, bool named_arg)
cc9b8628 2077{
2078 if (type)
2079 {
2080 /* If this type contains non-trivial constructors, then it is
2081 forbidden for the middle-end to create any new copies. */
2082 if (TREE_ADDRESSABLE (type))
2083 return true;
2084
39cd001a 2085 /* GCC post 3.4 passes *all* variable sized types by reference. */
2086 if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
cc9b8628 2087 return true;
8df5a43d 2088
2089 /* If a record type should be passed the same as its first (and only)
2090 member, use the type and mode of that member. */
2091 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2092 {
2093 type = TREE_TYPE (first_field (type));
2094 mode = TYPE_MODE (type);
2095 }
cc9b8628 2096 }
2097
b981d932 2098 return targetm.calls.pass_by_reference (ca, mode, type, named_arg);
cc9b8628 2099}
2100
13f08ee7 2101/* Return true if TYPE, which is passed by reference, should be callee
2102 copied instead of caller copied. */
2103
2104bool
2105reference_callee_copied (CUMULATIVE_ARGS *ca, enum machine_mode mode,
2106 tree type, bool named_arg)
2107{
2108 if (type && TREE_ADDRESSABLE (type))
2109 return false;
2110 return targetm.calls.callee_copies (ca, mode, type, named_arg);
2111}
2112
35a569c6 2113/* Structures to communicate between the subroutines of assign_parms.
2114 The first holds data persistent across all parameters, the second
2115 is cleared out for each parameter. */
897b77d6 2116
35a569c6 2117struct assign_parm_data_all
897b77d6 2118{
897b77d6 2119 CUMULATIVE_ARGS args_so_far;
897b77d6 2120 struct args_size stack_args_size;
35a569c6 2121 tree function_result_decl;
2122 tree orig_fnargs;
28bf151d 2123 rtx first_conversion_insn;
2124 rtx last_conversion_insn;
35a569c6 2125 HOST_WIDE_INT pretend_args_size;
2126 HOST_WIDE_INT extra_pretend_bytes;
2127 int reg_parm_stack_space;
2128};
897b77d6 2129
35a569c6 2130struct assign_parm_data_one
2131{
2132 tree nominal_type;
2133 tree passed_type;
2134 rtx entry_parm;
2135 rtx stack_parm;
2136 enum machine_mode nominal_mode;
2137 enum machine_mode passed_mode;
2138 enum machine_mode promoted_mode;
2139 struct locate_and_pad_arg_data locate;
2140 int partial;
2141 BOOL_BITFIELD named_arg : 1;
35a569c6 2142 BOOL_BITFIELD passed_pointer : 1;
2143 BOOL_BITFIELD on_stack : 1;
2144 BOOL_BITFIELD loaded_in_reg : 1;
2145};
eb749d77 2146
35a569c6 2147/* A subroutine of assign_parms. Initialize ALL. */
897b77d6 2148
35a569c6 2149static void
2150assign_parms_initialize_all (struct assign_parm_data_all *all)
2151{
132d5071 2152 tree fntype ATTRIBUTE_UNUSED;
897b77d6 2153
35a569c6 2154 memset (all, 0, sizeof (*all));
2155
2156 fntype = TREE_TYPE (current_function_decl);
2157
2158#ifdef INIT_CUMULATIVE_INCOMING_ARGS
2159 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far, fntype, NULL_RTX);
2160#else
2161 INIT_CUMULATIVE_ARGS (all->args_so_far, fntype, NULL_RTX,
2162 current_function_decl, -1);
2163#endif
2164
2165#ifdef REG_PARM_STACK_SPACE
2166 all->reg_parm_stack_space = REG_PARM_STACK_SPACE (current_function_decl);
2167#endif
2168}
897b77d6 2169
35a569c6 2170/* If ARGS contains entries with complex types, split the entry into two
2171 entries of the component type. Return a new list of substitutions are
2172 needed, else the old list. */
2173
3e992c41 2174static void
2175split_complex_args (VEC(tree, heap) **args)
35a569c6 2176{
3e992c41 2177 unsigned i;
35a569c6 2178 tree p;
2179
3e992c41 2180 for (i = 0; VEC_iterate (tree, *args, i, p); ++i)
35a569c6 2181 {
2182 tree type = TREE_TYPE (p);
2183 if (TREE_CODE (type) == COMPLEX_TYPE
2184 && targetm.calls.split_complex_arg (type))
2185 {
2186 tree decl;
2187 tree subtype = TREE_TYPE (type);
e6427ef0 2188 bool addressable = TREE_ADDRESSABLE (p);
35a569c6 2189
2190 /* Rewrite the PARM_DECL's type with its component. */
3e992c41 2191 p = copy_node (p);
35a569c6 2192 TREE_TYPE (p) = subtype;
2193 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2194 DECL_MODE (p) = VOIDmode;
2195 DECL_SIZE (p) = NULL;
2196 DECL_SIZE_UNIT (p) = NULL;
e6427ef0 2197 /* If this arg must go in memory, put it in a pseudo here.
2198 We can't allow it to go in memory as per normal parms,
2199 because the usual place might not have the imag part
2200 adjacent to the real part. */
2201 DECL_ARTIFICIAL (p) = addressable;
2202 DECL_IGNORED_P (p) = addressable;
2203 TREE_ADDRESSABLE (p) = 0;
35a569c6 2204 layout_decl (p, 0);
3e992c41 2205 VEC_replace (tree, *args, i, p);
35a569c6 2206
2207 /* Build a second synthetic decl. */
e60a6f7b 2208 decl = build_decl (EXPR_LOCATION (p),
2209 PARM_DECL, NULL_TREE, subtype);
35a569c6 2210 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
e6427ef0 2211 DECL_ARTIFICIAL (decl) = addressable;
2212 DECL_IGNORED_P (decl) = addressable;
35a569c6 2213 layout_decl (decl, 0);
3e992c41 2214 VEC_safe_insert (tree, heap, *args, ++i, decl);
35a569c6 2215 }
2216 }
35a569c6 2217}
2218
2219/* A subroutine of assign_parms. Adjust the parameter list to incorporate
2220 the hidden struct return argument, and (abi willing) complex args.
2221 Return the new parameter list. */
2222
3e992c41 2223static VEC(tree, heap) *
35a569c6 2224assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2225{
2226 tree fndecl = current_function_decl;
2227 tree fntype = TREE_TYPE (fndecl);
3e992c41 2228 VEC(tree, heap) *fnargs = NULL;
2229 tree arg;
2230
2231 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = TREE_CHAIN (arg))
2232 VEC_safe_push (tree, heap, fnargs, arg);
2233
2234 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
897b77d6 2235
2236 /* If struct value address is treated as the first argument, make it so. */
45550790 2237 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
18d50ae6 2238 && ! cfun->returns_pcc_struct
45550790 2239 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
897b77d6 2240 {
3ff448ca 2241 tree type = build_pointer_type (TREE_TYPE (fntype));
35a569c6 2242 tree decl;
897b77d6 2243
e60a6f7b 2244 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2245 PARM_DECL, NULL_TREE, type);
35a569c6 2246 DECL_ARG_TYPE (decl) = type;
2247 DECL_ARTIFICIAL (decl) = 1;
553acd9c 2248 DECL_IGNORED_P (decl) = 1;
897b77d6 2249
3e992c41 2250 TREE_CHAIN (decl) = all->orig_fnargs;
2251 all->orig_fnargs = decl;
2252 VEC_safe_insert (tree, heap, fnargs, 0, decl);
2253
35a569c6 2254 all->function_result_decl = decl;
897b77d6 2255 }
06ebc183 2256
92d40bc4 2257 /* If the target wants to split complex arguments into scalars, do so. */
2258 if (targetm.calls.split_complex_arg)
3e992c41 2259 split_complex_args (&fnargs);
915e81b8 2260
35a569c6 2261 return fnargs;
2262}
241399f6 2263
35a569c6 2264/* A subroutine of assign_parms. Examine PARM and pull out type and mode
2265 data for the parameter. Incorporate ABI specifics such as pass-by-
2266 reference and type promotion. */
897b77d6 2267
35a569c6 2268static void
2269assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2270 struct assign_parm_data_one *data)
2271{
2272 tree nominal_type, passed_type;
2273 enum machine_mode nominal_mode, passed_mode, promoted_mode;
3b2411a8 2274 int unsignedp;
897b77d6 2275
35a569c6 2276 memset (data, 0, sizeof (*data));
2277
f0b5f617 2278 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
18d50ae6 2279 if (!cfun->stdarg)
f0b5f617 2280 data->named_arg = 1; /* No variadic parms. */
e2704f58 2281 else if (TREE_CHAIN (parm))
f0b5f617 2282 data->named_arg = 1; /* Not the last non-variadic parm. */
e2704f58 2283 else if (targetm.calls.strict_argument_naming (&all->args_so_far))
f0b5f617 2284 data->named_arg = 1; /* Only variadic ones are unnamed. */
35a569c6 2285 else
f0b5f617 2286 data->named_arg = 0; /* Treat as variadic. */
35a569c6 2287
2288 nominal_type = TREE_TYPE (parm);
2289 passed_type = DECL_ARG_TYPE (parm);
2290
2291 /* Look out for errors propagating this far. Also, if the parameter's
2292 type is void then its value doesn't matter. */
2293 if (TREE_TYPE (parm) == error_mark_node
2294 /* This can happen after weird syntax errors
2295 or if an enum type is defined among the parms. */
2296 || TREE_CODE (parm) != PARM_DECL
2297 || passed_type == NULL
2298 || VOID_TYPE_P (nominal_type))
2299 {
2300 nominal_type = passed_type = void_type_node;
2301 nominal_mode = passed_mode = promoted_mode = VOIDmode;
2302 goto egress;
2303 }
d06f5fba 2304
35a569c6 2305 /* Find mode of arg as it is passed, and mode of arg as it should be
2306 during execution of this function. */
2307 passed_mode = TYPE_MODE (passed_type);
2308 nominal_mode = TYPE_MODE (nominal_type);
2309
8df5a43d 2310 /* If the parm is to be passed as a transparent union or record, use the
2311 type of the first field for the tests below. We have already verified
2312 that the modes are the same. */
2313 if ((TREE_CODE (passed_type) == UNION_TYPE
2314 || TREE_CODE (passed_type) == RECORD_TYPE)
2315 && TYPE_TRANSPARENT_AGGR (passed_type))
2316 passed_type = TREE_TYPE (first_field (passed_type));
35a569c6 2317
cc9b8628 2318 /* See if this arg was passed by invisible reference. */
2319 if (pass_by_reference (&all->args_so_far, passed_mode,
2320 passed_type, data->named_arg))
35a569c6 2321 {
2322 passed_type = nominal_type = build_pointer_type (passed_type);
2323 data->passed_pointer = true;
2324 passed_mode = nominal_mode = Pmode;
2325 }
897b77d6 2326
35a569c6 2327 /* Find mode as it is passed by the ABI. */
3b2411a8 2328 unsignedp = TYPE_UNSIGNED (passed_type);
2329 promoted_mode = promote_function_mode (passed_type, passed_mode, &unsignedp,
2330 TREE_TYPE (current_function_decl), 0);
897b77d6 2331
35a569c6 2332 egress:
2333 data->nominal_type = nominal_type;
2334 data->passed_type = passed_type;
2335 data->nominal_mode = nominal_mode;
2336 data->passed_mode = passed_mode;
2337 data->promoted_mode = promoted_mode;
2338}
24ec33e7 2339
35a569c6 2340/* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
897b77d6 2341
35a569c6 2342static void
2343assign_parms_setup_varargs (struct assign_parm_data_all *all,
2344 struct assign_parm_data_one *data, bool no_rtl)
2345{
2346 int varargs_pretend_bytes = 0;
2347
2348 targetm.calls.setup_incoming_varargs (&all->args_so_far,
2349 data->promoted_mode,
2350 data->passed_type,
2351 &varargs_pretend_bytes, no_rtl);
2352
2353 /* If the back-end has requested extra stack space, record how much is
2354 needed. Do not change pretend_args_size otherwise since it may be
2355 nonzero from an earlier partial argument. */
2356 if (varargs_pretend_bytes > 0)
2357 all->pretend_args_size = varargs_pretend_bytes;
2358}
7e8dfb30 2359
35a569c6 2360/* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2361 the incoming location of the current parameter. */
2362
2363static void
2364assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2365 struct assign_parm_data_one *data)
2366{
2367 HOST_WIDE_INT pretend_bytes = 0;
2368 rtx entry_parm;
2369 bool in_regs;
2370
2371 if (data->promoted_mode == VOIDmode)
2372 {
2373 data->entry_parm = data->stack_parm = const0_rtx;
2374 return;
2375 }
7e8dfb30 2376
897b77d6 2377#ifdef FUNCTION_INCOMING_ARG
35a569c6 2378 entry_parm = FUNCTION_INCOMING_ARG (all->args_so_far, data->promoted_mode,
2379 data->passed_type, data->named_arg);
897b77d6 2380#else
35a569c6 2381 entry_parm = FUNCTION_ARG (all->args_so_far, data->promoted_mode,
2382 data->passed_type, data->named_arg);
897b77d6 2383#endif
2384
35a569c6 2385 if (entry_parm == 0)
2386 data->promoted_mode = data->passed_mode;
897b77d6 2387
35a569c6 2388 /* Determine parm's home in the stack, in case it arrives in the stack
2389 or we should pretend it did. Compute the stack position and rtx where
2390 the argument arrives and its size.
897b77d6 2391
35a569c6 2392 There is one complexity here: If this was a parameter that would
2393 have been passed in registers, but wasn't only because it is
2394 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2395 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2396 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2397 as it was the previous time. */
2398 in_regs = entry_parm != 0;
897b77d6 2399#ifdef STACK_PARMS_IN_REG_PARM_AREA
35a569c6 2400 in_regs = true;
241399f6 2401#endif
35a569c6 2402 if (!in_regs && !data->named_arg)
2403 {
2404 if (targetm.calls.pretend_outgoing_varargs_named (&all->args_so_far))
241399f6 2405 {
35a569c6 2406 rtx tem;
897b77d6 2407#ifdef FUNCTION_INCOMING_ARG
35a569c6 2408 tem = FUNCTION_INCOMING_ARG (all->args_so_far, data->promoted_mode,
2409 data->passed_type, true);
897b77d6 2410#else
35a569c6 2411 tem = FUNCTION_ARG (all->args_so_far, data->promoted_mode,
2412 data->passed_type, true);
897b77d6 2413#endif
35a569c6 2414 in_regs = tem != NULL;
241399f6 2415 }
35a569c6 2416 }
241399f6 2417
35a569c6 2418 /* If this parameter was passed both in registers and in the stack, use
2419 the copy on the stack. */
0336f0f0 2420 if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2421 data->passed_type))
35a569c6 2422 entry_parm = 0;
241399f6 2423
35a569c6 2424 if (entry_parm)
2425 {
2426 int partial;
2427
f054eb3c 2428 partial = targetm.calls.arg_partial_bytes (&all->args_so_far,
2429 data->promoted_mode,
2430 data->passed_type,
2431 data->named_arg);
35a569c6 2432 data->partial = partial;
2433
2434 /* The caller might already have allocated stack space for the
2435 register parameters. */
2436 if (partial != 0 && all->reg_parm_stack_space == 0)
1cd50c9a 2437 {
35a569c6 2438 /* Part of this argument is passed in registers and part
2439 is passed on the stack. Ask the prologue code to extend
2440 the stack part so that we can recreate the full value.
2441
2442 PRETEND_BYTES is the size of the registers we need to store.
2443 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2444 stack space that the prologue should allocate.
2445
2446 Internally, gcc assumes that the argument pointer is aligned
2447 to STACK_BOUNDARY bits. This is used both for alignment
2448 optimizations (see init_emit) and to locate arguments that are
2449 aligned to more than PARM_BOUNDARY bits. We must preserve this
2450 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2451 a stack boundary. */
2452
2453 /* We assume at most one partial arg, and it must be the first
2454 argument on the stack. */
fdada98f 2455 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
35a569c6 2456
f054eb3c 2457 pretend_bytes = partial;
35a569c6 2458 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2459
2460 /* We want to align relative to the actual stack pointer, so
2461 don't include this in the stack size until later. */
2462 all->extra_pretend_bytes = all->pretend_args_size;
1cd50c9a 2463 }
35a569c6 2464 }
241399f6 2465
35a569c6 2466 locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2467 entry_parm ? data->partial : 0, current_function_decl,
2468 &all->stack_args_size, &data->locate);
897b77d6 2469
c6586120 2470 /* Update parm_stack_boundary if this parameter is passed in the
2471 stack. */
2472 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2473 crtl->parm_stack_boundary = data->locate.boundary;
2474
35a569c6 2475 /* Adjust offsets to include the pretend args. */
2476 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2477 data->locate.slot_offset.constant += pretend_bytes;
2478 data->locate.offset.constant += pretend_bytes;
27664a4b 2479
35a569c6 2480 data->entry_parm = entry_parm;
2481}
897b77d6 2482
35a569c6 2483/* A subroutine of assign_parms. If there is actually space on the stack
2484 for this parm, count it in stack_args_size and return true. */
897b77d6 2485
35a569c6 2486static bool
2487assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2488 struct assign_parm_data_one *data)
2489{
a133d57d 2490 /* Trivially true if we've no incoming register. */
35a569c6 2491 if (data->entry_parm == NULL)
2492 ;
2493 /* Also true if we're partially in registers and partially not,
2494 since we've arranged to drop the entire argument on the stack. */
2495 else if (data->partial != 0)
2496 ;
2497 /* Also true if the target says that it's passed in both registers
2498 and on the stack. */
2499 else if (GET_CODE (data->entry_parm) == PARALLEL
2500 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2501 ;
2502 /* Also true if the target says that there's stack allocated for
2503 all register parameters. */
2504 else if (all->reg_parm_stack_space > 0)
2505 ;
2506 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2507 else
2508 return false;
897b77d6 2509
35a569c6 2510 all->stack_args_size.constant += data->locate.size.constant;
2511 if (data->locate.size.var)
2512 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
06ebc183 2513
35a569c6 2514 return true;
2515}
bffcf014 2516
35a569c6 2517/* A subroutine of assign_parms. Given that this parameter is allocated
2518 stack space by the ABI, find it. */
897b77d6 2519
35a569c6 2520static void
2521assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2522{
2523 rtx offset_rtx, stack_parm;
2524 unsigned int align, boundary;
897b77d6 2525
35a569c6 2526 /* If we're passing this arg using a reg, make its stack home the
2527 aligned stack slot. */
2528 if (data->entry_parm)
2529 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2530 else
2531 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2532
abe32cce 2533 stack_parm = crtl->args.internal_arg_pointer;
35a569c6 2534 if (offset_rtx != const0_rtx)
2535 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2536 stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2537
d92e3973 2538 if (!data->passed_pointer)
7aeb4db5 2539 {
d92e3973 2540 set_mem_attributes (stack_parm, parm, 1);
2541 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2542 while promoted mode's size is needed. */
2543 if (data->promoted_mode != BLKmode
2544 && data->promoted_mode != DECL_MODE (parm))
7aeb4db5 2545 {
d92e3973 2546 set_mem_size (stack_parm,
2547 GEN_INT (GET_MODE_SIZE (data->promoted_mode)));
2548 if (MEM_EXPR (stack_parm) && MEM_OFFSET (stack_parm))
2549 {
2550 int offset = subreg_lowpart_offset (DECL_MODE (parm),
2551 data->promoted_mode);
2552 if (offset)
2553 set_mem_offset (stack_parm,
2554 plus_constant (MEM_OFFSET (stack_parm),
2555 -offset));
2556 }
7aeb4db5 2557 }
2558 }
35a569c6 2559
c5dc0c32 2560 boundary = data->locate.boundary;
2561 align = BITS_PER_UNIT;
35a569c6 2562
2563 /* If we're padding upward, we know that the alignment of the slot
2564 is FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2565 intentionally forcing upward padding. Otherwise we have to come
2566 up with a guess at the alignment based on OFFSET_RTX. */
c5dc0c32 2567 if (data->locate.where_pad != downward || data->entry_parm)
35a569c6 2568 align = boundary;
971ba038 2569 else if (CONST_INT_P (offset_rtx))
35a569c6 2570 {
2571 align = INTVAL (offset_rtx) * BITS_PER_UNIT | boundary;
2572 align = align & -align;
2573 }
c5dc0c32 2574 set_mem_align (stack_parm, align);
35a569c6 2575
2576 if (data->entry_parm)
2577 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2578
2579 data->stack_parm = stack_parm;
2580}
2581
2582/* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2583 always valid and contiguous. */
2584
2585static void
2586assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2587{
2588 rtx entry_parm = data->entry_parm;
2589 rtx stack_parm = data->stack_parm;
2590
2591 /* If this parm was passed part in regs and part in memory, pretend it
2592 arrived entirely in memory by pushing the register-part onto the stack.
2593 In the special case of a DImode or DFmode that is split, we could put
2594 it together in a pseudoreg directly, but for now that's not worth
2595 bothering with. */
2596 if (data->partial != 0)
2597 {
2598 /* Handle calls that pass values in multiple non-contiguous
2599 locations. The Irix 6 ABI has examples of this. */
2600 if (GET_CODE (entry_parm) == PARALLEL)
2601 emit_group_store (validize_mem (stack_parm), entry_parm,
48e1416a 2602 data->passed_type,
35a569c6 2603 int_size_in_bytes (data->passed_type));
897b77d6 2604 else
f054eb3c 2605 {
2606 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2607 move_block_from_reg (REGNO (entry_parm), validize_mem (stack_parm),
2608 data->partial / UNITS_PER_WORD);
2609 }
897b77d6 2610
35a569c6 2611 entry_parm = stack_parm;
2612 }
897b77d6 2613
35a569c6 2614 /* If we didn't decide this parm came in a register, by default it came
2615 on the stack. */
2616 else if (entry_parm == NULL)
2617 entry_parm = stack_parm;
2618
2619 /* When an argument is passed in multiple locations, we can't make use
2620 of this information, but we can save some copying if the whole argument
2621 is passed in a single register. */
2622 else if (GET_CODE (entry_parm) == PARALLEL
2623 && data->nominal_mode != BLKmode
2624 && data->passed_mode != BLKmode)
2625 {
2626 size_t i, len = XVECLEN (entry_parm, 0);
2627
2628 for (i = 0; i < len; i++)
2629 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2630 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2631 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2632 == data->passed_mode)
2633 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2634 {
2635 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2636 break;
2637 }
2638 }
4d6c855d 2639
35a569c6 2640 data->entry_parm = entry_parm;
2641}
897b77d6 2642
77c0eeb4 2643/* A subroutine of assign_parms. Reconstitute any values which were
2644 passed in multiple registers and would fit in a single register. */
2645
2646static void
2647assign_parm_remove_parallels (struct assign_parm_data_one *data)
2648{
2649 rtx entry_parm = data->entry_parm;
2650
2651 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2652 This can be done with register operations rather than on the
2653 stack, even if we will store the reconstituted parameter on the
2654 stack later. */
1cf0636a 2655 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
77c0eeb4 2656 {
2657 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
77b80ffd 2658 emit_group_store (parmreg, entry_parm, data->passed_type,
77c0eeb4 2659 GET_MODE_SIZE (GET_MODE (entry_parm)));
2660 entry_parm = parmreg;
2661 }
2662
2663 data->entry_parm = entry_parm;
2664}
2665
35a569c6 2666/* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2667 always valid and properly aligned. */
897b77d6 2668
35a569c6 2669static void
2670assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2671{
2672 rtx stack_parm = data->stack_parm;
2673
2674 /* If we can't trust the parm stack slot to be aligned enough for its
2675 ultimate type, don't use that slot after entry. We'll make another
2676 stack slot, if we need one. */
c5dc0c32 2677 if (stack_parm
2678 && ((STRICT_ALIGNMENT
2679 && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2680 || (data->nominal_type
2681 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2682 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
35a569c6 2683 stack_parm = NULL;
2684
2685 /* If parm was passed in memory, and we need to convert it on entry,
2686 don't store it back in that same slot. */
2687 else if (data->entry_parm == stack_parm
2688 && data->nominal_mode != BLKmode
2689 && data->nominal_mode != data->passed_mode)
2690 stack_parm = NULL;
2691
f1a0edff 2692 /* If stack protection is in effect for this function, don't leave any
2693 pointers in their passed stack slots. */
edb7afe8 2694 else if (crtl->stack_protect_guard
f1a0edff 2695 && (flag_stack_protect == 2
2696 || data->passed_pointer
2697 || POINTER_TYPE_P (data->nominal_type)))
2698 stack_parm = NULL;
2699
35a569c6 2700 data->stack_parm = stack_parm;
2701}
90b076ea 2702
35a569c6 2703/* A subroutine of assign_parms. Return true if the current parameter
2704 should be stored as a BLKmode in the current frame. */
2705
2706static bool
2707assign_parm_setup_block_p (struct assign_parm_data_one *data)
2708{
2709 if (data->nominal_mode == BLKmode)
2710 return true;
1cf0636a 2711 if (GET_MODE (data->entry_parm) == BLKmode)
2712 return true;
a2509aaa 2713
5f4cd670 2714#ifdef BLOCK_REG_PADDING
ed4b0b75 2715 /* Only assign_parm_setup_block knows how to deal with register arguments
2716 that are padded at the least significant end. */
2717 if (REG_P (data->entry_parm)
2718 && GET_MODE_SIZE (data->promoted_mode) < UNITS_PER_WORD
2719 && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2720 == (BYTES_BIG_ENDIAN ? upward : downward)))
35a569c6 2721 return true;
5f4cd670 2722#endif
35a569c6 2723
2724 return false;
2725}
2726
48e1416a 2727/* A subroutine of assign_parms. Arrange for the parameter to be
35a569c6 2728 present and valid in DATA->STACK_RTL. */
2729
2730static void
e2ff5c1b 2731assign_parm_setup_block (struct assign_parm_data_all *all,
2732 tree parm, struct assign_parm_data_one *data)
35a569c6 2733{
2734 rtx entry_parm = data->entry_parm;
2735 rtx stack_parm = data->stack_parm;
c5dc0c32 2736 HOST_WIDE_INT size;
2737 HOST_WIDE_INT size_stored;
35a569c6 2738
e2ff5c1b 2739 if (GET_CODE (entry_parm) == PARALLEL)
2740 entry_parm = emit_group_move_into_temps (entry_parm);
2741
c5dc0c32 2742 size = int_size_in_bytes (data->passed_type);
2743 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2744 if (stack_parm == 0)
2745 {
3a1c59da 2746 DECL_ALIGN (parm) = MAX (DECL_ALIGN (parm), BITS_PER_WORD);
c5dc0c32 2747 stack_parm = assign_stack_local (BLKmode, size_stored,
3a1c59da 2748 DECL_ALIGN (parm));
c5dc0c32 2749 if (GET_MODE_SIZE (GET_MODE (entry_parm)) == size)
2750 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2751 set_mem_attributes (stack_parm, parm, 1);
2752 }
2753
35a569c6 2754 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2755 calls that pass values in multiple non-contiguous locations. */
2756 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2757 {
35a569c6 2758 rtx mem;
2759
2760 /* Note that we will be storing an integral number of words.
2761 So we have to be careful to ensure that we allocate an
c5dc0c32 2762 integral number of words. We do this above when we call
35a569c6 2763 assign_stack_local if space was not allocated in the argument
2764 list. If it was, this will not work if PARM_BOUNDARY is not
2765 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2766 if it becomes a problem. Exception is when BLKmode arrives
2767 with arguments not conforming to word_mode. */
2768
c5dc0c32 2769 if (data->stack_parm == 0)
2770 ;
35a569c6 2771 else if (GET_CODE (entry_parm) == PARALLEL)
2772 ;
fdada98f 2773 else
2774 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
897b77d6 2775
35a569c6 2776 mem = validize_mem (stack_parm);
530178a9 2777
35a569c6 2778 /* Handle values in multiple non-contiguous locations. */
2779 if (GET_CODE (entry_parm) == PARALLEL)
e2ff5c1b 2780 {
28bf151d 2781 push_to_sequence2 (all->first_conversion_insn,
2782 all->last_conversion_insn);
e2ff5c1b 2783 emit_group_store (mem, entry_parm, data->passed_type, size);
28bf151d 2784 all->first_conversion_insn = get_insns ();
2785 all->last_conversion_insn = get_last_insn ();
e2ff5c1b 2786 end_sequence ();
2787 }
530178a9 2788
35a569c6 2789 else if (size == 0)
2790 ;
dd6fed02 2791
35a569c6 2792 /* If SIZE is that of a mode no bigger than a word, just use
2793 that mode's store operation. */
2794 else if (size <= UNITS_PER_WORD)
2795 {
2796 enum machine_mode mode
2797 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
530178a9 2798
35a569c6 2799 if (mode != BLKmode
5f4cd670 2800#ifdef BLOCK_REG_PADDING
35a569c6 2801 && (size == UNITS_PER_WORD
2802 || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2803 != (BYTES_BIG_ENDIAN ? upward : downward)))
5f4cd670 2804#endif
35a569c6 2805 )
2806 {
2973927c 2807 rtx reg;
2808
2809 /* We are really truncating a word_mode value containing
2810 SIZE bytes into a value of mode MODE. If such an
2811 operation requires no actual instructions, we can refer
2812 to the value directly in mode MODE, otherwise we must
2813 start with the register in word_mode and explicitly
2814 convert it. */
2815 if (TRULY_NOOP_TRUNCATION (size * BITS_PER_UNIT, BITS_PER_WORD))
2816 reg = gen_rtx_REG (mode, REGNO (entry_parm));
2817 else
2818 {
2819 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
2820 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
2821 }
35a569c6 2822 emit_move_insn (change_address (mem, mode, 0), reg);
2823 }
530178a9 2824
35a569c6 2825 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
2826 machine must be aligned to the left before storing
2827 to memory. Note that the previous test doesn't
2828 handle all cases (e.g. SIZE == 3). */
2829 else if (size != UNITS_PER_WORD
5f4cd670 2830#ifdef BLOCK_REG_PADDING
35a569c6 2831 && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2832 == downward)
5f4cd670 2833#else
35a569c6 2834 && BYTES_BIG_ENDIAN
5f4cd670 2835#endif
35a569c6 2836 )
2837 {
2838 rtx tem, x;
2839 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
e1b9bbec 2840 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
35a569c6 2841
92966f8b 2842 x = expand_shift (LSHIFT_EXPR, word_mode, reg,
7016c612 2843 build_int_cst (NULL_TREE, by),
7c446c95 2844 NULL_RTX, 1);
35a569c6 2845 tem = change_address (mem, word_mode, 0);
2846 emit_move_insn (tem, x);
897b77d6 2847 }
35a569c6 2848 else
e2ff5c1b 2849 move_block_from_reg (REGNO (entry_parm), mem,
35a569c6 2850 size_stored / UNITS_PER_WORD);
897b77d6 2851 }
35a569c6 2852 else
e2ff5c1b 2853 move_block_from_reg (REGNO (entry_parm), mem,
35a569c6 2854 size_stored / UNITS_PER_WORD);
2855 }
c5dc0c32 2856 else if (data->stack_parm == 0)
2857 {
28bf151d 2858 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
c5dc0c32 2859 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
2860 BLOCK_OP_NORMAL);
28bf151d 2861 all->first_conversion_insn = get_insns ();
2862 all->last_conversion_insn = get_last_insn ();
c5dc0c32 2863 end_sequence ();
2864 }
35a569c6 2865
c5dc0c32 2866 data->stack_parm = stack_parm;
35a569c6 2867 SET_DECL_RTL (parm, stack_parm);
2868}
2869
2870/* A subroutine of assign_parms. Allocate a pseudo to hold the current
2871 parameter. Get it there. Perform all ABI specified conversions. */
2872
2873static void
2874assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
2875 struct assign_parm_data_one *data)
2876{
2877 rtx parmreg;
2878 enum machine_mode promoted_nominal_mode;
2879 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
2880 bool did_conversion = false;
2881
2882 /* Store the parm in a pseudoregister during the function, but we may
c879dbcf 2883 need to do it in a wider mode. Using 2 here makes the result
2884 consistent with promote_decl_mode and thus expand_expr_real_1. */
35a569c6 2885 promoted_nominal_mode
3b2411a8 2886 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
c879dbcf 2887 TREE_TYPE (current_function_decl), 2);
35a569c6 2888
2889 parmreg = gen_reg_rtx (promoted_nominal_mode);
2890
2891 if (!DECL_ARTIFICIAL (parm))
2892 mark_user_reg (parmreg);
2893
2894 /* If this was an item that we received a pointer to,
2895 set DECL_RTL appropriately. */
2896 if (data->passed_pointer)
2897 {
2898 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
2899 set_mem_attributes (x, parm, 1);
2900 SET_DECL_RTL (parm, x);
2901 }
2902 else
b04fab2a 2903 SET_DECL_RTL (parm, parmreg);
35a569c6 2904
77c0eeb4 2905 assign_parm_remove_parallels (data);
2906
c879dbcf 2907 /* Copy the value into the register, thus bridging between
2908 assign_parm_find_data_types and expand_expr_real_1. */
35a569c6 2909 if (data->nominal_mode != data->passed_mode
2910 || promoted_nominal_mode != data->promoted_mode)
2911 {
2912 int save_tree_used;
2913
2914 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
2915 mode, by the caller. We now have to convert it to
2916 NOMINAL_MODE, if different. However, PARMREG may be in
2917 a different mode than NOMINAL_MODE if it is being stored
2918 promoted.
2919
2920 If ENTRY_PARM is a hard register, it might be in a register
2921 not valid for operating in its mode (e.g., an odd-numbered
2922 register for a DFmode). In that case, moves are the only
2923 thing valid, so we can't do a convert from there. This
2924 occurs when the calling sequence allow such misaligned
2925 usages.
2926
2927 In addition, the conversion may involve a call, which could
2928 clobber parameters which haven't been copied to pseudo
2929 registers yet. Therefore, we must first copy the parm to
2930 a pseudo reg here, and save the conversion until after all
2931 parameters have been moved. */
2932
2933 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
2934
2935 emit_move_insn (tempreg, validize_mem (data->entry_parm));
2936
28bf151d 2937 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
35a569c6 2938 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
2939
2940 if (GET_CODE (tempreg) == SUBREG
2941 && GET_MODE (tempreg) == data->nominal_mode
2942 && REG_P (SUBREG_REG (tempreg))
2943 && data->nominal_mode == data->passed_mode
2944 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm)
2945 && GET_MODE_SIZE (GET_MODE (tempreg))
2946 < GET_MODE_SIZE (GET_MODE (data->entry_parm)))
897b77d6 2947 {
35a569c6 2948 /* The argument is already sign/zero extended, so note it
2949 into the subreg. */
2950 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
2951 SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
2952 }
19e03a68 2953
35a569c6 2954 /* TREE_USED gets set erroneously during expand_assignment. */
2955 save_tree_used = TREE_USED (parm);
5b5037b3 2956 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
35a569c6 2957 TREE_USED (parm) = save_tree_used;
28bf151d 2958 all->first_conversion_insn = get_insns ();
2959 all->last_conversion_insn = get_last_insn ();
35a569c6 2960 end_sequence ();
19e03a68 2961
35a569c6 2962 did_conversion = true;
2963 }
2964 else
2965 emit_move_insn (parmreg, validize_mem (data->entry_parm));
2966
2967 /* If we were passed a pointer but the actual value can safely live
2968 in a register, put it in one. */
2969 if (data->passed_pointer
2970 && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
2971 /* If by-reference argument was promoted, demote it. */
2972 && (TYPE_MODE (TREE_TYPE (parm)) != GET_MODE (DECL_RTL (parm))
2973 || use_register_for_decl (parm)))
2974 {
2975 /* We can't use nominal_mode, because it will have been set to
2976 Pmode above. We must use the actual mode of the parm. */
2977 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
2978 mark_user_reg (parmreg);
8815f4da 2979
35a569c6 2980 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
2981 {
2982 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
2983 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
2984
28bf151d 2985 push_to_sequence2 (all->first_conversion_insn,
2986 all->last_conversion_insn);
35a569c6 2987 emit_move_insn (tempreg, DECL_RTL (parm));
2988 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
2989 emit_move_insn (parmreg, tempreg);
28bf151d 2990 all->first_conversion_insn = get_insns ();
2991 all->last_conversion_insn = get_last_insn ();
35a569c6 2992 end_sequence ();
897b77d6 2993
35a569c6 2994 did_conversion = true;
2995 }
2996 else
2997 emit_move_insn (parmreg, DECL_RTL (parm));
897b77d6 2998
35a569c6 2999 SET_DECL_RTL (parm, parmreg);
60d903f5 3000
35a569c6 3001 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3002 now the parm. */
3003 data->stack_parm = NULL;
3004 }
701e46d0 3005
35a569c6 3006 /* Mark the register as eliminable if we did no conversion and it was
3007 copied from memory at a fixed offset, and the arg pointer was not
3008 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3009 offset formed an invalid address, such memory-equivalences as we
3010 make here would screw up life analysis for it. */
3011 if (data->nominal_mode == data->passed_mode
3012 && !did_conversion
3013 && data->stack_parm != 0
3014 && MEM_P (data->stack_parm)
3015 && data->locate.offset.var == 0
3016 && reg_mentioned_p (virtual_incoming_args_rtx,
3017 XEXP (data->stack_parm, 0)))
3018 {
3019 rtx linsn = get_last_insn ();
3020 rtx sinsn, set;
5f85a240 3021
35a569c6 3022 /* Mark complex types separately. */
3023 if (GET_CODE (parmreg) == CONCAT)
3024 {
3025 enum machine_mode submode
3026 = GET_MODE_INNER (GET_MODE (parmreg));
de17a47b 3027 int regnor = REGNO (XEXP (parmreg, 0));
3028 int regnoi = REGNO (XEXP (parmreg, 1));
3029 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3030 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3031 GET_MODE_SIZE (submode));
35a569c6 3032
3033 /* Scan backwards for the set of the real and
3034 imaginary parts. */
3035 for (sinsn = linsn; sinsn != 0;
3036 sinsn = prev_nonnote_insn (sinsn))
3037 {
3038 set = single_set (sinsn);
3039 if (set == 0)
3040 continue;
3041
3042 if (SET_DEST (set) == regno_reg_rtx [regnoi])
750a330e 3043 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
35a569c6 3044 else if (SET_DEST (set) == regno_reg_rtx [regnor])
750a330e 3045 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
5f85a240 3046 }
35a569c6 3047 }
3048 else if ((set = single_set (linsn)) != 0
3049 && SET_DEST (set) == parmreg)
750a330e 3050 set_unique_reg_note (linsn, REG_EQUIV, data->stack_parm);
35a569c6 3051 }
3052
3053 /* For pointer data type, suggest pointer register. */
3054 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3055 mark_reg_pointer (parmreg,
3056 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3057}
3058
3059/* A subroutine of assign_parms. Allocate stack space to hold the current
3060 parameter. Get it there. Perform all ABI specified conversions. */
3061
3062static void
3063assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3064 struct assign_parm_data_one *data)
3065{
3066 /* Value must be stored in the stack slot STACK_PARM during function
3067 execution. */
c5dc0c32 3068 bool to_conversion = false;
35a569c6 3069
77c0eeb4 3070 assign_parm_remove_parallels (data);
3071
35a569c6 3072 if (data->promoted_mode != data->nominal_mode)
3073 {
3074 /* Conversion is required. */
3075 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
897b77d6 3076
35a569c6 3077 emit_move_insn (tempreg, validize_mem (data->entry_parm));
3078
28bf151d 3079 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
c5dc0c32 3080 to_conversion = true;
3081
35a569c6 3082 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3083 TYPE_UNSIGNED (TREE_TYPE (parm)));
3084
3085 if (data->stack_parm)
738ab6f5 3086 {
3087 int offset = subreg_lowpart_offset (data->nominal_mode,
3088 GET_MODE (data->stack_parm));
3089 /* ??? This may need a big-endian conversion on sparc64. */
3090 data->stack_parm
3091 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3092 if (offset && MEM_OFFSET (data->stack_parm))
3093 set_mem_offset (data->stack_parm,
3094 plus_constant (MEM_OFFSET (data->stack_parm),
3095 offset));
3096 }
35a569c6 3097 }
3098
3099 if (data->entry_parm != data->stack_parm)
3100 {
c5dc0c32 3101 rtx src, dest;
3102
35a569c6 3103 if (data->stack_parm == 0)
3104 {
c9b50df7 3105 int align = STACK_SLOT_ALIGNMENT (data->passed_type,
3106 GET_MODE (data->entry_parm),
3107 TYPE_ALIGN (data->passed_type));
35a569c6 3108 data->stack_parm
3109 = assign_stack_local (GET_MODE (data->entry_parm),
3110 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
c9b50df7 3111 align);
35a569c6 3112 set_mem_attributes (data->stack_parm, parm, 1);
897b77d6 3113 }
35a569c6 3114
c5dc0c32 3115 dest = validize_mem (data->stack_parm);
3116 src = validize_mem (data->entry_parm);
3117
3118 if (MEM_P (src))
897b77d6 3119 {
c5dc0c32 3120 /* Use a block move to handle potentially misaligned entry_parm. */
3121 if (!to_conversion)
28bf151d 3122 push_to_sequence2 (all->first_conversion_insn,
3123 all->last_conversion_insn);
c5dc0c32 3124 to_conversion = true;
3125
3126 emit_block_move (dest, src,
3127 GEN_INT (int_size_in_bytes (data->passed_type)),
3128 BLOCK_OP_NORMAL);
35a569c6 3129 }
3130 else
c5dc0c32 3131 emit_move_insn (dest, src);
3132 }
3133
3134 if (to_conversion)
3135 {
28bf151d 3136 all->first_conversion_insn = get_insns ();
3137 all->last_conversion_insn = get_last_insn ();
c5dc0c32 3138 end_sequence ();
35a569c6 3139 }
897b77d6 3140
35a569c6 3141 SET_DECL_RTL (parm, data->stack_parm);
3142}
b8f621ce 3143
35a569c6 3144/* A subroutine of assign_parms. If the ABI splits complex arguments, then
3145 undo the frobbing that we did in assign_parms_augmented_arg_list. */
006be676 3146
35a569c6 3147static void
3e992c41 3148assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3149 VEC(tree, heap) *fnargs)
35a569c6 3150{
3151 tree parm;
e6427ef0 3152 tree orig_fnargs = all->orig_fnargs;
3e992c41 3153 unsigned i = 0;
e513d163 3154
3e992c41 3155 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
35a569c6 3156 {
3157 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3158 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3159 {
3160 rtx tmp, real, imag;
3161 enum machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
897b77d6 3162
3e992c41 3163 real = DECL_RTL (VEC_index (tree, fnargs, i));
3164 imag = DECL_RTL (VEC_index (tree, fnargs, i + 1));
35a569c6 3165 if (inner != GET_MODE (real))
897b77d6 3166 {
35a569c6 3167 real = gen_lowpart_SUBREG (inner, real);
3168 imag = gen_lowpart_SUBREG (inner, imag);
3169 }
e6427ef0 3170
3171 if (TREE_ADDRESSABLE (parm))
3172 {
3173 rtx rmem, imem;
3174 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
c9b50df7 3175 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3176 DECL_MODE (parm),
3177 TYPE_ALIGN (TREE_TYPE (parm)));
e6427ef0 3178
3179 /* split_complex_arg put the real and imag parts in
3180 pseudos. Move them to memory. */
c9b50df7 3181 tmp = assign_stack_local (DECL_MODE (parm), size, align);
e6427ef0 3182 set_mem_attributes (tmp, parm, 1);
3183 rmem = adjust_address_nv (tmp, inner, 0);
3184 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
28bf151d 3185 push_to_sequence2 (all->first_conversion_insn,
3186 all->last_conversion_insn);
e6427ef0 3187 emit_move_insn (rmem, real);
3188 emit_move_insn (imem, imag);
28bf151d 3189 all->first_conversion_insn = get_insns ();
3190 all->last_conversion_insn = get_last_insn ();
e6427ef0 3191 end_sequence ();
3192 }
3193 else
3194 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
35a569c6 3195 SET_DECL_RTL (parm, tmp);
08531d36 3196
3e992c41 3197 real = DECL_INCOMING_RTL (VEC_index (tree, fnargs, i));
3198 imag = DECL_INCOMING_RTL (VEC_index (tree, fnargs, i + 1));
35a569c6 3199 if (inner != GET_MODE (real))
3200 {
3201 real = gen_lowpart_SUBREG (inner, real);
3202 imag = gen_lowpart_SUBREG (inner, imag);
897b77d6 3203 }
35a569c6 3204 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
d91cf567 3205 set_decl_incoming_rtl (parm, tmp, false);
3e992c41 3206 i++;
897b77d6 3207 }
897b77d6 3208 }
35a569c6 3209}
3210
3211/* Assign RTL expressions to the function's parameters. This may involve
3212 copying them into registers and using those registers as the DECL_RTL. */
3213
3f0895d3 3214static void
35a569c6 3215assign_parms (tree fndecl)
3216{
3217 struct assign_parm_data_all all;
3e992c41 3218 tree parm;
3219 VEC(tree, heap) *fnargs;
3220 unsigned i;
897b77d6 3221
abe32cce 3222 crtl->args.internal_arg_pointer
567925e3 3223 = targetm.calls.internal_arg_pointer ();
35a569c6 3224
3225 assign_parms_initialize_all (&all);
3226 fnargs = assign_parms_augmented_arg_list (&all);
3227
3e992c41 3228 for (i = 0; VEC_iterate (tree, fnargs, i, parm); ++i)
915e81b8 3229 {
35a569c6 3230 struct assign_parm_data_one data;
3231
3232 /* Extract the type of PARM; adjust it according to ABI. */
3233 assign_parm_find_data_types (&all, parm, &data);
3234
3235 /* Early out for errors and void parameters. */
3236 if (data.passed_mode == VOIDmode)
915e81b8 3237 {
35a569c6 3238 SET_DECL_RTL (parm, const0_rtx);
3239 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3240 continue;
3241 }
1b4f3c7d 3242
27a7a23a 3243 /* Estimate stack alignment from parameter alignment. */
3244 if (SUPPORTS_STACK_ALIGNMENT)
3245 {
3246 unsigned int align = FUNCTION_ARG_BOUNDARY (data.promoted_mode,
3247 data.passed_type);
8645d3e7 3248 align = MINIMUM_ALIGNMENT (data.passed_type, data.promoted_mode,
3249 align);
27a7a23a 3250 if (TYPE_ALIGN (data.nominal_type) > align)
8645d3e7 3251 align = MINIMUM_ALIGNMENT (data.nominal_type,
3252 TYPE_MODE (data.nominal_type),
3253 TYPE_ALIGN (data.nominal_type));
27a7a23a 3254 if (crtl->stack_alignment_estimated < align)
3255 {
3256 gcc_assert (!crtl->stack_realign_processed);
3257 crtl->stack_alignment_estimated = align;
3258 }
3259 }
48e1416a 3260
18d50ae6 3261 if (cfun->stdarg && !TREE_CHAIN (parm))
e2704f58 3262 assign_parms_setup_varargs (&all, &data, false);
1b4f3c7d 3263
35a569c6 3264 /* Find out where the parameter arrives in this function. */
3265 assign_parm_find_entry_rtl (&all, &data);
3266
3267 /* Find out where stack space for this parameter might be. */
3268 if (assign_parm_is_stack_parm (&all, &data))
3269 {
3270 assign_parm_find_stack_rtl (parm, &data);
3271 assign_parm_adjust_entry_rtl (&data);
915e81b8 3272 }
35a569c6 3273
3274 /* Record permanently how this parm was passed. */
d91cf567 3275 set_decl_incoming_rtl (parm, data.entry_parm, data.passed_pointer);
35a569c6 3276
3277 /* Update info on where next arg arrives in registers. */
3278 FUNCTION_ARG_ADVANCE (all.args_so_far, data.promoted_mode,
3279 data.passed_type, data.named_arg);
3280
3281 assign_parm_adjust_stack_rtl (&data);
3282
3283 if (assign_parm_setup_block_p (&data))
e2ff5c1b 3284 assign_parm_setup_block (&all, parm, &data);
35a569c6 3285 else if (data.passed_pointer || use_register_for_decl (parm))
3286 assign_parm_setup_reg (&all, parm, &data);
3287 else
3288 assign_parm_setup_stack (&all, parm, &data);
915e81b8 3289 }
3290
3e992c41 3291 if (targetm.calls.split_complex_arg)
e6427ef0 3292 assign_parms_unsplit_complex (&all, fnargs);
35a569c6 3293
3e992c41 3294 VEC_free (tree, heap, fnargs);
3295
b8f621ce 3296 /* Output all parameter conversion instructions (possibly including calls)
3297 now that all parameters have been copied out of hard registers. */
28bf151d 3298 emit_insn (all.first_conversion_insn);
b8f621ce 3299
27a7a23a 3300 /* Estimate reload stack alignment from scalar return mode. */
3301 if (SUPPORTS_STACK_ALIGNMENT)
3302 {
3303 if (DECL_RESULT (fndecl))
3304 {
3305 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3306 enum machine_mode mode = TYPE_MODE (type);
3307
3308 if (mode != BLKmode
3309 && mode != VOIDmode
3310 && !AGGREGATE_TYPE_P (type))
3311 {
3312 unsigned int align = GET_MODE_ALIGNMENT (mode);
3313 if (crtl->stack_alignment_estimated < align)
3314 {
3315 gcc_assert (!crtl->stack_realign_processed);
3316 crtl->stack_alignment_estimated = align;
3317 }
3318 }
48e1416a 3319 }
27a7a23a 3320 }
3321
ba133423 3322 /* If we are receiving a struct value address as the first argument, set up
3323 the RTL for the function result. As this might require code to convert
3324 the transmitted address to Pmode, we do this here to ensure that possible
3325 preliminary conversions of the address have been emitted already. */
35a569c6 3326 if (all.function_result_decl)
ba133423 3327 {
35a569c6 3328 tree result = DECL_RESULT (current_function_decl);
3329 rtx addr = DECL_RTL (all.function_result_decl);
ba133423 3330 rtx x;
de1b648b 3331
806e4c12 3332 if (DECL_BY_REFERENCE (result))
3333 x = addr;
3334 else
3335 {
3336 addr = convert_memory_address (Pmode, addr);
3337 x = gen_rtx_MEM (DECL_MODE (result), addr);
3338 set_mem_attributes (x, result, 1);
3339 }
ba133423 3340 SET_DECL_RTL (result, x);
3341 }
3342
b0cdd2bb 3343 /* We have aligned all the args, so add space for the pretend args. */
abe32cce 3344 crtl->args.pretend_args_size = all.pretend_args_size;
35a569c6 3345 all.stack_args_size.constant += all.extra_pretend_bytes;
abe32cce 3346 crtl->args.size = all.stack_args_size.constant;
897b77d6 3347
3348 /* Adjust function incoming argument size for alignment and
3349 minimum length. */
3350
3351#ifdef REG_PARM_STACK_SPACE
abe32cce 3352 crtl->args.size = MAX (crtl->args.size,
897b77d6 3353 REG_PARM_STACK_SPACE (fndecl));
2d7187c2 3354#endif
897b77d6 3355
abe32cce 3356 crtl->args.size = CEIL_ROUND (crtl->args.size,
26be63dd 3357 PARM_BOUNDARY / BITS_PER_UNIT);
8967ddf7 3358
897b77d6 3359#ifdef ARGS_GROW_DOWNWARD
abe32cce 3360 crtl->args.arg_offset_rtx
470f4908 3361 = (all.stack_args_size.var == 0 ? GEN_INT (-all.stack_args_size.constant)
35a569c6 3362 : expand_expr (size_diffop (all.stack_args_size.var,
3363 size_int (-all.stack_args_size.constant)),
b9c74b4d 3364 NULL_RTX, VOIDmode, EXPAND_NORMAL));
897b77d6 3365#else
abe32cce 3366 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
897b77d6 3367#endif
3368
3369 /* See how many bytes, if any, of its args a function should try to pop
3370 on return. */
3371
abe32cce 3372 crtl->args.pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
3373 crtl->args.size);
897b77d6 3374
ec195bc4 3375 /* For stdarg.h function, save info about
3376 regs and stack space used by the named args. */
897b77d6 3377
abe32cce 3378 crtl->args.info = all.args_so_far;
897b77d6 3379
3380 /* Set the rtx used for the function return value. Put this in its
3381 own variable so any optimizers that need this information don't have
3382 to include tree.h. Do this here so it gets done when an inlined
3383 function gets output. */
3384
abe32cce 3385 crtl->return_rtx
0e8e37b2 3386 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3387 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
8839b7f1 3388
3389 /* If scalar return value was computed in a pseudo-reg, or was a named
3390 return value that got dumped to the stack, copy that to the hard
3391 return register. */
3392 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3393 {
3394 tree decl_result = DECL_RESULT (fndecl);
3395 rtx decl_rtl = DECL_RTL (decl_result);
3396
3397 if (REG_P (decl_rtl)
3398 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3399 : DECL_REGISTER (decl_result))
3400 {
3401 rtx real_decl_rtl;
3402
46b3ff29 3403 real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3404 fndecl, true);
8839b7f1 3405 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
abe32cce 3406 /* The delay slot scheduler assumes that crtl->return_rtx
8839b7f1 3407 holds the hard register containing the return value, not a
3408 temporary pseudo. */
abe32cce 3409 crtl->return_rtx = real_decl_rtl;
8839b7f1 3410 }
3411 }
897b77d6 3412}
6b275368 3413
3414/* A subroutine of gimplify_parameters, invoked via walk_tree.
3415 For all seen types, gimplify their sizes. */
3416
3417static tree
3418gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3419{
3420 tree t = *tp;
3421
3422 *walk_subtrees = 0;
3423 if (TYPE_P (t))
3424 {
3425 if (POINTER_TYPE_P (t))
3426 *walk_subtrees = 1;
bc97b18f 3427 else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3428 && !TYPE_SIZES_GIMPLIFIED (t))
6b275368 3429 {
75a70cf9 3430 gimplify_type_sizes (t, (gimple_seq *) data);
6b275368 3431 *walk_subtrees = 1;
3432 }
3433 }
3434
3435 return NULL;
3436}
3437
3438/* Gimplify the parameter list for current_function_decl. This involves
3439 evaluating SAVE_EXPRs of variable sized parameters and generating code
75a70cf9 3440 to implement callee-copies reference parameters. Returns a sequence of
3441 statements to add to the beginning of the function. */
6b275368 3442
75a70cf9 3443gimple_seq
6b275368 3444gimplify_parameters (void)
3445{
3446 struct assign_parm_data_all all;
3e992c41 3447 tree parm;
75a70cf9 3448 gimple_seq stmts = NULL;
3e992c41 3449 VEC(tree, heap) *fnargs;
3450 unsigned i;
6b275368 3451
3452 assign_parms_initialize_all (&all);
3453 fnargs = assign_parms_augmented_arg_list (&all);
3454
3e992c41 3455 for (i = 0; VEC_iterate (tree, fnargs, i, parm); ++i)
6b275368 3456 {
3457 struct assign_parm_data_one data;
3458
3459 /* Extract the type of PARM; adjust it according to ABI. */
3460 assign_parm_find_data_types (&all, parm, &data);
3461
3462 /* Early out for errors and void parameters. */
3463 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3464 continue;
3465
3466 /* Update info on where next arg arrives in registers. */
3467 FUNCTION_ARG_ADVANCE (all.args_so_far, data.promoted_mode,
3468 data.passed_type, data.named_arg);
3469
3470 /* ??? Once upon a time variable_size stuffed parameter list
3471 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3472 turned out to be less than manageable in the gimple world.
3473 Now we have to hunt them down ourselves. */
3474 walk_tree_without_duplicates (&data.passed_type,
3475 gimplify_parm_type, &stmts);
3476
4852b829 3477 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
6b275368 3478 {
3479 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3480 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3481 }
3482
3483 if (data.passed_pointer)
3484 {
3485 tree type = TREE_TYPE (data.passed_type);
3486 if (reference_callee_copied (&all.args_so_far, TYPE_MODE (type),
3487 type, data.named_arg))
3488 {
3489 tree local, t;
3490
4852b829 3491 /* For constant-sized objects, this is trivial; for
6b275368 3492 variable-sized objects, we have to play games. */
4852b829 3493 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3494 && !(flag_stack_check == GENERIC_STACK_CHECK
3495 && compare_tree_int (DECL_SIZE_UNIT (parm),
3496 STACK_CHECK_MAX_VAR_SIZE) > 0))
6b275368 3497 {
3498 local = create_tmp_var (type, get_name (parm));
3499 DECL_IGNORED_P (local) = 0;
ab349ddd 3500 /* If PARM was addressable, move that flag over
3501 to the local copy, as its address will be taken,
3502 not the PARMs. */
3503 if (TREE_ADDRESSABLE (parm))
3504 {
3505 TREE_ADDRESSABLE (parm) = 0;
3506 TREE_ADDRESSABLE (local) = 1;
3507 }
6b275368 3508 }
3509 else
3510 {
c2f47e15 3511 tree ptr_type, addr;
6b275368 3512
3513 ptr_type = build_pointer_type (type);
3514 addr = create_tmp_var (ptr_type, get_name (parm));
3515 DECL_IGNORED_P (addr) = 0;
3516 local = build_fold_indirect_ref (addr);
3517
6b275368 3518 t = built_in_decls[BUILT_IN_ALLOCA];
c2f47e15 3519 t = build_call_expr (t, 1, DECL_SIZE_UNIT (parm));
6b275368 3520 t = fold_convert (ptr_type, t);
75a70cf9 3521 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
6b275368 3522 gimplify_and_add (t, &stmts);
3523 }
3524
75a70cf9 3525 gimplify_assign (local, parm, &stmts);
6b275368 3526
75fa4f82 3527 SET_DECL_VALUE_EXPR (parm, local);
3528 DECL_HAS_VALUE_EXPR_P (parm) = 1;
6b275368 3529 }
3530 }
3531 }
3532
3e992c41 3533 VEC_free (tree, heap, fnargs);
3534
6b275368 3535 return stmts;
3536}
96b1130a 3537\f
897b77d6 3538/* Compute the size and offset from the start of the stacked arguments for a
3539 parm passed in mode PASSED_MODE and with type TYPE.
3540
3541 INITIAL_OFFSET_PTR points to the current offset into the stacked
3542 arguments.
3543
241399f6 3544 The starting offset and size for this parm are returned in
3545 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
3546 nonzero, the offset is that of stack slot, which is returned in
3547 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
3548 padding required from the initial offset ptr to the stack slot.
897b77d6 3549
6ef828f9 3550 IN_REGS is nonzero if the argument will be passed in registers. It will
897b77d6 3551 never be set if REG_PARM_STACK_SPACE is not defined.
3552
3553 FNDECL is the function in which the argument was defined.
3554
3555 There are two types of rounding that are done. The first, controlled by
3556 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
3557 list to be aligned to the specific boundary (in bits). This rounding
3558 affects the initial and starting offsets, but not the argument size.
3559
3560 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
3561 optionally rounds the size of the parm to PARM_BOUNDARY. The
3562 initial offset is not affected by this rounding, while the size always
3563 is and the starting offset may be. */
3564
241399f6 3565/* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
3566 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
897b77d6 3567 callers pass in the total size of args so far as
241399f6 3568 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
897b77d6 3569
897b77d6 3570void
de1b648b 3571locate_and_pad_parm (enum machine_mode passed_mode, tree type, int in_regs,
3572 int partial, tree fndecl ATTRIBUTE_UNUSED,
3573 struct args_size *initial_offset_ptr,
3574 struct locate_and_pad_arg_data *locate)
897b77d6 3575{
241399f6 3576 tree sizetree;
3577 enum direction where_pad;
90ab54b2 3578 unsigned int boundary;
241399f6 3579 int reg_parm_stack_space = 0;
3580 int part_size_in_regs;
897b77d6 3581
3582#ifdef REG_PARM_STACK_SPACE
241399f6 3583 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
241399f6 3584
897b77d6 3585 /* If we have found a stack parm before we reach the end of the
3586 area reserved for registers, skip that area. */
3587 if (! in_regs)
3588 {
897b77d6 3589 if (reg_parm_stack_space > 0)
3590 {
3591 if (initial_offset_ptr->var)
3592 {
3593 initial_offset_ptr->var
3594 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
902de8ed 3595 ssize_int (reg_parm_stack_space));
897b77d6 3596 initial_offset_ptr->constant = 0;
3597 }
3598 else if (initial_offset_ptr->constant < reg_parm_stack_space)
3599 initial_offset_ptr->constant = reg_parm_stack_space;
3600 }
3601 }
3602#endif /* REG_PARM_STACK_SPACE */
3603
f054eb3c 3604 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
241399f6 3605
3606 sizetree
3607 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
3608 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
3609 boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
5f4cd670 3610 locate->where_pad = where_pad;
27a7a23a 3611
3612 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
3613 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
3614 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
3615
c5dc0c32 3616 locate->boundary = boundary;
897b77d6 3617
27a7a23a 3618 if (SUPPORTS_STACK_ALIGNMENT)
3619 {
3620 /* stack_alignment_estimated can't change after stack has been
3621 realigned. */
3622 if (crtl->stack_alignment_estimated < boundary)
3623 {
3624 if (!crtl->stack_realign_processed)
3625 crtl->stack_alignment_estimated = boundary;
3626 else
3627 {
3628 /* If stack is realigned and stack alignment value
3629 hasn't been finalized, it is OK not to increase
3630 stack_alignment_estimated. The bigger alignment
3631 requirement is recorded in stack_alignment_needed
3632 below. */
3633 gcc_assert (!crtl->stack_realign_finalized
3634 && crtl->stack_realign_needed);
3635 }
3636 }
3637 }
3638
90ab54b2 3639 /* Remember if the outgoing parameter requires extra alignment on the
3640 calling function side. */
edb7afe8 3641 if (crtl->stack_alignment_needed < boundary)
3642 crtl->stack_alignment_needed = boundary;
27a7a23a 3643 if (crtl->preferred_stack_boundary < boundary)
3644 crtl->preferred_stack_boundary = boundary;
90ab54b2 3645
897b77d6 3646#ifdef ARGS_GROW_DOWNWARD
241399f6 3647 locate->slot_offset.constant = -initial_offset_ptr->constant;
897b77d6 3648 if (initial_offset_ptr->var)
241399f6 3649 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
3650 initial_offset_ptr->var);
ac965869 3651
241399f6 3652 {
3653 tree s2 = sizetree;
3654 if (where_pad != none
3655 && (!host_integerp (sizetree, 1)
3656 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
3657 s2 = round_up (s2, PARM_BOUNDARY / BITS_PER_UNIT);
3658 SUB_PARM_SIZE (locate->slot_offset, s2);
3659 }
3660
3661 locate->slot_offset.constant += part_size_in_regs;
ac965869 3662
3663 if (!in_regs
3664#ifdef REG_PARM_STACK_SPACE
3665 || REG_PARM_STACK_SPACE (fndecl) > 0
3666#endif
3667 )
241399f6 3668 pad_to_arg_alignment (&locate->slot_offset, boundary,
3669 &locate->alignment_pad);
ac965869 3670
241399f6 3671 locate->size.constant = (-initial_offset_ptr->constant
3672 - locate->slot_offset.constant);
897b77d6 3673 if (initial_offset_ptr->var)
241399f6 3674 locate->size.var = size_binop (MINUS_EXPR,
3675 size_binop (MINUS_EXPR,
3676 ssize_int (0),
3677 initial_offset_ptr->var),
3678 locate->slot_offset.var);
3679
3680 /* Pad_below needs the pre-rounded size to know how much to pad
3681 below. */
3682 locate->offset = locate->slot_offset;
3683 if (where_pad == downward)
3684 pad_below (&locate->offset, passed_mode, sizetree);
ac965869 3685
897b77d6 3686#else /* !ARGS_GROW_DOWNWARD */
166b1e83 3687 if (!in_regs
3688#ifdef REG_PARM_STACK_SPACE
3689 || REG_PARM_STACK_SPACE (fndecl) > 0
3690#endif
3691 )
241399f6 3692 pad_to_arg_alignment (initial_offset_ptr, boundary,
3693 &locate->alignment_pad);
3694 locate->slot_offset = *initial_offset_ptr;
897b77d6 3695
3696#ifdef PUSH_ROUNDING
3697 if (passed_mode != BLKmode)
3698 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
3699#endif
3700
82f48b55 3701 /* Pad_below needs the pre-rounded size to know how much to pad below
3702 so this must be done before rounding up. */
241399f6 3703 locate->offset = locate->slot_offset;
3704 if (where_pad == downward)
3705 pad_below (&locate->offset, passed_mode, sizetree);
82f48b55 3706
897b77d6 3707 if (where_pad != none
46107564 3708 && (!host_integerp (sizetree, 1)
3709 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
897b77d6 3710 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
3711
241399f6 3712 ADD_PARM_SIZE (locate->size, sizetree);
3713
3714 locate->size.constant -= part_size_in_regs;
897b77d6 3715#endif /* ARGS_GROW_DOWNWARD */
b704e80f 3716
3717#ifdef FUNCTION_ARG_OFFSET
3718 locate->offset.constant += FUNCTION_ARG_OFFSET (passed_mode, type);
3719#endif
897b77d6 3720}
3721
ba585215 3722/* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
3723 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
3724
897b77d6 3725static void
de1b648b 3726pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
3727 struct args_size *alignment_pad)
897b77d6 3728{
ef2c4a29 3729 tree save_var = NULL_TREE;
3730 HOST_WIDE_INT save_constant = 0;
5cf5baa2 3731 int boundary_in_bytes = boundary / BITS_PER_UNIT;
891a1732 3732 HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
3733
3734#ifdef SPARC_STACK_BOUNDARY_HACK
1aecae7f 3735 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
3736 the real alignment of %sp. However, when it does this, the
3737 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
891a1732 3738 if (SPARC_STACK_BOUNDARY_HACK)
3739 sp_offset = 0;
3740#endif
9d855d2f 3741
b3f75873 3742 if (boundary > PARM_BOUNDARY)
9d855d2f 3743 {
3744 save_var = offset_ptr->var;
3745 save_constant = offset_ptr->constant;
3746 }
3747
3748 alignment_pad->var = NULL_TREE;
3749 alignment_pad->constant = 0;
9d855d2f 3750
897b77d6 3751 if (boundary > BITS_PER_UNIT)
3752 {
3753 if (offset_ptr->var)
3754 {
891a1732 3755 tree sp_offset_tree = ssize_int (sp_offset);
3756 tree offset = size_binop (PLUS_EXPR,
3757 ARGS_SIZE_TREE (*offset_ptr),
3758 sp_offset_tree);
897b77d6 3759#ifdef ARGS_GROW_DOWNWARD
891a1732 3760 tree rounded = round_down (offset, boundary / BITS_PER_UNIT);
897b77d6 3761#else
891a1732 3762 tree rounded = round_up (offset, boundary / BITS_PER_UNIT);
897b77d6 3763#endif
891a1732 3764
3765 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
241399f6 3766 /* ARGS_SIZE_TREE includes constant term. */
3767 offset_ptr->constant = 0;
b3f75873 3768 if (boundary > PARM_BOUNDARY)
d3371fcd 3769 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
902de8ed 3770 save_var);
897b77d6 3771 }
3772 else
06ebc183 3773 {
891a1732 3774 offset_ptr->constant = -sp_offset +
897b77d6 3775#ifdef ARGS_GROW_DOWNWARD
891a1732 3776 FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
897b77d6 3777#else
891a1732 3778 CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
897b77d6 3779#endif
b3f75873 3780 if (boundary > PARM_BOUNDARY)
06ebc183 3781 alignment_pad->constant = offset_ptr->constant - save_constant;
3782 }
897b77d6 3783 }
3784}
3785
3786static void
de1b648b 3787pad_below (struct args_size *offset_ptr, enum machine_mode passed_mode, tree sizetree)
897b77d6 3788{
3789 if (passed_mode != BLKmode)
3790 {
3791 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
3792 offset_ptr->constant
3793 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
3794 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
3795 - GET_MODE_SIZE (passed_mode));
3796 }
3797 else
3798 {
3799 if (TREE_CODE (sizetree) != INTEGER_CST
3800 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
3801 {
3802 /* Round the size up to multiple of PARM_BOUNDARY bits. */
3803 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
3804 /* Add it in. */
3805 ADD_PARM_SIZE (*offset_ptr, s2);
3806 SUB_PARM_SIZE (*offset_ptr, sizetree);
3807 }
3808 }
3809}
897b77d6 3810\f
897b77d6 3811
3072d30e 3812/* True if register REGNO was alive at a place where `setjmp' was
3813 called and was set more than once or is an argument. Such regs may
3814 be clobbered by `longjmp'. */
3815
3816static bool
3817regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
3818{
3819 /* There appear to be cases where some local vars never reach the
3820 backend but have bogus regnos. */
3821 if (regno >= max_reg_num ())
3822 return false;
3823
3824 return ((REG_N_SETS (regno) > 1
3825 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), regno))
3826 && REGNO_REG_SET_P (setjmp_crosses, regno));
3827}
3828
3829/* Walk the tree of blocks describing the binding levels within a
3830 function and warn about variables the might be killed by setjmp or
3831 vfork. This is done after calling flow_analysis before register
3832 allocation since that will clobber the pseudo-regs to hard
3833 regs. */
3834
3835static void
3836setjmp_vars_warning (bitmap setjmp_crosses, tree block)
897b77d6 3837{
19cb6b50 3838 tree decl, sub;
4ee9c684 3839
897b77d6 3840 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
3841 {
4ee9c684 3842 if (TREE_CODE (decl) == VAR_DECL
49bf95f0 3843 && DECL_RTL_SET_P (decl)
8ad4c111 3844 && REG_P (DECL_RTL (decl))
3072d30e 3845 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
48e1416a 3846 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
0d438110 3847 " %<longjmp%> or %<vfork%>", decl);
897b77d6 3848 }
4ee9c684 3849
93110716 3850 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
3072d30e 3851 setjmp_vars_warning (setjmp_crosses, sub);
897b77d6 3852}
3853
4ee9c684 3854/* Do the appropriate part of setjmp_vars_warning
897b77d6 3855 but for arguments instead of local variables. */
3856
3072d30e 3857static void
3858setjmp_args_warning (bitmap setjmp_crosses)
897b77d6 3859{
19cb6b50 3860 tree decl;
897b77d6 3861 for (decl = DECL_ARGUMENTS (current_function_decl);
3862 decl; decl = TREE_CHAIN (decl))
3863 if (DECL_RTL (decl) != 0
8ad4c111 3864 && REG_P (DECL_RTL (decl))
3072d30e 3865 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
48e1416a 3866 warning (OPT_Wclobbered,
0d438110 3867 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
3cf8b391 3868 decl);
897b77d6 3869}
3870
3072d30e 3871/* Generate warning messages for variables live across setjmp. */
3872
48e1416a 3873void
3072d30e 3874generate_setjmp_warnings (void)
3875{
3876 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
3877
3878 if (n_basic_blocks == NUM_FIXED_BLOCKS
3879 || bitmap_empty_p (setjmp_crosses))
3880 return;
3881
3882 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
3883 setjmp_args_warning (setjmp_crosses);
3884}
3885
897b77d6 3886\f
a36145ca 3887/* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
3888 and create duplicate blocks. */
3889/* ??? Need an option to either create block fragments or to create
3890 abstract origin duplicates of a source block. It really depends
3891 on what optimization has been performed. */
11b373ff 3892
f1ab82be 3893void
de1b648b 3894reorder_blocks (void)
11b373ff 3895{
f1ab82be 3896 tree block = DECL_INITIAL (current_function_decl);
573a97ad 3897 VEC(tree,heap) *block_stack;
11b373ff 3898
0c45344e 3899 if (block == NULL_TREE)
f1ab82be 3900 return;
9d819987 3901
573a97ad 3902 block_stack = VEC_alloc (tree, heap, 10);
5846cb0f 3903
a36145ca 3904 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4ee9c684 3905 clear_block_marks (block);
a36145ca 3906
f1ab82be 3907 /* Prune the old trees away, so that they don't get in the way. */
3908 BLOCK_SUBBLOCKS (block) = NULL_TREE;
3909 BLOCK_CHAIN (block) = NULL_TREE;
9d819987 3910
a36145ca 3911 /* Recreate the block tree from the note nesting. */
f1ab82be 3912 reorder_blocks_1 (get_insns (), block, &block_stack);
06ebc183 3913 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
5846cb0f 3914
573a97ad 3915 VEC_free (tree, heap, block_stack);
11b373ff 3916}
3917
a36145ca 3918/* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
60ecc450 3919
4ee9c684 3920void
3921clear_block_marks (tree block)
5e960ca9 3922{
a36145ca 3923 while (block)
5e960ca9 3924 {
a36145ca 3925 TREE_ASM_WRITTEN (block) = 0;
4ee9c684 3926 clear_block_marks (BLOCK_SUBBLOCKS (block));
a36145ca 3927 block = BLOCK_CHAIN (block);
5e960ca9 3928 }
3929}
3930
60ecc450 3931static void
573a97ad 3932reorder_blocks_1 (rtx insns, tree current_block, VEC(tree,heap) **p_block_stack)
60ecc450 3933{
3934 rtx insn;
3935
3936 for (insn = insns; insn; insn = NEXT_INSN (insn))
3937 {
6d7dc5b9 3938 if (NOTE_P (insn))
60ecc450 3939 {
ad4583d9 3940 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
60ecc450 3941 {
3942 tree block = NOTE_BLOCK (insn);
70392493 3943 tree origin;
3944
3945 origin = (BLOCK_FRAGMENT_ORIGIN (block)
3946 ? BLOCK_FRAGMENT_ORIGIN (block)
3947 : block);
a36145ca 3948
3949 /* If we have seen this block before, that means it now
3950 spans multiple address regions. Create a new fragment. */
60ecc450 3951 if (TREE_ASM_WRITTEN (block))
3952 {
a36145ca 3953 tree new_block = copy_node (block);
a36145ca 3954
a36145ca 3955 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
3956 BLOCK_FRAGMENT_CHAIN (new_block)
3957 = BLOCK_FRAGMENT_CHAIN (origin);
3958 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
3959
3960 NOTE_BLOCK (insn) = new_block;
3961 block = new_block;
60ecc450 3962 }
a36145ca 3963
60ecc450 3964 BLOCK_SUBBLOCKS (block) = 0;
3965 TREE_ASM_WRITTEN (block) = 1;
31ddae9f 3966 /* When there's only one block for the entire function,
3967 current_block == block and we mustn't do this, it
3968 will cause infinite recursion. */
3969 if (block != current_block)
3970 {
70392493 3971 if (block != origin)
3972 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block);
3973
31ddae9f 3974 BLOCK_SUPERCONTEXT (block) = current_block;
3975 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
3976 BLOCK_SUBBLOCKS (current_block) = block;
70392493 3977 current_block = origin;
31ddae9f 3978 }
573a97ad 3979 VEC_safe_push (tree, heap, *p_block_stack, block);
60ecc450 3980 }
ad4583d9 3981 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
60ecc450 3982 {
573a97ad 3983 NOTE_BLOCK (insn) = VEC_pop (tree, *p_block_stack);
60ecc450 3984 BLOCK_SUBBLOCKS (current_block)
3985 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
3986 current_block = BLOCK_SUPERCONTEXT (current_block);
3987 }
3988 }
60ecc450 3989 }
3990}
3991
11b373ff 3992/* Reverse the order of elements in the chain T of blocks,
3993 and return the new head of the chain (old last element). */
3994
4ee9c684 3995tree
de1b648b 3996blocks_nreverse (tree t)
11b373ff 3997{
19cb6b50 3998 tree prev = 0, decl, next;
11b373ff 3999 for (decl = t; decl; decl = next)
4000 {
4001 next = BLOCK_CHAIN (decl);
4002 BLOCK_CHAIN (decl) = prev;
4003 prev = decl;
4004 }
4005 return prev;
4006}
4007
5846cb0f 4008/* Count the subblocks of the list starting with BLOCK. If VECTOR is
4009 non-NULL, list them all into VECTOR, in a depth-first preorder
4010 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
396bfb69 4011 blocks. */
11b373ff 4012
4013static int
de1b648b 4014all_blocks (tree block, tree *vector)
11b373ff 4015{
396bfb69 4016 int n_blocks = 0;
4017
874a9b8d 4018 while (block)
4019 {
4020 TREE_ASM_WRITTEN (block) = 0;
396bfb69 4021
874a9b8d 4022 /* Record this block. */
4023 if (vector)
4024 vector[n_blocks] = block;
396bfb69 4025
874a9b8d 4026 ++n_blocks;
06ebc183 4027
874a9b8d 4028 /* Record the subblocks, and their subblocks... */
4029 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4030 vector ? vector + n_blocks : 0);
4031 block = BLOCK_CHAIN (block);
4032 }
11b373ff 4033
4034 return n_blocks;
4035}
5846cb0f 4036
4037/* Return a vector containing all the blocks rooted at BLOCK. The
4038 number of elements in the vector is stored in N_BLOCKS_P. The
4039 vector is dynamically allocated; it is the caller's responsibility
4040 to call `free' on the pointer returned. */
06ebc183 4041
5846cb0f 4042static tree *
de1b648b 4043get_block_vector (tree block, int *n_blocks_p)
5846cb0f 4044{
4045 tree *block_vector;
4046
4047 *n_blocks_p = all_blocks (block, NULL);
4c36ffe6 4048 block_vector = XNEWVEC (tree, *n_blocks_p);
5846cb0f 4049 all_blocks (block, block_vector);
4050
4051 return block_vector;
4052}
4053
177c2ebc 4054static GTY(()) int next_block_index = 2;
5846cb0f 4055
4056/* Set BLOCK_NUMBER for all the blocks in FN. */
4057
4058void
de1b648b 4059number_blocks (tree fn)
5846cb0f 4060{
4061 int i;
4062 int n_blocks;
4063 tree *block_vector;
4064
4065 /* For SDB and XCOFF debugging output, we start numbering the blocks
4066 from 1 within each function, rather than keeping a running
4067 count. */
4068#if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
0eb76379 4069 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
4070 next_block_index = 1;
5846cb0f 4071#endif
4072
4073 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4074
4075 /* The top-level BLOCK isn't numbered at all. */
4076 for (i = 1; i < n_blocks; ++i)
4077 /* We number the blocks from two. */
4078 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4079
4080 free (block_vector);
4081
4082 return;
4083}
baa8dec7 4084
4085/* If VAR is present in a subblock of BLOCK, return the subblock. */
4086
4b987fac 4087DEBUG_FUNCTION tree
de1b648b 4088debug_find_var_in_block_tree (tree var, tree block)
baa8dec7 4089{
4090 tree t;
4091
4092 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4093 if (t == var)
4094 return block;
4095
4096 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4097 {
4098 tree ret = debug_find_var_in_block_tree (var, t);
4099 if (ret)
4100 return ret;
4101 }
4102
4103 return NULL_TREE;
4104}
11b373ff 4105\f
87d4aa85 4106/* Keep track of whether we're in a dummy function context. If we are,
4107 we don't want to invoke the set_current_function hook, because we'll
4108 get into trouble if the hook calls target_reinit () recursively or
4109 when the initial initialization is not yet complete. */
4110
4111static bool in_dummy_function;
4112
46f8e3b0 4113/* Invoke the target hook when setting cfun. Update the optimization options
4114 if the function uses different options than the default. */
87d4aa85 4115
4116static void
4117invoke_set_current_function_hook (tree fndecl)
4118{
4119 if (!in_dummy_function)
46f8e3b0 4120 {
4121 tree opts = ((fndecl)
4122 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4123 : optimization_default_node);
4124
4125 if (!opts)
4126 opts = optimization_default_node;
4127
4128 /* Change optimization options if needed. */
4129 if (optimization_current_node != opts)
4130 {
4131 optimization_current_node = opts;
4132 cl_optimization_restore (TREE_OPTIMIZATION (opts));
4133 }
4134
4135 targetm.set_current_function (fndecl);
4136 }
87d4aa85 4137}
4138
4139/* cfun should never be set directly; use this function. */
4140
4141void
4142set_cfun (struct function *new_cfun)
4143{
4144 if (cfun != new_cfun)
4145 {
4146 cfun = new_cfun;
4147 invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4148 }
4149}
4150
87d4aa85 4151/* Initialized with NOGC, making this poisonous to the garbage collector. */
4152
4153static VEC(function_p,heap) *cfun_stack;
4154
4155/* Push the current cfun onto the stack, and set cfun to new_cfun. */
4156
4157void
4158push_cfun (struct function *new_cfun)
4159{
4160 VEC_safe_push (function_p, heap, cfun_stack, cfun);
4161 set_cfun (new_cfun);
4162}
4163
4164/* Pop cfun from the stack. */
4165
4166void
4167pop_cfun (void)
4168{
3c9dcda1 4169 struct function *new_cfun = VEC_pop (function_p, cfun_stack);
3c9dcda1 4170 set_cfun (new_cfun);
87d4aa85 4171}
a3adcd4a 4172
4173/* Return value of funcdef and increase it. */
4174int
48e1416a 4175get_next_funcdef_no (void)
a3adcd4a 4176{
4177 return funcdef_no++;
4178}
4179
ecc82929 4180/* Allocate a function structure for FNDECL and set its contents
87d4aa85 4181 to the defaults. Set cfun to the newly-allocated object.
4182 Some of the helper functions invoked during initialization assume
4183 that cfun has already been set. Therefore, assign the new object
4184 directly into cfun and invoke the back end hook explicitly at the
4185 very end, rather than initializing a temporary and calling set_cfun
4186 on it.
80f2ef47 4187
4188 ABSTRACT_P is true if this is a function that will never be seen by
4189 the middle-end. Such functions are front-end concepts (like C++
4190 function templates) that do not correspond directly to functions
4191 placed in object files. */
942cc45f 4192
ecc82929 4193void
80f2ef47 4194allocate_struct_function (tree fndecl, bool abstract_p)
897b77d6 4195{
ecc82929 4196 tree result;
4ee9c684 4197 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
897b77d6 4198
2457c754 4199 cfun = GGC_CNEW (struct function);
304c5bf1 4200
ecc82929 4201 init_eh_for_function ();
897b77d6 4202
ecc82929 4203 if (init_machine_status)
4204 cfun->machine = (*init_machine_status) ();
26df1c5e 4205
d3feb168 4206#ifdef OVERRIDE_ABI_FORMAT
4207 OVERRIDE_ABI_FORMAT (fndecl);
4208#endif
4209
342ad2d6 4210 invoke_set_current_function_hook (fndecl);
4211
22c61100 4212 if (fndecl != NULL_TREE)
ecc82929 4213 {
87d4aa85 4214 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4215 cfun->decl = fndecl;
285aabd1 4216 current_function_funcdef_no = get_next_funcdef_no ();
87d4aa85 4217
4218 result = DECL_RESULT (fndecl);
80f2ef47 4219 if (!abstract_p && aggregate_value_p (result, fndecl))
87d4aa85 4220 {
ecc82929 4221#ifdef PCC_STATIC_STRUCT_RETURN
18d50ae6 4222 cfun->returns_pcc_struct = 1;
ecc82929 4223#endif
18d50ae6 4224 cfun->returns_struct = 1;
87d4aa85 4225 }
4226
18d50ae6 4227 cfun->stdarg
87d4aa85 4228 = (fntype
4229 && TYPE_ARG_TYPES (fntype) != 0
4230 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4231 != void_type_node));
48e1416a 4232
87d4aa85 4233 /* Assume all registers in stdarg functions need to be saved. */
4234 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4235 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
cbeb677e 4236
4237 /* ??? This could be set on a per-function basis by the front-end
4238 but is this worth the hassle? */
4239 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
ecc82929 4240 }
87d4aa85 4241}
4242
4243/* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4244 instead of just setting it. */
a6c787e5 4245
87d4aa85 4246void
4247push_struct_function (tree fndecl)
4248{
4249 VEC_safe_push (function_p, heap, cfun_stack, cfun);
80f2ef47 4250 allocate_struct_function (fndecl, false);
ecc82929 4251}
897b77d6 4252
cbeb677e 4253/* Reset crtl and other non-struct-function variables to defaults as
f024691d 4254 appropriate for emitting rtl at the start of a function. */
897b77d6 4255
ecc82929 4256static void
87d4aa85 4257prepare_function_start (void)
ecc82929 4258{
fd6ffb7c 4259 gcc_assert (!crtl->emit.x_last_insn);
fef299ce 4260 init_temp_slots ();
957211e4 4261 init_emit ();
b079a207 4262 init_varasm_status ();
957211e4 4263 init_expr ();
7dfb44a0 4264 default_rtl_profile ();
897b77d6 4265
ecc82929 4266 cse_not_expected = ! optimize;
897b77d6 4267
ecc82929 4268 /* Caller save not needed yet. */
4269 caller_save_needed = 0;
897b77d6 4270
ecc82929 4271 /* We haven't done register allocation yet. */
4272 reg_renumber = 0;
897b77d6 4273
304c5bf1 4274 /* Indicate that we have not instantiated virtual registers yet. */
4275 virtuals_instantiated = 0;
4276
316bc009 4277 /* Indicate that we want CONCATs now. */
4278 generating_concat_p = 1;
4279
304c5bf1 4280 /* Indicate we have no need of a frame pointer yet. */
4281 frame_pointer_needed = 0;
304c5bf1 4282}
4283
4284/* Initialize the rtl expansion mechanism so that we can do simple things
4285 like generate sequences. This is used to provide a context during global
87d4aa85 4286 initialization of some passes. You must call expand_dummy_function_end
4287 to exit this context. */
4288
304c5bf1 4289void
de1b648b 4290init_dummy_function_start (void)
304c5bf1 4291{
87d4aa85 4292 gcc_assert (!in_dummy_function);
4293 in_dummy_function = true;
4294 push_struct_function (NULL_TREE);
4295 prepare_function_start ();
304c5bf1 4296}
4297
4298/* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4299 and initialize static variables for generating RTL for the statements
4300 of the function. */
4301
4302void
de1b648b 4303init_function_start (tree subr)
304c5bf1 4304{
87d4aa85 4305 if (subr && DECL_STRUCT_FUNCTION (subr))
4306 set_cfun (DECL_STRUCT_FUNCTION (subr));
4307 else
80f2ef47 4308 allocate_struct_function (subr, false);
87d4aa85 4309 prepare_function_start ();
304c5bf1 4310
897b77d6 4311 /* Warn if this value is an aggregate type,
4312 regardless of which calling convention we are using for it. */
efb9d9ee 4313 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4314 warning (OPT_Waggregate_return, "function returns an aggregate");
0a893c29 4315}
a590d94d 4316
25e880b1 4317/* Make sure all values used by the optimization passes have sane defaults. */
2a1990e9 4318unsigned int
de1b648b 4319init_function_for_compilation (void)
0a893c29 4320{
4321 reg_renumber = 0;
2a1990e9 4322 return 0;
897b77d6 4323}
4324
20099e35 4325struct rtl_opt_pass pass_init_function =
77fce4cd 4326{
20099e35 4327 {
4328 RTL_PASS,
0c297edc 4329 "*init_function", /* name */
48e1416a 4330 NULL, /* gate */
4331 init_function_for_compilation, /* execute */
77fce4cd 4332 NULL, /* sub */
4333 NULL, /* next */
4334 0, /* static_pass_number */
0b1615c1 4335 TV_NONE, /* tv_id */
77fce4cd 4336 0, /* properties_required */
4337 0, /* properties_provided */
4338 0, /* properties_destroyed */
4339 0, /* todo_flags_start */
20099e35 4340 0 /* todo_flags_finish */
4341 }
77fce4cd 4342};
4343
4344
897b77d6 4345void
de1b648b 4346expand_main_function (void)
897b77d6 4347{
72f9fc81 4348#if (defined(INVOKE__main) \
4349 || (!defined(HAS_INIT_SECTION) \
4350 && !defined(INIT_SECTION_ASM_OP) \
4351 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
62f615b1 4352 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
3ca5a306 4353#endif
897b77d6 4354}
4355\f
f1a0edff 4356/* Expand code to initialize the stack_protect_guard. This is invoked at
4357 the beginning of a function to be protected. */
4358
4359#ifndef HAVE_stack_protect_set
4360# define HAVE_stack_protect_set 0
4361# define gen_stack_protect_set(x,y) (gcc_unreachable (), NULL_RTX)
4362#endif
4363
4364void
4365stack_protect_prologue (void)
4366{
4367 tree guard_decl = targetm.stack_protect_guard ();
4368 rtx x, y;
4369
d2a99f05 4370 x = expand_normal (crtl->stack_protect_guard);
4371 y = expand_normal (guard_decl);
f1a0edff 4372
4373 /* Allow the target to copy from Y to X without leaking Y into a
4374 register. */
4375 if (HAVE_stack_protect_set)
4376 {
4377 rtx insn = gen_stack_protect_set (x, y);
4378 if (insn)
4379 {
4380 emit_insn (insn);
4381 return;
4382 }
4383 }
4384
4385 /* Otherwise do a straight move. */
4386 emit_move_insn (x, y);
4387}
4388
4389/* Expand code to verify the stack_protect_guard. This is invoked at
4390 the end of a function to be protected. */
4391
4392#ifndef HAVE_stack_protect_test
bbd33557 4393# define HAVE_stack_protect_test 0
4394# define gen_stack_protect_test(x, y, z) (gcc_unreachable (), NULL_RTX)
f1a0edff 4395#endif
4396
71d89928 4397void
f1a0edff 4398stack_protect_epilogue (void)
4399{
4400 tree guard_decl = targetm.stack_protect_guard ();
4401 rtx label = gen_label_rtx ();
4402 rtx x, y, tmp;
4403
d2a99f05 4404 x = expand_normal (crtl->stack_protect_guard);
4405 y = expand_normal (guard_decl);
f1a0edff 4406
4407 /* Allow the target to compare Y with X without leaking either into
4408 a register. */
4409 switch (HAVE_stack_protect_test != 0)
4410 {
4411 case 1:
419bcc46 4412 tmp = gen_stack_protect_test (x, y, label);
f1a0edff 4413 if (tmp)
4414 {
4415 emit_insn (tmp);
f1a0edff 4416 break;
4417 }
4418 /* FALLTHRU */
4419
4420 default:
4421 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
4422 break;
4423 }
4424
4425 /* The noreturn predictor has been moved to the tree level. The rtl-level
4426 predictors estimate this branch about 20%, which isn't enough to get
4427 things moved out of line. Since this is the only extant case of adding
4428 a noreturn function at the rtl level, it doesn't seem worth doing ought
4429 except adding the prediction by hand. */
4430 tmp = get_last_insn ();
4431 if (JUMP_P (tmp))
4432 predict_insn_def (tmp, PRED_NORETURN, TAKEN);
4433
4434 expand_expr_stmt (targetm.stack_protect_fail ());
4435 emit_label (label);
4436}
4437\f
897b77d6 4438/* Start the RTL for a new function, and set variables used for
4439 emitting RTL.
4440 SUBR is the FUNCTION_DECL node.
4441 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
4442 the function's parameters, which must be run at any return statement. */
4443
4444void
82aa4bd5 4445expand_function_start (tree subr)
897b77d6 4446{
897b77d6 4447 /* Make sure volatile mem refs aren't considered
4448 valid operands of arithmetic insns. */
4449 init_recog_no_volatile ();
4450
18d50ae6 4451 crtl->profile
7811c823 4452 = (profile_flag
4453 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
4454
18d50ae6 4455 crtl->limit_stack
8f8ac140 4456 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
4457
df4b504c 4458 /* Make the label for return statements to jump to. Do not special
4459 case machines with special return instructions -- they will be
4460 handled later during jump, ifcvt, or epilogue creation. */
897b77d6 4461 return_label = gen_label_rtx ();
897b77d6 4462
4463 /* Initialize rtx used to return the value. */
4464 /* Do this before assign_parms so that we copy the struct value address
4465 before any library calls that assign parms might generate. */
4466
4467 /* Decide whether to return the value in memory or in a register. */
45550790 4468 if (aggregate_value_p (DECL_RESULT (subr), subr))
897b77d6 4469 {
4470 /* Returning something that won't go in a register. */
19cb6b50 4471 rtx value_address = 0;
897b77d6 4472
4473#ifdef PCC_STATIC_STRUCT_RETURN
18d50ae6 4474 if (cfun->returns_pcc_struct)
897b77d6 4475 {
4476 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
4477 value_address = assemble_static_space (size);
4478 }
4479 else
4480#endif
4481 {
d8c09ceb 4482 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
897b77d6 4483 /* Expect to be passed the address of a place to store the value.
4484 If it is passed as an argument, assign_parms will take care of
4485 it. */
45550790 4486 if (sv)
897b77d6 4487 {
4488 value_address = gen_reg_rtx (Pmode);
45550790 4489 emit_move_insn (value_address, sv);
897b77d6 4490 }
4491 }
4492 if (value_address)
ce88c7f0 4493 {
648c102e 4494 rtx x = value_address;
4495 if (!DECL_BY_REFERENCE (DECL_RESULT (subr)))
4496 {
4497 x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), x);
4498 set_mem_attributes (x, DECL_RESULT (subr), 1);
4499 }
d05cd611 4500 SET_DECL_RTL (DECL_RESULT (subr), x);
ce88c7f0 4501 }
897b77d6 4502 }
4503 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
4504 /* If return mode is void, this decl rtl should not be used. */
0e8e37b2 4505 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
7ab29b28 4506 else
7e8dfb30 4507 {
7ab29b28 4508 /* Compute the return values into a pseudo reg, which we will copy
4509 into the true return register after the cleanups are done. */
05d18e8b 4510 tree return_type = TREE_TYPE (DECL_RESULT (subr));
4511 if (TYPE_MODE (return_type) != BLKmode
4512 && targetm.calls.return_in_msb (return_type))
4513 /* expand_function_end will insert the appropriate padding in
4514 this case. Use the return value's natural (unpadded) mode
4515 within the function proper. */
4516 SET_DECL_RTL (DECL_RESULT (subr),
4517 gen_reg_rtx (TYPE_MODE (return_type)));
92f708ec 4518 else
fdada98f 4519 {
05d18e8b 4520 /* In order to figure out what mode to use for the pseudo, we
4521 figure out what the mode of the eventual return register will
4522 actually be, and use that. */
46b3ff29 4523 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
05d18e8b 4524
4525 /* Structures that are returned in registers are not
4526 aggregate_value_p, so we may see a PARALLEL or a REG. */
4527 if (REG_P (hard_reg))
4528 SET_DECL_RTL (DECL_RESULT (subr),
4529 gen_reg_rtx (GET_MODE (hard_reg)));
4530 else
4531 {
4532 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
4533 SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
4534 }
fdada98f 4535 }
7e8dfb30 4536
b566e2e5 4537 /* Set DECL_REGISTER flag so that expand_function_end will copy the
4538 result to the real return register(s). */
4539 DECL_REGISTER (DECL_RESULT (subr)) = 1;
7e8dfb30 4540 }
897b77d6 4541
4542 /* Initialize rtx for parameters and local variables.
4543 In some cases this requires emitting insns. */
bffcf014 4544 assign_parms (subr);
897b77d6 4545
4ee9c684 4546 /* If function gets a static chain arg, store it. */
4547 if (cfun->static_chain_decl)
4548 {
3efaa21f 4549 tree parm = cfun->static_chain_decl;
82c7907c 4550 rtx local, chain, insn;
3efaa21f 4551
82c7907c 4552 local = gen_reg_rtx (Pmode);
4553 chain = targetm.calls.static_chain (current_function_decl, true);
4554
4555 set_decl_incoming_rtl (parm, chain, false);
3efaa21f 4556 SET_DECL_RTL (parm, local);
3efaa21f 4557 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4ee9c684 4558
82c7907c 4559 insn = emit_move_insn (local, chain);
4560
4561 /* Mark the register as eliminable, similar to parameters. */
4562 if (MEM_P (chain)
4563 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
4564 set_unique_reg_note (insn, REG_EQUIV, chain);
4ee9c684 4565 }
4566
4567 /* If the function receives a non-local goto, then store the
4568 bits we need to restore the frame pointer. */
4569 if (cfun->nonlocal_goto_save_area)
4570 {
4571 tree t_save;
4572 rtx r_save;
4573
4574 /* ??? We need to do this save early. Unfortunately here is
4575 before the frame variable gets declared. Help out... */
1a105fae 4576 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
4577 if (!DECL_RTL_SET_P (var))
4578 expand_decl (var);
4ee9c684 4579
b55f9493 4580 t_save = build4 (ARRAY_REF, ptr_type_node,
4581 cfun->nonlocal_goto_save_area,
4582 integer_zero_node, NULL_TREE, NULL_TREE);
4ee9c684 4583 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
3dce56cc 4584 r_save = convert_memory_address (Pmode, r_save);
50c48f9b 4585
6a5dfe57 4586 emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
4ee9c684 4587 update_nonlocal_goto_save_area ();
4588 }
50c48f9b 4589
897b77d6 4590 /* The following was moved from init_function_start.
4591 The move is supposed to make sdb output more accurate. */
4592 /* Indicate the beginning of the function body,
4593 as opposed to parm setup. */
31b97e8f 4594 emit_note (NOTE_INSN_FUNCTION_BEG);
897b77d6 4595
1edb3690 4596 gcc_assert (NOTE_P (get_last_insn ()));
4597
897b77d6 4598 parm_birth_insn = get_last_insn ();
4599
18d50ae6 4600 if (crtl->profile)
b8a21949 4601 {
b8a21949 4602#ifdef PROFILE_HOOK
4781f9b9 4603 PROFILE_HOOK (current_function_funcdef_no);
104d9861 4604#endif
b8a21949 4605 }
104d9861 4606
1edb3690 4607 /* After the display initializations is where the stack checking
4608 probe should go. */
4609 if(flag_stack_check)
4610 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
897b77d6 4611
897b77d6 4612 /* Make sure there is a line number after the function entry setup code. */
4613 force_next_line_note ();
4614}
4615\f
0a893c29 4616/* Undo the effects of init_dummy_function_start. */
4617void
de1b648b 4618expand_dummy_function_end (void)
0a893c29 4619{
87d4aa85 4620 gcc_assert (in_dummy_function);
4621
0a893c29 4622 /* End any sequences that failed to be closed due to syntax errors. */
4623 while (in_sequence_p ())
4624 end_sequence ();
4625
4626 /* Outside function body, can't compute type's actual size
4627 until next function's body starts. */
3c3bb268 4628
08513b52 4629 free_after_parsing (cfun);
4630 free_after_compilation (cfun);
87d4aa85 4631 pop_cfun ();
4632 in_dummy_function = false;
0a893c29 4633}
4634
2766437e 4635/* Call DOIT for each hard register used as a return value from
4636 the current function. */
631ef7ce 4637
4638void
de1b648b 4639diddle_return_value (void (*doit) (rtx, void *), void *arg)
631ef7ce 4640{
abe32cce 4641 rtx outgoing = crtl->return_rtx;
2766437e 4642
4643 if (! outgoing)
4644 return;
631ef7ce 4645
8ad4c111 4646 if (REG_P (outgoing))
2766437e 4647 (*doit) (outgoing, arg);
4648 else if (GET_CODE (outgoing) == PARALLEL)
4649 {
4650 int i;
631ef7ce 4651
2766437e 4652 for (i = 0; i < XVECLEN (outgoing, 0); i++)
4653 {
4654 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
4655
8ad4c111 4656 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
2766437e 4657 (*doit) (x, arg);
631ef7ce 4658 }
4659 }
4660}
4661
2766437e 4662static void
de1b648b 4663do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
2766437e 4664{
18b42941 4665 emit_clobber (reg);
2766437e 4666}
4667
4668void
de1b648b 4669clobber_return_register (void)
2766437e 4670{
4671 diddle_return_value (do_clobber_return_reg, NULL);
1b2c7cbd 4672
4673 /* In case we do use pseudo to return value, clobber it too. */
4674 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
4675 {
4676 tree decl_result = DECL_RESULT (current_function_decl);
4677 rtx decl_rtl = DECL_RTL (decl_result);
4678 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
4679 {
4680 do_clobber_return_reg (decl_rtl, NULL);
4681 }
4682 }
2766437e 4683}
4684
4685static void
de1b648b 4686do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
2766437e 4687{
18b42941 4688 emit_use (reg);
2766437e 4689}
4690
ab4605bf 4691static void
de1b648b 4692use_return_register (void)
2766437e 4693{
4694 diddle_return_value (do_use_return_reg, NULL);
4695}
4696
b69eb0ff 4697/* Possibly warn about unused parameters. */
4698void
4699do_warn_unused_parameter (tree fn)
4700{
4701 tree decl;
4702
4703 for (decl = DECL_ARGUMENTS (fn);
4704 decl; decl = TREE_CHAIN (decl))
4705 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
76776e6d 4706 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
4707 && !TREE_NO_WARNING (decl))
e8d0745d 4708 warning (OPT_Wunused_parameter, "unused parameter %q+D", decl);
b69eb0ff 4709}
4710
1f3233d1 4711static GTY(()) rtx initial_trampoline;
4712
6473f3f4 4713/* Generate RTL for the end of the current function. */
897b77d6 4714
4715void
de1b648b 4716expand_function_end (void)
897b77d6 4717{
9b56368f 4718 rtx clobber_after;
897b77d6 4719
2032b31d 4720 /* If arg_pointer_save_area was referenced only from a nested
4721 function, we will not have initialized it yet. Do that now. */
18d50ae6 4722 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
b079a207 4723 get_arg_pointer_save_area ();
2032b31d 4724
4852b829 4725 /* If we are doing generic stack checking and this function makes calls,
b22178d2 4726 do a stack probe at the start of the function to ensure we have enough
4727 space for another stack frame. */
4852b829 4728 if (flag_stack_check == GENERIC_STACK_CHECK)
b22178d2 4729 {
4730 rtx insn, seq;
4731
4732 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6d7dc5b9 4733 if (CALL_P (insn))
b22178d2 4734 {
d1b92264 4735 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
b22178d2 4736 start_sequence ();
d1b92264 4737 if (STACK_CHECK_MOVING_SP)
4738 anti_adjust_stack_and_probe (max_frame_size, true);
4739 else
4740 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
b22178d2 4741 seq = get_insns ();
4742 end_sequence ();
1edb3690 4743 emit_insn_before (seq, stack_check_probe_note);
b22178d2 4744 break;
4745 }
4746 }
4747
897b77d6 4748 /* End any sequences that failed to be closed due to syntax errors. */
4749 while (in_sequence_p ())
1bb04728 4750 end_sequence ();
897b77d6 4751
897b77d6 4752 clear_pending_stack_adjust ();
4753 do_pending_stack_adjust ();
4754
897b77d6 4755 /* Output a linenumber for the end of the function.
4756 SDB depends on this. */
bccd9980 4757 force_next_line_note ();
375c1c8a 4758 set_curr_insn_source_location (input_location);
897b77d6 4759
b41180f5 4760 /* Before the return label (if any), clobber the return
3fb1e43b 4761 registers so that they are not propagated live to the rest of
b41180f5 4762 the function. This can only happen with functions that drop
4763 through; if there had been a return statement, there would
9b56368f 4764 have either been a return rtx, or a jump to the return label.
4765
4766 We delay actual code generation after the current_function_value_rtx
4767 is computed. */
4768 clobber_after = get_last_insn ();
b41180f5 4769
7861133f 4770 /* Output the label for the actual return from the function. */
4771 emit_label (return_label);
897b77d6 4772
a7e05170 4773 if (USING_SJLJ_EXCEPTIONS)
4774 {
4775 /* Let except.c know where it should emit the call to unregister
4776 the function context for sjlj exceptions. */
4777 if (flag_exceptions)
4778 sjlj_emit_function_exit_after (get_last_insn ());
4779 }
3072d30e 4780 else
4781 {
4782 /* We want to ensure that instructions that may trap are not
4783 moved into the epilogue by scheduling, because we don't
4784 always emit unwind information for the epilogue. */
cbeb677e 4785 if (cfun->can_throw_non_call_exceptions)
3072d30e 4786 emit_insn (gen_blockage ());
4787 }
855f1e85 4788
80e467e2 4789 /* If this is an implementation of throw, do what's necessary to
4790 communicate between __builtin_eh_return and the epilogue. */
4791 expand_eh_return ();
4792
ae39498f 4793 /* If scalar return value was computed in a pseudo-reg, or was a named
4794 return value that got dumped to the stack, copy that to the hard
4795 return register. */
0e8e37b2 4796 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
897b77d6 4797 {
ae39498f 4798 tree decl_result = DECL_RESULT (current_function_decl);
4799 rtx decl_rtl = DECL_RTL (decl_result);
4800
4801 if (REG_P (decl_rtl)
4802 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
4803 : DECL_REGISTER (decl_result))
4804 {
abe32cce 4805 rtx real_decl_rtl = crtl->return_rtx;
897b77d6 4806
8839b7f1 4807 /* This should be set in assign_parms. */
fdada98f 4808 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
ae39498f 4809
4810 /* If this is a BLKmode structure being returned in registers,
4811 then use the mode computed in expand_return. Note that if
60d903f5 4812 decl_rtl is memory, then its mode may have been changed,
abe32cce 4813 but that crtl->return_rtx has not. */
ae39498f 4814 if (GET_MODE (real_decl_rtl) == BLKmode)
8839b7f1 4815 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
ae39498f 4816
05d18e8b 4817 /* If a non-BLKmode return value should be padded at the least
4818 significant end of the register, shift it left by the appropriate
4819 amount. BLKmode results are handled using the group load/store
4820 machinery. */
4821 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
4822 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
4823 {
4824 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
4825 REGNO (real_decl_rtl)),
4826 decl_rtl);
4827 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
4828 }
ae39498f 4829 /* If a named return value dumped decl_return to memory, then
60d903f5 4830 we may need to re-do the PROMOTE_MODE signed/unsigned
ae39498f 4831 extension. */
05d18e8b 4832 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
ae39498f 4833 {
78a8ed03 4834 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
3b2411a8 4835 promote_function_mode (TREE_TYPE (decl_result),
4836 GET_MODE (decl_rtl), &unsignedp,
4837 TREE_TYPE (current_function_decl), 1);
ae39498f 4838
4839 convert_move (real_decl_rtl, decl_rtl, unsignedp);
4840 }
3395ec76 4841 else if (GET_CODE (real_decl_rtl) == PARALLEL)
b566e2e5 4842 {
4843 /* If expand_function_start has created a PARALLEL for decl_rtl,
4844 move the result to the real return registers. Otherwise, do
4845 a group load from decl_rtl for a named return. */
4846 if (GET_CODE (decl_rtl) == PARALLEL)
4847 emit_group_move (real_decl_rtl, decl_rtl);
4848 else
4849 emit_group_load (real_decl_rtl, decl_rtl,
5f4cd670 4850 TREE_TYPE (decl_result),
b566e2e5 4851 int_size_in_bytes (TREE_TYPE (decl_result)));
4852 }
80e467e2 4853 /* In the case of complex integer modes smaller than a word, we'll
4854 need to generate some non-trivial bitfield insertions. Do that
4855 on a pseudo and not the hard register. */
4856 else if (GET_CODE (decl_rtl) == CONCAT
4857 && GET_MODE_CLASS (GET_MODE (decl_rtl)) == MODE_COMPLEX_INT
4858 && GET_MODE_BITSIZE (GET_MODE (decl_rtl)) <= BITS_PER_WORD)
4859 {
4860 int old_generating_concat_p;
4861 rtx tmp;
4862
4863 old_generating_concat_p = generating_concat_p;
4864 generating_concat_p = 0;
4865 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
4866 generating_concat_p = old_generating_concat_p;
4867
4868 emit_move_insn (tmp, decl_rtl);
4869 emit_move_insn (real_decl_rtl, tmp);
4870 }
ae39498f 4871 else
4872 emit_move_insn (real_decl_rtl, decl_rtl);
ae39498f 4873 }
897b77d6 4874 }
4875
4876 /* If returning a structure, arrange to return the address of the value
4877 in a place where debuggers expect to find it.
4878
4879 If returning a structure PCC style,
4880 the caller also depends on this value.
18d50ae6 4881 And cfun->returns_pcc_struct is not necessarily set. */
4882 if (cfun->returns_struct
4883 || cfun->returns_pcc_struct)
897b77d6 4884 {
806e4c12 4885 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
897b77d6 4886 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
806e4c12 4887 rtx outgoing;
4888
4889 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
4890 type = TREE_TYPE (type);
4891 else
4892 value_address = XEXP (value_address, 0);
4893
46b3ff29 4894 outgoing = targetm.calls.function_value (build_pointer_type (type),
4895 current_function_decl, true);
897b77d6 4896
4897 /* Mark this as a function return value so integrate will delete the
4898 assignment and USE below when inlining this function. */
4899 REG_FUNCTION_VALUE_P (outgoing) = 1;
4900
c54c9422 4901 /* The address may be ptr_mode and OUTGOING may be Pmode. */
85d654dd 4902 value_address = convert_memory_address (GET_MODE (outgoing),
4903 value_address);
c54c9422 4904
897b77d6 4905 emit_move_insn (outgoing, value_address);
c54c9422 4906
4907 /* Show return register used to hold result (in this case the address
4908 of the result. */
abe32cce 4909 crtl->return_rtx = outgoing;
897b77d6 4910 }
4911
9b56368f 4912 /* Emit the actual code to clobber return register. */
4913 {
01628e06 4914 rtx seq;
60d903f5 4915
9b56368f 4916 start_sequence ();
4917 clobber_return_register ();
31d3e01c 4918 seq = get_insns ();
9b56368f 4919 end_sequence ();
4920
01628e06 4921 emit_insn_after (seq, clobber_after);
9b56368f 4922 }
4923
01628e06 4924 /* Output the label for the naked return from the function. */
b2ee26d5 4925 if (naked_return_label)
4926 emit_label (naked_return_label);
62380d2d 4927
1b7fd1d9 4928 /* @@@ This is a kludge. We want to ensure that instructions that
4929 may trap are not moved into the epilogue by scheduling, because
d86df71c 4930 we don't always emit unwind information for the epilogue. */
cbeb677e 4931 if (!USING_SJLJ_EXCEPTIONS && cfun->can_throw_non_call_exceptions)
d86df71c 4932 emit_insn (gen_blockage ());
1b7fd1d9 4933
f1a0edff 4934 /* If stack protection is enabled for this function, check the guard. */
edb7afe8 4935 if (crtl->stack_protect_guard)
f1a0edff 4936 stack_protect_epilogue ();
4937
6a7492e8 4938 /* If we had calls to alloca, and this machine needs
4939 an accurate stack pointer to exit the function,
4940 insert some code to save and restore the stack pointer. */
4941 if (! EXIT_IGNORE_STACK
18d50ae6 4942 && cfun->calls_alloca)
6a7492e8 4943 {
4944 rtx tem = 0;
4945
4946 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
4947 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
4948 }
4949
2766437e 4950 /* ??? This should no longer be necessary since stupid is no longer with
4951 us, but there are some parts of the compiler (eg reload_combine, and
4952 sh mach_dep_reorg) that still try and compute their own lifetime info
4953 instead of using the general framework. */
4954 use_return_register ();
897b77d6 4955}
05927e40 4956
4957rtx
b079a207 4958get_arg_pointer_save_area (void)
05927e40 4959{
b079a207 4960 rtx ret = arg_pointer_save_area;
05927e40 4961
4962 if (! ret)
4963 {
b079a207 4964 ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
4965 arg_pointer_save_area = ret;
2032b31d 4966 }
4967
18d50ae6 4968 if (! crtl->arg_pointer_save_area_init)
2032b31d 4969 {
4970 rtx seq;
05927e40 4971
60d903f5 4972 /* Save the arg pointer at the beginning of the function. The
2032b31d 4973 generated stack slot may not be a valid memory address, so we
05927e40 4974 have to check it and fix it if necessary. */
4975 start_sequence ();
27a7a23a 4976 emit_move_insn (validize_mem (ret),
4977 crtl->args.internal_arg_pointer);
31d3e01c 4978 seq = get_insns ();
05927e40 4979 end_sequence ();
4980
2032b31d 4981 push_topmost_sequence ();
c838448c 4982 emit_insn_after (seq, entry_of_function ());
2032b31d 4983 pop_topmost_sequence ();
05927e40 4984 }
4985
4986 return ret;
4987}
b2c5602e 4988\f
25e880b1 4989/* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
4990 for the first time. */
b2c5602e 4991
60ecc450 4992static void
25e880b1 4993record_insns (rtx insns, rtx end, htab_t *hashp)
b2c5602e 4994{
31d3e01c 4995 rtx tmp;
25e880b1 4996 htab_t hash = *hashp;
60ecc450 4997
25e880b1 4998 if (hash == NULL)
4999 *hashp = hash
5000 = htab_create_ggc (17, htab_hash_pointer, htab_eq_pointer, NULL);
5001
5002 for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5003 {
5004 void **slot = htab_find_slot (hash, tmp, INSERT);
5005 gcc_assert (*slot == NULL);
5006 *slot = tmp;
5007 }
5008}
5009
5010/* INSN has been duplicated as COPY, as part of duping a basic block.
5011 If INSN is an epilogue insn, then record COPY as epilogue as well. */
5012
5013void
5014maybe_copy_epilogue_insn (rtx insn, rtx copy)
5015{
5016 void **slot;
5017
5018 if (epilogue_insn_hash == NULL
5019 || htab_find (epilogue_insn_hash, insn) == NULL)
5020 return;
5021
5022 slot = htab_find_slot (epilogue_insn_hash, copy, INSERT);
5023 gcc_assert (*slot == NULL);
5024 *slot = copy;
b2c5602e 5025}
5026
e69efecb 5027/* Set the locator of the insn chain starting at INSN to LOC. */
13751393 5028static void
de1b648b 5029set_insn_locators (rtx insn, int loc)
13751393 5030{
5031 while (insn != NULL_RTX)
5032 {
5033 if (INSN_P (insn))
5034 INSN_LOCATOR (insn) = loc;
5035 insn = NEXT_INSN (insn);
5036 }
5037}
5038
25e880b1 5039/* Determine if any INSNs in HASH are, or are part of, INSN. Because
5040 we can be running after reorg, SEQUENCE rtl is possible. */
b2c5602e 5041
25e880b1 5042static bool
5043contains (const_rtx insn, htab_t hash)
b2c5602e 5044{
25e880b1 5045 if (hash == NULL)
5046 return false;
b2c5602e 5047
25e880b1 5048 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
b2c5602e 5049 {
25e880b1 5050 int i;
b2c5602e 5051 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
25e880b1 5052 if (htab_find (hash, XVECEXP (PATTERN (insn), 0, i)))
5053 return true;
5054 return false;
b2c5602e 5055 }
25e880b1 5056
5057 return htab_find (hash, insn) != NULL;
b2c5602e 5058}
a590d94d 5059
5060int
52d07779 5061prologue_epilogue_contains (const_rtx insn)
a590d94d 5062{
25e880b1 5063 if (contains (insn, prologue_insn_hash))
a590d94d 5064 return 1;
25e880b1 5065 if (contains (insn, epilogue_insn_hash))
a590d94d 5066 return 1;
5067 return 0;
5068}
b2c5602e 5069
2bb09e1e 5070#ifdef HAVE_return
2215ca0d 5071/* Insert gen_return at the end of block BB. This also means updating
5072 block_for_insn appropriately. */
5073
5074static void
f2aa2401 5075emit_return_into_block (basic_block bb)
2215ca0d 5076{
5496dbfc 5077 emit_jump_insn_after (gen_return (), BB_END (bb));
2215ca0d 5078}
2bb09e1e 5079#endif /* HAVE_return */
2215ca0d 5080
c3418f42 5081/* Generate the prologue and epilogue RTL if the machine supports it. Thread
b2c5602e 5082 this into place with notes indicating where the prologue ends and where
5083 the epilogue begins. Update the basic block information when possible. */
5084
3072d30e 5085static void
5086thread_prologue_and_epilogue_insns (void)
b2c5602e 5087{
58d5b39c 5088 int inserted = 0;
777e249a 5089 edge e;
77ec0c64 5090#if defined (HAVE_sibcall_epilogue) || defined (HAVE_epilogue) || defined (HAVE_return) || defined (HAVE_prologue)
777e249a 5091 rtx seq;
77ec0c64 5092#endif
142e7d22 5093#if defined (HAVE_epilogue) || defined(HAVE_return)
5094 rtx epilogue_end = NULL_RTX;
5095#endif
cd665a06 5096 edge_iterator ei;
71caadc0 5097
c107ab96 5098 rtl_profile_for_bb (ENTRY_BLOCK_PTR);
b2c5602e 5099#ifdef HAVE_prologue
5100 if (HAVE_prologue)
5101 {
71caadc0 5102 start_sequence ();
06ebc183 5103 seq = gen_prologue ();
71caadc0 5104 emit_insn (seq);
b2c5602e 5105
48e1416a 5106 /* Insert an explicit USE for the frame pointer
3072d30e 5107 if the profiling is on and the frame pointer is required. */
18d50ae6 5108 if (crtl->profile && frame_pointer_needed)
18b42941 5109 emit_use (hard_frame_pointer_rtx);
3072d30e 5110
b2c5602e 5111 /* Retain a map of the prologue insns. */
25e880b1 5112 record_insns (seq, NULL, &prologue_insn_hash);
d86df71c 5113 emit_note (NOTE_INSN_PROLOGUE_END);
48e1416a 5114
d86df71c 5115#ifndef PROFILE_BEFORE_PROLOGUE
5116 /* Ensure that instructions are not moved into the prologue when
5117 profiling is on. The call to the profiling routine can be
5118 emitted within the live range of a call-clobbered register. */
18d50ae6 5119 if (crtl->profile)
d86df71c 5120 emit_insn (gen_blockage ());
5121#endif
3b934b09 5122
31d3e01c 5123 seq = get_insns ();
71caadc0 5124 end_sequence ();
13751393 5125 set_insn_locators (seq, prologue_locator);
71caadc0 5126
cb0ccc1e 5127 /* Can't deal with multiple successors of the entry block
6ee7c661 5128 at the moment. Function should always have at least one
5129 entry point. */
ea091dfd 5130 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR));
71caadc0 5131
ea091dfd 5132 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
6ee7c661 5133 inserted = 1;
b2c5602e 5134 }
b2c5602e 5135#endif
b2c5602e 5136
777e249a 5137 /* If the exit block has no non-fake predecessors, we don't need
5138 an epilogue. */
cd665a06 5139 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
777e249a 5140 if ((e->flags & EDGE_FAKE) == 0)
5141 break;
5142 if (e == NULL)
5143 goto epilogue_done;
5144
c107ab96 5145 rtl_profile_for_bb (EXIT_BLOCK_PTR);
2215ca0d 5146#ifdef HAVE_return
5147 if (optimize && HAVE_return)
5148 {
5149 /* If we're allowed to generate a simple return instruction,
5150 then by definition we don't need a full epilogue. Examine
06ebc183 5151 the block that falls through to EXIT. If it does not
5152 contain any code, examine its predecessors and try to
2215ca0d 5153 emit (conditional) return instructions. */
5154
5155 basic_block last;
2215ca0d 5156 rtx label;
5157
cd665a06 5158 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
2215ca0d 5159 if (e->flags & EDGE_FALLTHRU)
5160 break;
5161 if (e == NULL)
5162 goto epilogue_done;
5163 last = e->src;
5164
5165 /* Verify that there are no active instructions in the last block. */
5496dbfc 5166 label = BB_END (last);
6d7dc5b9 5167 while (label && !LABEL_P (label))
2215ca0d 5168 {
5169 if (active_insn_p (label))
5170 break;
5171 label = PREV_INSN (label);
5172 }
5173
6d7dc5b9 5174 if (BB_HEAD (last) == label && LABEL_P (label))
2215ca0d 5175 {
cd665a06 5176 edge_iterator ei2;
142e7d22 5177
cd665a06 5178 for (ei2 = ei_start (last->preds); (e = ei_safe_edge (ei2)); )
2215ca0d 5179 {
5180 basic_block bb = e->src;
5181 rtx jump;
5182
2215ca0d 5183 if (bb == ENTRY_BLOCK_PTR)
cd665a06 5184 {
5185 ei_next (&ei2);
5186 continue;
5187 }
2215ca0d 5188
5496dbfc 5189 jump = BB_END (bb);
6d7dc5b9 5190 if (!JUMP_P (jump) || JUMP_LABEL (jump) != label)
cd665a06 5191 {
5192 ei_next (&ei2);
5193 continue;
5194 }
2215ca0d 5195
5196 /* If we have an unconditional jump, we can replace that
5197 with a simple return instruction. */
5198 if (simplejump_p (jump))
5199 {
f2aa2401 5200 emit_return_into_block (bb);
e4bf866d 5201 delete_insn (jump);
2215ca0d 5202 }
5203
5204 /* If we have a conditional jump, we can try to replace
5205 that with a conditional return instruction. */
5206 else if (condjump_p (jump))
5207 {
0f51462d 5208 if (! redirect_jump (jump, 0, 0))
cd665a06 5209 {
5210 ei_next (&ei2);
5211 continue;
5212 }
06ebc183 5213
f7cfde85 5214 /* If this block has only one successor, it both jumps
5215 and falls through to the fallthru block, so we can't
5216 delete the edge. */
ea091dfd 5217 if (single_succ_p (bb))
cd665a06 5218 {
5219 ei_next (&ei2);
5220 continue;
5221 }
2215ca0d 5222 }
5223 else
cd665a06 5224 {
5225 ei_next (&ei2);
5226 continue;
5227 }
2215ca0d 5228
5229 /* Fix up the CFG for the successful change we just made. */
142e7d22 5230 redirect_edge_succ (e, EXIT_BLOCK_PTR);
2215ca0d 5231 }
2215ca0d 5232
ffb61627 5233 /* Emit a return insn for the exit fallthru block. Whether
5234 this is still reachable will be determined later. */
2215ca0d 5235
5496dbfc 5236 emit_barrier_after (BB_END (last));
f2aa2401 5237 emit_return_into_block (last);
5496dbfc 5238 epilogue_end = BB_END (last);
ea091dfd 5239 single_succ_edge (last)->flags &= ~EDGE_FALLTHRU;
06ebc183 5240 goto epilogue_done;
ffb61627 5241 }
2215ca0d 5242 }
5243#endif
25e880b1 5244
5245 /* A small fib -- epilogue is not yet completed, but we wish to re-use
5246 this marker for the splits of EH_RETURN patterns, and nothing else
5247 uses the flag in the meantime. */
5248 epilogue_completed = 1;
5249
5250#ifdef HAVE_eh_return
5251 /* Find non-fallthru edges that end with EH_RETURN instructions. On
5252 some targets, these get split to a special version of the epilogue
5253 code. In order to be able to properly annotate these with unwind
5254 info, try to split them now. If we get a valid split, drop an
5255 EPILOGUE_BEG note and mark the insns as epilogue insns. */
5256 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5257 {
5258 rtx prev, last, trial;
5259
5260 if (e->flags & EDGE_FALLTHRU)
5261 continue;
5262 last = BB_END (e->src);
5263 if (!eh_returnjump_p (last))
5264 continue;
5265
5266 prev = PREV_INSN (last);
5267 trial = try_split (PATTERN (last), last, 1);
5268 if (trial == last)
5269 continue;
5270
5271 record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
5272 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
5273 }
5274#endif
5275
9bb8a4af 5276 /* Find the edge that falls through to EXIT. Other edges may exist
5277 due to RETURN instructions, but those don't need epilogues.
5278 There really shouldn't be a mixture -- either all should have
5279 been converted or none, however... */
5280
cd665a06 5281 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
9bb8a4af 5282 if (e->flags & EDGE_FALLTHRU)
5283 break;
5284 if (e == NULL)
5285 goto epilogue_done;
5286
b2c5602e 5287#ifdef HAVE_epilogue
5288 if (HAVE_epilogue)
5289 {
777e249a 5290 start_sequence ();
31b97e8f 5291 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
777e249a 5292 seq = gen_epilogue ();
5293 emit_jump_insn (seq);
b2c5602e 5294
777e249a 5295 /* Retain a map of the epilogue insns. */
25e880b1 5296 record_insns (seq, NULL, &epilogue_insn_hash);
13751393 5297 set_insn_locators (seq, epilogue_locator);
b2c5602e 5298
31d3e01c 5299 seq = get_insns ();
06ebc183 5300 end_sequence ();
71caadc0 5301
777e249a 5302 insert_insn_on_edge (seq, e);
58d5b39c 5303 inserted = 1;
b2c5602e 5304 }
9bb8a4af 5305 else
b2c5602e 5306#endif
9bb8a4af 5307 {
5308 basic_block cur_bb;
5309
5310 if (! next_active_insn (BB_END (e->src)))
5311 goto epilogue_done;
5312 /* We have a fall-through edge to the exit block, the source is not
5313 at the end of the function, and there will be an assembler epilogue
5314 at the end of the function.
5315 We can't use force_nonfallthru here, because that would try to
5316 use return. Inserting a jump 'by hand' is extremely messy, so
5317 we take advantage of cfg_layout_finalize using
5318 fixup_fallthru_exit_predecessor. */
d2ed6106 5319 cfg_layout_initialize (0);
9bb8a4af 5320 FOR_EACH_BB (cur_bb)
4d2e5d52 5321 if (cur_bb->index >= NUM_FIXED_BLOCKS
5322 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
bc5f266a 5323 cur_bb->aux = cur_bb->next_bb;
9bb8a4af 5324 cfg_layout_finalize ();
5325 }
777e249a 5326epilogue_done:
c107ab96 5327 default_rtl_profile ();
71caadc0 5328
58d5b39c 5329 if (inserted)
e08b2eb8 5330 {
5331 commit_edge_insertions ();
5332
5333 /* The epilogue insns we inserted may cause the exit edge to no longer
5334 be fallthru. */
5335 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5336 {
5337 if (((e->flags & EDGE_FALLTHRU) != 0)
5338 && returnjump_p (BB_END (e->src)))
5339 e->flags &= ~EDGE_FALLTHRU;
5340 }
5341 }
60ecc450 5342
5343#ifdef HAVE_sibcall_epilogue
5344 /* Emit sibling epilogues before any sibling call sites. */
cd665a06 5345 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
60ecc450 5346 {
5347 basic_block bb = e->src;
5496dbfc 5348 rtx insn = BB_END (bb);
60ecc450 5349
6d7dc5b9 5350 if (!CALL_P (insn)
60ecc450 5351 || ! SIBLING_CALL_P (insn))
cd665a06 5352 {
5353 ei_next (&ei);
5354 continue;
5355 }
60ecc450 5356
5357 start_sequence ();
25e880b1 5358 emit_note (NOTE_INSN_EPILOGUE_BEG);
b92064be 5359 emit_insn (gen_sibcall_epilogue ());
5360 seq = get_insns ();
60ecc450 5361 end_sequence ();
5362
31d3e01c 5363 /* Retain a map of the epilogue insns. Used in life analysis to
5364 avoid getting rid of sibcall epilogue insns. Do this before we
5365 actually emit the sequence. */
25e880b1 5366 record_insns (seq, NULL, &epilogue_insn_hash);
13751393 5367 set_insn_locators (seq, epilogue_locator);
31d3e01c 5368
95e50ec0 5369 emit_insn_before (seq, insn);
cd665a06 5370 ei_next (&ei);
60ecc450 5371 }
5372#endif
58d5b39c 5373
142e7d22 5374#ifdef HAVE_epilogue
5375 if (epilogue_end)
5376 {
5377 rtx insn, next;
5378
5379 /* Similarly, move any line notes that appear after the epilogue.
424da949 5380 There is no need, however, to be quite so anal about the existence
737251e7 5381 of such a note. Also possibly move
dc8def52 5382 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
5383 info generation. */
06ebc183 5384 for (insn = epilogue_end; insn; insn = next)
142e7d22 5385 {
5386 next = NEXT_INSN (insn);
48e1416a 5387 if (NOTE_P (insn)
ad4583d9 5388 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
142e7d22 5389 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
5390 }
5391 }
5392#endif
3072d30e 5393
5394 /* Threading the prologue and epilogue changes the artificial refs
5395 in the entry and exit blocks. */
5396 epilogue_completed = 1;
5397 df_update_entry_exit_and_calls ();
b2c5602e 5398}
5399
25e880b1 5400/* Reposition the prologue-end and epilogue-begin notes after
5401 instruction scheduling. */
b2c5602e 5402
5403void
3072d30e 5404reposition_prologue_and_epilogue_notes (void)
b2c5602e 5405{
25e880b1 5406#if defined (HAVE_prologue) || defined (HAVE_epilogue) \
5407 || defined (HAVE_sibcall_epilogue)
25e880b1 5408 /* Since the hash table is created on demand, the fact that it is
5409 non-null is a signal that it is non-empty. */
5410 if (prologue_insn_hash != NULL)
b2c5602e 5411 {
25e880b1 5412 size_t len = htab_elements (prologue_insn_hash);
c009a3ec 5413 rtx insn, last = NULL, note = NULL;
b2c5602e 5414
25e880b1 5415 /* Scan from the beginning until we reach the last prologue insn. */
5416 /* ??? While we do have the CFG intact, there are two problems:
5417 (1) The prologue can contain loops (typically probing the stack),
5418 which means that the end of the prologue isn't in the first bb.
5419 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
3072d30e 5420 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
b2c5602e 5421 {
6d7dc5b9 5422 if (NOTE_P (insn))
12d1c03c 5423 {
ad4583d9 5424 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
60ecc450 5425 note = insn;
5426 }
25e880b1 5427 else if (contains (insn, prologue_insn_hash))
60ecc450 5428 {
5c0913b4 5429 last = insn;
5430 if (--len == 0)
5431 break;
5432 }
5433 }
60d903f5 5434
5c0913b4 5435 if (last)
5436 {
25e880b1 5437 if (note == NULL)
5c0913b4 5438 {
25e880b1 5439 /* Scan forward looking for the PROLOGUE_END note. It should
5440 be right at the beginning of the block, possibly with other
5441 insn notes that got moved there. */
5442 for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
5443 {
5444 if (NOTE_P (note)
5445 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
5446 break;
5447 }
5c0913b4 5448 }
2a588794 5449
5c0913b4 5450 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
6d7dc5b9 5451 if (LABEL_P (last))
5c0913b4 5452 last = NEXT_INSN (last);
5453 reorder_insns (note, note, last);
b2c5602e 5454 }
60ecc450 5455 }
5456
25e880b1 5457 if (epilogue_insn_hash != NULL)
60ecc450 5458 {
25e880b1 5459 edge_iterator ei;
5460 edge e;
b2c5602e 5461
25e880b1 5462 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
b2c5602e 5463 {
c009a3ec 5464 rtx insn, first = NULL, note = NULL;
5465 basic_block bb = e->src;
2a588794 5466
c009a3ec 5467 /* Scan from the beginning until we reach the first epilogue insn. */
25e880b1 5468 FOR_BB_INSNS (bb, insn)
5c0913b4 5469 {
25e880b1 5470 if (NOTE_P (insn))
5471 {
5472 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
5473 {
5474 note = insn;
c009a3ec 5475 if (first != NULL)
25e880b1 5476 break;
5477 }
5478 }
c009a3ec 5479 else if (first == NULL && contains (insn, epilogue_insn_hash))
25e880b1 5480 {
c009a3ec 5481 first = insn;
25e880b1 5482 if (note != NULL)
5483 break;
5484 }
12d1c03c 5485 }
c009a3ec 5486
5487 if (note)
5488 {
5489 /* If the function has a single basic block, and no real
48e1416a 5490 epilogue insns (e.g. sibcall with no cleanup), the
c009a3ec 5491 epilogue note can get scheduled before the prologue
5492 note. If we have frame related prologue insns, having
5493 them scanned during the epilogue will result in a crash.
5494 In this case re-order the epilogue note to just before
5495 the last insn in the block. */
5496 if (first == NULL)
5497 first = BB_END (bb);
5498
5499 if (PREV_INSN (first) != note)
5500 reorder_insns (note, note, PREV_INSN (first));
5501 }
b2c5602e 5502 }
5503 }
5504#endif /* HAVE_prologue or HAVE_epilogue */
5505}
a7b0c170 5506
35901471 5507/* Returns the name of the current function. */
5508const char *
5509current_function_name (void)
5510{
14a1521c 5511 if (cfun == NULL)
5512 return "<none>";
dc24ddbd 5513 return lang_hooks.decl_printable_name (cfun->decl, 2);
35901471 5514}
77fce4cd 5515\f
5516
2a1990e9 5517static unsigned int
77fce4cd 5518rest_of_handle_check_leaf_regs (void)
5519{
5520#ifdef LEAF_REGISTERS
5521 current_function_uses_only_leaf_regs
5522 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
5523#endif
2a1990e9 5524 return 0;
77fce4cd 5525}
5526
35df6eb4 5527/* Insert a TYPE into the used types hash table of CFUN. */
1a4c44c5 5528
35df6eb4 5529static void
5530used_types_insert_helper (tree type, struct function *func)
f6e59711 5531{
35df6eb4 5532 if (type != NULL && func != NULL)
f6e59711 5533 {
5534 void **slot;
5535
5536 if (func->used_types_hash == NULL)
5537 func->used_types_hash = htab_create_ggc (37, htab_hash_pointer,
35df6eb4 5538 htab_eq_pointer, NULL);
5539 slot = htab_find_slot (func->used_types_hash, type, INSERT);
f6e59711 5540 if (*slot == NULL)
35df6eb4 5541 *slot = type;
f6e59711 5542 }
5543}
5544
35df6eb4 5545/* Given a type, insert it into the used hash table in cfun. */
5546void
5547used_types_insert (tree t)
5548{
5549 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
bd564c29 5550 if (TYPE_NAME (t))
5551 break;
5552 else
5553 t = TREE_TYPE (t);
5554 if (TYPE_NAME (t) == NULL_TREE
5555 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
5556 t = TYPE_MAIN_VARIANT (t);
35df6eb4 5557 if (debug_info_level > DINFO_LEVEL_NONE)
1a4c44c5 5558 {
5559 if (cfun)
5560 used_types_insert_helper (t, cfun);
5561 else
5562 /* So this might be a type referenced by a global variable.
5563 Record that type so that we can later decide to emit its debug
5564 information. */
5565 types_used_by_cur_var_decl =
5566 tree_cons (t, NULL, types_used_by_cur_var_decl);
5567
5568 }
5569}
5570
5571/* Helper to Hash a struct types_used_by_vars_entry. */
5572
5573static hashval_t
5574hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
5575{
5576 gcc_assert (entry && entry->var_decl && entry->type);
5577
5578 return iterative_hash_object (entry->type,
5579 iterative_hash_object (entry->var_decl, 0));
5580}
5581
5582/* Hash function of the types_used_by_vars_entry hash table. */
5583
5584hashval_t
5585types_used_by_vars_do_hash (const void *x)
5586{
5587 const struct types_used_by_vars_entry *entry =
5588 (const struct types_used_by_vars_entry *) x;
5589
5590 return hash_types_used_by_vars_entry (entry);
5591}
5592
5593/*Equality function of the types_used_by_vars_entry hash table. */
5594
5595int
5596types_used_by_vars_eq (const void *x1, const void *x2)
5597{
5598 const struct types_used_by_vars_entry *e1 =
5599 (const struct types_used_by_vars_entry *) x1;
5600 const struct types_used_by_vars_entry *e2 =
5601 (const struct types_used_by_vars_entry *)x2;
5602
5603 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
5604}
5605
5606/* Inserts an entry into the types_used_by_vars_hash hash table. */
5607
5608void
5609types_used_by_var_decl_insert (tree type, tree var_decl)
5610{
5611 if (type != NULL && var_decl != NULL)
5612 {
5613 void **slot;
5614 struct types_used_by_vars_entry e;
5615 e.var_decl = var_decl;
5616 e.type = type;
5617 if (types_used_by_vars_hash == NULL)
5618 types_used_by_vars_hash =
5619 htab_create_ggc (37, types_used_by_vars_do_hash,
5620 types_used_by_vars_eq, NULL);
5621 slot = htab_find_slot_with_hash (types_used_by_vars_hash, &e,
5622 hash_types_used_by_vars_entry (&e), INSERT);
5623 if (*slot == NULL)
5624 {
5625 struct types_used_by_vars_entry *entry;
5626 entry = (struct types_used_by_vars_entry*) ggc_alloc
5627 (sizeof (struct types_used_by_vars_entry));
5628 entry->type = type;
5629 entry->var_decl = var_decl;
5630 *slot = entry;
5631 }
5632 }
35df6eb4 5633}
5634
20099e35 5635struct rtl_opt_pass pass_leaf_regs =
77fce4cd 5636{
20099e35 5637 {
5638 RTL_PASS,
0c297edc 5639 "*leaf_regs", /* name */
77fce4cd 5640 NULL, /* gate */
5641 rest_of_handle_check_leaf_regs, /* execute */
5642 NULL, /* sub */
5643 NULL, /* next */
5644 0, /* static_pass_number */
0b1615c1 5645 TV_NONE, /* tv_id */
77fce4cd 5646 0, /* properties_required */
5647 0, /* properties_provided */
5648 0, /* properties_destroyed */
5649 0, /* todo_flags_start */
20099e35 5650 0 /* todo_flags_finish */
5651 }
77fce4cd 5652};
5653
3072d30e 5654static unsigned int
5655rest_of_handle_thread_prologue_and_epilogue (void)
5656{
5657 if (optimize)
5658 cleanup_cfg (CLEANUP_EXPENSIVE);
5659 /* On some machines, the prologue and epilogue code, or parts thereof,
5660 can be represented as RTL. Doing so lets us schedule insns between
5661 it and the rest of the code and also allows delayed branch
5662 scheduling to operate in the epilogue. */
5663
5664 thread_prologue_and_epilogue_insns ();
5665 return 0;
5666}
5667
20099e35 5668struct rtl_opt_pass pass_thread_prologue_and_epilogue =
3072d30e 5669{
20099e35 5670 {
5671 RTL_PASS,
3072d30e 5672 "pro_and_epilogue", /* name */
5673 NULL, /* gate */
5674 rest_of_handle_thread_prologue_and_epilogue, /* execute */
5675 NULL, /* sub */
5676 NULL, /* next */
5677 0, /* static_pass_number */
5678 TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
5679 0, /* properties_required */
5680 0, /* properties_provided */
5681 0, /* properties_destroyed */
5682 TODO_verify_flow, /* todo_flags_start */
5683 TODO_dump_func |
314966f4 5684 TODO_df_verify |
0806b508 5685 TODO_df_finish | TODO_verify_rtl_sharing |
20099e35 5686 TODO_ggc_collect /* todo_flags_finish */
5687 }
3072d30e 5688};
9dc6d5bb 5689\f
5690
5691/* This mini-pass fixes fall-out from SSA in asm statements that have
48e1416a 5692 in-out constraints. Say you start with
9dc6d5bb 5693
5694 orig = inout;
5695 asm ("": "+mr" (inout));
5696 use (orig);
5697
5698 which is transformed very early to use explicit output and match operands:
5699
5700 orig = inout;
5701 asm ("": "=mr" (inout) : "0" (inout));
5702 use (orig);
5703
5704 Or, after SSA and copyprop,
5705
5706 asm ("": "=mr" (inout_2) : "0" (inout_1));
5707 use (inout_1);
5708
5709 Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
5710 they represent two separate values, so they will get different pseudo
5711 registers during expansion. Then, since the two operands need to match
5712 per the constraints, but use different pseudo registers, reload can
5713 only register a reload for these operands. But reloads can only be
5714 satisfied by hardregs, not by memory, so we need a register for this
5715 reload, just because we are presented with non-matching operands.
5716 So, even though we allow memory for this operand, no memory can be
5717 used for it, just because the two operands don't match. This can
5718 cause reload failures on register-starved targets.
5719
5720 So it's a symptom of reload not being able to use memory for reloads
5721 or, alternatively it's also a symptom of both operands not coming into
5722 reload as matching (in which case the pseudo could go to memory just
5723 fine, as the alternative allows it, and no reload would be necessary).
5724 We fix the latter problem here, by transforming
5725
5726 asm ("": "=mr" (inout_2) : "0" (inout_1));
5727
5728 back to
5729
5730 inout_2 = inout_1;
5731 asm ("": "=mr" (inout_2) : "0" (inout_2)); */
5732
5733static void
5734match_asm_constraints_1 (rtx insn, rtx *p_sets, int noutputs)
5735{
5736 int i;
5737 bool changed = false;
5738 rtx op = SET_SRC (p_sets[0]);
5739 int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
5740 rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
2457c754 5741 bool *output_matched = XALLOCAVEC (bool, noutputs);
9dc6d5bb 5742
3f982e5a 5743 memset (output_matched, 0, noutputs * sizeof (bool));
9dc6d5bb 5744 for (i = 0; i < ninputs; i++)
5745 {
5746 rtx input, output, insns;
5747 const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
5748 char *end;
d069e0d3 5749 int match, j;
9dc6d5bb 5750
fbcb9be4 5751 if (*constraint == '%')
5752 constraint++;
5753
9dc6d5bb 5754 match = strtoul (constraint, &end, 10);
5755 if (end == constraint)
5756 continue;
5757
5758 gcc_assert (match < noutputs);
5759 output = SET_DEST (p_sets[match]);
5760 input = RTVEC_ELT (inputs, i);
d069e0d3 5761 /* Only do the transformation for pseudos. */
5762 if (! REG_P (output)
5763 || rtx_equal_p (output, input)
9dc6d5bb 5764 || (GET_MODE (input) != VOIDmode
5765 && GET_MODE (input) != GET_MODE (output)))
5766 continue;
5767
d069e0d3 5768 /* We can't do anything if the output is also used as input,
5769 as we're going to overwrite it. */
5770 for (j = 0; j < ninputs; j++)
5771 if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
5772 break;
5773 if (j != ninputs)
5774 continue;
5775
3f982e5a 5776 /* Avoid changing the same input several times. For
5777 asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
5778 only change in once (to out1), rather than changing it
5779 first to out1 and afterwards to out2. */
5780 if (i > 0)
5781 {
5782 for (j = 0; j < noutputs; j++)
5783 if (output_matched[j] && input == SET_DEST (p_sets[j]))
5784 break;
5785 if (j != noutputs)
5786 continue;
5787 }
5788 output_matched[match] = true;
5789
9dc6d5bb 5790 start_sequence ();
d069e0d3 5791 emit_move_insn (output, input);
9dc6d5bb 5792 insns = get_insns ();
5793 end_sequence ();
9dc6d5bb 5794 emit_insn_before (insns, insn);
d069e0d3 5795
5796 /* Now replace all mentions of the input with output. We can't
f0b5f617 5797 just replace the occurrence in inputs[i], as the register might
d069e0d3 5798 also be used in some other input (or even in an address of an
5799 output), which would mean possibly increasing the number of
5800 inputs by one (namely 'output' in addition), which might pose
5801 a too complicated problem for reload to solve. E.g. this situation:
5802
5803 asm ("" : "=r" (output), "=m" (input) : "0" (input))
5804
c7684b8e 5805 Here 'input' is used in two occurrences as input (once for the
d069e0d3 5806 input operand, once for the address in the second output operand).
f0b5f617 5807 If we would replace only the occurrence of the input operand (to
d069e0d3 5808 make the matching) we would be left with this:
5809
5810 output = input
5811 asm ("" : "=r" (output), "=m" (input) : "0" (output))
5812
5813 Now we suddenly have two different input values (containing the same
5814 value, but different pseudos) where we formerly had only one.
5815 With more complicated asms this might lead to reload failures
5816 which wouldn't have happen without this pass. So, iterate over
c7684b8e 5817 all operands and replace all occurrences of the register used. */
d069e0d3 5818 for (j = 0; j < noutputs; j++)
f211ad17 5819 if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
d069e0d3 5820 && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
5821 SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
5822 input, output);
5823 for (j = 0; j < ninputs; j++)
5824 if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
5825 RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
5826 input, output);
5827
9dc6d5bb 5828 changed = true;
5829 }
5830
5831 if (changed)
5832 df_insn_rescan (insn);
5833}
5834
5835static unsigned
5836rest_of_match_asm_constraints (void)
5837{
5838 basic_block bb;
5839 rtx insn, pat, *p_sets;
5840 int noutputs;
5841
18d50ae6 5842 if (!crtl->has_asm_statement)
9dc6d5bb 5843 return 0;
5844
5845 df_set_flags (DF_DEFER_INSN_RESCAN);
5846 FOR_EACH_BB (bb)
5847 {
5848 FOR_BB_INSNS (bb, insn)
5849 {
5850 if (!INSN_P (insn))
5851 continue;
5852
5853 pat = PATTERN (insn);
5854 if (GET_CODE (pat) == PARALLEL)
5855 p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
5856 else if (GET_CODE (pat) == SET)
5857 p_sets = &PATTERN (insn), noutputs = 1;
5858 else
5859 continue;
5860
5861 if (GET_CODE (*p_sets) == SET
5862 && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
5863 match_asm_constraints_1 (insn, p_sets, noutputs);
5864 }
5865 }
5866
5867 return TODO_df_finish;
5868}
5869
20099e35 5870struct rtl_opt_pass pass_match_asm_constraints =
9dc6d5bb 5871{
20099e35 5872 {
5873 RTL_PASS,
9dc6d5bb 5874 "asmcons", /* name */
5875 NULL, /* gate */
5876 rest_of_match_asm_constraints, /* execute */
5877 NULL, /* sub */
5878 NULL, /* next */
5879 0, /* static_pass_number */
0b1615c1 5880 TV_NONE, /* tv_id */
9dc6d5bb 5881 0, /* properties_required */
5882 0, /* properties_provided */
5883 0, /* properties_destroyed */
5884 0, /* todo_flags_start */
20099e35 5885 TODO_dump_func /* todo_flags_finish */
5886 }
9dc6d5bb 5887};
3072d30e 5888
35901471 5889
1f3233d1 5890#include "gt-function.h"