]> gcc.gnu.org Git - gcc.git/blame - gcc/function.c
drupp@cs.washington.edu
[gcc.git] / gcc / function.c
CommitLineData
6f086dfc 1/* Expands front end tree to back end RTL for GNU C-Compiler
4013a709 2 Copyright (C) 1987, 88, 89, 91-94, 1995 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
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21/* This file handles the generation of rtl code from tree structure
22 at the level of the function as a whole.
23 It creates the rtl expressions for parameters and auto variables
24 and has full responsibility for allocating stack slots.
25
26 `expand_function_start' is called at the beginning of a function,
27 before the function body is parsed, and `expand_function_end' is
28 called after parsing the body.
29
30 Call `assign_stack_local' to allocate a stack slot for a local variable.
31 This is usually done during the RTL generation for the function body,
32 but it can also be done in the reload pass when a pseudo-register does
33 not get a hard register.
34
35 Call `put_var_into_stack' when you learn, belatedly, that a variable
36 previously given a pseudo-register must in fact go in the stack.
37 This function changes the DECL_RTL to be a stack slot instead of a reg
38 then scans all the RTL instructions so far generated to correct them. */
39
40#include "config.h"
41
42#include <stdio.h>
43
44#include "rtl.h"
45#include "tree.h"
46#include "flags.h"
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
JB
57#include "obstack.h"
58#include "bytecode.h"
6f086dfc 59
293e3de4
RS
60/* Some systems use __main in a way incompatible with its use in gcc, in these
61 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
62 give the same symbol without quotes for an alternative entry point. You
63 must define both, or niether. */
64#ifndef NAME__MAIN
65#define NAME__MAIN "__main"
66#define SYMBOL__MAIN __main
67#endif
68
6f086dfc
RS
69/* Round a value to the lowest integer less than it that is a multiple of
70 the required alignment. Avoid using division in case the value is
71 negative. Assume the alignment is a power of two. */
72#define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
73
74/* Similar, but round to the next highest integer that meets the
75 alignment. */
76#define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
77
78/* NEED_SEPARATE_AP means that we cannot derive ap from the value of fp
79 during rtl generation. If they are different register numbers, this is
80 always true. It may also be true if
81 FIRST_PARM_OFFSET - STARTING_FRAME_OFFSET is not a constant during rtl
82 generation. See fix_lexical_addr for details. */
83
84#if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
85#define NEED_SEPARATE_AP
86#endif
87
88/* Number of bytes of args popped by function being compiled on its return.
89 Zero if no bytes are to be popped.
90 May affect compilation of return insn or of function epilogue. */
91
92int current_function_pops_args;
93
94/* Nonzero if function being compiled needs to be given an address
95 where the value should be stored. */
96
97int current_function_returns_struct;
98
99/* Nonzero if function being compiled needs to
100 return the address of where it has put a structure value. */
101
102int current_function_returns_pcc_struct;
103
104/* Nonzero if function being compiled needs to be passed a static chain. */
105
106int current_function_needs_context;
107
108/* Nonzero if function being compiled can call setjmp. */
109
110int current_function_calls_setjmp;
111
112/* Nonzero if function being compiled can call longjmp. */
113
114int current_function_calls_longjmp;
115
116/* Nonzero if function being compiled receives nonlocal gotos
117 from nested functions. */
118
119int current_function_has_nonlocal_label;
120
8634413a
JW
121/* Nonzero if function being compiled has nonlocal gotos to parent
122 function. */
123
124int current_function_has_nonlocal_goto;
125
6f086dfc
RS
126/* Nonzero if function being compiled contains nested functions. */
127
128int current_function_contains_functions;
129
130/* Nonzero if function being compiled can call alloca,
131 either as a subroutine or builtin. */
132
133int current_function_calls_alloca;
134
135/* Nonzero if the current function returns a pointer type */
136
137int current_function_returns_pointer;
138
139/* If some insns can be deferred to the delay slots of the epilogue, the
140 delay list for them is recorded here. */
141
142rtx current_function_epilogue_delay_list;
143
144/* If function's args have a fixed size, this is that size, in bytes.
145 Otherwise, it is -1.
146 May affect compilation of return insn or of function epilogue. */
147
148int current_function_args_size;
149
150/* # bytes the prologue should push and pretend that the caller pushed them.
151 The prologue must do this, but only if parms can be passed in registers. */
152
153int current_function_pretend_args_size;
154
f7339633
PB
155/* # of bytes of outgoing arguments. If ACCUMULATE_OUTGOING_ARGS is
156 defined, the needed space is pushed by the prologue. */
6f086dfc
RS
157
158int current_function_outgoing_args_size;
159
160/* This is the offset from the arg pointer to the place where the first
161 anonymous arg can be found, if there is one. */
162
163rtx current_function_arg_offset_rtx;
164
165/* Nonzero if current function uses varargs.h or equivalent.
166 Zero for functions that use stdarg.h. */
167
168int current_function_varargs;
169
170/* Quantities of various kinds of registers
171 used for the current function's args. */
172
173CUMULATIVE_ARGS current_function_args_info;
174
175/* Name of function now being compiled. */
176
177char *current_function_name;
178
179/* If non-zero, an RTL expression for that location at which the current
180 function returns its result. Always equal to
181 DECL_RTL (DECL_RESULT (current_function_decl)), but provided
182 independently of the tree structures. */
183
184rtx current_function_return_rtx;
185
186/* Nonzero if the current function uses the constant pool. */
187
188int current_function_uses_const_pool;
189
190/* Nonzero if the current function uses pic_offset_table_rtx. */
191int current_function_uses_pic_offset_table;
192
193/* The arg pointer hard register, or the pseudo into which it was copied. */
194rtx current_function_internal_arg_pointer;
195
196/* The FUNCTION_DECL for an inline function currently being expanded. */
197tree inline_function_decl;
198
199/* Number of function calls seen so far in current function. */
200
201int function_call_count;
202
203/* List (chain of TREE_LIST) of LABEL_DECLs for all nonlocal labels
204 (labels to which there can be nonlocal gotos from nested functions)
205 in this function. */
206
207tree nonlocal_labels;
208
209/* RTX for stack slot that holds the current handler for nonlocal gotos.
210 Zero when function does not have nonlocal labels. */
211
212rtx nonlocal_goto_handler_slot;
213
214/* RTX for stack slot that holds the stack pointer value to restore
215 for a nonlocal goto.
216 Zero when function does not have nonlocal labels. */
217
218rtx nonlocal_goto_stack_level;
219
220/* Label that will go on parm cleanup code, if any.
221 Jumping to this label runs cleanup code for parameters, if
222 such code must be run. Following this code is the logical return label. */
223
224rtx cleanup_label;
225
226/* Label that will go on function epilogue.
227 Jumping to this label serves as a "return" instruction
228 on machines which require execution of the epilogue on all returns. */
229
230rtx return_label;
231
232/* List (chain of EXPR_LISTs) of pseudo-regs of SAVE_EXPRs.
233 So we can mark them all live at the end of the function, if nonopt. */
234rtx save_expr_regs;
235
236/* List (chain of EXPR_LISTs) of all stack slots in this function.
237 Made for the sake of unshare_all_rtl. */
238rtx stack_slot_list;
239
240/* Chain of all RTL_EXPRs that have insns in them. */
241tree rtl_expr_chain;
242
243/* Label to jump back to for tail recursion, or 0 if we have
244 not yet needed one for this function. */
245rtx tail_recursion_label;
246
247/* Place after which to insert the tail_recursion_label if we need one. */
248rtx tail_recursion_reentry;
249
250/* Location at which to save the argument pointer if it will need to be
251 referenced. There are two cases where this is done: if nonlocal gotos
252 exist, or if vars stored at an offset from the argument pointer will be
253 needed by inner routines. */
254
255rtx arg_pointer_save_area;
256
257/* Offset to end of allocated area of stack frame.
258 If stack grows down, this is the address of the last stack slot allocated.
259 If stack grows up, this is the address for the next slot. */
260int frame_offset;
261
262/* List (chain of TREE_LISTs) of static chains for containing functions.
263 Each link has a FUNCTION_DECL in the TREE_PURPOSE and a reg rtx
264 in an RTL_EXPR in the TREE_VALUE. */
265static tree context_display;
266
267/* List (chain of TREE_LISTs) of trampolines for nested functions.
268 The trampoline sets up the static chain and jumps to the function.
269 We supply the trampoline's address when the function's address is requested.
270
271 Each link has a FUNCTION_DECL in the TREE_PURPOSE and a reg rtx
272 in an RTL_EXPR in the TREE_VALUE. */
273static tree trampoline_list;
274
275/* Insn after which register parms and SAVE_EXPRs are born, if nonopt. */
276static rtx parm_birth_insn;
277
278#if 0
279/* Nonzero if a stack slot has been generated whose address is not
280 actually valid. It means that the generated rtl must all be scanned
281 to detect and correct the invalid addresses where they occur. */
282static int invalid_stack_slot;
283#endif
284
285/* Last insn of those whose job was to put parms into their nominal homes. */
286static rtx last_parm_insn;
287
288/* 1 + last pseudo register number used for loading a copy
289 of a parameter of this function. */
290static int max_parm_reg;
291
292/* Vector indexed by REGNO, containing location on stack in which
293 to put the parm which is nominally in pseudo register REGNO,
294 if we discover that that parm must go in the stack. */
295static rtx *parm_reg_stack_loc;
296
297#if 0 /* Turned off because 0 seems to work just as well. */
298/* Cleanup lists are required for binding levels regardless of whether
299 that binding level has cleanups or not. This node serves as the
300 cleanup list whenever an empty list is required. */
301static tree empty_cleanup_list;
302#endif
303
304/* Nonzero once virtual register instantiation has been done.
305 assign_stack_local uses frame_pointer_rtx when this is nonzero. */
306static int virtuals_instantiated;
307
46766466
RS
308/* These variables hold pointers to functions to
309 save and restore machine-specific data,
310 in push_function_context and pop_function_context. */
311void (*save_machine_status) ();
312void (*restore_machine_status) ();
313
6f086dfc
RS
314/* Nonzero if we need to distinguish between the return value of this function
315 and the return value of a function called by this function. This helps
316 integrate.c */
317
318extern int rtx_equal_function_value_matters;
e7a84011 319extern tree sequence_rtl_expr;
c20bf1f3
JB
320extern tree bc_runtime_type_code ();
321extern rtx bc_build_calldesc ();
322extern char *bc_emit_trampoline ();
323extern char *bc_end_function ();
6f086dfc
RS
324\f
325/* In order to evaluate some expressions, such as function calls returning
326 structures in memory, we need to temporarily allocate stack locations.
327 We record each allocated temporary in the following structure.
328
329 Associated with each temporary slot is a nesting level. When we pop up
330 one level, all temporaries associated with the previous level are freed.
331 Normally, all temporaries are freed after the execution of the statement
332 in which they were created. However, if we are inside a ({...}) grouping,
333 the result may be in a temporary and hence must be preserved. If the
334 result could be in a temporary, we preserve it if we can determine which
335 one it is in. If we cannot determine which temporary may contain the
336 result, all temporaries are preserved. A temporary is preserved by
337 pretending it was allocated at the previous nesting level.
338
339 Automatic variables are also assigned temporary slots, at the nesting
340 level where they are defined. They are marked a "kept" so that
341 free_temp_slots will not free them. */
342
343struct temp_slot
344{
345 /* Points to next temporary slot. */
346 struct temp_slot *next;
347 /* The rtx to used to reference the slot. */
348 rtx slot;
e5e76139
RK
349 /* The rtx used to represent the address if not the address of the
350 slot above. May be an EXPR_LIST if multiple addresses exist. */
351 rtx address;
6f086dfc
RS
352 /* The size, in units, of the slot. */
353 int size;
e7a84011
RK
354 /* The value of `sequence_rtl_expr' when this temporary is allocated. */
355 tree rtl_expr;
6f086dfc
RS
356 /* Non-zero if this temporary is currently in use. */
357 char in_use;
a25d4ba2
RK
358 /* Non-zero if this temporary has its address taken. */
359 char addr_taken;
6f086dfc
RS
360 /* Nesting level at which this slot is being used. */
361 int level;
362 /* Non-zero if this should survive a call to free_temp_slots. */
363 int keep;
364};
365
366/* List of all temporaries allocated, both available and in use. */
367
368struct temp_slot *temp_slots;
369
370/* Current nesting level for temporaries. */
371
372int temp_slot_level;
373\f
c20bf1f3
JB
374/* The FUNCTION_DECL node for the current function. */
375static tree this_function_decl;
376
377/* Callinfo pointer for the current function. */
378static rtx this_function_callinfo;
379
380/* The label in the bytecode file of this function's actual bytecode.
381 Not an rtx. */
382static char *this_function_bytecode;
383
384/* The call description vector for the current function. */
385static rtx this_function_calldesc;
386
387/* Size of the local variables allocated for the current function. */
388int local_vars_size;
389
390/* Current depth of the bytecode evaluation stack. */
391int stack_depth;
392
393/* Maximum depth of the evaluation stack in this function. */
394int max_stack_depth;
395
396/* Current depth in statement expressions. */
397static int stmt_expr_depth;
e15679f8
RK
398
399/* This structure is used to record MEMs or pseudos used to replace VAR, any
400 SUBREGs of VAR, and any MEMs containing VAR as an address. We need to
401 maintain this list in case two operands of an insn were required to match;
402 in that case we must ensure we use the same replacement. */
403
404struct fixup_replacement
405{
406 rtx old;
407 rtx new;
408 struct fixup_replacement *next;
409};
410
411/* Forward declarations. */
412
413static struct temp_slot *find_temp_slot_from_address PROTO((rtx));
414static void put_reg_into_stack PROTO((struct function *, rtx, tree,
415 enum machine_mode, enum machine_mode));
416static void fixup_var_refs PROTO((rtx, enum machine_mode, int));
417static struct fixup_replacement
418 *find_fixup_replacement PROTO((struct fixup_replacement **, rtx));
419static void fixup_var_refs_insns PROTO((rtx, enum machine_mode, int,
420 rtx, int));
421static void fixup_var_refs_1 PROTO((rtx, enum machine_mode, rtx *, rtx,
422 struct fixup_replacement **));
423static rtx fixup_memory_subreg PROTO((rtx, rtx, int));
424static rtx walk_fixup_memory_subreg PROTO((rtx, rtx, int));
425static rtx fixup_stack_1 PROTO((rtx, rtx));
426static void optimize_bit_field PROTO((rtx, rtx, rtx *));
427static void instantiate_decls PROTO((tree, int));
428static void instantiate_decls_1 PROTO((tree, int));
429static void instantiate_decl PROTO((rtx, int, int));
430static int instantiate_virtual_regs_1 PROTO((rtx *, rtx, int));
431static void delete_handlers PROTO((void));
432static void pad_to_arg_alignment PROTO((struct args_size *, int));
433static void pad_below PROTO((struct args_size *, enum machine_mode,
434 tree));
435static tree round_down PROTO((tree, int));
436static rtx round_trampoline_addr PROTO((rtx));
437static tree blocks_nreverse PROTO((tree));
438static int all_blocks PROTO((tree, tree *));
439static int *record_insns PROTO((rtx));
440static int contains PROTO((rtx, int *));
c20bf1f3 441\f
6f086dfc
RS
442/* Pointer to chain of `struct function' for containing functions. */
443struct function *outer_function_chain;
444
445/* Given a function decl for a containing function,
446 return the `struct function' for it. */
447
448struct function *
449find_function_data (decl)
450 tree decl;
451{
452 struct function *p;
453 for (p = outer_function_chain; p; p = p->next)
454 if (p->decl == decl)
455 return p;
456 abort ();
457}
458
459/* Save the current context for compilation of a nested function.
460 This is called from language-specific code.
461 The caller is responsible for saving any language-specific status,
6dc42e49 462 since this function knows only about language-independent variables. */
6f086dfc
RS
463
464void
e4a4639e
JM
465push_function_context_to (toplevel)
466 int toplevel;
6f086dfc
RS
467{
468 struct function *p = (struct function *) xmalloc (sizeof (struct function));
469
470 p->next = outer_function_chain;
471 outer_function_chain = p;
472
473 p->name = current_function_name;
474 p->decl = current_function_decl;
475 p->pops_args = current_function_pops_args;
476 p->returns_struct = current_function_returns_struct;
477 p->returns_pcc_struct = current_function_returns_pcc_struct;
478 p->needs_context = current_function_needs_context;
479 p->calls_setjmp = current_function_calls_setjmp;
480 p->calls_longjmp = current_function_calls_longjmp;
481 p->calls_alloca = current_function_calls_alloca;
482 p->has_nonlocal_label = current_function_has_nonlocal_label;
8634413a 483 p->has_nonlocal_goto = current_function_has_nonlocal_goto;
6f086dfc
RS
484 p->args_size = current_function_args_size;
485 p->pretend_args_size = current_function_pretend_args_size;
486 p->arg_offset_rtx = current_function_arg_offset_rtx;
3b69d50e 487 p->varargs = current_function_varargs;
6f086dfc
RS
488 p->uses_const_pool = current_function_uses_const_pool;
489 p->uses_pic_offset_table = current_function_uses_pic_offset_table;
490 p->internal_arg_pointer = current_function_internal_arg_pointer;
491 p->max_parm_reg = max_parm_reg;
492 p->parm_reg_stack_loc = parm_reg_stack_loc;
493 p->outgoing_args_size = current_function_outgoing_args_size;
494 p->return_rtx = current_function_return_rtx;
495 p->nonlocal_goto_handler_slot = nonlocal_goto_handler_slot;
496 p->nonlocal_goto_stack_level = nonlocal_goto_stack_level;
497 p->nonlocal_labels = nonlocal_labels;
498 p->cleanup_label = cleanup_label;
499 p->return_label = return_label;
500 p->save_expr_regs = save_expr_regs;
501 p->stack_slot_list = stack_slot_list;
502 p->parm_birth_insn = parm_birth_insn;
503 p->frame_offset = frame_offset;
504 p->tail_recursion_label = tail_recursion_label;
505 p->tail_recursion_reentry = tail_recursion_reentry;
506 p->arg_pointer_save_area = arg_pointer_save_area;
507 p->rtl_expr_chain = rtl_expr_chain;
508 p->last_parm_insn = last_parm_insn;
509 p->context_display = context_display;
510 p->trampoline_list = trampoline_list;
511 p->function_call_count = function_call_count;
512 p->temp_slots = temp_slots;
513 p->temp_slot_level = temp_slot_level;
514 p->fixup_var_refs_queue = 0;
f979c996 515 p->epilogue_delay_list = current_function_epilogue_delay_list;
6f086dfc 516
e4a4639e 517 save_tree_status (p, toplevel);
6f086dfc
RS
518 save_storage_status (p);
519 save_emit_status (p);
520 init_emit ();
521 save_expr_status (p);
522 save_stmt_status (p);
a506307a 523 save_varasm_status (p);
46766466
RS
524
525 if (save_machine_status)
526 (*save_machine_status) (p);
6f086dfc
RS
527}
528
e4a4639e
JM
529void
530push_function_context ()
531{
532 push_function_context_to (0);
533}
534
6f086dfc
RS
535/* Restore the last saved context, at the end of a nested function.
536 This function is called from language-specific code. */
537
538void
e4a4639e
JM
539pop_function_context_from (toplevel)
540 int toplevel;
6f086dfc
RS
541{
542 struct function *p = outer_function_chain;
543
544 outer_function_chain = p->next;
545
546 current_function_name = p->name;
547 current_function_decl = p->decl;
548 current_function_pops_args = p->pops_args;
549 current_function_returns_struct = p->returns_struct;
550 current_function_returns_pcc_struct = p->returns_pcc_struct;
551 current_function_needs_context = p->needs_context;
552 current_function_calls_setjmp = p->calls_setjmp;
553 current_function_calls_longjmp = p->calls_longjmp;
554 current_function_calls_alloca = p->calls_alloca;
555 current_function_has_nonlocal_label = p->has_nonlocal_label;
8634413a 556 current_function_has_nonlocal_goto = p->has_nonlocal_goto;
e4a4639e
JM
557 if (! toplevel)
558 current_function_contains_functions = 1;
6f086dfc
RS
559 current_function_args_size = p->args_size;
560 current_function_pretend_args_size = p->pretend_args_size;
561 current_function_arg_offset_rtx = p->arg_offset_rtx;
3b69d50e 562 current_function_varargs = p->varargs;
6f086dfc
RS
563 current_function_uses_const_pool = p->uses_const_pool;
564 current_function_uses_pic_offset_table = p->uses_pic_offset_table;
565 current_function_internal_arg_pointer = p->internal_arg_pointer;
566 max_parm_reg = p->max_parm_reg;
567 parm_reg_stack_loc = p->parm_reg_stack_loc;
568 current_function_outgoing_args_size = p->outgoing_args_size;
569 current_function_return_rtx = p->return_rtx;
570 nonlocal_goto_handler_slot = p->nonlocal_goto_handler_slot;
571 nonlocal_goto_stack_level = p->nonlocal_goto_stack_level;
572 nonlocal_labels = p->nonlocal_labels;
573 cleanup_label = p->cleanup_label;
574 return_label = p->return_label;
575 save_expr_regs = p->save_expr_regs;
576 stack_slot_list = p->stack_slot_list;
577 parm_birth_insn = p->parm_birth_insn;
578 frame_offset = p->frame_offset;
579 tail_recursion_label = p->tail_recursion_label;
580 tail_recursion_reentry = p->tail_recursion_reentry;
581 arg_pointer_save_area = p->arg_pointer_save_area;
582 rtl_expr_chain = p->rtl_expr_chain;
583 last_parm_insn = p->last_parm_insn;
584 context_display = p->context_display;
585 trampoline_list = p->trampoline_list;
586 function_call_count = p->function_call_count;
587 temp_slots = p->temp_slots;
588 temp_slot_level = p->temp_slot_level;
f979c996 589 current_function_epilogue_delay_list = p->epilogue_delay_list;
7cbc7b0c 590 reg_renumber = 0;
6f086dfc 591
e4a4639e 592 restore_tree_status (p, toplevel);
6f086dfc
RS
593 restore_storage_status (p);
594 restore_expr_status (p);
595 restore_emit_status (p);
596 restore_stmt_status (p);
a506307a 597 restore_varasm_status (p);
6f086dfc 598
46766466
RS
599 if (restore_machine_status)
600 (*restore_machine_status) (p);
601
6f086dfc
RS
602 /* Finish doing put_var_into_stack for any of our variables
603 which became addressable during the nested function. */
604 {
605 struct var_refs_queue *queue = p->fixup_var_refs_queue;
606 for (; queue; queue = queue->next)
00d8a4c1 607 fixup_var_refs (queue->modified, queue->promoted_mode, queue->unsignedp);
6f086dfc
RS
608 }
609
610 free (p);
611
612 /* Reset variables that have known state during rtx generation. */
613 rtx_equal_function_value_matters = 1;
614 virtuals_instantiated = 0;
615}
e4a4639e
JM
616
617void pop_function_context ()
618{
619 pop_function_context_from (0);
620}
6f086dfc
RS
621\f
622/* Allocate fixed slots in the stack frame of the current function. */
623
624/* Return size needed for stack frame based on slots so far allocated.
625 This size counts from zero. It is not rounded to STACK_BOUNDARY;
626 the caller may have to do that. */
627
628int
629get_frame_size ()
630{
631#ifdef FRAME_GROWS_DOWNWARD
632 return -frame_offset;
633#else
634 return frame_offset;
635#endif
636}
637
638/* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
639 with machine mode MODE.
640
641 ALIGN controls the amount of alignment for the address of the slot:
642 0 means according to MODE,
643 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
644 positive specifies alignment boundary in bits.
645
646 We do not round to stack_boundary here. */
647
648rtx
649assign_stack_local (mode, size, align)
650 enum machine_mode mode;
651 int size;
652 int align;
653{
654 register rtx x, addr;
655 int bigend_correction = 0;
656 int alignment;
657
658 if (align == 0)
659 {
660 alignment = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
661 if (mode == BLKmode)
662 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
663 }
664 else if (align == -1)
665 {
666 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
667 size = CEIL_ROUND (size, alignment);
668 }
669 else
670 alignment = align / BITS_PER_UNIT;
671
6f086dfc
RS
672 /* Round frame offset to that alignment.
673 We must be careful here, since FRAME_OFFSET might be negative and
674 division with a negative dividend isn't as well defined as we might
675 like. So we instead assume that ALIGNMENT is a power of two and
676 use logical operations which are unambiguous. */
677#ifdef FRAME_GROWS_DOWNWARD
678 frame_offset = FLOOR_ROUND (frame_offset, alignment);
679#else
680 frame_offset = CEIL_ROUND (frame_offset, alignment);
681#endif
682
683 /* On a big-endian machine, if we are allocating more space than we will use,
684 use the least significant bytes of those that are allocated. */
f76b9db2 685 if (BYTES_BIG_ENDIAN && mode != BLKmode)
6f086dfc 686 bigend_correction = size - GET_MODE_SIZE (mode);
6f086dfc
RS
687
688#ifdef FRAME_GROWS_DOWNWARD
689 frame_offset -= size;
690#endif
691
692 /* If we have already instantiated virtual registers, return the actual
693 address relative to the frame pointer. */
694 if (virtuals_instantiated)
695 addr = plus_constant (frame_pointer_rtx,
696 (frame_offset + bigend_correction
697 + STARTING_FRAME_OFFSET));
698 else
699 addr = plus_constant (virtual_stack_vars_rtx,
700 frame_offset + bigend_correction);
701
702#ifndef FRAME_GROWS_DOWNWARD
703 frame_offset += size;
704#endif
705
706 x = gen_rtx (MEM, mode, addr);
707
708 stack_slot_list = gen_rtx (EXPR_LIST, VOIDmode, x, stack_slot_list);
709
710 return x;
711}
712
713/* Assign a stack slot in a containing function.
714 First three arguments are same as in preceding function.
715 The last argument specifies the function to allocate in. */
716
717rtx
718assign_outer_stack_local (mode, size, align, function)
719 enum machine_mode mode;
720 int size;
721 int align;
722 struct function *function;
723{
724 register rtx x, addr;
725 int bigend_correction = 0;
726 int alignment;
727
728 /* Allocate in the memory associated with the function in whose frame
729 we are assigning. */
730 push_obstacks (function->function_obstack,
731 function->function_maybepermanent_obstack);
732
733 if (align == 0)
734 {
735 alignment = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
736 if (mode == BLKmode)
737 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
738 }
739 else if (align == -1)
740 {
741 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
742 size = CEIL_ROUND (size, alignment);
743 }
744 else
745 alignment = align / BITS_PER_UNIT;
746
6f086dfc
RS
747 /* Round frame offset to that alignment. */
748#ifdef FRAME_GROWS_DOWNWARD
2af69b62 749 function->frame_offset = FLOOR_ROUND (function->frame_offset, alignment);
6f086dfc 750#else
2af69b62 751 function->frame_offset = CEIL_ROUND (function->frame_offset, alignment);
6f086dfc
RS
752#endif
753
754 /* On a big-endian machine, if we are allocating more space than we will use,
755 use the least significant bytes of those that are allocated. */
f76b9db2 756 if (BYTES_BIG_ENDIAN && mode != BLKmode)
6f086dfc 757 bigend_correction = size - GET_MODE_SIZE (mode);
6f086dfc
RS
758
759#ifdef FRAME_GROWS_DOWNWARD
760 function->frame_offset -= size;
761#endif
762 addr = plus_constant (virtual_stack_vars_rtx,
763 function->frame_offset + bigend_correction);
764#ifndef FRAME_GROWS_DOWNWARD
765 function->frame_offset += size;
766#endif
767
768 x = gen_rtx (MEM, mode, addr);
769
770 function->stack_slot_list
771 = gen_rtx (EXPR_LIST, VOIDmode, x, function->stack_slot_list);
772
773 pop_obstacks ();
774
775 return x;
776}
777\f
778/* Allocate a temporary stack slot and record it for possible later
779 reuse.
780
781 MODE is the machine mode to be given to the returned rtx.
782
783 SIZE is the size in units of the space required. We do no rounding here
784 since assign_stack_local will do any required rounding.
785
d93d4205
MS
786 KEEP is 1 if this slot is to be retained after a call to
787 free_temp_slots. Automatic variables for a block are allocated
788 with this flag. KEEP is 2, if we allocate a longer term temporary,
789 whose lifetime is controlled by CLEANUP_POINT_EXPRs. */
6f086dfc
RS
790
791rtx
792assign_stack_temp (mode, size, keep)
793 enum machine_mode mode;
794 int size;
795 int keep;
796{
797 struct temp_slot *p, *best_p = 0;
798
303ec2aa
RK
799 /* If SIZE is -1 it means that somebody tried to allocate a temporary
800 of a variable size. */
801 if (size == -1)
802 abort ();
803
6f086dfc
RS
804 /* First try to find an available, already-allocated temporary that is the
805 exact size we require. */
806 for (p = temp_slots; p; p = p->next)
807 if (p->size == size && GET_MODE (p->slot) == mode && ! p->in_use)
808 break;
809
810 /* If we didn't find, one, try one that is larger than what we want. We
811 find the smallest such. */
812 if (p == 0)
813 for (p = temp_slots; p; p = p->next)
814 if (p->size > size && GET_MODE (p->slot) == mode && ! p->in_use
815 && (best_p == 0 || best_p->size > p->size))
816 best_p = p;
817
818 /* Make our best, if any, the one to use. */
819 if (best_p)
a45035b6
JW
820 {
821 /* If there are enough aligned bytes left over, make them into a new
822 temp_slot so that the extra bytes don't get wasted. Do this only
823 for BLKmode slots, so that we can be sure of the alignment. */
824 if (GET_MODE (best_p->slot) == BLKmode)
825 {
826 int alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
827 int rounded_size = CEIL_ROUND (size, alignment);
828
829 if (best_p->size - rounded_size >= alignment)
830 {
831 p = (struct temp_slot *) oballoc (sizeof (struct temp_slot));
a25d4ba2 832 p->in_use = p->addr_taken = 0;
a45035b6
JW
833 p->size = best_p->size - rounded_size;
834 p->slot = gen_rtx (MEM, BLKmode,
835 plus_constant (XEXP (best_p->slot, 0),
836 rounded_size));
e5e76139 837 p->address = 0;
84e24c03 838 p->rtl_expr = 0;
a45035b6
JW
839 p->next = temp_slots;
840 temp_slots = p;
841
842 stack_slot_list = gen_rtx (EXPR_LIST, VOIDmode, p->slot,
843 stack_slot_list);
844
845 best_p->size = rounded_size;
846 }
847 }
848
849 p = best_p;
850 }
851
6f086dfc
RS
852 /* If we still didn't find one, make a new temporary. */
853 if (p == 0)
854 {
b2a80c0d 855 int frame_offset_old = frame_offset;
6f086dfc 856 p = (struct temp_slot *) oballoc (sizeof (struct temp_slot));
6f086dfc
RS
857 /* If the temp slot mode doesn't indicate the alignment,
858 use the largest possible, so no one will be disappointed. */
e5e76139 859 p->slot = assign_stack_local (mode, size, mode == BLKmode ? -1 : 0);
b2a80c0d
DE
860 /* The following slot size computation is necessary because we don't
861 know the actual size of the temporary slot until assign_stack_local
862 has performed all the frame alignment and size rounding for the
863 requested temporary. Otherwise combine_temp_slots won't think that
864 adjacent slots really are adjacent. */
865#ifdef FRAME_GROWS_DOWNWARD
866 p->size = frame_offset_old - frame_offset;
867#else
868 p->size = frame_offset - frame_offset_old;
869#endif
e5e76139 870 p->address = 0;
6f086dfc
RS
871 p->next = temp_slots;
872 temp_slots = p;
873 }
874
875 p->in_use = 1;
a25d4ba2 876 p->addr_taken = 0;
e7a84011 877 p->rtl_expr = sequence_rtl_expr;
a25d4ba2 878
d93d4205
MS
879 if (keep == 2)
880 {
881 p->level = target_temp_slot_level;
882 p->keep = 0;
883 }
884 else
885 {
886 p->level = temp_slot_level;
887 p->keep = keep;
888 }
6f086dfc
RS
889 return p->slot;
890}
a45035b6
JW
891
892/* Combine temporary stack slots which are adjacent on the stack.
893
894 This allows for better use of already allocated stack space. This is only
895 done for BLKmode slots because we can be sure that we won't have alignment
896 problems in this case. */
897
898void
899combine_temp_slots ()
900{
901 struct temp_slot *p, *q;
902 struct temp_slot *prev_p, *prev_q;
e9b7093a
RS
903 /* Determine where to free back to after this function. */
904 rtx free_pointer = rtx_alloc (CONST_INT);
a45035b6 905
e9b7093a
RS
906 for (p = temp_slots, prev_p = 0; p; p = prev_p ? prev_p->next : temp_slots)
907 {
908 int delete_p = 0;
909 if (! p->in_use && GET_MODE (p->slot) == BLKmode)
910 for (q = p->next, prev_q = p; q; q = prev_q->next)
a45035b6 911 {
e9b7093a
RS
912 int delete_q = 0;
913 if (! q->in_use && GET_MODE (q->slot) == BLKmode)
a45035b6 914 {
e9b7093a
RS
915 if (rtx_equal_p (plus_constant (XEXP (p->slot, 0), p->size),
916 XEXP (q->slot, 0)))
917 {
918 /* Q comes after P; combine Q into P. */
919 p->size += q->size;
920 delete_q = 1;
921 }
922 else if (rtx_equal_p (plus_constant (XEXP (q->slot, 0), q->size),
923 XEXP (p->slot, 0)))
924 {
925 /* P comes after Q; combine P into Q. */
926 q->size += p->size;
927 delete_p = 1;
928 break;
929 }
a45035b6 930 }
e9b7093a
RS
931 /* Either delete Q or advance past it. */
932 if (delete_q)
933 prev_q->next = q->next;
934 else
935 prev_q = q;
a45035b6 936 }
e9b7093a
RS
937 /* Either delete P or advance past it. */
938 if (delete_p)
939 {
940 if (prev_p)
941 prev_p->next = p->next;
942 else
943 temp_slots = p->next;
944 }
945 else
946 prev_p = p;
947 }
948
949 /* Free all the RTL made by plus_constant. */
950 rtx_free (free_pointer);
a45035b6 951}
6f086dfc 952\f
e5e76139
RK
953/* Find the temp slot corresponding to the object at address X. */
954
955static struct temp_slot *
956find_temp_slot_from_address (x)
957 rtx x;
958{
959 struct temp_slot *p;
960 rtx next;
961
962 for (p = temp_slots; p; p = p->next)
963 {
964 if (! p->in_use)
965 continue;
966 else if (XEXP (p->slot, 0) == x
967 || p->address == x)
968 return p;
969
970 else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
971 for (next = p->address; next; next = XEXP (next, 1))
972 if (XEXP (next, 0) == x)
973 return p;
974 }
975
976 return 0;
977}
978
979/* Indicate that NEW is an alternate way of refering to the temp slot
980 that previous was known by OLD. */
981
982void
983update_temp_slot_address (old, new)
984 rtx old, new;
985{
986 struct temp_slot *p = find_temp_slot_from_address (old);
987
988 /* If none, return. Else add NEW as an alias. */
989 if (p == 0)
990 return;
991 else if (p->address == 0)
992 p->address = new;
993 else
994 {
995 if (GET_CODE (p->address) != EXPR_LIST)
996 p->address = gen_rtx (EXPR_LIST, VOIDmode, p->address, NULL_RTX);
997
998 p->address = gen_rtx (EXPR_LIST, VOIDmode, new, p->address);
999 }
1000}
1001
a25d4ba2
RK
1002/* If X could be a reference to a temporary slot, mark the fact that its
1003 adddress was taken. */
1004
1005void
1006mark_temp_addr_taken (x)
1007 rtx x;
1008{
1009 struct temp_slot *p;
1010
1011 if (x == 0)
1012 return;
1013
1014 /* If X is not in memory or is at a constant address, it cannot be in
1015 a temporary slot. */
1016 if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1017 return;
1018
1019 p = find_temp_slot_from_address (XEXP (x, 0));
1020 if (p != 0)
1021 p->addr_taken = 1;
1022}
1023
6f086dfc
RS
1024/* If X could be a reference to a temporary slot, mark that slot as belonging
1025 to the to one level higher. If X matched one of our slots, just mark that
1026 one. Otherwise, we can't easily predict which it is, so upgrade all of
1027 them. Kept slots need not be touched.
1028
1029 This is called when an ({...}) construct occurs and a statement
1030 returns a value in memory. */
1031
1032void
1033preserve_temp_slots (x)
1034 rtx x;
1035{
a25d4ba2 1036 struct temp_slot *p = 0;
6f086dfc 1037
73620b82
RK
1038 /* If there is no result, we still might have some objects whose address
1039 were taken, so we need to make sure they stay around. */
e3a77161 1040 if (x == 0)
73620b82
RK
1041 {
1042 for (p = temp_slots; p; p = p->next)
1043 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1044 p->level--;
1045
1046 return;
1047 }
e3a77161
RK
1048
1049 /* If X is a register that is being used as a pointer, see if we have
1050 a temporary slot we know it points to. To be consistent with
1051 the code below, we really should preserve all non-kept slots
1052 if we can't find a match, but that seems to be much too costly. */
a25d4ba2
RK
1053 if (GET_CODE (x) == REG && REGNO_POINTER_FLAG (REGNO (x)))
1054 p = find_temp_slot_from_address (x);
1055
6f086dfc 1056 /* If X is not in memory or is at a constant address, it cannot be in
e19571db
RK
1057 a temporary slot, but it can contain something whose address was
1058 taken. */
a25d4ba2 1059 if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
e19571db
RK
1060 {
1061 for (p = temp_slots; p; p = p->next)
1062 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1063 p->level--;
1064
1065 return;
1066 }
6f086dfc
RS
1067
1068 /* First see if we can find a match. */
73620b82 1069 if (p == 0)
a25d4ba2
RK
1070 p = find_temp_slot_from_address (XEXP (x, 0));
1071
e5e76139
RK
1072 if (p != 0)
1073 {
a25d4ba2
RK
1074 /* Move everything at our level whose address was taken to our new
1075 level in case we used its address. */
1076 struct temp_slot *q;
1077
1078 for (q = temp_slots; q; q = q->next)
1079 if (q != p && q->addr_taken && q->level == p->level)
1080 q->level--;
1081
e5e76139
RK
1082 p->level--;
1083 return;
1084 }
6f086dfc
RS
1085
1086 /* Otherwise, preserve all non-kept slots at this level. */
1087 for (p = temp_slots; p; p = p->next)
1088 if (p->in_use && p->level == temp_slot_level && ! p->keep)
1089 p->level--;
1090}
1091
422c8f63
RK
1092/* X is the result of an RTL_EXPR. If it is a temporary slot associated
1093 with that RTL_EXPR, promote it into a temporary slot at the present
1094 level so it will not be freed when we free slots made in the
1095 RTL_EXPR. */
1096
1097void
1098preserve_rtl_expr_result (x)
1099 rtx x;
1100{
1101 struct temp_slot *p;
1102
1103 /* If X is not in memory or is at a constant address, it cannot be in
1104 a temporary slot. */
1105 if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1106 return;
1107
199b61d8
RK
1108 /* If we can find a match, move it to our level unless it is already at
1109 an upper level. */
1110 p = find_temp_slot_from_address (XEXP (x, 0));
1111 if (p != 0)
1112 {
1113 p->level = MIN (p->level, temp_slot_level);
1114 p->rtl_expr = 0;
1115 }
422c8f63
RK
1116
1117 return;
1118}
1119
6f086dfc 1120/* Free all temporaries used so far. This is normally called at the end
e7a84011
RK
1121 of generating code for a statement. Don't free any temporaries
1122 currently in use for an RTL_EXPR that hasn't yet been emitted.
1123 We could eventually do better than this since it can be reused while
1124 generating the same RTL_EXPR, but this is complex and probably not
1125 worthwhile. */
6f086dfc
RS
1126
1127void
1128free_temp_slots ()
1129{
1130 struct temp_slot *p;
1131
1132 for (p = temp_slots; p; p = p->next)
e7a84011
RK
1133 if (p->in_use && p->level == temp_slot_level && ! p->keep
1134 && p->rtl_expr == 0)
1135 p->in_use = 0;
1136
1137 combine_temp_slots ();
1138}
1139
1140/* Free all temporary slots used in T, an RTL_EXPR node. */
1141
1142void
1143free_temps_for_rtl_expr (t)
1144 tree t;
1145{
1146 struct temp_slot *p;
1147
1148 for (p = temp_slots; p; p = p->next)
1149 if (p->rtl_expr == t)
6f086dfc 1150 p->in_use = 0;
a45035b6
JW
1151
1152 combine_temp_slots ();
6f086dfc
RS
1153}
1154
1155/* Push deeper into the nesting level for stack temporaries. */
1156
1157void
1158push_temp_slots ()
1159{
6f086dfc
RS
1160 temp_slot_level++;
1161}
1162
1163/* Pop a temporary nesting level. All slots in use in the current level
1164 are freed. */
1165
1166void
1167pop_temp_slots ()
1168{
1169 struct temp_slot *p;
1170
6f086dfc 1171 for (p = temp_slots; p; p = p->next)
e7a84011 1172 if (p->in_use && p->level == temp_slot_level && p->rtl_expr == 0)
6f086dfc
RS
1173 p->in_use = 0;
1174
a45035b6
JW
1175 combine_temp_slots ();
1176
6f086dfc
RS
1177 temp_slot_level--;
1178}
1179\f
1180/* Retroactively move an auto variable from a register to a stack slot.
1181 This is done when an address-reference to the variable is seen. */
1182
1183void
1184put_var_into_stack (decl)
1185 tree decl;
1186{
1187 register rtx reg;
00d8a4c1 1188 enum machine_mode promoted_mode, decl_mode;
6f086dfc 1189 struct function *function = 0;
c20bf1f3
JB
1190 tree context;
1191
1192 if (output_bytecode)
1193 return;
1194
1195 context = decl_function_context (decl);
6f086dfc 1196
00d8a4c1 1197 /* Get the current rtl used for this object and it's original mode. */
6f086dfc 1198 reg = TREE_CODE (decl) == SAVE_EXPR ? SAVE_EXPR_RTL (decl) : DECL_RTL (decl);
2baccce2
RS
1199
1200 /* No need to do anything if decl has no rtx yet
1201 since in that case caller is setting TREE_ADDRESSABLE
1202 and a stack slot will be assigned when the rtl is made. */
1203 if (reg == 0)
1204 return;
00d8a4c1
RK
1205
1206 /* Get the declared mode for this object. */
1207 decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1208 : DECL_MODE (decl));
2baccce2
RS
1209 /* Get the mode it's actually stored in. */
1210 promoted_mode = GET_MODE (reg);
6f086dfc
RS
1211
1212 /* If this variable comes from an outer function,
1213 find that function's saved context. */
1214 if (context != current_function_decl)
1215 for (function = outer_function_chain; function; function = function->next)
1216 if (function->decl == context)
1217 break;
1218
6f086dfc
RS
1219 /* If this is a variable-size object with a pseudo to address it,
1220 put that pseudo into the stack, if the var is nonlocal. */
a82ad570 1221 if (DECL_NONLOCAL (decl)
6f086dfc
RS
1222 && GET_CODE (reg) == MEM
1223 && GET_CODE (XEXP (reg, 0)) == REG
1224 && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
4cdb3e78
RS
1225 {
1226 reg = XEXP (reg, 0);
1227 decl_mode = promoted_mode = GET_MODE (reg);
1228 }
e15762df 1229
293e3de4
RS
1230 /* Now we should have a value that resides in one or more pseudo regs. */
1231
1232 if (GET_CODE (reg) == REG)
1233 put_reg_into_stack (function, reg, TREE_TYPE (decl),
1234 promoted_mode, decl_mode);
1235 else if (GET_CODE (reg) == CONCAT)
1236 {
1237 /* A CONCAT contains two pseudos; put them both in the stack.
1238 We do it so they end up consecutive. */
1239 enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
1240 tree part_type = TREE_TYPE (TREE_TYPE (decl));
1241#ifdef STACK_GROWS_DOWNWARD
1242 /* Since part 0 should have a lower address, do it second. */
1243 put_reg_into_stack (function, XEXP (reg, 1),
1244 part_type, part_mode, part_mode);
1245 put_reg_into_stack (function, XEXP (reg, 0),
1246 part_type, part_mode, part_mode);
1247#else
1248 put_reg_into_stack (function, XEXP (reg, 0),
1249 part_type, part_mode, part_mode);
1250 put_reg_into_stack (function, XEXP (reg, 1),
1251 part_type, part_mode, part_mode);
1252#endif
1253
1254 /* Change the CONCAT into a combined MEM for both parts. */
1255 PUT_CODE (reg, MEM);
1256 /* The two parts are in memory order already.
1257 Use the lower parts address as ours. */
1258 XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1259 /* Prevent sharing of rtl that might lose. */
1260 if (GET_CODE (XEXP (reg, 0)) == PLUS)
1261 XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
1262 }
1263}
1264
1265/* Subroutine of put_var_into_stack. This puts a single pseudo reg REG
1266 into the stack frame of FUNCTION (0 means the current function).
1267 DECL_MODE is the machine mode of the user-level data type.
1268 PROMOTED_MODE is the machine mode of the register. */
1269
1270static void
1271put_reg_into_stack (function, reg, type, promoted_mode, decl_mode)
1272 struct function *function;
1273 rtx reg;
1274 tree type;
1275 enum machine_mode promoted_mode, decl_mode;
1276{
1277 rtx new = 0;
6f086dfc
RS
1278
1279 if (function)
1280 {
1281 if (REGNO (reg) < function->max_parm_reg)
1282 new = function->parm_reg_stack_loc[REGNO (reg)];
1283 if (new == 0)
e15762df 1284 new = assign_outer_stack_local (decl_mode, GET_MODE_SIZE (decl_mode),
6f086dfc
RS
1285 0, function);
1286 }
1287 else
1288 {
1289 if (REGNO (reg) < max_parm_reg)
1290 new = parm_reg_stack_loc[REGNO (reg)];
1291 if (new == 0)
e15762df 1292 new = assign_stack_local (decl_mode, GET_MODE_SIZE (decl_mode), 0);
6f086dfc
RS
1293 }
1294
1295 XEXP (reg, 0) = XEXP (new, 0);
1296 /* `volatil' bit means one thing for MEMs, another entirely for REGs. */
1297 REG_USERVAR_P (reg) = 0;
1298 PUT_CODE (reg, MEM);
00d8a4c1 1299 PUT_MODE (reg, decl_mode);
6f086dfc
RS
1300
1301 /* If this is a memory ref that contains aggregate components,
1302 mark it as such for cse and loop optimize. */
05e3bdb9 1303 MEM_IN_STRUCT_P (reg) = AGGREGATE_TYPE_P (type);
6f086dfc
RS
1304
1305 /* Now make sure that all refs to the variable, previously made
1306 when it was a register, are fixed up to be valid again. */
1307 if (function)
1308 {
1309 struct var_refs_queue *temp;
1310
1311 /* Variable is inherited; fix it up when we get back to its function. */
1312 push_obstacks (function->function_obstack,
1313 function->function_maybepermanent_obstack);
4da73fa0
RK
1314
1315 /* See comment in restore_tree_status in tree.c for why this needs to be
1316 on saveable obstack. */
6f086dfc 1317 temp
4da73fa0 1318 = (struct var_refs_queue *) savealloc (sizeof (struct var_refs_queue));
6f086dfc 1319 temp->modified = reg;
00d8a4c1 1320 temp->promoted_mode = promoted_mode;
293e3de4 1321 temp->unsignedp = TREE_UNSIGNED (type);
6f086dfc
RS
1322 temp->next = function->fixup_var_refs_queue;
1323 function->fixup_var_refs_queue = temp;
1324 pop_obstacks ();
1325 }
1326 else
1327 /* Variable is local; fix it up now. */
293e3de4 1328 fixup_var_refs (reg, promoted_mode, TREE_UNSIGNED (type));
6f086dfc
RS
1329}
1330\f
1331static void
00d8a4c1 1332fixup_var_refs (var, promoted_mode, unsignedp)
6f086dfc 1333 rtx var;
00d8a4c1
RK
1334 enum machine_mode promoted_mode;
1335 int unsignedp;
6f086dfc
RS
1336{
1337 tree pending;
1338 rtx first_insn = get_insns ();
1339 struct sequence_stack *stack = sequence_stack;
1340 tree rtl_exps = rtl_expr_chain;
1341
1342 /* Must scan all insns for stack-refs that exceed the limit. */
00d8a4c1 1343 fixup_var_refs_insns (var, promoted_mode, unsignedp, first_insn, stack == 0);
6f086dfc
RS
1344
1345 /* Scan all pending sequences too. */
1346 for (; stack; stack = stack->next)
1347 {
1348 push_to_sequence (stack->first);
00d8a4c1
RK
1349 fixup_var_refs_insns (var, promoted_mode, unsignedp,
1350 stack->first, stack->next != 0);
6f086dfc
RS
1351 /* Update remembered end of sequence
1352 in case we added an insn at the end. */
1353 stack->last = get_last_insn ();
1354 end_sequence ();
1355 }
1356
1357 /* Scan all waiting RTL_EXPRs too. */
1358 for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1359 {
1360 rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1361 if (seq != const0_rtx && seq != 0)
1362 {
1363 push_to_sequence (seq);
00d8a4c1 1364 fixup_var_refs_insns (var, promoted_mode, unsignedp, seq, 0);
6f086dfc
RS
1365 end_sequence ();
1366 }
1367 }
1368}
1369\f
e15679f8 1370/* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
6f086dfc
RS
1371 some part of an insn. Return a struct fixup_replacement whose OLD
1372 value is equal to X. Allocate a new structure if no such entry exists. */
1373
1374static struct fixup_replacement *
2740a678 1375find_fixup_replacement (replacements, x)
6f086dfc
RS
1376 struct fixup_replacement **replacements;
1377 rtx x;
1378{
1379 struct fixup_replacement *p;
1380
1381 /* See if we have already replaced this. */
1382 for (p = *replacements; p && p->old != x; p = p->next)
1383 ;
1384
1385 if (p == 0)
1386 {
1387 p = (struct fixup_replacement *) oballoc (sizeof (struct fixup_replacement));
1388 p->old = x;
1389 p->new = 0;
1390 p->next = *replacements;
1391 *replacements = p;
1392 }
1393
1394 return p;
1395}
1396
1397/* Scan the insn-chain starting with INSN for refs to VAR
1398 and fix them up. TOPLEVEL is nonzero if this chain is the
1399 main chain of insns for the current function. */
1400
1401static void
00d8a4c1 1402fixup_var_refs_insns (var, promoted_mode, unsignedp, insn, toplevel)
6f086dfc 1403 rtx var;
00d8a4c1
RK
1404 enum machine_mode promoted_mode;
1405 int unsignedp;
6f086dfc
RS
1406 rtx insn;
1407 int toplevel;
1408{
02a10449
RK
1409 rtx call_dest = 0;
1410
6f086dfc
RS
1411 while (insn)
1412 {
1413 rtx next = NEXT_INSN (insn);
1414 rtx note;
e15762df 1415 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
6f086dfc 1416 {
63770d6a
RK
1417 /* If this is a CLOBBER of VAR, delete it.
1418
1419 If it has a REG_LIBCALL note, delete the REG_LIBCALL
1420 and REG_RETVAL notes too. */
1421 if (GET_CODE (PATTERN (insn)) == CLOBBER
1422 && XEXP (PATTERN (insn), 0) == var)
1423 {
1424 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1425 /* The REG_LIBCALL note will go away since we are going to
1426 turn INSN into a NOTE, so just delete the
1427 corresponding REG_RETVAL note. */
1428 remove_note (XEXP (note, 0),
1429 find_reg_note (XEXP (note, 0), REG_RETVAL,
1430 NULL_RTX));
1431
1432 /* In unoptimized compilation, we shouldn't call delete_insn
1433 except in jump.c doing warnings. */
1434 PUT_CODE (insn, NOTE);
1435 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1436 NOTE_SOURCE_FILE (insn) = 0;
1437 }
1438
6f086dfc
RS
1439 /* The insn to load VAR from a home in the arglist
1440 is now a no-op. When we see it, just delete it. */
63770d6a
RK
1441 else if (toplevel
1442 && GET_CODE (PATTERN (insn)) == SET
1443 && SET_DEST (PATTERN (insn)) == var
1444 /* If this represents the result of an insn group,
1445 don't delete the insn. */
1446 && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
1447 && rtx_equal_p (SET_SRC (PATTERN (insn)), var))
6f086dfc 1448 {
b4ff474c
RS
1449 /* In unoptimized compilation, we shouldn't call delete_insn
1450 except in jump.c doing warnings. */
1451 PUT_CODE (insn, NOTE);
1452 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1453 NOTE_SOURCE_FILE (insn) = 0;
6f086dfc
RS
1454 if (insn == last_parm_insn)
1455 last_parm_insn = PREV_INSN (next);
1456 }
1457 else
1458 {
02a10449
RK
1459 struct fixup_replacement *replacements = 0;
1460 rtx next_insn = NEXT_INSN (insn);
1461
1462#ifdef SMALL_REGISTER_CLASSES
1463 /* If the insn that copies the results of a CALL_INSN
1464 into a pseudo now references VAR, we have to use an
1465 intermediate pseudo since we want the life of the
1466 return value register to be only a single insn.
1467
1468 If we don't use an intermediate pseudo, such things as
1469 address computations to make the address of VAR valid
1470 if it is not can be placed beween the CALL_INSN and INSN.
1471
1472 To make sure this doesn't happen, we record the destination
1473 of the CALL_INSN and see if the next insn uses both that
1474 and VAR. */
1475
1476 if (call_dest != 0 && GET_CODE (insn) == INSN
1477 && reg_mentioned_p (var, PATTERN (insn))
1478 && reg_mentioned_p (call_dest, PATTERN (insn)))
1479 {
1480 rtx temp = gen_reg_rtx (GET_MODE (call_dest));
1481
1482 emit_insn_before (gen_move_insn (temp, call_dest), insn);
1483
1484 PATTERN (insn) = replace_rtx (PATTERN (insn),
1485 call_dest, temp);
1486 }
1487
1488 if (GET_CODE (insn) == CALL_INSN
1489 && GET_CODE (PATTERN (insn)) == SET)
1490 call_dest = SET_DEST (PATTERN (insn));
1491 else if (GET_CODE (insn) == CALL_INSN
1492 && GET_CODE (PATTERN (insn)) == PARALLEL
1493 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1494 call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1495 else
1496 call_dest = 0;
1497#endif
1498
6f086dfc
RS
1499 /* See if we have to do anything to INSN now that VAR is in
1500 memory. If it needs to be loaded into a pseudo, use a single
1501 pseudo for the entire insn in case there is a MATCH_DUP
1502 between two operands. We pass a pointer to the head of
1503 a list of struct fixup_replacements. If fixup_var_refs_1
1504 needs to allocate pseudos or replacement MEMs (for SUBREGs),
1505 it will record them in this list.
1506
1507 If it allocated a pseudo for any replacement, we copy into
1508 it here. */
1509
00d8a4c1
RK
1510 fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1511 &replacements);
6f086dfc 1512
77121fee
JW
1513 /* If this is last_parm_insn, and any instructions were output
1514 after it to fix it up, then we must set last_parm_insn to
1515 the last such instruction emitted. */
1516 if (insn == last_parm_insn)
1517 last_parm_insn = PREV_INSN (next_insn);
1518
6f086dfc
RS
1519 while (replacements)
1520 {
1521 if (GET_CODE (replacements->new) == REG)
1522 {
1523 rtx insert_before;
00d8a4c1 1524 rtx seq;
6f086dfc
RS
1525
1526 /* OLD might be a (subreg (mem)). */
1527 if (GET_CODE (replacements->old) == SUBREG)
1528 replacements->old
1529 = fixup_memory_subreg (replacements->old, insn, 0);
1530 else
1531 replacements->old
1532 = fixup_stack_1 (replacements->old, insn);
1533
5fa7422b 1534 insert_before = insn;
6f086dfc 1535
00d8a4c1
RK
1536 /* If we are changing the mode, do a conversion.
1537 This might be wasteful, but combine.c will
1538 eliminate much of the waste. */
1539
1540 if (GET_MODE (replacements->new)
1541 != GET_MODE (replacements->old))
1542 {
1543 start_sequence ();
1544 convert_move (replacements->new,
1545 replacements->old, unsignedp);
1546 seq = gen_sequence ();
1547 end_sequence ();
1548 }
1549 else
1550 seq = gen_move_insn (replacements->new,
1551 replacements->old);
1552
1553 emit_insn_before (seq, insert_before);
6f086dfc
RS
1554 }
1555
1556 replacements = replacements->next;
1557 }
1558 }
1559
1560 /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1561 But don't touch other insns referred to by reg-notes;
1562 we will get them elsewhere. */
1563 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1564 if (GET_CODE (note) != INSN_LIST)
ab6155b7
RK
1565 XEXP (note, 0)
1566 = walk_fixup_memory_subreg (XEXP (note, 0), insn, 1);
6f086dfc
RS
1567 }
1568 insn = next;
1569 }
1570}
1571\f
00d8a4c1
RK
1572/* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
1573 See if the rtx expression at *LOC in INSN needs to be changed.
6f086dfc
RS
1574
1575 REPLACEMENTS is a pointer to a list head that starts out zero, but may
1576 contain a list of original rtx's and replacements. If we find that we need
1577 to modify this insn by replacing a memory reference with a pseudo or by
1578 making a new MEM to implement a SUBREG, we consult that list to see if
1579 we have already chosen a replacement. If none has already been allocated,
1580 we allocate it and update the list. fixup_var_refs_insns will copy VAR
1581 or the SUBREG, as appropriate, to the pseudo. */
1582
1583static void
00d8a4c1 1584fixup_var_refs_1 (var, promoted_mode, loc, insn, replacements)
6f086dfc 1585 register rtx var;
00d8a4c1 1586 enum machine_mode promoted_mode;
6f086dfc
RS
1587 register rtx *loc;
1588 rtx insn;
1589 struct fixup_replacement **replacements;
1590{
1591 register int i;
1592 register rtx x = *loc;
1593 RTX_CODE code = GET_CODE (x);
1594 register char *fmt;
1595 register rtx tem, tem1;
1596 struct fixup_replacement *replacement;
1597
1598 switch (code)
1599 {
1600 case MEM:
1601 if (var == x)
1602 {
1603 /* If we already have a replacement, use it. Otherwise,
1604 try to fix up this address in case it is invalid. */
1605
2740a678 1606 replacement = find_fixup_replacement (replacements, var);
6f086dfc
RS
1607 if (replacement->new)
1608 {
1609 *loc = replacement->new;
1610 return;
1611 }
1612
1613 *loc = replacement->new = x = fixup_stack_1 (x, insn);
1614
00d8a4c1
RK
1615 /* Unless we are forcing memory to register or we changed the mode,
1616 we can leave things the way they are if the insn is valid. */
6f086dfc
RS
1617
1618 INSN_CODE (insn) = -1;
00d8a4c1
RK
1619 if (! flag_force_mem && GET_MODE (x) == promoted_mode
1620 && recog_memoized (insn) >= 0)
6f086dfc
RS
1621 return;
1622
00d8a4c1 1623 *loc = replacement->new = gen_reg_rtx (promoted_mode);
6f086dfc
RS
1624 return;
1625 }
1626
1627 /* If X contains VAR, we need to unshare it here so that we update
1628 each occurrence separately. But all identical MEMs in one insn
1629 must be replaced with the same rtx because of the possibility of
1630 MATCH_DUPs. */
1631
1632 if (reg_mentioned_p (var, x))
1633 {
2740a678 1634 replacement = find_fixup_replacement (replacements, x);
6f086dfc
RS
1635 if (replacement->new == 0)
1636 replacement->new = copy_most_rtx (x, var);
1637
1638 *loc = x = replacement->new;
1639 }
1640 break;
1641
1642 case REG:
1643 case CC0:
1644 case PC:
1645 case CONST_INT:
1646 case CONST:
1647 case SYMBOL_REF:
1648 case LABEL_REF:
1649 case CONST_DOUBLE:
1650 return;
1651
1652 case SIGN_EXTRACT:
1653 case ZERO_EXTRACT:
1654 /* Note that in some cases those types of expressions are altered
1655 by optimize_bit_field, and do not survive to get here. */
1656 if (XEXP (x, 0) == var
1657 || (GET_CODE (XEXP (x, 0)) == SUBREG
1658 && SUBREG_REG (XEXP (x, 0)) == var))
1659 {
1660 /* Get TEM as a valid MEM in the mode presently in the insn.
1661
1662 We don't worry about the possibility of MATCH_DUP here; it
1663 is highly unlikely and would be tricky to handle. */
1664
1665 tem = XEXP (x, 0);
1666 if (GET_CODE (tem) == SUBREG)
1667 tem = fixup_memory_subreg (tem, insn, 1);
1668 tem = fixup_stack_1 (tem, insn);
1669
1670 /* Unless we want to load from memory, get TEM into the proper mode
1671 for an extract from memory. This can only be done if the
1672 extract is at a constant position and length. */
1673
1674 if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
1675 && GET_CODE (XEXP (x, 2)) == CONST_INT
1676 && ! mode_dependent_address_p (XEXP (tem, 0))
1677 && ! MEM_VOLATILE_P (tem))
1678 {
1679 enum machine_mode wanted_mode = VOIDmode;
1680 enum machine_mode is_mode = GET_MODE (tem);
1681 int width = INTVAL (XEXP (x, 1));
1682 int pos = INTVAL (XEXP (x, 2));
1683
1684#ifdef HAVE_extzv
1685 if (GET_CODE (x) == ZERO_EXTRACT)
1686 wanted_mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
1687#endif
1688#ifdef HAVE_extv
1689 if (GET_CODE (x) == SIGN_EXTRACT)
1690 wanted_mode = insn_operand_mode[(int) CODE_FOR_extv][1];
1691#endif
6dc42e49 1692 /* If we have a narrower mode, we can do something. */
6f086dfc
RS
1693 if (wanted_mode != VOIDmode
1694 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
1695 {
1696 int offset = pos / BITS_PER_UNIT;
1697 rtx old_pos = XEXP (x, 2);
1698 rtx newmem;
1699
1700 /* If the bytes and bits are counted differently, we
1701 must adjust the offset. */
f76b9db2
ILT
1702 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
1703 offset = (GET_MODE_SIZE (is_mode)
1704 - GET_MODE_SIZE (wanted_mode) - offset);
6f086dfc
RS
1705
1706 pos %= GET_MODE_BITSIZE (wanted_mode);
1707
1708 newmem = gen_rtx (MEM, wanted_mode,
1709 plus_constant (XEXP (tem, 0), offset));
1710 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (tem);
1711 MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (tem);
1712 MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (tem);
1713
1714 /* Make the change and see if the insn remains valid. */
1715 INSN_CODE (insn) = -1;
1716 XEXP (x, 0) = newmem;
5f4f0e22 1717 XEXP (x, 2) = GEN_INT (pos);
6f086dfc
RS
1718
1719 if (recog_memoized (insn) >= 0)
1720 return;
1721
1722 /* Otherwise, restore old position. XEXP (x, 0) will be
1723 restored later. */
1724 XEXP (x, 2) = old_pos;
1725 }
1726 }
1727
1728 /* If we get here, the bitfield extract insn can't accept a memory
1729 reference. Copy the input into a register. */
1730
1731 tem1 = gen_reg_rtx (GET_MODE (tem));
1732 emit_insn_before (gen_move_insn (tem1, tem), insn);
1733 XEXP (x, 0) = tem1;
1734 return;
1735 }
1736 break;
1737
1738 case SUBREG:
1739 if (SUBREG_REG (x) == var)
1740 {
00d8a4c1
RK
1741 /* If this is a special SUBREG made because VAR was promoted
1742 from a wider mode, replace it with VAR and call ourself
1743 recursively, this time saying that the object previously
1744 had its current mode (by virtue of the SUBREG). */
1745
1746 if (SUBREG_PROMOTED_VAR_P (x))
1747 {
1748 *loc = var;
1749 fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements);
1750 return;
1751 }
1752
6f086dfc
RS
1753 /* If this SUBREG makes VAR wider, it has become a paradoxical
1754 SUBREG with VAR in memory, but these aren't allowed at this
1755 stage of the compilation. So load VAR into a pseudo and take
1756 a SUBREG of that pseudo. */
1757 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
1758 {
2740a678 1759 replacement = find_fixup_replacement (replacements, var);
6f086dfc
RS
1760 if (replacement->new == 0)
1761 replacement->new = gen_reg_rtx (GET_MODE (var));
1762 SUBREG_REG (x) = replacement->new;
1763 return;
1764 }
1765
1766 /* See if we have already found a replacement for this SUBREG.
1767 If so, use it. Otherwise, make a MEM and see if the insn
1768 is recognized. If not, or if we should force MEM into a register,
1769 make a pseudo for this SUBREG. */
2740a678 1770 replacement = find_fixup_replacement (replacements, x);
6f086dfc
RS
1771 if (replacement->new)
1772 {
1773 *loc = replacement->new;
1774 return;
1775 }
1776
1777 replacement->new = *loc = fixup_memory_subreg (x, insn, 0);
1778
f898f031 1779 INSN_CODE (insn) = -1;
6f086dfc
RS
1780 if (! flag_force_mem && recog_memoized (insn) >= 0)
1781 return;
1782
1783 *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
1784 return;
1785 }
1786 break;
1787
1788 case SET:
1789 /* First do special simplification of bit-field references. */
1790 if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
1791 || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
1792 optimize_bit_field (x, insn, 0);
1793 if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
1794 || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
5f4f0e22 1795 optimize_bit_field (x, insn, NULL_PTR);
6f086dfc
RS
1796
1797 /* If SET_DEST is now a paradoxical SUBREG, put the result of this
1798 insn into a pseudo and store the low part of the pseudo into VAR. */
1799 if (GET_CODE (SET_DEST (x)) == SUBREG
1800 && SUBREG_REG (SET_DEST (x)) == var
1801 && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
1802 > GET_MODE_SIZE (GET_MODE (var))))
1803 {
1804 SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
1805 emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
1806 tem)),
1807 insn);
1808 break;
1809 }
1810
1811 {
1812 rtx dest = SET_DEST (x);
1813 rtx src = SET_SRC (x);
1814 rtx outerdest = dest;
1815
1816 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1817 || GET_CODE (dest) == SIGN_EXTRACT
1818 || GET_CODE (dest) == ZERO_EXTRACT)
1819 dest = XEXP (dest, 0);
1820
1821 if (GET_CODE (src) == SUBREG)
1822 src = XEXP (src, 0);
1823
1824 /* If VAR does not appear at the top level of the SET
1825 just scan the lower levels of the tree. */
1826
1827 if (src != var && dest != var)
1828 break;
1829
1830 /* We will need to rerecognize this insn. */
1831 INSN_CODE (insn) = -1;
1832
1833#ifdef HAVE_insv
1834 if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var)
1835 {
1836 /* Since this case will return, ensure we fixup all the
1837 operands here. */
00d8a4c1
RK
1838 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
1839 insn, replacements);
1840 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
1841 insn, replacements);
1842 fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
1843 insn, replacements);
6f086dfc
RS
1844
1845 tem = XEXP (outerdest, 0);
1846
1847 /* Clean up (SUBREG:SI (MEM:mode ...) 0)
1848 that may appear inside a ZERO_EXTRACT.
1849 This was legitimate when the MEM was a REG. */
1850 if (GET_CODE (tem) == SUBREG
1851 && SUBREG_REG (tem) == var)
1852 tem = fixup_memory_subreg (tem, insn, 1);
1853 else
1854 tem = fixup_stack_1 (tem, insn);
1855
1856 if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
1857 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
1858 && ! mode_dependent_address_p (XEXP (tem, 0))
1859 && ! MEM_VOLATILE_P (tem))
1860 {
1861 enum machine_mode wanted_mode
1862 = insn_operand_mode[(int) CODE_FOR_insv][0];
1863 enum machine_mode is_mode = GET_MODE (tem);
1864 int width = INTVAL (XEXP (outerdest, 1));
1865 int pos = INTVAL (XEXP (outerdest, 2));
1866
6dc42e49 1867 /* If we have a narrower mode, we can do something. */
6f086dfc
RS
1868 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
1869 {
1870 int offset = pos / BITS_PER_UNIT;
1871 rtx old_pos = XEXP (outerdest, 2);
1872 rtx newmem;
1873
f76b9db2
ILT
1874 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
1875 offset = (GET_MODE_SIZE (is_mode)
1876 - GET_MODE_SIZE (wanted_mode) - offset);
6f086dfc
RS
1877
1878 pos %= GET_MODE_BITSIZE (wanted_mode);
1879
1880 newmem = gen_rtx (MEM, wanted_mode,
1881 plus_constant (XEXP (tem, 0), offset));
1882 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (tem);
1883 MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (tem);
1884 MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (tem);
1885
1886 /* Make the change and see if the insn remains valid. */
1887 INSN_CODE (insn) = -1;
1888 XEXP (outerdest, 0) = newmem;
5f4f0e22 1889 XEXP (outerdest, 2) = GEN_INT (pos);
6f086dfc
RS
1890
1891 if (recog_memoized (insn) >= 0)
1892 return;
1893
1894 /* Otherwise, restore old position. XEXP (x, 0) will be
1895 restored later. */
1896 XEXP (outerdest, 2) = old_pos;
1897 }
1898 }
1899
1900 /* If we get here, the bit-field store doesn't allow memory
1901 or isn't located at a constant position. Load the value into
1902 a register, do the store, and put it back into memory. */
1903
1904 tem1 = gen_reg_rtx (GET_MODE (tem));
1905 emit_insn_before (gen_move_insn (tem1, tem), insn);
1906 emit_insn_after (gen_move_insn (tem, tem1), insn);
1907 XEXP (outerdest, 0) = tem1;
1908 return;
1909 }
1910#endif
1911
1912 /* STRICT_LOW_PART is a no-op on memory references
1913 and it can cause combinations to be unrecognizable,
1914 so eliminate it. */
1915
1916 if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
1917 SET_DEST (x) = XEXP (SET_DEST (x), 0);
1918
1919 /* A valid insn to copy VAR into or out of a register
1920 must be left alone, to avoid an infinite loop here.
1921 If the reference to VAR is by a subreg, fix that up,
1922 since SUBREG is not valid for a memref.
e15762df
RK
1923 Also fix up the address of the stack slot.
1924
1925 Note that we must not try to recognize the insn until
1926 after we know that we have valid addresses and no
1927 (subreg (mem ...) ...) constructs, since these interfere
1928 with determining the validity of the insn. */
6f086dfc
RS
1929
1930 if ((SET_SRC (x) == var
1931 || (GET_CODE (SET_SRC (x)) == SUBREG
1932 && SUBREG_REG (SET_SRC (x)) == var))
1933 && (GET_CODE (SET_DEST (x)) == REG
1934 || (GET_CODE (SET_DEST (x)) == SUBREG
1935 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG))
e15762df 1936 && x == single_set (PATTERN (insn)))
6f086dfc 1937 {
e15762df
RK
1938 rtx pat;
1939
2740a678 1940 replacement = find_fixup_replacement (replacements, SET_SRC (x));
6f086dfc 1941 if (replacement->new)
6f086dfc 1942 SET_SRC (x) = replacement->new;
6f086dfc
RS
1943 else if (GET_CODE (SET_SRC (x)) == SUBREG)
1944 SET_SRC (x) = replacement->new
1945 = fixup_memory_subreg (SET_SRC (x), insn, 0);
1946 else
1947 SET_SRC (x) = replacement->new
1948 = fixup_stack_1 (SET_SRC (x), insn);
e15762df
RK
1949
1950 if (recog_memoized (insn) >= 0)
1951 return;
1952
1953 /* INSN is not valid, but we know that we want to
1954 copy SET_SRC (x) to SET_DEST (x) in some way. So
1955 we generate the move and see whether it requires more
1956 than one insn. If it does, we emit those insns and
1957 delete INSN. Otherwise, we an just replace the pattern
1958 of INSN; we have already verified above that INSN has
1959 no other function that to do X. */
1960
1961 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
1962 if (GET_CODE (pat) == SEQUENCE)
1963 {
1964 emit_insn_after (pat, insn);
1965 PUT_CODE (insn, NOTE);
1966 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1967 NOTE_SOURCE_FILE (insn) = 0;
1968 }
1969 else
1970 PATTERN (insn) = pat;
1971
6f086dfc
RS
1972 return;
1973 }
1974
1975 if ((SET_DEST (x) == var
1976 || (GET_CODE (SET_DEST (x)) == SUBREG
1977 && SUBREG_REG (SET_DEST (x)) == var))
1978 && (GET_CODE (SET_SRC (x)) == REG
1979 || (GET_CODE (SET_SRC (x)) == SUBREG
1980 && GET_CODE (SUBREG_REG (SET_SRC (x))) == REG))
e15762df 1981 && x == single_set (PATTERN (insn)))
6f086dfc 1982 {
e15762df
RK
1983 rtx pat;
1984
6f086dfc
RS
1985 if (GET_CODE (SET_DEST (x)) == SUBREG)
1986 SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn, 0);
1987 else
1988 SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
e15762df
RK
1989
1990 if (recog_memoized (insn) >= 0)
1991 return;
1992
1993 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
1994 if (GET_CODE (pat) == SEQUENCE)
1995 {
1996 emit_insn_after (pat, insn);
1997 PUT_CODE (insn, NOTE);
1998 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1999 NOTE_SOURCE_FILE (insn) = 0;
2000 }
2001 else
2002 PATTERN (insn) = pat;
2003
6f086dfc
RS
2004 return;
2005 }
2006
2007 /* Otherwise, storing into VAR must be handled specially
2008 by storing into a temporary and copying that into VAR
00d8a4c1
RK
2009 with a new insn after this one. Note that this case
2010 will be used when storing into a promoted scalar since
2011 the insn will now have different modes on the input
2012 and output and hence will be invalid (except for the case
2013 of setting it to a constant, which does not need any
2014 change if it is valid). We generate extra code in that case,
2015 but combine.c will eliminate it. */
6f086dfc
RS
2016
2017 if (dest == var)
2018 {
2019 rtx temp;
00d8a4c1
RK
2020 rtx fixeddest = SET_DEST (x);
2021
6f086dfc 2022 /* STRICT_LOW_PART can be discarded, around a MEM. */
00d8a4c1
RK
2023 if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2024 fixeddest = XEXP (fixeddest, 0);
6f086dfc 2025 /* Convert (SUBREG (MEM)) to a MEM in a changed mode. */
00d8a4c1
RK
2026 if (GET_CODE (fixeddest) == SUBREG)
2027 fixeddest = fixup_memory_subreg (fixeddest, insn, 0);
6f086dfc 2028 else
00d8a4c1
RK
2029 fixeddest = fixup_stack_1 (fixeddest, insn);
2030
2031 temp = gen_reg_rtx (GET_MODE (SET_SRC (x)) == VOIDmode
2032 ? GET_MODE (fixeddest)
2033 : GET_MODE (SET_SRC (x)));
2034
2035 emit_insn_after (gen_move_insn (fixeddest,
2036 gen_lowpart (GET_MODE (fixeddest),
2037 temp)),
2038 insn);
6f086dfc 2039
6f086dfc
RS
2040 SET_DEST (x) = temp;
2041 }
2042 }
2043 }
2044
2045 /* Nothing special about this RTX; fix its operands. */
2046
2047 fmt = GET_RTX_FORMAT (code);
2048 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2049 {
2050 if (fmt[i] == 'e')
00d8a4c1 2051 fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements);
6f086dfc
RS
2052 if (fmt[i] == 'E')
2053 {
2054 register int j;
2055 for (j = 0; j < XVECLEN (x, i); j++)
00d8a4c1
RK
2056 fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2057 insn, replacements);
6f086dfc
RS
2058 }
2059 }
2060}
2061\f
2062/* Given X, an rtx of the form (SUBREG:m1 (MEM:m2 addr)),
2063 return an rtx (MEM:m1 newaddr) which is equivalent.
2064 If any insns must be emitted to compute NEWADDR, put them before INSN.
2065
2066 UNCRITICAL nonzero means accept paradoxical subregs.
ab6155b7 2067 This is used for subregs found inside of ZERO_EXTRACTs and in REG_NOTES. */
6f086dfc
RS
2068
2069static rtx
2070fixup_memory_subreg (x, insn, uncritical)
2071 rtx x;
2072 rtx insn;
2073 int uncritical;
2074{
2075 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2076 rtx addr = XEXP (SUBREG_REG (x), 0);
2077 enum machine_mode mode = GET_MODE (x);
2078 rtx saved, result;
2079
2080 /* Paradoxical SUBREGs are usually invalid during RTL generation. */
2081 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
2082 && ! uncritical)
2083 abort ();
2084
f76b9db2
ILT
2085 if (BYTES_BIG_ENDIAN)
2086 offset += (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2087 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
6f086dfc
RS
2088 addr = plus_constant (addr, offset);
2089 if (!flag_force_addr && memory_address_p (mode, addr))
2090 /* Shortcut if no insns need be emitted. */
2091 return change_address (SUBREG_REG (x), mode, addr);
2092 start_sequence ();
2093 result = change_address (SUBREG_REG (x), mode, addr);
2094 emit_insn_before (gen_sequence (), insn);
2095 end_sequence ();
2096 return result;
2097}
2098
2099/* Do fixup_memory_subreg on all (SUBREG (MEM ...) ...) contained in X.
2100 Replace subexpressions of X in place.
2101 If X itself is a (SUBREG (MEM ...) ...), return the replacement expression.
2102 Otherwise return X, with its contents possibly altered.
2103
ab6155b7
RK
2104 If any insns must be emitted to compute NEWADDR, put them before INSN.
2105
2106 UNCRITICAL is as in fixup_memory_subreg. */
6f086dfc
RS
2107
2108static rtx
ab6155b7 2109walk_fixup_memory_subreg (x, insn, uncritical)
6f086dfc
RS
2110 register rtx x;
2111 rtx insn;
ab6155b7 2112 int uncritical;
6f086dfc
RS
2113{
2114 register enum rtx_code code;
2115 register char *fmt;
2116 register int i;
2117
2118 if (x == 0)
2119 return 0;
2120
2121 code = GET_CODE (x);
2122
2123 if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
ab6155b7 2124 return fixup_memory_subreg (x, insn, uncritical);
6f086dfc
RS
2125
2126 /* Nothing special about this RTX; fix its operands. */
2127
2128 fmt = GET_RTX_FORMAT (code);
2129 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2130 {
2131 if (fmt[i] == 'e')
ab6155b7 2132 XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn, uncritical);
6f086dfc
RS
2133 if (fmt[i] == 'E')
2134 {
2135 register int j;
2136 for (j = 0; j < XVECLEN (x, i); j++)
2137 XVECEXP (x, i, j)
ab6155b7 2138 = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn, uncritical);
6f086dfc
RS
2139 }
2140 }
2141 return x;
2142}
2143\f
6f086dfc
RS
2144/* For each memory ref within X, if it refers to a stack slot
2145 with an out of range displacement, put the address in a temp register
2146 (emitting new insns before INSN to load these registers)
2147 and alter the memory ref to use that register.
2148 Replace each such MEM rtx with a copy, to avoid clobberage. */
2149
2150static rtx
2151fixup_stack_1 (x, insn)
2152 rtx x;
2153 rtx insn;
2154{
2155 register int i;
2156 register RTX_CODE code = GET_CODE (x);
2157 register char *fmt;
2158
2159 if (code == MEM)
2160 {
2161 register rtx ad = XEXP (x, 0);
2162 /* If we have address of a stack slot but it's not valid
2163 (displacement is too large), compute the sum in a register. */
2164 if (GET_CODE (ad) == PLUS
2165 && GET_CODE (XEXP (ad, 0)) == REG
40d05551
RK
2166 && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2167 && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
2168 || XEXP (ad, 0) == current_function_internal_arg_pointer)
6f086dfc
RS
2169 && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2170 {
2171 rtx temp, seq;
2172 if (memory_address_p (GET_MODE (x), ad))
2173 return x;
2174
2175 start_sequence ();
2176 temp = copy_to_reg (ad);
2177 seq = gen_sequence ();
2178 end_sequence ();
2179 emit_insn_before (seq, insn);
2180 return change_address (x, VOIDmode, temp);
2181 }
2182 return x;
2183 }
2184
2185 fmt = GET_RTX_FORMAT (code);
2186 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2187 {
2188 if (fmt[i] == 'e')
2189 XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
2190 if (fmt[i] == 'E')
2191 {
2192 register int j;
2193 for (j = 0; j < XVECLEN (x, i); j++)
2194 XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2195 }
2196 }
2197 return x;
2198}
2199\f
2200/* Optimization: a bit-field instruction whose field
2201 happens to be a byte or halfword in memory
2202 can be changed to a move instruction.
2203
2204 We call here when INSN is an insn to examine or store into a bit-field.
2205 BODY is the SET-rtx to be altered.
2206
2207 EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2208 (Currently this is called only from function.c, and EQUIV_MEM
2209 is always 0.) */
2210
2211static void
2212optimize_bit_field (body, insn, equiv_mem)
2213 rtx body;
2214 rtx insn;
2215 rtx *equiv_mem;
2216{
2217 register rtx bitfield;
2218 int destflag;
2219 rtx seq = 0;
2220 enum machine_mode mode;
2221
2222 if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2223 || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2224 bitfield = SET_DEST (body), destflag = 1;
2225 else
2226 bitfield = SET_SRC (body), destflag = 0;
2227
2228 /* First check that the field being stored has constant size and position
2229 and is in fact a byte or halfword suitably aligned. */
2230
2231 if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2232 && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2233 && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2234 != BLKmode)
2235 && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2236 {
2237 register rtx memref = 0;
2238
2239 /* Now check that the containing word is memory, not a register,
2240 and that it is safe to change the machine mode. */
2241
2242 if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2243 memref = XEXP (bitfield, 0);
2244 else if (GET_CODE (XEXP (bitfield, 0)) == REG
2245 && equiv_mem != 0)
2246 memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2247 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2248 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2249 memref = SUBREG_REG (XEXP (bitfield, 0));
2250 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2251 && equiv_mem != 0
2252 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == REG)
2253 memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2254
2255 if (memref
2256 && ! mode_dependent_address_p (XEXP (memref, 0))
2257 && ! MEM_VOLATILE_P (memref))
2258 {
2259 /* Now adjust the address, first for any subreg'ing
2260 that we are now getting rid of,
2261 and then for which byte of the word is wanted. */
2262
2263 register int offset = INTVAL (XEXP (bitfield, 2));
b88a3142
RK
2264 rtx insns;
2265
6f086dfc 2266 /* Adjust OFFSET to count bits from low-address byte. */
f76b9db2
ILT
2267 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2268 offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2269 - offset - INTVAL (XEXP (bitfield, 1)));
2270
6f086dfc
RS
2271 /* Adjust OFFSET to count bytes from low-address byte. */
2272 offset /= BITS_PER_UNIT;
2273 if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2274 {
2275 offset += SUBREG_WORD (XEXP (bitfield, 0)) * UNITS_PER_WORD;
f76b9db2
ILT
2276 if (BYTES_BIG_ENDIAN)
2277 offset -= (MIN (UNITS_PER_WORD,
2278 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2279 - MIN (UNITS_PER_WORD,
2280 GET_MODE_SIZE (GET_MODE (memref))));
6f086dfc
RS
2281 }
2282
b88a3142
RK
2283 start_sequence ();
2284 memref = change_address (memref, mode,
6f086dfc 2285 plus_constant (XEXP (memref, 0), offset));
b88a3142
RK
2286 insns = get_insns ();
2287 end_sequence ();
2288 emit_insns_before (insns, insn);
6f086dfc
RS
2289
2290 /* Store this memory reference where
2291 we found the bit field reference. */
2292
2293 if (destflag)
2294 {
2295 validate_change (insn, &SET_DEST (body), memref, 1);
2296 if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2297 {
2298 rtx src = SET_SRC (body);
2299 while (GET_CODE (src) == SUBREG
2300 && SUBREG_WORD (src) == 0)
2301 src = SUBREG_REG (src);
2302 if (GET_MODE (src) != GET_MODE (memref))
2303 src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2304 validate_change (insn, &SET_SRC (body), src, 1);
2305 }
2306 else if (GET_MODE (SET_SRC (body)) != VOIDmode
2307 && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2308 /* This shouldn't happen because anything that didn't have
2309 one of these modes should have got converted explicitly
2310 and then referenced through a subreg.
2311 This is so because the original bit-field was
2312 handled by agg_mode and so its tree structure had
2313 the same mode that memref now has. */
2314 abort ();
2315 }
2316 else
2317 {
2318 rtx dest = SET_DEST (body);
2319
2320 while (GET_CODE (dest) == SUBREG
4013a709
RK
2321 && SUBREG_WORD (dest) == 0
2322 && (GET_MODE_CLASS (GET_MODE (dest))
2323 == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest)))))
6f086dfc
RS
2324 dest = SUBREG_REG (dest);
2325
2326 validate_change (insn, &SET_DEST (body), dest, 1);
2327
2328 if (GET_MODE (dest) == GET_MODE (memref))
2329 validate_change (insn, &SET_SRC (body), memref, 1);
2330 else
2331 {
2332 /* Convert the mem ref to the destination mode. */
2333 rtx newreg = gen_reg_rtx (GET_MODE (dest));
2334
2335 start_sequence ();
2336 convert_move (newreg, memref,
2337 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2338 seq = get_insns ();
2339 end_sequence ();
2340
2341 validate_change (insn, &SET_SRC (body), newreg, 1);
2342 }
2343 }
2344
2345 /* See if we can convert this extraction or insertion into
2346 a simple move insn. We might not be able to do so if this
2347 was, for example, part of a PARALLEL.
2348
2349 If we succeed, write out any needed conversions. If we fail,
2350 it is hard to guess why we failed, so don't do anything
2351 special; just let the optimization be suppressed. */
2352
2353 if (apply_change_group () && seq)
2354 emit_insns_before (seq, insn);
2355 }
2356 }
2357}
2358\f
2359/* These routines are responsible for converting virtual register references
2360 to the actual hard register references once RTL generation is complete.
2361
2362 The following four variables are used for communication between the
2363 routines. They contain the offsets of the virtual registers from their
2364 respective hard registers. */
2365
2366static int in_arg_offset;
2367static int var_offset;
2368static int dynamic_offset;
2369static int out_arg_offset;
2370
2371/* In most machines, the stack pointer register is equivalent to the bottom
2372 of the stack. */
2373
2374#ifndef STACK_POINTER_OFFSET
2375#define STACK_POINTER_OFFSET 0
2376#endif
2377
2378/* If not defined, pick an appropriate default for the offset of dynamically
2379 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2380 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
2381
2382#ifndef STACK_DYNAMIC_OFFSET
2383
2384#ifdef ACCUMULATE_OUTGOING_ARGS
2385/* The bottom of the stack points to the actual arguments. If
2386 REG_PARM_STACK_SPACE is defined, this includes the space for the register
2387 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
2388 stack space for register parameters is not pushed by the caller, but
2389 rather part of the fixed stack areas and hence not included in
2390 `current_function_outgoing_args_size'. Nevertheless, we must allow
2391 for it when allocating stack dynamic objects. */
2392
2393#if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2394#define STACK_DYNAMIC_OFFSET(FNDECL) \
2395(current_function_outgoing_args_size \
2396 + REG_PARM_STACK_SPACE (FNDECL) + (STACK_POINTER_OFFSET))
2397
2398#else
2399#define STACK_DYNAMIC_OFFSET(FNDECL) \
2400(current_function_outgoing_args_size + (STACK_POINTER_OFFSET))
2401#endif
2402
2403#else
2404#define STACK_DYNAMIC_OFFSET(FNDECL) STACK_POINTER_OFFSET
2405#endif
2406#endif
2407
2408/* Pass through the INSNS of function FNDECL and convert virtual register
2409 references to hard register references. */
2410
2411void
2412instantiate_virtual_regs (fndecl, insns)
2413 tree fndecl;
2414 rtx insns;
2415{
2416 rtx insn;
2417
2418 /* Compute the offsets to use for this function. */
2419 in_arg_offset = FIRST_PARM_OFFSET (fndecl);
2420 var_offset = STARTING_FRAME_OFFSET;
2421 dynamic_offset = STACK_DYNAMIC_OFFSET (fndecl);
2422 out_arg_offset = STACK_POINTER_OFFSET;
2423
2424 /* Scan all variables and parameters of this function. For each that is
2425 in memory, instantiate all virtual registers if the result is a valid
2426 address. If not, we do it later. That will handle most uses of virtual
2427 regs on many machines. */
2428 instantiate_decls (fndecl, 1);
2429
2430 /* Initialize recognition, indicating that volatile is OK. */
2431 init_recog ();
2432
2433 /* Scan through all the insns, instantiating every virtual register still
2434 present. */
2435 for (insn = insns; insn; insn = NEXT_INSN (insn))
2436 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
2437 || GET_CODE (insn) == CALL_INSN)
2438 {
2439 instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
5f4f0e22 2440 instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
6f086dfc
RS
2441 }
2442
2443 /* Now instantiate the remaining register equivalences for debugging info.
2444 These will not be valid addresses. */
2445 instantiate_decls (fndecl, 0);
2446
2447 /* Indicate that, from now on, assign_stack_local should use
2448 frame_pointer_rtx. */
2449 virtuals_instantiated = 1;
2450}
2451
2452/* Scan all decls in FNDECL (both variables and parameters) and instantiate
2453 all virtual registers in their DECL_RTL's.
2454
2455 If VALID_ONLY, do this only if the resulting address is still valid.
2456 Otherwise, always do it. */
2457
2458static void
2459instantiate_decls (fndecl, valid_only)
2460 tree fndecl;
2461 int valid_only;
2462{
2463 tree decl;
2464
a82ad570 2465 if (DECL_INLINE (fndecl))
6f086dfc
RS
2466 /* When compiling an inline function, the obstack used for
2467 rtl allocation is the maybepermanent_obstack. Calling
2468 `resume_temporary_allocation' switches us back to that
2469 obstack while we process this function's parameters. */
2470 resume_temporary_allocation ();
2471
2472 /* Process all parameters of the function. */
2473 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
2474 {
5a73491b
RK
2475 instantiate_decl (DECL_RTL (decl), int_size_in_bytes (TREE_TYPE (decl)),
2476 valid_only);
2477 instantiate_decl (DECL_INCOMING_RTL (decl),
2478 int_size_in_bytes (TREE_TYPE (decl)), valid_only);
6f086dfc
RS
2479 }
2480
2481 /* Now process all variables defined in the function or its subblocks. */
2482 instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
2483
a82ad570 2484 if (DECL_INLINE (fndecl))
6f086dfc
RS
2485 {
2486 /* Save all rtl allocated for this function by raising the
2487 high-water mark on the maybepermanent_obstack. */
2488 preserve_data ();
2489 /* All further rtl allocation is now done in the current_obstack. */
2490 rtl_in_current_obstack ();
2491 }
2492}
2493
2494/* Subroutine of instantiate_decls: Process all decls in the given
2495 BLOCK node and all its subblocks. */
2496
2497static void
2498instantiate_decls_1 (let, valid_only)
2499 tree let;
2500 int valid_only;
2501{
2502 tree t;
2503
2504 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
5a73491b
RK
2505 instantiate_decl (DECL_RTL (t), int_size_in_bytes (TREE_TYPE (t)),
2506 valid_only);
6f086dfc
RS
2507
2508 /* Process all subblocks. */
2509 for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
2510 instantiate_decls_1 (t, valid_only);
2511}
5a73491b 2512
8008b228 2513/* Subroutine of the preceding procedures: Given RTL representing a
5a73491b
RK
2514 decl and the size of the object, do any instantiation required.
2515
2516 If VALID_ONLY is non-zero, it means that the RTL should only be
2517 changed if the new address is valid. */
2518
2519static void
2520instantiate_decl (x, size, valid_only)
2521 rtx x;
2522 int size;
2523 int valid_only;
2524{
2525 enum machine_mode mode;
2526 rtx addr;
2527
2528 /* If this is not a MEM, no need to do anything. Similarly if the
2529 address is a constant or a register that is not a virtual register. */
2530
2531 if (x == 0 || GET_CODE (x) != MEM)
2532 return;
2533
2534 addr = XEXP (x, 0);
2535 if (CONSTANT_P (addr)
2536 || (GET_CODE (addr) == REG
2537 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
2538 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
2539 return;
2540
2541 /* If we should only do this if the address is valid, copy the address.
2542 We need to do this so we can undo any changes that might make the
2543 address invalid. This copy is unfortunate, but probably can't be
2544 avoided. */
2545
2546 if (valid_only)
2547 addr = copy_rtx (addr);
2548
2549 instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
2550
2551 if (! valid_only)
2552 return;
2553
2554 /* Now verify that the resulting address is valid for every integer or
2555 floating-point mode up to and including SIZE bytes long. We do this
2556 since the object might be accessed in any mode and frame addresses
2557 are shared. */
2558
2559 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2560 mode != VOIDmode && GET_MODE_SIZE (mode) <= size;
2561 mode = GET_MODE_WIDER_MODE (mode))
2562 if (! memory_address_p (mode, addr))
2563 return;
2564
2565 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
2566 mode != VOIDmode && GET_MODE_SIZE (mode) <= size;
2567 mode = GET_MODE_WIDER_MODE (mode))
2568 if (! memory_address_p (mode, addr))
2569 return;
2570
2571 /* Otherwise, put back the address, now that we have updated it and we
2572 know it is valid. */
2573
2574 XEXP (x, 0) = addr;
2575}
6f086dfc
RS
2576\f
2577/* Given a pointer to a piece of rtx and an optional pointer to the
2578 containing object, instantiate any virtual registers present in it.
2579
2580 If EXTRA_INSNS, we always do the replacement and generate
2581 any extra insns before OBJECT. If it zero, we do nothing if replacement
2582 is not valid.
2583
2584 Return 1 if we either had nothing to do or if we were able to do the
2585 needed replacement. Return 0 otherwise; we only return zero if
2586 EXTRA_INSNS is zero.
2587
2588 We first try some simple transformations to avoid the creation of extra
2589 pseudos. */
2590
2591static int
2592instantiate_virtual_regs_1 (loc, object, extra_insns)
2593 rtx *loc;
2594 rtx object;
2595 int extra_insns;
2596{
2597 rtx x;
2598 RTX_CODE code;
2599 rtx new = 0;
2600 int offset;
2601 rtx temp;
2602 rtx seq;
2603 int i, j;
2604 char *fmt;
2605
2606 /* Re-start here to avoid recursion in common cases. */
2607 restart:
2608
2609 x = *loc;
2610 if (x == 0)
2611 return 1;
2612
2613 code = GET_CODE (x);
2614
2615 /* Check for some special cases. */
2616 switch (code)
2617 {
2618 case CONST_INT:
2619 case CONST_DOUBLE:
2620 case CONST:
2621 case SYMBOL_REF:
2622 case CODE_LABEL:
2623 case PC:
2624 case CC0:
2625 case ASM_INPUT:
2626 case ADDR_VEC:
2627 case ADDR_DIFF_VEC:
2628 case RETURN:
2629 return 1;
2630
2631 case SET:
2632 /* We are allowed to set the virtual registers. This means that
2633 that the actual register should receive the source minus the
2634 appropriate offset. This is used, for example, in the handling
2635 of non-local gotos. */
2636 if (SET_DEST (x) == virtual_incoming_args_rtx)
2637 new = arg_pointer_rtx, offset = - in_arg_offset;
2638 else if (SET_DEST (x) == virtual_stack_vars_rtx)
dfd3dae6 2639 new = frame_pointer_rtx, offset = - var_offset;
6f086dfc
RS
2640 else if (SET_DEST (x) == virtual_stack_dynamic_rtx)
2641 new = stack_pointer_rtx, offset = - dynamic_offset;
2642 else if (SET_DEST (x) == virtual_outgoing_args_rtx)
2643 new = stack_pointer_rtx, offset = - out_arg_offset;
2644
2645 if (new)
2646 {
2647 /* The only valid sources here are PLUS or REG. Just do
2648 the simplest possible thing to handle them. */
2649 if (GET_CODE (SET_SRC (x)) != REG
2650 && GET_CODE (SET_SRC (x)) != PLUS)
2651 abort ();
2652
2653 start_sequence ();
2654 if (GET_CODE (SET_SRC (x)) != REG)
5f4f0e22 2655 temp = force_operand (SET_SRC (x), NULL_RTX);
6f086dfc
RS
2656 else
2657 temp = SET_SRC (x);
5f4f0e22 2658 temp = force_operand (plus_constant (temp, offset), NULL_RTX);
6f086dfc
RS
2659 seq = get_insns ();
2660 end_sequence ();
2661
2662 emit_insns_before (seq, object);
2663 SET_DEST (x) = new;
2664
2665 if (!validate_change (object, &SET_SRC (x), temp, 0)
2666 || ! extra_insns)
2667 abort ();
2668
2669 return 1;
2670 }
2671
2672 instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
2673 loc = &SET_SRC (x);
2674 goto restart;
2675
2676 case PLUS:
2677 /* Handle special case of virtual register plus constant. */
2678 if (CONSTANT_P (XEXP (x, 1)))
2679 {
b1f82ccf 2680 rtx old, new_offset;
6f086dfc
RS
2681
2682 /* Check for (plus (plus VIRT foo) (const_int)) first. */
2683 if (GET_CODE (XEXP (x, 0)) == PLUS)
2684 {
2685 rtx inner = XEXP (XEXP (x, 0), 0);
2686
2687 if (inner == virtual_incoming_args_rtx)
2688 new = arg_pointer_rtx, offset = in_arg_offset;
2689 else if (inner == virtual_stack_vars_rtx)
2690 new = frame_pointer_rtx, offset = var_offset;
2691 else if (inner == virtual_stack_dynamic_rtx)
2692 new = stack_pointer_rtx, offset = dynamic_offset;
2693 else if (inner == virtual_outgoing_args_rtx)
2694 new = stack_pointer_rtx, offset = out_arg_offset;
2695 else
2696 {
2697 loc = &XEXP (x, 0);
2698 goto restart;
2699 }
2700
2701 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
2702 extra_insns);
2703 new = gen_rtx (PLUS, Pmode, new, XEXP (XEXP (x, 0), 1));
2704 }
2705
2706 else if (XEXP (x, 0) == virtual_incoming_args_rtx)
2707 new = arg_pointer_rtx, offset = in_arg_offset;
2708 else if (XEXP (x, 0) == virtual_stack_vars_rtx)
2709 new = frame_pointer_rtx, offset = var_offset;
2710 else if (XEXP (x, 0) == virtual_stack_dynamic_rtx)
2711 new = stack_pointer_rtx, offset = dynamic_offset;
2712 else if (XEXP (x, 0) == virtual_outgoing_args_rtx)
2713 new = stack_pointer_rtx, offset = out_arg_offset;
2714 else
2715 {
2716 /* We know the second operand is a constant. Unless the
2717 first operand is a REG (which has been already checked),
2718 it needs to be checked. */
2719 if (GET_CODE (XEXP (x, 0)) != REG)
2720 {
2721 loc = &XEXP (x, 0);
2722 goto restart;
2723 }
2724 return 1;
2725 }
2726
b1f82ccf 2727 new_offset = plus_constant (XEXP (x, 1), offset);
6f086dfc 2728
b1f82ccf
DE
2729 /* If the new constant is zero, try to replace the sum with just
2730 the register. */
2731 if (new_offset == const0_rtx
2732 && validate_change (object, loc, new, 0))
6f086dfc
RS
2733 return 1;
2734
b1f82ccf
DE
2735 /* Next try to replace the register and new offset.
2736 There are two changes to validate here and we can't assume that
2737 in the case of old offset equals new just changing the register
2738 will yield a valid insn. In the interests of a little efficiency,
2739 however, we only call validate change once (we don't queue up the
2740 changes and then call apply_change_group). */
2741
2742 old = XEXP (x, 0);
2743 if (offset == 0
2744 ? ! validate_change (object, &XEXP (x, 0), new, 0)
2745 : (XEXP (x, 0) = new,
2746 ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
6f086dfc
RS
2747 {
2748 if (! extra_insns)
2749 {
2750 XEXP (x, 0) = old;
2751 return 0;
2752 }
2753
2754 /* Otherwise copy the new constant into a register and replace
2755 constant with that register. */
2756 temp = gen_reg_rtx (Pmode);
b1f82ccf 2757 XEXP (x, 0) = new;
6f086dfc 2758 if (validate_change (object, &XEXP (x, 1), temp, 0))
b1f82ccf 2759 emit_insn_before (gen_move_insn (temp, new_offset), object);
6f086dfc
RS
2760 else
2761 {
2762 /* If that didn't work, replace this expression with a
2763 register containing the sum. */
2764
6f086dfc 2765 XEXP (x, 0) = old;
b1f82ccf 2766 new = gen_rtx (PLUS, Pmode, new, new_offset);
6f086dfc
RS
2767
2768 start_sequence ();
5f4f0e22 2769 temp = force_operand (new, NULL_RTX);
6f086dfc
RS
2770 seq = get_insns ();
2771 end_sequence ();
2772
2773 emit_insns_before (seq, object);
2774 if (! validate_change (object, loc, temp, 0)
2775 && ! validate_replace_rtx (x, temp, object))
2776 abort ();
2777 }
2778 }
2779
2780 return 1;
2781 }
2782
2783 /* Fall through to generic two-operand expression case. */
2784 case EXPR_LIST:
2785 case CALL:
2786 case COMPARE:
2787 case MINUS:
2788 case MULT:
2789 case DIV: case UDIV:
2790 case MOD: case UMOD:
2791 case AND: case IOR: case XOR:
45620ed4
RK
2792 case ROTATERT: case ROTATE:
2793 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
6f086dfc
RS
2794 case NE: case EQ:
2795 case GE: case GT: case GEU: case GTU:
2796 case LE: case LT: case LEU: case LTU:
2797 if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
2798 instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
2799 loc = &XEXP (x, 0);
2800 goto restart;
2801
2802 case MEM:
2803 /* Most cases of MEM that convert to valid addresses have already been
2804 handled by our scan of regno_reg_rtx. The only special handling we
2805 need here is to make a copy of the rtx to ensure it isn't being
b335c2cc 2806 shared if we have to change it to a pseudo.
6f086dfc
RS
2807
2808 If the rtx is a simple reference to an address via a virtual register,
2809 it can potentially be shared. In such cases, first try to make it
2810 a valid address, which can also be shared. Otherwise, copy it and
2811 proceed normally.
2812
2813 First check for common cases that need no processing. These are
2814 usually due to instantiation already being done on a previous instance
2815 of a shared rtx. */
2816
2817 temp = XEXP (x, 0);
2818 if (CONSTANT_ADDRESS_P (temp)
2819#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2820 || temp == arg_pointer_rtx
b37f453b
DE
2821#endif
2822#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2823 || temp == hard_frame_pointer_rtx
6f086dfc
RS
2824#endif
2825 || temp == frame_pointer_rtx)
2826 return 1;
2827
2828 if (GET_CODE (temp) == PLUS
2829 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
2830 && (XEXP (temp, 0) == frame_pointer_rtx
b37f453b
DE
2831#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2832 || XEXP (temp, 0) == hard_frame_pointer_rtx
2833#endif
6f086dfc
RS
2834#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2835 || XEXP (temp, 0) == arg_pointer_rtx
2836#endif
2837 ))
2838 return 1;
2839
2840 if (temp == virtual_stack_vars_rtx
2841 || temp == virtual_incoming_args_rtx
2842 || (GET_CODE (temp) == PLUS
2843 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
2844 && (XEXP (temp, 0) == virtual_stack_vars_rtx
2845 || XEXP (temp, 0) == virtual_incoming_args_rtx)))
2846 {
2847 /* This MEM may be shared. If the substitution can be done without
2848 the need to generate new pseudos, we want to do it in place
2849 so all copies of the shared rtx benefit. The call below will
2850 only make substitutions if the resulting address is still
2851 valid.
2852
2853 Note that we cannot pass X as the object in the recursive call
2854 since the insn being processed may not allow all valid
6461be14
RS
2855 addresses. However, if we were not passed on object, we can
2856 only modify X without copying it if X will have a valid
2857 address.
6f086dfc 2858
6461be14
RS
2859 ??? Also note that this can still lose if OBJECT is an insn that
2860 has less restrictions on an address that some other insn.
2861 In that case, we will modify the shared address. This case
2862 doesn't seem very likely, though. */
2863
2864 if (instantiate_virtual_regs_1 (&XEXP (x, 0),
2865 object ? object : x, 0))
6f086dfc
RS
2866 return 1;
2867
2868 /* Otherwise make a copy and process that copy. We copy the entire
2869 RTL expression since it might be a PLUS which could also be
2870 shared. */
2871 *loc = x = copy_rtx (x);
2872 }
2873
2874 /* Fall through to generic unary operation case. */
2875 case USE:
2876 case CLOBBER:
2877 case SUBREG:
2878 case STRICT_LOW_PART:
2879 case NEG: case NOT:
2880 case PRE_DEC: case PRE_INC: case POST_DEC: case POST_INC:
2881 case SIGN_EXTEND: case ZERO_EXTEND:
2882 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2883 case FLOAT: case FIX:
2884 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2885 case ABS:
2886 case SQRT:
2887 case FFS:
2888 /* These case either have just one operand or we know that we need not
2889 check the rest of the operands. */
2890 loc = &XEXP (x, 0);
2891 goto restart;
2892
2893 case REG:
2894 /* Try to replace with a PLUS. If that doesn't work, compute the sum
2895 in front of this insn and substitute the temporary. */
2896 if (x == virtual_incoming_args_rtx)
2897 new = arg_pointer_rtx, offset = in_arg_offset;
2898 else if (x == virtual_stack_vars_rtx)
2899 new = frame_pointer_rtx, offset = var_offset;
2900 else if (x == virtual_stack_dynamic_rtx)
2901 new = stack_pointer_rtx, offset = dynamic_offset;
2902 else if (x == virtual_outgoing_args_rtx)
2903 new = stack_pointer_rtx, offset = out_arg_offset;
2904
2905 if (new)
2906 {
2907 temp = plus_constant (new, offset);
2908 if (!validate_change (object, loc, temp, 0))
2909 {
2910 if (! extra_insns)
2911 return 0;
2912
2913 start_sequence ();
5f4f0e22 2914 temp = force_operand (temp, NULL_RTX);
6f086dfc
RS
2915 seq = get_insns ();
2916 end_sequence ();
2917
2918 emit_insns_before (seq, object);
2919 if (! validate_change (object, loc, temp, 0)
2920 && ! validate_replace_rtx (x, temp, object))
2921 abort ();
2922 }
2923 }
2924
2925 return 1;
2926 }
2927
2928 /* Scan all subexpressions. */
2929 fmt = GET_RTX_FORMAT (code);
2930 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2931 if (*fmt == 'e')
2932 {
2933 if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
2934 return 0;
2935 }
2936 else if (*fmt == 'E')
2937 for (j = 0; j < XVECLEN (x, i); j++)
2938 if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
2939 extra_insns))
2940 return 0;
2941
2942 return 1;
2943}
2944\f
2945/* Optimization: assuming this function does not receive nonlocal gotos,
2946 delete the handlers for such, as well as the insns to establish
2947 and disestablish them. */
2948
2949static void
2950delete_handlers ()
2951{
2952 rtx insn;
2953 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2954 {
2955 /* Delete the handler by turning off the flag that would
2956 prevent jump_optimize from deleting it.
2957 Also permit deletion of the nonlocal labels themselves
2958 if nothing local refers to them. */
2959 if (GET_CODE (insn) == CODE_LABEL)
71cd4a8d
JW
2960 {
2961 tree t, last_t;
2962
2963 LABEL_PRESERVE_P (insn) = 0;
2964
2965 /* Remove it from the nonlocal_label list, to avoid confusing
2966 flow. */
2967 for (t = nonlocal_labels, last_t = 0; t;
2968 last_t = t, t = TREE_CHAIN (t))
2969 if (DECL_RTL (TREE_VALUE (t)) == insn)
2970 break;
2971 if (t)
2972 {
2973 if (! last_t)
2974 nonlocal_labels = TREE_CHAIN (nonlocal_labels);
2975 else
2976 TREE_CHAIN (last_t) = TREE_CHAIN (t);
2977 }
2978 }
6f086dfc 2979 if (GET_CODE (insn) == INSN
59257ff7
RK
2980 && ((nonlocal_goto_handler_slot != 0
2981 && reg_mentioned_p (nonlocal_goto_handler_slot, PATTERN (insn)))
2982 || (nonlocal_goto_stack_level != 0
2983 && reg_mentioned_p (nonlocal_goto_stack_level,
2984 PATTERN (insn)))))
6f086dfc
RS
2985 delete_insn (insn);
2986 }
2987}
2988
2989/* Return a list (chain of EXPR_LIST nodes) for the nonlocal labels
2990 of the current function. */
2991
2992rtx
2993nonlocal_label_rtx_list ()
2994{
2995 tree t;
2996 rtx x = 0;
2997
2998 for (t = nonlocal_labels; t; t = TREE_CHAIN (t))
2999 x = gen_rtx (EXPR_LIST, VOIDmode, label_rtx (TREE_VALUE (t)), x);
3000
3001 return x;
3002}
3003\f
3004/* Output a USE for any register use in RTL.
3005 This is used with -noreg to mark the extent of lifespan
3006 of any registers used in a user-visible variable's DECL_RTL. */
3007
3008void
3009use_variable (rtl)
3010 rtx rtl;
3011{
3012 if (GET_CODE (rtl) == REG)
3013 /* This is a register variable. */
3014 emit_insn (gen_rtx (USE, VOIDmode, rtl));
3015 else if (GET_CODE (rtl) == MEM
3016 && GET_CODE (XEXP (rtl, 0)) == REG
3017 && (REGNO (XEXP (rtl, 0)) < FIRST_VIRTUAL_REGISTER
3018 || REGNO (XEXP (rtl, 0)) > LAST_VIRTUAL_REGISTER)
3019 && XEXP (rtl, 0) != current_function_internal_arg_pointer)
3020 /* This is a variable-sized structure. */
3021 emit_insn (gen_rtx (USE, VOIDmode, XEXP (rtl, 0)));
3022}
3023
3024/* Like use_variable except that it outputs the USEs after INSN
3025 instead of at the end of the insn-chain. */
3026
3027void
3028use_variable_after (rtl, insn)
3029 rtx rtl, insn;
3030{
3031 if (GET_CODE (rtl) == REG)
3032 /* This is a register variable. */
3033 emit_insn_after (gen_rtx (USE, VOIDmode, rtl), insn);
3034 else if (GET_CODE (rtl) == MEM
3035 && GET_CODE (XEXP (rtl, 0)) == REG
3036 && (REGNO (XEXP (rtl, 0)) < FIRST_VIRTUAL_REGISTER
3037 || REGNO (XEXP (rtl, 0)) > LAST_VIRTUAL_REGISTER)
3038 && XEXP (rtl, 0) != current_function_internal_arg_pointer)
3039 /* This is a variable-sized structure. */
3040 emit_insn_after (gen_rtx (USE, VOIDmode, XEXP (rtl, 0)), insn);
3041}
3042\f
3043int
3044max_parm_reg_num ()
3045{
3046 return max_parm_reg;
3047}
3048
3049/* Return the first insn following those generated by `assign_parms'. */
3050
3051rtx
3052get_first_nonparm_insn ()
3053{
3054 if (last_parm_insn)
3055 return NEXT_INSN (last_parm_insn);
3056 return get_insns ();
3057}
3058
5378192b
RS
3059/* Return the first NOTE_INSN_BLOCK_BEG note in the function.
3060 Crash if there is none. */
3061
3062rtx
3063get_first_block_beg ()
3064{
3065 register rtx searcher;
3066 register rtx insn = get_first_nonparm_insn ();
3067
3068 for (searcher = insn; searcher; searcher = NEXT_INSN (searcher))
3069 if (GET_CODE (searcher) == NOTE
3070 && NOTE_LINE_NUMBER (searcher) == NOTE_INSN_BLOCK_BEG)
3071 return searcher;
3072
3073 abort (); /* Invalid call to this function. (See comments above.) */
3074 return NULL_RTX;
3075}
3076
d181c154
RS
3077/* Return 1 if EXP is an aggregate type (or a value with aggregate type).
3078 This means a type for which function calls must pass an address to the
3079 function or get an address back from the function.
3080 EXP may be a type node or an expression (whose type is tested). */
6f086dfc
RS
3081
3082int
3083aggregate_value_p (exp)
3084 tree exp;
3085{
9d790a4f
RS
3086 int i, regno, nregs;
3087 rtx reg;
d181c154
RS
3088 tree type;
3089 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 't')
3090 type = exp;
3091 else
3092 type = TREE_TYPE (exp);
3093
3094 if (RETURN_IN_MEMORY (type))
6f086dfc 3095 return 1;
05e3bdb9 3096 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
6f086dfc 3097 return 1;
9d790a4f
RS
3098 /* Make sure we have suitable call-clobbered regs to return
3099 the value in; if not, we must return it in memory. */
d181c154 3100 reg = hard_function_value (type, 0);
9d790a4f 3101 regno = REGNO (reg);
d181c154 3102 nregs = HARD_REGNO_NREGS (regno, TYPE_MODE (type));
9d790a4f
RS
3103 for (i = 0; i < nregs; i++)
3104 if (! call_used_regs[regno + i])
3105 return 1;
6f086dfc
RS
3106 return 0;
3107}
3108\f
3109/* Assign RTL expressions to the function's parameters.
3110 This may involve copying them into registers and using
3111 those registers as the RTL for them.
3112
3113 If SECOND_TIME is non-zero it means that this function is being
3114 called a second time. This is done by integrate.c when a function's
3115 compilation is deferred. We need to come back here in case the
3116 FUNCTION_ARG macro computes items needed for the rest of the compilation
3117 (such as changing which registers are fixed or caller-saved). But suppress
3118 writing any insns or setting DECL_RTL of anything in this case. */
3119
3120void
3121assign_parms (fndecl, second_time)
3122 tree fndecl;
3123 int second_time;
3124{
3125 register tree parm;
3126 register rtx entry_parm = 0;
3127 register rtx stack_parm = 0;
3128 CUMULATIVE_ARGS args_so_far;
a53e14c0 3129 enum machine_mode promoted_mode, passed_mode, nominal_mode;
00d8a4c1 3130 int unsignedp;
6f086dfc
RS
3131 /* Total space needed so far for args on the stack,
3132 given as a constant and a tree-expression. */
3133 struct args_size stack_args_size;
3134 tree fntype = TREE_TYPE (fndecl);
3135 tree fnargs = DECL_ARGUMENTS (fndecl);
3136 /* This is used for the arg pointer when referring to stack args. */
3137 rtx internal_arg_pointer;
3138 /* This is a dummy PARM_DECL that we used for the function result if
3139 the function returns a structure. */
3140 tree function_result_decl = 0;
3141 int nparmregs = list_length (fnargs) + LAST_VIRTUAL_REGISTER + 1;
3142 int varargs_setup = 0;
3412b298 3143 rtx conversion_insns = 0;
de3ab9df
RS
3144 /* FUNCTION_ARG may look at this variable. Since this is not
3145 expanding a call it will always be zero in this function. */
3146 int current_call_is_indirect = 0;
6f086dfc
RS
3147
3148 /* Nonzero if the last arg is named `__builtin_va_alist',
3149 which is used on some machines for old-fashioned non-ANSI varargs.h;
3150 this should be stuck onto the stack as if it had arrived there. */
3b69d50e
RK
3151 int hide_last_arg
3152 = (current_function_varargs
3153 && fnargs
6f086dfc
RS
3154 && (parm = tree_last (fnargs)) != 0
3155 && DECL_NAME (parm)
3156 && (! strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
3157 "__builtin_va_alist")));
3158
3159 /* Nonzero if function takes extra anonymous args.
3160 This means the last named arg must be on the stack
3161 right before the anonymous ones. */
3162 int stdarg
3163 = (TYPE_ARG_TYPES (fntype) != 0
3164 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3165 != void_type_node));
3166
3167 /* If the reg that the virtual arg pointer will be translated into is
3168 not a fixed reg or is the stack pointer, make a copy of the virtual
3169 arg pointer, and address parms via the copy. The frame pointer is
3170 considered fixed even though it is not marked as such.
3171
3172 The second time through, simply use ap to avoid generating rtx. */
3173
3174 if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
3175 || ! (fixed_regs[ARG_POINTER_REGNUM]
3176 || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM))
3177 && ! second_time)
3178 internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
3179 else
3180 internal_arg_pointer = virtual_incoming_args_rtx;
3181 current_function_internal_arg_pointer = internal_arg_pointer;
3182
3183 stack_args_size.constant = 0;
3184 stack_args_size.var = 0;
3185
3186 /* If struct value address is treated as the first argument, make it so. */
3187 if (aggregate_value_p (DECL_RESULT (fndecl))
3188 && ! current_function_returns_pcc_struct
3189 && struct_value_incoming_rtx == 0)
3190 {
3191 tree type = build_pointer_type (fntype);
3192
5f4f0e22 3193 function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
6f086dfc
RS
3194
3195 DECL_ARG_TYPE (function_result_decl) = type;
3196 TREE_CHAIN (function_result_decl) = fnargs;
3197 fnargs = function_result_decl;
3198 }
3199
3200 parm_reg_stack_loc = (rtx *) oballoc (nparmregs * sizeof (rtx));
4c9a05bc 3201 bzero ((char *) parm_reg_stack_loc, nparmregs * sizeof (rtx));
6f086dfc
RS
3202
3203#ifdef INIT_CUMULATIVE_INCOMING_ARGS
ea0d4c4b 3204 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
6f086dfc 3205#else
ea0d4c4b 3206 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX);
6f086dfc
RS
3207#endif
3208
3209 /* We haven't yet found an argument that we must push and pretend the
3210 caller did. */
3211 current_function_pretend_args_size = 0;
3212
3213 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
3214 {
05e3bdb9 3215 int aggregate = AGGREGATE_TYPE_P (TREE_TYPE (parm));
6f086dfc
RS
3216 struct args_size stack_offset;
3217 struct args_size arg_size;
3218 int passed_pointer = 0;
3219 tree passed_type = DECL_ARG_TYPE (parm);
3220
3221 /* Set LAST_NAMED if this is last named arg before some
3222 anonymous args. We treat it as if it were anonymous too. */
3223 int last_named = ((TREE_CHAIN (parm) == 0
3224 || DECL_NAME (TREE_CHAIN (parm)) == 0)
3b69d50e 3225 && (stdarg || current_function_varargs));
6f086dfc
RS
3226
3227 if (TREE_TYPE (parm) == error_mark_node
3228 /* This can happen after weird syntax errors
3229 or if an enum type is defined among the parms. */
3230 || TREE_CODE (parm) != PARM_DECL
3231 || passed_type == NULL)
3232 {
587cb682
TW
3233 DECL_INCOMING_RTL (parm) = DECL_RTL (parm) = gen_rtx (MEM, BLKmode,
3234 const0_rtx);
6f086dfc
RS
3235 TREE_USED (parm) = 1;
3236 continue;
3237 }
3238
3239 /* For varargs.h function, save info about regs and stack space
3240 used by the individual args, not including the va_alist arg. */
3b69d50e 3241 if (hide_last_arg && last_named)
6f086dfc
RS
3242 current_function_args_info = args_so_far;
3243
3244 /* Find mode of arg as it is passed, and mode of arg
3245 as it should be during execution of this function. */
3246 passed_mode = TYPE_MODE (passed_type);
3247 nominal_mode = TYPE_MODE (TREE_TYPE (parm));
3248
16bae307
RS
3249 /* If the parm's mode is VOID, its value doesn't matter,
3250 and avoid the usual things like emit_move_insn that could crash. */
3251 if (nominal_mode == VOIDmode)
3252 {
3253 DECL_INCOMING_RTL (parm) = DECL_RTL (parm) = const0_rtx;
3254 continue;
3255 }
3256
3f46679a
RK
3257 /* If the parm is to be passed as a transparent union, use the
3258 type of the first field for the tests below. We have already
3259 verified that the modes are the same. */
3260 if (DECL_TRANSPARENT_UNION (parm)
3261 || TYPE_TRANSPARENT_UNION (passed_type))
3262 passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
3263
a14ae508
RK
3264 /* See if this arg was passed by invisible reference. It is if
3265 it is an object whose size depends on the contents of the
3266 object itself or if the machine requires these objects be passed
3267 that way. */
3268
3269 if ((TREE_CODE (TYPE_SIZE (passed_type)) != INTEGER_CST
3270 && contains_placeholder_p (TYPE_SIZE (passed_type)))
4ac3d994 3271 || TYPE_NEEDS_CONSTRUCTING (passed_type)
6f086dfc 3272#ifdef FUNCTION_ARG_PASS_BY_REFERENCE
a14ae508
RK
3273 || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
3274 passed_type, ! last_named)
3275#endif
3276 )
6f086dfc
RS
3277 {
3278 passed_type = build_pointer_type (passed_type);
3279 passed_pointer = 1;
3280 passed_mode = nominal_mode = Pmode;
3281 }
6f086dfc 3282
a53e14c0
RK
3283 promoted_mode = passed_mode;
3284
3285#ifdef PROMOTE_FUNCTION_ARGS
3286 /* Compute the mode in which the arg is actually extended to. */
a5a52dbc 3287 promoted_mode = promote_mode (passed_type, promoted_mode, &unsignedp, 1);
a53e14c0
RK
3288#endif
3289
6f086dfc
RS
3290 /* Let machine desc say which reg (if any) the parm arrives in.
3291 0 means it arrives on the stack. */
3292#ifdef FUNCTION_INCOMING_ARG
a53e14c0 3293 entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
6f086dfc
RS
3294 passed_type, ! last_named);
3295#else
a53e14c0 3296 entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
6f086dfc
RS
3297 passed_type, ! last_named);
3298#endif
3299
a53e14c0
RK
3300 if (entry_parm)
3301 passed_mode = promoted_mode;
3302
6f086dfc
RS
3303#ifdef SETUP_INCOMING_VARARGS
3304 /* If this is the last named parameter, do any required setup for
3305 varargs or stdargs. We need to know about the case of this being an
3306 addressable type, in which case we skip the registers it
3307 would have arrived in.
3308
3309 For stdargs, LAST_NAMED will be set for two parameters, the one that
3310 is actually the last named, and the dummy parameter. We only
3311 want to do this action once.
3312
3313 Also, indicate when RTL generation is to be suppressed. */
3314 if (last_named && !varargs_setup)
3315 {
3316 SETUP_INCOMING_VARARGS (args_so_far, passed_mode, passed_type,
3317 current_function_pretend_args_size,
3318 second_time);
3319 varargs_setup = 1;
3320 }
3321#endif
3322
3323 /* Determine parm's home in the stack,
3324 in case it arrives in the stack or we should pretend it did.
3325
3326 Compute the stack position and rtx where the argument arrives
3327 and its size.
3328
3329 There is one complexity here: If this was a parameter that would
3330 have been passed in registers, but wasn't only because it is
3331 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
3332 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
3333 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
3334 0 as it was the previous time. */
3335
3336 locate_and_pad_parm (passed_mode, passed_type,
3337#ifdef STACK_PARMS_IN_REG_PARM_AREA
3338 1,
3339#else
3340#ifdef FUNCTION_INCOMING_ARG
3341 FUNCTION_INCOMING_ARG (args_so_far, passed_mode,
3342 passed_type,
3343 (! last_named
3344 || varargs_setup)) != 0,
3345#else
3346 FUNCTION_ARG (args_so_far, passed_mode,
3347 passed_type,
3348 ! last_named || varargs_setup) != 0,
3349#endif
3350#endif
3351 fndecl, &stack_args_size, &stack_offset, &arg_size);
3352
3353 if (! second_time)
3354 {
3355 rtx offset_rtx = ARGS_SIZE_RTX (stack_offset);
3356
3357 if (offset_rtx == const0_rtx)
3358 stack_parm = gen_rtx (MEM, passed_mode, internal_arg_pointer);
3359 else
3360 stack_parm = gen_rtx (MEM, passed_mode,
3361 gen_rtx (PLUS, Pmode,
3362 internal_arg_pointer, offset_rtx));
3363
3364 /* If this is a memory ref that contains aggregate components,
3365 mark it as such for cse and loop optimize. */
3366 MEM_IN_STRUCT_P (stack_parm) = aggregate;
3367 }
3368
3369 /* If this parameter was passed both in registers and in the stack,
3370 use the copy on the stack. */
3371 if (MUST_PASS_IN_STACK (passed_mode, passed_type))
3372 entry_parm = 0;
3373
461beb10 3374#ifdef FUNCTION_ARG_PARTIAL_NREGS
6f086dfc
RS
3375 /* If this parm was passed part in regs and part in memory,
3376 pretend it arrived entirely in memory
3377 by pushing the register-part onto the stack.
3378
3379 In the special case of a DImode or DFmode that is split,
3380 we could put it together in a pseudoreg directly,
3381 but for now that's not worth bothering with. */
3382
3383 if (entry_parm)
3384 {
461beb10
DE
3385 int nregs = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, passed_mode,
3386 passed_type, ! last_named);
6f086dfc
RS
3387
3388 if (nregs > 0)
3389 {
3390 current_function_pretend_args_size
3391 = (((nregs * UNITS_PER_WORD) + (PARM_BOUNDARY / BITS_PER_UNIT) - 1)
3392 / (PARM_BOUNDARY / BITS_PER_UNIT)
3393 * (PARM_BOUNDARY / BITS_PER_UNIT));
3394
3395 if (! second_time)
3396 move_block_from_reg (REGNO (entry_parm),
f31870c1
JW
3397 validize_mem (stack_parm), nregs,
3398 int_size_in_bytes (TREE_TYPE (parm)));
6f086dfc
RS
3399 entry_parm = stack_parm;
3400 }
3401 }
461beb10 3402#endif
6f086dfc
RS
3403
3404 /* If we didn't decide this parm came in a register,
3405 by default it came on the stack. */
3406 if (entry_parm == 0)
3407 entry_parm = stack_parm;
3408
3409 /* Record permanently how this parm was passed. */
3410 if (! second_time)
3411 DECL_INCOMING_RTL (parm) = entry_parm;
3412
3413 /* If there is actually space on the stack for this parm,
3414 count it in stack_args_size; otherwise set stack_parm to 0
3415 to indicate there is no preallocated stack slot for the parm. */
3416
3417 if (entry_parm == stack_parm
d9ca49d5 3418#if defined (REG_PARM_STACK_SPACE) && ! defined (MAYBE_REG_PARM_STACK_SPACE)
6f086dfc 3419 /* On some machines, even if a parm value arrives in a register
d9ca49d5
JW
3420 there is still an (uninitialized) stack slot allocated for it.
3421
3422 ??? When MAYBE_REG_PARM_STACK_SPACE is defined, we can't tell
3423 whether this parameter already has a stack slot allocated,
3424 because an arg block exists only if current_function_args_size
3425 is larger than some threshhold, and we haven't calculated that
3426 yet. So, for now, we just assume that stack slots never exist
3427 in this case. */
6f086dfc
RS
3428 || REG_PARM_STACK_SPACE (fndecl) > 0
3429#endif
3430 )
3431 {
3432 stack_args_size.constant += arg_size.constant;
3433 if (arg_size.var)
3434 ADD_PARM_SIZE (stack_args_size, arg_size.var);
3435 }
3436 else
3437 /* No stack slot was pushed for this parm. */
3438 stack_parm = 0;
3439
3440 /* Update info on where next arg arrives in registers. */
3441
3442 FUNCTION_ARG_ADVANCE (args_so_far, passed_mode,
3443 passed_type, ! last_named);
3444
3445 /* If this is our second time through, we are done with this parm. */
3446 if (second_time)
3447 continue;
3448
e16c591a
RS
3449 /* If we can't trust the parm stack slot to be aligned enough
3450 for its ultimate type, don't use that slot after entry.
3451 We'll make another stack slot, if we need one. */
3452 {
e16c591a
RS
3453 int thisparm_boundary
3454 = FUNCTION_ARG_BOUNDARY (passed_mode, passed_type);
e16c591a
RS
3455
3456 if (GET_MODE_ALIGNMENT (nominal_mode) > thisparm_boundary)
3457 stack_parm = 0;
3458 }
3459
cb61f66f
RS
3460 /* If parm was passed in memory, and we need to convert it on entry,
3461 don't store it back in that same slot. */
3462 if (entry_parm != 0
3463 && nominal_mode != BLKmode && nominal_mode != passed_mode)
3464 stack_parm = 0;
3465
3466#if 0
6f086dfc
RS
3467 /* Now adjust STACK_PARM to the mode and precise location
3468 where this parameter should live during execution,
3469 if we discover that it must live in the stack during execution.
3470 To make debuggers happier on big-endian machines, we store
3471 the value in the last bytes of the space available. */
3472
3473 if (nominal_mode != BLKmode && nominal_mode != passed_mode
3474 && stack_parm != 0)
3475 {
3476 rtx offset_rtx;
3477
f76b9db2
ILT
3478 if (BYTES_BIG_ENDIAN
3479 && GET_MODE_SIZE (nominal_mode) < UNITS_PER_WORD)
6f086dfc
RS
3480 stack_offset.constant += (GET_MODE_SIZE (passed_mode)
3481 - GET_MODE_SIZE (nominal_mode));
6f086dfc
RS
3482
3483 offset_rtx = ARGS_SIZE_RTX (stack_offset);
3484 if (offset_rtx == const0_rtx)
3485 stack_parm = gen_rtx (MEM, nominal_mode, internal_arg_pointer);
3486 else
3487 stack_parm = gen_rtx (MEM, nominal_mode,
3488 gen_rtx (PLUS, Pmode,
3489 internal_arg_pointer, offset_rtx));
3490
3491 /* If this is a memory ref that contains aggregate components,
3492 mark it as such for cse and loop optimize. */
3493 MEM_IN_STRUCT_P (stack_parm) = aggregate;
3494 }
cb61f66f 3495#endif /* 0 */
6f086dfc
RS
3496
3497 /* ENTRY_PARM is an RTX for the parameter as it arrives,
3498 in the mode in which it arrives.
3499 STACK_PARM is an RTX for a stack slot where the parameter can live
3500 during the function (in case we want to put it there).
3501 STACK_PARM is 0 if no stack slot was pushed for it.
3502
3503 Now output code if necessary to convert ENTRY_PARM to
3504 the type in which this function declares it,
3505 and store that result in an appropriate place,
3506 which may be a pseudo reg, may be STACK_PARM,
3507 or may be a local stack slot if STACK_PARM is 0.
3508
3509 Set DECL_RTL to that place. */
3510
3511 if (nominal_mode == BLKmode)
3512 {
3513 /* If a BLKmode arrives in registers, copy it to a stack slot. */
3514 if (GET_CODE (entry_parm) == REG)
3515 {
3516 int size_stored = CEIL_ROUND (int_size_in_bytes (TREE_TYPE (parm)),
3517 UNITS_PER_WORD);
3518
3519 /* Note that we will be storing an integral number of words.
3520 So we have to be careful to ensure that we allocate an
3521 integral number of words. We do this below in the
3522 assign_stack_local if space was not allocated in the argument
3523 list. If it was, this will not work if PARM_BOUNDARY is not
3524 a multiple of BITS_PER_WORD. It isn't clear how to fix this
3525 if it becomes a problem. */
3526
3527 if (stack_parm == 0)
7e41ffa2
RS
3528 {
3529 stack_parm
3530 = assign_stack_local (GET_MODE (entry_parm), size_stored, 0);
3531 /* If this is a memory ref that contains aggregate components,
3532 mark it as such for cse and loop optimize. */
3533 MEM_IN_STRUCT_P (stack_parm) = aggregate;
3534 }
3535
6f086dfc
RS
3536 else if (PARM_BOUNDARY % BITS_PER_WORD != 0)
3537 abort ();
3538
7a30f0c4
JW
3539 if (TREE_READONLY (parm))
3540 RTX_UNCHANGING_P (stack_parm) = 1;
3541
6f086dfc
RS
3542 move_block_from_reg (REGNO (entry_parm),
3543 validize_mem (stack_parm),
f31870c1
JW
3544 size_stored / UNITS_PER_WORD,
3545 int_size_in_bytes (TREE_TYPE (parm)));
6f086dfc
RS
3546 }
3547 DECL_RTL (parm) = stack_parm;
3548 }
74bd77a8 3549 else if (! ((obey_regdecls && ! DECL_REGISTER (parm)
a82ad570 3550 && ! DECL_INLINE (fndecl))
6f086dfc
RS
3551 /* layout_decl may set this. */
3552 || TREE_ADDRESSABLE (parm)
3553 || TREE_SIDE_EFFECTS (parm)
3554 /* If -ffloat-store specified, don't put explicit
3555 float variables into registers. */
3556 || (flag_float_store
3557 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
3558 /* Always assign pseudo to structure return or item passed
3559 by invisible reference. */
3560 || passed_pointer || parm == function_result_decl)
3561 {
00d8a4c1
RK
3562 /* Store the parm in a pseudoregister during the function, but we
3563 may need to do it in a wider mode. */
3564
3565 register rtx parmreg;
a03caf76 3566 int regno, regnoi, regnor;
00d8a4c1
RK
3567
3568 unsignedp = TREE_UNSIGNED (TREE_TYPE (parm));
a5a52dbc
RK
3569 nominal_mode = promote_mode (TREE_TYPE (parm), nominal_mode,
3570 &unsignedp, 1);
6f086dfc 3571
00d8a4c1 3572 parmreg = gen_reg_rtx (nominal_mode);
6f086dfc
RS
3573 REG_USERVAR_P (parmreg) = 1;
3574
3575 /* If this was an item that we received a pointer to, set DECL_RTL
3576 appropriately. */
3577 if (passed_pointer)
3578 {
3579 DECL_RTL (parm) = gen_rtx (MEM, TYPE_MODE (TREE_TYPE (passed_type)), parmreg);
3580 MEM_IN_STRUCT_P (DECL_RTL (parm)) = aggregate;
3581 }
3582 else
3583 DECL_RTL (parm) = parmreg;
3584
3585 /* Copy the value into the register. */
3586 if (GET_MODE (parmreg) != GET_MODE (entry_parm))
86f8eff3
RK
3587 {
3588 /* If ENTRY_PARM is a hard register, it might be in a register
3589 not valid for operating in its mode (e.g., an odd-numbered
3590 register for a DFmode). In that case, moves are the only
3591 thing valid, so we can't do a convert from there. This
3592 occurs when the calling sequence allow such misaligned
3412b298
JW
3593 usages.
3594
3595 In addition, the conversion may involve a call, which could
3596 clobber parameters which haven't been copied to pseudo
3597 registers yet. Therefore, we must first copy the parm to
3598 a pseudo reg here, and save the conversion until after all
3599 parameters have been moved. */
3600
3601 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
3602
3603 emit_move_insn (tempreg, validize_mem (entry_parm));
3604
3605 push_to_sequence (conversion_insns);
8c394878 3606 convert_move (parmreg, tempreg, unsignedp);
3412b298
JW
3607 conversion_insns = get_insns ();
3608 end_sequence ();
86f8eff3 3609 }
6f086dfc
RS
3610 else
3611 emit_move_insn (parmreg, validize_mem (entry_parm));
3612
74bd77a8
RS
3613 /* If we were passed a pointer but the actual value
3614 can safely live in a register, put it in one. */
16bae307 3615 if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
74bd77a8
RS
3616 && ! ((obey_regdecls && ! DECL_REGISTER (parm)
3617 && ! DECL_INLINE (fndecl))
3618 /* layout_decl may set this. */
3619 || TREE_ADDRESSABLE (parm)
3620 || TREE_SIDE_EFFECTS (parm)
3621 /* If -ffloat-store specified, don't put explicit
3622 float variables into registers. */
3623 || (flag_float_store
3624 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE)))
3625 {
2654605a
JW
3626 /* We can't use nominal_mode, because it will have been set to
3627 Pmode above. We must use the actual mode of the parm. */
3628 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
ba6da627 3629 REG_USERVAR_P (parmreg) = 1;
74bd77a8
RS
3630 emit_move_insn (parmreg, DECL_RTL (parm));
3631 DECL_RTL (parm) = parmreg;
c110c53d
RS
3632 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3633 now the parm. */
3634 stack_parm = 0;
74bd77a8 3635 }
137a2a7b
DE
3636#ifdef FUNCTION_ARG_CALLEE_COPIES
3637 /* If we are passed an arg by reference and it is our responsibility
3638 to make a copy, do it now.
3639 PASSED_TYPE and PASSED mode now refer to the pointer, not the
3640 original argument, so we must recreate them in the call to
3641 FUNCTION_ARG_CALLEE_COPIES. */
3642 /* ??? Later add code to handle the case that if the argument isn't
3643 modified, don't do the copy. */
3644
3645 else if (passed_pointer
3646 && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
3647 TYPE_MODE (DECL_ARG_TYPE (parm)),
3648 DECL_ARG_TYPE (parm),
3649 ! last_named))
3650 {
3651 rtx copy;
3652 tree type = DECL_ARG_TYPE (parm);
3653
3654 /* This sequence may involve a library call perhaps clobbering
3655 registers that haven't been copied to pseudos yet. */
3656
3657 push_to_sequence (conversion_insns);
3658
3659 if (TYPE_SIZE (type) == 0
3660 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1fd3ef7f
RK
3661 /* This is a variable sized object. */
3662 copy = gen_rtx (MEM, BLKmode,
3663 allocate_dynamic_stack_space
6673dddf 3664 (expr_size (parm), NULL_RTX,
1fd3ef7f 3665 TYPE_ALIGN (type)));
137a2a7b 3666 else
1fd3ef7f
RK
3667 copy = assign_stack_temp (TYPE_MODE (type),
3668 int_size_in_bytes (type), 1);
3668e76e 3669 MEM_IN_STRUCT_P (copy) = AGGREGATE_TYPE_P (type);
137a2a7b
DE
3670
3671 store_expr (parm, copy, 0);
3672 emit_move_insn (parmreg, XEXP (copy, 0));
3673 conversion_insns = get_insns ();
3674 end_sequence ();
3675 }
3676#endif /* FUNCTION_ARG_CALLEE_COPIES */
74bd77a8 3677
6f086dfc 3678 /* In any case, record the parm's desired stack location
14aceb29
RS
3679 in case we later discover it must live in the stack.
3680
3681 If it is a COMPLEX value, store the stack location for both
3682 halves. */
3683
3684 if (GET_CODE (parmreg) == CONCAT)
3685 regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
3686 else
3687 regno = REGNO (parmreg);
3688
3689 if (regno >= nparmregs)
6f086dfc
RS
3690 {
3691 rtx *new;
19fdd3ee 3692 int old_nparmregs = nparmregs;
14aceb29
RS
3693
3694 nparmregs = regno + 5;
6f086dfc 3695 new = (rtx *) oballoc (nparmregs * sizeof (rtx));
4c9a05bc
RK
3696 bcopy ((char *) parm_reg_stack_loc, (char *) new,
3697 old_nparmregs * sizeof (rtx));
3698 bzero ((char *) (new + old_nparmregs),
14aceb29 3699 (nparmregs - old_nparmregs) * sizeof (rtx));
6f086dfc
RS
3700 parm_reg_stack_loc = new;
3701 }
14aceb29
RS
3702
3703 if (GET_CODE (parmreg) == CONCAT)
3704 {
3705 enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
3706
a03caf76
RK
3707 regnor = REGNO (gen_realpart (submode, parmreg));
3708 regnoi = REGNO (gen_imagpart (submode, parmreg));
3709
7b1a0c14
RS
3710 if (stack_parm != 0)
3711 {
a03caf76 3712 parm_reg_stack_loc[regnor]
3d329b07 3713 = gen_realpart (submode, stack_parm);
a03caf76 3714 parm_reg_stack_loc[regnoi]
3d329b07 3715 = gen_imagpart (submode, stack_parm);
7b1a0c14
RS
3716 }
3717 else
3718 {
a03caf76
RK
3719 parm_reg_stack_loc[regnor] = 0;
3720 parm_reg_stack_loc[regnoi] = 0;
7b1a0c14 3721 }
14aceb29
RS
3722 }
3723 else
3724 parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
6f086dfc
RS
3725
3726 /* Mark the register as eliminable if we did no conversion
3727 and it was copied from memory at a fixed offset,
3728 and the arg pointer was not copied to a pseudo-reg.
3729 If the arg pointer is a pseudo reg or the offset formed
3730 an invalid address, such memory-equivalences
3731 as we make here would screw up life analysis for it. */
3732 if (nominal_mode == passed_mode
a03caf76 3733 && ! conversion_insns
6f086dfc 3734 && GET_CODE (entry_parm) == MEM
e16c591a 3735 && entry_parm == stack_parm
6f086dfc
RS
3736 && stack_offset.var == 0
3737 && reg_mentioned_p (virtual_incoming_args_rtx,
3738 XEXP (entry_parm, 0)))
a03caf76
RK
3739 {
3740 rtx linsn = get_last_insn ();
3741
3742 /* Mark complex types separately. */
3743 if (GET_CODE (parmreg) == CONCAT)
3744 {
3745 REG_NOTES (linsn)
3746 = gen_rtx (EXPR_LIST, REG_EQUIV,
3747 parm_reg_stack_loc[regnoi], REG_NOTES (linsn));
3748
3749 /* Now search backward for where we set the real part. */
3750 for (; linsn != 0
3751 && ! reg_referenced_p (parm_reg_stack_loc[regnor],
3752 PATTERN (linsn));
3753 linsn = prev_nonnote_insn (linsn))
3754 ;
3755
3756 REG_NOTES (linsn)
3757 = gen_rtx (EXPR_LIST, REG_EQUIV,
3758 parm_reg_stack_loc[regnor], REG_NOTES (linsn));
3759 }
3760 else
3761 REG_NOTES (linsn)
3762 = gen_rtx (EXPR_LIST, REG_EQUIV,
3763 entry_parm, REG_NOTES (linsn));
3764 }
6f086dfc
RS
3765
3766 /* For pointer data type, suggest pointer register. */
3767 if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
3768 mark_reg_pointer (parmreg);
3769 }
3770 else
3771 {
3772 /* Value must be stored in the stack slot STACK_PARM
3773 during function execution. */
3774
3775 if (passed_mode != nominal_mode)
86f8eff3
RK
3776 {
3777 /* Conversion is required. */
3412b298
JW
3778 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
3779
3780 emit_move_insn (tempreg, validize_mem (entry_parm));
86f8eff3 3781
3412b298
JW
3782 push_to_sequence (conversion_insns);
3783 entry_parm = convert_to_mode (nominal_mode, tempreg,
a53e14c0 3784 TREE_UNSIGNED (TREE_TYPE (parm)));
3412b298
JW
3785 conversion_insns = get_insns ();
3786 end_sequence ();
86f8eff3 3787 }
6f086dfc
RS
3788
3789 if (entry_parm != stack_parm)
3790 {
3791 if (stack_parm == 0)
7e41ffa2
RS
3792 {
3793 stack_parm
3794 = assign_stack_local (GET_MODE (entry_parm),
3795 GET_MODE_SIZE (GET_MODE (entry_parm)), 0);
3796 /* If this is a memory ref that contains aggregate components,
3797 mark it as such for cse and loop optimize. */
3798 MEM_IN_STRUCT_P (stack_parm) = aggregate;
3799 }
3800
3412b298
JW
3801 if (passed_mode != nominal_mode)
3802 {
3803 push_to_sequence (conversion_insns);
3804 emit_move_insn (validize_mem (stack_parm),
3805 validize_mem (entry_parm));
3806 conversion_insns = get_insns ();
3807 end_sequence ();
3808 }
3809 else
3810 emit_move_insn (validize_mem (stack_parm),
3811 validize_mem (entry_parm));
6f086dfc
RS
3812 }
3813
3814 DECL_RTL (parm) = stack_parm;
3815 }
3816
3817 /* If this "parameter" was the place where we are receiving the
3818 function's incoming structure pointer, set up the result. */
3819 if (parm == function_result_decl)
ccdecf58
RK
3820 {
3821 tree result = DECL_RESULT (fndecl);
3822 tree restype = TREE_TYPE (result);
3823
3824 DECL_RTL (result)
3825 = gen_rtx (MEM, DECL_MODE (result), DECL_RTL (parm));
3826
05e3bdb9 3827 MEM_IN_STRUCT_P (DECL_RTL (result)) = AGGREGATE_TYPE_P (restype);
ccdecf58 3828 }
6f086dfc
RS
3829
3830 if (TREE_THIS_VOLATILE (parm))
3831 MEM_VOLATILE_P (DECL_RTL (parm)) = 1;
3832 if (TREE_READONLY (parm))
3833 RTX_UNCHANGING_P (DECL_RTL (parm)) = 1;
3834 }
3835
3412b298
JW
3836 /* Output all parameter conversion instructions (possibly including calls)
3837 now that all parameters have been copied out of hard registers. */
3838 emit_insns (conversion_insns);
3839
6f086dfc
RS
3840 max_parm_reg = max_reg_num ();
3841 last_parm_insn = get_last_insn ();
3842
3843 current_function_args_size = stack_args_size.constant;
3844
3845 /* Adjust function incoming argument size for alignment and
3846 minimum length. */
3847
3848#ifdef REG_PARM_STACK_SPACE
6f90e075 3849#ifndef MAYBE_REG_PARM_STACK_SPACE
6f086dfc
RS
3850 current_function_args_size = MAX (current_function_args_size,
3851 REG_PARM_STACK_SPACE (fndecl));
3852#endif
6f90e075 3853#endif
6f086dfc
RS
3854
3855#ifdef STACK_BOUNDARY
3856#define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
3857
3858 current_function_args_size
3859 = ((current_function_args_size + STACK_BYTES - 1)
3860 / STACK_BYTES) * STACK_BYTES;
3861#endif
3862
3863#ifdef ARGS_GROW_DOWNWARD
3864 current_function_arg_offset_rtx
5f4f0e22 3865 = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
6f086dfc
RS
3866 : expand_expr (size_binop (MINUS_EXPR, stack_args_size.var,
3867 size_int (-stack_args_size.constant)),
5f4f0e22 3868 NULL_RTX, VOIDmode, 0));
6f086dfc
RS
3869#else
3870 current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
3871#endif
3872
3873 /* See how many bytes, if any, of its args a function should try to pop
3874 on return. */
3875
3876 current_function_pops_args = RETURN_POPS_ARGS (TREE_TYPE (fndecl),
3877 current_function_args_size);
3878
3b69d50e
RK
3879 /* For stdarg.h function, save info about
3880 regs and stack space used by the named args. */
6f086dfc 3881
3b69d50e 3882 if (!hide_last_arg)
6f086dfc
RS
3883 current_function_args_info = args_so_far;
3884
3885 /* Set the rtx used for the function return value. Put this in its
3886 own variable so any optimizers that need this information don't have
3887 to include tree.h. Do this here so it gets done when an inlined
3888 function gets output. */
3889
3890 current_function_return_rtx = DECL_RTL (DECL_RESULT (fndecl));
3891}
3892\f
75dc3319
RK
3893/* Indicate whether REGNO is an incoming argument to the current function
3894 that was promoted to a wider mode. If so, return the RTX for the
3895 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
3896 that REGNO is promoted from and whether the promotion was signed or
3897 unsigned. */
3898
3899#ifdef PROMOTE_FUNCTION_ARGS
3900
3901rtx
3902promoted_input_arg (regno, pmode, punsignedp)
3903 int regno;
3904 enum machine_mode *pmode;
3905 int *punsignedp;
3906{
3907 tree arg;
3908
3909 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
3910 arg = TREE_CHAIN (arg))
3911 if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
a5a52dbc 3912 && REGNO (DECL_INCOMING_RTL (arg)) == regno)
75dc3319
RK
3913 {
3914 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
3915 int unsignedp = TREE_UNSIGNED (TREE_TYPE (arg));
3916
a5a52dbc 3917 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
75dc3319
RK
3918 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
3919 && mode != DECL_MODE (arg))
3920 {
3921 *pmode = DECL_MODE (arg);
3922 *punsignedp = unsignedp;
3923 return DECL_INCOMING_RTL (arg);
3924 }
3925 }
3926
3927 return 0;
3928}
3929
3930#endif
3931\f
6f086dfc
RS
3932/* Compute the size and offset from the start of the stacked arguments for a
3933 parm passed in mode PASSED_MODE and with type TYPE.
3934
3935 INITIAL_OFFSET_PTR points to the current offset into the stacked
3936 arguments.
3937
3938 The starting offset and size for this parm are returned in *OFFSET_PTR
3939 and *ARG_SIZE_PTR, respectively.
3940
3941 IN_REGS is non-zero if the argument will be passed in registers. It will
3942 never be set if REG_PARM_STACK_SPACE is not defined.
3943
3944 FNDECL is the function in which the argument was defined.
3945
3946 There are two types of rounding that are done. The first, controlled by
3947 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
3948 list to be aligned to the specific boundary (in bits). This rounding
3949 affects the initial and starting offsets, but not the argument size.
3950
3951 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
3952 optionally rounds the size of the parm to PARM_BOUNDARY. The
3953 initial offset is not affected by this rounding, while the size always
3954 is and the starting offset may be. */
3955
3956/* offset_ptr will be negative for ARGS_GROW_DOWNWARD case;
3957 initial_offset_ptr is positive because locate_and_pad_parm's
3958 callers pass in the total size of args so far as
3959 initial_offset_ptr. arg_size_ptr is always positive.*/
3960
6f086dfc
RS
3961void
3962locate_and_pad_parm (passed_mode, type, in_regs, fndecl,
3963 initial_offset_ptr, offset_ptr, arg_size_ptr)
3964 enum machine_mode passed_mode;
3965 tree type;
3966 int in_regs;
3967 tree fndecl;
3968 struct args_size *initial_offset_ptr;
3969 struct args_size *offset_ptr;
3970 struct args_size *arg_size_ptr;
3971{
3972 tree sizetree
3973 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
3974 enum direction where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
3975 int boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
3976 int boundary_in_bytes = boundary / BITS_PER_UNIT;
3977 int reg_parm_stack_space = 0;
3978
3979#ifdef REG_PARM_STACK_SPACE
3980 /* If we have found a stack parm before we reach the end of the
3981 area reserved for registers, skip that area. */
3982 if (! in_regs)
3983 {
29008b51
JW
3984#ifdef MAYBE_REG_PARM_STACK_SPACE
3985 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
3986#else
6f086dfc 3987 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
29008b51 3988#endif
6f086dfc
RS
3989 if (reg_parm_stack_space > 0)
3990 {
3991 if (initial_offset_ptr->var)
3992 {
3993 initial_offset_ptr->var
3994 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
3995 size_int (reg_parm_stack_space));
3996 initial_offset_ptr->constant = 0;
3997 }
3998 else if (initial_offset_ptr->constant < reg_parm_stack_space)
3999 initial_offset_ptr->constant = reg_parm_stack_space;
4000 }
4001 }
4002#endif /* REG_PARM_STACK_SPACE */
4003
4004 arg_size_ptr->var = 0;
4005 arg_size_ptr->constant = 0;
4006
4007#ifdef ARGS_GROW_DOWNWARD
4008 if (initial_offset_ptr->var)
4009 {
4010 offset_ptr->constant = 0;
4011 offset_ptr->var = size_binop (MINUS_EXPR, integer_zero_node,
4012 initial_offset_ptr->var);
4013 }
4014 else
4015 {
4016 offset_ptr->constant = - initial_offset_ptr->constant;
4017 offset_ptr->var = 0;
4018 }
0b21dcf5 4019 if (where_pad != none
6f086dfc
RS
4020 && (TREE_CODE (sizetree) != INTEGER_CST
4021 || ((TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)))
4022 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4023 SUB_PARM_SIZE (*offset_ptr, sizetree);
66bcbe19
TG
4024 if (where_pad != downward)
4025 pad_to_arg_alignment (offset_ptr, boundary);
6f086dfc
RS
4026 if (initial_offset_ptr->var)
4027 {
4028 arg_size_ptr->var = size_binop (MINUS_EXPR,
4029 size_binop (MINUS_EXPR,
4030 integer_zero_node,
4031 initial_offset_ptr->var),
4032 offset_ptr->var);
4033 }
4034 else
4035 {
4036 arg_size_ptr->constant = (- initial_offset_ptr->constant -
4037 offset_ptr->constant);
4038 }
6f086dfc
RS
4039#else /* !ARGS_GROW_DOWNWARD */
4040 pad_to_arg_alignment (initial_offset_ptr, boundary);
4041 *offset_ptr = *initial_offset_ptr;
6f086dfc
RS
4042
4043#ifdef PUSH_ROUNDING
4044 if (passed_mode != BLKmode)
4045 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
4046#endif
4047
d4b0a7a0
DE
4048 /* Pad_below needs the pre-rounded size to know how much to pad below
4049 so this must be done before rounding up. */
ea5917da
DE
4050 if (where_pad == downward
4051 /* However, BLKmode args passed in regs have their padding done elsewhere.
4052 The stack slot must be able to hold the entire register. */
4053 && !(in_regs && passed_mode == BLKmode))
d4b0a7a0
DE
4054 pad_below (offset_ptr, passed_mode, sizetree);
4055
6f086dfc
RS
4056 if (where_pad != none
4057 && (TREE_CODE (sizetree) != INTEGER_CST
4058 || ((TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)))
4059 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4060
4061 ADD_PARM_SIZE (*arg_size_ptr, sizetree);
4062#endif /* ARGS_GROW_DOWNWARD */
4063}
4064
e16c591a
RS
4065/* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
4066 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
4067
6f086dfc
RS
4068static void
4069pad_to_arg_alignment (offset_ptr, boundary)
4070 struct args_size *offset_ptr;
4071 int boundary;
4072{
4073 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4074
4075 if (boundary > BITS_PER_UNIT)
4076 {
4077 if (offset_ptr->var)
4078 {
4079 offset_ptr->var =
4080#ifdef ARGS_GROW_DOWNWARD
4081 round_down
4082#else
4083 round_up
4084#endif
4085 (ARGS_SIZE_TREE (*offset_ptr),
4086 boundary / BITS_PER_UNIT);
4087 offset_ptr->constant = 0; /*?*/
4088 }
4089 else
4090 offset_ptr->constant =
4091#ifdef ARGS_GROW_DOWNWARD
4092 FLOOR_ROUND (offset_ptr->constant, boundary_in_bytes);
4093#else
4094 CEIL_ROUND (offset_ptr->constant, boundary_in_bytes);
4095#endif
4096 }
4097}
4098
4099static void
4100pad_below (offset_ptr, passed_mode, sizetree)
4101 struct args_size *offset_ptr;
4102 enum machine_mode passed_mode;
4103 tree sizetree;
4104{
4105 if (passed_mode != BLKmode)
4106 {
4107 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
4108 offset_ptr->constant
4109 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
4110 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
4111 - GET_MODE_SIZE (passed_mode));
4112 }
4113 else
4114 {
4115 if (TREE_CODE (sizetree) != INTEGER_CST
4116 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
4117 {
4118 /* Round the size up to multiple of PARM_BOUNDARY bits. */
4119 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4120 /* Add it in. */
4121 ADD_PARM_SIZE (*offset_ptr, s2);
4122 SUB_PARM_SIZE (*offset_ptr, sizetree);
4123 }
4124 }
4125}
4126
4127static tree
4128round_down (value, divisor)
4129 tree value;
4130 int divisor;
4131{
4132 return size_binop (MULT_EXPR,
4133 size_binop (FLOOR_DIV_EXPR, value, size_int (divisor)),
4134 size_int (divisor));
4135}
4136\f
4137/* Walk the tree of blocks describing the binding levels within a function
4138 and warn about uninitialized variables.
4139 This is done after calling flow_analysis and before global_alloc
4140 clobbers the pseudo-regs to hard regs. */
4141
4142void
4143uninitialized_vars_warning (block)
4144 tree block;
4145{
4146 register tree decl, sub;
4147 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
4148 {
4149 if (TREE_CODE (decl) == VAR_DECL
4150 /* These warnings are unreliable for and aggregates
4151 because assigning the fields one by one can fail to convince
4152 flow.c that the entire aggregate was initialized.
4153 Unions are troublesome because members may be shorter. */
05e3bdb9 4154 && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
6f086dfc
RS
4155 && DECL_RTL (decl) != 0
4156 && GET_CODE (DECL_RTL (decl)) == REG
4157 && regno_uninitialized (REGNO (DECL_RTL (decl))))
4158 warning_with_decl (decl,
3c8cd8bd 4159 "`%s' might be used uninitialized in this function");
6f086dfc
RS
4160 if (TREE_CODE (decl) == VAR_DECL
4161 && DECL_RTL (decl) != 0
4162 && GET_CODE (DECL_RTL (decl)) == REG
4163 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
4164 warning_with_decl (decl,
3c8cd8bd 4165 "variable `%s' might be clobbered by `longjmp' or `vfork'");
6f086dfc
RS
4166 }
4167 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
4168 uninitialized_vars_warning (sub);
4169}
4170
4171/* Do the appropriate part of uninitialized_vars_warning
4172 but for arguments instead of local variables. */
4173
4174void
4175setjmp_args_warning (block)
4176 tree block;
4177{
4178 register tree decl;
4179 for (decl = DECL_ARGUMENTS (current_function_decl);
4180 decl; decl = TREE_CHAIN (decl))
4181 if (DECL_RTL (decl) != 0
4182 && GET_CODE (DECL_RTL (decl)) == REG
4183 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
3c8cd8bd 4184 warning_with_decl (decl, "argument `%s' might be clobbered by `longjmp' or `vfork'");
6f086dfc
RS
4185}
4186
4187/* If this function call setjmp, put all vars into the stack
4188 unless they were declared `register'. */
4189
4190void
4191setjmp_protect (block)
4192 tree block;
4193{
4194 register tree decl, sub;
4195 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
4196 if ((TREE_CODE (decl) == VAR_DECL
4197 || TREE_CODE (decl) == PARM_DECL)
4198 && DECL_RTL (decl) != 0
4199 && GET_CODE (DECL_RTL (decl)) == REG
b335c2cc
TW
4200 /* If this variable came from an inline function, it must be
4201 that it's life doesn't overlap the setjmp. If there was a
4202 setjmp in the function, it would already be in memory. We
4203 must exclude such variable because their DECL_RTL might be
4204 set to strange things such as virtual_stack_vars_rtx. */
4205 && ! DECL_FROM_INLINE (decl)
6f086dfc
RS
4206 && (
4207#ifdef NON_SAVING_SETJMP
4208 /* If longjmp doesn't restore the registers,
4209 don't put anything in them. */
4210 NON_SAVING_SETJMP
4211 ||
4212#endif
a82ad570 4213 ! DECL_REGISTER (decl)))
6f086dfc
RS
4214 put_var_into_stack (decl);
4215 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
4216 setjmp_protect (sub);
4217}
4218\f
4219/* Like the previous function, but for args instead of local variables. */
4220
4221void
4222setjmp_protect_args ()
4223{
4224 register tree decl, sub;
4225 for (decl = DECL_ARGUMENTS (current_function_decl);
4226 decl; decl = TREE_CHAIN (decl))
4227 if ((TREE_CODE (decl) == VAR_DECL
4228 || TREE_CODE (decl) == PARM_DECL)
4229 && DECL_RTL (decl) != 0
4230 && GET_CODE (DECL_RTL (decl)) == REG
4231 && (
4232 /* If longjmp doesn't restore the registers,
4233 don't put anything in them. */
4234#ifdef NON_SAVING_SETJMP
4235 NON_SAVING_SETJMP
4236 ||
4237#endif
a82ad570 4238 ! DECL_REGISTER (decl)))
6f086dfc
RS
4239 put_var_into_stack (decl);
4240}
4241\f
4242/* Return the context-pointer register corresponding to DECL,
4243 or 0 if it does not need one. */
4244
4245rtx
4246lookup_static_chain (decl)
4247 tree decl;
4248{
4249 tree context = decl_function_context (decl);
4250 tree link;
4251
4252 if (context == 0)
4253 return 0;
4254
4255 /* We treat inline_function_decl as an alias for the current function
4256 because that is the inline function whose vars, types, etc.
4257 are being merged into the current function.
4258 See expand_inline_function. */
4259 if (context == current_function_decl || context == inline_function_decl)
4260 return virtual_stack_vars_rtx;
4261
4262 for (link = context_display; link; link = TREE_CHAIN (link))
4263 if (TREE_PURPOSE (link) == context)
4264 return RTL_EXPR_RTL (TREE_VALUE (link));
4265
4266 abort ();
4267}
4268\f
4269/* Convert a stack slot address ADDR for variable VAR
4270 (from a containing function)
4271 into an address valid in this function (using a static chain). */
4272
4273rtx
4274fix_lexical_addr (addr, var)
4275 rtx addr;
4276 tree var;
4277{
4278 rtx basereg;
4279 int displacement;
4280 tree context = decl_function_context (var);
4281 struct function *fp;
4282 rtx base = 0;
4283
4284 /* If this is the present function, we need not do anything. */
4285 if (context == current_function_decl || context == inline_function_decl)
4286 return addr;
4287
4288 for (fp = outer_function_chain; fp; fp = fp->next)
4289 if (fp->decl == context)
4290 break;
4291
4292 if (fp == 0)
4293 abort ();
4294
4295 /* Decode given address as base reg plus displacement. */
4296 if (GET_CODE (addr) == REG)
4297 basereg = addr, displacement = 0;
4298 else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
4299 basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
4300 else
4301 abort ();
4302
4303 /* We accept vars reached via the containing function's
4304 incoming arg pointer and via its stack variables pointer. */
4305 if (basereg == fp->internal_arg_pointer)
4306 {
4307 /* If reached via arg pointer, get the arg pointer value
4308 out of that function's stack frame.
4309
4310 There are two cases: If a separate ap is needed, allocate a
4311 slot in the outer function for it and dereference it that way.
4312 This is correct even if the real ap is actually a pseudo.
4313 Otherwise, just adjust the offset from the frame pointer to
4314 compensate. */
4315
4316#ifdef NEED_SEPARATE_AP
4317 rtx addr;
4318
4319 if (fp->arg_pointer_save_area == 0)
4320 fp->arg_pointer_save_area
4321 = assign_outer_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0, fp);
4322
4323 addr = fix_lexical_addr (XEXP (fp->arg_pointer_save_area, 0), var);
4324 addr = memory_address (Pmode, addr);
4325
4326 base = copy_to_reg (gen_rtx (MEM, Pmode, addr));
4327#else
4328 displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
86f8eff3 4329 base = lookup_static_chain (var);
6f086dfc
RS
4330#endif
4331 }
4332
4333 else if (basereg == virtual_stack_vars_rtx)
4334 {
4335 /* This is the same code as lookup_static_chain, duplicated here to
4336 avoid an extra call to decl_function_context. */
4337 tree link;
4338
4339 for (link = context_display; link; link = TREE_CHAIN (link))
4340 if (TREE_PURPOSE (link) == context)
4341 {
4342 base = RTL_EXPR_RTL (TREE_VALUE (link));
4343 break;
4344 }
4345 }
4346
4347 if (base == 0)
4348 abort ();
4349
4350 /* Use same offset, relative to appropriate static chain or argument
4351 pointer. */
4352 return plus_constant (base, displacement);
4353}
4354\f
4355/* Return the address of the trampoline for entering nested fn FUNCTION.
4356 If necessary, allocate a trampoline (in the stack frame)
4357 and emit rtl to initialize its contents (at entry to this function). */
4358
4359rtx
4360trampoline_address (function)
4361 tree function;
4362{
4363 tree link;
4364 tree rtlexp;
4365 rtx tramp;
4366 struct function *fp;
4367 tree fn_context;
4368
4369 /* Find an existing trampoline and return it. */
4370 for (link = trampoline_list; link; link = TREE_CHAIN (link))
4371 if (TREE_PURPOSE (link) == function)
e87ee2a9
RK
4372 return
4373 round_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
4374
6f086dfc
RS
4375 for (fp = outer_function_chain; fp; fp = fp->next)
4376 for (link = fp->trampoline_list; link; link = TREE_CHAIN (link))
4377 if (TREE_PURPOSE (link) == function)
4378 {
4379 tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
4380 function);
4381 return round_trampoline_addr (tramp);
4382 }
4383
4384 /* None exists; we must make one. */
4385
4386 /* Find the `struct function' for the function containing FUNCTION. */
4387 fp = 0;
4388 fn_context = decl_function_context (function);
4389 if (fn_context != current_function_decl)
4390 for (fp = outer_function_chain; fp; fp = fp->next)
4391 if (fp->decl == fn_context)
4392 break;
4393
4394 /* Allocate run-time space for this trampoline
4395 (usually in the defining function's stack frame). */
4396#ifdef ALLOCATE_TRAMPOLINE
4397 tramp = ALLOCATE_TRAMPOLINE (fp);
4398#else
4399 /* If rounding needed, allocate extra space
4400 to ensure we have TRAMPOLINE_SIZE bytes left after rounding up. */
4401#ifdef TRAMPOLINE_ALIGNMENT
4402#define TRAMPOLINE_REAL_SIZE (TRAMPOLINE_SIZE + TRAMPOLINE_ALIGNMENT - 1)
4403#else
4404#define TRAMPOLINE_REAL_SIZE (TRAMPOLINE_SIZE)
4405#endif
4406 if (fp != 0)
4407 tramp = assign_outer_stack_local (BLKmode, TRAMPOLINE_REAL_SIZE, 0, fp);
4408 else
4409 tramp = assign_stack_local (BLKmode, TRAMPOLINE_REAL_SIZE, 0);
4410#endif
4411
4412 /* Record the trampoline for reuse and note it for later initialization
4413 by expand_function_end. */
4414 if (fp != 0)
4415 {
28498644
RK
4416 push_obstacks (fp->function_maybepermanent_obstack,
4417 fp->function_maybepermanent_obstack);
6f086dfc
RS
4418 rtlexp = make_node (RTL_EXPR);
4419 RTL_EXPR_RTL (rtlexp) = tramp;
4420 fp->trampoline_list = tree_cons (function, rtlexp, fp->trampoline_list);
4421 pop_obstacks ();
4422 }
4423 else
4424 {
4425 /* Make the RTL_EXPR node temporary, not momentary, so that the
4426 trampoline_list doesn't become garbage. */
4427 int momentary = suspend_momentary ();
4428 rtlexp = make_node (RTL_EXPR);
4429 resume_momentary (momentary);
4430
4431 RTL_EXPR_RTL (rtlexp) = tramp;
4432 trampoline_list = tree_cons (function, rtlexp, trampoline_list);
4433 }
4434
4435 tramp = fix_lexical_addr (XEXP (tramp, 0), function);
4436 return round_trampoline_addr (tramp);
4437}
4438
4439/* Given a trampoline address,
4440 round it to multiple of TRAMPOLINE_ALIGNMENT. */
4441
4442static rtx
4443round_trampoline_addr (tramp)
4444 rtx tramp;
4445{
4446#ifdef TRAMPOLINE_ALIGNMENT
4447 /* Round address up to desired boundary. */
4448 rtx temp = gen_reg_rtx (Pmode);
4449 temp = expand_binop (Pmode, add_optab, tramp,
5f4f0e22 4450 GEN_INT (TRAMPOLINE_ALIGNMENT - 1),
6f086dfc
RS
4451 temp, 0, OPTAB_LIB_WIDEN);
4452 tramp = expand_binop (Pmode, and_optab, temp,
5f4f0e22 4453 GEN_INT (- TRAMPOLINE_ALIGNMENT),
6f086dfc
RS
4454 temp, 0, OPTAB_LIB_WIDEN);
4455#endif
4456 return tramp;
4457}
4458\f
467456d0
RS
4459/* The functions identify_blocks and reorder_blocks provide a way to
4460 reorder the tree of BLOCK nodes, for optimizers that reshuffle or
4461 duplicate portions of the RTL code. Call identify_blocks before
4462 changing the RTL, and call reorder_blocks after. */
4463
467456d0
RS
4464/* Put all this function's BLOCK nodes into a vector, and return it.
4465 Also store in each NOTE for the beginning or end of a block
4466 the index of that block in the vector.
4467 The arguments are TOP_BLOCK, the top-level block of the function,
4468 and INSNS, the insn chain of the function. */
4469
4470tree *
4471identify_blocks (top_block, insns)
4472 tree top_block;
4473 rtx insns;
4474{
fc289cd1
JW
4475 int n_blocks;
4476 tree *block_vector;
4477 int *block_stack;
467456d0
RS
4478 int depth = 0;
4479 int next_block_number = 0;
4480 int current_block_number = 0;
4481 rtx insn;
4482
fc289cd1
JW
4483 if (top_block == 0)
4484 return 0;
4485
4486 n_blocks = all_blocks (top_block, 0);
4487 block_vector = (tree *) xmalloc (n_blocks * sizeof (tree));
4488 block_stack = (int *) alloca (n_blocks * sizeof (int));
4489
467456d0
RS
4490 all_blocks (top_block, block_vector);
4491
4492 for (insn = insns; insn; insn = NEXT_INSN (insn))
4493 if (GET_CODE (insn) == NOTE)
4494 {
4495 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
4496 {
4497 block_stack[depth++] = current_block_number;
4498 current_block_number = next_block_number;
1b2ac438 4499 NOTE_BLOCK_NUMBER (insn) = next_block_number++;
467456d0
RS
4500 }
4501 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
4502 {
4503 current_block_number = block_stack[--depth];
1b2ac438 4504 NOTE_BLOCK_NUMBER (insn) = current_block_number;
467456d0
RS
4505 }
4506 }
4507
4508 return block_vector;
4509}
4510
4511/* Given BLOCK_VECTOR which was returned by identify_blocks,
4512 and a revised instruction chain, rebuild the tree structure
4513 of BLOCK nodes to correspond to the new order of RTL.
fc289cd1 4514 The new block tree is inserted below TOP_BLOCK.
467456d0
RS
4515 Returns the current top-level block. */
4516
4517tree
fc289cd1 4518reorder_blocks (block_vector, top_block, insns)
467456d0 4519 tree *block_vector;
fc289cd1 4520 tree top_block;
467456d0
RS
4521 rtx insns;
4522{
fc289cd1 4523 tree current_block = top_block;
467456d0
RS
4524 rtx insn;
4525
fc289cd1
JW
4526 if (block_vector == 0)
4527 return top_block;
4528
4529 /* Prune the old tree away, so that it doesn't get in the way. */
4530 BLOCK_SUBBLOCKS (current_block) = 0;
4531
467456d0
RS
4532 for (insn = insns; insn; insn = NEXT_INSN (insn))
4533 if (GET_CODE (insn) == NOTE)
4534 {
4535 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
4536 {
4537 tree block = block_vector[NOTE_BLOCK_NUMBER (insn)];
4538 /* If we have seen this block before, copy it. */
4539 if (TREE_ASM_WRITTEN (block))
4540 block = copy_node (block);
fc289cd1 4541 BLOCK_SUBBLOCKS (block) = 0;
467456d0
RS
4542 TREE_ASM_WRITTEN (block) = 1;
4543 BLOCK_SUPERCONTEXT (block) = current_block;
4544 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4545 BLOCK_SUBBLOCKS (current_block) = block;
4546 current_block = block;
1b2ac438 4547 NOTE_SOURCE_FILE (insn) = 0;
467456d0
RS
4548 }
4549 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
4550 {
4551 BLOCK_SUBBLOCKS (current_block)
4552 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
4553 current_block = BLOCK_SUPERCONTEXT (current_block);
1b2ac438 4554 NOTE_SOURCE_FILE (insn) = 0;
467456d0
RS
4555 }
4556 }
4557
4558 return current_block;
4559}
4560
4561/* Reverse the order of elements in the chain T of blocks,
4562 and return the new head of the chain (old last element). */
4563
4564static tree
4565blocks_nreverse (t)
4566 tree t;
4567{
4568 register tree prev = 0, decl, next;
4569 for (decl = t; decl; decl = next)
4570 {
4571 next = BLOCK_CHAIN (decl);
4572 BLOCK_CHAIN (decl) = prev;
4573 prev = decl;
4574 }
4575 return prev;
4576}
4577
4578/* Count the subblocks of BLOCK, and list them all into the vector VECTOR.
4579 Also clear TREE_ASM_WRITTEN in all blocks. */
4580
4581static int
4582all_blocks (block, vector)
4583 tree block;
4584 tree *vector;
4585{
4586 int n_blocks = 1;
4587 tree subblocks;
4588
4589 TREE_ASM_WRITTEN (block) = 0;
4590 /* Record this block. */
fc289cd1
JW
4591 if (vector)
4592 vector[0] = block;
467456d0
RS
4593
4594 /* Record the subblocks, and their subblocks. */
4595 for (subblocks = BLOCK_SUBBLOCKS (block);
4596 subblocks; subblocks = BLOCK_CHAIN (subblocks))
fc289cd1 4597 n_blocks += all_blocks (subblocks, vector ? vector + n_blocks : 0);
467456d0
RS
4598
4599 return n_blocks;
4600}
4601\f
c20bf1f3 4602/* Build bytecode call descriptor for function SUBR. */
e15679f8 4603
c20bf1f3
JB
4604rtx
4605bc_build_calldesc (subr)
4606 tree subr;
4607{
4608 tree calldesc = 0, arg;
4609 int nargs = 0;
4610
4611 /* Build the argument description vector in reverse order. */
4612 DECL_ARGUMENTS (subr) = nreverse (DECL_ARGUMENTS (subr));
4613 nargs = 0;
4614
4615 for (arg = DECL_ARGUMENTS (subr); arg; arg = TREE_CHAIN (arg))
4616 {
4617 ++nargs;
4618
4619 calldesc = tree_cons ((tree) 0, size_in_bytes (TREE_TYPE (arg)), calldesc);
4620 calldesc = tree_cons ((tree) 0, bc_runtime_type_code (TREE_TYPE (arg)), calldesc);
4621 }
4622
4623 DECL_ARGUMENTS (subr) = nreverse (DECL_ARGUMENTS (subr));
4624
4625 /* Prepend the function's return type. */
4626 calldesc = tree_cons ((tree) 0,
4627 size_in_bytes (TREE_TYPE (TREE_TYPE (subr))),
4628 calldesc);
4629
4630 calldesc = tree_cons ((tree) 0,
4631 bc_runtime_type_code (TREE_TYPE (TREE_TYPE (subr))),
4632 calldesc);
4633
4634 /* Prepend the arg count. */
4635 calldesc = tree_cons ((tree) 0, build_int_2 (nargs, 0), calldesc);
4636
4637 /* Output the call description vector and get its address. */
4638 calldesc = build_nt (CONSTRUCTOR, (tree) 0, calldesc);
4639 TREE_TYPE (calldesc) = build_array_type (integer_type_node,
4640 build_index_type (build_int_2 (nargs * 2, 0)));
4641
4642 return output_constant_def (calldesc);
4643}
4644
4645
6f086dfc
RS
4646/* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4647 and initialize static variables for generating RTL for the statements
4648 of the function. */
4649
4650void
4651init_function_start (subr, filename, line)
4652 tree subr;
4653 char *filename;
4654 int line;
4655{
4656 char *junk;
4657
c20bf1f3
JB
4658 if (output_bytecode)
4659 {
4660 this_function_decl = subr;
4661 this_function_calldesc = bc_build_calldesc (subr);
4662 local_vars_size = 0;
4663 stack_depth = 0;
4664 max_stack_depth = 0;
4665 stmt_expr_depth = 0;
4666 return;
4667 }
4668
6f086dfc
RS
4669 init_stmt_for_function ();
4670
4671 cse_not_expected = ! optimize;
4672
4673 /* Caller save not needed yet. */
4674 caller_save_needed = 0;
4675
4676 /* No stack slots have been made yet. */
4677 stack_slot_list = 0;
4678
4679 /* There is no stack slot for handling nonlocal gotos. */
4680 nonlocal_goto_handler_slot = 0;
4681 nonlocal_goto_stack_level = 0;
4682
4683 /* No labels have been declared for nonlocal use. */
4684 nonlocal_labels = 0;
4685
4686 /* No function calls so far in this function. */
4687 function_call_count = 0;
4688
4689 /* No parm regs have been allocated.
4690 (This is important for output_inline_function.) */
4691 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
4692
4693 /* Initialize the RTL mechanism. */
4694 init_emit ();
4695
4696 /* Initialize the queue of pending postincrement and postdecrements,
4697 and some other info in expr.c. */
4698 init_expr ();
4699
4700 /* We haven't done register allocation yet. */
4701 reg_renumber = 0;
4702
4703 init_const_rtx_hash_table ();
4704
4705 current_function_name = (*decl_printable_name) (subr, &junk);
4706
4707 /* Nonzero if this is a nested function that uses a static chain. */
4708
4709 current_function_needs_context
4710 = (decl_function_context (current_function_decl) != 0);
4711
4712 /* Set if a call to setjmp is seen. */
4713 current_function_calls_setjmp = 0;
4714
4715 /* Set if a call to longjmp is seen. */
4716 current_function_calls_longjmp = 0;
4717
4718 current_function_calls_alloca = 0;
4719 current_function_has_nonlocal_label = 0;
8634413a 4720 current_function_has_nonlocal_goto = 0;
6f086dfc
RS
4721 current_function_contains_functions = 0;
4722
4723 current_function_returns_pcc_struct = 0;
4724 current_function_returns_struct = 0;
4725 current_function_epilogue_delay_list = 0;
4726 current_function_uses_const_pool = 0;
4727 current_function_uses_pic_offset_table = 0;
4728
4729 /* We have not yet needed to make a label to jump to for tail-recursion. */
4730 tail_recursion_label = 0;
4731
4732 /* We haven't had a need to make a save area for ap yet. */
4733
4734 arg_pointer_save_area = 0;
4735
4736 /* No stack slots allocated yet. */
4737 frame_offset = 0;
4738
4739 /* No SAVE_EXPRs in this function yet. */
4740 save_expr_regs = 0;
4741
4742 /* No RTL_EXPRs in this function yet. */
4743 rtl_expr_chain = 0;
4744
4745 /* We have not allocated any temporaries yet. */
4746 temp_slots = 0;
4747 temp_slot_level = 0;
d93d4205 4748 target_temp_slot_level = 0;
6f086dfc
RS
4749
4750 /* Within function body, compute a type's size as soon it is laid out. */
4751 immediate_size_expand++;
4752
d9a98e1a
RK
4753 /* We haven't made any trampolines for this function yet. */
4754 trampoline_list = 0;
4755
6f086dfc
RS
4756 init_pending_stack_adjust ();
4757 inhibit_defer_pop = 0;
4758
4759 current_function_outgoing_args_size = 0;
4760
4761 /* Initialize the insn lengths. */
4762 init_insn_lengths ();
4763
4764 /* Prevent ever trying to delete the first instruction of a function.
4765 Also tell final how to output a linenum before the function prologue. */
4766 emit_line_note (filename, line);
4767
4768 /* Make sure first insn is a note even if we don't want linenums.
4769 This makes sure the first insn will never be deleted.
4770 Also, final expects a note to appear there. */
5f4f0e22 4771 emit_note (NULL_PTR, NOTE_INSN_DELETED);
6f086dfc
RS
4772
4773 /* Set flags used by final.c. */
4774 if (aggregate_value_p (DECL_RESULT (subr)))
4775 {
4776#ifdef PCC_STATIC_STRUCT_RETURN
1b8297c1 4777 current_function_returns_pcc_struct = 1;
6f086dfc 4778#endif
1b8297c1 4779 current_function_returns_struct = 1;
6f086dfc
RS
4780 }
4781
4782 /* Warn if this value is an aggregate type,
4783 regardless of which calling convention we are using for it. */
4784 if (warn_aggregate_return
05e3bdb9 4785 && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
6f086dfc
RS
4786 warning ("function returns an aggregate");
4787
4788 current_function_returns_pointer
8eda074c 4789 = POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
6f086dfc
RS
4790
4791 /* Indicate that we need to distinguish between the return value of the
4792 present function and the return value of a function being called. */
4793 rtx_equal_function_value_matters = 1;
4794
4795 /* Indicate that we have not instantiated virtual registers yet. */
4796 virtuals_instantiated = 0;
4797
4798 /* Indicate we have no need of a frame pointer yet. */
4799 frame_pointer_needed = 0;
4800
4801 /* By default assume not varargs. */
4802 current_function_varargs = 0;
4803}
4804
4805/* Indicate that the current function uses extra args
4806 not explicitly mentioned in the argument list in any fashion. */
4807
4808void
4809mark_varargs ()
4810{
4811 current_function_varargs = 1;
4812}
4813
4814/* Expand a call to __main at the beginning of a possible main function. */
4815
e2fd1d94
JM
4816#if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
4817#undef HAS_INIT_SECTION
4818#define HAS_INIT_SECTION
4819#endif
4820
6f086dfc
RS
4821void
4822expand_main_function ()
4823{
c20bf1f3
JB
4824 if (!output_bytecode)
4825 {
4826 /* The zero below avoids a possible parse error */
4827 0;
e2fd1d94 4828#if !defined (HAS_INIT_SECTION)
ae0d8288 4829 emit_library_call (gen_rtx (SYMBOL_REF, Pmode, NAME__MAIN), 0,
c20bf1f3 4830 VOIDmode, 0);
e2fd1d94 4831#endif /* not HAS_INIT_SECTION */
c20bf1f3 4832 }
6f086dfc
RS
4833}
4834\f
c20bf1f3
JB
4835extern struct obstack permanent_obstack;
4836
4837/* Expand start of bytecode function. See comment at
4838 expand_function_start below for details. */
4839
4840void
4841bc_expand_function_start (subr, parms_have_cleanups)
4842 tree subr;
4843 int parms_have_cleanups;
4844{
4845 char label[20], *name;
4846 static int nlab;
4847 tree thisarg;
4848 int argsz;
4849
4850 if (TREE_PUBLIC (subr))
4851 bc_globalize_label (IDENTIFIER_POINTER (DECL_NAME (subr)));
4852
4853#ifdef DEBUG_PRINT_CODE
4854 fprintf (stderr, "\n<func %s>\n", IDENTIFIER_POINTER (DECL_NAME (subr)));
4855#endif
4856
4857 for (argsz = 0, thisarg = DECL_ARGUMENTS (subr); thisarg; thisarg = TREE_CHAIN (thisarg))
4858 {
4859 if (DECL_RTL (thisarg))
4860 abort (); /* Should be NULL here I think. */
4861 else if (TREE_CONSTANT (DECL_SIZE (thisarg)))
4862 {
4863 DECL_RTL (thisarg) = bc_gen_rtx ((char *) 0, argsz, (struct bc_label *) 0);
4864 argsz += TREE_INT_CST_LOW (DECL_SIZE (thisarg));
4865 }
4866 else
4867 {
4868 /* Variable-sized objects are pointers to their storage. */
4869 DECL_RTL (thisarg) = bc_gen_rtx ((char *) 0, argsz, (struct bc_label *) 0);
4870 argsz += POINTER_SIZE;
4871 }
4872 }
4873
4874 bc_begin_function (bc_xstrdup (IDENTIFIER_POINTER (DECL_NAME (subr))));
4875
4876 ASM_GENERATE_INTERNAL_LABEL (label, "LX", nlab);
4877
4878 ++nlab;
4879 name = (char *) obstack_copy0 (&permanent_obstack, label, strlen (label));
4880 this_function_callinfo = bc_gen_rtx (name, 0, (struct bc_label *) 0);
e7a42772
JB
4881 this_function_bytecode =
4882 bc_emit_trampoline (BYTECODE_LABEL (this_function_callinfo));
c20bf1f3
JB
4883}
4884
4885
4886/* Expand end of bytecode function. See details the comment of
4887 expand_function_end(), below. */
4888
4889void
4890bc_expand_function_end ()
4891{
4892 char *ptrconsts;
4893
4894 expand_null_return ();
4895
4896 /* Emit any fixup code. This must be done before the call to
4897 to BC_END_FUNCTION (), since that will cause the bytecode
4898 segment to be finished off and closed. */
4899
e15679f8 4900 expand_fixups (NULL_RTX);
c20bf1f3
JB
4901
4902 ptrconsts = bc_end_function ();
4903
4904 bc_align_const (2 /* INT_ALIGN */);
4905
4906 /* If this changes also make sure to change bc-interp.h! */
4907
e7a42772 4908 bc_emit_const_labeldef (BYTECODE_LABEL (this_function_callinfo));
c20bf1f3
JB
4909 bc_emit_const ((char *) &max_stack_depth, sizeof max_stack_depth);
4910 bc_emit_const ((char *) &local_vars_size, sizeof local_vars_size);
4911 bc_emit_const_labelref (this_function_bytecode, 0);
4912 bc_emit_const_labelref (ptrconsts, 0);
e7a42772 4913 bc_emit_const_labelref (BYTECODE_LABEL (this_function_calldesc), 0);
c20bf1f3
JB
4914}
4915
4916
6f086dfc
RS
4917/* Start the RTL for a new function, and set variables used for
4918 emitting RTL.
4919 SUBR is the FUNCTION_DECL node.
4920 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
4921 the function's parameters, which must be run at any return statement. */
4922
4923void
4924expand_function_start (subr, parms_have_cleanups)
4925 tree subr;
4926 int parms_have_cleanups;
4927{
4928 register int i;
4929 tree tem;
4930 rtx last_ptr;
4931
c20bf1f3
JB
4932 if (output_bytecode)
4933 {
4934 bc_expand_function_start (subr, parms_have_cleanups);
4935 return;
4936 }
4937
6f086dfc
RS
4938 /* Make sure volatile mem refs aren't considered
4939 valid operands of arithmetic insns. */
4940 init_recog_no_volatile ();
4941
4942 /* If function gets a static chain arg, store it in the stack frame.
4943 Do this first, so it gets the first stack slot offset. */
4944 if (current_function_needs_context)
3e2481e9
JW
4945 {
4946 last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
f0c51a1e
RK
4947
4948#ifdef SMALL_REGISTER_CLASSES
4949 /* Delay copying static chain if it is not a register to avoid
4950 conflicts with regs used for parameters. */
4951 if (GET_CODE (static_chain_incoming_rtx) == REG)
4952#endif
4953 emit_move_insn (last_ptr, static_chain_incoming_rtx);
3e2481e9 4954 }
6f086dfc
RS
4955
4956 /* If the parameters of this function need cleaning up, get a label
4957 for the beginning of the code which executes those cleanups. This must
4958 be done before doing anything with return_label. */
4959 if (parms_have_cleanups)
4960 cleanup_label = gen_label_rtx ();
4961 else
4962 cleanup_label = 0;
4963
4964 /* Make the label for return statements to jump to, if this machine
4965 does not have a one-instruction return and uses an epilogue,
4966 or if it returns a structure, or if it has parm cleanups. */
4967#ifdef HAVE_return
4968 if (cleanup_label == 0 && HAVE_return
4969 && ! current_function_returns_pcc_struct
4970 && ! (current_function_returns_struct && ! optimize))
4971 return_label = 0;
4972 else
4973 return_label = gen_label_rtx ();
4974#else
4975 return_label = gen_label_rtx ();
4976#endif
4977
4978 /* Initialize rtx used to return the value. */
4979 /* Do this before assign_parms so that we copy the struct value address
4980 before any library calls that assign parms might generate. */
4981
4982 /* Decide whether to return the value in memory or in a register. */
4983 if (aggregate_value_p (DECL_RESULT (subr)))
4984 {
4985 /* Returning something that won't go in a register. */
4acc00bf 4986 register rtx value_address = 0;
6f086dfc
RS
4987
4988#ifdef PCC_STATIC_STRUCT_RETURN
4989 if (current_function_returns_pcc_struct)
4990 {
4991 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
4992 value_address = assemble_static_space (size);
4993 }
4994 else
4995#endif
4996 {
4997 /* Expect to be passed the address of a place to store the value.
4998 If it is passed as an argument, assign_parms will take care of
4999 it. */
5000 if (struct_value_incoming_rtx)
5001 {
5002 value_address = gen_reg_rtx (Pmode);
5003 emit_move_insn (value_address, struct_value_incoming_rtx);
5004 }
5005 }
5006 if (value_address)
ccdecf58
RK
5007 {
5008 DECL_RTL (DECL_RESULT (subr))
5009 = gen_rtx (MEM, DECL_MODE (DECL_RESULT (subr)), value_address);
5010 MEM_IN_STRUCT_P (DECL_RTL (DECL_RESULT (subr)))
05e3bdb9 5011 = AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
ccdecf58 5012 }
6f086dfc
RS
5013 }
5014 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
5015 /* If return mode is void, this decl rtl should not be used. */
5016 DECL_RTL (DECL_RESULT (subr)) = 0;
5017 else if (parms_have_cleanups)
a53e14c0
RK
5018 {
5019 /* If function will end with cleanup code for parms,
5020 compute the return values into a pseudo reg,
5021 which we will copy into the true return register
5022 after the cleanups are done. */
5023
5024 enum machine_mode mode = DECL_MODE (DECL_RESULT (subr));
a5a52dbc 5025
a53e14c0
RK
5026#ifdef PROMOTE_FUNCTION_RETURN
5027 tree type = TREE_TYPE (DECL_RESULT (subr));
5028 int unsignedp = TREE_UNSIGNED (type);
5029
a5a52dbc 5030 mode = promote_mode (type, mode, &unsignedp, 1);
a53e14c0
RK
5031#endif
5032
5033 DECL_RTL (DECL_RESULT (subr)) = gen_reg_rtx (mode);
5034 }
6f086dfc
RS
5035 else
5036 /* Scalar, returned in a register. */
5037 {
5038#ifdef FUNCTION_OUTGOING_VALUE
5039 DECL_RTL (DECL_RESULT (subr))
5040 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (subr)), subr);
5041#else
5042 DECL_RTL (DECL_RESULT (subr))
5043 = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (subr)), subr);
5044#endif
5045
5046 /* Mark this reg as the function's return value. */
5047 if (GET_CODE (DECL_RTL (DECL_RESULT (subr))) == REG)
5048 {
5049 REG_FUNCTION_VALUE_P (DECL_RTL (DECL_RESULT (subr))) = 1;
5050 /* Needed because we may need to move this to memory
5051 in case it's a named return value whose address is taken. */
a82ad570 5052 DECL_REGISTER (DECL_RESULT (subr)) = 1;
6f086dfc
RS
5053 }
5054 }
5055
5056 /* Initialize rtx for parameters and local variables.
5057 In some cases this requires emitting insns. */
5058
5059 assign_parms (subr, 0);
5060
f0c51a1e
RK
5061#ifdef SMALL_REGISTER_CLASSES
5062 /* Copy the static chain now if it wasn't a register. The delay is to
5063 avoid conflicts with the parameter passing registers. */
5064
5065 if (current_function_needs_context)
5066 if (GET_CODE (static_chain_incoming_rtx) != REG)
5067 emit_move_insn (last_ptr, static_chain_incoming_rtx);
5068#endif
5069
6f086dfc
RS
5070 /* The following was moved from init_function_start.
5071 The move is supposed to make sdb output more accurate. */
5072 /* Indicate the beginning of the function body,
5073 as opposed to parm setup. */
5f4f0e22 5074 emit_note (NULL_PTR, NOTE_INSN_FUNCTION_BEG);
6f086dfc
RS
5075
5076 /* If doing stupid allocation, mark parms as born here. */
5077
5078 if (GET_CODE (get_last_insn ()) != NOTE)
5f4f0e22 5079 emit_note (NULL_PTR, NOTE_INSN_DELETED);
6f086dfc
RS
5080 parm_birth_insn = get_last_insn ();
5081
5082 if (obey_regdecls)
5083 {
5084 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_parm_reg; i++)
5085 use_variable (regno_reg_rtx[i]);
5086
5087 if (current_function_internal_arg_pointer != virtual_incoming_args_rtx)
5088 use_variable (current_function_internal_arg_pointer);
5089 }
5090
5091 /* Fetch static chain values for containing functions. */
5092 tem = decl_function_context (current_function_decl);
ac9e20f0
RK
5093 /* If not doing stupid register allocation copy the static chain
5094 pointer into a psuedo. If we have small register classes, copy the
5095 value from memory if static_chain_incoming_rtx is a REG. If we do
5096 stupid register allocation, we use the stack address generated above. */
3e2481e9 5097 if (tem && ! obey_regdecls)
ac9e20f0
RK
5098 {
5099#ifdef SMALL_REGISTER_CLASSES
426749e8
RK
5100 /* If the static chain originally came in a register, put it back
5101 there, then move it out in the next insn. The reason for
5102 this peculiar code is to satisfy function integration. */
ac9e20f0 5103 if (GET_CODE (static_chain_incoming_rtx) == REG)
426749e8 5104 emit_move_insn (static_chain_incoming_rtx, last_ptr);
ac9e20f0 5105#endif
426749e8
RK
5106
5107 last_ptr = copy_to_reg (static_chain_incoming_rtx);
ac9e20f0
RK
5108 }
5109
6f086dfc
RS
5110 context_display = 0;
5111 while (tem)
5112 {
5113 tree rtlexp = make_node (RTL_EXPR);
5114
5115 RTL_EXPR_RTL (rtlexp) = last_ptr;
5116 context_display = tree_cons (tem, rtlexp, context_display);
5117 tem = decl_function_context (tem);
5118 if (tem == 0)
5119 break;
5120 /* Chain thru stack frames, assuming pointer to next lexical frame
5121 is found at the place we always store it. */
5122#ifdef FRAME_GROWS_DOWNWARD
5123 last_ptr = plus_constant (last_ptr, - GET_MODE_SIZE (Pmode));
5124#endif
5125 last_ptr = copy_to_reg (gen_rtx (MEM, Pmode,
5126 memory_address (Pmode, last_ptr)));
e5884dac
RK
5127
5128 /* If we are not optimizing, ensure that we know that this
5129 piece of context is live over the entire function. */
5130 if (! optimize)
5131 save_expr_regs = gen_rtx (EXPR_LIST, VOIDmode, last_ptr,
5132 save_expr_regs);
6f086dfc
RS
5133 }
5134
5135 /* After the display initializations is where the tail-recursion label
5136 should go, if we end up needing one. Ensure we have a NOTE here
5137 since some things (like trampolines) get placed before this. */
5f4f0e22 5138 tail_recursion_reentry = emit_note (NULL_PTR, NOTE_INSN_DELETED);
6f086dfc
RS
5139
5140 /* Evaluate now the sizes of any types declared among the arguments. */
5141 for (tem = nreverse (get_pending_sizes ()); tem; tem = TREE_CHAIN (tem))
4752d3bc 5142 expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode, 0);
6f086dfc
RS
5143
5144 /* Make sure there is a line number after the function entry setup code. */
5145 force_next_line_note ();
5146}
5147\f
5148/* Generate RTL for the end of the current function.
980697fd 5149 FILENAME and LINE are the current position in the source file.
6f086dfc 5150
980697fd 5151 It is up to language-specific callers to do cleanups for parameters--
1be07046 5152 or else, supply 1 for END_BINDINGS and we will call expand_end_bindings. */
6f086dfc
RS
5153
5154void
1be07046 5155expand_function_end (filename, line, end_bindings)
6f086dfc
RS
5156 char *filename;
5157 int line;
1be07046 5158 int end_bindings;
6f086dfc
RS
5159{
5160 register int i;
5161 tree link;
5162
5163 static rtx initial_trampoline;
5164
c20bf1f3
JB
5165 if (output_bytecode)
5166 {
5167 bc_expand_function_end ();
5168 return;
5169 }
5170
6f086dfc
RS
5171#ifdef NON_SAVING_SETJMP
5172 /* Don't put any variables in registers if we call setjmp
5173 on a machine that fails to restore the registers. */
5174 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
5175 {
b88a3142
RK
5176 if (DECL_INITIAL (current_function_decl) != error_mark_node)
5177 setjmp_protect (DECL_INITIAL (current_function_decl));
5178
6f086dfc
RS
5179 setjmp_protect_args ();
5180 }
5181#endif
5182
5183 /* Save the argument pointer if a save area was made for it. */
5184 if (arg_pointer_save_area)
5185 {
5186 rtx x = gen_move_insn (arg_pointer_save_area, virtual_incoming_args_rtx);
5187 emit_insn_before (x, tail_recursion_reentry);
5188 }
5189
5190 /* Initialize any trampolines required by this function. */
5191 for (link = trampoline_list; link; link = TREE_CHAIN (link))
5192 {
5193 tree function = TREE_PURPOSE (link);
5194 rtx context = lookup_static_chain (function);
5195 rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
5196 rtx seq;
5197
5198 /* First make sure this compilation has a template for
5199 initializing trampolines. */
5200 if (initial_trampoline == 0)
86f8eff3
RK
5201 {
5202 end_temporary_allocation ();
5203 initial_trampoline
5204 = gen_rtx (MEM, BLKmode, assemble_trampoline_template ());
5205 resume_temporary_allocation ();
5206 }
6f086dfc
RS
5207
5208 /* Generate insns to initialize the trampoline. */
5209 start_sequence ();
5210 tramp = change_address (initial_trampoline, BLKmode,
5211 round_trampoline_addr (XEXP (tramp, 0)));
5f4f0e22 5212 emit_block_move (tramp, initial_trampoline, GEN_INT (TRAMPOLINE_SIZE),
6f086dfc
RS
5213 FUNCTION_BOUNDARY / BITS_PER_UNIT);
5214 INITIALIZE_TRAMPOLINE (XEXP (tramp, 0),
5215 XEXP (DECL_RTL (function), 0), context);
5216 seq = get_insns ();
5217 end_sequence ();
5218
5219 /* Put those insns at entry to the containing function (this one). */
5220 emit_insns_before (seq, tail_recursion_reentry);
5221 }
6f086dfc
RS
5222
5223#if 0 /* I think unused parms are legitimate enough. */
5224 /* Warn about unused parms. */
5225 if (warn_unused)
5226 {
5227 rtx decl;
5228
5229 for (decl = DECL_ARGUMENTS (current_function_decl);
5230 decl; decl = TREE_CHAIN (decl))
5231 if (! TREE_USED (decl) && TREE_CODE (decl) == VAR_DECL)
5232 warning_with_decl (decl, "unused parameter `%s'");
5233 }
5234#endif
5235
5236 /* Delete handlers for nonlocal gotos if nothing uses them. */
5237 if (nonlocal_goto_handler_slot != 0 && !current_function_has_nonlocal_label)
5238 delete_handlers ();
5239
5240 /* End any sequences that failed to be closed due to syntax errors. */
5241 while (in_sequence_p ())
5f4f0e22 5242 end_sequence ();
6f086dfc
RS
5243
5244 /* Outside function body, can't compute type's actual size
5245 until next function's body starts. */
5246 immediate_size_expand--;
5247
5248 /* If doing stupid register allocation,
5249 mark register parms as dying here. */
5250
5251 if (obey_regdecls)
5252 {
5253 rtx tem;
5254 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_parm_reg; i++)
5255 use_variable (regno_reg_rtx[i]);
5256
5257 /* Likewise for the regs of all the SAVE_EXPRs in the function. */
5258
5259 for (tem = save_expr_regs; tem; tem = XEXP (tem, 1))
5260 {
5261 use_variable (XEXP (tem, 0));
5262 use_variable_after (XEXP (tem, 0), parm_birth_insn);
5263 }
5264
5265 if (current_function_internal_arg_pointer != virtual_incoming_args_rtx)
5266 use_variable (current_function_internal_arg_pointer);
5267 }
5268
5269 clear_pending_stack_adjust ();
5270 do_pending_stack_adjust ();
5271
5272 /* Mark the end of the function body.
5273 If control reaches this insn, the function can drop through
5274 without returning a value. */
5f4f0e22 5275 emit_note (NULL_PTR, NOTE_INSN_FUNCTION_END);
6f086dfc
RS
5276
5277 /* Output a linenumber for the end of the function.
5278 SDB depends on this. */
5279 emit_line_note_force (filename, line);
5280
5281 /* Output the label for the actual return from the function,
5282 if one is expected. This happens either because a function epilogue
5283 is used instead of a return instruction, or because a return was done
5284 with a goto in order to run local cleanups, or because of pcc-style
5285 structure returning. */
5286
5287 if (return_label)
5288 emit_label (return_label);
5289
1be07046
RS
5290 /* C++ uses this. */
5291 if (end_bindings)
5292 expand_end_bindings (0, 0, 0);
5293
6f086dfc
RS
5294 /* If we had calls to alloca, and this machine needs
5295 an accurate stack pointer to exit the function,
5296 insert some code to save and restore the stack pointer. */
5297#ifdef EXIT_IGNORE_STACK
5298 if (! EXIT_IGNORE_STACK)
5299#endif
5300 if (current_function_calls_alloca)
5301 {
59257ff7
RK
5302 rtx tem = 0;
5303
5304 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
5f4f0e22 5305 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
6f086dfc
RS
5306 }
5307
5308 /* If scalar return value was computed in a pseudo-reg,
5309 copy that to the hard return register. */
5310 if (DECL_RTL (DECL_RESULT (current_function_decl)) != 0
5311 && GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) == REG
5312 && (REGNO (DECL_RTL (DECL_RESULT (current_function_decl)))
5313 >= FIRST_PSEUDO_REGISTER))
5314 {
5315 rtx real_decl_result;
5316
5317#ifdef FUNCTION_OUTGOING_VALUE
5318 real_decl_result
5319 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (current_function_decl)),
5320 current_function_decl);
5321#else
5322 real_decl_result
5323 = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (current_function_decl)),
5324 current_function_decl);
5325#endif
5326 REG_FUNCTION_VALUE_P (real_decl_result) = 1;
5327 emit_move_insn (real_decl_result,
5328 DECL_RTL (DECL_RESULT (current_function_decl)));
5329 emit_insn (gen_rtx (USE, VOIDmode, real_decl_result));
5330 }
5331
5332 /* If returning a structure, arrange to return the address of the value
5333 in a place where debuggers expect to find it.
5334
5335 If returning a structure PCC style,
5336 the caller also depends on this value.
5337 And current_function_returns_pcc_struct is not necessarily set. */
5338 if (current_function_returns_struct
5339 || current_function_returns_pcc_struct)
5340 {
5341 rtx value_address = XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5342 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5343#ifdef FUNCTION_OUTGOING_VALUE
5344 rtx outgoing
5345 = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
5346 current_function_decl);
5347#else
5348 rtx outgoing
5349 = FUNCTION_VALUE (build_pointer_type (type),
5350 current_function_decl);
5351#endif
5352
5353 /* Mark this as a function return value so integrate will delete the
5354 assignment and USE below when inlining this function. */
5355 REG_FUNCTION_VALUE_P (outgoing) = 1;
5356
5357 emit_move_insn (outgoing, value_address);
5358 use_variable (outgoing);
5359 }
5360
5361 /* Output a return insn if we are using one.
5362 Otherwise, let the rtl chain end here, to drop through
5363 into the epilogue. */
5364
5365#ifdef HAVE_return
5366 if (HAVE_return)
5367 {
5368 emit_jump_insn (gen_return ());
5369 emit_barrier ();
5370 }
5371#endif
5372
5373 /* Fix up any gotos that jumped out to the outermost
5374 binding level of the function.
5375 Must follow emitting RETURN_LABEL. */
5376
5377 /* If you have any cleanups to do at this point,
5378 and they need to create temporary variables,
5379 then you will lose. */
e15679f8 5380 expand_fixups (get_insns ());
6f086dfc 5381}
bdac5f58
TW
5382\f
5383/* These arrays record the INSN_UIDs of the prologue and epilogue insns. */
5384
5385static int *prologue;
5386static int *epilogue;
5387
5388/* Create an array that records the INSN_UIDs of INSNS (either a sequence
5389 or a single insn). */
5390
5391static int *
5392record_insns (insns)
5393 rtx insns;
5394{
5395 int *vec;
5396
5397 if (GET_CODE (insns) == SEQUENCE)
5398 {
5399 int len = XVECLEN (insns, 0);
5400 vec = (int *) oballoc ((len + 1) * sizeof (int));
5401 vec[len] = 0;
5402 while (--len >= 0)
5403 vec[len] = INSN_UID (XVECEXP (insns, 0, len));
5404 }
5405 else
5406 {
5407 vec = (int *) oballoc (2 * sizeof (int));
5408 vec[0] = INSN_UID (insns);
5409 vec[1] = 0;
5410 }
5411 return vec;
5412}
5413
10914065 5414/* Determine how many INSN_UIDs in VEC are part of INSN. */
bdac5f58 5415
10914065 5416static int
bdac5f58
TW
5417contains (insn, vec)
5418 rtx insn;
5419 int *vec;
5420{
5421 register int i, j;
5422
5423 if (GET_CODE (insn) == INSN
5424 && GET_CODE (PATTERN (insn)) == SEQUENCE)
5425 {
10914065 5426 int count = 0;
bdac5f58
TW
5427 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
5428 for (j = 0; vec[j]; j++)
5429 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == vec[j])
10914065
TW
5430 count++;
5431 return count;
bdac5f58
TW
5432 }
5433 else
5434 {
5435 for (j = 0; vec[j]; j++)
5436 if (INSN_UID (insn) == vec[j])
10914065 5437 return 1;
bdac5f58
TW
5438 }
5439 return 0;
5440}
5441
5442/* Generate the prologe and epilogue RTL if the machine supports it. Thread
5443 this into place with notes indicating where the prologue ends and where
5444 the epilogue begins. Update the basic block information when possible. */
5445
5446void
5447thread_prologue_and_epilogue_insns (f)
5448 rtx f;
5449{
5450#ifdef HAVE_prologue
5451 if (HAVE_prologue)
5452 {
5453 rtx head, seq, insn;
5454
5455 /* The first insn (a NOTE_INSN_DELETED) is followed by zero or more
5456 prologue insns and a NOTE_INSN_PROLOGUE_END. */
5457 emit_note_after (NOTE_INSN_PROLOGUE_END, f);
5458 seq = gen_prologue ();
5459 head = emit_insn_after (seq, f);
5460
5461 /* Include the new prologue insns in the first block. Ignore them
5462 if they form a basic block unto themselves. */
5463 if (basic_block_head && n_basic_blocks
5464 && GET_CODE (basic_block_head[0]) != CODE_LABEL)
5465 basic_block_head[0] = NEXT_INSN (f);
5466
5467 /* Retain a map of the prologue insns. */
5468 prologue = record_insns (GET_CODE (seq) == SEQUENCE ? seq : head);
5469 }
5470 else
5471#endif
5472 prologue = 0;
5473
5474#ifdef HAVE_epilogue
5475 if (HAVE_epilogue)
5476 {
5477 rtx insn = get_last_insn ();
5478 rtx prev = prev_nonnote_insn (insn);
5479
5480 /* If we end with a BARRIER, we don't need an epilogue. */
5481 if (! (prev && GET_CODE (prev) == BARRIER))
5482 {
a78bdb38
JW
5483 rtx tail, seq, tem;
5484 rtx first_use = 0;
5485 rtx last_use = 0;
bdac5f58 5486
a78bdb38
JW
5487 /* The last basic block ends with a NOTE_INSN_EPILOGUE_BEG, the
5488 epilogue insns, the USE insns at the end of a function,
5489 the jump insn that returns, and then a BARRIER. */
bdac5f58 5490
a78bdb38 5491 /* Move the USE insns at the end of a function onto a list. */
bdac5f58
TW
5492 while (prev
5493 && GET_CODE (prev) == INSN
5494 && GET_CODE (PATTERN (prev)) == USE)
5495 {
a78bdb38 5496 tem = prev;
bdac5f58 5497 prev = prev_nonnote_insn (prev);
a78bdb38
JW
5498
5499 NEXT_INSN (PREV_INSN (tem)) = NEXT_INSN (tem);
5500 PREV_INSN (NEXT_INSN (tem)) = PREV_INSN (tem);
83eb3b0d
RK
5501 if (first_use)
5502 {
5503 NEXT_INSN (tem) = first_use;
5504 PREV_INSN (first_use) = tem;
5505 }
5506 first_use = tem;
5507 if (!last_use)
a78bdb38 5508 last_use = tem;
bdac5f58
TW
5509 }
5510
a78bdb38
JW
5511 emit_barrier_after (insn);
5512
bdac5f58
TW
5513 seq = gen_epilogue ();
5514 tail = emit_jump_insn_after (seq, insn);
a78bdb38
JW
5515
5516 /* Insert the USE insns immediately before the return insn, which
5517 must be the first instruction before the final barrier. */
5518 if (first_use)
5519 {
5520 tem = prev_nonnote_insn (get_last_insn ());
5521 NEXT_INSN (PREV_INSN (tem)) = first_use;
5522 PREV_INSN (first_use) = PREV_INSN (tem);
5523 PREV_INSN (tem) = last_use;
5524 NEXT_INSN (last_use) = tem;
5525 }
5526
bdac5f58
TW
5527 emit_note_after (NOTE_INSN_EPILOGUE_BEG, insn);
5528
5529 /* Include the new epilogue insns in the last block. Ignore
5530 them if they form a basic block unto themselves. */
5531 if (basic_block_end && n_basic_blocks
5532 && GET_CODE (basic_block_end[n_basic_blocks - 1]) != JUMP_INSN)
5533 basic_block_end[n_basic_blocks - 1] = tail;
5534
5535 /* Retain a map of the epilogue insns. */
5536 epilogue = record_insns (GET_CODE (seq) == SEQUENCE ? seq : tail);
5537 return;
5538 }
5539 }
5540#endif
5541 epilogue = 0;
5542}
5543
5544/* Reposition the prologue-end and epilogue-begin notes after instruction
5545 scheduling and delayed branch scheduling. */
5546
5547void
5548reposition_prologue_and_epilogue_notes (f)
5549 rtx f;
5550{
5551#if defined (HAVE_prologue) || defined (HAVE_epilogue)
5552 /* Reposition the prologue and epilogue notes. */
5553 if (n_basic_blocks)
5554 {
5555 rtx next, prev;
bf526252 5556 int len;
bdac5f58
TW
5557
5558 if (prologue)
5559 {
bf526252
RK
5560 register rtx insn, note = 0;
5561
5562 /* Scan from the beginning until we reach the last prologue insn.
5563 We apparently can't depend on basic_block_{head,end} after
5564 reorg has run. */
5565 for (len = 0; prologue[len]; len++)
5566 ;
9392c110
JH
5567 for (insn = f; len && insn; insn = NEXT_INSN (insn))
5568 {
5569 if (GET_CODE (insn) == NOTE)
5570 {
5571 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
5572 note = insn;
5573 }
5574 else if ((len -= contains (insn, prologue)) == 0)
5575 {
5576 /* Find the prologue-end note if we haven't already, and
5577 move it to just after the last prologue insn. */
5578 if (note == 0)
5579 {
5580 for (note = insn; note = NEXT_INSN (note);)
5581 if (GET_CODE (note) == NOTE
5582 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
5583 break;
5584 }
5585 next = NEXT_INSN (note);
5586 prev = PREV_INSN (note);
5587 if (prev)
5588 NEXT_INSN (prev) = next;
5589 if (next)
5590 PREV_INSN (next) = prev;
5591 add_insn_after (note, insn);
5592 }
5593 }
bdac5f58
TW
5594 }
5595
5596 if (epilogue)
5597 {
bf526252
RK
5598 register rtx insn, note = 0;
5599
5600 /* Scan from the end until we reach the first epilogue insn.
5601 We apparently can't depend on basic_block_{head,end} after
5602 reorg has run. */
5603 for (len = 0; epilogue[len]; len++)
5604 ;
9392c110
JH
5605 for (insn = get_last_insn (); len && insn; insn = PREV_INSN (insn))
5606 {
5607 if (GET_CODE (insn) == NOTE)
5608 {
5609 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
5610 note = insn;
5611 }
5612 else if ((len -= contains (insn, epilogue)) == 0)
5613 {
5614 /* Find the epilogue-begin note if we haven't already, and
5615 move it to just before the first epilogue insn. */
5616 if (note == 0)
5617 {
5618 for (note = insn; note = PREV_INSN (note);)
5619 if (GET_CODE (note) == NOTE
5620 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
5621 break;
5622 }
5623 next = NEXT_INSN (note);
5624 prev = PREV_INSN (note);
5625 if (prev)
5626 NEXT_INSN (prev) = next;
5627 if (next)
5628 PREV_INSN (next) = prev;
5629 add_insn_after (note, PREV_INSN (insn));
5630 }
5631 }
bdac5f58
TW
5632 }
5633 }
5634#endif /* HAVE_prologue or HAVE_epilogue */
5635}
This page took 0.866748 seconds and 5 git commands to generate.