]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/function.c
Remove obstacks.
[thirdparty/gcc.git] / gcc / function.c
CommitLineData
6f086dfc 1/* Expands front end tree to back end RTL for GNU C-Compiler
af841dbd
JL
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000 Free Software Foundation, Inc.
6f086dfc
RS
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
a35311b0
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
6f086dfc 21
6f086dfc
RS
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
34 not get a hard register.
35
36 Call `put_var_into_stack' when you learn, belatedly, that a variable
37 previously given a pseudo-register must in fact go in the stack.
38 This function changes the DECL_RTL to be a stack slot instead of a reg
39 then scans all the RTL instructions so far generated to correct them. */
40
41#include "config.h"
670ee920 42#include "system.h"
6f086dfc
RS
43#include "rtl.h"
44#include "tree.h"
45#include "flags.h"
1ef08c63 46#include "except.h"
6f086dfc
RS
47#include "function.h"
48#include "insn-flags.h"
49#include "expr.h"
50#include "insn-codes.h"
51#include "regs.h"
52#include "hard-reg-set.h"
53#include "insn-config.h"
54#include "recog.h"
55#include "output.h"
bdac5f58 56#include "basic-block.h"
c20bf1f3 57#include "obstack.h"
10f0ad3d 58#include "toplev.h"
fe9b4957 59#include "hash.h"
87ff9c8e 60#include "ggc.h"
b1474bb7 61#include "tm_p.h"
6f086dfc 62
f73ad30e
JH
63#ifndef ACCUMULATE_OUTGOING_ARGS
64#define ACCUMULATE_OUTGOING_ARGS 0
65#endif
66
189cc377
RK
67#ifndef TRAMPOLINE_ALIGNMENT
68#define TRAMPOLINE_ALIGNMENT FUNCTION_BOUNDARY
69#endif
70
d16790f2
JW
71#ifndef LOCAL_ALIGNMENT
72#define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
73#endif
74
a0871656
JH
75#if !defined (PREFERRED_STACK_BOUNDARY) && defined (STACK_BOUNDARY)
76#define PREFERRED_STACK_BOUNDARY STACK_BOUNDARY
77#endif
78
293e3de4
RS
79/* Some systems use __main in a way incompatible with its use in gcc, in these
80 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
81 give the same symbol without quotes for an alternative entry point. You
0f41302f 82 must define both, or neither. */
293e3de4
RS
83#ifndef NAME__MAIN
84#define NAME__MAIN "__main"
85#define SYMBOL__MAIN __main
86#endif
87
6f086dfc
RS
88/* Round a value to the lowest integer less than it that is a multiple of
89 the required alignment. Avoid using division in case the value is
90 negative. Assume the alignment is a power of two. */
91#define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
92
93/* Similar, but round to the next highest integer that meets the
94 alignment. */
95#define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
96
97/* NEED_SEPARATE_AP means that we cannot derive ap from the value of fp
98 during rtl generation. If they are different register numbers, this is
99 always true. It may also be true if
100 FIRST_PARM_OFFSET - STARTING_FRAME_OFFSET is not a constant during rtl
101 generation. See fix_lexical_addr for details. */
102
103#if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
104#define NEED_SEPARATE_AP
105#endif
106
54ff41b7
JW
107/* Nonzero if function being compiled doesn't contain any calls
108 (ignoring the prologue and epilogue). This is set prior to
109 local register allocation and is valid for the remaining
718fe406 110 compiler passes. */
54ff41b7
JW
111int current_function_is_leaf;
112
fb13d4d0
JM
113/* Nonzero if function being compiled doesn't contain any instructions
114 that can throw an exception. This is set prior to final. */
115
116int current_function_nothrow;
117
fdb8a883
JW
118/* Nonzero if function being compiled doesn't modify the stack pointer
119 (ignoring the prologue and epilogue). This is only valid after
718fe406 120 life_analysis has run. */
fdb8a883
JW
121int current_function_sp_is_unchanging;
122
54ff41b7
JW
123/* Nonzero if the function being compiled is a leaf function which only
124 uses leaf registers. This is valid after reload (specifically after
125 sched2) and is useful only if the port defines LEAF_REGISTERS. */
54ff41b7
JW
126int current_function_uses_only_leaf_regs;
127
6f086dfc
RS
128/* Nonzero once virtual register instantiation has been done.
129 assign_stack_local uses frame_pointer_rtx when this is nonzero. */
130static int virtuals_instantiated;
131
46766466
RS
132/* These variables hold pointers to functions to
133 save and restore machine-specific data,
134 in push_function_context and pop_function_context. */
711d877c
KG
135void (*init_machine_status) PARAMS ((struct function *));
136void (*save_machine_status) PARAMS ((struct function *));
137void (*restore_machine_status) PARAMS ((struct function *));
138void (*mark_machine_status) PARAMS ((struct function *));
139void (*free_machine_status) PARAMS ((struct function *));
46766466 140
8c5666b4 141/* Likewise, but for language-specific data. */
711d877c
KG
142void (*init_lang_status) PARAMS ((struct function *));
143void (*save_lang_status) PARAMS ((struct function *));
144void (*restore_lang_status) PARAMS ((struct function *));
145void (*mark_lang_status) PARAMS ((struct function *));
146void (*free_lang_status) PARAMS ((struct function *));
8c5666b4 147
49ad7cfa
BS
148/* The FUNCTION_DECL for an inline function currently being expanded. */
149tree inline_function_decl;
b384405b
BS
150
151/* The currently compiled function. */
01d939e8 152struct function *cfun = 0;
b384405b
BS
153
154/* Global list of all compiled functions. */
155struct function *all_functions = 0;
5c7675e9
RH
156
157/* These arrays record the INSN_UIDs of the prologue and epilogue insns. */
0a1c58a2
JL
158static varray_type prologue;
159static varray_type epilogue;
160
161/* Array of INSN_UIDs to hold the INSN_UIDs for each sibcall epilogue
162 in this function. */
163static varray_type sibcall_epilogue;
6f086dfc
RS
164\f
165/* In order to evaluate some expressions, such as function calls returning
166 structures in memory, we need to temporarily allocate stack locations.
167 We record each allocated temporary in the following structure.
168
169 Associated with each temporary slot is a nesting level. When we pop up
170 one level, all temporaries associated with the previous level are freed.
171 Normally, all temporaries are freed after the execution of the statement
172 in which they were created. However, if we are inside a ({...}) grouping,
173 the result may be in a temporary and hence must be preserved. If the
174 result could be in a temporary, we preserve it if we can determine which
175 one it is in. If we cannot determine which temporary may contain the
176 result, all temporaries are preserved. A temporary is preserved by
177 pretending it was allocated at the previous nesting level.
178
179 Automatic variables are also assigned temporary slots, at the nesting
180 level where they are defined. They are marked a "kept" so that
181 free_temp_slots will not free them. */
182
183struct temp_slot
184{
185 /* Points to next temporary slot. */
186 struct temp_slot *next;
0f41302f 187 /* The rtx to used to reference the slot. */
6f086dfc 188 rtx slot;
e5e76139
RK
189 /* The rtx used to represent the address if not the address of the
190 slot above. May be an EXPR_LIST if multiple addresses exist. */
191 rtx address;
718fe406 192 /* The alignment (in bits) of the slot. */
d16790f2 193 int align;
6f086dfc 194 /* The size, in units, of the slot. */
e5e809f4 195 HOST_WIDE_INT size;
a4c6502a
MM
196 /* The alias set for the slot. If the alias set is zero, we don't
197 know anything about the alias set of the slot. We must only
198 reuse a slot if it is assigned an object of the same alias set.
199 Otherwise, the rest of the compiler may assume that the new use
200 of the slot cannot alias the old use of the slot, which is
201 false. If the slot has alias set zero, then we can't reuse the
202 slot at all, since we have no idea what alias set may have been
203 imposed on the memory. For example, if the stack slot is the
204 call frame for an inline functioned, we have no idea what alias
205 sets will be assigned to various pieces of the call frame. */
3bdf5ad1 206 HOST_WIDE_INT alias_set;
e7a84011
RK
207 /* The value of `sequence_rtl_expr' when this temporary is allocated. */
208 tree rtl_expr;
6f086dfc
RS
209 /* Non-zero if this temporary is currently in use. */
210 char in_use;
a25d4ba2
RK
211 /* Non-zero if this temporary has its address taken. */
212 char addr_taken;
6f086dfc
RS
213 /* Nesting level at which this slot is being used. */
214 int level;
215 /* Non-zero if this should survive a call to free_temp_slots. */
216 int keep;
fc91b0d0
RK
217 /* The offset of the slot from the frame_pointer, including extra space
218 for alignment. This info is for combine_temp_slots. */
e5e809f4 219 HOST_WIDE_INT base_offset;
fc91b0d0
RK
220 /* The size of the slot, including extra space for alignment. This
221 info is for combine_temp_slots. */
e5e809f4 222 HOST_WIDE_INT full_size;
6f086dfc 223};
6f086dfc 224\f
e15679f8
RK
225/* This structure is used to record MEMs or pseudos used to replace VAR, any
226 SUBREGs of VAR, and any MEMs containing VAR as an address. We need to
227 maintain this list in case two operands of an insn were required to match;
228 in that case we must ensure we use the same replacement. */
229
230struct fixup_replacement
231{
232 rtx old;
233 rtx new;
234 struct fixup_replacement *next;
235};
718fe406 236
fe9b4957
MM
237struct insns_for_mem_entry {
238 /* The KEY in HE will be a MEM. */
239 struct hash_entry he;
240 /* These are the INSNS which reference the MEM. */
241 rtx insns;
242};
243
e15679f8
RK
244/* Forward declarations. */
245
711d877c
KG
246static rtx assign_stack_local_1 PARAMS ((enum machine_mode, HOST_WIDE_INT,
247 int, struct function *));
248static rtx assign_stack_temp_for_type PARAMS ((enum machine_mode,
249 HOST_WIDE_INT, int, tree));
250static struct temp_slot *find_temp_slot_from_address PARAMS ((rtx));
251static void put_reg_into_stack PARAMS ((struct function *, rtx, tree,
252 enum machine_mode, enum machine_mode,
770ae6cc
RK
253 int, unsigned int, int,
254 struct hash_table *));
018577e4
R
255static void schedule_fixup_var_refs PARAMS ((struct function *, rtx, tree,
256 enum machine_mode,
257 struct hash_table *));
718fe406 258static void fixup_var_refs PARAMS ((rtx, enum machine_mode, int,
711d877c 259 struct hash_table *));
e15679f8 260static struct fixup_replacement
711d877c
KG
261 *find_fixup_replacement PARAMS ((struct fixup_replacement **, rtx));
262static void fixup_var_refs_insns PARAMS ((rtx, enum machine_mode, int,
263 rtx, int, struct hash_table *));
264static void fixup_var_refs_1 PARAMS ((rtx, enum machine_mode, rtx *, rtx,
265 struct fixup_replacement **));
266static rtx fixup_memory_subreg PARAMS ((rtx, rtx, int));
267static rtx walk_fixup_memory_subreg PARAMS ((rtx, rtx, int));
268static rtx fixup_stack_1 PARAMS ((rtx, rtx));
269static void optimize_bit_field PARAMS ((rtx, rtx, rtx *));
270static void instantiate_decls PARAMS ((tree, int));
271static void instantiate_decls_1 PARAMS ((tree, int));
770ae6cc 272static void instantiate_decl PARAMS ((rtx, HOST_WIDE_INT, int));
711d877c
KG
273static int instantiate_virtual_regs_1 PARAMS ((rtx *, rtx, int));
274static void delete_handlers PARAMS ((void));
275static void pad_to_arg_alignment PARAMS ((struct args_size *, int,
276 struct args_size *));
51723711 277#ifndef ARGS_GROW_DOWNWARD
711d877c
KG
278static void pad_below PARAMS ((struct args_size *, enum machine_mode,
279 tree));
51723711 280#endif
711d877c 281static rtx round_trampoline_addr PARAMS ((rtx));
0a1c58a2
JL
282static tree *identify_blocks_1 PARAMS ((rtx, tree *, tree *, tree *));
283static void reorder_blocks_1 PARAMS ((rtx, tree, varray_type *));
711d877c
KG
284static tree blocks_nreverse PARAMS ((tree));
285static int all_blocks PARAMS ((tree, tree *));
18c038b9 286static tree *get_block_vector PARAMS ((tree, int *));
ec97b83a
KG
287/* We always define `record_insns' even if its not used so that we
288 can always export `prologue_epilogue_contains'. */
0a1c58a2
JL
289static void record_insns PARAMS ((rtx, varray_type *)) ATTRIBUTE_UNUSED;
290static int contains PARAMS ((rtx, varray_type));
73ef99fb 291#ifdef HAVE_return
86c82654 292static void emit_return_into_block PARAMS ((basic_block, rtx));
73ef99fb 293#endif
711d877c 294static void put_addressof_into_stack PARAMS ((rtx, struct hash_table *));
718fe406 295static boolean purge_addressof_1 PARAMS ((rtx *, rtx, int, int,
711d877c 296 struct hash_table *));
659e47fb 297static void purge_single_hard_subreg_set PARAMS ((rtx));
7393c642
RK
298#ifdef HAVE_epilogue
299static void keep_stack_depressed PARAMS ((rtx));
300#endif
711d877c
KG
301static int is_addressof PARAMS ((rtx *, void *));
302static struct hash_entry *insns_for_mem_newfunc PARAMS ((struct hash_entry *,
303 struct hash_table *,
304 hash_table_key));
305static unsigned long insns_for_mem_hash PARAMS ((hash_table_key));
306static boolean insns_for_mem_comp PARAMS ((hash_table_key, hash_table_key));
307static int insns_for_mem_walk PARAMS ((rtx *, void *));
308static void compute_insns_for_mem PARAMS ((rtx, rtx, struct hash_table *));
309static void mark_temp_slot PARAMS ((struct temp_slot *));
310static void mark_function_status PARAMS ((struct function *));
311static void mark_function_chain PARAMS ((void *));
312static void prepare_function_start PARAMS ((void));
c13fde05
RH
313static void do_clobber_return_reg PARAMS ((rtx, void *));
314static void do_use_return_reg PARAMS ((rtx, void *));
c20bf1f3 315\f
6f086dfc
RS
316/* Pointer to chain of `struct function' for containing functions. */
317struct function *outer_function_chain;
318
319/* Given a function decl for a containing function,
320 return the `struct function' for it. */
321
322struct function *
323find_function_data (decl)
324 tree decl;
325{
326 struct function *p;
e5e809f4 327
6f086dfc
RS
328 for (p = outer_function_chain; p; p = p->next)
329 if (p->decl == decl)
330 return p;
e5e809f4 331
6f086dfc
RS
332 abort ();
333}
334
335/* Save the current context for compilation of a nested function.
8c5666b4
BS
336 This is called from language-specific code. The caller should use
337 the save_lang_status callback to save any language-specific state,
338 since this function knows only about language-independent
339 variables. */
6f086dfc
RS
340
341void
a0dabda5
JM
342push_function_context_to (context)
343 tree context;
6f086dfc 344{
36edd3cc
BS
345 struct function *p, *context_data;
346
347 if (context)
348 {
349 context_data = (context == current_function_decl
01d939e8 350 ? cfun
36edd3cc
BS
351 : find_function_data (context));
352 context_data->contains_functions = 1;
353 }
b384405b 354
01d939e8 355 if (cfun == 0)
b384405b 356 init_dummy_function_start ();
01d939e8 357 p = cfun;
6f086dfc
RS
358
359 p->next = outer_function_chain;
360 outer_function_chain = p;
6f086dfc
RS
361 p->fixup_var_refs_queue = 0;
362
8c5666b4
BS
363 if (save_lang_status)
364 (*save_lang_status) (p);
46766466
RS
365 if (save_machine_status)
366 (*save_machine_status) (p);
b384405b 367
01d939e8 368 cfun = 0;
6f086dfc
RS
369}
370
e4a4639e
JM
371void
372push_function_context ()
373{
a0dabda5 374 push_function_context_to (current_function_decl);
e4a4639e
JM
375}
376
6f086dfc
RS
377/* Restore the last saved context, at the end of a nested function.
378 This function is called from language-specific code. */
379
380void
a0dabda5 381pop_function_context_from (context)
ca3075bd 382 tree context ATTRIBUTE_UNUSED;
6f086dfc
RS
383{
384 struct function *p = outer_function_chain;
e5e809f4 385 struct var_refs_queue *queue;
a3770a81 386 struct var_refs_queue *next;
6f086dfc 387
01d939e8 388 cfun = p;
6f086dfc
RS
389 outer_function_chain = p->next;
390
6f086dfc 391 current_function_decl = p->decl;
7cbc7b0c 392 reg_renumber = 0;
6f086dfc 393
6f086dfc 394 restore_emit_status (p);
6f086dfc 395
46766466
RS
396 if (restore_machine_status)
397 (*restore_machine_status) (p);
8c5666b4
BS
398 if (restore_lang_status)
399 (*restore_lang_status) (p);
46766466 400
6f086dfc
RS
401 /* Finish doing put_var_into_stack for any of our variables
402 which became addressable during the nested function. */
a3770a81
RH
403 for (queue = p->fixup_var_refs_queue; queue; queue = next)
404 {
405 next = queue->next;
406 fixup_var_refs (queue->modified, queue->promoted_mode,
407 queue->unsignedp, 0);
408 free (queue);
409 }
410 p->fixup_var_refs_queue = 0;
6f086dfc 411
6f086dfc
RS
412 /* Reset variables that have known state during rtx generation. */
413 rtx_equal_function_value_matters = 1;
414 virtuals_instantiated = 0;
1b3d8f8a 415 generating_concat_p = 1;
6f086dfc 416}
e4a4639e 417
36edd3cc
BS
418void
419pop_function_context ()
e4a4639e 420{
a0dabda5 421 pop_function_context_from (current_function_decl);
e4a4639e 422}
e2ecd91c 423
fa51b01b
RH
424/* Clear out all parts of the state in F that can safely be discarded
425 after the function has been parsed, but not compiled, to let
426 garbage collection reclaim the memory. */
427
428void
429free_after_parsing (f)
430 struct function *f;
431{
432 /* f->expr->forced_labels is used by code generation. */
433 /* f->emit->regno_reg_rtx is used by code generation. */
434 /* f->varasm is used by code generation. */
435 /* f->eh->eh_return_stub_label is used by code generation. */
436
437 if (free_lang_status)
438 (*free_lang_status) (f);
439 free_stmt_status (f);
440}
441
e2ecd91c
BS
442/* Clear out all parts of the state in F that can safely be discarded
443 after the function has been compiled, to let garbage collection
0a8a198c 444 reclaim the memory. */
21cd906e 445
e2ecd91c 446void
0a8a198c 447free_after_compilation (f)
e2ecd91c
BS
448 struct function *f;
449{
bedda2da
MM
450 struct temp_slot *ts;
451 struct temp_slot *next;
452
fa51b01b
RH
453 free_eh_status (f);
454 free_expr_status (f);
0a8a198c
MM
455 free_emit_status (f);
456 free_varasm_status (f);
e2ecd91c 457
fa51b01b
RH
458 if (free_machine_status)
459 (*free_machine_status) (f);
460
5faf03ae
MM
461 if (f->x_parm_reg_stack_loc)
462 free (f->x_parm_reg_stack_loc);
fa51b01b 463
bedda2da
MM
464 for (ts = f->x_temp_slots; ts; ts = next)
465 {
466 next = ts->next;
467 free (ts);
468 }
469 f->x_temp_slots = NULL;
470
fa51b01b
RH
471 f->arg_offset_rtx = NULL;
472 f->return_rtx = NULL;
473 f->internal_arg_pointer = NULL;
474 f->x_nonlocal_labels = NULL;
475 f->x_nonlocal_goto_handler_slots = NULL;
476 f->x_nonlocal_goto_handler_labels = NULL;
477 f->x_nonlocal_goto_stack_level = NULL;
478 f->x_cleanup_label = NULL;
479 f->x_return_label = NULL;
480 f->x_save_expr_regs = NULL;
481 f->x_stack_slot_list = NULL;
482 f->x_rtl_expr_chain = NULL;
483 f->x_tail_recursion_label = NULL;
484 f->x_tail_recursion_reentry = NULL;
485 f->x_arg_pointer_save_area = NULL;
486 f->x_context_display = NULL;
487 f->x_trampoline_list = NULL;
488 f->x_parm_birth_insn = NULL;
489 f->x_last_parm_insn = NULL;
490 f->x_parm_reg_stack_loc = NULL;
fa51b01b
RH
491 f->fixup_var_refs_queue = NULL;
492 f->original_arg_vector = NULL;
493 f->original_decl_initial = NULL;
494 f->inl_last_parm_insn = NULL;
495 f->epilogue_delay_list = NULL;
e2ecd91c 496}
6f086dfc
RS
497\f
498/* Allocate fixed slots in the stack frame of the current function. */
499
49ad7cfa
BS
500/* Return size needed for stack frame based on slots so far allocated in
501 function F.
c795bca9 502 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
6f086dfc
RS
503 the caller may have to do that. */
504
8af5168b 505HOST_WIDE_INT
49ad7cfa
BS
506get_func_frame_size (f)
507 struct function *f;
6f086dfc
RS
508{
509#ifdef FRAME_GROWS_DOWNWARD
49ad7cfa 510 return -f->x_frame_offset;
6f086dfc 511#else
49ad7cfa 512 return f->x_frame_offset;
6f086dfc
RS
513#endif
514}
515
49ad7cfa
BS
516/* Return size needed for stack frame based on slots so far allocated.
517 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
518 the caller may have to do that. */
519HOST_WIDE_INT
520get_frame_size ()
521{
01d939e8 522 return get_func_frame_size (cfun);
49ad7cfa
BS
523}
524
6f086dfc
RS
525/* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
526 with machine mode MODE.
718fe406 527
6f086dfc
RS
528 ALIGN controls the amount of alignment for the address of the slot:
529 0 means according to MODE,
530 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
531 positive specifies alignment boundary in bits.
532
e2ecd91c 533 We do not round to stack_boundary here.
6f086dfc 534
e2ecd91c
BS
535 FUNCTION specifies the function to allocate in. */
536
537static rtx
538assign_stack_local_1 (mode, size, align, function)
6f086dfc 539 enum machine_mode mode;
e5e809f4 540 HOST_WIDE_INT size;
6f086dfc 541 int align;
e2ecd91c 542 struct function *function;
6f086dfc
RS
543{
544 register rtx x, addr;
545 int bigend_correction = 0;
546 int alignment;
547
548 if (align == 0)
549 {
d16790f2
JW
550 tree type;
551
6f086dfc 552 if (mode == BLKmode)
d16790f2 553 alignment = BIGGEST_ALIGNMENT;
dbab7b72 554 else
718fe406 555 alignment = GET_MODE_ALIGNMENT (mode);
d16790f2
JW
556
557 /* Allow the target to (possibly) increase the alignment of this
558 stack slot. */
559 type = type_for_mode (mode, 0);
560 if (type)
561 alignment = LOCAL_ALIGNMENT (type, alignment);
562
563 alignment /= BITS_PER_UNIT;
6f086dfc
RS
564 }
565 else if (align == -1)
566 {
567 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
568 size = CEIL_ROUND (size, alignment);
569 }
570 else
571 alignment = align / BITS_PER_UNIT;
572
1474e303 573#ifdef FRAME_GROWS_DOWNWARD
e2ecd91c 574 function->x_frame_offset -= size;
1474e303
JL
575#endif
576
a0871656
JH
577 /* Ignore alignment we can't do with expected alignment of the boundary. */
578 if (alignment * BITS_PER_UNIT > PREFERRED_STACK_BOUNDARY)
579 alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
580
581 if (function->stack_alignment_needed < alignment * BITS_PER_UNIT)
582 function->stack_alignment_needed = alignment * BITS_PER_UNIT;
583
6f086dfc
RS
584 /* Round frame offset to that alignment.
585 We must be careful here, since FRAME_OFFSET might be negative and
586 division with a negative dividend isn't as well defined as we might
587 like. So we instead assume that ALIGNMENT is a power of two and
588 use logical operations which are unambiguous. */
589#ifdef FRAME_GROWS_DOWNWARD
e2ecd91c 590 function->x_frame_offset = FLOOR_ROUND (function->x_frame_offset, alignment);
6f086dfc 591#else
e2ecd91c 592 function->x_frame_offset = CEIL_ROUND (function->x_frame_offset, alignment);
6f086dfc
RS
593#endif
594
595 /* On a big-endian machine, if we are allocating more space than we will use,
596 use the least significant bytes of those that are allocated. */
f76b9db2 597 if (BYTES_BIG_ENDIAN && mode != BLKmode)
6f086dfc 598 bigend_correction = size - GET_MODE_SIZE (mode);
6f086dfc 599
6f086dfc
RS
600 /* If we have already instantiated virtual registers, return the actual
601 address relative to the frame pointer. */
01d939e8 602 if (function == cfun && virtuals_instantiated)
6f086dfc
RS
603 addr = plus_constant (frame_pointer_rtx,
604 (frame_offset + bigend_correction
605 + STARTING_FRAME_OFFSET));
606 else
607 addr = plus_constant (virtual_stack_vars_rtx,
3b71623b 608 function->x_frame_offset + bigend_correction);
6f086dfc
RS
609
610#ifndef FRAME_GROWS_DOWNWARD
e2ecd91c 611 function->x_frame_offset += size;
6f086dfc
RS
612#endif
613
38a448ca 614 x = gen_rtx_MEM (mode, addr);
6f086dfc 615
e2ecd91c
BS
616 function->x_stack_slot_list
617 = gen_rtx_EXPR_LIST (VOIDmode, x, function->x_stack_slot_list);
618
6f086dfc
RS
619 return x;
620}
621
e2ecd91c
BS
622/* Wrapper around assign_stack_local_1; assign a local stack slot for the
623 current function. */
3bdf5ad1 624
e2ecd91c
BS
625rtx
626assign_stack_local (mode, size, align)
6f086dfc 627 enum machine_mode mode;
e5e809f4 628 HOST_WIDE_INT size;
6f086dfc 629 int align;
6f086dfc 630{
01d939e8 631 return assign_stack_local_1 (mode, size, align, cfun);
6f086dfc
RS
632}
633\f
634/* Allocate a temporary stack slot and record it for possible later
635 reuse.
636
637 MODE is the machine mode to be given to the returned rtx.
638
639 SIZE is the size in units of the space required. We do no rounding here
640 since assign_stack_local will do any required rounding.
641
d93d4205
MS
642 KEEP is 1 if this slot is to be retained after a call to
643 free_temp_slots. Automatic variables for a block are allocated
e5e809f4
JL
644 with this flag. KEEP is 2 if we allocate a longer term temporary,
645 whose lifetime is controlled by CLEANUP_POINT_EXPRs. KEEP is 3
646 if we are to allocate something at an inner level to be treated as
718fe406 647 a variable in the block (e.g., a SAVE_EXPR).
a4c6502a
MM
648
649 TYPE is the type that will be used for the stack slot. */
6f086dfc 650
d16790f2
JW
651static rtx
652assign_stack_temp_for_type (mode, size, keep, type)
6f086dfc 653 enum machine_mode mode;
e5e809f4 654 HOST_WIDE_INT size;
6f086dfc 655 int keep;
d16790f2 656 tree type;
6f086dfc 657{
d16790f2 658 int align;
3bdf5ad1 659 HOST_WIDE_INT alias_set;
6f086dfc
RS
660 struct temp_slot *p, *best_p = 0;
661
303ec2aa
RK
662 /* If SIZE is -1 it means that somebody tried to allocate a temporary
663 of a variable size. */
664 if (size == -1)
665 abort ();
666
a4c6502a
MM
667 /* If we know the alias set for the memory that will be used, use
668 it. If there's no TYPE, then we don't know anything about the
669 alias set for the memory. */
670 if (type)
671 alias_set = get_alias_set (type);
718fe406 672 else
a4c6502a
MM
673 alias_set = 0;
674
d16790f2
JW
675 if (mode == BLKmode)
676 align = BIGGEST_ALIGNMENT;
dbab7b72
JH
677 else
678 align = GET_MODE_ALIGNMENT (mode);
6f086dfc 679
d16790f2
JW
680 if (! type)
681 type = type_for_mode (mode, 0);
3bdf5ad1 682
d16790f2
JW
683 if (type)
684 align = LOCAL_ALIGNMENT (type, align);
685
686 /* Try to find an available, already-allocated temporary of the proper
687 mode which meets the size and alignment requirements. Choose the
688 smallest one with the closest alignment. */
689 for (p = temp_slots; p; p = p->next)
690 if (p->align >= align && p->size >= size && GET_MODE (p->slot) == mode
691 && ! p->in_use
3bdf5ad1 692 && (! flag_strict_aliasing
a4c6502a 693 || (alias_set && p->alias_set == alias_set))
d16790f2
JW
694 && (best_p == 0 || best_p->size > p->size
695 || (best_p->size == p->size && best_p->align > p->align)))
696 {
697 if (p->align == align && p->size == size)
698 {
699 best_p = 0;
700 break;
701 }
6f086dfc 702 best_p = p;
d16790f2 703 }
6f086dfc
RS
704
705 /* Make our best, if any, the one to use. */
706 if (best_p)
a45035b6
JW
707 {
708 /* If there are enough aligned bytes left over, make them into a new
709 temp_slot so that the extra bytes don't get wasted. Do this only
710 for BLKmode slots, so that we can be sure of the alignment. */
3bdf5ad1 711 if (GET_MODE (best_p->slot) == BLKmode)
a45035b6 712 {
d16790f2 713 int alignment = best_p->align / BITS_PER_UNIT;
e5e809f4 714 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
a45035b6
JW
715
716 if (best_p->size - rounded_size >= alignment)
717 {
bedda2da 718 p = (struct temp_slot *) xmalloc (sizeof (struct temp_slot));
a25d4ba2 719 p->in_use = p->addr_taken = 0;
a45035b6 720 p->size = best_p->size - rounded_size;
307d8cd6
RK
721 p->base_offset = best_p->base_offset + rounded_size;
722 p->full_size = best_p->full_size - rounded_size;
38a448ca
RH
723 p->slot = gen_rtx_MEM (BLKmode,
724 plus_constant (XEXP (best_p->slot, 0),
725 rounded_size));
d16790f2 726 p->align = best_p->align;
e5e76139 727 p->address = 0;
591ccf92 728 p->rtl_expr = 0;
3bdf5ad1 729 p->alias_set = best_p->alias_set;
a45035b6
JW
730 p->next = temp_slots;
731 temp_slots = p;
732
38a448ca
RH
733 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
734 stack_slot_list);
a45035b6
JW
735
736 best_p->size = rounded_size;
291dde90 737 best_p->full_size = rounded_size;
a45035b6
JW
738 }
739 }
740
741 p = best_p;
742 }
718fe406 743
6f086dfc
RS
744 /* If we still didn't find one, make a new temporary. */
745 if (p == 0)
746 {
e5e809f4
JL
747 HOST_WIDE_INT frame_offset_old = frame_offset;
748
bedda2da 749 p = (struct temp_slot *) xmalloc (sizeof (struct temp_slot));
e5e809f4 750
c87a0a39
JL
751 /* We are passing an explicit alignment request to assign_stack_local.
752 One side effect of that is assign_stack_local will not round SIZE
753 to ensure the frame offset remains suitably aligned.
754
755 So for requests which depended on the rounding of SIZE, we go ahead
756 and round it now. We also make sure ALIGNMENT is at least
757 BIGGEST_ALIGNMENT. */
010529e5 758 if (mode == BLKmode && align < BIGGEST_ALIGNMENT)
6f67a30d
JW
759 abort();
760 p->slot = assign_stack_local (mode,
010529e5
AS
761 (mode == BLKmode
762 ? CEIL_ROUND (size, align / BITS_PER_UNIT)
763 : size),
6f67a30d 764 align);
d16790f2
JW
765
766 p->align = align;
a4c6502a 767 p->alias_set = alias_set;
e5e809f4 768
b2a80c0d
DE
769 /* The following slot size computation is necessary because we don't
770 know the actual size of the temporary slot until assign_stack_local
771 has performed all the frame alignment and size rounding for the
fc91b0d0
RK
772 requested temporary. Note that extra space added for alignment
773 can be either above or below this stack slot depending on which
774 way the frame grows. We include the extra space if and only if it
775 is above this slot. */
b2a80c0d
DE
776#ifdef FRAME_GROWS_DOWNWARD
777 p->size = frame_offset_old - frame_offset;
778#else
fc91b0d0
RK
779 p->size = size;
780#endif
e5e809f4 781
fc91b0d0
RK
782 /* Now define the fields used by combine_temp_slots. */
783#ifdef FRAME_GROWS_DOWNWARD
784 p->base_offset = frame_offset;
785 p->full_size = frame_offset_old - frame_offset;
786#else
787 p->base_offset = frame_offset_old;
788 p->full_size = frame_offset - frame_offset_old;
b2a80c0d 789#endif
e5e76139 790 p->address = 0;
6f086dfc
RS
791 p->next = temp_slots;
792 temp_slots = p;
793 }
794
795 p->in_use = 1;
a25d4ba2 796 p->addr_taken = 0;
591ccf92 797 p->rtl_expr = seq_rtl_expr;
a25d4ba2 798
d93d4205
MS
799 if (keep == 2)
800 {
801 p->level = target_temp_slot_level;
802 p->keep = 0;
803 }
e5e809f4
JL
804 else if (keep == 3)
805 {
806 p->level = var_temp_slot_level;
807 p->keep = 0;
808 }
d93d4205
MS
809 else
810 {
811 p->level = temp_slot_level;
812 p->keep = keep;
813 }
1995f267
RK
814
815 /* We may be reusing an old slot, so clear any MEM flags that may have been
816 set from before. */
817 RTX_UNCHANGING_P (p->slot) = 0;
818 MEM_IN_STRUCT_P (p->slot) = 0;
c6df88cb 819 MEM_SCALAR_P (p->slot) = 0;
3bdf5ad1
RK
820 MEM_ALIAS_SET (p->slot) = alias_set;
821
822 if (type != 0)
823 MEM_SET_IN_STRUCT_P (p->slot, AGGREGATE_TYPE_P (type));
824
6f086dfc
RS
825 return p->slot;
826}
d16790f2
JW
827
828/* Allocate a temporary stack slot and record it for possible later
829 reuse. First three arguments are same as in preceding function. */
830
831rtx
832assign_stack_temp (mode, size, keep)
833 enum machine_mode mode;
834 HOST_WIDE_INT size;
835 int keep;
836{
837 return assign_stack_temp_for_type (mode, size, keep, NULL_TREE);
838}
638141a6 839\f
230f21b4
PB
840/* Assign a temporary of given TYPE.
841 KEEP is as for assign_stack_temp.
842 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
b55d9ff8
RK
843 it is 0 if a register is OK.
844 DONT_PROMOTE is 1 if we should not promote values in register
845 to wider modes. */
230f21b4
PB
846
847rtx
b55d9ff8 848assign_temp (type, keep, memory_required, dont_promote)
230f21b4
PB
849 tree type;
850 int keep;
851 int memory_required;
0ce8a59c 852 int dont_promote ATTRIBUTE_UNUSED;
230f21b4
PB
853{
854 enum machine_mode mode = TYPE_MODE (type);
0ce8a59c 855#ifndef PROMOTE_FOR_CALL_ONLY
638141a6 856 int unsignedp = TREE_UNSIGNED (type);
0ce8a59c 857#endif
638141a6 858
230f21b4
PB
859 if (mode == BLKmode || memory_required)
860 {
e5e809f4 861 HOST_WIDE_INT size = int_size_in_bytes (type);
230f21b4
PB
862 rtx tmp;
863
44affdae
JH
864 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
865 problems with allocating the stack space. */
866 if (size == 0)
867 size = 1;
868
230f21b4
PB
869 /* Unfortunately, we don't yet know how to allocate variable-sized
870 temporaries. However, sometimes we have a fixed upper limit on
871 the size (which is stored in TYPE_ARRAY_MAX_SIZE) and can use that
0f41302f 872 instead. This is the case for Chill variable-sized strings. */
230f21b4
PB
873 if (size == -1 && TREE_CODE (type) == ARRAY_TYPE
874 && TYPE_ARRAY_MAX_SIZE (type) != NULL_TREE
3bdf5ad1
RK
875 && host_integerp (TYPE_ARRAY_MAX_SIZE (type), 1))
876 size = tree_low_cst (TYPE_ARRAY_MAX_SIZE (type), 1);
230f21b4 877
d16790f2 878 tmp = assign_stack_temp_for_type (mode, size, keep, type);
230f21b4
PB
879 return tmp;
880 }
638141a6 881
230f21b4 882#ifndef PROMOTE_FOR_CALL_ONLY
b55d9ff8
RK
883 if (! dont_promote)
884 mode = promote_mode (type, mode, &unsignedp, 0);
230f21b4 885#endif
638141a6 886
230f21b4
PB
887 return gen_reg_rtx (mode);
888}
638141a6 889\f
a45035b6
JW
890/* Combine temporary stack slots which are adjacent on the stack.
891
892 This allows for better use of already allocated stack space. This is only
893 done for BLKmode slots because we can be sure that we won't have alignment
894 problems in this case. */
895
896void
897combine_temp_slots ()
898{
899 struct temp_slot *p, *q;
900 struct temp_slot *prev_p, *prev_q;
e5e809f4
JL
901 int num_slots;
902
a4c6502a
MM
903 /* We can't combine slots, because the information about which slot
904 is in which alias set will be lost. */
905 if (flag_strict_aliasing)
906 return;
907
718fe406 908 /* If there are a lot of temp slots, don't do anything unless
e5e809f4
JL
909 high levels of optimizaton. */
910 if (! flag_expensive_optimizations)
911 for (p = temp_slots, num_slots = 0; p; p = p->next, num_slots++)
912 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
913 return;
a45035b6 914
e9b7093a
RS
915 for (p = temp_slots, prev_p = 0; p; p = prev_p ? prev_p->next : temp_slots)
916 {
917 int delete_p = 0;
e5e809f4 918
e9b7093a
RS
919 if (! p->in_use && GET_MODE (p->slot) == BLKmode)
920 for (q = p->next, prev_q = p; q; q = prev_q->next)
a45035b6 921 {
e9b7093a
RS
922 int delete_q = 0;
923 if (! q->in_use && GET_MODE (q->slot) == BLKmode)
a45035b6 924 {
fc91b0d0 925 if (p->base_offset + p->full_size == q->base_offset)
e9b7093a
RS
926 {
927 /* Q comes after P; combine Q into P. */
928 p->size += q->size;
307d8cd6 929 p->full_size += q->full_size;
e9b7093a
RS
930 delete_q = 1;
931 }
fc91b0d0 932 else if (q->base_offset + q->full_size == p->base_offset)
e9b7093a
RS
933 {
934 /* P comes after Q; combine P into Q. */
935 q->size += p->size;
307d8cd6 936 q->full_size += p->full_size;
e9b7093a
RS
937 delete_p = 1;
938 break;
939 }
a45035b6 940 }
e9b7093a
RS
941 /* Either delete Q or advance past it. */
942 if (delete_q)
bedda2da
MM
943 {
944 prev_q->next = q->next;
945 free (q);
946 }
e9b7093a
RS
947 else
948 prev_q = q;
a45035b6 949 }
e9b7093a
RS
950 /* Either delete P or advance past it. */
951 if (delete_p)
952 {
953 if (prev_p)
954 prev_p->next = p->next;
955 else
956 temp_slots = p->next;
957 }
958 else
959 prev_p = p;
960 }
a45035b6 961}
6f086dfc 962\f
e5e76139
RK
963/* Find the temp slot corresponding to the object at address X. */
964
965static struct temp_slot *
966find_temp_slot_from_address (x)
967 rtx x;
968{
969 struct temp_slot *p;
970 rtx next;
971
972 for (p = temp_slots; p; p = p->next)
973 {
974 if (! p->in_use)
975 continue;
e5e809f4 976
e5e76139 977 else if (XEXP (p->slot, 0) == x
abb52246
RK
978 || p->address == x
979 || (GET_CODE (x) == PLUS
980 && XEXP (x, 0) == virtual_stack_vars_rtx
981 && GET_CODE (XEXP (x, 1)) == CONST_INT
982 && INTVAL (XEXP (x, 1)) >= p->base_offset
983 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size))
e5e76139
RK
984 return p;
985
986 else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
987 for (next = p->address; next; next = XEXP (next, 1))
988 if (XEXP (next, 0) == x)
989 return p;
990 }
991
14a774a9
RK
992 /* If we have a sum involving a register, see if it points to a temp
993 slot. */
994 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == REG
995 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
996 return p;
997 else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG
998 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
999 return p;
1000
e5e76139
RK
1001 return 0;
1002}
718fe406 1003
9faa82d8 1004/* Indicate that NEW is an alternate way of referring to the temp slot
e5e809f4 1005 that previously was known by OLD. */
e5e76139
RK
1006
1007void
1008update_temp_slot_address (old, new)
1009 rtx old, new;
1010{
14a774a9 1011 struct temp_slot *p;
e5e76139 1012
14a774a9 1013 if (rtx_equal_p (old, new))
e5e76139 1014 return;
14a774a9
RK
1015
1016 p = find_temp_slot_from_address (old);
1017
700f19f0
RK
1018 /* If we didn't find one, see if both OLD is a PLUS. If so, and NEW
1019 is a register, see if one operand of the PLUS is a temporary
1020 location. If so, NEW points into it. Otherwise, if both OLD and
1021 NEW are a PLUS and if there is a register in common between them.
1022 If so, try a recursive call on those values. */
14a774a9
RK
1023 if (p == 0)
1024 {
700f19f0
RK
1025 if (GET_CODE (old) != PLUS)
1026 return;
1027
1028 if (GET_CODE (new) == REG)
1029 {
1030 update_temp_slot_address (XEXP (old, 0), new);
1031 update_temp_slot_address (XEXP (old, 1), new);
1032 return;
1033 }
1034 else if (GET_CODE (new) != PLUS)
14a774a9
RK
1035 return;
1036
1037 if (rtx_equal_p (XEXP (old, 0), XEXP (new, 0)))
1038 update_temp_slot_address (XEXP (old, 1), XEXP (new, 1));
1039 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 0)))
1040 update_temp_slot_address (XEXP (old, 0), XEXP (new, 1));
1041 else if (rtx_equal_p (XEXP (old, 0), XEXP (new, 1)))
1042 update_temp_slot_address (XEXP (old, 1), XEXP (new, 0));
1043 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 1)))
1044 update_temp_slot_address (XEXP (old, 0), XEXP (new, 0));
1045
1046 return;
1047 }
1048
718fe406 1049 /* Otherwise add an alias for the temp's address. */
e5e76139
RK
1050 else if (p->address == 0)
1051 p->address = new;
1052 else
1053 {
1054 if (GET_CODE (p->address) != EXPR_LIST)
38a448ca 1055 p->address = gen_rtx_EXPR_LIST (VOIDmode, p->address, NULL_RTX);
e5e76139 1056
38a448ca 1057 p->address = gen_rtx_EXPR_LIST (VOIDmode, new, p->address);
e5e76139
RK
1058 }
1059}
1060
a25d4ba2 1061/* If X could be a reference to a temporary slot, mark the fact that its
9faa82d8 1062 address was taken. */
a25d4ba2
RK
1063
1064void
1065mark_temp_addr_taken (x)
1066 rtx x;
1067{
1068 struct temp_slot *p;
1069
1070 if (x == 0)
1071 return;
1072
1073 /* If X is not in memory or is at a constant address, it cannot be in
1074 a temporary slot. */
1075 if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1076 return;
1077
1078 p = find_temp_slot_from_address (XEXP (x, 0));
1079 if (p != 0)
1080 p->addr_taken = 1;
1081}
1082
9cca6a99
MS
1083/* If X could be a reference to a temporary slot, mark that slot as
1084 belonging to the to one level higher than the current level. If X
1085 matched one of our slots, just mark that one. Otherwise, we can't
1086 easily predict which it is, so upgrade all of them. Kept slots
1087 need not be touched.
6f086dfc
RS
1088
1089 This is called when an ({...}) construct occurs and a statement
1090 returns a value in memory. */
1091
1092void
1093preserve_temp_slots (x)
1094 rtx x;
1095{
a25d4ba2 1096 struct temp_slot *p = 0;
6f086dfc 1097
73620b82
RK
1098 /* If there is no result, we still might have some objects whose address
1099 were taken, so we need to make sure they stay around. */
e3a77161 1100 if (x == 0)
73620b82
RK
1101 {
1102 for (p = temp_slots; p; p = p->next)
1103 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1104 p->level--;
1105
1106 return;
1107 }
e3a77161
RK
1108
1109 /* If X is a register that is being used as a pointer, see if we have
1110 a temporary slot we know it points to. To be consistent with
1111 the code below, we really should preserve all non-kept slots
1112 if we can't find a match, but that seems to be much too costly. */
a25d4ba2
RK
1113 if (GET_CODE (x) == REG && REGNO_POINTER_FLAG (REGNO (x)))
1114 p = find_temp_slot_from_address (x);
1115
6f086dfc 1116 /* If X is not in memory or is at a constant address, it cannot be in
e19571db
RK
1117 a temporary slot, but it can contain something whose address was
1118 taken. */
a25d4ba2 1119 if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
e19571db
RK
1120 {
1121 for (p = temp_slots; p; p = p->next)
1122 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1123 p->level--;
1124
1125 return;
1126 }
6f086dfc
RS
1127
1128 /* First see if we can find a match. */
73620b82 1129 if (p == 0)
a25d4ba2
RK
1130 p = find_temp_slot_from_address (XEXP (x, 0));
1131
e5e76139
RK
1132 if (p != 0)
1133 {
a25d4ba2
RK
1134 /* Move everything at our level whose address was taken to our new
1135 level in case we used its address. */
1136 struct temp_slot *q;
1137
9cca6a99
MS
1138 if (p->level == temp_slot_level)
1139 {
1140 for (q = temp_slots; q; q = q->next)
1141 if (q != p && q->addr_taken && q->level == p->level)
1142 q->level--;
a25d4ba2 1143
9cca6a99
MS
1144 p->level--;
1145 p->addr_taken = 0;
1146 }
e5e76139
RK
1147 return;
1148 }
6f086dfc
RS
1149
1150 /* Otherwise, preserve all non-kept slots at this level. */
1151 for (p = temp_slots; p; p = p->next)
1152 if (p->in_use && p->level == temp_slot_level && ! p->keep)
1153 p->level--;
1154}
1155
591ccf92
MM
1156/* X is the result of an RTL_EXPR. If it is a temporary slot associated
1157 with that RTL_EXPR, promote it into a temporary slot at the present
1158 level so it will not be freed when we free slots made in the
1159 RTL_EXPR. */
1160
1161void
1162preserve_rtl_expr_result (x)
1163 rtx x;
1164{
1165 struct temp_slot *p;
1166
1167 /* If X is not in memory or is at a constant address, it cannot be in
1168 a temporary slot. */
1169 if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1170 return;
1171
1172 /* If we can find a match, move it to our level unless it is already at
1173 an upper level. */
1174 p = find_temp_slot_from_address (XEXP (x, 0));
64dc53f3
MM
1175 if (p != 0)
1176 {
1177 p->level = MIN (p->level, temp_slot_level);
1178 p->rtl_expr = 0;
1179 }
591ccf92
MM
1180
1181 return;
1182}
1183
6f086dfc 1184/* Free all temporaries used so far. This is normally called at the end
e7a84011
RK
1185 of generating code for a statement. Don't free any temporaries
1186 currently in use for an RTL_EXPR that hasn't yet been emitted.
1187 We could eventually do better than this since it can be reused while
1188 generating the same RTL_EXPR, but this is complex and probably not
1189 worthwhile. */
6f086dfc
RS
1190
1191void
1192free_temp_slots ()
1193{
1194 struct temp_slot *p;
1195
1196 for (p = temp_slots; p; p = p->next)
591ccf92
MM
1197 if (p->in_use && p->level == temp_slot_level && ! p->keep
1198 && p->rtl_expr == 0)
1199 p->in_use = 0;
1200
1201 combine_temp_slots ();
1202}
1203
1204/* Free all temporary slots used in T, an RTL_EXPR node. */
1205
1206void
1207free_temps_for_rtl_expr (t)
1208 tree t;
1209{
1210 struct temp_slot *p;
1211
1212 for (p = temp_slots; p; p = p->next)
1213 if (p->rtl_expr == t)
64dc53f3
MM
1214 {
1215 /* If this slot is below the current TEMP_SLOT_LEVEL, then it
1216 needs to be preserved. This can happen if a temporary in
1217 the RTL_EXPR was addressed; preserve_temp_slots will move
1218 the temporary into a higher level. */
1219 if (temp_slot_level <= p->level)
1220 p->in_use = 0;
1221 else
1222 p->rtl_expr = NULL_TREE;
1223 }
a45035b6
JW
1224
1225 combine_temp_slots ();
6f086dfc
RS
1226}
1227
956d6950 1228/* Mark all temporaries ever allocated in this function as not suitable
a94e4054
RK
1229 for reuse until the current level is exited. */
1230
1231void
1232mark_all_temps_used ()
1233{
1234 struct temp_slot *p;
1235
1236 for (p = temp_slots; p; p = p->next)
1237 {
85b119d1 1238 p->in_use = p->keep = 1;
27ce006b 1239 p->level = MIN (p->level, temp_slot_level);
a94e4054
RK
1240 }
1241}
1242
6f086dfc
RS
1243/* Push deeper into the nesting level for stack temporaries. */
1244
1245void
1246push_temp_slots ()
1247{
6f086dfc
RS
1248 temp_slot_level++;
1249}
1250
e5e809f4
JL
1251/* Likewise, but save the new level as the place to allocate variables
1252 for blocks. */
1253
ca3075bd 1254#if 0
e5e809f4
JL
1255void
1256push_temp_slots_for_block ()
1257{
1258 push_temp_slots ();
1259
1260 var_temp_slot_level = temp_slot_level;
1261}
1262
f5963e61
JL
1263/* Likewise, but save the new level as the place to allocate temporaries
1264 for TARGET_EXPRs. */
1265
1266void
1267push_temp_slots_for_target ()
1268{
1269 push_temp_slots ();
1270
1271 target_temp_slot_level = temp_slot_level;
1272}
1273
1274/* Set and get the value of target_temp_slot_level. The only
1275 permitted use of these functions is to save and restore this value. */
1276
1277int
1278get_target_temp_slot_level ()
1279{
1280 return target_temp_slot_level;
1281}
1282
1283void
1284set_target_temp_slot_level (level)
1285 int level;
1286{
1287 target_temp_slot_level = level;
1288}
ca3075bd 1289#endif
f5963e61 1290
6f086dfc
RS
1291/* Pop a temporary nesting level. All slots in use in the current level
1292 are freed. */
1293
1294void
1295pop_temp_slots ()
1296{
1297 struct temp_slot *p;
1298
6f086dfc 1299 for (p = temp_slots; p; p = p->next)
591ccf92 1300 if (p->in_use && p->level == temp_slot_level && p->rtl_expr == 0)
6f086dfc
RS
1301 p->in_use = 0;
1302
a45035b6
JW
1303 combine_temp_slots ();
1304
6f086dfc
RS
1305 temp_slot_level--;
1306}
bc0ebdf9
RK
1307
1308/* Initialize temporary slots. */
1309
1310void
1311init_temp_slots ()
1312{
1313 /* We have not allocated any temporaries yet. */
1314 temp_slots = 0;
1315 temp_slot_level = 0;
e5e809f4 1316 var_temp_slot_level = 0;
bc0ebdf9
RK
1317 target_temp_slot_level = 0;
1318}
6f086dfc
RS
1319\f
1320/* Retroactively move an auto variable from a register to a stack slot.
1321 This is done when an address-reference to the variable is seen. */
1322
1323void
1324put_var_into_stack (decl)
1325 tree decl;
1326{
1327 register rtx reg;
00d8a4c1 1328 enum machine_mode promoted_mode, decl_mode;
6f086dfc 1329 struct function *function = 0;
c20bf1f3 1330 tree context;
e9a25f70 1331 int can_use_addressof;
c357082f
RK
1332 int volatilep = TREE_CODE (decl) != SAVE_EXPR && TREE_THIS_VOLATILE (decl);
1333 int usedp = (TREE_USED (decl)
1334 || (TREE_CODE (decl) != SAVE_EXPR && DECL_INITIAL (decl) != 0));
c20bf1f3 1335
c20bf1f3 1336 context = decl_function_context (decl);
6f086dfc 1337
9ec36da5 1338 /* Get the current rtl used for this object and its original mode. */
6f086dfc 1339 reg = TREE_CODE (decl) == SAVE_EXPR ? SAVE_EXPR_RTL (decl) : DECL_RTL (decl);
2baccce2
RS
1340
1341 /* No need to do anything if decl has no rtx yet
1342 since in that case caller is setting TREE_ADDRESSABLE
1343 and a stack slot will be assigned when the rtl is made. */
1344 if (reg == 0)
1345 return;
00d8a4c1
RK
1346
1347 /* Get the declared mode for this object. */
1348 decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1349 : DECL_MODE (decl));
2baccce2
RS
1350 /* Get the mode it's actually stored in. */
1351 promoted_mode = GET_MODE (reg);
6f086dfc
RS
1352
1353 /* If this variable comes from an outer function,
1354 find that function's saved context. */
4ac74fb8 1355 if (context != current_function_decl && context != inline_function_decl)
6f086dfc
RS
1356 for (function = outer_function_chain; function; function = function->next)
1357 if (function->decl == context)
1358 break;
1359
6f086dfc
RS
1360 /* If this is a variable-size object with a pseudo to address it,
1361 put that pseudo into the stack, if the var is nonlocal. */
c357082f 1362 if (TREE_CODE (decl) != SAVE_EXPR && DECL_NONLOCAL (decl)
6f086dfc
RS
1363 && GET_CODE (reg) == MEM
1364 && GET_CODE (XEXP (reg, 0)) == REG
1365 && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
4cdb3e78
RS
1366 {
1367 reg = XEXP (reg, 0);
1368 decl_mode = promoted_mode = GET_MODE (reg);
1369 }
e15762df 1370
e9a25f70
JL
1371 can_use_addressof
1372 = (function == 0
e5e809f4 1373 && optimize > 0
e9a25f70
JL
1374 /* FIXME make it work for promoted modes too */
1375 && decl_mode == promoted_mode
1376#ifdef NON_SAVING_SETJMP
1377 && ! (NON_SAVING_SETJMP && current_function_calls_setjmp)
1378#endif
1379 );
1380
1381 /* If we can't use ADDRESSOF, make sure we see through one we already
1382 generated. */
1383 if (! can_use_addressof && GET_CODE (reg) == MEM
1384 && GET_CODE (XEXP (reg, 0)) == ADDRESSOF)
1385 reg = XEXP (XEXP (reg, 0), 0);
1386
293e3de4
RS
1387 /* Now we should have a value that resides in one or more pseudo regs. */
1388
1389 if (GET_CODE (reg) == REG)
e9a25f70
JL
1390 {
1391 /* If this variable lives in the current function and we don't need
1392 to put things in the stack for the sake of setjmp, try to keep it
1393 in a register until we know we actually need the address. */
1394 if (can_use_addressof)
1395 gen_mem_addressof (reg, decl);
1396 else
c357082f
RK
1397 put_reg_into_stack (function, reg, TREE_TYPE (decl), promoted_mode,
1398 decl_mode, volatilep, 0, usedp, 0);
e9a25f70 1399 }
293e3de4
RS
1400 else if (GET_CODE (reg) == CONCAT)
1401 {
1402 /* A CONCAT contains two pseudos; put them both in the stack.
018577e4
R
1403 We do it so they end up consecutive.
1404 We fixup references to the parts only after we fixup references
1405 to the whole CONCAT, lest we do double fixups for the latter
1406 references. */
293e3de4 1407 enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
c3b247b4 1408 tree part_type = type_for_mode (part_mode, 0);
018577e4
R
1409 rtx lopart = XEXP (reg, 0);
1410 rtx hipart = XEXP (reg, 1);
4738c10d 1411#ifdef FRAME_GROWS_DOWNWARD
293e3de4 1412 /* Since part 0 should have a lower address, do it second. */
018577e4
R
1413 put_reg_into_stack (function, hipart, part_type, part_mode,
1414 part_mode, volatilep, 0, 0, 0);
1415 put_reg_into_stack (function, lopart, part_type, part_mode,
1416 part_mode, volatilep, 0, 0, 0);
293e3de4 1417#else
018577e4
R
1418 put_reg_into_stack (function, lopart, part_type, part_mode,
1419 part_mode, volatilep, 0, 0, 0);
1420 put_reg_into_stack (function, hipart, part_type, part_mode,
1421 part_mode, volatilep, 0, 0, 0);
293e3de4
RS
1422#endif
1423
1424 /* Change the CONCAT into a combined MEM for both parts. */
1425 PUT_CODE (reg, MEM);
c357082f 1426 set_mem_attributes (reg, decl, 1);
0006e95b 1427
293e3de4
RS
1428 /* The two parts are in memory order already.
1429 Use the lower parts address as ours. */
1430 XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1431 /* Prevent sharing of rtl that might lose. */
1432 if (GET_CODE (XEXP (reg, 0)) == PLUS)
1433 XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
018577e4
R
1434 if (usedp)
1435 {
1436 schedule_fixup_var_refs (function, reg, TREE_TYPE (decl),
1437 promoted_mode, 0);
1438 schedule_fixup_var_refs (function, lopart, part_type, part_mode, 0);
1439 schedule_fixup_var_refs (function, hipart, part_type, part_mode, 0);
1440 }
293e3de4 1441 }
86fa911a
RK
1442 else
1443 return;
718fe406 1444
7d384cc0 1445 if (current_function_check_memory_usage)
ebb1b59a
BS
1446 emit_library_call (chkr_set_right_libfunc, LCT_CONST_MAKE_BLOCK, VOIDmode,
1447 3, XEXP (reg, 0), Pmode,
86fa911a
RK
1448 GEN_INT (GET_MODE_SIZE (GET_MODE (reg))),
1449 TYPE_MODE (sizetype),
956d6950
JL
1450 GEN_INT (MEMORY_USE_RW),
1451 TYPE_MODE (integer_type_node));
293e3de4
RS
1452}
1453
1454/* Subroutine of put_var_into_stack. This puts a single pseudo reg REG
1455 into the stack frame of FUNCTION (0 means the current function).
1456 DECL_MODE is the machine mode of the user-level data type.
0006e95b 1457 PROMOTED_MODE is the machine mode of the register.
e5e809f4
JL
1458 VOLATILE_P is nonzero if this is for a "volatile" decl.
1459 USED_P is nonzero if this reg might have already been used in an insn. */
293e3de4
RS
1460
1461static void
e9a25f70 1462put_reg_into_stack (function, reg, type, promoted_mode, decl_mode, volatile_p,
fe9b4957 1463 original_regno, used_p, ht)
293e3de4
RS
1464 struct function *function;
1465 rtx reg;
1466 tree type;
1467 enum machine_mode promoted_mode, decl_mode;
0006e95b 1468 int volatile_p;
770ae6cc 1469 unsigned int original_regno;
e5e809f4 1470 int used_p;
fe9b4957 1471 struct hash_table *ht;
293e3de4 1472{
01d939e8 1473 struct function *func = function ? function : cfun;
293e3de4 1474 rtx new = 0;
770ae6cc 1475 unsigned int regno = original_regno;
e9a25f70
JL
1476
1477 if (regno == 0)
1478 regno = REGNO (reg);
6f086dfc 1479
e2ecd91c
BS
1480 if (regno < func->x_max_parm_reg)
1481 new = func->x_parm_reg_stack_loc[regno];
770ae6cc 1482
e2ecd91c
BS
1483 if (new == 0)
1484 new = assign_stack_local_1 (decl_mode, GET_MODE_SIZE (decl_mode), 0, func);
6f086dfc 1485
ef178af3 1486 PUT_CODE (reg, MEM);
0006e95b 1487 PUT_MODE (reg, decl_mode);
6f086dfc
RS
1488 XEXP (reg, 0) = XEXP (new, 0);
1489 /* `volatil' bit means one thing for MEMs, another entirely for REGs. */
0006e95b 1490 MEM_VOLATILE_P (reg) = volatile_p;
6f086dfc
RS
1491
1492 /* If this is a memory ref that contains aggregate components,
bdd3e6ab
JW
1493 mark it as such for cse and loop optimize. If we are reusing a
1494 previously generated stack slot, then we need to copy the bit in
1495 case it was set for other reasons. For instance, it is set for
1496 __builtin_va_alist. */
8b4944fb
RH
1497 if (type)
1498 {
1499 MEM_SET_IN_STRUCT_P (reg,
1500 AGGREGATE_TYPE_P (type) || MEM_IN_STRUCT_P (new));
1501 MEM_ALIAS_SET (reg) = get_alias_set (type);
1502 }
018577e4
R
1503 if (used_p)
1504 schedule_fixup_var_refs (function, reg, type, promoted_mode, ht);
1505}
6f086dfc 1506
018577e4
R
1507/* Make sure that all refs to the variable, previously made
1508 when it was a register, are fixed up to be valid again.
1509 See function above for meaning of arguments. */
1510static void
1511schedule_fixup_var_refs (function, reg, type, promoted_mode, ht)
1512 struct function *function;
1513 rtx reg;
1514 tree type;
1515 enum machine_mode promoted_mode;
1516 struct hash_table *ht;
1517{
8b4944fb
RH
1518 int unsigned_p = type ? TREE_UNSIGNED (type) : 0;
1519
018577e4 1520 if (function != 0)
6f086dfc
RS
1521 {
1522 struct var_refs_queue *temp;
1523
6f086dfc 1524 temp
a3770a81 1525 = (struct var_refs_queue *) xmalloc (sizeof (struct var_refs_queue));
6f086dfc 1526 temp->modified = reg;
00d8a4c1 1527 temp->promoted_mode = promoted_mode;
8b4944fb 1528 temp->unsignedp = unsigned_p;
6f086dfc
RS
1529 temp->next = function->fixup_var_refs_queue;
1530 function->fixup_var_refs_queue = temp;
6f086dfc 1531 }
018577e4 1532 else
6f086dfc 1533 /* Variable is local; fix it up now. */
8b4944fb 1534 fixup_var_refs (reg, promoted_mode, unsigned_p, ht);
6f086dfc
RS
1535}
1536\f
1537static void
fe9b4957 1538fixup_var_refs (var, promoted_mode, unsignedp, ht)
6f086dfc 1539 rtx var;
00d8a4c1
RK
1540 enum machine_mode promoted_mode;
1541 int unsignedp;
fe9b4957 1542 struct hash_table *ht;
6f086dfc
RS
1543{
1544 tree pending;
1545 rtx first_insn = get_insns ();
49ad7cfa 1546 struct sequence_stack *stack = seq_stack;
6f086dfc 1547 tree rtl_exps = rtl_expr_chain;
0a1c58a2 1548 rtx insn;
6f086dfc
RS
1549
1550 /* Must scan all insns for stack-refs that exceed the limit. */
718fe406 1551 fixup_var_refs_insns (var, promoted_mode, unsignedp, first_insn,
fe9b4957
MM
1552 stack == 0, ht);
1553 /* If there's a hash table, it must record all uses of VAR. */
1554 if (ht)
1555 return;
6f086dfc
RS
1556
1557 /* Scan all pending sequences too. */
1558 for (; stack; stack = stack->next)
1559 {
1560 push_to_sequence (stack->first);
00d8a4c1 1561 fixup_var_refs_insns (var, promoted_mode, unsignedp,
fe9b4957 1562 stack->first, stack->next != 0, 0);
6f086dfc
RS
1563 /* Update remembered end of sequence
1564 in case we added an insn at the end. */
1565 stack->last = get_last_insn ();
1566 end_sequence ();
1567 }
1568
1569 /* Scan all waiting RTL_EXPRs too. */
1570 for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1571 {
1572 rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1573 if (seq != const0_rtx && seq != 0)
1574 {
1575 push_to_sequence (seq);
718fe406 1576 fixup_var_refs_insns (var, promoted_mode, unsignedp, seq, 0, 0);
6f086dfc
RS
1577 end_sequence ();
1578 }
1579 }
d33c2956
DB
1580
1581 /* Scan the catch clauses for exception handling too. */
c14f7160 1582 push_to_full_sequence (catch_clauses, catch_clauses_last);
718fe406 1583 fixup_var_refs_insns (var, promoted_mode, unsignedp, catch_clauses, 0, 0);
c14f7160 1584 end_full_sequence (&catch_clauses, &catch_clauses_last);
0a1c58a2
JL
1585
1586 /* Scan sequences saved in CALL_PLACEHOLDERS too. */
1587 for (insn = first_insn; insn; insn = NEXT_INSN (insn))
1588 {
1589 if (GET_CODE (insn) == CALL_INSN
1590 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1591 {
1592 int i;
1593
1594 /* Look at the Normal call, sibling call and tail recursion
1595 sequences attached to the CALL_PLACEHOLDER. */
1596 for (i = 0; i < 3; i++)
1597 {
1598 rtx seq = XEXP (PATTERN (insn), i);
1599 if (seq)
1600 {
1601 push_to_sequence (seq);
1602 fixup_var_refs_insns (var, promoted_mode, unsignedp,
1603 seq, 0, 0);
1604 XEXP (PATTERN (insn), i) = get_insns ();
1605 end_sequence ();
1606 }
1607 }
1608 }
1609 }
6f086dfc
RS
1610}
1611\f
e15679f8 1612/* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
6f086dfc 1613 some part of an insn. Return a struct fixup_replacement whose OLD
0f41302f 1614 value is equal to X. Allocate a new structure if no such entry exists. */
6f086dfc
RS
1615
1616static struct fixup_replacement *
2740a678 1617find_fixup_replacement (replacements, x)
6f086dfc
RS
1618 struct fixup_replacement **replacements;
1619 rtx x;
1620{
1621 struct fixup_replacement *p;
1622
1623 /* See if we have already replaced this. */
c5c76735 1624 for (p = *replacements; p != 0 && ! rtx_equal_p (p->old, x); p = p->next)
6f086dfc
RS
1625 ;
1626
1627 if (p == 0)
1628 {
1f8f4a0b 1629 p = (struct fixup_replacement *) xmalloc (sizeof (struct fixup_replacement));
6f086dfc
RS
1630 p->old = x;
1631 p->new = 0;
1632 p->next = *replacements;
1633 *replacements = p;
1634 }
1635
1636 return p;
1637}
1638
1639/* Scan the insn-chain starting with INSN for refs to VAR
1640 and fix them up. TOPLEVEL is nonzero if this chain is the
1641 main chain of insns for the current function. */
1642
1643static void
fe9b4957 1644fixup_var_refs_insns (var, promoted_mode, unsignedp, insn, toplevel, ht)
6f086dfc 1645 rtx var;
00d8a4c1
RK
1646 enum machine_mode promoted_mode;
1647 int unsignedp;
6f086dfc
RS
1648 rtx insn;
1649 int toplevel;
fe9b4957 1650 struct hash_table *ht;
6f086dfc 1651{
02a10449 1652 rtx call_dest = 0;
07444f1d 1653 rtx insn_list = NULL_RTX;
fe9b4957
MM
1654
1655 /* If we already know which INSNs reference VAR there's no need
1656 to walk the entire instruction chain. */
1657 if (ht)
1658 {
718fe406 1659 insn_list = ((struct insns_for_mem_entry *)
fe9b4957
MM
1660 hash_lookup (ht, var, /*create=*/0, /*copy=*/0))->insns;
1661 insn = insn_list ? XEXP (insn_list, 0) : NULL_RTX;
1662 insn_list = XEXP (insn_list, 1);
1663 }
02a10449 1664
6f086dfc
RS
1665 while (insn)
1666 {
1667 rtx next = NEXT_INSN (insn);
e5e809f4 1668 rtx set, prev, prev_set;
6f086dfc 1669 rtx note;
e5e809f4 1670
2c3c49de 1671 if (INSN_P (insn))
6f086dfc 1672 {
ef178af3
ZW
1673 /* Remember the notes in case we delete the insn. */
1674 note = REG_NOTES (insn);
1675
63770d6a
RK
1676 /* If this is a CLOBBER of VAR, delete it.
1677
1678 If it has a REG_LIBCALL note, delete the REG_LIBCALL
1679 and REG_RETVAL notes too. */
718fe406 1680 if (GET_CODE (PATTERN (insn)) == CLOBBER
07362cb3
JW
1681 && (XEXP (PATTERN (insn), 0) == var
1682 || (GET_CODE (XEXP (PATTERN (insn), 0)) == CONCAT
1683 && (XEXP (XEXP (PATTERN (insn), 0), 0) == var
1684 || XEXP (XEXP (PATTERN (insn), 0), 1) == var))))
63770d6a
RK
1685 {
1686 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1687 /* The REG_LIBCALL note will go away since we are going to
1688 turn INSN into a NOTE, so just delete the
1689 corresponding REG_RETVAL note. */
1690 remove_note (XEXP (note, 0),
1691 find_reg_note (XEXP (note, 0), REG_RETVAL,
1692 NULL_RTX));
1693
1694 /* In unoptimized compilation, we shouldn't call delete_insn
1695 except in jump.c doing warnings. */
1696 PUT_CODE (insn, NOTE);
1697 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1698 NOTE_SOURCE_FILE (insn) = 0;
1699 }
1700
6f086dfc 1701 /* The insn to load VAR from a home in the arglist
e5e809f4
JL
1702 is now a no-op. When we see it, just delete it.
1703 Similarly if this is storing VAR from a register from which
1704 it was loaded in the previous insn. This will occur
1705 when an ADDRESSOF was made for an arglist slot. */
63770d6a 1706 else if (toplevel
e5e809f4
JL
1707 && (set = single_set (insn)) != 0
1708 && SET_DEST (set) == var
63770d6a
RK
1709 /* If this represents the result of an insn group,
1710 don't delete the insn. */
1711 && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
e5e809f4
JL
1712 && (rtx_equal_p (SET_SRC (set), var)
1713 || (GET_CODE (SET_SRC (set)) == REG
1714 && (prev = prev_nonnote_insn (insn)) != 0
1715 && (prev_set = single_set (prev)) != 0
1716 && SET_DEST (prev_set) == SET_SRC (set)
1717 && rtx_equal_p (SET_SRC (prev_set), var))))
6f086dfc 1718 {
b4ff474c
RS
1719 /* In unoptimized compilation, we shouldn't call delete_insn
1720 except in jump.c doing warnings. */
1721 PUT_CODE (insn, NOTE);
1722 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1723 NOTE_SOURCE_FILE (insn) = 0;
6f086dfc
RS
1724 if (insn == last_parm_insn)
1725 last_parm_insn = PREV_INSN (next);
1726 }
1727 else
1728 {
02a10449
RK
1729 struct fixup_replacement *replacements = 0;
1730 rtx next_insn = NEXT_INSN (insn);
1731
e9a25f70
JL
1732 if (SMALL_REGISTER_CLASSES)
1733 {
1734 /* If the insn that copies the results of a CALL_INSN
1735 into a pseudo now references VAR, we have to use an
1736 intermediate pseudo since we want the life of the
1737 return value register to be only a single insn.
02a10449 1738
e9a25f70
JL
1739 If we don't use an intermediate pseudo, such things as
1740 address computations to make the address of VAR valid
1741 if it is not can be placed between the CALL_INSN and INSN.
02a10449 1742
e9a25f70
JL
1743 To make sure this doesn't happen, we record the destination
1744 of the CALL_INSN and see if the next insn uses both that
1745 and VAR. */
02a10449 1746
f95182a4
ILT
1747 if (call_dest != 0 && GET_CODE (insn) == INSN
1748 && reg_mentioned_p (var, PATTERN (insn))
1749 && reg_mentioned_p (call_dest, PATTERN (insn)))
1750 {
1751 rtx temp = gen_reg_rtx (GET_MODE (call_dest));
02a10449 1752
f95182a4 1753 emit_insn_before (gen_move_insn (temp, call_dest), insn);
02a10449 1754
f95182a4
ILT
1755 PATTERN (insn) = replace_rtx (PATTERN (insn),
1756 call_dest, temp);
1757 }
718fe406 1758
f95182a4
ILT
1759 if (GET_CODE (insn) == CALL_INSN
1760 && GET_CODE (PATTERN (insn)) == SET)
1761 call_dest = SET_DEST (PATTERN (insn));
1762 else if (GET_CODE (insn) == CALL_INSN
1763 && GET_CODE (PATTERN (insn)) == PARALLEL
1764 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1765 call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1766 else
1767 call_dest = 0;
1768 }
02a10449 1769
6f086dfc
RS
1770 /* See if we have to do anything to INSN now that VAR is in
1771 memory. If it needs to be loaded into a pseudo, use a single
1772 pseudo for the entire insn in case there is a MATCH_DUP
1773 between two operands. We pass a pointer to the head of
1774 a list of struct fixup_replacements. If fixup_var_refs_1
1775 needs to allocate pseudos or replacement MEMs (for SUBREGs),
1776 it will record them in this list.
718fe406 1777
6f086dfc
RS
1778 If it allocated a pseudo for any replacement, we copy into
1779 it here. */
1780
00d8a4c1
RK
1781 fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1782 &replacements);
6f086dfc 1783
77121fee
JW
1784 /* If this is last_parm_insn, and any instructions were output
1785 after it to fix it up, then we must set last_parm_insn to
1786 the last such instruction emitted. */
1787 if (insn == last_parm_insn)
1788 last_parm_insn = PREV_INSN (next_insn);
1789
6f086dfc
RS
1790 while (replacements)
1791 {
1f8f4a0b
MM
1792 struct fixup_replacement *next;
1793
6f086dfc
RS
1794 if (GET_CODE (replacements->new) == REG)
1795 {
1796 rtx insert_before;
00d8a4c1 1797 rtx seq;
6f086dfc
RS
1798
1799 /* OLD might be a (subreg (mem)). */
1800 if (GET_CODE (replacements->old) == SUBREG)
1801 replacements->old
1802 = fixup_memory_subreg (replacements->old, insn, 0);
1803 else
1804 replacements->old
1805 = fixup_stack_1 (replacements->old, insn);
1806
5fa7422b 1807 insert_before = insn;
6f086dfc 1808
00d8a4c1
RK
1809 /* If we are changing the mode, do a conversion.
1810 This might be wasteful, but combine.c will
1811 eliminate much of the waste. */
1812
1813 if (GET_MODE (replacements->new)
1814 != GET_MODE (replacements->old))
1815 {
1816 start_sequence ();
1817 convert_move (replacements->new,
1818 replacements->old, unsignedp);
1819 seq = gen_sequence ();
1820 end_sequence ();
1821 }
1822 else
1823 seq = gen_move_insn (replacements->new,
1824 replacements->old);
1825
1826 emit_insn_before (seq, insert_before);
6f086dfc
RS
1827 }
1828
1f8f4a0b
MM
1829 next = replacements->next;
1830 free (replacements);
1831 replacements = next;
6f086dfc
RS
1832 }
1833 }
1834
1835 /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1836 But don't touch other insns referred to by reg-notes;
1837 we will get them elsewhere. */
ef178af3
ZW
1838 while (note)
1839 {
1840 if (GET_CODE (note) != INSN_LIST)
1841 XEXP (note, 0)
1842 = walk_fixup_memory_subreg (XEXP (note, 0), insn, 1);
718fe406 1843 note = XEXP (note, 1);
ef178af3 1844 }
6f086dfc 1845 }
fe9b4957
MM
1846
1847 if (!ht)
1848 insn = next;
1849 else if (insn_list)
1850 {
1851 insn = XEXP (insn_list, 0);
1852 insn_list = XEXP (insn_list, 1);
1853 }
1854 else
1855 insn = NULL_RTX;
6f086dfc
RS
1856 }
1857}
1858\f
00d8a4c1 1859/* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
718fe406 1860 See if the rtx expression at *LOC in INSN needs to be changed.
6f086dfc
RS
1861
1862 REPLACEMENTS is a pointer to a list head that starts out zero, but may
1863 contain a list of original rtx's and replacements. If we find that we need
1864 to modify this insn by replacing a memory reference with a pseudo or by
1865 making a new MEM to implement a SUBREG, we consult that list to see if
1866 we have already chosen a replacement. If none has already been allocated,
1867 we allocate it and update the list. fixup_var_refs_insns will copy VAR
1868 or the SUBREG, as appropriate, to the pseudo. */
1869
1870static void
00d8a4c1 1871fixup_var_refs_1 (var, promoted_mode, loc, insn, replacements)
6f086dfc 1872 register rtx var;
00d8a4c1 1873 enum machine_mode promoted_mode;
6f086dfc
RS
1874 register rtx *loc;
1875 rtx insn;
1876 struct fixup_replacement **replacements;
1877{
1878 register int i;
1879 register rtx x = *loc;
1880 RTX_CODE code = GET_CODE (x);
6f7d635c 1881 register const char *fmt;
6f086dfc
RS
1882 register rtx tem, tem1;
1883 struct fixup_replacement *replacement;
1884
1885 switch (code)
1886 {
e9a25f70
JL
1887 case ADDRESSOF:
1888 if (XEXP (x, 0) == var)
1889 {
956d6950
JL
1890 /* Prevent sharing of rtl that might lose. */
1891 rtx sub = copy_rtx (XEXP (var, 0));
1892
956d6950
JL
1893 if (! validate_change (insn, loc, sub, 0))
1894 {
5f98f7c4
RH
1895 rtx y = gen_reg_rtx (GET_MODE (sub));
1896 rtx seq, new_insn;
1897
1898 /* We should be able to replace with a register or all is lost.
1899 Note that we can't use validate_change to verify this, since
1900 we're not caring for replacing all dups simultaneously. */
1901 if (! validate_replace_rtx (*loc, y, insn))
1902 abort ();
1903
1904 /* Careful! First try to recognize a direct move of the
1905 value, mimicking how things are done in gen_reload wrt
1906 PLUS. Consider what happens when insn is a conditional
1907 move instruction and addsi3 clobbers flags. */
1908
1909 start_sequence ();
1910 new_insn = emit_insn (gen_rtx_SET (VOIDmode, y, sub));
1911 seq = gen_sequence ();
1912 end_sequence ();
1913
1914 if (recog_memoized (new_insn) < 0)
1915 {
1916 /* That failed. Fall back on force_operand and hope. */
956d6950 1917
5f98f7c4
RH
1918 start_sequence ();
1919 force_operand (sub, y);
1920 seq = gen_sequence ();
1921 end_sequence ();
1922 }
956d6950 1923
5f98f7c4
RH
1924#ifdef HAVE_cc0
1925 /* Don't separate setter from user. */
1926 if (PREV_INSN (insn) && sets_cc0_p (PREV_INSN (insn)))
1927 insn = PREV_INSN (insn);
1928#endif
1929
1930 emit_insn_before (seq, insn);
1931 }
e9a25f70
JL
1932 }
1933 return;
1934
6f086dfc
RS
1935 case MEM:
1936 if (var == x)
1937 {
718fe406 1938 /* If we already have a replacement, use it. Otherwise,
6f086dfc
RS
1939 try to fix up this address in case it is invalid. */
1940
2740a678 1941 replacement = find_fixup_replacement (replacements, var);
6f086dfc
RS
1942 if (replacement->new)
1943 {
1944 *loc = replacement->new;
1945 return;
1946 }
1947
1948 *loc = replacement->new = x = fixup_stack_1 (x, insn);
1949
00d8a4c1
RK
1950 /* Unless we are forcing memory to register or we changed the mode,
1951 we can leave things the way they are if the insn is valid. */
718fe406 1952
6f086dfc 1953 INSN_CODE (insn) = -1;
00d8a4c1
RK
1954 if (! flag_force_mem && GET_MODE (x) == promoted_mode
1955 && recog_memoized (insn) >= 0)
6f086dfc
RS
1956 return;
1957
00d8a4c1 1958 *loc = replacement->new = gen_reg_rtx (promoted_mode);
6f086dfc
RS
1959 return;
1960 }
1961
1962 /* If X contains VAR, we need to unshare it here so that we update
1963 each occurrence separately. But all identical MEMs in one insn
1964 must be replaced with the same rtx because of the possibility of
1965 MATCH_DUPs. */
1966
1967 if (reg_mentioned_p (var, x))
1968 {
2740a678 1969 replacement = find_fixup_replacement (replacements, x);
6f086dfc
RS
1970 if (replacement->new == 0)
1971 replacement->new = copy_most_rtx (x, var);
1972
1973 *loc = x = replacement->new;
3f546a53 1974 code = GET_CODE (x);
6f086dfc
RS
1975 }
1976 break;
1977
1978 case REG:
1979 case CC0:
1980 case PC:
1981 case CONST_INT:
1982 case CONST:
1983 case SYMBOL_REF:
1984 case LABEL_REF:
1985 case CONST_DOUBLE:
1986 return;
1987
1988 case SIGN_EXTRACT:
1989 case ZERO_EXTRACT:
1990 /* Note that in some cases those types of expressions are altered
1991 by optimize_bit_field, and do not survive to get here. */
1992 if (XEXP (x, 0) == var
1993 || (GET_CODE (XEXP (x, 0)) == SUBREG
1994 && SUBREG_REG (XEXP (x, 0)) == var))
1995 {
1996 /* Get TEM as a valid MEM in the mode presently in the insn.
1997
1998 We don't worry about the possibility of MATCH_DUP here; it
1999 is highly unlikely and would be tricky to handle. */
2000
2001 tem = XEXP (x, 0);
2002 if (GET_CODE (tem) == SUBREG)
0e09cc26
RK
2003 {
2004 if (GET_MODE_BITSIZE (GET_MODE (tem))
2005 > GET_MODE_BITSIZE (GET_MODE (var)))
2006 {
2007 replacement = find_fixup_replacement (replacements, var);
2008 if (replacement->new == 0)
2009 replacement->new = gen_reg_rtx (GET_MODE (var));
2010 SUBREG_REG (tem) = replacement->new;
226ed43f
JW
2011
2012 /* The following code works only if we have a MEM, so we
2013 need to handle the subreg here. We directly substitute
2014 it assuming that a subreg must be OK here. We already
2015 scheduled a replacement to copy the mem into the
2016 subreg. */
2017 XEXP (x, 0) = tem;
2018 return;
0e09cc26 2019 }
ef933d26
RK
2020 else
2021 tem = fixup_memory_subreg (tem, insn, 0);
0e09cc26
RK
2022 }
2023 else
2024 tem = fixup_stack_1 (tem, insn);
6f086dfc
RS
2025
2026 /* Unless we want to load from memory, get TEM into the proper mode
2027 for an extract from memory. This can only be done if the
2028 extract is at a constant position and length. */
2029
2030 if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
2031 && GET_CODE (XEXP (x, 2)) == CONST_INT
2032 && ! mode_dependent_address_p (XEXP (tem, 0))
2033 && ! MEM_VOLATILE_P (tem))
2034 {
2035 enum machine_mode wanted_mode = VOIDmode;
2036 enum machine_mode is_mode = GET_MODE (tem);
e5e809f4 2037 HOST_WIDE_INT pos = INTVAL (XEXP (x, 2));
6f086dfc
RS
2038
2039#ifdef HAVE_extzv
2040 if (GET_CODE (x) == ZERO_EXTRACT)
0d8e55d8 2041 {
a995e389
RH
2042 wanted_mode
2043 = insn_data[(int) CODE_FOR_extzv].operand[1].mode;
0d8e55d8
JL
2044 if (wanted_mode == VOIDmode)
2045 wanted_mode = word_mode;
2046 }
6f086dfc
RS
2047#endif
2048#ifdef HAVE_extv
2049 if (GET_CODE (x) == SIGN_EXTRACT)
0d8e55d8 2050 {
a995e389 2051 wanted_mode = insn_data[(int) CODE_FOR_extv].operand[1].mode;
0d8e55d8
JL
2052 if (wanted_mode == VOIDmode)
2053 wanted_mode = word_mode;
2054 }
6f086dfc 2055#endif
6dc42e49 2056 /* If we have a narrower mode, we can do something. */
6f086dfc
RS
2057 if (wanted_mode != VOIDmode
2058 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2059 {
e5e809f4 2060 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
6f086dfc
RS
2061 rtx old_pos = XEXP (x, 2);
2062 rtx newmem;
2063
2064 /* If the bytes and bits are counted differently, we
2065 must adjust the offset. */
f76b9db2
ILT
2066 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2067 offset = (GET_MODE_SIZE (is_mode)
2068 - GET_MODE_SIZE (wanted_mode) - offset);
6f086dfc
RS
2069
2070 pos %= GET_MODE_BITSIZE (wanted_mode);
2071
38a448ca
RH
2072 newmem = gen_rtx_MEM (wanted_mode,
2073 plus_constant (XEXP (tem, 0), offset));
c6df88cb 2074 MEM_COPY_ATTRIBUTES (newmem, tem);
6f086dfc
RS
2075
2076 /* Make the change and see if the insn remains valid. */
2077 INSN_CODE (insn) = -1;
2078 XEXP (x, 0) = newmem;
5f4f0e22 2079 XEXP (x, 2) = GEN_INT (pos);
6f086dfc
RS
2080
2081 if (recog_memoized (insn) >= 0)
2082 return;
2083
2084 /* Otherwise, restore old position. XEXP (x, 0) will be
2085 restored later. */
2086 XEXP (x, 2) = old_pos;
2087 }
2088 }
2089
2090 /* If we get here, the bitfield extract insn can't accept a memory
2091 reference. Copy the input into a register. */
2092
2093 tem1 = gen_reg_rtx (GET_MODE (tem));
2094 emit_insn_before (gen_move_insn (tem1, tem), insn);
2095 XEXP (x, 0) = tem1;
2096 return;
2097 }
2098 break;
718fe406 2099
6f086dfc
RS
2100 case SUBREG:
2101 if (SUBREG_REG (x) == var)
2102 {
00d8a4c1
RK
2103 /* If this is a special SUBREG made because VAR was promoted
2104 from a wider mode, replace it with VAR and call ourself
2105 recursively, this time saying that the object previously
2106 had its current mode (by virtue of the SUBREG). */
2107
2108 if (SUBREG_PROMOTED_VAR_P (x))
2109 {
2110 *loc = var;
2111 fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements);
2112 return;
2113 }
2114
6f086dfc 2115 /* If this SUBREG makes VAR wider, it has become a paradoxical
718fe406 2116 SUBREG with VAR in memory, but these aren't allowed at this
6f086dfc
RS
2117 stage of the compilation. So load VAR into a pseudo and take
2118 a SUBREG of that pseudo. */
2119 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
2120 {
2740a678 2121 replacement = find_fixup_replacement (replacements, var);
6f086dfc
RS
2122 if (replacement->new == 0)
2123 replacement->new = gen_reg_rtx (GET_MODE (var));
2124 SUBREG_REG (x) = replacement->new;
2125 return;
2126 }
2127
2128 /* See if we have already found a replacement for this SUBREG.
2129 If so, use it. Otherwise, make a MEM and see if the insn
2130 is recognized. If not, or if we should force MEM into a register,
2131 make a pseudo for this SUBREG. */
2740a678 2132 replacement = find_fixup_replacement (replacements, x);
6f086dfc
RS
2133 if (replacement->new)
2134 {
2135 *loc = replacement->new;
2136 return;
2137 }
718fe406 2138
6f086dfc
RS
2139 replacement->new = *loc = fixup_memory_subreg (x, insn, 0);
2140
f898f031 2141 INSN_CODE (insn) = -1;
6f086dfc
RS
2142 if (! flag_force_mem && recog_memoized (insn) >= 0)
2143 return;
2144
2145 *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
2146 return;
2147 }
2148 break;
2149
2150 case SET:
2151 /* First do special simplification of bit-field references. */
2152 if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
2153 || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2154 optimize_bit_field (x, insn, 0);
2155 if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
2156 || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
5f4f0e22 2157 optimize_bit_field (x, insn, NULL_PTR);
6f086dfc 2158
0e09cc26
RK
2159 /* For a paradoxical SUBREG inside a ZERO_EXTRACT, load the object
2160 into a register and then store it back out. */
2161 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2162 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG
2163 && SUBREG_REG (XEXP (SET_DEST (x), 0)) == var
2164 && (GET_MODE_SIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2165 > GET_MODE_SIZE (GET_MODE (var))))
2166 {
2167 replacement = find_fixup_replacement (replacements, var);
2168 if (replacement->new == 0)
2169 replacement->new = gen_reg_rtx (GET_MODE (var));
2170
2171 SUBREG_REG (XEXP (SET_DEST (x), 0)) = replacement->new;
2172 emit_insn_after (gen_move_insn (var, replacement->new), insn);
2173 }
2174
6f086dfc 2175 /* If SET_DEST is now a paradoxical SUBREG, put the result of this
0f41302f 2176 insn into a pseudo and store the low part of the pseudo into VAR. */
6f086dfc
RS
2177 if (GET_CODE (SET_DEST (x)) == SUBREG
2178 && SUBREG_REG (SET_DEST (x)) == var
2179 && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2180 > GET_MODE_SIZE (GET_MODE (var))))
2181 {
2182 SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
2183 emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
2184 tem)),
2185 insn);
2186 break;
2187 }
718fe406 2188
6f086dfc
RS
2189 {
2190 rtx dest = SET_DEST (x);
2191 rtx src = SET_SRC (x);
29a82058 2192#ifdef HAVE_insv
6f086dfc 2193 rtx outerdest = dest;
29a82058 2194#endif
6f086dfc
RS
2195
2196 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2197 || GET_CODE (dest) == SIGN_EXTRACT
2198 || GET_CODE (dest) == ZERO_EXTRACT)
2199 dest = XEXP (dest, 0);
2200
2201 if (GET_CODE (src) == SUBREG)
2202 src = XEXP (src, 0);
2203
2204 /* If VAR does not appear at the top level of the SET
2205 just scan the lower levels of the tree. */
2206
718fe406 2207 if (src != var && dest != var)
6f086dfc
RS
2208 break;
2209
2210 /* We will need to rerecognize this insn. */
2211 INSN_CODE (insn) = -1;
2212
2213#ifdef HAVE_insv
2214 if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var)
2215 {
2216 /* Since this case will return, ensure we fixup all the
2217 operands here. */
00d8a4c1
RK
2218 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
2219 insn, replacements);
2220 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
2221 insn, replacements);
2222 fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
2223 insn, replacements);
6f086dfc
RS
2224
2225 tem = XEXP (outerdest, 0);
2226
2227 /* Clean up (SUBREG:SI (MEM:mode ...) 0)
2228 that may appear inside a ZERO_EXTRACT.
2229 This was legitimate when the MEM was a REG. */
2230 if (GET_CODE (tem) == SUBREG
2231 && SUBREG_REG (tem) == var)
0e09cc26 2232 tem = fixup_memory_subreg (tem, insn, 0);
6f086dfc
RS
2233 else
2234 tem = fixup_stack_1 (tem, insn);
2235
2236 if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
2237 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
2238 && ! mode_dependent_address_p (XEXP (tem, 0))
2239 && ! MEM_VOLATILE_P (tem))
2240 {
0d8e55d8 2241 enum machine_mode wanted_mode;
6f086dfc 2242 enum machine_mode is_mode = GET_MODE (tem);
e5e809f4 2243 HOST_WIDE_INT pos = INTVAL (XEXP (outerdest, 2));
6f086dfc 2244
a995e389 2245 wanted_mode = insn_data[(int) CODE_FOR_insv].operand[0].mode;
0d8e55d8
JL
2246 if (wanted_mode == VOIDmode)
2247 wanted_mode = word_mode;
2248
6dc42e49 2249 /* If we have a narrower mode, we can do something. */
6f086dfc
RS
2250 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2251 {
e5e809f4 2252 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
6f086dfc
RS
2253 rtx old_pos = XEXP (outerdest, 2);
2254 rtx newmem;
2255
f76b9db2
ILT
2256 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2257 offset = (GET_MODE_SIZE (is_mode)
2258 - GET_MODE_SIZE (wanted_mode) - offset);
6f086dfc
RS
2259
2260 pos %= GET_MODE_BITSIZE (wanted_mode);
2261
38a448ca 2262 newmem = gen_rtx_MEM (wanted_mode,
c5c76735
JL
2263 plus_constant (XEXP (tem, 0),
2264 offset));
c6df88cb 2265 MEM_COPY_ATTRIBUTES (newmem, tem);
6f086dfc
RS
2266
2267 /* Make the change and see if the insn remains valid. */
2268 INSN_CODE (insn) = -1;
2269 XEXP (outerdest, 0) = newmem;
5f4f0e22 2270 XEXP (outerdest, 2) = GEN_INT (pos);
718fe406 2271
6f086dfc
RS
2272 if (recog_memoized (insn) >= 0)
2273 return;
718fe406 2274
6f086dfc
RS
2275 /* Otherwise, restore old position. XEXP (x, 0) will be
2276 restored later. */
2277 XEXP (outerdest, 2) = old_pos;
2278 }
2279 }
2280
2281 /* If we get here, the bit-field store doesn't allow memory
2282 or isn't located at a constant position. Load the value into
2283 a register, do the store, and put it back into memory. */
2284
2285 tem1 = gen_reg_rtx (GET_MODE (tem));
2286 emit_insn_before (gen_move_insn (tem1, tem), insn);
2287 emit_insn_after (gen_move_insn (tem, tem1), insn);
2288 XEXP (outerdest, 0) = tem1;
2289 return;
2290 }
2291#endif
2292
2293 /* STRICT_LOW_PART is a no-op on memory references
2294 and it can cause combinations to be unrecognizable,
2295 so eliminate it. */
2296
2297 if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2298 SET_DEST (x) = XEXP (SET_DEST (x), 0);
2299
2300 /* A valid insn to copy VAR into or out of a register
2301 must be left alone, to avoid an infinite loop here.
2302 If the reference to VAR is by a subreg, fix that up,
2303 since SUBREG is not valid for a memref.
e15762df
RK
2304 Also fix up the address of the stack slot.
2305
2306 Note that we must not try to recognize the insn until
2307 after we know that we have valid addresses and no
2308 (subreg (mem ...) ...) constructs, since these interfere
2309 with determining the validity of the insn. */
6f086dfc
RS
2310
2311 if ((SET_SRC (x) == var
2312 || (GET_CODE (SET_SRC (x)) == SUBREG
2313 && SUBREG_REG (SET_SRC (x)) == var))
2314 && (GET_CODE (SET_DEST (x)) == REG
2315 || (GET_CODE (SET_DEST (x)) == SUBREG
2316 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG))
1d273bf5 2317 && GET_MODE (var) == promoted_mode
c46722a7 2318 && x == single_set (insn))
6f086dfc 2319 {
cc0cbae1 2320 rtx pat, last;
e15762df 2321
2740a678 2322 replacement = find_fixup_replacement (replacements, SET_SRC (x));
6f086dfc 2323 if (replacement->new)
6f086dfc 2324 SET_SRC (x) = replacement->new;
6f086dfc
RS
2325 else if (GET_CODE (SET_SRC (x)) == SUBREG)
2326 SET_SRC (x) = replacement->new
2327 = fixup_memory_subreg (SET_SRC (x), insn, 0);
2328 else
2329 SET_SRC (x) = replacement->new
2330 = fixup_stack_1 (SET_SRC (x), insn);
e15762df
RK
2331
2332 if (recog_memoized (insn) >= 0)
2333 return;
2334
2335 /* INSN is not valid, but we know that we want to
2336 copy SET_SRC (x) to SET_DEST (x) in some way. So
2337 we generate the move and see whether it requires more
2338 than one insn. If it does, we emit those insns and
718fe406 2339 delete INSN. Otherwise, we an just replace the pattern
e15762df
RK
2340 of INSN; we have already verified above that INSN has
2341 no other function that to do X. */
2342
2343 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2344 if (GET_CODE (pat) == SEQUENCE)
2345 {
cc0cbae1
JW
2346 last = emit_insn_before (pat, insn);
2347
2348 /* INSN might have REG_RETVAL or other important notes, so
2349 we need to store the pattern of the last insn in the
2350 sequence into INSN similarly to the normal case. LAST
2351 should not have REG_NOTES, but we allow them if INSN has
2352 no REG_NOTES. */
2353 if (REG_NOTES (last) && REG_NOTES (insn))
2354 abort ();
2355 if (REG_NOTES (last))
2356 REG_NOTES (insn) = REG_NOTES (last);
2357 PATTERN (insn) = PATTERN (last);
2358
2359 PUT_CODE (last, NOTE);
2360 NOTE_LINE_NUMBER (last) = NOTE_INSN_DELETED;
2361 NOTE_SOURCE_FILE (last) = 0;
e15762df
RK
2362 }
2363 else
2364 PATTERN (insn) = pat;
2365
6f086dfc
RS
2366 return;
2367 }
2368
2369 if ((SET_DEST (x) == var
2370 || (GET_CODE (SET_DEST (x)) == SUBREG
2371 && SUBREG_REG (SET_DEST (x)) == var))
2372 && (GET_CODE (SET_SRC (x)) == REG
2373 || (GET_CODE (SET_SRC (x)) == SUBREG
2374 && GET_CODE (SUBREG_REG (SET_SRC (x))) == REG))
1d273bf5 2375 && GET_MODE (var) == promoted_mode
c46722a7 2376 && x == single_set (insn))
6f086dfc 2377 {
cc0cbae1 2378 rtx pat, last;
e15762df 2379
6f086dfc
RS
2380 if (GET_CODE (SET_DEST (x)) == SUBREG)
2381 SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn, 0);
2382 else
2383 SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
e15762df
RK
2384
2385 if (recog_memoized (insn) >= 0)
2386 return;
2387
2388 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2389 if (GET_CODE (pat) == SEQUENCE)
2390 {
cc0cbae1
JW
2391 last = emit_insn_before (pat, insn);
2392
2393 /* INSN might have REG_RETVAL or other important notes, so
2394 we need to store the pattern of the last insn in the
2395 sequence into INSN similarly to the normal case. LAST
2396 should not have REG_NOTES, but we allow them if INSN has
2397 no REG_NOTES. */
2398 if (REG_NOTES (last) && REG_NOTES (insn))
2399 abort ();
2400 if (REG_NOTES (last))
2401 REG_NOTES (insn) = REG_NOTES (last);
2402 PATTERN (insn) = PATTERN (last);
2403
2404 PUT_CODE (last, NOTE);
2405 NOTE_LINE_NUMBER (last) = NOTE_INSN_DELETED;
2406 NOTE_SOURCE_FILE (last) = 0;
e15762df
RK
2407 }
2408 else
2409 PATTERN (insn) = pat;
2410
6f086dfc
RS
2411 return;
2412 }
2413
2414 /* Otherwise, storing into VAR must be handled specially
2415 by storing into a temporary and copying that into VAR
00d8a4c1
RK
2416 with a new insn after this one. Note that this case
2417 will be used when storing into a promoted scalar since
2418 the insn will now have different modes on the input
2419 and output and hence will be invalid (except for the case
2420 of setting it to a constant, which does not need any
2421 change if it is valid). We generate extra code in that case,
2422 but combine.c will eliminate it. */
6f086dfc
RS
2423
2424 if (dest == var)
2425 {
2426 rtx temp;
00d8a4c1
RK
2427 rtx fixeddest = SET_DEST (x);
2428
6f086dfc 2429 /* STRICT_LOW_PART can be discarded, around a MEM. */
00d8a4c1
RK
2430 if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2431 fixeddest = XEXP (fixeddest, 0);
6f086dfc 2432 /* Convert (SUBREG (MEM)) to a MEM in a changed mode. */
00d8a4c1 2433 if (GET_CODE (fixeddest) == SUBREG)
926d1ca5
RK
2434 {
2435 fixeddest = fixup_memory_subreg (fixeddest, insn, 0);
2436 promoted_mode = GET_MODE (fixeddest);
2437 }
6f086dfc 2438 else
00d8a4c1
RK
2439 fixeddest = fixup_stack_1 (fixeddest, insn);
2440
926d1ca5 2441 temp = gen_reg_rtx (promoted_mode);
00d8a4c1
RK
2442
2443 emit_insn_after (gen_move_insn (fixeddest,
2444 gen_lowpart (GET_MODE (fixeddest),
2445 temp)),
2446 insn);
6f086dfc 2447
6f086dfc
RS
2448 SET_DEST (x) = temp;
2449 }
2450 }
e9a25f70
JL
2451
2452 default:
2453 break;
6f086dfc
RS
2454 }
2455
2456 /* Nothing special about this RTX; fix its operands. */
2457
2458 fmt = GET_RTX_FORMAT (code);
2459 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2460 {
2461 if (fmt[i] == 'e')
00d8a4c1 2462 fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements);
d4757e6a 2463 else if (fmt[i] == 'E')
6f086dfc
RS
2464 {
2465 register int j;
2466 for (j = 0; j < XVECLEN (x, i); j++)
00d8a4c1
RK
2467 fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2468 insn, replacements);
6f086dfc
RS
2469 }
2470 }
2471}
2472\f
2473/* Given X, an rtx of the form (SUBREG:m1 (MEM:m2 addr)),
2474 return an rtx (MEM:m1 newaddr) which is equivalent.
2475 If any insns must be emitted to compute NEWADDR, put them before INSN.
2476
2477 UNCRITICAL nonzero means accept paradoxical subregs.
0f41302f 2478 This is used for subregs found inside REG_NOTES. */
6f086dfc
RS
2479
2480static rtx
2481fixup_memory_subreg (x, insn, uncritical)
2482 rtx x;
2483 rtx insn;
2484 int uncritical;
2485{
2486 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2487 rtx addr = XEXP (SUBREG_REG (x), 0);
2488 enum machine_mode mode = GET_MODE (x);
29a82058 2489 rtx result;
6f086dfc
RS
2490
2491 /* Paradoxical SUBREGs are usually invalid during RTL generation. */
2492 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
2493 && ! uncritical)
2494 abort ();
2495
f76b9db2
ILT
2496 if (BYTES_BIG_ENDIAN)
2497 offset += (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2498 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
6f086dfc
RS
2499 addr = plus_constant (addr, offset);
2500 if (!flag_force_addr && memory_address_p (mode, addr))
2501 /* Shortcut if no insns need be emitted. */
2502 return change_address (SUBREG_REG (x), mode, addr);
2503 start_sequence ();
2504 result = change_address (SUBREG_REG (x), mode, addr);
2505 emit_insn_before (gen_sequence (), insn);
2506 end_sequence ();
2507 return result;
2508}
2509
2510/* Do fixup_memory_subreg on all (SUBREG (MEM ...) ...) contained in X.
2511 Replace subexpressions of X in place.
2512 If X itself is a (SUBREG (MEM ...) ...), return the replacement expression.
2513 Otherwise return X, with its contents possibly altered.
2514
718fe406 2515 If any insns must be emitted to compute NEWADDR, put them before INSN.
ab6155b7
RK
2516
2517 UNCRITICAL is as in fixup_memory_subreg. */
6f086dfc
RS
2518
2519static rtx
ab6155b7 2520walk_fixup_memory_subreg (x, insn, uncritical)
6f086dfc
RS
2521 register rtx x;
2522 rtx insn;
ab6155b7 2523 int uncritical;
6f086dfc
RS
2524{
2525 register enum rtx_code code;
6f7d635c 2526 register const char *fmt;
6f086dfc
RS
2527 register int i;
2528
2529 if (x == 0)
2530 return 0;
2531
2532 code = GET_CODE (x);
2533
2534 if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
ab6155b7 2535 return fixup_memory_subreg (x, insn, uncritical);
6f086dfc
RS
2536
2537 /* Nothing special about this RTX; fix its operands. */
2538
2539 fmt = GET_RTX_FORMAT (code);
2540 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2541 {
2542 if (fmt[i] == 'e')
ab6155b7 2543 XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn, uncritical);
d4757e6a 2544 else if (fmt[i] == 'E')
6f086dfc
RS
2545 {
2546 register int j;
2547 for (j = 0; j < XVECLEN (x, i); j++)
2548 XVECEXP (x, i, j)
ab6155b7 2549 = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn, uncritical);
6f086dfc
RS
2550 }
2551 }
2552 return x;
2553}
2554\f
6f086dfc
RS
2555/* For each memory ref within X, if it refers to a stack slot
2556 with an out of range displacement, put the address in a temp register
2557 (emitting new insns before INSN to load these registers)
2558 and alter the memory ref to use that register.
2559 Replace each such MEM rtx with a copy, to avoid clobberage. */
2560
2561static rtx
2562fixup_stack_1 (x, insn)
2563 rtx x;
2564 rtx insn;
2565{
2566 register int i;
2567 register RTX_CODE code = GET_CODE (x);
6f7d635c 2568 register const char *fmt;
6f086dfc
RS
2569
2570 if (code == MEM)
2571 {
2572 register rtx ad = XEXP (x, 0);
2573 /* If we have address of a stack slot but it's not valid
2574 (displacement is too large), compute the sum in a register. */
2575 if (GET_CODE (ad) == PLUS
2576 && GET_CODE (XEXP (ad, 0)) == REG
40d05551
RK
2577 && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2578 && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
e9a25f70
JL
2579 || REGNO (XEXP (ad, 0)) == FRAME_POINTER_REGNUM
2580#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2581 || REGNO (XEXP (ad, 0)) == HARD_FRAME_POINTER_REGNUM
2582#endif
2583 || REGNO (XEXP (ad, 0)) == STACK_POINTER_REGNUM
956d6950 2584 || REGNO (XEXP (ad, 0)) == ARG_POINTER_REGNUM
40d05551 2585 || XEXP (ad, 0) == current_function_internal_arg_pointer)
6f086dfc
RS
2586 && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2587 {
2588 rtx temp, seq;
2589 if (memory_address_p (GET_MODE (x), ad))
2590 return x;
2591
2592 start_sequence ();
2593 temp = copy_to_reg (ad);
2594 seq = gen_sequence ();
2595 end_sequence ();
2596 emit_insn_before (seq, insn);
2597 return change_address (x, VOIDmode, temp);
2598 }
2599 return x;
2600 }
2601
2602 fmt = GET_RTX_FORMAT (code);
2603 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2604 {
2605 if (fmt[i] == 'e')
2606 XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
d4757e6a 2607 else if (fmt[i] == 'E')
6f086dfc
RS
2608 {
2609 register int j;
2610 for (j = 0; j < XVECLEN (x, i); j++)
2611 XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2612 }
2613 }
2614 return x;
2615}
2616\f
2617/* Optimization: a bit-field instruction whose field
2618 happens to be a byte or halfword in memory
2619 can be changed to a move instruction.
2620
2621 We call here when INSN is an insn to examine or store into a bit-field.
2622 BODY is the SET-rtx to be altered.
2623
2624 EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2625 (Currently this is called only from function.c, and EQUIV_MEM
2626 is always 0.) */
2627
2628static void
2629optimize_bit_field (body, insn, equiv_mem)
2630 rtx body;
2631 rtx insn;
2632 rtx *equiv_mem;
2633{
2634 register rtx bitfield;
2635 int destflag;
2636 rtx seq = 0;
2637 enum machine_mode mode;
2638
2639 if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2640 || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2641 bitfield = SET_DEST (body), destflag = 1;
2642 else
2643 bitfield = SET_SRC (body), destflag = 0;
2644
2645 /* First check that the field being stored has constant size and position
2646 and is in fact a byte or halfword suitably aligned. */
2647
2648 if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2649 && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2650 && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2651 != BLKmode)
2652 && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2653 {
2654 register rtx memref = 0;
2655
2656 /* Now check that the containing word is memory, not a register,
2657 and that it is safe to change the machine mode. */
2658
2659 if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2660 memref = XEXP (bitfield, 0);
2661 else if (GET_CODE (XEXP (bitfield, 0)) == REG
2662 && equiv_mem != 0)
2663 memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2664 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2665 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2666 memref = SUBREG_REG (XEXP (bitfield, 0));
2667 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2668 && equiv_mem != 0
2669 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == REG)
2670 memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2671
2672 if (memref
2673 && ! mode_dependent_address_p (XEXP (memref, 0))
2674 && ! MEM_VOLATILE_P (memref))
2675 {
2676 /* Now adjust the address, first for any subreg'ing
2677 that we are now getting rid of,
2678 and then for which byte of the word is wanted. */
2679
e5e809f4 2680 HOST_WIDE_INT offset = INTVAL (XEXP (bitfield, 2));
b88a3142
RK
2681 rtx insns;
2682
6f086dfc 2683 /* Adjust OFFSET to count bits from low-address byte. */
f76b9db2
ILT
2684 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2685 offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2686 - offset - INTVAL (XEXP (bitfield, 1)));
2687
6f086dfc
RS
2688 /* Adjust OFFSET to count bytes from low-address byte. */
2689 offset /= BITS_PER_UNIT;
2690 if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2691 {
2692 offset += SUBREG_WORD (XEXP (bitfield, 0)) * UNITS_PER_WORD;
f76b9db2
ILT
2693 if (BYTES_BIG_ENDIAN)
2694 offset -= (MIN (UNITS_PER_WORD,
2695 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2696 - MIN (UNITS_PER_WORD,
2697 GET_MODE_SIZE (GET_MODE (memref))));
6f086dfc
RS
2698 }
2699
b88a3142
RK
2700 start_sequence ();
2701 memref = change_address (memref, mode,
6f086dfc 2702 plus_constant (XEXP (memref, 0), offset));
b88a3142
RK
2703 insns = get_insns ();
2704 end_sequence ();
2705 emit_insns_before (insns, insn);
6f086dfc
RS
2706
2707 /* Store this memory reference where
2708 we found the bit field reference. */
2709
2710 if (destflag)
2711 {
2712 validate_change (insn, &SET_DEST (body), memref, 1);
2713 if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2714 {
2715 rtx src = SET_SRC (body);
2716 while (GET_CODE (src) == SUBREG
2717 && SUBREG_WORD (src) == 0)
2718 src = SUBREG_REG (src);
2719 if (GET_MODE (src) != GET_MODE (memref))
2720 src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2721 validate_change (insn, &SET_SRC (body), src, 1);
2722 }
2723 else if (GET_MODE (SET_SRC (body)) != VOIDmode
2724 && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2725 /* This shouldn't happen because anything that didn't have
2726 one of these modes should have got converted explicitly
2727 and then referenced through a subreg.
2728 This is so because the original bit-field was
2729 handled by agg_mode and so its tree structure had
2730 the same mode that memref now has. */
2731 abort ();
2732 }
2733 else
2734 {
2735 rtx dest = SET_DEST (body);
2736
2737 while (GET_CODE (dest) == SUBREG
4013a709
RK
2738 && SUBREG_WORD (dest) == 0
2739 && (GET_MODE_CLASS (GET_MODE (dest))
ab87f8c8
JL
2740 == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest))))
2741 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
2742 <= UNITS_PER_WORD))
6f086dfc
RS
2743 dest = SUBREG_REG (dest);
2744
2745 validate_change (insn, &SET_DEST (body), dest, 1);
2746
2747 if (GET_MODE (dest) == GET_MODE (memref))
2748 validate_change (insn, &SET_SRC (body), memref, 1);
2749 else
2750 {
2751 /* Convert the mem ref to the destination mode. */
2752 rtx newreg = gen_reg_rtx (GET_MODE (dest));
2753
2754 start_sequence ();
2755 convert_move (newreg, memref,
2756 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2757 seq = get_insns ();
2758 end_sequence ();
2759
2760 validate_change (insn, &SET_SRC (body), newreg, 1);
2761 }
2762 }
2763
2764 /* See if we can convert this extraction or insertion into
2765 a simple move insn. We might not be able to do so if this
2766 was, for example, part of a PARALLEL.
2767
2768 If we succeed, write out any needed conversions. If we fail,
2769 it is hard to guess why we failed, so don't do anything
2770 special; just let the optimization be suppressed. */
2771
2772 if (apply_change_group () && seq)
2773 emit_insns_before (seq, insn);
2774 }
2775 }
2776}
2777\f
2778/* These routines are responsible for converting virtual register references
2779 to the actual hard register references once RTL generation is complete.
2780
2781 The following four variables are used for communication between the
2782 routines. They contain the offsets of the virtual registers from their
2783 respective hard registers. */
2784
2785static int in_arg_offset;
2786static int var_offset;
2787static int dynamic_offset;
2788static int out_arg_offset;
71038426 2789static int cfa_offset;
6f086dfc
RS
2790
2791/* In most machines, the stack pointer register is equivalent to the bottom
2792 of the stack. */
2793
2794#ifndef STACK_POINTER_OFFSET
2795#define STACK_POINTER_OFFSET 0
2796#endif
2797
2798/* If not defined, pick an appropriate default for the offset of dynamically
2799 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2800 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
2801
2802#ifndef STACK_DYNAMIC_OFFSET
2803
6f086dfc
RS
2804/* The bottom of the stack points to the actual arguments. If
2805 REG_PARM_STACK_SPACE is defined, this includes the space for the register
2806 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
718fe406 2807 stack space for register parameters is not pushed by the caller, but
6f086dfc
RS
2808 rather part of the fixed stack areas and hence not included in
2809 `current_function_outgoing_args_size'. Nevertheless, we must allow
2810 for it when allocating stack dynamic objects. */
2811
2812#if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2813#define STACK_DYNAMIC_OFFSET(FNDECL) \
f73ad30e
JH
2814((ACCUMULATE_OUTGOING_ARGS \
2815 ? (current_function_outgoing_args_size + REG_PARM_STACK_SPACE (FNDECL)) : 0)\
2816 + (STACK_POINTER_OFFSET)) \
6f086dfc
RS
2817
2818#else
2819#define STACK_DYNAMIC_OFFSET(FNDECL) \
f73ad30e
JH
2820((ACCUMULATE_OUTGOING_ARGS ? current_function_outgoing_args_size : 0) \
2821 + (STACK_POINTER_OFFSET))
6f086dfc
RS
2822#endif
2823#endif
2824
2c849145 2825/* On most machines, the CFA coincides with the first incoming parm. */
71038426
RH
2826
2827#ifndef ARG_POINTER_CFA_OFFSET
2c849145 2828#define ARG_POINTER_CFA_OFFSET(FNDECL) FIRST_PARM_OFFSET (FNDECL)
71038426
RH
2829#endif
2830
e9a25f70
JL
2831/* Build up a (MEM (ADDRESSOF (REG))) rtx for a register REG that just had
2832 its address taken. DECL is the decl for the object stored in the
2833 register, for later use if we do need to force REG into the stack.
2834 REG is overwritten by the MEM like in put_reg_into_stack. */
2835
2836rtx
2837gen_mem_addressof (reg, decl)
2838 rtx reg;
2839 tree decl;
2840{
8f985ec4
ZW
2841 rtx r = gen_rtx_ADDRESSOF (Pmode, gen_reg_rtx (GET_MODE (reg)),
2842 REGNO (reg), decl);
14a774a9 2843
95ca22f4 2844 /* If the original REG was a user-variable, then so is the REG whose
14a774a9 2845 address is being taken. Likewise for unchanging. */
95ca22f4 2846 REG_USERVAR_P (XEXP (r, 0)) = REG_USERVAR_P (reg);
14a774a9 2847 RTX_UNCHANGING_P (XEXP (r, 0)) = RTX_UNCHANGING_P (reg);
e9a25f70 2848
e9a25f70 2849 PUT_CODE (reg, MEM);
ef178af3 2850 XEXP (reg, 0) = r;
8b4944fb
RH
2851 if (decl)
2852 {
2853 tree type = TREE_TYPE (decl);
2854
2855 PUT_MODE (reg, DECL_MODE (decl));
2856 MEM_VOLATILE_P (reg) = TREE_SIDE_EFFECTS (decl);
2857 MEM_SET_IN_STRUCT_P (reg, AGGREGATE_TYPE_P (type));
2858 MEM_ALIAS_SET (reg) = get_alias_set (decl);
e9a25f70 2859
8b4944fb
RH
2860 if (TREE_USED (decl) || DECL_INITIAL (decl) != 0)
2861 fixup_var_refs (reg, GET_MODE (reg), TREE_UNSIGNED (type), 0);
2862 }
2863 else
caa511fd
JW
2864 {
2865 /* We have no alias information about this newly created MEM. */
2866 MEM_ALIAS_SET (reg) = 0;
2867
2868 fixup_var_refs (reg, GET_MODE (reg), 0, 0);
2869 }
e5e809f4 2870
e9a25f70
JL
2871 return reg;
2872}
2873
2874/* If DECL has an RTL that is an ADDRESSOF rtx, put it into the stack. */
2875
2876void
2877flush_addressof (decl)
2878 tree decl;
2879{
2880 if ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL)
2881 && DECL_RTL (decl) != 0
2882 && GET_CODE (DECL_RTL (decl)) == MEM
2883 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF
2884 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == REG)
fe9b4957 2885 put_addressof_into_stack (XEXP (DECL_RTL (decl), 0), 0);
e9a25f70
JL
2886}
2887
2888/* Force the register pointed to by R, an ADDRESSOF rtx, into the stack. */
2889
2890static void
fe9b4957 2891put_addressof_into_stack (r, ht)
e9a25f70 2892 rtx r;
fe9b4957 2893 struct hash_table *ht;
e9a25f70 2894{
8b4944fb
RH
2895 tree decl, type;
2896 int volatile_p, used_p;
2897
e9a25f70
JL
2898 rtx reg = XEXP (r, 0);
2899
2900 if (GET_CODE (reg) != REG)
2901 abort ();
2902
8b4944fb
RH
2903 decl = ADDRESSOF_DECL (r);
2904 if (decl)
2905 {
2906 type = TREE_TYPE (decl);
2907 volatile_p = (TREE_CODE (decl) != SAVE_EXPR
2908 && TREE_THIS_VOLATILE (decl));
2909 used_p = (TREE_USED (decl)
2910 || (TREE_CODE (decl) != SAVE_EXPR
2911 && DECL_INITIAL (decl) != 0));
2912 }
2913 else
2914 {
2915 type = NULL_TREE;
2916 volatile_p = 0;
2917 used_p = 1;
2918 }
2919
2920 put_reg_into_stack (0, reg, type, GET_MODE (reg), GET_MODE (reg),
2921 volatile_p, ADDRESSOF_REGNO (r), used_p, ht);
e9a25f70
JL
2922}
2923
b5bd3b3c
AS
2924/* List of replacements made below in purge_addressof_1 when creating
2925 bitfield insertions. */
8b04083b
VM
2926static rtx purge_bitfield_addressof_replacements;
2927
2928/* List of replacements made below in purge_addressof_1 for patterns
2929 (MEM (ADDRESSOF (REG ...))). The key of the list entry is the
2930 corresponding (ADDRESSOF (REG ...)) and value is a substitution for
2931 the all pattern. List PURGE_BITFIELD_ADDRESSOF_REPLACEMENTS is not
2932 enough in complex cases, e.g. when some field values can be
718fe406 2933 extracted by usage MEM with narrower mode. */
b5bd3b3c
AS
2934static rtx purge_addressof_replacements;
2935
e9a25f70
JL
2936/* Helper function for purge_addressof. See if the rtx expression at *LOC
2937 in INSN needs to be changed. If FORCE, always put any ADDRESSOFs into
8c36698e
NC
2938 the stack. If the function returns FALSE then the replacement could not
2939 be made. */
e9a25f70 2940
8c36698e 2941static boolean
fe9b4957 2942purge_addressof_1 (loc, insn, force, store, ht)
e9a25f70
JL
2943 rtx *loc;
2944 rtx insn;
f7b6d104 2945 int force, store;
fe9b4957 2946 struct hash_table *ht;
e9a25f70
JL
2947{
2948 rtx x;
2949 RTX_CODE code;
2950 int i, j;
6f7d635c 2951 const char *fmt;
8c36698e 2952 boolean result = true;
e9a25f70
JL
2953
2954 /* Re-start here to avoid recursion in common cases. */
2955 restart:
2956
2957 x = *loc;
2958 if (x == 0)
8c36698e 2959 return true;
e9a25f70
JL
2960
2961 code = GET_CODE (x);
2962
c5c76735
JL
2963 /* If we don't return in any of the cases below, we will recurse inside
2964 the RTX, which will normally result in any ADDRESSOF being forced into
2965 memory. */
2966 if (code == SET)
2967 {
8c36698e
NC
2968 result = purge_addressof_1 (&SET_DEST (x), insn, force, 1, ht);
2969 result &= purge_addressof_1 (&SET_SRC (x), insn, force, 0, ht);
2970 return result;
c5c76735
JL
2971 }
2972
2973 else if (code == ADDRESSOF && GET_CODE (XEXP (x, 0)) == MEM)
e9a25f70 2974 {
956d6950
JL
2975 /* We must create a copy of the rtx because it was created by
2976 overwriting a REG rtx which is always shared. */
2977 rtx sub = copy_rtx (XEXP (XEXP (x, 0), 0));
c5c76735 2978 rtx insns;
e9a25f70 2979
ab87f8c8
JL
2980 if (validate_change (insn, loc, sub, 0)
2981 || validate_replace_rtx (x, sub, insn))
8c36698e 2982 return true;
718fe406 2983
e9a25f70 2984 start_sequence ();
ab87f8c8
JL
2985 sub = force_operand (sub, NULL_RTX);
2986 if (! validate_change (insn, loc, sub, 0)
2987 && ! validate_replace_rtx (x, sub, insn))
e9a25f70
JL
2988 abort ();
2989
f7b6d104 2990 insns = gen_sequence ();
e9a25f70 2991 end_sequence ();
18e765cb 2992 emit_insn_before (insns, insn);
8c36698e 2993 return true;
e9a25f70 2994 }
c5c76735 2995
e9a25f70
JL
2996 else if (code == MEM && GET_CODE (XEXP (x, 0)) == ADDRESSOF && ! force)
2997 {
2998 rtx sub = XEXP (XEXP (x, 0), 0);
ab87f8c8 2999 rtx sub2;
e5e809f4 3000
6d8ccdbb 3001 if (GET_CODE (sub) == MEM)
ab87f8c8
JL
3002 {
3003 sub2 = gen_rtx_MEM (GET_MODE (x), copy_rtx (XEXP (sub, 0)));
3004 MEM_COPY_ATTRIBUTES (sub2, sub);
ab87f8c8
JL
3005 sub = sub2;
3006 }
c5c76735
JL
3007 else if (GET_CODE (sub) == REG
3008 && (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode))
3009 ;
e5e809f4 3010 else if (GET_CODE (sub) == REG && GET_MODE (x) != GET_MODE (sub))
e9a25f70 3011 {
f7b6d104
RH
3012 int size_x, size_sub;
3013
b5bd3b3c
AS
3014 if (!insn)
3015 {
3016 /* When processing REG_NOTES look at the list of
3017 replacements done on the insn to find the register that X
3018 was replaced by. */
3019 rtx tem;
3020
8b04083b
VM
3021 for (tem = purge_bitfield_addressof_replacements;
3022 tem != NULL_RTX;
b5bd3b3c 3023 tem = XEXP (XEXP (tem, 1), 1))
8b04083b
VM
3024 if (rtx_equal_p (x, XEXP (tem, 0)))
3025 {
3026 *loc = XEXP (XEXP (tem, 1), 0);
8c36698e 3027 return true;
8b04083b 3028 }
fbdfe39c 3029
718fe406 3030 /* See comment for purge_addressof_replacements. */
8b04083b
VM
3031 for (tem = purge_addressof_replacements;
3032 tem != NULL_RTX;
3033 tem = XEXP (XEXP (tem, 1), 1))
3034 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3035 {
3036 rtx z = XEXP (XEXP (tem, 1), 0);
fbdfe39c 3037
8b04083b
VM
3038 if (GET_MODE (x) == GET_MODE (z)
3039 || (GET_CODE (XEXP (XEXP (tem, 1), 0)) != REG
3040 && GET_CODE (XEXP (XEXP (tem, 1), 0)) != SUBREG))
3041 abort ();
3042
3043 /* It can happen that the note may speak of things
3044 in a wider (or just different) mode than the
3045 code did. This is especially true of
718fe406 3046 REG_RETVAL. */
8b04083b
VM
3047
3048 if (GET_CODE (z) == SUBREG && SUBREG_WORD (z) == 0)
3049 z = SUBREG_REG (z);
718fe406 3050
8b04083b
VM
3051 if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3052 && (GET_MODE_SIZE (GET_MODE (x))
3053 > GET_MODE_SIZE (GET_MODE (z))))
3054 {
3055 /* This can occur as a result in invalid
718fe406 3056 pointer casts, e.g. float f; ...
8b04083b
VM
3057 *(long long int *)&f.
3058 ??? We could emit a warning here, but
3059 without a line number that wouldn't be
3060 very helpful. */
3061 z = gen_rtx_SUBREG (GET_MODE (x), z, 0);
3062 }
3063 else
3064 z = gen_lowpart (GET_MODE (x), z);
3065
3066 *loc = z;
aa608fe6 3067 return true;
8b04083b 3068 }
b5bd3b3c 3069
8c36698e
NC
3070 /* Sometimes we may not be able to find the replacement. For
3071 example when the original insn was a MEM in a wider mode,
3072 and the note is part of a sign extension of a narrowed
3073 version of that MEM. Gcc testcase compile/990829-1.c can
3074 generate an example of this siutation. Rather than complain
3075 we return false, which will prompt our caller to remove the
3076 offending note. */
3077 return false;
b5bd3b3c
AS
3078 }
3079
f7b6d104
RH
3080 size_x = GET_MODE_BITSIZE (GET_MODE (x));
3081 size_sub = GET_MODE_BITSIZE (GET_MODE (sub));
3082
3083 /* Don't even consider working with paradoxical subregs,
3084 or the moral equivalent seen here. */
470032d7 3085 if (size_x <= size_sub
d006aa54 3086 && int_mode_for_mode (GET_MODE (sub)) != BLKmode)
e9a25f70 3087 {
f7b6d104
RH
3088 /* Do a bitfield insertion to mirror what would happen
3089 in memory. */
3090
f7b6d104
RH
3091 rtx val, seq;
3092
f7b6d104
RH
3093 if (store)
3094 {
fe9b4957 3095 rtx p = PREV_INSN (insn);
de0dd934 3096
f7b6d104
RH
3097 start_sequence ();
3098 val = gen_reg_rtx (GET_MODE (x));
3099 if (! validate_change (insn, loc, val, 0))
b5bd3b3c
AS
3100 {
3101 /* Discard the current sequence and put the
3102 ADDRESSOF on stack. */
3103 end_sequence ();
3104 goto give_up;
3105 }
f7b6d104
RH
3106 seq = gen_sequence ();
3107 end_sequence ();
3108 emit_insn_before (seq, insn);
718fe406 3109 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
fe9b4957 3110 insn, ht);
718fe406 3111
f7b6d104 3112 start_sequence ();
47401c4d 3113 store_bit_field (sub, size_x, 0, GET_MODE (x),
f7b6d104 3114 val, GET_MODE_SIZE (GET_MODE (sub)),
19caa751 3115 GET_MODE_ALIGNMENT (GET_MODE (sub)));
f7b6d104 3116
de0dd934
R
3117 /* Make sure to unshare any shared rtl that store_bit_field
3118 might have created. */
d1b81779 3119 unshare_all_rtl_again (get_insns ());
de0dd934 3120
f7b6d104
RH
3121 seq = gen_sequence ();
3122 end_sequence ();
fe9b4957
MM
3123 p = emit_insn_after (seq, insn);
3124 if (NEXT_INSN (insn))
718fe406 3125 compute_insns_for_mem (NEXT_INSN (insn),
fe9b4957
MM
3126 p ? NEXT_INSN (p) : NULL_RTX,
3127 ht);
f7b6d104
RH
3128 }
3129 else
3130 {
fe9b4957
MM
3131 rtx p = PREV_INSN (insn);
3132
f7b6d104 3133 start_sequence ();
47401c4d 3134 val = extract_bit_field (sub, size_x, 0, 1, NULL_RTX,
f7b6d104
RH
3135 GET_MODE (x), GET_MODE (x),
3136 GET_MODE_SIZE (GET_MODE (sub)),
3137 GET_MODE_SIZE (GET_MODE (sub)));
3138
f7b6d104 3139 if (! validate_change (insn, loc, val, 0))
b5bd3b3c
AS
3140 {
3141 /* Discard the current sequence and put the
3142 ADDRESSOF on stack. */
3143 end_sequence ();
3144 goto give_up;
3145 }
f7b6d104
RH
3146
3147 seq = gen_sequence ();
3148 end_sequence ();
3149 emit_insn_before (seq, insn);
fe9b4957
MM
3150 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3151 insn, ht);
f7b6d104
RH
3152 }
3153
b5bd3b3c
AS
3154 /* Remember the replacement so that the same one can be done
3155 on the REG_NOTES. */
8b04083b 3156 purge_bitfield_addressof_replacements
b5bd3b3c 3157 = gen_rtx_EXPR_LIST (VOIDmode, x,
8b04083b
VM
3158 gen_rtx_EXPR_LIST
3159 (VOIDmode, val,
3160 purge_bitfield_addressof_replacements));
b5bd3b3c 3161
f7b6d104 3162 /* We replaced with a reg -- all done. */
8c36698e 3163 return true;
e9a25f70
JL
3164 }
3165 }
c5c76735 3166
e9a25f70 3167 else if (validate_change (insn, loc, sub, 0))
fbdfe39c
RH
3168 {
3169 /* Remember the replacement so that the same one can be done
3170 on the REG_NOTES. */
8b04083b
VM
3171 if (GET_CODE (sub) == REG || GET_CODE (sub) == SUBREG)
3172 {
3173 rtx tem;
3174
3175 for (tem = purge_addressof_replacements;
3176 tem != NULL_RTX;
3177 tem = XEXP (XEXP (tem, 1), 1))
3178 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3179 {
3180 XEXP (XEXP (tem, 1), 0) = sub;
8c36698e 3181 return true;
8b04083b
VM
3182 }
3183 purge_addressof_replacements
3184 = gen_rtx (EXPR_LIST, VOIDmode, XEXP (x, 0),
3185 gen_rtx_EXPR_LIST (VOIDmode, sub,
3186 purge_addressof_replacements));
8c36698e 3187 return true;
8b04083b 3188 }
fbdfe39c
RH
3189 goto restart;
3190 }
b5bd3b3c 3191 give_up:;
e9a25f70
JL
3192 /* else give up and put it into the stack */
3193 }
c5c76735 3194
e9a25f70
JL
3195 else if (code == ADDRESSOF)
3196 {
fe9b4957 3197 put_addressof_into_stack (x, ht);
aa608fe6 3198 return true;
e9a25f70 3199 }
f7b6d104
RH
3200 else if (code == SET)
3201 {
8c36698e
NC
3202 result = purge_addressof_1 (&SET_DEST (x), insn, force, 1, ht);
3203 result &= purge_addressof_1 (&SET_SRC (x), insn, force, 0, ht);
3204 return result;
f7b6d104 3205 }
e9a25f70 3206
718fe406 3207 /* Scan all subexpressions. */
e9a25f70
JL
3208 fmt = GET_RTX_FORMAT (code);
3209 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3210 {
3211 if (*fmt == 'e')
8c36698e 3212 result &= purge_addressof_1 (&XEXP (x, i), insn, force, 0, ht);
e9a25f70
JL
3213 else if (*fmt == 'E')
3214 for (j = 0; j < XVECLEN (x, i); j++)
8c36698e 3215 result &= purge_addressof_1 (&XVECEXP (x, i, j), insn, force, 0, ht);
fe9b4957 3216 }
8c36698e
NC
3217
3218 return result;
fe9b4957
MM
3219}
3220
3221/* Return a new hash table entry in HT. */
3222
3223static struct hash_entry *
3224insns_for_mem_newfunc (he, ht, k)
3225 struct hash_entry *he;
3226 struct hash_table *ht;
3227 hash_table_key k ATTRIBUTE_UNUSED;
3228{
3229 struct insns_for_mem_entry *ifmhe;
3230 if (he)
3231 return he;
3232
3233 ifmhe = ((struct insns_for_mem_entry *)
3234 hash_allocate (ht, sizeof (struct insns_for_mem_entry)));
3235 ifmhe->insns = NULL_RTX;
3236
3237 return &ifmhe->he;
3238}
3239
3240/* Return a hash value for K, a REG. */
3241
3242static unsigned long
3243insns_for_mem_hash (k)
3244 hash_table_key k;
3245{
3246 /* K is really a RTX. Just use the address as the hash value. */
3247 return (unsigned long) k;
3248}
3249
3250/* Return non-zero if K1 and K2 (two REGs) are the same. */
3251
3252static boolean
3253insns_for_mem_comp (k1, k2)
3254 hash_table_key k1;
3255 hash_table_key k2;
3256{
3257 return k1 == k2;
3258}
3259
3260struct insns_for_mem_walk_info {
3261 /* The hash table that we are using to record which INSNs use which
3262 MEMs. */
3263 struct hash_table *ht;
3264
3265 /* The INSN we are currently proessing. */
3266 rtx insn;
3267
3268 /* Zero if we are walking to find ADDRESSOFs, one if we are walking
3269 to find the insns that use the REGs in the ADDRESSOFs. */
3270 int pass;
3271};
3272
3273/* Called from compute_insns_for_mem via for_each_rtx. If R is a REG
3274 that might be used in an ADDRESSOF expression, record this INSN in
3275 the hash table given by DATA (which is really a pointer to an
3276 insns_for_mem_walk_info structure). */
3277
3278static int
3279insns_for_mem_walk (r, data)
3280 rtx *r;
3281 void *data;
3282{
718fe406 3283 struct insns_for_mem_walk_info *ifmwi
fe9b4957
MM
3284 = (struct insns_for_mem_walk_info *) data;
3285
3286 if (ifmwi->pass == 0 && *r && GET_CODE (*r) == ADDRESSOF
3287 && GET_CODE (XEXP (*r, 0)) == REG)
3288 hash_lookup (ifmwi->ht, XEXP (*r, 0), /*create=*/1, /*copy=*/0);
3289 else if (ifmwi->pass == 1 && *r && GET_CODE (*r) == REG)
3290 {
3291 /* Lookup this MEM in the hashtable, creating it if necessary. */
718fe406 3292 struct insns_for_mem_entry *ifme
fe9b4957
MM
3293 = (struct insns_for_mem_entry *) hash_lookup (ifmwi->ht,
3294 *r,
3295 /*create=*/0,
3296 /*copy=*/0);
3297
3298 /* If we have not already recorded this INSN, do so now. Since
3299 we process the INSNs in order, we know that if we have
3300 recorded it it must be at the front of the list. */
3301 if (ifme && (!ifme->insns || XEXP (ifme->insns, 0) != ifmwi->insn))
1f8f4a0b
MM
3302 ifme->insns = gen_rtx_EXPR_LIST (VOIDmode, ifmwi->insn,
3303 ifme->insns);
e9a25f70 3304 }
fe9b4957
MM
3305
3306 return 0;
3307}
3308
3309/* Walk the INSNS, until we reach LAST_INSN, recording which INSNs use
3310 which REGs in HT. */
3311
3312static void
3313compute_insns_for_mem (insns, last_insn, ht)
3314 rtx insns;
3315 rtx last_insn;
3316 struct hash_table *ht;
3317{
3318 rtx insn;
3319 struct insns_for_mem_walk_info ifmwi;
3320 ifmwi.ht = ht;
3321
3322 for (ifmwi.pass = 0; ifmwi.pass < 2; ++ifmwi.pass)
3323 for (insn = insns; insn != last_insn; insn = NEXT_INSN (insn))
2c3c49de 3324 if (INSN_P (insn))
fe9b4957
MM
3325 {
3326 ifmwi.insn = insn;
3327 for_each_rtx (&insn, insns_for_mem_walk, &ifmwi);
3328 }
e9a25f70
JL
3329}
3330
8c36698e
NC
3331/* Helper function for purge_addressof called through for_each_rtx.
3332 Returns true iff the rtl is an ADDRESSOF. */
3333static int
3334is_addressof (rtl, data)
718fe406
KH
3335 rtx *rtl;
3336 void *data ATTRIBUTE_UNUSED;
8c36698e 3337{
718fe406 3338 return GET_CODE (*rtl) == ADDRESSOF;
8c36698e
NC
3339}
3340
e9a25f70
JL
3341/* Eliminate all occurrences of ADDRESSOF from INSNS. Elide any remaining
3342 (MEM (ADDRESSOF)) patterns, and force any needed registers into the
3343 stack. */
3344
3345void
3346purge_addressof (insns)
3347 rtx insns;
3348{
3349 rtx insn;
fe9b4957 3350 struct hash_table ht;
718fe406 3351
fe9b4957
MM
3352 /* When we actually purge ADDRESSOFs, we turn REGs into MEMs. That
3353 requires a fixup pass over the instruction stream to correct
3354 INSNs that depended on the REG being a REG, and not a MEM. But,
bedda2da 3355 these fixup passes are slow. Furthermore, most MEMs are not
fe9b4957
MM
3356 mentioned in very many instructions. So, we speed up the process
3357 by pre-calculating which REGs occur in which INSNs; that allows
3358 us to perform the fixup passes much more quickly. */
718fe406 3359 hash_table_init (&ht,
fe9b4957
MM
3360 insns_for_mem_newfunc,
3361 insns_for_mem_hash,
3362 insns_for_mem_comp);
3363 compute_insns_for_mem (insns, NULL_RTX, &ht);
3364
e9a25f70
JL
3365 for (insn = insns; insn; insn = NEXT_INSN (insn))
3366 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3367 || GET_CODE (insn) == CALL_INSN)
3368 {
8c36698e
NC
3369 if (! purge_addressof_1 (&PATTERN (insn), insn,
3370 asm_noperands (PATTERN (insn)) > 0, 0, &ht))
3371 /* If we could not replace the ADDRESSOFs in the insn,
3372 something is wrong. */
3373 abort ();
718fe406 3374
8c36698e
NC
3375 if (! purge_addressof_1 (&REG_NOTES (insn), NULL_RTX, 0, 0, &ht))
3376 {
3377 /* If we could not replace the ADDRESSOFs in the insn's notes,
3378 we can just remove the offending notes instead. */
3379 rtx note;
3380
3381 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3382 {
3383 /* If we find a REG_RETVAL note then the insn is a libcall.
3384 Such insns must have REG_EQUAL notes as well, in order
3385 for later passes of the compiler to work. So it is not
3386 safe to delete the notes here, and instead we abort. */
3387 if (REG_NOTE_KIND (note) == REG_RETVAL)
3388 abort ();
718fe406 3389 if (for_each_rtx (&note, is_addressof, NULL))
8c36698e
NC
3390 remove_note (insn, note);
3391 }
3392 }
e9a25f70 3393 }
fe9b4957
MM
3394
3395 /* Clean up. */
3396 hash_table_free (&ht);
8b04083b 3397 purge_bitfield_addressof_replacements = 0;
da9b1f9c 3398 purge_addressof_replacements = 0;
4fa48eae
JL
3399
3400 /* REGs are shared. purge_addressof will destructively replace a REG
3401 with a MEM, which creates shared MEMs.
3402
3403 Unfortunately, the children of put_reg_into_stack assume that MEMs
3404 referring to the same stack slot are shared (fixup_var_refs and
3405 the associated hash table code).
3406
3407 So, we have to do another unsharing pass after we have flushed any
3408 REGs that had their address taken into the stack.
3409
3410 It may be worth tracking whether or not we converted any REGs into
3411 MEMs to avoid this overhead when it is not needed. */
3412 unshare_all_rtl_again (get_insns ());
e9a25f70
JL
3413}
3414\f
659e47fb
AH
3415/* Convert a SET of a hard subreg to a set of the appropriet hard
3416 register. A subroutine of purge_hard_subreg_sets. */
3417
3418static void
3419purge_single_hard_subreg_set (pattern)
3420 rtx pattern;
3421{
3422 rtx reg = SET_DEST (pattern);
3423 enum machine_mode mode = GET_MODE (SET_DEST (pattern));
3424 int word = 0;
3425
3426 while (GET_CODE (reg) == SUBREG)
3427 {
3428 word += SUBREG_WORD (reg);
3429 reg = SUBREG_REG (reg);
3430 }
3431
3432 if (REGNO (reg) < FIRST_PSEUDO_REGISTER)
3433 {
3434 reg = gen_rtx_REG (mode, REGNO (reg) + word);
3435 SET_DEST (pattern) = reg;
3436 }
3437}
3438
3439/* Eliminate all occurrences of SETs of hard subregs from INSNS. The
3440 only such SETs that we expect to see are those left in because
3441 integrate can't handle sets of parts of a return value register.
3442
3443 We don't use alter_subreg because we only want to eliminate subregs
3444 of hard registers. */
3445
3446void
3447purge_hard_subreg_sets (insn)
3448 rtx insn;
3449{
3450 for (; insn; insn = NEXT_INSN (insn))
3451 {
3452 if (INSN_P (insn))
3453 {
3454 rtx pattern = PATTERN (insn);
3455 switch (GET_CODE (pattern))
3456 {
3457 case SET:
3458 if (GET_CODE (SET_DEST (pattern)) == SUBREG)
3459 purge_single_hard_subreg_set (pattern);
3460 break;
3461 case PARALLEL:
3462 {
3463 int j;
3464 for (j = XVECLEN (pattern, 0) - 1; j >= 0; j--)
3465 {
3466 rtx inner_pattern = XVECEXP (pattern, 0, j);
3467 if (GET_CODE (inner_pattern) == SET
3468 && GET_CODE (SET_DEST (inner_pattern)) == SUBREG)
3469 purge_single_hard_subreg_set (inner_pattern);
3470 }
3471 }
3472 break;
3473 default:
3474 break;
3475 }
3476 }
3477 }
3478}
3479\f
6f086dfc
RS
3480/* Pass through the INSNS of function FNDECL and convert virtual register
3481 references to hard register references. */
3482
3483void
3484instantiate_virtual_regs (fndecl, insns)
3485 tree fndecl;
3486 rtx insns;
3487{
3488 rtx insn;
770ae6cc 3489 unsigned int i;
6f086dfc
RS
3490
3491 /* Compute the offsets to use for this function. */
3492 in_arg_offset = FIRST_PARM_OFFSET (fndecl);
3493 var_offset = STARTING_FRAME_OFFSET;
3494 dynamic_offset = STACK_DYNAMIC_OFFSET (fndecl);
3495 out_arg_offset = STACK_POINTER_OFFSET;
2c849145 3496 cfa_offset = ARG_POINTER_CFA_OFFSET (fndecl);
6f086dfc
RS
3497
3498 /* Scan all variables and parameters of this function. For each that is
3499 in memory, instantiate all virtual registers if the result is a valid
3500 address. If not, we do it later. That will handle most uses of virtual
3501 regs on many machines. */
3502 instantiate_decls (fndecl, 1);
3503
3504 /* Initialize recognition, indicating that volatile is OK. */
3505 init_recog ();
3506
3507 /* Scan through all the insns, instantiating every virtual register still
3508 present. */
3509 for (insn = insns; insn; insn = NEXT_INSN (insn))
3510 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3511 || GET_CODE (insn) == CALL_INSN)
3512 {
3513 instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
5f4f0e22 3514 instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
6f086dfc
RS
3515 }
3516
e9a25f70
JL
3517 /* Instantiate the stack slots for the parm registers, for later use in
3518 addressof elimination. */
3519 for (i = 0; i < max_parm_reg; ++i)
3520 if (parm_reg_stack_loc[i])
3521 instantiate_virtual_regs_1 (&parm_reg_stack_loc[i], NULL_RTX, 0);
3522
6f086dfc
RS
3523 /* Now instantiate the remaining register equivalences for debugging info.
3524 These will not be valid addresses. */
3525 instantiate_decls (fndecl, 0);
3526
3527 /* Indicate that, from now on, assign_stack_local should use
3528 frame_pointer_rtx. */
3529 virtuals_instantiated = 1;
3530}
3531
3532/* Scan all decls in FNDECL (both variables and parameters) and instantiate
3533 all virtual registers in their DECL_RTL's.
3534
3535 If VALID_ONLY, do this only if the resulting address is still valid.
3536 Otherwise, always do it. */
3537
3538static void
3539instantiate_decls (fndecl, valid_only)
3540 tree fndecl;
3541 int valid_only;
3542{
3543 tree decl;
3544
6f086dfc
RS
3545 /* Process all parameters of the function. */
3546 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
3547 {
e5e809f4
JL
3548 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
3549
718fe406 3550 instantiate_decl (DECL_RTL (decl), size, valid_only);
ce717ce4
JW
3551
3552 /* If the parameter was promoted, then the incoming RTL mode may be
3553 larger than the declared type size. We must use the larger of
3554 the two sizes. */
3555 size = MAX (GET_MODE_SIZE (GET_MODE (DECL_INCOMING_RTL (decl))), size);
3556 instantiate_decl (DECL_INCOMING_RTL (decl), size, valid_only);
6f086dfc
RS
3557 }
3558
0f41302f 3559 /* Now process all variables defined in the function or its subblocks. */
6f086dfc 3560 instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
6f086dfc
RS
3561}
3562
3563/* Subroutine of instantiate_decls: Process all decls in the given
3564 BLOCK node and all its subblocks. */
3565
3566static void
3567instantiate_decls_1 (let, valid_only)
3568 tree let;
3569 int valid_only;
3570{
3571 tree t;
3572
3573 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
5a73491b
RK
3574 instantiate_decl (DECL_RTL (t), int_size_in_bytes (TREE_TYPE (t)),
3575 valid_only);
6f086dfc
RS
3576
3577 /* Process all subblocks. */
3578 for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
3579 instantiate_decls_1 (t, valid_only);
3580}
5a73491b 3581
8008b228 3582/* Subroutine of the preceding procedures: Given RTL representing a
5a73491b
RK
3583 decl and the size of the object, do any instantiation required.
3584
3585 If VALID_ONLY is non-zero, it means that the RTL should only be
3586 changed if the new address is valid. */
3587
3588static void
3589instantiate_decl (x, size, valid_only)
3590 rtx x;
770ae6cc 3591 HOST_WIDE_INT size;
5a73491b
RK
3592 int valid_only;
3593{
3594 enum machine_mode mode;
3595 rtx addr;
3596
3597 /* If this is not a MEM, no need to do anything. Similarly if the
3598 address is a constant or a register that is not a virtual register. */
3599
3600 if (x == 0 || GET_CODE (x) != MEM)
3601 return;
3602
3603 addr = XEXP (x, 0);
3604 if (CONSTANT_P (addr)
956d6950 3605 || (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == REG)
5a73491b
RK
3606 || (GET_CODE (addr) == REG
3607 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
3608 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
3609 return;
3610
3611 /* If we should only do this if the address is valid, copy the address.
3612 We need to do this so we can undo any changes that might make the
3613 address invalid. This copy is unfortunate, but probably can't be
3614 avoided. */
3615
3616 if (valid_only)
3617 addr = copy_rtx (addr);
3618
3619 instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
3620
770ae6cc 3621 if (valid_only && size >= 0)
87ce34d6 3622 {
770ae6cc
RK
3623 unsigned HOST_WIDE_INT decl_size = size;
3624
87ce34d6
JW
3625 /* Now verify that the resulting address is valid for every integer or
3626 floating-point mode up to and including SIZE bytes long. We do this
3627 since the object might be accessed in any mode and frame addresses
3628 are shared. */
3629
3630 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
770ae6cc 3631 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
87ce34d6
JW
3632 mode = GET_MODE_WIDER_MODE (mode))
3633 if (! memory_address_p (mode, addr))
3634 return;
5a73491b 3635
87ce34d6 3636 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
770ae6cc 3637 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
87ce34d6
JW
3638 mode = GET_MODE_WIDER_MODE (mode))
3639 if (! memory_address_p (mode, addr))
3640 return;
3641 }
5a73491b 3642
87ce34d6
JW
3643 /* Put back the address now that we have updated it and we either know
3644 it is valid or we don't care whether it is valid. */
5a73491b
RK
3645
3646 XEXP (x, 0) = addr;
3647}
6f086dfc
RS
3648\f
3649/* Given a pointer to a piece of rtx and an optional pointer to the
3650 containing object, instantiate any virtual registers present in it.
3651
3652 If EXTRA_INSNS, we always do the replacement and generate
3653 any extra insns before OBJECT. If it zero, we do nothing if replacement
3654 is not valid.
3655
3656 Return 1 if we either had nothing to do or if we were able to do the
718fe406 3657 needed replacement. Return 0 otherwise; we only return zero if
6f086dfc
RS
3658 EXTRA_INSNS is zero.
3659
3660 We first try some simple transformations to avoid the creation of extra
3661 pseudos. */
3662
3663static int
3664instantiate_virtual_regs_1 (loc, object, extra_insns)
3665 rtx *loc;
3666 rtx object;
3667 int extra_insns;
3668{
3669 rtx x;
3670 RTX_CODE code;
3671 rtx new = 0;
07444f1d 3672 HOST_WIDE_INT offset = 0;
6f086dfc
RS
3673 rtx temp;
3674 rtx seq;
3675 int i, j;
6f7d635c 3676 const char *fmt;
6f086dfc
RS
3677
3678 /* Re-start here to avoid recursion in common cases. */
3679 restart:
3680
3681 x = *loc;
3682 if (x == 0)
3683 return 1;
3684
3685 code = GET_CODE (x);
3686
3687 /* Check for some special cases. */
3688 switch (code)
3689 {
3690 case CONST_INT:
3691 case CONST_DOUBLE:
3692 case CONST:
3693 case SYMBOL_REF:
3694 case CODE_LABEL:
3695 case PC:
3696 case CC0:
3697 case ASM_INPUT:
3698 case ADDR_VEC:
3699 case ADDR_DIFF_VEC:
3700 case RETURN:
3701 return 1;
3702
3703 case SET:
3704 /* We are allowed to set the virtual registers. This means that
38e01259 3705 the actual register should receive the source minus the
6f086dfc
RS
3706 appropriate offset. This is used, for example, in the handling
3707 of non-local gotos. */
3708 if (SET_DEST (x) == virtual_incoming_args_rtx)
718fe406 3709 new = arg_pointer_rtx, offset = -in_arg_offset;
6f086dfc 3710 else if (SET_DEST (x) == virtual_stack_vars_rtx)
718fe406 3711 new = frame_pointer_rtx, offset = -var_offset;
6f086dfc 3712 else if (SET_DEST (x) == virtual_stack_dynamic_rtx)
718fe406 3713 new = stack_pointer_rtx, offset = -dynamic_offset;
6f086dfc 3714 else if (SET_DEST (x) == virtual_outgoing_args_rtx)
718fe406 3715 new = stack_pointer_rtx, offset = -out_arg_offset;
71038426 3716 else if (SET_DEST (x) == virtual_cfa_rtx)
718fe406 3717 new = arg_pointer_rtx, offset = -cfa_offset;
6f086dfc
RS
3718
3719 if (new)
3720 {
14a774a9
RK
3721 rtx src = SET_SRC (x);
3722
3723 instantiate_virtual_regs_1 (&src, NULL_RTX, 0);
3724
6f086dfc
RS
3725 /* The only valid sources here are PLUS or REG. Just do
3726 the simplest possible thing to handle them. */
14a774a9 3727 if (GET_CODE (src) != REG && GET_CODE (src) != PLUS)
6f086dfc
RS
3728 abort ();
3729
3730 start_sequence ();
14a774a9
RK
3731 if (GET_CODE (src) != REG)
3732 temp = force_operand (src, NULL_RTX);
6f086dfc 3733 else
14a774a9 3734 temp = src;
5f4f0e22 3735 temp = force_operand (plus_constant (temp, offset), NULL_RTX);
6f086dfc
RS
3736 seq = get_insns ();
3737 end_sequence ();
3738
3739 emit_insns_before (seq, object);
3740 SET_DEST (x) = new;
3741
e9a25f70 3742 if (! validate_change (object, &SET_SRC (x), temp, 0)
6f086dfc
RS
3743 || ! extra_insns)
3744 abort ();
3745
3746 return 1;
3747 }
3748
3749 instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
3750 loc = &SET_SRC (x);
3751 goto restart;
3752
3753 case PLUS:
3754 /* Handle special case of virtual register plus constant. */
3755 if (CONSTANT_P (XEXP (x, 1)))
3756 {
b1f82ccf 3757 rtx old, new_offset;
6f086dfc
RS
3758
3759 /* Check for (plus (plus VIRT foo) (const_int)) first. */
3760 if (GET_CODE (XEXP (x, 0)) == PLUS)
3761 {
3762 rtx inner = XEXP (XEXP (x, 0), 0);
3763
3764 if (inner == virtual_incoming_args_rtx)
3765 new = arg_pointer_rtx, offset = in_arg_offset;
3766 else if (inner == virtual_stack_vars_rtx)
3767 new = frame_pointer_rtx, offset = var_offset;
3768 else if (inner == virtual_stack_dynamic_rtx)
3769 new = stack_pointer_rtx, offset = dynamic_offset;
3770 else if (inner == virtual_outgoing_args_rtx)
3771 new = stack_pointer_rtx, offset = out_arg_offset;
71038426 3772 else if (inner == virtual_cfa_rtx)
718fe406 3773 new = arg_pointer_rtx, offset = cfa_offset;
6f086dfc
RS
3774 else
3775 {
3776 loc = &XEXP (x, 0);
3777 goto restart;
3778 }
3779
3780 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
3781 extra_insns);
38a448ca 3782 new = gen_rtx_PLUS (Pmode, new, XEXP (XEXP (x, 0), 1));
6f086dfc
RS
3783 }
3784
3785 else if (XEXP (x, 0) == virtual_incoming_args_rtx)
3786 new = arg_pointer_rtx, offset = in_arg_offset;
3787 else if (XEXP (x, 0) == virtual_stack_vars_rtx)
3788 new = frame_pointer_rtx, offset = var_offset;
3789 else if (XEXP (x, 0) == virtual_stack_dynamic_rtx)
3790 new = stack_pointer_rtx, offset = dynamic_offset;
3791 else if (XEXP (x, 0) == virtual_outgoing_args_rtx)
3792 new = stack_pointer_rtx, offset = out_arg_offset;
718fe406
KH
3793 else if (XEXP (x, 0) == virtual_cfa_rtx)
3794 new = arg_pointer_rtx, offset = cfa_offset;
6f086dfc
RS
3795 else
3796 {
3797 /* We know the second operand is a constant. Unless the
3798 first operand is a REG (which has been already checked),
3799 it needs to be checked. */
3800 if (GET_CODE (XEXP (x, 0)) != REG)
3801 {
3802 loc = &XEXP (x, 0);
3803 goto restart;
3804 }
3805 return 1;
3806 }
3807
b1f82ccf 3808 new_offset = plus_constant (XEXP (x, 1), offset);
6f086dfc 3809
b1f82ccf
DE
3810 /* If the new constant is zero, try to replace the sum with just
3811 the register. */
3812 if (new_offset == const0_rtx
3813 && validate_change (object, loc, new, 0))
6f086dfc
RS
3814 return 1;
3815
b1f82ccf
DE
3816 /* Next try to replace the register and new offset.
3817 There are two changes to validate here and we can't assume that
3818 in the case of old offset equals new just changing the register
3819 will yield a valid insn. In the interests of a little efficiency,
3820 however, we only call validate change once (we don't queue up the
0f41302f 3821 changes and then call apply_change_group). */
b1f82ccf
DE
3822
3823 old = XEXP (x, 0);
3824 if (offset == 0
3825 ? ! validate_change (object, &XEXP (x, 0), new, 0)
3826 : (XEXP (x, 0) = new,
3827 ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
6f086dfc
RS
3828 {
3829 if (! extra_insns)
3830 {
3831 XEXP (x, 0) = old;
3832 return 0;
3833 }
3834
3835 /* Otherwise copy the new constant into a register and replace
3836 constant with that register. */
3837 temp = gen_reg_rtx (Pmode);
b1f82ccf 3838 XEXP (x, 0) = new;
6f086dfc 3839 if (validate_change (object, &XEXP (x, 1), temp, 0))
b1f82ccf 3840 emit_insn_before (gen_move_insn (temp, new_offset), object);
6f086dfc
RS
3841 else
3842 {
3843 /* If that didn't work, replace this expression with a
3844 register containing the sum. */
3845
6f086dfc 3846 XEXP (x, 0) = old;
38a448ca 3847 new = gen_rtx_PLUS (Pmode, new, new_offset);
6f086dfc
RS
3848
3849 start_sequence ();
5f4f0e22 3850 temp = force_operand (new, NULL_RTX);
6f086dfc
RS
3851 seq = get_insns ();
3852 end_sequence ();
3853
3854 emit_insns_before (seq, object);
3855 if (! validate_change (object, loc, temp, 0)
3856 && ! validate_replace_rtx (x, temp, object))
3857 abort ();
3858 }
3859 }
3860
3861 return 1;
3862 }
3863
3864 /* Fall through to generic two-operand expression case. */
3865 case EXPR_LIST:
3866 case CALL:
3867 case COMPARE:
3868 case MINUS:
3869 case MULT:
3870 case DIV: case UDIV:
3871 case MOD: case UMOD:
3872 case AND: case IOR: case XOR:
45620ed4
RK
3873 case ROTATERT: case ROTATE:
3874 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
6f086dfc
RS
3875 case NE: case EQ:
3876 case GE: case GT: case GEU: case GTU:
3877 case LE: case LT: case LEU: case LTU:
3878 if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
3879 instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
3880 loc = &XEXP (x, 0);
3881 goto restart;
3882
3883 case MEM:
3884 /* Most cases of MEM that convert to valid addresses have already been
4fd796bb 3885 handled by our scan of decls. The only special handling we
6f086dfc 3886 need here is to make a copy of the rtx to ensure it isn't being
718fe406 3887 shared if we have to change it to a pseudo.
6f086dfc
RS
3888
3889 If the rtx is a simple reference to an address via a virtual register,
3890 it can potentially be shared. In such cases, first try to make it
3891 a valid address, which can also be shared. Otherwise, copy it and
718fe406 3892 proceed normally.
6f086dfc
RS
3893
3894 First check for common cases that need no processing. These are
3895 usually due to instantiation already being done on a previous instance
3896 of a shared rtx. */
3897
3898 temp = XEXP (x, 0);
3899 if (CONSTANT_ADDRESS_P (temp)
3900#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3901 || temp == arg_pointer_rtx
b37f453b
DE
3902#endif
3903#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3904 || temp == hard_frame_pointer_rtx
6f086dfc
RS
3905#endif
3906 || temp == frame_pointer_rtx)
3907 return 1;
3908
3909 if (GET_CODE (temp) == PLUS
3910 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3911 && (XEXP (temp, 0) == frame_pointer_rtx
b37f453b
DE
3912#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3913 || XEXP (temp, 0) == hard_frame_pointer_rtx
3914#endif
6f086dfc
RS
3915#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3916 || XEXP (temp, 0) == arg_pointer_rtx
3917#endif
3918 ))
3919 return 1;
3920
3921 if (temp == virtual_stack_vars_rtx
3922 || temp == virtual_incoming_args_rtx
3923 || (GET_CODE (temp) == PLUS
3924 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3925 && (XEXP (temp, 0) == virtual_stack_vars_rtx
3926 || XEXP (temp, 0) == virtual_incoming_args_rtx)))
3927 {
3928 /* This MEM may be shared. If the substitution can be done without
3929 the need to generate new pseudos, we want to do it in place
3930 so all copies of the shared rtx benefit. The call below will
3931 only make substitutions if the resulting address is still
3932 valid.
3933
3934 Note that we cannot pass X as the object in the recursive call
3935 since the insn being processed may not allow all valid
6461be14
RS
3936 addresses. However, if we were not passed on object, we can
3937 only modify X without copying it if X will have a valid
3938 address.
6f086dfc 3939
6461be14
RS
3940 ??? Also note that this can still lose if OBJECT is an insn that
3941 has less restrictions on an address that some other insn.
3942 In that case, we will modify the shared address. This case
4fd796bb
RK
3943 doesn't seem very likely, though. One case where this could
3944 happen is in the case of a USE or CLOBBER reference, but we
3945 take care of that below. */
6461be14
RS
3946
3947 if (instantiate_virtual_regs_1 (&XEXP (x, 0),
3948 object ? object : x, 0))
6f086dfc
RS
3949 return 1;
3950
3951 /* Otherwise make a copy and process that copy. We copy the entire
3952 RTL expression since it might be a PLUS which could also be
3953 shared. */
3954 *loc = x = copy_rtx (x);
3955 }
3956
3957 /* Fall through to generic unary operation case. */
6f086dfc
RS
3958 case SUBREG:
3959 case STRICT_LOW_PART:
3960 case NEG: case NOT:
3961 case PRE_DEC: case PRE_INC: case POST_DEC: case POST_INC:
3962 case SIGN_EXTEND: case ZERO_EXTEND:
3963 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3964 case FLOAT: case FIX:
3965 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3966 case ABS:
3967 case SQRT:
3968 case FFS:
3969 /* These case either have just one operand or we know that we need not
3970 check the rest of the operands. */
3971 loc = &XEXP (x, 0);
3972 goto restart;
3973
4fd796bb
RK
3974 case USE:
3975 case CLOBBER:
3976 /* If the operand is a MEM, see if the change is a valid MEM. If not,
3977 go ahead and make the invalid one, but do it to a copy. For a REG,
718fe406 3978 just make the recursive call, since there's no chance of a problem. */
4fd796bb
RK
3979
3980 if ((GET_CODE (XEXP (x, 0)) == MEM
3981 && instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), XEXP (x, 0),
3982 0))
3983 || (GET_CODE (XEXP (x, 0)) == REG
7694ce35 3984 && instantiate_virtual_regs_1 (&XEXP (x, 0), object, 0)))
4fd796bb
RK
3985 return 1;
3986
3987 XEXP (x, 0) = copy_rtx (XEXP (x, 0));
3988 loc = &XEXP (x, 0);
3989 goto restart;
3990
6f086dfc
RS
3991 case REG:
3992 /* Try to replace with a PLUS. If that doesn't work, compute the sum
3993 in front of this insn and substitute the temporary. */
3994 if (x == virtual_incoming_args_rtx)
3995 new = arg_pointer_rtx, offset = in_arg_offset;
3996 else if (x == virtual_stack_vars_rtx)
3997 new = frame_pointer_rtx, offset = var_offset;
3998 else if (x == virtual_stack_dynamic_rtx)
3999 new = stack_pointer_rtx, offset = dynamic_offset;
4000 else if (x == virtual_outgoing_args_rtx)
4001 new = stack_pointer_rtx, offset = out_arg_offset;
71038426 4002 else if (x == virtual_cfa_rtx)
718fe406 4003 new = arg_pointer_rtx, offset = cfa_offset;
6f086dfc
RS
4004
4005 if (new)
4006 {
4007 temp = plus_constant (new, offset);
4008 if (!validate_change (object, loc, temp, 0))
4009 {
4010 if (! extra_insns)
4011 return 0;
4012
4013 start_sequence ();
5f4f0e22 4014 temp = force_operand (temp, NULL_RTX);
6f086dfc
RS
4015 seq = get_insns ();
4016 end_sequence ();
4017
4018 emit_insns_before (seq, object);
4019 if (! validate_change (object, loc, temp, 0)
4020 && ! validate_replace_rtx (x, temp, object))
4021 abort ();
4022 }
4023 }
4024
4025 return 1;
e9a25f70
JL
4026
4027 case ADDRESSOF:
4028 if (GET_CODE (XEXP (x, 0)) == REG)
4029 return 1;
4030
4031 else if (GET_CODE (XEXP (x, 0)) == MEM)
4032 {
4033 /* If we have a (addressof (mem ..)), do any instantiation inside
4034 since we know we'll be making the inside valid when we finally
4035 remove the ADDRESSOF. */
4036 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), NULL_RTX, 0);
4037 return 1;
4038 }
4039 break;
718fe406 4040
e9a25f70
JL
4041 default:
4042 break;
6f086dfc
RS
4043 }
4044
4045 /* Scan all subexpressions. */
4046 fmt = GET_RTX_FORMAT (code);
4047 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
4048 if (*fmt == 'e')
4049 {
4050 if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
4051 return 0;
4052 }
4053 else if (*fmt == 'E')
4054 for (j = 0; j < XVECLEN (x, i); j++)
4055 if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
4056 extra_insns))
4057 return 0;
4058
4059 return 1;
4060}
4061\f
4062/* Optimization: assuming this function does not receive nonlocal gotos,
4063 delete the handlers for such, as well as the insns to establish
4064 and disestablish them. */
4065
4066static void
4067delete_handlers ()
4068{
4069 rtx insn;
4070 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4071 {
4072 /* Delete the handler by turning off the flag that would
4073 prevent jump_optimize from deleting it.
4074 Also permit deletion of the nonlocal labels themselves
4075 if nothing local refers to them. */
4076 if (GET_CODE (insn) == CODE_LABEL)
71cd4a8d
JW
4077 {
4078 tree t, last_t;
4079
4080 LABEL_PRESERVE_P (insn) = 0;
4081
4082 /* Remove it from the nonlocal_label list, to avoid confusing
4083 flow. */
4084 for (t = nonlocal_labels, last_t = 0; t;
4085 last_t = t, t = TREE_CHAIN (t))
4086 if (DECL_RTL (TREE_VALUE (t)) == insn)
4087 break;
4088 if (t)
4089 {
4090 if (! last_t)
4091 nonlocal_labels = TREE_CHAIN (nonlocal_labels);
4092 else
4093 TREE_CHAIN (last_t) = TREE_CHAIN (t);
4094 }
4095 }
ba716ac9
BS
4096 if (GET_CODE (insn) == INSN)
4097 {
4098 int can_delete = 0;
4099 rtx t;
4100 for (t = nonlocal_goto_handler_slots; t != 0; t = XEXP (t, 1))
4101 if (reg_mentioned_p (t, PATTERN (insn)))
4102 {
4103 can_delete = 1;
4104 break;
4105 }
4106 if (can_delete
59257ff7
RK
4107 || (nonlocal_goto_stack_level != 0
4108 && reg_mentioned_p (nonlocal_goto_stack_level,
ba716ac9
BS
4109 PATTERN (insn))))
4110 delete_insn (insn);
4111 }
6f086dfc
RS
4112 }
4113}
6f086dfc 4114\f
6f086dfc
RS
4115int
4116max_parm_reg_num ()
4117{
4118 return max_parm_reg;
4119}
4120
4121/* Return the first insn following those generated by `assign_parms'. */
4122
4123rtx
4124get_first_nonparm_insn ()
4125{
4126 if (last_parm_insn)
4127 return NEXT_INSN (last_parm_insn);
4128 return get_insns ();
4129}
4130
5378192b
RS
4131/* Return the first NOTE_INSN_BLOCK_BEG note in the function.
4132 Crash if there is none. */
4133
4134rtx
4135get_first_block_beg ()
4136{
4137 register rtx searcher;
4138 register rtx insn = get_first_nonparm_insn ();
4139
4140 for (searcher = insn; searcher; searcher = NEXT_INSN (searcher))
4141 if (GET_CODE (searcher) == NOTE
4142 && NOTE_LINE_NUMBER (searcher) == NOTE_INSN_BLOCK_BEG)
4143 return searcher;
4144
4145 abort (); /* Invalid call to this function. (See comments above.) */
4146 return NULL_RTX;
4147}
4148
d181c154
RS
4149/* Return 1 if EXP is an aggregate type (or a value with aggregate type).
4150 This means a type for which function calls must pass an address to the
4151 function or get an address back from the function.
4152 EXP may be a type node or an expression (whose type is tested). */
6f086dfc
RS
4153
4154int
4155aggregate_value_p (exp)
4156 tree exp;
4157{
9d790a4f
RS
4158 int i, regno, nregs;
4159 rtx reg;
2f939d94
TP
4160
4161 tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
d181c154 4162
d7bf8ada
MM
4163 if (TREE_CODE (type) == VOID_TYPE)
4164 return 0;
d181c154 4165 if (RETURN_IN_MEMORY (type))
6f086dfc 4166 return 1;
956d6950 4167 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
49a2e5b2
DE
4168 and thus can't be returned in registers. */
4169 if (TREE_ADDRESSABLE (type))
4170 return 1;
05e3bdb9 4171 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
6f086dfc 4172 return 1;
9d790a4f
RS
4173 /* Make sure we have suitable call-clobbered regs to return
4174 the value in; if not, we must return it in memory. */
4dc07bd7 4175 reg = hard_function_value (type, 0, 0);
e71f7aa5
JW
4176
4177 /* If we have something other than a REG (e.g. a PARALLEL), then assume
4178 it is OK. */
4179 if (GET_CODE (reg) != REG)
4180 return 0;
4181
9d790a4f 4182 regno = REGNO (reg);
d181c154 4183 nregs = HARD_REGNO_NREGS (regno, TYPE_MODE (type));
9d790a4f
RS
4184 for (i = 0; i < nregs; i++)
4185 if (! call_used_regs[regno + i])
4186 return 1;
6f086dfc
RS
4187 return 0;
4188}
4189\f
4190/* Assign RTL expressions to the function's parameters.
4191 This may involve copying them into registers and using
0d1416c6 4192 those registers as the RTL for them. */
6f086dfc
RS
4193
4194void
0d1416c6 4195assign_parms (fndecl)
6f086dfc 4196 tree fndecl;
6f086dfc
RS
4197{
4198 register tree parm;
4199 register rtx entry_parm = 0;
4200 register rtx stack_parm = 0;
4201 CUMULATIVE_ARGS args_so_far;
621061f4
RK
4202 enum machine_mode promoted_mode, passed_mode;
4203 enum machine_mode nominal_mode, promoted_nominal_mode;
00d8a4c1 4204 int unsignedp;
6f086dfc
RS
4205 /* Total space needed so far for args on the stack,
4206 given as a constant and a tree-expression. */
4207 struct args_size stack_args_size;
4208 tree fntype = TREE_TYPE (fndecl);
4209 tree fnargs = DECL_ARGUMENTS (fndecl);
4210 /* This is used for the arg pointer when referring to stack args. */
4211 rtx internal_arg_pointer;
718fe406 4212 /* This is a dummy PARM_DECL that we used for the function result if
6f086dfc
RS
4213 the function returns a structure. */
4214 tree function_result_decl = 0;
54ea1de9 4215#ifdef SETUP_INCOMING_VARARGS
6f086dfc 4216 int varargs_setup = 0;
54ea1de9 4217#endif
3412b298 4218 rtx conversion_insns = 0;
4fc026cd 4219 struct args_size alignment_pad;
6f086dfc
RS
4220
4221 /* Nonzero if the last arg is named `__builtin_va_alist',
4222 which is used on some machines for old-fashioned non-ANSI varargs.h;
4223 this should be stuck onto the stack as if it had arrived there. */
3b69d50e
RK
4224 int hide_last_arg
4225 = (current_function_varargs
4226 && fnargs
6f086dfc
RS
4227 && (parm = tree_last (fnargs)) != 0
4228 && DECL_NAME (parm)
4229 && (! strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
4230 "__builtin_va_alist")));
4231
4232 /* Nonzero if function takes extra anonymous args.
4233 This means the last named arg must be on the stack
0f41302f 4234 right before the anonymous ones. */
6f086dfc
RS
4235 int stdarg
4236 = (TYPE_ARG_TYPES (fntype) != 0
4237 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4238 != void_type_node));
4239
ebb904cb
RK
4240 current_function_stdarg = stdarg;
4241
6f086dfc
RS
4242 /* If the reg that the virtual arg pointer will be translated into is
4243 not a fixed reg or is the stack pointer, make a copy of the virtual
4244 arg pointer, and address parms via the copy. The frame pointer is
4245 considered fixed even though it is not marked as such.
4246
4247 The second time through, simply use ap to avoid generating rtx. */
4248
4249 if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
4250 || ! (fixed_regs[ARG_POINTER_REGNUM]
0d1416c6 4251 || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM)))
6f086dfc
RS
4252 internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
4253 else
4254 internal_arg_pointer = virtual_incoming_args_rtx;
4255 current_function_internal_arg_pointer = internal_arg_pointer;
4256
4257 stack_args_size.constant = 0;
4258 stack_args_size.var = 0;
4259
4260 /* If struct value address is treated as the first argument, make it so. */
4261 if (aggregate_value_p (DECL_RESULT (fndecl))
4262 && ! current_function_returns_pcc_struct
4263 && struct_value_incoming_rtx == 0)
4264 {
f9f29478 4265 tree type = build_pointer_type (TREE_TYPE (fntype));
6f086dfc 4266
5f4f0e22 4267 function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
6f086dfc
RS
4268
4269 DECL_ARG_TYPE (function_result_decl) = type;
4270 TREE_CHAIN (function_result_decl) = fnargs;
4271 fnargs = function_result_decl;
4272 }
718fe406 4273
e9a25f70 4274 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
e2ecd91c 4275 parm_reg_stack_loc = (rtx *) xcalloc (max_parm_reg, sizeof (rtx));
6f086dfc
RS
4276
4277#ifdef INIT_CUMULATIVE_INCOMING_ARGS
ea0d4c4b 4278 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
6f086dfc 4279#else
2c7ee1a6 4280 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, 0);
6f086dfc
RS
4281#endif
4282
4283 /* We haven't yet found an argument that we must push and pretend the
4284 caller did. */
4285 current_function_pretend_args_size = 0;
4286
4287 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
4288 {
6f086dfc
RS
4289 struct args_size stack_offset;
4290 struct args_size arg_size;
4291 int passed_pointer = 0;
621061f4 4292 int did_conversion = 0;
6f086dfc 4293 tree passed_type = DECL_ARG_TYPE (parm);
621061f4 4294 tree nominal_type = TREE_TYPE (parm);
9ab70a9b 4295 int pretend_named;
6f086dfc
RS
4296
4297 /* Set LAST_NAMED if this is last named arg before some
bf9c83fe 4298 anonymous args. */
6f086dfc
RS
4299 int last_named = ((TREE_CHAIN (parm) == 0
4300 || DECL_NAME (TREE_CHAIN (parm)) == 0)
3b69d50e 4301 && (stdarg || current_function_varargs));
bf9c83fe
JW
4302 /* Set NAMED_ARG if this arg should be treated as a named arg. For
4303 most machines, if this is a varargs/stdarg function, then we treat
4304 the last named arg as if it were anonymous too. */
e5e809f4 4305 int named_arg = STRICT_ARGUMENT_NAMING ? 1 : ! last_named;
6f086dfc
RS
4306
4307 if (TREE_TYPE (parm) == error_mark_node
4308 /* This can happen after weird syntax errors
4309 or if an enum type is defined among the parms. */
4310 || TREE_CODE (parm) != PARM_DECL
4311 || passed_type == NULL)
4312 {
38a448ca
RH
4313 DECL_INCOMING_RTL (parm) = DECL_RTL (parm)
4314 = gen_rtx_MEM (BLKmode, const0_rtx);
6f086dfc
RS
4315 TREE_USED (parm) = 1;
4316 continue;
4317 }
4318
4319 /* For varargs.h function, save info about regs and stack space
4320 used by the individual args, not including the va_alist arg. */
3b69d50e 4321 if (hide_last_arg && last_named)
6f086dfc
RS
4322 current_function_args_info = args_so_far;
4323
4324 /* Find mode of arg as it is passed, and mode of arg
4325 as it should be during execution of this function. */
4326 passed_mode = TYPE_MODE (passed_type);
621061f4 4327 nominal_mode = TYPE_MODE (nominal_type);
6f086dfc 4328
16bae307
RS
4329 /* If the parm's mode is VOID, its value doesn't matter,
4330 and avoid the usual things like emit_move_insn that could crash. */
4331 if (nominal_mode == VOIDmode)
4332 {
4333 DECL_INCOMING_RTL (parm) = DECL_RTL (parm) = const0_rtx;
4334 continue;
4335 }
4336
3f46679a
RK
4337 /* If the parm is to be passed as a transparent union, use the
4338 type of the first field for the tests below. We have already
4339 verified that the modes are the same. */
4340 if (DECL_TRANSPARENT_UNION (parm)
2bf105ab
RK
4341 || (TREE_CODE (passed_type) == UNION_TYPE
4342 && TYPE_TRANSPARENT_UNION (passed_type)))
3f46679a
RK
4343 passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
4344
a14ae508
RK
4345 /* See if this arg was passed by invisible reference. It is if
4346 it is an object whose size depends on the contents of the
4347 object itself or if the machine requires these objects be passed
4348 that way. */
4349
4350 if ((TREE_CODE (TYPE_SIZE (passed_type)) != INTEGER_CST
4351 && contains_placeholder_p (TYPE_SIZE (passed_type)))
657bb6dc 4352 || TREE_ADDRESSABLE (passed_type)
6f086dfc 4353#ifdef FUNCTION_ARG_PASS_BY_REFERENCE
a14ae508 4354 || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
bf9c83fe 4355 passed_type, named_arg)
a14ae508
RK
4356#endif
4357 )
6f086dfc 4358 {
621061f4 4359 passed_type = nominal_type = build_pointer_type (passed_type);
6f086dfc
RS
4360 passed_pointer = 1;
4361 passed_mode = nominal_mode = Pmode;
4362 }
6f086dfc 4363
a53e14c0
RK
4364 promoted_mode = passed_mode;
4365
4366#ifdef PROMOTE_FUNCTION_ARGS
4367 /* Compute the mode in which the arg is actually extended to. */
7940255d 4368 unsignedp = TREE_UNSIGNED (passed_type);
a5a52dbc 4369 promoted_mode = promote_mode (passed_type, promoted_mode, &unsignedp, 1);
a53e14c0
RK
4370#endif
4371
6f086dfc
RS
4372 /* Let machine desc say which reg (if any) the parm arrives in.
4373 0 means it arrives on the stack. */
4374#ifdef FUNCTION_INCOMING_ARG
a53e14c0 4375 entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
bf9c83fe 4376 passed_type, named_arg);
6f086dfc 4377#else
a53e14c0 4378 entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
bf9c83fe 4379 passed_type, named_arg);
6f086dfc
RS
4380#endif
4381
621061f4
RK
4382 if (entry_parm == 0)
4383 promoted_mode = passed_mode;
a53e14c0 4384
6f086dfc
RS
4385#ifdef SETUP_INCOMING_VARARGS
4386 /* If this is the last named parameter, do any required setup for
4387 varargs or stdargs. We need to know about the case of this being an
4388 addressable type, in which case we skip the registers it
4389 would have arrived in.
4390
4391 For stdargs, LAST_NAMED will be set for two parameters, the one that
4392 is actually the last named, and the dummy parameter. We only
4393 want to do this action once.
4394
4395 Also, indicate when RTL generation is to be suppressed. */
4396 if (last_named && !varargs_setup)
4397 {
621061f4 4398 SETUP_INCOMING_VARARGS (args_so_far, promoted_mode, passed_type,
0d1416c6 4399 current_function_pretend_args_size, 0);
6f086dfc
RS
4400 varargs_setup = 1;
4401 }
4402#endif
4403
4404 /* Determine parm's home in the stack,
4405 in case it arrives in the stack or we should pretend it did.
4406
4407 Compute the stack position and rtx where the argument arrives
4408 and its size.
4409
4410 There is one complexity here: If this was a parameter that would
4411 have been passed in registers, but wasn't only because it is
4412 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
4413 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
4414 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
4415 0 as it was the previous time. */
4416
9ab70a9b 4417 pretend_named = named_arg || PRETEND_OUTGOING_VARARGS_NAMED;
0f11903b 4418 locate_and_pad_parm (promoted_mode, passed_type,
6f086dfc
RS
4419#ifdef STACK_PARMS_IN_REG_PARM_AREA
4420 1,
4421#else
4422#ifdef FUNCTION_INCOMING_ARG
621061f4 4423 FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
6f086dfc 4424 passed_type,
9ab70a9b 4425 pretend_named) != 0,
6f086dfc 4426#else
621061f4 4427 FUNCTION_ARG (args_so_far, promoted_mode,
6f086dfc 4428 passed_type,
9ab70a9b 4429 pretend_named) != 0,
6f086dfc
RS
4430#endif
4431#endif
4fc026cd 4432 fndecl, &stack_args_size, &stack_offset, &arg_size,
718fe406 4433 &alignment_pad);
6f086dfc 4434
0d1416c6
BS
4435 {
4436 rtx offset_rtx = ARGS_SIZE_RTX (stack_offset);
4437
4438 if (offset_rtx == const0_rtx)
4439 stack_parm = gen_rtx_MEM (promoted_mode, internal_arg_pointer);
4440 else
4441 stack_parm = gen_rtx_MEM (promoted_mode,
4442 gen_rtx_PLUS (Pmode,
4443 internal_arg_pointer,
4444 offset_rtx));
4445
3bdf5ad1 4446 set_mem_attributes (stack_parm, parm, 1);
0d1416c6 4447 }
6f086dfc
RS
4448
4449 /* If this parameter was passed both in registers and in the stack,
4450 use the copy on the stack. */
621061f4 4451 if (MUST_PASS_IN_STACK (promoted_mode, passed_type))
6f086dfc
RS
4452 entry_parm = 0;
4453
461beb10 4454#ifdef FUNCTION_ARG_PARTIAL_NREGS
6f086dfc
RS
4455 /* If this parm was passed part in regs and part in memory,
4456 pretend it arrived entirely in memory
4457 by pushing the register-part onto the stack.
4458
4459 In the special case of a DImode or DFmode that is split,
4460 we could put it together in a pseudoreg directly,
4461 but for now that's not worth bothering with. */
4462
4463 if (entry_parm)
4464 {
621061f4 4465 int nregs = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, promoted_mode,
bf9c83fe 4466 passed_type, named_arg);
6f086dfc
RS
4467
4468 if (nregs > 0)
4469 {
4470 current_function_pretend_args_size
4471 = (((nregs * UNITS_PER_WORD) + (PARM_BOUNDARY / BITS_PER_UNIT) - 1)
4472 / (PARM_BOUNDARY / BITS_PER_UNIT)
4473 * (PARM_BOUNDARY / BITS_PER_UNIT));
4474
0d1416c6
BS
4475 /* Handle calls that pass values in multiple non-contiguous
4476 locations. The Irix 6 ABI has examples of this. */
4477 if (GET_CODE (entry_parm) == PARALLEL)
4478 emit_group_store (validize_mem (stack_parm), entry_parm,
4479 int_size_in_bytes (TREE_TYPE (parm)),
19caa751 4480 TYPE_ALIGN (TREE_TYPE (parm)));
718fe406 4481
0d1416c6
BS
4482 else
4483 move_block_from_reg (REGNO (entry_parm),
4484 validize_mem (stack_parm), nregs,
4485 int_size_in_bytes (TREE_TYPE (parm)));
4486
6f086dfc
RS
4487 entry_parm = stack_parm;
4488 }
4489 }
461beb10 4490#endif
6f086dfc
RS
4491
4492 /* If we didn't decide this parm came in a register,
4493 by default it came on the stack. */
4494 if (entry_parm == 0)
4495 entry_parm = stack_parm;
4496
4497 /* Record permanently how this parm was passed. */
0d1416c6 4498 DECL_INCOMING_RTL (parm) = entry_parm;
6f086dfc
RS
4499
4500 /* If there is actually space on the stack for this parm,
4501 count it in stack_args_size; otherwise set stack_parm to 0
4502 to indicate there is no preallocated stack slot for the parm. */
4503
4504 if (entry_parm == stack_parm
ab87f8c8
JL
4505 || (GET_CODE (entry_parm) == PARALLEL
4506 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
d9ca49d5 4507#if defined (REG_PARM_STACK_SPACE) && ! defined (MAYBE_REG_PARM_STACK_SPACE)
6f086dfc 4508 /* On some machines, even if a parm value arrives in a register
d9ca49d5
JW
4509 there is still an (uninitialized) stack slot allocated for it.
4510
4511 ??? When MAYBE_REG_PARM_STACK_SPACE is defined, we can't tell
4512 whether this parameter already has a stack slot allocated,
4513 because an arg block exists only if current_function_args_size
abc95ed3 4514 is larger than some threshold, and we haven't calculated that
d9ca49d5
JW
4515 yet. So, for now, we just assume that stack slots never exist
4516 in this case. */
6f086dfc
RS
4517 || REG_PARM_STACK_SPACE (fndecl) > 0
4518#endif
4519 )
4520 {
4521 stack_args_size.constant += arg_size.constant;
4522 if (arg_size.var)
4523 ADD_PARM_SIZE (stack_args_size, arg_size.var);
4524 }
4525 else
4526 /* No stack slot was pushed for this parm. */
4527 stack_parm = 0;
4528
4529 /* Update info on where next arg arrives in registers. */
4530
621061f4 4531 FUNCTION_ARG_ADVANCE (args_so_far, promoted_mode,
bf9c83fe 4532 passed_type, named_arg);
6f086dfc 4533
e16c591a
RS
4534 /* If we can't trust the parm stack slot to be aligned enough
4535 for its ultimate type, don't use that slot after entry.
4536 We'll make another stack slot, if we need one. */
4537 {
c8d8ed65 4538 unsigned int thisparm_boundary
621061f4 4539 = FUNCTION_ARG_BOUNDARY (promoted_mode, passed_type);
e16c591a
RS
4540
4541 if (GET_MODE_ALIGNMENT (nominal_mode) > thisparm_boundary)
4542 stack_parm = 0;
4543 }
4544
cb61f66f
RS
4545 /* If parm was passed in memory, and we need to convert it on entry,
4546 don't store it back in that same slot. */
4547 if (entry_parm != 0
4548 && nominal_mode != BLKmode && nominal_mode != passed_mode)
4549 stack_parm = 0;
4550
6f086dfc
RS
4551 /* ENTRY_PARM is an RTX for the parameter as it arrives,
4552 in the mode in which it arrives.
4553 STACK_PARM is an RTX for a stack slot where the parameter can live
4554 during the function (in case we want to put it there).
4555 STACK_PARM is 0 if no stack slot was pushed for it.
4556
4557 Now output code if necessary to convert ENTRY_PARM to
4558 the type in which this function declares it,
4559 and store that result in an appropriate place,
4560 which may be a pseudo reg, may be STACK_PARM,
4561 or may be a local stack slot if STACK_PARM is 0.
4562
4563 Set DECL_RTL to that place. */
4564
5c4cdc9f 4565 if (nominal_mode == BLKmode || GET_CODE (entry_parm) == PARALLEL)
6f086dfc 4566 {
5c4cdc9f
JW
4567 /* If a BLKmode arrives in registers, copy it to a stack slot.
4568 Handle calls that pass values in multiple non-contiguous
4569 locations. The Irix 6 ABI has examples of this. */
4570 if (GET_CODE (entry_parm) == REG
4571 || GET_CODE (entry_parm) == PARALLEL)
6f086dfc 4572 {
621061f4
RK
4573 int size_stored
4574 = CEIL_ROUND (int_size_in_bytes (TREE_TYPE (parm)),
4575 UNITS_PER_WORD);
6f086dfc
RS
4576
4577 /* Note that we will be storing an integral number of words.
4578 So we have to be careful to ensure that we allocate an
4579 integral number of words. We do this below in the
4580 assign_stack_local if space was not allocated in the argument
4581 list. If it was, this will not work if PARM_BOUNDARY is not
4582 a multiple of BITS_PER_WORD. It isn't clear how to fix this
4583 if it becomes a problem. */
4584
4585 if (stack_parm == 0)
7e41ffa2
RS
4586 {
4587 stack_parm
621061f4
RK
4588 = assign_stack_local (GET_MODE (entry_parm),
4589 size_stored, 0);
3bdf5ad1 4590 set_mem_attributes (stack_parm, parm, 1);
7e41ffa2
RS
4591 }
4592
6f086dfc
RS
4593 else if (PARM_BOUNDARY % BITS_PER_WORD != 0)
4594 abort ();
4595
5c4cdc9f
JW
4596 /* Handle calls that pass values in multiple non-contiguous
4597 locations. The Irix 6 ABI has examples of this. */
4598 if (GET_CODE (entry_parm) == PARALLEL)
aac5cc16
RH
4599 emit_group_store (validize_mem (stack_parm), entry_parm,
4600 int_size_in_bytes (TREE_TYPE (parm)),
19caa751 4601 TYPE_ALIGN (TREE_TYPE (parm)));
5c4cdc9f
JW
4602 else
4603 move_block_from_reg (REGNO (entry_parm),
4604 validize_mem (stack_parm),
4605 size_stored / UNITS_PER_WORD,
4606 int_size_in_bytes (TREE_TYPE (parm)));
6f086dfc
RS
4607 }
4608 DECL_RTL (parm) = stack_parm;
4609 }
d29c259b
RH
4610 else if (! ((! optimize
4611 && ! DECL_REGISTER (parm)
a82ad570 4612 && ! DECL_INLINE (fndecl))
6f086dfc
RS
4613 /* layout_decl may set this. */
4614 || TREE_ADDRESSABLE (parm)
4615 || TREE_SIDE_EFFECTS (parm)
4616 /* If -ffloat-store specified, don't put explicit
4617 float variables into registers. */
4618 || (flag_float_store
4619 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
4620 /* Always assign pseudo to structure return or item passed
4621 by invisible reference. */
4622 || passed_pointer || parm == function_result_decl)
4623 {
00d8a4c1
RK
4624 /* Store the parm in a pseudoregister during the function, but we
4625 may need to do it in a wider mode. */
4626
4627 register rtx parmreg;
770ae6cc 4628 unsigned int regno, regnoi = 0, regnor = 0;
00d8a4c1
RK
4629
4630 unsignedp = TREE_UNSIGNED (TREE_TYPE (parm));
cd5b3469 4631
621061f4
RK
4632 promoted_nominal_mode
4633 = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
6f086dfc 4634
621061f4 4635 parmreg = gen_reg_rtx (promoted_nominal_mode);
ddb7361a 4636 mark_user_reg (parmreg);
6f086dfc
RS
4637
4638 /* If this was an item that we received a pointer to, set DECL_RTL
4639 appropriately. */
4640 if (passed_pointer)
4641 {
621061f4 4642 DECL_RTL (parm)
38a448ca 4643 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (passed_type)), parmreg);
3bdf5ad1 4644 set_mem_attributes (DECL_RTL (parm), parm, 1);
6f086dfc
RS
4645 }
4646 else
4647 DECL_RTL (parm) = parmreg;
4648
4649 /* Copy the value into the register. */
621061f4
RK
4650 if (nominal_mode != passed_mode
4651 || promoted_nominal_mode != promoted_mode)
86f8eff3 4652 {
efd8cba0 4653 int save_tree_used;
621061f4 4654 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
718fe406 4655 mode, by the caller. We now have to convert it to
621061f4 4656 NOMINAL_MODE, if different. However, PARMREG may be in
956d6950 4657 a different mode than NOMINAL_MODE if it is being stored
621061f4
RK
4658 promoted.
4659
4660 If ENTRY_PARM is a hard register, it might be in a register
86f8eff3
RK
4661 not valid for operating in its mode (e.g., an odd-numbered
4662 register for a DFmode). In that case, moves are the only
4663 thing valid, so we can't do a convert from there. This
4664 occurs when the calling sequence allow such misaligned
3412b298
JW
4665 usages.
4666
4667 In addition, the conversion may involve a call, which could
4668 clobber parameters which haven't been copied to pseudo
4669 registers yet. Therefore, we must first copy the parm to
4670 a pseudo reg here, and save the conversion until after all
4671 parameters have been moved. */
4672
4673 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4674
4675 emit_move_insn (tempreg, validize_mem (entry_parm));
4676
4677 push_to_sequence (conversion_insns);
ad241351
RK
4678 tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
4679
efd8cba0
DB
4680 /* TREE_USED gets set erroneously during expand_assignment. */
4681 save_tree_used = TREE_USED (parm);
621061f4
RK
4682 expand_assignment (parm,
4683 make_tree (nominal_type, tempreg), 0, 0);
efd8cba0 4684 TREE_USED (parm) = save_tree_used;
3412b298 4685 conversion_insns = get_insns ();
621061f4 4686 did_conversion = 1;
3412b298 4687 end_sequence ();
86f8eff3 4688 }
6f086dfc
RS
4689 else
4690 emit_move_insn (parmreg, validize_mem (entry_parm));
4691
74bd77a8
RS
4692 /* If we were passed a pointer but the actual value
4693 can safely live in a register, put it in one. */
16bae307 4694 if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
d29c259b
RH
4695 && ! ((! optimize
4696 && ! DECL_REGISTER (parm)
74bd77a8
RS
4697 && ! DECL_INLINE (fndecl))
4698 /* layout_decl may set this. */
4699 || TREE_ADDRESSABLE (parm)
4700 || TREE_SIDE_EFFECTS (parm)
4701 /* If -ffloat-store specified, don't put explicit
4702 float variables into registers. */
4703 || (flag_float_store
4704 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE)))
4705 {
2654605a
JW
4706 /* We can't use nominal_mode, because it will have been set to
4707 Pmode above. We must use the actual mode of the parm. */
4708 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
ddb7361a 4709 mark_user_reg (parmreg);
74bd77a8
RS
4710 emit_move_insn (parmreg, DECL_RTL (parm));
4711 DECL_RTL (parm) = parmreg;
c110c53d
RS
4712 /* STACK_PARM is the pointer, not the parm, and PARMREG is
4713 now the parm. */
4714 stack_parm = 0;
74bd77a8 4715 }
137a2a7b
DE
4716#ifdef FUNCTION_ARG_CALLEE_COPIES
4717 /* If we are passed an arg by reference and it is our responsibility
4718 to make a copy, do it now.
4719 PASSED_TYPE and PASSED mode now refer to the pointer, not the
4720 original argument, so we must recreate them in the call to
4721 FUNCTION_ARG_CALLEE_COPIES. */
4722 /* ??? Later add code to handle the case that if the argument isn't
4723 modified, don't do the copy. */
4724
4725 else if (passed_pointer
4726 && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
4727 TYPE_MODE (DECL_ARG_TYPE (parm)),
4728 DECL_ARG_TYPE (parm),
bf9c83fe 4729 named_arg)
926b1b99 4730 && ! TREE_ADDRESSABLE (DECL_ARG_TYPE (parm)))
137a2a7b
DE
4731 {
4732 rtx copy;
4733 tree type = DECL_ARG_TYPE (parm);
4734
4735 /* This sequence may involve a library call perhaps clobbering
4736 registers that haven't been copied to pseudos yet. */
4737
4738 push_to_sequence (conversion_insns);
4739
d0f062fb 4740 if (!COMPLETE_TYPE_P (type)
137a2a7b 4741 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1fd3ef7f 4742 /* This is a variable sized object. */
38a448ca
RH
4743 copy = gen_rtx_MEM (BLKmode,
4744 allocate_dynamic_stack_space
4745 (expr_size (parm), NULL_RTX,
4746 TYPE_ALIGN (type)));
137a2a7b 4747 else
1fd3ef7f
RK
4748 copy = assign_stack_temp (TYPE_MODE (type),
4749 int_size_in_bytes (type), 1);
a696c1d6 4750 set_mem_attributes (copy, parm, 1);
137a2a7b
DE
4751
4752 store_expr (parm, copy, 0);
4753 emit_move_insn (parmreg, XEXP (copy, 0));
7d384cc0 4754 if (current_function_check_memory_usage)
ebb1b59a
BS
4755 emit_library_call (chkr_set_right_libfunc,
4756 LCT_CONST_MAKE_BLOCK, VOIDmode, 3,
6a9c4aed 4757 XEXP (copy, 0), Pmode,
86fa911a
RK
4758 GEN_INT (int_size_in_bytes (type)),
4759 TYPE_MODE (sizetype),
956d6950
JL
4760 GEN_INT (MEMORY_USE_RW),
4761 TYPE_MODE (integer_type_node));
137a2a7b 4762 conversion_insns = get_insns ();
621061f4 4763 did_conversion = 1;
137a2a7b
DE
4764 end_sequence ();
4765 }
4766#endif /* FUNCTION_ARG_CALLEE_COPIES */
74bd77a8 4767
6f086dfc 4768 /* In any case, record the parm's desired stack location
718fe406 4769 in case we later discover it must live in the stack.
14aceb29
RS
4770
4771 If it is a COMPLEX value, store the stack location for both
4772 halves. */
4773
4774 if (GET_CODE (parmreg) == CONCAT)
4775 regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
4776 else
4777 regno = REGNO (parmreg);
4778
e9a25f70 4779 if (regno >= max_parm_reg)
6f086dfc
RS
4780 {
4781 rtx *new;
e9a25f70 4782 int old_max_parm_reg = max_parm_reg;
14aceb29 4783
e9a25f70
JL
4784 /* It's slow to expand this one register at a time,
4785 but it's also rare and we need max_parm_reg to be
4786 precisely correct. */
4787 max_parm_reg = regno + 1;
e2ecd91c
BS
4788 new = (rtx *) xrealloc (parm_reg_stack_loc,
4789 max_parm_reg * sizeof (rtx));
e9a25f70
JL
4790 bzero ((char *) (new + old_max_parm_reg),
4791 (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
6f086dfc
RS
4792 parm_reg_stack_loc = new;
4793 }
14aceb29
RS
4794
4795 if (GET_CODE (parmreg) == CONCAT)
4796 {
4797 enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
4798
a03caf76
RK
4799 regnor = REGNO (gen_realpart (submode, parmreg));
4800 regnoi = REGNO (gen_imagpart (submode, parmreg));
4801
7b1a0c14
RS
4802 if (stack_parm != 0)
4803 {
a03caf76 4804 parm_reg_stack_loc[regnor]
3d329b07 4805 = gen_realpart (submode, stack_parm);
a03caf76 4806 parm_reg_stack_loc[regnoi]
3d329b07 4807 = gen_imagpart (submode, stack_parm);
7b1a0c14
RS
4808 }
4809 else
4810 {
a03caf76
RK
4811 parm_reg_stack_loc[regnor] = 0;
4812 parm_reg_stack_loc[regnoi] = 0;
7b1a0c14 4813 }
14aceb29
RS
4814 }
4815 else
4816 parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
6f086dfc
RS
4817
4818 /* Mark the register as eliminable if we did no conversion
4819 and it was copied from memory at a fixed offset,
4820 and the arg pointer was not copied to a pseudo-reg.
4821 If the arg pointer is a pseudo reg or the offset formed
4822 an invalid address, such memory-equivalences
4823 as we make here would screw up life analysis for it. */
4824 if (nominal_mode == passed_mode
621061f4 4825 && ! did_conversion
38b610ed
ILT
4826 && stack_parm != 0
4827 && GET_CODE (stack_parm) == MEM
6f086dfc
RS
4828 && stack_offset.var == 0
4829 && reg_mentioned_p (virtual_incoming_args_rtx,
38b610ed 4830 XEXP (stack_parm, 0)))
a03caf76
RK
4831 {
4832 rtx linsn = get_last_insn ();
69685820 4833 rtx sinsn, set;
a03caf76
RK
4834
4835 /* Mark complex types separately. */
4836 if (GET_CODE (parmreg) == CONCAT)
69685820
RK
4837 /* Scan backwards for the set of the real and
4838 imaginary parts. */
4839 for (sinsn = linsn; sinsn != 0;
4840 sinsn = prev_nonnote_insn (sinsn))
4841 {
4842 set = single_set (sinsn);
4843 if (set != 0
4844 && SET_DEST (set) == regno_reg_rtx [regnoi])
4845 REG_NOTES (sinsn)
38a448ca
RH
4846 = gen_rtx_EXPR_LIST (REG_EQUIV,
4847 parm_reg_stack_loc[regnoi],
4848 REG_NOTES (sinsn));
69685820
RK
4849 else if (set != 0
4850 && SET_DEST (set) == regno_reg_rtx [regnor])
4851 REG_NOTES (sinsn)
38a448ca
RH
4852 = gen_rtx_EXPR_LIST (REG_EQUIV,
4853 parm_reg_stack_loc[regnor],
4854 REG_NOTES (sinsn));
69685820
RK
4855 }
4856 else if ((set = single_set (linsn)) != 0
4857 && SET_DEST (set) == parmreg)
718fe406 4858 REG_NOTES (linsn)
38a448ca
RH
4859 = gen_rtx_EXPR_LIST (REG_EQUIV,
4860 stack_parm, REG_NOTES (linsn));
a03caf76 4861 }
6f086dfc
RS
4862
4863 /* For pointer data type, suggest pointer register. */
e5e809f4 4864 if (POINTER_TYPE_P (TREE_TYPE (parm)))
6c6166bd 4865 mark_reg_pointer (parmreg,
bdb429a5
RK
4866 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4867
6f086dfc
RS
4868 }
4869 else
4870 {
4871 /* Value must be stored in the stack slot STACK_PARM
4872 during function execution. */
4873
621061f4 4874 if (promoted_mode != nominal_mode)
86f8eff3
RK
4875 {
4876 /* Conversion is required. */
3412b298
JW
4877 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4878
4879 emit_move_insn (tempreg, validize_mem (entry_parm));
86f8eff3 4880
3412b298
JW
4881 push_to_sequence (conversion_insns);
4882 entry_parm = convert_to_mode (nominal_mode, tempreg,
a53e14c0 4883 TREE_UNSIGNED (TREE_TYPE (parm)));
de957303
DE
4884 if (stack_parm)
4885 {
4886 /* ??? This may need a big-endian conversion on sparc64. */
4887 stack_parm = change_address (stack_parm, nominal_mode,
4888 NULL_RTX);
4889 }
3412b298 4890 conversion_insns = get_insns ();
621061f4 4891 did_conversion = 1;
3412b298 4892 end_sequence ();
86f8eff3 4893 }
6f086dfc
RS
4894
4895 if (entry_parm != stack_parm)
4896 {
4897 if (stack_parm == 0)
7e41ffa2
RS
4898 {
4899 stack_parm
4900 = assign_stack_local (GET_MODE (entry_parm),
4901 GET_MODE_SIZE (GET_MODE (entry_parm)), 0);
3bdf5ad1 4902 set_mem_attributes (stack_parm, parm, 1);
7e41ffa2
RS
4903 }
4904
621061f4 4905 if (promoted_mode != nominal_mode)
3412b298
JW
4906 {
4907 push_to_sequence (conversion_insns);
4908 emit_move_insn (validize_mem (stack_parm),
4909 validize_mem (entry_parm));
4910 conversion_insns = get_insns ();
4911 end_sequence ();
4912 }
4913 else
4914 emit_move_insn (validize_mem (stack_parm),
4915 validize_mem (entry_parm));
6f086dfc 4916 }
7d384cc0 4917 if (current_function_check_memory_usage)
86fa911a
RK
4918 {
4919 push_to_sequence (conversion_insns);
ebb1b59a
BS
4920 emit_library_call (chkr_set_right_libfunc, LCT_CONST_MAKE_BLOCK,
4921 VOIDmode, 3, XEXP (stack_parm, 0), Pmode,
718fe406 4922 GEN_INT (GET_MODE_SIZE (GET_MODE
86fa911a
RK
4923 (entry_parm))),
4924 TYPE_MODE (sizetype),
956d6950
JL
4925 GEN_INT (MEMORY_USE_RW),
4926 TYPE_MODE (integer_type_node));
6f086dfc 4927
86fa911a
RK
4928 conversion_insns = get_insns ();
4929 end_sequence ();
4930 }
6f086dfc
RS
4931 DECL_RTL (parm) = stack_parm;
4932 }
718fe406 4933
6f086dfc
RS
4934 /* If this "parameter" was the place where we are receiving the
4935 function's incoming structure pointer, set up the result. */
4936 if (parm == function_result_decl)
ccdecf58
RK
4937 {
4938 tree result = DECL_RESULT (fndecl);
ccdecf58
RK
4939
4940 DECL_RTL (result)
38a448ca 4941 = gen_rtx_MEM (DECL_MODE (result), DECL_RTL (parm));
ccdecf58 4942
3bdf5ad1 4943 set_mem_attributes (DECL_RTL (result), result, 1);
ccdecf58 4944 }
6f086dfc
RS
4945 }
4946
3412b298
JW
4947 /* Output all parameter conversion instructions (possibly including calls)
4948 now that all parameters have been copied out of hard registers. */
4949 emit_insns (conversion_insns);
4950
6f086dfc
RS
4951 last_parm_insn = get_last_insn ();
4952
4953 current_function_args_size = stack_args_size.constant;
4954
4955 /* Adjust function incoming argument size for alignment and
4956 minimum length. */
4957
4958#ifdef REG_PARM_STACK_SPACE
6f90e075 4959#ifndef MAYBE_REG_PARM_STACK_SPACE
6f086dfc
RS
4960 current_function_args_size = MAX (current_function_args_size,
4961 REG_PARM_STACK_SPACE (fndecl));
4962#endif
6f90e075 4963#endif
6f086dfc 4964
4433e339
RH
4965#ifdef STACK_BOUNDARY
4966#define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
4967
4968 current_function_args_size
4969 = ((current_function_args_size + STACK_BYTES - 1)
4970 / STACK_BYTES) * STACK_BYTES;
718fe406 4971#endif
4433e339 4972
6f086dfc
RS
4973#ifdef ARGS_GROW_DOWNWARD
4974 current_function_arg_offset_rtx
5f4f0e22 4975 = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
718fe406
KH
4976 : expand_expr (size_diffop (stack_args_size.var,
4977 size_int (-stack_args_size.constant)),
86fa911a 4978 NULL_RTX, VOIDmode, EXPAND_MEMORY_USE_BAD));
6f086dfc
RS
4979#else
4980 current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
4981#endif
4982
4983 /* See how many bytes, if any, of its args a function should try to pop
4984 on return. */
4985
64e6d9cc 4986 current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
6f086dfc
RS
4987 current_function_args_size);
4988
3b69d50e
RK
4989 /* For stdarg.h function, save info about
4990 regs and stack space used by the named args. */
6f086dfc 4991
3b69d50e 4992 if (!hide_last_arg)
6f086dfc
RS
4993 current_function_args_info = args_so_far;
4994
4995 /* Set the rtx used for the function return value. Put this in its
4996 own variable so any optimizers that need this information don't have
4997 to include tree.h. Do this here so it gets done when an inlined
4998 function gets output. */
4999
5000 current_function_return_rtx = DECL_RTL (DECL_RESULT (fndecl));
5001}
5002\f
75dc3319
RK
5003/* Indicate whether REGNO is an incoming argument to the current function
5004 that was promoted to a wider mode. If so, return the RTX for the
5005 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
5006 that REGNO is promoted from and whether the promotion was signed or
5007 unsigned. */
5008
5009#ifdef PROMOTE_FUNCTION_ARGS
5010
5011rtx
5012promoted_input_arg (regno, pmode, punsignedp)
770ae6cc 5013 unsigned int regno;
75dc3319
RK
5014 enum machine_mode *pmode;
5015 int *punsignedp;
5016{
5017 tree arg;
5018
5019 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
5020 arg = TREE_CHAIN (arg))
5021 if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
621061f4
RK
5022 && REGNO (DECL_INCOMING_RTL (arg)) == regno
5023 && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
75dc3319
RK
5024 {
5025 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
5026 int unsignedp = TREE_UNSIGNED (TREE_TYPE (arg));
5027
a5a52dbc 5028 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
75dc3319
RK
5029 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
5030 && mode != DECL_MODE (arg))
5031 {
5032 *pmode = DECL_MODE (arg);
5033 *punsignedp = unsignedp;
5034 return DECL_INCOMING_RTL (arg);
5035 }
5036 }
5037
5038 return 0;
5039}
5040
5041#endif
5042\f
6f086dfc
RS
5043/* Compute the size and offset from the start of the stacked arguments for a
5044 parm passed in mode PASSED_MODE and with type TYPE.
5045
5046 INITIAL_OFFSET_PTR points to the current offset into the stacked
5047 arguments.
5048
5049 The starting offset and size for this parm are returned in *OFFSET_PTR
5050 and *ARG_SIZE_PTR, respectively.
5051
5052 IN_REGS is non-zero if the argument will be passed in registers. It will
5053 never be set if REG_PARM_STACK_SPACE is not defined.
5054
5055 FNDECL is the function in which the argument was defined.
5056
5057 There are two types of rounding that are done. The first, controlled by
5058 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
5059 list to be aligned to the specific boundary (in bits). This rounding
5060 affects the initial and starting offsets, but not the argument size.
5061
5062 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
5063 optionally rounds the size of the parm to PARM_BOUNDARY. The
5064 initial offset is not affected by this rounding, while the size always
5065 is and the starting offset may be. */
5066
718fe406 5067/* offset_ptr will be negative for ARGS_GROW_DOWNWARD case;
6f086dfc
RS
5068 initial_offset_ptr is positive because locate_and_pad_parm's
5069 callers pass in the total size of args so far as
5070 initial_offset_ptr. arg_size_ptr is always positive.*/
5071
6f086dfc
RS
5072void
5073locate_and_pad_parm (passed_mode, type, in_regs, fndecl,
4fc026cd 5074 initial_offset_ptr, offset_ptr, arg_size_ptr,
718fe406 5075 alignment_pad)
6f086dfc
RS
5076 enum machine_mode passed_mode;
5077 tree type;
57bed152 5078 int in_regs ATTRIBUTE_UNUSED;
91813b28 5079 tree fndecl ATTRIBUTE_UNUSED;
6f086dfc
RS
5080 struct args_size *initial_offset_ptr;
5081 struct args_size *offset_ptr;
5082 struct args_size *arg_size_ptr;
4fc026cd
CM
5083 struct args_size *alignment_pad;
5084
6f086dfc
RS
5085{
5086 tree sizetree
5087 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
5088 enum direction where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
5089 int boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
6f086dfc
RS
5090
5091#ifdef REG_PARM_STACK_SPACE
5092 /* If we have found a stack parm before we reach the end of the
5093 area reserved for registers, skip that area. */
5094 if (! in_regs)
5095 {
29a82058
JL
5096 int reg_parm_stack_space = 0;
5097
29008b51
JW
5098#ifdef MAYBE_REG_PARM_STACK_SPACE
5099 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
5100#else
6f086dfc 5101 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
29008b51 5102#endif
6f086dfc
RS
5103 if (reg_parm_stack_space > 0)
5104 {
5105 if (initial_offset_ptr->var)
5106 {
5107 initial_offset_ptr->var
5108 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
fed3cef0 5109 ssize_int (reg_parm_stack_space));
6f086dfc
RS
5110 initial_offset_ptr->constant = 0;
5111 }
5112 else if (initial_offset_ptr->constant < reg_parm_stack_space)
5113 initial_offset_ptr->constant = reg_parm_stack_space;
5114 }
5115 }
5116#endif /* REG_PARM_STACK_SPACE */
5117
5118 arg_size_ptr->var = 0;
5119 arg_size_ptr->constant = 0;
5120
5121#ifdef ARGS_GROW_DOWNWARD
5122 if (initial_offset_ptr->var)
5123 {
5124 offset_ptr->constant = 0;
fed3cef0 5125 offset_ptr->var = size_binop (MINUS_EXPR, ssize_int (0),
6f086dfc
RS
5126 initial_offset_ptr->var);
5127 }
5128 else
5129 {
718fe406 5130 offset_ptr->constant = -initial_offset_ptr->constant;
6f086dfc
RS
5131 offset_ptr->var = 0;
5132 }
0b21dcf5 5133 if (where_pad != none
6f086dfc
RS
5134 && (TREE_CODE (sizetree) != INTEGER_CST
5135 || ((TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)))
5136 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5137 SUB_PARM_SIZE (*offset_ptr, sizetree);
66bcbe19 5138 if (where_pad != downward)
4fc026cd 5139 pad_to_arg_alignment (offset_ptr, boundary, alignment_pad);
6f086dfc 5140 if (initial_offset_ptr->var)
fed3cef0
RK
5141 arg_size_ptr->var = size_binop (MINUS_EXPR,
5142 size_binop (MINUS_EXPR,
5143 ssize_int (0),
5144 initial_offset_ptr->var),
5145 offset_ptr->var);
5146
6f086dfc 5147 else
718fe406
KH
5148 arg_size_ptr->constant = (-initial_offset_ptr->constant
5149 - offset_ptr->constant);
fed3cef0 5150
6f086dfc 5151#else /* !ARGS_GROW_DOWNWARD */
4fc026cd 5152 pad_to_arg_alignment (initial_offset_ptr, boundary, alignment_pad);
6f086dfc 5153 *offset_ptr = *initial_offset_ptr;
6f086dfc
RS
5154
5155#ifdef PUSH_ROUNDING
5156 if (passed_mode != BLKmode)
5157 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
5158#endif
5159
d4b0a7a0
DE
5160 /* Pad_below needs the pre-rounded size to know how much to pad below
5161 so this must be done before rounding up. */
ea5917da
DE
5162 if (where_pad == downward
5163 /* However, BLKmode args passed in regs have their padding done elsewhere.
5164 The stack slot must be able to hold the entire register. */
5165 && !(in_regs && passed_mode == BLKmode))
d4b0a7a0
DE
5166 pad_below (offset_ptr, passed_mode, sizetree);
5167
6f086dfc
RS
5168 if (where_pad != none
5169 && (TREE_CODE (sizetree) != INTEGER_CST
5170 || ((TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)))
5171 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5172
5173 ADD_PARM_SIZE (*arg_size_ptr, sizetree);
5174#endif /* ARGS_GROW_DOWNWARD */
5175}
5176
e16c591a
RS
5177/* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
5178 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
5179
6f086dfc 5180static void
4fc026cd 5181pad_to_arg_alignment (offset_ptr, boundary, alignment_pad)
6f086dfc
RS
5182 struct args_size *offset_ptr;
5183 int boundary;
4fc026cd 5184 struct args_size *alignment_pad;
6f086dfc 5185{
a544cfd2
KG
5186 tree save_var = NULL_TREE;
5187 HOST_WIDE_INT save_constant = 0;
4fc026cd 5188
6f086dfc 5189 int boundary_in_bytes = boundary / BITS_PER_UNIT;
718fe406 5190
9399d5c6 5191 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
4fc026cd
CM
5192 {
5193 save_var = offset_ptr->var;
5194 save_constant = offset_ptr->constant;
5195 }
5196
5197 alignment_pad->var = NULL_TREE;
5198 alignment_pad->constant = 0;
4fc026cd 5199
6f086dfc
RS
5200 if (boundary > BITS_PER_UNIT)
5201 {
5202 if (offset_ptr->var)
5203 {
718fe406 5204 offset_ptr->var =
6f086dfc 5205#ifdef ARGS_GROW_DOWNWARD
718fe406 5206 round_down
6f086dfc
RS
5207#else
5208 round_up
5209#endif
5210 (ARGS_SIZE_TREE (*offset_ptr),
5211 boundary / BITS_PER_UNIT);
5212 offset_ptr->constant = 0; /*?*/
9399d5c6 5213 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
fed3cef0
RK
5214 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
5215 save_var);
6f086dfc
RS
5216 }
5217 else
718fe406 5218 {
fbb57b2a 5219 offset_ptr->constant =
6f086dfc 5220#ifdef ARGS_GROW_DOWNWARD
fbb57b2a 5221 FLOOR_ROUND (offset_ptr->constant, boundary_in_bytes);
6f086dfc 5222#else
fbb57b2a 5223 CEIL_ROUND (offset_ptr->constant, boundary_in_bytes);
6f086dfc 5224#endif
718fe406
KH
5225 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5226 alignment_pad->constant = offset_ptr->constant - save_constant;
5227 }
6f086dfc
RS
5228 }
5229}
5230
51723711 5231#ifndef ARGS_GROW_DOWNWARD
6f086dfc
RS
5232static void
5233pad_below (offset_ptr, passed_mode, sizetree)
5234 struct args_size *offset_ptr;
5235 enum machine_mode passed_mode;
5236 tree sizetree;
5237{
5238 if (passed_mode != BLKmode)
5239 {
5240 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
5241 offset_ptr->constant
5242 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
5243 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
5244 - GET_MODE_SIZE (passed_mode));
5245 }
5246 else
5247 {
5248 if (TREE_CODE (sizetree) != INTEGER_CST
5249 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
5250 {
5251 /* Round the size up to multiple of PARM_BOUNDARY bits. */
5252 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5253 /* Add it in. */
5254 ADD_PARM_SIZE (*offset_ptr, s2);
5255 SUB_PARM_SIZE (*offset_ptr, sizetree);
5256 }
5257 }
5258}
51723711 5259#endif
6f086dfc
RS
5260\f
5261/* Walk the tree of blocks describing the binding levels within a function
5262 and warn about uninitialized variables.
5263 This is done after calling flow_analysis and before global_alloc
5264 clobbers the pseudo-regs to hard regs. */
5265
5266void
5267uninitialized_vars_warning (block)
5268 tree block;
5269{
5270 register tree decl, sub;
5271 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5272 {
8fbe1035
ML
5273 if (warn_uninitialized
5274 && TREE_CODE (decl) == VAR_DECL
6f086dfc
RS
5275 /* These warnings are unreliable for and aggregates
5276 because assigning the fields one by one can fail to convince
5277 flow.c that the entire aggregate was initialized.
5278 Unions are troublesome because members may be shorter. */
05e3bdb9 5279 && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
6f086dfc
RS
5280 && DECL_RTL (decl) != 0
5281 && GET_CODE (DECL_RTL (decl)) == REG
6acdd0fd
JL
5282 /* Global optimizations can make it difficult to determine if a
5283 particular variable has been initialized. However, a VAR_DECL
5284 with a nonzero DECL_INITIAL had an initializer, so do not
5285 claim it is potentially uninitialized.
5286
5287 We do not care about the actual value in DECL_INITIAL, so we do
5288 not worry that it may be a dangling pointer. */
5289 && DECL_INITIAL (decl) == NULL_TREE
6f086dfc
RS
5290 && regno_uninitialized (REGNO (DECL_RTL (decl))))
5291 warning_with_decl (decl,
3c8cd8bd 5292 "`%s' might be used uninitialized in this function");
8fbe1035
ML
5293 if (extra_warnings
5294 && TREE_CODE (decl) == VAR_DECL
6f086dfc
RS
5295 && DECL_RTL (decl) != 0
5296 && GET_CODE (DECL_RTL (decl)) == REG
5297 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5298 warning_with_decl (decl,
3c8cd8bd 5299 "variable `%s' might be clobbered by `longjmp' or `vfork'");
6f086dfc
RS
5300 }
5301 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5302 uninitialized_vars_warning (sub);
5303}
5304
5305/* Do the appropriate part of uninitialized_vars_warning
5306 but for arguments instead of local variables. */
5307
5308void
0cd6ef35 5309setjmp_args_warning ()
6f086dfc
RS
5310{
5311 register tree decl;
5312 for (decl = DECL_ARGUMENTS (current_function_decl);
5313 decl; decl = TREE_CHAIN (decl))
5314 if (DECL_RTL (decl) != 0
5315 && GET_CODE (DECL_RTL (decl)) == REG
5316 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
718fe406
KH
5317 warning_with_decl (decl,
5318 "argument `%s' might be clobbered by `longjmp' or `vfork'");
6f086dfc
RS
5319}
5320
5321/* If this function call setjmp, put all vars into the stack
5322 unless they were declared `register'. */
5323
5324void
5325setjmp_protect (block)
5326 tree block;
5327{
5328 register tree decl, sub;
5329 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5330 if ((TREE_CODE (decl) == VAR_DECL
5331 || TREE_CODE (decl) == PARM_DECL)
5332 && DECL_RTL (decl) != 0
e9a25f70
JL
5333 && (GET_CODE (DECL_RTL (decl)) == REG
5334 || (GET_CODE (DECL_RTL (decl)) == MEM
5335 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
b335c2cc 5336 /* If this variable came from an inline function, it must be
9ec36da5 5337 that its life doesn't overlap the setjmp. If there was a
b335c2cc
TW
5338 setjmp in the function, it would already be in memory. We
5339 must exclude such variable because their DECL_RTL might be
5340 set to strange things such as virtual_stack_vars_rtx. */
5341 && ! DECL_FROM_INLINE (decl)
6f086dfc
RS
5342 && (
5343#ifdef NON_SAVING_SETJMP
5344 /* If longjmp doesn't restore the registers,
5345 don't put anything in them. */
5346 NON_SAVING_SETJMP
5347 ||
5348#endif
a82ad570 5349 ! DECL_REGISTER (decl)))
6f086dfc
RS
5350 put_var_into_stack (decl);
5351 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5352 setjmp_protect (sub);
5353}
5354\f
5355/* Like the previous function, but for args instead of local variables. */
5356
5357void
5358setjmp_protect_args ()
5359{
29a82058 5360 register tree decl;
6f086dfc
RS
5361 for (decl = DECL_ARGUMENTS (current_function_decl);
5362 decl; decl = TREE_CHAIN (decl))
5363 if ((TREE_CODE (decl) == VAR_DECL
5364 || TREE_CODE (decl) == PARM_DECL)
5365 && DECL_RTL (decl) != 0
e9a25f70
JL
5366 && (GET_CODE (DECL_RTL (decl)) == REG
5367 || (GET_CODE (DECL_RTL (decl)) == MEM
5368 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
6f086dfc
RS
5369 && (
5370 /* If longjmp doesn't restore the registers,
5371 don't put anything in them. */
5372#ifdef NON_SAVING_SETJMP
5373 NON_SAVING_SETJMP
5374 ||
5375#endif
a82ad570 5376 ! DECL_REGISTER (decl)))
6f086dfc
RS
5377 put_var_into_stack (decl);
5378}
5379\f
5380/* Return the context-pointer register corresponding to DECL,
5381 or 0 if it does not need one. */
5382
5383rtx
5384lookup_static_chain (decl)
5385 tree decl;
5386{
b001a02f
PB
5387 tree context = decl_function_context (decl);
5388 tree link;
7ad8c4bf 5389
38ee6ed9
JM
5390 if (context == 0
5391 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_NO_STATIC_CHAIN (decl)))
7ad8c4bf 5392 return 0;
38ee6ed9 5393
6f086dfc
RS
5394 /* We treat inline_function_decl as an alias for the current function
5395 because that is the inline function whose vars, types, etc.
5396 are being merged into the current function.
5397 See expand_inline_function. */
5398 if (context == current_function_decl || context == inline_function_decl)
5399 return virtual_stack_vars_rtx;
5400
5401 for (link = context_display; link; link = TREE_CHAIN (link))
5402 if (TREE_PURPOSE (link) == context)
5403 return RTL_EXPR_RTL (TREE_VALUE (link));
5404
5405 abort ();
5406}
5407\f
5408/* Convert a stack slot address ADDR for variable VAR
5409 (from a containing function)
5410 into an address valid in this function (using a static chain). */
5411
5412rtx
5413fix_lexical_addr (addr, var)
5414 rtx addr;
5415 tree var;
5416{
5417 rtx basereg;
e5e809f4 5418 HOST_WIDE_INT displacement;
6f086dfc
RS
5419 tree context = decl_function_context (var);
5420 struct function *fp;
5421 rtx base = 0;
5422
5423 /* If this is the present function, we need not do anything. */
5424 if (context == current_function_decl || context == inline_function_decl)
5425 return addr;
5426
5427 for (fp = outer_function_chain; fp; fp = fp->next)
5428 if (fp->decl == context)
5429 break;
5430
5431 if (fp == 0)
5432 abort ();
5433
e9a25f70
JL
5434 if (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == MEM)
5435 addr = XEXP (XEXP (addr, 0), 0);
5436
6f086dfc
RS
5437 /* Decode given address as base reg plus displacement. */
5438 if (GET_CODE (addr) == REG)
5439 basereg = addr, displacement = 0;
5440 else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
5441 basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
5442 else
5443 abort ();
5444
5445 /* We accept vars reached via the containing function's
5446 incoming arg pointer and via its stack variables pointer. */
5447 if (basereg == fp->internal_arg_pointer)
5448 {
5449 /* If reached via arg pointer, get the arg pointer value
5450 out of that function's stack frame.
5451
5452 There are two cases: If a separate ap is needed, allocate a
5453 slot in the outer function for it and dereference it that way.
5454 This is correct even if the real ap is actually a pseudo.
5455 Otherwise, just adjust the offset from the frame pointer to
5456 compensate. */
5457
5458#ifdef NEED_SEPARATE_AP
5459 rtx addr;
5460
49ad7cfa
BS
5461 if (fp->x_arg_pointer_save_area == 0)
5462 fp->x_arg_pointer_save_area
e2ecd91c 5463 = assign_stack_local_1 (Pmode, GET_MODE_SIZE (Pmode), 0, fp);
6f086dfc 5464
49ad7cfa 5465 addr = fix_lexical_addr (XEXP (fp->x_arg_pointer_save_area, 0), var);
6f086dfc
RS
5466 addr = memory_address (Pmode, addr);
5467
3bdf5ad1
RK
5468 base = gen_rtx_MEM (Pmode, addr);
5469 MEM_ALIAS_SET (base) = get_frame_alias_set ();
5470 base = copy_to_reg (base);
6f086dfc
RS
5471#else
5472 displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
86f8eff3 5473 base = lookup_static_chain (var);
6f086dfc
RS
5474#endif
5475 }
5476
5477 else if (basereg == virtual_stack_vars_rtx)
5478 {
5479 /* This is the same code as lookup_static_chain, duplicated here to
5480 avoid an extra call to decl_function_context. */
5481 tree link;
5482
5483 for (link = context_display; link; link = TREE_CHAIN (link))
5484 if (TREE_PURPOSE (link) == context)
5485 {
5486 base = RTL_EXPR_RTL (TREE_VALUE (link));
5487 break;
5488 }
5489 }
5490
5491 if (base == 0)
5492 abort ();
5493
5494 /* Use same offset, relative to appropriate static chain or argument
5495 pointer. */
5496 return plus_constant (base, displacement);
5497}
5498\f
5499/* Return the address of the trampoline for entering nested fn FUNCTION.
5500 If necessary, allocate a trampoline (in the stack frame)
5501 and emit rtl to initialize its contents (at entry to this function). */
5502
5503rtx
5504trampoline_address (function)
5505 tree function;
5506{
5507 tree link;
5508 tree rtlexp;
5509 rtx tramp;
5510 struct function *fp;
5511 tree fn_context;
5512
5513 /* Find an existing trampoline and return it. */
5514 for (link = trampoline_list; link; link = TREE_CHAIN (link))
5515 if (TREE_PURPOSE (link) == function)
e87ee2a9
RK
5516 return
5517 round_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
5518
6f086dfc 5519 for (fp = outer_function_chain; fp; fp = fp->next)
49ad7cfa 5520 for (link = fp->x_trampoline_list; link; link = TREE_CHAIN (link))
6f086dfc
RS
5521 if (TREE_PURPOSE (link) == function)
5522 {
5523 tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
5524 function);
5525 return round_trampoline_addr (tramp);
5526 }
5527
5528 /* None exists; we must make one. */
5529
5530 /* Find the `struct function' for the function containing FUNCTION. */
5531 fp = 0;
5532 fn_context = decl_function_context (function);
4ac74fb8
RK
5533 if (fn_context != current_function_decl
5534 && fn_context != inline_function_decl)
6f086dfc
RS
5535 for (fp = outer_function_chain; fp; fp = fp->next)
5536 if (fp->decl == fn_context)
5537 break;
5538
5539 /* Allocate run-time space for this trampoline
5540 (usually in the defining function's stack frame). */
5541#ifdef ALLOCATE_TRAMPOLINE
5542 tramp = ALLOCATE_TRAMPOLINE (fp);
5543#else
5544 /* If rounding needed, allocate extra space
5545 to ensure we have TRAMPOLINE_SIZE bytes left after rounding up. */
5546#ifdef TRAMPOLINE_ALIGNMENT
b02ab63a
RK
5547#define TRAMPOLINE_REAL_SIZE \
5548 (TRAMPOLINE_SIZE + (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT) - 1)
6f086dfc
RS
5549#else
5550#define TRAMPOLINE_REAL_SIZE (TRAMPOLINE_SIZE)
5551#endif
e2ecd91c 5552 tramp = assign_stack_local_1 (BLKmode, TRAMPOLINE_REAL_SIZE, 0,
01d939e8 5553 fp ? fp : cfun);
6f086dfc
RS
5554#endif
5555
5556 /* Record the trampoline for reuse and note it for later initialization
5557 by expand_function_end. */
5558 if (fp != 0)
5559 {
6f086dfc
RS
5560 rtlexp = make_node (RTL_EXPR);
5561 RTL_EXPR_RTL (rtlexp) = tramp;
49ad7cfa
BS
5562 fp->x_trampoline_list = tree_cons (function, rtlexp,
5563 fp->x_trampoline_list);
6f086dfc
RS
5564 }
5565 else
5566 {
5567 /* Make the RTL_EXPR node temporary, not momentary, so that the
5568 trampoline_list doesn't become garbage. */
6f086dfc 5569 rtlexp = make_node (RTL_EXPR);
6f086dfc
RS
5570
5571 RTL_EXPR_RTL (rtlexp) = tramp;
5572 trampoline_list = tree_cons (function, rtlexp, trampoline_list);
5573 }
5574
5575 tramp = fix_lexical_addr (XEXP (tramp, 0), function);
5576 return round_trampoline_addr (tramp);
5577}
5578
5579/* Given a trampoline address,
5580 round it to multiple of TRAMPOLINE_ALIGNMENT. */
5581
5582static rtx
5583round_trampoline_addr (tramp)
5584 rtx tramp;
5585{
5586#ifdef TRAMPOLINE_ALIGNMENT
5587 /* Round address up to desired boundary. */
5588 rtx temp = gen_reg_rtx (Pmode);
5589 temp = expand_binop (Pmode, add_optab, tramp,
b02ab63a 5590 GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1),
6f086dfc
RS
5591 temp, 0, OPTAB_LIB_WIDEN);
5592 tramp = expand_binop (Pmode, and_optab, temp,
718fe406 5593 GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT),
6f086dfc
RS
5594 temp, 0, OPTAB_LIB_WIDEN);
5595#endif
5596 return tramp;
5597}
5598\f
b2a59b15
MS
5599/* Put all this function's BLOCK nodes including those that are chained
5600 onto the first block into a vector, and return it.
467456d0
RS
5601 Also store in each NOTE for the beginning or end of a block
5602 the index of that block in the vector.
b2a59b15 5603 The arguments are BLOCK, the chain of top-level blocks of the function,
467456d0
RS
5604 and INSNS, the insn chain of the function. */
5605
1a4450c7 5606void
116eebd6 5607identify_blocks ()
467456d0 5608{
fc289cd1 5609 int n_blocks;
0a1c58a2 5610 tree *block_vector, *last_block_vector;
1a4450c7 5611 tree *block_stack;
116eebd6 5612 tree block = DECL_INITIAL (current_function_decl);
467456d0 5613
b2a59b15 5614 if (block == 0)
1a4450c7 5615 return;
fc289cd1 5616
1a4450c7
MM
5617 /* Fill the BLOCK_VECTOR with all of the BLOCKs in this function, in
5618 depth-first order. */
18c038b9 5619 block_vector = get_block_vector (block, &n_blocks);
4da896b2 5620 block_stack = (tree *) xmalloc (n_blocks * sizeof (tree));
1a4450c7 5621
718fe406 5622 last_block_vector = identify_blocks_1 (get_insns (),
116eebd6 5623 block_vector + 1,
718fe406 5624 block_vector + n_blocks,
116eebd6 5625 block_stack);
0a1c58a2
JL
5626
5627 /* If we didn't use all of the subblocks, we've misplaced block notes. */
5628 /* ??? This appears to happen all the time. Latent bugs elsewhere? */
5629 if (0 && last_block_vector != block_vector + n_blocks)
5630 abort ();
5631
5632 free (block_vector);
5633 free (block_stack);
5634}
5635
5636/* Subroutine of identify_blocks. Do the block substitution on the
5637 insn chain beginning with INSNS. Recurse for CALL_PLACEHOLDER chains.
5638
5639 BLOCK_STACK is pushed and popped for each BLOCK_BEGIN/BLOCK_END pair.
5640 BLOCK_VECTOR is incremented for each block seen. */
5641
5642static tree *
5643identify_blocks_1 (insns, block_vector, end_block_vector, orig_block_stack)
5644 rtx insns;
5645 tree *block_vector;
5646 tree *end_block_vector;
5647 tree *orig_block_stack;
5648{
5649 rtx insn;
5650 tree *block_stack = orig_block_stack;
5651
467456d0 5652 for (insn = insns; insn; insn = NEXT_INSN (insn))
0a1c58a2
JL
5653 {
5654 if (GET_CODE (insn) == NOTE)
5655 {
5656 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5657 {
5658 tree b;
1a4450c7 5659
0a1c58a2
JL
5660 /* If there are more block notes than BLOCKs, something
5661 is badly wrong. */
5662 if (block_vector == end_block_vector)
5663 abort ();
e6fd097e 5664
0a1c58a2
JL
5665 b = *block_vector++;
5666 NOTE_BLOCK (insn) = b;
5667 *block_stack++ = b;
5668 }
5669 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5670 {
5671 /* If there are more NOTE_INSN_BLOCK_ENDs than
5672 NOTE_INSN_BLOCK_BEGs, something is badly wrong. */
5673 if (block_stack == orig_block_stack)
5674 abort ();
e6fd097e 5675
0a1c58a2
JL
5676 NOTE_BLOCK (insn) = *--block_stack;
5677 }
718fe406 5678 }
0a1c58a2
JL
5679 else if (GET_CODE (insn) == CALL_INSN
5680 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5681 {
5682 rtx cp = PATTERN (insn);
5683
718fe406
KH
5684 block_vector = identify_blocks_1 (XEXP (cp, 0), block_vector,
5685 end_block_vector, block_stack);
0a1c58a2
JL
5686 if (XEXP (cp, 1))
5687 block_vector = identify_blocks_1 (XEXP (cp, 1), block_vector,
5688 end_block_vector, block_stack);
5689 if (XEXP (cp, 2))
5690 block_vector = identify_blocks_1 (XEXP (cp, 2), block_vector,
5691 end_block_vector, block_stack);
5692 }
5693 }
467456d0 5694
0a1c58a2
JL
5695 /* If there are more NOTE_INSN_BLOCK_BEGINs than NOTE_INSN_BLOCK_ENDs,
5696 something is badly wrong. */
5697 if (block_stack != orig_block_stack)
5698 abort ();
5699
5700 return block_vector;
467456d0
RS
5701}
5702
116eebd6 5703/* Identify BLOCKs referenced by more than one
718fe406 5704 NOTE_INSN_BLOCK_{BEG,END}, and create duplicate blocks. */
467456d0 5705
116eebd6
MM
5706void
5707reorder_blocks ()
467456d0 5708{
116eebd6 5709 tree block = DECL_INITIAL (current_function_decl);
18c038b9 5710 varray_type block_stack;
467456d0 5711
1a4450c7 5712 if (block == NULL_TREE)
116eebd6 5713 return;
fc289cd1 5714
18c038b9
MM
5715 VARRAY_TREE_INIT (block_stack, 10, "block_stack");
5716
116eebd6
MM
5717 /* Prune the old trees away, so that they don't get in the way. */
5718 BLOCK_SUBBLOCKS (block) = NULL_TREE;
5719 BLOCK_CHAIN (block) = NULL_TREE;
fc289cd1 5720
116eebd6 5721 reorder_blocks_1 (get_insns (), block, &block_stack);
467456d0 5722
718fe406 5723 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
18c038b9
MM
5724
5725 VARRAY_FREE (block_stack);
467456d0
RS
5726}
5727
0a1c58a2
JL
5728/* Helper function for reorder_blocks. Process the insn chain beginning
5729 at INSNS. Recurse for CALL_PLACEHOLDER insns. */
5730
5731static void
5732reorder_blocks_1 (insns, current_block, p_block_stack)
5733 rtx insns;
5734 tree current_block;
5735 varray_type *p_block_stack;
5736{
5737 rtx insn;
5738
5739 for (insn = insns; insn; insn = NEXT_INSN (insn))
5740 {
5741 if (GET_CODE (insn) == NOTE)
5742 {
5743 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5744 {
5745 tree block = NOTE_BLOCK (insn);
5746 /* If we have seen this block before, copy it. */
5747 if (TREE_ASM_WRITTEN (block))
5748 {
5749 block = copy_node (block);
5750 NOTE_BLOCK (insn) = block;
5751 }
5752 BLOCK_SUBBLOCKS (block) = 0;
5753 TREE_ASM_WRITTEN (block) = 1;
718fe406 5754 BLOCK_SUPERCONTEXT (block) = current_block;
0a1c58a2
JL
5755 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
5756 BLOCK_SUBBLOCKS (current_block) = block;
5757 current_block = block;
5758 VARRAY_PUSH_TREE (*p_block_stack, block);
5759 }
5760 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5761 {
5762 NOTE_BLOCK (insn) = VARRAY_TOP_TREE (*p_block_stack);
5763 VARRAY_POP (*p_block_stack);
5764 BLOCK_SUBBLOCKS (current_block)
5765 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
5766 current_block = BLOCK_SUPERCONTEXT (current_block);
5767 }
5768 }
5769 else if (GET_CODE (insn) == CALL_INSN
5770 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5771 {
5772 rtx cp = PATTERN (insn);
5773 reorder_blocks_1 (XEXP (cp, 0), current_block, p_block_stack);
5774 if (XEXP (cp, 1))
5775 reorder_blocks_1 (XEXP (cp, 1), current_block, p_block_stack);
5776 if (XEXP (cp, 2))
5777 reorder_blocks_1 (XEXP (cp, 2), current_block, p_block_stack);
5778 }
5779 }
5780}
5781
467456d0
RS
5782/* Reverse the order of elements in the chain T of blocks,
5783 and return the new head of the chain (old last element). */
5784
5785static tree
5786blocks_nreverse (t)
5787 tree t;
5788{
5789 register tree prev = 0, decl, next;
5790 for (decl = t; decl; decl = next)
5791 {
5792 next = BLOCK_CHAIN (decl);
5793 BLOCK_CHAIN (decl) = prev;
5794 prev = decl;
5795 }
5796 return prev;
5797}
5798
18c038b9
MM
5799/* Count the subblocks of the list starting with BLOCK. If VECTOR is
5800 non-NULL, list them all into VECTOR, in a depth-first preorder
5801 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
b2a59b15 5802 blocks. */
467456d0
RS
5803
5804static int
5805all_blocks (block, vector)
5806 tree block;
5807 tree *vector;
5808{
b2a59b15
MS
5809 int n_blocks = 0;
5810
5811 while (block)
5812 {
5813 TREE_ASM_WRITTEN (block) = 0;
5814
5815 /* Record this block. */
5816 if (vector)
5817 vector[n_blocks] = block;
5818
5819 ++n_blocks;
718fe406 5820
b2a59b15
MS
5821 /* Record the subblocks, and their subblocks... */
5822 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
5823 vector ? vector + n_blocks : 0);
5824 block = BLOCK_CHAIN (block);
5825 }
467456d0
RS
5826
5827 return n_blocks;
5828}
18c038b9
MM
5829
5830/* Return a vector containing all the blocks rooted at BLOCK. The
5831 number of elements in the vector is stored in N_BLOCKS_P. The
5832 vector is dynamically allocated; it is the caller's responsibility
5833 to call `free' on the pointer returned. */
718fe406 5834
18c038b9
MM
5835static tree *
5836get_block_vector (block, n_blocks_p)
5837 tree block;
5838 int *n_blocks_p;
5839{
5840 tree *block_vector;
5841
5842 *n_blocks_p = all_blocks (block, NULL);
5843 block_vector = (tree *) xmalloc (*n_blocks_p * sizeof (tree));
5844 all_blocks (block, block_vector);
5845
5846 return block_vector;
5847}
5848
5849static int next_block_index = 2;
5850
5851/* Set BLOCK_NUMBER for all the blocks in FN. */
5852
5853void
5854number_blocks (fn)
5855 tree fn;
5856{
5857 int i;
5858 int n_blocks;
5859 tree *block_vector;
5860
5861 /* For SDB and XCOFF debugging output, we start numbering the blocks
5862 from 1 within each function, rather than keeping a running
5863 count. */
5864#if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
b0e3a658
RK
5865 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
5866 next_block_index = 1;
18c038b9
MM
5867#endif
5868
5869 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
5870
5871 /* The top-level BLOCK isn't numbered at all. */
5872 for (i = 1; i < n_blocks; ++i)
5873 /* We number the blocks from two. */
5874 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
5875
5876 free (block_vector);
5877
5878 return;
5879}
467456d0 5880\f
b384405b
BS
5881/* Allocate a function structure and reset its contents to the defaults. */
5882static void
5883prepare_function_start ()
6f086dfc 5884{
01d939e8 5885 cfun = (struct function *) xcalloc (1, sizeof (struct function));
e2ecd91c 5886
6f086dfc 5887 init_stmt_for_function ();
fa51b01b 5888 init_eh_for_function ();
6f086dfc
RS
5889
5890 cse_not_expected = ! optimize;
5891
5892 /* Caller save not needed yet. */
5893 caller_save_needed = 0;
5894
5895 /* No stack slots have been made yet. */
5896 stack_slot_list = 0;
5897
b384405b
BS
5898 current_function_has_nonlocal_label = 0;
5899 current_function_has_nonlocal_goto = 0;
5900
6f086dfc 5901 /* There is no stack slot for handling nonlocal gotos. */
ba716ac9 5902 nonlocal_goto_handler_slots = 0;
6f086dfc
RS
5903 nonlocal_goto_stack_level = 0;
5904
5905 /* No labels have been declared for nonlocal use. */
5906 nonlocal_labels = 0;
e881bb1b 5907 nonlocal_goto_handler_labels = 0;
6f086dfc
RS
5908
5909 /* No function calls so far in this function. */
5910 function_call_count = 0;
5911
5912 /* No parm regs have been allocated.
5913 (This is important for output_inline_function.) */
5914 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
5915
5916 /* Initialize the RTL mechanism. */
5917 init_emit ();
5918
5919 /* Initialize the queue of pending postincrement and postdecrements,
5920 and some other info in expr.c. */
5921 init_expr ();
718fe406 5922
6f086dfc
RS
5923 /* We haven't done register allocation yet. */
5924 reg_renumber = 0;
5925
01d939e8 5926 init_varasm_status (cfun);
6f086dfc 5927
e2ecd91c 5928 /* Clear out data used for inlining. */
01d939e8
BS
5929 cfun->inlinable = 0;
5930 cfun->original_decl_initial = 0;
718fe406 5931 cfun->original_arg_vector = 0;
e2ecd91c 5932
c2f8b491 5933#ifdef STACK_BOUNDARY
c487e484 5934 cfun->stack_alignment_needed = STACK_BOUNDARY;
c2f8b491 5935 cfun->preferred_stack_boundary = STACK_BOUNDARY;
c487e484
RE
5936#else
5937 cfun->stack_alignment_needed = 0;
0a1c58a2 5938 cfun->preferred_stack_boundary = 0;
c2f8b491 5939#endif
a0871656 5940
6f086dfc
RS
5941 /* Set if a call to setjmp is seen. */
5942 current_function_calls_setjmp = 0;
5943
5944 /* Set if a call to longjmp is seen. */
5945 current_function_calls_longjmp = 0;
5946
5947 current_function_calls_alloca = 0;
6f086dfc 5948 current_function_contains_functions = 0;
54ff41b7 5949 current_function_is_leaf = 0;
fb13d4d0 5950 current_function_nothrow = 0;
fdb8a883 5951 current_function_sp_is_unchanging = 0;
54ff41b7 5952 current_function_uses_only_leaf_regs = 0;
acd693d1 5953 current_function_has_computed_jump = 0;
173cd503 5954 current_function_is_thunk = 0;
6f086dfc
RS
5955
5956 current_function_returns_pcc_struct = 0;
5957 current_function_returns_struct = 0;
5958 current_function_epilogue_delay_list = 0;
5959 current_function_uses_const_pool = 0;
5960 current_function_uses_pic_offset_table = 0;
aeb302bb 5961 current_function_cannot_inline = 0;
6f086dfc
RS
5962
5963 /* We have not yet needed to make a label to jump to for tail-recursion. */
5964 tail_recursion_label = 0;
5965
5966 /* We haven't had a need to make a save area for ap yet. */
6f086dfc
RS
5967 arg_pointer_save_area = 0;
5968
5969 /* No stack slots allocated yet. */
5970 frame_offset = 0;
5971
5972 /* No SAVE_EXPRs in this function yet. */
5973 save_expr_regs = 0;
5974
5975 /* No RTL_EXPRs in this function yet. */
5976 rtl_expr_chain = 0;
5977
bc0ebdf9
RK
5978 /* Set up to allocate temporaries. */
5979 init_temp_slots ();
6f086dfc 5980
b384405b
BS
5981 /* Indicate that we need to distinguish between the return value of the
5982 present function and the return value of a function being called. */
5983 rtx_equal_function_value_matters = 1;
5984
5985 /* Indicate that we have not instantiated virtual registers yet. */
5986 virtuals_instantiated = 0;
5987
1b3d8f8a
GK
5988 /* Indicate that we want CONCATs now. */
5989 generating_concat_p = 1;
5990
b384405b
BS
5991 /* Indicate we have no need of a frame pointer yet. */
5992 frame_pointer_needed = 0;
5993
5994 /* By default assume not varargs or stdarg. */
5995 current_function_varargs = 0;
5996 current_function_stdarg = 0;
6f086dfc 5997
d9a98e1a
RK
5998 /* We haven't made any trampolines for this function yet. */
5999 trampoline_list = 0;
6000
6f086dfc
RS
6001 init_pending_stack_adjust ();
6002 inhibit_defer_pop = 0;
6003
6004 current_function_outgoing_args_size = 0;
36edd3cc 6005
0a8a198c 6006 if (init_lang_status)
01d939e8 6007 (*init_lang_status) (cfun);
36edd3cc 6008 if (init_machine_status)
01d939e8 6009 (*init_machine_status) (cfun);
b384405b
BS
6010}
6011
6012/* Initialize the rtl expansion mechanism so that we can do simple things
6013 like generate sequences. This is used to provide a context during global
6014 initialization of some passes. */
6015void
6016init_dummy_function_start ()
6017{
6018 prepare_function_start ();
6019}
6020
6021/* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
6022 and initialize static variables for generating RTL for the statements
6023 of the function. */
6024
6025void
6026init_function_start (subr, filename, line)
6027 tree subr;
36244024 6028 const char *filename;
b384405b
BS
6029 int line;
6030{
6031 prepare_function_start ();
6032
6033 /* Remember this function for later. */
01d939e8
BS
6034 cfun->next_global = all_functions;
6035 all_functions = cfun;
718fe406 6036
b384405b 6037 current_function_name = (*decl_printable_name) (subr, 2);
01d939e8 6038 cfun->decl = subr;
b384405b
BS
6039
6040 /* Nonzero if this is a nested function that uses a static chain. */
6041
6042 current_function_needs_context
6043 = (decl_function_context (current_function_decl) != 0
6044 && ! DECL_NO_STATIC_CHAIN (current_function_decl));
6045
6046 /* Within function body, compute a type's size as soon it is laid out. */
6047 immediate_size_expand++;
6f086dfc 6048
6f086dfc 6049 /* Prevent ever trying to delete the first instruction of a function.
b274104c 6050 Also tell final how to output a linenum before the function prologue.
718fe406 6051 Note linenums could be missing, e.g. when compiling a Java .class file. */
b274104c
PB
6052 if (line > 0)
6053 emit_line_note (filename, line);
6f086dfc
RS
6054
6055 /* Make sure first insn is a note even if we don't want linenums.
6056 This makes sure the first insn will never be deleted.
6057 Also, final expects a note to appear there. */
5f4f0e22 6058 emit_note (NULL_PTR, NOTE_INSN_DELETED);
6f086dfc
RS
6059
6060 /* Set flags used by final.c. */
6061 if (aggregate_value_p (DECL_RESULT (subr)))
6062 {
6063#ifdef PCC_STATIC_STRUCT_RETURN
1b8297c1 6064 current_function_returns_pcc_struct = 1;
6f086dfc 6065#endif
1b8297c1 6066 current_function_returns_struct = 1;
6f086dfc
RS
6067 }
6068
6069 /* Warn if this value is an aggregate type,
6070 regardless of which calling convention we are using for it. */
6071 if (warn_aggregate_return
05e3bdb9 6072 && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
6f086dfc
RS
6073 warning ("function returns an aggregate");
6074
6075 current_function_returns_pointer
8eda074c 6076 = POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
49ad7cfa 6077}
5c7675e9 6078
49ad7cfa
BS
6079/* Make sure all values used by the optimization passes have sane
6080 defaults. */
6081void
6082init_function_for_compilation ()
6083{
6084 reg_renumber = 0;
0a1c58a2 6085
5c7675e9 6086 /* No prologue/epilogue insns yet. */
0a1c58a2
JL
6087 VARRAY_GROW (prologue, 0);
6088 VARRAY_GROW (epilogue, 0);
6089 VARRAY_GROW (sibcall_epilogue, 0);
6f086dfc
RS
6090}
6091
6092/* Indicate that the current function uses extra args
6093 not explicitly mentioned in the argument list in any fashion. */
6094
6095void
6096mark_varargs ()
6097{
6098 current_function_varargs = 1;
6099}
6100
6101/* Expand a call to __main at the beginning of a possible main function. */
6102
e2fd1d94
JM
6103#if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
6104#undef HAS_INIT_SECTION
6105#define HAS_INIT_SECTION
6106#endif
6107
6f086dfc
RS
6108void
6109expand_main_function ()
6110{
e2fd1d94 6111#if !defined (HAS_INIT_SECTION)
b93a436e
JL
6112 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, NAME__MAIN), 0,
6113 VOIDmode, 0);
e2fd1d94 6114#endif /* not HAS_INIT_SECTION */
6f086dfc
RS
6115}
6116\f
c20bf1f3
JB
6117extern struct obstack permanent_obstack;
6118
6f086dfc
RS
6119/* Start the RTL for a new function, and set variables used for
6120 emitting RTL.
6121 SUBR is the FUNCTION_DECL node.
6122 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
6123 the function's parameters, which must be run at any return statement. */
6124
6125void
6126expand_function_start (subr, parms_have_cleanups)
6127 tree subr;
6128 int parms_have_cleanups;
6129{
6f086dfc 6130 tree tem;
4e86caed 6131 rtx last_ptr = NULL_RTX;
6f086dfc
RS
6132
6133 /* Make sure volatile mem refs aren't considered
6134 valid operands of arithmetic insns. */
6135 init_recog_no_volatile ();
6136
7d384cc0
KR
6137 /* Set this before generating any memory accesses. */
6138 current_function_check_memory_usage
6139 = (flag_check_memory_usage
6140 && ! DECL_NO_CHECK_MEMORY_USAGE (current_function_decl));
6141
07417085
KR
6142 current_function_instrument_entry_exit
6143 = (flag_instrument_function_entry_exit
6144 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6145
a157febd
GK
6146 current_function_limit_stack
6147 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
6148
6f086dfc
RS
6149 /* If function gets a static chain arg, store it in the stack frame.
6150 Do this first, so it gets the first stack slot offset. */
6151 if (current_function_needs_context)
3e2481e9
JW
6152 {
6153 last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
f0c51a1e 6154
f0c51a1e
RK
6155 /* Delay copying static chain if it is not a register to avoid
6156 conflicts with regs used for parameters. */
f95182a4
ILT
6157 if (! SMALL_REGISTER_CLASSES
6158 || GET_CODE (static_chain_incoming_rtx) == REG)
718fe406 6159 emit_move_insn (last_ptr, static_chain_incoming_rtx);
3e2481e9 6160 }
6f086dfc
RS
6161
6162 /* If the parameters of this function need cleaning up, get a label
6163 for the beginning of the code which executes those cleanups. This must
6164 be done before doing anything with return_label. */
6165 if (parms_have_cleanups)
6166 cleanup_label = gen_label_rtx ();
6167 else
6168 cleanup_label = 0;
6169
6170 /* Make the label for return statements to jump to, if this machine
6171 does not have a one-instruction return and uses an epilogue,
6172 or if it returns a structure, or if it has parm cleanups. */
6173#ifdef HAVE_return
6174 if (cleanup_label == 0 && HAVE_return
07417085 6175 && ! current_function_instrument_entry_exit
6f086dfc
RS
6176 && ! current_function_returns_pcc_struct
6177 && ! (current_function_returns_struct && ! optimize))
6178 return_label = 0;
6179 else
6180 return_label = gen_label_rtx ();
6181#else
6182 return_label = gen_label_rtx ();
6183#endif
6184
6185 /* Initialize rtx used to return the value. */
6186 /* Do this before assign_parms so that we copy the struct value address
6187 before any library calls that assign parms might generate. */
6188
6189 /* Decide whether to return the value in memory or in a register. */
6190 if (aggregate_value_p (DECL_RESULT (subr)))
6191 {
6192 /* Returning something that won't go in a register. */
4acc00bf 6193 register rtx value_address = 0;
6f086dfc
RS
6194
6195#ifdef PCC_STATIC_STRUCT_RETURN
6196 if (current_function_returns_pcc_struct)
6197 {
6198 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
6199 value_address = assemble_static_space (size);
6200 }
6201 else
6202#endif
6203 {
6204 /* Expect to be passed the address of a place to store the value.
6205 If it is passed as an argument, assign_parms will take care of
6206 it. */
6207 if (struct_value_incoming_rtx)
6208 {
6209 value_address = gen_reg_rtx (Pmode);
6210 emit_move_insn (value_address, struct_value_incoming_rtx);
6211 }
6212 }
6213 if (value_address)
ccdecf58
RK
6214 {
6215 DECL_RTL (DECL_RESULT (subr))
38a448ca 6216 = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), value_address);
3bdf5ad1
RK
6217 set_mem_attributes (DECL_RTL (DECL_RESULT (subr)),
6218 DECL_RESULT (subr), 1);
ccdecf58 6219 }
6f086dfc
RS
6220 }
6221 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
6222 /* If return mode is void, this decl rtl should not be used. */
6223 DECL_RTL (DECL_RESULT (subr)) = 0;
07417085 6224 else if (parms_have_cleanups || current_function_instrument_entry_exit)
a53e14c0
RK
6225 {
6226 /* If function will end with cleanup code for parms,
6227 compute the return values into a pseudo reg,
6228 which we will copy into the true return register
6229 after the cleanups are done. */
6230
6231 enum machine_mode mode = DECL_MODE (DECL_RESULT (subr));
a5a52dbc 6232
a53e14c0
RK
6233#ifdef PROMOTE_FUNCTION_RETURN
6234 tree type = TREE_TYPE (DECL_RESULT (subr));
6235 int unsignedp = TREE_UNSIGNED (type);
6236
a5a52dbc 6237 mode = promote_mode (type, mode, &unsignedp, 1);
a53e14c0
RK
6238#endif
6239
6240 DECL_RTL (DECL_RESULT (subr)) = gen_reg_rtx (mode);
6241 }
6f086dfc
RS
6242 else
6243 /* Scalar, returned in a register. */
6244 {
6f086dfc 6245 DECL_RTL (DECL_RESULT (subr))
ab36bd3e 6246 = hard_function_value (TREE_TYPE (DECL_RESULT (subr)), subr, 1);
6f086dfc
RS
6247
6248 /* Mark this reg as the function's return value. */
6249 if (GET_CODE (DECL_RTL (DECL_RESULT (subr))) == REG)
6250 {
6251 REG_FUNCTION_VALUE_P (DECL_RTL (DECL_RESULT (subr))) = 1;
6252 /* Needed because we may need to move this to memory
6253 in case it's a named return value whose address is taken. */
a82ad570 6254 DECL_REGISTER (DECL_RESULT (subr)) = 1;
6f086dfc
RS
6255 }
6256 }
6257
6258 /* Initialize rtx for parameters and local variables.
6259 In some cases this requires emitting insns. */
6260
0d1416c6 6261 assign_parms (subr);
6f086dfc 6262
f0c51a1e
RK
6263 /* Copy the static chain now if it wasn't a register. The delay is to
6264 avoid conflicts with the parameter passing registers. */
6265
f95182a4 6266 if (SMALL_REGISTER_CLASSES && current_function_needs_context)
f0c51a1e
RK
6267 if (GET_CODE (static_chain_incoming_rtx) != REG)
6268 emit_move_insn (last_ptr, static_chain_incoming_rtx);
f0c51a1e 6269
6f086dfc
RS
6270 /* The following was moved from init_function_start.
6271 The move is supposed to make sdb output more accurate. */
6272 /* Indicate the beginning of the function body,
6273 as opposed to parm setup. */
5f4f0e22 6274 emit_note (NULL_PTR, NOTE_INSN_FUNCTION_BEG);
6f086dfc 6275
6f086dfc 6276 if (GET_CODE (get_last_insn ()) != NOTE)
5f4f0e22 6277 emit_note (NULL_PTR, NOTE_INSN_DELETED);
6f086dfc
RS
6278 parm_birth_insn = get_last_insn ();
6279
6d7306f7
JM
6280 context_display = 0;
6281 if (current_function_needs_context)
ac9e20f0 6282 {
6d7306f7
JM
6283 /* Fetch static chain values for containing functions. */
6284 tem = decl_function_context (current_function_decl);
d29c259b
RH
6285 /* Copy the static chain pointer into a pseudo. If we have
6286 small register classes, copy the value from memory if
6287 static_chain_incoming_rtx is a REG. */
6288 if (tem)
6d7306f7 6289 {
6d7306f7
JM
6290 /* If the static chain originally came in a register, put it back
6291 there, then move it out in the next insn. The reason for
6292 this peculiar code is to satisfy function integration. */
f95182a4
ILT
6293 if (SMALL_REGISTER_CLASSES
6294 && GET_CODE (static_chain_incoming_rtx) == REG)
6d7306f7 6295 emit_move_insn (static_chain_incoming_rtx, last_ptr);
6d7306f7
JM
6296 last_ptr = copy_to_reg (static_chain_incoming_rtx);
6297 }
ac9e20f0 6298
6d7306f7
JM
6299 while (tem)
6300 {
6301 tree rtlexp = make_node (RTL_EXPR);
6f086dfc 6302
6d7306f7
JM
6303 RTL_EXPR_RTL (rtlexp) = last_ptr;
6304 context_display = tree_cons (tem, rtlexp, context_display);
6305 tem = decl_function_context (tem);
6306 if (tem == 0)
6307 break;
6308 /* Chain thru stack frames, assuming pointer to next lexical frame
6309 is found at the place we always store it. */
6f086dfc 6310#ifdef FRAME_GROWS_DOWNWARD
718fe406 6311 last_ptr = plus_constant (last_ptr, -GET_MODE_SIZE (Pmode));
6f086dfc 6312#endif
3bdf5ad1
RK
6313 last_ptr = gen_rtx_MEM (Pmode, memory_address (Pmode, last_ptr));
6314 MEM_ALIAS_SET (last_ptr) = get_frame_alias_set ();
6315 last_ptr = copy_to_reg (last_ptr);
6d7306f7
JM
6316
6317 /* If we are not optimizing, ensure that we know that this
6318 piece of context is live over the entire function. */
6319 if (! optimize)
38a448ca
RH
6320 save_expr_regs = gen_rtx_EXPR_LIST (VOIDmode, last_ptr,
6321 save_expr_regs);
6d7306f7 6322 }
6f086dfc
RS
6323 }
6324
07417085
KR
6325 if (current_function_instrument_entry_exit)
6326 {
6327 rtx fun = DECL_RTL (current_function_decl);
6328 if (GET_CODE (fun) == MEM)
6329 fun = XEXP (fun, 0);
6330 else
6331 abort ();
6332 emit_library_call (profile_function_entry_libfunc, 0, VOIDmode, 2,
6333 fun, Pmode,
6334 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6335 0,
6336 hard_frame_pointer_rtx),
6337 Pmode);
6338 }
6339
6f086dfc
RS
6340 /* After the display initializations is where the tail-recursion label
6341 should go, if we end up needing one. Ensure we have a NOTE here
6342 since some things (like trampolines) get placed before this. */
5f4f0e22 6343 tail_recursion_reentry = emit_note (NULL_PTR, NOTE_INSN_DELETED);
6f086dfc
RS
6344
6345 /* Evaluate now the sizes of any types declared among the arguments. */
6346 for (tem = nreverse (get_pending_sizes ()); tem; tem = TREE_CHAIN (tem))
7b05e286 6347 {
86fa911a
RK
6348 expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode,
6349 EXPAND_MEMORY_USE_BAD);
7b05e286
JW
6350 /* Flush the queue in case this parameter declaration has
6351 side-effects. */
6352 emit_queue ();
6353 }
6f086dfc
RS
6354
6355 /* Make sure there is a line number after the function entry setup code. */
6356 force_next_line_note ();
6357}
6358\f
49ad7cfa
BS
6359/* Undo the effects of init_dummy_function_start. */
6360void
6361expand_dummy_function_end ()
6362{
6363 /* End any sequences that failed to be closed due to syntax errors. */
6364 while (in_sequence_p ())
6365 end_sequence ();
6366
6367 /* Outside function body, can't compute type's actual size
6368 until next function's body starts. */
fa51b01b 6369
01d939e8
BS
6370 free_after_parsing (cfun);
6371 free_after_compilation (cfun);
6372 free (cfun);
6373 cfun = 0;
49ad7cfa
BS
6374}
6375
c13fde05
RH
6376/* Call DOIT for each hard register used as a return value from
6377 the current function. */
bd695e1e
RH
6378
6379void
c13fde05
RH
6380diddle_return_value (doit, arg)
6381 void (*doit) PARAMS ((rtx, void *));
6382 void *arg;
bd695e1e 6383{
c13fde05 6384 rtx outgoing = current_function_return_rtx;
5828d725 6385 int pcc;
c13fde05
RH
6386
6387 if (! outgoing)
6388 return;
bd695e1e 6389
5828d725 6390 pcc = (current_function_returns_struct
718fe406 6391 || current_function_returns_pcc_struct);
5828d725
RH
6392
6393 if ((GET_CODE (outgoing) == REG
6394 && REGNO (outgoing) >= FIRST_PSEUDO_REGISTER)
6395 || pcc)
bd695e1e 6396 {
c13fde05 6397 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5828d725
RH
6398
6399 /* A PCC-style return returns a pointer to the memory in which
6400 the structure is stored. */
6401 if (pcc)
6402 type = build_pointer_type (type);
6403
c13fde05
RH
6404#ifdef FUNCTION_OUTGOING_VALUE
6405 outgoing = FUNCTION_OUTGOING_VALUE (type, current_function_decl);
6406#else
6407 outgoing = FUNCTION_VALUE (type, current_function_decl);
6408#endif
6409 /* If this is a BLKmode structure being returned in registers, then use
6410 the mode computed in expand_return. */
6411 if (GET_MODE (outgoing) == BLKmode)
3e4eac3f 6412 PUT_MODE (outgoing, GET_MODE (current_function_return_rtx));
5828d725 6413 REG_FUNCTION_VALUE_P (outgoing) = 1;
c13fde05 6414 }
bd695e1e 6415
c13fde05
RH
6416 if (GET_CODE (outgoing) == REG)
6417 (*doit) (outgoing, arg);
6418 else if (GET_CODE (outgoing) == PARALLEL)
6419 {
6420 int i;
bd695e1e 6421
c13fde05
RH
6422 for (i = 0; i < XVECLEN (outgoing, 0); i++)
6423 {
6424 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
6425
6426 if (GET_CODE (x) == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
6427 (*doit) (x, arg);
bd695e1e
RH
6428 }
6429 }
6430}
6431
c13fde05
RH
6432static void
6433do_clobber_return_reg (reg, arg)
6434 rtx reg;
6435 void *arg ATTRIBUTE_UNUSED;
6436{
6437 emit_insn (gen_rtx_CLOBBER (VOIDmode, reg));
6438}
6439
6440void
6441clobber_return_register ()
6442{
6443 diddle_return_value (do_clobber_return_reg, NULL);
6444}
6445
6446static void
6447do_use_return_reg (reg, arg)
6448 rtx reg;
6449 void *arg ATTRIBUTE_UNUSED;
6450{
6451 emit_insn (gen_rtx_USE (VOIDmode, reg));
6452}
6453
6454void
6455use_return_register ()
6456{
6457 diddle_return_value (do_use_return_reg, NULL);
6458}
6459
6f086dfc 6460/* Generate RTL for the end of the current function.
718fe406 6461 FILENAME and LINE are the current position in the source file.
6f086dfc 6462
980697fd 6463 It is up to language-specific callers to do cleanups for parameters--
1be07046 6464 or else, supply 1 for END_BINDINGS and we will call expand_end_bindings. */
6f086dfc
RS
6465
6466void
1be07046 6467expand_function_end (filename, line, end_bindings)
3b304f5b 6468 const char *filename;
6f086dfc 6469 int line;
1be07046 6470 int end_bindings;
6f086dfc 6471{
6f086dfc
RS
6472 tree link;
6473
1e2414db 6474#ifdef TRAMPOLINE_TEMPLATE
6f086dfc 6475 static rtx initial_trampoline;
1e2414db 6476#endif
6f086dfc 6477
49ad7cfa
BS
6478 finish_expr_for_function ();
6479
6f086dfc
RS
6480#ifdef NON_SAVING_SETJMP
6481 /* Don't put any variables in registers if we call setjmp
6482 on a machine that fails to restore the registers. */
6483 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
6484 {
b88a3142
RK
6485 if (DECL_INITIAL (current_function_decl) != error_mark_node)
6486 setjmp_protect (DECL_INITIAL (current_function_decl));
6487
6f086dfc
RS
6488 setjmp_protect_args ();
6489 }
6490#endif
6491
6492 /* Save the argument pointer if a save area was made for it. */
6493 if (arg_pointer_save_area)
6494 {
ea0f9a85
JW
6495 /* arg_pointer_save_area may not be a valid memory address, so we
6496 have to check it and fix it if necessary. */
6497 rtx seq;
6498 start_sequence ();
6499 emit_move_insn (validize_mem (arg_pointer_save_area),
6500 virtual_incoming_args_rtx);
6501 seq = gen_sequence ();
6502 end_sequence ();
6503 emit_insn_before (seq, tail_recursion_reentry);
6f086dfc
RS
6504 }
6505
6506 /* Initialize any trampolines required by this function. */
6507 for (link = trampoline_list; link; link = TREE_CHAIN (link))
6508 {
6509 tree function = TREE_PURPOSE (link);
57bed152 6510 rtx context ATTRIBUTE_UNUSED = lookup_static_chain (function);
6f086dfc 6511 rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
7a87758d 6512#ifdef TRAMPOLINE_TEMPLATE
1e2414db 6513 rtx blktramp;
7a87758d 6514#endif
6f086dfc
RS
6515 rtx seq;
6516
1e2414db 6517#ifdef TRAMPOLINE_TEMPLATE
6f086dfc
RS
6518 /* First make sure this compilation has a template for
6519 initializing trampolines. */
6520 if (initial_trampoline == 0)
86f8eff3 6521 {
86f8eff3 6522 initial_trampoline
38a448ca 6523 = gen_rtx_MEM (BLKmode, assemble_trampoline_template ());
76095e2f
RH
6524
6525 ggc_add_rtx_root (&initial_trampoline, 1);
86f8eff3 6526 }
1e2414db 6527#endif
6f086dfc
RS
6528
6529 /* Generate insns to initialize the trampoline. */
6530 start_sequence ();
1e2414db
RK
6531 tramp = round_trampoline_addr (XEXP (tramp, 0));
6532#ifdef TRAMPOLINE_TEMPLATE
6533 blktramp = change_address (initial_trampoline, BLKmode, tramp);
6534 emit_block_move (blktramp, initial_trampoline,
6535 GEN_INT (TRAMPOLINE_SIZE),
744bfbfa 6536 TRAMPOLINE_ALIGNMENT);
1e2414db
RK
6537#endif
6538 INITIALIZE_TRAMPOLINE (tramp, XEXP (DECL_RTL (function), 0), context);
6f086dfc
RS
6539 seq = get_insns ();
6540 end_sequence ();
6541
6542 /* Put those insns at entry to the containing function (this one). */
6543 emit_insns_before (seq, tail_recursion_reentry);
6544 }
6f086dfc 6545
11044f66
RK
6546 /* If we are doing stack checking and this function makes calls,
6547 do a stack probe at the start of the function to ensure we have enough
6548 space for another stack frame. */
6549 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
6550 {
6551 rtx insn, seq;
6552
6553 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6554 if (GET_CODE (insn) == CALL_INSN)
6555 {
6556 start_sequence ();
6557 probe_stack_range (STACK_CHECK_PROTECT,
6558 GEN_INT (STACK_CHECK_MAX_FRAME_SIZE));
6559 seq = get_insns ();
6560 end_sequence ();
6561 emit_insns_before (seq, tail_recursion_reentry);
6562 break;
6563 }
6564 }
6565
db8717d9 6566 /* Warn about unused parms if extra warnings were specified. */
078721e1
AC
6567 /* Either ``-W -Wunused'' or ``-Wunused-parameter'' enables this
6568 warning. WARN_UNUSED_PARAMETER is negative when set by
718fe406 6569 -Wunused. */
078721e1
AC
6570 if (warn_unused_parameter > 0
6571 || (warn_unused_parameter < 0 && extra_warnings))
6f086dfc 6572 {
db8717d9 6573 tree decl;
6f086dfc
RS
6574
6575 for (decl = DECL_ARGUMENTS (current_function_decl);
6576 decl; decl = TREE_CHAIN (decl))
497dc802
JM
6577 if (! TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
6578 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
6f086dfc
RS
6579 warning_with_decl (decl, "unused parameter `%s'");
6580 }
6f086dfc
RS
6581
6582 /* Delete handlers for nonlocal gotos if nothing uses them. */
ba716ac9
BS
6583 if (nonlocal_goto_handler_slots != 0
6584 && ! current_function_has_nonlocal_label)
6f086dfc
RS
6585 delete_handlers ();
6586
6587 /* End any sequences that failed to be closed due to syntax errors. */
6588 while (in_sequence_p ())
5f4f0e22 6589 end_sequence ();
6f086dfc
RS
6590
6591 /* Outside function body, can't compute type's actual size
6592 until next function's body starts. */
6593 immediate_size_expand--;
6594
6f086dfc
RS
6595 clear_pending_stack_adjust ();
6596 do_pending_stack_adjust ();
6597
6598 /* Mark the end of the function body.
6599 If control reaches this insn, the function can drop through
6600 without returning a value. */
5f4f0e22 6601 emit_note (NULL_PTR, NOTE_INSN_FUNCTION_END);
6f086dfc 6602
82e415a3
DE
6603 /* Must mark the last line number note in the function, so that the test
6604 coverage code can avoid counting the last line twice. This just tells
6605 the code to ignore the immediately following line note, since there
6606 already exists a copy of this note somewhere above. This line number
6607 note is still needed for debugging though, so we can't delete it. */
6608 if (flag_test_coverage)
b3b42a4d 6609 emit_note (NULL_PTR, NOTE_INSN_REPEATED_LINE_NUMBER);
82e415a3 6610
6f086dfc
RS
6611 /* Output a linenumber for the end of the function.
6612 SDB depends on this. */
6613 emit_line_note_force (filename, line);
6614
6615 /* Output the label for the actual return from the function,
6616 if one is expected. This happens either because a function epilogue
6617 is used instead of a return instruction, or because a return was done
6618 with a goto in order to run local cleanups, or because of pcc-style
6619 structure returning. */
6620
6621 if (return_label)
bd695e1e 6622 {
b313a0fe
RH
6623 rtx before, after;
6624
bd695e1e
RH
6625 /* Before the return label, clobber the return registers so that
6626 they are not propogated live to the rest of the function. This
6627 can only happen with functions that drop through; if there had
6628 been a return statement, there would have either been a return
6629 rtx, or a jump to the return label. */
b313a0fe
RH
6630
6631 before = get_last_insn ();
c13fde05 6632 clobber_return_register ();
b313a0fe
RH
6633 after = get_last_insn ();
6634
6635 if (before != after)
6636 cfun->x_clobber_return_insn = after;
bd695e1e
RH
6637
6638 emit_label (return_label);
6639 }
6f086dfc 6640
1be07046
RS
6641 /* C++ uses this. */
6642 if (end_bindings)
6643 expand_end_bindings (0, 0, 0);
6644
e5a1e0e8
MS
6645 /* Now handle any leftover exception regions that may have been
6646 created for the parameters. */
6647 {
6648 rtx last = get_last_insn ();
6649 rtx label;
6650
6651 expand_leftover_cleanups ();
6652
1e4ceb6f
MM
6653 /* If there are any catch_clauses remaining, output them now. */
6654 emit_insns (catch_clauses);
c14f7160 6655 catch_clauses = catch_clauses_last = NULL_RTX;
e5a1e0e8
MS
6656 /* If the above emitted any code, may sure we jump around it. */
6657 if (last != get_last_insn ())
6658 {
6659 label = gen_label_rtx ();
6660 last = emit_jump_insn_after (gen_jump (label), last);
6661 last = emit_barrier_after (last);
6662 emit_label (label);
6663 }
6664 }
6665
07417085
KR
6666 if (current_function_instrument_entry_exit)
6667 {
6668 rtx fun = DECL_RTL (current_function_decl);
6669 if (GET_CODE (fun) == MEM)
6670 fun = XEXP (fun, 0);
6671 else
6672 abort ();
6673 emit_library_call (profile_function_exit_libfunc, 0, VOIDmode, 2,
6674 fun, Pmode,
6675 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6676 0,
6677 hard_frame_pointer_rtx),
6678 Pmode);
6679 }
6680
6f086dfc
RS
6681 /* If we had calls to alloca, and this machine needs
6682 an accurate stack pointer to exit the function,
6683 insert some code to save and restore the stack pointer. */
6684#ifdef EXIT_IGNORE_STACK
6685 if (! EXIT_IGNORE_STACK)
6686#endif
6687 if (current_function_calls_alloca)
6688 {
59257ff7
RK
6689 rtx tem = 0;
6690
6691 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
5f4f0e22 6692 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
6f086dfc
RS
6693 }
6694
3e4eac3f
RH
6695 /* If scalar return value was computed in a pseudo-reg, or was a named
6696 return value that got dumped to the stack, copy that to the hard
6697 return register. */
6698 if (DECL_RTL (DECL_RESULT (current_function_decl)) != 0)
6f086dfc 6699 {
3e4eac3f
RH
6700 tree decl_result = DECL_RESULT (current_function_decl);
6701 rtx decl_rtl = DECL_RTL (decl_result);
6702
6703 if (REG_P (decl_rtl)
6704 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
6705 : DECL_REGISTER (decl_result))
6706 {
6707 rtx real_decl_rtl;
6f086dfc
RS
6708
6709#ifdef FUNCTION_OUTGOING_VALUE
3e4eac3f
RH
6710 real_decl_rtl = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl_result),
6711 current_function_decl);
6f086dfc 6712#else
3e4eac3f
RH
6713 real_decl_rtl = FUNCTION_VALUE (TREE_TYPE (decl_result),
6714 current_function_decl);
6f086dfc 6715#endif
3e4eac3f
RH
6716 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
6717
6718 /* If this is a BLKmode structure being returned in registers,
6719 then use the mode computed in expand_return. Note that if
6720 decl_rtl is memory, then its mode may have been changed,
6721 but that current_function_return_rtx has not. */
6722 if (GET_MODE (real_decl_rtl) == BLKmode)
6723 PUT_MODE (real_decl_rtl, GET_MODE (current_function_return_rtx));
6724
6725 /* If a named return value dumped decl_return to memory, then
6726 we may need to re-do the PROMOTE_MODE signed/unsigned
6727 extension. */
6728 if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
6729 {
6730 int unsignedp = TREE_UNSIGNED (TREE_TYPE (decl_result));
6731
6732#ifdef PROMOTE_FUNCTION_RETURN
6733 promote_mode (TREE_TYPE (decl_result), GET_MODE (decl_rtl),
6734 &unsignedp, 1);
6735#endif
6736
6737 convert_move (real_decl_rtl, decl_rtl, unsignedp);
6738 }
6739 else
6740 emit_move_insn (real_decl_rtl, decl_rtl);
6741
6742 /* The delay slot scheduler assumes that current_function_return_rtx
6743 holds the hard register containing the return value, not a
6744 temporary pseudo. */
6745 current_function_return_rtx = real_decl_rtl;
6746 }
6f086dfc
RS
6747 }
6748
6749 /* If returning a structure, arrange to return the address of the value
6750 in a place where debuggers expect to find it.
6751
6752 If returning a structure PCC style,
6753 the caller also depends on this value.
6754 And current_function_returns_pcc_struct is not necessarily set. */
6755 if (current_function_returns_struct
6756 || current_function_returns_pcc_struct)
6757 {
718fe406
KH
6758 rtx value_address =
6759 XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6f086dfc
RS
6760 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
6761#ifdef FUNCTION_OUTGOING_VALUE
6762 rtx outgoing
6763 = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
6764 current_function_decl);
6765#else
6766 rtx outgoing
6767 = FUNCTION_VALUE (build_pointer_type (type),
6768 current_function_decl);
6769#endif
6770
6771 /* Mark this as a function return value so integrate will delete the
6772 assignment and USE below when inlining this function. */
6773 REG_FUNCTION_VALUE_P (outgoing) = 1;
6774
6775 emit_move_insn (outgoing, value_address);
6f086dfc
RS
6776 }
6777
c13fde05
RH
6778 /* ??? This should no longer be necessary since stupid is no longer with
6779 us, but there are some parts of the compiler (eg reload_combine, and
6780 sh mach_dep_reorg) that still try and compute their own lifetime info
6781 instead of using the general framework. */
6782 use_return_register ();
6783
718fe406 6784 /* If this is an implementation of __throw, do what's necessary to
71038426
RH
6785 communicate between __builtin_eh_return and the epilogue. */
6786 expand_eh_return ();
6787
6f086dfc
RS
6788 /* Output a return insn if we are using one.
6789 Otherwise, let the rtl chain end here, to drop through
6790 into the epilogue. */
6791
6792#ifdef HAVE_return
6793 if (HAVE_return)
6794 {
6795 emit_jump_insn (gen_return ());
6796 emit_barrier ();
6797 }
6798#endif
6799
6800 /* Fix up any gotos that jumped out to the outermost
6801 binding level of the function.
6802 Must follow emitting RETURN_LABEL. */
6803
6804 /* If you have any cleanups to do at this point,
6805 and they need to create temporary variables,
6806 then you will lose. */
e15679f8 6807 expand_fixups (get_insns ());
6f086dfc 6808}
bdac5f58 6809\f
0a1c58a2
JL
6810/* Extend a vector that records the INSN_UIDs of INSNS (either a
6811 sequence or a single insn). */
bdac5f58 6812
0a1c58a2
JL
6813static void
6814record_insns (insns, vecp)
bdac5f58 6815 rtx insns;
0a1c58a2 6816 varray_type *vecp;
bdac5f58 6817{
bdac5f58
TW
6818 if (GET_CODE (insns) == SEQUENCE)
6819 {
6820 int len = XVECLEN (insns, 0);
0a1c58a2
JL
6821 int i = VARRAY_SIZE (*vecp);
6822
6823 VARRAY_GROW (*vecp, i + len);
bdac5f58 6824 while (--len >= 0)
0a1c58a2
JL
6825 {
6826 VARRAY_INT (*vecp, i) = INSN_UID (XVECEXP (insns, 0, len));
6827 ++i;
6828 }
bdac5f58
TW
6829 }
6830 else
6831 {
0a1c58a2
JL
6832 int i = VARRAY_SIZE (*vecp);
6833 VARRAY_GROW (*vecp, i + 1);
6834 VARRAY_INT (*vecp, i) = INSN_UID (insns);
bdac5f58 6835 }
bdac5f58
TW
6836}
6837
10914065 6838/* Determine how many INSN_UIDs in VEC are part of INSN. */
bdac5f58 6839
10914065 6840static int
bdac5f58
TW
6841contains (insn, vec)
6842 rtx insn;
0a1c58a2 6843 varray_type vec;
bdac5f58
TW
6844{
6845 register int i, j;
6846
6847 if (GET_CODE (insn) == INSN
6848 && GET_CODE (PATTERN (insn)) == SEQUENCE)
6849 {
10914065 6850 int count = 0;
bdac5f58 6851 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
0a1c58a2
JL
6852 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
6853 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == VARRAY_INT (vec, j))
10914065
TW
6854 count++;
6855 return count;
bdac5f58
TW
6856 }
6857 else
6858 {
0a1c58a2
JL
6859 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
6860 if (INSN_UID (insn) == VARRAY_INT (vec, j))
10914065 6861 return 1;
bdac5f58
TW
6862 }
6863 return 0;
6864}
5c7675e9
RH
6865
6866int
6867prologue_epilogue_contains (insn)
6868 rtx insn;
6869{
0a1c58a2 6870 if (contains (insn, prologue))
5c7675e9 6871 return 1;
0a1c58a2 6872 if (contains (insn, epilogue))
5c7675e9
RH
6873 return 1;
6874 return 0;
6875}
bdac5f58 6876
0a1c58a2
JL
6877int
6878sibcall_epilogue_contains (insn)
718fe406 6879 rtx insn;
0a1c58a2
JL
6880{
6881 if (sibcall_epilogue)
6882 return contains (insn, sibcall_epilogue);
6883 return 0;
6884}
6885
73ef99fb 6886#ifdef HAVE_return
69732dcb
RH
6887/* Insert gen_return at the end of block BB. This also means updating
6888 block_for_insn appropriately. */
6889
6890static void
86c82654 6891emit_return_into_block (bb, line_note)
69732dcb 6892 basic_block bb;
86c82654 6893 rtx line_note;
69732dcb
RH
6894{
6895 rtx p, end;
6896
718fe406 6897 p = NEXT_INSN (bb->end);
86c82654
RH
6898 end = emit_jump_insn_after (gen_return (), bb->end);
6899 if (line_note)
6900 emit_line_note_after (NOTE_SOURCE_FILE (line_note),
6901 NOTE_LINE_NUMBER (line_note), bb->end);
6902
69732dcb
RH
6903 while (1)
6904 {
6905 set_block_for_insn (p, bb);
86c82654 6906 if (p == bb->end)
69732dcb 6907 break;
86c82654 6908 p = PREV_INSN (p);
69732dcb
RH
6909 }
6910 bb->end = end;
6911}
73ef99fb 6912#endif /* HAVE_return */
69732dcb 6913
7393c642
RK
6914#ifdef HAVE_epilogue
6915
6916/* Modify SEQ, a SEQUENCE that is part of the epilogue, to no modifications
6917 to the stack pointer. */
6918
6919static void
6920keep_stack_depressed (seq)
6921 rtx seq;
6922{
6923 int i;
6924 rtx sp_from_reg = 0;
6925 int sp_modified_unknown = 0;
6926
6927 /* If the epilogue is just a single instruction, it's OK as is */
6928
6929 if (GET_CODE (seq) != SEQUENCE) return;
6930
6931 /* Scan all insns in SEQ looking for ones that modified the stack
6932 pointer. Record if it modified the stack pointer by copying it
6933 from the frame pointer or if it modified it in some other way.
6934 Then modify any subsequent stack pointer references to take that
6935 into account. We start by only allowing SP to be copied from a
6936 register (presumably FP) and then be subsequently referenced. */
6937
6938 for (i = 0; i < XVECLEN (seq, 0); i++)
6939 {
6940 rtx insn = XVECEXP (seq, 0, i);
6941
6942 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
6943 continue;
6944
6945 if (reg_set_p (stack_pointer_rtx, insn))
6946 {
6947 rtx set = single_set (insn);
6948
6949 /* If SP is set as a side-effect, we can't support this. */
6950 if (set == 0)
6951 abort ();
6952
6953 if (GET_CODE (SET_SRC (set)) == REG)
6954 sp_from_reg = SET_SRC (set);
6955 else
6956 sp_modified_unknown = 1;
6957
6958 /* Don't allow the SP modification to happen. */
6959 PUT_CODE (insn, NOTE);
6960 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
6961 NOTE_SOURCE_FILE (insn) = 0;
6962 }
6963 else if (reg_referenced_p (stack_pointer_rtx, PATTERN (insn)))
6964 {
6965 if (sp_modified_unknown)
6966 abort ();
6967
6968 else if (sp_from_reg != 0)
6969 PATTERN (insn)
6970 = replace_rtx (PATTERN (insn), stack_pointer_rtx, sp_from_reg);
6971 }
6972 }
6973}
6974#endif
6975
9faa82d8 6976/* Generate the prologue and epilogue RTL if the machine supports it. Thread
bdac5f58
TW
6977 this into place with notes indicating where the prologue ends and where
6978 the epilogue begins. Update the basic block information when possible. */
6979
6980void
6981thread_prologue_and_epilogue_insns (f)
54ea1de9 6982 rtx f ATTRIBUTE_UNUSED;
bdac5f58 6983{
ca1117cc 6984 int inserted = 0;
19d3c25c
RH
6985 edge e;
6986 rtx seq;
ca1117cc
RH
6987#ifdef HAVE_prologue
6988 rtx prologue_end = NULL_RTX;
6989#endif
86c82654
RH
6990#if defined (HAVE_epilogue) || defined(HAVE_return)
6991 rtx epilogue_end = NULL_RTX;
6992#endif
e881bb1b 6993
bdac5f58
TW
6994#ifdef HAVE_prologue
6995 if (HAVE_prologue)
6996 {
e881bb1b 6997 start_sequence ();
718fe406 6998 seq = gen_prologue ();
e881bb1b 6999 emit_insn (seq);
bdac5f58
TW
7000
7001 /* Retain a map of the prologue insns. */
e881bb1b
RH
7002 if (GET_CODE (seq) != SEQUENCE)
7003 seq = get_insns ();
0a1c58a2 7004 record_insns (seq, &prologue);
ca1117cc 7005 prologue_end = emit_note (NULL, NOTE_INSN_PROLOGUE_END);
9185a8d5 7006
e881bb1b
RH
7007 seq = gen_sequence ();
7008 end_sequence ();
7009
7010 /* If optimization is off, and perhaps in an empty function,
7011 the entry block will have no successors. */
7012 if (ENTRY_BLOCK_PTR->succ)
7013 {
7014 /* Can't deal with multiple successsors of the entry block. */
7015 if (ENTRY_BLOCK_PTR->succ->succ_next)
7016 abort ();
7017
7018 insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
ca1117cc 7019 inserted = 1;
e881bb1b
RH
7020 }
7021 else
7022 emit_insn_after (seq, f);
bdac5f58 7023 }
bdac5f58 7024#endif
bdac5f58 7025
19d3c25c
RH
7026 /* If the exit block has no non-fake predecessors, we don't need
7027 an epilogue. */
718fe406 7028 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
19d3c25c
RH
7029 if ((e->flags & EDGE_FAKE) == 0)
7030 break;
7031 if (e == NULL)
7032 goto epilogue_done;
7033
69732dcb
RH
7034#ifdef HAVE_return
7035 if (optimize && HAVE_return)
7036 {
7037 /* If we're allowed to generate a simple return instruction,
7038 then by definition we don't need a full epilogue. Examine
718fe406
KH
7039 the block that falls through to EXIT. If it does not
7040 contain any code, examine its predecessors and try to
69732dcb
RH
7041 emit (conditional) return instructions. */
7042
7043 basic_block last;
7044 edge e_next;
7045 rtx label;
7046
718fe406 7047 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
69732dcb
RH
7048 if (e->flags & EDGE_FALLTHRU)
7049 break;
7050 if (e == NULL)
7051 goto epilogue_done;
7052 last = e->src;
7053
7054 /* Verify that there are no active instructions in the last block. */
7055 label = last->end;
7056 while (label && GET_CODE (label) != CODE_LABEL)
7057 {
7058 if (active_insn_p (label))
7059 break;
7060 label = PREV_INSN (label);
7061 }
7062
7063 if (last->head == label && GET_CODE (label) == CODE_LABEL)
7064 {
718fe406 7065 rtx epilogue_line_note = NULL_RTX;
86c82654
RH
7066
7067 /* Locate the line number associated with the closing brace,
7068 if we can find one. */
7069 for (seq = get_last_insn ();
7070 seq && ! active_insn_p (seq);
7071 seq = PREV_INSN (seq))
7072 if (GET_CODE (seq) == NOTE && NOTE_LINE_NUMBER (seq) > 0)
7073 {
7074 epilogue_line_note = seq;
7075 break;
7076 }
7077
718fe406 7078 for (e = last->pred; e; e = e_next)
69732dcb
RH
7079 {
7080 basic_block bb = e->src;
7081 rtx jump;
7082
7083 e_next = e->pred_next;
7084 if (bb == ENTRY_BLOCK_PTR)
7085 continue;
7086
7087 jump = bb->end;
a617c13f 7088 if ((GET_CODE (jump) != JUMP_INSN) || JUMP_LABEL (jump) != label)
69732dcb
RH
7089 continue;
7090
7091 /* If we have an unconditional jump, we can replace that
7092 with a simple return instruction. */
7093 if (simplejump_p (jump))
7094 {
86c82654 7095 emit_return_into_block (bb, epilogue_line_note);
69732dcb
RH
7096 flow_delete_insn (jump);
7097 }
7098
7099 /* If we have a conditional jump, we can try to replace
7100 that with a conditional return instruction. */
7101 else if (condjump_p (jump))
7102 {
7103 rtx ret, *loc;
7104
7105 ret = SET_SRC (PATTERN (jump));
7106 if (GET_CODE (XEXP (ret, 1)) == LABEL_REF)
7107 loc = &XEXP (ret, 1);
7108 else
7109 loc = &XEXP (ret, 2);
7110 ret = gen_rtx_RETURN (VOIDmode);
7111
7112 if (! validate_change (jump, loc, ret, 0))
7113 continue;
7114 if (JUMP_LABEL (jump))
7115 LABEL_NUSES (JUMP_LABEL (jump))--;
718fe406 7116
3a75e42e
CP
7117 /* If this block has only one successor, it both jumps
7118 and falls through to the fallthru block, so we can't
7119 delete the edge. */
718fe406
KH
7120 if (bb->succ->succ_next == NULL)
7121 continue;
69732dcb
RH
7122 }
7123 else
7124 continue;
7125
7126 /* Fix up the CFG for the successful change we just made. */
86c82654 7127 redirect_edge_succ (e, EXIT_BLOCK_PTR);
69732dcb 7128 }
69732dcb 7129
2dd8bc01
GK
7130 /* Emit a return insn for the exit fallthru block. Whether
7131 this is still reachable will be determined later. */
69732dcb 7132
2dd8bc01 7133 emit_barrier_after (last->end);
86c82654
RH
7134 emit_return_into_block (last, epilogue_line_note);
7135 epilogue_end = last->end;
718fe406 7136 goto epilogue_done;
2dd8bc01 7137 }
69732dcb
RH
7138 }
7139#endif
bdac5f58
TW
7140#ifdef HAVE_epilogue
7141 if (HAVE_epilogue)
7142 {
19d3c25c
RH
7143 /* Find the edge that falls through to EXIT. Other edges may exist
7144 due to RETURN instructions, but those don't need epilogues.
7145 There really shouldn't be a mixture -- either all should have
7146 been converted or none, however... */
e881bb1b 7147
718fe406 7148 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
19d3c25c
RH
7149 if (e->flags & EDGE_FALLTHRU)
7150 break;
7151 if (e == NULL)
7152 goto epilogue_done;
a78bdb38 7153
19d3c25c 7154 start_sequence ();
86c82654 7155 epilogue_end = emit_note (NULL, NOTE_INSN_EPILOGUE_BEG);
a78bdb38 7156
19d3c25c 7157 seq = gen_epilogue ();
7393c642
RK
7158
7159 /* If this function returns with the stack depressed, massage
7160 the epilogue to actually do that. */
43db0363
RK
7161 if (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
7162 && TYPE_RETURNS_STACK_DEPRESSED (TREE_TYPE (current_function_decl)))
7393c642
RK
7163 keep_stack_depressed (seq);
7164
19d3c25c 7165 emit_jump_insn (seq);
bdac5f58 7166
19d3c25c
RH
7167 /* Retain a map of the epilogue insns. */
7168 if (GET_CODE (seq) != SEQUENCE)
7169 seq = get_insns ();
0a1c58a2 7170 record_insns (seq, &epilogue);
bdac5f58 7171
19d3c25c 7172 seq = gen_sequence ();
718fe406 7173 end_sequence ();
e881bb1b 7174
19d3c25c 7175 insert_insn_on_edge (seq, e);
ca1117cc 7176 inserted = 1;
bdac5f58
TW
7177 }
7178#endif
19d3c25c 7179epilogue_done:
e881bb1b 7180
ca1117cc 7181 if (inserted)
e881bb1b 7182 commit_edge_insertions ();
0a1c58a2
JL
7183
7184#ifdef HAVE_sibcall_epilogue
7185 /* Emit sibling epilogues before any sibling call sites. */
718fe406 7186 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
0a1c58a2
JL
7187 {
7188 basic_block bb = e->src;
7189 rtx insn = bb->end;
7190 rtx i;
1b513b77 7191 rtx newinsn;
0a1c58a2
JL
7192
7193 if (GET_CODE (insn) != CALL_INSN
7194 || ! SIBLING_CALL_P (insn))
7195 continue;
7196
7197 start_sequence ();
7198 seq = gen_sibcall_epilogue ();
7199 end_sequence ();
7200
7201 i = PREV_INSN (insn);
1b513b77 7202 newinsn = emit_insn_before (seq, insn);
0a1c58a2
JL
7203
7204 /* Update the UID to basic block map. */
7205 for (i = NEXT_INSN (i); i != insn; i = NEXT_INSN (i))
7206 set_block_for_insn (i, bb);
7207
7208 /* Retain a map of the epilogue insns. Used in life analysis to
7209 avoid getting rid of sibcall epilogue insns. */
1b513b77
JH
7210 record_insns (GET_CODE (seq) == SEQUENCE
7211 ? seq : newinsn, &sibcall_epilogue);
0a1c58a2
JL
7212 }
7213#endif
ca1117cc
RH
7214
7215#ifdef HAVE_prologue
7216 if (prologue_end)
7217 {
7218 rtx insn, prev;
7219
7220 /* GDB handles `break f' by setting a breakpoint on the first
30196c1f 7221 line note after the prologue. Which means (1) that if
ca1117cc 7222 there are line number notes before where we inserted the
30196c1f
RH
7223 prologue we should move them, and (2) we should generate a
7224 note before the end of the first basic block, if there isn't
7225 one already there. */
ca1117cc 7226
718fe406 7227 for (insn = prologue_end; insn; insn = prev)
ca1117cc
RH
7228 {
7229 prev = PREV_INSN (insn);
7230 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7231 {
7232 /* Note that we cannot reorder the first insn in the
7233 chain, since rest_of_compilation relies on that
30196c1f 7234 remaining constant. */
ca1117cc 7235 if (prev == NULL)
30196c1f
RH
7236 break;
7237 reorder_insns (insn, insn, prologue_end);
ca1117cc
RH
7238 }
7239 }
7240
30196c1f
RH
7241 /* Find the last line number note in the first block. */
7242 for (insn = BASIC_BLOCK (0)->end;
7243 insn != prologue_end;
7244 insn = PREV_INSN (insn))
7245 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7246 break;
7247
7248 /* If we didn't find one, make a copy of the first line number
7249 we run across. */
7250 if (! insn)
ca1117cc 7251 {
30196c1f
RH
7252 for (insn = next_active_insn (prologue_end);
7253 insn;
7254 insn = PREV_INSN (insn))
7255 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7256 {
7257 emit_line_note_after (NOTE_SOURCE_FILE (insn),
7258 NOTE_LINE_NUMBER (insn),
7259 prologue_end);
7260 break;
7261 }
ca1117cc
RH
7262 }
7263 }
7264#endif
86c82654
RH
7265#ifdef HAVE_epilogue
7266 if (epilogue_end)
7267 {
7268 rtx insn, next;
7269
7270 /* Similarly, move any line notes that appear after the epilogue.
7271 There is no need, however, to be quite so anal about the existance
7272 of such a note. */
718fe406 7273 for (insn = epilogue_end; insn; insn = next)
86c82654
RH
7274 {
7275 next = NEXT_INSN (insn);
7276 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7277 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
7278 }
7279 }
7280#endif
bdac5f58
TW
7281}
7282
7283/* Reposition the prologue-end and epilogue-begin notes after instruction
7284 scheduling and delayed branch scheduling. */
7285
7286void
7287reposition_prologue_and_epilogue_notes (f)
79c9824e 7288 rtx f ATTRIBUTE_UNUSED;
bdac5f58
TW
7289{
7290#if defined (HAVE_prologue) || defined (HAVE_epilogue)
0a1c58a2
JL
7291 int len;
7292
7293 if ((len = VARRAY_SIZE (prologue)) > 0)
bdac5f58 7294 {
0a1c58a2 7295 register rtx insn, note = 0;
bdac5f58 7296
0a1c58a2
JL
7297 /* Scan from the beginning until we reach the last prologue insn.
7298 We apparently can't depend on basic_block_{head,end} after
7299 reorg has run. */
7300 for (insn = f; len && insn; insn = NEXT_INSN (insn))
bdac5f58 7301 {
0a1c58a2 7302 if (GET_CODE (insn) == NOTE)
9392c110 7303 {
0a1c58a2
JL
7304 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
7305 note = insn;
7306 }
7307 else if ((len -= contains (insn, prologue)) == 0)
7308 {
7309 rtx next;
7310 /* Find the prologue-end note if we haven't already, and
7311 move it to just after the last prologue insn. */
7312 if (note == 0)
9392c110 7313 {
0a1c58a2
JL
7314 for (note = insn; (note = NEXT_INSN (note));)
7315 if (GET_CODE (note) == NOTE
7316 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
7317 break;
9392c110 7318 }
c93b03c2 7319
0a1c58a2 7320 next = NEXT_INSN (note);
c93b03c2 7321
718fe406 7322 /* Whether or not we can depend on BLOCK_HEAD,
0a1c58a2
JL
7323 attempt to keep it up-to-date. */
7324 if (BLOCK_HEAD (0) == note)
7325 BLOCK_HEAD (0) = next;
c93b03c2 7326
0a1c58a2
JL
7327 remove_insn (note);
7328 add_insn_after (note, insn);
9392c110 7329 }
bdac5f58 7330 }
0a1c58a2
JL
7331 }
7332
7333 if ((len = VARRAY_SIZE (epilogue)) > 0)
7334 {
7335 register rtx insn, note = 0;
bdac5f58 7336
0a1c58a2
JL
7337 /* Scan from the end until we reach the first epilogue insn.
7338 We apparently can't depend on basic_block_{head,end} after
7339 reorg has run. */
7340 for (insn = get_last_insn (); len && insn; insn = PREV_INSN (insn))
bdac5f58 7341 {
0a1c58a2 7342 if (GET_CODE (insn) == NOTE)
9392c110 7343 {
0a1c58a2
JL
7344 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
7345 note = insn;
7346 }
7347 else if ((len -= contains (insn, epilogue)) == 0)
7348 {
7349 /* Find the epilogue-begin note if we haven't already, and
7350 move it to just before the first epilogue insn. */
7351 if (note == 0)
9392c110 7352 {
0a1c58a2
JL
7353 for (note = insn; (note = PREV_INSN (note));)
7354 if (GET_CODE (note) == NOTE
7355 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
7356 break;
9392c110 7357 }
c93b03c2 7358
718fe406 7359 /* Whether or not we can depend on BLOCK_HEAD,
0a1c58a2
JL
7360 attempt to keep it up-to-date. */
7361 if (n_basic_blocks
7362 && BLOCK_HEAD (n_basic_blocks-1) == insn)
7363 BLOCK_HEAD (n_basic_blocks-1) = note;
c93b03c2 7364
0a1c58a2
JL
7365 remove_insn (note);
7366 add_insn_before (note, insn);
9392c110 7367 }
bdac5f58
TW
7368 }
7369 }
7370#endif /* HAVE_prologue or HAVE_epilogue */
7371}
87ff9c8e
RH
7372
7373/* Mark T for GC. */
7374
7375static void
7376mark_temp_slot (t)
718fe406 7377 struct temp_slot *t;
87ff9c8e
RH
7378{
7379 while (t)
7380 {
7381 ggc_mark_rtx (t->slot);
7382 ggc_mark_rtx (t->address);
591ccf92
MM
7383 ggc_mark_tree (t->rtl_expr);
7384
87ff9c8e
RH
7385 t = t->next;
7386 }
7387}
7388
7389/* Mark P for GC. */
7390
7391static void
fa51b01b 7392mark_function_status (p)
87ff9c8e
RH
7393 struct function *p;
7394{
7395 int i;
7396 rtx *r;
7397
7398 if (p == 0)
7399 return;
7400
7401 ggc_mark_rtx (p->arg_offset_rtx);
7402
21cd906e
MM
7403 if (p->x_parm_reg_stack_loc)
7404 for (i = p->x_max_parm_reg, r = p->x_parm_reg_stack_loc;
7405 i > 0; --i, ++r)
7406 ggc_mark_rtx (*r);
87ff9c8e
RH
7407
7408 ggc_mark_rtx (p->return_rtx);
7409 ggc_mark_rtx (p->x_cleanup_label);
7410 ggc_mark_rtx (p->x_return_label);
7411 ggc_mark_rtx (p->x_save_expr_regs);
7412 ggc_mark_rtx (p->x_stack_slot_list);
7413 ggc_mark_rtx (p->x_parm_birth_insn);
7414 ggc_mark_rtx (p->x_tail_recursion_label);
7415 ggc_mark_rtx (p->x_tail_recursion_reentry);
7416 ggc_mark_rtx (p->internal_arg_pointer);
7417 ggc_mark_rtx (p->x_arg_pointer_save_area);
7418 ggc_mark_tree (p->x_rtl_expr_chain);
7419 ggc_mark_rtx (p->x_last_parm_insn);
7420 ggc_mark_tree (p->x_context_display);
7421 ggc_mark_tree (p->x_trampoline_list);
7422 ggc_mark_rtx (p->epilogue_delay_list);
b313a0fe 7423 ggc_mark_rtx (p->x_clobber_return_insn);
87ff9c8e
RH
7424
7425 mark_temp_slot (p->x_temp_slots);
7426
7427 {
7428 struct var_refs_queue *q = p->fixup_var_refs_queue;
7429 while (q)
7430 {
7431 ggc_mark_rtx (q->modified);
7432 q = q->next;
7433 }
7434 }
7435
7436 ggc_mark_rtx (p->x_nonlocal_goto_handler_slots);
afe3d090 7437 ggc_mark_rtx (p->x_nonlocal_goto_handler_labels);
87ff9c8e
RH
7438 ggc_mark_rtx (p->x_nonlocal_goto_stack_level);
7439 ggc_mark_tree (p->x_nonlocal_labels);
7440}
7441
7442/* Mark the function chain ARG (which is really a struct function **)
7443 for GC. */
7444
7445static void
7446mark_function_chain (arg)
7447 void *arg;
7448{
7449 struct function *f = *(struct function **) arg;
7450
7451 for (; f; f = f->next_global)
7452 {
87ff9c8e
RH
7453 ggc_mark_tree (f->decl);
7454
fa51b01b
RH
7455 mark_function_status (f);
7456 mark_eh_status (f->eh);
7457 mark_stmt_status (f->stmt);
7458 mark_expr_status (f->expr);
7459 mark_emit_status (f->emit);
7460 mark_varasm_status (f->varasm);
87ff9c8e
RH
7461
7462 if (mark_machine_status)
7463 (*mark_machine_status) (f);
7464 if (mark_lang_status)
7465 (*mark_lang_status) (f);
7466
7467 if (f->original_arg_vector)
7468 ggc_mark_rtvec ((rtvec) f->original_arg_vector);
7469 if (f->original_decl_initial)
7470 ggc_mark_tree (f->original_decl_initial);
7471 }
7472}
7473
7474/* Called once, at initialization, to initialize function.c. */
7475
7476void
7477init_function_once ()
7478{
7479 ggc_add_root (&all_functions, 1, sizeof all_functions,
7480 mark_function_chain);
0a1c58a2
JL
7481
7482 VARRAY_INT_INIT (prologue, 0, "prologue");
7483 VARRAY_INT_INIT (epilogue, 0, "epilogue");
7484 VARRAY_INT_INIT (sibcall_epilogue, 0, "sibcall_epilogue");
87ff9c8e 7485}