]> gcc.gnu.org Git - gcc.git/blame - gcc/function.c
function.c (assign_parms): If TREE_ADDRESSABLE is set...
[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 || TREE_SIDE_EFFECTS (parm)
4614 /* If -ffloat-store specified, don't put explicit
4615 float variables into registers. */
4616 || (flag_float_store
4617 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
4618 /* Always assign pseudo to structure return or item passed
4619 by invisible reference. */
4620 || passed_pointer || parm == function_result_decl)
4621 {
00d8a4c1
RK
4622 /* Store the parm in a pseudoregister during the function, but we
4623 may need to do it in a wider mode. */
4624
4625 register rtx parmreg;
770ae6cc 4626 unsigned int regno, regnoi = 0, regnor = 0;
00d8a4c1
RK
4627
4628 unsignedp = TREE_UNSIGNED (TREE_TYPE (parm));
cd5b3469 4629
621061f4
RK
4630 promoted_nominal_mode
4631 = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
6f086dfc 4632
621061f4 4633 parmreg = gen_reg_rtx (promoted_nominal_mode);
ddb7361a 4634 mark_user_reg (parmreg);
6f086dfc
RS
4635
4636 /* If this was an item that we received a pointer to, set DECL_RTL
4637 appropriately. */
4638 if (passed_pointer)
4639 {
621061f4 4640 DECL_RTL (parm)
38a448ca 4641 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (passed_type)), parmreg);
3bdf5ad1 4642 set_mem_attributes (DECL_RTL (parm), parm, 1);
6f086dfc
RS
4643 }
4644 else
4645 DECL_RTL (parm) = parmreg;
4646
4647 /* Copy the value into the register. */
621061f4
RK
4648 if (nominal_mode != passed_mode
4649 || promoted_nominal_mode != promoted_mode)
86f8eff3 4650 {
efd8cba0 4651 int save_tree_used;
621061f4 4652 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
718fe406 4653 mode, by the caller. We now have to convert it to
621061f4 4654 NOMINAL_MODE, if different. However, PARMREG may be in
956d6950 4655 a different mode than NOMINAL_MODE if it is being stored
621061f4
RK
4656 promoted.
4657
4658 If ENTRY_PARM is a hard register, it might be in a register
86f8eff3
RK
4659 not valid for operating in its mode (e.g., an odd-numbered
4660 register for a DFmode). In that case, moves are the only
4661 thing valid, so we can't do a convert from there. This
4662 occurs when the calling sequence allow such misaligned
3412b298
JW
4663 usages.
4664
4665 In addition, the conversion may involve a call, which could
4666 clobber parameters which haven't been copied to pseudo
4667 registers yet. Therefore, we must first copy the parm to
4668 a pseudo reg here, and save the conversion until after all
4669 parameters have been moved. */
4670
4671 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4672
4673 emit_move_insn (tempreg, validize_mem (entry_parm));
4674
4675 push_to_sequence (conversion_insns);
ad241351
RK
4676 tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
4677
efd8cba0
DB
4678 /* TREE_USED gets set erroneously during expand_assignment. */
4679 save_tree_used = TREE_USED (parm);
621061f4
RK
4680 expand_assignment (parm,
4681 make_tree (nominal_type, tempreg), 0, 0);
efd8cba0 4682 TREE_USED (parm) = save_tree_used;
3412b298 4683 conversion_insns = get_insns ();
621061f4 4684 did_conversion = 1;
3412b298 4685 end_sequence ();
86f8eff3 4686 }
6f086dfc
RS
4687 else
4688 emit_move_insn (parmreg, validize_mem (entry_parm));
4689
74bd77a8
RS
4690 /* If we were passed a pointer but the actual value
4691 can safely live in a register, put it in one. */
16bae307 4692 if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
d29c259b
RH
4693 && ! ((! optimize
4694 && ! DECL_REGISTER (parm)
74bd77a8 4695 && ! DECL_INLINE (fndecl))
74bd77a8
RS
4696 || TREE_SIDE_EFFECTS (parm)
4697 /* If -ffloat-store specified, don't put explicit
4698 float variables into registers. */
4699 || (flag_float_store
4700 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE)))
4701 {
2654605a
JW
4702 /* We can't use nominal_mode, because it will have been set to
4703 Pmode above. We must use the actual mode of the parm. */
4704 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
ddb7361a 4705 mark_user_reg (parmreg);
74bd77a8
RS
4706 emit_move_insn (parmreg, DECL_RTL (parm));
4707 DECL_RTL (parm) = parmreg;
c110c53d
RS
4708 /* STACK_PARM is the pointer, not the parm, and PARMREG is
4709 now the parm. */
4710 stack_parm = 0;
74bd77a8 4711 }
137a2a7b
DE
4712#ifdef FUNCTION_ARG_CALLEE_COPIES
4713 /* If we are passed an arg by reference and it is our responsibility
4714 to make a copy, do it now.
4715 PASSED_TYPE and PASSED mode now refer to the pointer, not the
4716 original argument, so we must recreate them in the call to
4717 FUNCTION_ARG_CALLEE_COPIES. */
4718 /* ??? Later add code to handle the case that if the argument isn't
4719 modified, don't do the copy. */
4720
4721 else if (passed_pointer
4722 && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
4723 TYPE_MODE (DECL_ARG_TYPE (parm)),
4724 DECL_ARG_TYPE (parm),
bf9c83fe 4725 named_arg)
926b1b99 4726 && ! TREE_ADDRESSABLE (DECL_ARG_TYPE (parm)))
137a2a7b
DE
4727 {
4728 rtx copy;
4729 tree type = DECL_ARG_TYPE (parm);
4730
4731 /* This sequence may involve a library call perhaps clobbering
4732 registers that haven't been copied to pseudos yet. */
4733
4734 push_to_sequence (conversion_insns);
4735
d0f062fb 4736 if (!COMPLETE_TYPE_P (type)
137a2a7b 4737 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1fd3ef7f 4738 /* This is a variable sized object. */
38a448ca
RH
4739 copy = gen_rtx_MEM (BLKmode,
4740 allocate_dynamic_stack_space
4741 (expr_size (parm), NULL_RTX,
4742 TYPE_ALIGN (type)));
137a2a7b 4743 else
1fd3ef7f
RK
4744 copy = assign_stack_temp (TYPE_MODE (type),
4745 int_size_in_bytes (type), 1);
a696c1d6 4746 set_mem_attributes (copy, parm, 1);
137a2a7b
DE
4747
4748 store_expr (parm, copy, 0);
4749 emit_move_insn (parmreg, XEXP (copy, 0));
7d384cc0 4750 if (current_function_check_memory_usage)
ebb1b59a
BS
4751 emit_library_call (chkr_set_right_libfunc,
4752 LCT_CONST_MAKE_BLOCK, VOIDmode, 3,
6a9c4aed 4753 XEXP (copy, 0), Pmode,
86fa911a
RK
4754 GEN_INT (int_size_in_bytes (type)),
4755 TYPE_MODE (sizetype),
956d6950
JL
4756 GEN_INT (MEMORY_USE_RW),
4757 TYPE_MODE (integer_type_node));
137a2a7b 4758 conversion_insns = get_insns ();
621061f4 4759 did_conversion = 1;
137a2a7b
DE
4760 end_sequence ();
4761 }
4762#endif /* FUNCTION_ARG_CALLEE_COPIES */
74bd77a8 4763
6f086dfc 4764 /* In any case, record the parm's desired stack location
718fe406 4765 in case we later discover it must live in the stack.
14aceb29
RS
4766
4767 If it is a COMPLEX value, store the stack location for both
4768 halves. */
4769
4770 if (GET_CODE (parmreg) == CONCAT)
4771 regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
4772 else
4773 regno = REGNO (parmreg);
4774
e9a25f70 4775 if (regno >= max_parm_reg)
6f086dfc
RS
4776 {
4777 rtx *new;
e9a25f70 4778 int old_max_parm_reg = max_parm_reg;
14aceb29 4779
e9a25f70
JL
4780 /* It's slow to expand this one register at a time,
4781 but it's also rare and we need max_parm_reg to be
4782 precisely correct. */
4783 max_parm_reg = regno + 1;
e2ecd91c
BS
4784 new = (rtx *) xrealloc (parm_reg_stack_loc,
4785 max_parm_reg * sizeof (rtx));
961192e1 4786 memset ((char *) (new + old_max_parm_reg), 0,
e9a25f70 4787 (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
6f086dfc
RS
4788 parm_reg_stack_loc = new;
4789 }
14aceb29
RS
4790
4791 if (GET_CODE (parmreg) == CONCAT)
4792 {
4793 enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
4794
a03caf76
RK
4795 regnor = REGNO (gen_realpart (submode, parmreg));
4796 regnoi = REGNO (gen_imagpart (submode, parmreg));
4797
7b1a0c14
RS
4798 if (stack_parm != 0)
4799 {
a03caf76 4800 parm_reg_stack_loc[regnor]
3d329b07 4801 = gen_realpart (submode, stack_parm);
a03caf76 4802 parm_reg_stack_loc[regnoi]
3d329b07 4803 = gen_imagpart (submode, stack_parm);
7b1a0c14
RS
4804 }
4805 else
4806 {
a03caf76
RK
4807 parm_reg_stack_loc[regnor] = 0;
4808 parm_reg_stack_loc[regnoi] = 0;
7b1a0c14 4809 }
14aceb29
RS
4810 }
4811 else
4812 parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
6f086dfc
RS
4813
4814 /* Mark the register as eliminable if we did no conversion
4815 and it was copied from memory at a fixed offset,
4816 and the arg pointer was not copied to a pseudo-reg.
4817 If the arg pointer is a pseudo reg or the offset formed
4818 an invalid address, such memory-equivalences
4819 as we make here would screw up life analysis for it. */
4820 if (nominal_mode == passed_mode
621061f4 4821 && ! did_conversion
38b610ed
ILT
4822 && stack_parm != 0
4823 && GET_CODE (stack_parm) == MEM
6f086dfc
RS
4824 && stack_offset.var == 0
4825 && reg_mentioned_p (virtual_incoming_args_rtx,
38b610ed 4826 XEXP (stack_parm, 0)))
a03caf76
RK
4827 {
4828 rtx linsn = get_last_insn ();
69685820 4829 rtx sinsn, set;
a03caf76
RK
4830
4831 /* Mark complex types separately. */
4832 if (GET_CODE (parmreg) == CONCAT)
69685820
RK
4833 /* Scan backwards for the set of the real and
4834 imaginary parts. */
4835 for (sinsn = linsn; sinsn != 0;
4836 sinsn = prev_nonnote_insn (sinsn))
4837 {
4838 set = single_set (sinsn);
4839 if (set != 0
4840 && SET_DEST (set) == regno_reg_rtx [regnoi])
4841 REG_NOTES (sinsn)
38a448ca
RH
4842 = gen_rtx_EXPR_LIST (REG_EQUIV,
4843 parm_reg_stack_loc[regnoi],
4844 REG_NOTES (sinsn));
69685820
RK
4845 else if (set != 0
4846 && SET_DEST (set) == regno_reg_rtx [regnor])
4847 REG_NOTES (sinsn)
38a448ca
RH
4848 = gen_rtx_EXPR_LIST (REG_EQUIV,
4849 parm_reg_stack_loc[regnor],
4850 REG_NOTES (sinsn));
69685820
RK
4851 }
4852 else if ((set = single_set (linsn)) != 0
4853 && SET_DEST (set) == parmreg)
718fe406 4854 REG_NOTES (linsn)
38a448ca
RH
4855 = gen_rtx_EXPR_LIST (REG_EQUIV,
4856 stack_parm, REG_NOTES (linsn));
a03caf76 4857 }
6f086dfc
RS
4858
4859 /* For pointer data type, suggest pointer register. */
e5e809f4 4860 if (POINTER_TYPE_P (TREE_TYPE (parm)))
6c6166bd 4861 mark_reg_pointer (parmreg,
bdb429a5
RK
4862 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
4863
d96a6d1a
JM
4864 /* If something wants our address, try to use ADDRESSOF. */
4865 if (TREE_ADDRESSABLE (parm))
4866 put_var_into_stack (parm);
6f086dfc
RS
4867 }
4868 else
4869 {
4870 /* Value must be stored in the stack slot STACK_PARM
4871 during function execution. */
4872
621061f4 4873 if (promoted_mode != nominal_mode)
86f8eff3
RK
4874 {
4875 /* Conversion is required. */
3412b298
JW
4876 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4877
4878 emit_move_insn (tempreg, validize_mem (entry_parm));
86f8eff3 4879
3412b298
JW
4880 push_to_sequence (conversion_insns);
4881 entry_parm = convert_to_mode (nominal_mode, tempreg,
a53e14c0 4882 TREE_UNSIGNED (TREE_TYPE (parm)));
de957303
DE
4883 if (stack_parm)
4884 {
4885 /* ??? This may need a big-endian conversion on sparc64. */
4886 stack_parm = change_address (stack_parm, nominal_mode,
4887 NULL_RTX);
4888 }
3412b298 4889 conversion_insns = get_insns ();
621061f4 4890 did_conversion = 1;
3412b298 4891 end_sequence ();
86f8eff3 4892 }
6f086dfc
RS
4893
4894 if (entry_parm != stack_parm)
4895 {
4896 if (stack_parm == 0)
7e41ffa2
RS
4897 {
4898 stack_parm
4899 = assign_stack_local (GET_MODE (entry_parm),
4900 GET_MODE_SIZE (GET_MODE (entry_parm)), 0);
3bdf5ad1 4901 set_mem_attributes (stack_parm, parm, 1);
7e41ffa2
RS
4902 }
4903
621061f4 4904 if (promoted_mode != nominal_mode)
3412b298
JW
4905 {
4906 push_to_sequence (conversion_insns);
4907 emit_move_insn (validize_mem (stack_parm),
4908 validize_mem (entry_parm));
4909 conversion_insns = get_insns ();
4910 end_sequence ();
4911 }
4912 else
4913 emit_move_insn (validize_mem (stack_parm),
4914 validize_mem (entry_parm));
6f086dfc 4915 }
7d384cc0 4916 if (current_function_check_memory_usage)
86fa911a
RK
4917 {
4918 push_to_sequence (conversion_insns);
ebb1b59a
BS
4919 emit_library_call (chkr_set_right_libfunc, LCT_CONST_MAKE_BLOCK,
4920 VOIDmode, 3, XEXP (stack_parm, 0), Pmode,
718fe406 4921 GEN_INT (GET_MODE_SIZE (GET_MODE
86fa911a
RK
4922 (entry_parm))),
4923 TYPE_MODE (sizetype),
956d6950
JL
4924 GEN_INT (MEMORY_USE_RW),
4925 TYPE_MODE (integer_type_node));
6f086dfc 4926
86fa911a
RK
4927 conversion_insns = get_insns ();
4928 end_sequence ();
4929 }
6f086dfc
RS
4930 DECL_RTL (parm) = stack_parm;
4931 }
718fe406 4932
6f086dfc
RS
4933 /* If this "parameter" was the place where we are receiving the
4934 function's incoming structure pointer, set up the result. */
4935 if (parm == function_result_decl)
ccdecf58
RK
4936 {
4937 tree result = DECL_RESULT (fndecl);
ccdecf58
RK
4938
4939 DECL_RTL (result)
38a448ca 4940 = gen_rtx_MEM (DECL_MODE (result), DECL_RTL (parm));
ccdecf58 4941
3bdf5ad1 4942 set_mem_attributes (DECL_RTL (result), result, 1);
ccdecf58 4943 }
6f086dfc
RS
4944 }
4945
3412b298
JW
4946 /* Output all parameter conversion instructions (possibly including calls)
4947 now that all parameters have been copied out of hard registers. */
4948 emit_insns (conversion_insns);
4949
6f086dfc
RS
4950 last_parm_insn = get_last_insn ();
4951
4952 current_function_args_size = stack_args_size.constant;
4953
4954 /* Adjust function incoming argument size for alignment and
4955 minimum length. */
4956
4957#ifdef REG_PARM_STACK_SPACE
6f90e075 4958#ifndef MAYBE_REG_PARM_STACK_SPACE
6f086dfc
RS
4959 current_function_args_size = MAX (current_function_args_size,
4960 REG_PARM_STACK_SPACE (fndecl));
4961#endif
6f90e075 4962#endif
6f086dfc 4963
4433e339
RH
4964#ifdef STACK_BOUNDARY
4965#define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
4966
4967 current_function_args_size
4968 = ((current_function_args_size + STACK_BYTES - 1)
4969 / STACK_BYTES) * STACK_BYTES;
718fe406 4970#endif
4433e339 4971
6f086dfc
RS
4972#ifdef ARGS_GROW_DOWNWARD
4973 current_function_arg_offset_rtx
5f4f0e22 4974 = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
718fe406
KH
4975 : expand_expr (size_diffop (stack_args_size.var,
4976 size_int (-stack_args_size.constant)),
86fa911a 4977 NULL_RTX, VOIDmode, EXPAND_MEMORY_USE_BAD));
6f086dfc
RS
4978#else
4979 current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
4980#endif
4981
4982 /* See how many bytes, if any, of its args a function should try to pop
4983 on return. */
4984
64e6d9cc 4985 current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
6f086dfc
RS
4986 current_function_args_size);
4987
3b69d50e
RK
4988 /* For stdarg.h function, save info about
4989 regs and stack space used by the named args. */
6f086dfc 4990
3b69d50e 4991 if (!hide_last_arg)
6f086dfc
RS
4992 current_function_args_info = args_so_far;
4993
4994 /* Set the rtx used for the function return value. Put this in its
4995 own variable so any optimizers that need this information don't have
4996 to include tree.h. Do this here so it gets done when an inlined
4997 function gets output. */
4998
4999 current_function_return_rtx = DECL_RTL (DECL_RESULT (fndecl));
5000}
5001\f
75dc3319
RK
5002/* Indicate whether REGNO is an incoming argument to the current function
5003 that was promoted to a wider mode. If so, return the RTX for the
5004 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
5005 that REGNO is promoted from and whether the promotion was signed or
5006 unsigned. */
5007
5008#ifdef PROMOTE_FUNCTION_ARGS
5009
5010rtx
5011promoted_input_arg (regno, pmode, punsignedp)
770ae6cc 5012 unsigned int regno;
75dc3319
RK
5013 enum machine_mode *pmode;
5014 int *punsignedp;
5015{
5016 tree arg;
5017
5018 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
5019 arg = TREE_CHAIN (arg))
5020 if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
621061f4
RK
5021 && REGNO (DECL_INCOMING_RTL (arg)) == regno
5022 && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
75dc3319
RK
5023 {
5024 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
5025 int unsignedp = TREE_UNSIGNED (TREE_TYPE (arg));
5026
a5a52dbc 5027 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
75dc3319
RK
5028 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
5029 && mode != DECL_MODE (arg))
5030 {
5031 *pmode = DECL_MODE (arg);
5032 *punsignedp = unsignedp;
5033 return DECL_INCOMING_RTL (arg);
5034 }
5035 }
5036
5037 return 0;
5038}
5039
5040#endif
5041\f
6f086dfc
RS
5042/* Compute the size and offset from the start of the stacked arguments for a
5043 parm passed in mode PASSED_MODE and with type TYPE.
5044
5045 INITIAL_OFFSET_PTR points to the current offset into the stacked
5046 arguments.
5047
5048 The starting offset and size for this parm are returned in *OFFSET_PTR
5049 and *ARG_SIZE_PTR, respectively.
5050
5051 IN_REGS is non-zero if the argument will be passed in registers. It will
5052 never be set if REG_PARM_STACK_SPACE is not defined.
5053
5054 FNDECL is the function in which the argument was defined.
5055
5056 There are two types of rounding that are done. The first, controlled by
5057 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
5058 list to be aligned to the specific boundary (in bits). This rounding
5059 affects the initial and starting offsets, but not the argument size.
5060
5061 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
5062 optionally rounds the size of the parm to PARM_BOUNDARY. The
5063 initial offset is not affected by this rounding, while the size always
5064 is and the starting offset may be. */
5065
718fe406 5066/* offset_ptr will be negative for ARGS_GROW_DOWNWARD case;
6f086dfc
RS
5067 initial_offset_ptr is positive because locate_and_pad_parm's
5068 callers pass in the total size of args so far as
5069 initial_offset_ptr. arg_size_ptr is always positive.*/
5070
6f086dfc
RS
5071void
5072locate_and_pad_parm (passed_mode, type, in_regs, fndecl,
4fc026cd 5073 initial_offset_ptr, offset_ptr, arg_size_ptr,
718fe406 5074 alignment_pad)
6f086dfc
RS
5075 enum machine_mode passed_mode;
5076 tree type;
57bed152 5077 int in_regs ATTRIBUTE_UNUSED;
91813b28 5078 tree fndecl ATTRIBUTE_UNUSED;
6f086dfc
RS
5079 struct args_size *initial_offset_ptr;
5080 struct args_size *offset_ptr;
5081 struct args_size *arg_size_ptr;
4fc026cd
CM
5082 struct args_size *alignment_pad;
5083
6f086dfc
RS
5084{
5085 tree sizetree
5086 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
5087 enum direction where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
5088 int boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
6f086dfc
RS
5089
5090#ifdef REG_PARM_STACK_SPACE
5091 /* If we have found a stack parm before we reach the end of the
5092 area reserved for registers, skip that area. */
5093 if (! in_regs)
5094 {
29a82058
JL
5095 int reg_parm_stack_space = 0;
5096
29008b51
JW
5097#ifdef MAYBE_REG_PARM_STACK_SPACE
5098 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
5099#else
6f086dfc 5100 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
29008b51 5101#endif
6f086dfc
RS
5102 if (reg_parm_stack_space > 0)
5103 {
5104 if (initial_offset_ptr->var)
5105 {
5106 initial_offset_ptr->var
5107 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
fed3cef0 5108 ssize_int (reg_parm_stack_space));
6f086dfc
RS
5109 initial_offset_ptr->constant = 0;
5110 }
5111 else if (initial_offset_ptr->constant < reg_parm_stack_space)
5112 initial_offset_ptr->constant = reg_parm_stack_space;
5113 }
5114 }
5115#endif /* REG_PARM_STACK_SPACE */
5116
5117 arg_size_ptr->var = 0;
5118 arg_size_ptr->constant = 0;
23ccfa6d
RH
5119 alignment_pad->var = 0;
5120 alignment_pad->constant = 0;
6f086dfc
RS
5121
5122#ifdef ARGS_GROW_DOWNWARD
5123 if (initial_offset_ptr->var)
5124 {
5125 offset_ptr->constant = 0;
fed3cef0 5126 offset_ptr->var = size_binop (MINUS_EXPR, ssize_int (0),
6f086dfc
RS
5127 initial_offset_ptr->var);
5128 }
5129 else
5130 {
718fe406 5131 offset_ptr->constant = -initial_offset_ptr->constant;
6f086dfc
RS
5132 offset_ptr->var = 0;
5133 }
0b21dcf5 5134 if (where_pad != none
1468899d
RK
5135 && (!host_integerp (sizetree, 1)
5136 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
6f086dfc
RS
5137 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5138 SUB_PARM_SIZE (*offset_ptr, sizetree);
66bcbe19 5139 if (where_pad != downward)
4fc026cd 5140 pad_to_arg_alignment (offset_ptr, boundary, alignment_pad);
6f086dfc 5141 if (initial_offset_ptr->var)
fed3cef0
RK
5142 arg_size_ptr->var = size_binop (MINUS_EXPR,
5143 size_binop (MINUS_EXPR,
5144 ssize_int (0),
5145 initial_offset_ptr->var),
5146 offset_ptr->var);
5147
6f086dfc 5148 else
718fe406
KH
5149 arg_size_ptr->constant = (-initial_offset_ptr->constant
5150 - offset_ptr->constant);
fed3cef0 5151
6f086dfc 5152#else /* !ARGS_GROW_DOWNWARD */
832ea3b3
FS
5153 if (!in_regs
5154#ifdef REG_PARM_STACK_SPACE
5155 || REG_PARM_STACK_SPACE (fndecl) > 0
5156#endif
5157 )
5158 pad_to_arg_alignment (initial_offset_ptr, boundary, alignment_pad);
6f086dfc 5159 *offset_ptr = *initial_offset_ptr;
6f086dfc
RS
5160
5161#ifdef PUSH_ROUNDING
5162 if (passed_mode != BLKmode)
5163 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
5164#endif
5165
d4b0a7a0
DE
5166 /* Pad_below needs the pre-rounded size to know how much to pad below
5167 so this must be done before rounding up. */
ea5917da
DE
5168 if (where_pad == downward
5169 /* However, BLKmode args passed in regs have their padding done elsewhere.
5170 The stack slot must be able to hold the entire register. */
5171 && !(in_regs && passed_mode == BLKmode))
d4b0a7a0
DE
5172 pad_below (offset_ptr, passed_mode, sizetree);
5173
6f086dfc 5174 if (where_pad != none
1468899d
RK
5175 && (!host_integerp (sizetree, 1)
5176 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
6f086dfc
RS
5177 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5178
5179 ADD_PARM_SIZE (*arg_size_ptr, sizetree);
5180#endif /* ARGS_GROW_DOWNWARD */
5181}
5182
e16c591a
RS
5183/* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
5184 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
5185
6f086dfc 5186static void
4fc026cd 5187pad_to_arg_alignment (offset_ptr, boundary, alignment_pad)
6f086dfc
RS
5188 struct args_size *offset_ptr;
5189 int boundary;
4fc026cd 5190 struct args_size *alignment_pad;
6f086dfc 5191{
a544cfd2
KG
5192 tree save_var = NULL_TREE;
5193 HOST_WIDE_INT save_constant = 0;
4fc026cd 5194
6f086dfc 5195 int boundary_in_bytes = boundary / BITS_PER_UNIT;
718fe406 5196
9399d5c6 5197 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
4fc026cd
CM
5198 {
5199 save_var = offset_ptr->var;
5200 save_constant = offset_ptr->constant;
5201 }
5202
5203 alignment_pad->var = NULL_TREE;
5204 alignment_pad->constant = 0;
4fc026cd 5205
6f086dfc
RS
5206 if (boundary > BITS_PER_UNIT)
5207 {
5208 if (offset_ptr->var)
5209 {
718fe406 5210 offset_ptr->var =
6f086dfc 5211#ifdef ARGS_GROW_DOWNWARD
718fe406 5212 round_down
6f086dfc
RS
5213#else
5214 round_up
5215#endif
5216 (ARGS_SIZE_TREE (*offset_ptr),
5217 boundary / BITS_PER_UNIT);
5218 offset_ptr->constant = 0; /*?*/
9399d5c6 5219 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
fed3cef0
RK
5220 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
5221 save_var);
6f086dfc
RS
5222 }
5223 else
718fe406 5224 {
fbb57b2a 5225 offset_ptr->constant =
6f086dfc 5226#ifdef ARGS_GROW_DOWNWARD
fbb57b2a 5227 FLOOR_ROUND (offset_ptr->constant, boundary_in_bytes);
6f086dfc 5228#else
fbb57b2a 5229 CEIL_ROUND (offset_ptr->constant, boundary_in_bytes);
6f086dfc 5230#endif
718fe406
KH
5231 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5232 alignment_pad->constant = offset_ptr->constant - save_constant;
5233 }
6f086dfc
RS
5234 }
5235}
5236
51723711 5237#ifndef ARGS_GROW_DOWNWARD
6f086dfc
RS
5238static void
5239pad_below (offset_ptr, passed_mode, sizetree)
5240 struct args_size *offset_ptr;
5241 enum machine_mode passed_mode;
5242 tree sizetree;
5243{
5244 if (passed_mode != BLKmode)
5245 {
5246 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
5247 offset_ptr->constant
5248 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
5249 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
5250 - GET_MODE_SIZE (passed_mode));
5251 }
5252 else
5253 {
5254 if (TREE_CODE (sizetree) != INTEGER_CST
5255 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
5256 {
5257 /* Round the size up to multiple of PARM_BOUNDARY bits. */
5258 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5259 /* Add it in. */
5260 ADD_PARM_SIZE (*offset_ptr, s2);
5261 SUB_PARM_SIZE (*offset_ptr, sizetree);
5262 }
5263 }
5264}
51723711 5265#endif
6f086dfc
RS
5266\f
5267/* Walk the tree of blocks describing the binding levels within a function
5268 and warn about uninitialized variables.
5269 This is done after calling flow_analysis and before global_alloc
5270 clobbers the pseudo-regs to hard regs. */
5271
5272void
5273uninitialized_vars_warning (block)
5274 tree block;
5275{
5276 register tree decl, sub;
5277 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5278 {
8fbe1035
ML
5279 if (warn_uninitialized
5280 && TREE_CODE (decl) == VAR_DECL
6f086dfc
RS
5281 /* These warnings are unreliable for and aggregates
5282 because assigning the fields one by one can fail to convince
5283 flow.c that the entire aggregate was initialized.
5284 Unions are troublesome because members may be shorter. */
05e3bdb9 5285 && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
6f086dfc
RS
5286 && DECL_RTL (decl) != 0
5287 && GET_CODE (DECL_RTL (decl)) == REG
6acdd0fd
JL
5288 /* Global optimizations can make it difficult to determine if a
5289 particular variable has been initialized. However, a VAR_DECL
5290 with a nonzero DECL_INITIAL had an initializer, so do not
5291 claim it is potentially uninitialized.
5292
5293 We do not care about the actual value in DECL_INITIAL, so we do
5294 not worry that it may be a dangling pointer. */
5295 && DECL_INITIAL (decl) == NULL_TREE
6f086dfc
RS
5296 && regno_uninitialized (REGNO (DECL_RTL (decl))))
5297 warning_with_decl (decl,
3c8cd8bd 5298 "`%s' might be used uninitialized in this function");
8fbe1035
ML
5299 if (extra_warnings
5300 && TREE_CODE (decl) == VAR_DECL
6f086dfc
RS
5301 && DECL_RTL (decl) != 0
5302 && GET_CODE (DECL_RTL (decl)) == REG
5303 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5304 warning_with_decl (decl,
3c8cd8bd 5305 "variable `%s' might be clobbered by `longjmp' or `vfork'");
6f086dfc
RS
5306 }
5307 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5308 uninitialized_vars_warning (sub);
5309}
5310
5311/* Do the appropriate part of uninitialized_vars_warning
5312 but for arguments instead of local variables. */
5313
5314void
0cd6ef35 5315setjmp_args_warning ()
6f086dfc
RS
5316{
5317 register tree decl;
5318 for (decl = DECL_ARGUMENTS (current_function_decl);
5319 decl; decl = TREE_CHAIN (decl))
5320 if (DECL_RTL (decl) != 0
5321 && GET_CODE (DECL_RTL (decl)) == REG
5322 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
718fe406
KH
5323 warning_with_decl (decl,
5324 "argument `%s' might be clobbered by `longjmp' or `vfork'");
6f086dfc
RS
5325}
5326
5327/* If this function call setjmp, put all vars into the stack
5328 unless they were declared `register'. */
5329
5330void
5331setjmp_protect (block)
5332 tree block;
5333{
5334 register tree decl, sub;
5335 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5336 if ((TREE_CODE (decl) == VAR_DECL
5337 || TREE_CODE (decl) == PARM_DECL)
5338 && DECL_RTL (decl) != 0
e9a25f70
JL
5339 && (GET_CODE (DECL_RTL (decl)) == REG
5340 || (GET_CODE (DECL_RTL (decl)) == MEM
5341 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
b335c2cc 5342 /* If this variable came from an inline function, it must be
9ec36da5 5343 that its life doesn't overlap the setjmp. If there was a
b335c2cc
TW
5344 setjmp in the function, it would already be in memory. We
5345 must exclude such variable because their DECL_RTL might be
5346 set to strange things such as virtual_stack_vars_rtx. */
5347 && ! DECL_FROM_INLINE (decl)
6f086dfc
RS
5348 && (
5349#ifdef NON_SAVING_SETJMP
5350 /* If longjmp doesn't restore the registers,
5351 don't put anything in them. */
5352 NON_SAVING_SETJMP
5353 ||
5354#endif
a82ad570 5355 ! DECL_REGISTER (decl)))
6f086dfc
RS
5356 put_var_into_stack (decl);
5357 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5358 setjmp_protect (sub);
5359}
5360\f
5361/* Like the previous function, but for args instead of local variables. */
5362
5363void
5364setjmp_protect_args ()
5365{
29a82058 5366 register tree decl;
6f086dfc
RS
5367 for (decl = DECL_ARGUMENTS (current_function_decl);
5368 decl; decl = TREE_CHAIN (decl))
5369 if ((TREE_CODE (decl) == VAR_DECL
5370 || TREE_CODE (decl) == PARM_DECL)
5371 && DECL_RTL (decl) != 0
e9a25f70
JL
5372 && (GET_CODE (DECL_RTL (decl)) == REG
5373 || (GET_CODE (DECL_RTL (decl)) == MEM
5374 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
6f086dfc
RS
5375 && (
5376 /* If longjmp doesn't restore the registers,
5377 don't put anything in them. */
5378#ifdef NON_SAVING_SETJMP
5379 NON_SAVING_SETJMP
5380 ||
5381#endif
a82ad570 5382 ! DECL_REGISTER (decl)))
6f086dfc
RS
5383 put_var_into_stack (decl);
5384}
5385\f
5386/* Return the context-pointer register corresponding to DECL,
5387 or 0 if it does not need one. */
5388
5389rtx
5390lookup_static_chain (decl)
5391 tree decl;
5392{
b001a02f
PB
5393 tree context = decl_function_context (decl);
5394 tree link;
7ad8c4bf 5395
38ee6ed9
JM
5396 if (context == 0
5397 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_NO_STATIC_CHAIN (decl)))
7ad8c4bf 5398 return 0;
38ee6ed9 5399
6f086dfc
RS
5400 /* We treat inline_function_decl as an alias for the current function
5401 because that is the inline function whose vars, types, etc.
5402 are being merged into the current function.
5403 See expand_inline_function. */
5404 if (context == current_function_decl || context == inline_function_decl)
5405 return virtual_stack_vars_rtx;
5406
5407 for (link = context_display; link; link = TREE_CHAIN (link))
5408 if (TREE_PURPOSE (link) == context)
5409 return RTL_EXPR_RTL (TREE_VALUE (link));
5410
5411 abort ();
5412}
5413\f
5414/* Convert a stack slot address ADDR for variable VAR
5415 (from a containing function)
5416 into an address valid in this function (using a static chain). */
5417
5418rtx
5419fix_lexical_addr (addr, var)
5420 rtx addr;
5421 tree var;
5422{
5423 rtx basereg;
e5e809f4 5424 HOST_WIDE_INT displacement;
6f086dfc
RS
5425 tree context = decl_function_context (var);
5426 struct function *fp;
5427 rtx base = 0;
5428
5429 /* If this is the present function, we need not do anything. */
5430 if (context == current_function_decl || context == inline_function_decl)
5431 return addr;
5432
5433 for (fp = outer_function_chain; fp; fp = fp->next)
5434 if (fp->decl == context)
5435 break;
5436
5437 if (fp == 0)
5438 abort ();
5439
e9a25f70
JL
5440 if (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == MEM)
5441 addr = XEXP (XEXP (addr, 0), 0);
5442
6f086dfc
RS
5443 /* Decode given address as base reg plus displacement. */
5444 if (GET_CODE (addr) == REG)
5445 basereg = addr, displacement = 0;
5446 else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
5447 basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
5448 else
5449 abort ();
5450
5451 /* We accept vars reached via the containing function's
5452 incoming arg pointer and via its stack variables pointer. */
5453 if (basereg == fp->internal_arg_pointer)
5454 {
5455 /* If reached via arg pointer, get the arg pointer value
5456 out of that function's stack frame.
5457
5458 There are two cases: If a separate ap is needed, allocate a
5459 slot in the outer function for it and dereference it that way.
5460 This is correct even if the real ap is actually a pseudo.
5461 Otherwise, just adjust the offset from the frame pointer to
5462 compensate. */
5463
5464#ifdef NEED_SEPARATE_AP
5465 rtx addr;
5466
49ad7cfa
BS
5467 if (fp->x_arg_pointer_save_area == 0)
5468 fp->x_arg_pointer_save_area
e2ecd91c 5469 = assign_stack_local_1 (Pmode, GET_MODE_SIZE (Pmode), 0, fp);
6f086dfc 5470
49ad7cfa 5471 addr = fix_lexical_addr (XEXP (fp->x_arg_pointer_save_area, 0), var);
6f086dfc
RS
5472 addr = memory_address (Pmode, addr);
5473
3bdf5ad1
RK
5474 base = gen_rtx_MEM (Pmode, addr);
5475 MEM_ALIAS_SET (base) = get_frame_alias_set ();
5476 base = copy_to_reg (base);
6f086dfc
RS
5477#else
5478 displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
86f8eff3 5479 base = lookup_static_chain (var);
6f086dfc
RS
5480#endif
5481 }
5482
5483 else if (basereg == virtual_stack_vars_rtx)
5484 {
5485 /* This is the same code as lookup_static_chain, duplicated here to
5486 avoid an extra call to decl_function_context. */
5487 tree link;
5488
5489 for (link = context_display; link; link = TREE_CHAIN (link))
5490 if (TREE_PURPOSE (link) == context)
5491 {
5492 base = RTL_EXPR_RTL (TREE_VALUE (link));
5493 break;
5494 }
5495 }
5496
5497 if (base == 0)
5498 abort ();
5499
5500 /* Use same offset, relative to appropriate static chain or argument
5501 pointer. */
5502 return plus_constant (base, displacement);
5503}
5504\f
5505/* Return the address of the trampoline for entering nested fn FUNCTION.
5506 If necessary, allocate a trampoline (in the stack frame)
5507 and emit rtl to initialize its contents (at entry to this function). */
5508
5509rtx
5510trampoline_address (function)
5511 tree function;
5512{
5513 tree link;
5514 tree rtlexp;
5515 rtx tramp;
5516 struct function *fp;
5517 tree fn_context;
5518
5519 /* Find an existing trampoline and return it. */
5520 for (link = trampoline_list; link; link = TREE_CHAIN (link))
5521 if (TREE_PURPOSE (link) == function)
e87ee2a9
RK
5522 return
5523 round_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
5524
6f086dfc 5525 for (fp = outer_function_chain; fp; fp = fp->next)
49ad7cfa 5526 for (link = fp->x_trampoline_list; link; link = TREE_CHAIN (link))
6f086dfc
RS
5527 if (TREE_PURPOSE (link) == function)
5528 {
5529 tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
5530 function);
5531 return round_trampoline_addr (tramp);
5532 }
5533
5534 /* None exists; we must make one. */
5535
5536 /* Find the `struct function' for the function containing FUNCTION. */
5537 fp = 0;
5538 fn_context = decl_function_context (function);
4ac74fb8
RK
5539 if (fn_context != current_function_decl
5540 && fn_context != inline_function_decl)
6f086dfc
RS
5541 for (fp = outer_function_chain; fp; fp = fp->next)
5542 if (fp->decl == fn_context)
5543 break;
5544
5545 /* Allocate run-time space for this trampoline
5546 (usually in the defining function's stack frame). */
5547#ifdef ALLOCATE_TRAMPOLINE
5548 tramp = ALLOCATE_TRAMPOLINE (fp);
5549#else
5550 /* If rounding needed, allocate extra space
5551 to ensure we have TRAMPOLINE_SIZE bytes left after rounding up. */
5552#ifdef TRAMPOLINE_ALIGNMENT
b02ab63a
RK
5553#define TRAMPOLINE_REAL_SIZE \
5554 (TRAMPOLINE_SIZE + (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT) - 1)
6f086dfc
RS
5555#else
5556#define TRAMPOLINE_REAL_SIZE (TRAMPOLINE_SIZE)
5557#endif
e2ecd91c 5558 tramp = assign_stack_local_1 (BLKmode, TRAMPOLINE_REAL_SIZE, 0,
01d939e8 5559 fp ? fp : cfun);
6f086dfc
RS
5560#endif
5561
5562 /* Record the trampoline for reuse and note it for later initialization
5563 by expand_function_end. */
5564 if (fp != 0)
5565 {
6f086dfc
RS
5566 rtlexp = make_node (RTL_EXPR);
5567 RTL_EXPR_RTL (rtlexp) = tramp;
49ad7cfa
BS
5568 fp->x_trampoline_list = tree_cons (function, rtlexp,
5569 fp->x_trampoline_list);
6f086dfc
RS
5570 }
5571 else
5572 {
5573 /* Make the RTL_EXPR node temporary, not momentary, so that the
5574 trampoline_list doesn't become garbage. */
6f086dfc 5575 rtlexp = make_node (RTL_EXPR);
6f086dfc
RS
5576
5577 RTL_EXPR_RTL (rtlexp) = tramp;
5578 trampoline_list = tree_cons (function, rtlexp, trampoline_list);
5579 }
5580
5581 tramp = fix_lexical_addr (XEXP (tramp, 0), function);
5582 return round_trampoline_addr (tramp);
5583}
5584
5585/* Given a trampoline address,
5586 round it to multiple of TRAMPOLINE_ALIGNMENT. */
5587
5588static rtx
5589round_trampoline_addr (tramp)
5590 rtx tramp;
5591{
5592#ifdef TRAMPOLINE_ALIGNMENT
5593 /* Round address up to desired boundary. */
5594 rtx temp = gen_reg_rtx (Pmode);
5595 temp = expand_binop (Pmode, add_optab, tramp,
b02ab63a 5596 GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1),
6f086dfc
RS
5597 temp, 0, OPTAB_LIB_WIDEN);
5598 tramp = expand_binop (Pmode, and_optab, temp,
718fe406 5599 GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT),
6f086dfc
RS
5600 temp, 0, OPTAB_LIB_WIDEN);
5601#endif
5602 return tramp;
5603}
5604\f
b2a59b15
MS
5605/* Put all this function's BLOCK nodes including those that are chained
5606 onto the first block into a vector, and return it.
467456d0
RS
5607 Also store in each NOTE for the beginning or end of a block
5608 the index of that block in the vector.
b2a59b15 5609 The arguments are BLOCK, the chain of top-level blocks of the function,
467456d0
RS
5610 and INSNS, the insn chain of the function. */
5611
1a4450c7 5612void
116eebd6 5613identify_blocks ()
467456d0 5614{
fc289cd1 5615 int n_blocks;
0a1c58a2 5616 tree *block_vector, *last_block_vector;
1a4450c7 5617 tree *block_stack;
116eebd6 5618 tree block = DECL_INITIAL (current_function_decl);
467456d0 5619
b2a59b15 5620 if (block == 0)
1a4450c7 5621 return;
fc289cd1 5622
1a4450c7
MM
5623 /* Fill the BLOCK_VECTOR with all of the BLOCKs in this function, in
5624 depth-first order. */
18c038b9 5625 block_vector = get_block_vector (block, &n_blocks);
4da896b2 5626 block_stack = (tree *) xmalloc (n_blocks * sizeof (tree));
1a4450c7 5627
718fe406 5628 last_block_vector = identify_blocks_1 (get_insns (),
116eebd6 5629 block_vector + 1,
718fe406 5630 block_vector + n_blocks,
116eebd6 5631 block_stack);
0a1c58a2
JL
5632
5633 /* If we didn't use all of the subblocks, we've misplaced block notes. */
5634 /* ??? This appears to happen all the time. Latent bugs elsewhere? */
5635 if (0 && last_block_vector != block_vector + n_blocks)
5636 abort ();
5637
5638 free (block_vector);
5639 free (block_stack);
5640}
5641
5642/* Subroutine of identify_blocks. Do the block substitution on the
5643 insn chain beginning with INSNS. Recurse for CALL_PLACEHOLDER chains.
5644
5645 BLOCK_STACK is pushed and popped for each BLOCK_BEGIN/BLOCK_END pair.
5646 BLOCK_VECTOR is incremented for each block seen. */
5647
5648static tree *
5649identify_blocks_1 (insns, block_vector, end_block_vector, orig_block_stack)
5650 rtx insns;
5651 tree *block_vector;
5652 tree *end_block_vector;
5653 tree *orig_block_stack;
5654{
5655 rtx insn;
5656 tree *block_stack = orig_block_stack;
5657
467456d0 5658 for (insn = insns; insn; insn = NEXT_INSN (insn))
0a1c58a2
JL
5659 {
5660 if (GET_CODE (insn) == NOTE)
5661 {
5662 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5663 {
5664 tree b;
1a4450c7 5665
0a1c58a2
JL
5666 /* If there are more block notes than BLOCKs, something
5667 is badly wrong. */
5668 if (block_vector == end_block_vector)
5669 abort ();
e6fd097e 5670
0a1c58a2
JL
5671 b = *block_vector++;
5672 NOTE_BLOCK (insn) = b;
5673 *block_stack++ = b;
5674 }
5675 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5676 {
5677 /* If there are more NOTE_INSN_BLOCK_ENDs than
5678 NOTE_INSN_BLOCK_BEGs, something is badly wrong. */
5679 if (block_stack == orig_block_stack)
5680 abort ();
e6fd097e 5681
0a1c58a2
JL
5682 NOTE_BLOCK (insn) = *--block_stack;
5683 }
718fe406 5684 }
0a1c58a2
JL
5685 else if (GET_CODE (insn) == CALL_INSN
5686 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5687 {
5688 rtx cp = PATTERN (insn);
5689
718fe406
KH
5690 block_vector = identify_blocks_1 (XEXP (cp, 0), block_vector,
5691 end_block_vector, block_stack);
0a1c58a2
JL
5692 if (XEXP (cp, 1))
5693 block_vector = identify_blocks_1 (XEXP (cp, 1), block_vector,
5694 end_block_vector, block_stack);
5695 if (XEXP (cp, 2))
5696 block_vector = identify_blocks_1 (XEXP (cp, 2), block_vector,
5697 end_block_vector, block_stack);
5698 }
5699 }
467456d0 5700
0a1c58a2
JL
5701 /* If there are more NOTE_INSN_BLOCK_BEGINs than NOTE_INSN_BLOCK_ENDs,
5702 something is badly wrong. */
5703 if (block_stack != orig_block_stack)
5704 abort ();
5705
5706 return block_vector;
467456d0
RS
5707}
5708
116eebd6 5709/* Identify BLOCKs referenced by more than one
718fe406 5710 NOTE_INSN_BLOCK_{BEG,END}, and create duplicate blocks. */
467456d0 5711
116eebd6
MM
5712void
5713reorder_blocks ()
467456d0 5714{
116eebd6 5715 tree block = DECL_INITIAL (current_function_decl);
18c038b9 5716 varray_type block_stack;
467456d0 5717
1a4450c7 5718 if (block == NULL_TREE)
116eebd6 5719 return;
fc289cd1 5720
18c038b9
MM
5721 VARRAY_TREE_INIT (block_stack, 10, "block_stack");
5722
116eebd6
MM
5723 /* Prune the old trees away, so that they don't get in the way. */
5724 BLOCK_SUBBLOCKS (block) = NULL_TREE;
5725 BLOCK_CHAIN (block) = NULL_TREE;
fc289cd1 5726
116eebd6 5727 reorder_blocks_1 (get_insns (), block, &block_stack);
467456d0 5728
718fe406 5729 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
18c038b9
MM
5730
5731 VARRAY_FREE (block_stack);
467456d0
RS
5732}
5733
0a1c58a2
JL
5734/* Helper function for reorder_blocks. Process the insn chain beginning
5735 at INSNS. Recurse for CALL_PLACEHOLDER insns. */
5736
5737static void
5738reorder_blocks_1 (insns, current_block, p_block_stack)
5739 rtx insns;
5740 tree current_block;
5741 varray_type *p_block_stack;
5742{
5743 rtx insn;
5744
5745 for (insn = insns; insn; insn = NEXT_INSN (insn))
5746 {
5747 if (GET_CODE (insn) == NOTE)
5748 {
5749 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5750 {
5751 tree block = NOTE_BLOCK (insn);
5752 /* If we have seen this block before, copy it. */
5753 if (TREE_ASM_WRITTEN (block))
5754 {
5755 block = copy_node (block);
5756 NOTE_BLOCK (insn) = block;
5757 }
5758 BLOCK_SUBBLOCKS (block) = 0;
5759 TREE_ASM_WRITTEN (block) = 1;
718fe406 5760 BLOCK_SUPERCONTEXT (block) = current_block;
0a1c58a2
JL
5761 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
5762 BLOCK_SUBBLOCKS (current_block) = block;
5763 current_block = block;
5764 VARRAY_PUSH_TREE (*p_block_stack, block);
5765 }
5766 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5767 {
5768 NOTE_BLOCK (insn) = VARRAY_TOP_TREE (*p_block_stack);
5769 VARRAY_POP (*p_block_stack);
5770 BLOCK_SUBBLOCKS (current_block)
5771 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
5772 current_block = BLOCK_SUPERCONTEXT (current_block);
5773 }
5774 }
5775 else if (GET_CODE (insn) == CALL_INSN
5776 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
5777 {
5778 rtx cp = PATTERN (insn);
5779 reorder_blocks_1 (XEXP (cp, 0), current_block, p_block_stack);
5780 if (XEXP (cp, 1))
5781 reorder_blocks_1 (XEXP (cp, 1), current_block, p_block_stack);
5782 if (XEXP (cp, 2))
5783 reorder_blocks_1 (XEXP (cp, 2), current_block, p_block_stack);
5784 }
5785 }
5786}
5787
467456d0
RS
5788/* Reverse the order of elements in the chain T of blocks,
5789 and return the new head of the chain (old last element). */
5790
5791static tree
5792blocks_nreverse (t)
5793 tree t;
5794{
5795 register tree prev = 0, decl, next;
5796 for (decl = t; decl; decl = next)
5797 {
5798 next = BLOCK_CHAIN (decl);
5799 BLOCK_CHAIN (decl) = prev;
5800 prev = decl;
5801 }
5802 return prev;
5803}
5804
18c038b9
MM
5805/* Count the subblocks of the list starting with BLOCK. If VECTOR is
5806 non-NULL, list them all into VECTOR, in a depth-first preorder
5807 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
b2a59b15 5808 blocks. */
467456d0
RS
5809
5810static int
5811all_blocks (block, vector)
5812 tree block;
5813 tree *vector;
5814{
b2a59b15
MS
5815 int n_blocks = 0;
5816
5817 while (block)
5818 {
5819 TREE_ASM_WRITTEN (block) = 0;
5820
5821 /* Record this block. */
5822 if (vector)
5823 vector[n_blocks] = block;
5824
5825 ++n_blocks;
718fe406 5826
b2a59b15
MS
5827 /* Record the subblocks, and their subblocks... */
5828 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
5829 vector ? vector + n_blocks : 0);
5830 block = BLOCK_CHAIN (block);
5831 }
467456d0
RS
5832
5833 return n_blocks;
5834}
18c038b9
MM
5835
5836/* Return a vector containing all the blocks rooted at BLOCK. The
5837 number of elements in the vector is stored in N_BLOCKS_P. The
5838 vector is dynamically allocated; it is the caller's responsibility
5839 to call `free' on the pointer returned. */
718fe406 5840
18c038b9
MM
5841static tree *
5842get_block_vector (block, n_blocks_p)
5843 tree block;
5844 int *n_blocks_p;
5845{
5846 tree *block_vector;
5847
5848 *n_blocks_p = all_blocks (block, NULL);
5849 block_vector = (tree *) xmalloc (*n_blocks_p * sizeof (tree));
5850 all_blocks (block, block_vector);
5851
5852 return block_vector;
5853}
5854
5855static int next_block_index = 2;
5856
5857/* Set BLOCK_NUMBER for all the blocks in FN. */
5858
5859void
5860number_blocks (fn)
5861 tree fn;
5862{
5863 int i;
5864 int n_blocks;
5865 tree *block_vector;
5866
5867 /* For SDB and XCOFF debugging output, we start numbering the blocks
5868 from 1 within each function, rather than keeping a running
5869 count. */
5870#if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
b0e3a658
RK
5871 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
5872 next_block_index = 1;
18c038b9
MM
5873#endif
5874
5875 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
5876
5877 /* The top-level BLOCK isn't numbered at all. */
5878 for (i = 1; i < n_blocks; ++i)
5879 /* We number the blocks from two. */
5880 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
5881
5882 free (block_vector);
5883
5884 return;
5885}
467456d0 5886\f
b384405b
BS
5887/* Allocate a function structure and reset its contents to the defaults. */
5888static void
5889prepare_function_start ()
6f086dfc 5890{
01d939e8 5891 cfun = (struct function *) xcalloc (1, sizeof (struct function));
e2ecd91c 5892
6f086dfc 5893 init_stmt_for_function ();
fa51b01b 5894 init_eh_for_function ();
6f086dfc
RS
5895
5896 cse_not_expected = ! optimize;
5897
5898 /* Caller save not needed yet. */
5899 caller_save_needed = 0;
5900
5901 /* No stack slots have been made yet. */
5902 stack_slot_list = 0;
5903
b384405b
BS
5904 current_function_has_nonlocal_label = 0;
5905 current_function_has_nonlocal_goto = 0;
5906
6f086dfc 5907 /* There is no stack slot for handling nonlocal gotos. */
ba716ac9 5908 nonlocal_goto_handler_slots = 0;
6f086dfc
RS
5909 nonlocal_goto_stack_level = 0;
5910
5911 /* No labels have been declared for nonlocal use. */
5912 nonlocal_labels = 0;
e881bb1b 5913 nonlocal_goto_handler_labels = 0;
6f086dfc
RS
5914
5915 /* No function calls so far in this function. */
5916 function_call_count = 0;
5917
5918 /* No parm regs have been allocated.
5919 (This is important for output_inline_function.) */
5920 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
5921
5922 /* Initialize the RTL mechanism. */
5923 init_emit ();
5924
5925 /* Initialize the queue of pending postincrement and postdecrements,
5926 and some other info in expr.c. */
5927 init_expr ();
718fe406 5928
6f086dfc
RS
5929 /* We haven't done register allocation yet. */
5930 reg_renumber = 0;
5931
01d939e8 5932 init_varasm_status (cfun);
6f086dfc 5933
e2ecd91c 5934 /* Clear out data used for inlining. */
01d939e8
BS
5935 cfun->inlinable = 0;
5936 cfun->original_decl_initial = 0;
718fe406 5937 cfun->original_arg_vector = 0;
e2ecd91c 5938
c2f8b491 5939#ifdef STACK_BOUNDARY
c487e484 5940 cfun->stack_alignment_needed = STACK_BOUNDARY;
c2f8b491 5941 cfun->preferred_stack_boundary = STACK_BOUNDARY;
c487e484
RE
5942#else
5943 cfun->stack_alignment_needed = 0;
0a1c58a2 5944 cfun->preferred_stack_boundary = 0;
c2f8b491 5945#endif
a0871656 5946
6f086dfc
RS
5947 /* Set if a call to setjmp is seen. */
5948 current_function_calls_setjmp = 0;
5949
5950 /* Set if a call to longjmp is seen. */
5951 current_function_calls_longjmp = 0;
5952
5953 current_function_calls_alloca = 0;
6f086dfc 5954 current_function_contains_functions = 0;
54ff41b7 5955 current_function_is_leaf = 0;
fb13d4d0 5956 current_function_nothrow = 0;
fdb8a883 5957 current_function_sp_is_unchanging = 0;
54ff41b7 5958 current_function_uses_only_leaf_regs = 0;
acd693d1 5959 current_function_has_computed_jump = 0;
173cd503 5960 current_function_is_thunk = 0;
6f086dfc
RS
5961
5962 current_function_returns_pcc_struct = 0;
5963 current_function_returns_struct = 0;
5964 current_function_epilogue_delay_list = 0;
5965 current_function_uses_const_pool = 0;
5966 current_function_uses_pic_offset_table = 0;
aeb302bb 5967 current_function_cannot_inline = 0;
6f086dfc
RS
5968
5969 /* We have not yet needed to make a label to jump to for tail-recursion. */
5970 tail_recursion_label = 0;
5971
5972 /* We haven't had a need to make a save area for ap yet. */
6f086dfc
RS
5973 arg_pointer_save_area = 0;
5974
5975 /* No stack slots allocated yet. */
5976 frame_offset = 0;
5977
5978 /* No SAVE_EXPRs in this function yet. */
5979 save_expr_regs = 0;
5980
5981 /* No RTL_EXPRs in this function yet. */
5982 rtl_expr_chain = 0;
5983
bc0ebdf9
RK
5984 /* Set up to allocate temporaries. */
5985 init_temp_slots ();
6f086dfc 5986
b384405b
BS
5987 /* Indicate that we need to distinguish between the return value of the
5988 present function and the return value of a function being called. */
5989 rtx_equal_function_value_matters = 1;
5990
5991 /* Indicate that we have not instantiated virtual registers yet. */
5992 virtuals_instantiated = 0;
5993
1b3d8f8a
GK
5994 /* Indicate that we want CONCATs now. */
5995 generating_concat_p = 1;
5996
b384405b
BS
5997 /* Indicate we have no need of a frame pointer yet. */
5998 frame_pointer_needed = 0;
5999
6000 /* By default assume not varargs or stdarg. */
6001 current_function_varargs = 0;
6002 current_function_stdarg = 0;
6f086dfc 6003
d9a98e1a
RK
6004 /* We haven't made any trampolines for this function yet. */
6005 trampoline_list = 0;
6006
6f086dfc
RS
6007 init_pending_stack_adjust ();
6008 inhibit_defer_pop = 0;
6009
6010 current_function_outgoing_args_size = 0;
36edd3cc 6011
0a8a198c 6012 if (init_lang_status)
01d939e8 6013 (*init_lang_status) (cfun);
36edd3cc 6014 if (init_machine_status)
01d939e8 6015 (*init_machine_status) (cfun);
b384405b
BS
6016}
6017
6018/* Initialize the rtl expansion mechanism so that we can do simple things
6019 like generate sequences. This is used to provide a context during global
6020 initialization of some passes. */
6021void
6022init_dummy_function_start ()
6023{
6024 prepare_function_start ();
6025}
6026
6027/* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
6028 and initialize static variables for generating RTL for the statements
6029 of the function. */
6030
6031void
6032init_function_start (subr, filename, line)
6033 tree subr;
36244024 6034 const char *filename;
b384405b
BS
6035 int line;
6036{
6037 prepare_function_start ();
6038
6039 /* Remember this function for later. */
01d939e8
BS
6040 cfun->next_global = all_functions;
6041 all_functions = cfun;
718fe406 6042
b384405b 6043 current_function_name = (*decl_printable_name) (subr, 2);
01d939e8 6044 cfun->decl = subr;
b384405b
BS
6045
6046 /* Nonzero if this is a nested function that uses a static chain. */
6047
6048 current_function_needs_context
6049 = (decl_function_context (current_function_decl) != 0
6050 && ! DECL_NO_STATIC_CHAIN (current_function_decl));
6051
6052 /* Within function body, compute a type's size as soon it is laid out. */
6053 immediate_size_expand++;
6f086dfc 6054
6f086dfc 6055 /* Prevent ever trying to delete the first instruction of a function.
b274104c 6056 Also tell final how to output a linenum before the function prologue.
718fe406 6057 Note linenums could be missing, e.g. when compiling a Java .class file. */
b274104c
PB
6058 if (line > 0)
6059 emit_line_note (filename, line);
6f086dfc
RS
6060
6061 /* Make sure first insn is a note even if we don't want linenums.
6062 This makes sure the first insn will never be deleted.
6063 Also, final expects a note to appear there. */
5f4f0e22 6064 emit_note (NULL_PTR, NOTE_INSN_DELETED);
6f086dfc
RS
6065
6066 /* Set flags used by final.c. */
6067 if (aggregate_value_p (DECL_RESULT (subr)))
6068 {
6069#ifdef PCC_STATIC_STRUCT_RETURN
1b8297c1 6070 current_function_returns_pcc_struct = 1;
6f086dfc 6071#endif
1b8297c1 6072 current_function_returns_struct = 1;
6f086dfc
RS
6073 }
6074
6075 /* Warn if this value is an aggregate type,
6076 regardless of which calling convention we are using for it. */
6077 if (warn_aggregate_return
05e3bdb9 6078 && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
6f086dfc
RS
6079 warning ("function returns an aggregate");
6080
6081 current_function_returns_pointer
8eda074c 6082 = POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
49ad7cfa 6083}
5c7675e9 6084
49ad7cfa
BS
6085/* Make sure all values used by the optimization passes have sane
6086 defaults. */
6087void
6088init_function_for_compilation ()
6089{
6090 reg_renumber = 0;
0a1c58a2 6091
5c7675e9 6092 /* No prologue/epilogue insns yet. */
0a1c58a2
JL
6093 VARRAY_GROW (prologue, 0);
6094 VARRAY_GROW (epilogue, 0);
6095 VARRAY_GROW (sibcall_epilogue, 0);
6f086dfc
RS
6096}
6097
6098/* Indicate that the current function uses extra args
6099 not explicitly mentioned in the argument list in any fashion. */
6100
6101void
6102mark_varargs ()
6103{
6104 current_function_varargs = 1;
6105}
6106
6107/* Expand a call to __main at the beginning of a possible main function. */
6108
e2fd1d94
JM
6109#if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
6110#undef HAS_INIT_SECTION
6111#define HAS_INIT_SECTION
6112#endif
6113
6f086dfc
RS
6114void
6115expand_main_function ()
6116{
e2fd1d94 6117#if !defined (HAS_INIT_SECTION)
b93a436e
JL
6118 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, NAME__MAIN), 0,
6119 VOIDmode, 0);
e2fd1d94 6120#endif /* not HAS_INIT_SECTION */
6f086dfc
RS
6121}
6122\f
c20bf1f3
JB
6123extern struct obstack permanent_obstack;
6124
6f086dfc
RS
6125/* Start the RTL for a new function, and set variables used for
6126 emitting RTL.
6127 SUBR is the FUNCTION_DECL node.
6128 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
6129 the function's parameters, which must be run at any return statement. */
6130
6131void
6132expand_function_start (subr, parms_have_cleanups)
6133 tree subr;
6134 int parms_have_cleanups;
6135{
6f086dfc 6136 tree tem;
4e86caed 6137 rtx last_ptr = NULL_RTX;
6f086dfc
RS
6138
6139 /* Make sure volatile mem refs aren't considered
6140 valid operands of arithmetic insns. */
6141 init_recog_no_volatile ();
6142
7d384cc0
KR
6143 /* Set this before generating any memory accesses. */
6144 current_function_check_memory_usage
6145 = (flag_check_memory_usage
6146 && ! DECL_NO_CHECK_MEMORY_USAGE (current_function_decl));
6147
07417085
KR
6148 current_function_instrument_entry_exit
6149 = (flag_instrument_function_entry_exit
6150 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6151
a157febd
GK
6152 current_function_limit_stack
6153 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
6154
6f086dfc
RS
6155 /* If function gets a static chain arg, store it in the stack frame.
6156 Do this first, so it gets the first stack slot offset. */
6157 if (current_function_needs_context)
3e2481e9
JW
6158 {
6159 last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
f0c51a1e 6160
f0c51a1e
RK
6161 /* Delay copying static chain if it is not a register to avoid
6162 conflicts with regs used for parameters. */
f95182a4
ILT
6163 if (! SMALL_REGISTER_CLASSES
6164 || GET_CODE (static_chain_incoming_rtx) == REG)
718fe406 6165 emit_move_insn (last_ptr, static_chain_incoming_rtx);
3e2481e9 6166 }
6f086dfc
RS
6167
6168 /* If the parameters of this function need cleaning up, get a label
6169 for the beginning of the code which executes those cleanups. This must
6170 be done before doing anything with return_label. */
6171 if (parms_have_cleanups)
6172 cleanup_label = gen_label_rtx ();
6173 else
6174 cleanup_label = 0;
6175
6176 /* Make the label for return statements to jump to, if this machine
6177 does not have a one-instruction return and uses an epilogue,
6178 or if it returns a structure, or if it has parm cleanups. */
6179#ifdef HAVE_return
6180 if (cleanup_label == 0 && HAVE_return
07417085 6181 && ! current_function_instrument_entry_exit
6f086dfc
RS
6182 && ! current_function_returns_pcc_struct
6183 && ! (current_function_returns_struct && ! optimize))
6184 return_label = 0;
6185 else
6186 return_label = gen_label_rtx ();
6187#else
6188 return_label = gen_label_rtx ();
6189#endif
6190
6191 /* Initialize rtx used to return the value. */
6192 /* Do this before assign_parms so that we copy the struct value address
6193 before any library calls that assign parms might generate. */
6194
6195 /* Decide whether to return the value in memory or in a register. */
6196 if (aggregate_value_p (DECL_RESULT (subr)))
6197 {
6198 /* Returning something that won't go in a register. */
4acc00bf 6199 register rtx value_address = 0;
6f086dfc
RS
6200
6201#ifdef PCC_STATIC_STRUCT_RETURN
6202 if (current_function_returns_pcc_struct)
6203 {
6204 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
6205 value_address = assemble_static_space (size);
6206 }
6207 else
6208#endif
6209 {
6210 /* Expect to be passed the address of a place to store the value.
6211 If it is passed as an argument, assign_parms will take care of
6212 it. */
6213 if (struct_value_incoming_rtx)
6214 {
6215 value_address = gen_reg_rtx (Pmode);
6216 emit_move_insn (value_address, struct_value_incoming_rtx);
6217 }
6218 }
6219 if (value_address)
ccdecf58
RK
6220 {
6221 DECL_RTL (DECL_RESULT (subr))
38a448ca 6222 = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), value_address);
3bdf5ad1
RK
6223 set_mem_attributes (DECL_RTL (DECL_RESULT (subr)),
6224 DECL_RESULT (subr), 1);
ccdecf58 6225 }
6f086dfc
RS
6226 }
6227 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
6228 /* If return mode is void, this decl rtl should not be used. */
6229 DECL_RTL (DECL_RESULT (subr)) = 0;
07417085 6230 else if (parms_have_cleanups || current_function_instrument_entry_exit)
a53e14c0
RK
6231 {
6232 /* If function will end with cleanup code for parms,
6233 compute the return values into a pseudo reg,
6234 which we will copy into the true return register
6235 after the cleanups are done. */
6236
6237 enum machine_mode mode = DECL_MODE (DECL_RESULT (subr));
a5a52dbc 6238
a53e14c0
RK
6239#ifdef PROMOTE_FUNCTION_RETURN
6240 tree type = TREE_TYPE (DECL_RESULT (subr));
6241 int unsignedp = TREE_UNSIGNED (type);
6242
a5a52dbc 6243 mode = promote_mode (type, mode, &unsignedp, 1);
a53e14c0
RK
6244#endif
6245
6246 DECL_RTL (DECL_RESULT (subr)) = gen_reg_rtx (mode);
6247 }
6f086dfc
RS
6248 else
6249 /* Scalar, returned in a register. */
6250 {
6f086dfc 6251 DECL_RTL (DECL_RESULT (subr))
ab36bd3e 6252 = hard_function_value (TREE_TYPE (DECL_RESULT (subr)), subr, 1);
6f086dfc
RS
6253
6254 /* Mark this reg as the function's return value. */
6255 if (GET_CODE (DECL_RTL (DECL_RESULT (subr))) == REG)
6256 {
6257 REG_FUNCTION_VALUE_P (DECL_RTL (DECL_RESULT (subr))) = 1;
6258 /* Needed because we may need to move this to memory
6259 in case it's a named return value whose address is taken. */
a82ad570 6260 DECL_REGISTER (DECL_RESULT (subr)) = 1;
6f086dfc
RS
6261 }
6262 }
6263
6264 /* Initialize rtx for parameters and local variables.
6265 In some cases this requires emitting insns. */
6266
0d1416c6 6267 assign_parms (subr);
6f086dfc 6268
f0c51a1e
RK
6269 /* Copy the static chain now if it wasn't a register. The delay is to
6270 avoid conflicts with the parameter passing registers. */
6271
f95182a4 6272 if (SMALL_REGISTER_CLASSES && current_function_needs_context)
f0c51a1e
RK
6273 if (GET_CODE (static_chain_incoming_rtx) != REG)
6274 emit_move_insn (last_ptr, static_chain_incoming_rtx);
f0c51a1e 6275
6f086dfc
RS
6276 /* The following was moved from init_function_start.
6277 The move is supposed to make sdb output more accurate. */
6278 /* Indicate the beginning of the function body,
6279 as opposed to parm setup. */
5f4f0e22 6280 emit_note (NULL_PTR, NOTE_INSN_FUNCTION_BEG);
6f086dfc 6281
6f086dfc 6282 if (GET_CODE (get_last_insn ()) != NOTE)
5f4f0e22 6283 emit_note (NULL_PTR, NOTE_INSN_DELETED);
6f086dfc
RS
6284 parm_birth_insn = get_last_insn ();
6285
6d7306f7
JM
6286 context_display = 0;
6287 if (current_function_needs_context)
ac9e20f0 6288 {
6d7306f7
JM
6289 /* Fetch static chain values for containing functions. */
6290 tem = decl_function_context (current_function_decl);
d29c259b
RH
6291 /* Copy the static chain pointer into a pseudo. If we have
6292 small register classes, copy the value from memory if
6293 static_chain_incoming_rtx is a REG. */
6294 if (tem)
6d7306f7 6295 {
6d7306f7
JM
6296 /* If the static chain originally came in a register, put it back
6297 there, then move it out in the next insn. The reason for
6298 this peculiar code is to satisfy function integration. */
f95182a4
ILT
6299 if (SMALL_REGISTER_CLASSES
6300 && GET_CODE (static_chain_incoming_rtx) == REG)
6d7306f7 6301 emit_move_insn (static_chain_incoming_rtx, last_ptr);
6d7306f7
JM
6302 last_ptr = copy_to_reg (static_chain_incoming_rtx);
6303 }
ac9e20f0 6304
6d7306f7
JM
6305 while (tem)
6306 {
6307 tree rtlexp = make_node (RTL_EXPR);
6f086dfc 6308
6d7306f7
JM
6309 RTL_EXPR_RTL (rtlexp) = last_ptr;
6310 context_display = tree_cons (tem, rtlexp, context_display);
6311 tem = decl_function_context (tem);
6312 if (tem == 0)
6313 break;
6314 /* Chain thru stack frames, assuming pointer to next lexical frame
6315 is found at the place we always store it. */
6f086dfc 6316#ifdef FRAME_GROWS_DOWNWARD
768f0669
JJ
6317 last_ptr = plus_constant (last_ptr,
6318 -(HOST_WIDE_INT) GET_MODE_SIZE (Pmode));
6f086dfc 6319#endif
3bdf5ad1
RK
6320 last_ptr = gen_rtx_MEM (Pmode, memory_address (Pmode, last_ptr));
6321 MEM_ALIAS_SET (last_ptr) = get_frame_alias_set ();
6322 last_ptr = copy_to_reg (last_ptr);
6d7306f7
JM
6323
6324 /* If we are not optimizing, ensure that we know that this
6325 piece of context is live over the entire function. */
6326 if (! optimize)
38a448ca
RH
6327 save_expr_regs = gen_rtx_EXPR_LIST (VOIDmode, last_ptr,
6328 save_expr_regs);
6d7306f7 6329 }
6f086dfc
RS
6330 }
6331
07417085
KR
6332 if (current_function_instrument_entry_exit)
6333 {
6334 rtx fun = DECL_RTL (current_function_decl);
6335 if (GET_CODE (fun) == MEM)
6336 fun = XEXP (fun, 0);
6337 else
6338 abort ();
6339 emit_library_call (profile_function_entry_libfunc, 0, VOIDmode, 2,
6340 fun, Pmode,
6341 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6342 0,
6343 hard_frame_pointer_rtx),
6344 Pmode);
6345 }
6346
6f086dfc
RS
6347 /* After the display initializations is where the tail-recursion label
6348 should go, if we end up needing one. Ensure we have a NOTE here
6349 since some things (like trampolines) get placed before this. */
5f4f0e22 6350 tail_recursion_reentry = emit_note (NULL_PTR, NOTE_INSN_DELETED);
6f086dfc
RS
6351
6352 /* Evaluate now the sizes of any types declared among the arguments. */
6353 for (tem = nreverse (get_pending_sizes ()); tem; tem = TREE_CHAIN (tem))
7b05e286 6354 {
86fa911a
RK
6355 expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode,
6356 EXPAND_MEMORY_USE_BAD);
7b05e286
JW
6357 /* Flush the queue in case this parameter declaration has
6358 side-effects. */
6359 emit_queue ();
6360 }
6f086dfc
RS
6361
6362 /* Make sure there is a line number after the function entry setup code. */
6363 force_next_line_note ();
6364}
6365\f
49ad7cfa
BS
6366/* Undo the effects of init_dummy_function_start. */
6367void
6368expand_dummy_function_end ()
6369{
6370 /* End any sequences that failed to be closed due to syntax errors. */
6371 while (in_sequence_p ())
6372 end_sequence ();
6373
6374 /* Outside function body, can't compute type's actual size
6375 until next function's body starts. */
fa51b01b 6376
01d939e8
BS
6377 free_after_parsing (cfun);
6378 free_after_compilation (cfun);
6379 free (cfun);
6380 cfun = 0;
49ad7cfa
BS
6381}
6382
c13fde05
RH
6383/* Call DOIT for each hard register used as a return value from
6384 the current function. */
bd695e1e
RH
6385
6386void
c13fde05
RH
6387diddle_return_value (doit, arg)
6388 void (*doit) PARAMS ((rtx, void *));
6389 void *arg;
bd695e1e 6390{
c13fde05 6391 rtx outgoing = current_function_return_rtx;
5828d725 6392 int pcc;
c13fde05
RH
6393
6394 if (! outgoing)
6395 return;
bd695e1e 6396
5828d725 6397 pcc = (current_function_returns_struct
718fe406 6398 || current_function_returns_pcc_struct);
5828d725
RH
6399
6400 if ((GET_CODE (outgoing) == REG
6401 && REGNO (outgoing) >= FIRST_PSEUDO_REGISTER)
6402 || pcc)
bd695e1e 6403 {
c13fde05 6404 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5828d725
RH
6405
6406 /* A PCC-style return returns a pointer to the memory in which
6407 the structure is stored. */
6408 if (pcc)
6409 type = build_pointer_type (type);
6410
c13fde05
RH
6411#ifdef FUNCTION_OUTGOING_VALUE
6412 outgoing = FUNCTION_OUTGOING_VALUE (type, current_function_decl);
6413#else
6414 outgoing = FUNCTION_VALUE (type, current_function_decl);
6415#endif
6416 /* If this is a BLKmode structure being returned in registers, then use
6417 the mode computed in expand_return. */
6418 if (GET_MODE (outgoing) == BLKmode)
3e4eac3f 6419 PUT_MODE (outgoing, GET_MODE (current_function_return_rtx));
5828d725 6420 REG_FUNCTION_VALUE_P (outgoing) = 1;
c13fde05 6421 }
bd695e1e 6422
c13fde05
RH
6423 if (GET_CODE (outgoing) == REG)
6424 (*doit) (outgoing, arg);
6425 else if (GET_CODE (outgoing) == PARALLEL)
6426 {
6427 int i;
bd695e1e 6428
c13fde05
RH
6429 for (i = 0; i < XVECLEN (outgoing, 0); i++)
6430 {
6431 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
6432
6433 if (GET_CODE (x) == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
6434 (*doit) (x, arg);
bd695e1e
RH
6435 }
6436 }
6437}
6438
c13fde05
RH
6439static void
6440do_clobber_return_reg (reg, arg)
6441 rtx reg;
6442 void *arg ATTRIBUTE_UNUSED;
6443{
6444 emit_insn (gen_rtx_CLOBBER (VOIDmode, reg));
6445}
6446
6447void
6448clobber_return_register ()
6449{
6450 diddle_return_value (do_clobber_return_reg, NULL);
6451}
6452
6453static void
6454do_use_return_reg (reg, arg)
6455 rtx reg;
6456 void *arg ATTRIBUTE_UNUSED;
6457{
6458 emit_insn (gen_rtx_USE (VOIDmode, reg));
6459}
6460
6461void
6462use_return_register ()
6463{
6464 diddle_return_value (do_use_return_reg, NULL);
6465}
6466
6f086dfc 6467/* Generate RTL for the end of the current function.
718fe406 6468 FILENAME and LINE are the current position in the source file.
6f086dfc 6469
980697fd 6470 It is up to language-specific callers to do cleanups for parameters--
1be07046 6471 or else, supply 1 for END_BINDINGS and we will call expand_end_bindings. */
6f086dfc
RS
6472
6473void
1be07046 6474expand_function_end (filename, line, end_bindings)
3b304f5b 6475 const char *filename;
6f086dfc 6476 int line;
1be07046 6477 int end_bindings;
6f086dfc 6478{
6f086dfc
RS
6479 tree link;
6480
1e2414db 6481#ifdef TRAMPOLINE_TEMPLATE
6f086dfc 6482 static rtx initial_trampoline;
1e2414db 6483#endif
6f086dfc 6484
49ad7cfa
BS
6485 finish_expr_for_function ();
6486
6f086dfc
RS
6487#ifdef NON_SAVING_SETJMP
6488 /* Don't put any variables in registers if we call setjmp
6489 on a machine that fails to restore the registers. */
6490 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
6491 {
b88a3142
RK
6492 if (DECL_INITIAL (current_function_decl) != error_mark_node)
6493 setjmp_protect (DECL_INITIAL (current_function_decl));
6494
6f086dfc
RS
6495 setjmp_protect_args ();
6496 }
6497#endif
6498
6499 /* Save the argument pointer if a save area was made for it. */
6500 if (arg_pointer_save_area)
6501 {
ea0f9a85
JW
6502 /* arg_pointer_save_area may not be a valid memory address, so we
6503 have to check it and fix it if necessary. */
6504 rtx seq;
6505 start_sequence ();
6506 emit_move_insn (validize_mem (arg_pointer_save_area),
6507 virtual_incoming_args_rtx);
6508 seq = gen_sequence ();
6509 end_sequence ();
6510 emit_insn_before (seq, tail_recursion_reentry);
6f086dfc
RS
6511 }
6512
6513 /* Initialize any trampolines required by this function. */
6514 for (link = trampoline_list; link; link = TREE_CHAIN (link))
6515 {
6516 tree function = TREE_PURPOSE (link);
57bed152 6517 rtx context ATTRIBUTE_UNUSED = lookup_static_chain (function);
6f086dfc 6518 rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
7a87758d 6519#ifdef TRAMPOLINE_TEMPLATE
1e2414db 6520 rtx blktramp;
7a87758d 6521#endif
6f086dfc
RS
6522 rtx seq;
6523
1e2414db 6524#ifdef TRAMPOLINE_TEMPLATE
6f086dfc
RS
6525 /* First make sure this compilation has a template for
6526 initializing trampolines. */
6527 if (initial_trampoline == 0)
86f8eff3 6528 {
86f8eff3 6529 initial_trampoline
38a448ca 6530 = gen_rtx_MEM (BLKmode, assemble_trampoline_template ());
76095e2f
RH
6531
6532 ggc_add_rtx_root (&initial_trampoline, 1);
86f8eff3 6533 }
1e2414db 6534#endif
6f086dfc
RS
6535
6536 /* Generate insns to initialize the trampoline. */
6537 start_sequence ();
1e2414db
RK
6538 tramp = round_trampoline_addr (XEXP (tramp, 0));
6539#ifdef TRAMPOLINE_TEMPLATE
6540 blktramp = change_address (initial_trampoline, BLKmode, tramp);
6541 emit_block_move (blktramp, initial_trampoline,
6542 GEN_INT (TRAMPOLINE_SIZE),
744bfbfa 6543 TRAMPOLINE_ALIGNMENT);
1e2414db
RK
6544#endif
6545 INITIALIZE_TRAMPOLINE (tramp, XEXP (DECL_RTL (function), 0), context);
6f086dfc
RS
6546 seq = get_insns ();
6547 end_sequence ();
6548
6549 /* Put those insns at entry to the containing function (this one). */
6550 emit_insns_before (seq, tail_recursion_reentry);
6551 }
6f086dfc 6552
11044f66
RK
6553 /* If we are doing stack checking and this function makes calls,
6554 do a stack probe at the start of the function to ensure we have enough
6555 space for another stack frame. */
6556 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
6557 {
6558 rtx insn, seq;
6559
6560 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6561 if (GET_CODE (insn) == CALL_INSN)
6562 {
6563 start_sequence ();
6564 probe_stack_range (STACK_CHECK_PROTECT,
6565 GEN_INT (STACK_CHECK_MAX_FRAME_SIZE));
6566 seq = get_insns ();
6567 end_sequence ();
6568 emit_insns_before (seq, tail_recursion_reentry);
6569 break;
6570 }
6571 }
6572
db8717d9 6573 /* Warn about unused parms if extra warnings were specified. */
078721e1
AC
6574 /* Either ``-W -Wunused'' or ``-Wunused-parameter'' enables this
6575 warning. WARN_UNUSED_PARAMETER is negative when set by
718fe406 6576 -Wunused. */
078721e1
AC
6577 if (warn_unused_parameter > 0
6578 || (warn_unused_parameter < 0 && extra_warnings))
6f086dfc 6579 {
db8717d9 6580 tree decl;
6f086dfc
RS
6581
6582 for (decl = DECL_ARGUMENTS (current_function_decl);
6583 decl; decl = TREE_CHAIN (decl))
497dc802
JM
6584 if (! TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
6585 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
6f086dfc
RS
6586 warning_with_decl (decl, "unused parameter `%s'");
6587 }
6f086dfc
RS
6588
6589 /* Delete handlers for nonlocal gotos if nothing uses them. */
ba716ac9
BS
6590 if (nonlocal_goto_handler_slots != 0
6591 && ! current_function_has_nonlocal_label)
6f086dfc
RS
6592 delete_handlers ();
6593
6594 /* End any sequences that failed to be closed due to syntax errors. */
6595 while (in_sequence_p ())
5f4f0e22 6596 end_sequence ();
6f086dfc
RS
6597
6598 /* Outside function body, can't compute type's actual size
6599 until next function's body starts. */
6600 immediate_size_expand--;
6601
6f086dfc
RS
6602 clear_pending_stack_adjust ();
6603 do_pending_stack_adjust ();
6604
6605 /* Mark the end of the function body.
6606 If control reaches this insn, the function can drop through
6607 without returning a value. */
5f4f0e22 6608 emit_note (NULL_PTR, NOTE_INSN_FUNCTION_END);
6f086dfc 6609
82e415a3
DE
6610 /* Must mark the last line number note in the function, so that the test
6611 coverage code can avoid counting the last line twice. This just tells
6612 the code to ignore the immediately following line note, since there
6613 already exists a copy of this note somewhere above. This line number
6614 note is still needed for debugging though, so we can't delete it. */
6615 if (flag_test_coverage)
b3b42a4d 6616 emit_note (NULL_PTR, NOTE_INSN_REPEATED_LINE_NUMBER);
82e415a3 6617
6f086dfc
RS
6618 /* Output a linenumber for the end of the function.
6619 SDB depends on this. */
6620 emit_line_note_force (filename, line);
6621
6622 /* Output the label for the actual return from the function,
6623 if one is expected. This happens either because a function epilogue
6624 is used instead of a return instruction, or because a return was done
6625 with a goto in order to run local cleanups, or because of pcc-style
6626 structure returning. */
6627
6628 if (return_label)
bd695e1e 6629 {
b313a0fe
RH
6630 rtx before, after;
6631
bd695e1e
RH
6632 /* Before the return label, clobber the return registers so that
6633 they are not propogated live to the rest of the function. This
6634 can only happen with functions that drop through; if there had
6635 been a return statement, there would have either been a return
6636 rtx, or a jump to the return label. */
b313a0fe
RH
6637
6638 before = get_last_insn ();
c13fde05 6639 clobber_return_register ();
b313a0fe
RH
6640 after = get_last_insn ();
6641
6642 if (before != after)
6643 cfun->x_clobber_return_insn = after;
bd695e1e
RH
6644
6645 emit_label (return_label);
6646 }
6f086dfc 6647
1be07046
RS
6648 /* C++ uses this. */
6649 if (end_bindings)
6650 expand_end_bindings (0, 0, 0);
6651
e5a1e0e8
MS
6652 /* Now handle any leftover exception regions that may have been
6653 created for the parameters. */
6654 {
6655 rtx last = get_last_insn ();
6656 rtx label;
6657
6658 expand_leftover_cleanups ();
6659
1e4ceb6f
MM
6660 /* If there are any catch_clauses remaining, output them now. */
6661 emit_insns (catch_clauses);
c14f7160 6662 catch_clauses = catch_clauses_last = NULL_RTX;
e5a1e0e8
MS
6663 /* If the above emitted any code, may sure we jump around it. */
6664 if (last != get_last_insn ())
6665 {
6666 label = gen_label_rtx ();
6667 last = emit_jump_insn_after (gen_jump (label), last);
6668 last = emit_barrier_after (last);
6669 emit_label (label);
6670 }
6671 }
6672
07417085
KR
6673 if (current_function_instrument_entry_exit)
6674 {
6675 rtx fun = DECL_RTL (current_function_decl);
6676 if (GET_CODE (fun) == MEM)
6677 fun = XEXP (fun, 0);
6678 else
6679 abort ();
6680 emit_library_call (profile_function_exit_libfunc, 0, VOIDmode, 2,
6681 fun, Pmode,
6682 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6683 0,
6684 hard_frame_pointer_rtx),
6685 Pmode);
6686 }
6687
6f086dfc
RS
6688 /* If we had calls to alloca, and this machine needs
6689 an accurate stack pointer to exit the function,
6690 insert some code to save and restore the stack pointer. */
6691#ifdef EXIT_IGNORE_STACK
6692 if (! EXIT_IGNORE_STACK)
6693#endif
6694 if (current_function_calls_alloca)
6695 {
59257ff7
RK
6696 rtx tem = 0;
6697
6698 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
5f4f0e22 6699 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
6f086dfc
RS
6700 }
6701
3e4eac3f
RH
6702 /* If scalar return value was computed in a pseudo-reg, or was a named
6703 return value that got dumped to the stack, copy that to the hard
6704 return register. */
6705 if (DECL_RTL (DECL_RESULT (current_function_decl)) != 0)
6f086dfc 6706 {
3e4eac3f
RH
6707 tree decl_result = DECL_RESULT (current_function_decl);
6708 rtx decl_rtl = DECL_RTL (decl_result);
6709
6710 if (REG_P (decl_rtl)
6711 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
6712 : DECL_REGISTER (decl_result))
6713 {
6714 rtx real_decl_rtl;
6f086dfc
RS
6715
6716#ifdef FUNCTION_OUTGOING_VALUE
3e4eac3f
RH
6717 real_decl_rtl = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl_result),
6718 current_function_decl);
6f086dfc 6719#else
3e4eac3f
RH
6720 real_decl_rtl = FUNCTION_VALUE (TREE_TYPE (decl_result),
6721 current_function_decl);
6f086dfc 6722#endif
3e4eac3f
RH
6723 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
6724
6725 /* If this is a BLKmode structure being returned in registers,
6726 then use the mode computed in expand_return. Note that if
6727 decl_rtl is memory, then its mode may have been changed,
6728 but that current_function_return_rtx has not. */
6729 if (GET_MODE (real_decl_rtl) == BLKmode)
6730 PUT_MODE (real_decl_rtl, GET_MODE (current_function_return_rtx));
6731
6732 /* If a named return value dumped decl_return to memory, then
6733 we may need to re-do the PROMOTE_MODE signed/unsigned
6734 extension. */
6735 if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
6736 {
6737 int unsignedp = TREE_UNSIGNED (TREE_TYPE (decl_result));
6738
6739#ifdef PROMOTE_FUNCTION_RETURN
6740 promote_mode (TREE_TYPE (decl_result), GET_MODE (decl_rtl),
6741 &unsignedp, 1);
6742#endif
6743
6744 convert_move (real_decl_rtl, decl_rtl, unsignedp);
6745 }
6746 else
6747 emit_move_insn (real_decl_rtl, decl_rtl);
6748
6749 /* The delay slot scheduler assumes that current_function_return_rtx
6750 holds the hard register containing the return value, not a
6751 temporary pseudo. */
6752 current_function_return_rtx = real_decl_rtl;
6753 }
6f086dfc
RS
6754 }
6755
6756 /* If returning a structure, arrange to return the address of the value
6757 in a place where debuggers expect to find it.
6758
6759 If returning a structure PCC style,
6760 the caller also depends on this value.
6761 And current_function_returns_pcc_struct is not necessarily set. */
6762 if (current_function_returns_struct
6763 || current_function_returns_pcc_struct)
6764 {
718fe406
KH
6765 rtx value_address =
6766 XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6f086dfc
RS
6767 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
6768#ifdef FUNCTION_OUTGOING_VALUE
6769 rtx outgoing
6770 = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
6771 current_function_decl);
6772#else
6773 rtx outgoing
6774 = FUNCTION_VALUE (build_pointer_type (type),
6775 current_function_decl);
6776#endif
6777
6778 /* Mark this as a function return value so integrate will delete the
6779 assignment and USE below when inlining this function. */
6780 REG_FUNCTION_VALUE_P (outgoing) = 1;
6781
6782 emit_move_insn (outgoing, value_address);
6f086dfc
RS
6783 }
6784
c13fde05
RH
6785 /* ??? This should no longer be necessary since stupid is no longer with
6786 us, but there are some parts of the compiler (eg reload_combine, and
6787 sh mach_dep_reorg) that still try and compute their own lifetime info
6788 instead of using the general framework. */
6789 use_return_register ();
6790
718fe406 6791 /* If this is an implementation of __throw, do what's necessary to
71038426
RH
6792 communicate between __builtin_eh_return and the epilogue. */
6793 expand_eh_return ();
6794
6f086dfc
RS
6795 /* Output a return insn if we are using one.
6796 Otherwise, let the rtl chain end here, to drop through
6797 into the epilogue. */
6798
6799#ifdef HAVE_return
6800 if (HAVE_return)
6801 {
6802 emit_jump_insn (gen_return ());
6803 emit_barrier ();
6804 }
6805#endif
6806
6807 /* Fix up any gotos that jumped out to the outermost
6808 binding level of the function.
6809 Must follow emitting RETURN_LABEL. */
6810
6811 /* If you have any cleanups to do at this point,
6812 and they need to create temporary variables,
6813 then you will lose. */
e15679f8 6814 expand_fixups (get_insns ());
6f086dfc 6815}
bdac5f58 6816\f
0a1c58a2
JL
6817/* Extend a vector that records the INSN_UIDs of INSNS (either a
6818 sequence or a single insn). */
bdac5f58 6819
0a1c58a2
JL
6820static void
6821record_insns (insns, vecp)
bdac5f58 6822 rtx insns;
0a1c58a2 6823 varray_type *vecp;
bdac5f58 6824{
bdac5f58
TW
6825 if (GET_CODE (insns) == SEQUENCE)
6826 {
6827 int len = XVECLEN (insns, 0);
0a1c58a2
JL
6828 int i = VARRAY_SIZE (*vecp);
6829
6830 VARRAY_GROW (*vecp, i + len);
bdac5f58 6831 while (--len >= 0)
0a1c58a2
JL
6832 {
6833 VARRAY_INT (*vecp, i) = INSN_UID (XVECEXP (insns, 0, len));
6834 ++i;
6835 }
bdac5f58
TW
6836 }
6837 else
6838 {
0a1c58a2
JL
6839 int i = VARRAY_SIZE (*vecp);
6840 VARRAY_GROW (*vecp, i + 1);
6841 VARRAY_INT (*vecp, i) = INSN_UID (insns);
bdac5f58 6842 }
bdac5f58
TW
6843}
6844
10914065 6845/* Determine how many INSN_UIDs in VEC are part of INSN. */
bdac5f58 6846
10914065 6847static int
bdac5f58
TW
6848contains (insn, vec)
6849 rtx insn;
0a1c58a2 6850 varray_type vec;
bdac5f58
TW
6851{
6852 register int i, j;
6853
6854 if (GET_CODE (insn) == INSN
6855 && GET_CODE (PATTERN (insn)) == SEQUENCE)
6856 {
10914065 6857 int count = 0;
bdac5f58 6858 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
0a1c58a2
JL
6859 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
6860 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == VARRAY_INT (vec, j))
10914065
TW
6861 count++;
6862 return count;
bdac5f58
TW
6863 }
6864 else
6865 {
0a1c58a2
JL
6866 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
6867 if (INSN_UID (insn) == VARRAY_INT (vec, j))
10914065 6868 return 1;
bdac5f58
TW
6869 }
6870 return 0;
6871}
5c7675e9
RH
6872
6873int
6874prologue_epilogue_contains (insn)
6875 rtx insn;
6876{
0a1c58a2 6877 if (contains (insn, prologue))
5c7675e9 6878 return 1;
0a1c58a2 6879 if (contains (insn, epilogue))
5c7675e9
RH
6880 return 1;
6881 return 0;
6882}
bdac5f58 6883
0a1c58a2
JL
6884int
6885sibcall_epilogue_contains (insn)
718fe406 6886 rtx insn;
0a1c58a2
JL
6887{
6888 if (sibcall_epilogue)
6889 return contains (insn, sibcall_epilogue);
6890 return 0;
6891}
6892
73ef99fb 6893#ifdef HAVE_return
69732dcb
RH
6894/* Insert gen_return at the end of block BB. This also means updating
6895 block_for_insn appropriately. */
6896
6897static void
86c82654 6898emit_return_into_block (bb, line_note)
69732dcb 6899 basic_block bb;
86c82654 6900 rtx line_note;
69732dcb
RH
6901{
6902 rtx p, end;
6903
718fe406 6904 p = NEXT_INSN (bb->end);
86c82654
RH
6905 end = emit_jump_insn_after (gen_return (), bb->end);
6906 if (line_note)
6907 emit_line_note_after (NOTE_SOURCE_FILE (line_note),
6908 NOTE_LINE_NUMBER (line_note), bb->end);
6909
69732dcb
RH
6910 while (1)
6911 {
6912 set_block_for_insn (p, bb);
86c82654 6913 if (p == bb->end)
69732dcb 6914 break;
86c82654 6915 p = PREV_INSN (p);
69732dcb
RH
6916 }
6917 bb->end = end;
6918}
73ef99fb 6919#endif /* HAVE_return */
69732dcb 6920
7393c642
RK
6921#ifdef HAVE_epilogue
6922
6923/* Modify SEQ, a SEQUENCE that is part of the epilogue, to no modifications
6924 to the stack pointer. */
6925
6926static void
6927keep_stack_depressed (seq)
6928 rtx seq;
6929{
6930 int i;
6931 rtx sp_from_reg = 0;
6932 int sp_modified_unknown = 0;
6933
6934 /* If the epilogue is just a single instruction, it's OK as is */
6935
6936 if (GET_CODE (seq) != SEQUENCE) return;
6937
6938 /* Scan all insns in SEQ looking for ones that modified the stack
6939 pointer. Record if it modified the stack pointer by copying it
6940 from the frame pointer or if it modified it in some other way.
6941 Then modify any subsequent stack pointer references to take that
6942 into account. We start by only allowing SP to be copied from a
6943 register (presumably FP) and then be subsequently referenced. */
6944
6945 for (i = 0; i < XVECLEN (seq, 0); i++)
6946 {
6947 rtx insn = XVECEXP (seq, 0, i);
6948
6949 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
6950 continue;
6951
6952 if (reg_set_p (stack_pointer_rtx, insn))
6953 {
6954 rtx set = single_set (insn);
6955
6956 /* If SP is set as a side-effect, we can't support this. */
6957 if (set == 0)
6958 abort ();
6959
6960 if (GET_CODE (SET_SRC (set)) == REG)
6961 sp_from_reg = SET_SRC (set);
6962 else
6963 sp_modified_unknown = 1;
6964
6965 /* Don't allow the SP modification to happen. */
6966 PUT_CODE (insn, NOTE);
6967 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
6968 NOTE_SOURCE_FILE (insn) = 0;
6969 }
6970 else if (reg_referenced_p (stack_pointer_rtx, PATTERN (insn)))
6971 {
6972 if (sp_modified_unknown)
6973 abort ();
6974
6975 else if (sp_from_reg != 0)
6976 PATTERN (insn)
6977 = replace_rtx (PATTERN (insn), stack_pointer_rtx, sp_from_reg);
6978 }
6979 }
6980}
6981#endif
6982
9faa82d8 6983/* Generate the prologue and epilogue RTL if the machine supports it. Thread
bdac5f58
TW
6984 this into place with notes indicating where the prologue ends and where
6985 the epilogue begins. Update the basic block information when possible. */
6986
6987void
6988thread_prologue_and_epilogue_insns (f)
54ea1de9 6989 rtx f ATTRIBUTE_UNUSED;
bdac5f58 6990{
ca1117cc 6991 int inserted = 0;
19d3c25c
RH
6992 edge e;
6993 rtx seq;
ca1117cc
RH
6994#ifdef HAVE_prologue
6995 rtx prologue_end = NULL_RTX;
6996#endif
86c82654
RH
6997#if defined (HAVE_epilogue) || defined(HAVE_return)
6998 rtx epilogue_end = NULL_RTX;
6999#endif
e881bb1b 7000
bdac5f58
TW
7001#ifdef HAVE_prologue
7002 if (HAVE_prologue)
7003 {
e881bb1b 7004 start_sequence ();
718fe406 7005 seq = gen_prologue ();
e881bb1b 7006 emit_insn (seq);
bdac5f58
TW
7007
7008 /* Retain a map of the prologue insns. */
e881bb1b
RH
7009 if (GET_CODE (seq) != SEQUENCE)
7010 seq = get_insns ();
0a1c58a2 7011 record_insns (seq, &prologue);
ca1117cc 7012 prologue_end = emit_note (NULL, NOTE_INSN_PROLOGUE_END);
9185a8d5 7013
e881bb1b
RH
7014 seq = gen_sequence ();
7015 end_sequence ();
7016
7017 /* If optimization is off, and perhaps in an empty function,
7018 the entry block will have no successors. */
7019 if (ENTRY_BLOCK_PTR->succ)
7020 {
7021 /* Can't deal with multiple successsors of the entry block. */
7022 if (ENTRY_BLOCK_PTR->succ->succ_next)
7023 abort ();
7024
7025 insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
ca1117cc 7026 inserted = 1;
e881bb1b
RH
7027 }
7028 else
7029 emit_insn_after (seq, f);
bdac5f58 7030 }
bdac5f58 7031#endif
bdac5f58 7032
19d3c25c
RH
7033 /* If the exit block has no non-fake predecessors, we don't need
7034 an epilogue. */
718fe406 7035 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
19d3c25c
RH
7036 if ((e->flags & EDGE_FAKE) == 0)
7037 break;
7038 if (e == NULL)
7039 goto epilogue_done;
7040
69732dcb
RH
7041#ifdef HAVE_return
7042 if (optimize && HAVE_return)
7043 {
7044 /* If we're allowed to generate a simple return instruction,
7045 then by definition we don't need a full epilogue. Examine
718fe406
KH
7046 the block that falls through to EXIT. If it does not
7047 contain any code, examine its predecessors and try to
69732dcb
RH
7048 emit (conditional) return instructions. */
7049
7050 basic_block last;
7051 edge e_next;
7052 rtx label;
7053
718fe406 7054 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
69732dcb
RH
7055 if (e->flags & EDGE_FALLTHRU)
7056 break;
7057 if (e == NULL)
7058 goto epilogue_done;
7059 last = e->src;
7060
7061 /* Verify that there are no active instructions in the last block. */
7062 label = last->end;
7063 while (label && GET_CODE (label) != CODE_LABEL)
7064 {
7065 if (active_insn_p (label))
7066 break;
7067 label = PREV_INSN (label);
7068 }
7069
7070 if (last->head == label && GET_CODE (label) == CODE_LABEL)
7071 {
718fe406 7072 rtx epilogue_line_note = NULL_RTX;
86c82654
RH
7073
7074 /* Locate the line number associated with the closing brace,
7075 if we can find one. */
7076 for (seq = get_last_insn ();
7077 seq && ! active_insn_p (seq);
7078 seq = PREV_INSN (seq))
7079 if (GET_CODE (seq) == NOTE && NOTE_LINE_NUMBER (seq) > 0)
7080 {
7081 epilogue_line_note = seq;
7082 break;
7083 }
7084
718fe406 7085 for (e = last->pred; e; e = e_next)
69732dcb
RH
7086 {
7087 basic_block bb = e->src;
7088 rtx jump;
7089
7090 e_next = e->pred_next;
7091 if (bb == ENTRY_BLOCK_PTR)
7092 continue;
7093
7094 jump = bb->end;
a617c13f 7095 if ((GET_CODE (jump) != JUMP_INSN) || JUMP_LABEL (jump) != label)
69732dcb
RH
7096 continue;
7097
7098 /* If we have an unconditional jump, we can replace that
7099 with a simple return instruction. */
7100 if (simplejump_p (jump))
7101 {
86c82654 7102 emit_return_into_block (bb, epilogue_line_note);
69732dcb
RH
7103 flow_delete_insn (jump);
7104 }
7105
7106 /* If we have a conditional jump, we can try to replace
7107 that with a conditional return instruction. */
7108 else if (condjump_p (jump))
7109 {
7110 rtx ret, *loc;
7111
7112 ret = SET_SRC (PATTERN (jump));
7113 if (GET_CODE (XEXP (ret, 1)) == LABEL_REF)
7114 loc = &XEXP (ret, 1);
7115 else
7116 loc = &XEXP (ret, 2);
7117 ret = gen_rtx_RETURN (VOIDmode);
7118
7119 if (! validate_change (jump, loc, ret, 0))
7120 continue;
7121 if (JUMP_LABEL (jump))
7122 LABEL_NUSES (JUMP_LABEL (jump))--;
718fe406 7123
3a75e42e
CP
7124 /* If this block has only one successor, it both jumps
7125 and falls through to the fallthru block, so we can't
7126 delete the edge. */
718fe406
KH
7127 if (bb->succ->succ_next == NULL)
7128 continue;
69732dcb
RH
7129 }
7130 else
7131 continue;
7132
7133 /* Fix up the CFG for the successful change we just made. */
86c82654 7134 redirect_edge_succ (e, EXIT_BLOCK_PTR);
69732dcb 7135 }
69732dcb 7136
2dd8bc01
GK
7137 /* Emit a return insn for the exit fallthru block. Whether
7138 this is still reachable will be determined later. */
69732dcb 7139
2dd8bc01 7140 emit_barrier_after (last->end);
86c82654
RH
7141 emit_return_into_block (last, epilogue_line_note);
7142 epilogue_end = last->end;
718fe406 7143 goto epilogue_done;
2dd8bc01 7144 }
69732dcb
RH
7145 }
7146#endif
bdac5f58
TW
7147#ifdef HAVE_epilogue
7148 if (HAVE_epilogue)
7149 {
19d3c25c
RH
7150 /* Find the edge that falls through to EXIT. Other edges may exist
7151 due to RETURN instructions, but those don't need epilogues.
7152 There really shouldn't be a mixture -- either all should have
7153 been converted or none, however... */
e881bb1b 7154
718fe406 7155 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
19d3c25c
RH
7156 if (e->flags & EDGE_FALLTHRU)
7157 break;
7158 if (e == NULL)
7159 goto epilogue_done;
a78bdb38 7160
19d3c25c 7161 start_sequence ();
86c82654 7162 epilogue_end = emit_note (NULL, NOTE_INSN_EPILOGUE_BEG);
a78bdb38 7163
19d3c25c 7164 seq = gen_epilogue ();
7393c642
RK
7165
7166 /* If this function returns with the stack depressed, massage
7167 the epilogue to actually do that. */
43db0363
RK
7168 if (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
7169 && TYPE_RETURNS_STACK_DEPRESSED (TREE_TYPE (current_function_decl)))
7393c642
RK
7170 keep_stack_depressed (seq);
7171
19d3c25c 7172 emit_jump_insn (seq);
bdac5f58 7173
19d3c25c
RH
7174 /* Retain a map of the epilogue insns. */
7175 if (GET_CODE (seq) != SEQUENCE)
7176 seq = get_insns ();
0a1c58a2 7177 record_insns (seq, &epilogue);
bdac5f58 7178
19d3c25c 7179 seq = gen_sequence ();
718fe406 7180 end_sequence ();
e881bb1b 7181
19d3c25c 7182 insert_insn_on_edge (seq, e);
ca1117cc 7183 inserted = 1;
bdac5f58
TW
7184 }
7185#endif
19d3c25c 7186epilogue_done:
e881bb1b 7187
ca1117cc 7188 if (inserted)
e881bb1b 7189 commit_edge_insertions ();
0a1c58a2
JL
7190
7191#ifdef HAVE_sibcall_epilogue
7192 /* Emit sibling epilogues before any sibling call sites. */
718fe406 7193 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
0a1c58a2
JL
7194 {
7195 basic_block bb = e->src;
7196 rtx insn = bb->end;
7197 rtx i;
1b513b77 7198 rtx newinsn;
0a1c58a2
JL
7199
7200 if (GET_CODE (insn) != CALL_INSN
7201 || ! SIBLING_CALL_P (insn))
7202 continue;
7203
7204 start_sequence ();
7205 seq = gen_sibcall_epilogue ();
7206 end_sequence ();
7207
7208 i = PREV_INSN (insn);
1b513b77 7209 newinsn = emit_insn_before (seq, insn);
0a1c58a2
JL
7210
7211 /* Update the UID to basic block map. */
7212 for (i = NEXT_INSN (i); i != insn; i = NEXT_INSN (i))
7213 set_block_for_insn (i, bb);
7214
7215 /* Retain a map of the epilogue insns. Used in life analysis to
7216 avoid getting rid of sibcall epilogue insns. */
1b513b77
JH
7217 record_insns (GET_CODE (seq) == SEQUENCE
7218 ? seq : newinsn, &sibcall_epilogue);
0a1c58a2
JL
7219 }
7220#endif
ca1117cc
RH
7221
7222#ifdef HAVE_prologue
7223 if (prologue_end)
7224 {
7225 rtx insn, prev;
7226
7227 /* GDB handles `break f' by setting a breakpoint on the first
30196c1f 7228 line note after the prologue. Which means (1) that if
ca1117cc 7229 there are line number notes before where we inserted the
30196c1f
RH
7230 prologue we should move them, and (2) we should generate a
7231 note before the end of the first basic block, if there isn't
7232 one already there. */
ca1117cc 7233
718fe406 7234 for (insn = prologue_end; insn; insn = prev)
ca1117cc
RH
7235 {
7236 prev = PREV_INSN (insn);
7237 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7238 {
7239 /* Note that we cannot reorder the first insn in the
7240 chain, since rest_of_compilation relies on that
30196c1f 7241 remaining constant. */
ca1117cc 7242 if (prev == NULL)
30196c1f
RH
7243 break;
7244 reorder_insns (insn, insn, prologue_end);
ca1117cc
RH
7245 }
7246 }
7247
30196c1f
RH
7248 /* Find the last line number note in the first block. */
7249 for (insn = BASIC_BLOCK (0)->end;
7250 insn != prologue_end;
7251 insn = PREV_INSN (insn))
7252 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7253 break;
7254
7255 /* If we didn't find one, make a copy of the first line number
7256 we run across. */
7257 if (! insn)
ca1117cc 7258 {
30196c1f
RH
7259 for (insn = next_active_insn (prologue_end);
7260 insn;
7261 insn = PREV_INSN (insn))
7262 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7263 {
7264 emit_line_note_after (NOTE_SOURCE_FILE (insn),
7265 NOTE_LINE_NUMBER (insn),
7266 prologue_end);
7267 break;
7268 }
ca1117cc
RH
7269 }
7270 }
7271#endif
86c82654
RH
7272#ifdef HAVE_epilogue
7273 if (epilogue_end)
7274 {
7275 rtx insn, next;
7276
7277 /* Similarly, move any line notes that appear after the epilogue.
7278 There is no need, however, to be quite so anal about the existance
7279 of such a note. */
718fe406 7280 for (insn = epilogue_end; insn; insn = next)
86c82654
RH
7281 {
7282 next = NEXT_INSN (insn);
7283 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
7284 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
7285 }
7286 }
7287#endif
bdac5f58
TW
7288}
7289
7290/* Reposition the prologue-end and epilogue-begin notes after instruction
7291 scheduling and delayed branch scheduling. */
7292
7293void
7294reposition_prologue_and_epilogue_notes (f)
79c9824e 7295 rtx f ATTRIBUTE_UNUSED;
bdac5f58
TW
7296{
7297#if defined (HAVE_prologue) || defined (HAVE_epilogue)
0a1c58a2
JL
7298 int len;
7299
7300 if ((len = VARRAY_SIZE (prologue)) > 0)
bdac5f58 7301 {
0a1c58a2 7302 register rtx insn, note = 0;
bdac5f58 7303
0a1c58a2
JL
7304 /* Scan from the beginning until we reach the last prologue insn.
7305 We apparently can't depend on basic_block_{head,end} after
7306 reorg has run. */
7307 for (insn = f; len && insn; insn = NEXT_INSN (insn))
bdac5f58 7308 {
0a1c58a2 7309 if (GET_CODE (insn) == NOTE)
9392c110 7310 {
0a1c58a2
JL
7311 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
7312 note = insn;
7313 }
7314 else if ((len -= contains (insn, prologue)) == 0)
7315 {
7316 rtx next;
7317 /* Find the prologue-end note if we haven't already, and
7318 move it to just after the last prologue insn. */
7319 if (note == 0)
9392c110 7320 {
0a1c58a2
JL
7321 for (note = insn; (note = NEXT_INSN (note));)
7322 if (GET_CODE (note) == NOTE
7323 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
7324 break;
9392c110 7325 }
c93b03c2 7326
0a1c58a2 7327 next = NEXT_INSN (note);
c93b03c2 7328
718fe406 7329 /* Whether or not we can depend on BLOCK_HEAD,
0a1c58a2
JL
7330 attempt to keep it up-to-date. */
7331 if (BLOCK_HEAD (0) == note)
7332 BLOCK_HEAD (0) = next;
c93b03c2 7333
0a1c58a2
JL
7334 remove_insn (note);
7335 add_insn_after (note, insn);
9392c110 7336 }
bdac5f58 7337 }
0a1c58a2
JL
7338 }
7339
7340 if ((len = VARRAY_SIZE (epilogue)) > 0)
7341 {
7342 register rtx insn, note = 0;
bdac5f58 7343
0a1c58a2
JL
7344 /* Scan from the end until we reach the first epilogue insn.
7345 We apparently can't depend on basic_block_{head,end} after
7346 reorg has run. */
7347 for (insn = get_last_insn (); len && insn; insn = PREV_INSN (insn))
bdac5f58 7348 {
0a1c58a2 7349 if (GET_CODE (insn) == NOTE)
9392c110 7350 {
0a1c58a2
JL
7351 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
7352 note = insn;
7353 }
7354 else if ((len -= contains (insn, epilogue)) == 0)
7355 {
7356 /* Find the epilogue-begin note if we haven't already, and
7357 move it to just before the first epilogue insn. */
7358 if (note == 0)
9392c110 7359 {
0a1c58a2
JL
7360 for (note = insn; (note = PREV_INSN (note));)
7361 if (GET_CODE (note) == NOTE
7362 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
7363 break;
9392c110 7364 }
c93b03c2 7365
718fe406 7366 /* Whether or not we can depend on BLOCK_HEAD,
0a1c58a2
JL
7367 attempt to keep it up-to-date. */
7368 if (n_basic_blocks
7369 && BLOCK_HEAD (n_basic_blocks-1) == insn)
7370 BLOCK_HEAD (n_basic_blocks-1) = note;
c93b03c2 7371
0a1c58a2
JL
7372 remove_insn (note);
7373 add_insn_before (note, insn);
9392c110 7374 }
bdac5f58
TW
7375 }
7376 }
7377#endif /* HAVE_prologue or HAVE_epilogue */
7378}
87ff9c8e
RH
7379
7380/* Mark T for GC. */
7381
7382static void
7383mark_temp_slot (t)
718fe406 7384 struct temp_slot *t;
87ff9c8e
RH
7385{
7386 while (t)
7387 {
7388 ggc_mark_rtx (t->slot);
7389 ggc_mark_rtx (t->address);
591ccf92
MM
7390 ggc_mark_tree (t->rtl_expr);
7391
87ff9c8e
RH
7392 t = t->next;
7393 }
7394}
7395
7396/* Mark P for GC. */
7397
7398static void
fa51b01b 7399mark_function_status (p)
87ff9c8e
RH
7400 struct function *p;
7401{
7402 int i;
7403 rtx *r;
7404
7405 if (p == 0)
7406 return;
7407
7408 ggc_mark_rtx (p->arg_offset_rtx);
7409
21cd906e
MM
7410 if (p->x_parm_reg_stack_loc)
7411 for (i = p->x_max_parm_reg, r = p->x_parm_reg_stack_loc;
7412 i > 0; --i, ++r)
7413 ggc_mark_rtx (*r);
87ff9c8e
RH
7414
7415 ggc_mark_rtx (p->return_rtx);
7416 ggc_mark_rtx (p->x_cleanup_label);
7417 ggc_mark_rtx (p->x_return_label);
7418 ggc_mark_rtx (p->x_save_expr_regs);
7419 ggc_mark_rtx (p->x_stack_slot_list);
7420 ggc_mark_rtx (p->x_parm_birth_insn);
7421 ggc_mark_rtx (p->x_tail_recursion_label);
7422 ggc_mark_rtx (p->x_tail_recursion_reentry);
7423 ggc_mark_rtx (p->internal_arg_pointer);
7424 ggc_mark_rtx (p->x_arg_pointer_save_area);
7425 ggc_mark_tree (p->x_rtl_expr_chain);
7426 ggc_mark_rtx (p->x_last_parm_insn);
7427 ggc_mark_tree (p->x_context_display);
7428 ggc_mark_tree (p->x_trampoline_list);
7429 ggc_mark_rtx (p->epilogue_delay_list);
b313a0fe 7430 ggc_mark_rtx (p->x_clobber_return_insn);
87ff9c8e
RH
7431
7432 mark_temp_slot (p->x_temp_slots);
7433
7434 {
7435 struct var_refs_queue *q = p->fixup_var_refs_queue;
7436 while (q)
7437 {
7438 ggc_mark_rtx (q->modified);
7439 q = q->next;
7440 }
7441 }
7442
7443 ggc_mark_rtx (p->x_nonlocal_goto_handler_slots);
afe3d090 7444 ggc_mark_rtx (p->x_nonlocal_goto_handler_labels);
87ff9c8e
RH
7445 ggc_mark_rtx (p->x_nonlocal_goto_stack_level);
7446 ggc_mark_tree (p->x_nonlocal_labels);
7447}
7448
7449/* Mark the function chain ARG (which is really a struct function **)
7450 for GC. */
7451
7452static void
7453mark_function_chain (arg)
7454 void *arg;
7455{
7456 struct function *f = *(struct function **) arg;
7457
7458 for (; f; f = f->next_global)
7459 {
87ff9c8e
RH
7460 ggc_mark_tree (f->decl);
7461
fa51b01b
RH
7462 mark_function_status (f);
7463 mark_eh_status (f->eh);
7464 mark_stmt_status (f->stmt);
7465 mark_expr_status (f->expr);
7466 mark_emit_status (f->emit);
7467 mark_varasm_status (f->varasm);
87ff9c8e
RH
7468
7469 if (mark_machine_status)
7470 (*mark_machine_status) (f);
7471 if (mark_lang_status)
7472 (*mark_lang_status) (f);
7473
7474 if (f->original_arg_vector)
7475 ggc_mark_rtvec ((rtvec) f->original_arg_vector);
7476 if (f->original_decl_initial)
7477 ggc_mark_tree (f->original_decl_initial);
7478 }
7479}
7480
7481/* Called once, at initialization, to initialize function.c. */
7482
7483void
7484init_function_once ()
7485{
7486 ggc_add_root (&all_functions, 1, sizeof all_functions,
7487 mark_function_chain);
0a1c58a2
JL
7488
7489 VARRAY_INT_INIT (prologue, 0, "prologue");
7490 VARRAY_INT_INIT (epilogue, 0, "epilogue");
7491 VARRAY_INT_INIT (sibcall_epilogue, 0, "sibcall_epilogue");
87ff9c8e 7492}
This page took 2.124199 seconds and 5 git commands to generate.