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