import of gcc-2.8
[official-gcc.git] / gcc / function.c
blob3b650305d43cd95f66d578cb23bb4d607ab72367
1 /* Expands front end tree to back end RTL for GNU C-Compiler
2 Copyright (C) 1987, 88, 89, 91-96, 1997 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This file handles the generation of rtl code from tree structure
23 at the level of the function as a whole.
24 It creates the rtl expressions for parameters and auto variables
25 and has full responsibility for allocating stack slots.
27 `expand_function_start' is called at the beginning of a function,
28 before the function body is parsed, and `expand_function_end' is
29 called after parsing the body.
31 Call `assign_stack_local' to allocate a stack slot for a local variable.
32 This is usually done during the RTL generation for the function body,
33 but it can also be done in the reload pass when a pseudo-register does
34 not get a hard register.
36 Call `put_var_into_stack' when you learn, belatedly, that a variable
37 previously given a pseudo-register must in fact go in the stack.
38 This function changes the DECL_RTL to be a stack slot instead of a reg
39 then scans all the RTL instructions so far generated to correct them. */
41 #include "config.h"
42 #include <stdio.h>
43 #include "rtl.h"
44 #include "tree.h"
45 #include "flags.h"
46 #include "except.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"
56 #include "basic-block.h"
57 #include "obstack.h"
58 #include "bytecode.h"
59 #include "bc-emit.h"
61 #ifndef TRAMPOLINE_ALIGNMENT
62 #define TRAMPOLINE_ALIGNMENT FUNCTION_BOUNDARY
63 #endif
65 /* Some systems use __main in a way incompatible with its use in gcc, in these
66 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
67 give the same symbol without quotes for an alternative entry point. You
68 must define both, or neither. */
69 #ifndef NAME__MAIN
70 #define NAME__MAIN "__main"
71 #define SYMBOL__MAIN __main
72 #endif
74 /* Round a value to the lowest integer less than it that is a multiple of
75 the required alignment. Avoid using division in case the value is
76 negative. Assume the alignment is a power of two. */
77 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
79 /* Similar, but round to the next highest integer that meets the
80 alignment. */
81 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
83 /* NEED_SEPARATE_AP means that we cannot derive ap from the value of fp
84 during rtl generation. If they are different register numbers, this is
85 always true. It may also be true if
86 FIRST_PARM_OFFSET - STARTING_FRAME_OFFSET is not a constant during rtl
87 generation. See fix_lexical_addr for details. */
89 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
90 #define NEED_SEPARATE_AP
91 #endif
93 /* Number of bytes of args popped by function being compiled on its return.
94 Zero if no bytes are to be popped.
95 May affect compilation of return insn or of function epilogue. */
97 int current_function_pops_args;
99 /* Nonzero if function being compiled needs to be given an address
100 where the value should be stored. */
102 int current_function_returns_struct;
104 /* Nonzero if function being compiled needs to
105 return the address of where it has put a structure value. */
107 int current_function_returns_pcc_struct;
109 /* Nonzero if function being compiled needs to be passed a static chain. */
111 int current_function_needs_context;
113 /* Nonzero if function being compiled can call setjmp. */
115 int current_function_calls_setjmp;
117 /* Nonzero if function being compiled can call longjmp. */
119 int current_function_calls_longjmp;
121 /* Nonzero if function being compiled receives nonlocal gotos
122 from nested functions. */
124 int current_function_has_nonlocal_label;
126 /* Nonzero if function being compiled has nonlocal gotos to parent
127 function. */
129 int current_function_has_nonlocal_goto;
131 /* Nonzero if function being compiled contains nested functions. */
133 int current_function_contains_functions;
135 /* Nonzero if the current function is a thunk (a lightweight function that
136 just adjusts one of its arguments and forwards to another function), so
137 we should try to cut corners where we can. */
138 int current_function_is_thunk;
140 /* Nonzero if function being compiled can call alloca,
141 either as a subroutine or builtin. */
143 int current_function_calls_alloca;
145 /* Nonzero if the current function returns a pointer type */
147 int current_function_returns_pointer;
149 /* If some insns can be deferred to the delay slots of the epilogue, the
150 delay list for them is recorded here. */
152 rtx current_function_epilogue_delay_list;
154 /* If function's args have a fixed size, this is that size, in bytes.
155 Otherwise, it is -1.
156 May affect compilation of return insn or of function epilogue. */
158 int current_function_args_size;
160 /* # bytes the prologue should push and pretend that the caller pushed them.
161 The prologue must do this, but only if parms can be passed in registers. */
163 int current_function_pretend_args_size;
165 /* # of bytes of outgoing arguments. If ACCUMULATE_OUTGOING_ARGS is
166 defined, the needed space is pushed by the prologue. */
168 int current_function_outgoing_args_size;
170 /* This is the offset from the arg pointer to the place where the first
171 anonymous arg can be found, if there is one. */
173 rtx current_function_arg_offset_rtx;
175 /* Nonzero if current function uses varargs.h or equivalent.
176 Zero for functions that use stdarg.h. */
178 int current_function_varargs;
180 /* Nonzero if current function uses stdarg.h or equivalent.
181 Zero for functions that use varargs.h. */
183 int current_function_stdarg;
185 /* Quantities of various kinds of registers
186 used for the current function's args. */
188 CUMULATIVE_ARGS current_function_args_info;
190 /* Name of function now being compiled. */
192 char *current_function_name;
194 /* If non-zero, an RTL expression for the location at which the current
195 function returns its result. If the current function returns its
196 result in a register, current_function_return_rtx will always be
197 the hard register containing the result. */
199 rtx current_function_return_rtx;
201 /* Nonzero if the current function uses the constant pool. */
203 int current_function_uses_const_pool;
205 /* Nonzero if the current function uses pic_offset_table_rtx. */
206 int current_function_uses_pic_offset_table;
208 /* The arg pointer hard register, or the pseudo into which it was copied. */
209 rtx current_function_internal_arg_pointer;
211 /* The FUNCTION_DECL for an inline function currently being expanded. */
212 tree inline_function_decl;
214 /* Number of function calls seen so far in current function. */
216 int function_call_count;
218 /* List (chain of TREE_LIST) of LABEL_DECLs for all nonlocal labels
219 (labels to which there can be nonlocal gotos from nested functions)
220 in this function. */
222 tree nonlocal_labels;
224 /* RTX for stack slot that holds the current handler for nonlocal gotos.
225 Zero when function does not have nonlocal labels. */
227 rtx nonlocal_goto_handler_slot;
229 /* RTX for stack slot that holds the stack pointer value to restore
230 for a nonlocal goto.
231 Zero when function does not have nonlocal labels. */
233 rtx nonlocal_goto_stack_level;
235 /* Label that will go on parm cleanup code, if any.
236 Jumping to this label runs cleanup code for parameters, if
237 such code must be run. Following this code is the logical return label. */
239 rtx cleanup_label;
241 /* Label that will go on function epilogue.
242 Jumping to this label serves as a "return" instruction
243 on machines which require execution of the epilogue on all returns. */
245 rtx return_label;
247 /* List (chain of EXPR_LISTs) of pseudo-regs of SAVE_EXPRs.
248 So we can mark them all live at the end of the function, if nonopt. */
249 rtx save_expr_regs;
251 /* List (chain of EXPR_LISTs) of all stack slots in this function.
252 Made for the sake of unshare_all_rtl. */
253 rtx stack_slot_list;
255 /* Chain of all RTL_EXPRs that have insns in them. */
256 tree rtl_expr_chain;
258 /* Label to jump back to for tail recursion, or 0 if we have
259 not yet needed one for this function. */
260 rtx tail_recursion_label;
262 /* Place after which to insert the tail_recursion_label if we need one. */
263 rtx tail_recursion_reentry;
265 /* Location at which to save the argument pointer if it will need to be
266 referenced. There are two cases where this is done: if nonlocal gotos
267 exist, or if vars stored at an offset from the argument pointer will be
268 needed by inner routines. */
270 rtx arg_pointer_save_area;
272 /* Offset to end of allocated area of stack frame.
273 If stack grows down, this is the address of the last stack slot allocated.
274 If stack grows up, this is the address for the next slot. */
275 HOST_WIDE_INT frame_offset;
277 /* List (chain of TREE_LISTs) of static chains for containing functions.
278 Each link has a FUNCTION_DECL in the TREE_PURPOSE and a reg rtx
279 in an RTL_EXPR in the TREE_VALUE. */
280 static tree context_display;
282 /* List (chain of TREE_LISTs) of trampolines for nested functions.
283 The trampoline sets up the static chain and jumps to the function.
284 We supply the trampoline's address when the function's address is requested.
286 Each link has a FUNCTION_DECL in the TREE_PURPOSE and a reg rtx
287 in an RTL_EXPR in the TREE_VALUE. */
288 static tree trampoline_list;
290 /* Insn after which register parms and SAVE_EXPRs are born, if nonopt. */
291 static rtx parm_birth_insn;
293 #if 0
294 /* Nonzero if a stack slot has been generated whose address is not
295 actually valid. It means that the generated rtl must all be scanned
296 to detect and correct the invalid addresses where they occur. */
297 static int invalid_stack_slot;
298 #endif
300 /* Last insn of those whose job was to put parms into their nominal homes. */
301 static rtx last_parm_insn;
303 /* 1 + last pseudo register number possibly used for loading a copy
304 of a parameter of this function. */
305 int max_parm_reg;
307 /* Vector indexed by REGNO, containing location on stack in which
308 to put the parm which is nominally in pseudo register REGNO,
309 if we discover that that parm must go in the stack. The highest
310 element in this vector is one less than MAX_PARM_REG, above. */
311 rtx *parm_reg_stack_loc;
313 /* Nonzero once virtual register instantiation has been done.
314 assign_stack_local uses frame_pointer_rtx when this is nonzero. */
315 static int virtuals_instantiated;
317 /* These variables hold pointers to functions to
318 save and restore machine-specific data,
319 in push_function_context and pop_function_context. */
320 void (*save_machine_status) PROTO((struct function *));
321 void (*restore_machine_status) PROTO((struct function *));
323 /* Nonzero if we need to distinguish between the return value of this function
324 and the return value of a function called by this function. This helps
325 integrate.c */
327 extern int rtx_equal_function_value_matters;
328 extern tree sequence_rtl_expr;
330 /* In order to evaluate some expressions, such as function calls returning
331 structures in memory, we need to temporarily allocate stack locations.
332 We record each allocated temporary in the following structure.
334 Associated with each temporary slot is a nesting level. When we pop up
335 one level, all temporaries associated with the previous level are freed.
336 Normally, all temporaries are freed after the execution of the statement
337 in which they were created. However, if we are inside a ({...}) grouping,
338 the result may be in a temporary and hence must be preserved. If the
339 result could be in a temporary, we preserve it if we can determine which
340 one it is in. If we cannot determine which temporary may contain the
341 result, all temporaries are preserved. A temporary is preserved by
342 pretending it was allocated at the previous nesting level.
344 Automatic variables are also assigned temporary slots, at the nesting
345 level where they are defined. They are marked a "kept" so that
346 free_temp_slots will not free them. */
348 struct temp_slot
350 /* Points to next temporary slot. */
351 struct temp_slot *next;
352 /* The rtx to used to reference the slot. */
353 rtx slot;
354 /* The rtx used to represent the address if not the address of the
355 slot above. May be an EXPR_LIST if multiple addresses exist. */
356 rtx address;
357 /* The size, in units, of the slot. */
358 int size;
359 /* The value of `sequence_rtl_expr' when this temporary is allocated. */
360 tree rtl_expr;
361 /* Non-zero if this temporary is currently in use. */
362 char in_use;
363 /* Non-zero if this temporary has its address taken. */
364 char addr_taken;
365 /* Nesting level at which this slot is being used. */
366 int level;
367 /* Non-zero if this should survive a call to free_temp_slots. */
368 int keep;
369 /* The offset of the slot from the frame_pointer, including extra space
370 for alignment. This info is for combine_temp_slots. */
371 int base_offset;
372 /* The size of the slot, including extra space for alignment. This
373 info is for combine_temp_slots. */
374 int full_size;
377 /* List of all temporaries allocated, both available and in use. */
379 struct temp_slot *temp_slots;
381 /* Current nesting level for temporaries. */
383 int temp_slot_level;
385 /* The FUNCTION_DECL node for the current function. */
386 static tree this_function_decl;
388 /* Callinfo pointer for the current function. */
389 static rtx this_function_callinfo;
391 /* The label in the bytecode file of this function's actual bytecode.
392 Not an rtx. */
393 static char *this_function_bytecode;
395 /* The call description vector for the current function. */
396 static rtx this_function_calldesc;
398 /* Size of the local variables allocated for the current function. */
399 int local_vars_size;
401 /* Current depth of the bytecode evaluation stack. */
402 int stack_depth;
404 /* Maximum depth of the evaluation stack in this function. */
405 int max_stack_depth;
407 /* Current depth in statement expressions. */
408 static int stmt_expr_depth;
410 /* This structure is used to record MEMs or pseudos used to replace VAR, any
411 SUBREGs of VAR, and any MEMs containing VAR as an address. We need to
412 maintain this list in case two operands of an insn were required to match;
413 in that case we must ensure we use the same replacement. */
415 struct fixup_replacement
417 rtx old;
418 rtx new;
419 struct fixup_replacement *next;
422 /* Forward declarations. */
424 static struct temp_slot *find_temp_slot_from_address PROTO((rtx));
425 static void put_reg_into_stack PROTO((struct function *, rtx, tree,
426 enum machine_mode, enum machine_mode,
427 int, int));
428 static void fixup_var_refs PROTO((rtx, enum machine_mode, int));
429 static struct fixup_replacement
430 *find_fixup_replacement PROTO((struct fixup_replacement **, rtx));
431 static void fixup_var_refs_insns PROTO((rtx, enum machine_mode, int,
432 rtx, int));
433 static void fixup_var_refs_1 PROTO((rtx, enum machine_mode, rtx *, rtx,
434 struct fixup_replacement **));
435 static rtx fixup_memory_subreg PROTO((rtx, rtx, int));
436 static rtx walk_fixup_memory_subreg PROTO((rtx, rtx, int));
437 static rtx fixup_stack_1 PROTO((rtx, rtx));
438 static void optimize_bit_field PROTO((rtx, rtx, rtx *));
439 static void instantiate_decls PROTO((tree, int));
440 static void instantiate_decls_1 PROTO((tree, int));
441 static void instantiate_decl PROTO((rtx, int, int));
442 static int instantiate_virtual_regs_1 PROTO((rtx *, rtx, int));
443 static void delete_handlers PROTO((void));
444 static void pad_to_arg_alignment PROTO((struct args_size *, int));
445 static void pad_below PROTO((struct args_size *, enum machine_mode,
446 tree));
447 static tree round_down PROTO((tree, int));
448 static rtx round_trampoline_addr PROTO((rtx));
449 static tree blocks_nreverse PROTO((tree));
450 static int all_blocks PROTO((tree, tree *));
451 static int *record_insns PROTO((rtx));
452 static int contains PROTO((rtx, int *));
453 static void put_addressof_into_stack PROTO((rtx));
454 static void purge_addressof_1 PROTO((rtx *, rtx, int));
456 /* Pointer to chain of `struct function' for containing functions. */
457 struct function *outer_function_chain;
459 /* Given a function decl for a containing function,
460 return the `struct function' for it. */
462 struct function *
463 find_function_data (decl)
464 tree decl;
466 struct function *p;
467 for (p = outer_function_chain; p; p = p->next)
468 if (p->decl == decl)
469 return p;
470 abort ();
473 /* Save the current context for compilation of a nested function.
474 This is called from language-specific code.
475 The caller is responsible for saving any language-specific status,
476 since this function knows only about language-independent variables. */
478 void
479 push_function_context_to (context)
480 tree context;
482 struct function *p = (struct function *) xmalloc (sizeof (struct function));
484 p->next = outer_function_chain;
485 outer_function_chain = p;
487 p->name = current_function_name;
488 p->decl = current_function_decl;
489 p->pops_args = current_function_pops_args;
490 p->returns_struct = current_function_returns_struct;
491 p->returns_pcc_struct = current_function_returns_pcc_struct;
492 p->returns_pointer = current_function_returns_pointer;
493 p->needs_context = current_function_needs_context;
494 p->calls_setjmp = current_function_calls_setjmp;
495 p->calls_longjmp = current_function_calls_longjmp;
496 p->calls_alloca = current_function_calls_alloca;
497 p->has_nonlocal_label = current_function_has_nonlocal_label;
498 p->has_nonlocal_goto = current_function_has_nonlocal_goto;
499 p->contains_functions = current_function_contains_functions;
500 p->is_thunk = current_function_is_thunk;
501 p->args_size = current_function_args_size;
502 p->pretend_args_size = current_function_pretend_args_size;
503 p->arg_offset_rtx = current_function_arg_offset_rtx;
504 p->varargs = current_function_varargs;
505 p->stdarg = current_function_stdarg;
506 p->uses_const_pool = current_function_uses_const_pool;
507 p->uses_pic_offset_table = current_function_uses_pic_offset_table;
508 p->internal_arg_pointer = current_function_internal_arg_pointer;
509 p->max_parm_reg = max_parm_reg;
510 p->parm_reg_stack_loc = parm_reg_stack_loc;
511 p->outgoing_args_size = current_function_outgoing_args_size;
512 p->return_rtx = current_function_return_rtx;
513 p->nonlocal_goto_handler_slot = nonlocal_goto_handler_slot;
514 p->nonlocal_goto_stack_level = nonlocal_goto_stack_level;
515 p->nonlocal_labels = nonlocal_labels;
516 p->cleanup_label = cleanup_label;
517 p->return_label = return_label;
518 p->save_expr_regs = save_expr_regs;
519 p->stack_slot_list = stack_slot_list;
520 p->parm_birth_insn = parm_birth_insn;
521 p->frame_offset = frame_offset;
522 p->tail_recursion_label = tail_recursion_label;
523 p->tail_recursion_reentry = tail_recursion_reentry;
524 p->arg_pointer_save_area = arg_pointer_save_area;
525 p->rtl_expr_chain = rtl_expr_chain;
526 p->last_parm_insn = last_parm_insn;
527 p->context_display = context_display;
528 p->trampoline_list = trampoline_list;
529 p->function_call_count = function_call_count;
530 p->temp_slots = temp_slots;
531 p->temp_slot_level = temp_slot_level;
532 p->fixup_var_refs_queue = 0;
533 p->epilogue_delay_list = current_function_epilogue_delay_list;
534 p->args_info = current_function_args_info;
536 save_tree_status (p, context);
537 save_storage_status (p);
538 save_emit_status (p);
539 init_emit ();
540 save_expr_status (p);
541 save_stmt_status (p);
542 save_varasm_status (p, context);
544 if (save_machine_status)
545 (*save_machine_status) (p);
548 void
549 push_function_context ()
551 push_function_context_to (current_function_decl);
554 /* Restore the last saved context, at the end of a nested function.
555 This function is called from language-specific code. */
557 void
558 pop_function_context_from (context)
559 tree context;
561 struct function *p = outer_function_chain;
563 outer_function_chain = p->next;
565 current_function_contains_functions
566 = p->contains_functions || p->inline_obstacks
567 || context == current_function_decl;
568 current_function_name = p->name;
569 current_function_decl = p->decl;
570 current_function_pops_args = p->pops_args;
571 current_function_returns_struct = p->returns_struct;
572 current_function_returns_pcc_struct = p->returns_pcc_struct;
573 current_function_returns_pointer = p->returns_pointer;
574 current_function_needs_context = p->needs_context;
575 current_function_calls_setjmp = p->calls_setjmp;
576 current_function_calls_longjmp = p->calls_longjmp;
577 current_function_calls_alloca = p->calls_alloca;
578 current_function_has_nonlocal_label = p->has_nonlocal_label;
579 current_function_has_nonlocal_goto = p->has_nonlocal_goto;
580 current_function_is_thunk = p->is_thunk;
581 current_function_args_size = p->args_size;
582 current_function_pretend_args_size = p->pretend_args_size;
583 current_function_arg_offset_rtx = p->arg_offset_rtx;
584 current_function_varargs = p->varargs;
585 current_function_stdarg = p->stdarg;
586 current_function_uses_const_pool = p->uses_const_pool;
587 current_function_uses_pic_offset_table = p->uses_pic_offset_table;
588 current_function_internal_arg_pointer = p->internal_arg_pointer;
589 max_parm_reg = p->max_parm_reg;
590 parm_reg_stack_loc = p->parm_reg_stack_loc;
591 current_function_outgoing_args_size = p->outgoing_args_size;
592 current_function_return_rtx = p->return_rtx;
593 nonlocal_goto_handler_slot = p->nonlocal_goto_handler_slot;
594 nonlocal_goto_stack_level = p->nonlocal_goto_stack_level;
595 nonlocal_labels = p->nonlocal_labels;
596 cleanup_label = p->cleanup_label;
597 return_label = p->return_label;
598 save_expr_regs = p->save_expr_regs;
599 stack_slot_list = p->stack_slot_list;
600 parm_birth_insn = p->parm_birth_insn;
601 frame_offset = p->frame_offset;
602 tail_recursion_label = p->tail_recursion_label;
603 tail_recursion_reentry = p->tail_recursion_reentry;
604 arg_pointer_save_area = p->arg_pointer_save_area;
605 rtl_expr_chain = p->rtl_expr_chain;
606 last_parm_insn = p->last_parm_insn;
607 context_display = p->context_display;
608 trampoline_list = p->trampoline_list;
609 function_call_count = p->function_call_count;
610 temp_slots = p->temp_slots;
611 temp_slot_level = p->temp_slot_level;
612 current_function_epilogue_delay_list = p->epilogue_delay_list;
613 reg_renumber = 0;
614 current_function_args_info = p->args_info;
616 restore_tree_status (p, context);
617 restore_storage_status (p);
618 restore_expr_status (p);
619 restore_emit_status (p);
620 restore_stmt_status (p);
621 restore_varasm_status (p);
623 if (restore_machine_status)
624 (*restore_machine_status) (p);
626 /* Finish doing put_var_into_stack for any of our variables
627 which became addressable during the nested function. */
629 struct var_refs_queue *queue = p->fixup_var_refs_queue;
630 for (; queue; queue = queue->next)
631 fixup_var_refs (queue->modified, queue->promoted_mode, queue->unsignedp);
634 free (p);
636 /* Reset variables that have known state during rtx generation. */
637 rtx_equal_function_value_matters = 1;
638 virtuals_instantiated = 0;
641 void pop_function_context ()
643 pop_function_context_from (current_function_decl);
646 /* Allocate fixed slots in the stack frame of the current function. */
648 /* Return size needed for stack frame based on slots so far allocated.
649 This size counts from zero. It is not rounded to STACK_BOUNDARY;
650 the caller may have to do that. */
652 HOST_WIDE_INT
653 get_frame_size ()
655 #ifdef FRAME_GROWS_DOWNWARD
656 return -frame_offset;
657 #else
658 return frame_offset;
659 #endif
662 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
663 with machine mode MODE.
665 ALIGN controls the amount of alignment for the address of the slot:
666 0 means according to MODE,
667 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
668 positive specifies alignment boundary in bits.
670 We do not round to stack_boundary here. */
673 assign_stack_local (mode, size, align)
674 enum machine_mode mode;
675 int size;
676 int align;
678 register rtx x, addr;
679 int bigend_correction = 0;
680 int alignment;
682 if (align == 0)
684 alignment = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
685 if (mode == BLKmode)
686 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
688 else if (align == -1)
690 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
691 size = CEIL_ROUND (size, alignment);
693 else
694 alignment = align / BITS_PER_UNIT;
696 /* Round frame offset to that alignment.
697 We must be careful here, since FRAME_OFFSET might be negative and
698 division with a negative dividend isn't as well defined as we might
699 like. So we instead assume that ALIGNMENT is a power of two and
700 use logical operations which are unambiguous. */
701 #ifdef FRAME_GROWS_DOWNWARD
702 frame_offset = FLOOR_ROUND (frame_offset, alignment);
703 #else
704 frame_offset = CEIL_ROUND (frame_offset, alignment);
705 #endif
707 /* On a big-endian machine, if we are allocating more space than we will use,
708 use the least significant bytes of those that are allocated. */
709 if (BYTES_BIG_ENDIAN && mode != BLKmode)
710 bigend_correction = size - GET_MODE_SIZE (mode);
712 #ifdef FRAME_GROWS_DOWNWARD
713 frame_offset -= size;
714 #endif
716 /* If we have already instantiated virtual registers, return the actual
717 address relative to the frame pointer. */
718 if (virtuals_instantiated)
719 addr = plus_constant (frame_pointer_rtx,
720 (frame_offset + bigend_correction
721 + STARTING_FRAME_OFFSET));
722 else
723 addr = plus_constant (virtual_stack_vars_rtx,
724 frame_offset + bigend_correction);
726 #ifndef FRAME_GROWS_DOWNWARD
727 frame_offset += size;
728 #endif
730 x = gen_rtx (MEM, mode, addr);
732 stack_slot_list = gen_rtx (EXPR_LIST, VOIDmode, x, stack_slot_list);
734 return x;
737 /* Assign a stack slot in a containing function.
738 First three arguments are same as in preceding function.
739 The last argument specifies the function to allocate in. */
742 assign_outer_stack_local (mode, size, align, function)
743 enum machine_mode mode;
744 int size;
745 int align;
746 struct function *function;
748 register rtx x, addr;
749 int bigend_correction = 0;
750 int alignment;
752 /* Allocate in the memory associated with the function in whose frame
753 we are assigning. */
754 push_obstacks (function->function_obstack,
755 function->function_maybepermanent_obstack);
757 if (align == 0)
759 alignment = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
760 if (mode == BLKmode)
761 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
763 else if (align == -1)
765 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
766 size = CEIL_ROUND (size, alignment);
768 else
769 alignment = align / BITS_PER_UNIT;
771 /* Round frame offset to that alignment. */
772 #ifdef FRAME_GROWS_DOWNWARD
773 function->frame_offset = FLOOR_ROUND (function->frame_offset, alignment);
774 #else
775 function->frame_offset = CEIL_ROUND (function->frame_offset, alignment);
776 #endif
778 /* On a big-endian machine, if we are allocating more space than we will use,
779 use the least significant bytes of those that are allocated. */
780 if (BYTES_BIG_ENDIAN && mode != BLKmode)
781 bigend_correction = size - GET_MODE_SIZE (mode);
783 #ifdef FRAME_GROWS_DOWNWARD
784 function->frame_offset -= size;
785 #endif
786 addr = plus_constant (virtual_stack_vars_rtx,
787 function->frame_offset + bigend_correction);
788 #ifndef FRAME_GROWS_DOWNWARD
789 function->frame_offset += size;
790 #endif
792 x = gen_rtx (MEM, mode, addr);
794 function->stack_slot_list
795 = gen_rtx (EXPR_LIST, VOIDmode, x, function->stack_slot_list);
797 pop_obstacks ();
799 return x;
802 /* Allocate a temporary stack slot and record it for possible later
803 reuse.
805 MODE is the machine mode to be given to the returned rtx.
807 SIZE is the size in units of the space required. We do no rounding here
808 since assign_stack_local will do any required rounding.
810 KEEP is 1 if this slot is to be retained after a call to
811 free_temp_slots. Automatic variables for a block are allocated
812 with this flag. KEEP is 2, if we allocate a longer term temporary,
813 whose lifetime is controlled by CLEANUP_POINT_EXPRs. */
816 assign_stack_temp (mode, size, keep)
817 enum machine_mode mode;
818 int size;
819 int keep;
821 struct temp_slot *p, *best_p = 0;
823 /* If SIZE is -1 it means that somebody tried to allocate a temporary
824 of a variable size. */
825 if (size == -1)
826 abort ();
828 /* First try to find an available, already-allocated temporary that is the
829 exact size we require. */
830 for (p = temp_slots; p; p = p->next)
831 if (p->size == size && GET_MODE (p->slot) == mode && ! p->in_use)
832 break;
834 /* If we didn't find, one, try one that is larger than what we want. We
835 find the smallest such. */
836 if (p == 0)
837 for (p = temp_slots; p; p = p->next)
838 if (p->size > size && GET_MODE (p->slot) == mode && ! p->in_use
839 && (best_p == 0 || best_p->size > p->size))
840 best_p = p;
842 /* Make our best, if any, the one to use. */
843 if (best_p)
845 /* If there are enough aligned bytes left over, make them into a new
846 temp_slot so that the extra bytes don't get wasted. Do this only
847 for BLKmode slots, so that we can be sure of the alignment. */
848 if (GET_MODE (best_p->slot) == BLKmode)
850 int alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
851 int rounded_size = CEIL_ROUND (size, alignment);
853 if (best_p->size - rounded_size >= alignment)
855 p = (struct temp_slot *) oballoc (sizeof (struct temp_slot));
856 p->in_use = p->addr_taken = 0;
857 p->size = best_p->size - rounded_size;
858 p->base_offset = best_p->base_offset + rounded_size;
859 p->full_size = best_p->full_size - rounded_size;
860 p->slot = gen_rtx (MEM, BLKmode,
861 plus_constant (XEXP (best_p->slot, 0),
862 rounded_size));
863 p->address = 0;
864 p->rtl_expr = 0;
865 p->next = temp_slots;
866 temp_slots = p;
868 stack_slot_list = gen_rtx (EXPR_LIST, VOIDmode, p->slot,
869 stack_slot_list);
871 best_p->size = rounded_size;
872 best_p->full_size = rounded_size;
876 p = best_p;
879 /* If we still didn't find one, make a new temporary. */
880 if (p == 0)
882 int frame_offset_old = frame_offset;
883 p = (struct temp_slot *) oballoc (sizeof (struct temp_slot));
884 /* If the temp slot mode doesn't indicate the alignment,
885 use the largest possible, so no one will be disappointed. */
886 p->slot = assign_stack_local (mode, size, mode == BLKmode ? -1 : 0);
887 /* The following slot size computation is necessary because we don't
888 know the actual size of the temporary slot until assign_stack_local
889 has performed all the frame alignment and size rounding for the
890 requested temporary. Note that extra space added for alignment
891 can be either above or below this stack slot depending on which
892 way the frame grows. We include the extra space if and only if it
893 is above this slot. */
894 #ifdef FRAME_GROWS_DOWNWARD
895 p->size = frame_offset_old - frame_offset;
896 #else
897 p->size = size;
898 #endif
899 /* Now define the fields used by combine_temp_slots. */
900 #ifdef FRAME_GROWS_DOWNWARD
901 p->base_offset = frame_offset;
902 p->full_size = frame_offset_old - frame_offset;
903 #else
904 p->base_offset = frame_offset_old;
905 p->full_size = frame_offset - frame_offset_old;
906 #endif
907 p->address = 0;
908 p->next = temp_slots;
909 temp_slots = p;
912 p->in_use = 1;
913 p->addr_taken = 0;
914 p->rtl_expr = sequence_rtl_expr;
916 if (keep == 2)
918 p->level = target_temp_slot_level;
919 p->keep = 0;
921 else
923 p->level = temp_slot_level;
924 p->keep = keep;
927 /* We may be reusing an old slot, so clear any MEM flags that may have been
928 set from before. */
929 RTX_UNCHANGING_P (p->slot) = 0;
930 MEM_IN_STRUCT_P (p->slot) = 0;
931 return p->slot;
934 /* Assign a temporary of given TYPE.
935 KEEP is as for assign_stack_temp.
936 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
937 it is 0 if a register is OK.
938 DONT_PROMOTE is 1 if we should not promote values in register
939 to wider modes. */
942 assign_temp (type, keep, memory_required, dont_promote)
943 tree type;
944 int keep;
945 int memory_required;
946 int dont_promote;
948 enum machine_mode mode = TYPE_MODE (type);
949 int unsignedp = TREE_UNSIGNED (type);
951 if (mode == BLKmode || memory_required)
953 int size = int_size_in_bytes (type);
954 rtx tmp;
956 /* Unfortunately, we don't yet know how to allocate variable-sized
957 temporaries. However, sometimes we have a fixed upper limit on
958 the size (which is stored in TYPE_ARRAY_MAX_SIZE) and can use that
959 instead. This is the case for Chill variable-sized strings. */
960 if (size == -1 && TREE_CODE (type) == ARRAY_TYPE
961 && TYPE_ARRAY_MAX_SIZE (type) != NULL_TREE
962 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (type)) == INTEGER_CST)
963 size = TREE_INT_CST_LOW (TYPE_ARRAY_MAX_SIZE (type));
965 tmp = assign_stack_temp (mode, size, keep);
966 MEM_IN_STRUCT_P (tmp) = AGGREGATE_TYPE_P (type);
967 return tmp;
970 #ifndef PROMOTE_FOR_CALL_ONLY
971 if (! dont_promote)
972 mode = promote_mode (type, mode, &unsignedp, 0);
973 #endif
975 return gen_reg_rtx (mode);
978 /* Combine temporary stack slots which are adjacent on the stack.
980 This allows for better use of already allocated stack space. This is only
981 done for BLKmode slots because we can be sure that we won't have alignment
982 problems in this case. */
984 void
985 combine_temp_slots ()
987 struct temp_slot *p, *q;
988 struct temp_slot *prev_p, *prev_q;
989 /* Determine where to free back to after this function. */
990 rtx free_pointer = rtx_alloc (CONST_INT);
992 for (p = temp_slots, prev_p = 0; p; p = prev_p ? prev_p->next : temp_slots)
994 int delete_p = 0;
995 if (! p->in_use && GET_MODE (p->slot) == BLKmode)
996 for (q = p->next, prev_q = p; q; q = prev_q->next)
998 int delete_q = 0;
999 if (! q->in_use && GET_MODE (q->slot) == BLKmode)
1001 if (p->base_offset + p->full_size == q->base_offset)
1003 /* Q comes after P; combine Q into P. */
1004 p->size += q->size;
1005 p->full_size += q->full_size;
1006 delete_q = 1;
1008 else if (q->base_offset + q->full_size == p->base_offset)
1010 /* P comes after Q; combine P into Q. */
1011 q->size += p->size;
1012 q->full_size += p->full_size;
1013 delete_p = 1;
1014 break;
1017 /* Either delete Q or advance past it. */
1018 if (delete_q)
1019 prev_q->next = q->next;
1020 else
1021 prev_q = q;
1023 /* Either delete P or advance past it. */
1024 if (delete_p)
1026 if (prev_p)
1027 prev_p->next = p->next;
1028 else
1029 temp_slots = p->next;
1031 else
1032 prev_p = p;
1035 /* Free all the RTL made by plus_constant. */
1036 rtx_free (free_pointer);
1039 /* Find the temp slot corresponding to the object at address X. */
1041 static struct temp_slot *
1042 find_temp_slot_from_address (x)
1043 rtx x;
1045 struct temp_slot *p;
1046 rtx next;
1048 for (p = temp_slots; p; p = p->next)
1050 if (! p->in_use)
1051 continue;
1052 else if (XEXP (p->slot, 0) == x
1053 || p->address == x
1054 || (GET_CODE (x) == PLUS
1055 && XEXP (x, 0) == virtual_stack_vars_rtx
1056 && GET_CODE (XEXP (x, 1)) == CONST_INT
1057 && INTVAL (XEXP (x, 1)) >= p->base_offset
1058 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size))
1059 return p;
1061 else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
1062 for (next = p->address; next; next = XEXP (next, 1))
1063 if (XEXP (next, 0) == x)
1064 return p;
1067 return 0;
1070 /* Indicate that NEW is an alternate way of referring to the temp slot
1071 that previous was known by OLD. */
1073 void
1074 update_temp_slot_address (old, new)
1075 rtx old, new;
1077 struct temp_slot *p = find_temp_slot_from_address (old);
1079 /* If none, return. Else add NEW as an alias. */
1080 if (p == 0)
1081 return;
1082 else if (p->address == 0)
1083 p->address = new;
1084 else
1086 if (GET_CODE (p->address) != EXPR_LIST)
1087 p->address = gen_rtx (EXPR_LIST, VOIDmode, p->address, NULL_RTX);
1089 p->address = gen_rtx (EXPR_LIST, VOIDmode, new, p->address);
1093 /* If X could be a reference to a temporary slot, mark the fact that its
1094 address was taken. */
1096 void
1097 mark_temp_addr_taken (x)
1098 rtx x;
1100 struct temp_slot *p;
1102 if (x == 0)
1103 return;
1105 /* If X is not in memory or is at a constant address, it cannot be in
1106 a temporary slot. */
1107 if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1108 return;
1110 p = find_temp_slot_from_address (XEXP (x, 0));
1111 if (p != 0)
1112 p->addr_taken = 1;
1115 /* If X could be a reference to a temporary slot, mark that slot as
1116 belonging to the to one level higher than the current level. If X
1117 matched one of our slots, just mark that one. Otherwise, we can't
1118 easily predict which it is, so upgrade all of them. Kept slots
1119 need not be touched.
1121 This is called when an ({...}) construct occurs and a statement
1122 returns a value in memory. */
1124 void
1125 preserve_temp_slots (x)
1126 rtx x;
1128 struct temp_slot *p = 0;
1130 /* If there is no result, we still might have some objects whose address
1131 were taken, so we need to make sure they stay around. */
1132 if (x == 0)
1134 for (p = temp_slots; p; p = p->next)
1135 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1136 p->level--;
1138 return;
1141 /* If X is a register that is being used as a pointer, see if we have
1142 a temporary slot we know it points to. To be consistent with
1143 the code below, we really should preserve all non-kept slots
1144 if we can't find a match, but that seems to be much too costly. */
1145 if (GET_CODE (x) == REG && REGNO_POINTER_FLAG (REGNO (x)))
1146 p = find_temp_slot_from_address (x);
1148 /* If X is not in memory or is at a constant address, it cannot be in
1149 a temporary slot, but it can contain something whose address was
1150 taken. */
1151 if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
1153 for (p = temp_slots; p; p = p->next)
1154 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1155 p->level--;
1157 return;
1160 /* First see if we can find a match. */
1161 if (p == 0)
1162 p = find_temp_slot_from_address (XEXP (x, 0));
1164 if (p != 0)
1166 /* Move everything at our level whose address was taken to our new
1167 level in case we used its address. */
1168 struct temp_slot *q;
1170 if (p->level == temp_slot_level)
1172 for (q = temp_slots; q; q = q->next)
1173 if (q != p && q->addr_taken && q->level == p->level)
1174 q->level--;
1176 p->level--;
1177 p->addr_taken = 0;
1179 return;
1182 /* Otherwise, preserve all non-kept slots at this level. */
1183 for (p = temp_slots; p; p = p->next)
1184 if (p->in_use && p->level == temp_slot_level && ! p->keep)
1185 p->level--;
1188 /* X is the result of an RTL_EXPR. If it is a temporary slot associated
1189 with that RTL_EXPR, promote it into a temporary slot at the present
1190 level so it will not be freed when we free slots made in the
1191 RTL_EXPR. */
1193 void
1194 preserve_rtl_expr_result (x)
1195 rtx x;
1197 struct temp_slot *p;
1199 /* If X is not in memory or is at a constant address, it cannot be in
1200 a temporary slot. */
1201 if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1202 return;
1204 /* If we can find a match, move it to our level unless it is already at
1205 an upper level. */
1206 p = find_temp_slot_from_address (XEXP (x, 0));
1207 if (p != 0)
1209 p->level = MIN (p->level, temp_slot_level);
1210 p->rtl_expr = 0;
1213 return;
1216 /* Free all temporaries used so far. This is normally called at the end
1217 of generating code for a statement. Don't free any temporaries
1218 currently in use for an RTL_EXPR that hasn't yet been emitted.
1219 We could eventually do better than this since it can be reused while
1220 generating the same RTL_EXPR, but this is complex and probably not
1221 worthwhile. */
1223 void
1224 free_temp_slots ()
1226 struct temp_slot *p;
1228 for (p = temp_slots; p; p = p->next)
1229 if (p->in_use && p->level == temp_slot_level && ! p->keep
1230 && p->rtl_expr == 0)
1231 p->in_use = 0;
1233 combine_temp_slots ();
1236 /* Free all temporary slots used in T, an RTL_EXPR node. */
1238 void
1239 free_temps_for_rtl_expr (t)
1240 tree t;
1242 struct temp_slot *p;
1244 for (p = temp_slots; p; p = p->next)
1245 if (p->rtl_expr == t)
1246 p->in_use = 0;
1248 combine_temp_slots ();
1251 /* Mark all temporaries ever allocated in this function as not suitable
1252 for reuse until the current level is exited. */
1254 void
1255 mark_all_temps_used ()
1257 struct temp_slot *p;
1259 for (p = temp_slots; p; p = p->next)
1261 p->in_use = p->keep = 1;
1262 p->level = MIN (p->level, temp_slot_level);
1266 /* Push deeper into the nesting level for stack temporaries. */
1268 void
1269 push_temp_slots ()
1271 temp_slot_level++;
1274 /* Pop a temporary nesting level. All slots in use in the current level
1275 are freed. */
1277 void
1278 pop_temp_slots ()
1280 struct temp_slot *p;
1282 for (p = temp_slots; p; p = p->next)
1283 if (p->in_use && p->level == temp_slot_level && p->rtl_expr == 0)
1284 p->in_use = 0;
1286 combine_temp_slots ();
1288 temp_slot_level--;
1291 /* Initialize temporary slots. */
1293 void
1294 init_temp_slots ()
1296 /* We have not allocated any temporaries yet. */
1297 temp_slots = 0;
1298 temp_slot_level = 0;
1299 target_temp_slot_level = 0;
1302 /* Retroactively move an auto variable from a register to a stack slot.
1303 This is done when an address-reference to the variable is seen. */
1305 void
1306 put_var_into_stack (decl)
1307 tree decl;
1309 register rtx reg;
1310 enum machine_mode promoted_mode, decl_mode;
1311 struct function *function = 0;
1312 tree context;
1313 int can_use_addressof;
1315 if (output_bytecode)
1316 return;
1318 context = decl_function_context (decl);
1320 /* Get the current rtl used for this object and it's original mode. */
1321 reg = TREE_CODE (decl) == SAVE_EXPR ? SAVE_EXPR_RTL (decl) : DECL_RTL (decl);
1323 /* No need to do anything if decl has no rtx yet
1324 since in that case caller is setting TREE_ADDRESSABLE
1325 and a stack slot will be assigned when the rtl is made. */
1326 if (reg == 0)
1327 return;
1329 /* Get the declared mode for this object. */
1330 decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1331 : DECL_MODE (decl));
1332 /* Get the mode it's actually stored in. */
1333 promoted_mode = GET_MODE (reg);
1335 /* If this variable comes from an outer function,
1336 find that function's saved context. */
1337 if (context != current_function_decl && context != inline_function_decl)
1338 for (function = outer_function_chain; function; function = function->next)
1339 if (function->decl == context)
1340 break;
1342 /* If this is a variable-size object with a pseudo to address it,
1343 put that pseudo into the stack, if the var is nonlocal. */
1344 if (DECL_NONLOCAL (decl)
1345 && GET_CODE (reg) == MEM
1346 && GET_CODE (XEXP (reg, 0)) == REG
1347 && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
1349 reg = XEXP (reg, 0);
1350 decl_mode = promoted_mode = GET_MODE (reg);
1353 can_use_addressof
1354 = (function == 0
1355 /* FIXME make it work for promoted modes too */
1356 && decl_mode == promoted_mode
1357 #ifdef NON_SAVING_SETJMP
1358 && ! (NON_SAVING_SETJMP && current_function_calls_setjmp)
1359 #endif
1362 /* If we can't use ADDRESSOF, make sure we see through one we already
1363 generated. */
1364 if (! can_use_addressof && GET_CODE (reg) == MEM
1365 && GET_CODE (XEXP (reg, 0)) == ADDRESSOF)
1366 reg = XEXP (XEXP (reg, 0), 0);
1368 /* Now we should have a value that resides in one or more pseudo regs. */
1370 if (GET_CODE (reg) == REG)
1372 /* If this variable lives in the current function and we don't need
1373 to put things in the stack for the sake of setjmp, try to keep it
1374 in a register until we know we actually need the address. */
1375 if (can_use_addressof)
1376 gen_mem_addressof (reg, decl);
1377 else
1378 put_reg_into_stack (function, reg, TREE_TYPE (decl),
1379 promoted_mode, decl_mode,
1380 TREE_SIDE_EFFECTS (decl), 0);
1382 else if (GET_CODE (reg) == CONCAT)
1384 /* A CONCAT contains two pseudos; put them both in the stack.
1385 We do it so they end up consecutive. */
1386 enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
1387 tree part_type = TREE_TYPE (TREE_TYPE (decl));
1388 #ifdef FRAME_GROWS_DOWNWARD
1389 /* Since part 0 should have a lower address, do it second. */
1390 put_reg_into_stack (function, XEXP (reg, 1), part_type, part_mode,
1391 part_mode, TREE_SIDE_EFFECTS (decl), 0);
1392 put_reg_into_stack (function, XEXP (reg, 0), part_type, part_mode,
1393 part_mode, TREE_SIDE_EFFECTS (decl), 0);
1394 #else
1395 put_reg_into_stack (function, XEXP (reg, 0), part_type, part_mode,
1396 part_mode, TREE_SIDE_EFFECTS (decl), 0);
1397 put_reg_into_stack (function, XEXP (reg, 1), part_type, part_mode,
1398 part_mode, TREE_SIDE_EFFECTS (decl), 0);
1399 #endif
1401 /* Change the CONCAT into a combined MEM for both parts. */
1402 PUT_CODE (reg, MEM);
1403 MEM_VOLATILE_P (reg) = MEM_VOLATILE_P (XEXP (reg, 0));
1405 /* The two parts are in memory order already.
1406 Use the lower parts address as ours. */
1407 XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1408 /* Prevent sharing of rtl that might lose. */
1409 if (GET_CODE (XEXP (reg, 0)) == PLUS)
1410 XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
1412 else
1413 return;
1415 if (flag_check_memory_usage)
1416 emit_library_call (chkr_set_right_libfunc, 1, VOIDmode, 3,
1417 XEXP (reg, 0), ptr_mode,
1418 GEN_INT (GET_MODE_SIZE (GET_MODE (reg))),
1419 TYPE_MODE (sizetype),
1420 GEN_INT (MEMORY_USE_RW),
1421 TYPE_MODE (integer_type_node));
1424 /* Subroutine of put_var_into_stack. This puts a single pseudo reg REG
1425 into the stack frame of FUNCTION (0 means the current function).
1426 DECL_MODE is the machine mode of the user-level data type.
1427 PROMOTED_MODE is the machine mode of the register.
1428 VOLATILE_P is nonzero if this is for a "volatile" decl. */
1430 static void
1431 put_reg_into_stack (function, reg, type, promoted_mode, decl_mode, volatile_p,
1432 original_regno)
1433 struct function *function;
1434 rtx reg;
1435 tree type;
1436 enum machine_mode promoted_mode, decl_mode;
1437 int volatile_p;
1438 int original_regno;
1440 rtx new = 0;
1441 int regno = original_regno;
1443 if (regno == 0)
1444 regno = REGNO (reg);
1446 if (function)
1448 if (regno < function->max_parm_reg)
1449 new = function->parm_reg_stack_loc[regno];
1450 if (new == 0)
1451 new = assign_outer_stack_local (decl_mode, GET_MODE_SIZE (decl_mode),
1452 0, function);
1454 else
1456 if (regno < max_parm_reg)
1457 new = parm_reg_stack_loc[regno];
1458 if (new == 0)
1459 new = assign_stack_local (decl_mode, GET_MODE_SIZE (decl_mode), 0);
1462 PUT_MODE (reg, decl_mode);
1463 XEXP (reg, 0) = XEXP (new, 0);
1464 /* `volatil' bit means one thing for MEMs, another entirely for REGs. */
1465 MEM_VOLATILE_P (reg) = volatile_p;
1466 PUT_CODE (reg, MEM);
1468 /* If this is a memory ref that contains aggregate components,
1469 mark it as such for cse and loop optimize. */
1470 MEM_IN_STRUCT_P (reg) = AGGREGATE_TYPE_P (type);
1472 /* Now make sure that all refs to the variable, previously made
1473 when it was a register, are fixed up to be valid again. */
1474 if (function)
1476 struct var_refs_queue *temp;
1478 /* Variable is inherited; fix it up when we get back to its function. */
1479 push_obstacks (function->function_obstack,
1480 function->function_maybepermanent_obstack);
1482 /* See comment in restore_tree_status in tree.c for why this needs to be
1483 on saveable obstack. */
1484 temp
1485 = (struct var_refs_queue *) savealloc (sizeof (struct var_refs_queue));
1486 temp->modified = reg;
1487 temp->promoted_mode = promoted_mode;
1488 temp->unsignedp = TREE_UNSIGNED (type);
1489 temp->next = function->fixup_var_refs_queue;
1490 function->fixup_var_refs_queue = temp;
1491 pop_obstacks ();
1493 else
1494 /* Variable is local; fix it up now. */
1495 fixup_var_refs (reg, promoted_mode, TREE_UNSIGNED (type));
1498 static void
1499 fixup_var_refs (var, promoted_mode, unsignedp)
1500 rtx var;
1501 enum machine_mode promoted_mode;
1502 int unsignedp;
1504 tree pending;
1505 rtx first_insn = get_insns ();
1506 struct sequence_stack *stack = sequence_stack;
1507 tree rtl_exps = rtl_expr_chain;
1509 /* Must scan all insns for stack-refs that exceed the limit. */
1510 fixup_var_refs_insns (var, promoted_mode, unsignedp, first_insn, stack == 0);
1512 /* Scan all pending sequences too. */
1513 for (; stack; stack = stack->next)
1515 push_to_sequence (stack->first);
1516 fixup_var_refs_insns (var, promoted_mode, unsignedp,
1517 stack->first, stack->next != 0);
1518 /* Update remembered end of sequence
1519 in case we added an insn at the end. */
1520 stack->last = get_last_insn ();
1521 end_sequence ();
1524 /* Scan all waiting RTL_EXPRs too. */
1525 for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1527 rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1528 if (seq != const0_rtx && seq != 0)
1530 push_to_sequence (seq);
1531 fixup_var_refs_insns (var, promoted_mode, unsignedp, seq, 0);
1532 end_sequence ();
1537 /* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
1538 some part of an insn. Return a struct fixup_replacement whose OLD
1539 value is equal to X. Allocate a new structure if no such entry exists. */
1541 static struct fixup_replacement *
1542 find_fixup_replacement (replacements, x)
1543 struct fixup_replacement **replacements;
1544 rtx x;
1546 struct fixup_replacement *p;
1548 /* See if we have already replaced this. */
1549 for (p = *replacements; p && p->old != x; p = p->next)
1552 if (p == 0)
1554 p = (struct fixup_replacement *) oballoc (sizeof (struct fixup_replacement));
1555 p->old = x;
1556 p->new = 0;
1557 p->next = *replacements;
1558 *replacements = p;
1561 return p;
1564 /* Scan the insn-chain starting with INSN for refs to VAR
1565 and fix them up. TOPLEVEL is nonzero if this chain is the
1566 main chain of insns for the current function. */
1568 static void
1569 fixup_var_refs_insns (var, promoted_mode, unsignedp, insn, toplevel)
1570 rtx var;
1571 enum machine_mode promoted_mode;
1572 int unsignedp;
1573 rtx insn;
1574 int toplevel;
1576 rtx call_dest = 0;
1578 while (insn)
1580 rtx next = NEXT_INSN (insn);
1581 rtx note;
1582 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1584 /* If this is a CLOBBER of VAR, delete it.
1586 If it has a REG_LIBCALL note, delete the REG_LIBCALL
1587 and REG_RETVAL notes too. */
1588 if (GET_CODE (PATTERN (insn)) == CLOBBER
1589 && XEXP (PATTERN (insn), 0) == var)
1591 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1592 /* The REG_LIBCALL note will go away since we are going to
1593 turn INSN into a NOTE, so just delete the
1594 corresponding REG_RETVAL note. */
1595 remove_note (XEXP (note, 0),
1596 find_reg_note (XEXP (note, 0), REG_RETVAL,
1597 NULL_RTX));
1599 /* In unoptimized compilation, we shouldn't call delete_insn
1600 except in jump.c doing warnings. */
1601 PUT_CODE (insn, NOTE);
1602 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1603 NOTE_SOURCE_FILE (insn) = 0;
1606 /* The insn to load VAR from a home in the arglist
1607 is now a no-op. When we see it, just delete it. */
1608 else if (toplevel
1609 && GET_CODE (PATTERN (insn)) == SET
1610 && SET_DEST (PATTERN (insn)) == var
1611 /* If this represents the result of an insn group,
1612 don't delete the insn. */
1613 && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
1614 && rtx_equal_p (SET_SRC (PATTERN (insn)), var))
1616 /* In unoptimized compilation, we shouldn't call delete_insn
1617 except in jump.c doing warnings. */
1618 PUT_CODE (insn, NOTE);
1619 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1620 NOTE_SOURCE_FILE (insn) = 0;
1621 if (insn == last_parm_insn)
1622 last_parm_insn = PREV_INSN (next);
1624 else
1626 struct fixup_replacement *replacements = 0;
1627 rtx next_insn = NEXT_INSN (insn);
1629 if (SMALL_REGISTER_CLASSES)
1631 /* If the insn that copies the results of a CALL_INSN
1632 into a pseudo now references VAR, we have to use an
1633 intermediate pseudo since we want the life of the
1634 return value register to be only a single insn.
1636 If we don't use an intermediate pseudo, such things as
1637 address computations to make the address of VAR valid
1638 if it is not can be placed between the CALL_INSN and INSN.
1640 To make sure this doesn't happen, we record the destination
1641 of the CALL_INSN and see if the next insn uses both that
1642 and VAR. */
1644 if (call_dest != 0 && GET_CODE (insn) == INSN
1645 && reg_mentioned_p (var, PATTERN (insn))
1646 && reg_mentioned_p (call_dest, PATTERN (insn)))
1648 rtx temp = gen_reg_rtx (GET_MODE (call_dest));
1650 emit_insn_before (gen_move_insn (temp, call_dest), insn);
1652 PATTERN (insn) = replace_rtx (PATTERN (insn),
1653 call_dest, temp);
1656 if (GET_CODE (insn) == CALL_INSN
1657 && GET_CODE (PATTERN (insn)) == SET)
1658 call_dest = SET_DEST (PATTERN (insn));
1659 else if (GET_CODE (insn) == CALL_INSN
1660 && GET_CODE (PATTERN (insn)) == PARALLEL
1661 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1662 call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1663 else
1664 call_dest = 0;
1667 /* See if we have to do anything to INSN now that VAR is in
1668 memory. If it needs to be loaded into a pseudo, use a single
1669 pseudo for the entire insn in case there is a MATCH_DUP
1670 between two operands. We pass a pointer to the head of
1671 a list of struct fixup_replacements. If fixup_var_refs_1
1672 needs to allocate pseudos or replacement MEMs (for SUBREGs),
1673 it will record them in this list.
1675 If it allocated a pseudo for any replacement, we copy into
1676 it here. */
1678 fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1679 &replacements);
1681 /* If this is last_parm_insn, and any instructions were output
1682 after it to fix it up, then we must set last_parm_insn to
1683 the last such instruction emitted. */
1684 if (insn == last_parm_insn)
1685 last_parm_insn = PREV_INSN (next_insn);
1687 while (replacements)
1689 if (GET_CODE (replacements->new) == REG)
1691 rtx insert_before;
1692 rtx seq;
1694 /* OLD might be a (subreg (mem)). */
1695 if (GET_CODE (replacements->old) == SUBREG)
1696 replacements->old
1697 = fixup_memory_subreg (replacements->old, insn, 0);
1698 else
1699 replacements->old
1700 = fixup_stack_1 (replacements->old, insn);
1702 insert_before = insn;
1704 /* If we are changing the mode, do a conversion.
1705 This might be wasteful, but combine.c will
1706 eliminate much of the waste. */
1708 if (GET_MODE (replacements->new)
1709 != GET_MODE (replacements->old))
1711 start_sequence ();
1712 convert_move (replacements->new,
1713 replacements->old, unsignedp);
1714 seq = gen_sequence ();
1715 end_sequence ();
1717 else
1718 seq = gen_move_insn (replacements->new,
1719 replacements->old);
1721 emit_insn_before (seq, insert_before);
1724 replacements = replacements->next;
1728 /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1729 But don't touch other insns referred to by reg-notes;
1730 we will get them elsewhere. */
1731 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1732 if (GET_CODE (note) != INSN_LIST)
1733 XEXP (note, 0)
1734 = walk_fixup_memory_subreg (XEXP (note, 0), insn, 1);
1736 insn = next;
1740 /* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
1741 See if the rtx expression at *LOC in INSN needs to be changed.
1743 REPLACEMENTS is a pointer to a list head that starts out zero, but may
1744 contain a list of original rtx's and replacements. If we find that we need
1745 to modify this insn by replacing a memory reference with a pseudo or by
1746 making a new MEM to implement a SUBREG, we consult that list to see if
1747 we have already chosen a replacement. If none has already been allocated,
1748 we allocate it and update the list. fixup_var_refs_insns will copy VAR
1749 or the SUBREG, as appropriate, to the pseudo. */
1751 static void
1752 fixup_var_refs_1 (var, promoted_mode, loc, insn, replacements)
1753 register rtx var;
1754 enum machine_mode promoted_mode;
1755 register rtx *loc;
1756 rtx insn;
1757 struct fixup_replacement **replacements;
1759 register int i;
1760 register rtx x = *loc;
1761 RTX_CODE code = GET_CODE (x);
1762 register char *fmt;
1763 register rtx tem, tem1;
1764 struct fixup_replacement *replacement;
1766 switch (code)
1768 case ADDRESSOF:
1769 if (XEXP (x, 0) == var)
1771 /* Prevent sharing of rtl that might lose. */
1772 rtx sub = copy_rtx (XEXP (var, 0));
1774 start_sequence ();
1776 if (! validate_change (insn, loc, sub, 0))
1778 rtx y = force_operand (sub, NULL_RTX);
1780 if (! validate_change (insn, loc, y, 0))
1781 *loc = copy_to_reg (y);
1784 emit_insn_before (gen_sequence (), insn);
1785 end_sequence ();
1787 return;
1789 case MEM:
1790 if (var == x)
1792 /* If we already have a replacement, use it. Otherwise,
1793 try to fix up this address in case it is invalid. */
1795 replacement = find_fixup_replacement (replacements, var);
1796 if (replacement->new)
1798 *loc = replacement->new;
1799 return;
1802 *loc = replacement->new = x = fixup_stack_1 (x, insn);
1804 /* Unless we are forcing memory to register or we changed the mode,
1805 we can leave things the way they are if the insn is valid. */
1807 INSN_CODE (insn) = -1;
1808 if (! flag_force_mem && GET_MODE (x) == promoted_mode
1809 && recog_memoized (insn) >= 0)
1810 return;
1812 *loc = replacement->new = gen_reg_rtx (promoted_mode);
1813 return;
1816 /* If X contains VAR, we need to unshare it here so that we update
1817 each occurrence separately. But all identical MEMs in one insn
1818 must be replaced with the same rtx because of the possibility of
1819 MATCH_DUPs. */
1821 if (reg_mentioned_p (var, x))
1823 replacement = find_fixup_replacement (replacements, x);
1824 if (replacement->new == 0)
1825 replacement->new = copy_most_rtx (x, var);
1827 *loc = x = replacement->new;
1829 break;
1831 case REG:
1832 case CC0:
1833 case PC:
1834 case CONST_INT:
1835 case CONST:
1836 case SYMBOL_REF:
1837 case LABEL_REF:
1838 case CONST_DOUBLE:
1839 return;
1841 case SIGN_EXTRACT:
1842 case ZERO_EXTRACT:
1843 /* Note that in some cases those types of expressions are altered
1844 by optimize_bit_field, and do not survive to get here. */
1845 if (XEXP (x, 0) == var
1846 || (GET_CODE (XEXP (x, 0)) == SUBREG
1847 && SUBREG_REG (XEXP (x, 0)) == var))
1849 /* Get TEM as a valid MEM in the mode presently in the insn.
1851 We don't worry about the possibility of MATCH_DUP here; it
1852 is highly unlikely and would be tricky to handle. */
1854 tem = XEXP (x, 0);
1855 if (GET_CODE (tem) == SUBREG)
1857 if (GET_MODE_BITSIZE (GET_MODE (tem))
1858 > GET_MODE_BITSIZE (GET_MODE (var)))
1860 replacement = find_fixup_replacement (replacements, var);
1861 if (replacement->new == 0)
1862 replacement->new = gen_reg_rtx (GET_MODE (var));
1863 SUBREG_REG (tem) = replacement->new;
1865 else
1866 tem = fixup_memory_subreg (tem, insn, 0);
1868 else
1869 tem = fixup_stack_1 (tem, insn);
1871 /* Unless we want to load from memory, get TEM into the proper mode
1872 for an extract from memory. This can only be done if the
1873 extract is at a constant position and length. */
1875 if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
1876 && GET_CODE (XEXP (x, 2)) == CONST_INT
1877 && ! mode_dependent_address_p (XEXP (tem, 0))
1878 && ! MEM_VOLATILE_P (tem))
1880 enum machine_mode wanted_mode = VOIDmode;
1881 enum machine_mode is_mode = GET_MODE (tem);
1882 int width = INTVAL (XEXP (x, 1));
1883 int pos = INTVAL (XEXP (x, 2));
1885 #ifdef HAVE_extzv
1886 if (GET_CODE (x) == ZERO_EXTRACT)
1887 wanted_mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
1888 #endif
1889 #ifdef HAVE_extv
1890 if (GET_CODE (x) == SIGN_EXTRACT)
1891 wanted_mode = insn_operand_mode[(int) CODE_FOR_extv][1];
1892 #endif
1893 /* If we have a narrower mode, we can do something. */
1894 if (wanted_mode != VOIDmode
1895 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
1897 int offset = pos / BITS_PER_UNIT;
1898 rtx old_pos = XEXP (x, 2);
1899 rtx newmem;
1901 /* If the bytes and bits are counted differently, we
1902 must adjust the offset. */
1903 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
1904 offset = (GET_MODE_SIZE (is_mode)
1905 - GET_MODE_SIZE (wanted_mode) - offset);
1907 pos %= GET_MODE_BITSIZE (wanted_mode);
1909 newmem = gen_rtx (MEM, wanted_mode,
1910 plus_constant (XEXP (tem, 0), offset));
1911 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (tem);
1912 MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (tem);
1913 MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (tem);
1915 /* Make the change and see if the insn remains valid. */
1916 INSN_CODE (insn) = -1;
1917 XEXP (x, 0) = newmem;
1918 XEXP (x, 2) = GEN_INT (pos);
1920 if (recog_memoized (insn) >= 0)
1921 return;
1923 /* Otherwise, restore old position. XEXP (x, 0) will be
1924 restored later. */
1925 XEXP (x, 2) = old_pos;
1929 /* If we get here, the bitfield extract insn can't accept a memory
1930 reference. Copy the input into a register. */
1932 tem1 = gen_reg_rtx (GET_MODE (tem));
1933 emit_insn_before (gen_move_insn (tem1, tem), insn);
1934 XEXP (x, 0) = tem1;
1935 return;
1937 break;
1939 case SUBREG:
1940 if (SUBREG_REG (x) == var)
1942 /* If this is a special SUBREG made because VAR was promoted
1943 from a wider mode, replace it with VAR and call ourself
1944 recursively, this time saying that the object previously
1945 had its current mode (by virtue of the SUBREG). */
1947 if (SUBREG_PROMOTED_VAR_P (x))
1949 *loc = var;
1950 fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements);
1951 return;
1954 /* If this SUBREG makes VAR wider, it has become a paradoxical
1955 SUBREG with VAR in memory, but these aren't allowed at this
1956 stage of the compilation. So load VAR into a pseudo and take
1957 a SUBREG of that pseudo. */
1958 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
1960 replacement = find_fixup_replacement (replacements, var);
1961 if (replacement->new == 0)
1962 replacement->new = gen_reg_rtx (GET_MODE (var));
1963 SUBREG_REG (x) = replacement->new;
1964 return;
1967 /* See if we have already found a replacement for this SUBREG.
1968 If so, use it. Otherwise, make a MEM and see if the insn
1969 is recognized. If not, or if we should force MEM into a register,
1970 make a pseudo for this SUBREG. */
1971 replacement = find_fixup_replacement (replacements, x);
1972 if (replacement->new)
1974 *loc = replacement->new;
1975 return;
1978 replacement->new = *loc = fixup_memory_subreg (x, insn, 0);
1980 INSN_CODE (insn) = -1;
1981 if (! flag_force_mem && recog_memoized (insn) >= 0)
1982 return;
1984 *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
1985 return;
1987 break;
1989 case SET:
1990 /* First do special simplification of bit-field references. */
1991 if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
1992 || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
1993 optimize_bit_field (x, insn, 0);
1994 if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
1995 || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
1996 optimize_bit_field (x, insn, NULL_PTR);
1998 /* For a paradoxical SUBREG inside a ZERO_EXTRACT, load the object
1999 into a register and then store it back out. */
2000 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2001 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG
2002 && SUBREG_REG (XEXP (SET_DEST (x), 0)) == var
2003 && (GET_MODE_SIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2004 > GET_MODE_SIZE (GET_MODE (var))))
2006 replacement = find_fixup_replacement (replacements, var);
2007 if (replacement->new == 0)
2008 replacement->new = gen_reg_rtx (GET_MODE (var));
2010 SUBREG_REG (XEXP (SET_DEST (x), 0)) = replacement->new;
2011 emit_insn_after (gen_move_insn (var, replacement->new), insn);
2014 /* If SET_DEST is now a paradoxical SUBREG, put the result of this
2015 insn into a pseudo and store the low part of the pseudo into VAR. */
2016 if (GET_CODE (SET_DEST (x)) == SUBREG
2017 && SUBREG_REG (SET_DEST (x)) == var
2018 && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2019 > GET_MODE_SIZE (GET_MODE (var))))
2021 SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
2022 emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
2023 tem)),
2024 insn);
2025 break;
2029 rtx dest = SET_DEST (x);
2030 rtx src = SET_SRC (x);
2031 rtx outerdest = dest;
2033 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2034 || GET_CODE (dest) == SIGN_EXTRACT
2035 || GET_CODE (dest) == ZERO_EXTRACT)
2036 dest = XEXP (dest, 0);
2038 if (GET_CODE (src) == SUBREG)
2039 src = XEXP (src, 0);
2041 /* If VAR does not appear at the top level of the SET
2042 just scan the lower levels of the tree. */
2044 if (src != var && dest != var)
2045 break;
2047 /* We will need to rerecognize this insn. */
2048 INSN_CODE (insn) = -1;
2050 #ifdef HAVE_insv
2051 if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var)
2053 /* Since this case will return, ensure we fixup all the
2054 operands here. */
2055 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
2056 insn, replacements);
2057 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
2058 insn, replacements);
2059 fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
2060 insn, replacements);
2062 tem = XEXP (outerdest, 0);
2064 /* Clean up (SUBREG:SI (MEM:mode ...) 0)
2065 that may appear inside a ZERO_EXTRACT.
2066 This was legitimate when the MEM was a REG. */
2067 if (GET_CODE (tem) == SUBREG
2068 && SUBREG_REG (tem) == var)
2069 tem = fixup_memory_subreg (tem, insn, 0);
2070 else
2071 tem = fixup_stack_1 (tem, insn);
2073 if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
2074 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
2075 && ! mode_dependent_address_p (XEXP (tem, 0))
2076 && ! MEM_VOLATILE_P (tem))
2078 enum machine_mode wanted_mode
2079 = insn_operand_mode[(int) CODE_FOR_insv][0];
2080 enum machine_mode is_mode = GET_MODE (tem);
2081 int width = INTVAL (XEXP (outerdest, 1));
2082 int pos = INTVAL (XEXP (outerdest, 2));
2084 /* If we have a narrower mode, we can do something. */
2085 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2087 int offset = pos / BITS_PER_UNIT;
2088 rtx old_pos = XEXP (outerdest, 2);
2089 rtx newmem;
2091 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2092 offset = (GET_MODE_SIZE (is_mode)
2093 - GET_MODE_SIZE (wanted_mode) - offset);
2095 pos %= GET_MODE_BITSIZE (wanted_mode);
2097 newmem = gen_rtx (MEM, wanted_mode,
2098 plus_constant (XEXP (tem, 0), offset));
2099 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (tem);
2100 MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (tem);
2101 MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (tem);
2103 /* Make the change and see if the insn remains valid. */
2104 INSN_CODE (insn) = -1;
2105 XEXP (outerdest, 0) = newmem;
2106 XEXP (outerdest, 2) = GEN_INT (pos);
2108 if (recog_memoized (insn) >= 0)
2109 return;
2111 /* Otherwise, restore old position. XEXP (x, 0) will be
2112 restored later. */
2113 XEXP (outerdest, 2) = old_pos;
2117 /* If we get here, the bit-field store doesn't allow memory
2118 or isn't located at a constant position. Load the value into
2119 a register, do the store, and put it back into memory. */
2121 tem1 = gen_reg_rtx (GET_MODE (tem));
2122 emit_insn_before (gen_move_insn (tem1, tem), insn);
2123 emit_insn_after (gen_move_insn (tem, tem1), insn);
2124 XEXP (outerdest, 0) = tem1;
2125 return;
2127 #endif
2129 /* STRICT_LOW_PART is a no-op on memory references
2130 and it can cause combinations to be unrecognizable,
2131 so eliminate it. */
2133 if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2134 SET_DEST (x) = XEXP (SET_DEST (x), 0);
2136 /* A valid insn to copy VAR into or out of a register
2137 must be left alone, to avoid an infinite loop here.
2138 If the reference to VAR is by a subreg, fix that up,
2139 since SUBREG is not valid for a memref.
2140 Also fix up the address of the stack slot.
2142 Note that we must not try to recognize the insn until
2143 after we know that we have valid addresses and no
2144 (subreg (mem ...) ...) constructs, since these interfere
2145 with determining the validity of the insn. */
2147 if ((SET_SRC (x) == var
2148 || (GET_CODE (SET_SRC (x)) == SUBREG
2149 && SUBREG_REG (SET_SRC (x)) == var))
2150 && (GET_CODE (SET_DEST (x)) == REG
2151 || (GET_CODE (SET_DEST (x)) == SUBREG
2152 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG))
2153 && GET_MODE (var) == promoted_mode
2154 && x == single_set (insn))
2156 rtx pat;
2158 replacement = find_fixup_replacement (replacements, SET_SRC (x));
2159 if (replacement->new)
2160 SET_SRC (x) = replacement->new;
2161 else if (GET_CODE (SET_SRC (x)) == SUBREG)
2162 SET_SRC (x) = replacement->new
2163 = fixup_memory_subreg (SET_SRC (x), insn, 0);
2164 else
2165 SET_SRC (x) = replacement->new
2166 = fixup_stack_1 (SET_SRC (x), insn);
2168 if (recog_memoized (insn) >= 0)
2169 return;
2171 /* INSN is not valid, but we know that we want to
2172 copy SET_SRC (x) to SET_DEST (x) in some way. So
2173 we generate the move and see whether it requires more
2174 than one insn. If it does, we emit those insns and
2175 delete INSN. Otherwise, we an just replace the pattern
2176 of INSN; we have already verified above that INSN has
2177 no other function that to do X. */
2179 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2180 if (GET_CODE (pat) == SEQUENCE)
2182 emit_insn_after (pat, insn);
2183 PUT_CODE (insn, NOTE);
2184 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2185 NOTE_SOURCE_FILE (insn) = 0;
2187 else
2188 PATTERN (insn) = pat;
2190 return;
2193 if ((SET_DEST (x) == var
2194 || (GET_CODE (SET_DEST (x)) == SUBREG
2195 && SUBREG_REG (SET_DEST (x)) == var))
2196 && (GET_CODE (SET_SRC (x)) == REG
2197 || (GET_CODE (SET_SRC (x)) == SUBREG
2198 && GET_CODE (SUBREG_REG (SET_SRC (x))) == REG))
2199 && GET_MODE (var) == promoted_mode
2200 && x == single_set (insn))
2202 rtx pat;
2204 if (GET_CODE (SET_DEST (x)) == SUBREG)
2205 SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn, 0);
2206 else
2207 SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
2209 if (recog_memoized (insn) >= 0)
2210 return;
2212 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2213 if (GET_CODE (pat) == SEQUENCE)
2215 emit_insn_after (pat, insn);
2216 PUT_CODE (insn, NOTE);
2217 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2218 NOTE_SOURCE_FILE (insn) = 0;
2220 else
2221 PATTERN (insn) = pat;
2223 return;
2226 /* Otherwise, storing into VAR must be handled specially
2227 by storing into a temporary and copying that into VAR
2228 with a new insn after this one. Note that this case
2229 will be used when storing into a promoted scalar since
2230 the insn will now have different modes on the input
2231 and output and hence will be invalid (except for the case
2232 of setting it to a constant, which does not need any
2233 change if it is valid). We generate extra code in that case,
2234 but combine.c will eliminate it. */
2236 if (dest == var)
2238 rtx temp;
2239 rtx fixeddest = SET_DEST (x);
2241 /* STRICT_LOW_PART can be discarded, around a MEM. */
2242 if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2243 fixeddest = XEXP (fixeddest, 0);
2244 /* Convert (SUBREG (MEM)) to a MEM in a changed mode. */
2245 if (GET_CODE (fixeddest) == SUBREG)
2247 fixeddest = fixup_memory_subreg (fixeddest, insn, 0);
2248 promoted_mode = GET_MODE (fixeddest);
2250 else
2251 fixeddest = fixup_stack_1 (fixeddest, insn);
2253 temp = gen_reg_rtx (promoted_mode);
2255 emit_insn_after (gen_move_insn (fixeddest,
2256 gen_lowpart (GET_MODE (fixeddest),
2257 temp)),
2258 insn);
2260 SET_DEST (x) = temp;
2264 default:
2265 break;
2268 /* Nothing special about this RTX; fix its operands. */
2270 fmt = GET_RTX_FORMAT (code);
2271 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2273 if (fmt[i] == 'e')
2274 fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements);
2275 if (fmt[i] == 'E')
2277 register int j;
2278 for (j = 0; j < XVECLEN (x, i); j++)
2279 fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2280 insn, replacements);
2285 /* Given X, an rtx of the form (SUBREG:m1 (MEM:m2 addr)),
2286 return an rtx (MEM:m1 newaddr) which is equivalent.
2287 If any insns must be emitted to compute NEWADDR, put them before INSN.
2289 UNCRITICAL nonzero means accept paradoxical subregs.
2290 This is used for subregs found inside REG_NOTES. */
2292 static rtx
2293 fixup_memory_subreg (x, insn, uncritical)
2294 rtx x;
2295 rtx insn;
2296 int uncritical;
2298 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2299 rtx addr = XEXP (SUBREG_REG (x), 0);
2300 enum machine_mode mode = GET_MODE (x);
2301 rtx saved, result;
2303 /* Paradoxical SUBREGs are usually invalid during RTL generation. */
2304 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
2305 && ! uncritical)
2306 abort ();
2308 if (BYTES_BIG_ENDIAN)
2309 offset += (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2310 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
2311 addr = plus_constant (addr, offset);
2312 if (!flag_force_addr && memory_address_p (mode, addr))
2313 /* Shortcut if no insns need be emitted. */
2314 return change_address (SUBREG_REG (x), mode, addr);
2315 start_sequence ();
2316 result = change_address (SUBREG_REG (x), mode, addr);
2317 emit_insn_before (gen_sequence (), insn);
2318 end_sequence ();
2319 return result;
2322 /* Do fixup_memory_subreg on all (SUBREG (MEM ...) ...) contained in X.
2323 Replace subexpressions of X in place.
2324 If X itself is a (SUBREG (MEM ...) ...), return the replacement expression.
2325 Otherwise return X, with its contents possibly altered.
2327 If any insns must be emitted to compute NEWADDR, put them before INSN.
2329 UNCRITICAL is as in fixup_memory_subreg. */
2331 static rtx
2332 walk_fixup_memory_subreg (x, insn, uncritical)
2333 register rtx x;
2334 rtx insn;
2335 int uncritical;
2337 register enum rtx_code code;
2338 register char *fmt;
2339 register int i;
2341 if (x == 0)
2342 return 0;
2344 code = GET_CODE (x);
2346 if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
2347 return fixup_memory_subreg (x, insn, uncritical);
2349 /* Nothing special about this RTX; fix its operands. */
2351 fmt = GET_RTX_FORMAT (code);
2352 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2354 if (fmt[i] == 'e')
2355 XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn, uncritical);
2356 if (fmt[i] == 'E')
2358 register int j;
2359 for (j = 0; j < XVECLEN (x, i); j++)
2360 XVECEXP (x, i, j)
2361 = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn, uncritical);
2364 return x;
2367 /* For each memory ref within X, if it refers to a stack slot
2368 with an out of range displacement, put the address in a temp register
2369 (emitting new insns before INSN to load these registers)
2370 and alter the memory ref to use that register.
2371 Replace each such MEM rtx with a copy, to avoid clobberage. */
2373 static rtx
2374 fixup_stack_1 (x, insn)
2375 rtx x;
2376 rtx insn;
2378 register int i;
2379 register RTX_CODE code = GET_CODE (x);
2380 register char *fmt;
2382 if (code == MEM)
2384 register rtx ad = XEXP (x, 0);
2385 /* If we have address of a stack slot but it's not valid
2386 (displacement is too large), compute the sum in a register. */
2387 if (GET_CODE (ad) == PLUS
2388 && GET_CODE (XEXP (ad, 0)) == REG
2389 && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2390 && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
2391 || REGNO (XEXP (ad, 0)) == FRAME_POINTER_REGNUM
2392 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2393 || REGNO (XEXP (ad, 0)) == HARD_FRAME_POINTER_REGNUM
2394 #endif
2395 || REGNO (XEXP (ad, 0)) == STACK_POINTER_REGNUM
2396 || REGNO (XEXP (ad, 0)) == ARG_POINTER_REGNUM
2397 || XEXP (ad, 0) == current_function_internal_arg_pointer)
2398 && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2400 rtx temp, seq;
2401 if (memory_address_p (GET_MODE (x), ad))
2402 return x;
2404 start_sequence ();
2405 temp = copy_to_reg (ad);
2406 seq = gen_sequence ();
2407 end_sequence ();
2408 emit_insn_before (seq, insn);
2409 return change_address (x, VOIDmode, temp);
2411 return x;
2414 fmt = GET_RTX_FORMAT (code);
2415 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2417 if (fmt[i] == 'e')
2418 XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
2419 if (fmt[i] == 'E')
2421 register int j;
2422 for (j = 0; j < XVECLEN (x, i); j++)
2423 XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2426 return x;
2429 /* Optimization: a bit-field instruction whose field
2430 happens to be a byte or halfword in memory
2431 can be changed to a move instruction.
2433 We call here when INSN is an insn to examine or store into a bit-field.
2434 BODY is the SET-rtx to be altered.
2436 EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2437 (Currently this is called only from function.c, and EQUIV_MEM
2438 is always 0.) */
2440 static void
2441 optimize_bit_field (body, insn, equiv_mem)
2442 rtx body;
2443 rtx insn;
2444 rtx *equiv_mem;
2446 register rtx bitfield;
2447 int destflag;
2448 rtx seq = 0;
2449 enum machine_mode mode;
2451 if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2452 || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2453 bitfield = SET_DEST (body), destflag = 1;
2454 else
2455 bitfield = SET_SRC (body), destflag = 0;
2457 /* First check that the field being stored has constant size and position
2458 and is in fact a byte or halfword suitably aligned. */
2460 if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2461 && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2462 && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2463 != BLKmode)
2464 && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2466 register rtx memref = 0;
2468 /* Now check that the containing word is memory, not a register,
2469 and that it is safe to change the machine mode. */
2471 if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2472 memref = XEXP (bitfield, 0);
2473 else if (GET_CODE (XEXP (bitfield, 0)) == REG
2474 && equiv_mem != 0)
2475 memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2476 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2477 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2478 memref = SUBREG_REG (XEXP (bitfield, 0));
2479 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2480 && equiv_mem != 0
2481 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == REG)
2482 memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2484 if (memref
2485 && ! mode_dependent_address_p (XEXP (memref, 0))
2486 && ! MEM_VOLATILE_P (memref))
2488 /* Now adjust the address, first for any subreg'ing
2489 that we are now getting rid of,
2490 and then for which byte of the word is wanted. */
2492 register int offset = INTVAL (XEXP (bitfield, 2));
2493 rtx insns;
2495 /* Adjust OFFSET to count bits from low-address byte. */
2496 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2497 offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2498 - offset - INTVAL (XEXP (bitfield, 1)));
2500 /* Adjust OFFSET to count bytes from low-address byte. */
2501 offset /= BITS_PER_UNIT;
2502 if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2504 offset += SUBREG_WORD (XEXP (bitfield, 0)) * UNITS_PER_WORD;
2505 if (BYTES_BIG_ENDIAN)
2506 offset -= (MIN (UNITS_PER_WORD,
2507 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2508 - MIN (UNITS_PER_WORD,
2509 GET_MODE_SIZE (GET_MODE (memref))));
2512 start_sequence ();
2513 memref = change_address (memref, mode,
2514 plus_constant (XEXP (memref, 0), offset));
2515 insns = get_insns ();
2516 end_sequence ();
2517 emit_insns_before (insns, insn);
2519 /* Store this memory reference where
2520 we found the bit field reference. */
2522 if (destflag)
2524 validate_change (insn, &SET_DEST (body), memref, 1);
2525 if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2527 rtx src = SET_SRC (body);
2528 while (GET_CODE (src) == SUBREG
2529 && SUBREG_WORD (src) == 0)
2530 src = SUBREG_REG (src);
2531 if (GET_MODE (src) != GET_MODE (memref))
2532 src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2533 validate_change (insn, &SET_SRC (body), src, 1);
2535 else if (GET_MODE (SET_SRC (body)) != VOIDmode
2536 && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2537 /* This shouldn't happen because anything that didn't have
2538 one of these modes should have got converted explicitly
2539 and then referenced through a subreg.
2540 This is so because the original bit-field was
2541 handled by agg_mode and so its tree structure had
2542 the same mode that memref now has. */
2543 abort ();
2545 else
2547 rtx dest = SET_DEST (body);
2549 while (GET_CODE (dest) == SUBREG
2550 && SUBREG_WORD (dest) == 0
2551 && (GET_MODE_CLASS (GET_MODE (dest))
2552 == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest)))))
2553 dest = SUBREG_REG (dest);
2555 validate_change (insn, &SET_DEST (body), dest, 1);
2557 if (GET_MODE (dest) == GET_MODE (memref))
2558 validate_change (insn, &SET_SRC (body), memref, 1);
2559 else
2561 /* Convert the mem ref to the destination mode. */
2562 rtx newreg = gen_reg_rtx (GET_MODE (dest));
2564 start_sequence ();
2565 convert_move (newreg, memref,
2566 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2567 seq = get_insns ();
2568 end_sequence ();
2570 validate_change (insn, &SET_SRC (body), newreg, 1);
2574 /* See if we can convert this extraction or insertion into
2575 a simple move insn. We might not be able to do so if this
2576 was, for example, part of a PARALLEL.
2578 If we succeed, write out any needed conversions. If we fail,
2579 it is hard to guess why we failed, so don't do anything
2580 special; just let the optimization be suppressed. */
2582 if (apply_change_group () && seq)
2583 emit_insns_before (seq, insn);
2588 /* These routines are responsible for converting virtual register references
2589 to the actual hard register references once RTL generation is complete.
2591 The following four variables are used for communication between the
2592 routines. They contain the offsets of the virtual registers from their
2593 respective hard registers. */
2595 static int in_arg_offset;
2596 static int var_offset;
2597 static int dynamic_offset;
2598 static int out_arg_offset;
2600 /* In most machines, the stack pointer register is equivalent to the bottom
2601 of the stack. */
2603 #ifndef STACK_POINTER_OFFSET
2604 #define STACK_POINTER_OFFSET 0
2605 #endif
2607 /* If not defined, pick an appropriate default for the offset of dynamically
2608 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2609 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
2611 #ifndef STACK_DYNAMIC_OFFSET
2613 #ifdef ACCUMULATE_OUTGOING_ARGS
2614 /* The bottom of the stack points to the actual arguments. If
2615 REG_PARM_STACK_SPACE is defined, this includes the space for the register
2616 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
2617 stack space for register parameters is not pushed by the caller, but
2618 rather part of the fixed stack areas and hence not included in
2619 `current_function_outgoing_args_size'. Nevertheless, we must allow
2620 for it when allocating stack dynamic objects. */
2622 #if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2623 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2624 (current_function_outgoing_args_size \
2625 + REG_PARM_STACK_SPACE (FNDECL) + (STACK_POINTER_OFFSET))
2627 #else
2628 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2629 (current_function_outgoing_args_size + (STACK_POINTER_OFFSET))
2630 #endif
2632 #else
2633 #define STACK_DYNAMIC_OFFSET(FNDECL) STACK_POINTER_OFFSET
2634 #endif
2635 #endif
2637 /* Build up a (MEM (ADDRESSOF (REG))) rtx for a register REG that just had
2638 its address taken. DECL is the decl for the object stored in the
2639 register, for later use if we do need to force REG into the stack.
2640 REG is overwritten by the MEM like in put_reg_into_stack. */
2643 gen_mem_addressof (reg, decl)
2644 rtx reg;
2645 tree decl;
2647 tree type = TREE_TYPE (decl);
2649 rtx r = gen_rtx (ADDRESSOF, Pmode, gen_reg_rtx (GET_MODE (reg)));
2650 ADDRESSOF_REGNO (r) = REGNO (reg);
2651 SET_ADDRESSOF_DECL (r, decl);
2653 XEXP (reg, 0) = r;
2654 PUT_CODE (reg, MEM);
2655 PUT_MODE (reg, DECL_MODE (decl));
2656 MEM_VOLATILE_P (reg) = TREE_SIDE_EFFECTS (decl);
2657 MEM_IN_STRUCT_P (reg) = AGGREGATE_TYPE_P (type);
2659 fixup_var_refs (reg, GET_MODE (reg), TREE_UNSIGNED (type));
2660 return reg;
2663 /* If DECL has an RTL that is an ADDRESSOF rtx, put it into the stack. */
2665 void
2666 flush_addressof (decl)
2667 tree decl;
2669 if ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL)
2670 && DECL_RTL (decl) != 0
2671 && GET_CODE (DECL_RTL (decl)) == MEM
2672 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF
2673 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == REG)
2674 put_addressof_into_stack (XEXP (DECL_RTL (decl), 0));
2677 /* Force the register pointed to by R, an ADDRESSOF rtx, into the stack. */
2679 static void
2680 put_addressof_into_stack (r)
2681 rtx r;
2683 tree decl = ADDRESSOF_DECL (r);
2684 rtx reg = XEXP (r, 0);
2686 if (GET_CODE (reg) != REG)
2687 abort ();
2689 put_reg_into_stack (0, reg, TREE_TYPE (decl), GET_MODE (reg),
2690 DECL_MODE (decl), TREE_SIDE_EFFECTS (decl),
2691 ADDRESSOF_REGNO (r));
2694 /* Helper function for purge_addressof. See if the rtx expression at *LOC
2695 in INSN needs to be changed. If FORCE, always put any ADDRESSOFs into
2696 the stack. */
2698 static void
2699 purge_addressof_1 (loc, insn, force)
2700 rtx *loc;
2701 rtx insn;
2702 int force;
2704 rtx x;
2705 RTX_CODE code;
2706 int i, j;
2707 char *fmt;
2709 /* Re-start here to avoid recursion in common cases. */
2710 restart:
2712 x = *loc;
2713 if (x == 0)
2714 return;
2716 code = GET_CODE (x);
2718 if (code == ADDRESSOF && GET_CODE (XEXP (x, 0)) == MEM)
2720 rtx insns;
2721 /* We must create a copy of the rtx because it was created by
2722 overwriting a REG rtx which is always shared. */
2723 rtx sub = copy_rtx (XEXP (XEXP (x, 0), 0));
2725 if (validate_change (insn, loc, sub, 0))
2726 return;
2728 start_sequence ();
2729 if (! validate_change (insn, loc,
2730 force_operand (sub, NULL_RTX),
2732 abort ();
2734 insns = get_insns ();
2735 end_sequence ();
2736 emit_insns_before (insns, insn);
2737 return;
2739 else if (code == MEM && GET_CODE (XEXP (x, 0)) == ADDRESSOF && ! force)
2741 rtx sub = XEXP (XEXP (x, 0), 0);
2742 if (GET_CODE (sub) != REG)
2743 sub = copy_rtx (sub);
2744 if (GET_CODE (sub) == REG && GET_MODE (x) != GET_MODE (sub))
2746 if (! BYTES_BIG_ENDIAN && ! WORDS_BIG_ENDIAN)
2748 rtx sub2 = gen_rtx (SUBREG, GET_MODE (x), sub, 0);
2749 if (validate_change (insn, loc, sub2, 0))
2750 goto restart;
2753 else if (validate_change (insn, loc, sub, 0))
2754 goto restart;
2755 /* else give up and put it into the stack */
2757 else if (code == ADDRESSOF)
2759 put_addressof_into_stack (x);
2760 return;
2763 /* Scan all subexpressions. */
2764 fmt = GET_RTX_FORMAT (code);
2765 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2767 if (*fmt == 'e')
2768 purge_addressof_1 (&XEXP (x, i), insn, force);
2769 else if (*fmt == 'E')
2770 for (j = 0; j < XVECLEN (x, i); j++)
2771 purge_addressof_1 (&XVECEXP (x, i, j), insn, force);
2775 /* Eliminate all occurrences of ADDRESSOF from INSNS. Elide any remaining
2776 (MEM (ADDRESSOF)) patterns, and force any needed registers into the
2777 stack. */
2779 void
2780 purge_addressof (insns)
2781 rtx insns;
2783 rtx insn;
2784 for (insn = insns; insn; insn = NEXT_INSN (insn))
2785 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
2786 || GET_CODE (insn) == CALL_INSN)
2788 purge_addressof_1 (&PATTERN (insn), insn,
2789 asm_noperands (PATTERN (insn)) > 0);
2790 purge_addressof_1 (&REG_NOTES (insn), NULL_RTX, 0);
2794 /* Pass through the INSNS of function FNDECL and convert virtual register
2795 references to hard register references. */
2797 void
2798 instantiate_virtual_regs (fndecl, insns)
2799 tree fndecl;
2800 rtx insns;
2802 rtx insn;
2803 int i;
2805 /* Compute the offsets to use for this function. */
2806 in_arg_offset = FIRST_PARM_OFFSET (fndecl);
2807 var_offset = STARTING_FRAME_OFFSET;
2808 dynamic_offset = STACK_DYNAMIC_OFFSET (fndecl);
2809 out_arg_offset = STACK_POINTER_OFFSET;
2811 /* Scan all variables and parameters of this function. For each that is
2812 in memory, instantiate all virtual registers if the result is a valid
2813 address. If not, we do it later. That will handle most uses of virtual
2814 regs on many machines. */
2815 instantiate_decls (fndecl, 1);
2817 /* Initialize recognition, indicating that volatile is OK. */
2818 init_recog ();
2820 /* Scan through all the insns, instantiating every virtual register still
2821 present. */
2822 for (insn = insns; insn; insn = NEXT_INSN (insn))
2823 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
2824 || GET_CODE (insn) == CALL_INSN)
2826 instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
2827 instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
2830 /* Instantiate the stack slots for the parm registers, for later use in
2831 addressof elimination. */
2832 for (i = 0; i < max_parm_reg; ++i)
2833 if (parm_reg_stack_loc[i])
2834 instantiate_virtual_regs_1 (&parm_reg_stack_loc[i], NULL_RTX, 0);
2836 /* Now instantiate the remaining register equivalences for debugging info.
2837 These will not be valid addresses. */
2838 instantiate_decls (fndecl, 0);
2840 /* Indicate that, from now on, assign_stack_local should use
2841 frame_pointer_rtx. */
2842 virtuals_instantiated = 1;
2845 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
2846 all virtual registers in their DECL_RTL's.
2848 If VALID_ONLY, do this only if the resulting address is still valid.
2849 Otherwise, always do it. */
2851 static void
2852 instantiate_decls (fndecl, valid_only)
2853 tree fndecl;
2854 int valid_only;
2856 tree decl;
2858 if (DECL_SAVED_INSNS (fndecl))
2859 /* When compiling an inline function, the obstack used for
2860 rtl allocation is the maybepermanent_obstack. Calling
2861 `resume_temporary_allocation' switches us back to that
2862 obstack while we process this function's parameters. */
2863 resume_temporary_allocation ();
2865 /* Process all parameters of the function. */
2866 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
2868 int size = int_size_in_bytes (TREE_TYPE (decl));
2869 instantiate_decl (DECL_RTL (decl), size, valid_only);
2871 /* If the parameter was promoted, then the incoming RTL mode may be
2872 larger than the declared type size. We must use the larger of
2873 the two sizes. */
2874 size = MAX (GET_MODE_SIZE (GET_MODE (DECL_INCOMING_RTL (decl))), size);
2875 instantiate_decl (DECL_INCOMING_RTL (decl), size, valid_only);
2878 /* Now process all variables defined in the function or its subblocks. */
2879 instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
2881 if (DECL_INLINE (fndecl) || DECL_DEFER_OUTPUT (fndecl))
2883 /* Save all rtl allocated for this function by raising the
2884 high-water mark on the maybepermanent_obstack. */
2885 preserve_data ();
2886 /* All further rtl allocation is now done in the current_obstack. */
2887 rtl_in_current_obstack ();
2891 /* Subroutine of instantiate_decls: Process all decls in the given
2892 BLOCK node and all its subblocks. */
2894 static void
2895 instantiate_decls_1 (let, valid_only)
2896 tree let;
2897 int valid_only;
2899 tree t;
2901 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
2902 instantiate_decl (DECL_RTL (t), int_size_in_bytes (TREE_TYPE (t)),
2903 valid_only);
2905 /* Process all subblocks. */
2906 for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
2907 instantiate_decls_1 (t, valid_only);
2910 /* Subroutine of the preceding procedures: Given RTL representing a
2911 decl and the size of the object, do any instantiation required.
2913 If VALID_ONLY is non-zero, it means that the RTL should only be
2914 changed if the new address is valid. */
2916 static void
2917 instantiate_decl (x, size, valid_only)
2918 rtx x;
2919 int size;
2920 int valid_only;
2922 enum machine_mode mode;
2923 rtx addr;
2925 /* If this is not a MEM, no need to do anything. Similarly if the
2926 address is a constant or a register that is not a virtual register. */
2928 if (x == 0 || GET_CODE (x) != MEM)
2929 return;
2931 addr = XEXP (x, 0);
2932 if (CONSTANT_P (addr)
2933 || (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == REG)
2934 || (GET_CODE (addr) == REG
2935 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
2936 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
2937 return;
2939 /* If we should only do this if the address is valid, copy the address.
2940 We need to do this so we can undo any changes that might make the
2941 address invalid. This copy is unfortunate, but probably can't be
2942 avoided. */
2944 if (valid_only)
2945 addr = copy_rtx (addr);
2947 instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
2949 if (valid_only)
2951 /* Now verify that the resulting address is valid for every integer or
2952 floating-point mode up to and including SIZE bytes long. We do this
2953 since the object might be accessed in any mode and frame addresses
2954 are shared. */
2956 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2957 mode != VOIDmode && GET_MODE_SIZE (mode) <= size;
2958 mode = GET_MODE_WIDER_MODE (mode))
2959 if (! memory_address_p (mode, addr))
2960 return;
2962 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
2963 mode != VOIDmode && GET_MODE_SIZE (mode) <= size;
2964 mode = GET_MODE_WIDER_MODE (mode))
2965 if (! memory_address_p (mode, addr))
2966 return;
2969 /* Put back the address now that we have updated it and we either know
2970 it is valid or we don't care whether it is valid. */
2972 XEXP (x, 0) = addr;
2975 /* Given a pointer to a piece of rtx and an optional pointer to the
2976 containing object, instantiate any virtual registers present in it.
2978 If EXTRA_INSNS, we always do the replacement and generate
2979 any extra insns before OBJECT. If it zero, we do nothing if replacement
2980 is not valid.
2982 Return 1 if we either had nothing to do or if we were able to do the
2983 needed replacement. Return 0 otherwise; we only return zero if
2984 EXTRA_INSNS is zero.
2986 We first try some simple transformations to avoid the creation of extra
2987 pseudos. */
2989 static int
2990 instantiate_virtual_regs_1 (loc, object, extra_insns)
2991 rtx *loc;
2992 rtx object;
2993 int extra_insns;
2995 rtx x;
2996 RTX_CODE code;
2997 rtx new = 0;
2998 int offset;
2999 rtx temp;
3000 rtx seq;
3001 int i, j;
3002 char *fmt;
3004 /* Re-start here to avoid recursion in common cases. */
3005 restart:
3007 x = *loc;
3008 if (x == 0)
3009 return 1;
3011 code = GET_CODE (x);
3013 /* Check for some special cases. */
3014 switch (code)
3016 case CONST_INT:
3017 case CONST_DOUBLE:
3018 case CONST:
3019 case SYMBOL_REF:
3020 case CODE_LABEL:
3021 case PC:
3022 case CC0:
3023 case ASM_INPUT:
3024 case ADDR_VEC:
3025 case ADDR_DIFF_VEC:
3026 case RETURN:
3027 return 1;
3029 case SET:
3030 /* We are allowed to set the virtual registers. This means that
3031 that the actual register should receive the source minus the
3032 appropriate offset. This is used, for example, in the handling
3033 of non-local gotos. */
3034 if (SET_DEST (x) == virtual_incoming_args_rtx)
3035 new = arg_pointer_rtx, offset = - in_arg_offset;
3036 else if (SET_DEST (x) == virtual_stack_vars_rtx)
3037 new = frame_pointer_rtx, offset = - var_offset;
3038 else if (SET_DEST (x) == virtual_stack_dynamic_rtx)
3039 new = stack_pointer_rtx, offset = - dynamic_offset;
3040 else if (SET_DEST (x) == virtual_outgoing_args_rtx)
3041 new = stack_pointer_rtx, offset = - out_arg_offset;
3043 if (new)
3045 /* The only valid sources here are PLUS or REG. Just do
3046 the simplest possible thing to handle them. */
3047 if (GET_CODE (SET_SRC (x)) != REG
3048 && GET_CODE (SET_SRC (x)) != PLUS)
3049 abort ();
3051 start_sequence ();
3052 if (GET_CODE (SET_SRC (x)) != REG)
3053 temp = force_operand (SET_SRC (x), NULL_RTX);
3054 else
3055 temp = SET_SRC (x);
3056 temp = force_operand (plus_constant (temp, offset), NULL_RTX);
3057 seq = get_insns ();
3058 end_sequence ();
3060 emit_insns_before (seq, object);
3061 SET_DEST (x) = new;
3063 if (! validate_change (object, &SET_SRC (x), temp, 0)
3064 || ! extra_insns)
3065 abort ();
3067 return 1;
3070 instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
3071 loc = &SET_SRC (x);
3072 goto restart;
3074 case PLUS:
3075 /* Handle special case of virtual register plus constant. */
3076 if (CONSTANT_P (XEXP (x, 1)))
3078 rtx old, new_offset;
3080 /* Check for (plus (plus VIRT foo) (const_int)) first. */
3081 if (GET_CODE (XEXP (x, 0)) == PLUS)
3083 rtx inner = XEXP (XEXP (x, 0), 0);
3085 if (inner == virtual_incoming_args_rtx)
3086 new = arg_pointer_rtx, offset = in_arg_offset;
3087 else if (inner == virtual_stack_vars_rtx)
3088 new = frame_pointer_rtx, offset = var_offset;
3089 else if (inner == virtual_stack_dynamic_rtx)
3090 new = stack_pointer_rtx, offset = dynamic_offset;
3091 else if (inner == virtual_outgoing_args_rtx)
3092 new = stack_pointer_rtx, offset = out_arg_offset;
3093 else
3095 loc = &XEXP (x, 0);
3096 goto restart;
3099 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
3100 extra_insns);
3101 new = gen_rtx (PLUS, Pmode, new, XEXP (XEXP (x, 0), 1));
3104 else if (XEXP (x, 0) == virtual_incoming_args_rtx)
3105 new = arg_pointer_rtx, offset = in_arg_offset;
3106 else if (XEXP (x, 0) == virtual_stack_vars_rtx)
3107 new = frame_pointer_rtx, offset = var_offset;
3108 else if (XEXP (x, 0) == virtual_stack_dynamic_rtx)
3109 new = stack_pointer_rtx, offset = dynamic_offset;
3110 else if (XEXP (x, 0) == virtual_outgoing_args_rtx)
3111 new = stack_pointer_rtx, offset = out_arg_offset;
3112 else
3114 /* We know the second operand is a constant. Unless the
3115 first operand is a REG (which has been already checked),
3116 it needs to be checked. */
3117 if (GET_CODE (XEXP (x, 0)) != REG)
3119 loc = &XEXP (x, 0);
3120 goto restart;
3122 return 1;
3125 new_offset = plus_constant (XEXP (x, 1), offset);
3127 /* If the new constant is zero, try to replace the sum with just
3128 the register. */
3129 if (new_offset == const0_rtx
3130 && validate_change (object, loc, new, 0))
3131 return 1;
3133 /* Next try to replace the register and new offset.
3134 There are two changes to validate here and we can't assume that
3135 in the case of old offset equals new just changing the register
3136 will yield a valid insn. In the interests of a little efficiency,
3137 however, we only call validate change once (we don't queue up the
3138 changes and then call apply_change_group). */
3140 old = XEXP (x, 0);
3141 if (offset == 0
3142 ? ! validate_change (object, &XEXP (x, 0), new, 0)
3143 : (XEXP (x, 0) = new,
3144 ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
3146 if (! extra_insns)
3148 XEXP (x, 0) = old;
3149 return 0;
3152 /* Otherwise copy the new constant into a register and replace
3153 constant with that register. */
3154 temp = gen_reg_rtx (Pmode);
3155 XEXP (x, 0) = new;
3156 if (validate_change (object, &XEXP (x, 1), temp, 0))
3157 emit_insn_before (gen_move_insn (temp, new_offset), object);
3158 else
3160 /* If that didn't work, replace this expression with a
3161 register containing the sum. */
3163 XEXP (x, 0) = old;
3164 new = gen_rtx (PLUS, Pmode, new, new_offset);
3166 start_sequence ();
3167 temp = force_operand (new, NULL_RTX);
3168 seq = get_insns ();
3169 end_sequence ();
3171 emit_insns_before (seq, object);
3172 if (! validate_change (object, loc, temp, 0)
3173 && ! validate_replace_rtx (x, temp, object))
3174 abort ();
3178 return 1;
3181 /* Fall through to generic two-operand expression case. */
3182 case EXPR_LIST:
3183 case CALL:
3184 case COMPARE:
3185 case MINUS:
3186 case MULT:
3187 case DIV: case UDIV:
3188 case MOD: case UMOD:
3189 case AND: case IOR: case XOR:
3190 case ROTATERT: case ROTATE:
3191 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
3192 case NE: case EQ:
3193 case GE: case GT: case GEU: case GTU:
3194 case LE: case LT: case LEU: case LTU:
3195 if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
3196 instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
3197 loc = &XEXP (x, 0);
3198 goto restart;
3200 case MEM:
3201 /* Most cases of MEM that convert to valid addresses have already been
3202 handled by our scan of decls. The only special handling we
3203 need here is to make a copy of the rtx to ensure it isn't being
3204 shared if we have to change it to a pseudo.
3206 If the rtx is a simple reference to an address via a virtual register,
3207 it can potentially be shared. In such cases, first try to make it
3208 a valid address, which can also be shared. Otherwise, copy it and
3209 proceed normally.
3211 First check for common cases that need no processing. These are
3212 usually due to instantiation already being done on a previous instance
3213 of a shared rtx. */
3215 temp = XEXP (x, 0);
3216 if (CONSTANT_ADDRESS_P (temp)
3217 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3218 || temp == arg_pointer_rtx
3219 #endif
3220 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3221 || temp == hard_frame_pointer_rtx
3222 #endif
3223 || temp == frame_pointer_rtx)
3224 return 1;
3226 if (GET_CODE (temp) == PLUS
3227 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3228 && (XEXP (temp, 0) == frame_pointer_rtx
3229 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3230 || XEXP (temp, 0) == hard_frame_pointer_rtx
3231 #endif
3232 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3233 || XEXP (temp, 0) == arg_pointer_rtx
3234 #endif
3236 return 1;
3238 if (temp == virtual_stack_vars_rtx
3239 || temp == virtual_incoming_args_rtx
3240 || (GET_CODE (temp) == PLUS
3241 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3242 && (XEXP (temp, 0) == virtual_stack_vars_rtx
3243 || XEXP (temp, 0) == virtual_incoming_args_rtx)))
3245 /* This MEM may be shared. If the substitution can be done without
3246 the need to generate new pseudos, we want to do it in place
3247 so all copies of the shared rtx benefit. The call below will
3248 only make substitutions if the resulting address is still
3249 valid.
3251 Note that we cannot pass X as the object in the recursive call
3252 since the insn being processed may not allow all valid
3253 addresses. However, if we were not passed on object, we can
3254 only modify X without copying it if X will have a valid
3255 address.
3257 ??? Also note that this can still lose if OBJECT is an insn that
3258 has less restrictions on an address that some other insn.
3259 In that case, we will modify the shared address. This case
3260 doesn't seem very likely, though. One case where this could
3261 happen is in the case of a USE or CLOBBER reference, but we
3262 take care of that below. */
3264 if (instantiate_virtual_regs_1 (&XEXP (x, 0),
3265 object ? object : x, 0))
3266 return 1;
3268 /* Otherwise make a copy and process that copy. We copy the entire
3269 RTL expression since it might be a PLUS which could also be
3270 shared. */
3271 *loc = x = copy_rtx (x);
3274 /* Fall through to generic unary operation case. */
3275 case SUBREG:
3276 case STRICT_LOW_PART:
3277 case NEG: case NOT:
3278 case PRE_DEC: case PRE_INC: case POST_DEC: case POST_INC:
3279 case SIGN_EXTEND: case ZERO_EXTEND:
3280 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3281 case FLOAT: case FIX:
3282 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3283 case ABS:
3284 case SQRT:
3285 case FFS:
3286 /* These case either have just one operand or we know that we need not
3287 check the rest of the operands. */
3288 loc = &XEXP (x, 0);
3289 goto restart;
3291 case USE:
3292 case CLOBBER:
3293 /* If the operand is a MEM, see if the change is a valid MEM. If not,
3294 go ahead and make the invalid one, but do it to a copy. For a REG,
3295 just make the recursive call, since there's no chance of a problem. */
3297 if ((GET_CODE (XEXP (x, 0)) == MEM
3298 && instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), XEXP (x, 0),
3300 || (GET_CODE (XEXP (x, 0)) == REG
3301 && instantiate_virtual_regs_1 (&XEXP (x, 0), object, 0)))
3302 return 1;
3304 XEXP (x, 0) = copy_rtx (XEXP (x, 0));
3305 loc = &XEXP (x, 0);
3306 goto restart;
3308 case REG:
3309 /* Try to replace with a PLUS. If that doesn't work, compute the sum
3310 in front of this insn and substitute the temporary. */
3311 if (x == virtual_incoming_args_rtx)
3312 new = arg_pointer_rtx, offset = in_arg_offset;
3313 else if (x == virtual_stack_vars_rtx)
3314 new = frame_pointer_rtx, offset = var_offset;
3315 else if (x == virtual_stack_dynamic_rtx)
3316 new = stack_pointer_rtx, offset = dynamic_offset;
3317 else if (x == virtual_outgoing_args_rtx)
3318 new = stack_pointer_rtx, offset = out_arg_offset;
3320 if (new)
3322 temp = plus_constant (new, offset);
3323 if (!validate_change (object, loc, temp, 0))
3325 if (! extra_insns)
3326 return 0;
3328 start_sequence ();
3329 temp = force_operand (temp, NULL_RTX);
3330 seq = get_insns ();
3331 end_sequence ();
3333 emit_insns_before (seq, object);
3334 if (! validate_change (object, loc, temp, 0)
3335 && ! validate_replace_rtx (x, temp, object))
3336 abort ();
3340 return 1;
3342 case ADDRESSOF:
3343 if (GET_CODE (XEXP (x, 0)) == REG)
3344 return 1;
3346 else if (GET_CODE (XEXP (x, 0)) == MEM)
3348 /* If we have a (addressof (mem ..)), do any instantiation inside
3349 since we know we'll be making the inside valid when we finally
3350 remove the ADDRESSOF. */
3351 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), NULL_RTX, 0);
3352 return 1;
3354 break;
3356 default:
3357 break;
3360 /* Scan all subexpressions. */
3361 fmt = GET_RTX_FORMAT (code);
3362 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3363 if (*fmt == 'e')
3365 if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
3366 return 0;
3368 else if (*fmt == 'E')
3369 for (j = 0; j < XVECLEN (x, i); j++)
3370 if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
3371 extra_insns))
3372 return 0;
3374 return 1;
3377 /* Optimization: assuming this function does not receive nonlocal gotos,
3378 delete the handlers for such, as well as the insns to establish
3379 and disestablish them. */
3381 static void
3382 delete_handlers ()
3384 rtx insn;
3385 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3387 /* Delete the handler by turning off the flag that would
3388 prevent jump_optimize from deleting it.
3389 Also permit deletion of the nonlocal labels themselves
3390 if nothing local refers to them. */
3391 if (GET_CODE (insn) == CODE_LABEL)
3393 tree t, last_t;
3395 LABEL_PRESERVE_P (insn) = 0;
3397 /* Remove it from the nonlocal_label list, to avoid confusing
3398 flow. */
3399 for (t = nonlocal_labels, last_t = 0; t;
3400 last_t = t, t = TREE_CHAIN (t))
3401 if (DECL_RTL (TREE_VALUE (t)) == insn)
3402 break;
3403 if (t)
3405 if (! last_t)
3406 nonlocal_labels = TREE_CHAIN (nonlocal_labels);
3407 else
3408 TREE_CHAIN (last_t) = TREE_CHAIN (t);
3411 if (GET_CODE (insn) == INSN
3412 && ((nonlocal_goto_handler_slot != 0
3413 && reg_mentioned_p (nonlocal_goto_handler_slot, PATTERN (insn)))
3414 || (nonlocal_goto_stack_level != 0
3415 && reg_mentioned_p (nonlocal_goto_stack_level,
3416 PATTERN (insn)))))
3417 delete_insn (insn);
3421 /* Return a list (chain of EXPR_LIST nodes) for the nonlocal labels
3422 of the current function. */
3425 nonlocal_label_rtx_list ()
3427 tree t;
3428 rtx x = 0;
3430 for (t = nonlocal_labels; t; t = TREE_CHAIN (t))
3431 x = gen_rtx (EXPR_LIST, VOIDmode, label_rtx (TREE_VALUE (t)), x);
3433 return x;
3436 /* Output a USE for any register use in RTL.
3437 This is used with -noreg to mark the extent of lifespan
3438 of any registers used in a user-visible variable's DECL_RTL. */
3440 void
3441 use_variable (rtl)
3442 rtx rtl;
3444 if (GET_CODE (rtl) == REG)
3445 /* This is a register variable. */
3446 emit_insn (gen_rtx (USE, VOIDmode, rtl));
3447 else if (GET_CODE (rtl) == MEM
3448 && GET_CODE (XEXP (rtl, 0)) == REG
3449 && (REGNO (XEXP (rtl, 0)) < FIRST_VIRTUAL_REGISTER
3450 || REGNO (XEXP (rtl, 0)) > LAST_VIRTUAL_REGISTER)
3451 && XEXP (rtl, 0) != current_function_internal_arg_pointer)
3452 /* This is a variable-sized structure. */
3453 emit_insn (gen_rtx (USE, VOIDmode, XEXP (rtl, 0)));
3456 /* Like use_variable except that it outputs the USEs after INSN
3457 instead of at the end of the insn-chain. */
3459 void
3460 use_variable_after (rtl, insn)
3461 rtx rtl, insn;
3463 if (GET_CODE (rtl) == REG)
3464 /* This is a register variable. */
3465 emit_insn_after (gen_rtx (USE, VOIDmode, rtl), insn);
3466 else if (GET_CODE (rtl) == MEM
3467 && GET_CODE (XEXP (rtl, 0)) == REG
3468 && (REGNO (XEXP (rtl, 0)) < FIRST_VIRTUAL_REGISTER
3469 || REGNO (XEXP (rtl, 0)) > LAST_VIRTUAL_REGISTER)
3470 && XEXP (rtl, 0) != current_function_internal_arg_pointer)
3471 /* This is a variable-sized structure. */
3472 emit_insn_after (gen_rtx (USE, VOIDmode, XEXP (rtl, 0)), insn);
3476 max_parm_reg_num ()
3478 return max_parm_reg;
3481 /* Return the first insn following those generated by `assign_parms'. */
3484 get_first_nonparm_insn ()
3486 if (last_parm_insn)
3487 return NEXT_INSN (last_parm_insn);
3488 return get_insns ();
3491 /* Return the first NOTE_INSN_BLOCK_BEG note in the function.
3492 Crash if there is none. */
3495 get_first_block_beg ()
3497 register rtx searcher;
3498 register rtx insn = get_first_nonparm_insn ();
3500 for (searcher = insn; searcher; searcher = NEXT_INSN (searcher))
3501 if (GET_CODE (searcher) == NOTE
3502 && NOTE_LINE_NUMBER (searcher) == NOTE_INSN_BLOCK_BEG)
3503 return searcher;
3505 abort (); /* Invalid call to this function. (See comments above.) */
3506 return NULL_RTX;
3509 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
3510 This means a type for which function calls must pass an address to the
3511 function or get an address back from the function.
3512 EXP may be a type node or an expression (whose type is tested). */
3515 aggregate_value_p (exp)
3516 tree exp;
3518 int i, regno, nregs;
3519 rtx reg;
3520 tree type;
3521 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 't')
3522 type = exp;
3523 else
3524 type = TREE_TYPE (exp);
3526 if (RETURN_IN_MEMORY (type))
3527 return 1;
3528 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
3529 and thus can't be returned in registers. */
3530 if (TREE_ADDRESSABLE (type))
3531 return 1;
3532 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
3533 return 1;
3534 /* Make sure we have suitable call-clobbered regs to return
3535 the value in; if not, we must return it in memory. */
3536 reg = hard_function_value (type, 0);
3538 /* If we have something other than a REG (e.g. a PARALLEL), then assume
3539 it is OK. */
3540 if (GET_CODE (reg) != REG)
3541 return 0;
3543 regno = REGNO (reg);
3544 nregs = HARD_REGNO_NREGS (regno, TYPE_MODE (type));
3545 for (i = 0; i < nregs; i++)
3546 if (! call_used_regs[regno + i])
3547 return 1;
3548 return 0;
3551 /* Assign RTL expressions to the function's parameters.
3552 This may involve copying them into registers and using
3553 those registers as the RTL for them.
3555 If SECOND_TIME is non-zero it means that this function is being
3556 called a second time. This is done by integrate.c when a function's
3557 compilation is deferred. We need to come back here in case the
3558 FUNCTION_ARG macro computes items needed for the rest of the compilation
3559 (such as changing which registers are fixed or caller-saved). But suppress
3560 writing any insns or setting DECL_RTL of anything in this case. */
3562 void
3563 assign_parms (fndecl, second_time)
3564 tree fndecl;
3565 int second_time;
3567 register tree parm;
3568 register rtx entry_parm = 0;
3569 register rtx stack_parm = 0;
3570 CUMULATIVE_ARGS args_so_far;
3571 enum machine_mode promoted_mode, passed_mode;
3572 enum machine_mode nominal_mode, promoted_nominal_mode;
3573 int unsignedp;
3574 /* Total space needed so far for args on the stack,
3575 given as a constant and a tree-expression. */
3576 struct args_size stack_args_size;
3577 tree fntype = TREE_TYPE (fndecl);
3578 tree fnargs = DECL_ARGUMENTS (fndecl);
3579 /* This is used for the arg pointer when referring to stack args. */
3580 rtx internal_arg_pointer;
3581 /* This is a dummy PARM_DECL that we used for the function result if
3582 the function returns a structure. */
3583 tree function_result_decl = 0;
3584 int varargs_setup = 0;
3585 rtx conversion_insns = 0;
3587 /* Nonzero if the last arg is named `__builtin_va_alist',
3588 which is used on some machines for old-fashioned non-ANSI varargs.h;
3589 this should be stuck onto the stack as if it had arrived there. */
3590 int hide_last_arg
3591 = (current_function_varargs
3592 && fnargs
3593 && (parm = tree_last (fnargs)) != 0
3594 && DECL_NAME (parm)
3595 && (! strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
3596 "__builtin_va_alist")));
3598 /* Nonzero if function takes extra anonymous args.
3599 This means the last named arg must be on the stack
3600 right before the anonymous ones. */
3601 int stdarg
3602 = (TYPE_ARG_TYPES (fntype) != 0
3603 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3604 != void_type_node));
3606 current_function_stdarg = stdarg;
3608 /* If the reg that the virtual arg pointer will be translated into is
3609 not a fixed reg or is the stack pointer, make a copy of the virtual
3610 arg pointer, and address parms via the copy. The frame pointer is
3611 considered fixed even though it is not marked as such.
3613 The second time through, simply use ap to avoid generating rtx. */
3615 if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
3616 || ! (fixed_regs[ARG_POINTER_REGNUM]
3617 || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM))
3618 && ! second_time)
3619 internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
3620 else
3621 internal_arg_pointer = virtual_incoming_args_rtx;
3622 current_function_internal_arg_pointer = internal_arg_pointer;
3624 stack_args_size.constant = 0;
3625 stack_args_size.var = 0;
3627 /* If struct value address is treated as the first argument, make it so. */
3628 if (aggregate_value_p (DECL_RESULT (fndecl))
3629 && ! current_function_returns_pcc_struct
3630 && struct_value_incoming_rtx == 0)
3632 tree type = build_pointer_type (TREE_TYPE (fntype));
3634 function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
3636 DECL_ARG_TYPE (function_result_decl) = type;
3637 TREE_CHAIN (function_result_decl) = fnargs;
3638 fnargs = function_result_decl;
3641 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
3642 parm_reg_stack_loc = (rtx *) savealloc (max_parm_reg * sizeof (rtx));
3643 bzero ((char *) parm_reg_stack_loc, max_parm_reg * sizeof (rtx));
3645 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
3646 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
3647 #else
3648 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, 0);
3649 #endif
3651 /* We haven't yet found an argument that we must push and pretend the
3652 caller did. */
3653 current_function_pretend_args_size = 0;
3655 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
3657 int aggregate = AGGREGATE_TYPE_P (TREE_TYPE (parm));
3658 struct args_size stack_offset;
3659 struct args_size arg_size;
3660 int passed_pointer = 0;
3661 int did_conversion = 0;
3662 tree passed_type = DECL_ARG_TYPE (parm);
3663 tree nominal_type = TREE_TYPE (parm);
3665 /* Set LAST_NAMED if this is last named arg before some
3666 anonymous args. We treat it as if it were anonymous too. */
3667 int last_named = ((TREE_CHAIN (parm) == 0
3668 || DECL_NAME (TREE_CHAIN (parm)) == 0)
3669 && (stdarg || current_function_varargs));
3671 if (TREE_TYPE (parm) == error_mark_node
3672 /* This can happen after weird syntax errors
3673 or if an enum type is defined among the parms. */
3674 || TREE_CODE (parm) != PARM_DECL
3675 || passed_type == NULL)
3677 DECL_INCOMING_RTL (parm) = DECL_RTL (parm) = gen_rtx (MEM, BLKmode,
3678 const0_rtx);
3679 TREE_USED (parm) = 1;
3680 continue;
3683 /* For varargs.h function, save info about regs and stack space
3684 used by the individual args, not including the va_alist arg. */
3685 if (hide_last_arg && last_named)
3686 current_function_args_info = args_so_far;
3688 /* Find mode of arg as it is passed, and mode of arg
3689 as it should be during execution of this function. */
3690 passed_mode = TYPE_MODE (passed_type);
3691 nominal_mode = TYPE_MODE (nominal_type);
3693 /* If the parm's mode is VOID, its value doesn't matter,
3694 and avoid the usual things like emit_move_insn that could crash. */
3695 if (nominal_mode == VOIDmode)
3697 DECL_INCOMING_RTL (parm) = DECL_RTL (parm) = const0_rtx;
3698 continue;
3701 /* If the parm is to be passed as a transparent union, use the
3702 type of the first field for the tests below. We have already
3703 verified that the modes are the same. */
3704 if (DECL_TRANSPARENT_UNION (parm)
3705 || TYPE_TRANSPARENT_UNION (passed_type))
3706 passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
3708 /* See if this arg was passed by invisible reference. It is if
3709 it is an object whose size depends on the contents of the
3710 object itself or if the machine requires these objects be passed
3711 that way. */
3713 if ((TREE_CODE (TYPE_SIZE (passed_type)) != INTEGER_CST
3714 && contains_placeholder_p (TYPE_SIZE (passed_type)))
3715 || TREE_ADDRESSABLE (passed_type)
3716 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
3717 || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
3718 passed_type, ! last_named)
3719 #endif
3722 passed_type = nominal_type = build_pointer_type (passed_type);
3723 passed_pointer = 1;
3724 passed_mode = nominal_mode = Pmode;
3727 promoted_mode = passed_mode;
3729 #ifdef PROMOTE_FUNCTION_ARGS
3730 /* Compute the mode in which the arg is actually extended to. */
3731 promoted_mode = promote_mode (passed_type, promoted_mode, &unsignedp, 1);
3732 #endif
3734 /* Let machine desc say which reg (if any) the parm arrives in.
3735 0 means it arrives on the stack. */
3736 #ifdef FUNCTION_INCOMING_ARG
3737 entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
3738 passed_type, ! last_named);
3739 #else
3740 entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
3741 passed_type, ! last_named);
3742 #endif
3744 if (entry_parm == 0)
3745 promoted_mode = passed_mode;
3747 #ifdef SETUP_INCOMING_VARARGS
3748 /* If this is the last named parameter, do any required setup for
3749 varargs or stdargs. We need to know about the case of this being an
3750 addressable type, in which case we skip the registers it
3751 would have arrived in.
3753 For stdargs, LAST_NAMED will be set for two parameters, the one that
3754 is actually the last named, and the dummy parameter. We only
3755 want to do this action once.
3757 Also, indicate when RTL generation is to be suppressed. */
3758 if (last_named && !varargs_setup)
3760 SETUP_INCOMING_VARARGS (args_so_far, promoted_mode, passed_type,
3761 current_function_pretend_args_size,
3762 second_time);
3763 varargs_setup = 1;
3765 #endif
3767 /* Determine parm's home in the stack,
3768 in case it arrives in the stack or we should pretend it did.
3770 Compute the stack position and rtx where the argument arrives
3771 and its size.
3773 There is one complexity here: If this was a parameter that would
3774 have been passed in registers, but wasn't only because it is
3775 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
3776 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
3777 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
3778 0 as it was the previous time. */
3780 locate_and_pad_parm (promoted_mode, passed_type,
3781 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3783 #else
3784 #ifdef FUNCTION_INCOMING_ARG
3785 FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
3786 passed_type,
3787 (! last_named
3788 || varargs_setup)) != 0,
3789 #else
3790 FUNCTION_ARG (args_so_far, promoted_mode,
3791 passed_type,
3792 ! last_named || varargs_setup) != 0,
3793 #endif
3794 #endif
3795 fndecl, &stack_args_size, &stack_offset, &arg_size);
3797 if (! second_time)
3799 rtx offset_rtx = ARGS_SIZE_RTX (stack_offset);
3801 if (offset_rtx == const0_rtx)
3802 stack_parm = gen_rtx (MEM, promoted_mode, internal_arg_pointer);
3803 else
3804 stack_parm = gen_rtx (MEM, promoted_mode,
3805 gen_rtx (PLUS, Pmode,
3806 internal_arg_pointer, offset_rtx));
3808 /* If this is a memory ref that contains aggregate components,
3809 mark it as such for cse and loop optimize. Likewise if it
3810 is readonly. */
3811 MEM_IN_STRUCT_P (stack_parm) = aggregate;
3812 RTX_UNCHANGING_P (stack_parm) = TREE_READONLY (parm);
3815 /* If this parameter was passed both in registers and in the stack,
3816 use the copy on the stack. */
3817 if (MUST_PASS_IN_STACK (promoted_mode, passed_type))
3818 entry_parm = 0;
3820 #ifdef FUNCTION_ARG_PARTIAL_NREGS
3821 /* If this parm was passed part in regs and part in memory,
3822 pretend it arrived entirely in memory
3823 by pushing the register-part onto the stack.
3825 In the special case of a DImode or DFmode that is split,
3826 we could put it together in a pseudoreg directly,
3827 but for now that's not worth bothering with. */
3829 if (entry_parm)
3831 int nregs = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, promoted_mode,
3832 passed_type, ! last_named);
3834 if (nregs > 0)
3836 current_function_pretend_args_size
3837 = (((nregs * UNITS_PER_WORD) + (PARM_BOUNDARY / BITS_PER_UNIT) - 1)
3838 / (PARM_BOUNDARY / BITS_PER_UNIT)
3839 * (PARM_BOUNDARY / BITS_PER_UNIT));
3841 if (! second_time)
3843 /* Handle calls that pass values in multiple non-contiguous
3844 locations. The Irix 6 ABI has examples of this. */
3845 if (GET_CODE (entry_parm) == PARALLEL)
3846 emit_group_store (validize_mem (stack_parm),
3847 entry_parm);
3848 else
3849 move_block_from_reg (REGNO (entry_parm),
3850 validize_mem (stack_parm), nregs,
3851 int_size_in_bytes (TREE_TYPE (parm)));
3853 entry_parm = stack_parm;
3856 #endif
3858 /* If we didn't decide this parm came in a register,
3859 by default it came on the stack. */
3860 if (entry_parm == 0)
3861 entry_parm = stack_parm;
3863 /* Record permanently how this parm was passed. */
3864 if (! second_time)
3865 DECL_INCOMING_RTL (parm) = entry_parm;
3867 /* If there is actually space on the stack for this parm,
3868 count it in stack_args_size; otherwise set stack_parm to 0
3869 to indicate there is no preallocated stack slot for the parm. */
3871 if (entry_parm == stack_parm
3872 #if defined (REG_PARM_STACK_SPACE) && ! defined (MAYBE_REG_PARM_STACK_SPACE)
3873 /* On some machines, even if a parm value arrives in a register
3874 there is still an (uninitialized) stack slot allocated for it.
3876 ??? When MAYBE_REG_PARM_STACK_SPACE is defined, we can't tell
3877 whether this parameter already has a stack slot allocated,
3878 because an arg block exists only if current_function_args_size
3879 is larger than some threshold, and we haven't calculated that
3880 yet. So, for now, we just assume that stack slots never exist
3881 in this case. */
3882 || REG_PARM_STACK_SPACE (fndecl) > 0
3883 #endif
3886 stack_args_size.constant += arg_size.constant;
3887 if (arg_size.var)
3888 ADD_PARM_SIZE (stack_args_size, arg_size.var);
3890 else
3891 /* No stack slot was pushed for this parm. */
3892 stack_parm = 0;
3894 /* Update info on where next arg arrives in registers. */
3896 FUNCTION_ARG_ADVANCE (args_so_far, promoted_mode,
3897 passed_type, ! last_named);
3899 /* If this is our second time through, we are done with this parm. */
3900 if (second_time)
3901 continue;
3903 /* If we can't trust the parm stack slot to be aligned enough
3904 for its ultimate type, don't use that slot after entry.
3905 We'll make another stack slot, if we need one. */
3907 int thisparm_boundary
3908 = FUNCTION_ARG_BOUNDARY (promoted_mode, passed_type);
3910 if (GET_MODE_ALIGNMENT (nominal_mode) > thisparm_boundary)
3911 stack_parm = 0;
3914 /* If parm was passed in memory, and we need to convert it on entry,
3915 don't store it back in that same slot. */
3916 if (entry_parm != 0
3917 && nominal_mode != BLKmode && nominal_mode != passed_mode)
3918 stack_parm = 0;
3920 #if 0
3921 /* Now adjust STACK_PARM to the mode and precise location
3922 where this parameter should live during execution,
3923 if we discover that it must live in the stack during execution.
3924 To make debuggers happier on big-endian machines, we store
3925 the value in the last bytes of the space available. */
3927 if (nominal_mode != BLKmode && nominal_mode != passed_mode
3928 && stack_parm != 0)
3930 rtx offset_rtx;
3932 if (BYTES_BIG_ENDIAN
3933 && GET_MODE_SIZE (nominal_mode) < UNITS_PER_WORD)
3934 stack_offset.constant += (GET_MODE_SIZE (passed_mode)
3935 - GET_MODE_SIZE (nominal_mode));
3937 offset_rtx = ARGS_SIZE_RTX (stack_offset);
3938 if (offset_rtx == const0_rtx)
3939 stack_parm = gen_rtx (MEM, nominal_mode, internal_arg_pointer);
3940 else
3941 stack_parm = gen_rtx (MEM, nominal_mode,
3942 gen_rtx (PLUS, Pmode,
3943 if (flag_check_memory_usage)
3945 push_to_sequence (conversion_insns);
3946 emit_library_call (chkr_set_right_libfunc, 1, VOIDmode, 3,
3947 XEXP (stack_parm, 0), ptr_mode,
3948 GEN_INT (int_size_in_bytes
3949 (TREE_TYPE (parm))),
3950 TYPE_MODE (sizetype),
3951 GEN_INT (MEMORY_USE_RW),
3952 TYPE_MODE (integer_type_node));
3953 conversion_insns = get_insns ();
3954 end_sequence ();
3956 internal_arg_pointer, offset_rtx));
3958 /* If this is a memory ref that contains aggregate components,
3959 mark it as such for cse and loop optimize. */
3960 MEM_IN_STRUCT_P (stack_parm) = aggregate;
3962 #endif /* 0 */
3964 #ifdef STACK_REGS
3965 /* We need this "use" info, because the gcc-register->stack-register
3966 converter in reg-stack.c needs to know which registers are active
3967 at the start of the function call. The actual parameter loading
3968 instructions are not always available then anymore, since they might
3969 have been optimised away. */
3971 if (GET_CODE (entry_parm) == REG && !(hide_last_arg && last_named))
3972 emit_insn (gen_rtx (USE, GET_MODE (entry_parm), entry_parm));
3973 #endif
3975 /* ENTRY_PARM is an RTX for the parameter as it arrives,
3976 in the mode in which it arrives.
3977 STACK_PARM is an RTX for a stack slot where the parameter can live
3978 during the function (in case we want to put it there).
3979 STACK_PARM is 0 if no stack slot was pushed for it.
3981 Now output code if necessary to convert ENTRY_PARM to
3982 the type in which this function declares it,
3983 and store that result in an appropriate place,
3984 which may be a pseudo reg, may be STACK_PARM,
3985 or may be a local stack slot if STACK_PARM is 0.
3987 Set DECL_RTL to that place. */
3989 if (nominal_mode == BLKmode || GET_CODE (entry_parm) == PARALLEL)
3991 /* If a BLKmode arrives in registers, copy it to a stack slot.
3992 Handle calls that pass values in multiple non-contiguous
3993 locations. The Irix 6 ABI has examples of this. */
3994 if (GET_CODE (entry_parm) == REG
3995 || GET_CODE (entry_parm) == PARALLEL)
3997 int size_stored
3998 = CEIL_ROUND (int_size_in_bytes (TREE_TYPE (parm)),
3999 UNITS_PER_WORD);
4001 /* Note that we will be storing an integral number of words.
4002 So we have to be careful to ensure that we allocate an
4003 integral number of words. We do this below in the
4004 assign_stack_local if space was not allocated in the argument
4005 list. If it was, this will not work if PARM_BOUNDARY is not
4006 a multiple of BITS_PER_WORD. It isn't clear how to fix this
4007 if it becomes a problem. */
4009 if (stack_parm == 0)
4011 stack_parm
4012 = assign_stack_local (GET_MODE (entry_parm),
4013 size_stored, 0);
4015 /* If this is a memory ref that contains aggregate
4016 components, mark it as such for cse and loop optimize. */
4017 MEM_IN_STRUCT_P (stack_parm) = aggregate;
4020 else if (PARM_BOUNDARY % BITS_PER_WORD != 0)
4021 abort ();
4023 if (TREE_READONLY (parm))
4024 RTX_UNCHANGING_P (stack_parm) = 1;
4026 /* Handle calls that pass values in multiple non-contiguous
4027 locations. The Irix 6 ABI has examples of this. */
4028 if (GET_CODE (entry_parm) == PARALLEL)
4029 emit_group_store (validize_mem (stack_parm), entry_parm);
4030 else
4031 move_block_from_reg (REGNO (entry_parm),
4032 validize_mem (stack_parm),
4033 size_stored / UNITS_PER_WORD,
4034 int_size_in_bytes (TREE_TYPE (parm)));
4036 DECL_RTL (parm) = stack_parm;
4038 else if (! ((obey_regdecls && ! DECL_REGISTER (parm)
4039 && ! DECL_INLINE (fndecl))
4040 /* layout_decl may set this. */
4041 || TREE_ADDRESSABLE (parm)
4042 || TREE_SIDE_EFFECTS (parm)
4043 /* If -ffloat-store specified, don't put explicit
4044 float variables into registers. */
4045 || (flag_float_store
4046 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
4047 /* Always assign pseudo to structure return or item passed
4048 by invisible reference. */
4049 || passed_pointer || parm == function_result_decl)
4051 /* Store the parm in a pseudoregister during the function, but we
4052 may need to do it in a wider mode. */
4054 register rtx parmreg;
4055 int regno, regnoi, regnor;
4057 unsignedp = TREE_UNSIGNED (TREE_TYPE (parm));
4059 promoted_nominal_mode
4060 = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
4062 parmreg = gen_reg_rtx (promoted_nominal_mode);
4063 mark_user_reg (parmreg);
4065 /* If this was an item that we received a pointer to, set DECL_RTL
4066 appropriately. */
4067 if (passed_pointer)
4069 DECL_RTL (parm)
4070 = gen_rtx (MEM, TYPE_MODE (TREE_TYPE (passed_type)), parmreg);
4071 MEM_IN_STRUCT_P (DECL_RTL (parm)) = aggregate;
4073 else
4074 DECL_RTL (parm) = parmreg;
4076 /* Copy the value into the register. */
4077 if (nominal_mode != passed_mode
4078 || promoted_nominal_mode != promoted_mode)
4080 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
4081 mode, by the caller. We now have to convert it to
4082 NOMINAL_MODE, if different. However, PARMREG may be in
4083 a different mode than NOMINAL_MODE if it is being stored
4084 promoted.
4086 If ENTRY_PARM is a hard register, it might be in a register
4087 not valid for operating in its mode (e.g., an odd-numbered
4088 register for a DFmode). In that case, moves are the only
4089 thing valid, so we can't do a convert from there. This
4090 occurs when the calling sequence allow such misaligned
4091 usages.
4093 In addition, the conversion may involve a call, which could
4094 clobber parameters which haven't been copied to pseudo
4095 registers yet. Therefore, we must first copy the parm to
4096 a pseudo reg here, and save the conversion until after all
4097 parameters have been moved. */
4099 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4101 emit_move_insn (tempreg, validize_mem (entry_parm));
4103 push_to_sequence (conversion_insns);
4104 tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
4106 expand_assignment (parm,
4107 make_tree (nominal_type, tempreg), 0, 0);
4108 conversion_insns = get_insns ();
4109 did_conversion = 1;
4110 end_sequence ();
4112 else
4113 emit_move_insn (parmreg, validize_mem (entry_parm));
4115 /* If we were passed a pointer but the actual value
4116 can safely live in a register, put it in one. */
4117 if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
4118 && ! ((obey_regdecls && ! DECL_REGISTER (parm)
4119 && ! DECL_INLINE (fndecl))
4120 /* layout_decl may set this. */
4121 || TREE_ADDRESSABLE (parm)
4122 || TREE_SIDE_EFFECTS (parm)
4123 /* If -ffloat-store specified, don't put explicit
4124 float variables into registers. */
4125 || (flag_float_store
4126 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE)))
4128 /* We can't use nominal_mode, because it will have been set to
4129 Pmode above. We must use the actual mode of the parm. */
4130 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
4131 mark_user_reg (parmreg);
4132 emit_move_insn (parmreg, DECL_RTL (parm));
4133 DECL_RTL (parm) = parmreg;
4134 /* STACK_PARM is the pointer, not the parm, and PARMREG is
4135 now the parm. */
4136 stack_parm = 0;
4138 #ifdef FUNCTION_ARG_CALLEE_COPIES
4139 /* If we are passed an arg by reference and it is our responsibility
4140 to make a copy, do it now.
4141 PASSED_TYPE and PASSED mode now refer to the pointer, not the
4142 original argument, so we must recreate them in the call to
4143 FUNCTION_ARG_CALLEE_COPIES. */
4144 /* ??? Later add code to handle the case that if the argument isn't
4145 modified, don't do the copy. */
4147 else if (passed_pointer
4148 && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
4149 TYPE_MODE (DECL_ARG_TYPE (parm)),
4150 DECL_ARG_TYPE (parm),
4151 ! last_named)
4152 && ! TREE_ADDRESSABLE (DECL_ARG_TYPE (parm)))
4154 rtx copy;
4155 tree type = DECL_ARG_TYPE (parm);
4157 /* This sequence may involve a library call perhaps clobbering
4158 registers that haven't been copied to pseudos yet. */
4160 push_to_sequence (conversion_insns);
4162 if (TYPE_SIZE (type) == 0
4163 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4164 /* This is a variable sized object. */
4165 copy = gen_rtx (MEM, BLKmode,
4166 allocate_dynamic_stack_space
4167 (expr_size (parm), NULL_RTX,
4168 TYPE_ALIGN (type)));
4169 else
4170 copy = assign_stack_temp (TYPE_MODE (type),
4171 int_size_in_bytes (type), 1);
4172 MEM_IN_STRUCT_P (copy) = AGGREGATE_TYPE_P (type);
4173 RTX_UNCHANGING_P (copy) = TREE_READONLY (parm);
4175 store_expr (parm, copy, 0);
4176 emit_move_insn (parmreg, XEXP (copy, 0));
4177 if (flag_check_memory_usage)
4178 emit_library_call (chkr_set_right_libfunc, 1, VOIDmode, 3,
4179 XEXP (copy, 0), ptr_mode,
4180 GEN_INT (int_size_in_bytes (type)),
4181 TYPE_MODE (sizetype),
4182 GEN_INT (MEMORY_USE_RW),
4183 TYPE_MODE (integer_type_node));
4184 conversion_insns = get_insns ();
4185 did_conversion = 1;
4186 end_sequence ();
4188 #endif /* FUNCTION_ARG_CALLEE_COPIES */
4190 /* In any case, record the parm's desired stack location
4191 in case we later discover it must live in the stack.
4193 If it is a COMPLEX value, store the stack location for both
4194 halves. */
4196 if (GET_CODE (parmreg) == CONCAT)
4197 regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
4198 else
4199 regno = REGNO (parmreg);
4201 if (regno >= max_parm_reg)
4203 rtx *new;
4204 int old_max_parm_reg = max_parm_reg;
4206 /* It's slow to expand this one register at a time,
4207 but it's also rare and we need max_parm_reg to be
4208 precisely correct. */
4209 max_parm_reg = regno + 1;
4210 new = (rtx *) savealloc (max_parm_reg * sizeof (rtx));
4211 bcopy ((char *) parm_reg_stack_loc, (char *) new,
4212 old_max_parm_reg * sizeof (rtx));
4213 bzero ((char *) (new + old_max_parm_reg),
4214 (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
4215 parm_reg_stack_loc = new;
4218 if (GET_CODE (parmreg) == CONCAT)
4220 enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
4222 regnor = REGNO (gen_realpart (submode, parmreg));
4223 regnoi = REGNO (gen_imagpart (submode, parmreg));
4225 if (stack_parm != 0)
4227 parm_reg_stack_loc[regnor]
4228 = gen_realpart (submode, stack_parm);
4229 parm_reg_stack_loc[regnoi]
4230 = gen_imagpart (submode, stack_parm);
4232 else
4234 parm_reg_stack_loc[regnor] = 0;
4235 parm_reg_stack_loc[regnoi] = 0;
4238 else
4239 parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
4241 /* Mark the register as eliminable if we did no conversion
4242 and it was copied from memory at a fixed offset,
4243 and the arg pointer was not copied to a pseudo-reg.
4244 If the arg pointer is a pseudo reg or the offset formed
4245 an invalid address, such memory-equivalences
4246 as we make here would screw up life analysis for it. */
4247 if (nominal_mode == passed_mode
4248 && ! did_conversion
4249 && stack_parm != 0
4250 && GET_CODE (stack_parm) == MEM
4251 && stack_offset.var == 0
4252 && reg_mentioned_p (virtual_incoming_args_rtx,
4253 XEXP (stack_parm, 0)))
4255 rtx linsn = get_last_insn ();
4256 rtx sinsn, set;
4258 /* Mark complex types separately. */
4259 if (GET_CODE (parmreg) == CONCAT)
4260 /* Scan backwards for the set of the real and
4261 imaginary parts. */
4262 for (sinsn = linsn; sinsn != 0;
4263 sinsn = prev_nonnote_insn (sinsn))
4265 set = single_set (sinsn);
4266 if (set != 0
4267 && SET_DEST (set) == regno_reg_rtx [regnoi])
4268 REG_NOTES (sinsn)
4269 = gen_rtx (EXPR_LIST, REG_EQUIV,
4270 parm_reg_stack_loc[regnoi],
4271 REG_NOTES (sinsn));
4272 else if (set != 0
4273 && SET_DEST (set) == regno_reg_rtx [regnor])
4274 REG_NOTES (sinsn)
4275 = gen_rtx (EXPR_LIST, REG_EQUIV,
4276 parm_reg_stack_loc[regnor],
4277 REG_NOTES (sinsn));
4279 else if ((set = single_set (linsn)) != 0
4280 && SET_DEST (set) == parmreg)
4281 REG_NOTES (linsn)
4282 = gen_rtx (EXPR_LIST, REG_EQUIV,
4283 stack_parm, REG_NOTES (linsn));
4286 /* For pointer data type, suggest pointer register. */
4287 if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
4288 mark_reg_pointer (parmreg,
4289 (TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm)))
4290 / BITS_PER_UNIT));
4292 else
4294 /* Value must be stored in the stack slot STACK_PARM
4295 during function execution. */
4297 if (promoted_mode != nominal_mode)
4299 /* Conversion is required. */
4300 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4302 emit_move_insn (tempreg, validize_mem (entry_parm));
4304 push_to_sequence (conversion_insns);
4305 entry_parm = convert_to_mode (nominal_mode, tempreg,
4306 TREE_UNSIGNED (TREE_TYPE (parm)));
4307 conversion_insns = get_insns ();
4308 did_conversion = 1;
4309 end_sequence ();
4312 if (entry_parm != stack_parm)
4314 if (stack_parm == 0)
4316 stack_parm
4317 = assign_stack_local (GET_MODE (entry_parm),
4318 GET_MODE_SIZE (GET_MODE (entry_parm)), 0);
4319 /* If this is a memory ref that contains aggregate components,
4320 mark it as such for cse and loop optimize. */
4321 MEM_IN_STRUCT_P (stack_parm) = aggregate;
4324 if (promoted_mode != nominal_mode)
4326 push_to_sequence (conversion_insns);
4327 emit_move_insn (validize_mem (stack_parm),
4328 validize_mem (entry_parm));
4329 conversion_insns = get_insns ();
4330 end_sequence ();
4332 else
4333 emit_move_insn (validize_mem (stack_parm),
4334 validize_mem (entry_parm));
4336 if (flag_check_memory_usage)
4338 push_to_sequence (conversion_insns);
4339 emit_library_call (chkr_set_right_libfunc, 1, VOIDmode, 3,
4340 XEXP (stack_parm, 0), ptr_mode,
4341 GEN_INT (GET_MODE_SIZE (GET_MODE
4342 (entry_parm))),
4343 TYPE_MODE (sizetype),
4344 GEN_INT (MEMORY_USE_RW),
4345 TYPE_MODE (integer_type_node));
4347 conversion_insns = get_insns ();
4348 end_sequence ();
4350 DECL_RTL (parm) = stack_parm;
4353 /* If this "parameter" was the place where we are receiving the
4354 function's incoming structure pointer, set up the result. */
4355 if (parm == function_result_decl)
4357 tree result = DECL_RESULT (fndecl);
4358 tree restype = TREE_TYPE (result);
4360 DECL_RTL (result)
4361 = gen_rtx (MEM, DECL_MODE (result), DECL_RTL (parm));
4363 MEM_IN_STRUCT_P (DECL_RTL (result)) = AGGREGATE_TYPE_P (restype);
4366 if (TREE_THIS_VOLATILE (parm))
4367 MEM_VOLATILE_P (DECL_RTL (parm)) = 1;
4368 if (TREE_READONLY (parm))
4369 RTX_UNCHANGING_P (DECL_RTL (parm)) = 1;
4372 /* Output all parameter conversion instructions (possibly including calls)
4373 now that all parameters have been copied out of hard registers. */
4374 emit_insns (conversion_insns);
4376 last_parm_insn = get_last_insn ();
4378 current_function_args_size = stack_args_size.constant;
4380 /* Adjust function incoming argument size for alignment and
4381 minimum length. */
4383 #ifdef REG_PARM_STACK_SPACE
4384 #ifndef MAYBE_REG_PARM_STACK_SPACE
4385 current_function_args_size = MAX (current_function_args_size,
4386 REG_PARM_STACK_SPACE (fndecl));
4387 #endif
4388 #endif
4390 #ifdef STACK_BOUNDARY
4391 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
4393 current_function_args_size
4394 = ((current_function_args_size + STACK_BYTES - 1)
4395 / STACK_BYTES) * STACK_BYTES;
4396 #endif
4398 #ifdef ARGS_GROW_DOWNWARD
4399 current_function_arg_offset_rtx
4400 = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
4401 : expand_expr (size_binop (MINUS_EXPR, stack_args_size.var,
4402 size_int (-stack_args_size.constant)),
4403 NULL_RTX, VOIDmode, EXPAND_MEMORY_USE_BAD));
4404 #else
4405 current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
4406 #endif
4408 /* See how many bytes, if any, of its args a function should try to pop
4409 on return. */
4411 current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
4412 current_function_args_size);
4414 /* For stdarg.h function, save info about
4415 regs and stack space used by the named args. */
4417 if (!hide_last_arg)
4418 current_function_args_info = args_so_far;
4420 /* Set the rtx used for the function return value. Put this in its
4421 own variable so any optimizers that need this information don't have
4422 to include tree.h. Do this here so it gets done when an inlined
4423 function gets output. */
4425 current_function_return_rtx = DECL_RTL (DECL_RESULT (fndecl));
4428 /* Indicate whether REGNO is an incoming argument to the current function
4429 that was promoted to a wider mode. If so, return the RTX for the
4430 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
4431 that REGNO is promoted from and whether the promotion was signed or
4432 unsigned. */
4434 #ifdef PROMOTE_FUNCTION_ARGS
4437 promoted_input_arg (regno, pmode, punsignedp)
4438 int regno;
4439 enum machine_mode *pmode;
4440 int *punsignedp;
4442 tree arg;
4444 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
4445 arg = TREE_CHAIN (arg))
4446 if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
4447 && REGNO (DECL_INCOMING_RTL (arg)) == regno
4448 && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
4450 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
4451 int unsignedp = TREE_UNSIGNED (TREE_TYPE (arg));
4453 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
4454 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
4455 && mode != DECL_MODE (arg))
4457 *pmode = DECL_MODE (arg);
4458 *punsignedp = unsignedp;
4459 return DECL_INCOMING_RTL (arg);
4463 return 0;
4466 #endif
4468 /* Compute the size and offset from the start of the stacked arguments for a
4469 parm passed in mode PASSED_MODE and with type TYPE.
4471 INITIAL_OFFSET_PTR points to the current offset into the stacked
4472 arguments.
4474 The starting offset and size for this parm are returned in *OFFSET_PTR
4475 and *ARG_SIZE_PTR, respectively.
4477 IN_REGS is non-zero if the argument will be passed in registers. It will
4478 never be set if REG_PARM_STACK_SPACE is not defined.
4480 FNDECL is the function in which the argument was defined.
4482 There are two types of rounding that are done. The first, controlled by
4483 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
4484 list to be aligned to the specific boundary (in bits). This rounding
4485 affects the initial and starting offsets, but not the argument size.
4487 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
4488 optionally rounds the size of the parm to PARM_BOUNDARY. The
4489 initial offset is not affected by this rounding, while the size always
4490 is and the starting offset may be. */
4492 /* offset_ptr will be negative for ARGS_GROW_DOWNWARD case;
4493 initial_offset_ptr is positive because locate_and_pad_parm's
4494 callers pass in the total size of args so far as
4495 initial_offset_ptr. arg_size_ptr is always positive.*/
4497 void
4498 locate_and_pad_parm (passed_mode, type, in_regs, fndecl,
4499 initial_offset_ptr, offset_ptr, arg_size_ptr)
4500 enum machine_mode passed_mode;
4501 tree type;
4502 int in_regs;
4503 tree fndecl;
4504 struct args_size *initial_offset_ptr;
4505 struct args_size *offset_ptr;
4506 struct args_size *arg_size_ptr;
4508 tree sizetree
4509 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
4510 enum direction where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
4511 int boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
4512 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4513 int reg_parm_stack_space = 0;
4515 #ifdef REG_PARM_STACK_SPACE
4516 /* If we have found a stack parm before we reach the end of the
4517 area reserved for registers, skip that area. */
4518 if (! in_regs)
4520 #ifdef MAYBE_REG_PARM_STACK_SPACE
4521 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
4522 #else
4523 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
4524 #endif
4525 if (reg_parm_stack_space > 0)
4527 if (initial_offset_ptr->var)
4529 initial_offset_ptr->var
4530 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
4531 size_int (reg_parm_stack_space));
4532 initial_offset_ptr->constant = 0;
4534 else if (initial_offset_ptr->constant < reg_parm_stack_space)
4535 initial_offset_ptr->constant = reg_parm_stack_space;
4538 #endif /* REG_PARM_STACK_SPACE */
4540 arg_size_ptr->var = 0;
4541 arg_size_ptr->constant = 0;
4543 #ifdef ARGS_GROW_DOWNWARD
4544 if (initial_offset_ptr->var)
4546 offset_ptr->constant = 0;
4547 offset_ptr->var = size_binop (MINUS_EXPR, integer_zero_node,
4548 initial_offset_ptr->var);
4550 else
4552 offset_ptr->constant = - initial_offset_ptr->constant;
4553 offset_ptr->var = 0;
4555 if (where_pad != none
4556 && (TREE_CODE (sizetree) != INTEGER_CST
4557 || ((TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)))
4558 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4559 SUB_PARM_SIZE (*offset_ptr, sizetree);
4560 if (where_pad != downward)
4561 pad_to_arg_alignment (offset_ptr, boundary);
4562 if (initial_offset_ptr->var)
4564 arg_size_ptr->var = size_binop (MINUS_EXPR,
4565 size_binop (MINUS_EXPR,
4566 integer_zero_node,
4567 initial_offset_ptr->var),
4568 offset_ptr->var);
4570 else
4572 arg_size_ptr->constant = (- initial_offset_ptr->constant
4573 - offset_ptr->constant);
4575 #else /* !ARGS_GROW_DOWNWARD */
4576 pad_to_arg_alignment (initial_offset_ptr, boundary);
4577 *offset_ptr = *initial_offset_ptr;
4579 #ifdef PUSH_ROUNDING
4580 if (passed_mode != BLKmode)
4581 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
4582 #endif
4584 /* Pad_below needs the pre-rounded size to know how much to pad below
4585 so this must be done before rounding up. */
4586 if (where_pad == downward
4587 /* However, BLKmode args passed in regs have their padding done elsewhere.
4588 The stack slot must be able to hold the entire register. */
4589 && !(in_regs && passed_mode == BLKmode))
4590 pad_below (offset_ptr, passed_mode, sizetree);
4592 if (where_pad != none
4593 && (TREE_CODE (sizetree) != INTEGER_CST
4594 || ((TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)))
4595 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4597 ADD_PARM_SIZE (*arg_size_ptr, sizetree);
4598 #endif /* ARGS_GROW_DOWNWARD */
4601 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
4602 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
4604 static void
4605 pad_to_arg_alignment (offset_ptr, boundary)
4606 struct args_size *offset_ptr;
4607 int boundary;
4609 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4611 if (boundary > BITS_PER_UNIT)
4613 if (offset_ptr->var)
4615 offset_ptr->var =
4616 #ifdef ARGS_GROW_DOWNWARD
4617 round_down
4618 #else
4619 round_up
4620 #endif
4621 (ARGS_SIZE_TREE (*offset_ptr),
4622 boundary / BITS_PER_UNIT);
4623 offset_ptr->constant = 0; /*?*/
4625 else
4626 offset_ptr->constant =
4627 #ifdef ARGS_GROW_DOWNWARD
4628 FLOOR_ROUND (offset_ptr->constant, boundary_in_bytes);
4629 #else
4630 CEIL_ROUND (offset_ptr->constant, boundary_in_bytes);
4631 #endif
4635 static void
4636 pad_below (offset_ptr, passed_mode, sizetree)
4637 struct args_size *offset_ptr;
4638 enum machine_mode passed_mode;
4639 tree sizetree;
4641 if (passed_mode != BLKmode)
4643 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
4644 offset_ptr->constant
4645 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
4646 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
4647 - GET_MODE_SIZE (passed_mode));
4649 else
4651 if (TREE_CODE (sizetree) != INTEGER_CST
4652 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
4654 /* Round the size up to multiple of PARM_BOUNDARY bits. */
4655 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4656 /* Add it in. */
4657 ADD_PARM_SIZE (*offset_ptr, s2);
4658 SUB_PARM_SIZE (*offset_ptr, sizetree);
4663 static tree
4664 round_down (value, divisor)
4665 tree value;
4666 int divisor;
4668 return size_binop (MULT_EXPR,
4669 size_binop (FLOOR_DIV_EXPR, value, size_int (divisor)),
4670 size_int (divisor));
4673 /* Walk the tree of blocks describing the binding levels within a function
4674 and warn about uninitialized variables.
4675 This is done after calling flow_analysis and before global_alloc
4676 clobbers the pseudo-regs to hard regs. */
4678 void
4679 uninitialized_vars_warning (block)
4680 tree block;
4682 register tree decl, sub;
4683 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
4685 if (TREE_CODE (decl) == VAR_DECL
4686 /* These warnings are unreliable for and aggregates
4687 because assigning the fields one by one can fail to convince
4688 flow.c that the entire aggregate was initialized.
4689 Unions are troublesome because members may be shorter. */
4690 && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
4691 && DECL_RTL (decl) != 0
4692 && GET_CODE (DECL_RTL (decl)) == REG
4693 && regno_uninitialized (REGNO (DECL_RTL (decl))))
4694 warning_with_decl (decl,
4695 "`%s' might be used uninitialized in this function");
4696 if (TREE_CODE (decl) == VAR_DECL
4697 && DECL_RTL (decl) != 0
4698 && GET_CODE (DECL_RTL (decl)) == REG
4699 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
4700 warning_with_decl (decl,
4701 "variable `%s' might be clobbered by `longjmp' or `vfork'");
4703 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
4704 uninitialized_vars_warning (sub);
4707 /* Do the appropriate part of uninitialized_vars_warning
4708 but for arguments instead of local variables. */
4710 void
4711 setjmp_args_warning ()
4713 register tree decl;
4714 for (decl = DECL_ARGUMENTS (current_function_decl);
4715 decl; decl = TREE_CHAIN (decl))
4716 if (DECL_RTL (decl) != 0
4717 && GET_CODE (DECL_RTL (decl)) == REG
4718 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
4719 warning_with_decl (decl, "argument `%s' might be clobbered by `longjmp' or `vfork'");
4722 /* If this function call setjmp, put all vars into the stack
4723 unless they were declared `register'. */
4725 void
4726 setjmp_protect (block)
4727 tree block;
4729 register tree decl, sub;
4730 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
4731 if ((TREE_CODE (decl) == VAR_DECL
4732 || TREE_CODE (decl) == PARM_DECL)
4733 && DECL_RTL (decl) != 0
4734 && (GET_CODE (DECL_RTL (decl)) == REG
4735 || (GET_CODE (DECL_RTL (decl)) == MEM
4736 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
4737 /* If this variable came from an inline function, it must be
4738 that it's life doesn't overlap the setjmp. If there was a
4739 setjmp in the function, it would already be in memory. We
4740 must exclude such variable because their DECL_RTL might be
4741 set to strange things such as virtual_stack_vars_rtx. */
4742 && ! DECL_FROM_INLINE (decl)
4743 && (
4744 #ifdef NON_SAVING_SETJMP
4745 /* If longjmp doesn't restore the registers,
4746 don't put anything in them. */
4747 NON_SAVING_SETJMP
4749 #endif
4750 ! DECL_REGISTER (decl)))
4751 put_var_into_stack (decl);
4752 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
4753 setjmp_protect (sub);
4756 /* Like the previous function, but for args instead of local variables. */
4758 void
4759 setjmp_protect_args ()
4761 register tree decl, sub;
4762 for (decl = DECL_ARGUMENTS (current_function_decl);
4763 decl; decl = TREE_CHAIN (decl))
4764 if ((TREE_CODE (decl) == VAR_DECL
4765 || TREE_CODE (decl) == PARM_DECL)
4766 && DECL_RTL (decl) != 0
4767 && (GET_CODE (DECL_RTL (decl)) == REG
4768 || (GET_CODE (DECL_RTL (decl)) == MEM
4769 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
4770 && (
4771 /* If longjmp doesn't restore the registers,
4772 don't put anything in them. */
4773 #ifdef NON_SAVING_SETJMP
4774 NON_SAVING_SETJMP
4776 #endif
4777 ! DECL_REGISTER (decl)))
4778 put_var_into_stack (decl);
4781 /* Return the context-pointer register corresponding to DECL,
4782 or 0 if it does not need one. */
4785 lookup_static_chain (decl)
4786 tree decl;
4788 tree context = decl_function_context (decl);
4789 tree link;
4791 if (context == 0
4792 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_NO_STATIC_CHAIN (decl)))
4793 return 0;
4795 /* We treat inline_function_decl as an alias for the current function
4796 because that is the inline function whose vars, types, etc.
4797 are being merged into the current function.
4798 See expand_inline_function. */
4799 if (context == current_function_decl || context == inline_function_decl)
4800 return virtual_stack_vars_rtx;
4802 for (link = context_display; link; link = TREE_CHAIN (link))
4803 if (TREE_PURPOSE (link) == context)
4804 return RTL_EXPR_RTL (TREE_VALUE (link));
4806 abort ();
4809 /* Convert a stack slot address ADDR for variable VAR
4810 (from a containing function)
4811 into an address valid in this function (using a static chain). */
4814 fix_lexical_addr (addr, var)
4815 rtx addr;
4816 tree var;
4818 rtx basereg;
4819 int displacement;
4820 tree context = decl_function_context (var);
4821 struct function *fp;
4822 rtx base = 0;
4824 /* If this is the present function, we need not do anything. */
4825 if (context == current_function_decl || context == inline_function_decl)
4826 return addr;
4828 for (fp = outer_function_chain; fp; fp = fp->next)
4829 if (fp->decl == context)
4830 break;
4832 if (fp == 0)
4833 abort ();
4835 if (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == MEM)
4836 addr = XEXP (XEXP (addr, 0), 0);
4838 /* Decode given address as base reg plus displacement. */
4839 if (GET_CODE (addr) == REG)
4840 basereg = addr, displacement = 0;
4841 else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
4842 basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
4843 else
4844 abort ();
4846 /* We accept vars reached via the containing function's
4847 incoming arg pointer and via its stack variables pointer. */
4848 if (basereg == fp->internal_arg_pointer)
4850 /* If reached via arg pointer, get the arg pointer value
4851 out of that function's stack frame.
4853 There are two cases: If a separate ap is needed, allocate a
4854 slot in the outer function for it and dereference it that way.
4855 This is correct even if the real ap is actually a pseudo.
4856 Otherwise, just adjust the offset from the frame pointer to
4857 compensate. */
4859 #ifdef NEED_SEPARATE_AP
4860 rtx addr;
4862 if (fp->arg_pointer_save_area == 0)
4863 fp->arg_pointer_save_area
4864 = assign_outer_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0, fp);
4866 addr = fix_lexical_addr (XEXP (fp->arg_pointer_save_area, 0), var);
4867 addr = memory_address (Pmode, addr);
4869 base = copy_to_reg (gen_rtx (MEM, Pmode, addr));
4870 #else
4871 displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
4872 base = lookup_static_chain (var);
4873 #endif
4876 else if (basereg == virtual_stack_vars_rtx)
4878 /* This is the same code as lookup_static_chain, duplicated here to
4879 avoid an extra call to decl_function_context. */
4880 tree link;
4882 for (link = context_display; link; link = TREE_CHAIN (link))
4883 if (TREE_PURPOSE (link) == context)
4885 base = RTL_EXPR_RTL (TREE_VALUE (link));
4886 break;
4890 if (base == 0)
4891 abort ();
4893 /* Use same offset, relative to appropriate static chain or argument
4894 pointer. */
4895 return plus_constant (base, displacement);
4898 /* Return the address of the trampoline for entering nested fn FUNCTION.
4899 If necessary, allocate a trampoline (in the stack frame)
4900 and emit rtl to initialize its contents (at entry to this function). */
4903 trampoline_address (function)
4904 tree function;
4906 tree link;
4907 tree rtlexp;
4908 rtx tramp;
4909 struct function *fp;
4910 tree fn_context;
4912 /* Find an existing trampoline and return it. */
4913 for (link = trampoline_list; link; link = TREE_CHAIN (link))
4914 if (TREE_PURPOSE (link) == function)
4915 return
4916 round_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
4918 for (fp = outer_function_chain; fp; fp = fp->next)
4919 for (link = fp->trampoline_list; link; link = TREE_CHAIN (link))
4920 if (TREE_PURPOSE (link) == function)
4922 tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
4923 function);
4924 return round_trampoline_addr (tramp);
4927 /* None exists; we must make one. */
4929 /* Find the `struct function' for the function containing FUNCTION. */
4930 fp = 0;
4931 fn_context = decl_function_context (function);
4932 if (fn_context != current_function_decl
4933 && fn_context != inline_function_decl)
4934 for (fp = outer_function_chain; fp; fp = fp->next)
4935 if (fp->decl == fn_context)
4936 break;
4938 /* Allocate run-time space for this trampoline
4939 (usually in the defining function's stack frame). */
4940 #ifdef ALLOCATE_TRAMPOLINE
4941 tramp = ALLOCATE_TRAMPOLINE (fp);
4942 #else
4943 /* If rounding needed, allocate extra space
4944 to ensure we have TRAMPOLINE_SIZE bytes left after rounding up. */
4945 #ifdef TRAMPOLINE_ALIGNMENT
4946 #define TRAMPOLINE_REAL_SIZE \
4947 (TRAMPOLINE_SIZE + (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT) - 1)
4948 #else
4949 #define TRAMPOLINE_REAL_SIZE (TRAMPOLINE_SIZE)
4950 #endif
4951 if (fp != 0)
4952 tramp = assign_outer_stack_local (BLKmode, TRAMPOLINE_REAL_SIZE, 0, fp);
4953 else
4954 tramp = assign_stack_local (BLKmode, TRAMPOLINE_REAL_SIZE, 0);
4955 #endif
4957 /* Record the trampoline for reuse and note it for later initialization
4958 by expand_function_end. */
4959 if (fp != 0)
4961 push_obstacks (fp->function_maybepermanent_obstack,
4962 fp->function_maybepermanent_obstack);
4963 rtlexp = make_node (RTL_EXPR);
4964 RTL_EXPR_RTL (rtlexp) = tramp;
4965 fp->trampoline_list = tree_cons (function, rtlexp, fp->trampoline_list);
4966 pop_obstacks ();
4968 else
4970 /* Make the RTL_EXPR node temporary, not momentary, so that the
4971 trampoline_list doesn't become garbage. */
4972 int momentary = suspend_momentary ();
4973 rtlexp = make_node (RTL_EXPR);
4974 resume_momentary (momentary);
4976 RTL_EXPR_RTL (rtlexp) = tramp;
4977 trampoline_list = tree_cons (function, rtlexp, trampoline_list);
4980 tramp = fix_lexical_addr (XEXP (tramp, 0), function);
4981 return round_trampoline_addr (tramp);
4984 /* Given a trampoline address,
4985 round it to multiple of TRAMPOLINE_ALIGNMENT. */
4987 static rtx
4988 round_trampoline_addr (tramp)
4989 rtx tramp;
4991 #ifdef TRAMPOLINE_ALIGNMENT
4992 /* Round address up to desired boundary. */
4993 rtx temp = gen_reg_rtx (Pmode);
4994 temp = expand_binop (Pmode, add_optab, tramp,
4995 GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1),
4996 temp, 0, OPTAB_LIB_WIDEN);
4997 tramp = expand_binop (Pmode, and_optab, temp,
4998 GEN_INT (- TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT),
4999 temp, 0, OPTAB_LIB_WIDEN);
5000 #endif
5001 return tramp;
5004 /* The functions identify_blocks and reorder_blocks provide a way to
5005 reorder the tree of BLOCK nodes, for optimizers that reshuffle or
5006 duplicate portions of the RTL code. Call identify_blocks before
5007 changing the RTL, and call reorder_blocks after. */
5009 /* Put all this function's BLOCK nodes including those that are chained
5010 onto the first block into a vector, and return it.
5011 Also store in each NOTE for the beginning or end of a block
5012 the index of that block in the vector.
5013 The arguments are BLOCK, the chain of top-level blocks of the function,
5014 and INSNS, the insn chain of the function. */
5016 tree *
5017 identify_blocks (block, insns)
5018 tree block;
5019 rtx insns;
5021 int n_blocks;
5022 tree *block_vector;
5023 int *block_stack;
5024 int depth = 0;
5025 int next_block_number = 1;
5026 int current_block_number = 1;
5027 rtx insn;
5029 if (block == 0)
5030 return 0;
5032 n_blocks = all_blocks (block, 0);
5033 block_vector = (tree *) xmalloc (n_blocks * sizeof (tree));
5034 block_stack = (int *) alloca (n_blocks * sizeof (int));
5036 all_blocks (block, block_vector);
5038 for (insn = insns; insn; insn = NEXT_INSN (insn))
5039 if (GET_CODE (insn) == NOTE)
5041 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5043 block_stack[depth++] = current_block_number;
5044 current_block_number = next_block_number;
5045 NOTE_BLOCK_NUMBER (insn) = next_block_number++;
5047 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5049 current_block_number = block_stack[--depth];
5050 NOTE_BLOCK_NUMBER (insn) = current_block_number;
5054 if (n_blocks != next_block_number)
5055 abort ();
5057 return block_vector;
5060 /* Given BLOCK_VECTOR which was returned by identify_blocks,
5061 and a revised instruction chain, rebuild the tree structure
5062 of BLOCK nodes to correspond to the new order of RTL.
5063 The new block tree is inserted below TOP_BLOCK.
5064 Returns the current top-level block. */
5066 tree
5067 reorder_blocks (block_vector, block, insns)
5068 tree *block_vector;
5069 tree block;
5070 rtx insns;
5072 tree current_block = block;
5073 rtx insn;
5075 if (block_vector == 0)
5076 return block;
5078 /* Prune the old trees away, so that it doesn't get in the way. */
5079 BLOCK_SUBBLOCKS (current_block) = 0;
5080 BLOCK_CHAIN (current_block) = 0;
5082 for (insn = insns; insn; insn = NEXT_INSN (insn))
5083 if (GET_CODE (insn) == NOTE)
5085 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
5087 tree block = block_vector[NOTE_BLOCK_NUMBER (insn)];
5088 /* If we have seen this block before, copy it. */
5089 if (TREE_ASM_WRITTEN (block))
5090 block = copy_node (block);
5091 BLOCK_SUBBLOCKS (block) = 0;
5092 TREE_ASM_WRITTEN (block) = 1;
5093 BLOCK_SUPERCONTEXT (block) = current_block;
5094 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
5095 BLOCK_SUBBLOCKS (current_block) = block;
5096 current_block = block;
5097 NOTE_SOURCE_FILE (insn) = 0;
5099 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
5101 BLOCK_SUBBLOCKS (current_block)
5102 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
5103 current_block = BLOCK_SUPERCONTEXT (current_block);
5104 NOTE_SOURCE_FILE (insn) = 0;
5108 BLOCK_SUBBLOCKS (current_block)
5109 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
5110 return current_block;
5113 /* Reverse the order of elements in the chain T of blocks,
5114 and return the new head of the chain (old last element). */
5116 static tree
5117 blocks_nreverse (t)
5118 tree t;
5120 register tree prev = 0, decl, next;
5121 for (decl = t; decl; decl = next)
5123 next = BLOCK_CHAIN (decl);
5124 BLOCK_CHAIN (decl) = prev;
5125 prev = decl;
5127 return prev;
5130 /* Count the subblocks of the list starting with BLOCK, and list them
5131 all into the vector VECTOR. Also clear TREE_ASM_WRITTEN in all
5132 blocks. */
5134 static int
5135 all_blocks (block, vector)
5136 tree block;
5137 tree *vector;
5139 int n_blocks = 0;
5141 while (block)
5143 TREE_ASM_WRITTEN (block) = 0;
5145 /* Record this block. */
5146 if (vector)
5147 vector[n_blocks] = block;
5149 ++n_blocks;
5151 /* Record the subblocks, and their subblocks... */
5152 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
5153 vector ? vector + n_blocks : 0);
5154 block = BLOCK_CHAIN (block);
5157 return n_blocks;
5160 /* Build bytecode call descriptor for function SUBR. */
5163 bc_build_calldesc (subr)
5164 tree subr;
5166 tree calldesc = 0, arg;
5167 int nargs = 0;
5169 /* Build the argument description vector in reverse order. */
5170 DECL_ARGUMENTS (subr) = nreverse (DECL_ARGUMENTS (subr));
5171 nargs = 0;
5173 for (arg = DECL_ARGUMENTS (subr); arg; arg = TREE_CHAIN (arg))
5175 ++nargs;
5177 calldesc = tree_cons ((tree) 0, size_in_bytes (TREE_TYPE (arg)), calldesc);
5178 calldesc = tree_cons ((tree) 0, bc_runtime_type_code (TREE_TYPE (arg)), calldesc);
5181 DECL_ARGUMENTS (subr) = nreverse (DECL_ARGUMENTS (subr));
5183 /* Prepend the function's return type. */
5184 calldesc = tree_cons ((tree) 0,
5185 size_in_bytes (TREE_TYPE (TREE_TYPE (subr))),
5186 calldesc);
5188 calldesc = tree_cons ((tree) 0,
5189 bc_runtime_type_code (TREE_TYPE (TREE_TYPE (subr))),
5190 calldesc);
5192 /* Prepend the arg count. */
5193 calldesc = tree_cons ((tree) 0, build_int_2 (nargs, 0), calldesc);
5195 /* Output the call description vector and get its address. */
5196 calldesc = build_nt (CONSTRUCTOR, (tree) 0, calldesc);
5197 TREE_TYPE (calldesc) = build_array_type (integer_type_node,
5198 build_index_type (build_int_2 (nargs * 2, 0)));
5200 return output_constant_def (calldesc);
5204 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
5205 and initialize static variables for generating RTL for the statements
5206 of the function. */
5208 void
5209 init_function_start (subr, filename, line)
5210 tree subr;
5211 char *filename;
5212 int line;
5214 if (output_bytecode)
5216 this_function_decl = subr;
5217 this_function_calldesc = bc_build_calldesc (subr);
5218 local_vars_size = 0;
5219 stack_depth = 0;
5220 max_stack_depth = 0;
5221 stmt_expr_depth = 0;
5222 return;
5225 init_stmt_for_function ();
5227 cse_not_expected = ! optimize;
5229 /* Caller save not needed yet. */
5230 caller_save_needed = 0;
5232 /* No stack slots have been made yet. */
5233 stack_slot_list = 0;
5235 /* There is no stack slot for handling nonlocal gotos. */
5236 nonlocal_goto_handler_slot = 0;
5237 nonlocal_goto_stack_level = 0;
5239 /* No labels have been declared for nonlocal use. */
5240 nonlocal_labels = 0;
5242 /* No function calls so far in this function. */
5243 function_call_count = 0;
5245 /* No parm regs have been allocated.
5246 (This is important for output_inline_function.) */
5247 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
5249 /* Initialize the RTL mechanism. */
5250 init_emit ();
5252 /* Initialize the queue of pending postincrement and postdecrements,
5253 and some other info in expr.c. */
5254 init_expr ();
5256 /* We haven't done register allocation yet. */
5257 reg_renumber = 0;
5259 init_const_rtx_hash_table ();
5261 current_function_name = (*decl_printable_name) (subr, 2);
5263 /* Nonzero if this is a nested function that uses a static chain. */
5265 current_function_needs_context
5266 = (decl_function_context (current_function_decl) != 0
5267 && ! DECL_NO_STATIC_CHAIN (current_function_decl));
5269 /* Set if a call to setjmp is seen. */
5270 current_function_calls_setjmp = 0;
5272 /* Set if a call to longjmp is seen. */
5273 current_function_calls_longjmp = 0;
5275 current_function_calls_alloca = 0;
5276 current_function_has_nonlocal_label = 0;
5277 current_function_has_nonlocal_goto = 0;
5278 current_function_contains_functions = 0;
5279 current_function_is_thunk = 0;
5281 current_function_returns_pcc_struct = 0;
5282 current_function_returns_struct = 0;
5283 current_function_epilogue_delay_list = 0;
5284 current_function_uses_const_pool = 0;
5285 current_function_uses_pic_offset_table = 0;
5287 /* We have not yet needed to make a label to jump to for tail-recursion. */
5288 tail_recursion_label = 0;
5290 /* We haven't had a need to make a save area for ap yet. */
5292 arg_pointer_save_area = 0;
5294 /* No stack slots allocated yet. */
5295 frame_offset = 0;
5297 /* No SAVE_EXPRs in this function yet. */
5298 save_expr_regs = 0;
5300 /* No RTL_EXPRs in this function yet. */
5301 rtl_expr_chain = 0;
5303 /* Set up to allocate temporaries. */
5304 init_temp_slots ();
5306 /* Within function body, compute a type's size as soon it is laid out. */
5307 immediate_size_expand++;
5309 /* We haven't made any trampolines for this function yet. */
5310 trampoline_list = 0;
5312 init_pending_stack_adjust ();
5313 inhibit_defer_pop = 0;
5315 current_function_outgoing_args_size = 0;
5317 /* Prevent ever trying to delete the first instruction of a function.
5318 Also tell final how to output a linenum before the function prologue. */
5319 emit_line_note (filename, line);
5321 /* Make sure first insn is a note even if we don't want linenums.
5322 This makes sure the first insn will never be deleted.
5323 Also, final expects a note to appear there. */
5324 emit_note (NULL_PTR, NOTE_INSN_DELETED);
5326 /* Set flags used by final.c. */
5327 if (aggregate_value_p (DECL_RESULT (subr)))
5329 #ifdef PCC_STATIC_STRUCT_RETURN
5330 current_function_returns_pcc_struct = 1;
5331 #endif
5332 current_function_returns_struct = 1;
5335 /* Warn if this value is an aggregate type,
5336 regardless of which calling convention we are using for it. */
5337 if (warn_aggregate_return
5338 && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
5339 warning ("function returns an aggregate");
5341 current_function_returns_pointer
5342 = POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
5344 /* Indicate that we need to distinguish between the return value of the
5345 present function and the return value of a function being called. */
5346 rtx_equal_function_value_matters = 1;
5348 /* Indicate that we have not instantiated virtual registers yet. */
5349 virtuals_instantiated = 0;
5351 /* Indicate we have no need of a frame pointer yet. */
5352 frame_pointer_needed = 0;
5354 /* By default assume not varargs or stdarg. */
5355 current_function_varargs = 0;
5356 current_function_stdarg = 0;
5359 /* Indicate that the current function uses extra args
5360 not explicitly mentioned in the argument list in any fashion. */
5362 void
5363 mark_varargs ()
5365 current_function_varargs = 1;
5368 /* Expand a call to __main at the beginning of a possible main function. */
5370 #if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
5371 #undef HAS_INIT_SECTION
5372 #define HAS_INIT_SECTION
5373 #endif
5375 void
5376 expand_main_function ()
5378 if (!output_bytecode)
5380 /* The zero below avoids a possible parse error */
5382 #if !defined (HAS_INIT_SECTION)
5383 emit_library_call (gen_rtx (SYMBOL_REF, Pmode, NAME__MAIN), 0,
5384 VOIDmode, 0);
5385 #endif /* not HAS_INIT_SECTION */
5389 extern struct obstack permanent_obstack;
5391 /* Expand start of bytecode function. See comment at
5392 expand_function_start below for details. */
5394 void
5395 bc_expand_function_start (subr, parms_have_cleanups)
5396 tree subr;
5397 int parms_have_cleanups;
5399 char label[20], *name;
5400 static int nlab;
5401 tree thisarg;
5402 int argsz;
5404 if (TREE_PUBLIC (subr))
5405 bc_globalize_label (IDENTIFIER_POINTER (DECL_NAME (subr)));
5407 #ifdef DEBUG_PRINT_CODE
5408 fprintf (stderr, "\n<func %s>\n", IDENTIFIER_POINTER (DECL_NAME (subr)));
5409 #endif
5411 for (argsz = 0, thisarg = DECL_ARGUMENTS (subr); thisarg; thisarg = TREE_CHAIN (thisarg))
5413 if (DECL_RTL (thisarg))
5414 abort (); /* Should be NULL here I think. */
5415 else if (TREE_CONSTANT (DECL_SIZE (thisarg)))
5417 DECL_RTL (thisarg) = bc_gen_rtx ((char *) 0, argsz, (struct bc_label *) 0);
5418 argsz += TREE_INT_CST_LOW (DECL_SIZE (thisarg));
5420 else
5422 /* Variable-sized objects are pointers to their storage. */
5423 DECL_RTL (thisarg) = bc_gen_rtx ((char *) 0, argsz, (struct bc_label *) 0);
5424 argsz += POINTER_SIZE;
5428 bc_begin_function (xstrdup (IDENTIFIER_POINTER (DECL_NAME (subr))));
5430 ASM_GENERATE_INTERNAL_LABEL (label, "LX", nlab);
5432 ++nlab;
5433 name = (char *) obstack_copy0 (&permanent_obstack, label, strlen (label));
5434 this_function_callinfo = bc_gen_rtx (name, 0, (struct bc_label *) 0);
5435 this_function_bytecode
5436 = bc_emit_trampoline (BYTECODE_LABEL (this_function_callinfo));
5440 /* Expand end of bytecode function. See details the comment of
5441 expand_function_end(), below. */
5443 void
5444 bc_expand_function_end ()
5446 char *ptrconsts;
5448 expand_null_return ();
5450 /* Emit any fixup code. This must be done before the call to
5451 to BC_END_FUNCTION (), since that will cause the bytecode
5452 segment to be finished off and closed. */
5454 expand_fixups (NULL_RTX);
5456 ptrconsts = bc_end_function ();
5458 bc_align_const (2 /* INT_ALIGN */);
5460 /* If this changes also make sure to change bc-interp.h! */
5462 bc_emit_const_labeldef (BYTECODE_LABEL (this_function_callinfo));
5463 bc_emit_const ((char *) &max_stack_depth, sizeof max_stack_depth);
5464 bc_emit_const ((char *) &local_vars_size, sizeof local_vars_size);
5465 bc_emit_const_labelref (this_function_bytecode, 0);
5466 bc_emit_const_labelref (ptrconsts, 0);
5467 bc_emit_const_labelref (BYTECODE_LABEL (this_function_calldesc), 0);
5471 /* Start the RTL for a new function, and set variables used for
5472 emitting RTL.
5473 SUBR is the FUNCTION_DECL node.
5474 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
5475 the function's parameters, which must be run at any return statement. */
5477 void
5478 expand_function_start (subr, parms_have_cleanups)
5479 tree subr;
5480 int parms_have_cleanups;
5482 register int i;
5483 tree tem;
5484 rtx last_ptr;
5486 if (output_bytecode)
5488 bc_expand_function_start (subr, parms_have_cleanups);
5489 return;
5492 /* Make sure volatile mem refs aren't considered
5493 valid operands of arithmetic insns. */
5494 init_recog_no_volatile ();
5496 /* If function gets a static chain arg, store it in the stack frame.
5497 Do this first, so it gets the first stack slot offset. */
5498 if (current_function_needs_context)
5500 last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5502 /* Delay copying static chain if it is not a register to avoid
5503 conflicts with regs used for parameters. */
5504 if (! SMALL_REGISTER_CLASSES
5505 || GET_CODE (static_chain_incoming_rtx) == REG)
5506 emit_move_insn (last_ptr, static_chain_incoming_rtx);
5509 /* If the parameters of this function need cleaning up, get a label
5510 for the beginning of the code which executes those cleanups. This must
5511 be done before doing anything with return_label. */
5512 if (parms_have_cleanups)
5513 cleanup_label = gen_label_rtx ();
5514 else
5515 cleanup_label = 0;
5517 /* Make the label for return statements to jump to, if this machine
5518 does not have a one-instruction return and uses an epilogue,
5519 or if it returns a structure, or if it has parm cleanups. */
5520 #ifdef HAVE_return
5521 if (cleanup_label == 0 && HAVE_return
5522 && ! current_function_returns_pcc_struct
5523 && ! (current_function_returns_struct && ! optimize))
5524 return_label = 0;
5525 else
5526 return_label = gen_label_rtx ();
5527 #else
5528 return_label = gen_label_rtx ();
5529 #endif
5531 /* Initialize rtx used to return the value. */
5532 /* Do this before assign_parms so that we copy the struct value address
5533 before any library calls that assign parms might generate. */
5535 /* Decide whether to return the value in memory or in a register. */
5536 if (aggregate_value_p (DECL_RESULT (subr)))
5538 /* Returning something that won't go in a register. */
5539 register rtx value_address = 0;
5541 #ifdef PCC_STATIC_STRUCT_RETURN
5542 if (current_function_returns_pcc_struct)
5544 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
5545 value_address = assemble_static_space (size);
5547 else
5548 #endif
5550 /* Expect to be passed the address of a place to store the value.
5551 If it is passed as an argument, assign_parms will take care of
5552 it. */
5553 if (struct_value_incoming_rtx)
5555 value_address = gen_reg_rtx (Pmode);
5556 emit_move_insn (value_address, struct_value_incoming_rtx);
5559 if (value_address)
5561 DECL_RTL (DECL_RESULT (subr))
5562 = gen_rtx (MEM, DECL_MODE (DECL_RESULT (subr)), value_address);
5563 MEM_IN_STRUCT_P (DECL_RTL (DECL_RESULT (subr)))
5564 = AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
5567 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
5568 /* If return mode is void, this decl rtl should not be used. */
5569 DECL_RTL (DECL_RESULT (subr)) = 0;
5570 else if (parms_have_cleanups)
5572 /* If function will end with cleanup code for parms,
5573 compute the return values into a pseudo reg,
5574 which we will copy into the true return register
5575 after the cleanups are done. */
5577 enum machine_mode mode = DECL_MODE (DECL_RESULT (subr));
5579 #ifdef PROMOTE_FUNCTION_RETURN
5580 tree type = TREE_TYPE (DECL_RESULT (subr));
5581 int unsignedp = TREE_UNSIGNED (type);
5583 mode = promote_mode (type, mode, &unsignedp, 1);
5584 #endif
5586 DECL_RTL (DECL_RESULT (subr)) = gen_reg_rtx (mode);
5588 else
5589 /* Scalar, returned in a register. */
5591 #ifdef FUNCTION_OUTGOING_VALUE
5592 DECL_RTL (DECL_RESULT (subr))
5593 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (subr)), subr);
5594 #else
5595 DECL_RTL (DECL_RESULT (subr))
5596 = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (subr)), subr);
5597 #endif
5599 /* Mark this reg as the function's return value. */
5600 if (GET_CODE (DECL_RTL (DECL_RESULT (subr))) == REG)
5602 REG_FUNCTION_VALUE_P (DECL_RTL (DECL_RESULT (subr))) = 1;
5603 /* Needed because we may need to move this to memory
5604 in case it's a named return value whose address is taken. */
5605 DECL_REGISTER (DECL_RESULT (subr)) = 1;
5609 /* Initialize rtx for parameters and local variables.
5610 In some cases this requires emitting insns. */
5612 assign_parms (subr, 0);
5614 /* Copy the static chain now if it wasn't a register. The delay is to
5615 avoid conflicts with the parameter passing registers. */
5617 if (SMALL_REGISTER_CLASSES && current_function_needs_context)
5618 if (GET_CODE (static_chain_incoming_rtx) != REG)
5619 emit_move_insn (last_ptr, static_chain_incoming_rtx);
5621 /* The following was moved from init_function_start.
5622 The move is supposed to make sdb output more accurate. */
5623 /* Indicate the beginning of the function body,
5624 as opposed to parm setup. */
5625 emit_note (NULL_PTR, NOTE_INSN_FUNCTION_BEG);
5627 /* If doing stupid allocation, mark parms as born here. */
5629 if (GET_CODE (get_last_insn ()) != NOTE)
5630 emit_note (NULL_PTR, NOTE_INSN_DELETED);
5631 parm_birth_insn = get_last_insn ();
5633 if (obey_regdecls)
5635 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_parm_reg; i++)
5636 use_variable (regno_reg_rtx[i]);
5638 if (current_function_internal_arg_pointer != virtual_incoming_args_rtx)
5639 use_variable (current_function_internal_arg_pointer);
5642 context_display = 0;
5643 if (current_function_needs_context)
5645 /* Fetch static chain values for containing functions. */
5646 tem = decl_function_context (current_function_decl);
5647 /* If not doing stupid register allocation copy the static chain
5648 pointer into a pseudo. If we have small register classes, copy
5649 the value from memory if static_chain_incoming_rtx is a REG. If
5650 we do stupid register allocation, we use the stack address
5651 generated above. */
5652 if (tem && ! obey_regdecls)
5654 /* If the static chain originally came in a register, put it back
5655 there, then move it out in the next insn. The reason for
5656 this peculiar code is to satisfy function integration. */
5657 if (SMALL_REGISTER_CLASSES
5658 && GET_CODE (static_chain_incoming_rtx) == REG)
5659 emit_move_insn (static_chain_incoming_rtx, last_ptr);
5660 last_ptr = copy_to_reg (static_chain_incoming_rtx);
5663 while (tem)
5665 tree rtlexp = make_node (RTL_EXPR);
5667 RTL_EXPR_RTL (rtlexp) = last_ptr;
5668 context_display = tree_cons (tem, rtlexp, context_display);
5669 tem = decl_function_context (tem);
5670 if (tem == 0)
5671 break;
5672 /* Chain thru stack frames, assuming pointer to next lexical frame
5673 is found at the place we always store it. */
5674 #ifdef FRAME_GROWS_DOWNWARD
5675 last_ptr = plus_constant (last_ptr, - GET_MODE_SIZE (Pmode));
5676 #endif
5677 last_ptr = copy_to_reg (gen_rtx (MEM, Pmode,
5678 memory_address (Pmode, last_ptr)));
5680 /* If we are not optimizing, ensure that we know that this
5681 piece of context is live over the entire function. */
5682 if (! optimize)
5683 save_expr_regs = gen_rtx (EXPR_LIST, VOIDmode, last_ptr,
5684 save_expr_regs);
5688 /* After the display initializations is where the tail-recursion label
5689 should go, if we end up needing one. Ensure we have a NOTE here
5690 since some things (like trampolines) get placed before this. */
5691 tail_recursion_reentry = emit_note (NULL_PTR, NOTE_INSN_DELETED);
5693 /* Evaluate now the sizes of any types declared among the arguments. */
5694 for (tem = nreverse (get_pending_sizes ()); tem; tem = TREE_CHAIN (tem))
5696 expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode,
5697 EXPAND_MEMORY_USE_BAD);
5698 /* Flush the queue in case this parameter declaration has
5699 side-effects. */
5700 emit_queue ();
5703 /* Make sure there is a line number after the function entry setup code. */
5704 force_next_line_note ();
5707 /* Generate RTL for the end of the current function.
5708 FILENAME and LINE are the current position in the source file.
5710 It is up to language-specific callers to do cleanups for parameters--
5711 or else, supply 1 for END_BINDINGS and we will call expand_end_bindings. */
5713 void
5714 expand_function_end (filename, line, end_bindings)
5715 char *filename;
5716 int line;
5717 int end_bindings;
5719 register int i;
5720 tree link;
5722 #ifdef TRAMPOLINE_TEMPLATE
5723 static rtx initial_trampoline;
5724 #endif
5726 if (output_bytecode)
5728 bc_expand_function_end ();
5729 return;
5732 #ifdef NON_SAVING_SETJMP
5733 /* Don't put any variables in registers if we call setjmp
5734 on a machine that fails to restore the registers. */
5735 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
5737 if (DECL_INITIAL (current_function_decl) != error_mark_node)
5738 setjmp_protect (DECL_INITIAL (current_function_decl));
5740 setjmp_protect_args ();
5742 #endif
5744 /* Save the argument pointer if a save area was made for it. */
5745 if (arg_pointer_save_area)
5747 rtx x = gen_move_insn (arg_pointer_save_area, virtual_incoming_args_rtx);
5748 emit_insn_before (x, tail_recursion_reentry);
5751 /* Initialize any trampolines required by this function. */
5752 for (link = trampoline_list; link; link = TREE_CHAIN (link))
5754 tree function = TREE_PURPOSE (link);
5755 rtx context = lookup_static_chain (function);
5756 rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
5757 rtx blktramp;
5758 rtx seq;
5760 #ifdef TRAMPOLINE_TEMPLATE
5761 /* First make sure this compilation has a template for
5762 initializing trampolines. */
5763 if (initial_trampoline == 0)
5765 end_temporary_allocation ();
5766 initial_trampoline
5767 = gen_rtx (MEM, BLKmode, assemble_trampoline_template ());
5768 resume_temporary_allocation ();
5770 #endif
5772 /* Generate insns to initialize the trampoline. */
5773 start_sequence ();
5774 tramp = round_trampoline_addr (XEXP (tramp, 0));
5775 #ifdef TRAMPOLINE_TEMPLATE
5776 blktramp = change_address (initial_trampoline, BLKmode, tramp);
5777 emit_block_move (blktramp, initial_trampoline,
5778 GEN_INT (TRAMPOLINE_SIZE),
5779 TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5780 #endif
5781 INITIALIZE_TRAMPOLINE (tramp, XEXP (DECL_RTL (function), 0), context);
5782 seq = get_insns ();
5783 end_sequence ();
5785 /* Put those insns at entry to the containing function (this one). */
5786 emit_insns_before (seq, tail_recursion_reentry);
5789 /* If we are doing stack checking and this function makes calls,
5790 do a stack probe at the start of the function to ensure we have enough
5791 space for another stack frame. */
5792 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
5794 rtx insn, seq;
5796 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5797 if (GET_CODE (insn) == CALL_INSN)
5799 start_sequence ();
5800 probe_stack_range (STACK_CHECK_PROTECT,
5801 GEN_INT (STACK_CHECK_MAX_FRAME_SIZE));
5802 seq = get_insns ();
5803 end_sequence ();
5804 emit_insns_before (seq, tail_recursion_reentry);
5805 break;
5809 /* Warn about unused parms if extra warnings were specified. */
5810 if (warn_unused && extra_warnings)
5812 tree decl;
5814 for (decl = DECL_ARGUMENTS (current_function_decl);
5815 decl; decl = TREE_CHAIN (decl))
5816 if (! TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
5817 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
5818 warning_with_decl (decl, "unused parameter `%s'");
5821 /* Delete handlers for nonlocal gotos if nothing uses them. */
5822 if (nonlocal_goto_handler_slot != 0 && !current_function_has_nonlocal_label)
5823 delete_handlers ();
5825 /* End any sequences that failed to be closed due to syntax errors. */
5826 while (in_sequence_p ())
5827 end_sequence ();
5829 /* Outside function body, can't compute type's actual size
5830 until next function's body starts. */
5831 immediate_size_expand--;
5833 /* If doing stupid register allocation,
5834 mark register parms as dying here. */
5836 if (obey_regdecls)
5838 rtx tem;
5839 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_parm_reg; i++)
5840 use_variable (regno_reg_rtx[i]);
5842 /* Likewise for the regs of all the SAVE_EXPRs in the function. */
5844 for (tem = save_expr_regs; tem; tem = XEXP (tem, 1))
5846 use_variable (XEXP (tem, 0));
5847 use_variable_after (XEXP (tem, 0), parm_birth_insn);
5850 if (current_function_internal_arg_pointer != virtual_incoming_args_rtx)
5851 use_variable (current_function_internal_arg_pointer);
5854 clear_pending_stack_adjust ();
5855 do_pending_stack_adjust ();
5857 /* Mark the end of the function body.
5858 If control reaches this insn, the function can drop through
5859 without returning a value. */
5860 emit_note (NULL_PTR, NOTE_INSN_FUNCTION_END);
5862 /* Must mark the last line number note in the function, so that the test
5863 coverage code can avoid counting the last line twice. This just tells
5864 the code to ignore the immediately following line note, since there
5865 already exists a copy of this note somewhere above. This line number
5866 note is still needed for debugging though, so we can't delete it. */
5867 if (flag_test_coverage)
5868 emit_note (NULL_PTR, NOTE_REPEATED_LINE_NUMBER);
5870 /* Output a linenumber for the end of the function.
5871 SDB depends on this. */
5872 emit_line_note_force (filename, line);
5874 /* Output the label for the actual return from the function,
5875 if one is expected. This happens either because a function epilogue
5876 is used instead of a return instruction, or because a return was done
5877 with a goto in order to run local cleanups, or because of pcc-style
5878 structure returning. */
5880 if (return_label)
5881 emit_label (return_label);
5883 /* C++ uses this. */
5884 if (end_bindings)
5885 expand_end_bindings (0, 0, 0);
5887 /* Now handle any leftover exception regions that may have been
5888 created for the parameters. */
5890 rtx last = get_last_insn ();
5891 rtx label;
5893 expand_leftover_cleanups ();
5895 /* If the above emitted any code, may sure we jump around it. */
5896 if (last != get_last_insn ())
5898 label = gen_label_rtx ();
5899 last = emit_jump_insn_after (gen_jump (label), last);
5900 last = emit_barrier_after (last);
5901 emit_label (label);
5905 /* If we had calls to alloca, and this machine needs
5906 an accurate stack pointer to exit the function,
5907 insert some code to save and restore the stack pointer. */
5908 #ifdef EXIT_IGNORE_STACK
5909 if (! EXIT_IGNORE_STACK)
5910 #endif
5911 if (current_function_calls_alloca)
5913 rtx tem = 0;
5915 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
5916 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
5919 /* If scalar return value was computed in a pseudo-reg,
5920 copy that to the hard return register. */
5921 if (DECL_RTL (DECL_RESULT (current_function_decl)) != 0
5922 && GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) == REG
5923 && (REGNO (DECL_RTL (DECL_RESULT (current_function_decl)))
5924 >= FIRST_PSEUDO_REGISTER))
5926 rtx real_decl_result;
5928 #ifdef FUNCTION_OUTGOING_VALUE
5929 real_decl_result
5930 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (current_function_decl)),
5931 current_function_decl);
5932 #else
5933 real_decl_result
5934 = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (current_function_decl)),
5935 current_function_decl);
5936 #endif
5937 REG_FUNCTION_VALUE_P (real_decl_result) = 1;
5938 /* If this is a BLKmode structure being returned in registers, then use
5939 the mode computed in expand_return. */
5940 if (GET_MODE (real_decl_result) == BLKmode)
5941 PUT_MODE (real_decl_result,
5942 GET_MODE (DECL_RTL (DECL_RESULT (current_function_decl))));
5943 emit_move_insn (real_decl_result,
5944 DECL_RTL (DECL_RESULT (current_function_decl)));
5945 emit_insn (gen_rtx (USE, VOIDmode, real_decl_result));
5947 /* The delay slot scheduler assumes that current_function_return_rtx
5948 holds the hard register containing the return value, not a temporary
5949 pseudo. */
5950 current_function_return_rtx = real_decl_result;
5953 /* If returning a structure, arrange to return the address of the value
5954 in a place where debuggers expect to find it.
5956 If returning a structure PCC style,
5957 the caller also depends on this value.
5958 And current_function_returns_pcc_struct is not necessarily set. */
5959 if (current_function_returns_struct
5960 || current_function_returns_pcc_struct)
5962 rtx value_address = XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5963 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5964 #ifdef FUNCTION_OUTGOING_VALUE
5965 rtx outgoing
5966 = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
5967 current_function_decl);
5968 #else
5969 rtx outgoing
5970 = FUNCTION_VALUE (build_pointer_type (type),
5971 current_function_decl);
5972 #endif
5974 /* Mark this as a function return value so integrate will delete the
5975 assignment and USE below when inlining this function. */
5976 REG_FUNCTION_VALUE_P (outgoing) = 1;
5978 emit_move_insn (outgoing, value_address);
5979 use_variable (outgoing);
5982 /* Output a return insn if we are using one.
5983 Otherwise, let the rtl chain end here, to drop through
5984 into the epilogue. */
5986 #ifdef HAVE_return
5987 if (HAVE_return)
5989 emit_jump_insn (gen_return ());
5990 emit_barrier ();
5992 #endif
5994 /* Fix up any gotos that jumped out to the outermost
5995 binding level of the function.
5996 Must follow emitting RETURN_LABEL. */
5998 /* If you have any cleanups to do at this point,
5999 and they need to create temporary variables,
6000 then you will lose. */
6001 expand_fixups (get_insns ());
6004 /* These arrays record the INSN_UIDs of the prologue and epilogue insns. */
6006 static int *prologue;
6007 static int *epilogue;
6009 /* Create an array that records the INSN_UIDs of INSNS (either a sequence
6010 or a single insn). */
6012 static int *
6013 record_insns (insns)
6014 rtx insns;
6016 int *vec;
6018 if (GET_CODE (insns) == SEQUENCE)
6020 int len = XVECLEN (insns, 0);
6021 vec = (int *) oballoc ((len + 1) * sizeof (int));
6022 vec[len] = 0;
6023 while (--len >= 0)
6024 vec[len] = INSN_UID (XVECEXP (insns, 0, len));
6026 else
6028 vec = (int *) oballoc (2 * sizeof (int));
6029 vec[0] = INSN_UID (insns);
6030 vec[1] = 0;
6032 return vec;
6035 /* Determine how many INSN_UIDs in VEC are part of INSN. */
6037 static int
6038 contains (insn, vec)
6039 rtx insn;
6040 int *vec;
6042 register int i, j;
6044 if (GET_CODE (insn) == INSN
6045 && GET_CODE (PATTERN (insn)) == SEQUENCE)
6047 int count = 0;
6048 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
6049 for (j = 0; vec[j]; j++)
6050 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == vec[j])
6051 count++;
6052 return count;
6054 else
6056 for (j = 0; vec[j]; j++)
6057 if (INSN_UID (insn) == vec[j])
6058 return 1;
6060 return 0;
6063 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
6064 this into place with notes indicating where the prologue ends and where
6065 the epilogue begins. Update the basic block information when possible. */
6067 void
6068 thread_prologue_and_epilogue_insns (f)
6069 rtx f;
6071 #ifdef HAVE_prologue
6072 if (HAVE_prologue)
6074 rtx head, seq, insn;
6076 /* The first insn (a NOTE_INSN_DELETED) is followed by zero or more
6077 prologue insns and a NOTE_INSN_PROLOGUE_END. */
6078 emit_note_after (NOTE_INSN_PROLOGUE_END, f);
6079 seq = gen_prologue ();
6080 head = emit_insn_after (seq, f);
6082 /* Include the new prologue insns in the first block. Ignore them
6083 if they form a basic block unto themselves. */
6084 if (basic_block_head && n_basic_blocks
6085 && GET_CODE (basic_block_head[0]) != CODE_LABEL)
6086 basic_block_head[0] = NEXT_INSN (f);
6088 /* Retain a map of the prologue insns. */
6089 prologue = record_insns (GET_CODE (seq) == SEQUENCE ? seq : head);
6091 else
6092 #endif
6093 prologue = 0;
6095 #ifdef HAVE_epilogue
6096 if (HAVE_epilogue)
6098 rtx insn = get_last_insn ();
6099 rtx prev = prev_nonnote_insn (insn);
6101 /* If we end with a BARRIER, we don't need an epilogue. */
6102 if (! (prev && GET_CODE (prev) == BARRIER))
6104 rtx tail, seq, tem;
6105 rtx first_use = 0;
6106 rtx last_use = 0;
6108 /* The last basic block ends with a NOTE_INSN_EPILOGUE_BEG, the
6109 epilogue insns, the USE insns at the end of a function,
6110 the jump insn that returns, and then a BARRIER. */
6112 /* Move the USE insns at the end of a function onto a list. */
6113 while (prev
6114 && GET_CODE (prev) == INSN
6115 && GET_CODE (PATTERN (prev)) == USE)
6117 tem = prev;
6118 prev = prev_nonnote_insn (prev);
6120 NEXT_INSN (PREV_INSN (tem)) = NEXT_INSN (tem);
6121 PREV_INSN (NEXT_INSN (tem)) = PREV_INSN (tem);
6122 if (first_use)
6124 NEXT_INSN (tem) = first_use;
6125 PREV_INSN (first_use) = tem;
6127 first_use = tem;
6128 if (!last_use)
6129 last_use = tem;
6132 emit_barrier_after (insn);
6134 seq = gen_epilogue ();
6135 tail = emit_jump_insn_after (seq, insn);
6137 /* Insert the USE insns immediately before the return insn, which
6138 must be the first instruction before the final barrier. */
6139 if (first_use)
6141 tem = prev_nonnote_insn (get_last_insn ());
6142 NEXT_INSN (PREV_INSN (tem)) = first_use;
6143 PREV_INSN (first_use) = PREV_INSN (tem);
6144 PREV_INSN (tem) = last_use;
6145 NEXT_INSN (last_use) = tem;
6148 emit_note_after (NOTE_INSN_EPILOGUE_BEG, insn);
6150 /* Include the new epilogue insns in the last block. Ignore
6151 them if they form a basic block unto themselves. */
6152 if (basic_block_end && n_basic_blocks
6153 && GET_CODE (basic_block_end[n_basic_blocks - 1]) != JUMP_INSN)
6154 basic_block_end[n_basic_blocks - 1] = tail;
6156 /* Retain a map of the epilogue insns. */
6157 epilogue = record_insns (GET_CODE (seq) == SEQUENCE ? seq : tail);
6158 return;
6161 #endif
6162 epilogue = 0;
6165 /* Reposition the prologue-end and epilogue-begin notes after instruction
6166 scheduling and delayed branch scheduling. */
6168 void
6169 reposition_prologue_and_epilogue_notes (f)
6170 rtx f;
6172 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
6173 /* Reposition the prologue and epilogue notes. */
6174 if (n_basic_blocks)
6176 rtx next, prev;
6177 int len;
6179 if (prologue)
6181 register rtx insn, note = 0;
6183 /* Scan from the beginning until we reach the last prologue insn.
6184 We apparently can't depend on basic_block_{head,end} after
6185 reorg has run. */
6186 for (len = 0; prologue[len]; len++)
6188 for (insn = f; len && insn; insn = NEXT_INSN (insn))
6190 if (GET_CODE (insn) == NOTE)
6192 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
6193 note = insn;
6195 else if ((len -= contains (insn, prologue)) == 0)
6197 /* Find the prologue-end note if we haven't already, and
6198 move it to just after the last prologue insn. */
6199 if (note == 0)
6201 for (note = insn; note = NEXT_INSN (note);)
6202 if (GET_CODE (note) == NOTE
6203 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
6204 break;
6206 next = NEXT_INSN (note);
6207 prev = PREV_INSN (note);
6208 if (prev)
6209 NEXT_INSN (prev) = next;
6210 if (next)
6211 PREV_INSN (next) = prev;
6212 add_insn_after (note, insn);
6217 if (epilogue)
6219 register rtx insn, note = 0;
6221 /* Scan from the end until we reach the first epilogue insn.
6222 We apparently can't depend on basic_block_{head,end} after
6223 reorg has run. */
6224 for (len = 0; epilogue[len]; len++)
6226 for (insn = get_last_insn (); len && insn; insn = PREV_INSN (insn))
6228 if (GET_CODE (insn) == NOTE)
6230 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
6231 note = insn;
6233 else if ((len -= contains (insn, epilogue)) == 0)
6235 /* Find the epilogue-begin note if we haven't already, and
6236 move it to just before the first epilogue insn. */
6237 if (note == 0)
6239 for (note = insn; note = PREV_INSN (note);)
6240 if (GET_CODE (note) == NOTE
6241 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
6242 break;
6244 next = NEXT_INSN (note);
6245 prev = PREV_INSN (note);
6246 if (prev)
6247 NEXT_INSN (prev) = next;
6248 if (next)
6249 PREV_INSN (next) = prev;
6250 add_insn_after (note, PREV_INSN (insn));
6255 #endif /* HAVE_prologue or HAVE_epilogue */