]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/function.c
8ce5ef5ef79c277423b99ec05f2cc09797a6a0b9
[thirdparty/gcc.git] / gcc / function.c
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
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 "system.h"
43 #include "coretypes.h"
44 #include "tm.h"
45 #include "rtl.h"
46 #include "tree.h"
47 #include "flags.h"
48 #include "except.h"
49 #include "function.h"
50 #include "expr.h"
51 #include "optabs.h"
52 #include "libfuncs.h"
53 #include "regs.h"
54 #include "hard-reg-set.h"
55 #include "insn-config.h"
56 #include "recog.h"
57 #include "output.h"
58 #include "basic-block.h"
59 #include "toplev.h"
60 #include "hashtab.h"
61 #include "ggc.h"
62 #include "tm_p.h"
63 #include "integrate.h"
64 #include "langhooks.h"
65 #include "target.h"
66
67 #ifndef TRAMPOLINE_ALIGNMENT
68 #define TRAMPOLINE_ALIGNMENT FUNCTION_BOUNDARY
69 #endif
70
71 #ifndef LOCAL_ALIGNMENT
72 #define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
73 #endif
74
75 #ifndef STACK_ALIGNMENT_NEEDED
76 #define STACK_ALIGNMENT_NEEDED 1
77 #endif
78
79 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
80
81 /* Some systems use __main in a way incompatible with its use in gcc, in these
82 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
83 give the same symbol without quotes for an alternative entry point. You
84 must define both, or neither. */
85 #ifndef NAME__MAIN
86 #define NAME__MAIN "__main"
87 #endif
88
89 /* Round a value to the lowest integer less than it that is a multiple of
90 the required alignment. Avoid using division in case the value is
91 negative. Assume the alignment is a power of two. */
92 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
93
94 /* Similar, but round to the next highest integer that meets the
95 alignment. */
96 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
97
98 /* NEED_SEPARATE_AP means that we cannot derive ap from the value of fp
99 during rtl generation. If they are different register numbers, this is
100 always true. It may also be true if
101 FIRST_PARM_OFFSET - STARTING_FRAME_OFFSET is not a constant during rtl
102 generation. See fix_lexical_addr for details. */
103
104 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
105 #define NEED_SEPARATE_AP
106 #endif
107
108 /* Nonzero if function being compiled doesn't contain any calls
109 (ignoring the prologue and epilogue). This is set prior to
110 local register allocation and is valid for the remaining
111 compiler passes. */
112 int current_function_is_leaf;
113
114 /* Nonzero if function being compiled doesn't contain any instructions
115 that can throw an exception. This is set prior to final. */
116
117 int current_function_nothrow;
118
119 /* Nonzero if function being compiled doesn't modify the stack pointer
120 (ignoring the prologue and epilogue). This is only valid after
121 life_analysis has run. */
122 int current_function_sp_is_unchanging;
123
124 /* Nonzero if the function being compiled is a leaf function which only
125 uses leaf registers. This is valid after reload (specifically after
126 sched2) and is useful only if the port defines LEAF_REGISTERS. */
127 int current_function_uses_only_leaf_regs;
128
129 /* Nonzero once virtual register instantiation has been done.
130 assign_stack_local uses frame_pointer_rtx when this is nonzero.
131 calls.c:emit_library_call_value_1 uses it to set up
132 post-instantiation libcalls. */
133 int virtuals_instantiated;
134
135 /* Nonzero if at least one trampoline has been created. */
136 int trampolines_created;
137
138 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
139 static GTY(()) int funcdef_no;
140
141 /* These variables hold pointers to functions to create and destroy
142 target specific, per-function data structures. */
143 struct machine_function * (*init_machine_status) (void);
144
145 /* The FUNCTION_DECL for an inline function currently being expanded. */
146 tree inline_function_decl;
147
148 /* The currently compiled function. */
149 struct function *cfun = 0;
150
151 /* These arrays record the INSN_UIDs of the prologue and epilogue insns. */
152 static GTY(()) varray_type prologue;
153 static GTY(()) varray_type epilogue;
154
155 /* Array of INSN_UIDs to hold the INSN_UIDs for each sibcall epilogue
156 in this function. */
157 static GTY(()) varray_type sibcall_epilogue;
158 \f
159 /* In order to evaluate some expressions, such as function calls returning
160 structures in memory, we need to temporarily allocate stack locations.
161 We record each allocated temporary in the following structure.
162
163 Associated with each temporary slot is a nesting level. When we pop up
164 one level, all temporaries associated with the previous level are freed.
165 Normally, all temporaries are freed after the execution of the statement
166 in which they were created. However, if we are inside a ({...}) grouping,
167 the result may be in a temporary and hence must be preserved. If the
168 result could be in a temporary, we preserve it if we can determine which
169 one it is in. If we cannot determine which temporary may contain the
170 result, all temporaries are preserved. A temporary is preserved by
171 pretending it was allocated at the previous nesting level.
172
173 Automatic variables are also assigned temporary slots, at the nesting
174 level where they are defined. They are marked a "kept" so that
175 free_temp_slots will not free them. */
176
177 struct temp_slot GTY(())
178 {
179 /* Points to next temporary slot. */
180 struct temp_slot *next;
181 /* The rtx to used to reference the slot. */
182 rtx slot;
183 /* The rtx used to represent the address if not the address of the
184 slot above. May be an EXPR_LIST if multiple addresses exist. */
185 rtx address;
186 /* The alignment (in bits) of the slot. */
187 unsigned int align;
188 /* The size, in units, of the slot. */
189 HOST_WIDE_INT size;
190 /* The type of the object in the slot, or zero if it doesn't correspond
191 to a type. We use this to determine whether a slot can be reused.
192 It can be reused if objects of the type of the new slot will always
193 conflict with objects of the type of the old slot. */
194 tree type;
195 /* The value of `sequence_rtl_expr' when this temporary is allocated. */
196 tree rtl_expr;
197 /* Nonzero if this temporary is currently in use. */
198 char in_use;
199 /* Nonzero if this temporary has its address taken. */
200 char addr_taken;
201 /* Nesting level at which this slot is being used. */
202 int level;
203 /* Nonzero if this should survive a call to free_temp_slots. */
204 int keep;
205 /* The offset of the slot from the frame_pointer, including extra space
206 for alignment. This info is for combine_temp_slots. */
207 HOST_WIDE_INT base_offset;
208 /* The size of the slot, including extra space for alignment. This
209 info is for combine_temp_slots. */
210 HOST_WIDE_INT full_size;
211 };
212 \f
213 /* This structure is used to record MEMs or pseudos used to replace VAR, any
214 SUBREGs of VAR, and any MEMs containing VAR as an address. We need to
215 maintain this list in case two operands of an insn were required to match;
216 in that case we must ensure we use the same replacement. */
217
218 struct fixup_replacement GTY(())
219 {
220 rtx old;
221 rtx new;
222 struct fixup_replacement *next;
223 };
224
225 struct insns_for_mem_entry
226 {
227 /* A MEM. */
228 rtx key;
229 /* These are the INSNs which reference the MEM. */
230 rtx insns;
231 };
232
233 /* Forward declarations. */
234
235 static rtx assign_stack_local_1 (enum machine_mode, HOST_WIDE_INT, int,
236 struct function *);
237 static struct temp_slot *find_temp_slot_from_address (rtx);
238 static void put_reg_into_stack (struct function *, rtx, tree, enum machine_mode,
239 unsigned int, bool, bool, bool, htab_t);
240 static void schedule_fixup_var_refs (struct function *, rtx, tree, enum machine_mode,
241 htab_t);
242 static void fixup_var_refs (rtx, enum machine_mode, int, rtx, htab_t);
243 static struct fixup_replacement
244 *find_fixup_replacement (struct fixup_replacement **, rtx);
245 static void fixup_var_refs_insns (rtx, rtx, enum machine_mode, int, int, rtx);
246 static void fixup_var_refs_insns_with_hash (htab_t, rtx, enum machine_mode, int, rtx);
247 static void fixup_var_refs_insn (rtx, rtx, enum machine_mode, int, int, rtx);
248 static void fixup_var_refs_1 (rtx, enum machine_mode, rtx *, rtx,
249 struct fixup_replacement **, rtx);
250 static rtx fixup_memory_subreg (rtx, rtx, enum machine_mode, int);
251 static rtx walk_fixup_memory_subreg (rtx, rtx, enum machine_mode, int);
252 static rtx fixup_stack_1 (rtx, rtx);
253 static void optimize_bit_field (rtx, rtx, rtx *);
254 static void instantiate_decls (tree, int);
255 static void instantiate_decls_1 (tree, int);
256 static void instantiate_decl (rtx, HOST_WIDE_INT, int);
257 static rtx instantiate_new_reg (rtx, HOST_WIDE_INT *);
258 static int instantiate_virtual_regs_1 (rtx *, rtx, int);
259 static void delete_handlers (void);
260 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
261 static void pad_below (struct args_size *, enum machine_mode, tree);
262 static rtx round_trampoline_addr (rtx);
263 static rtx adjust_trampoline_addr (rtx);
264 static tree *identify_blocks_1 (rtx, tree *, tree *, tree *);
265 static void reorder_blocks_0 (tree);
266 static void reorder_blocks_1 (rtx, tree, varray_type *);
267 static void reorder_fix_fragments (tree);
268 static tree blocks_nreverse (tree);
269 static int all_blocks (tree, tree *);
270 static tree *get_block_vector (tree, int *);
271 extern tree debug_find_var_in_block_tree (tree, tree);
272 /* We always define `record_insns' even if it's not used so that we
273 can always export `prologue_epilogue_contains'. */
274 static void record_insns (rtx, varray_type *) ATTRIBUTE_UNUSED;
275 static int contains (rtx, varray_type);
276 #ifdef HAVE_return
277 static void emit_return_into_block (basic_block, rtx);
278 #endif
279 static void put_addressof_into_stack (rtx, htab_t);
280 static bool purge_addressof_1 (rtx *, rtx, int, int, int, htab_t);
281 static void purge_single_hard_subreg_set (rtx);
282 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
283 static rtx keep_stack_depressed (rtx);
284 #endif
285 static int is_addressof (rtx *, void *);
286 static hashval_t insns_for_mem_hash (const void *);
287 static int insns_for_mem_comp (const void *, const void *);
288 static int insns_for_mem_walk (rtx *, void *);
289 static void compute_insns_for_mem (rtx, rtx, htab_t);
290 static void prepare_function_start (tree);
291 static void do_clobber_return_reg (rtx, void *);
292 static void do_use_return_reg (rtx, void *);
293 static void instantiate_virtual_regs_lossage (rtx);
294 static tree split_complex_args (tree);
295 static void set_insn_locators (rtx, int) ATTRIBUTE_UNUSED;
296 \f
297 /* Pointer to chain of `struct function' for containing functions. */
298 struct function *outer_function_chain;
299
300 /* List of insns that were postponed by purge_addressof_1. */
301 static rtx postponed_insns;
302
303 /* Given a function decl for a containing function,
304 return the `struct function' for it. */
305
306 struct function *
307 find_function_data (tree decl)
308 {
309 struct function *p;
310
311 for (p = outer_function_chain; p; p = p->outer)
312 if (p->decl == decl)
313 return p;
314
315 abort ();
316 }
317
318 /* Save the current context for compilation of a nested function.
319 This is called from language-specific code. The caller should use
320 the enter_nested langhook to save any language-specific state,
321 since this function knows only about language-independent
322 variables. */
323
324 void
325 push_function_context_to (tree context)
326 {
327 struct function *p;
328
329 if (context)
330 {
331 if (context == current_function_decl)
332 cfun->contains_functions = 1;
333 else
334 {
335 struct function *containing = find_function_data (context);
336 containing->contains_functions = 1;
337 }
338 }
339
340 if (cfun == 0)
341 init_dummy_function_start ();
342 p = cfun;
343
344 p->outer = outer_function_chain;
345 outer_function_chain = p;
346 p->fixup_var_refs_queue = 0;
347
348 lang_hooks.function.enter_nested (p);
349
350 cfun = 0;
351 }
352
353 void
354 push_function_context (void)
355 {
356 push_function_context_to (current_function_decl);
357 }
358
359 /* Restore the last saved context, at the end of a nested function.
360 This function is called from language-specific code. */
361
362 void
363 pop_function_context_from (tree context ATTRIBUTE_UNUSED)
364 {
365 struct function *p = outer_function_chain;
366 struct var_refs_queue *queue;
367
368 cfun = p;
369 outer_function_chain = p->outer;
370
371 current_function_decl = p->decl;
372 reg_renumber = 0;
373
374 restore_emit_status (p);
375
376 lang_hooks.function.leave_nested (p);
377
378 /* Finish doing put_var_into_stack for any of our variables which became
379 addressable during the nested function. If only one entry has to be
380 fixed up, just do that one. Otherwise, first make a list of MEMs that
381 are not to be unshared. */
382 if (p->fixup_var_refs_queue == 0)
383 ;
384 else if (p->fixup_var_refs_queue->next == 0)
385 fixup_var_refs (p->fixup_var_refs_queue->modified,
386 p->fixup_var_refs_queue->promoted_mode,
387 p->fixup_var_refs_queue->unsignedp,
388 p->fixup_var_refs_queue->modified, 0);
389 else
390 {
391 rtx list = 0;
392
393 for (queue = p->fixup_var_refs_queue; queue; queue = queue->next)
394 list = gen_rtx_EXPR_LIST (VOIDmode, queue->modified, list);
395
396 for (queue = p->fixup_var_refs_queue; queue; queue = queue->next)
397 fixup_var_refs (queue->modified, queue->promoted_mode,
398 queue->unsignedp, list, 0);
399
400 }
401
402 p->fixup_var_refs_queue = 0;
403
404 /* Reset variables that have known state during rtx generation. */
405 rtx_equal_function_value_matters = 1;
406 virtuals_instantiated = 0;
407 generating_concat_p = 1;
408 }
409
410 void
411 pop_function_context (void)
412 {
413 pop_function_context_from (current_function_decl);
414 }
415
416 /* Clear out all parts of the state in F that can safely be discarded
417 after the function has been parsed, but not compiled, to let
418 garbage collection reclaim the memory. */
419
420 void
421 free_after_parsing (struct function *f)
422 {
423 /* f->expr->forced_labels is used by code generation. */
424 /* f->emit->regno_reg_rtx is used by code generation. */
425 /* f->varasm is used by code generation. */
426 /* f->eh->eh_return_stub_label is used by code generation. */
427
428 lang_hooks.function.final (f);
429 f->stmt = NULL;
430 }
431
432 /* Clear out all parts of the state in F that can safely be discarded
433 after the function has been compiled, to let garbage collection
434 reclaim the memory. */
435
436 void
437 free_after_compilation (struct function *f)
438 {
439 f->eh = NULL;
440 f->expr = NULL;
441 f->emit = NULL;
442 f->varasm = NULL;
443 f->machine = NULL;
444
445 f->x_temp_slots = NULL;
446 f->arg_offset_rtx = NULL;
447 f->return_rtx = NULL;
448 f->internal_arg_pointer = NULL;
449 f->x_nonlocal_labels = NULL;
450 f->x_nonlocal_goto_handler_slots = NULL;
451 f->x_nonlocal_goto_handler_labels = NULL;
452 f->x_nonlocal_goto_stack_level = NULL;
453 f->x_cleanup_label = NULL;
454 f->x_return_label = NULL;
455 f->x_naked_return_label = NULL;
456 f->computed_goto_common_label = NULL;
457 f->computed_goto_common_reg = NULL;
458 f->x_save_expr_regs = NULL;
459 f->x_stack_slot_list = NULL;
460 f->x_rtl_expr_chain = NULL;
461 f->x_tail_recursion_label = NULL;
462 f->x_tail_recursion_reentry = NULL;
463 f->x_arg_pointer_save_area = NULL;
464 f->x_clobber_return_insn = NULL;
465 f->x_context_display = NULL;
466 f->x_trampoline_list = NULL;
467 f->x_parm_birth_insn = NULL;
468 f->x_last_parm_insn = NULL;
469 f->x_parm_reg_stack_loc = NULL;
470 f->fixup_var_refs_queue = NULL;
471 f->original_arg_vector = NULL;
472 f->original_decl_initial = NULL;
473 f->inl_last_parm_insn = NULL;
474 f->epilogue_delay_list = NULL;
475 }
476 \f
477 /* Allocate fixed slots in the stack frame of the current function. */
478
479 /* Return size needed for stack frame based on slots so far allocated in
480 function F.
481 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
482 the caller may have to do that. */
483
484 HOST_WIDE_INT
485 get_func_frame_size (struct function *f)
486 {
487 #ifdef FRAME_GROWS_DOWNWARD
488 return -f->x_frame_offset;
489 #else
490 return f->x_frame_offset;
491 #endif
492 }
493
494 /* Return size needed for stack frame based on slots so far allocated.
495 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
496 the caller may have to do that. */
497 HOST_WIDE_INT
498 get_frame_size (void)
499 {
500 return get_func_frame_size (cfun);
501 }
502
503 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
504 with machine mode MODE.
505
506 ALIGN controls the amount of alignment for the address of the slot:
507 0 means according to MODE,
508 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
509 -2 means use BITS_PER_UNIT,
510 positive specifies alignment boundary in bits.
511
512 We do not round to stack_boundary here.
513
514 FUNCTION specifies the function to allocate in. */
515
516 static rtx
517 assign_stack_local_1 (enum machine_mode mode, HOST_WIDE_INT size, int align,
518 struct function *function)
519 {
520 rtx x, addr;
521 int bigend_correction = 0;
522 int alignment;
523 int frame_off, frame_alignment, frame_phase;
524
525 if (align == 0)
526 {
527 tree type;
528
529 if (mode == BLKmode)
530 alignment = BIGGEST_ALIGNMENT;
531 else
532 alignment = GET_MODE_ALIGNMENT (mode);
533
534 /* Allow the target to (possibly) increase the alignment of this
535 stack slot. */
536 type = lang_hooks.types.type_for_mode (mode, 0);
537 if (type)
538 alignment = LOCAL_ALIGNMENT (type, alignment);
539
540 alignment /= BITS_PER_UNIT;
541 }
542 else if (align == -1)
543 {
544 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
545 size = CEIL_ROUND (size, alignment);
546 }
547 else if (align == -2)
548 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
549 else
550 alignment = align / BITS_PER_UNIT;
551
552 #ifdef FRAME_GROWS_DOWNWARD
553 function->x_frame_offset -= size;
554 #endif
555
556 /* Ignore alignment we can't do with expected alignment of the boundary. */
557 if (alignment * BITS_PER_UNIT > PREFERRED_STACK_BOUNDARY)
558 alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
559
560 if (function->stack_alignment_needed < alignment * BITS_PER_UNIT)
561 function->stack_alignment_needed = alignment * BITS_PER_UNIT;
562
563 /* Calculate how many bytes the start of local variables is off from
564 stack alignment. */
565 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
566 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
567 frame_phase = frame_off ? frame_alignment - frame_off : 0;
568
569 /* Round the frame offset to the specified alignment. The default is
570 to always honor requests to align the stack but a port may choose to
571 do its own stack alignment by defining STACK_ALIGNMENT_NEEDED. */
572 if (STACK_ALIGNMENT_NEEDED
573 || mode != BLKmode
574 || size != 0)
575 {
576 /* We must be careful here, since FRAME_OFFSET might be negative and
577 division with a negative dividend isn't as well defined as we might
578 like. So we instead assume that ALIGNMENT is a power of two and
579 use logical operations which are unambiguous. */
580 #ifdef FRAME_GROWS_DOWNWARD
581 function->x_frame_offset
582 = (FLOOR_ROUND (function->x_frame_offset - frame_phase, alignment)
583 + frame_phase);
584 #else
585 function->x_frame_offset
586 = (CEIL_ROUND (function->x_frame_offset - frame_phase, alignment)
587 + frame_phase);
588 #endif
589 }
590
591 /* On a big-endian machine, if we are allocating more space than we will use,
592 use the least significant bytes of those that are allocated. */
593 if (BYTES_BIG_ENDIAN && mode != BLKmode)
594 bigend_correction = size - GET_MODE_SIZE (mode);
595
596 /* If we have already instantiated virtual registers, return the actual
597 address relative to the frame pointer. */
598 if (function == cfun && virtuals_instantiated)
599 addr = plus_constant (frame_pointer_rtx,
600 trunc_int_for_mode
601 (frame_offset + bigend_correction
602 + STARTING_FRAME_OFFSET, Pmode));
603 else
604 addr = plus_constant (virtual_stack_vars_rtx,
605 trunc_int_for_mode
606 (function->x_frame_offset + bigend_correction,
607 Pmode));
608
609 #ifndef FRAME_GROWS_DOWNWARD
610 function->x_frame_offset += size;
611 #endif
612
613 x = gen_rtx_MEM (mode, addr);
614
615 function->x_stack_slot_list
616 = gen_rtx_EXPR_LIST (VOIDmode, x, function->x_stack_slot_list);
617
618 return x;
619 }
620
621 /* Wrapper around assign_stack_local_1; assign a local stack slot for the
622 current function. */
623
624 rtx
625 assign_stack_local (enum machine_mode mode, HOST_WIDE_INT size, int align)
626 {
627 return assign_stack_local_1 (mode, size, align, cfun);
628 }
629 \f
630 /* Allocate a temporary stack slot and record it for possible later
631 reuse.
632
633 MODE is the machine mode to be given to the returned rtx.
634
635 SIZE is the size in units of the space required. We do no rounding here
636 since assign_stack_local will do any required rounding.
637
638 KEEP is 1 if this slot is to be retained after a call to
639 free_temp_slots. Automatic variables for a block are allocated
640 with this flag. KEEP is 2 if we allocate a longer term temporary,
641 whose lifetime is controlled by CLEANUP_POINT_EXPRs. KEEP is 3
642 if we are to allocate something at an inner level to be treated as
643 a variable in the block (e.g., a SAVE_EXPR).
644
645 TYPE is the type that will be used for the stack slot. */
646
647 rtx
648 assign_stack_temp_for_type (enum machine_mode mode, HOST_WIDE_INT size, int keep,
649 tree type)
650 {
651 unsigned int align;
652 struct temp_slot *p, *best_p = 0;
653 rtx slot;
654
655 /* If SIZE is -1 it means that somebody tried to allocate a temporary
656 of a variable size. */
657 if (size == -1)
658 abort ();
659
660 if (mode == BLKmode)
661 align = BIGGEST_ALIGNMENT;
662 else
663 align = GET_MODE_ALIGNMENT (mode);
664
665 if (! type)
666 type = lang_hooks.types.type_for_mode (mode, 0);
667
668 if (type)
669 align = LOCAL_ALIGNMENT (type, align);
670
671 /* Try to find an available, already-allocated temporary of the proper
672 mode which meets the size and alignment requirements. Choose the
673 smallest one with the closest alignment. */
674 for (p = temp_slots; p; p = p->next)
675 if (p->align >= align && p->size >= size && GET_MODE (p->slot) == mode
676 && ! p->in_use
677 && objects_must_conflict_p (p->type, type)
678 && (best_p == 0 || best_p->size > p->size
679 || (best_p->size == p->size && best_p->align > p->align)))
680 {
681 if (p->align == align && p->size == size)
682 {
683 best_p = 0;
684 break;
685 }
686 best_p = p;
687 }
688
689 /* Make our best, if any, the one to use. */
690 if (best_p)
691 {
692 /* If there are enough aligned bytes left over, make them into a new
693 temp_slot so that the extra bytes don't get wasted. Do this only
694 for BLKmode slots, so that we can be sure of the alignment. */
695 if (GET_MODE (best_p->slot) == BLKmode)
696 {
697 int alignment = best_p->align / BITS_PER_UNIT;
698 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
699
700 if (best_p->size - rounded_size >= alignment)
701 {
702 p = ggc_alloc (sizeof (struct temp_slot));
703 p->in_use = p->addr_taken = 0;
704 p->size = best_p->size - rounded_size;
705 p->base_offset = best_p->base_offset + rounded_size;
706 p->full_size = best_p->full_size - rounded_size;
707 p->slot = gen_rtx_MEM (BLKmode,
708 plus_constant (XEXP (best_p->slot, 0),
709 rounded_size));
710 p->align = best_p->align;
711 p->address = 0;
712 p->rtl_expr = 0;
713 p->type = best_p->type;
714 p->next = temp_slots;
715 temp_slots = p;
716
717 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
718 stack_slot_list);
719
720 best_p->size = rounded_size;
721 best_p->full_size = rounded_size;
722 }
723 }
724
725 p = best_p;
726 }
727
728 /* If we still didn't find one, make a new temporary. */
729 if (p == 0)
730 {
731 HOST_WIDE_INT frame_offset_old = frame_offset;
732
733 p = ggc_alloc (sizeof (struct temp_slot));
734
735 /* We are passing an explicit alignment request to assign_stack_local.
736 One side effect of that is assign_stack_local will not round SIZE
737 to ensure the frame offset remains suitably aligned.
738
739 So for requests which depended on the rounding of SIZE, we go ahead
740 and round it now. We also make sure ALIGNMENT is at least
741 BIGGEST_ALIGNMENT. */
742 if (mode == BLKmode && align < BIGGEST_ALIGNMENT)
743 abort ();
744 p->slot = assign_stack_local (mode,
745 (mode == BLKmode
746 ? CEIL_ROUND (size, (int) align / BITS_PER_UNIT)
747 : size),
748 align);
749
750 p->align = align;
751
752 /* The following slot size computation is necessary because we don't
753 know the actual size of the temporary slot until assign_stack_local
754 has performed all the frame alignment and size rounding for the
755 requested temporary. Note that extra space added for alignment
756 can be either above or below this stack slot depending on which
757 way the frame grows. We include the extra space if and only if it
758 is above this slot. */
759 #ifdef FRAME_GROWS_DOWNWARD
760 p->size = frame_offset_old - frame_offset;
761 #else
762 p->size = size;
763 #endif
764
765 /* Now define the fields used by combine_temp_slots. */
766 #ifdef FRAME_GROWS_DOWNWARD
767 p->base_offset = frame_offset;
768 p->full_size = frame_offset_old - frame_offset;
769 #else
770 p->base_offset = frame_offset_old;
771 p->full_size = frame_offset - frame_offset_old;
772 #endif
773 p->address = 0;
774 p->next = temp_slots;
775 temp_slots = p;
776 }
777
778 p->in_use = 1;
779 p->addr_taken = 0;
780 p->rtl_expr = seq_rtl_expr;
781 p->type = type;
782
783 if (keep == 2)
784 {
785 p->level = target_temp_slot_level;
786 p->keep = 1;
787 }
788 else if (keep == 3)
789 {
790 p->level = var_temp_slot_level;
791 p->keep = 0;
792 }
793 else
794 {
795 p->level = temp_slot_level;
796 p->keep = keep;
797 }
798
799
800 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
801 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
802 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
803
804 /* If we know the alias set for the memory that will be used, use
805 it. If there's no TYPE, then we don't know anything about the
806 alias set for the memory. */
807 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
808 set_mem_align (slot, align);
809
810 /* If a type is specified, set the relevant flags. */
811 if (type != 0)
812 {
813 RTX_UNCHANGING_P (slot) = (lang_hooks.honor_readonly
814 && TYPE_READONLY (type));
815 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
816 MEM_SET_IN_STRUCT_P (slot, AGGREGATE_TYPE_P (type));
817 }
818
819 return slot;
820 }
821
822 /* Allocate a temporary stack slot and record it for possible later
823 reuse. First three arguments are same as in preceding function. */
824
825 rtx
826 assign_stack_temp (enum machine_mode mode, HOST_WIDE_INT size, int keep)
827 {
828 return assign_stack_temp_for_type (mode, size, keep, NULL_TREE);
829 }
830 \f
831 /* Assign a temporary.
832 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
833 and so that should be used in error messages. In either case, we
834 allocate of the given type.
835 KEEP is as for assign_stack_temp.
836 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
837 it is 0 if a register is OK.
838 DONT_PROMOTE is 1 if we should not promote values in register
839 to wider modes. */
840
841 rtx
842 assign_temp (tree type_or_decl, int keep, int memory_required,
843 int dont_promote ATTRIBUTE_UNUSED)
844 {
845 tree type, decl;
846 enum machine_mode mode;
847 #ifdef PROMOTE_MODE
848 int unsignedp;
849 #endif
850
851 if (DECL_P (type_or_decl))
852 decl = type_or_decl, type = TREE_TYPE (decl);
853 else
854 decl = NULL, type = type_or_decl;
855
856 mode = TYPE_MODE (type);
857 #ifdef PROMOTE_MODE
858 unsignedp = TYPE_UNSIGNED (type);
859 #endif
860
861 if (mode == BLKmode || memory_required)
862 {
863 HOST_WIDE_INT size = int_size_in_bytes (type);
864 rtx tmp;
865
866 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
867 problems with allocating the stack space. */
868 if (size == 0)
869 size = 1;
870
871 /* Unfortunately, we don't yet know how to allocate variable-sized
872 temporaries. However, sometimes we have a fixed upper limit on
873 the size (which is stored in TYPE_ARRAY_MAX_SIZE) and can use that
874 instead. This is the case for Chill variable-sized strings. */
875 if (size == -1 && TREE_CODE (type) == ARRAY_TYPE
876 && TYPE_ARRAY_MAX_SIZE (type) != NULL_TREE
877 && host_integerp (TYPE_ARRAY_MAX_SIZE (type), 1))
878 size = tree_low_cst (TYPE_ARRAY_MAX_SIZE (type), 1);
879
880 /* The size of the temporary may be too large to fit into an integer. */
881 /* ??? Not sure this should happen except for user silliness, so limit
882 this to things that aren't compiler-generated temporaries. The
883 rest of the time we'll abort in assign_stack_temp_for_type. */
884 if (decl && size == -1
885 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
886 {
887 error ("%Jsize of variable '%D' is too large", decl, decl);
888 size = 1;
889 }
890
891 tmp = assign_stack_temp_for_type (mode, size, keep, type);
892 return tmp;
893 }
894
895 #ifdef PROMOTE_MODE
896 if (! dont_promote)
897 mode = promote_mode (type, mode, &unsignedp, 0);
898 #endif
899
900 return gen_reg_rtx (mode);
901 }
902 \f
903 /* Combine temporary stack slots which are adjacent on the stack.
904
905 This allows for better use of already allocated stack space. This is only
906 done for BLKmode slots because we can be sure that we won't have alignment
907 problems in this case. */
908
909 void
910 combine_temp_slots (void)
911 {
912 struct temp_slot *p, *q;
913 struct temp_slot *prev_p, *prev_q;
914 int num_slots;
915
916 /* We can't combine slots, because the information about which slot
917 is in which alias set will be lost. */
918 if (flag_strict_aliasing)
919 return;
920
921 /* If there are a lot of temp slots, don't do anything unless
922 high levels of optimization. */
923 if (! flag_expensive_optimizations)
924 for (p = temp_slots, num_slots = 0; p; p = p->next, num_slots++)
925 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
926 return;
927
928 for (p = temp_slots, prev_p = 0; p; p = prev_p ? prev_p->next : temp_slots)
929 {
930 int delete_p = 0;
931
932 if (! p->in_use && GET_MODE (p->slot) == BLKmode)
933 for (q = p->next, prev_q = p; q; q = prev_q->next)
934 {
935 int delete_q = 0;
936 if (! q->in_use && GET_MODE (q->slot) == BLKmode)
937 {
938 if (p->base_offset + p->full_size == q->base_offset)
939 {
940 /* Q comes after P; combine Q into P. */
941 p->size += q->size;
942 p->full_size += q->full_size;
943 delete_q = 1;
944 }
945 else if (q->base_offset + q->full_size == p->base_offset)
946 {
947 /* P comes after Q; combine P into Q. */
948 q->size += p->size;
949 q->full_size += p->full_size;
950 delete_p = 1;
951 break;
952 }
953 }
954 /* Either delete Q or advance past it. */
955 if (delete_q)
956 prev_q->next = q->next;
957 else
958 prev_q = q;
959 }
960 /* Either delete P or advance past it. */
961 if (delete_p)
962 {
963 if (prev_p)
964 prev_p->next = p->next;
965 else
966 temp_slots = p->next;
967 }
968 else
969 prev_p = p;
970 }
971 }
972 \f
973 /* Find the temp slot corresponding to the object at address X. */
974
975 static struct temp_slot *
976 find_temp_slot_from_address (rtx x)
977 {
978 struct temp_slot *p;
979 rtx next;
980
981 for (p = temp_slots; p; p = p->next)
982 {
983 if (! p->in_use)
984 continue;
985
986 else if (XEXP (p->slot, 0) == x
987 || p->address == x
988 || (GET_CODE (x) == PLUS
989 && XEXP (x, 0) == virtual_stack_vars_rtx
990 && GET_CODE (XEXP (x, 1)) == CONST_INT
991 && INTVAL (XEXP (x, 1)) >= p->base_offset
992 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size))
993 return p;
994
995 else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
996 for (next = p->address; next; next = XEXP (next, 1))
997 if (XEXP (next, 0) == x)
998 return p;
999 }
1000
1001 /* If we have a sum involving a register, see if it points to a temp
1002 slot. */
1003 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == REG
1004 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
1005 return p;
1006 else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG
1007 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
1008 return p;
1009
1010 return 0;
1011 }
1012
1013 /* Indicate that NEW is an alternate way of referring to the temp slot
1014 that previously was known by OLD. */
1015
1016 void
1017 update_temp_slot_address (rtx old, rtx new)
1018 {
1019 struct temp_slot *p;
1020
1021 if (rtx_equal_p (old, new))
1022 return;
1023
1024 p = find_temp_slot_from_address (old);
1025
1026 /* If we didn't find one, see if both OLD is a PLUS. If so, and NEW
1027 is a register, see if one operand of the PLUS is a temporary
1028 location. If so, NEW points into it. Otherwise, if both OLD and
1029 NEW are a PLUS and if there is a register in common between them.
1030 If so, try a recursive call on those values. */
1031 if (p == 0)
1032 {
1033 if (GET_CODE (old) != PLUS)
1034 return;
1035
1036 if (GET_CODE (new) == REG)
1037 {
1038 update_temp_slot_address (XEXP (old, 0), new);
1039 update_temp_slot_address (XEXP (old, 1), new);
1040 return;
1041 }
1042 else if (GET_CODE (new) != PLUS)
1043 return;
1044
1045 if (rtx_equal_p (XEXP (old, 0), XEXP (new, 0)))
1046 update_temp_slot_address (XEXP (old, 1), XEXP (new, 1));
1047 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 0)))
1048 update_temp_slot_address (XEXP (old, 0), XEXP (new, 1));
1049 else if (rtx_equal_p (XEXP (old, 0), XEXP (new, 1)))
1050 update_temp_slot_address (XEXP (old, 1), XEXP (new, 0));
1051 else if (rtx_equal_p (XEXP (old, 1), XEXP (new, 1)))
1052 update_temp_slot_address (XEXP (old, 0), XEXP (new, 0));
1053
1054 return;
1055 }
1056
1057 /* Otherwise add an alias for the temp's address. */
1058 else if (p->address == 0)
1059 p->address = new;
1060 else
1061 {
1062 if (GET_CODE (p->address) != EXPR_LIST)
1063 p->address = gen_rtx_EXPR_LIST (VOIDmode, p->address, NULL_RTX);
1064
1065 p->address = gen_rtx_EXPR_LIST (VOIDmode, new, p->address);
1066 }
1067 }
1068
1069 /* If X could be a reference to a temporary slot, mark the fact that its
1070 address was taken. */
1071
1072 void
1073 mark_temp_addr_taken (rtx x)
1074 {
1075 struct temp_slot *p;
1076
1077 if (x == 0)
1078 return;
1079
1080 /* If X is not in memory or is at a constant address, it cannot be in
1081 a temporary slot. */
1082 if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1083 return;
1084
1085 p = find_temp_slot_from_address (XEXP (x, 0));
1086 if (p != 0)
1087 p->addr_taken = 1;
1088 }
1089
1090 /* If X could be a reference to a temporary slot, mark that slot as
1091 belonging to the to one level higher than the current level. If X
1092 matched one of our slots, just mark that one. Otherwise, we can't
1093 easily predict which it is, so upgrade all of them. Kept slots
1094 need not be touched.
1095
1096 This is called when an ({...}) construct occurs and a statement
1097 returns a value in memory. */
1098
1099 void
1100 preserve_temp_slots (rtx x)
1101 {
1102 struct temp_slot *p = 0;
1103
1104 /* If there is no result, we still might have some objects whose address
1105 were taken, so we need to make sure they stay around. */
1106 if (x == 0)
1107 {
1108 for (p = temp_slots; p; p = p->next)
1109 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1110 p->level--;
1111
1112 return;
1113 }
1114
1115 /* If X is a register that is being used as a pointer, see if we have
1116 a temporary slot we know it points to. To be consistent with
1117 the code below, we really should preserve all non-kept slots
1118 if we can't find a match, but that seems to be much too costly. */
1119 if (GET_CODE (x) == REG && REG_POINTER (x))
1120 p = find_temp_slot_from_address (x);
1121
1122 /* If X is not in memory or is at a constant address, it cannot be in
1123 a temporary slot, but it can contain something whose address was
1124 taken. */
1125 if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
1126 {
1127 for (p = temp_slots; p; p = p->next)
1128 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1129 p->level--;
1130
1131 return;
1132 }
1133
1134 /* First see if we can find a match. */
1135 if (p == 0)
1136 p = find_temp_slot_from_address (XEXP (x, 0));
1137
1138 if (p != 0)
1139 {
1140 /* Move everything at our level whose address was taken to our new
1141 level in case we used its address. */
1142 struct temp_slot *q;
1143
1144 if (p->level == temp_slot_level)
1145 {
1146 for (q = temp_slots; q; q = q->next)
1147 if (q != p && q->addr_taken && q->level == p->level)
1148 q->level--;
1149
1150 p->level--;
1151 p->addr_taken = 0;
1152 }
1153 return;
1154 }
1155
1156 /* Otherwise, preserve all non-kept slots at this level. */
1157 for (p = temp_slots; p; p = p->next)
1158 if (p->in_use && p->level == temp_slot_level && ! p->keep)
1159 p->level--;
1160 }
1161
1162 /* X is the result of an RTL_EXPR. If it is a temporary slot associated
1163 with that RTL_EXPR, promote it into a temporary slot at the present
1164 level so it will not be freed when we free slots made in the
1165 RTL_EXPR. */
1166
1167 void
1168 preserve_rtl_expr_result (rtx x)
1169 {
1170 struct temp_slot *p;
1171
1172 /* If X is not in memory or is at a constant address, it cannot be in
1173 a temporary slot. */
1174 if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1175 return;
1176
1177 /* If we can find a match, move it to our level unless it is already at
1178 an upper level. */
1179 p = find_temp_slot_from_address (XEXP (x, 0));
1180 if (p != 0)
1181 {
1182 p->level = MIN (p->level, temp_slot_level);
1183 p->rtl_expr = 0;
1184 }
1185
1186 return;
1187 }
1188
1189 /* Free all temporaries used so far. This is normally called at the end
1190 of generating code for a statement. Don't free any temporaries
1191 currently in use for an RTL_EXPR that hasn't yet been emitted.
1192 We could eventually do better than this since it can be reused while
1193 generating the same RTL_EXPR, but this is complex and probably not
1194 worthwhile. */
1195
1196 void
1197 free_temp_slots (void)
1198 {
1199 struct temp_slot *p;
1200
1201 for (p = temp_slots; p; p = p->next)
1202 if (p->in_use && p->level == temp_slot_level && ! p->keep
1203 && p->rtl_expr == 0)
1204 p->in_use = 0;
1205
1206 combine_temp_slots ();
1207 }
1208
1209 /* Free all temporary slots used in T, an RTL_EXPR node. */
1210
1211 void
1212 free_temps_for_rtl_expr (tree t)
1213 {
1214 struct temp_slot *p;
1215
1216 for (p = temp_slots; p; p = p->next)
1217 if (p->rtl_expr == t)
1218 {
1219 /* If this slot is below the current TEMP_SLOT_LEVEL, then it
1220 needs to be preserved. This can happen if a temporary in
1221 the RTL_EXPR was addressed; preserve_temp_slots will move
1222 the temporary into a higher level. */
1223 if (temp_slot_level <= p->level)
1224 p->in_use = 0;
1225 else
1226 p->rtl_expr = NULL_TREE;
1227 }
1228
1229 combine_temp_slots ();
1230 }
1231
1232 /* Mark all temporaries ever allocated in this function as not suitable
1233 for reuse until the current level is exited. */
1234
1235 void
1236 mark_all_temps_used (void)
1237 {
1238 struct temp_slot *p;
1239
1240 for (p = temp_slots; p; p = p->next)
1241 {
1242 p->in_use = p->keep = 1;
1243 p->level = MIN (p->level, temp_slot_level);
1244 }
1245 }
1246
1247 /* Push deeper into the nesting level for stack temporaries. */
1248
1249 void
1250 push_temp_slots (void)
1251 {
1252 temp_slot_level++;
1253 }
1254
1255 /* Pop a temporary nesting level. All slots in use in the current level
1256 are freed. */
1257
1258 void
1259 pop_temp_slots (void)
1260 {
1261 struct temp_slot *p;
1262
1263 for (p = temp_slots; p; p = p->next)
1264 if (p->in_use && p->level == temp_slot_level && p->rtl_expr == 0)
1265 p->in_use = 0;
1266
1267 combine_temp_slots ();
1268
1269 temp_slot_level--;
1270 }
1271
1272 /* Initialize temporary slots. */
1273
1274 void
1275 init_temp_slots (void)
1276 {
1277 /* We have not allocated any temporaries yet. */
1278 temp_slots = 0;
1279 temp_slot_level = 0;
1280 var_temp_slot_level = 0;
1281 target_temp_slot_level = 0;
1282 }
1283 \f
1284 /* Retroactively move an auto variable from a register to a stack
1285 slot. This is done when an address-reference to the variable is
1286 seen. If RESCAN is true, all previously emitted instructions are
1287 examined and modified to handle the fact that DECL is now
1288 addressable. */
1289
1290 void
1291 put_var_into_stack (tree decl, int rescan)
1292 {
1293 rtx orig_reg, reg;
1294 enum machine_mode promoted_mode, decl_mode;
1295 struct function *function = 0;
1296 tree context;
1297 bool can_use_addressof_p;
1298 bool volatile_p = TREE_CODE (decl) != SAVE_EXPR && TREE_THIS_VOLATILE (decl);
1299 bool used_p = (TREE_USED (decl)
1300 || (TREE_CODE (decl) != SAVE_EXPR && DECL_INITIAL (decl) != 0));
1301
1302 context = decl_function_context (decl);
1303
1304 /* Get the current rtl used for this object and its original mode. */
1305 orig_reg = reg = (TREE_CODE (decl) == SAVE_EXPR
1306 ? SAVE_EXPR_RTL (decl)
1307 : DECL_RTL_IF_SET (decl));
1308
1309 /* No need to do anything if decl has no rtx yet
1310 since in that case caller is setting TREE_ADDRESSABLE
1311 and a stack slot will be assigned when the rtl is made. */
1312 if (reg == 0)
1313 return;
1314
1315 /* Get the declared mode for this object. */
1316 decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1317 : DECL_MODE (decl));
1318 /* Get the mode it's actually stored in. */
1319 promoted_mode = GET_MODE (reg);
1320
1321 /* If this variable comes from an outer function, find that
1322 function's saved context. Don't use find_function_data here,
1323 because it might not be in any active function.
1324 FIXME: Is that really supposed to happen?
1325 It does in ObjC at least. */
1326 if (context != current_function_decl && context != inline_function_decl)
1327 for (function = outer_function_chain; function; function = function->outer)
1328 if (function->decl == context)
1329 break;
1330
1331 /* If this is a variable-sized object or a structure passed by invisible
1332 reference, with a pseudo to address it, put that pseudo into the stack
1333 if the var is non-local. */
1334 if (TREE_CODE (decl) != SAVE_EXPR && DECL_NONLOCAL (decl)
1335 && GET_CODE (reg) == MEM
1336 && GET_CODE (XEXP (reg, 0)) == REG
1337 && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
1338 {
1339 orig_reg = reg = XEXP (reg, 0);
1340 decl_mode = promoted_mode = GET_MODE (reg);
1341 }
1342
1343 /* If this variable lives in the current function and we don't need to put it
1344 in the stack for the sake of setjmp or the non-locality, try to keep it in
1345 a register until we know we actually need the address. */
1346 can_use_addressof_p
1347 = (function == 0
1348 && ! (TREE_CODE (decl) != SAVE_EXPR && DECL_NONLOCAL (decl))
1349 && optimize > 0
1350 /* FIXME make it work for promoted modes too */
1351 && decl_mode == promoted_mode
1352 #ifdef NON_SAVING_SETJMP
1353 && ! (NON_SAVING_SETJMP && current_function_calls_setjmp)
1354 #endif
1355 );
1356
1357 /* If we can't use ADDRESSOF, make sure we see through one we already
1358 generated. */
1359 if (! can_use_addressof_p
1360 && GET_CODE (reg) == MEM
1361 && GET_CODE (XEXP (reg, 0)) == ADDRESSOF)
1362 reg = XEXP (XEXP (reg, 0), 0);
1363
1364 /* Now we should have a value that resides in one or more pseudo regs. */
1365
1366 if (GET_CODE (reg) == REG)
1367 {
1368 if (can_use_addressof_p)
1369 gen_mem_addressof (reg, decl, rescan);
1370 else
1371 put_reg_into_stack (function, reg, TREE_TYPE (decl), decl_mode,
1372 0, volatile_p, used_p, false, 0);
1373
1374 /* If this was previously a MEM but we've removed the ADDRESSOF,
1375 set this address into that MEM so we always use the same
1376 rtx for this variable. */
1377 if (orig_reg != reg && GET_CODE (orig_reg) == MEM)
1378 XEXP (orig_reg, 0) = XEXP (reg, 0);
1379 }
1380 else if (GET_CODE (reg) == CONCAT)
1381 {
1382 /* A CONCAT contains two pseudos; put them both in the stack.
1383 We do it so they end up consecutive.
1384 We fixup references to the parts only after we fixup references
1385 to the whole CONCAT, lest we do double fixups for the latter
1386 references. */
1387 enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
1388 tree part_type = lang_hooks.types.type_for_mode (part_mode, 0);
1389 rtx lopart = XEXP (reg, 0);
1390 rtx hipart = XEXP (reg, 1);
1391 #ifdef FRAME_GROWS_DOWNWARD
1392 /* Since part 0 should have a lower address, do it second. */
1393 put_reg_into_stack (function, hipart, part_type, part_mode,
1394 0, volatile_p, false, false, 0);
1395 put_reg_into_stack (function, lopart, part_type, part_mode,
1396 0, volatile_p, false, true, 0);
1397 #else
1398 put_reg_into_stack (function, lopart, part_type, part_mode,
1399 0, volatile_p, false, false, 0);
1400 put_reg_into_stack (function, hipart, part_type, part_mode,
1401 0, volatile_p, false, true, 0);
1402 #endif
1403
1404 /* Change the CONCAT into a combined MEM for both parts. */
1405 PUT_CODE (reg, MEM);
1406 MEM_ATTRS (reg) = 0;
1407
1408 /* set_mem_attributes uses DECL_RTL to avoid re-generating of
1409 already computed alias sets. Here we want to re-generate. */
1410 if (DECL_P (decl))
1411 SET_DECL_RTL (decl, NULL);
1412 set_mem_attributes (reg, decl, 1);
1413 if (DECL_P (decl))
1414 SET_DECL_RTL (decl, reg);
1415
1416 /* The two parts are in memory order already.
1417 Use the lower parts address as ours. */
1418 XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1419 /* Prevent sharing of rtl that might lose. */
1420 if (GET_CODE (XEXP (reg, 0)) == PLUS)
1421 XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
1422 if (used_p && rescan)
1423 {
1424 schedule_fixup_var_refs (function, reg, TREE_TYPE (decl),
1425 promoted_mode, 0);
1426 schedule_fixup_var_refs (function, lopart, part_type, part_mode, 0);
1427 schedule_fixup_var_refs (function, hipart, part_type, part_mode, 0);
1428 }
1429 }
1430 else
1431 return;
1432 }
1433
1434 /* Subroutine of put_var_into_stack. This puts a single pseudo reg REG
1435 into the stack frame of FUNCTION (0 means the current function).
1436 TYPE is the user-level data type of the value hold in the register.
1437 DECL_MODE is the machine mode of the user-level data type.
1438 ORIGINAL_REGNO must be set if the real regno is not visible in REG.
1439 VOLATILE_P is true if this is for a "volatile" decl.
1440 USED_P is true if this reg might have already been used in an insn.
1441 CONSECUTIVE_P is true if the stack slot assigned to reg must be
1442 consecutive with the previous stack slot. */
1443
1444 static void
1445 put_reg_into_stack (struct function *function, rtx reg, tree type,
1446 enum machine_mode decl_mode, unsigned int original_regno,
1447 bool volatile_p, bool used_p, bool consecutive_p,
1448 htab_t ht)
1449 {
1450 struct function *func = function ? function : cfun;
1451 enum machine_mode mode = GET_MODE (reg);
1452 unsigned int regno = original_regno;
1453 rtx new = 0;
1454
1455 if (regno == 0)
1456 regno = REGNO (reg);
1457
1458 if (regno < func->x_max_parm_reg)
1459 {
1460 if (!func->x_parm_reg_stack_loc)
1461 abort ();
1462 new = func->x_parm_reg_stack_loc[regno];
1463 }
1464
1465 if (new == 0)
1466 new = assign_stack_local_1 (decl_mode, GET_MODE_SIZE (decl_mode),
1467 consecutive_p ? -2 : 0, func);
1468
1469 PUT_CODE (reg, MEM);
1470 PUT_MODE (reg, decl_mode);
1471 XEXP (reg, 0) = XEXP (new, 0);
1472 MEM_ATTRS (reg) = 0;
1473 /* `volatil' bit means one thing for MEMs, another entirely for REGs. */
1474 MEM_VOLATILE_P (reg) = volatile_p;
1475
1476 /* If this is a memory ref that contains aggregate components,
1477 mark it as such for cse and loop optimize. If we are reusing a
1478 previously generated stack slot, then we need to copy the bit in
1479 case it was set for other reasons. For instance, it is set for
1480 __builtin_va_alist. */
1481 if (type)
1482 {
1483 MEM_SET_IN_STRUCT_P (reg,
1484 AGGREGATE_TYPE_P (type) || MEM_IN_STRUCT_P (new));
1485 set_mem_alias_set (reg, get_alias_set (type));
1486 }
1487
1488 if (used_p)
1489 schedule_fixup_var_refs (function, reg, type, mode, ht);
1490 }
1491
1492 /* Make sure that all refs to the variable, previously made
1493 when it was a register, are fixed up to be valid again.
1494 See function above for meaning of arguments. */
1495
1496 static void
1497 schedule_fixup_var_refs (struct function *function, rtx reg, tree type,
1498 enum machine_mode promoted_mode, htab_t ht)
1499 {
1500 int unsigned_p = type ? TYPE_UNSIGNED (type) : 0;
1501
1502 if (function != 0)
1503 {
1504 struct var_refs_queue *temp;
1505
1506 temp = ggc_alloc (sizeof (struct var_refs_queue));
1507 temp->modified = reg;
1508 temp->promoted_mode = promoted_mode;
1509 temp->unsignedp = unsigned_p;
1510 temp->next = function->fixup_var_refs_queue;
1511 function->fixup_var_refs_queue = temp;
1512 }
1513 else
1514 /* Variable is local; fix it up now. */
1515 fixup_var_refs (reg, promoted_mode, unsigned_p, reg, ht);
1516 }
1517 \f
1518 static void
1519 fixup_var_refs (rtx var, enum machine_mode promoted_mode, int unsignedp,
1520 rtx may_share, htab_t ht)
1521 {
1522 tree pending;
1523 rtx first_insn = get_insns ();
1524 struct sequence_stack *stack = seq_stack;
1525 tree rtl_exps = rtl_expr_chain;
1526 int save_volatile_ok = volatile_ok;
1527
1528 /* If there's a hash table, it must record all uses of VAR. */
1529 if (ht)
1530 {
1531 if (stack != 0)
1532 abort ();
1533 fixup_var_refs_insns_with_hash (ht, var, promoted_mode, unsignedp,
1534 may_share);
1535 return;
1536 }
1537
1538 /* Volatile is valid in MEMs because all we're doing in changing the
1539 address inside. */
1540 volatile_ok = 1;
1541 fixup_var_refs_insns (first_insn, var, promoted_mode, unsignedp,
1542 stack == 0, may_share);
1543
1544 /* Scan all pending sequences too. */
1545 for (; stack; stack = stack->next)
1546 {
1547 push_to_full_sequence (stack->first, stack->last);
1548 fixup_var_refs_insns (stack->first, var, promoted_mode, unsignedp,
1549 stack->next != 0, may_share);
1550 /* Update remembered end of sequence
1551 in case we added an insn at the end. */
1552 stack->last = get_last_insn ();
1553 end_sequence ();
1554 }
1555
1556 /* Scan all waiting RTL_EXPRs too. */
1557 for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1558 {
1559 rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1560 if (seq != const0_rtx && seq != 0)
1561 {
1562 push_to_sequence (seq);
1563 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0,
1564 may_share);
1565 end_sequence ();
1566 }
1567 }
1568
1569 volatile_ok = save_volatile_ok;
1570 }
1571 \f
1572 /* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
1573 some part of an insn. Return a struct fixup_replacement whose OLD
1574 value is equal to X. Allocate a new structure if no such entry exists. */
1575
1576 static struct fixup_replacement *
1577 find_fixup_replacement (struct fixup_replacement **replacements, rtx x)
1578 {
1579 struct fixup_replacement *p;
1580
1581 /* See if we have already replaced this. */
1582 for (p = *replacements; p != 0 && ! rtx_equal_p (p->old, x); p = p->next)
1583 ;
1584
1585 if (p == 0)
1586 {
1587 p = xmalloc (sizeof (struct fixup_replacement));
1588 p->old = x;
1589 p->new = 0;
1590 p->next = *replacements;
1591 *replacements = p;
1592 }
1593
1594 return p;
1595 }
1596
1597 /* Scan the insn-chain starting with INSN for refs to VAR and fix them
1598 up. TOPLEVEL is nonzero if this chain is the main chain of insns
1599 for the current function. MAY_SHARE is either a MEM that is not
1600 to be unshared or a list of them. */
1601
1602 static void
1603 fixup_var_refs_insns (rtx insn, rtx var, enum machine_mode promoted_mode,
1604 int unsignedp, int toplevel, rtx may_share)
1605 {
1606 while (insn)
1607 {
1608 /* fixup_var_refs_insn might modify insn, so save its next
1609 pointer now. */
1610 rtx next = NEXT_INSN (insn);
1611
1612 /* CALL_PLACEHOLDERs are special; we have to switch into each of
1613 the three sequences they (potentially) contain, and process
1614 them recursively. The CALL_INSN itself is not interesting. */
1615
1616 if (GET_CODE (insn) == CALL_INSN
1617 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1618 {
1619 int i;
1620
1621 /* Look at the Normal call, sibling call and tail recursion
1622 sequences attached to the CALL_PLACEHOLDER. */
1623 for (i = 0; i < 3; i++)
1624 {
1625 rtx seq = XEXP (PATTERN (insn), i);
1626 if (seq)
1627 {
1628 push_to_sequence (seq);
1629 fixup_var_refs_insns (seq, var, promoted_mode, unsignedp, 0,
1630 may_share);
1631 XEXP (PATTERN (insn), i) = get_insns ();
1632 end_sequence ();
1633 }
1634 }
1635 }
1636
1637 else if (INSN_P (insn))
1638 fixup_var_refs_insn (insn, var, promoted_mode, unsignedp, toplevel,
1639 may_share);
1640
1641 insn = next;
1642 }
1643 }
1644
1645 /* Look up the insns which reference VAR in HT and fix them up. Other
1646 arguments are the same as fixup_var_refs_insns.
1647
1648 N.B. No need for special processing of CALL_PLACEHOLDERs here,
1649 because the hash table will point straight to the interesting insn
1650 (inside the CALL_PLACEHOLDER). */
1651
1652 static void
1653 fixup_var_refs_insns_with_hash (htab_t ht, rtx var, enum machine_mode promoted_mode,
1654 int unsignedp, rtx may_share)
1655 {
1656 struct insns_for_mem_entry tmp;
1657 struct insns_for_mem_entry *ime;
1658 rtx insn_list;
1659
1660 tmp.key = var;
1661 ime = htab_find (ht, &tmp);
1662 for (insn_list = ime->insns; insn_list != 0; insn_list = XEXP (insn_list, 1))
1663 if (INSN_P (XEXP (insn_list, 0)))
1664 fixup_var_refs_insn (XEXP (insn_list, 0), var, promoted_mode,
1665 unsignedp, 1, may_share);
1666 }
1667
1668
1669 /* Per-insn processing by fixup_var_refs_insns(_with_hash). INSN is
1670 the insn under examination, VAR is the variable to fix up
1671 references to, PROMOTED_MODE and UNSIGNEDP describe VAR, and
1672 TOPLEVEL is nonzero if this is the main insn chain for this
1673 function. */
1674
1675 static void
1676 fixup_var_refs_insn (rtx insn, rtx var, enum machine_mode promoted_mode,
1677 int unsignedp, int toplevel, rtx no_share)
1678 {
1679 rtx call_dest = 0;
1680 rtx set, prev, prev_set;
1681 rtx note;
1682
1683 /* Remember the notes in case we delete the insn. */
1684 note = REG_NOTES (insn);
1685
1686 /* If this is a CLOBBER of VAR, delete it.
1687
1688 If it has a REG_LIBCALL note, delete the REG_LIBCALL
1689 and REG_RETVAL notes too. */
1690 if (GET_CODE (PATTERN (insn)) == CLOBBER
1691 && (XEXP (PATTERN (insn), 0) == var
1692 || (GET_CODE (XEXP (PATTERN (insn), 0)) == CONCAT
1693 && (XEXP (XEXP (PATTERN (insn), 0), 0) == var
1694 || XEXP (XEXP (PATTERN (insn), 0), 1) == var))))
1695 {
1696 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1697 /* The REG_LIBCALL note will go away since we are going to
1698 turn INSN into a NOTE, so just delete the
1699 corresponding REG_RETVAL note. */
1700 remove_note (XEXP (note, 0),
1701 find_reg_note (XEXP (note, 0), REG_RETVAL,
1702 NULL_RTX));
1703
1704 delete_insn (insn);
1705 }
1706
1707 /* The insn to load VAR from a home in the arglist
1708 is now a no-op. When we see it, just delete it.
1709 Similarly if this is storing VAR from a register from which
1710 it was loaded in the previous insn. This will occur
1711 when an ADDRESSOF was made for an arglist slot. */
1712 else if (toplevel
1713 && (set = single_set (insn)) != 0
1714 && SET_DEST (set) == var
1715 /* If this represents the result of an insn group,
1716 don't delete the insn. */
1717 && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
1718 && (rtx_equal_p (SET_SRC (set), var)
1719 || (GET_CODE (SET_SRC (set)) == REG
1720 && (prev = prev_nonnote_insn (insn)) != 0
1721 && (prev_set = single_set (prev)) != 0
1722 && SET_DEST (prev_set) == SET_SRC (set)
1723 && rtx_equal_p (SET_SRC (prev_set), var))))
1724 {
1725 delete_insn (insn);
1726 }
1727 else
1728 {
1729 struct fixup_replacement *replacements = 0;
1730 rtx next_insn = NEXT_INSN (insn);
1731
1732 if (SMALL_REGISTER_CLASSES)
1733 {
1734 /* If the insn that copies the results of a CALL_INSN
1735 into a pseudo now references VAR, we have to use an
1736 intermediate pseudo since we want the life of the
1737 return value register to be only a single insn.
1738
1739 If we don't use an intermediate pseudo, such things as
1740 address computations to make the address of VAR valid
1741 if it is not can be placed between the CALL_INSN and INSN.
1742
1743 To make sure this doesn't happen, we record the destination
1744 of the CALL_INSN and see if the next insn uses both that
1745 and VAR. */
1746
1747 if (call_dest != 0 && GET_CODE (insn) == INSN
1748 && reg_mentioned_p (var, PATTERN (insn))
1749 && reg_mentioned_p (call_dest, PATTERN (insn)))
1750 {
1751 rtx temp = gen_reg_rtx (GET_MODE (call_dest));
1752
1753 emit_insn_before (gen_move_insn (temp, call_dest), insn);
1754
1755 PATTERN (insn) = replace_rtx (PATTERN (insn),
1756 call_dest, temp);
1757 }
1758
1759 if (GET_CODE (insn) == CALL_INSN
1760 && GET_CODE (PATTERN (insn)) == SET)
1761 call_dest = SET_DEST (PATTERN (insn));
1762 else if (GET_CODE (insn) == CALL_INSN
1763 && GET_CODE (PATTERN (insn)) == PARALLEL
1764 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1765 call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1766 else
1767 call_dest = 0;
1768 }
1769
1770 /* See if we have to do anything to INSN now that VAR is in
1771 memory. If it needs to be loaded into a pseudo, use a single
1772 pseudo for the entire insn in case there is a MATCH_DUP
1773 between two operands. We pass a pointer to the head of
1774 a list of struct fixup_replacements. If fixup_var_refs_1
1775 needs to allocate pseudos or replacement MEMs (for SUBREGs),
1776 it will record them in this list.
1777
1778 If it allocated a pseudo for any replacement, we copy into
1779 it here. */
1780
1781 fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1782 &replacements, no_share);
1783
1784 /* If this is last_parm_insn, and any instructions were output
1785 after it to fix it up, then we must set last_parm_insn to
1786 the last such instruction emitted. */
1787 if (insn == last_parm_insn)
1788 last_parm_insn = PREV_INSN (next_insn);
1789
1790 while (replacements)
1791 {
1792 struct fixup_replacement *next;
1793
1794 if (GET_CODE (replacements->new) == REG)
1795 {
1796 rtx insert_before;
1797 rtx seq;
1798
1799 /* OLD might be a (subreg (mem)). */
1800 if (GET_CODE (replacements->old) == SUBREG)
1801 replacements->old
1802 = fixup_memory_subreg (replacements->old, insn,
1803 promoted_mode, 0);
1804 else
1805 replacements->old
1806 = fixup_stack_1 (replacements->old, insn);
1807
1808 insert_before = insn;
1809
1810 /* If we are changing the mode, do a conversion.
1811 This might be wasteful, but combine.c will
1812 eliminate much of the waste. */
1813
1814 if (GET_MODE (replacements->new)
1815 != GET_MODE (replacements->old))
1816 {
1817 start_sequence ();
1818 convert_move (replacements->new,
1819 replacements->old, unsignedp);
1820 seq = get_insns ();
1821 end_sequence ();
1822 }
1823 else
1824 seq = gen_move_insn (replacements->new,
1825 replacements->old);
1826
1827 emit_insn_before (seq, insert_before);
1828 }
1829
1830 next = replacements->next;
1831 free (replacements);
1832 replacements = next;
1833 }
1834 }
1835
1836 /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1837 But don't touch other insns referred to by reg-notes;
1838 we will get them elsewhere. */
1839 while (note)
1840 {
1841 if (GET_CODE (note) != INSN_LIST)
1842 XEXP (note, 0)
1843 = walk_fixup_memory_subreg (XEXP (note, 0), insn,
1844 promoted_mode, 1);
1845 note = XEXP (note, 1);
1846 }
1847 }
1848 \f
1849 /* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
1850 See if the rtx expression at *LOC in INSN needs to be changed.
1851
1852 REPLACEMENTS is a pointer to a list head that starts out zero, but may
1853 contain a list of original rtx's and replacements. If we find that we need
1854 to modify this insn by replacing a memory reference with a pseudo or by
1855 making a new MEM to implement a SUBREG, we consult that list to see if
1856 we have already chosen a replacement. If none has already been allocated,
1857 we allocate it and update the list. fixup_var_refs_insn will copy VAR
1858 or the SUBREG, as appropriate, to the pseudo. */
1859
1860 static void
1861 fixup_var_refs_1 (rtx var, enum machine_mode promoted_mode, rtx *loc, rtx insn,
1862 struct fixup_replacement **replacements, rtx no_share)
1863 {
1864 int i;
1865 rtx x = *loc;
1866 RTX_CODE code = GET_CODE (x);
1867 const char *fmt;
1868 rtx tem, tem1;
1869 struct fixup_replacement *replacement;
1870
1871 switch (code)
1872 {
1873 case ADDRESSOF:
1874 if (XEXP (x, 0) == var)
1875 {
1876 /* Prevent sharing of rtl that might lose. */
1877 rtx sub = copy_rtx (XEXP (var, 0));
1878
1879 if (! validate_change (insn, loc, sub, 0))
1880 {
1881 rtx y = gen_reg_rtx (GET_MODE (sub));
1882 rtx seq, new_insn;
1883
1884 /* We should be able to replace with a register or all is lost.
1885 Note that we can't use validate_change to verify this, since
1886 we're not caring for replacing all dups simultaneously. */
1887 if (! validate_replace_rtx (*loc, y, insn))
1888 abort ();
1889
1890 /* Careful! First try to recognize a direct move of the
1891 value, mimicking how things are done in gen_reload wrt
1892 PLUS. Consider what happens when insn is a conditional
1893 move instruction and addsi3 clobbers flags. */
1894
1895 start_sequence ();
1896 new_insn = emit_insn (gen_rtx_SET (VOIDmode, y, sub));
1897 seq = get_insns ();
1898 end_sequence ();
1899
1900 if (recog_memoized (new_insn) < 0)
1901 {
1902 /* That failed. Fall back on force_operand and hope. */
1903
1904 start_sequence ();
1905 sub = force_operand (sub, y);
1906 if (sub != y)
1907 emit_insn (gen_move_insn (y, sub));
1908 seq = get_insns ();
1909 end_sequence ();
1910 }
1911
1912 #ifdef HAVE_cc0
1913 /* Don't separate setter from user. */
1914 if (PREV_INSN (insn) && sets_cc0_p (PREV_INSN (insn)))
1915 insn = PREV_INSN (insn);
1916 #endif
1917
1918 emit_insn_before (seq, insn);
1919 }
1920 }
1921 return;
1922
1923 case MEM:
1924 if (var == x)
1925 {
1926 /* If we already have a replacement, use it. Otherwise,
1927 try to fix up this address in case it is invalid. */
1928
1929 replacement = find_fixup_replacement (replacements, var);
1930 if (replacement->new)
1931 {
1932 *loc = replacement->new;
1933 return;
1934 }
1935
1936 *loc = replacement->new = x = fixup_stack_1 (x, insn);
1937
1938 /* Unless we are forcing memory to register or we changed the mode,
1939 we can leave things the way they are if the insn is valid. */
1940
1941 INSN_CODE (insn) = -1;
1942 if (! flag_force_mem && GET_MODE (x) == promoted_mode
1943 && recog_memoized (insn) >= 0)
1944 return;
1945
1946 *loc = replacement->new = gen_reg_rtx (promoted_mode);
1947 return;
1948 }
1949
1950 /* If X contains VAR, we need to unshare it here so that we update
1951 each occurrence separately. But all identical MEMs in one insn
1952 must be replaced with the same rtx because of the possibility of
1953 MATCH_DUPs. */
1954
1955 if (reg_mentioned_p (var, x))
1956 {
1957 replacement = find_fixup_replacement (replacements, x);
1958 if (replacement->new == 0)
1959 replacement->new = copy_most_rtx (x, no_share);
1960
1961 *loc = x = replacement->new;
1962 code = GET_CODE (x);
1963 }
1964 break;
1965
1966 case REG:
1967 case CC0:
1968 case PC:
1969 case CONST_INT:
1970 case CONST:
1971 case SYMBOL_REF:
1972 case LABEL_REF:
1973 case CONST_DOUBLE:
1974 case CONST_VECTOR:
1975 return;
1976
1977 case SIGN_EXTRACT:
1978 case ZERO_EXTRACT:
1979 /* Note that in some cases those types of expressions are altered
1980 by optimize_bit_field, and do not survive to get here. */
1981 if (XEXP (x, 0) == var
1982 || (GET_CODE (XEXP (x, 0)) == SUBREG
1983 && SUBREG_REG (XEXP (x, 0)) == var))
1984 {
1985 /* Get TEM as a valid MEM in the mode presently in the insn.
1986
1987 We don't worry about the possibility of MATCH_DUP here; it
1988 is highly unlikely and would be tricky to handle. */
1989
1990 tem = XEXP (x, 0);
1991 if (GET_CODE (tem) == SUBREG)
1992 {
1993 if (GET_MODE_BITSIZE (GET_MODE (tem))
1994 > GET_MODE_BITSIZE (GET_MODE (var)))
1995 {
1996 replacement = find_fixup_replacement (replacements, var);
1997 if (replacement->new == 0)
1998 replacement->new = gen_reg_rtx (GET_MODE (var));
1999 SUBREG_REG (tem) = replacement->new;
2000
2001 /* The following code works only if we have a MEM, so we
2002 need to handle the subreg here. We directly substitute
2003 it assuming that a subreg must be OK here. We already
2004 scheduled a replacement to copy the mem into the
2005 subreg. */
2006 XEXP (x, 0) = tem;
2007 return;
2008 }
2009 else
2010 tem = fixup_memory_subreg (tem, insn, promoted_mode, 0);
2011 }
2012 else
2013 tem = fixup_stack_1 (tem, insn);
2014
2015 /* Unless we want to load from memory, get TEM into the proper mode
2016 for an extract from memory. This can only be done if the
2017 extract is at a constant position and length. */
2018
2019 if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
2020 && GET_CODE (XEXP (x, 2)) == CONST_INT
2021 && ! mode_dependent_address_p (XEXP (tem, 0))
2022 && ! MEM_VOLATILE_P (tem))
2023 {
2024 enum machine_mode wanted_mode = VOIDmode;
2025 enum machine_mode is_mode = GET_MODE (tem);
2026 HOST_WIDE_INT pos = INTVAL (XEXP (x, 2));
2027
2028 if (GET_CODE (x) == ZERO_EXTRACT)
2029 {
2030 enum machine_mode new_mode
2031 = mode_for_extraction (EP_extzv, 1);
2032 if (new_mode != MAX_MACHINE_MODE)
2033 wanted_mode = new_mode;
2034 }
2035 else if (GET_CODE (x) == SIGN_EXTRACT)
2036 {
2037 enum machine_mode new_mode
2038 = mode_for_extraction (EP_extv, 1);
2039 if (new_mode != MAX_MACHINE_MODE)
2040 wanted_mode = new_mode;
2041 }
2042
2043 /* If we have a narrower mode, we can do something. */
2044 if (wanted_mode != VOIDmode
2045 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2046 {
2047 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2048 rtx old_pos = XEXP (x, 2);
2049 rtx newmem;
2050
2051 /* If the bytes and bits are counted differently, we
2052 must adjust the offset. */
2053 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2054 offset = (GET_MODE_SIZE (is_mode)
2055 - GET_MODE_SIZE (wanted_mode) - offset);
2056
2057 pos %= GET_MODE_BITSIZE (wanted_mode);
2058
2059 newmem = adjust_address_nv (tem, wanted_mode, offset);
2060
2061 /* Make the change and see if the insn remains valid. */
2062 INSN_CODE (insn) = -1;
2063 XEXP (x, 0) = newmem;
2064 XEXP (x, 2) = GEN_INT (pos);
2065
2066 if (recog_memoized (insn) >= 0)
2067 return;
2068
2069 /* Otherwise, restore old position. XEXP (x, 0) will be
2070 restored later. */
2071 XEXP (x, 2) = old_pos;
2072 }
2073 }
2074
2075 /* If we get here, the bitfield extract insn can't accept a memory
2076 reference. Copy the input into a register. */
2077
2078 tem1 = gen_reg_rtx (GET_MODE (tem));
2079 emit_insn_before (gen_move_insn (tem1, tem), insn);
2080 XEXP (x, 0) = tem1;
2081 return;
2082 }
2083 break;
2084
2085 case SUBREG:
2086 if (SUBREG_REG (x) == var)
2087 {
2088 /* If this is a special SUBREG made because VAR was promoted
2089 from a wider mode, replace it with VAR and call ourself
2090 recursively, this time saying that the object previously
2091 had its current mode (by virtue of the SUBREG). */
2092
2093 if (SUBREG_PROMOTED_VAR_P (x))
2094 {
2095 *loc = var;
2096 fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements,
2097 no_share);
2098 return;
2099 }
2100
2101 /* If this SUBREG makes VAR wider, it has become a paradoxical
2102 SUBREG with VAR in memory, but these aren't allowed at this
2103 stage of the compilation. So load VAR into a pseudo and take
2104 a SUBREG of that pseudo. */
2105 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
2106 {
2107 replacement = find_fixup_replacement (replacements, var);
2108 if (replacement->new == 0)
2109 replacement->new = gen_reg_rtx (promoted_mode);
2110 SUBREG_REG (x) = replacement->new;
2111 return;
2112 }
2113
2114 /* See if we have already found a replacement for this SUBREG.
2115 If so, use it. Otherwise, make a MEM and see if the insn
2116 is recognized. If not, or if we should force MEM into a register,
2117 make a pseudo for this SUBREG. */
2118 replacement = find_fixup_replacement (replacements, x);
2119 if (replacement->new)
2120 {
2121 enum machine_mode mode = GET_MODE (x);
2122 *loc = replacement->new;
2123
2124 /* Careful! We may have just replaced a SUBREG by a MEM, which
2125 means that the insn may have become invalid again. We can't
2126 in this case make a new replacement since we already have one
2127 and we must deal with MATCH_DUPs. */
2128 if (GET_CODE (replacement->new) == MEM)
2129 {
2130 INSN_CODE (insn) = -1;
2131 if (recog_memoized (insn) >= 0)
2132 return;
2133
2134 fixup_var_refs_1 (replacement->new, mode, &PATTERN (insn),
2135 insn, replacements, no_share);
2136 }
2137
2138 return;
2139 }
2140
2141 replacement->new = *loc = fixup_memory_subreg (x, insn,
2142 promoted_mode, 0);
2143
2144 INSN_CODE (insn) = -1;
2145 if (! flag_force_mem && recog_memoized (insn) >= 0)
2146 return;
2147
2148 *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
2149 return;
2150 }
2151 break;
2152
2153 case SET:
2154 /* First do special simplification of bit-field references. */
2155 if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
2156 || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2157 optimize_bit_field (x, insn, 0);
2158 if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
2159 || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
2160 optimize_bit_field (x, insn, 0);
2161
2162 /* For a paradoxical SUBREG inside a ZERO_EXTRACT, load the object
2163 into a register and then store it back out. */
2164 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2165 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG
2166 && SUBREG_REG (XEXP (SET_DEST (x), 0)) == var
2167 && (GET_MODE_SIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2168 > GET_MODE_SIZE (GET_MODE (var))))
2169 {
2170 replacement = find_fixup_replacement (replacements, var);
2171 if (replacement->new == 0)
2172 replacement->new = gen_reg_rtx (GET_MODE (var));
2173
2174 SUBREG_REG (XEXP (SET_DEST (x), 0)) = replacement->new;
2175 emit_insn_after (gen_move_insn (var, replacement->new), insn);
2176 }
2177
2178 /* If SET_DEST is now a paradoxical SUBREG, put the result of this
2179 insn into a pseudo and store the low part of the pseudo into VAR. */
2180 if (GET_CODE (SET_DEST (x)) == SUBREG
2181 && SUBREG_REG (SET_DEST (x)) == var
2182 && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2183 > GET_MODE_SIZE (GET_MODE (var))))
2184 {
2185 SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
2186 emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
2187 tem)),
2188 insn);
2189 break;
2190 }
2191
2192 {
2193 rtx dest = SET_DEST (x);
2194 rtx src = SET_SRC (x);
2195 rtx outerdest = dest;
2196
2197 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2198 || GET_CODE (dest) == SIGN_EXTRACT
2199 || GET_CODE (dest) == ZERO_EXTRACT)
2200 dest = XEXP (dest, 0);
2201
2202 if (GET_CODE (src) == SUBREG)
2203 src = SUBREG_REG (src);
2204
2205 /* If VAR does not appear at the top level of the SET
2206 just scan the lower levels of the tree. */
2207
2208 if (src != var && dest != var)
2209 break;
2210
2211 /* We will need to rerecognize this insn. */
2212 INSN_CODE (insn) = -1;
2213
2214 if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var
2215 && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
2216 {
2217 /* Since this case will return, ensure we fixup all the
2218 operands here. */
2219 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
2220 insn, replacements, no_share);
2221 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
2222 insn, replacements, no_share);
2223 fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
2224 insn, replacements, no_share);
2225
2226 tem = XEXP (outerdest, 0);
2227
2228 /* Clean up (SUBREG:SI (MEM:mode ...) 0)
2229 that may appear inside a ZERO_EXTRACT.
2230 This was legitimate when the MEM was a REG. */
2231 if (GET_CODE (tem) == SUBREG
2232 && SUBREG_REG (tem) == var)
2233 tem = fixup_memory_subreg (tem, insn, promoted_mode, 0);
2234 else
2235 tem = fixup_stack_1 (tem, insn);
2236
2237 if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
2238 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
2239 && ! mode_dependent_address_p (XEXP (tem, 0))
2240 && ! MEM_VOLATILE_P (tem))
2241 {
2242 enum machine_mode wanted_mode;
2243 enum machine_mode is_mode = GET_MODE (tem);
2244 HOST_WIDE_INT pos = INTVAL (XEXP (outerdest, 2));
2245
2246 wanted_mode = mode_for_extraction (EP_insv, 0);
2247
2248 /* If we have a narrower mode, we can do something. */
2249 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2250 {
2251 HOST_WIDE_INT offset = pos / BITS_PER_UNIT;
2252 rtx old_pos = XEXP (outerdest, 2);
2253 rtx newmem;
2254
2255 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2256 offset = (GET_MODE_SIZE (is_mode)
2257 - GET_MODE_SIZE (wanted_mode) - offset);
2258
2259 pos %= GET_MODE_BITSIZE (wanted_mode);
2260
2261 newmem = adjust_address_nv (tem, wanted_mode, offset);
2262
2263 /* Make the change and see if the insn remains valid. */
2264 INSN_CODE (insn) = -1;
2265 XEXP (outerdest, 0) = newmem;
2266 XEXP (outerdest, 2) = GEN_INT (pos);
2267
2268 if (recog_memoized (insn) >= 0)
2269 return;
2270
2271 /* Otherwise, restore old position. XEXP (x, 0) will be
2272 restored later. */
2273 XEXP (outerdest, 2) = old_pos;
2274 }
2275 }
2276
2277 /* If we get here, the bit-field store doesn't allow memory
2278 or isn't located at a constant position. Load the value into
2279 a register, do the store, and put it back into memory. */
2280
2281 tem1 = gen_reg_rtx (GET_MODE (tem));
2282 emit_insn_before (gen_move_insn (tem1, tem), insn);
2283 emit_insn_after (gen_move_insn (tem, tem1), insn);
2284 XEXP (outerdest, 0) = tem1;
2285 return;
2286 }
2287
2288 /* STRICT_LOW_PART is a no-op on memory references
2289 and it can cause combinations to be unrecognizable,
2290 so eliminate it. */
2291
2292 if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2293 SET_DEST (x) = XEXP (SET_DEST (x), 0);
2294
2295 /* A valid insn to copy VAR into or out of a register
2296 must be left alone, to avoid an infinite loop here.
2297 If the reference to VAR is by a subreg, fix that up,
2298 since SUBREG is not valid for a memref.
2299 Also fix up the address of the stack slot.
2300
2301 Note that we must not try to recognize the insn until
2302 after we know that we have valid addresses and no
2303 (subreg (mem ...) ...) constructs, since these interfere
2304 with determining the validity of the insn. */
2305
2306 if ((SET_SRC (x) == var
2307 || (GET_CODE (SET_SRC (x)) == SUBREG
2308 && SUBREG_REG (SET_SRC (x)) == var))
2309 && (GET_CODE (SET_DEST (x)) == REG
2310 || (GET_CODE (SET_DEST (x)) == SUBREG
2311 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG))
2312 && GET_MODE (var) == promoted_mode
2313 && x == single_set (insn))
2314 {
2315 rtx pat, last;
2316
2317 if (GET_CODE (SET_SRC (x)) == SUBREG
2318 && (GET_MODE_SIZE (GET_MODE (SET_SRC (x)))
2319 > GET_MODE_SIZE (GET_MODE (var))))
2320 {
2321 /* This (subreg VAR) is now a paradoxical subreg. We need
2322 to replace VAR instead of the subreg. */
2323 replacement = find_fixup_replacement (replacements, var);
2324 if (replacement->new == NULL_RTX)
2325 replacement->new = gen_reg_rtx (GET_MODE (var));
2326 SUBREG_REG (SET_SRC (x)) = replacement->new;
2327 }
2328 else
2329 {
2330 replacement = find_fixup_replacement (replacements, SET_SRC (x));
2331 if (replacement->new)
2332 SET_SRC (x) = replacement->new;
2333 else if (GET_CODE (SET_SRC (x)) == SUBREG)
2334 SET_SRC (x) = replacement->new
2335 = fixup_memory_subreg (SET_SRC (x), insn, promoted_mode,
2336 0);
2337 else
2338 SET_SRC (x) = replacement->new
2339 = fixup_stack_1 (SET_SRC (x), insn);
2340 }
2341
2342 if (recog_memoized (insn) >= 0)
2343 return;
2344
2345 /* INSN is not valid, but we know that we want to
2346 copy SET_SRC (x) to SET_DEST (x) in some way. So
2347 we generate the move and see whether it requires more
2348 than one insn. If it does, we emit those insns and
2349 delete INSN. Otherwise, we can just replace the pattern
2350 of INSN; we have already verified above that INSN has
2351 no other function that to do X. */
2352
2353 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2354 if (NEXT_INSN (pat) != NULL_RTX)
2355 {
2356 last = emit_insn_before (pat, insn);
2357
2358 /* INSN might have REG_RETVAL or other important notes, so
2359 we need to store the pattern of the last insn in the
2360 sequence into INSN similarly to the normal case. LAST
2361 should not have REG_NOTES, but we allow them if INSN has
2362 no REG_NOTES. */
2363 if (REG_NOTES (last) && REG_NOTES (insn))
2364 abort ();
2365 if (REG_NOTES (last))
2366 REG_NOTES (insn) = REG_NOTES (last);
2367 PATTERN (insn) = PATTERN (last);
2368
2369 delete_insn (last);
2370 }
2371 else
2372 PATTERN (insn) = PATTERN (pat);
2373
2374 return;
2375 }
2376
2377 if ((SET_DEST (x) == var
2378 || (GET_CODE (SET_DEST (x)) == SUBREG
2379 && SUBREG_REG (SET_DEST (x)) == var))
2380 && (GET_CODE (SET_SRC (x)) == REG
2381 || (GET_CODE (SET_SRC (x)) == SUBREG
2382 && GET_CODE (SUBREG_REG (SET_SRC (x))) == REG))
2383 && GET_MODE (var) == promoted_mode
2384 && x == single_set (insn))
2385 {
2386 rtx pat, last;
2387
2388 if (GET_CODE (SET_DEST (x)) == SUBREG)
2389 SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn,
2390 promoted_mode, 0);
2391 else
2392 SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
2393
2394 if (recog_memoized (insn) >= 0)
2395 return;
2396
2397 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2398 if (NEXT_INSN (pat) != NULL_RTX)
2399 {
2400 last = emit_insn_before (pat, insn);
2401
2402 /* INSN might have REG_RETVAL or other important notes, so
2403 we need to store the pattern of the last insn in the
2404 sequence into INSN similarly to the normal case. LAST
2405 should not have REG_NOTES, but we allow them if INSN has
2406 no REG_NOTES. */
2407 if (REG_NOTES (last) && REG_NOTES (insn))
2408 abort ();
2409 if (REG_NOTES (last))
2410 REG_NOTES (insn) = REG_NOTES (last);
2411 PATTERN (insn) = PATTERN (last);
2412
2413 delete_insn (last);
2414 }
2415 else
2416 PATTERN (insn) = PATTERN (pat);
2417
2418 return;
2419 }
2420
2421 /* Otherwise, storing into VAR must be handled specially
2422 by storing into a temporary and copying that into VAR
2423 with a new insn after this one. Note that this case
2424 will be used when storing into a promoted scalar since
2425 the insn will now have different modes on the input
2426 and output and hence will be invalid (except for the case
2427 of setting it to a constant, which does not need any
2428 change if it is valid). We generate extra code in that case,
2429 but combine.c will eliminate it. */
2430
2431 if (dest == var)
2432 {
2433 rtx temp;
2434 rtx fixeddest = SET_DEST (x);
2435 enum machine_mode temp_mode;
2436
2437 /* STRICT_LOW_PART can be discarded, around a MEM. */
2438 if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2439 fixeddest = XEXP (fixeddest, 0);
2440 /* Convert (SUBREG (MEM)) to a MEM in a changed mode. */
2441 if (GET_CODE (fixeddest) == SUBREG)
2442 {
2443 fixeddest = fixup_memory_subreg (fixeddest, insn,
2444 promoted_mode, 0);
2445 temp_mode = GET_MODE (fixeddest);
2446 }
2447 else
2448 {
2449 fixeddest = fixup_stack_1 (fixeddest, insn);
2450 temp_mode = promoted_mode;
2451 }
2452
2453 temp = gen_reg_rtx (temp_mode);
2454
2455 emit_insn_after (gen_move_insn (fixeddest,
2456 gen_lowpart (GET_MODE (fixeddest),
2457 temp)),
2458 insn);
2459
2460 SET_DEST (x) = temp;
2461 }
2462 }
2463
2464 default:
2465 break;
2466 }
2467
2468 /* Nothing special about this RTX; fix its operands. */
2469
2470 fmt = GET_RTX_FORMAT (code);
2471 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2472 {
2473 if (fmt[i] == 'e')
2474 fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements,
2475 no_share);
2476 else if (fmt[i] == 'E')
2477 {
2478 int j;
2479 for (j = 0; j < XVECLEN (x, i); j++)
2480 fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2481 insn, replacements, no_share);
2482 }
2483 }
2484 }
2485 \f
2486 /* Previously, X had the form (SUBREG:m1 (REG:PROMOTED_MODE ...)).
2487 The REG was placed on the stack, so X now has the form (SUBREG:m1
2488 (MEM:m2 ...)).
2489
2490 Return an rtx (MEM:m1 newaddr) which is equivalent. If any insns
2491 must be emitted to compute NEWADDR, put them before INSN.
2492
2493 UNCRITICAL nonzero means accept paradoxical subregs.
2494 This is used for subregs found inside REG_NOTES. */
2495
2496 static rtx
2497 fixup_memory_subreg (rtx x, rtx insn, enum machine_mode promoted_mode, int uncritical)
2498 {
2499 int offset;
2500 rtx mem = SUBREG_REG (x);
2501 rtx addr = XEXP (mem, 0);
2502 enum machine_mode mode = GET_MODE (x);
2503 rtx result, seq;
2504
2505 /* Paradoxical SUBREGs are usually invalid during RTL generation. */
2506 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (mem)) && ! uncritical)
2507 abort ();
2508
2509 offset = SUBREG_BYTE (x);
2510 if (BYTES_BIG_ENDIAN)
2511 /* If the PROMOTED_MODE is wider than the mode of the MEM, adjust
2512 the offset so that it points to the right location within the
2513 MEM. */
2514 offset -= (GET_MODE_SIZE (promoted_mode) - GET_MODE_SIZE (GET_MODE (mem)));
2515
2516 if (!flag_force_addr
2517 && memory_address_p (mode, plus_constant (addr, offset)))
2518 /* Shortcut if no insns need be emitted. */
2519 return adjust_address (mem, mode, offset);
2520
2521 start_sequence ();
2522 result = adjust_address (mem, mode, offset);
2523 seq = get_insns ();
2524 end_sequence ();
2525
2526 emit_insn_before (seq, insn);
2527 return result;
2528 }
2529
2530 /* Do fixup_memory_subreg on all (SUBREG (MEM ...) ...) contained in X.
2531 Replace subexpressions of X in place.
2532 If X itself is a (SUBREG (MEM ...) ...), return the replacement expression.
2533 Otherwise return X, with its contents possibly altered.
2534
2535 INSN, PROMOTED_MODE and UNCRITICAL are as for
2536 fixup_memory_subreg. */
2537
2538 static rtx
2539 walk_fixup_memory_subreg (rtx x, rtx insn, enum machine_mode promoted_mode,
2540 int uncritical)
2541 {
2542 enum rtx_code code;
2543 const char *fmt;
2544 int i;
2545
2546 if (x == 0)
2547 return 0;
2548
2549 code = GET_CODE (x);
2550
2551 if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
2552 return fixup_memory_subreg (x, insn, promoted_mode, uncritical);
2553
2554 /* Nothing special about this RTX; fix its operands. */
2555
2556 fmt = GET_RTX_FORMAT (code);
2557 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2558 {
2559 if (fmt[i] == 'e')
2560 XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn,
2561 promoted_mode, uncritical);
2562 else if (fmt[i] == 'E')
2563 {
2564 int j;
2565 for (j = 0; j < XVECLEN (x, i); j++)
2566 XVECEXP (x, i, j)
2567 = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn,
2568 promoted_mode, uncritical);
2569 }
2570 }
2571 return x;
2572 }
2573 \f
2574 /* For each memory ref within X, if it refers to a stack slot
2575 with an out of range displacement, put the address in a temp register
2576 (emitting new insns before INSN to load these registers)
2577 and alter the memory ref to use that register.
2578 Replace each such MEM rtx with a copy, to avoid clobberage. */
2579
2580 static rtx
2581 fixup_stack_1 (rtx x, rtx insn)
2582 {
2583 int i;
2584 RTX_CODE code = GET_CODE (x);
2585 const char *fmt;
2586
2587 if (code == MEM)
2588 {
2589 rtx ad = XEXP (x, 0);
2590 /* If we have address of a stack slot but it's not valid
2591 (displacement is too large), compute the sum in a register. */
2592 if (GET_CODE (ad) == PLUS
2593 && GET_CODE (XEXP (ad, 0)) == REG
2594 && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2595 && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
2596 || REGNO (XEXP (ad, 0)) == FRAME_POINTER_REGNUM
2597 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2598 || REGNO (XEXP (ad, 0)) == HARD_FRAME_POINTER_REGNUM
2599 #endif
2600 || REGNO (XEXP (ad, 0)) == STACK_POINTER_REGNUM
2601 || REGNO (XEXP (ad, 0)) == ARG_POINTER_REGNUM
2602 || XEXP (ad, 0) == current_function_internal_arg_pointer)
2603 && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2604 {
2605 rtx temp, seq;
2606 if (memory_address_p (GET_MODE (x), ad))
2607 return x;
2608
2609 start_sequence ();
2610 temp = copy_to_reg (ad);
2611 seq = get_insns ();
2612 end_sequence ();
2613 emit_insn_before (seq, insn);
2614 return replace_equiv_address (x, temp);
2615 }
2616 return x;
2617 }
2618
2619 fmt = GET_RTX_FORMAT (code);
2620 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2621 {
2622 if (fmt[i] == 'e')
2623 XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
2624 else if (fmt[i] == 'E')
2625 {
2626 int j;
2627 for (j = 0; j < XVECLEN (x, i); j++)
2628 XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2629 }
2630 }
2631 return x;
2632 }
2633 \f
2634 /* Optimization: a bit-field instruction whose field
2635 happens to be a byte or halfword in memory
2636 can be changed to a move instruction.
2637
2638 We call here when INSN is an insn to examine or store into a bit-field.
2639 BODY is the SET-rtx to be altered.
2640
2641 EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2642 (Currently this is called only from function.c, and EQUIV_MEM
2643 is always 0.) */
2644
2645 static void
2646 optimize_bit_field (rtx body, rtx insn, rtx *equiv_mem)
2647 {
2648 rtx bitfield;
2649 int destflag;
2650 rtx seq = 0;
2651 enum machine_mode mode;
2652
2653 if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2654 || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2655 bitfield = SET_DEST (body), destflag = 1;
2656 else
2657 bitfield = SET_SRC (body), destflag = 0;
2658
2659 /* First check that the field being stored has constant size and position
2660 and is in fact a byte or halfword suitably aligned. */
2661
2662 if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2663 && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2664 && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2665 != BLKmode)
2666 && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2667 {
2668 rtx memref = 0;
2669
2670 /* Now check that the containing word is memory, not a register,
2671 and that it is safe to change the machine mode. */
2672
2673 if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2674 memref = XEXP (bitfield, 0);
2675 else if (GET_CODE (XEXP (bitfield, 0)) == REG
2676 && equiv_mem != 0)
2677 memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2678 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2679 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2680 memref = SUBREG_REG (XEXP (bitfield, 0));
2681 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2682 && equiv_mem != 0
2683 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == REG)
2684 memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2685
2686 if (memref
2687 && ! mode_dependent_address_p (XEXP (memref, 0))
2688 && ! MEM_VOLATILE_P (memref))
2689 {
2690 /* Now adjust the address, first for any subreg'ing
2691 that we are now getting rid of,
2692 and then for which byte of the word is wanted. */
2693
2694 HOST_WIDE_INT offset = INTVAL (XEXP (bitfield, 2));
2695 rtx insns;
2696
2697 /* Adjust OFFSET to count bits from low-address byte. */
2698 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2699 offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2700 - offset - INTVAL (XEXP (bitfield, 1)));
2701
2702 /* Adjust OFFSET to count bytes from low-address byte. */
2703 offset /= BITS_PER_UNIT;
2704 if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2705 {
2706 offset += (SUBREG_BYTE (XEXP (bitfield, 0))
2707 / UNITS_PER_WORD) * UNITS_PER_WORD;
2708 if (BYTES_BIG_ENDIAN)
2709 offset -= (MIN (UNITS_PER_WORD,
2710 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2711 - MIN (UNITS_PER_WORD,
2712 GET_MODE_SIZE (GET_MODE (memref))));
2713 }
2714
2715 start_sequence ();
2716 memref = adjust_address (memref, mode, offset);
2717 insns = get_insns ();
2718 end_sequence ();
2719 emit_insn_before (insns, insn);
2720
2721 /* Store this memory reference where
2722 we found the bit field reference. */
2723
2724 if (destflag)
2725 {
2726 validate_change (insn, &SET_DEST (body), memref, 1);
2727 if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2728 {
2729 rtx src = SET_SRC (body);
2730 while (GET_CODE (src) == SUBREG
2731 && SUBREG_BYTE (src) == 0)
2732 src = SUBREG_REG (src);
2733 if (GET_MODE (src) != GET_MODE (memref))
2734 src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2735 validate_change (insn, &SET_SRC (body), src, 1);
2736 }
2737 else if (GET_MODE (SET_SRC (body)) != VOIDmode
2738 && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2739 /* This shouldn't happen because anything that didn't have
2740 one of these modes should have got converted explicitly
2741 and then referenced through a subreg.
2742 This is so because the original bit-field was
2743 handled by agg_mode and so its tree structure had
2744 the same mode that memref now has. */
2745 abort ();
2746 }
2747 else
2748 {
2749 rtx dest = SET_DEST (body);
2750
2751 while (GET_CODE (dest) == SUBREG
2752 && SUBREG_BYTE (dest) == 0
2753 && (GET_MODE_CLASS (GET_MODE (dest))
2754 == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest))))
2755 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
2756 <= UNITS_PER_WORD))
2757 dest = SUBREG_REG (dest);
2758
2759 validate_change (insn, &SET_DEST (body), dest, 1);
2760
2761 if (GET_MODE (dest) == GET_MODE (memref))
2762 validate_change (insn, &SET_SRC (body), memref, 1);
2763 else
2764 {
2765 /* Convert the mem ref to the destination mode. */
2766 rtx newreg = gen_reg_rtx (GET_MODE (dest));
2767
2768 start_sequence ();
2769 convert_move (newreg, memref,
2770 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2771 seq = get_insns ();
2772 end_sequence ();
2773
2774 validate_change (insn, &SET_SRC (body), newreg, 1);
2775 }
2776 }
2777
2778 /* See if we can convert this extraction or insertion into
2779 a simple move insn. We might not be able to do so if this
2780 was, for example, part of a PARALLEL.
2781
2782 If we succeed, write out any needed conversions. If we fail,
2783 it is hard to guess why we failed, so don't do anything
2784 special; just let the optimization be suppressed. */
2785
2786 if (apply_change_group () && seq)
2787 emit_insn_before (seq, insn);
2788 }
2789 }
2790 }
2791 \f
2792 /* These routines are responsible for converting virtual register references
2793 to the actual hard register references once RTL generation is complete.
2794
2795 The following four variables are used for communication between the
2796 routines. They contain the offsets of the virtual registers from their
2797 respective hard registers. */
2798
2799 static int in_arg_offset;
2800 static int var_offset;
2801 static int dynamic_offset;
2802 static int out_arg_offset;
2803 static int cfa_offset;
2804
2805 /* In most machines, the stack pointer register is equivalent to the bottom
2806 of the stack. */
2807
2808 #ifndef STACK_POINTER_OFFSET
2809 #define STACK_POINTER_OFFSET 0
2810 #endif
2811
2812 /* If not defined, pick an appropriate default for the offset of dynamically
2813 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2814 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
2815
2816 #ifndef STACK_DYNAMIC_OFFSET
2817
2818 /* The bottom of the stack points to the actual arguments. If
2819 REG_PARM_STACK_SPACE is defined, this includes the space for the register
2820 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
2821 stack space for register parameters is not pushed by the caller, but
2822 rather part of the fixed stack areas and hence not included in
2823 `current_function_outgoing_args_size'. Nevertheless, we must allow
2824 for it when allocating stack dynamic objects. */
2825
2826 #if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2827 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2828 ((ACCUMULATE_OUTGOING_ARGS \
2829 ? (current_function_outgoing_args_size + REG_PARM_STACK_SPACE (FNDECL)) : 0)\
2830 + (STACK_POINTER_OFFSET)) \
2831
2832 #else
2833 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2834 ((ACCUMULATE_OUTGOING_ARGS ? current_function_outgoing_args_size : 0) \
2835 + (STACK_POINTER_OFFSET))
2836 #endif
2837 #endif
2838
2839 /* On most machines, the CFA coincides with the first incoming parm. */
2840
2841 #ifndef ARG_POINTER_CFA_OFFSET
2842 #define ARG_POINTER_CFA_OFFSET(FNDECL) FIRST_PARM_OFFSET (FNDECL)
2843 #endif
2844
2845 /* Build up a (MEM (ADDRESSOF (REG))) rtx for a register REG that just
2846 had its address taken. DECL is the decl or SAVE_EXPR for the
2847 object stored in the register, for later use if we do need to force
2848 REG into the stack. REG is overwritten by the MEM like in
2849 put_reg_into_stack. RESCAN is true if previously emitted
2850 instructions must be rescanned and modified now that the REG has
2851 been transformed. */
2852
2853 rtx
2854 gen_mem_addressof (rtx reg, tree decl, int rescan)
2855 {
2856 rtx r = gen_rtx_ADDRESSOF (Pmode, gen_reg_rtx (GET_MODE (reg)),
2857 REGNO (reg), decl);
2858
2859 /* Calculate this before we start messing with decl's RTL. */
2860 HOST_WIDE_INT set = decl ? get_alias_set (decl) : 0;
2861
2862 /* If the original REG was a user-variable, then so is the REG whose
2863 address is being taken. Likewise for unchanging. */
2864 REG_USERVAR_P (XEXP (r, 0)) = REG_USERVAR_P (reg);
2865 RTX_UNCHANGING_P (XEXP (r, 0)) = RTX_UNCHANGING_P (reg);
2866
2867 PUT_CODE (reg, MEM);
2868 MEM_VOLATILE_P (reg) = 0;
2869 MEM_ATTRS (reg) = 0;
2870 XEXP (reg, 0) = r;
2871
2872 if (decl)
2873 {
2874 tree type = TREE_TYPE (decl);
2875 enum machine_mode decl_mode
2876 = (DECL_P (decl) ? DECL_MODE (decl) : TYPE_MODE (TREE_TYPE (decl)));
2877 rtx decl_rtl = (TREE_CODE (decl) == SAVE_EXPR ? SAVE_EXPR_RTL (decl)
2878 : DECL_RTL_IF_SET (decl));
2879
2880 PUT_MODE (reg, decl_mode);
2881
2882 /* Clear DECL_RTL momentarily so functions below will work
2883 properly, then set it again. */
2884 if (DECL_P (decl) && decl_rtl == reg)
2885 SET_DECL_RTL (decl, 0);
2886
2887 set_mem_attributes (reg, decl, 1);
2888 set_mem_alias_set (reg, set);
2889
2890 if (DECL_P (decl) && decl_rtl == reg)
2891 SET_DECL_RTL (decl, reg);
2892
2893 if (rescan
2894 && (TREE_USED (decl) || (DECL_P (decl) && DECL_INITIAL (decl) != 0)))
2895 fixup_var_refs (reg, GET_MODE (reg), TYPE_UNSIGNED (type), reg, 0);
2896 }
2897 else if (rescan)
2898 {
2899 /* This can only happen during reload. Clear the same flag bits as
2900 reload. */
2901 RTX_UNCHANGING_P (reg) = 0;
2902 MEM_IN_STRUCT_P (reg) = 0;
2903 MEM_SCALAR_P (reg) = 0;
2904
2905 fixup_var_refs (reg, GET_MODE (reg), 0, reg, 0);
2906 }
2907
2908 return reg;
2909 }
2910
2911 /* If DECL has an RTL that is an ADDRESSOF rtx, put it into the stack. */
2912
2913 void
2914 flush_addressof (tree decl)
2915 {
2916 if ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL)
2917 && DECL_RTL (decl) != 0
2918 && GET_CODE (DECL_RTL (decl)) == MEM
2919 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF
2920 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == REG)
2921 put_addressof_into_stack (XEXP (DECL_RTL (decl), 0), 0);
2922 }
2923
2924 /* Force the register pointed to by R, an ADDRESSOF rtx, into the stack. */
2925
2926 static void
2927 put_addressof_into_stack (rtx r, htab_t ht)
2928 {
2929 tree decl, type;
2930 bool volatile_p, used_p;
2931
2932 rtx reg = XEXP (r, 0);
2933
2934 if (GET_CODE (reg) != REG)
2935 abort ();
2936
2937 decl = ADDRESSOF_DECL (r);
2938 if (decl)
2939 {
2940 type = TREE_TYPE (decl);
2941 volatile_p = (TREE_CODE (decl) != SAVE_EXPR
2942 && TREE_THIS_VOLATILE (decl));
2943 used_p = (TREE_USED (decl)
2944 || (DECL_P (decl) && DECL_INITIAL (decl) != 0));
2945 }
2946 else
2947 {
2948 type = NULL_TREE;
2949 volatile_p = false;
2950 used_p = true;
2951 }
2952
2953 put_reg_into_stack (0, reg, type, GET_MODE (reg), ADDRESSOF_REGNO (r),
2954 volatile_p, used_p, false, ht);
2955 }
2956
2957 /* List of replacements made below in purge_addressof_1 when creating
2958 bitfield insertions. */
2959 static rtx purge_bitfield_addressof_replacements;
2960
2961 /* List of replacements made below in purge_addressof_1 for patterns
2962 (MEM (ADDRESSOF (REG ...))). The key of the list entry is the
2963 corresponding (ADDRESSOF (REG ...)) and value is a substitution for
2964 the all pattern. List PURGE_BITFIELD_ADDRESSOF_REPLACEMENTS is not
2965 enough in complex cases, e.g. when some field values can be
2966 extracted by usage MEM with narrower mode. */
2967 static rtx purge_addressof_replacements;
2968
2969 /* Helper function for purge_addressof. See if the rtx expression at *LOC
2970 in INSN needs to be changed. If FORCE, always put any ADDRESSOFs into
2971 the stack. If the function returns FALSE then the replacement could not
2972 be made. If MAY_POSTPONE is true and we would not put the addressof
2973 to stack, postpone processing of the insn. */
2974
2975 static bool
2976 purge_addressof_1 (rtx *loc, rtx insn, int force, int store, int may_postpone,
2977 htab_t ht)
2978 {
2979 rtx x;
2980 RTX_CODE code;
2981 int i, j;
2982 const char *fmt;
2983 bool result = true;
2984 bool libcall = false;
2985
2986 /* Re-start here to avoid recursion in common cases. */
2987 restart:
2988
2989 x = *loc;
2990 if (x == 0)
2991 return true;
2992
2993 /* Is this a libcall? */
2994 if (!insn)
2995 libcall = REG_NOTE_KIND (*loc) == REG_RETVAL;
2996
2997 code = GET_CODE (x);
2998
2999 /* If we don't return in any of the cases below, we will recurse inside
3000 the RTX, which will normally result in any ADDRESSOF being forced into
3001 memory. */
3002 if (code == SET)
3003 {
3004 result = purge_addressof_1 (&SET_DEST (x), insn, force, 1,
3005 may_postpone, ht);
3006 result &= purge_addressof_1 (&SET_SRC (x), insn, force, 0,
3007 may_postpone, ht);
3008 return result;
3009 }
3010 else if (code == ADDRESSOF)
3011 {
3012 rtx sub, insns;
3013
3014 if (GET_CODE (XEXP (x, 0)) != MEM)
3015 put_addressof_into_stack (x, ht);
3016
3017 /* We must create a copy of the rtx because it was created by
3018 overwriting a REG rtx which is always shared. */
3019 sub = copy_rtx (XEXP (XEXP (x, 0), 0));
3020 if (validate_change (insn, loc, sub, 0)
3021 || validate_replace_rtx (x, sub, insn))
3022 return true;
3023
3024 start_sequence ();
3025
3026 /* If SUB is a hard or virtual register, try it as a pseudo-register.
3027 Otherwise, perhaps SUB is an expression, so generate code to compute
3028 it. */
3029 if (GET_CODE (sub) == REG && REGNO (sub) <= LAST_VIRTUAL_REGISTER)
3030 sub = copy_to_reg (sub);
3031 else
3032 sub = force_operand (sub, NULL_RTX);
3033
3034 if (! validate_change (insn, loc, sub, 0)
3035 && ! validate_replace_rtx (x, sub, insn))
3036 abort ();
3037
3038 insns = get_insns ();
3039 end_sequence ();
3040 emit_insn_before (insns, insn);
3041 return true;
3042 }
3043
3044 else if (code == MEM && GET_CODE (XEXP (x, 0)) == ADDRESSOF && ! force)
3045 {
3046 rtx sub = XEXP (XEXP (x, 0), 0);
3047
3048 if (GET_CODE (sub) == MEM)
3049 sub = adjust_address_nv (sub, GET_MODE (x), 0);
3050 else if (GET_CODE (sub) == REG
3051 && (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode))
3052 ;
3053 else if (GET_CODE (sub) == REG && GET_MODE (x) != GET_MODE (sub))
3054 {
3055 int size_x, size_sub;
3056
3057 if (may_postpone)
3058 {
3059 /* Postpone for now, so that we do not emit bitfield arithmetics
3060 unless there is some benefit from it. */
3061 if (!postponed_insns || XEXP (postponed_insns, 0) != insn)
3062 postponed_insns = alloc_INSN_LIST (insn, postponed_insns);
3063 return true;
3064 }
3065
3066 if (!insn)
3067 {
3068 /* When processing REG_NOTES look at the list of
3069 replacements done on the insn to find the register that X
3070 was replaced by. */
3071 rtx tem;
3072
3073 for (tem = purge_bitfield_addressof_replacements;
3074 tem != NULL_RTX;
3075 tem = XEXP (XEXP (tem, 1), 1))
3076 if (rtx_equal_p (x, XEXP (tem, 0)))
3077 {
3078 *loc = XEXP (XEXP (tem, 1), 0);
3079 return true;
3080 }
3081
3082 /* See comment for purge_addressof_replacements. */
3083 for (tem = purge_addressof_replacements;
3084 tem != NULL_RTX;
3085 tem = XEXP (XEXP (tem, 1), 1))
3086 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3087 {
3088 rtx z = XEXP (XEXP (tem, 1), 0);
3089
3090 if (GET_MODE (x) == GET_MODE (z)
3091 || (GET_CODE (XEXP (XEXP (tem, 1), 0)) != REG
3092 && GET_CODE (XEXP (XEXP (tem, 1), 0)) != SUBREG))
3093 abort ();
3094
3095 /* It can happen that the note may speak of things
3096 in a wider (or just different) mode than the
3097 code did. This is especially true of
3098 REG_RETVAL. */
3099
3100 if (GET_CODE (z) == SUBREG && SUBREG_BYTE (z) == 0)
3101 z = SUBREG_REG (z);
3102
3103 if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3104 && (GET_MODE_SIZE (GET_MODE (x))
3105 > GET_MODE_SIZE (GET_MODE (z))))
3106 {
3107 /* This can occur as a result in invalid
3108 pointer casts, e.g. float f; ...
3109 *(long long int *)&f.
3110 ??? We could emit a warning here, but
3111 without a line number that wouldn't be
3112 very helpful. */
3113 z = gen_rtx_SUBREG (GET_MODE (x), z, 0);
3114 }
3115 else
3116 z = gen_lowpart (GET_MODE (x), z);
3117
3118 *loc = z;
3119 return true;
3120 }
3121
3122 /* When we are processing the REG_NOTES of the last instruction
3123 of a libcall, there will be typically no replacements
3124 for that insn; the replacements happened before, piecemeal
3125 fashion. OTOH we are not interested in the details of
3126 this for the REG_EQUAL note, we want to know the big picture,
3127 which can be succinctly described with a simple SUBREG.
3128 Note that removing the REG_EQUAL note is not an option
3129 on the last insn of a libcall, so we must do a replacement. */
3130
3131 /* In compile/990107-1.c:7 compiled at -O1 -m1 for sh-elf,
3132 we got
3133 (mem:DI (addressof:SI (reg/v:DF 160) 159 0x401c8510)
3134 [0 S8 A32]), which can be expressed with a simple
3135 same-size subreg */
3136 if ((GET_MODE_SIZE (GET_MODE (x))
3137 <= GET_MODE_SIZE (GET_MODE (sub)))
3138 /* Again, invalid pointer casts (as in
3139 compile/990203-1.c) can require paradoxical
3140 subregs. */
3141 || (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
3142 && (GET_MODE_SIZE (GET_MODE (x))
3143 > GET_MODE_SIZE (GET_MODE (sub)))
3144 && libcall))
3145 {
3146 *loc = gen_rtx_SUBREG (GET_MODE (x), sub, 0);
3147 return true;
3148 }
3149 /* ??? Are there other cases we should handle? */
3150
3151 /* Sometimes we may not be able to find the replacement. For
3152 example when the original insn was a MEM in a wider mode,
3153 and the note is part of a sign extension of a narrowed
3154 version of that MEM. Gcc testcase compile/990829-1.c can
3155 generate an example of this situation. Rather than complain
3156 we return false, which will prompt our caller to remove the
3157 offending note. */
3158 return false;
3159 }
3160
3161 size_x = GET_MODE_BITSIZE (GET_MODE (x));
3162 size_sub = GET_MODE_BITSIZE (GET_MODE (sub));
3163
3164 /* Do not frob unchanging MEMs. If a later reference forces the
3165 pseudo to the stack, we can wind up with multiple writes to
3166 an unchanging memory, which is invalid. */
3167 if (RTX_UNCHANGING_P (x) && size_x != size_sub)
3168 ;
3169
3170 /* Don't even consider working with paradoxical subregs,
3171 or the moral equivalent seen here. */
3172 else if (size_x <= size_sub
3173 && int_mode_for_mode (GET_MODE (sub)) != BLKmode)
3174 {
3175 /* Do a bitfield insertion to mirror what would happen
3176 in memory. */
3177
3178 rtx val, seq;
3179
3180 if (store)
3181 {
3182 rtx p = PREV_INSN (insn);
3183
3184 start_sequence ();
3185 val = gen_reg_rtx (GET_MODE (x));
3186 if (! validate_change (insn, loc, val, 0))
3187 {
3188 /* Discard the current sequence and put the
3189 ADDRESSOF on stack. */
3190 end_sequence ();
3191 goto give_up;
3192 }
3193 seq = get_insns ();
3194 end_sequence ();
3195 emit_insn_before (seq, insn);
3196 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3197 insn, ht);
3198
3199 start_sequence ();
3200 store_bit_field (sub, size_x, 0, GET_MODE (x),
3201 val, GET_MODE_SIZE (GET_MODE (sub)));
3202
3203 /* Make sure to unshare any shared rtl that store_bit_field
3204 might have created. */
3205 unshare_all_rtl_again (get_insns ());
3206
3207 seq = get_insns ();
3208 end_sequence ();
3209 p = emit_insn_after (seq, insn);
3210 if (NEXT_INSN (insn))
3211 compute_insns_for_mem (NEXT_INSN (insn),
3212 p ? NEXT_INSN (p) : NULL_RTX,
3213 ht);
3214 }
3215 else
3216 {
3217 rtx p = PREV_INSN (insn);
3218
3219 start_sequence ();
3220 val = extract_bit_field (sub, size_x, 0, 1, NULL_RTX,
3221 GET_MODE (x), GET_MODE (x),
3222 GET_MODE_SIZE (GET_MODE (sub)));
3223
3224 if (! validate_change (insn, loc, val, 0))
3225 {
3226 /* Discard the current sequence and put the
3227 ADDRESSOF on stack. */
3228 end_sequence ();
3229 goto give_up;
3230 }
3231
3232 seq = get_insns ();
3233 end_sequence ();
3234 emit_insn_before (seq, insn);
3235 compute_insns_for_mem (p ? NEXT_INSN (p) : get_insns (),
3236 insn, ht);
3237 }
3238
3239 /* Remember the replacement so that the same one can be done
3240 on the REG_NOTES. */
3241 purge_bitfield_addressof_replacements
3242 = gen_rtx_EXPR_LIST (VOIDmode, x,
3243 gen_rtx_EXPR_LIST
3244 (VOIDmode, val,
3245 purge_bitfield_addressof_replacements));
3246
3247 /* We replaced with a reg -- all done. */
3248 return true;
3249 }
3250 }
3251
3252 else if (validate_change (insn, loc, sub, 0))
3253 {
3254 /* Remember the replacement so that the same one can be done
3255 on the REG_NOTES. */
3256 if (GET_CODE (sub) == REG || GET_CODE (sub) == SUBREG)
3257 {
3258 rtx tem;
3259
3260 for (tem = purge_addressof_replacements;
3261 tem != NULL_RTX;
3262 tem = XEXP (XEXP (tem, 1), 1))
3263 if (rtx_equal_p (XEXP (x, 0), XEXP (tem, 0)))
3264 {
3265 XEXP (XEXP (tem, 1), 0) = sub;
3266 return true;
3267 }
3268 purge_addressof_replacements
3269 = gen_rtx_EXPR_LIST (VOIDmode, XEXP (x, 0),
3270 gen_rtx_EXPR_LIST (VOIDmode, sub,
3271 purge_addressof_replacements));
3272 return true;
3273 }
3274 goto restart;
3275 }
3276 }
3277
3278 give_up:
3279 /* Scan all subexpressions. */
3280 fmt = GET_RTX_FORMAT (code);
3281 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3282 {
3283 if (*fmt == 'e')
3284 result &= purge_addressof_1 (&XEXP (x, i), insn, force, 0,
3285 may_postpone, ht);
3286 else if (*fmt == 'E')
3287 for (j = 0; j < XVECLEN (x, i); j++)
3288 result &= purge_addressof_1 (&XVECEXP (x, i, j), insn, force, 0,
3289 may_postpone, ht);
3290 }
3291
3292 return result;
3293 }
3294
3295 /* Return a hash value for K, a REG. */
3296
3297 static hashval_t
3298 insns_for_mem_hash (const void *k)
3299 {
3300 /* Use the address of the key for the hash value. */
3301 struct insns_for_mem_entry *m = (struct insns_for_mem_entry *) k;
3302 return htab_hash_pointer (m->key);
3303 }
3304
3305 /* Return nonzero if K1 and K2 (two REGs) are the same. */
3306
3307 static int
3308 insns_for_mem_comp (const void *k1, const void *k2)
3309 {
3310 struct insns_for_mem_entry *m1 = (struct insns_for_mem_entry *) k1;
3311 struct insns_for_mem_entry *m2 = (struct insns_for_mem_entry *) k2;
3312 return m1->key == m2->key;
3313 }
3314
3315 struct insns_for_mem_walk_info
3316 {
3317 /* The hash table that we are using to record which INSNs use which
3318 MEMs. */
3319 htab_t ht;
3320
3321 /* The INSN we are currently processing. */
3322 rtx insn;
3323
3324 /* Zero if we are walking to find ADDRESSOFs, one if we are walking
3325 to find the insns that use the REGs in the ADDRESSOFs. */
3326 int pass;
3327 };
3328
3329 /* Called from compute_insns_for_mem via for_each_rtx. If R is a REG
3330 that might be used in an ADDRESSOF expression, record this INSN in
3331 the hash table given by DATA (which is really a pointer to an
3332 insns_for_mem_walk_info structure). */
3333
3334 static int
3335 insns_for_mem_walk (rtx *r, void *data)
3336 {
3337 struct insns_for_mem_walk_info *ifmwi
3338 = (struct insns_for_mem_walk_info *) data;
3339 struct insns_for_mem_entry tmp;
3340 tmp.insns = NULL_RTX;
3341
3342 if (ifmwi->pass == 0 && *r && GET_CODE (*r) == ADDRESSOF
3343 && GET_CODE (XEXP (*r, 0)) == REG)
3344 {
3345 void **e;
3346 tmp.key = XEXP (*r, 0);
3347 e = htab_find_slot (ifmwi->ht, &tmp, INSERT);
3348 if (*e == NULL)
3349 {
3350 *e = ggc_alloc (sizeof (tmp));
3351 memcpy (*e, &tmp, sizeof (tmp));
3352 }
3353 }
3354 else if (ifmwi->pass == 1 && *r && GET_CODE (*r) == REG)
3355 {
3356 struct insns_for_mem_entry *ifme;
3357 tmp.key = *r;
3358 ifme = htab_find (ifmwi->ht, &tmp);
3359
3360 /* If we have not already recorded this INSN, do so now. Since
3361 we process the INSNs in order, we know that if we have
3362 recorded it it must be at the front of the list. */
3363 if (ifme && (!ifme->insns || XEXP (ifme->insns, 0) != ifmwi->insn))
3364 ifme->insns = gen_rtx_EXPR_LIST (VOIDmode, ifmwi->insn,
3365 ifme->insns);
3366 }
3367
3368 return 0;
3369 }
3370
3371 /* Walk the INSNS, until we reach LAST_INSN, recording which INSNs use
3372 which REGs in HT. */
3373
3374 static void
3375 compute_insns_for_mem (rtx insns, rtx last_insn, htab_t ht)
3376 {
3377 rtx insn;
3378 struct insns_for_mem_walk_info ifmwi;
3379 ifmwi.ht = ht;
3380
3381 for (ifmwi.pass = 0; ifmwi.pass < 2; ++ifmwi.pass)
3382 for (insn = insns; insn != last_insn; insn = NEXT_INSN (insn))
3383 if (INSN_P (insn))
3384 {
3385 ifmwi.insn = insn;
3386 for_each_rtx (&insn, insns_for_mem_walk, &ifmwi);
3387 }
3388 }
3389
3390 /* Helper function for purge_addressof called through for_each_rtx.
3391 Returns true iff the rtl is an ADDRESSOF. */
3392
3393 static int
3394 is_addressof (rtx *rtl, void *data ATTRIBUTE_UNUSED)
3395 {
3396 return GET_CODE (*rtl) == ADDRESSOF;
3397 }
3398
3399 /* Eliminate all occurrences of ADDRESSOF from INSNS. Elide any remaining
3400 (MEM (ADDRESSOF)) patterns, and force any needed registers into the
3401 stack. */
3402
3403 void
3404 purge_addressof (rtx insns)
3405 {
3406 rtx insn, tmp;
3407 htab_t ht;
3408
3409 /* When we actually purge ADDRESSOFs, we turn REGs into MEMs. That
3410 requires a fixup pass over the instruction stream to correct
3411 INSNs that depended on the REG being a REG, and not a MEM. But,
3412 these fixup passes are slow. Furthermore, most MEMs are not
3413 mentioned in very many instructions. So, we speed up the process
3414 by pre-calculating which REGs occur in which INSNs; that allows
3415 us to perform the fixup passes much more quickly. */
3416 ht = htab_create_ggc (1000, insns_for_mem_hash, insns_for_mem_comp, NULL);
3417 compute_insns_for_mem (insns, NULL_RTX, ht);
3418
3419 postponed_insns = NULL;
3420
3421 for (insn = insns; insn; insn = NEXT_INSN (insn))
3422 if (INSN_P (insn))
3423 {
3424 if (! purge_addressof_1 (&PATTERN (insn), insn,
3425 asm_noperands (PATTERN (insn)) > 0, 0, 1, ht))
3426 /* If we could not replace the ADDRESSOFs in the insn,
3427 something is wrong. */
3428 abort ();
3429
3430 if (! purge_addressof_1 (&REG_NOTES (insn), NULL_RTX, 0, 0, 0, ht))
3431 {
3432 /* If we could not replace the ADDRESSOFs in the insn's notes,
3433 we can just remove the offending notes instead. */
3434 rtx note;
3435
3436 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3437 {
3438 /* If we find a REG_RETVAL note then the insn is a libcall.
3439 Such insns must have REG_EQUAL notes as well, in order
3440 for later passes of the compiler to work. So it is not
3441 safe to delete the notes here, and instead we abort. */
3442 if (REG_NOTE_KIND (note) == REG_RETVAL)
3443 abort ();
3444 if (for_each_rtx (&note, is_addressof, NULL))
3445 remove_note (insn, note);
3446 }
3447 }
3448 }
3449
3450 /* Process the postponed insns. */
3451 while (postponed_insns)
3452 {
3453 insn = XEXP (postponed_insns, 0);
3454 tmp = postponed_insns;
3455 postponed_insns = XEXP (postponed_insns, 1);
3456 free_INSN_LIST_node (tmp);
3457
3458 if (! purge_addressof_1 (&PATTERN (insn), insn,
3459 asm_noperands (PATTERN (insn)) > 0, 0, 0, ht))
3460 abort ();
3461 }
3462
3463 /* Clean up. */
3464 purge_bitfield_addressof_replacements = 0;
3465 purge_addressof_replacements = 0;
3466
3467 /* REGs are shared. purge_addressof will destructively replace a REG
3468 with a MEM, which creates shared MEMs.
3469
3470 Unfortunately, the children of put_reg_into_stack assume that MEMs
3471 referring to the same stack slot are shared (fixup_var_refs and
3472 the associated hash table code).
3473
3474 So, we have to do another unsharing pass after we have flushed any
3475 REGs that had their address taken into the stack.
3476
3477 It may be worth tracking whether or not we converted any REGs into
3478 MEMs to avoid this overhead when it is not needed. */
3479 unshare_all_rtl_again (get_insns ());
3480 }
3481 \f
3482 /* Convert a SET of a hard subreg to a set of the appropriate hard
3483 register. A subroutine of purge_hard_subreg_sets. */
3484
3485 static void
3486 purge_single_hard_subreg_set (rtx pattern)
3487 {
3488 rtx reg = SET_DEST (pattern);
3489 enum machine_mode mode = GET_MODE (SET_DEST (pattern));
3490 int offset = 0;
3491
3492 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG
3493 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
3494 {
3495 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
3496 GET_MODE (SUBREG_REG (reg)),
3497 SUBREG_BYTE (reg),
3498 GET_MODE (reg));
3499 reg = SUBREG_REG (reg);
3500 }
3501
3502
3503 if (GET_CODE (reg) == REG && REGNO (reg) < FIRST_PSEUDO_REGISTER)
3504 {
3505 reg = gen_rtx_REG (mode, REGNO (reg) + offset);
3506 SET_DEST (pattern) = reg;
3507 }
3508 }
3509
3510 /* Eliminate all occurrences of SETs of hard subregs from INSNS. The
3511 only such SETs that we expect to see are those left in because
3512 integrate can't handle sets of parts of a return value register.
3513
3514 We don't use alter_subreg because we only want to eliminate subregs
3515 of hard registers. */
3516
3517 void
3518 purge_hard_subreg_sets (rtx insn)
3519 {
3520 for (; insn; insn = NEXT_INSN (insn))
3521 {
3522 if (INSN_P (insn))
3523 {
3524 rtx pattern = PATTERN (insn);
3525 switch (GET_CODE (pattern))
3526 {
3527 case SET:
3528 if (GET_CODE (SET_DEST (pattern)) == SUBREG)
3529 purge_single_hard_subreg_set (pattern);
3530 break;
3531 case PARALLEL:
3532 {
3533 int j;
3534 for (j = XVECLEN (pattern, 0) - 1; j >= 0; j--)
3535 {
3536 rtx inner_pattern = XVECEXP (pattern, 0, j);
3537 if (GET_CODE (inner_pattern) == SET
3538 && GET_CODE (SET_DEST (inner_pattern)) == SUBREG)
3539 purge_single_hard_subreg_set (inner_pattern);
3540 }
3541 }
3542 break;
3543 default:
3544 break;
3545 }
3546 }
3547 }
3548 }
3549 \f
3550 /* Pass through the INSNS of function FNDECL and convert virtual register
3551 references to hard register references. */
3552
3553 void
3554 instantiate_virtual_regs (tree fndecl, rtx insns)
3555 {
3556 rtx insn;
3557 unsigned int i;
3558
3559 /* Compute the offsets to use for this function. */
3560 in_arg_offset = FIRST_PARM_OFFSET (fndecl);
3561 var_offset = STARTING_FRAME_OFFSET;
3562 dynamic_offset = STACK_DYNAMIC_OFFSET (fndecl);
3563 out_arg_offset = STACK_POINTER_OFFSET;
3564 cfa_offset = ARG_POINTER_CFA_OFFSET (fndecl);
3565
3566 /* Scan all variables and parameters of this function. For each that is
3567 in memory, instantiate all virtual registers if the result is a valid
3568 address. If not, we do it later. That will handle most uses of virtual
3569 regs on many machines. */
3570 instantiate_decls (fndecl, 1);
3571
3572 /* Initialize recognition, indicating that volatile is OK. */
3573 init_recog ();
3574
3575 /* Scan through all the insns, instantiating every virtual register still
3576 present. */
3577 for (insn = insns; insn; insn = NEXT_INSN (insn))
3578 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
3579 || GET_CODE (insn) == CALL_INSN)
3580 {
3581 instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
3582 if (INSN_DELETED_P (insn))
3583 continue;
3584 instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
3585 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
3586 if (GET_CODE (insn) == CALL_INSN)
3587 instantiate_virtual_regs_1 (&CALL_INSN_FUNCTION_USAGE (insn),
3588 NULL_RTX, 0);
3589
3590 /* Past this point all ASM statements should match. Verify that
3591 to avoid failures later in the compilation process. */
3592 if (asm_noperands (PATTERN (insn)) >= 0
3593 && ! check_asm_operands (PATTERN (insn)))
3594 instantiate_virtual_regs_lossage (insn);
3595 }
3596
3597 /* Instantiate the stack slots for the parm registers, for later use in
3598 addressof elimination. */
3599 for (i = 0; i < max_parm_reg; ++i)
3600 if (parm_reg_stack_loc[i])
3601 instantiate_virtual_regs_1 (&parm_reg_stack_loc[i], NULL_RTX, 0);
3602
3603 /* Now instantiate the remaining register equivalences for debugging info.
3604 These will not be valid addresses. */
3605 instantiate_decls (fndecl, 0);
3606
3607 /* Indicate that, from now on, assign_stack_local should use
3608 frame_pointer_rtx. */
3609 virtuals_instantiated = 1;
3610 }
3611
3612 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
3613 all virtual registers in their DECL_RTL's.
3614
3615 If VALID_ONLY, do this only if the resulting address is still valid.
3616 Otherwise, always do it. */
3617
3618 static void
3619 instantiate_decls (tree fndecl, int valid_only)
3620 {
3621 tree decl;
3622
3623 /* Process all parameters of the function. */
3624 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
3625 {
3626 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
3627 HOST_WIDE_INT size_rtl;
3628
3629 instantiate_decl (DECL_RTL (decl), size, valid_only);
3630
3631 /* If the parameter was promoted, then the incoming RTL mode may be
3632 larger than the declared type size. We must use the larger of
3633 the two sizes. */
3634 size_rtl = GET_MODE_SIZE (GET_MODE (DECL_INCOMING_RTL (decl)));
3635 size = MAX (size_rtl, size);
3636 instantiate_decl (DECL_INCOMING_RTL (decl), size, valid_only);
3637 }
3638
3639 /* Now process all variables defined in the function or its subblocks. */
3640 instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
3641 }
3642
3643 /* Subroutine of instantiate_decls: Process all decls in the given
3644 BLOCK node and all its subblocks. */
3645
3646 static void
3647 instantiate_decls_1 (tree let, int valid_only)
3648 {
3649 tree t;
3650
3651 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
3652 if (DECL_RTL_SET_P (t))
3653 instantiate_decl (DECL_RTL (t),
3654 int_size_in_bytes (TREE_TYPE (t)),
3655 valid_only);
3656
3657 /* Process all subblocks. */
3658 for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
3659 instantiate_decls_1 (t, valid_only);
3660 }
3661
3662 /* Subroutine of the preceding procedures: Given RTL representing a
3663 decl and the size of the object, do any instantiation required.
3664
3665 If VALID_ONLY is nonzero, it means that the RTL should only be
3666 changed if the new address is valid. */
3667
3668 static void
3669 instantiate_decl (rtx x, HOST_WIDE_INT size, int valid_only)
3670 {
3671 enum machine_mode mode;
3672 rtx addr;
3673
3674 /* If this is not a MEM, no need to do anything. Similarly if the
3675 address is a constant or a register that is not a virtual register. */
3676
3677 if (x == 0 || GET_CODE (x) != MEM)
3678 return;
3679
3680 addr = XEXP (x, 0);
3681 if (CONSTANT_P (addr)
3682 || (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == REG)
3683 || (GET_CODE (addr) == REG
3684 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
3685 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
3686 return;
3687
3688 /* If we should only do this if the address is valid, copy the address.
3689 We need to do this so we can undo any changes that might make the
3690 address invalid. This copy is unfortunate, but probably can't be
3691 avoided. */
3692
3693 if (valid_only)
3694 addr = copy_rtx (addr);
3695
3696 instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
3697
3698 if (valid_only && size >= 0)
3699 {
3700 unsigned HOST_WIDE_INT decl_size = size;
3701
3702 /* Now verify that the resulting address is valid for every integer or
3703 floating-point mode up to and including SIZE bytes long. We do this
3704 since the object might be accessed in any mode and frame addresses
3705 are shared. */
3706
3707 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3708 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3709 mode = GET_MODE_WIDER_MODE (mode))
3710 if (! memory_address_p (mode, addr))
3711 return;
3712
3713 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
3714 mode != VOIDmode && GET_MODE_SIZE (mode) <= decl_size;
3715 mode = GET_MODE_WIDER_MODE (mode))
3716 if (! memory_address_p (mode, addr))
3717 return;
3718 }
3719
3720 /* Put back the address now that we have updated it and we either know
3721 it is valid or we don't care whether it is valid. */
3722
3723 XEXP (x, 0) = addr;
3724 }
3725 \f
3726 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
3727 is a virtual register, return the equivalent hard register and set the
3728 offset indirectly through the pointer. Otherwise, return 0. */
3729
3730 static rtx
3731 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
3732 {
3733 rtx new;
3734 HOST_WIDE_INT offset;
3735
3736 if (x == virtual_incoming_args_rtx)
3737 new = arg_pointer_rtx, offset = in_arg_offset;
3738 else if (x == virtual_stack_vars_rtx)
3739 new = frame_pointer_rtx, offset = var_offset;
3740 else if (x == virtual_stack_dynamic_rtx)
3741 new = stack_pointer_rtx, offset = dynamic_offset;
3742 else if (x == virtual_outgoing_args_rtx)
3743 new = stack_pointer_rtx, offset = out_arg_offset;
3744 else if (x == virtual_cfa_rtx)
3745 new = arg_pointer_rtx, offset = cfa_offset;
3746 else
3747 return 0;
3748
3749 *poffset = offset;
3750 return new;
3751 }
3752 \f
3753
3754 /* Called when instantiate_virtual_regs has failed to update the instruction.
3755 Usually this means that non-matching instruction has been emit, however for
3756 asm statements it may be the problem in the constraints. */
3757 static void
3758 instantiate_virtual_regs_lossage (rtx insn)
3759 {
3760 if (asm_noperands (PATTERN (insn)) >= 0)
3761 {
3762 error_for_asm (insn, "impossible constraint in `asm'");
3763 delete_insn (insn);
3764 }
3765 else
3766 abort ();
3767 }
3768 /* Given a pointer to a piece of rtx and an optional pointer to the
3769 containing object, instantiate any virtual registers present in it.
3770
3771 If EXTRA_INSNS, we always do the replacement and generate
3772 any extra insns before OBJECT. If it zero, we do nothing if replacement
3773 is not valid.
3774
3775 Return 1 if we either had nothing to do or if we were able to do the
3776 needed replacement. Return 0 otherwise; we only return zero if
3777 EXTRA_INSNS is zero.
3778
3779 We first try some simple transformations to avoid the creation of extra
3780 pseudos. */
3781
3782 static int
3783 instantiate_virtual_regs_1 (rtx *loc, rtx object, int extra_insns)
3784 {
3785 rtx x;
3786 RTX_CODE code;
3787 rtx new = 0;
3788 HOST_WIDE_INT offset = 0;
3789 rtx temp;
3790 rtx seq;
3791 int i, j;
3792 const char *fmt;
3793
3794 /* Re-start here to avoid recursion in common cases. */
3795 restart:
3796
3797 x = *loc;
3798 if (x == 0)
3799 return 1;
3800
3801 /* We may have detected and deleted invalid asm statements. */
3802 if (object && INSN_P (object) && INSN_DELETED_P (object))
3803 return 1;
3804
3805 code = GET_CODE (x);
3806
3807 /* Check for some special cases. */
3808 switch (code)
3809 {
3810 case CONST_INT:
3811 case CONST_DOUBLE:
3812 case CONST_VECTOR:
3813 case CONST:
3814 case SYMBOL_REF:
3815 case CODE_LABEL:
3816 case PC:
3817 case CC0:
3818 case ASM_INPUT:
3819 case ADDR_VEC:
3820 case ADDR_DIFF_VEC:
3821 case RETURN:
3822 return 1;
3823
3824 case SET:
3825 /* We are allowed to set the virtual registers. This means that
3826 the actual register should receive the source minus the
3827 appropriate offset. This is used, for example, in the handling
3828 of non-local gotos. */
3829 if ((new = instantiate_new_reg (SET_DEST (x), &offset)) != 0)
3830 {
3831 rtx src = SET_SRC (x);
3832
3833 /* We are setting the register, not using it, so the relevant
3834 offset is the negative of the offset to use were we using
3835 the register. */
3836 offset = - offset;
3837 instantiate_virtual_regs_1 (&src, NULL_RTX, 0);
3838
3839 /* The only valid sources here are PLUS or REG. Just do
3840 the simplest possible thing to handle them. */
3841 if (GET_CODE (src) != REG && GET_CODE (src) != PLUS)
3842 {
3843 instantiate_virtual_regs_lossage (object);
3844 return 1;
3845 }
3846
3847 start_sequence ();
3848 if (GET_CODE (src) != REG)
3849 temp = force_operand (src, NULL_RTX);
3850 else
3851 temp = src;
3852 temp = force_operand (plus_constant (temp, offset), NULL_RTX);
3853 seq = get_insns ();
3854 end_sequence ();
3855
3856 emit_insn_before (seq, object);
3857 SET_DEST (x) = new;
3858
3859 if (! validate_change (object, &SET_SRC (x), temp, 0)
3860 || ! extra_insns)
3861 instantiate_virtual_regs_lossage (object);
3862
3863 return 1;
3864 }
3865
3866 instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
3867 loc = &SET_SRC (x);
3868 goto restart;
3869
3870 case PLUS:
3871 /* Handle special case of virtual register plus constant. */
3872 if (CONSTANT_P (XEXP (x, 1)))
3873 {
3874 rtx old, new_offset;
3875
3876 /* Check for (plus (plus VIRT foo) (const_int)) first. */
3877 if (GET_CODE (XEXP (x, 0)) == PLUS)
3878 {
3879 if ((new = instantiate_new_reg (XEXP (XEXP (x, 0), 0), &offset)))
3880 {
3881 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
3882 extra_insns);
3883 new = gen_rtx_PLUS (Pmode, new, XEXP (XEXP (x, 0), 1));
3884 }
3885 else
3886 {
3887 loc = &XEXP (x, 0);
3888 goto restart;
3889 }
3890 }
3891
3892 #ifdef POINTERS_EXTEND_UNSIGNED
3893 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
3894 we can commute the PLUS and SUBREG because pointers into the
3895 frame are well-behaved. */
3896 else if (GET_CODE (XEXP (x, 0)) == SUBREG && GET_MODE (x) == ptr_mode
3897 && GET_CODE (XEXP (x, 1)) == CONST_INT
3898 && 0 != (new
3899 = instantiate_new_reg (SUBREG_REG (XEXP (x, 0)),
3900 &offset))
3901 && validate_change (object, loc,
3902 plus_constant (gen_lowpart (ptr_mode,
3903 new),
3904 offset
3905 + INTVAL (XEXP (x, 1))),
3906 0))
3907 return 1;
3908 #endif
3909 else if ((new = instantiate_new_reg (XEXP (x, 0), &offset)) == 0)
3910 {
3911 /* We know the second operand is a constant. Unless the
3912 first operand is a REG (which has been already checked),
3913 it needs to be checked. */
3914 if (GET_CODE (XEXP (x, 0)) != REG)
3915 {
3916 loc = &XEXP (x, 0);
3917 goto restart;
3918 }
3919 return 1;
3920 }
3921
3922 new_offset = plus_constant (XEXP (x, 1), offset);
3923
3924 /* If the new constant is zero, try to replace the sum with just
3925 the register. */
3926 if (new_offset == const0_rtx
3927 && validate_change (object, loc, new, 0))
3928 return 1;
3929
3930 /* Next try to replace the register and new offset.
3931 There are two changes to validate here and we can't assume that
3932 in the case of old offset equals new just changing the register
3933 will yield a valid insn. In the interests of a little efficiency,
3934 however, we only call validate change once (we don't queue up the
3935 changes and then call apply_change_group). */
3936
3937 old = XEXP (x, 0);
3938 if (offset == 0
3939 ? ! validate_change (object, &XEXP (x, 0), new, 0)
3940 : (XEXP (x, 0) = new,
3941 ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
3942 {
3943 if (! extra_insns)
3944 {
3945 XEXP (x, 0) = old;
3946 return 0;
3947 }
3948
3949 /* Otherwise copy the new constant into a register and replace
3950 constant with that register. */
3951 temp = gen_reg_rtx (Pmode);
3952 XEXP (x, 0) = new;
3953 if (validate_change (object, &XEXP (x, 1), temp, 0))
3954 emit_insn_before (gen_move_insn (temp, new_offset), object);
3955 else
3956 {
3957 /* If that didn't work, replace this expression with a
3958 register containing the sum. */
3959
3960 XEXP (x, 0) = old;
3961 new = gen_rtx_PLUS (Pmode, new, new_offset);
3962
3963 start_sequence ();
3964 temp = force_operand (new, NULL_RTX);
3965 seq = get_insns ();
3966 end_sequence ();
3967
3968 emit_insn_before (seq, object);
3969 if (! validate_change (object, loc, temp, 0)
3970 && ! validate_replace_rtx (x, temp, object))
3971 {
3972 instantiate_virtual_regs_lossage (object);
3973 return 1;
3974 }
3975 }
3976 }
3977
3978 return 1;
3979 }
3980
3981 /* Fall through to generic two-operand expression case. */
3982 case EXPR_LIST:
3983 case CALL:
3984 case COMPARE:
3985 case MINUS:
3986 case MULT:
3987 case DIV: case UDIV:
3988 case MOD: case UMOD:
3989 case AND: case IOR: case XOR:
3990 case ROTATERT: case ROTATE:
3991 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
3992 case NE: case EQ:
3993 case GE: case GT: case GEU: case GTU:
3994 case LE: case LT: case LEU: case LTU:
3995 if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
3996 instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
3997 loc = &XEXP (x, 0);
3998 goto restart;
3999
4000 case MEM:
4001 /* Most cases of MEM that convert to valid addresses have already been
4002 handled by our scan of decls. The only special handling we
4003 need here is to make a copy of the rtx to ensure it isn't being
4004 shared if we have to change it to a pseudo.
4005
4006 If the rtx is a simple reference to an address via a virtual register,
4007 it can potentially be shared. In such cases, first try to make it
4008 a valid address, which can also be shared. Otherwise, copy it and
4009 proceed normally.
4010
4011 First check for common cases that need no processing. These are
4012 usually due to instantiation already being done on a previous instance
4013 of a shared rtx. */
4014
4015 temp = XEXP (x, 0);
4016 if (CONSTANT_ADDRESS_P (temp)
4017 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4018 || temp == arg_pointer_rtx
4019 #endif
4020 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
4021 || temp == hard_frame_pointer_rtx
4022 #endif
4023 || temp == frame_pointer_rtx)
4024 return 1;
4025
4026 if (GET_CODE (temp) == PLUS
4027 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
4028 && (XEXP (temp, 0) == frame_pointer_rtx
4029 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
4030 || XEXP (temp, 0) == hard_frame_pointer_rtx
4031 #endif
4032 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4033 || XEXP (temp, 0) == arg_pointer_rtx
4034 #endif
4035 ))
4036 return 1;
4037
4038 if (temp == virtual_stack_vars_rtx
4039 || temp == virtual_incoming_args_rtx
4040 || (GET_CODE (temp) == PLUS
4041 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
4042 && (XEXP (temp, 0) == virtual_stack_vars_rtx
4043 || XEXP (temp, 0) == virtual_incoming_args_rtx)))
4044 {
4045 /* This MEM may be shared. If the substitution can be done without
4046 the need to generate new pseudos, we want to do it in place
4047 so all copies of the shared rtx benefit. The call below will
4048 only make substitutions if the resulting address is still
4049 valid.
4050
4051 Note that we cannot pass X as the object in the recursive call
4052 since the insn being processed may not allow all valid
4053 addresses. However, if we were not passed on object, we can
4054 only modify X without copying it if X will have a valid
4055 address.
4056
4057 ??? Also note that this can still lose if OBJECT is an insn that
4058 has less restrictions on an address that some other insn.
4059 In that case, we will modify the shared address. This case
4060 doesn't seem very likely, though. One case where this could
4061 happen is in the case of a USE or CLOBBER reference, but we
4062 take care of that below. */
4063
4064 if (instantiate_virtual_regs_1 (&XEXP (x, 0),
4065 object ? object : x, 0))
4066 return 1;
4067
4068 /* Otherwise make a copy and process that copy. We copy the entire
4069 RTL expression since it might be a PLUS which could also be
4070 shared. */
4071 *loc = x = copy_rtx (x);
4072 }
4073
4074 /* Fall through to generic unary operation case. */
4075 case PREFETCH:
4076 case SUBREG:
4077 case STRICT_LOW_PART:
4078 case NEG: case NOT:
4079 case PRE_DEC: case PRE_INC: case POST_DEC: case POST_INC:
4080 case SIGN_EXTEND: case ZERO_EXTEND:
4081 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
4082 case FLOAT: case FIX:
4083 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
4084 case ABS:
4085 case SQRT:
4086 case FFS:
4087 case CLZ: case CTZ:
4088 case POPCOUNT: case PARITY:
4089 /* These case either have just one operand or we know that we need not
4090 check the rest of the operands. */
4091 loc = &XEXP (x, 0);
4092 goto restart;
4093
4094 case USE:
4095 case CLOBBER:
4096 /* If the operand is a MEM, see if the change is a valid MEM. If not,
4097 go ahead and make the invalid one, but do it to a copy. For a REG,
4098 just make the recursive call, since there's no chance of a problem. */
4099
4100 if ((GET_CODE (XEXP (x, 0)) == MEM
4101 && instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), XEXP (x, 0),
4102 0))
4103 || (GET_CODE (XEXP (x, 0)) == REG
4104 && instantiate_virtual_regs_1 (&XEXP (x, 0), object, 0)))
4105 return 1;
4106
4107 XEXP (x, 0) = copy_rtx (XEXP (x, 0));
4108 loc = &XEXP (x, 0);
4109 goto restart;
4110
4111 case REG:
4112 /* Try to replace with a PLUS. If that doesn't work, compute the sum
4113 in front of this insn and substitute the temporary. */
4114 if ((new = instantiate_new_reg (x, &offset)) != 0)
4115 {
4116 temp = plus_constant (new, offset);
4117 if (!validate_change (object, loc, temp, 0))
4118 {
4119 if (! extra_insns)
4120 return 0;
4121
4122 start_sequence ();
4123 temp = force_operand (temp, NULL_RTX);
4124 seq = get_insns ();
4125 end_sequence ();
4126
4127 emit_insn_before (seq, object);
4128 if (! validate_change (object, loc, temp, 0)
4129 && ! validate_replace_rtx (x, temp, object))
4130 instantiate_virtual_regs_lossage (object);
4131 }
4132 }
4133
4134 return 1;
4135
4136 case ADDRESSOF:
4137 if (GET_CODE (XEXP (x, 0)) == REG)
4138 return 1;
4139
4140 else if (GET_CODE (XEXP (x, 0)) == MEM)
4141 {
4142 /* If we have a (addressof (mem ..)), do any instantiation inside
4143 since we know we'll be making the inside valid when we finally
4144 remove the ADDRESSOF. */
4145 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), NULL_RTX, 0);
4146 return 1;
4147 }
4148 break;
4149
4150 default:
4151 break;
4152 }
4153
4154 /* Scan all subexpressions. */
4155 fmt = GET_RTX_FORMAT (code);
4156 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
4157 if (*fmt == 'e')
4158 {
4159 if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
4160 return 0;
4161 }
4162 else if (*fmt == 'E')
4163 for (j = 0; j < XVECLEN (x, i); j++)
4164 if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
4165 extra_insns))
4166 return 0;
4167
4168 return 1;
4169 }
4170 \f
4171 /* Optimization: assuming this function does not receive nonlocal gotos,
4172 delete the handlers for such, as well as the insns to establish
4173 and disestablish them. */
4174
4175 static void
4176 delete_handlers (void)
4177 {
4178 rtx insn;
4179 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4180 {
4181 /* Delete the handler by turning off the flag that would
4182 prevent jump_optimize from deleting it.
4183 Also permit deletion of the nonlocal labels themselves
4184 if nothing local refers to them. */
4185 if (GET_CODE (insn) == CODE_LABEL)
4186 {
4187 tree t, last_t;
4188
4189 LABEL_PRESERVE_P (insn) = 0;
4190
4191 /* Remove it from the nonlocal_label list, to avoid confusing
4192 flow. */
4193 for (t = nonlocal_labels, last_t = 0; t;
4194 last_t = t, t = TREE_CHAIN (t))
4195 if (DECL_RTL (TREE_VALUE (t)) == insn)
4196 break;
4197 if (t)
4198 {
4199 if (! last_t)
4200 nonlocal_labels = TREE_CHAIN (nonlocal_labels);
4201 else
4202 TREE_CHAIN (last_t) = TREE_CHAIN (t);
4203 }
4204 }
4205 if (GET_CODE (insn) == INSN)
4206 {
4207 int can_delete = 0;
4208 rtx t;
4209 for (t = nonlocal_goto_handler_slots; t != 0; t = XEXP (t, 1))
4210 if (reg_mentioned_p (t, PATTERN (insn)))
4211 {
4212 can_delete = 1;
4213 break;
4214 }
4215 if (can_delete
4216 || (nonlocal_goto_stack_level != 0
4217 && reg_mentioned_p (nonlocal_goto_stack_level,
4218 PATTERN (insn))))
4219 delete_related_insns (insn);
4220 }
4221 }
4222 }
4223 \f
4224 /* Return the first insn following those generated by `assign_parms'. */
4225
4226 rtx
4227 get_first_nonparm_insn (void)
4228 {
4229 if (last_parm_insn)
4230 return NEXT_INSN (last_parm_insn);
4231 return get_insns ();
4232 }
4233
4234 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
4235 This means a type for which function calls must pass an address to the
4236 function or get an address back from the function.
4237 EXP may be a type node or an expression (whose type is tested). */
4238
4239 int
4240 aggregate_value_p (tree exp, tree fntype)
4241 {
4242 int i, regno, nregs;
4243 rtx reg;
4244
4245 tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
4246
4247 if (fntype)
4248 switch (TREE_CODE (fntype))
4249 {
4250 case CALL_EXPR:
4251 fntype = get_callee_fndecl (fntype);
4252 fntype = fntype ? TREE_TYPE (fntype) : 0;
4253 break;
4254 case FUNCTION_DECL:
4255 fntype = TREE_TYPE (fntype);
4256 break;
4257 case FUNCTION_TYPE:
4258 case METHOD_TYPE:
4259 break;
4260 case IDENTIFIER_NODE:
4261 fntype = 0;
4262 break;
4263 default:
4264 /* We don't expect other rtl types here. */
4265 abort();
4266 }
4267
4268 if (TREE_CODE (type) == VOID_TYPE)
4269 return 0;
4270 if (targetm.calls.return_in_memory (type, fntype))
4271 return 1;
4272 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
4273 and thus can't be returned in registers. */
4274 if (TREE_ADDRESSABLE (type))
4275 return 1;
4276 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
4277 return 1;
4278 /* Make sure we have suitable call-clobbered regs to return
4279 the value in; if not, we must return it in memory. */
4280 reg = hard_function_value (type, 0, 0);
4281
4282 /* If we have something other than a REG (e.g. a PARALLEL), then assume
4283 it is OK. */
4284 if (GET_CODE (reg) != REG)
4285 return 0;
4286
4287 regno = REGNO (reg);
4288 nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
4289 for (i = 0; i < nregs; i++)
4290 if (! call_used_regs[regno + i])
4291 return 1;
4292 return 0;
4293 }
4294 \f
4295 /* Assign RTL expressions to the function's parameters.
4296 This may involve copying them into registers and using
4297 those registers as the RTL for them. */
4298
4299 void
4300 assign_parms (tree fndecl)
4301 {
4302 tree parm;
4303 CUMULATIVE_ARGS args_so_far;
4304 /* Total space needed so far for args on the stack,
4305 given as a constant and a tree-expression. */
4306 struct args_size stack_args_size;
4307 HOST_WIDE_INT extra_pretend_bytes = 0;
4308 tree fntype = TREE_TYPE (fndecl);
4309 tree fnargs = DECL_ARGUMENTS (fndecl), orig_fnargs;
4310 /* This is used for the arg pointer when referring to stack args. */
4311 rtx internal_arg_pointer;
4312 /* This is a dummy PARM_DECL that we used for the function result if
4313 the function returns a structure. */
4314 tree function_result_decl = 0;
4315 int varargs_setup = 0;
4316 int reg_parm_stack_space ATTRIBUTE_UNUSED = 0;
4317 rtx conversion_insns = 0;
4318
4319 /* Nonzero if function takes extra anonymous args.
4320 This means the last named arg must be on the stack
4321 right before the anonymous ones. */
4322 int stdarg
4323 = (TYPE_ARG_TYPES (fntype) != 0
4324 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4325 != void_type_node));
4326
4327 current_function_stdarg = stdarg;
4328
4329 /* If the reg that the virtual arg pointer will be translated into is
4330 not a fixed reg or is the stack pointer, make a copy of the virtual
4331 arg pointer, and address parms via the copy. The frame pointer is
4332 considered fixed even though it is not marked as such.
4333
4334 The second time through, simply use ap to avoid generating rtx. */
4335
4336 if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
4337 || ! (fixed_regs[ARG_POINTER_REGNUM]
4338 || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM)))
4339 internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
4340 else
4341 internal_arg_pointer = virtual_incoming_args_rtx;
4342 current_function_internal_arg_pointer = internal_arg_pointer;
4343
4344 stack_args_size.constant = 0;
4345 stack_args_size.var = 0;
4346
4347 /* If struct value address is treated as the first argument, make it so. */
4348 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
4349 && ! current_function_returns_pcc_struct
4350 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
4351 {
4352 tree type = build_pointer_type (TREE_TYPE (fntype));
4353
4354 function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
4355
4356 DECL_ARG_TYPE (function_result_decl) = type;
4357 TREE_CHAIN (function_result_decl) = fnargs;
4358 fnargs = function_result_decl;
4359 }
4360
4361 orig_fnargs = fnargs;
4362
4363 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
4364 parm_reg_stack_loc = ggc_alloc_cleared (max_parm_reg * sizeof (rtx));
4365
4366 /* If the target wants to split complex arguments into scalars, do so. */
4367 if (targetm.calls.split_complex_arg)
4368 fnargs = split_complex_args (fnargs);
4369
4370 #ifdef REG_PARM_STACK_SPACE
4371 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
4372 #endif
4373
4374 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
4375 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
4376 #else
4377 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, fndecl, -1);
4378 #endif
4379
4380 /* We haven't yet found an argument that we must push and pretend the
4381 caller did. */
4382 current_function_pretend_args_size = 0;
4383
4384 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
4385 {
4386 rtx entry_parm;
4387 rtx stack_parm;
4388 enum machine_mode promoted_mode, passed_mode;
4389 enum machine_mode nominal_mode, promoted_nominal_mode;
4390 int unsignedp;
4391 struct locate_and_pad_arg_data locate;
4392 int passed_pointer = 0;
4393 int did_conversion = 0;
4394 tree passed_type = DECL_ARG_TYPE (parm);
4395 tree nominal_type = TREE_TYPE (parm);
4396 int last_named = 0, named_arg;
4397 int in_regs;
4398 int partial = 0;
4399 int pretend_bytes = 0;
4400 int loaded_in_reg = 0;
4401
4402 /* Set LAST_NAMED if this is last named arg before last
4403 anonymous args. */
4404 if (stdarg)
4405 {
4406 tree tem;
4407
4408 for (tem = TREE_CHAIN (parm); tem; tem = TREE_CHAIN (tem))
4409 if (DECL_NAME (tem))
4410 break;
4411
4412 if (tem == 0)
4413 last_named = 1;
4414 }
4415 /* Set NAMED_ARG if this arg should be treated as a named arg. For
4416 most machines, if this is a varargs/stdarg function, then we treat
4417 the last named arg as if it were anonymous too. */
4418 named_arg = (targetm.calls.strict_argument_naming (&args_so_far)
4419 ? 1 : !last_named);
4420
4421 if (TREE_TYPE (parm) == error_mark_node
4422 /* This can happen after weird syntax errors
4423 or if an enum type is defined among the parms. */
4424 || TREE_CODE (parm) != PARM_DECL
4425 || passed_type == NULL)
4426 {
4427 SET_DECL_RTL (parm, gen_rtx_MEM (BLKmode, const0_rtx));
4428 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4429 TREE_USED (parm) = 1;
4430 continue;
4431 }
4432
4433 /* Find mode of arg as it is passed, and mode of arg
4434 as it should be during execution of this function. */
4435 passed_mode = TYPE_MODE (passed_type);
4436 nominal_mode = TYPE_MODE (nominal_type);
4437
4438 /* If the parm's mode is VOID, its value doesn't matter,
4439 and avoid the usual things like emit_move_insn that could crash. */
4440 if (nominal_mode == VOIDmode)
4441 {
4442 SET_DECL_RTL (parm, const0_rtx);
4443 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
4444 continue;
4445 }
4446
4447 /* If the parm is to be passed as a transparent union, use the
4448 type of the first field for the tests below. We have already
4449 verified that the modes are the same. */
4450 if (DECL_TRANSPARENT_UNION (parm)
4451 || (TREE_CODE (passed_type) == UNION_TYPE
4452 && TYPE_TRANSPARENT_UNION (passed_type)))
4453 passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
4454
4455 /* See if this arg was passed by invisible reference. It is if
4456 it is an object whose size depends on the contents of the
4457 object itself or if the machine requires these objects be passed
4458 that way. */
4459
4460 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (passed_type))
4461 || TREE_ADDRESSABLE (passed_type)
4462 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
4463 || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
4464 passed_type, named_arg)
4465 #endif
4466 )
4467 {
4468 passed_type = nominal_type = build_pointer_type (passed_type);
4469 passed_pointer = 1;
4470 passed_mode = nominal_mode = Pmode;
4471 }
4472 /* See if the frontend wants to pass this by invisible reference. */
4473 else if (passed_type != nominal_type
4474 && POINTER_TYPE_P (passed_type)
4475 && TREE_TYPE (passed_type) == nominal_type)
4476 {
4477 nominal_type = passed_type;
4478 passed_pointer = 1;
4479 passed_mode = nominal_mode = Pmode;
4480 }
4481
4482 promoted_mode = passed_mode;
4483
4484 if (targetm.calls.promote_function_args (TREE_TYPE (fndecl)))
4485 {
4486 /* Compute the mode in which the arg is actually extended to. */
4487 unsignedp = TYPE_UNSIGNED (passed_type);
4488 promoted_mode = promote_mode (passed_type, promoted_mode,
4489 &unsignedp, 1);
4490 }
4491
4492 /* Let machine desc say which reg (if any) the parm arrives in.
4493 0 means it arrives on the stack. */
4494 #ifdef FUNCTION_INCOMING_ARG
4495 entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4496 passed_type, named_arg);
4497 #else
4498 entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
4499 passed_type, named_arg);
4500 #endif
4501
4502 if (entry_parm == 0)
4503 promoted_mode = passed_mode;
4504
4505 /* If this is the last named parameter, do any required setup for
4506 varargs or stdargs. We need to know about the case of this being an
4507 addressable type, in which case we skip the registers it
4508 would have arrived in.
4509
4510 For stdargs, LAST_NAMED will be set for two parameters, the one that
4511 is actually the last named, and the dummy parameter. We only
4512 want to do this action once.
4513
4514 Also, indicate when RTL generation is to be suppressed. */
4515 if (last_named && !varargs_setup)
4516 {
4517 int varargs_pretend_bytes = 0;
4518 targetm.calls.setup_incoming_varargs (&args_so_far, promoted_mode,
4519 passed_type,
4520 &varargs_pretend_bytes, 0);
4521 varargs_setup = 1;
4522
4523 /* If the back-end has requested extra stack space, record how
4524 much is needed. Do not change pretend_args_size otherwise
4525 since it may be nonzero from an earlier partial argument. */
4526 if (varargs_pretend_bytes > 0)
4527 current_function_pretend_args_size = varargs_pretend_bytes;
4528 }
4529
4530 /* Determine parm's home in the stack,
4531 in case it arrives in the stack or we should pretend it did.
4532
4533 Compute the stack position and rtx where the argument arrives
4534 and its size.
4535
4536 There is one complexity here: If this was a parameter that would
4537 have been passed in registers, but wasn't only because it is
4538 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
4539 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
4540 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
4541 0 as it was the previous time. */
4542 in_regs = entry_parm != 0;
4543 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4544 in_regs = 1;
4545 #endif
4546 if (!in_regs && !named_arg)
4547 {
4548 int pretend_named =
4549 targetm.calls.pretend_outgoing_varargs_named (&args_so_far);
4550 if (pretend_named)
4551 {
4552 #ifdef FUNCTION_INCOMING_ARG
4553 in_regs = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
4554 passed_type,
4555 pretend_named) != 0;
4556 #else
4557 in_regs = FUNCTION_ARG (args_so_far, promoted_mode,
4558 passed_type,
4559 pretend_named) != 0;
4560 #endif
4561 }
4562 }
4563
4564 /* If this parameter was passed both in registers and in the stack,
4565 use the copy on the stack. */
4566 if (MUST_PASS_IN_STACK (promoted_mode, passed_type))
4567 entry_parm = 0;
4568
4569 #ifdef FUNCTION_ARG_PARTIAL_NREGS
4570 if (entry_parm)
4571 {
4572 partial = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, promoted_mode,
4573 passed_type, named_arg);
4574 if (partial
4575 /* The caller might already have allocated stack space
4576 for the register parameters. */
4577 && reg_parm_stack_space == 0)
4578 {
4579 /* Part of this argument is passed in registers and part
4580 is passed on the stack. Ask the prologue code to extend
4581 the stack part so that we can recreate the full value.
4582
4583 PRETEND_BYTES is the size of the registers we need to store.
4584 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
4585 stack space that the prologue should allocate.
4586
4587 Internally, gcc assumes that the argument pointer is
4588 aligned to STACK_BOUNDARY bits. This is used both for
4589 alignment optimizations (see init_emit) and to locate
4590 arguments that are aligned to more than PARM_BOUNDARY
4591 bits. We must preserve this invariant by rounding
4592 CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to a stack
4593 boundary. */
4594
4595 /* We assume at most one partial arg, and it must be the first
4596 argument on the stack. */
4597 if (extra_pretend_bytes || current_function_pretend_args_size)
4598 abort ();
4599
4600 pretend_bytes = partial * UNITS_PER_WORD;
4601 current_function_pretend_args_size
4602 = CEIL_ROUND (pretend_bytes, STACK_BYTES);
4603
4604 /* We want to align relative to the actual stack pointer, so
4605 don't include this in the stack size until later. */
4606 extra_pretend_bytes = current_function_pretend_args_size;
4607 }
4608 }
4609 #endif
4610
4611 memset (&locate, 0, sizeof (locate));
4612 locate_and_pad_parm (promoted_mode, passed_type, in_regs,
4613 entry_parm ? partial : 0, fndecl,
4614 &stack_args_size, &locate);
4615 /* Adjust offsets to include the pretend args. */
4616 locate.slot_offset.constant += extra_pretend_bytes - pretend_bytes;
4617 locate.offset.constant += extra_pretend_bytes - pretend_bytes;
4618
4619 {
4620 rtx offset_rtx;
4621
4622 /* If we're passing this arg using a reg, make its stack home
4623 the aligned stack slot. */
4624 if (entry_parm)
4625 offset_rtx = ARGS_SIZE_RTX (locate.slot_offset);
4626 else
4627 offset_rtx = ARGS_SIZE_RTX (locate.offset);
4628
4629 if (offset_rtx == const0_rtx)
4630 stack_parm = gen_rtx_MEM (promoted_mode, internal_arg_pointer);
4631 else
4632 stack_parm = gen_rtx_MEM (promoted_mode,
4633 gen_rtx_PLUS (Pmode,
4634 internal_arg_pointer,
4635 offset_rtx));
4636
4637 set_mem_attributes (stack_parm, parm, 1);
4638 if (entry_parm && MEM_ATTRS (stack_parm)->align < PARM_BOUNDARY)
4639 set_mem_align (stack_parm, PARM_BOUNDARY);
4640
4641 /* Set also REG_ATTRS if parameter was passed in a register. */
4642 if (entry_parm)
4643 set_reg_attrs_for_parm (entry_parm, stack_parm);
4644 }
4645
4646 /* If this parm was passed part in regs and part in memory,
4647 pretend it arrived entirely in memory
4648 by pushing the register-part onto the stack.
4649
4650 In the special case of a DImode or DFmode that is split,
4651 we could put it together in a pseudoreg directly,
4652 but for now that's not worth bothering with. */
4653
4654 if (partial)
4655 {
4656 /* Handle calls that pass values in multiple non-contiguous
4657 locations. The Irix 6 ABI has examples of this. */
4658 if (GET_CODE (entry_parm) == PARALLEL)
4659 emit_group_store (validize_mem (stack_parm), entry_parm,
4660 TREE_TYPE (parm),
4661 int_size_in_bytes (TREE_TYPE (parm)));
4662
4663 else
4664 move_block_from_reg (REGNO (entry_parm), validize_mem (stack_parm),
4665 partial);
4666
4667 entry_parm = stack_parm;
4668 }
4669
4670 /* If we didn't decide this parm came in a register,
4671 by default it came on the stack. */
4672 if (entry_parm == 0)
4673 entry_parm = stack_parm;
4674
4675 /* Record permanently how this parm was passed. */
4676 set_decl_incoming_rtl (parm, entry_parm);
4677
4678 /* If there is actually space on the stack for this parm,
4679 count it in stack_args_size; otherwise set stack_parm to 0
4680 to indicate there is no preallocated stack slot for the parm. */
4681
4682 if (entry_parm == stack_parm
4683 || (GET_CODE (entry_parm) == PARALLEL
4684 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
4685 #if defined (REG_PARM_STACK_SPACE)
4686 /* On some machines, even if a parm value arrives in a register
4687 there is still an (uninitialized) stack slot allocated
4688 for it. */
4689 || REG_PARM_STACK_SPACE (fndecl) > 0
4690 #endif
4691 )
4692 {
4693 stack_args_size.constant += locate.size.constant;
4694 if (locate.size.var)
4695 ADD_PARM_SIZE (stack_args_size, locate.size.var);
4696 }
4697 else
4698 /* No stack slot was pushed for this parm. */
4699 stack_parm = 0;
4700
4701 /* Update info on where next arg arrives in registers. */
4702
4703 FUNCTION_ARG_ADVANCE (args_so_far, promoted_mode,
4704 passed_type, named_arg);
4705
4706 /* If we can't trust the parm stack slot to be aligned enough
4707 for its ultimate type, don't use that slot after entry.
4708 We'll make another stack slot, if we need one. */
4709 {
4710 unsigned int thisparm_boundary
4711 = FUNCTION_ARG_BOUNDARY (promoted_mode, passed_type);
4712
4713 if (GET_MODE_ALIGNMENT (nominal_mode) > thisparm_boundary)
4714 stack_parm = 0;
4715 }
4716
4717 /* If parm was passed in memory, and we need to convert it on entry,
4718 don't store it back in that same slot. */
4719 if (entry_parm == stack_parm
4720 && nominal_mode != BLKmode && nominal_mode != passed_mode)
4721 stack_parm = 0;
4722
4723 /* When an argument is passed in multiple locations, we can't
4724 make use of this information, but we can save some copying if
4725 the whole argument is passed in a single register. */
4726 if (GET_CODE (entry_parm) == PARALLEL
4727 && nominal_mode != BLKmode && passed_mode != BLKmode)
4728 {
4729 int i, len = XVECLEN (entry_parm, 0);
4730
4731 for (i = 0; i < len; i++)
4732 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
4733 && GET_CODE (XEXP (XVECEXP (entry_parm, 0, i), 0)) == REG
4734 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
4735 == passed_mode)
4736 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
4737 {
4738 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
4739 set_decl_incoming_rtl (parm, entry_parm);
4740 break;
4741 }
4742 }
4743
4744 /* ENTRY_PARM is an RTX for the parameter as it arrives,
4745 in the mode in which it arrives.
4746 STACK_PARM is an RTX for a stack slot where the parameter can live
4747 during the function (in case we want to put it there).
4748 STACK_PARM is 0 if no stack slot was pushed for it.
4749
4750 Now output code if necessary to convert ENTRY_PARM to
4751 the type in which this function declares it,
4752 and store that result in an appropriate place,
4753 which may be a pseudo reg, may be STACK_PARM,
4754 or may be a local stack slot if STACK_PARM is 0.
4755
4756 Set DECL_RTL to that place. */
4757
4758 if (GET_CODE (entry_parm) == PARALLEL && nominal_mode != BLKmode
4759 && XVECLEN (entry_parm, 0) > 1)
4760 {
4761 /* Reconstitute objects the size of a register or larger using
4762 register operations instead of the stack. */
4763 rtx parmreg = gen_reg_rtx (nominal_mode);
4764
4765 if (REG_P (parmreg))
4766 {
4767 unsigned int regno = REGNO (parmreg);
4768
4769 emit_group_store (parmreg, entry_parm, TREE_TYPE (parm),
4770 int_size_in_bytes (TREE_TYPE (parm)));
4771 SET_DECL_RTL (parm, parmreg);
4772 loaded_in_reg = 1;
4773
4774 if (regno >= max_parm_reg)
4775 {
4776 rtx *new;
4777 int old_max_parm_reg = max_parm_reg;
4778
4779 /* It's slow to expand this one register at a time,
4780 but it's also rare and we need max_parm_reg to be
4781 precisely correct. */
4782 max_parm_reg = regno + 1;
4783 new = ggc_realloc (parm_reg_stack_loc,
4784 max_parm_reg * sizeof (rtx));
4785 memset (new + old_max_parm_reg, 0,
4786 (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
4787 parm_reg_stack_loc = new;
4788 parm_reg_stack_loc[regno] = stack_parm;
4789 }
4790 }
4791 }
4792
4793 if (nominal_mode == BLKmode
4794 #ifdef BLOCK_REG_PADDING
4795 || (locate.where_pad == (BYTES_BIG_ENDIAN ? upward : downward)
4796 && GET_MODE_SIZE (promoted_mode) < UNITS_PER_WORD)
4797 #endif
4798 || GET_CODE (entry_parm) == PARALLEL)
4799 {
4800 /* If a BLKmode arrives in registers, copy it to a stack slot.
4801 Handle calls that pass values in multiple non-contiguous
4802 locations. The Irix 6 ABI has examples of this. */
4803 if (GET_CODE (entry_parm) == REG
4804 || (GET_CODE (entry_parm) == PARALLEL
4805 && (!loaded_in_reg || !optimize)))
4806 {
4807 int size = int_size_in_bytes (TREE_TYPE (parm));
4808 int size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
4809 rtx mem;
4810
4811 /* Note that we will be storing an integral number of words.
4812 So we have to be careful to ensure that we allocate an
4813 integral number of words. We do this below in the
4814 assign_stack_local if space was not allocated in the argument
4815 list. If it was, this will not work if PARM_BOUNDARY is not
4816 a multiple of BITS_PER_WORD. It isn't clear how to fix this
4817 if it becomes a problem. Exception is when BLKmode arrives
4818 with arguments not conforming to word_mode. */
4819
4820 if (stack_parm == 0)
4821 {
4822 stack_parm = assign_stack_local (BLKmode, size_stored, 0);
4823 PUT_MODE (stack_parm, GET_MODE (entry_parm));
4824 set_mem_attributes (stack_parm, parm, 1);
4825 }
4826 else if (GET_CODE (entry_parm) == PARALLEL
4827 && GET_MODE(entry_parm) == BLKmode)
4828 ;
4829 else if (PARM_BOUNDARY % BITS_PER_WORD != 0)
4830 abort ();
4831
4832 mem = validize_mem (stack_parm);
4833
4834 /* Handle calls that pass values in multiple non-contiguous
4835 locations. The Irix 6 ABI has examples of this. */
4836 if (GET_CODE (entry_parm) == PARALLEL)
4837 emit_group_store (mem, entry_parm, TREE_TYPE (parm), size);
4838
4839 else if (size == 0)
4840 ;
4841
4842 /* If SIZE is that of a mode no bigger than a word, just use
4843 that mode's store operation. */
4844 else if (size <= UNITS_PER_WORD)
4845 {
4846 enum machine_mode mode
4847 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
4848
4849 if (mode != BLKmode
4850 #ifdef BLOCK_REG_PADDING
4851 && (size == UNITS_PER_WORD
4852 || (BLOCK_REG_PADDING (mode, TREE_TYPE (parm), 1)
4853 != (BYTES_BIG_ENDIAN ? upward : downward)))
4854 #endif
4855 )
4856 {
4857 rtx reg = gen_rtx_REG (mode, REGNO (entry_parm));
4858 emit_move_insn (change_address (mem, mode, 0), reg);
4859 }
4860
4861 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
4862 machine must be aligned to the left before storing
4863 to memory. Note that the previous test doesn't
4864 handle all cases (e.g. SIZE == 3). */
4865 else if (size != UNITS_PER_WORD
4866 #ifdef BLOCK_REG_PADDING
4867 && (BLOCK_REG_PADDING (mode, TREE_TYPE (parm), 1)
4868 == downward)
4869 #else
4870 && BYTES_BIG_ENDIAN
4871 #endif
4872 )
4873 {
4874 rtx tem, x;
4875 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4876 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
4877
4878 x = expand_binop (word_mode, ashl_optab, reg,
4879 GEN_INT (by), 0, 1, OPTAB_WIDEN);
4880 tem = change_address (mem, word_mode, 0);
4881 emit_move_insn (tem, x);
4882 }
4883 else
4884 move_block_from_reg (REGNO (entry_parm), mem,
4885 size_stored / UNITS_PER_WORD);
4886 }
4887 else
4888 move_block_from_reg (REGNO (entry_parm), mem,
4889 size_stored / UNITS_PER_WORD);
4890 }
4891 /* If parm is already bound to register pair, don't change
4892 this binding. */
4893 if (! DECL_RTL_SET_P (parm))
4894 SET_DECL_RTL (parm, stack_parm);
4895 }
4896 else if (! ((! optimize
4897 && ! DECL_REGISTER (parm))
4898 || TREE_SIDE_EFFECTS (parm)
4899 /* If -ffloat-store specified, don't put explicit
4900 float variables into registers. */
4901 || (flag_float_store
4902 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
4903 /* Always assign pseudo to structure return or item passed
4904 by invisible reference. */
4905 || passed_pointer || parm == function_result_decl)
4906 {
4907 /* Store the parm in a pseudoregister during the function, but we
4908 may need to do it in a wider mode. */
4909
4910 rtx parmreg;
4911 unsigned int regno, regnoi = 0, regnor = 0;
4912
4913 unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
4914
4915 promoted_nominal_mode
4916 = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
4917
4918 parmreg = gen_reg_rtx (promoted_nominal_mode);
4919 mark_user_reg (parmreg);
4920
4921 /* If this was an item that we received a pointer to, set DECL_RTL
4922 appropriately. */
4923 if (passed_pointer)
4924 {
4925 rtx x = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (passed_type)),
4926 parmreg);
4927 set_mem_attributes (x, parm, 1);
4928 SET_DECL_RTL (parm, x);
4929 }
4930 else
4931 {
4932 SET_DECL_RTL (parm, parmreg);
4933 maybe_set_unchanging (DECL_RTL (parm), parm);
4934 }
4935
4936 /* Copy the value into the register. */
4937 if (nominal_mode != passed_mode
4938 || promoted_nominal_mode != promoted_mode)
4939 {
4940 int save_tree_used;
4941 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
4942 mode, by the caller. We now have to convert it to
4943 NOMINAL_MODE, if different. However, PARMREG may be in
4944 a different mode than NOMINAL_MODE if it is being stored
4945 promoted.
4946
4947 If ENTRY_PARM is a hard register, it might be in a register
4948 not valid for operating in its mode (e.g., an odd-numbered
4949 register for a DFmode). In that case, moves are the only
4950 thing valid, so we can't do a convert from there. This
4951 occurs when the calling sequence allow such misaligned
4952 usages.
4953
4954 In addition, the conversion may involve a call, which could
4955 clobber parameters which haven't been copied to pseudo
4956 registers yet. Therefore, we must first copy the parm to
4957 a pseudo reg here, and save the conversion until after all
4958 parameters have been moved. */
4959
4960 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4961
4962 emit_move_insn (tempreg, validize_mem (entry_parm));
4963
4964 push_to_sequence (conversion_insns);
4965 tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
4966
4967 if (GET_CODE (tempreg) == SUBREG
4968 && GET_MODE (tempreg) == nominal_mode
4969 && GET_CODE (SUBREG_REG (tempreg)) == REG
4970 && nominal_mode == passed_mode
4971 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (entry_parm)
4972 && GET_MODE_SIZE (GET_MODE (tempreg))
4973 < GET_MODE_SIZE (GET_MODE (entry_parm)))
4974 {
4975 /* The argument is already sign/zero extended, so note it
4976 into the subreg. */
4977 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
4978 SUBREG_PROMOTED_UNSIGNED_SET (tempreg, unsignedp);
4979 }
4980
4981 /* TREE_USED gets set erroneously during expand_assignment. */
4982 save_tree_used = TREE_USED (parm);
4983 expand_assignment (parm,
4984 make_tree (nominal_type, tempreg), 0);
4985 TREE_USED (parm) = save_tree_used;
4986 conversion_insns = get_insns ();
4987 did_conversion = 1;
4988 end_sequence ();
4989 }
4990 else
4991 emit_move_insn (parmreg, validize_mem (entry_parm));
4992
4993 /* If we were passed a pointer but the actual value
4994 can safely live in a register, put it in one. */
4995 if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
4996 /* If by-reference argument was promoted, demote it. */
4997 && (TYPE_MODE (TREE_TYPE (parm)) != GET_MODE (DECL_RTL (parm))
4998 || ! ((! optimize
4999 && ! DECL_REGISTER (parm))
5000 || TREE_SIDE_EFFECTS (parm)
5001 /* If -ffloat-store specified, don't put explicit
5002 float variables into registers. */
5003 || (flag_float_store
5004 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))))
5005 {
5006 /* We can't use nominal_mode, because it will have been set to
5007 Pmode above. We must use the actual mode of the parm. */
5008 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
5009 mark_user_reg (parmreg);
5010 if (GET_MODE (parmreg) != GET_MODE (DECL_RTL (parm)))
5011 {
5012 rtx tempreg = gen_reg_rtx (GET_MODE (DECL_RTL (parm)));
5013 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
5014 push_to_sequence (conversion_insns);
5015 emit_move_insn (tempreg, DECL_RTL (parm));
5016 SET_DECL_RTL (parm,
5017 convert_to_mode (GET_MODE (parmreg),
5018 tempreg,
5019 unsigned_p));
5020 emit_move_insn (parmreg, DECL_RTL (parm));
5021 conversion_insns = get_insns();
5022 did_conversion = 1;
5023 end_sequence ();
5024 }
5025 else
5026 emit_move_insn (parmreg, DECL_RTL (parm));
5027 SET_DECL_RTL (parm, parmreg);
5028 /* STACK_PARM is the pointer, not the parm, and PARMREG is
5029 now the parm. */
5030 stack_parm = 0;
5031 }
5032 #ifdef FUNCTION_ARG_CALLEE_COPIES
5033 /* If we are passed an arg by reference and it is our responsibility
5034 to make a copy, do it now.
5035 PASSED_TYPE and PASSED mode now refer to the pointer, not the
5036 original argument, so we must recreate them in the call to
5037 FUNCTION_ARG_CALLEE_COPIES. */
5038 /* ??? Later add code to handle the case that if the argument isn't
5039 modified, don't do the copy. */
5040
5041 else if (passed_pointer
5042 && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
5043 TYPE_MODE (TREE_TYPE (passed_type)),
5044 TREE_TYPE (passed_type),
5045 named_arg)
5046 && ! TREE_ADDRESSABLE (TREE_TYPE (passed_type)))
5047 {
5048 rtx copy;
5049 tree type = TREE_TYPE (passed_type);
5050
5051 /* This sequence may involve a library call perhaps clobbering
5052 registers that haven't been copied to pseudos yet. */
5053
5054 push_to_sequence (conversion_insns);
5055
5056 if (!COMPLETE_TYPE_P (type)
5057 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5058 /* This is a variable sized object. */
5059 copy = gen_rtx_MEM (BLKmode,
5060 allocate_dynamic_stack_space
5061 (expr_size (parm), NULL_RTX,
5062 TYPE_ALIGN (type)));
5063 else
5064 copy = assign_stack_temp (TYPE_MODE (type),
5065 int_size_in_bytes (type), 1);
5066 set_mem_attributes (copy, parm, 1);
5067
5068 store_expr (parm, copy, 0);
5069 emit_move_insn (parmreg, XEXP (copy, 0));
5070 conversion_insns = get_insns ();
5071 did_conversion = 1;
5072 end_sequence ();
5073 }
5074 #endif /* FUNCTION_ARG_CALLEE_COPIES */
5075
5076 /* In any case, record the parm's desired stack location
5077 in case we later discover it must live in the stack.
5078
5079 If it is a COMPLEX value, store the stack location for both
5080 halves. */
5081
5082 if (GET_CODE (parmreg) == CONCAT)
5083 regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
5084 else
5085 regno = REGNO (parmreg);
5086
5087 if (regno >= max_parm_reg)
5088 {
5089 rtx *new;
5090 int old_max_parm_reg = max_parm_reg;
5091
5092 /* It's slow to expand this one register at a time,
5093 but it's also rare and we need max_parm_reg to be
5094 precisely correct. */
5095 max_parm_reg = regno + 1;
5096 new = ggc_realloc (parm_reg_stack_loc,
5097 max_parm_reg * sizeof (rtx));
5098 memset (new + old_max_parm_reg, 0,
5099 (max_parm_reg - old_max_parm_reg) * sizeof (rtx));
5100 parm_reg_stack_loc = new;
5101 }
5102
5103 if (GET_CODE (parmreg) == CONCAT)
5104 {
5105 enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
5106
5107 regnor = REGNO (gen_realpart (submode, parmreg));
5108 regnoi = REGNO (gen_imagpart (submode, parmreg));
5109
5110 if (stack_parm != 0)
5111 {
5112 parm_reg_stack_loc[regnor]
5113 = gen_realpart (submode, stack_parm);
5114 parm_reg_stack_loc[regnoi]
5115 = gen_imagpart (submode, stack_parm);
5116 }
5117 else
5118 {
5119 parm_reg_stack_loc[regnor] = 0;
5120 parm_reg_stack_loc[regnoi] = 0;
5121 }
5122 }
5123 else
5124 parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
5125
5126 /* Mark the register as eliminable if we did no conversion
5127 and it was copied from memory at a fixed offset,
5128 and the arg pointer was not copied to a pseudo-reg.
5129 If the arg pointer is a pseudo reg or the offset formed
5130 an invalid address, such memory-equivalences
5131 as we make here would screw up life analysis for it. */
5132 if (nominal_mode == passed_mode
5133 && ! did_conversion
5134 && stack_parm != 0
5135 && GET_CODE (stack_parm) == MEM
5136 && locate.offset.var == 0
5137 && reg_mentioned_p (virtual_incoming_args_rtx,
5138 XEXP (stack_parm, 0)))
5139 {
5140 rtx linsn = get_last_insn ();
5141 rtx sinsn, set;
5142
5143 /* Mark complex types separately. */
5144 if (GET_CODE (parmreg) == CONCAT)
5145 /* Scan backwards for the set of the real and
5146 imaginary parts. */
5147 for (sinsn = linsn; sinsn != 0;
5148 sinsn = prev_nonnote_insn (sinsn))
5149 {
5150 set = single_set (sinsn);
5151 if (set != 0
5152 && SET_DEST (set) == regno_reg_rtx [regnoi])
5153 REG_NOTES (sinsn)
5154 = gen_rtx_EXPR_LIST (REG_EQUIV,
5155 parm_reg_stack_loc[regnoi],
5156 REG_NOTES (sinsn));
5157 else if (set != 0
5158 && SET_DEST (set) == regno_reg_rtx [regnor])
5159 REG_NOTES (sinsn)
5160 = gen_rtx_EXPR_LIST (REG_EQUIV,
5161 parm_reg_stack_loc[regnor],
5162 REG_NOTES (sinsn));
5163 }
5164 else if ((set = single_set (linsn)) != 0
5165 && SET_DEST (set) == parmreg)
5166 REG_NOTES (linsn)
5167 = gen_rtx_EXPR_LIST (REG_EQUIV,
5168 stack_parm, REG_NOTES (linsn));
5169 }
5170
5171 /* For pointer data type, suggest pointer register. */
5172 if (POINTER_TYPE_P (TREE_TYPE (parm)))
5173 mark_reg_pointer (parmreg,
5174 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
5175
5176 /* If something wants our address, try to use ADDRESSOF. */
5177 if (TREE_ADDRESSABLE (parm))
5178 {
5179 /* If we end up putting something into the stack,
5180 fixup_var_refs_insns will need to make a pass over
5181 all the instructions. It looks through the pending
5182 sequences -- but it can't see the ones in the
5183 CONVERSION_INSNS, if they're not on the sequence
5184 stack. So, we go back to that sequence, just so that
5185 the fixups will happen. */
5186 push_to_sequence (conversion_insns);
5187 put_var_into_stack (parm, /*rescan=*/true);
5188 conversion_insns = get_insns ();
5189 end_sequence ();
5190 }
5191 }
5192 else
5193 {
5194 /* Value must be stored in the stack slot STACK_PARM
5195 during function execution. */
5196
5197 if (promoted_mode != nominal_mode)
5198 {
5199 /* Conversion is required. */
5200 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
5201
5202 emit_move_insn (tempreg, validize_mem (entry_parm));
5203
5204 push_to_sequence (conversion_insns);
5205 entry_parm = convert_to_mode (nominal_mode, tempreg,
5206 TYPE_UNSIGNED (TREE_TYPE (parm)));
5207 if (stack_parm)
5208 /* ??? This may need a big-endian conversion on sparc64. */
5209 stack_parm = adjust_address (stack_parm, nominal_mode, 0);
5210
5211 conversion_insns = get_insns ();
5212 did_conversion = 1;
5213 end_sequence ();
5214 }
5215
5216 if (entry_parm != stack_parm)
5217 {
5218 if (stack_parm == 0)
5219 {
5220 stack_parm
5221 = assign_stack_local (GET_MODE (entry_parm),
5222 GET_MODE_SIZE (GET_MODE (entry_parm)),
5223 0);
5224 set_mem_attributes (stack_parm, parm, 1);
5225 }
5226
5227 if (promoted_mode != nominal_mode)
5228 {
5229 push_to_sequence (conversion_insns);
5230 emit_move_insn (validize_mem (stack_parm),
5231 validize_mem (entry_parm));
5232 conversion_insns = get_insns ();
5233 end_sequence ();
5234 }
5235 else
5236 emit_move_insn (validize_mem (stack_parm),
5237 validize_mem (entry_parm));
5238 }
5239
5240 SET_DECL_RTL (parm, stack_parm);
5241 }
5242 }
5243
5244 if (targetm.calls.split_complex_arg && fnargs != orig_fnargs)
5245 {
5246 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm))
5247 {
5248 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
5249 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
5250 {
5251 rtx tmp, real, imag;
5252 enum machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
5253
5254 real = DECL_RTL (fnargs);
5255 imag = DECL_RTL (TREE_CHAIN (fnargs));
5256 if (inner != GET_MODE (real))
5257 {
5258 real = gen_lowpart_SUBREG (inner, real);
5259 imag = gen_lowpart_SUBREG (inner, imag);
5260 }
5261 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
5262 SET_DECL_RTL (parm, tmp);
5263
5264 real = DECL_INCOMING_RTL (fnargs);
5265 imag = DECL_INCOMING_RTL (TREE_CHAIN (fnargs));
5266 if (inner != GET_MODE (real))
5267 {
5268 real = gen_lowpart_SUBREG (inner, real);
5269 imag = gen_lowpart_SUBREG (inner, imag);
5270 }
5271 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
5272 set_decl_incoming_rtl (parm, tmp);
5273 fnargs = TREE_CHAIN (fnargs);
5274 }
5275 else
5276 {
5277 SET_DECL_RTL (parm, DECL_RTL (fnargs));
5278 set_decl_incoming_rtl (parm, DECL_INCOMING_RTL (fnargs));
5279
5280 /* Set MEM_EXPR to the original decl, i.e. to PARM,
5281 instead of the copy of decl, i.e. FNARGS. */
5282 if (DECL_INCOMING_RTL (parm)
5283 && GET_CODE (DECL_INCOMING_RTL (parm)) == MEM)
5284 set_mem_expr (DECL_INCOMING_RTL (parm), parm);
5285 }
5286 fnargs = TREE_CHAIN (fnargs);
5287 }
5288 }
5289
5290 /* Output all parameter conversion instructions (possibly including calls)
5291 now that all parameters have been copied out of hard registers. */
5292 emit_insn (conversion_insns);
5293
5294 /* If we are receiving a struct value address as the first argument, set up
5295 the RTL for the function result. As this might require code to convert
5296 the transmitted address to Pmode, we do this here to ensure that possible
5297 preliminary conversions of the address have been emitted already. */
5298 if (function_result_decl)
5299 {
5300 tree result = DECL_RESULT (fndecl);
5301 rtx addr = DECL_RTL (function_result_decl);
5302 rtx x;
5303
5304 addr = convert_memory_address (Pmode, addr);
5305 x = gen_rtx_MEM (DECL_MODE (result), addr);
5306 set_mem_attributes (x, result, 1);
5307 SET_DECL_RTL (result, x);
5308 }
5309
5310 last_parm_insn = get_last_insn ();
5311
5312 /* We have aligned all the args, so add space for the pretend args. */
5313 stack_args_size.constant += extra_pretend_bytes;
5314 current_function_args_size = stack_args_size.constant;
5315
5316 /* Adjust function incoming argument size for alignment and
5317 minimum length. */
5318
5319 #ifdef REG_PARM_STACK_SPACE
5320 current_function_args_size = MAX (current_function_args_size,
5321 REG_PARM_STACK_SPACE (fndecl));
5322 #endif
5323
5324 current_function_args_size
5325 = ((current_function_args_size + STACK_BYTES - 1)
5326 / STACK_BYTES) * STACK_BYTES;
5327
5328 #ifdef ARGS_GROW_DOWNWARD
5329 current_function_arg_offset_rtx
5330 = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
5331 : expand_expr (size_diffop (stack_args_size.var,
5332 size_int (-stack_args_size.constant)),
5333 NULL_RTX, VOIDmode, 0));
5334 #else
5335 current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
5336 #endif
5337
5338 /* See how many bytes, if any, of its args a function should try to pop
5339 on return. */
5340
5341 current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
5342 current_function_args_size);
5343
5344 /* For stdarg.h function, save info about
5345 regs and stack space used by the named args. */
5346
5347 current_function_args_info = args_so_far;
5348
5349 /* Set the rtx used for the function return value. Put this in its
5350 own variable so any optimizers that need this information don't have
5351 to include tree.h. Do this here so it gets done when an inlined
5352 function gets output. */
5353
5354 current_function_return_rtx
5355 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
5356 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
5357
5358 /* If scalar return value was computed in a pseudo-reg, or was a named
5359 return value that got dumped to the stack, copy that to the hard
5360 return register. */
5361 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
5362 {
5363 tree decl_result = DECL_RESULT (fndecl);
5364 rtx decl_rtl = DECL_RTL (decl_result);
5365
5366 if (REG_P (decl_rtl)
5367 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5368 : DECL_REGISTER (decl_result))
5369 {
5370 rtx real_decl_rtl;
5371
5372 #ifdef FUNCTION_OUTGOING_VALUE
5373 real_decl_rtl = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl_result),
5374 fndecl);
5375 #else
5376 real_decl_rtl = FUNCTION_VALUE (TREE_TYPE (decl_result),
5377 fndecl);
5378 #endif
5379 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
5380 /* The delay slot scheduler assumes that current_function_return_rtx
5381 holds the hard register containing the return value, not a
5382 temporary pseudo. */
5383 current_function_return_rtx = real_decl_rtl;
5384 }
5385 }
5386 }
5387
5388 /* If ARGS contains entries with complex types, split the entry into two
5389 entries of the component type. Return a new list of substitutions are
5390 needed, else the old list. */
5391
5392 static tree
5393 split_complex_args (tree args)
5394 {
5395 tree p;
5396
5397 /* Before allocating memory, check for the common case of no complex. */
5398 for (p = args; p; p = TREE_CHAIN (p))
5399 {
5400 tree type = TREE_TYPE (p);
5401 if (TREE_CODE (type) == COMPLEX_TYPE
5402 && targetm.calls.split_complex_arg (type))
5403 goto found;
5404 }
5405 return args;
5406
5407 found:
5408 args = copy_list (args);
5409
5410 for (p = args; p; p = TREE_CHAIN (p))
5411 {
5412 tree type = TREE_TYPE (p);
5413 if (TREE_CODE (type) == COMPLEX_TYPE
5414 && targetm.calls.split_complex_arg (type))
5415 {
5416 tree decl;
5417 tree subtype = TREE_TYPE (type);
5418
5419 /* Rewrite the PARM_DECL's type with its component. */
5420 TREE_TYPE (p) = subtype;
5421 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
5422 DECL_MODE (p) = VOIDmode;
5423 DECL_SIZE (p) = NULL;
5424 DECL_SIZE_UNIT (p) = NULL;
5425 layout_decl (p, 0);
5426
5427 /* Build a second synthetic decl. */
5428 decl = build_decl (PARM_DECL, NULL_TREE, subtype);
5429 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
5430 layout_decl (decl, 0);
5431
5432 /* Splice it in; skip the new decl. */
5433 TREE_CHAIN (decl) = TREE_CHAIN (p);
5434 TREE_CHAIN (p) = decl;
5435 p = decl;
5436 }
5437 }
5438
5439 return args;
5440 }
5441 \f
5442 /* Indicate whether REGNO is an incoming argument to the current function
5443 that was promoted to a wider mode. If so, return the RTX for the
5444 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
5445 that REGNO is promoted from and whether the promotion was signed or
5446 unsigned. */
5447
5448 rtx
5449 promoted_input_arg (unsigned int regno, enum machine_mode *pmode, int *punsignedp)
5450 {
5451 tree arg;
5452
5453 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
5454 arg = TREE_CHAIN (arg))
5455 if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
5456 && REGNO (DECL_INCOMING_RTL (arg)) == regno
5457 && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
5458 {
5459 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
5460 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (arg));
5461
5462 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
5463 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
5464 && mode != DECL_MODE (arg))
5465 {
5466 *pmode = DECL_MODE (arg);
5467 *punsignedp = unsignedp;
5468 return DECL_INCOMING_RTL (arg);
5469 }
5470 }
5471
5472 return 0;
5473 }
5474
5475 \f
5476 /* Compute the size and offset from the start of the stacked arguments for a
5477 parm passed in mode PASSED_MODE and with type TYPE.
5478
5479 INITIAL_OFFSET_PTR points to the current offset into the stacked
5480 arguments.
5481
5482 The starting offset and size for this parm are returned in
5483 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
5484 nonzero, the offset is that of stack slot, which is returned in
5485 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
5486 padding required from the initial offset ptr to the stack slot.
5487
5488 IN_REGS is nonzero if the argument will be passed in registers. It will
5489 never be set if REG_PARM_STACK_SPACE is not defined.
5490
5491 FNDECL is the function in which the argument was defined.
5492
5493 There are two types of rounding that are done. The first, controlled by
5494 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
5495 list to be aligned to the specific boundary (in bits). This rounding
5496 affects the initial and starting offsets, but not the argument size.
5497
5498 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
5499 optionally rounds the size of the parm to PARM_BOUNDARY. The
5500 initial offset is not affected by this rounding, while the size always
5501 is and the starting offset may be. */
5502
5503 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
5504 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
5505 callers pass in the total size of args so far as
5506 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
5507
5508 void
5509 locate_and_pad_parm (enum machine_mode passed_mode, tree type, int in_regs,
5510 int partial, tree fndecl ATTRIBUTE_UNUSED,
5511 struct args_size *initial_offset_ptr,
5512 struct locate_and_pad_arg_data *locate)
5513 {
5514 tree sizetree;
5515 enum direction where_pad;
5516 int boundary;
5517 int reg_parm_stack_space = 0;
5518 int part_size_in_regs;
5519
5520 #ifdef REG_PARM_STACK_SPACE
5521 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
5522
5523 /* If we have found a stack parm before we reach the end of the
5524 area reserved for registers, skip that area. */
5525 if (! in_regs)
5526 {
5527 if (reg_parm_stack_space > 0)
5528 {
5529 if (initial_offset_ptr->var)
5530 {
5531 initial_offset_ptr->var
5532 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
5533 ssize_int (reg_parm_stack_space));
5534 initial_offset_ptr->constant = 0;
5535 }
5536 else if (initial_offset_ptr->constant < reg_parm_stack_space)
5537 initial_offset_ptr->constant = reg_parm_stack_space;
5538 }
5539 }
5540 #endif /* REG_PARM_STACK_SPACE */
5541
5542 part_size_in_regs = 0;
5543 if (reg_parm_stack_space == 0)
5544 part_size_in_regs = ((partial * UNITS_PER_WORD)
5545 / (PARM_BOUNDARY / BITS_PER_UNIT)
5546 * (PARM_BOUNDARY / BITS_PER_UNIT));
5547
5548 sizetree
5549 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
5550 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
5551 boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
5552 locate->where_pad = where_pad;
5553
5554 #ifdef ARGS_GROW_DOWNWARD
5555 locate->slot_offset.constant = -initial_offset_ptr->constant;
5556 if (initial_offset_ptr->var)
5557 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
5558 initial_offset_ptr->var);
5559
5560 {
5561 tree s2 = sizetree;
5562 if (where_pad != none
5563 && (!host_integerp (sizetree, 1)
5564 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5565 s2 = round_up (s2, PARM_BOUNDARY / BITS_PER_UNIT);
5566 SUB_PARM_SIZE (locate->slot_offset, s2);
5567 }
5568
5569 locate->slot_offset.constant += part_size_in_regs;
5570
5571 if (!in_regs
5572 #ifdef REG_PARM_STACK_SPACE
5573 || REG_PARM_STACK_SPACE (fndecl) > 0
5574 #endif
5575 )
5576 pad_to_arg_alignment (&locate->slot_offset, boundary,
5577 &locate->alignment_pad);
5578
5579 locate->size.constant = (-initial_offset_ptr->constant
5580 - locate->slot_offset.constant);
5581 if (initial_offset_ptr->var)
5582 locate->size.var = size_binop (MINUS_EXPR,
5583 size_binop (MINUS_EXPR,
5584 ssize_int (0),
5585 initial_offset_ptr->var),
5586 locate->slot_offset.var);
5587
5588 /* Pad_below needs the pre-rounded size to know how much to pad
5589 below. */
5590 locate->offset = locate->slot_offset;
5591 if (where_pad == downward)
5592 pad_below (&locate->offset, passed_mode, sizetree);
5593
5594 #else /* !ARGS_GROW_DOWNWARD */
5595 if (!in_regs
5596 #ifdef REG_PARM_STACK_SPACE
5597 || REG_PARM_STACK_SPACE (fndecl) > 0
5598 #endif
5599 )
5600 pad_to_arg_alignment (initial_offset_ptr, boundary,
5601 &locate->alignment_pad);
5602 locate->slot_offset = *initial_offset_ptr;
5603
5604 #ifdef PUSH_ROUNDING
5605 if (passed_mode != BLKmode)
5606 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
5607 #endif
5608
5609 /* Pad_below needs the pre-rounded size to know how much to pad below
5610 so this must be done before rounding up. */
5611 locate->offset = locate->slot_offset;
5612 if (where_pad == downward)
5613 pad_below (&locate->offset, passed_mode, sizetree);
5614
5615 if (where_pad != none
5616 && (!host_integerp (sizetree, 1)
5617 || (tree_low_cst (sizetree, 1) * BITS_PER_UNIT) % PARM_BOUNDARY))
5618 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5619
5620 ADD_PARM_SIZE (locate->size, sizetree);
5621
5622 locate->size.constant -= part_size_in_regs;
5623 #endif /* ARGS_GROW_DOWNWARD */
5624 }
5625
5626 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
5627 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
5628
5629 static void
5630 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
5631 struct args_size *alignment_pad)
5632 {
5633 tree save_var = NULL_TREE;
5634 HOST_WIDE_INT save_constant = 0;
5635 int boundary_in_bytes = boundary / BITS_PER_UNIT;
5636 HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
5637
5638 #ifdef SPARC_STACK_BOUNDARY_HACK
5639 /* The sparc port has a bug. It sometimes claims a STACK_BOUNDARY
5640 higher than the real alignment of %sp. However, when it does this,
5641 the alignment of %sp+STACK_POINTER_OFFSET will be STACK_BOUNDARY.
5642 This is a temporary hack while the sparc port is fixed. */
5643 if (SPARC_STACK_BOUNDARY_HACK)
5644 sp_offset = 0;
5645 #endif
5646
5647 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5648 {
5649 save_var = offset_ptr->var;
5650 save_constant = offset_ptr->constant;
5651 }
5652
5653 alignment_pad->var = NULL_TREE;
5654 alignment_pad->constant = 0;
5655
5656 if (boundary > BITS_PER_UNIT)
5657 {
5658 if (offset_ptr->var)
5659 {
5660 tree sp_offset_tree = ssize_int (sp_offset);
5661 tree offset = size_binop (PLUS_EXPR,
5662 ARGS_SIZE_TREE (*offset_ptr),
5663 sp_offset_tree);
5664 #ifdef ARGS_GROW_DOWNWARD
5665 tree rounded = round_down (offset, boundary / BITS_PER_UNIT);
5666 #else
5667 tree rounded = round_up (offset, boundary / BITS_PER_UNIT);
5668 #endif
5669
5670 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
5671 /* ARGS_SIZE_TREE includes constant term. */
5672 offset_ptr->constant = 0;
5673 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5674 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
5675 save_var);
5676 }
5677 else
5678 {
5679 offset_ptr->constant = -sp_offset +
5680 #ifdef ARGS_GROW_DOWNWARD
5681 FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
5682 #else
5683 CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes);
5684 #endif
5685 if (boundary > PARM_BOUNDARY && boundary > STACK_BOUNDARY)
5686 alignment_pad->constant = offset_ptr->constant - save_constant;
5687 }
5688 }
5689 }
5690
5691 static void
5692 pad_below (struct args_size *offset_ptr, enum machine_mode passed_mode, tree sizetree)
5693 {
5694 if (passed_mode != BLKmode)
5695 {
5696 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
5697 offset_ptr->constant
5698 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
5699 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
5700 - GET_MODE_SIZE (passed_mode));
5701 }
5702 else
5703 {
5704 if (TREE_CODE (sizetree) != INTEGER_CST
5705 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
5706 {
5707 /* Round the size up to multiple of PARM_BOUNDARY bits. */
5708 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
5709 /* Add it in. */
5710 ADD_PARM_SIZE (*offset_ptr, s2);
5711 SUB_PARM_SIZE (*offset_ptr, sizetree);
5712 }
5713 }
5714 }
5715 \f
5716 /* Walk the tree of blocks describing the binding levels within a function
5717 and warn about uninitialized variables.
5718 This is done after calling flow_analysis and before global_alloc
5719 clobbers the pseudo-regs to hard regs. */
5720
5721 void
5722 uninitialized_vars_warning (tree block)
5723 {
5724 tree decl, sub;
5725 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5726 {
5727 if (warn_uninitialized
5728 && TREE_CODE (decl) == VAR_DECL
5729 /* These warnings are unreliable for and aggregates
5730 because assigning the fields one by one can fail to convince
5731 flow.c that the entire aggregate was initialized.
5732 Unions are troublesome because members may be shorter. */
5733 && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
5734 && DECL_RTL_SET_P (decl)
5735 && GET_CODE (DECL_RTL (decl)) == REG
5736 /* Global optimizations can make it difficult to determine if a
5737 particular variable has been initialized. However, a VAR_DECL
5738 with a nonzero DECL_INITIAL had an initializer, so do not
5739 claim it is potentially uninitialized.
5740
5741 When the DECL_INITIAL is NULL call the language hook to tell us
5742 if we want to warn. */
5743 && (DECL_INITIAL (decl) == NULL_TREE || lang_hooks.decl_uninit (decl))
5744 && regno_uninitialized (REGNO (DECL_RTL (decl))))
5745 warning ("%J'%D' might be used uninitialized in this function",
5746 decl, decl);
5747 if (extra_warnings
5748 && TREE_CODE (decl) == VAR_DECL
5749 && DECL_RTL_SET_P (decl)
5750 && GET_CODE (DECL_RTL (decl)) == REG
5751 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5752 warning ("%Jvariable '%D' might be clobbered by `longjmp' or `vfork'",
5753 decl, decl);
5754 }
5755 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5756 uninitialized_vars_warning (sub);
5757 }
5758
5759 /* Do the appropriate part of uninitialized_vars_warning
5760 but for arguments instead of local variables. */
5761
5762 void
5763 setjmp_args_warning (void)
5764 {
5765 tree decl;
5766 for (decl = DECL_ARGUMENTS (current_function_decl);
5767 decl; decl = TREE_CHAIN (decl))
5768 if (DECL_RTL (decl) != 0
5769 && GET_CODE (DECL_RTL (decl)) == REG
5770 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
5771 warning ("%Jargument '%D' might be clobbered by `longjmp' or `vfork'",
5772 decl, decl);
5773 }
5774
5775 /* If this function call setjmp, put all vars into the stack
5776 unless they were declared `register'. */
5777
5778 void
5779 setjmp_protect (tree block)
5780 {
5781 tree decl, sub;
5782 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
5783 if ((TREE_CODE (decl) == VAR_DECL
5784 || TREE_CODE (decl) == PARM_DECL)
5785 && DECL_RTL (decl) != 0
5786 && (GET_CODE (DECL_RTL (decl)) == REG
5787 || (GET_CODE (DECL_RTL (decl)) == MEM
5788 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5789 /* If this variable came from an inline function, it must be
5790 that its life doesn't overlap the setjmp. If there was a
5791 setjmp in the function, it would already be in memory. We
5792 must exclude such variable because their DECL_RTL might be
5793 set to strange things such as virtual_stack_vars_rtx. */
5794 && ! DECL_FROM_INLINE (decl)
5795 && (
5796 #ifdef NON_SAVING_SETJMP
5797 /* If longjmp doesn't restore the registers,
5798 don't put anything in them. */
5799 NON_SAVING_SETJMP
5800 ||
5801 #endif
5802 ! DECL_REGISTER (decl)))
5803 put_var_into_stack (decl, /*rescan=*/true);
5804 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
5805 setjmp_protect (sub);
5806 }
5807 \f
5808 /* Like the previous function, but for args instead of local variables. */
5809
5810 void
5811 setjmp_protect_args (void)
5812 {
5813 tree decl;
5814 for (decl = DECL_ARGUMENTS (current_function_decl);
5815 decl; decl = TREE_CHAIN (decl))
5816 if ((TREE_CODE (decl) == VAR_DECL
5817 || TREE_CODE (decl) == PARM_DECL)
5818 && DECL_RTL (decl) != 0
5819 && (GET_CODE (DECL_RTL (decl)) == REG
5820 || (GET_CODE (DECL_RTL (decl)) == MEM
5821 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == ADDRESSOF))
5822 && (
5823 /* If longjmp doesn't restore the registers,
5824 don't put anything in them. */
5825 #ifdef NON_SAVING_SETJMP
5826 NON_SAVING_SETJMP
5827 ||
5828 #endif
5829 ! DECL_REGISTER (decl)))
5830 put_var_into_stack (decl, /*rescan=*/true);
5831 }
5832 \f
5833 /* Return the context-pointer register corresponding to DECL,
5834 or 0 if it does not need one. */
5835
5836 rtx
5837 lookup_static_chain (tree decl)
5838 {
5839 tree context = decl_function_context (decl);
5840 tree link;
5841
5842 if (context == 0
5843 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_NO_STATIC_CHAIN (decl)))
5844 return 0;
5845
5846 /* We treat inline_function_decl as an alias for the current function
5847 because that is the inline function whose vars, types, etc.
5848 are being merged into the current function.
5849 See expand_inline_function. */
5850 if (context == current_function_decl || context == inline_function_decl)
5851 return virtual_stack_vars_rtx;
5852
5853 for (link = context_display; link; link = TREE_CHAIN (link))
5854 if (TREE_PURPOSE (link) == context)
5855 return RTL_EXPR_RTL (TREE_VALUE (link));
5856
5857 abort ();
5858 }
5859 \f
5860 /* Convert a stack slot address ADDR for variable VAR
5861 (from a containing function)
5862 into an address valid in this function (using a static chain). */
5863
5864 rtx
5865 fix_lexical_addr (rtx addr, tree var)
5866 {
5867 rtx basereg;
5868 HOST_WIDE_INT displacement;
5869 tree context = decl_function_context (var);
5870 struct function *fp;
5871 rtx base = 0;
5872
5873 /* If this is the present function, we need not do anything. */
5874 if (context == current_function_decl || context == inline_function_decl)
5875 return addr;
5876
5877 fp = find_function_data (context);
5878
5879 if (GET_CODE (addr) == ADDRESSOF && GET_CODE (XEXP (addr, 0)) == MEM)
5880 addr = XEXP (XEXP (addr, 0), 0);
5881
5882 /* Decode given address as base reg plus displacement. */
5883 if (GET_CODE (addr) == REG)
5884 basereg = addr, displacement = 0;
5885 else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
5886 basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
5887 else
5888 abort ();
5889
5890 /* We accept vars reached via the containing function's
5891 incoming arg pointer and via its stack variables pointer. */
5892 if (basereg == fp->internal_arg_pointer)
5893 {
5894 /* If reached via arg pointer, get the arg pointer value
5895 out of that function's stack frame.
5896
5897 There are two cases: If a separate ap is needed, allocate a
5898 slot in the outer function for it and dereference it that way.
5899 This is correct even if the real ap is actually a pseudo.
5900 Otherwise, just adjust the offset from the frame pointer to
5901 compensate. */
5902
5903 #ifdef NEED_SEPARATE_AP
5904 rtx addr;
5905
5906 addr = get_arg_pointer_save_area (fp);
5907 addr = fix_lexical_addr (XEXP (addr, 0), var);
5908 addr = memory_address (Pmode, addr);
5909
5910 base = gen_rtx_MEM (Pmode, addr);
5911 set_mem_alias_set (base, get_frame_alias_set ());
5912 base = copy_to_reg (base);
5913 #else
5914 displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
5915 base = lookup_static_chain (var);
5916 #endif
5917 }
5918
5919 else if (basereg == virtual_stack_vars_rtx)
5920 {
5921 /* This is the same code as lookup_static_chain, duplicated here to
5922 avoid an extra call to decl_function_context. */
5923 tree link;
5924
5925 for (link = context_display; link; link = TREE_CHAIN (link))
5926 if (TREE_PURPOSE (link) == context)
5927 {
5928 base = RTL_EXPR_RTL (TREE_VALUE (link));
5929 break;
5930 }
5931 }
5932
5933 if (base == 0)
5934 abort ();
5935
5936 /* Use same offset, relative to appropriate static chain or argument
5937 pointer. */
5938 return plus_constant (base, displacement);
5939 }
5940 \f
5941 /* Return the address of the trampoline for entering nested fn FUNCTION.
5942 If necessary, allocate a trampoline (in the stack frame)
5943 and emit rtl to initialize its contents (at entry to this function). */
5944
5945 rtx
5946 trampoline_address (tree function)
5947 {
5948 tree link;
5949 tree rtlexp;
5950 rtx tramp;
5951 struct function *fp;
5952 tree fn_context;
5953
5954 /* Find an existing trampoline and return it. */
5955 for (link = trampoline_list; link; link = TREE_CHAIN (link))
5956 if (TREE_PURPOSE (link) == function)
5957 return
5958 adjust_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
5959
5960 for (fp = outer_function_chain; fp; fp = fp->outer)
5961 for (link = fp->x_trampoline_list; link; link = TREE_CHAIN (link))
5962 if (TREE_PURPOSE (link) == function)
5963 {
5964 tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
5965 function);
5966 return adjust_trampoline_addr (tramp);
5967 }
5968
5969 /* None exists; we must make one. */
5970
5971 /* Find the `struct function' for the function containing FUNCTION. */
5972 fp = 0;
5973 fn_context = decl_function_context (function);
5974 if (fn_context != current_function_decl
5975 && fn_context != inline_function_decl)
5976 fp = find_function_data (fn_context);
5977
5978 /* Allocate run-time space for this trampoline. */
5979 /* If rounding needed, allocate extra space
5980 to ensure we have TRAMPOLINE_SIZE bytes left after rounding up. */
5981 #define TRAMPOLINE_REAL_SIZE \
5982 (TRAMPOLINE_SIZE + (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT) - 1)
5983 tramp = assign_stack_local_1 (BLKmode, TRAMPOLINE_REAL_SIZE, 0,
5984 fp ? fp : cfun);
5985 /* Record the trampoline for reuse and note it for later initialization
5986 by expand_function_end. */
5987 if (fp != 0)
5988 {
5989 rtlexp = make_node (RTL_EXPR);
5990 RTL_EXPR_RTL (rtlexp) = tramp;
5991 fp->x_trampoline_list = tree_cons (function, rtlexp,
5992 fp->x_trampoline_list);
5993 }
5994 else
5995 {
5996 /* Make the RTL_EXPR node temporary, not momentary, so that the
5997 trampoline_list doesn't become garbage. */
5998 rtlexp = make_node (RTL_EXPR);
5999
6000 RTL_EXPR_RTL (rtlexp) = tramp;
6001 trampoline_list = tree_cons (function, rtlexp, trampoline_list);
6002 }
6003
6004 tramp = fix_lexical_addr (XEXP (tramp, 0), function);
6005 return adjust_trampoline_addr (tramp);
6006 }
6007
6008 /* Given a trampoline address,
6009 round it to multiple of TRAMPOLINE_ALIGNMENT. */
6010
6011 static rtx
6012 round_trampoline_addr (rtx tramp)
6013 {
6014 /* Round address up to desired boundary. */
6015 rtx temp = gen_reg_rtx (Pmode);
6016 rtx addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
6017 rtx mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
6018
6019 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
6020 temp, 0, OPTAB_LIB_WIDEN);
6021 tramp = expand_simple_binop (Pmode, AND, temp, mask,
6022 temp, 0, OPTAB_LIB_WIDEN);
6023
6024 return tramp;
6025 }
6026
6027 /* Given a trampoline address, round it then apply any
6028 platform-specific adjustments so that the result can be used for a
6029 function call . */
6030
6031 static rtx
6032 adjust_trampoline_addr (rtx tramp)
6033 {
6034 tramp = round_trampoline_addr (tramp);
6035 #ifdef TRAMPOLINE_ADJUST_ADDRESS
6036 TRAMPOLINE_ADJUST_ADDRESS (tramp);
6037 #endif
6038 return tramp;
6039 }
6040 \f
6041 /* Put all this function's BLOCK nodes including those that are chained
6042 onto the first block into a vector, and return it.
6043 Also store in each NOTE for the beginning or end of a block
6044 the index of that block in the vector.
6045 The arguments are BLOCK, the chain of top-level blocks of the function,
6046 and INSNS, the insn chain of the function. */
6047
6048 void
6049 identify_blocks (void)
6050 {
6051 int n_blocks;
6052 tree *block_vector, *last_block_vector;
6053 tree *block_stack;
6054 tree block = DECL_INITIAL (current_function_decl);
6055
6056 if (block == 0)
6057 return;
6058
6059 /* Fill the BLOCK_VECTOR with all of the BLOCKs in this function, in
6060 depth-first order. */
6061 block_vector = get_block_vector (block, &n_blocks);
6062 block_stack = xmalloc (n_blocks * sizeof (tree));
6063
6064 last_block_vector = identify_blocks_1 (get_insns (),
6065 block_vector + 1,
6066 block_vector + n_blocks,
6067 block_stack);
6068
6069 /* If we didn't use all of the subblocks, we've misplaced block notes. */
6070 /* ??? This appears to happen all the time. Latent bugs elsewhere? */
6071 if (0 && last_block_vector != block_vector + n_blocks)
6072 abort ();
6073
6074 free (block_vector);
6075 free (block_stack);
6076 }
6077
6078 /* Subroutine of identify_blocks. Do the block substitution on the
6079 insn chain beginning with INSNS. Recurse for CALL_PLACEHOLDER chains.
6080
6081 BLOCK_STACK is pushed and popped for each BLOCK_BEGIN/BLOCK_END pair.
6082 BLOCK_VECTOR is incremented for each block seen. */
6083
6084 static tree *
6085 identify_blocks_1 (rtx insns, tree *block_vector, tree *end_block_vector,
6086 tree *orig_block_stack)
6087 {
6088 rtx insn;
6089 tree *block_stack = orig_block_stack;
6090
6091 for (insn = insns; insn; insn = NEXT_INSN (insn))
6092 {
6093 if (GET_CODE (insn) == NOTE)
6094 {
6095 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
6096 {
6097 tree b;
6098
6099 /* If there are more block notes than BLOCKs, something
6100 is badly wrong. */
6101 if (block_vector == end_block_vector)
6102 abort ();
6103
6104 b = *block_vector++;
6105 NOTE_BLOCK (insn) = b;
6106 *block_stack++ = b;
6107 }
6108 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
6109 {
6110 /* If there are more NOTE_INSN_BLOCK_ENDs than
6111 NOTE_INSN_BLOCK_BEGs, something is badly wrong. */
6112 if (block_stack == orig_block_stack)
6113 abort ();
6114
6115 NOTE_BLOCK (insn) = *--block_stack;
6116 }
6117 }
6118 else if (GET_CODE (insn) == CALL_INSN
6119 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
6120 {
6121 rtx cp = PATTERN (insn);
6122
6123 block_vector = identify_blocks_1 (XEXP (cp, 0), block_vector,
6124 end_block_vector, block_stack);
6125 if (XEXP (cp, 1))
6126 block_vector = identify_blocks_1 (XEXP (cp, 1), block_vector,
6127 end_block_vector, block_stack);
6128 if (XEXP (cp, 2))
6129 block_vector = identify_blocks_1 (XEXP (cp, 2), block_vector,
6130 end_block_vector, block_stack);
6131 }
6132 }
6133
6134 /* If there are more NOTE_INSN_BLOCK_BEGINs than NOTE_INSN_BLOCK_ENDs,
6135 something is badly wrong. */
6136 if (block_stack != orig_block_stack)
6137 abort ();
6138
6139 return block_vector;
6140 }
6141
6142 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
6143 and create duplicate blocks. */
6144 /* ??? Need an option to either create block fragments or to create
6145 abstract origin duplicates of a source block. It really depends
6146 on what optimization has been performed. */
6147
6148 void
6149 reorder_blocks (void)
6150 {
6151 tree block = DECL_INITIAL (current_function_decl);
6152 varray_type block_stack;
6153
6154 if (block == NULL_TREE)
6155 return;
6156
6157 VARRAY_TREE_INIT (block_stack, 10, "block_stack");
6158
6159 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
6160 reorder_blocks_0 (block);
6161
6162 /* Prune the old trees away, so that they don't get in the way. */
6163 BLOCK_SUBBLOCKS (block) = NULL_TREE;
6164 BLOCK_CHAIN (block) = NULL_TREE;
6165
6166 /* Recreate the block tree from the note nesting. */
6167 reorder_blocks_1 (get_insns (), block, &block_stack);
6168 BLOCK_SUBBLOCKS (block) = blocks_nreverse (BLOCK_SUBBLOCKS (block));
6169
6170 /* Remove deleted blocks from the block fragment chains. */
6171 reorder_fix_fragments (block);
6172 }
6173
6174 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
6175
6176 static void
6177 reorder_blocks_0 (tree block)
6178 {
6179 while (block)
6180 {
6181 TREE_ASM_WRITTEN (block) = 0;
6182 reorder_blocks_0 (BLOCK_SUBBLOCKS (block));
6183 block = BLOCK_CHAIN (block);
6184 }
6185 }
6186
6187 static void
6188 reorder_blocks_1 (rtx insns, tree current_block, varray_type *p_block_stack)
6189 {
6190 rtx insn;
6191
6192 for (insn = insns; insn; insn = NEXT_INSN (insn))
6193 {
6194 if (GET_CODE (insn) == NOTE)
6195 {
6196 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
6197 {
6198 tree block = NOTE_BLOCK (insn);
6199
6200 /* If we have seen this block before, that means it now
6201 spans multiple address regions. Create a new fragment. */
6202 if (TREE_ASM_WRITTEN (block))
6203 {
6204 tree new_block = copy_node (block);
6205 tree origin;
6206
6207 origin = (BLOCK_FRAGMENT_ORIGIN (block)
6208 ? BLOCK_FRAGMENT_ORIGIN (block)
6209 : block);
6210 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
6211 BLOCK_FRAGMENT_CHAIN (new_block)
6212 = BLOCK_FRAGMENT_CHAIN (origin);
6213 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
6214
6215 NOTE_BLOCK (insn) = new_block;
6216 block = new_block;
6217 }
6218
6219 BLOCK_SUBBLOCKS (block) = 0;
6220 TREE_ASM_WRITTEN (block) = 1;
6221 /* When there's only one block for the entire function,
6222 current_block == block and we mustn't do this, it
6223 will cause infinite recursion. */
6224 if (block != current_block)
6225 {
6226 BLOCK_SUPERCONTEXT (block) = current_block;
6227 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
6228 BLOCK_SUBBLOCKS (current_block) = block;
6229 current_block = block;
6230 }
6231 VARRAY_PUSH_TREE (*p_block_stack, block);
6232 }
6233 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
6234 {
6235 NOTE_BLOCK (insn) = VARRAY_TOP_TREE (*p_block_stack);
6236 VARRAY_POP (*p_block_stack);
6237 BLOCK_SUBBLOCKS (current_block)
6238 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
6239 current_block = BLOCK_SUPERCONTEXT (current_block);
6240 }
6241 }
6242 else if (GET_CODE (insn) == CALL_INSN
6243 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
6244 {
6245 rtx cp = PATTERN (insn);
6246 reorder_blocks_1 (XEXP (cp, 0), current_block, p_block_stack);
6247 if (XEXP (cp, 1))
6248 reorder_blocks_1 (XEXP (cp, 1), current_block, p_block_stack);
6249 if (XEXP (cp, 2))
6250 reorder_blocks_1 (XEXP (cp, 2), current_block, p_block_stack);
6251 }
6252 }
6253 }
6254
6255 /* Rationalize BLOCK_FRAGMENT_ORIGIN. If an origin block no longer
6256 appears in the block tree, select one of the fragments to become
6257 the new origin block. */
6258
6259 static void
6260 reorder_fix_fragments (tree block)
6261 {
6262 while (block)
6263 {
6264 tree dup_origin = BLOCK_FRAGMENT_ORIGIN (block);
6265 tree new_origin = NULL_TREE;
6266
6267 if (dup_origin)
6268 {
6269 if (! TREE_ASM_WRITTEN (dup_origin))
6270 {
6271 new_origin = BLOCK_FRAGMENT_CHAIN (dup_origin);
6272
6273 /* Find the first of the remaining fragments. There must
6274 be at least one -- the current block. */
6275 while (! TREE_ASM_WRITTEN (new_origin))
6276 new_origin = BLOCK_FRAGMENT_CHAIN (new_origin);
6277 BLOCK_FRAGMENT_ORIGIN (new_origin) = NULL_TREE;
6278 }
6279 }
6280 else if (! dup_origin)
6281 new_origin = block;
6282
6283 /* Re-root the rest of the fragments to the new origin. In the
6284 case that DUP_ORIGIN was null, that means BLOCK was the origin
6285 of a chain of fragments and we want to remove those fragments
6286 that didn't make it to the output. */
6287 if (new_origin)
6288 {
6289 tree *pp = &BLOCK_FRAGMENT_CHAIN (new_origin);
6290 tree chain = *pp;
6291
6292 while (chain)
6293 {
6294 if (TREE_ASM_WRITTEN (chain))
6295 {
6296 BLOCK_FRAGMENT_ORIGIN (chain) = new_origin;
6297 *pp = chain;
6298 pp = &BLOCK_FRAGMENT_CHAIN (chain);
6299 }
6300 chain = BLOCK_FRAGMENT_CHAIN (chain);
6301 }
6302 *pp = NULL_TREE;
6303 }
6304
6305 reorder_fix_fragments (BLOCK_SUBBLOCKS (block));
6306 block = BLOCK_CHAIN (block);
6307 }
6308 }
6309
6310 /* Reverse the order of elements in the chain T of blocks,
6311 and return the new head of the chain (old last element). */
6312
6313 static tree
6314 blocks_nreverse (tree t)
6315 {
6316 tree prev = 0, decl, next;
6317 for (decl = t; decl; decl = next)
6318 {
6319 next = BLOCK_CHAIN (decl);
6320 BLOCK_CHAIN (decl) = prev;
6321 prev = decl;
6322 }
6323 return prev;
6324 }
6325
6326 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
6327 non-NULL, list them all into VECTOR, in a depth-first preorder
6328 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
6329 blocks. */
6330
6331 static int
6332 all_blocks (tree block, tree *vector)
6333 {
6334 int n_blocks = 0;
6335
6336 while (block)
6337 {
6338 TREE_ASM_WRITTEN (block) = 0;
6339
6340 /* Record this block. */
6341 if (vector)
6342 vector[n_blocks] = block;
6343
6344 ++n_blocks;
6345
6346 /* Record the subblocks, and their subblocks... */
6347 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
6348 vector ? vector + n_blocks : 0);
6349 block = BLOCK_CHAIN (block);
6350 }
6351
6352 return n_blocks;
6353 }
6354
6355 /* Return a vector containing all the blocks rooted at BLOCK. The
6356 number of elements in the vector is stored in N_BLOCKS_P. The
6357 vector is dynamically allocated; it is the caller's responsibility
6358 to call `free' on the pointer returned. */
6359
6360 static tree *
6361 get_block_vector (tree block, int *n_blocks_p)
6362 {
6363 tree *block_vector;
6364
6365 *n_blocks_p = all_blocks (block, NULL);
6366 block_vector = xmalloc (*n_blocks_p * sizeof (tree));
6367 all_blocks (block, block_vector);
6368
6369 return block_vector;
6370 }
6371
6372 static GTY(()) int next_block_index = 2;
6373
6374 /* Set BLOCK_NUMBER for all the blocks in FN. */
6375
6376 void
6377 number_blocks (tree fn)
6378 {
6379 int i;
6380 int n_blocks;
6381 tree *block_vector;
6382
6383 /* For SDB and XCOFF debugging output, we start numbering the blocks
6384 from 1 within each function, rather than keeping a running
6385 count. */
6386 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
6387 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
6388 next_block_index = 1;
6389 #endif
6390
6391 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
6392
6393 /* The top-level BLOCK isn't numbered at all. */
6394 for (i = 1; i < n_blocks; ++i)
6395 /* We number the blocks from two. */
6396 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
6397
6398 free (block_vector);
6399
6400 return;
6401 }
6402
6403 /* If VAR is present in a subblock of BLOCK, return the subblock. */
6404
6405 tree
6406 debug_find_var_in_block_tree (tree var, tree block)
6407 {
6408 tree t;
6409
6410 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
6411 if (t == var)
6412 return block;
6413
6414 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
6415 {
6416 tree ret = debug_find_var_in_block_tree (var, t);
6417 if (ret)
6418 return ret;
6419 }
6420
6421 return NULL_TREE;
6422 }
6423 \f
6424 /* Allocate a function structure for FNDECL and set its contents
6425 to the defaults. */
6426
6427 void
6428 allocate_struct_function (tree fndecl)
6429 {
6430 tree result;
6431
6432 cfun = ggc_alloc_cleared (sizeof (struct function));
6433
6434 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
6435
6436 cfun->stack_alignment_needed = STACK_BOUNDARY;
6437 cfun->preferred_stack_boundary = STACK_BOUNDARY;
6438
6439 current_function_funcdef_no = funcdef_no++;
6440
6441 cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
6442
6443 init_stmt_for_function ();
6444 init_eh_for_function ();
6445
6446 lang_hooks.function.init (cfun);
6447 if (init_machine_status)
6448 cfun->machine = (*init_machine_status) ();
6449
6450 if (fndecl == NULL)
6451 return;
6452
6453 DECL_STRUCT_FUNCTION (fndecl) = cfun;
6454 cfun->decl = fndecl;
6455
6456 result = DECL_RESULT (fndecl);
6457 if (aggregate_value_p (result, fndecl))
6458 {
6459 #ifdef PCC_STATIC_STRUCT_RETURN
6460 current_function_returns_pcc_struct = 1;
6461 #endif
6462 current_function_returns_struct = 1;
6463 }
6464
6465 current_function_returns_pointer = POINTER_TYPE_P (TREE_TYPE (result));
6466
6467 current_function_needs_context
6468 = (decl_function_context (current_function_decl) != 0
6469 && ! DECL_NO_STATIC_CHAIN (current_function_decl));
6470 }
6471
6472 /* Reset cfun, and other non-struct-function variables to defaults as
6473 appropriate for emitting rtl at the start of a function. */
6474
6475 static void
6476 prepare_function_start (tree fndecl)
6477 {
6478 if (fndecl && DECL_STRUCT_FUNCTION (fndecl))
6479 cfun = DECL_STRUCT_FUNCTION (fndecl);
6480 else
6481 allocate_struct_function (fndecl);
6482 init_emit ();
6483 init_varasm_status (cfun);
6484 init_expr ();
6485
6486 cse_not_expected = ! optimize;
6487
6488 /* Caller save not needed yet. */
6489 caller_save_needed = 0;
6490
6491 /* We haven't done register allocation yet. */
6492 reg_renumber = 0;
6493
6494 /* Indicate that we need to distinguish between the return value of the
6495 present function and the return value of a function being called. */
6496 rtx_equal_function_value_matters = 1;
6497
6498 /* Indicate that we have not instantiated virtual registers yet. */
6499 virtuals_instantiated = 0;
6500
6501 /* Indicate that we want CONCATs now. */
6502 generating_concat_p = 1;
6503
6504 /* Indicate we have no need of a frame pointer yet. */
6505 frame_pointer_needed = 0;
6506 }
6507
6508 /* Initialize the rtl expansion mechanism so that we can do simple things
6509 like generate sequences. This is used to provide a context during global
6510 initialization of some passes. */
6511 void
6512 init_dummy_function_start (void)
6513 {
6514 prepare_function_start (NULL);
6515 }
6516
6517 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
6518 and initialize static variables for generating RTL for the statements
6519 of the function. */
6520
6521 void
6522 init_function_start (tree subr)
6523 {
6524 prepare_function_start (subr);
6525
6526 /* Within function body, compute a type's size as soon it is laid out. */
6527 immediate_size_expand++;
6528
6529 /* Prevent ever trying to delete the first instruction of a
6530 function. Also tell final how to output a linenum before the
6531 function prologue. Note linenums could be missing, e.g. when
6532 compiling a Java .class file. */
6533 if (DECL_SOURCE_LINE (subr))
6534 emit_line_note (DECL_SOURCE_LOCATION (subr));
6535
6536 /* Make sure first insn is a note even if we don't want linenums.
6537 This makes sure the first insn will never be deleted.
6538 Also, final expects a note to appear there. */
6539 emit_note (NOTE_INSN_DELETED);
6540
6541 /* Warn if this value is an aggregate type,
6542 regardless of which calling convention we are using for it. */
6543 if (warn_aggregate_return
6544 && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
6545 warning ("function returns an aggregate");
6546 }
6547
6548 /* Make sure all values used by the optimization passes have sane
6549 defaults. */
6550 void
6551 init_function_for_compilation (void)
6552 {
6553 reg_renumber = 0;
6554
6555 /* No prologue/epilogue insns yet. */
6556 VARRAY_GROW (prologue, 0);
6557 VARRAY_GROW (epilogue, 0);
6558 VARRAY_GROW (sibcall_epilogue, 0);
6559 }
6560
6561 /* Expand a call to __main at the beginning of a possible main function. */
6562
6563 #if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
6564 #undef HAS_INIT_SECTION
6565 #define HAS_INIT_SECTION
6566 #endif
6567
6568 void
6569 expand_main_function (void)
6570 {
6571 #ifdef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
6572 if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN)
6573 {
6574 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
6575 rtx tmp, seq;
6576
6577 start_sequence ();
6578 /* Forcibly align the stack. */
6579 #ifdef STACK_GROWS_DOWNWARD
6580 tmp = expand_simple_binop (Pmode, AND, stack_pointer_rtx, GEN_INT(-align),
6581 stack_pointer_rtx, 1, OPTAB_WIDEN);
6582 #else
6583 tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
6584 GEN_INT (align - 1), NULL_RTX, 1, OPTAB_WIDEN);
6585 tmp = expand_simple_binop (Pmode, AND, tmp, GEN_INT (-align),
6586 stack_pointer_rtx, 1, OPTAB_WIDEN);
6587 #endif
6588 if (tmp != stack_pointer_rtx)
6589 emit_move_insn (stack_pointer_rtx, tmp);
6590
6591 /* Enlist allocate_dynamic_stack_space to pick up the pieces. */
6592 tmp = force_reg (Pmode, const0_rtx);
6593 allocate_dynamic_stack_space (tmp, NULL_RTX, BIGGEST_ALIGNMENT);
6594 seq = get_insns ();
6595 end_sequence ();
6596
6597 for (tmp = get_last_insn (); tmp; tmp = PREV_INSN (tmp))
6598 if (NOTE_P (tmp) && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_FUNCTION_BEG)
6599 break;
6600 if (tmp)
6601 emit_insn_before (seq, tmp);
6602 else
6603 emit_insn (seq);
6604 }
6605 #endif
6606
6607 #ifndef HAS_INIT_SECTION
6608 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
6609 #endif
6610 }
6611 \f
6612 /* The PENDING_SIZES represent the sizes of variable-sized types.
6613 Create RTL for the various sizes now (using temporary variables),
6614 so that we can refer to the sizes from the RTL we are generating
6615 for the current function. The PENDING_SIZES are a TREE_LIST. The
6616 TREE_VALUE of each node is a SAVE_EXPR. */
6617
6618 void
6619 expand_pending_sizes (tree pending_sizes)
6620 {
6621 tree tem;
6622
6623 /* Evaluate now the sizes of any types declared among the arguments. */
6624 for (tem = pending_sizes; tem; tem = TREE_CHAIN (tem))
6625 {
6626 expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode, 0);
6627 /* Flush the queue in case this parameter declaration has
6628 side-effects. */
6629 emit_queue ();
6630 }
6631 }
6632
6633 /* Start the RTL for a new function, and set variables used for
6634 emitting RTL.
6635 SUBR is the FUNCTION_DECL node.
6636 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
6637 the function's parameters, which must be run at any return statement. */
6638
6639 void
6640 expand_function_start (tree subr, int parms_have_cleanups)
6641 {
6642 tree tem;
6643 rtx last_ptr = NULL_RTX;
6644
6645 /* Make sure volatile mem refs aren't considered
6646 valid operands of arithmetic insns. */
6647 init_recog_no_volatile ();
6648
6649 current_function_instrument_entry_exit
6650 = (flag_instrument_function_entry_exit
6651 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6652
6653 current_function_profile
6654 = (profile_flag
6655 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
6656
6657 current_function_limit_stack
6658 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
6659
6660 /* If function gets a static chain arg, store it in the stack frame.
6661 Do this first, so it gets the first stack slot offset. */
6662 if (current_function_needs_context)
6663 {
6664 last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
6665
6666 /* Delay copying static chain if it is not a register to avoid
6667 conflicts with regs used for parameters. */
6668 if (! SMALL_REGISTER_CLASSES
6669 || GET_CODE (static_chain_incoming_rtx) == REG)
6670 emit_move_insn (last_ptr, static_chain_incoming_rtx);
6671 }
6672
6673 /* If the parameters of this function need cleaning up, get a label
6674 for the beginning of the code which executes those cleanups. This must
6675 be done before doing anything with return_label. */
6676 if (parms_have_cleanups)
6677 cleanup_label = gen_label_rtx ();
6678 else
6679 cleanup_label = 0;
6680
6681 /* Make the label for return statements to jump to. Do not special
6682 case machines with special return instructions -- they will be
6683 handled later during jump, ifcvt, or epilogue creation. */
6684 return_label = gen_label_rtx ();
6685
6686 /* Initialize rtx used to return the value. */
6687 /* Do this before assign_parms so that we copy the struct value address
6688 before any library calls that assign parms might generate. */
6689
6690 /* Decide whether to return the value in memory or in a register. */
6691 if (aggregate_value_p (DECL_RESULT (subr), subr))
6692 {
6693 /* Returning something that won't go in a register. */
6694 rtx value_address = 0;
6695
6696 #ifdef PCC_STATIC_STRUCT_RETURN
6697 if (current_function_returns_pcc_struct)
6698 {
6699 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
6700 value_address = assemble_static_space (size);
6701 }
6702 else
6703 #endif
6704 {
6705 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 1);
6706 /* Expect to be passed the address of a place to store the value.
6707 If it is passed as an argument, assign_parms will take care of
6708 it. */
6709 if (sv)
6710 {
6711 value_address = gen_reg_rtx (Pmode);
6712 emit_move_insn (value_address, sv);
6713 }
6714 }
6715 if (value_address)
6716 {
6717 rtx x = gen_rtx_MEM (DECL_MODE (DECL_RESULT (subr)), value_address);
6718 set_mem_attributes (x, DECL_RESULT (subr), 1);
6719 SET_DECL_RTL (DECL_RESULT (subr), x);
6720 }
6721 }
6722 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
6723 /* If return mode is void, this decl rtl should not be used. */
6724 SET_DECL_RTL (DECL_RESULT (subr), NULL_RTX);
6725 else
6726 {
6727 /* Compute the return values into a pseudo reg, which we will copy
6728 into the true return register after the cleanups are done. */
6729
6730 /* In order to figure out what mode to use for the pseudo, we
6731 figure out what the mode of the eventual return register will
6732 actually be, and use that. */
6733 rtx hard_reg
6734 = hard_function_value (TREE_TYPE (DECL_RESULT (subr)),
6735 subr, 1);
6736
6737 /* Structures that are returned in registers are not aggregate_value_p,
6738 so we may see a PARALLEL or a REG. */
6739 if (REG_P (hard_reg))
6740 SET_DECL_RTL (DECL_RESULT (subr), gen_reg_rtx (GET_MODE (hard_reg)));
6741 else if (GET_CODE (hard_reg) == PARALLEL)
6742 SET_DECL_RTL (DECL_RESULT (subr), gen_group_rtx (hard_reg));
6743 else
6744 abort ();
6745
6746 /* Set DECL_REGISTER flag so that expand_function_end will copy the
6747 result to the real return register(s). */
6748 DECL_REGISTER (DECL_RESULT (subr)) = 1;
6749 }
6750
6751 /* Initialize rtx for parameters and local variables.
6752 In some cases this requires emitting insns. */
6753
6754 assign_parms (subr);
6755
6756 /* Copy the static chain now if it wasn't a register. The delay is to
6757 avoid conflicts with the parameter passing registers. */
6758
6759 if (SMALL_REGISTER_CLASSES && current_function_needs_context)
6760 if (GET_CODE (static_chain_incoming_rtx) != REG)
6761 emit_move_insn (last_ptr, static_chain_incoming_rtx);
6762
6763 /* The following was moved from init_function_start.
6764 The move is supposed to make sdb output more accurate. */
6765 /* Indicate the beginning of the function body,
6766 as opposed to parm setup. */
6767 emit_note (NOTE_INSN_FUNCTION_BEG);
6768
6769 if (GET_CODE (get_last_insn ()) != NOTE)
6770 emit_note (NOTE_INSN_DELETED);
6771 parm_birth_insn = get_last_insn ();
6772
6773 context_display = 0;
6774 if (current_function_needs_context)
6775 {
6776 /* Fetch static chain values for containing functions. */
6777 tem = decl_function_context (current_function_decl);
6778 /* Copy the static chain pointer into a pseudo. If we have
6779 small register classes, copy the value from memory if
6780 static_chain_incoming_rtx is a REG. */
6781 if (tem)
6782 {
6783 /* If the static chain originally came in a register, put it back
6784 there, then move it out in the next insn. The reason for
6785 this peculiar code is to satisfy function integration. */
6786 if (SMALL_REGISTER_CLASSES
6787 && GET_CODE (static_chain_incoming_rtx) == REG)
6788 emit_move_insn (static_chain_incoming_rtx, last_ptr);
6789 last_ptr = copy_to_reg (static_chain_incoming_rtx);
6790 }
6791
6792 while (tem)
6793 {
6794 tree rtlexp = make_node (RTL_EXPR);
6795
6796 RTL_EXPR_RTL (rtlexp) = last_ptr;
6797 context_display = tree_cons (tem, rtlexp, context_display);
6798 tem = decl_function_context (tem);
6799 if (tem == 0)
6800 break;
6801 /* Chain through stack frames, assuming pointer to next lexical frame
6802 is found at the place we always store it. */
6803 #ifdef FRAME_GROWS_DOWNWARD
6804 last_ptr = plus_constant (last_ptr,
6805 -(HOST_WIDE_INT) GET_MODE_SIZE (Pmode));
6806 #endif
6807 last_ptr = gen_rtx_MEM (Pmode, memory_address (Pmode, last_ptr));
6808 set_mem_alias_set (last_ptr, get_frame_alias_set ());
6809 last_ptr = copy_to_reg (last_ptr);
6810
6811 /* If we are not optimizing, ensure that we know that this
6812 piece of context is live over the entire function. */
6813 if (! optimize)
6814 save_expr_regs = gen_rtx_EXPR_LIST (VOIDmode, last_ptr,
6815 save_expr_regs);
6816 }
6817 }
6818
6819 if (current_function_instrument_entry_exit)
6820 {
6821 rtx fun = DECL_RTL (current_function_decl);
6822 if (GET_CODE (fun) == MEM)
6823 fun = XEXP (fun, 0);
6824 else
6825 abort ();
6826 emit_library_call (profile_function_entry_libfunc, LCT_NORMAL, VOIDmode,
6827 2, fun, Pmode,
6828 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
6829 0,
6830 hard_frame_pointer_rtx),
6831 Pmode);
6832 }
6833
6834 if (current_function_profile)
6835 {
6836 #ifdef PROFILE_HOOK
6837 PROFILE_HOOK (current_function_funcdef_no);
6838 #endif
6839 }
6840
6841 /* After the display initializations is where the tail-recursion label
6842 should go, if we end up needing one. Ensure we have a NOTE here
6843 since some things (like trampolines) get placed before this. */
6844 tail_recursion_reentry = emit_note (NOTE_INSN_DELETED);
6845
6846 /* Evaluate now the sizes of any types declared among the arguments. */
6847 expand_pending_sizes (nreverse (get_pending_sizes ()));
6848
6849 /* Make sure there is a line number after the function entry setup code. */
6850 force_next_line_note ();
6851 }
6852 \f
6853 /* Undo the effects of init_dummy_function_start. */
6854 void
6855 expand_dummy_function_end (void)
6856 {
6857 /* End any sequences that failed to be closed due to syntax errors. */
6858 while (in_sequence_p ())
6859 end_sequence ();
6860
6861 /* Outside function body, can't compute type's actual size
6862 until next function's body starts. */
6863
6864 free_after_parsing (cfun);
6865 free_after_compilation (cfun);
6866 cfun = 0;
6867 }
6868
6869 /* Call DOIT for each hard register used as a return value from
6870 the current function. */
6871
6872 void
6873 diddle_return_value (void (*doit) (rtx, void *), void *arg)
6874 {
6875 rtx outgoing = current_function_return_rtx;
6876
6877 if (! outgoing)
6878 return;
6879
6880 if (GET_CODE (outgoing) == REG)
6881 (*doit) (outgoing, arg);
6882 else if (GET_CODE (outgoing) == PARALLEL)
6883 {
6884 int i;
6885
6886 for (i = 0; i < XVECLEN (outgoing, 0); i++)
6887 {
6888 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
6889
6890 if (GET_CODE (x) == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
6891 (*doit) (x, arg);
6892 }
6893 }
6894 }
6895
6896 static void
6897 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
6898 {
6899 emit_insn (gen_rtx_CLOBBER (VOIDmode, reg));
6900 }
6901
6902 void
6903 clobber_return_register (void)
6904 {
6905 diddle_return_value (do_clobber_return_reg, NULL);
6906
6907 /* In case we do use pseudo to return value, clobber it too. */
6908 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
6909 {
6910 tree decl_result = DECL_RESULT (current_function_decl);
6911 rtx decl_rtl = DECL_RTL (decl_result);
6912 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
6913 {
6914 do_clobber_return_reg (decl_rtl, NULL);
6915 }
6916 }
6917 }
6918
6919 static void
6920 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
6921 {
6922 emit_insn (gen_rtx_USE (VOIDmode, reg));
6923 }
6924
6925 void
6926 use_return_register (void)
6927 {
6928 diddle_return_value (do_use_return_reg, NULL);
6929 }
6930
6931 /* Possibly warn about unused parameters. */
6932 void
6933 do_warn_unused_parameter (tree fn)
6934 {
6935 tree decl;
6936
6937 for (decl = DECL_ARGUMENTS (fn);
6938 decl; decl = TREE_CHAIN (decl))
6939 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
6940 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl))
6941 warning ("%Junused parameter '%D'", decl, decl);
6942 }
6943
6944 static GTY(()) rtx initial_trampoline;
6945
6946 /* Generate RTL for the end of the current function. */
6947
6948 void
6949 expand_function_end (void)
6950 {
6951 tree link;
6952 rtx clobber_after;
6953
6954 finish_expr_for_function ();
6955
6956 /* If arg_pointer_save_area was referenced only from a nested
6957 function, we will not have initialized it yet. Do that now. */
6958 if (arg_pointer_save_area && ! cfun->arg_pointer_save_area_init)
6959 get_arg_pointer_save_area (cfun);
6960
6961 #ifdef NON_SAVING_SETJMP
6962 /* Don't put any variables in registers if we call setjmp
6963 on a machine that fails to restore the registers. */
6964 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
6965 {
6966 if (DECL_INITIAL (current_function_decl) != error_mark_node)
6967 setjmp_protect (DECL_INITIAL (current_function_decl));
6968
6969 setjmp_protect_args ();
6970 }
6971 #endif
6972
6973 /* Initialize any trampolines required by this function. */
6974 for (link = trampoline_list; link; link = TREE_CHAIN (link))
6975 {
6976 tree function = TREE_PURPOSE (link);
6977 rtx context ATTRIBUTE_UNUSED = lookup_static_chain (function);
6978 rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
6979 #ifdef TRAMPOLINE_TEMPLATE
6980 rtx blktramp;
6981 #endif
6982 rtx seq;
6983
6984 #ifdef TRAMPOLINE_TEMPLATE
6985 /* First make sure this compilation has a template for
6986 initializing trampolines. */
6987 if (initial_trampoline == 0)
6988 {
6989 initial_trampoline
6990 = gen_rtx_MEM (BLKmode, assemble_trampoline_template ());
6991 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
6992 }
6993 #endif
6994
6995 /* Generate insns to initialize the trampoline. */
6996 start_sequence ();
6997 tramp = round_trampoline_addr (XEXP (tramp, 0));
6998 #ifdef TRAMPOLINE_TEMPLATE
6999 blktramp = replace_equiv_address (initial_trampoline, tramp);
7000 emit_block_move (blktramp, initial_trampoline,
7001 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
7002 #endif
7003 trampolines_created = 1;
7004 INITIALIZE_TRAMPOLINE (tramp, XEXP (DECL_RTL (function), 0), context);
7005 seq = get_insns ();
7006 end_sequence ();
7007
7008 /* Put those insns at entry to the containing function (this one). */
7009 emit_insn_before (seq, tail_recursion_reentry);
7010 }
7011
7012 /* If we are doing stack checking and this function makes calls,
7013 do a stack probe at the start of the function to ensure we have enough
7014 space for another stack frame. */
7015 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
7016 {
7017 rtx insn, seq;
7018
7019 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
7020 if (GET_CODE (insn) == CALL_INSN)
7021 {
7022 start_sequence ();
7023 probe_stack_range (STACK_CHECK_PROTECT,
7024 GEN_INT (STACK_CHECK_MAX_FRAME_SIZE));
7025 seq = get_insns ();
7026 end_sequence ();
7027 emit_insn_before (seq, tail_recursion_reentry);
7028 break;
7029 }
7030 }
7031
7032 /* Possibly warn about unused parameters.
7033 When frontend does unit-at-a-time, the warning is already
7034 issued at finalization time. */
7035 if (warn_unused_parameter
7036 && !lang_hooks.callgraph.expand_function)
7037 do_warn_unused_parameter (current_function_decl);
7038
7039 /* Delete handlers for nonlocal gotos if nothing uses them. */
7040 if (nonlocal_goto_handler_slots != 0
7041 && ! current_function_has_nonlocal_label)
7042 delete_handlers ();
7043
7044 /* End any sequences that failed to be closed due to syntax errors. */
7045 while (in_sequence_p ())
7046 end_sequence ();
7047
7048 /* Outside function body, can't compute type's actual size
7049 until next function's body starts. */
7050 immediate_size_expand--;
7051
7052 clear_pending_stack_adjust ();
7053 do_pending_stack_adjust ();
7054
7055 /* @@@ This is a kludge. We want to ensure that instructions that
7056 may trap are not moved into the epilogue by scheduling, because
7057 we don't always emit unwind information for the epilogue.
7058 However, not all machine descriptions define a blockage insn, so
7059 emit an ASM_INPUT to act as one. */
7060 if (flag_non_call_exceptions)
7061 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
7062
7063 /* Mark the end of the function body.
7064 If control reaches this insn, the function can drop through
7065 without returning a value. */
7066 emit_note (NOTE_INSN_FUNCTION_END);
7067
7068 /* Must mark the last line number note in the function, so that the test
7069 coverage code can avoid counting the last line twice. This just tells
7070 the code to ignore the immediately following line note, since there
7071 already exists a copy of this note somewhere above. This line number
7072 note is still needed for debugging though, so we can't delete it. */
7073 if (flag_test_coverage)
7074 emit_note (NOTE_INSN_REPEATED_LINE_NUMBER);
7075
7076 /* Output a linenumber for the end of the function.
7077 SDB depends on this. */
7078 force_next_line_note ();
7079 emit_line_note (input_location);
7080
7081 /* Before the return label (if any), clobber the return
7082 registers so that they are not propagated live to the rest of
7083 the function. This can only happen with functions that drop
7084 through; if there had been a return statement, there would
7085 have either been a return rtx, or a jump to the return label.
7086
7087 We delay actual code generation after the current_function_value_rtx
7088 is computed. */
7089 clobber_after = get_last_insn ();
7090
7091 /* Output the label for the actual return from the function,
7092 if one is expected. This happens either because a function epilogue
7093 is used instead of a return instruction, or because a return was done
7094 with a goto in order to run local cleanups, or because of pcc-style
7095 structure returning. */
7096 if (return_label)
7097 emit_label (return_label);
7098
7099 if (current_function_instrument_entry_exit)
7100 {
7101 rtx fun = DECL_RTL (current_function_decl);
7102 if (GET_CODE (fun) == MEM)
7103 fun = XEXP (fun, 0);
7104 else
7105 abort ();
7106 emit_library_call (profile_function_exit_libfunc, LCT_NORMAL, VOIDmode,
7107 2, fun, Pmode,
7108 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
7109 0,
7110 hard_frame_pointer_rtx),
7111 Pmode);
7112 }
7113
7114 /* Let except.c know where it should emit the call to unregister
7115 the function context for sjlj exceptions. */
7116 if (flag_exceptions && USING_SJLJ_EXCEPTIONS)
7117 sjlj_emit_function_exit_after (get_last_insn ());
7118
7119 /* If we had calls to alloca, and this machine needs
7120 an accurate stack pointer to exit the function,
7121 insert some code to save and restore the stack pointer. */
7122 if (! EXIT_IGNORE_STACK
7123 && current_function_calls_alloca)
7124 {
7125 rtx tem = 0;
7126
7127 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
7128 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
7129 }
7130
7131 /* If scalar return value was computed in a pseudo-reg, or was a named
7132 return value that got dumped to the stack, copy that to the hard
7133 return register. */
7134 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
7135 {
7136 tree decl_result = DECL_RESULT (current_function_decl);
7137 rtx decl_rtl = DECL_RTL (decl_result);
7138
7139 if (REG_P (decl_rtl)
7140 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
7141 : DECL_REGISTER (decl_result))
7142 {
7143 rtx real_decl_rtl = current_function_return_rtx;
7144
7145 /* This should be set in assign_parms. */
7146 if (! REG_FUNCTION_VALUE_P (real_decl_rtl))
7147 abort ();
7148
7149 /* If this is a BLKmode structure being returned in registers,
7150 then use the mode computed in expand_return. Note that if
7151 decl_rtl is memory, then its mode may have been changed,
7152 but that current_function_return_rtx has not. */
7153 if (GET_MODE (real_decl_rtl) == BLKmode)
7154 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
7155
7156 /* If a named return value dumped decl_return to memory, then
7157 we may need to re-do the PROMOTE_MODE signed/unsigned
7158 extension. */
7159 if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
7160 {
7161 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
7162
7163 if (targetm.calls.promote_function_return (TREE_TYPE (current_function_decl)))
7164 promote_mode (TREE_TYPE (decl_result), GET_MODE (decl_rtl),
7165 &unsignedp, 1);
7166
7167 convert_move (real_decl_rtl, decl_rtl, unsignedp);
7168 }
7169 else if (GET_CODE (real_decl_rtl) == PARALLEL)
7170 {
7171 /* If expand_function_start has created a PARALLEL for decl_rtl,
7172 move the result to the real return registers. Otherwise, do
7173 a group load from decl_rtl for a named return. */
7174 if (GET_CODE (decl_rtl) == PARALLEL)
7175 emit_group_move (real_decl_rtl, decl_rtl);
7176 else
7177 emit_group_load (real_decl_rtl, decl_rtl,
7178 TREE_TYPE (decl_result),
7179 int_size_in_bytes (TREE_TYPE (decl_result)));
7180 }
7181 else
7182 emit_move_insn (real_decl_rtl, decl_rtl);
7183 }
7184 }
7185
7186 /* If returning a structure, arrange to return the address of the value
7187 in a place where debuggers expect to find it.
7188
7189 If returning a structure PCC style,
7190 the caller also depends on this value.
7191 And current_function_returns_pcc_struct is not necessarily set. */
7192 if (current_function_returns_struct
7193 || current_function_returns_pcc_struct)
7194 {
7195 rtx value_address
7196 = XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
7197 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
7198 #ifdef FUNCTION_OUTGOING_VALUE
7199 rtx outgoing
7200 = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
7201 current_function_decl);
7202 #else
7203 rtx outgoing
7204 = FUNCTION_VALUE (build_pointer_type (type), current_function_decl);
7205 #endif
7206
7207 /* Mark this as a function return value so integrate will delete the
7208 assignment and USE below when inlining this function. */
7209 REG_FUNCTION_VALUE_P (outgoing) = 1;
7210
7211 /* The address may be ptr_mode and OUTGOING may be Pmode. */
7212 value_address = convert_memory_address (GET_MODE (outgoing),
7213 value_address);
7214
7215 emit_move_insn (outgoing, value_address);
7216
7217 /* Show return register used to hold result (in this case the address
7218 of the result. */
7219 current_function_return_rtx = outgoing;
7220 }
7221
7222 /* If this is an implementation of throw, do what's necessary to
7223 communicate between __builtin_eh_return and the epilogue. */
7224 expand_eh_return ();
7225
7226 /* Emit the actual code to clobber return register. */
7227 {
7228 rtx seq, after;
7229
7230 start_sequence ();
7231 clobber_return_register ();
7232 seq = get_insns ();
7233 end_sequence ();
7234
7235 after = emit_insn_after (seq, clobber_after);
7236
7237 if (clobber_after != after)
7238 cfun->x_clobber_return_insn = after;
7239 }
7240
7241 /* Output the label for the naked return from the function, if one is
7242 expected. This is currently used only by __builtin_return. */
7243 if (naked_return_label)
7244 emit_label (naked_return_label);
7245
7246 /* ??? This should no longer be necessary since stupid is no longer with
7247 us, but there are some parts of the compiler (eg reload_combine, and
7248 sh mach_dep_reorg) that still try and compute their own lifetime info
7249 instead of using the general framework. */
7250 use_return_register ();
7251
7252 /* Fix up any gotos that jumped out to the outermost
7253 binding level of the function.
7254 Must follow emitting RETURN_LABEL. */
7255
7256 /* If you have any cleanups to do at this point,
7257 and they need to create temporary variables,
7258 then you will lose. */
7259 expand_fixups (get_insns ());
7260 }
7261
7262 rtx
7263 get_arg_pointer_save_area (struct function *f)
7264 {
7265 rtx ret = f->x_arg_pointer_save_area;
7266
7267 if (! ret)
7268 {
7269 ret = assign_stack_local_1 (Pmode, GET_MODE_SIZE (Pmode), 0, f);
7270 f->x_arg_pointer_save_area = ret;
7271 }
7272
7273 if (f == cfun && ! f->arg_pointer_save_area_init)
7274 {
7275 rtx seq;
7276
7277 /* Save the arg pointer at the beginning of the function. The
7278 generated stack slot may not be a valid memory address, so we
7279 have to check it and fix it if necessary. */
7280 start_sequence ();
7281 emit_move_insn (validize_mem (ret), virtual_incoming_args_rtx);
7282 seq = get_insns ();
7283 end_sequence ();
7284
7285 push_topmost_sequence ();
7286 emit_insn_after (seq, get_insns ());
7287 pop_topmost_sequence ();
7288 }
7289
7290 return ret;
7291 }
7292 \f
7293 /* Extend a vector that records the INSN_UIDs of INSNS
7294 (a list of one or more insns). */
7295
7296 static void
7297 record_insns (rtx insns, varray_type *vecp)
7298 {
7299 int i, len;
7300 rtx tmp;
7301
7302 tmp = insns;
7303 len = 0;
7304 while (tmp != NULL_RTX)
7305 {
7306 len++;
7307 tmp = NEXT_INSN (tmp);
7308 }
7309
7310 i = VARRAY_SIZE (*vecp);
7311 VARRAY_GROW (*vecp, i + len);
7312 tmp = insns;
7313 while (tmp != NULL_RTX)
7314 {
7315 VARRAY_INT (*vecp, i) = INSN_UID (tmp);
7316 i++;
7317 tmp = NEXT_INSN (tmp);
7318 }
7319 }
7320
7321 /* Set the locator of the insn chain starting at INSN to LOC. */
7322 static void
7323 set_insn_locators (rtx insn, int loc)
7324 {
7325 while (insn != NULL_RTX)
7326 {
7327 if (INSN_P (insn))
7328 INSN_LOCATOR (insn) = loc;
7329 insn = NEXT_INSN (insn);
7330 }
7331 }
7332
7333 /* Determine how many INSN_UIDs in VEC are part of INSN. Because we can
7334 be running after reorg, SEQUENCE rtl is possible. */
7335
7336 static int
7337 contains (rtx insn, varray_type vec)
7338 {
7339 int i, j;
7340
7341 if (GET_CODE (insn) == INSN
7342 && GET_CODE (PATTERN (insn)) == SEQUENCE)
7343 {
7344 int count = 0;
7345 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
7346 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7347 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == VARRAY_INT (vec, j))
7348 count++;
7349 return count;
7350 }
7351 else
7352 {
7353 for (j = VARRAY_SIZE (vec) - 1; j >= 0; --j)
7354 if (INSN_UID (insn) == VARRAY_INT (vec, j))
7355 return 1;
7356 }
7357 return 0;
7358 }
7359
7360 int
7361 prologue_epilogue_contains (rtx insn)
7362 {
7363 if (contains (insn, prologue))
7364 return 1;
7365 if (contains (insn, epilogue))
7366 return 1;
7367 return 0;
7368 }
7369
7370 int
7371 sibcall_epilogue_contains (rtx insn)
7372 {
7373 if (sibcall_epilogue)
7374 return contains (insn, sibcall_epilogue);
7375 return 0;
7376 }
7377
7378 #ifdef HAVE_return
7379 /* Insert gen_return at the end of block BB. This also means updating
7380 block_for_insn appropriately. */
7381
7382 static void
7383 emit_return_into_block (basic_block bb, rtx line_note)
7384 {
7385 emit_jump_insn_after (gen_return (), BB_END (bb));
7386 if (line_note)
7387 emit_note_copy_after (line_note, PREV_INSN (BB_END (bb)));
7388 }
7389 #endif /* HAVE_return */
7390
7391 #if defined(HAVE_epilogue) && defined(INCOMING_RETURN_ADDR_RTX)
7392
7393 /* These functions convert the epilogue into a variant that does not modify the
7394 stack pointer. This is used in cases where a function returns an object
7395 whose size is not known until it is computed. The called function leaves the
7396 object on the stack, leaves the stack depressed, and returns a pointer to
7397 the object.
7398
7399 What we need to do is track all modifications and references to the stack
7400 pointer, deleting the modifications and changing the references to point to
7401 the location the stack pointer would have pointed to had the modifications
7402 taken place.
7403
7404 These functions need to be portable so we need to make as few assumptions
7405 about the epilogue as we can. However, the epilogue basically contains
7406 three things: instructions to reset the stack pointer, instructions to
7407 reload registers, possibly including the frame pointer, and an
7408 instruction to return to the caller.
7409
7410 If we can't be sure of what a relevant epilogue insn is doing, we abort.
7411 We also make no attempt to validate the insns we make since if they are
7412 invalid, we probably can't do anything valid. The intent is that these
7413 routines get "smarter" as more and more machines start to use them and
7414 they try operating on different epilogues.
7415
7416 We use the following structure to track what the part of the epilogue that
7417 we've already processed has done. We keep two copies of the SP equivalence,
7418 one for use during the insn we are processing and one for use in the next
7419 insn. The difference is because one part of a PARALLEL may adjust SP
7420 and the other may use it. */
7421
7422 struct epi_info
7423 {
7424 rtx sp_equiv_reg; /* REG that SP is set from, perhaps SP. */
7425 HOST_WIDE_INT sp_offset; /* Offset from SP_EQUIV_REG of present SP. */
7426 rtx new_sp_equiv_reg; /* REG to be used at end of insn. */
7427 HOST_WIDE_INT new_sp_offset; /* Offset to be used at end of insn. */
7428 rtx equiv_reg_src; /* If nonzero, the value that SP_EQUIV_REG
7429 should be set to once we no longer need
7430 its value. */
7431 rtx const_equiv[FIRST_PSEUDO_REGISTER]; /* Any known constant equivalences
7432 for registers. */
7433 };
7434
7435 static void handle_epilogue_set (rtx, struct epi_info *);
7436 static void update_epilogue_consts (rtx, rtx, void *);
7437 static void emit_equiv_load (struct epi_info *);
7438
7439 /* Modify INSN, a list of one or more insns that is part of the epilogue, to
7440 no modifications to the stack pointer. Return the new list of insns. */
7441
7442 static rtx
7443 keep_stack_depressed (rtx insns)
7444 {
7445 int j;
7446 struct epi_info info;
7447 rtx insn, next;
7448
7449 /* If the epilogue is just a single instruction, it must be OK as is. */
7450 if (NEXT_INSN (insns) == NULL_RTX)
7451 return insns;
7452
7453 /* Otherwise, start a sequence, initialize the information we have, and
7454 process all the insns we were given. */
7455 start_sequence ();
7456
7457 info.sp_equiv_reg = stack_pointer_rtx;
7458 info.sp_offset = 0;
7459 info.equiv_reg_src = 0;
7460
7461 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
7462 info.const_equiv[j] = 0;
7463
7464 insn = insns;
7465 next = NULL_RTX;
7466 while (insn != NULL_RTX)
7467 {
7468 next = NEXT_INSN (insn);
7469
7470 if (!INSN_P (insn))
7471 {
7472 add_insn (insn);
7473 insn = next;
7474 continue;
7475 }
7476
7477 /* If this insn references the register that SP is equivalent to and
7478 we have a pending load to that register, we must force out the load
7479 first and then indicate we no longer know what SP's equivalent is. */
7480 if (info.equiv_reg_src != 0
7481 && reg_referenced_p (info.sp_equiv_reg, PATTERN (insn)))
7482 {
7483 emit_equiv_load (&info);
7484 info.sp_equiv_reg = 0;
7485 }
7486
7487 info.new_sp_equiv_reg = info.sp_equiv_reg;
7488 info.new_sp_offset = info.sp_offset;
7489
7490 /* If this is a (RETURN) and the return address is on the stack,
7491 update the address and change to an indirect jump. */
7492 if (GET_CODE (PATTERN (insn)) == RETURN
7493 || (GET_CODE (PATTERN (insn)) == PARALLEL
7494 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == RETURN))
7495 {
7496 rtx retaddr = INCOMING_RETURN_ADDR_RTX;
7497 rtx base = 0;
7498 HOST_WIDE_INT offset = 0;
7499 rtx jump_insn, jump_set;
7500
7501 /* If the return address is in a register, we can emit the insn
7502 unchanged. Otherwise, it must be a MEM and we see what the
7503 base register and offset are. In any case, we have to emit any
7504 pending load to the equivalent reg of SP, if any. */
7505 if (GET_CODE (retaddr) == REG)
7506 {
7507 emit_equiv_load (&info);
7508 add_insn (insn);
7509 insn = next;
7510 continue;
7511 }
7512 else if (GET_CODE (retaddr) == MEM
7513 && GET_CODE (XEXP (retaddr, 0)) == REG)
7514 base = gen_rtx_REG (Pmode, REGNO (XEXP (retaddr, 0))), offset = 0;
7515 else if (GET_CODE (retaddr) == MEM
7516 && GET_CODE (XEXP (retaddr, 0)) == PLUS
7517 && GET_CODE (XEXP (XEXP (retaddr, 0), 0)) == REG
7518 && GET_CODE (XEXP (XEXP (retaddr, 0), 1)) == CONST_INT)
7519 {
7520 base = gen_rtx_REG (Pmode, REGNO (XEXP (XEXP (retaddr, 0), 0)));
7521 offset = INTVAL (XEXP (XEXP (retaddr, 0), 1));
7522 }
7523 else
7524 abort ();
7525
7526 /* If the base of the location containing the return pointer
7527 is SP, we must update it with the replacement address. Otherwise,
7528 just build the necessary MEM. */
7529 retaddr = plus_constant (base, offset);
7530 if (base == stack_pointer_rtx)
7531 retaddr = simplify_replace_rtx (retaddr, stack_pointer_rtx,
7532 plus_constant (info.sp_equiv_reg,
7533 info.sp_offset));
7534
7535 retaddr = gen_rtx_MEM (Pmode, retaddr);
7536
7537 /* If there is a pending load to the equivalent register for SP
7538 and we reference that register, we must load our address into
7539 a scratch register and then do that load. */
7540 if (info.equiv_reg_src
7541 && reg_overlap_mentioned_p (info.equiv_reg_src, retaddr))
7542 {
7543 unsigned int regno;
7544 rtx reg;
7545
7546 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7547 if (HARD_REGNO_MODE_OK (regno, Pmode)
7548 && !fixed_regs[regno]
7549 && TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
7550 && !REGNO_REG_SET_P (EXIT_BLOCK_PTR->global_live_at_start,
7551 regno)
7552 && !refers_to_regno_p (regno,
7553 regno + hard_regno_nregs[regno]
7554 [Pmode],
7555 info.equiv_reg_src, NULL)
7556 && info.const_equiv[regno] == 0)
7557 break;
7558
7559 if (regno == FIRST_PSEUDO_REGISTER)
7560 abort ();
7561
7562 reg = gen_rtx_REG (Pmode, regno);
7563 emit_move_insn (reg, retaddr);
7564 retaddr = reg;
7565 }
7566
7567 emit_equiv_load (&info);
7568 jump_insn = emit_jump_insn (gen_indirect_jump (retaddr));
7569
7570 /* Show the SET in the above insn is a RETURN. */
7571 jump_set = single_set (jump_insn);
7572 if (jump_set == 0)
7573 abort ();
7574 else
7575 SET_IS_RETURN_P (jump_set) = 1;
7576 }
7577
7578 /* If SP is not mentioned in the pattern and its equivalent register, if
7579 any, is not modified, just emit it. Otherwise, if neither is set,
7580 replace the reference to SP and emit the insn. If none of those are
7581 true, handle each SET individually. */
7582 else if (!reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))
7583 && (info.sp_equiv_reg == stack_pointer_rtx
7584 || !reg_set_p (info.sp_equiv_reg, insn)))
7585 add_insn (insn);
7586 else if (! reg_set_p (stack_pointer_rtx, insn)
7587 && (info.sp_equiv_reg == stack_pointer_rtx
7588 || !reg_set_p (info.sp_equiv_reg, insn)))
7589 {
7590 if (! validate_replace_rtx (stack_pointer_rtx,
7591 plus_constant (info.sp_equiv_reg,
7592 info.sp_offset),
7593 insn))
7594 abort ();
7595
7596 add_insn (insn);
7597 }
7598 else if (GET_CODE (PATTERN (insn)) == SET)
7599 handle_epilogue_set (PATTERN (insn), &info);
7600 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
7601 {
7602 for (j = 0; j < XVECLEN (PATTERN (insn), 0); j++)
7603 if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET)
7604 handle_epilogue_set (XVECEXP (PATTERN (insn), 0, j), &info);
7605 }
7606 else
7607 add_insn (insn);
7608
7609 info.sp_equiv_reg = info.new_sp_equiv_reg;
7610 info.sp_offset = info.new_sp_offset;
7611
7612 /* Now update any constants this insn sets. */
7613 note_stores (PATTERN (insn), update_epilogue_consts, &info);
7614 insn = next;
7615 }
7616
7617 insns = get_insns ();
7618 end_sequence ();
7619 return insns;
7620 }
7621
7622 /* SET is a SET from an insn in the epilogue. P is a pointer to the epi_info
7623 structure that contains information about what we've seen so far. We
7624 process this SET by either updating that data or by emitting one or
7625 more insns. */
7626
7627 static void
7628 handle_epilogue_set (rtx set, struct epi_info *p)
7629 {
7630 /* First handle the case where we are setting SP. Record what it is being
7631 set from. If unknown, abort. */
7632 if (reg_set_p (stack_pointer_rtx, set))
7633 {
7634 if (SET_DEST (set) != stack_pointer_rtx)
7635 abort ();
7636
7637 if (GET_CODE (SET_SRC (set)) == PLUS)
7638 {
7639 p->new_sp_equiv_reg = XEXP (SET_SRC (set), 0);
7640 if (GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
7641 p->new_sp_offset = INTVAL (XEXP (SET_SRC (set), 1));
7642 else if (GET_CODE (XEXP (SET_SRC (set), 1)) == REG
7643 && REGNO (XEXP (SET_SRC (set), 1)) < FIRST_PSEUDO_REGISTER
7644 && p->const_equiv[REGNO (XEXP (SET_SRC (set), 1))] != 0)
7645 p->new_sp_offset
7646 = INTVAL (p->const_equiv[REGNO (XEXP (SET_SRC (set), 1))]);
7647 else
7648 abort ();
7649 }
7650 else
7651 p->new_sp_equiv_reg = SET_SRC (set), p->new_sp_offset = 0;
7652
7653 /* If we are adjusting SP, we adjust from the old data. */
7654 if (p->new_sp_equiv_reg == stack_pointer_rtx)
7655 {
7656 p->new_sp_equiv_reg = p->sp_equiv_reg;
7657 p->new_sp_offset += p->sp_offset;
7658 }
7659
7660 if (p->new_sp_equiv_reg == 0 || GET_CODE (p->new_sp_equiv_reg) != REG)
7661 abort ();
7662
7663 return;
7664 }
7665
7666 /* Next handle the case where we are setting SP's equivalent register.
7667 If we already have a value to set it to, abort. We could update, but
7668 there seems little point in handling that case. Note that we have
7669 to allow for the case where we are setting the register set in
7670 the previous part of a PARALLEL inside a single insn. But use the
7671 old offset for any updates within this insn. We must allow for the case
7672 where the register is being set in a different (usually wider) mode than
7673 Pmode). */
7674 else if (p->new_sp_equiv_reg != 0 && reg_set_p (p->new_sp_equiv_reg, set))
7675 {
7676 if (p->equiv_reg_src != 0
7677 || GET_CODE (p->new_sp_equiv_reg) != REG
7678 || GET_CODE (SET_DEST (set)) != REG
7679 || GET_MODE_BITSIZE (GET_MODE (SET_DEST (set))) > BITS_PER_WORD
7680 || REGNO (p->new_sp_equiv_reg) != REGNO (SET_DEST (set)))
7681 abort ();
7682 else
7683 p->equiv_reg_src
7684 = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7685 plus_constant (p->sp_equiv_reg,
7686 p->sp_offset));
7687 }
7688
7689 /* Otherwise, replace any references to SP in the insn to its new value
7690 and emit the insn. */
7691 else
7692 {
7693 SET_SRC (set) = simplify_replace_rtx (SET_SRC (set), stack_pointer_rtx,
7694 plus_constant (p->sp_equiv_reg,
7695 p->sp_offset));
7696 SET_DEST (set) = simplify_replace_rtx (SET_DEST (set), stack_pointer_rtx,
7697 plus_constant (p->sp_equiv_reg,
7698 p->sp_offset));
7699 emit_insn (set);
7700 }
7701 }
7702
7703 /* Update the tracking information for registers set to constants. */
7704
7705 static void
7706 update_epilogue_consts (rtx dest, rtx x, void *data)
7707 {
7708 struct epi_info *p = (struct epi_info *) data;
7709 rtx new;
7710
7711 if (GET_CODE (dest) != REG || REGNO (dest) >= FIRST_PSEUDO_REGISTER)
7712 return;
7713
7714 /* If we are either clobbering a register or doing a partial set,
7715 show we don't know the value. */
7716 else if (GET_CODE (x) == CLOBBER || ! rtx_equal_p (dest, SET_DEST (x)))
7717 p->const_equiv[REGNO (dest)] = 0;
7718
7719 /* If we are setting it to a constant, record that constant. */
7720 else if (GET_CODE (SET_SRC (x)) == CONST_INT)
7721 p->const_equiv[REGNO (dest)] = SET_SRC (x);
7722
7723 /* If this is a binary operation between a register we have been tracking
7724 and a constant, see if we can compute a new constant value. */
7725 else if (ARITHMETIC_P (SET_SRC (x))
7726 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
7727 && REGNO (XEXP (SET_SRC (x), 0)) < FIRST_PSEUDO_REGISTER
7728 && p->const_equiv[REGNO (XEXP (SET_SRC (x), 0))] != 0
7729 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
7730 && 0 != (new = simplify_binary_operation
7731 (GET_CODE (SET_SRC (x)), GET_MODE (dest),
7732 p->const_equiv[REGNO (XEXP (SET_SRC (x), 0))],
7733 XEXP (SET_SRC (x), 1)))
7734 && GET_CODE (new) == CONST_INT)
7735 p->const_equiv[REGNO (dest)] = new;
7736
7737 /* Otherwise, we can't do anything with this value. */
7738 else
7739 p->const_equiv[REGNO (dest)] = 0;
7740 }
7741
7742 /* Emit an insn to do the load shown in p->equiv_reg_src, if needed. */
7743
7744 static void
7745 emit_equiv_load (struct epi_info *p)
7746 {
7747 if (p->equiv_reg_src != 0)
7748 {
7749 rtx dest = p->sp_equiv_reg;
7750
7751 if (GET_MODE (p->equiv_reg_src) != GET_MODE (dest))
7752 dest = gen_rtx_REG (GET_MODE (p->equiv_reg_src),
7753 REGNO (p->sp_equiv_reg));
7754
7755 emit_move_insn (dest, p->equiv_reg_src);
7756 p->equiv_reg_src = 0;
7757 }
7758 }
7759 #endif
7760
7761 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
7762 this into place with notes indicating where the prologue ends and where
7763 the epilogue begins. Update the basic block information when possible. */
7764
7765 void
7766 thread_prologue_and_epilogue_insns (rtx f ATTRIBUTE_UNUSED)
7767 {
7768 int inserted = 0;
7769 edge e;
7770 #if defined (HAVE_sibcall_epilogue) || defined (HAVE_epilogue) || defined (HAVE_return) || defined (HAVE_prologue)
7771 rtx seq;
7772 #endif
7773 #ifdef HAVE_prologue
7774 rtx prologue_end = NULL_RTX;
7775 #endif
7776 #if defined (HAVE_epilogue) || defined(HAVE_return)
7777 rtx epilogue_end = NULL_RTX;
7778 #endif
7779
7780 #ifdef HAVE_prologue
7781 if (HAVE_prologue)
7782 {
7783 start_sequence ();
7784 seq = gen_prologue ();
7785 emit_insn (seq);
7786
7787 /* Retain a map of the prologue insns. */
7788 record_insns (seq, &prologue);
7789 prologue_end = emit_note (NOTE_INSN_PROLOGUE_END);
7790
7791 seq = get_insns ();
7792 end_sequence ();
7793 set_insn_locators (seq, prologue_locator);
7794
7795 /* Can't deal with multiple successors of the entry block
7796 at the moment. Function should always have at least one
7797 entry point. */
7798 if (!ENTRY_BLOCK_PTR->succ || ENTRY_BLOCK_PTR->succ->succ_next)
7799 abort ();
7800
7801 insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
7802 inserted = 1;
7803 }
7804 #endif
7805
7806 /* If the exit block has no non-fake predecessors, we don't need
7807 an epilogue. */
7808 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7809 if ((e->flags & EDGE_FAKE) == 0)
7810 break;
7811 if (e == NULL)
7812 goto epilogue_done;
7813
7814 #ifdef HAVE_return
7815 if (optimize && HAVE_return)
7816 {
7817 /* If we're allowed to generate a simple return instruction,
7818 then by definition we don't need a full epilogue. Examine
7819 the block that falls through to EXIT. If it does not
7820 contain any code, examine its predecessors and try to
7821 emit (conditional) return instructions. */
7822
7823 basic_block last;
7824 edge e_next;
7825 rtx label;
7826
7827 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7828 if (e->flags & EDGE_FALLTHRU)
7829 break;
7830 if (e == NULL)
7831 goto epilogue_done;
7832 last = e->src;
7833
7834 /* Verify that there are no active instructions in the last block. */
7835 label = BB_END (last);
7836 while (label && GET_CODE (label) != CODE_LABEL)
7837 {
7838 if (active_insn_p (label))
7839 break;
7840 label = PREV_INSN (label);
7841 }
7842
7843 if (BB_HEAD (last) == label && GET_CODE (label) == CODE_LABEL)
7844 {
7845 rtx epilogue_line_note = NULL_RTX;
7846
7847 /* Locate the line number associated with the closing brace,
7848 if we can find one. */
7849 for (seq = get_last_insn ();
7850 seq && ! active_insn_p (seq);
7851 seq = PREV_INSN (seq))
7852 if (GET_CODE (seq) == NOTE && NOTE_LINE_NUMBER (seq) > 0)
7853 {
7854 epilogue_line_note = seq;
7855 break;
7856 }
7857
7858 for (e = last->pred; e; e = e_next)
7859 {
7860 basic_block bb = e->src;
7861 rtx jump;
7862
7863 e_next = e->pred_next;
7864 if (bb == ENTRY_BLOCK_PTR)
7865 continue;
7866
7867 jump = BB_END (bb);
7868 if ((GET_CODE (jump) != JUMP_INSN) || JUMP_LABEL (jump) != label)
7869 continue;
7870
7871 /* If we have an unconditional jump, we can replace that
7872 with a simple return instruction. */
7873 if (simplejump_p (jump))
7874 {
7875 emit_return_into_block (bb, epilogue_line_note);
7876 delete_insn (jump);
7877 }
7878
7879 /* If we have a conditional jump, we can try to replace
7880 that with a conditional return instruction. */
7881 else if (condjump_p (jump))
7882 {
7883 if (! redirect_jump (jump, 0, 0))
7884 continue;
7885
7886 /* If this block has only one successor, it both jumps
7887 and falls through to the fallthru block, so we can't
7888 delete the edge. */
7889 if (bb->succ->succ_next == NULL)
7890 continue;
7891 }
7892 else
7893 continue;
7894
7895 /* Fix up the CFG for the successful change we just made. */
7896 redirect_edge_succ (e, EXIT_BLOCK_PTR);
7897 }
7898
7899 /* Emit a return insn for the exit fallthru block. Whether
7900 this is still reachable will be determined later. */
7901
7902 emit_barrier_after (BB_END (last));
7903 emit_return_into_block (last, epilogue_line_note);
7904 epilogue_end = BB_END (last);
7905 last->succ->flags &= ~EDGE_FALLTHRU;
7906 goto epilogue_done;
7907 }
7908 }
7909 #endif
7910 #ifdef HAVE_epilogue
7911 if (HAVE_epilogue)
7912 {
7913 /* Find the edge that falls through to EXIT. Other edges may exist
7914 due to RETURN instructions, but those don't need epilogues.
7915 There really shouldn't be a mixture -- either all should have
7916 been converted or none, however... */
7917
7918 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7919 if (e->flags & EDGE_FALLTHRU)
7920 break;
7921 if (e == NULL)
7922 goto epilogue_done;
7923
7924 start_sequence ();
7925 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
7926
7927 seq = gen_epilogue ();
7928
7929 #ifdef INCOMING_RETURN_ADDR_RTX
7930 /* If this function returns with the stack depressed and we can support
7931 it, massage the epilogue to actually do that. */
7932 if (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
7933 && TYPE_RETURNS_STACK_DEPRESSED (TREE_TYPE (current_function_decl)))
7934 seq = keep_stack_depressed (seq);
7935 #endif
7936
7937 emit_jump_insn (seq);
7938
7939 /* Retain a map of the epilogue insns. */
7940 record_insns (seq, &epilogue);
7941 set_insn_locators (seq, epilogue_locator);
7942
7943 seq = get_insns ();
7944 end_sequence ();
7945
7946 insert_insn_on_edge (seq, e);
7947 inserted = 1;
7948 }
7949 #endif
7950 epilogue_done:
7951
7952 if (inserted)
7953 commit_edge_insertions ();
7954
7955 #ifdef HAVE_sibcall_epilogue
7956 /* Emit sibling epilogues before any sibling call sites. */
7957 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
7958 {
7959 basic_block bb = e->src;
7960 rtx insn = BB_END (bb);
7961 rtx i;
7962 rtx newinsn;
7963
7964 if (GET_CODE (insn) != CALL_INSN
7965 || ! SIBLING_CALL_P (insn))
7966 continue;
7967
7968 start_sequence ();
7969 emit_insn (gen_sibcall_epilogue ());
7970 seq = get_insns ();
7971 end_sequence ();
7972
7973 /* Retain a map of the epilogue insns. Used in life analysis to
7974 avoid getting rid of sibcall epilogue insns. Do this before we
7975 actually emit the sequence. */
7976 record_insns (seq, &sibcall_epilogue);
7977 set_insn_locators (seq, epilogue_locator);
7978
7979 i = PREV_INSN (insn);
7980 newinsn = emit_insn_before (seq, insn);
7981 }
7982 #endif
7983
7984 #ifdef HAVE_prologue
7985 /* This is probably all useless now that we use locators. */
7986 if (prologue_end)
7987 {
7988 rtx insn, prev;
7989
7990 /* GDB handles `break f' by setting a breakpoint on the first
7991 line note after the prologue. Which means (1) that if
7992 there are line number notes before where we inserted the
7993 prologue we should move them, and (2) we should generate a
7994 note before the end of the first basic block, if there isn't
7995 one already there.
7996
7997 ??? This behavior is completely broken when dealing with
7998 multiple entry functions. We simply place the note always
7999 into first basic block and let alternate entry points
8000 to be missed.
8001 */
8002
8003 for (insn = prologue_end; insn; insn = prev)
8004 {
8005 prev = PREV_INSN (insn);
8006 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
8007 {
8008 /* Note that we cannot reorder the first insn in the
8009 chain, since rest_of_compilation relies on that
8010 remaining constant. */
8011 if (prev == NULL)
8012 break;
8013 reorder_insns (insn, insn, prologue_end);
8014 }
8015 }
8016
8017 /* Find the last line number note in the first block. */
8018 for (insn = BB_END (ENTRY_BLOCK_PTR->next_bb);
8019 insn != prologue_end && insn;
8020 insn = PREV_INSN (insn))
8021 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
8022 break;
8023
8024 /* If we didn't find one, make a copy of the first line number
8025 we run across. */
8026 if (! insn)
8027 {
8028 for (insn = next_active_insn (prologue_end);
8029 insn;
8030 insn = PREV_INSN (insn))
8031 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
8032 {
8033 emit_note_copy_after (insn, prologue_end);
8034 break;
8035 }
8036 }
8037 }
8038 #endif
8039 #ifdef HAVE_epilogue
8040 if (epilogue_end)
8041 {
8042 rtx insn, next;
8043
8044 /* Similarly, move any line notes that appear after the epilogue.
8045 There is no need, however, to be quite so anal about the existence
8046 of such a note. Also move the NOTE_INSN_FUNCTION_END and (possibly)
8047 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
8048 info generation. */
8049 for (insn = epilogue_end; insn; insn = next)
8050 {
8051 next = NEXT_INSN (insn);
8052 if (GET_CODE (insn) == NOTE
8053 && (NOTE_LINE_NUMBER (insn) > 0
8054 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG
8055 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END))
8056 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
8057 }
8058 }
8059 #endif
8060 }
8061
8062 /* Reposition the prologue-end and epilogue-begin notes after instruction
8063 scheduling and delayed branch scheduling. */
8064
8065 void
8066 reposition_prologue_and_epilogue_notes (rtx f ATTRIBUTE_UNUSED)
8067 {
8068 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
8069 rtx insn, last, note;
8070 int len;
8071
8072 if ((len = VARRAY_SIZE (prologue)) > 0)
8073 {
8074 last = 0, note = 0;
8075
8076 /* Scan from the beginning until we reach the last prologue insn.
8077 We apparently can't depend on basic_block_{head,end} after
8078 reorg has run. */
8079 for (insn = f; insn; insn = NEXT_INSN (insn))
8080 {
8081 if (GET_CODE (insn) == NOTE)
8082 {
8083 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
8084 note = insn;
8085 }
8086 else if (contains (insn, prologue))
8087 {
8088 last = insn;
8089 if (--len == 0)
8090 break;
8091 }
8092 }
8093
8094 if (last)
8095 {
8096 /* Find the prologue-end note if we haven't already, and
8097 move it to just after the last prologue insn. */
8098 if (note == 0)
8099 {
8100 for (note = last; (note = NEXT_INSN (note));)
8101 if (GET_CODE (note) == NOTE
8102 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
8103 break;
8104 }
8105
8106 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
8107 if (GET_CODE (last) == CODE_LABEL)
8108 last = NEXT_INSN (last);
8109 reorder_insns (note, note, last);
8110 }
8111 }
8112
8113 if ((len = VARRAY_SIZE (epilogue)) > 0)
8114 {
8115 last = 0, note = 0;
8116
8117 /* Scan from the end until we reach the first epilogue insn.
8118 We apparently can't depend on basic_block_{head,end} after
8119 reorg has run. */
8120 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
8121 {
8122 if (GET_CODE (insn) == NOTE)
8123 {
8124 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
8125 note = insn;
8126 }
8127 else if (contains (insn, epilogue))
8128 {
8129 last = insn;
8130 if (--len == 0)
8131 break;
8132 }
8133 }
8134
8135 if (last)
8136 {
8137 /* Find the epilogue-begin note if we haven't already, and
8138 move it to just before the first epilogue insn. */
8139 if (note == 0)
8140 {
8141 for (note = insn; (note = PREV_INSN (note));)
8142 if (GET_CODE (note) == NOTE
8143 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
8144 break;
8145 }
8146
8147 if (PREV_INSN (last) != note)
8148 reorder_insns (note, note, PREV_INSN (last));
8149 }
8150 }
8151 #endif /* HAVE_prologue or HAVE_epilogue */
8152 }
8153
8154 /* Called once, at initialization, to initialize function.c. */
8155
8156 void
8157 init_function_once (void)
8158 {
8159 VARRAY_INT_INIT (prologue, 0, "prologue");
8160 VARRAY_INT_INIT (epilogue, 0, "epilogue");
8161 VARRAY_INT_INIT (sibcall_epilogue, 0, "sibcall_epilogue");
8162 }
8163
8164 /* Returns the name of the current function. */
8165 const char *
8166 current_function_name (void)
8167 {
8168 return lang_hooks.decl_printable_name (cfun->decl, 2);
8169 }
8170
8171 #include "gt-function.h"