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