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