re PR tree-optimization/56118 (Piecewise vector / complex initialization from constan...
[gcc.git] / gcc / function.c
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 /* This file handles the generation of rtl code from tree structure
21 at the level of the function as a whole.
22 It creates the rtl expressions for parameters and auto variables
23 and has full responsibility for allocating stack slots.
24
25 `expand_function_start' is called at the beginning of a function,
26 before the function body is parsed, and `expand_function_end' is
27 called after parsing the body.
28
29 Call `assign_stack_local' to allocate a stack slot for a local variable.
30 This is usually done during the RTL generation for the function body,
31 but it can also be done in the reload pass when a pseudo-register does
32 not get a hard register. */
33
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "backend.h"
38 #include "target.h"
39 #include "rtl.h"
40 #include "tree.h"
41 #include "gimple-expr.h"
42 #include "cfghooks.h"
43 #include "df.h"
44 #include "tm_p.h"
45 #include "stringpool.h"
46 #include "expmed.h"
47 #include "optabs.h"
48 #include "regs.h"
49 #include "emit-rtl.h"
50 #include "recog.h"
51 #include "rtl-error.h"
52 #include "alias.h"
53 #include "fold-const.h"
54 #include "stor-layout.h"
55 #include "varasm.h"
56 #include "except.h"
57 #include "dojump.h"
58 #include "explow.h"
59 #include "calls.h"
60 #include "expr.h"
61 #include "optabs-tree.h"
62 #include "output.h"
63 #include "langhooks.h"
64 #include "common/common-target.h"
65 #include "gimplify.h"
66 #include "tree-pass.h"
67 #include "cfgrtl.h"
68 #include "cfganal.h"
69 #include "cfgbuild.h"
70 #include "cfgcleanup.h"
71 #include "cfgexpand.h"
72 #include "shrink-wrap.h"
73 #include "toplev.h"
74 #include "rtl-iter.h"
75 #include "tree-chkp.h"
76 #include "rtl-chkp.h"
77 #include "tree-dfa.h"
78
79 /* So we can assign to cfun in this file. */
80 #undef cfun
81
82 #ifndef STACK_ALIGNMENT_NEEDED
83 #define STACK_ALIGNMENT_NEEDED 1
84 #endif
85
86 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
87
88 /* Round a value to the lowest integer less than it that is a multiple of
89 the required alignment. Avoid using division in case the value is
90 negative. Assume the alignment is a power of two. */
91 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
92
93 /* Similar, but round to the next highest integer that meets the
94 alignment. */
95 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
96
97 /* Nonzero once virtual register instantiation has been done.
98 assign_stack_local uses frame_pointer_rtx when this is nonzero.
99 calls.c:emit_library_call_value_1 uses it to set up
100 post-instantiation libcalls. */
101 int virtuals_instantiated;
102
103 /* Assign unique numbers to labels generated for profiling, debugging, etc. */
104 static GTY(()) int funcdef_no;
105
106 /* These variables hold pointers to functions to create and destroy
107 target specific, per-function data structures. */
108 struct machine_function * (*init_machine_status) (void);
109
110 /* The currently compiled function. */
111 struct function *cfun = 0;
112
113 /* These hashes record the prologue and epilogue insns. */
114
115 struct insn_cache_hasher : ggc_cache_ptr_hash<rtx_def>
116 {
117 static hashval_t hash (rtx x) { return htab_hash_pointer (x); }
118 static bool equal (rtx a, rtx b) { return a == b; }
119 };
120
121 static GTY((cache))
122 hash_table<insn_cache_hasher> *prologue_insn_hash;
123 static GTY((cache))
124 hash_table<insn_cache_hasher> *epilogue_insn_hash;
125 \f
126
127 hash_table<used_type_hasher> *types_used_by_vars_hash = NULL;
128 vec<tree, va_gc> *types_used_by_cur_var_decl;
129
130 /* Forward declarations. */
131
132 static struct temp_slot *find_temp_slot_from_address (rtx);
133 static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
134 static void pad_below (struct args_size *, machine_mode, tree);
135 static void reorder_blocks_1 (rtx_insn *, tree, vec<tree> *);
136 static int all_blocks (tree, tree *);
137 static tree *get_block_vector (tree, int *);
138 extern tree debug_find_var_in_block_tree (tree, tree);
139 /* We always define `record_insns' even if it's not used so that we
140 can always export `prologue_epilogue_contains'. */
141 static void record_insns (rtx_insn *, rtx, hash_table<insn_cache_hasher> **)
142 ATTRIBUTE_UNUSED;
143 static bool contains (const_rtx, hash_table<insn_cache_hasher> *);
144 static void prepare_function_start (void);
145 static void do_clobber_return_reg (rtx, void *);
146 static void do_use_return_reg (rtx, void *);
147
148 \f
149 /* Stack of nested functions. */
150 /* Keep track of the cfun stack. */
151
152 static vec<function *> function_context_stack;
153
154 /* Save the current context for compilation of a nested function.
155 This is called from language-specific code. */
156
157 void
158 push_function_context (void)
159 {
160 if (cfun == 0)
161 allocate_struct_function (NULL, false);
162
163 function_context_stack.safe_push (cfun);
164 set_cfun (NULL);
165 }
166
167 /* Restore the last saved context, at the end of a nested function.
168 This function is called from language-specific code. */
169
170 void
171 pop_function_context (void)
172 {
173 struct function *p = function_context_stack.pop ();
174 set_cfun (p);
175 current_function_decl = p->decl;
176
177 /* Reset variables that have known state during rtx generation. */
178 virtuals_instantiated = 0;
179 generating_concat_p = 1;
180 }
181
182 /* Clear out all parts of the state in F that can safely be discarded
183 after the function has been parsed, but not compiled, to let
184 garbage collection reclaim the memory. */
185
186 void
187 free_after_parsing (struct function *f)
188 {
189 f->language = 0;
190 }
191
192 /* Clear out all parts of the state in F that can safely be discarded
193 after the function has been compiled, to let garbage collection
194 reclaim the memory. */
195
196 void
197 free_after_compilation (struct function *f)
198 {
199 prologue_insn_hash = NULL;
200 epilogue_insn_hash = NULL;
201
202 free (crtl->emit.regno_pointer_align);
203
204 memset (crtl, 0, sizeof (struct rtl_data));
205 f->eh = NULL;
206 f->machine = NULL;
207 f->cfg = NULL;
208 f->curr_properties &= ~PROP_cfg;
209
210 regno_reg_rtx = NULL;
211 }
212 \f
213 /* Return size needed for stack frame based on slots so far allocated.
214 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
215 the caller may have to do that. */
216
217 HOST_WIDE_INT
218 get_frame_size (void)
219 {
220 if (FRAME_GROWS_DOWNWARD)
221 return -frame_offset;
222 else
223 return frame_offset;
224 }
225
226 /* Issue an error message and return TRUE if frame OFFSET overflows in
227 the signed target pointer arithmetics for function FUNC. Otherwise
228 return FALSE. */
229
230 bool
231 frame_offset_overflow (HOST_WIDE_INT offset, tree func)
232 {
233 unsigned HOST_WIDE_INT size = FRAME_GROWS_DOWNWARD ? -offset : offset;
234
235 if (size > ((unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (Pmode) - 1))
236 /* Leave room for the fixed part of the frame. */
237 - 64 * UNITS_PER_WORD)
238 {
239 error_at (DECL_SOURCE_LOCATION (func),
240 "total size of local objects too large");
241 return TRUE;
242 }
243
244 return FALSE;
245 }
246
247 /* Return stack slot alignment in bits for TYPE and MODE. */
248
249 static unsigned int
250 get_stack_local_alignment (tree type, machine_mode mode)
251 {
252 unsigned int alignment;
253
254 if (mode == BLKmode)
255 alignment = BIGGEST_ALIGNMENT;
256 else
257 alignment = GET_MODE_ALIGNMENT (mode);
258
259 /* Allow the frond-end to (possibly) increase the alignment of this
260 stack slot. */
261 if (! type)
262 type = lang_hooks.types.type_for_mode (mode, 0);
263
264 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
265 }
266
267 /* Determine whether it is possible to fit a stack slot of size SIZE and
268 alignment ALIGNMENT into an area in the stack frame that starts at
269 frame offset START and has a length of LENGTH. If so, store the frame
270 offset to be used for the stack slot in *POFFSET and return true;
271 return false otherwise. This function will extend the frame size when
272 given a start/length pair that lies at the end of the frame. */
273
274 static bool
275 try_fit_stack_local (HOST_WIDE_INT start, HOST_WIDE_INT length,
276 HOST_WIDE_INT size, unsigned int alignment,
277 HOST_WIDE_INT *poffset)
278 {
279 HOST_WIDE_INT this_frame_offset;
280 int frame_off, frame_alignment, frame_phase;
281
282 /* Calculate how many bytes the start of local variables is off from
283 stack alignment. */
284 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
285 frame_off = STARTING_FRAME_OFFSET % frame_alignment;
286 frame_phase = frame_off ? frame_alignment - frame_off : 0;
287
288 /* Round the frame offset to the specified alignment. */
289
290 /* We must be careful here, since FRAME_OFFSET might be negative and
291 division with a negative dividend isn't as well defined as we might
292 like. So we instead assume that ALIGNMENT is a power of two and
293 use logical operations which are unambiguous. */
294 if (FRAME_GROWS_DOWNWARD)
295 this_frame_offset
296 = (FLOOR_ROUND (start + length - size - frame_phase,
297 (unsigned HOST_WIDE_INT) alignment)
298 + frame_phase);
299 else
300 this_frame_offset
301 = (CEIL_ROUND (start - frame_phase,
302 (unsigned HOST_WIDE_INT) alignment)
303 + frame_phase);
304
305 /* See if it fits. If this space is at the edge of the frame,
306 consider extending the frame to make it fit. Our caller relies on
307 this when allocating a new slot. */
308 if (frame_offset == start && this_frame_offset < frame_offset)
309 frame_offset = this_frame_offset;
310 else if (this_frame_offset < start)
311 return false;
312 else if (start + length == frame_offset
313 && this_frame_offset + size > start + length)
314 frame_offset = this_frame_offset + size;
315 else if (this_frame_offset + size > start + length)
316 return false;
317
318 *poffset = this_frame_offset;
319 return true;
320 }
321
322 /* Create a new frame_space structure describing free space in the stack
323 frame beginning at START and ending at END, and chain it into the
324 function's frame_space_list. */
325
326 static void
327 add_frame_space (HOST_WIDE_INT start, HOST_WIDE_INT end)
328 {
329 struct frame_space *space = ggc_alloc<frame_space> ();
330 space->next = crtl->frame_space_list;
331 crtl->frame_space_list = space;
332 space->start = start;
333 space->length = end - start;
334 }
335
336 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
337 with machine mode MODE.
338
339 ALIGN controls the amount of alignment for the address of the slot:
340 0 means according to MODE,
341 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
342 -2 means use BITS_PER_UNIT,
343 positive specifies alignment boundary in bits.
344
345 KIND has ASLK_REDUCE_ALIGN bit set if it is OK to reduce
346 alignment and ASLK_RECORD_PAD bit set if we should remember
347 extra space we allocated for alignment purposes. When we are
348 called from assign_stack_temp_for_type, it is not set so we don't
349 track the same stack slot in two independent lists.
350
351 We do not round to stack_boundary here. */
352
353 rtx
354 assign_stack_local_1 (machine_mode mode, HOST_WIDE_INT size,
355 int align, int kind)
356 {
357 rtx x, addr;
358 int bigend_correction = 0;
359 HOST_WIDE_INT slot_offset = 0, old_frame_offset;
360 unsigned int alignment, alignment_in_bits;
361
362 if (align == 0)
363 {
364 alignment = get_stack_local_alignment (NULL, mode);
365 alignment /= BITS_PER_UNIT;
366 }
367 else if (align == -1)
368 {
369 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
370 size = CEIL_ROUND (size, alignment);
371 }
372 else if (align == -2)
373 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
374 else
375 alignment = align / BITS_PER_UNIT;
376
377 alignment_in_bits = alignment * BITS_PER_UNIT;
378
379 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
380 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
381 {
382 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
383 alignment = alignment_in_bits / BITS_PER_UNIT;
384 }
385
386 if (SUPPORTS_STACK_ALIGNMENT)
387 {
388 if (crtl->stack_alignment_estimated < alignment_in_bits)
389 {
390 if (!crtl->stack_realign_processed)
391 crtl->stack_alignment_estimated = alignment_in_bits;
392 else
393 {
394 /* If stack is realigned and stack alignment value
395 hasn't been finalized, it is OK not to increase
396 stack_alignment_estimated. The bigger alignment
397 requirement is recorded in stack_alignment_needed
398 below. */
399 gcc_assert (!crtl->stack_realign_finalized);
400 if (!crtl->stack_realign_needed)
401 {
402 /* It is OK to reduce the alignment as long as the
403 requested size is 0 or the estimated stack
404 alignment >= mode alignment. */
405 gcc_assert ((kind & ASLK_REDUCE_ALIGN)
406 || size == 0
407 || (crtl->stack_alignment_estimated
408 >= GET_MODE_ALIGNMENT (mode)));
409 alignment_in_bits = crtl->stack_alignment_estimated;
410 alignment = alignment_in_bits / BITS_PER_UNIT;
411 }
412 }
413 }
414 }
415
416 if (crtl->stack_alignment_needed < alignment_in_bits)
417 crtl->stack_alignment_needed = alignment_in_bits;
418 if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
419 crtl->max_used_stack_slot_alignment = alignment_in_bits;
420
421 if (mode != BLKmode || size != 0)
422 {
423 if (kind & ASLK_RECORD_PAD)
424 {
425 struct frame_space **psp;
426
427 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
428 {
429 struct frame_space *space = *psp;
430 if (!try_fit_stack_local (space->start, space->length, size,
431 alignment, &slot_offset))
432 continue;
433 *psp = space->next;
434 if (slot_offset > space->start)
435 add_frame_space (space->start, slot_offset);
436 if (slot_offset + size < space->start + space->length)
437 add_frame_space (slot_offset + size,
438 space->start + space->length);
439 goto found_space;
440 }
441 }
442 }
443 else if (!STACK_ALIGNMENT_NEEDED)
444 {
445 slot_offset = frame_offset;
446 goto found_space;
447 }
448
449 old_frame_offset = frame_offset;
450
451 if (FRAME_GROWS_DOWNWARD)
452 {
453 frame_offset -= size;
454 try_fit_stack_local (frame_offset, size, size, alignment, &slot_offset);
455
456 if (kind & ASLK_RECORD_PAD)
457 {
458 if (slot_offset > frame_offset)
459 add_frame_space (frame_offset, slot_offset);
460 if (slot_offset + size < old_frame_offset)
461 add_frame_space (slot_offset + size, old_frame_offset);
462 }
463 }
464 else
465 {
466 frame_offset += size;
467 try_fit_stack_local (old_frame_offset, size, size, alignment, &slot_offset);
468
469 if (kind & ASLK_RECORD_PAD)
470 {
471 if (slot_offset > old_frame_offset)
472 add_frame_space (old_frame_offset, slot_offset);
473 if (slot_offset + size < frame_offset)
474 add_frame_space (slot_offset + size, frame_offset);
475 }
476 }
477
478 found_space:
479 /* On a big-endian machine, if we are allocating more space than we will use,
480 use the least significant bytes of those that are allocated. */
481 if (BYTES_BIG_ENDIAN && mode != BLKmode && GET_MODE_SIZE (mode) < size)
482 bigend_correction = size - GET_MODE_SIZE (mode);
483
484 /* If we have already instantiated virtual registers, return the actual
485 address relative to the frame pointer. */
486 if (virtuals_instantiated)
487 addr = plus_constant (Pmode, frame_pointer_rtx,
488 trunc_int_for_mode
489 (slot_offset + bigend_correction
490 + STARTING_FRAME_OFFSET, Pmode));
491 else
492 addr = plus_constant (Pmode, virtual_stack_vars_rtx,
493 trunc_int_for_mode
494 (slot_offset + bigend_correction,
495 Pmode));
496
497 x = gen_rtx_MEM (mode, addr);
498 set_mem_align (x, alignment_in_bits);
499 MEM_NOTRAP_P (x) = 1;
500
501 stack_slot_list
502 = gen_rtx_EXPR_LIST (VOIDmode, x, stack_slot_list);
503
504 if (frame_offset_overflow (frame_offset, current_function_decl))
505 frame_offset = 0;
506
507 return x;
508 }
509
510 /* Wrap up assign_stack_local_1 with last parameter as false. */
511
512 rtx
513 assign_stack_local (machine_mode mode, HOST_WIDE_INT size, int align)
514 {
515 return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
516 }
517 \f
518 /* In order to evaluate some expressions, such as function calls returning
519 structures in memory, we need to temporarily allocate stack locations.
520 We record each allocated temporary in the following structure.
521
522 Associated with each temporary slot is a nesting level. When we pop up
523 one level, all temporaries associated with the previous level are freed.
524 Normally, all temporaries are freed after the execution of the statement
525 in which they were created. However, if we are inside a ({...}) grouping,
526 the result may be in a temporary and hence must be preserved. If the
527 result could be in a temporary, we preserve it if we can determine which
528 one it is in. If we cannot determine which temporary may contain the
529 result, all temporaries are preserved. A temporary is preserved by
530 pretending it was allocated at the previous nesting level. */
531
532 struct GTY(()) temp_slot {
533 /* Points to next temporary slot. */
534 struct temp_slot *next;
535 /* Points to previous temporary slot. */
536 struct temp_slot *prev;
537 /* The rtx to used to reference the slot. */
538 rtx slot;
539 /* The size, in units, of the slot. */
540 HOST_WIDE_INT size;
541 /* The type of the object in the slot, or zero if it doesn't correspond
542 to a type. We use this to determine whether a slot can be reused.
543 It can be reused if objects of the type of the new slot will always
544 conflict with objects of the type of the old slot. */
545 tree type;
546 /* The alignment (in bits) of the slot. */
547 unsigned int align;
548 /* Nonzero if this temporary is currently in use. */
549 char in_use;
550 /* Nesting level at which this slot is being used. */
551 int level;
552 /* The offset of the slot from the frame_pointer, including extra space
553 for alignment. This info is for combine_temp_slots. */
554 HOST_WIDE_INT base_offset;
555 /* The size of the slot, including extra space for alignment. This
556 info is for combine_temp_slots. */
557 HOST_WIDE_INT full_size;
558 };
559
560 /* Entry for the below hash table. */
561 struct GTY((for_user)) temp_slot_address_entry {
562 hashval_t hash;
563 rtx address;
564 struct temp_slot *temp_slot;
565 };
566
567 struct temp_address_hasher : ggc_ptr_hash<temp_slot_address_entry>
568 {
569 static hashval_t hash (temp_slot_address_entry *);
570 static bool equal (temp_slot_address_entry *, temp_slot_address_entry *);
571 };
572
573 /* A table of addresses that represent a stack slot. The table is a mapping
574 from address RTXen to a temp slot. */
575 static GTY(()) hash_table<temp_address_hasher> *temp_slot_address_table;
576 static size_t n_temp_slots_in_use;
577
578 /* Removes temporary slot TEMP from LIST. */
579
580 static void
581 cut_slot_from_list (struct temp_slot *temp, struct temp_slot **list)
582 {
583 if (temp->next)
584 temp->next->prev = temp->prev;
585 if (temp->prev)
586 temp->prev->next = temp->next;
587 else
588 *list = temp->next;
589
590 temp->prev = temp->next = NULL;
591 }
592
593 /* Inserts temporary slot TEMP to LIST. */
594
595 static void
596 insert_slot_to_list (struct temp_slot *temp, struct temp_slot **list)
597 {
598 temp->next = *list;
599 if (*list)
600 (*list)->prev = temp;
601 temp->prev = NULL;
602 *list = temp;
603 }
604
605 /* Returns the list of used temp slots at LEVEL. */
606
607 static struct temp_slot **
608 temp_slots_at_level (int level)
609 {
610 if (level >= (int) vec_safe_length (used_temp_slots))
611 vec_safe_grow_cleared (used_temp_slots, level + 1);
612
613 return &(*used_temp_slots)[level];
614 }
615
616 /* Returns the maximal temporary slot level. */
617
618 static int
619 max_slot_level (void)
620 {
621 if (!used_temp_slots)
622 return -1;
623
624 return used_temp_slots->length () - 1;
625 }
626
627 /* Moves temporary slot TEMP to LEVEL. */
628
629 static void
630 move_slot_to_level (struct temp_slot *temp, int level)
631 {
632 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
633 insert_slot_to_list (temp, temp_slots_at_level (level));
634 temp->level = level;
635 }
636
637 /* Make temporary slot TEMP available. */
638
639 static void
640 make_slot_available (struct temp_slot *temp)
641 {
642 cut_slot_from_list (temp, temp_slots_at_level (temp->level));
643 insert_slot_to_list (temp, &avail_temp_slots);
644 temp->in_use = 0;
645 temp->level = -1;
646 n_temp_slots_in_use--;
647 }
648
649 /* Compute the hash value for an address -> temp slot mapping.
650 The value is cached on the mapping entry. */
651 static hashval_t
652 temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
653 {
654 int do_not_record = 0;
655 return hash_rtx (t->address, GET_MODE (t->address),
656 &do_not_record, NULL, false);
657 }
658
659 /* Return the hash value for an address -> temp slot mapping. */
660 hashval_t
661 temp_address_hasher::hash (temp_slot_address_entry *t)
662 {
663 return t->hash;
664 }
665
666 /* Compare two address -> temp slot mapping entries. */
667 bool
668 temp_address_hasher::equal (temp_slot_address_entry *t1,
669 temp_slot_address_entry *t2)
670 {
671 return exp_equiv_p (t1->address, t2->address, 0, true);
672 }
673
674 /* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
675 static void
676 insert_temp_slot_address (rtx address, struct temp_slot *temp_slot)
677 {
678 struct temp_slot_address_entry *t = ggc_alloc<temp_slot_address_entry> ();
679 t->address = address;
680 t->temp_slot = temp_slot;
681 t->hash = temp_slot_address_compute_hash (t);
682 *temp_slot_address_table->find_slot_with_hash (t, t->hash, INSERT) = t;
683 }
684
685 /* Remove an address -> temp slot mapping entry if the temp slot is
686 not in use anymore. Callback for remove_unused_temp_slot_addresses. */
687 int
688 remove_unused_temp_slot_addresses_1 (temp_slot_address_entry **slot, void *)
689 {
690 const struct temp_slot_address_entry *t = *slot;
691 if (! t->temp_slot->in_use)
692 temp_slot_address_table->clear_slot (slot);
693 return 1;
694 }
695
696 /* Remove all mappings of addresses to unused temp slots. */
697 static void
698 remove_unused_temp_slot_addresses (void)
699 {
700 /* Use quicker clearing if there aren't any active temp slots. */
701 if (n_temp_slots_in_use)
702 temp_slot_address_table->traverse
703 <void *, remove_unused_temp_slot_addresses_1> (NULL);
704 else
705 temp_slot_address_table->empty ();
706 }
707
708 /* Find the temp slot corresponding to the object at address X. */
709
710 static struct temp_slot *
711 find_temp_slot_from_address (rtx x)
712 {
713 struct temp_slot *p;
714 struct temp_slot_address_entry tmp, *t;
715
716 /* First try the easy way:
717 See if X exists in the address -> temp slot mapping. */
718 tmp.address = x;
719 tmp.temp_slot = NULL;
720 tmp.hash = temp_slot_address_compute_hash (&tmp);
721 t = temp_slot_address_table->find_with_hash (&tmp, tmp.hash);
722 if (t)
723 return t->temp_slot;
724
725 /* If we have a sum involving a register, see if it points to a temp
726 slot. */
727 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
728 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
729 return p;
730 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
731 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
732 return p;
733
734 /* Last resort: Address is a virtual stack var address. */
735 if (GET_CODE (x) == PLUS
736 && XEXP (x, 0) == virtual_stack_vars_rtx
737 && CONST_INT_P (XEXP (x, 1)))
738 {
739 int i;
740 for (i = max_slot_level (); i >= 0; i--)
741 for (p = *temp_slots_at_level (i); p; p = p->next)
742 {
743 if (INTVAL (XEXP (x, 1)) >= p->base_offset
744 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size)
745 return p;
746 }
747 }
748
749 return NULL;
750 }
751 \f
752 /* Allocate a temporary stack slot and record it for possible later
753 reuse.
754
755 MODE is the machine mode to be given to the returned rtx.
756
757 SIZE is the size in units of the space required. We do no rounding here
758 since assign_stack_local will do any required rounding.
759
760 TYPE is the type that will be used for the stack slot. */
761
762 rtx
763 assign_stack_temp_for_type (machine_mode mode, HOST_WIDE_INT size,
764 tree type)
765 {
766 unsigned int align;
767 struct temp_slot *p, *best_p = 0, *selected = NULL, **pp;
768 rtx slot;
769
770 /* If SIZE is -1 it means that somebody tried to allocate a temporary
771 of a variable size. */
772 gcc_assert (size != -1);
773
774 align = get_stack_local_alignment (type, mode);
775
776 /* Try to find an available, already-allocated temporary of the proper
777 mode which meets the size and alignment requirements. Choose the
778 smallest one with the closest alignment.
779
780 If assign_stack_temp is called outside of the tree->rtl expansion,
781 we cannot reuse the stack slots (that may still refer to
782 VIRTUAL_STACK_VARS_REGNUM). */
783 if (!virtuals_instantiated)
784 {
785 for (p = avail_temp_slots; p; p = p->next)
786 {
787 if (p->align >= align && p->size >= size
788 && GET_MODE (p->slot) == mode
789 && objects_must_conflict_p (p->type, type)
790 && (best_p == 0 || best_p->size > p->size
791 || (best_p->size == p->size && best_p->align > p->align)))
792 {
793 if (p->align == align && p->size == size)
794 {
795 selected = p;
796 cut_slot_from_list (selected, &avail_temp_slots);
797 best_p = 0;
798 break;
799 }
800 best_p = p;
801 }
802 }
803 }
804
805 /* Make our best, if any, the one to use. */
806 if (best_p)
807 {
808 selected = best_p;
809 cut_slot_from_list (selected, &avail_temp_slots);
810
811 /* If there are enough aligned bytes left over, make them into a new
812 temp_slot so that the extra bytes don't get wasted. Do this only
813 for BLKmode slots, so that we can be sure of the alignment. */
814 if (GET_MODE (best_p->slot) == BLKmode)
815 {
816 int alignment = best_p->align / BITS_PER_UNIT;
817 HOST_WIDE_INT rounded_size = CEIL_ROUND (size, alignment);
818
819 if (best_p->size - rounded_size >= alignment)
820 {
821 p = ggc_alloc<temp_slot> ();
822 p->in_use = 0;
823 p->size = best_p->size - rounded_size;
824 p->base_offset = best_p->base_offset + rounded_size;
825 p->full_size = best_p->full_size - rounded_size;
826 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
827 p->align = best_p->align;
828 p->type = best_p->type;
829 insert_slot_to_list (p, &avail_temp_slots);
830
831 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, p->slot,
832 stack_slot_list);
833
834 best_p->size = rounded_size;
835 best_p->full_size = rounded_size;
836 }
837 }
838 }
839
840 /* If we still didn't find one, make a new temporary. */
841 if (selected == 0)
842 {
843 HOST_WIDE_INT frame_offset_old = frame_offset;
844
845 p = ggc_alloc<temp_slot> ();
846
847 /* We are passing an explicit alignment request to assign_stack_local.
848 One side effect of that is assign_stack_local will not round SIZE
849 to ensure the frame offset remains suitably aligned.
850
851 So for requests which depended on the rounding of SIZE, we go ahead
852 and round it now. We also make sure ALIGNMENT is at least
853 BIGGEST_ALIGNMENT. */
854 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
855 p->slot = assign_stack_local_1 (mode,
856 (mode == BLKmode
857 ? CEIL_ROUND (size,
858 (int) align
859 / BITS_PER_UNIT)
860 : size),
861 align, 0);
862
863 p->align = align;
864
865 /* The following slot size computation is necessary because we don't
866 know the actual size of the temporary slot until assign_stack_local
867 has performed all the frame alignment and size rounding for the
868 requested temporary. Note that extra space added for alignment
869 can be either above or below this stack slot depending on which
870 way the frame grows. We include the extra space if and only if it
871 is above this slot. */
872 if (FRAME_GROWS_DOWNWARD)
873 p->size = frame_offset_old - frame_offset;
874 else
875 p->size = size;
876
877 /* Now define the fields used by combine_temp_slots. */
878 if (FRAME_GROWS_DOWNWARD)
879 {
880 p->base_offset = frame_offset;
881 p->full_size = frame_offset_old - frame_offset;
882 }
883 else
884 {
885 p->base_offset = frame_offset_old;
886 p->full_size = frame_offset - frame_offset_old;
887 }
888
889 selected = p;
890 }
891
892 p = selected;
893 p->in_use = 1;
894 p->type = type;
895 p->level = temp_slot_level;
896 n_temp_slots_in_use++;
897
898 pp = temp_slots_at_level (p->level);
899 insert_slot_to_list (p, pp);
900 insert_temp_slot_address (XEXP (p->slot, 0), p);
901
902 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
903 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
904 stack_slot_list = gen_rtx_EXPR_LIST (VOIDmode, slot, stack_slot_list);
905
906 /* If we know the alias set for the memory that will be used, use
907 it. If there's no TYPE, then we don't know anything about the
908 alias set for the memory. */
909 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
910 set_mem_align (slot, align);
911
912 /* If a type is specified, set the relevant flags. */
913 if (type != 0)
914 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
915 MEM_NOTRAP_P (slot) = 1;
916
917 return slot;
918 }
919
920 /* Allocate a temporary stack slot and record it for possible later
921 reuse. First two arguments are same as in preceding function. */
922
923 rtx
924 assign_stack_temp (machine_mode mode, HOST_WIDE_INT size)
925 {
926 return assign_stack_temp_for_type (mode, size, NULL_TREE);
927 }
928 \f
929 /* Assign a temporary.
930 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
931 and so that should be used in error messages. In either case, we
932 allocate of the given type.
933 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
934 it is 0 if a register is OK.
935 DONT_PROMOTE is 1 if we should not promote values in register
936 to wider modes. */
937
938 rtx
939 assign_temp (tree type_or_decl, int memory_required,
940 int dont_promote ATTRIBUTE_UNUSED)
941 {
942 tree type, decl;
943 machine_mode mode;
944 #ifdef PROMOTE_MODE
945 int unsignedp;
946 #endif
947
948 if (DECL_P (type_or_decl))
949 decl = type_or_decl, type = TREE_TYPE (decl);
950 else
951 decl = NULL, type = type_or_decl;
952
953 mode = TYPE_MODE (type);
954 #ifdef PROMOTE_MODE
955 unsignedp = TYPE_UNSIGNED (type);
956 #endif
957
958 if (mode == BLKmode || memory_required)
959 {
960 HOST_WIDE_INT size = int_size_in_bytes (type);
961 rtx tmp;
962
963 /* Zero sized arrays are GNU C extension. Set size to 1 to avoid
964 problems with allocating the stack space. */
965 if (size == 0)
966 size = 1;
967
968 /* Unfortunately, we don't yet know how to allocate variable-sized
969 temporaries. However, sometimes we can find a fixed upper limit on
970 the size, so try that instead. */
971 else if (size == -1)
972 size = max_int_size_in_bytes (type);
973
974 /* The size of the temporary may be too large to fit into an integer. */
975 /* ??? Not sure this should happen except for user silliness, so limit
976 this to things that aren't compiler-generated temporaries. The
977 rest of the time we'll die in assign_stack_temp_for_type. */
978 if (decl && size == -1
979 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
980 {
981 error ("size of variable %q+D is too large", decl);
982 size = 1;
983 }
984
985 tmp = assign_stack_temp_for_type (mode, size, type);
986 return tmp;
987 }
988
989 #ifdef PROMOTE_MODE
990 if (! dont_promote)
991 mode = promote_mode (type, mode, &unsignedp);
992 #endif
993
994 return gen_reg_rtx (mode);
995 }
996 \f
997 /* Combine temporary stack slots which are adjacent on the stack.
998
999 This allows for better use of already allocated stack space. This is only
1000 done for BLKmode slots because we can be sure that we won't have alignment
1001 problems in this case. */
1002
1003 static void
1004 combine_temp_slots (void)
1005 {
1006 struct temp_slot *p, *q, *next, *next_q;
1007 int num_slots;
1008
1009 /* We can't combine slots, because the information about which slot
1010 is in which alias set will be lost. */
1011 if (flag_strict_aliasing)
1012 return;
1013
1014 /* If there are a lot of temp slots, don't do anything unless
1015 high levels of optimization. */
1016 if (! flag_expensive_optimizations)
1017 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
1018 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1019 return;
1020
1021 for (p = avail_temp_slots; p; p = next)
1022 {
1023 int delete_p = 0;
1024
1025 next = p->next;
1026
1027 if (GET_MODE (p->slot) != BLKmode)
1028 continue;
1029
1030 for (q = p->next; q; q = next_q)
1031 {
1032 int delete_q = 0;
1033
1034 next_q = q->next;
1035
1036 if (GET_MODE (q->slot) != BLKmode)
1037 continue;
1038
1039 if (p->base_offset + p->full_size == q->base_offset)
1040 {
1041 /* Q comes after P; combine Q into P. */
1042 p->size += q->size;
1043 p->full_size += q->full_size;
1044 delete_q = 1;
1045 }
1046 else if (q->base_offset + q->full_size == p->base_offset)
1047 {
1048 /* P comes after Q; combine P into Q. */
1049 q->size += p->size;
1050 q->full_size += p->full_size;
1051 delete_p = 1;
1052 break;
1053 }
1054 if (delete_q)
1055 cut_slot_from_list (q, &avail_temp_slots);
1056 }
1057
1058 /* Either delete P or advance past it. */
1059 if (delete_p)
1060 cut_slot_from_list (p, &avail_temp_slots);
1061 }
1062 }
1063 \f
1064 /* Indicate that NEW_RTX is an alternate way of referring to the temp
1065 slot that previously was known by OLD_RTX. */
1066
1067 void
1068 update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1069 {
1070 struct temp_slot *p;
1071
1072 if (rtx_equal_p (old_rtx, new_rtx))
1073 return;
1074
1075 p = find_temp_slot_from_address (old_rtx);
1076
1077 /* If we didn't find one, see if both OLD_RTX is a PLUS. If so, and
1078 NEW_RTX is a register, see if one operand of the PLUS is a
1079 temporary location. If so, NEW_RTX points into it. Otherwise,
1080 if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1081 in common between them. If so, try a recursive call on those
1082 values. */
1083 if (p == 0)
1084 {
1085 if (GET_CODE (old_rtx) != PLUS)
1086 return;
1087
1088 if (REG_P (new_rtx))
1089 {
1090 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1091 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1092 return;
1093 }
1094 else if (GET_CODE (new_rtx) != PLUS)
1095 return;
1096
1097 if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1098 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1099 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1100 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1101 else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1102 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1103 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1104 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
1105
1106 return;
1107 }
1108
1109 /* Otherwise add an alias for the temp's address. */
1110 insert_temp_slot_address (new_rtx, p);
1111 }
1112
1113 /* If X could be a reference to a temporary slot, mark that slot as
1114 belonging to the to one level higher than the current level. If X
1115 matched one of our slots, just mark that one. Otherwise, we can't
1116 easily predict which it is, so upgrade all of them.
1117
1118 This is called when an ({...}) construct occurs and a statement
1119 returns a value in memory. */
1120
1121 void
1122 preserve_temp_slots (rtx x)
1123 {
1124 struct temp_slot *p = 0, *next;
1125
1126 if (x == 0)
1127 return;
1128
1129 /* If X is a register that is being used as a pointer, see if we have
1130 a temporary slot we know it points to. */
1131 if (REG_P (x) && REG_POINTER (x))
1132 p = find_temp_slot_from_address (x);
1133
1134 /* If X is not in memory or is at a constant address, it cannot be in
1135 a temporary slot. */
1136 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1137 return;
1138
1139 /* First see if we can find a match. */
1140 if (p == 0)
1141 p = find_temp_slot_from_address (XEXP (x, 0));
1142
1143 if (p != 0)
1144 {
1145 if (p->level == temp_slot_level)
1146 move_slot_to_level (p, temp_slot_level - 1);
1147 return;
1148 }
1149
1150 /* Otherwise, preserve all non-kept slots at this level. */
1151 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1152 {
1153 next = p->next;
1154 move_slot_to_level (p, temp_slot_level - 1);
1155 }
1156 }
1157
1158 /* Free all temporaries used so far. This is normally called at the
1159 end of generating code for a statement. */
1160
1161 void
1162 free_temp_slots (void)
1163 {
1164 struct temp_slot *p, *next;
1165 bool some_available = false;
1166
1167 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1168 {
1169 next = p->next;
1170 make_slot_available (p);
1171 some_available = true;
1172 }
1173
1174 if (some_available)
1175 {
1176 remove_unused_temp_slot_addresses ();
1177 combine_temp_slots ();
1178 }
1179 }
1180
1181 /* Push deeper into the nesting level for stack temporaries. */
1182
1183 void
1184 push_temp_slots (void)
1185 {
1186 temp_slot_level++;
1187 }
1188
1189 /* Pop a temporary nesting level. All slots in use in the current level
1190 are freed. */
1191
1192 void
1193 pop_temp_slots (void)
1194 {
1195 free_temp_slots ();
1196 temp_slot_level--;
1197 }
1198
1199 /* Initialize temporary slots. */
1200
1201 void
1202 init_temp_slots (void)
1203 {
1204 /* We have not allocated any temporaries yet. */
1205 avail_temp_slots = 0;
1206 vec_alloc (used_temp_slots, 0);
1207 temp_slot_level = 0;
1208 n_temp_slots_in_use = 0;
1209
1210 /* Set up the table to map addresses to temp slots. */
1211 if (! temp_slot_address_table)
1212 temp_slot_address_table = hash_table<temp_address_hasher>::create_ggc (32);
1213 else
1214 temp_slot_address_table->empty ();
1215 }
1216 \f
1217 /* Functions and data structures to keep track of the values hard regs
1218 had at the start of the function. */
1219
1220 /* Private type used by get_hard_reg_initial_reg, get_hard_reg_initial_val,
1221 and has_hard_reg_initial_val.. */
1222 struct GTY(()) initial_value_pair {
1223 rtx hard_reg;
1224 rtx pseudo;
1225 };
1226 /* ??? This could be a VEC but there is currently no way to define an
1227 opaque VEC type. This could be worked around by defining struct
1228 initial_value_pair in function.h. */
1229 struct GTY(()) initial_value_struct {
1230 int num_entries;
1231 int max_entries;
1232 initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
1233 };
1234
1235 /* If a pseudo represents an initial hard reg (or expression), return
1236 it, else return NULL_RTX. */
1237
1238 rtx
1239 get_hard_reg_initial_reg (rtx reg)
1240 {
1241 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1242 int i;
1243
1244 if (ivs == 0)
1245 return NULL_RTX;
1246
1247 for (i = 0; i < ivs->num_entries; i++)
1248 if (rtx_equal_p (ivs->entries[i].pseudo, reg))
1249 return ivs->entries[i].hard_reg;
1250
1251 return NULL_RTX;
1252 }
1253
1254 /* Make sure that there's a pseudo register of mode MODE that stores the
1255 initial value of hard register REGNO. Return an rtx for such a pseudo. */
1256
1257 rtx
1258 get_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1259 {
1260 struct initial_value_struct *ivs;
1261 rtx rv;
1262
1263 rv = has_hard_reg_initial_val (mode, regno);
1264 if (rv)
1265 return rv;
1266
1267 ivs = crtl->hard_reg_initial_vals;
1268 if (ivs == 0)
1269 {
1270 ivs = ggc_alloc<initial_value_struct> ();
1271 ivs->num_entries = 0;
1272 ivs->max_entries = 5;
1273 ivs->entries = ggc_vec_alloc<initial_value_pair> (5);
1274 crtl->hard_reg_initial_vals = ivs;
1275 }
1276
1277 if (ivs->num_entries >= ivs->max_entries)
1278 {
1279 ivs->max_entries += 5;
1280 ivs->entries = GGC_RESIZEVEC (initial_value_pair, ivs->entries,
1281 ivs->max_entries);
1282 }
1283
1284 ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
1285 ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
1286
1287 return ivs->entries[ivs->num_entries++].pseudo;
1288 }
1289
1290 /* See if get_hard_reg_initial_val has been used to create a pseudo
1291 for the initial value of hard register REGNO in mode MODE. Return
1292 the associated pseudo if so, otherwise return NULL. */
1293
1294 rtx
1295 has_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1296 {
1297 struct initial_value_struct *ivs;
1298 int i;
1299
1300 ivs = crtl->hard_reg_initial_vals;
1301 if (ivs != 0)
1302 for (i = 0; i < ivs->num_entries; i++)
1303 if (GET_MODE (ivs->entries[i].hard_reg) == mode
1304 && REGNO (ivs->entries[i].hard_reg) == regno)
1305 return ivs->entries[i].pseudo;
1306
1307 return NULL_RTX;
1308 }
1309
1310 unsigned int
1311 emit_initial_value_sets (void)
1312 {
1313 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1314 int i;
1315 rtx_insn *seq;
1316
1317 if (ivs == 0)
1318 return 0;
1319
1320 start_sequence ();
1321 for (i = 0; i < ivs->num_entries; i++)
1322 emit_move_insn (ivs->entries[i].pseudo, ivs->entries[i].hard_reg);
1323 seq = get_insns ();
1324 end_sequence ();
1325
1326 emit_insn_at_entry (seq);
1327 return 0;
1328 }
1329
1330 /* Return the hardreg-pseudoreg initial values pair entry I and
1331 TRUE if I is a valid entry, or FALSE if I is not a valid entry. */
1332 bool
1333 initial_value_entry (int i, rtx *hreg, rtx *preg)
1334 {
1335 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1336 if (!ivs || i >= ivs->num_entries)
1337 return false;
1338
1339 *hreg = ivs->entries[i].hard_reg;
1340 *preg = ivs->entries[i].pseudo;
1341 return true;
1342 }
1343 \f
1344 /* These routines are responsible for converting virtual register references
1345 to the actual hard register references once RTL generation is complete.
1346
1347 The following four variables are used for communication between the
1348 routines. They contain the offsets of the virtual registers from their
1349 respective hard registers. */
1350
1351 static int in_arg_offset;
1352 static int var_offset;
1353 static int dynamic_offset;
1354 static int out_arg_offset;
1355 static int cfa_offset;
1356
1357 /* In most machines, the stack pointer register is equivalent to the bottom
1358 of the stack. */
1359
1360 #ifndef STACK_POINTER_OFFSET
1361 #define STACK_POINTER_OFFSET 0
1362 #endif
1363
1364 #if defined (REG_PARM_STACK_SPACE) && !defined (INCOMING_REG_PARM_STACK_SPACE)
1365 #define INCOMING_REG_PARM_STACK_SPACE REG_PARM_STACK_SPACE
1366 #endif
1367
1368 /* If not defined, pick an appropriate default for the offset of dynamically
1369 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1370 INCOMING_REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1371
1372 #ifndef STACK_DYNAMIC_OFFSET
1373
1374 /* The bottom of the stack points to the actual arguments. If
1375 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1376 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1377 stack space for register parameters is not pushed by the caller, but
1378 rather part of the fixed stack areas and hence not included in
1379 `crtl->outgoing_args_size'. Nevertheless, we must allow
1380 for it when allocating stack dynamic objects. */
1381
1382 #ifdef INCOMING_REG_PARM_STACK_SPACE
1383 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1384 ((ACCUMULATE_OUTGOING_ARGS \
1385 ? (crtl->outgoing_args_size \
1386 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1387 : INCOMING_REG_PARM_STACK_SPACE (FNDECL))) \
1388 : 0) + (STACK_POINTER_OFFSET))
1389 #else
1390 #define STACK_DYNAMIC_OFFSET(FNDECL) \
1391 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0) \
1392 + (STACK_POINTER_OFFSET))
1393 #endif
1394 #endif
1395
1396 \f
1397 /* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1398 is a virtual register, return the equivalent hard register and set the
1399 offset indirectly through the pointer. Otherwise, return 0. */
1400
1401 static rtx
1402 instantiate_new_reg (rtx x, HOST_WIDE_INT *poffset)
1403 {
1404 rtx new_rtx;
1405 HOST_WIDE_INT offset;
1406
1407 if (x == virtual_incoming_args_rtx)
1408 {
1409 if (stack_realign_drap)
1410 {
1411 /* Replace virtual_incoming_args_rtx with internal arg
1412 pointer if DRAP is used to realign stack. */
1413 new_rtx = crtl->args.internal_arg_pointer;
1414 offset = 0;
1415 }
1416 else
1417 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1418 }
1419 else if (x == virtual_stack_vars_rtx)
1420 new_rtx = frame_pointer_rtx, offset = var_offset;
1421 else if (x == virtual_stack_dynamic_rtx)
1422 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1423 else if (x == virtual_outgoing_args_rtx)
1424 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1425 else if (x == virtual_cfa_rtx)
1426 {
1427 #ifdef FRAME_POINTER_CFA_OFFSET
1428 new_rtx = frame_pointer_rtx;
1429 #else
1430 new_rtx = arg_pointer_rtx;
1431 #endif
1432 offset = cfa_offset;
1433 }
1434 else if (x == virtual_preferred_stack_boundary_rtx)
1435 {
1436 new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1437 offset = 0;
1438 }
1439 else
1440 return NULL_RTX;
1441
1442 *poffset = offset;
1443 return new_rtx;
1444 }
1445
1446 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1447 registers present inside of *LOC. The expression is simplified,
1448 as much as possible, but is not to be considered "valid" in any sense
1449 implied by the target. Return true if any change is made. */
1450
1451 static bool
1452 instantiate_virtual_regs_in_rtx (rtx *loc)
1453 {
1454 if (!*loc)
1455 return false;
1456 bool changed = false;
1457 subrtx_ptr_iterator::array_type array;
1458 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
1459 {
1460 rtx *loc = *iter;
1461 if (rtx x = *loc)
1462 {
1463 rtx new_rtx;
1464 HOST_WIDE_INT offset;
1465 switch (GET_CODE (x))
1466 {
1467 case REG:
1468 new_rtx = instantiate_new_reg (x, &offset);
1469 if (new_rtx)
1470 {
1471 *loc = plus_constant (GET_MODE (x), new_rtx, offset);
1472 changed = true;
1473 }
1474 iter.skip_subrtxes ();
1475 break;
1476
1477 case PLUS:
1478 new_rtx = instantiate_new_reg (XEXP (x, 0), &offset);
1479 if (new_rtx)
1480 {
1481 XEXP (x, 0) = new_rtx;
1482 *loc = plus_constant (GET_MODE (x), x, offset, true);
1483 changed = true;
1484 iter.skip_subrtxes ();
1485 break;
1486 }
1487
1488 /* FIXME -- from old code */
1489 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1490 we can commute the PLUS and SUBREG because pointers into the
1491 frame are well-behaved. */
1492 break;
1493
1494 default:
1495 break;
1496 }
1497 }
1498 }
1499 return changed;
1500 }
1501
1502 /* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1503 matches the predicate for insn CODE operand OPERAND. */
1504
1505 static int
1506 safe_insn_predicate (int code, int operand, rtx x)
1507 {
1508 return code < 0 || insn_operand_matches ((enum insn_code) code, operand, x);
1509 }
1510
1511 /* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1512 registers present inside of insn. The result will be a valid insn. */
1513
1514 static void
1515 instantiate_virtual_regs_in_insn (rtx_insn *insn)
1516 {
1517 HOST_WIDE_INT offset;
1518 int insn_code, i;
1519 bool any_change = false;
1520 rtx set, new_rtx, x;
1521 rtx_insn *seq;
1522
1523 /* There are some special cases to be handled first. */
1524 set = single_set (insn);
1525 if (set)
1526 {
1527 /* We're allowed to assign to a virtual register. This is interpreted
1528 to mean that the underlying register gets assigned the inverse
1529 transformation. This is used, for example, in the handling of
1530 non-local gotos. */
1531 new_rtx = instantiate_new_reg (SET_DEST (set), &offset);
1532 if (new_rtx)
1533 {
1534 start_sequence ();
1535
1536 instantiate_virtual_regs_in_rtx (&SET_SRC (set));
1537 x = simplify_gen_binary (PLUS, GET_MODE (new_rtx), SET_SRC (set),
1538 gen_int_mode (-offset, GET_MODE (new_rtx)));
1539 x = force_operand (x, new_rtx);
1540 if (x != new_rtx)
1541 emit_move_insn (new_rtx, x);
1542
1543 seq = get_insns ();
1544 end_sequence ();
1545
1546 emit_insn_before (seq, insn);
1547 delete_insn (insn);
1548 return;
1549 }
1550
1551 /* Handle a straight copy from a virtual register by generating a
1552 new add insn. The difference between this and falling through
1553 to the generic case is avoiding a new pseudo and eliminating a
1554 move insn in the initial rtl stream. */
1555 new_rtx = instantiate_new_reg (SET_SRC (set), &offset);
1556 if (new_rtx && offset != 0
1557 && REG_P (SET_DEST (set))
1558 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1559 {
1560 start_sequence ();
1561
1562 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS, new_rtx,
1563 gen_int_mode (offset,
1564 GET_MODE (SET_DEST (set))),
1565 SET_DEST (set), 1, OPTAB_LIB_WIDEN);
1566 if (x != SET_DEST (set))
1567 emit_move_insn (SET_DEST (set), x);
1568
1569 seq = get_insns ();
1570 end_sequence ();
1571
1572 emit_insn_before (seq, insn);
1573 delete_insn (insn);
1574 return;
1575 }
1576
1577 extract_insn (insn);
1578 insn_code = INSN_CODE (insn);
1579
1580 /* Handle a plus involving a virtual register by determining if the
1581 operands remain valid if they're modified in place. */
1582 if (GET_CODE (SET_SRC (set)) == PLUS
1583 && recog_data.n_operands >= 3
1584 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1585 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1586 && CONST_INT_P (recog_data.operand[2])
1587 && (new_rtx = instantiate_new_reg (recog_data.operand[1], &offset)))
1588 {
1589 offset += INTVAL (recog_data.operand[2]);
1590
1591 /* If the sum is zero, then replace with a plain move. */
1592 if (offset == 0
1593 && REG_P (SET_DEST (set))
1594 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1595 {
1596 start_sequence ();
1597 emit_move_insn (SET_DEST (set), new_rtx);
1598 seq = get_insns ();
1599 end_sequence ();
1600
1601 emit_insn_before (seq, insn);
1602 delete_insn (insn);
1603 return;
1604 }
1605
1606 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1607
1608 /* Using validate_change and apply_change_group here leaves
1609 recog_data in an invalid state. Since we know exactly what
1610 we want to check, do those two by hand. */
1611 if (safe_insn_predicate (insn_code, 1, new_rtx)
1612 && safe_insn_predicate (insn_code, 2, x))
1613 {
1614 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1615 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1616 any_change = true;
1617
1618 /* Fall through into the regular operand fixup loop in
1619 order to take care of operands other than 1 and 2. */
1620 }
1621 }
1622 }
1623 else
1624 {
1625 extract_insn (insn);
1626 insn_code = INSN_CODE (insn);
1627 }
1628
1629 /* In the general case, we expect virtual registers to appear only in
1630 operands, and then only as either bare registers or inside memories. */
1631 for (i = 0; i < recog_data.n_operands; ++i)
1632 {
1633 x = recog_data.operand[i];
1634 switch (GET_CODE (x))
1635 {
1636 case MEM:
1637 {
1638 rtx addr = XEXP (x, 0);
1639
1640 if (!instantiate_virtual_regs_in_rtx (&addr))
1641 continue;
1642
1643 start_sequence ();
1644 x = replace_equiv_address (x, addr, true);
1645 /* It may happen that the address with the virtual reg
1646 was valid (e.g. based on the virtual stack reg, which might
1647 be acceptable to the predicates with all offsets), whereas
1648 the address now isn't anymore, for instance when the address
1649 is still offsetted, but the base reg isn't virtual-stack-reg
1650 anymore. Below we would do a force_reg on the whole operand,
1651 but this insn might actually only accept memory. Hence,
1652 before doing that last resort, try to reload the address into
1653 a register, so this operand stays a MEM. */
1654 if (!safe_insn_predicate (insn_code, i, x))
1655 {
1656 addr = force_reg (GET_MODE (addr), addr);
1657 x = replace_equiv_address (x, addr, true);
1658 }
1659 seq = get_insns ();
1660 end_sequence ();
1661 if (seq)
1662 emit_insn_before (seq, insn);
1663 }
1664 break;
1665
1666 case REG:
1667 new_rtx = instantiate_new_reg (x, &offset);
1668 if (new_rtx == NULL)
1669 continue;
1670 if (offset == 0)
1671 x = new_rtx;
1672 else
1673 {
1674 start_sequence ();
1675
1676 /* Careful, special mode predicates may have stuff in
1677 insn_data[insn_code].operand[i].mode that isn't useful
1678 to us for computing a new value. */
1679 /* ??? Recognize address_operand and/or "p" constraints
1680 to see if (plus new offset) is a valid before we put
1681 this through expand_simple_binop. */
1682 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1683 gen_int_mode (offset, GET_MODE (x)),
1684 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1685 seq = get_insns ();
1686 end_sequence ();
1687 emit_insn_before (seq, insn);
1688 }
1689 break;
1690
1691 case SUBREG:
1692 new_rtx = instantiate_new_reg (SUBREG_REG (x), &offset);
1693 if (new_rtx == NULL)
1694 continue;
1695 if (offset != 0)
1696 {
1697 start_sequence ();
1698 new_rtx = expand_simple_binop
1699 (GET_MODE (new_rtx), PLUS, new_rtx,
1700 gen_int_mode (offset, GET_MODE (new_rtx)),
1701 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1702 seq = get_insns ();
1703 end_sequence ();
1704 emit_insn_before (seq, insn);
1705 }
1706 x = simplify_gen_subreg (recog_data.operand_mode[i], new_rtx,
1707 GET_MODE (new_rtx), SUBREG_BYTE (x));
1708 gcc_assert (x);
1709 break;
1710
1711 default:
1712 continue;
1713 }
1714
1715 /* At this point, X contains the new value for the operand.
1716 Validate the new value vs the insn predicate. Note that
1717 asm insns will have insn_code -1 here. */
1718 if (!safe_insn_predicate (insn_code, i, x))
1719 {
1720 start_sequence ();
1721 if (REG_P (x))
1722 {
1723 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1724 x = copy_to_reg (x);
1725 }
1726 else
1727 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1728 seq = get_insns ();
1729 end_sequence ();
1730 if (seq)
1731 emit_insn_before (seq, insn);
1732 }
1733
1734 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1735 any_change = true;
1736 }
1737
1738 if (any_change)
1739 {
1740 /* Propagate operand changes into the duplicates. */
1741 for (i = 0; i < recog_data.n_dups; ++i)
1742 *recog_data.dup_loc[i]
1743 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1744
1745 /* Force re-recognition of the instruction for validation. */
1746 INSN_CODE (insn) = -1;
1747 }
1748
1749 if (asm_noperands (PATTERN (insn)) >= 0)
1750 {
1751 if (!check_asm_operands (PATTERN (insn)))
1752 {
1753 error_for_asm (insn, "impossible constraint in %<asm%>");
1754 /* For asm goto, instead of fixing up all the edges
1755 just clear the template and clear input operands
1756 (asm goto doesn't have any output operands). */
1757 if (JUMP_P (insn))
1758 {
1759 rtx asm_op = extract_asm_operands (PATTERN (insn));
1760 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1761 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1762 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1763 }
1764 else
1765 delete_insn (insn);
1766 }
1767 }
1768 else
1769 {
1770 if (recog_memoized (insn) < 0)
1771 fatal_insn_not_found (insn);
1772 }
1773 }
1774
1775 /* Subroutine of instantiate_decls. Given RTL representing a decl,
1776 do any instantiation required. */
1777
1778 void
1779 instantiate_decl_rtl (rtx x)
1780 {
1781 rtx addr;
1782
1783 if (x == 0)
1784 return;
1785
1786 /* If this is a CONCAT, recurse for the pieces. */
1787 if (GET_CODE (x) == CONCAT)
1788 {
1789 instantiate_decl_rtl (XEXP (x, 0));
1790 instantiate_decl_rtl (XEXP (x, 1));
1791 return;
1792 }
1793
1794 /* If this is not a MEM, no need to do anything. Similarly if the
1795 address is a constant or a register that is not a virtual register. */
1796 if (!MEM_P (x))
1797 return;
1798
1799 addr = XEXP (x, 0);
1800 if (CONSTANT_P (addr)
1801 || (REG_P (addr)
1802 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
1803 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
1804 return;
1805
1806 instantiate_virtual_regs_in_rtx (&XEXP (x, 0));
1807 }
1808
1809 /* Helper for instantiate_decls called via walk_tree: Process all decls
1810 in the given DECL_VALUE_EXPR. */
1811
1812 static tree
1813 instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1814 {
1815 tree t = *tp;
1816 if (! EXPR_P (t))
1817 {
1818 *walk_subtrees = 0;
1819 if (DECL_P (t))
1820 {
1821 if (DECL_RTL_SET_P (t))
1822 instantiate_decl_rtl (DECL_RTL (t));
1823 if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1824 && DECL_INCOMING_RTL (t))
1825 instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1826 if ((TREE_CODE (t) == VAR_DECL
1827 || TREE_CODE (t) == RESULT_DECL)
1828 && DECL_HAS_VALUE_EXPR_P (t))
1829 {
1830 tree v = DECL_VALUE_EXPR (t);
1831 walk_tree (&v, instantiate_expr, NULL, NULL);
1832 }
1833 }
1834 }
1835 return NULL;
1836 }
1837
1838 /* Subroutine of instantiate_decls: Process all decls in the given
1839 BLOCK node and all its subblocks. */
1840
1841 static void
1842 instantiate_decls_1 (tree let)
1843 {
1844 tree t;
1845
1846 for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1847 {
1848 if (DECL_RTL_SET_P (t))
1849 instantiate_decl_rtl (DECL_RTL (t));
1850 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t))
1851 {
1852 tree v = DECL_VALUE_EXPR (t);
1853 walk_tree (&v, instantiate_expr, NULL, NULL);
1854 }
1855 }
1856
1857 /* Process all subblocks. */
1858 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1859 instantiate_decls_1 (t);
1860 }
1861
1862 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
1863 all virtual registers in their DECL_RTL's. */
1864
1865 static void
1866 instantiate_decls (tree fndecl)
1867 {
1868 tree decl;
1869 unsigned ix;
1870
1871 /* Process all parameters of the function. */
1872 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1873 {
1874 instantiate_decl_rtl (DECL_RTL (decl));
1875 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1876 if (DECL_HAS_VALUE_EXPR_P (decl))
1877 {
1878 tree v = DECL_VALUE_EXPR (decl);
1879 walk_tree (&v, instantiate_expr, NULL, NULL);
1880 }
1881 }
1882
1883 if ((decl = DECL_RESULT (fndecl))
1884 && TREE_CODE (decl) == RESULT_DECL)
1885 {
1886 if (DECL_RTL_SET_P (decl))
1887 instantiate_decl_rtl (DECL_RTL (decl));
1888 if (DECL_HAS_VALUE_EXPR_P (decl))
1889 {
1890 tree v = DECL_VALUE_EXPR (decl);
1891 walk_tree (&v, instantiate_expr, NULL, NULL);
1892 }
1893 }
1894
1895 /* Process the saved static chain if it exists. */
1896 decl = DECL_STRUCT_FUNCTION (fndecl)->static_chain_decl;
1897 if (decl && DECL_HAS_VALUE_EXPR_P (decl))
1898 instantiate_decl_rtl (DECL_RTL (DECL_VALUE_EXPR (decl)));
1899
1900 /* Now process all variables defined in the function or its subblocks. */
1901 instantiate_decls_1 (DECL_INITIAL (fndecl));
1902
1903 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1904 if (DECL_RTL_SET_P (decl))
1905 instantiate_decl_rtl (DECL_RTL (decl));
1906 vec_free (cfun->local_decls);
1907 }
1908
1909 /* Pass through the INSNS of function FNDECL and convert virtual register
1910 references to hard register references. */
1911
1912 static unsigned int
1913 instantiate_virtual_regs (void)
1914 {
1915 rtx_insn *insn;
1916
1917 /* Compute the offsets to use for this function. */
1918 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1919 var_offset = STARTING_FRAME_OFFSET;
1920 dynamic_offset = STACK_DYNAMIC_OFFSET (current_function_decl);
1921 out_arg_offset = STACK_POINTER_OFFSET;
1922 #ifdef FRAME_POINTER_CFA_OFFSET
1923 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1924 #else
1925 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1926 #endif
1927
1928 /* Initialize recognition, indicating that volatile is OK. */
1929 init_recog ();
1930
1931 /* Scan through all the insns, instantiating every virtual register still
1932 present. */
1933 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1934 if (INSN_P (insn))
1935 {
1936 /* These patterns in the instruction stream can never be recognized.
1937 Fortunately, they shouldn't contain virtual registers either. */
1938 if (GET_CODE (PATTERN (insn)) == USE
1939 || GET_CODE (PATTERN (insn)) == CLOBBER
1940 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
1941 continue;
1942 else if (DEBUG_INSN_P (insn))
1943 instantiate_virtual_regs_in_rtx (&INSN_VAR_LOCATION (insn));
1944 else
1945 instantiate_virtual_regs_in_insn (insn);
1946
1947 if (insn->deleted ())
1948 continue;
1949
1950 instantiate_virtual_regs_in_rtx (&REG_NOTES (insn));
1951
1952 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
1953 if (CALL_P (insn))
1954 instantiate_virtual_regs_in_rtx (&CALL_INSN_FUNCTION_USAGE (insn));
1955 }
1956
1957 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
1958 instantiate_decls (current_function_decl);
1959
1960 targetm.instantiate_decls ();
1961
1962 /* Indicate that, from now on, assign_stack_local should use
1963 frame_pointer_rtx. */
1964 virtuals_instantiated = 1;
1965
1966 return 0;
1967 }
1968
1969 namespace {
1970
1971 const pass_data pass_data_instantiate_virtual_regs =
1972 {
1973 RTL_PASS, /* type */
1974 "vregs", /* name */
1975 OPTGROUP_NONE, /* optinfo_flags */
1976 TV_NONE, /* tv_id */
1977 0, /* properties_required */
1978 0, /* properties_provided */
1979 0, /* properties_destroyed */
1980 0, /* todo_flags_start */
1981 0, /* todo_flags_finish */
1982 };
1983
1984 class pass_instantiate_virtual_regs : public rtl_opt_pass
1985 {
1986 public:
1987 pass_instantiate_virtual_regs (gcc::context *ctxt)
1988 : rtl_opt_pass (pass_data_instantiate_virtual_regs, ctxt)
1989 {}
1990
1991 /* opt_pass methods: */
1992 virtual unsigned int execute (function *)
1993 {
1994 return instantiate_virtual_regs ();
1995 }
1996
1997 }; // class pass_instantiate_virtual_regs
1998
1999 } // anon namespace
2000
2001 rtl_opt_pass *
2002 make_pass_instantiate_virtual_regs (gcc::context *ctxt)
2003 {
2004 return new pass_instantiate_virtual_regs (ctxt);
2005 }
2006
2007 \f
2008 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
2009 This means a type for which function calls must pass an address to the
2010 function or get an address back from the function.
2011 EXP may be a type node or an expression (whose type is tested). */
2012
2013 int
2014 aggregate_value_p (const_tree exp, const_tree fntype)
2015 {
2016 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
2017 int i, regno, nregs;
2018 rtx reg;
2019
2020 if (fntype)
2021 switch (TREE_CODE (fntype))
2022 {
2023 case CALL_EXPR:
2024 {
2025 tree fndecl = get_callee_fndecl (fntype);
2026 if (fndecl)
2027 fntype = TREE_TYPE (fndecl);
2028 else if (CALL_EXPR_FN (fntype))
2029 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype)));
2030 else
2031 /* For internal functions, assume nothing needs to be
2032 returned in memory. */
2033 return 0;
2034 }
2035 break;
2036 case FUNCTION_DECL:
2037 fntype = TREE_TYPE (fntype);
2038 break;
2039 case FUNCTION_TYPE:
2040 case METHOD_TYPE:
2041 break;
2042 case IDENTIFIER_NODE:
2043 fntype = NULL_TREE;
2044 break;
2045 default:
2046 /* We don't expect other tree types here. */
2047 gcc_unreachable ();
2048 }
2049
2050 if (VOID_TYPE_P (type))
2051 return 0;
2052
2053 /* If a record should be passed the same as its first (and only) member
2054 don't pass it as an aggregate. */
2055 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2056 return aggregate_value_p (first_field (type), fntype);
2057
2058 /* If the front end has decided that this needs to be passed by
2059 reference, do so. */
2060 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2061 && DECL_BY_REFERENCE (exp))
2062 return 1;
2063
2064 /* Function types that are TREE_ADDRESSABLE force return in memory. */
2065 if (fntype && TREE_ADDRESSABLE (fntype))
2066 return 1;
2067
2068 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2069 and thus can't be returned in registers. */
2070 if (TREE_ADDRESSABLE (type))
2071 return 1;
2072
2073 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2074 return 1;
2075
2076 if (targetm.calls.return_in_memory (type, fntype))
2077 return 1;
2078
2079 /* Make sure we have suitable call-clobbered regs to return
2080 the value in; if not, we must return it in memory. */
2081 reg = hard_function_value (type, 0, fntype, 0);
2082
2083 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2084 it is OK. */
2085 if (!REG_P (reg))
2086 return 0;
2087
2088 regno = REGNO (reg);
2089 nregs = hard_regno_nregs[regno][TYPE_MODE (type)];
2090 for (i = 0; i < nregs; i++)
2091 if (! call_used_regs[regno + i])
2092 return 1;
2093
2094 return 0;
2095 }
2096 \f
2097 /* Return true if we should assign DECL a pseudo register; false if it
2098 should live on the local stack. */
2099
2100 bool
2101 use_register_for_decl (const_tree decl)
2102 {
2103 if (TREE_CODE (decl) == SSA_NAME)
2104 {
2105 /* We often try to use the SSA_NAME, instead of its underlying
2106 decl, to get type information and guide decisions, to avoid
2107 differences of behavior between anonymous and named
2108 variables, but in this one case we have to go for the actual
2109 variable if there is one. The main reason is that, at least
2110 at -O0, we want to place user variables on the stack, but we
2111 don't mind using pseudos for anonymous or ignored temps.
2112 Should we take the SSA_NAME, we'd conclude all SSA_NAMEs
2113 should go in pseudos, whereas their corresponding variables
2114 might have to go on the stack. So, disregarding the decl
2115 here would negatively impact debug info at -O0, enable
2116 coalescing between SSA_NAMEs that ought to get different
2117 stack/pseudo assignments, and get the incoming argument
2118 processing thoroughly confused by PARM_DECLs expected to live
2119 in stack slots but assigned to pseudos. */
2120 if (!SSA_NAME_VAR (decl))
2121 return TYPE_MODE (TREE_TYPE (decl)) != BLKmode
2122 && !(flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)));
2123
2124 decl = SSA_NAME_VAR (decl);
2125 }
2126
2127 /* Honor volatile. */
2128 if (TREE_SIDE_EFFECTS (decl))
2129 return false;
2130
2131 /* Honor addressability. */
2132 if (TREE_ADDRESSABLE (decl))
2133 return false;
2134
2135 /* RESULT_DECLs are a bit special in that they're assigned without
2136 regard to use_register_for_decl, but we generally only store in
2137 them. If we coalesce their SSA NAMEs, we'd better return a
2138 result that matches the assignment in expand_function_start. */
2139 if (TREE_CODE (decl) == RESULT_DECL)
2140 {
2141 /* If it's not an aggregate, we're going to use a REG or a
2142 PARALLEL containing a REG. */
2143 if (!aggregate_value_p (decl, current_function_decl))
2144 return true;
2145
2146 /* If expand_function_start determines the return value, we'll
2147 use MEM if it's not by reference. */
2148 if (cfun->returns_pcc_struct
2149 || (targetm.calls.struct_value_rtx
2150 (TREE_TYPE (current_function_decl), 1)))
2151 return DECL_BY_REFERENCE (decl);
2152
2153 /* Otherwise, we're taking an extra all.function_result_decl
2154 argument. It's set up in assign_parms_augmented_arg_list,
2155 under the (negated) conditions above, and then it's used to
2156 set up the RESULT_DECL rtl in assign_params, after looping
2157 over all parameters. Now, if the RESULT_DECL is not by
2158 reference, we'll use a MEM either way. */
2159 if (!DECL_BY_REFERENCE (decl))
2160 return false;
2161
2162 /* Otherwise, if RESULT_DECL is DECL_BY_REFERENCE, it will take
2163 the function_result_decl's assignment. Since it's a pointer,
2164 we can short-circuit a number of the tests below, and we must
2165 duplicat e them because we don't have the
2166 function_result_decl to test. */
2167 if (!targetm.calls.allocate_stack_slots_for_args ())
2168 return true;
2169 /* We don't set DECL_IGNORED_P for the function_result_decl. */
2170 if (optimize)
2171 return true;
2172 /* We don't set DECL_REGISTER for the function_result_decl. */
2173 return false;
2174 }
2175
2176 /* Decl is implicitly addressible by bound stores and loads
2177 if it is an aggregate holding bounds. */
2178 if (chkp_function_instrumented_p (current_function_decl)
2179 && TREE_TYPE (decl)
2180 && !BOUNDED_P (decl)
2181 && chkp_type_has_pointer (TREE_TYPE (decl)))
2182 return false;
2183
2184 /* Only register-like things go in registers. */
2185 if (DECL_MODE (decl) == BLKmode)
2186 return false;
2187
2188 /* If -ffloat-store specified, don't put explicit float variables
2189 into registers. */
2190 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2191 propagates values across these stores, and it probably shouldn't. */
2192 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2193 return false;
2194
2195 if (!targetm.calls.allocate_stack_slots_for_args ())
2196 return true;
2197
2198 /* If we're not interested in tracking debugging information for
2199 this decl, then we can certainly put it in a register. */
2200 if (DECL_IGNORED_P (decl))
2201 return true;
2202
2203 if (optimize)
2204 return true;
2205
2206 if (!DECL_REGISTER (decl))
2207 return false;
2208
2209 switch (TREE_CODE (TREE_TYPE (decl)))
2210 {
2211 case RECORD_TYPE:
2212 case UNION_TYPE:
2213 case QUAL_UNION_TYPE:
2214 /* When not optimizing, disregard register keyword for variables with
2215 types containing methods, otherwise the methods won't be callable
2216 from the debugger. */
2217 if (TYPE_METHODS (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
2218 return false;
2219 break;
2220 default:
2221 break;
2222 }
2223
2224 return true;
2225 }
2226
2227 /* Structures to communicate between the subroutines of assign_parms.
2228 The first holds data persistent across all parameters, the second
2229 is cleared out for each parameter. */
2230
2231 struct assign_parm_data_all
2232 {
2233 /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2234 should become a job of the target or otherwise encapsulated. */
2235 CUMULATIVE_ARGS args_so_far_v;
2236 cumulative_args_t args_so_far;
2237 struct args_size stack_args_size;
2238 tree function_result_decl;
2239 tree orig_fnargs;
2240 rtx_insn *first_conversion_insn;
2241 rtx_insn *last_conversion_insn;
2242 HOST_WIDE_INT pretend_args_size;
2243 HOST_WIDE_INT extra_pretend_bytes;
2244 int reg_parm_stack_space;
2245 };
2246
2247 struct assign_parm_data_one
2248 {
2249 tree nominal_type;
2250 tree passed_type;
2251 rtx entry_parm;
2252 rtx stack_parm;
2253 machine_mode nominal_mode;
2254 machine_mode passed_mode;
2255 machine_mode promoted_mode;
2256 struct locate_and_pad_arg_data locate;
2257 int partial;
2258 BOOL_BITFIELD named_arg : 1;
2259 BOOL_BITFIELD passed_pointer : 1;
2260 BOOL_BITFIELD on_stack : 1;
2261 BOOL_BITFIELD loaded_in_reg : 1;
2262 };
2263
2264 struct bounds_parm_data
2265 {
2266 assign_parm_data_one parm_data;
2267 tree bounds_parm;
2268 tree ptr_parm;
2269 rtx ptr_entry;
2270 int bound_no;
2271 };
2272
2273 /* A subroutine of assign_parms. Initialize ALL. */
2274
2275 static void
2276 assign_parms_initialize_all (struct assign_parm_data_all *all)
2277 {
2278 tree fntype ATTRIBUTE_UNUSED;
2279
2280 memset (all, 0, sizeof (*all));
2281
2282 fntype = TREE_TYPE (current_function_decl);
2283
2284 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
2285 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2286 #else
2287 INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2288 current_function_decl, -1);
2289 #endif
2290 all->args_so_far = pack_cumulative_args (&all->args_so_far_v);
2291
2292 #ifdef INCOMING_REG_PARM_STACK_SPACE
2293 all->reg_parm_stack_space
2294 = INCOMING_REG_PARM_STACK_SPACE (current_function_decl);
2295 #endif
2296 }
2297
2298 /* If ARGS contains entries with complex types, split the entry into two
2299 entries of the component type. Return a new list of substitutions are
2300 needed, else the old list. */
2301
2302 static void
2303 split_complex_args (vec<tree> *args)
2304 {
2305 unsigned i;
2306 tree p;
2307
2308 FOR_EACH_VEC_ELT (*args, i, p)
2309 {
2310 tree type = TREE_TYPE (p);
2311 if (TREE_CODE (type) == COMPLEX_TYPE
2312 && targetm.calls.split_complex_arg (type))
2313 {
2314 tree decl;
2315 tree subtype = TREE_TYPE (type);
2316 bool addressable = TREE_ADDRESSABLE (p);
2317
2318 /* Rewrite the PARM_DECL's type with its component. */
2319 p = copy_node (p);
2320 TREE_TYPE (p) = subtype;
2321 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2322 DECL_MODE (p) = VOIDmode;
2323 DECL_SIZE (p) = NULL;
2324 DECL_SIZE_UNIT (p) = NULL;
2325 /* If this arg must go in memory, put it in a pseudo here.
2326 We can't allow it to go in memory as per normal parms,
2327 because the usual place might not have the imag part
2328 adjacent to the real part. */
2329 DECL_ARTIFICIAL (p) = addressable;
2330 DECL_IGNORED_P (p) = addressable;
2331 TREE_ADDRESSABLE (p) = 0;
2332 layout_decl (p, 0);
2333 (*args)[i] = p;
2334
2335 /* Build a second synthetic decl. */
2336 decl = build_decl (EXPR_LOCATION (p),
2337 PARM_DECL, NULL_TREE, subtype);
2338 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2339 DECL_ARTIFICIAL (decl) = addressable;
2340 DECL_IGNORED_P (decl) = addressable;
2341 layout_decl (decl, 0);
2342 args->safe_insert (++i, decl);
2343 }
2344 }
2345 }
2346
2347 /* A subroutine of assign_parms. Adjust the parameter list to incorporate
2348 the hidden struct return argument, and (abi willing) complex args.
2349 Return the new parameter list. */
2350
2351 static vec<tree>
2352 assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2353 {
2354 tree fndecl = current_function_decl;
2355 tree fntype = TREE_TYPE (fndecl);
2356 vec<tree> fnargs = vNULL;
2357 tree arg;
2358
2359 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2360 fnargs.safe_push (arg);
2361
2362 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2363
2364 /* If struct value address is treated as the first argument, make it so. */
2365 if (aggregate_value_p (DECL_RESULT (fndecl), fndecl)
2366 && ! cfun->returns_pcc_struct
2367 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2368 {
2369 tree type = build_pointer_type (TREE_TYPE (fntype));
2370 tree decl;
2371
2372 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2373 PARM_DECL, get_identifier (".result_ptr"), type);
2374 DECL_ARG_TYPE (decl) = type;
2375 DECL_ARTIFICIAL (decl) = 1;
2376 DECL_NAMELESS (decl) = 1;
2377 TREE_CONSTANT (decl) = 1;
2378 /* We don't set DECL_IGNORED_P or DECL_REGISTER here. If this
2379 changes, the end of the RESULT_DECL handling block in
2380 use_register_for_decl must be adjusted to match. */
2381
2382 DECL_CHAIN (decl) = all->orig_fnargs;
2383 all->orig_fnargs = decl;
2384 fnargs.safe_insert (0, decl);
2385
2386 all->function_result_decl = decl;
2387
2388 /* If function is instrumented then bounds of the
2389 passed structure address is the second argument. */
2390 if (chkp_function_instrumented_p (fndecl))
2391 {
2392 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2393 PARM_DECL, get_identifier (".result_bnd"),
2394 pointer_bounds_type_node);
2395 DECL_ARG_TYPE (decl) = pointer_bounds_type_node;
2396 DECL_ARTIFICIAL (decl) = 1;
2397 DECL_NAMELESS (decl) = 1;
2398 TREE_CONSTANT (decl) = 1;
2399
2400 DECL_CHAIN (decl) = DECL_CHAIN (all->orig_fnargs);
2401 DECL_CHAIN (all->orig_fnargs) = decl;
2402 fnargs.safe_insert (1, decl);
2403 }
2404 }
2405
2406 /* If the target wants to split complex arguments into scalars, do so. */
2407 if (targetm.calls.split_complex_arg)
2408 split_complex_args (&fnargs);
2409
2410 return fnargs;
2411 }
2412
2413 /* A subroutine of assign_parms. Examine PARM and pull out type and mode
2414 data for the parameter. Incorporate ABI specifics such as pass-by-
2415 reference and type promotion. */
2416
2417 static void
2418 assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2419 struct assign_parm_data_one *data)
2420 {
2421 tree nominal_type, passed_type;
2422 machine_mode nominal_mode, passed_mode, promoted_mode;
2423 int unsignedp;
2424
2425 memset (data, 0, sizeof (*data));
2426
2427 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
2428 if (!cfun->stdarg)
2429 data->named_arg = 1; /* No variadic parms. */
2430 else if (DECL_CHAIN (parm))
2431 data->named_arg = 1; /* Not the last non-variadic parm. */
2432 else if (targetm.calls.strict_argument_naming (all->args_so_far))
2433 data->named_arg = 1; /* Only variadic ones are unnamed. */
2434 else
2435 data->named_arg = 0; /* Treat as variadic. */
2436
2437 nominal_type = TREE_TYPE (parm);
2438 passed_type = DECL_ARG_TYPE (parm);
2439
2440 /* Look out for errors propagating this far. Also, if the parameter's
2441 type is void then its value doesn't matter. */
2442 if (TREE_TYPE (parm) == error_mark_node
2443 /* This can happen after weird syntax errors
2444 or if an enum type is defined among the parms. */
2445 || TREE_CODE (parm) != PARM_DECL
2446 || passed_type == NULL
2447 || VOID_TYPE_P (nominal_type))
2448 {
2449 nominal_type = passed_type = void_type_node;
2450 nominal_mode = passed_mode = promoted_mode = VOIDmode;
2451 goto egress;
2452 }
2453
2454 /* Find mode of arg as it is passed, and mode of arg as it should be
2455 during execution of this function. */
2456 passed_mode = TYPE_MODE (passed_type);
2457 nominal_mode = TYPE_MODE (nominal_type);
2458
2459 /* If the parm is to be passed as a transparent union or record, use the
2460 type of the first field for the tests below. We have already verified
2461 that the modes are the same. */
2462 if ((TREE_CODE (passed_type) == UNION_TYPE
2463 || TREE_CODE (passed_type) == RECORD_TYPE)
2464 && TYPE_TRANSPARENT_AGGR (passed_type))
2465 passed_type = TREE_TYPE (first_field (passed_type));
2466
2467 /* See if this arg was passed by invisible reference. */
2468 if (pass_by_reference (&all->args_so_far_v, passed_mode,
2469 passed_type, data->named_arg))
2470 {
2471 passed_type = nominal_type = build_pointer_type (passed_type);
2472 data->passed_pointer = true;
2473 passed_mode = nominal_mode = TYPE_MODE (nominal_type);
2474 }
2475
2476 /* Find mode as it is passed by the ABI. */
2477 unsignedp = TYPE_UNSIGNED (passed_type);
2478 promoted_mode = promote_function_mode (passed_type, passed_mode, &unsignedp,
2479 TREE_TYPE (current_function_decl), 0);
2480
2481 egress:
2482 data->nominal_type = nominal_type;
2483 data->passed_type = passed_type;
2484 data->nominal_mode = nominal_mode;
2485 data->passed_mode = passed_mode;
2486 data->promoted_mode = promoted_mode;
2487 }
2488
2489 /* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2490
2491 static void
2492 assign_parms_setup_varargs (struct assign_parm_data_all *all,
2493 struct assign_parm_data_one *data, bool no_rtl)
2494 {
2495 int varargs_pretend_bytes = 0;
2496
2497 targetm.calls.setup_incoming_varargs (all->args_so_far,
2498 data->promoted_mode,
2499 data->passed_type,
2500 &varargs_pretend_bytes, no_rtl);
2501
2502 /* If the back-end has requested extra stack space, record how much is
2503 needed. Do not change pretend_args_size otherwise since it may be
2504 nonzero from an earlier partial argument. */
2505 if (varargs_pretend_bytes > 0)
2506 all->pretend_args_size = varargs_pretend_bytes;
2507 }
2508
2509 /* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2510 the incoming location of the current parameter. */
2511
2512 static void
2513 assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2514 struct assign_parm_data_one *data)
2515 {
2516 HOST_WIDE_INT pretend_bytes = 0;
2517 rtx entry_parm;
2518 bool in_regs;
2519
2520 if (data->promoted_mode == VOIDmode)
2521 {
2522 data->entry_parm = data->stack_parm = const0_rtx;
2523 return;
2524 }
2525
2526 entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2527 data->promoted_mode,
2528 data->passed_type,
2529 data->named_arg);
2530
2531 if (entry_parm == 0)
2532 data->promoted_mode = data->passed_mode;
2533
2534 /* Determine parm's home in the stack, in case it arrives in the stack
2535 or we should pretend it did. Compute the stack position and rtx where
2536 the argument arrives and its size.
2537
2538 There is one complexity here: If this was a parameter that would
2539 have been passed in registers, but wasn't only because it is
2540 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2541 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2542 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2543 as it was the previous time. */
2544 in_regs = (entry_parm != 0) || POINTER_BOUNDS_TYPE_P (data->passed_type);
2545 #ifdef STACK_PARMS_IN_REG_PARM_AREA
2546 in_regs = true;
2547 #endif
2548 if (!in_regs && !data->named_arg)
2549 {
2550 if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2551 {
2552 rtx tem;
2553 tem = targetm.calls.function_incoming_arg (all->args_so_far,
2554 data->promoted_mode,
2555 data->passed_type, true);
2556 in_regs = tem != NULL;
2557 }
2558 }
2559
2560 /* If this parameter was passed both in registers and in the stack, use
2561 the copy on the stack. */
2562 if (targetm.calls.must_pass_in_stack (data->promoted_mode,
2563 data->passed_type))
2564 entry_parm = 0;
2565
2566 if (entry_parm)
2567 {
2568 int partial;
2569
2570 partial = targetm.calls.arg_partial_bytes (all->args_so_far,
2571 data->promoted_mode,
2572 data->passed_type,
2573 data->named_arg);
2574 data->partial = partial;
2575
2576 /* The caller might already have allocated stack space for the
2577 register parameters. */
2578 if (partial != 0 && all->reg_parm_stack_space == 0)
2579 {
2580 /* Part of this argument is passed in registers and part
2581 is passed on the stack. Ask the prologue code to extend
2582 the stack part so that we can recreate the full value.
2583
2584 PRETEND_BYTES is the size of the registers we need to store.
2585 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2586 stack space that the prologue should allocate.
2587
2588 Internally, gcc assumes that the argument pointer is aligned
2589 to STACK_BOUNDARY bits. This is used both for alignment
2590 optimizations (see init_emit) and to locate arguments that are
2591 aligned to more than PARM_BOUNDARY bits. We must preserve this
2592 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2593 a stack boundary. */
2594
2595 /* We assume at most one partial arg, and it must be the first
2596 argument on the stack. */
2597 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2598
2599 pretend_bytes = partial;
2600 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2601
2602 /* We want to align relative to the actual stack pointer, so
2603 don't include this in the stack size until later. */
2604 all->extra_pretend_bytes = all->pretend_args_size;
2605 }
2606 }
2607
2608 locate_and_pad_parm (data->promoted_mode, data->passed_type, in_regs,
2609 all->reg_parm_stack_space,
2610 entry_parm ? data->partial : 0, current_function_decl,
2611 &all->stack_args_size, &data->locate);
2612
2613 /* Update parm_stack_boundary if this parameter is passed in the
2614 stack. */
2615 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2616 crtl->parm_stack_boundary = data->locate.boundary;
2617
2618 /* Adjust offsets to include the pretend args. */
2619 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2620 data->locate.slot_offset.constant += pretend_bytes;
2621 data->locate.offset.constant += pretend_bytes;
2622
2623 data->entry_parm = entry_parm;
2624 }
2625
2626 /* A subroutine of assign_parms. If there is actually space on the stack
2627 for this parm, count it in stack_args_size and return true. */
2628
2629 static bool
2630 assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2631 struct assign_parm_data_one *data)
2632 {
2633 /* Bounds are never passed on the stack to keep compatibility
2634 with not instrumented code. */
2635 if (POINTER_BOUNDS_TYPE_P (data->passed_type))
2636 return false;
2637 /* Trivially true if we've no incoming register. */
2638 else if (data->entry_parm == NULL)
2639 ;
2640 /* Also true if we're partially in registers and partially not,
2641 since we've arranged to drop the entire argument on the stack. */
2642 else if (data->partial != 0)
2643 ;
2644 /* Also true if the target says that it's passed in both registers
2645 and on the stack. */
2646 else if (GET_CODE (data->entry_parm) == PARALLEL
2647 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2648 ;
2649 /* Also true if the target says that there's stack allocated for
2650 all register parameters. */
2651 else if (all->reg_parm_stack_space > 0)
2652 ;
2653 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2654 else
2655 return false;
2656
2657 all->stack_args_size.constant += data->locate.size.constant;
2658 if (data->locate.size.var)
2659 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2660
2661 return true;
2662 }
2663
2664 /* A subroutine of assign_parms. Given that this parameter is allocated
2665 stack space by the ABI, find it. */
2666
2667 static void
2668 assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2669 {
2670 rtx offset_rtx, stack_parm;
2671 unsigned int align, boundary;
2672
2673 /* If we're passing this arg using a reg, make its stack home the
2674 aligned stack slot. */
2675 if (data->entry_parm)
2676 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2677 else
2678 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2679
2680 stack_parm = crtl->args.internal_arg_pointer;
2681 if (offset_rtx != const0_rtx)
2682 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2683 stack_parm = gen_rtx_MEM (data->promoted_mode, stack_parm);
2684
2685 if (!data->passed_pointer)
2686 {
2687 set_mem_attributes (stack_parm, parm, 1);
2688 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2689 while promoted mode's size is needed. */
2690 if (data->promoted_mode != BLKmode
2691 && data->promoted_mode != DECL_MODE (parm))
2692 {
2693 set_mem_size (stack_parm, GET_MODE_SIZE (data->promoted_mode));
2694 if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2695 {
2696 int offset = subreg_lowpart_offset (DECL_MODE (parm),
2697 data->promoted_mode);
2698 if (offset)
2699 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2700 }
2701 }
2702 }
2703
2704 boundary = data->locate.boundary;
2705 align = BITS_PER_UNIT;
2706
2707 /* If we're padding upward, we know that the alignment of the slot
2708 is TARGET_FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2709 intentionally forcing upward padding. Otherwise we have to come
2710 up with a guess at the alignment based on OFFSET_RTX. */
2711 if (data->locate.where_pad != downward || data->entry_parm)
2712 align = boundary;
2713 else if (CONST_INT_P (offset_rtx))
2714 {
2715 align = INTVAL (offset_rtx) * BITS_PER_UNIT | boundary;
2716 align = align & -align;
2717 }
2718 set_mem_align (stack_parm, align);
2719
2720 if (data->entry_parm)
2721 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2722
2723 data->stack_parm = stack_parm;
2724 }
2725
2726 /* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2727 always valid and contiguous. */
2728
2729 static void
2730 assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2731 {
2732 rtx entry_parm = data->entry_parm;
2733 rtx stack_parm = data->stack_parm;
2734
2735 /* If this parm was passed part in regs and part in memory, pretend it
2736 arrived entirely in memory by pushing the register-part onto the stack.
2737 In the special case of a DImode or DFmode that is split, we could put
2738 it together in a pseudoreg directly, but for now that's not worth
2739 bothering with. */
2740 if (data->partial != 0)
2741 {
2742 /* Handle calls that pass values in multiple non-contiguous
2743 locations. The Irix 6 ABI has examples of this. */
2744 if (GET_CODE (entry_parm) == PARALLEL)
2745 emit_group_store (validize_mem (copy_rtx (stack_parm)), entry_parm,
2746 data->passed_type,
2747 int_size_in_bytes (data->passed_type));
2748 else
2749 {
2750 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2751 move_block_from_reg (REGNO (entry_parm),
2752 validize_mem (copy_rtx (stack_parm)),
2753 data->partial / UNITS_PER_WORD);
2754 }
2755
2756 entry_parm = stack_parm;
2757 }
2758
2759 /* If we didn't decide this parm came in a register, by default it came
2760 on the stack. */
2761 else if (entry_parm == NULL)
2762 entry_parm = stack_parm;
2763
2764 /* When an argument is passed in multiple locations, we can't make use
2765 of this information, but we can save some copying if the whole argument
2766 is passed in a single register. */
2767 else if (GET_CODE (entry_parm) == PARALLEL
2768 && data->nominal_mode != BLKmode
2769 && data->passed_mode != BLKmode)
2770 {
2771 size_t i, len = XVECLEN (entry_parm, 0);
2772
2773 for (i = 0; i < len; i++)
2774 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2775 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2776 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2777 == data->passed_mode)
2778 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2779 {
2780 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2781 break;
2782 }
2783 }
2784
2785 data->entry_parm = entry_parm;
2786 }
2787
2788 /* A subroutine of assign_parms. Reconstitute any values which were
2789 passed in multiple registers and would fit in a single register. */
2790
2791 static void
2792 assign_parm_remove_parallels (struct assign_parm_data_one *data)
2793 {
2794 rtx entry_parm = data->entry_parm;
2795
2796 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2797 This can be done with register operations rather than on the
2798 stack, even if we will store the reconstituted parameter on the
2799 stack later. */
2800 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2801 {
2802 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2803 emit_group_store (parmreg, entry_parm, data->passed_type,
2804 GET_MODE_SIZE (GET_MODE (entry_parm)));
2805 entry_parm = parmreg;
2806 }
2807
2808 data->entry_parm = entry_parm;
2809 }
2810
2811 /* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2812 always valid and properly aligned. */
2813
2814 static void
2815 assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2816 {
2817 rtx stack_parm = data->stack_parm;
2818
2819 /* If we can't trust the parm stack slot to be aligned enough for its
2820 ultimate type, don't use that slot after entry. We'll make another
2821 stack slot, if we need one. */
2822 if (stack_parm
2823 && ((STRICT_ALIGNMENT
2824 && GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm))
2825 || (data->nominal_type
2826 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2827 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2828 stack_parm = NULL;
2829
2830 /* If parm was passed in memory, and we need to convert it on entry,
2831 don't store it back in that same slot. */
2832 else if (data->entry_parm == stack_parm
2833 && data->nominal_mode != BLKmode
2834 && data->nominal_mode != data->passed_mode)
2835 stack_parm = NULL;
2836
2837 /* If stack protection is in effect for this function, don't leave any
2838 pointers in their passed stack slots. */
2839 else if (crtl->stack_protect_guard
2840 && (flag_stack_protect == 2
2841 || data->passed_pointer
2842 || POINTER_TYPE_P (data->nominal_type)))
2843 stack_parm = NULL;
2844
2845 data->stack_parm = stack_parm;
2846 }
2847
2848 /* A subroutine of assign_parms. Return true if the current parameter
2849 should be stored as a BLKmode in the current frame. */
2850
2851 static bool
2852 assign_parm_setup_block_p (struct assign_parm_data_one *data)
2853 {
2854 if (data->nominal_mode == BLKmode)
2855 return true;
2856 if (GET_MODE (data->entry_parm) == BLKmode)
2857 return true;
2858
2859 #ifdef BLOCK_REG_PADDING
2860 /* Only assign_parm_setup_block knows how to deal with register arguments
2861 that are padded at the least significant end. */
2862 if (REG_P (data->entry_parm)
2863 && GET_MODE_SIZE (data->promoted_mode) < UNITS_PER_WORD
2864 && (BLOCK_REG_PADDING (data->passed_mode, data->passed_type, 1)
2865 == (BYTES_BIG_ENDIAN ? upward : downward)))
2866 return true;
2867 #endif
2868
2869 return false;
2870 }
2871
2872 /* A subroutine of assign_parms. Arrange for the parameter to be
2873 present and valid in DATA->STACK_RTL. */
2874
2875 static void
2876 assign_parm_setup_block (struct assign_parm_data_all *all,
2877 tree parm, struct assign_parm_data_one *data)
2878 {
2879 rtx entry_parm = data->entry_parm;
2880 rtx stack_parm = data->stack_parm;
2881 rtx target_reg = NULL_RTX;
2882 bool in_conversion_seq = false;
2883 HOST_WIDE_INT size;
2884 HOST_WIDE_INT size_stored;
2885
2886 if (GET_CODE (entry_parm) == PARALLEL)
2887 entry_parm = emit_group_move_into_temps (entry_parm);
2888
2889 /* If we want the parameter in a pseudo, don't use a stack slot. */
2890 if (is_gimple_reg (parm) && use_register_for_decl (parm))
2891 {
2892 tree def = ssa_default_def (cfun, parm);
2893 gcc_assert (def);
2894 machine_mode mode = promote_ssa_mode (def, NULL);
2895 rtx reg = gen_reg_rtx (mode);
2896 if (GET_CODE (reg) != CONCAT)
2897 stack_parm = reg;
2898 else
2899 {
2900 target_reg = reg;
2901 /* Avoid allocating a stack slot, if there isn't one
2902 preallocated by the ABI. It might seem like we should
2903 always prefer a pseudo, but converting between
2904 floating-point and integer modes goes through the stack
2905 on various machines, so it's better to use the reserved
2906 stack slot than to risk wasting it and allocating more
2907 for the conversion. */
2908 if (stack_parm == NULL_RTX)
2909 {
2910 int save = generating_concat_p;
2911 generating_concat_p = 0;
2912 stack_parm = gen_reg_rtx (mode);
2913 generating_concat_p = save;
2914 }
2915 }
2916 data->stack_parm = NULL;
2917 }
2918
2919 size = int_size_in_bytes (data->passed_type);
2920 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2921 if (stack_parm == 0)
2922 {
2923 DECL_ALIGN (parm) = MAX (DECL_ALIGN (parm), BITS_PER_WORD);
2924 stack_parm = assign_stack_local (BLKmode, size_stored,
2925 DECL_ALIGN (parm));
2926 if (GET_MODE_SIZE (GET_MODE (entry_parm)) == size)
2927 PUT_MODE (stack_parm, GET_MODE (entry_parm));
2928 set_mem_attributes (stack_parm, parm, 1);
2929 }
2930
2931 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2932 calls that pass values in multiple non-contiguous locations. */
2933 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2934 {
2935 rtx mem;
2936
2937 /* Note that we will be storing an integral number of words.
2938 So we have to be careful to ensure that we allocate an
2939 integral number of words. We do this above when we call
2940 assign_stack_local if space was not allocated in the argument
2941 list. If it was, this will not work if PARM_BOUNDARY is not
2942 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2943 if it becomes a problem. Exception is when BLKmode arrives
2944 with arguments not conforming to word_mode. */
2945
2946 if (data->stack_parm == 0)
2947 ;
2948 else if (GET_CODE (entry_parm) == PARALLEL)
2949 ;
2950 else
2951 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2952
2953 mem = validize_mem (copy_rtx (stack_parm));
2954
2955 /* Handle values in multiple non-contiguous locations. */
2956 if (GET_CODE (entry_parm) == PARALLEL && !MEM_P (mem))
2957 emit_group_store (mem, entry_parm, data->passed_type, size);
2958 else if (GET_CODE (entry_parm) == PARALLEL)
2959 {
2960 push_to_sequence2 (all->first_conversion_insn,
2961 all->last_conversion_insn);
2962 emit_group_store (mem, entry_parm, data->passed_type, size);
2963 all->first_conversion_insn = get_insns ();
2964 all->last_conversion_insn = get_last_insn ();
2965 end_sequence ();
2966 in_conversion_seq = true;
2967 }
2968
2969 else if (size == 0)
2970 ;
2971
2972 /* If SIZE is that of a mode no bigger than a word, just use
2973 that mode's store operation. */
2974 else if (size <= UNITS_PER_WORD)
2975 {
2976 machine_mode mode
2977 = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2978
2979 if (mode != BLKmode
2980 #ifdef BLOCK_REG_PADDING
2981 && (size == UNITS_PER_WORD
2982 || (BLOCK_REG_PADDING (mode, data->passed_type, 1)
2983 != (BYTES_BIG_ENDIAN ? upward : downward)))
2984 #endif
2985 )
2986 {
2987 rtx reg;
2988
2989 /* We are really truncating a word_mode value containing
2990 SIZE bytes into a value of mode MODE. If such an
2991 operation requires no actual instructions, we can refer
2992 to the value directly in mode MODE, otherwise we must
2993 start with the register in word_mode and explicitly
2994 convert it. */
2995 if (TRULY_NOOP_TRUNCATION (size * BITS_PER_UNIT, BITS_PER_WORD))
2996 reg = gen_rtx_REG (mode, REGNO (entry_parm));
2997 else
2998 {
2999 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
3000 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
3001 }
3002 emit_move_insn (change_address (mem, mode, 0), reg);
3003 }
3004
3005 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
3006 machine must be aligned to the left before storing
3007 to memory. Note that the previous test doesn't
3008 handle all cases (e.g. SIZE == 3). */
3009 else if (size != UNITS_PER_WORD
3010 #ifdef BLOCK_REG_PADDING
3011 && (BLOCK_REG_PADDING (mode, data->passed_type, 1)
3012 == downward)
3013 #else
3014 && BYTES_BIG_ENDIAN
3015 #endif
3016 )
3017 {
3018 rtx tem, x;
3019 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
3020 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
3021
3022 x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
3023 tem = change_address (mem, word_mode, 0);
3024 emit_move_insn (tem, x);
3025 }
3026 else if (!MEM_P (mem))
3027 emit_move_insn (mem, entry_parm);
3028 else
3029 move_block_from_reg (REGNO (entry_parm), mem,
3030 size_stored / UNITS_PER_WORD);
3031 }
3032 else if (!MEM_P (mem))
3033 emit_move_insn (mem, entry_parm);
3034 else
3035 move_block_from_reg (REGNO (entry_parm), mem,
3036 size_stored / UNITS_PER_WORD);
3037 }
3038 else if (data->stack_parm == 0)
3039 {
3040 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3041 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
3042 BLOCK_OP_NORMAL);
3043 all->first_conversion_insn = get_insns ();
3044 all->last_conversion_insn = get_last_insn ();
3045 end_sequence ();
3046 in_conversion_seq = true;
3047 }
3048
3049 if (target_reg)
3050 {
3051 if (!in_conversion_seq)
3052 emit_move_insn (target_reg, stack_parm);
3053 else
3054 {
3055 push_to_sequence2 (all->first_conversion_insn,
3056 all->last_conversion_insn);
3057 emit_move_insn (target_reg, stack_parm);
3058 all->first_conversion_insn = get_insns ();
3059 all->last_conversion_insn = get_last_insn ();
3060 end_sequence ();
3061 }
3062 stack_parm = target_reg;
3063 }
3064
3065 data->stack_parm = stack_parm;
3066 set_parm_rtl (parm, stack_parm);
3067 }
3068
3069 /* A subroutine of assign_parms. Allocate a pseudo to hold the current
3070 parameter. Get it there. Perform all ABI specified conversions. */
3071
3072 static void
3073 assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
3074 struct assign_parm_data_one *data)
3075 {
3076 rtx parmreg, validated_mem;
3077 rtx equiv_stack_parm;
3078 machine_mode promoted_nominal_mode;
3079 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
3080 bool did_conversion = false;
3081 bool need_conversion, moved;
3082 rtx rtl;
3083
3084 /* Store the parm in a pseudoregister during the function, but we may
3085 need to do it in a wider mode. Using 2 here makes the result
3086 consistent with promote_decl_mode and thus expand_expr_real_1. */
3087 promoted_nominal_mode
3088 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
3089 TREE_TYPE (current_function_decl), 2);
3090
3091 parmreg = gen_reg_rtx (promoted_nominal_mode);
3092 if (!DECL_ARTIFICIAL (parm))
3093 mark_user_reg (parmreg);
3094
3095 /* If this was an item that we received a pointer to,
3096 set rtl appropriately. */
3097 if (data->passed_pointer)
3098 {
3099 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->passed_type)), parmreg);
3100 set_mem_attributes (rtl, parm, 1);
3101 }
3102 else
3103 rtl = parmreg;
3104
3105 assign_parm_remove_parallels (data);
3106
3107 /* Copy the value into the register, thus bridging between
3108 assign_parm_find_data_types and expand_expr_real_1. */
3109
3110 equiv_stack_parm = data->stack_parm;
3111 validated_mem = validize_mem (copy_rtx (data->entry_parm));
3112
3113 need_conversion = (data->nominal_mode != data->passed_mode
3114 || promoted_nominal_mode != data->promoted_mode);
3115 moved = false;
3116
3117 if (need_conversion
3118 && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
3119 && data->nominal_mode == data->passed_mode
3120 && data->nominal_mode == GET_MODE (data->entry_parm))
3121 {
3122 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
3123 mode, by the caller. We now have to convert it to
3124 NOMINAL_MODE, if different. However, PARMREG may be in
3125 a different mode than NOMINAL_MODE if it is being stored
3126 promoted.
3127
3128 If ENTRY_PARM is a hard register, it might be in a register
3129 not valid for operating in its mode (e.g., an odd-numbered
3130 register for a DFmode). In that case, moves are the only
3131 thing valid, so we can't do a convert from there. This
3132 occurs when the calling sequence allow such misaligned
3133 usages.
3134
3135 In addition, the conversion may involve a call, which could
3136 clobber parameters which haven't been copied to pseudo
3137 registers yet.
3138
3139 First, we try to emit an insn which performs the necessary
3140 conversion. We verify that this insn does not clobber any
3141 hard registers. */
3142
3143 enum insn_code icode;
3144 rtx op0, op1;
3145
3146 icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
3147 unsignedp);
3148
3149 op0 = parmreg;
3150 op1 = validated_mem;
3151 if (icode != CODE_FOR_nothing
3152 && insn_operand_matches (icode, 0, op0)
3153 && insn_operand_matches (icode, 1, op1))
3154 {
3155 enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
3156 rtx_insn *insn, *insns;
3157 rtx t = op1;
3158 HARD_REG_SET hardregs;
3159
3160 start_sequence ();
3161 /* If op1 is a hard register that is likely spilled, first
3162 force it into a pseudo, otherwise combiner might extend
3163 its lifetime too much. */
3164 if (GET_CODE (t) == SUBREG)
3165 t = SUBREG_REG (t);
3166 if (REG_P (t)
3167 && HARD_REGISTER_P (t)
3168 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (t))
3169 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (t))))
3170 {
3171 t = gen_reg_rtx (GET_MODE (op1));
3172 emit_move_insn (t, op1);
3173 }
3174 else
3175 t = op1;
3176 rtx_insn *pat = gen_extend_insn (op0, t, promoted_nominal_mode,
3177 data->passed_mode, unsignedp);
3178 emit_insn (pat);
3179 insns = get_insns ();
3180
3181 moved = true;
3182 CLEAR_HARD_REG_SET (hardregs);
3183 for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3184 {
3185 if (INSN_P (insn))
3186 note_stores (PATTERN (insn), record_hard_reg_sets,
3187 &hardregs);
3188 if (!hard_reg_set_empty_p (hardregs))
3189 moved = false;
3190 }
3191
3192 end_sequence ();
3193
3194 if (moved)
3195 {
3196 emit_insn (insns);
3197 if (equiv_stack_parm != NULL_RTX)
3198 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3199 equiv_stack_parm);
3200 }
3201 }
3202 }
3203
3204 if (moved)
3205 /* Nothing to do. */
3206 ;
3207 else if (need_conversion)
3208 {
3209 /* We did not have an insn to convert directly, or the sequence
3210 generated appeared unsafe. We must first copy the parm to a
3211 pseudo reg, and save the conversion until after all
3212 parameters have been moved. */
3213
3214 int save_tree_used;
3215 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3216
3217 emit_move_insn (tempreg, validated_mem);
3218
3219 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3220 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3221
3222 if (GET_CODE (tempreg) == SUBREG
3223 && GET_MODE (tempreg) == data->nominal_mode
3224 && REG_P (SUBREG_REG (tempreg))
3225 && data->nominal_mode == data->passed_mode
3226 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm)
3227 && GET_MODE_SIZE (GET_MODE (tempreg))
3228 < GET_MODE_SIZE (GET_MODE (data->entry_parm)))
3229 {
3230 /* The argument is already sign/zero extended, so note it
3231 into the subreg. */
3232 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3233 SUBREG_PROMOTED_SET (tempreg, unsignedp);
3234 }
3235
3236 /* TREE_USED gets set erroneously during expand_assignment. */
3237 save_tree_used = TREE_USED (parm);
3238 SET_DECL_RTL (parm, rtl);
3239 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3240 SET_DECL_RTL (parm, NULL_RTX);
3241 TREE_USED (parm) = save_tree_used;
3242 all->first_conversion_insn = get_insns ();
3243 all->last_conversion_insn = get_last_insn ();
3244 end_sequence ();
3245
3246 did_conversion = true;
3247 }
3248 else
3249 emit_move_insn (parmreg, validated_mem);
3250
3251 /* If we were passed a pointer but the actual value can safely live
3252 in a register, retrieve it and use it directly. */
3253 if (data->passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode)
3254 {
3255 /* We can't use nominal_mode, because it will have been set to
3256 Pmode above. We must use the actual mode of the parm. */
3257 if (use_register_for_decl (parm))
3258 {
3259 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3260 mark_user_reg (parmreg);
3261 }
3262 else
3263 {
3264 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3265 TYPE_MODE (TREE_TYPE (parm)),
3266 TYPE_ALIGN (TREE_TYPE (parm)));
3267 parmreg
3268 = assign_stack_local (TYPE_MODE (TREE_TYPE (parm)),
3269 GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parm))),
3270 align);
3271 set_mem_attributes (parmreg, parm, 1);
3272 }
3273
3274 if (GET_MODE (parmreg) != GET_MODE (rtl))
3275 {
3276 rtx tempreg = gen_reg_rtx (GET_MODE (rtl));
3277 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3278
3279 push_to_sequence2 (all->first_conversion_insn,
3280 all->last_conversion_insn);
3281 emit_move_insn (tempreg, rtl);
3282 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3283 emit_move_insn (parmreg, tempreg);
3284 all->first_conversion_insn = get_insns ();
3285 all->last_conversion_insn = get_last_insn ();
3286 end_sequence ();
3287
3288 did_conversion = true;
3289 }
3290 else
3291 emit_move_insn (parmreg, rtl);
3292
3293 rtl = parmreg;
3294
3295 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3296 now the parm. */
3297 data->stack_parm = NULL;
3298 }
3299
3300 set_parm_rtl (parm, rtl);
3301
3302 /* Mark the register as eliminable if we did no conversion and it was
3303 copied from memory at a fixed offset, and the arg pointer was not
3304 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3305 offset formed an invalid address, such memory-equivalences as we
3306 make here would screw up life analysis for it. */
3307 if (data->nominal_mode == data->passed_mode
3308 && !did_conversion
3309 && data->stack_parm != 0
3310 && MEM_P (data->stack_parm)
3311 && data->locate.offset.var == 0
3312 && reg_mentioned_p (virtual_incoming_args_rtx,
3313 XEXP (data->stack_parm, 0)))
3314 {
3315 rtx_insn *linsn = get_last_insn ();
3316 rtx_insn *sinsn;
3317 rtx set;
3318
3319 /* Mark complex types separately. */
3320 if (GET_CODE (parmreg) == CONCAT)
3321 {
3322 machine_mode submode
3323 = GET_MODE_INNER (GET_MODE (parmreg));
3324 int regnor = REGNO (XEXP (parmreg, 0));
3325 int regnoi = REGNO (XEXP (parmreg, 1));
3326 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3327 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3328 GET_MODE_SIZE (submode));
3329
3330 /* Scan backwards for the set of the real and
3331 imaginary parts. */
3332 for (sinsn = linsn; sinsn != 0;
3333 sinsn = prev_nonnote_insn (sinsn))
3334 {
3335 set = single_set (sinsn);
3336 if (set == 0)
3337 continue;
3338
3339 if (SET_DEST (set) == regno_reg_rtx [regnoi])
3340 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3341 else if (SET_DEST (set) == regno_reg_rtx [regnor])
3342 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3343 }
3344 }
3345 else
3346 set_dst_reg_note (linsn, REG_EQUIV, equiv_stack_parm, parmreg);
3347 }
3348
3349 /* For pointer data type, suggest pointer register. */
3350 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3351 mark_reg_pointer (parmreg,
3352 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3353 }
3354
3355 /* A subroutine of assign_parms. Allocate stack space to hold the current
3356 parameter. Get it there. Perform all ABI specified conversions. */
3357
3358 static void
3359 assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3360 struct assign_parm_data_one *data)
3361 {
3362 /* Value must be stored in the stack slot STACK_PARM during function
3363 execution. */
3364 bool to_conversion = false;
3365
3366 assign_parm_remove_parallels (data);
3367
3368 if (data->promoted_mode != data->nominal_mode)
3369 {
3370 /* Conversion is required. */
3371 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3372
3373 emit_move_insn (tempreg, validize_mem (copy_rtx (data->entry_parm)));
3374
3375 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3376 to_conversion = true;
3377
3378 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3379 TYPE_UNSIGNED (TREE_TYPE (parm)));
3380
3381 if (data->stack_parm)
3382 {
3383 int offset = subreg_lowpart_offset (data->nominal_mode,
3384 GET_MODE (data->stack_parm));
3385 /* ??? This may need a big-endian conversion on sparc64. */
3386 data->stack_parm
3387 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3388 if (offset && MEM_OFFSET_KNOWN_P (data->stack_parm))
3389 set_mem_offset (data->stack_parm,
3390 MEM_OFFSET (data->stack_parm) + offset);
3391 }
3392 }
3393
3394 if (data->entry_parm != data->stack_parm)
3395 {
3396 rtx src, dest;
3397
3398 if (data->stack_parm == 0)
3399 {
3400 int align = STACK_SLOT_ALIGNMENT (data->passed_type,
3401 GET_MODE (data->entry_parm),
3402 TYPE_ALIGN (data->passed_type));
3403 data->stack_parm
3404 = assign_stack_local (GET_MODE (data->entry_parm),
3405 GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3406 align);
3407 set_mem_attributes (data->stack_parm, parm, 1);
3408 }
3409
3410 dest = validize_mem (copy_rtx (data->stack_parm));
3411 src = validize_mem (copy_rtx (data->entry_parm));
3412
3413 if (MEM_P (src))
3414 {
3415 /* Use a block move to handle potentially misaligned entry_parm. */
3416 if (!to_conversion)
3417 push_to_sequence2 (all->first_conversion_insn,
3418 all->last_conversion_insn);
3419 to_conversion = true;
3420
3421 emit_block_move (dest, src,
3422 GEN_INT (int_size_in_bytes (data->passed_type)),
3423 BLOCK_OP_NORMAL);
3424 }
3425 else
3426 emit_move_insn (dest, src);
3427 }
3428
3429 if (to_conversion)
3430 {
3431 all->first_conversion_insn = get_insns ();
3432 all->last_conversion_insn = get_last_insn ();
3433 end_sequence ();
3434 }
3435
3436 set_parm_rtl (parm, data->stack_parm);
3437 }
3438
3439 /* A subroutine of assign_parms. If the ABI splits complex arguments, then
3440 undo the frobbing that we did in assign_parms_augmented_arg_list. */
3441
3442 static void
3443 assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3444 vec<tree> fnargs)
3445 {
3446 tree parm;
3447 tree orig_fnargs = all->orig_fnargs;
3448 unsigned i = 0;
3449
3450 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3451 {
3452 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3453 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3454 {
3455 rtx tmp, real, imag;
3456 machine_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3457
3458 real = DECL_RTL (fnargs[i]);
3459 imag = DECL_RTL (fnargs[i + 1]);
3460 if (inner != GET_MODE (real))
3461 {
3462 real = gen_lowpart_SUBREG (inner, real);
3463 imag = gen_lowpart_SUBREG (inner, imag);
3464 }
3465
3466 if (TREE_ADDRESSABLE (parm))
3467 {
3468 rtx rmem, imem;
3469 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3470 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3471 DECL_MODE (parm),
3472 TYPE_ALIGN (TREE_TYPE (parm)));
3473
3474 /* split_complex_arg put the real and imag parts in
3475 pseudos. Move them to memory. */
3476 tmp = assign_stack_local (DECL_MODE (parm), size, align);
3477 set_mem_attributes (tmp, parm, 1);
3478 rmem = adjust_address_nv (tmp, inner, 0);
3479 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3480 push_to_sequence2 (all->first_conversion_insn,
3481 all->last_conversion_insn);
3482 emit_move_insn (rmem, real);
3483 emit_move_insn (imem, imag);
3484 all->first_conversion_insn = get_insns ();
3485 all->last_conversion_insn = get_last_insn ();
3486 end_sequence ();
3487 }
3488 else
3489 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3490 set_parm_rtl (parm, tmp);
3491
3492 real = DECL_INCOMING_RTL (fnargs[i]);
3493 imag = DECL_INCOMING_RTL (fnargs[i + 1]);
3494 if (inner != GET_MODE (real))
3495 {
3496 real = gen_lowpart_SUBREG (inner, real);
3497 imag = gen_lowpart_SUBREG (inner, imag);
3498 }
3499 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3500 set_decl_incoming_rtl (parm, tmp, false);
3501 i++;
3502 }
3503 }
3504 }
3505
3506 /* Load bounds of PARM from bounds table. */
3507 static void
3508 assign_parm_load_bounds (struct assign_parm_data_one *data,
3509 tree parm,
3510 rtx entry,
3511 unsigned bound_no)
3512 {
3513 bitmap_iterator bi;
3514 unsigned i, offs = 0;
3515 int bnd_no = -1;
3516 rtx slot = NULL, ptr = NULL;
3517
3518 if (parm)
3519 {
3520 bitmap slots;
3521 bitmap_obstack_initialize (NULL);
3522 slots = BITMAP_ALLOC (NULL);
3523 chkp_find_bound_slots (TREE_TYPE (parm), slots);
3524 EXECUTE_IF_SET_IN_BITMAP (slots, 0, i, bi)
3525 {
3526 if (bound_no)
3527 bound_no--;
3528 else
3529 {
3530 bnd_no = i;
3531 break;
3532 }
3533 }
3534 BITMAP_FREE (slots);
3535 bitmap_obstack_release (NULL);
3536 }
3537
3538 /* We may have bounds not associated with any pointer. */
3539 if (bnd_no != -1)
3540 offs = bnd_no * POINTER_SIZE / BITS_PER_UNIT;
3541
3542 /* Find associated pointer. */
3543 if (bnd_no == -1)
3544 {
3545 /* If bounds are not associated with any bounds,
3546 then it is passed in a register or special slot. */
3547 gcc_assert (data->entry_parm);
3548 ptr = const0_rtx;
3549 }
3550 else if (MEM_P (entry))
3551 slot = adjust_address (entry, Pmode, offs);
3552 else if (REG_P (entry))
3553 ptr = gen_rtx_REG (Pmode, REGNO (entry) + bnd_no);
3554 else if (GET_CODE (entry) == PARALLEL)
3555 ptr = chkp_get_value_with_offs (entry, GEN_INT (offs));
3556 else
3557 gcc_unreachable ();
3558 data->entry_parm = targetm.calls.load_bounds_for_arg (slot, ptr,
3559 data->entry_parm);
3560 }
3561
3562 /* Assign RTL expressions to the function's bounds parameters BNDARGS. */
3563
3564 static void
3565 assign_bounds (vec<bounds_parm_data> &bndargs,
3566 struct assign_parm_data_all &all,
3567 bool assign_regs, bool assign_special,
3568 bool assign_bt)
3569 {
3570 unsigned i, pass;
3571 bounds_parm_data *pbdata;
3572
3573 if (!bndargs.exists ())
3574 return;
3575
3576 /* We make few passes to store input bounds. Firstly handle bounds
3577 passed in registers. After that we load bounds passed in special
3578 slots. Finally we load bounds from Bounds Table. */
3579 for (pass = 0; pass < 3; pass++)
3580 FOR_EACH_VEC_ELT (bndargs, i, pbdata)
3581 {
3582 /* Pass 0 => regs only. */
3583 if (pass == 0
3584 && (!assign_regs
3585 ||(!pbdata->parm_data.entry_parm
3586 || GET_CODE (pbdata->parm_data.entry_parm) != REG)))
3587 continue;
3588 /* Pass 1 => slots only. */
3589 else if (pass == 1
3590 && (!assign_special
3591 || (!pbdata->parm_data.entry_parm
3592 || GET_CODE (pbdata->parm_data.entry_parm) == REG)))
3593 continue;
3594 /* Pass 2 => BT only. */
3595 else if (pass == 2
3596 && (!assign_bt
3597 || pbdata->parm_data.entry_parm))
3598 continue;
3599
3600 if (!pbdata->parm_data.entry_parm
3601 || GET_CODE (pbdata->parm_data.entry_parm) != REG)
3602 assign_parm_load_bounds (&pbdata->parm_data, pbdata->ptr_parm,
3603 pbdata->ptr_entry, pbdata->bound_no);
3604
3605 set_decl_incoming_rtl (pbdata->bounds_parm,
3606 pbdata->parm_data.entry_parm, false);
3607
3608 if (assign_parm_setup_block_p (&pbdata->parm_data))
3609 assign_parm_setup_block (&all, pbdata->bounds_parm,
3610 &pbdata->parm_data);
3611 else if (pbdata->parm_data.passed_pointer
3612 || use_register_for_decl (pbdata->bounds_parm))
3613 assign_parm_setup_reg (&all, pbdata->bounds_parm,
3614 &pbdata->parm_data);
3615 else
3616 assign_parm_setup_stack (&all, pbdata->bounds_parm,
3617 &pbdata->parm_data);
3618 }
3619 }
3620
3621 /* Assign RTL expressions to the function's parameters. This may involve
3622 copying them into registers and using those registers as the DECL_RTL. */
3623
3624 static void
3625 assign_parms (tree fndecl)
3626 {
3627 struct assign_parm_data_all all;
3628 tree parm;
3629 vec<tree> fnargs;
3630 unsigned i, bound_no = 0;
3631 tree last_arg = NULL;
3632 rtx last_arg_entry = NULL;
3633 vec<bounds_parm_data> bndargs = vNULL;
3634 bounds_parm_data bdata;
3635
3636 crtl->args.internal_arg_pointer
3637 = targetm.calls.internal_arg_pointer ();
3638
3639 assign_parms_initialize_all (&all);
3640 fnargs = assign_parms_augmented_arg_list (&all);
3641
3642 FOR_EACH_VEC_ELT (fnargs, i, parm)
3643 {
3644 struct assign_parm_data_one data;
3645
3646 /* Extract the type of PARM; adjust it according to ABI. */
3647 assign_parm_find_data_types (&all, parm, &data);
3648
3649 /* Early out for errors and void parameters. */
3650 if (data.passed_mode == VOIDmode)
3651 {
3652 SET_DECL_RTL (parm, const0_rtx);
3653 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3654 continue;
3655 }
3656
3657 /* Estimate stack alignment from parameter alignment. */
3658 if (SUPPORTS_STACK_ALIGNMENT)
3659 {
3660 unsigned int align
3661 = targetm.calls.function_arg_boundary (data.promoted_mode,
3662 data.passed_type);
3663 align = MINIMUM_ALIGNMENT (data.passed_type, data.promoted_mode,
3664 align);
3665 if (TYPE_ALIGN (data.nominal_type) > align)
3666 align = MINIMUM_ALIGNMENT (data.nominal_type,
3667 TYPE_MODE (data.nominal_type),
3668 TYPE_ALIGN (data.nominal_type));
3669 if (crtl->stack_alignment_estimated < align)
3670 {
3671 gcc_assert (!crtl->stack_realign_processed);
3672 crtl->stack_alignment_estimated = align;
3673 }
3674 }
3675
3676 /* Find out where the parameter arrives in this function. */
3677 assign_parm_find_entry_rtl (&all, &data);
3678
3679 /* Find out where stack space for this parameter might be. */
3680 if (assign_parm_is_stack_parm (&all, &data))
3681 {
3682 assign_parm_find_stack_rtl (parm, &data);
3683 assign_parm_adjust_entry_rtl (&data);
3684 }
3685 if (!POINTER_BOUNDS_TYPE_P (data.passed_type))
3686 {
3687 /* Remember where last non bounds arg was passed in case
3688 we have to load associated bounds for it from Bounds
3689 Table. */
3690 last_arg = parm;
3691 last_arg_entry = data.entry_parm;
3692 bound_no = 0;
3693 }
3694 /* Record permanently how this parm was passed. */
3695 if (data.passed_pointer)
3696 {
3697 rtx incoming_rtl
3698 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.passed_type)),
3699 data.entry_parm);
3700 set_decl_incoming_rtl (parm, incoming_rtl, true);
3701 }
3702 else
3703 set_decl_incoming_rtl (parm, data.entry_parm, false);
3704
3705 assign_parm_adjust_stack_rtl (&data);
3706
3707 /* Bounds should be loaded in the particular order to
3708 have registers allocated correctly. Collect info about
3709 input bounds and load them later. */
3710 if (POINTER_BOUNDS_TYPE_P (data.passed_type))
3711 {
3712 /* Expect bounds in instrumented functions only. */
3713 gcc_assert (chkp_function_instrumented_p (fndecl));
3714
3715 bdata.parm_data = data;
3716 bdata.bounds_parm = parm;
3717 bdata.ptr_parm = last_arg;
3718 bdata.ptr_entry = last_arg_entry;
3719 bdata.bound_no = bound_no;
3720 bndargs.safe_push (bdata);
3721 }
3722 else
3723 {
3724 if (assign_parm_setup_block_p (&data))
3725 assign_parm_setup_block (&all, parm, &data);
3726 else if (data.passed_pointer || use_register_for_decl (parm))
3727 assign_parm_setup_reg (&all, parm, &data);
3728 else
3729 assign_parm_setup_stack (&all, parm, &data);
3730 }
3731
3732 if (cfun->stdarg && !DECL_CHAIN (parm))
3733 {
3734 int pretend_bytes = 0;
3735
3736 assign_parms_setup_varargs (&all, &data, false);
3737
3738 if (chkp_function_instrumented_p (fndecl))
3739 {
3740 /* We expect this is the last parm. Otherwise it is wrong
3741 to assign bounds right now. */
3742 gcc_assert (i == (fnargs.length () - 1));
3743 assign_bounds (bndargs, all, true, false, false);
3744 targetm.calls.setup_incoming_vararg_bounds (all.args_so_far,
3745 data.promoted_mode,
3746 data.passed_type,
3747 &pretend_bytes,
3748 false);
3749 assign_bounds (bndargs, all, false, true, true);
3750 bndargs.release ();
3751 }
3752 }
3753
3754 /* Update info on where next arg arrives in registers. */
3755 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3756 data.passed_type, data.named_arg);
3757
3758 if (POINTER_BOUNDS_TYPE_P (data.passed_type))
3759 bound_no++;
3760 }
3761
3762 assign_bounds (bndargs, all, true, true, true);
3763 bndargs.release ();
3764
3765 if (targetm.calls.split_complex_arg)
3766 assign_parms_unsplit_complex (&all, fnargs);
3767
3768 fnargs.release ();
3769
3770 /* Output all parameter conversion instructions (possibly including calls)
3771 now that all parameters have been copied out of hard registers. */
3772 emit_insn (all.first_conversion_insn);
3773
3774 /* Estimate reload stack alignment from scalar return mode. */
3775 if (SUPPORTS_STACK_ALIGNMENT)
3776 {
3777 if (DECL_RESULT (fndecl))
3778 {
3779 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3780 machine_mode mode = TYPE_MODE (type);
3781
3782 if (mode != BLKmode
3783 && mode != VOIDmode
3784 && !AGGREGATE_TYPE_P (type))
3785 {
3786 unsigned int align = GET_MODE_ALIGNMENT (mode);
3787 if (crtl->stack_alignment_estimated < align)
3788 {
3789 gcc_assert (!crtl->stack_realign_processed);
3790 crtl->stack_alignment_estimated = align;
3791 }
3792 }
3793 }
3794 }
3795
3796 /* If we are receiving a struct value address as the first argument, set up
3797 the RTL for the function result. As this might require code to convert
3798 the transmitted address to Pmode, we do this here to ensure that possible
3799 preliminary conversions of the address have been emitted already. */
3800 if (all.function_result_decl)
3801 {
3802 tree result = DECL_RESULT (current_function_decl);
3803 rtx addr = DECL_RTL (all.function_result_decl);
3804 rtx x;
3805
3806 if (DECL_BY_REFERENCE (result))
3807 {
3808 SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3809 x = addr;
3810 }
3811 else
3812 {
3813 SET_DECL_VALUE_EXPR (result,
3814 build1 (INDIRECT_REF, TREE_TYPE (result),
3815 all.function_result_decl));
3816 addr = convert_memory_address (Pmode, addr);
3817 x = gen_rtx_MEM (DECL_MODE (result), addr);
3818 set_mem_attributes (x, result, 1);
3819 }
3820
3821 DECL_HAS_VALUE_EXPR_P (result) = 1;
3822
3823 set_parm_rtl (result, x);
3824 }
3825
3826 /* We have aligned all the args, so add space for the pretend args. */
3827 crtl->args.pretend_args_size = all.pretend_args_size;
3828 all.stack_args_size.constant += all.extra_pretend_bytes;
3829 crtl->args.size = all.stack_args_size.constant;
3830
3831 /* Adjust function incoming argument size for alignment and
3832 minimum length. */
3833
3834 crtl->args.size = MAX (crtl->args.size, all.reg_parm_stack_space);
3835 crtl->args.size = CEIL_ROUND (crtl->args.size,
3836 PARM_BOUNDARY / BITS_PER_UNIT);
3837
3838 if (ARGS_GROW_DOWNWARD)
3839 {
3840 crtl->args.arg_offset_rtx
3841 = (all.stack_args_size.var == 0 ? GEN_INT (-all.stack_args_size.constant)
3842 : expand_expr (size_diffop (all.stack_args_size.var,
3843 size_int (-all.stack_args_size.constant)),
3844 NULL_RTX, VOIDmode, EXPAND_NORMAL));
3845 }
3846 else
3847 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3848
3849 /* See how many bytes, if any, of its args a function should try to pop
3850 on return. */
3851
3852 crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3853 TREE_TYPE (fndecl),
3854 crtl->args.size);
3855
3856 /* For stdarg.h function, save info about
3857 regs and stack space used by the named args. */
3858
3859 crtl->args.info = all.args_so_far_v;
3860
3861 /* Set the rtx used for the function return value. Put this in its
3862 own variable so any optimizers that need this information don't have
3863 to include tree.h. Do this here so it gets done when an inlined
3864 function gets output. */
3865
3866 crtl->return_rtx
3867 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3868 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3869
3870 /* If scalar return value was computed in a pseudo-reg, or was a named
3871 return value that got dumped to the stack, copy that to the hard
3872 return register. */
3873 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3874 {
3875 tree decl_result = DECL_RESULT (fndecl);
3876 rtx decl_rtl = DECL_RTL (decl_result);
3877
3878 if (REG_P (decl_rtl)
3879 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3880 : DECL_REGISTER (decl_result))
3881 {
3882 rtx real_decl_rtl;
3883
3884 real_decl_rtl = targetm.calls.function_value (TREE_TYPE (decl_result),
3885 fndecl, true);
3886 if (chkp_function_instrumented_p (fndecl))
3887 crtl->return_bnd
3888 = targetm.calls.chkp_function_value_bounds (TREE_TYPE (decl_result),
3889 fndecl, true);
3890 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3891 /* The delay slot scheduler assumes that crtl->return_rtx
3892 holds the hard register containing the return value, not a
3893 temporary pseudo. */
3894 crtl->return_rtx = real_decl_rtl;
3895 }
3896 }
3897 }
3898
3899 /* A subroutine of gimplify_parameters, invoked via walk_tree.
3900 For all seen types, gimplify their sizes. */
3901
3902 static tree
3903 gimplify_parm_type (tree *tp, int *walk_subtrees, void *data)
3904 {
3905 tree t = *tp;
3906
3907 *walk_subtrees = 0;
3908 if (TYPE_P (t))
3909 {
3910 if (POINTER_TYPE_P (t))
3911 *walk_subtrees = 1;
3912 else if (TYPE_SIZE (t) && !TREE_CONSTANT (TYPE_SIZE (t))
3913 && !TYPE_SIZES_GIMPLIFIED (t))
3914 {
3915 gimplify_type_sizes (t, (gimple_seq *) data);
3916 *walk_subtrees = 1;
3917 }
3918 }
3919
3920 return NULL;
3921 }
3922
3923 /* Gimplify the parameter list for current_function_decl. This involves
3924 evaluating SAVE_EXPRs of variable sized parameters and generating code
3925 to implement callee-copies reference parameters. Returns a sequence of
3926 statements to add to the beginning of the function. */
3927
3928 gimple_seq
3929 gimplify_parameters (void)
3930 {
3931 struct assign_parm_data_all all;
3932 tree parm;
3933 gimple_seq stmts = NULL;
3934 vec<tree> fnargs;
3935 unsigned i;
3936
3937 assign_parms_initialize_all (&all);
3938 fnargs = assign_parms_augmented_arg_list (&all);
3939
3940 FOR_EACH_VEC_ELT (fnargs, i, parm)
3941 {
3942 struct assign_parm_data_one data;
3943
3944 /* Extract the type of PARM; adjust it according to ABI. */
3945 assign_parm_find_data_types (&all, parm, &data);
3946
3947 /* Early out for errors and void parameters. */
3948 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3949 continue;
3950
3951 /* Update info on where next arg arrives in registers. */
3952 targetm.calls.function_arg_advance (all.args_so_far, data.promoted_mode,
3953 data.passed_type, data.named_arg);
3954
3955 /* ??? Once upon a time variable_size stuffed parameter list
3956 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3957 turned out to be less than manageable in the gimple world.
3958 Now we have to hunt them down ourselves. */
3959 walk_tree_without_duplicates (&data.passed_type,
3960 gimplify_parm_type, &stmts);
3961
3962 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3963 {
3964 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3965 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3966 }
3967
3968 if (data.passed_pointer)
3969 {
3970 tree type = TREE_TYPE (data.passed_type);
3971 if (reference_callee_copied (&all.args_so_far_v, TYPE_MODE (type),
3972 type, data.named_arg))
3973 {
3974 tree local, t;
3975
3976 /* For constant-sized objects, this is trivial; for
3977 variable-sized objects, we have to play games. */
3978 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3979 && !(flag_stack_check == GENERIC_STACK_CHECK
3980 && compare_tree_int (DECL_SIZE_UNIT (parm),
3981 STACK_CHECK_MAX_VAR_SIZE) > 0))
3982 {
3983 local = create_tmp_var (type, get_name (parm));
3984 DECL_IGNORED_P (local) = 0;
3985 /* If PARM was addressable, move that flag over
3986 to the local copy, as its address will be taken,
3987 not the PARMs. Keep the parms address taken
3988 as we'll query that flag during gimplification. */
3989 if (TREE_ADDRESSABLE (parm))
3990 TREE_ADDRESSABLE (local) = 1;
3991 else if (TREE_CODE (type) == COMPLEX_TYPE
3992 || TREE_CODE (type) == VECTOR_TYPE)
3993 DECL_GIMPLE_REG_P (local) = 1;
3994 }
3995 else
3996 {
3997 tree ptr_type, addr;
3998
3999 ptr_type = build_pointer_type (type);
4000 addr = create_tmp_reg (ptr_type, get_name (parm));
4001 DECL_IGNORED_P (addr) = 0;
4002 local = build_fold_indirect_ref (addr);
4003
4004 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
4005 t = build_call_expr (t, 2, DECL_SIZE_UNIT (parm),
4006 size_int (DECL_ALIGN (parm)));
4007
4008 /* The call has been built for a variable-sized object. */
4009 CALL_ALLOCA_FOR_VAR_P (t) = 1;
4010 t = fold_convert (ptr_type, t);
4011 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
4012 gimplify_and_add (t, &stmts);
4013 }
4014
4015 gimplify_assign (local, parm, &stmts);
4016
4017 SET_DECL_VALUE_EXPR (parm, local);
4018 DECL_HAS_VALUE_EXPR_P (parm) = 1;
4019 }
4020 }
4021 }
4022
4023 fnargs.release ();
4024
4025 return stmts;
4026 }
4027 \f
4028 /* Compute the size and offset from the start of the stacked arguments for a
4029 parm passed in mode PASSED_MODE and with type TYPE.
4030
4031 INITIAL_OFFSET_PTR points to the current offset into the stacked
4032 arguments.
4033
4034 The starting offset and size for this parm are returned in
4035 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
4036 nonzero, the offset is that of stack slot, which is returned in
4037 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
4038 padding required from the initial offset ptr to the stack slot.
4039
4040 IN_REGS is nonzero if the argument will be passed in registers. It will
4041 never be set if REG_PARM_STACK_SPACE is not defined.
4042
4043 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
4044 for arguments which are passed in registers.
4045
4046 FNDECL is the function in which the argument was defined.
4047
4048 There are two types of rounding that are done. The first, controlled by
4049 TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
4050 argument list to be aligned to the specific boundary (in bits). This
4051 rounding affects the initial and starting offsets, but not the argument
4052 size.
4053
4054 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
4055 optionally rounds the size of the parm to PARM_BOUNDARY. The
4056 initial offset is not affected by this rounding, while the size always
4057 is and the starting offset may be. */
4058
4059 /* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
4060 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
4061 callers pass in the total size of args so far as
4062 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
4063
4064 void
4065 locate_and_pad_parm (machine_mode passed_mode, tree type, int in_regs,
4066 int reg_parm_stack_space, int partial,
4067 tree fndecl ATTRIBUTE_UNUSED,
4068 struct args_size *initial_offset_ptr,
4069 struct locate_and_pad_arg_data *locate)
4070 {
4071 tree sizetree;
4072 enum direction where_pad;
4073 unsigned int boundary, round_boundary;
4074 int part_size_in_regs;
4075
4076 /* If we have found a stack parm before we reach the end of the
4077 area reserved for registers, skip that area. */
4078 if (! in_regs)
4079 {
4080 if (reg_parm_stack_space > 0)
4081 {
4082 if (initial_offset_ptr->var)
4083 {
4084 initial_offset_ptr->var
4085 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
4086 ssize_int (reg_parm_stack_space));
4087 initial_offset_ptr->constant = 0;
4088 }
4089 else if (initial_offset_ptr->constant < reg_parm_stack_space)
4090 initial_offset_ptr->constant = reg_parm_stack_space;
4091 }
4092 }
4093
4094 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
4095
4096 sizetree
4097 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
4098 where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
4099 boundary = targetm.calls.function_arg_boundary (passed_mode, type);
4100 round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
4101 type);
4102 locate->where_pad = where_pad;
4103
4104 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
4105 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4106 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4107
4108 locate->boundary = boundary;
4109
4110 if (SUPPORTS_STACK_ALIGNMENT)
4111 {
4112 /* stack_alignment_estimated can't change after stack has been
4113 realigned. */
4114 if (crtl->stack_alignment_estimated < boundary)
4115 {
4116 if (!crtl->stack_realign_processed)
4117 crtl->stack_alignment_estimated = boundary;
4118 else
4119 {
4120 /* If stack is realigned and stack alignment value
4121 hasn't been finalized, it is OK not to increase
4122 stack_alignment_estimated. The bigger alignment
4123 requirement is recorded in stack_alignment_needed
4124 below. */
4125 gcc_assert (!crtl->stack_realign_finalized
4126 && crtl->stack_realign_needed);
4127 }
4128 }
4129 }
4130
4131 /* Remember if the outgoing parameter requires extra alignment on the
4132 calling function side. */
4133 if (crtl->stack_alignment_needed < boundary)
4134 crtl->stack_alignment_needed = boundary;
4135 if (crtl->preferred_stack_boundary < boundary)
4136 crtl->preferred_stack_boundary = boundary;
4137
4138 if (ARGS_GROW_DOWNWARD)
4139 {
4140 locate->slot_offset.constant = -initial_offset_ptr->constant;
4141 if (initial_offset_ptr->var)
4142 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
4143 initial_offset_ptr->var);
4144
4145 {
4146 tree s2 = sizetree;
4147 if (where_pad != none
4148 && (!tree_fits_uhwi_p (sizetree)
4149 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4150 s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
4151 SUB_PARM_SIZE (locate->slot_offset, s2);
4152 }
4153
4154 locate->slot_offset.constant += part_size_in_regs;
4155
4156 if (!in_regs || reg_parm_stack_space > 0)
4157 pad_to_arg_alignment (&locate->slot_offset, boundary,
4158 &locate->alignment_pad);
4159
4160 locate->size.constant = (-initial_offset_ptr->constant
4161 - locate->slot_offset.constant);
4162 if (initial_offset_ptr->var)
4163 locate->size.var = size_binop (MINUS_EXPR,
4164 size_binop (MINUS_EXPR,
4165 ssize_int (0),
4166 initial_offset_ptr->var),
4167 locate->slot_offset.var);
4168
4169 /* Pad_below needs the pre-rounded size to know how much to pad
4170 below. */
4171 locate->offset = locate->slot_offset;
4172 if (where_pad == downward)
4173 pad_below (&locate->offset, passed_mode, sizetree);
4174
4175 }
4176 else
4177 {
4178 if (!in_regs || reg_parm_stack_space > 0)
4179 pad_to_arg_alignment (initial_offset_ptr, boundary,
4180 &locate->alignment_pad);
4181 locate->slot_offset = *initial_offset_ptr;
4182
4183 #ifdef PUSH_ROUNDING
4184 if (passed_mode != BLKmode)
4185 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
4186 #endif
4187
4188 /* Pad_below needs the pre-rounded size to know how much to pad below
4189 so this must be done before rounding up. */
4190 locate->offset = locate->slot_offset;
4191 if (where_pad == downward)
4192 pad_below (&locate->offset, passed_mode, sizetree);
4193
4194 if (where_pad != none
4195 && (!tree_fits_uhwi_p (sizetree)
4196 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4197 sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
4198
4199 ADD_PARM_SIZE (locate->size, sizetree);
4200
4201 locate->size.constant -= part_size_in_regs;
4202 }
4203
4204 #ifdef FUNCTION_ARG_OFFSET
4205 locate->offset.constant += FUNCTION_ARG_OFFSET (passed_mode, type);
4206 #endif
4207 }
4208
4209 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
4210 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
4211
4212 static void
4213 pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
4214 struct args_size *alignment_pad)
4215 {
4216 tree save_var = NULL_TREE;
4217 HOST_WIDE_INT save_constant = 0;
4218 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4219 HOST_WIDE_INT sp_offset = STACK_POINTER_OFFSET;
4220
4221 #ifdef SPARC_STACK_BOUNDARY_HACK
4222 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
4223 the real alignment of %sp. However, when it does this, the
4224 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
4225 if (SPARC_STACK_BOUNDARY_HACK)
4226 sp_offset = 0;
4227 #endif
4228
4229 if (boundary > PARM_BOUNDARY)
4230 {
4231 save_var = offset_ptr->var;
4232 save_constant = offset_ptr->constant;
4233 }
4234
4235 alignment_pad->var = NULL_TREE;
4236 alignment_pad->constant = 0;
4237
4238 if (boundary > BITS_PER_UNIT)
4239 {
4240 if (offset_ptr->var)
4241 {
4242 tree sp_offset_tree = ssize_int (sp_offset);
4243 tree offset = size_binop (PLUS_EXPR,
4244 ARGS_SIZE_TREE (*offset_ptr),
4245 sp_offset_tree);
4246 tree rounded;
4247 if (ARGS_GROW_DOWNWARD)
4248 rounded = round_down (offset, boundary / BITS_PER_UNIT);
4249 else
4250 rounded = round_up (offset, boundary / BITS_PER_UNIT);
4251
4252 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
4253 /* ARGS_SIZE_TREE includes constant term. */
4254 offset_ptr->constant = 0;
4255 if (boundary > PARM_BOUNDARY)
4256 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
4257 save_var);
4258 }
4259 else
4260 {
4261 offset_ptr->constant = -sp_offset +
4262 (ARGS_GROW_DOWNWARD
4263 ? FLOOR_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes)
4264 : CEIL_ROUND (offset_ptr->constant + sp_offset, boundary_in_bytes));
4265
4266 if (boundary > PARM_BOUNDARY)
4267 alignment_pad->constant = offset_ptr->constant - save_constant;
4268 }
4269 }
4270 }
4271
4272 static void
4273 pad_below (struct args_size *offset_ptr, machine_mode passed_mode, tree sizetree)
4274 {
4275 if (passed_mode != BLKmode)
4276 {
4277 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
4278 offset_ptr->constant
4279 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
4280 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
4281 - GET_MODE_SIZE (passed_mode));
4282 }
4283 else
4284 {
4285 if (TREE_CODE (sizetree) != INTEGER_CST
4286 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
4287 {
4288 /* Round the size up to multiple of PARM_BOUNDARY bits. */
4289 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4290 /* Add it in. */
4291 ADD_PARM_SIZE (*offset_ptr, s2);
4292 SUB_PARM_SIZE (*offset_ptr, sizetree);
4293 }
4294 }
4295 }
4296 \f
4297
4298 /* True if register REGNO was alive at a place where `setjmp' was
4299 called and was set more than once or is an argument. Such regs may
4300 be clobbered by `longjmp'. */
4301
4302 static bool
4303 regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
4304 {
4305 /* There appear to be cases where some local vars never reach the
4306 backend but have bogus regnos. */
4307 if (regno >= max_reg_num ())
4308 return false;
4309
4310 return ((REG_N_SETS (regno) > 1
4311 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
4312 regno))
4313 && REGNO_REG_SET_P (setjmp_crosses, regno));
4314 }
4315
4316 /* Walk the tree of blocks describing the binding levels within a
4317 function and warn about variables the might be killed by setjmp or
4318 vfork. This is done after calling flow_analysis before register
4319 allocation since that will clobber the pseudo-regs to hard
4320 regs. */
4321
4322 static void
4323 setjmp_vars_warning (bitmap setjmp_crosses, tree block)
4324 {
4325 tree decl, sub;
4326
4327 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
4328 {
4329 if (TREE_CODE (decl) == VAR_DECL
4330 && DECL_RTL_SET_P (decl)
4331 && REG_P (DECL_RTL (decl))
4332 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4333 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
4334 " %<longjmp%> or %<vfork%>", decl);
4335 }
4336
4337 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
4338 setjmp_vars_warning (setjmp_crosses, sub);
4339 }
4340
4341 /* Do the appropriate part of setjmp_vars_warning
4342 but for arguments instead of local variables. */
4343
4344 static void
4345 setjmp_args_warning (bitmap setjmp_crosses)
4346 {
4347 tree decl;
4348 for (decl = DECL_ARGUMENTS (current_function_decl);
4349 decl; decl = DECL_CHAIN (decl))
4350 if (DECL_RTL (decl) != 0
4351 && REG_P (DECL_RTL (decl))
4352 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4353 warning (OPT_Wclobbered,
4354 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
4355 decl);
4356 }
4357
4358 /* Generate warning messages for variables live across setjmp. */
4359
4360 void
4361 generate_setjmp_warnings (void)
4362 {
4363 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4364
4365 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS
4366 || bitmap_empty_p (setjmp_crosses))
4367 return;
4368
4369 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4370 setjmp_args_warning (setjmp_crosses);
4371 }
4372
4373 \f
4374 /* Reverse the order of elements in the fragment chain T of blocks,
4375 and return the new head of the chain (old last element).
4376 In addition to that clear BLOCK_SAME_RANGE flags when needed
4377 and adjust BLOCK_SUPERCONTEXT from the super fragment to
4378 its super fragment origin. */
4379
4380 static tree
4381 block_fragments_nreverse (tree t)
4382 {
4383 tree prev = 0, block, next, prev_super = 0;
4384 tree super = BLOCK_SUPERCONTEXT (t);
4385 if (BLOCK_FRAGMENT_ORIGIN (super))
4386 super = BLOCK_FRAGMENT_ORIGIN (super);
4387 for (block = t; block; block = next)
4388 {
4389 next = BLOCK_FRAGMENT_CHAIN (block);
4390 BLOCK_FRAGMENT_CHAIN (block) = prev;
4391 if ((prev && !BLOCK_SAME_RANGE (prev))
4392 || (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (block))
4393 != prev_super))
4394 BLOCK_SAME_RANGE (block) = 0;
4395 prev_super = BLOCK_SUPERCONTEXT (block);
4396 BLOCK_SUPERCONTEXT (block) = super;
4397 prev = block;
4398 }
4399 t = BLOCK_FRAGMENT_ORIGIN (t);
4400 if (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (t))
4401 != prev_super)
4402 BLOCK_SAME_RANGE (t) = 0;
4403 BLOCK_SUPERCONTEXT (t) = super;
4404 return prev;
4405 }
4406
4407 /* Reverse the order of elements in the chain T of blocks,
4408 and return the new head of the chain (old last element).
4409 Also do the same on subblocks and reverse the order of elements
4410 in BLOCK_FRAGMENT_CHAIN as well. */
4411
4412 static tree
4413 blocks_nreverse_all (tree t)
4414 {
4415 tree prev = 0, block, next;
4416 for (block = t; block; block = next)
4417 {
4418 next = BLOCK_CHAIN (block);
4419 BLOCK_CHAIN (block) = prev;
4420 if (BLOCK_FRAGMENT_CHAIN (block)
4421 && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4422 {
4423 BLOCK_FRAGMENT_CHAIN (block)
4424 = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4425 if (!BLOCK_SAME_RANGE (BLOCK_FRAGMENT_CHAIN (block)))
4426 BLOCK_SAME_RANGE (block) = 0;
4427 }
4428 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4429 prev = block;
4430 }
4431 return prev;
4432 }
4433
4434
4435 /* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4436 and create duplicate blocks. */
4437 /* ??? Need an option to either create block fragments or to create
4438 abstract origin duplicates of a source block. It really depends
4439 on what optimization has been performed. */
4440
4441 void
4442 reorder_blocks (void)
4443 {
4444 tree block = DECL_INITIAL (current_function_decl);
4445
4446 if (block == NULL_TREE)
4447 return;
4448
4449 auto_vec<tree, 10> block_stack;
4450
4451 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4452 clear_block_marks (block);
4453
4454 /* Prune the old trees away, so that they don't get in the way. */
4455 BLOCK_SUBBLOCKS (block) = NULL_TREE;
4456 BLOCK_CHAIN (block) = NULL_TREE;
4457
4458 /* Recreate the block tree from the note nesting. */
4459 reorder_blocks_1 (get_insns (), block, &block_stack);
4460 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4461 }
4462
4463 /* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
4464
4465 void
4466 clear_block_marks (tree block)
4467 {
4468 while (block)
4469 {
4470 TREE_ASM_WRITTEN (block) = 0;
4471 clear_block_marks (BLOCK_SUBBLOCKS (block));
4472 block = BLOCK_CHAIN (block);
4473 }
4474 }
4475
4476 static void
4477 reorder_blocks_1 (rtx_insn *insns, tree current_block,
4478 vec<tree> *p_block_stack)
4479 {
4480 rtx_insn *insn;
4481 tree prev_beg = NULL_TREE, prev_end = NULL_TREE;
4482
4483 for (insn = insns; insn; insn = NEXT_INSN (insn))
4484 {
4485 if (NOTE_P (insn))
4486 {
4487 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4488 {
4489 tree block = NOTE_BLOCK (insn);
4490 tree origin;
4491
4492 gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4493 origin = block;
4494
4495 if (prev_end)
4496 BLOCK_SAME_RANGE (prev_end) = 0;
4497 prev_end = NULL_TREE;
4498
4499 /* If we have seen this block before, that means it now
4500 spans multiple address regions. Create a new fragment. */
4501 if (TREE_ASM_WRITTEN (block))
4502 {
4503 tree new_block = copy_node (block);
4504
4505 BLOCK_SAME_RANGE (new_block) = 0;
4506 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4507 BLOCK_FRAGMENT_CHAIN (new_block)
4508 = BLOCK_FRAGMENT_CHAIN (origin);
4509 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4510
4511 NOTE_BLOCK (insn) = new_block;
4512 block = new_block;
4513 }
4514
4515 if (prev_beg == current_block && prev_beg)
4516 BLOCK_SAME_RANGE (block) = 1;
4517
4518 prev_beg = origin;
4519
4520 BLOCK_SUBBLOCKS (block) = 0;
4521 TREE_ASM_WRITTEN (block) = 1;
4522 /* When there's only one block for the entire function,
4523 current_block == block and we mustn't do this, it
4524 will cause infinite recursion. */
4525 if (block != current_block)
4526 {
4527 tree super;
4528 if (block != origin)
4529 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block
4530 || BLOCK_FRAGMENT_ORIGIN (BLOCK_SUPERCONTEXT
4531 (origin))
4532 == current_block);
4533 if (p_block_stack->is_empty ())
4534 super = current_block;
4535 else
4536 {
4537 super = p_block_stack->last ();
4538 gcc_assert (super == current_block
4539 || BLOCK_FRAGMENT_ORIGIN (super)
4540 == current_block);
4541 }
4542 BLOCK_SUPERCONTEXT (block) = super;
4543 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4544 BLOCK_SUBBLOCKS (current_block) = block;
4545 current_block = origin;
4546 }
4547 p_block_stack->safe_push (block);
4548 }
4549 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4550 {
4551 NOTE_BLOCK (insn) = p_block_stack->pop ();
4552 current_block = BLOCK_SUPERCONTEXT (current_block);
4553 if (BLOCK_FRAGMENT_ORIGIN (current_block))
4554 current_block = BLOCK_FRAGMENT_ORIGIN (current_block);
4555 prev_beg = NULL_TREE;
4556 prev_end = BLOCK_SAME_RANGE (NOTE_BLOCK (insn))
4557 ? NOTE_BLOCK (insn) : NULL_TREE;
4558 }
4559 }
4560 else
4561 {
4562 prev_beg = NULL_TREE;
4563 if (prev_end)
4564 BLOCK_SAME_RANGE (prev_end) = 0;
4565 prev_end = NULL_TREE;
4566 }
4567 }
4568 }
4569
4570 /* Reverse the order of elements in the chain T of blocks,
4571 and return the new head of the chain (old last element). */
4572
4573 tree
4574 blocks_nreverse (tree t)
4575 {
4576 tree prev = 0, block, next;
4577 for (block = t; block; block = next)
4578 {
4579 next = BLOCK_CHAIN (block);
4580 BLOCK_CHAIN (block) = prev;
4581 prev = block;
4582 }
4583 return prev;
4584 }
4585
4586 /* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4587 by modifying the last node in chain 1 to point to chain 2. */
4588
4589 tree
4590 block_chainon (tree op1, tree op2)
4591 {
4592 tree t1;
4593
4594 if (!op1)
4595 return op2;
4596 if (!op2)
4597 return op1;
4598
4599 for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4600 continue;
4601 BLOCK_CHAIN (t1) = op2;
4602
4603 #ifdef ENABLE_TREE_CHECKING
4604 {
4605 tree t2;
4606 for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4607 gcc_assert (t2 != t1);
4608 }
4609 #endif
4610
4611 return op1;
4612 }
4613
4614 /* Count the subblocks of the list starting with BLOCK. If VECTOR is
4615 non-NULL, list them all into VECTOR, in a depth-first preorder
4616 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
4617 blocks. */
4618
4619 static int
4620 all_blocks (tree block, tree *vector)
4621 {
4622 int n_blocks = 0;
4623
4624 while (block)
4625 {
4626 TREE_ASM_WRITTEN (block) = 0;
4627
4628 /* Record this block. */
4629 if (vector)
4630 vector[n_blocks] = block;
4631
4632 ++n_blocks;
4633
4634 /* Record the subblocks, and their subblocks... */
4635 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4636 vector ? vector + n_blocks : 0);
4637 block = BLOCK_CHAIN (block);
4638 }
4639
4640 return n_blocks;
4641 }
4642
4643 /* Return a vector containing all the blocks rooted at BLOCK. The
4644 number of elements in the vector is stored in N_BLOCKS_P. The
4645 vector is dynamically allocated; it is the caller's responsibility
4646 to call `free' on the pointer returned. */
4647
4648 static tree *
4649 get_block_vector (tree block, int *n_blocks_p)
4650 {
4651 tree *block_vector;
4652
4653 *n_blocks_p = all_blocks (block, NULL);
4654 block_vector = XNEWVEC (tree, *n_blocks_p);
4655 all_blocks (block, block_vector);
4656
4657 return block_vector;
4658 }
4659
4660 static GTY(()) int next_block_index = 2;
4661
4662 /* Set BLOCK_NUMBER for all the blocks in FN. */
4663
4664 void
4665 number_blocks (tree fn)
4666 {
4667 int i;
4668 int n_blocks;
4669 tree *block_vector;
4670
4671 /* For SDB and XCOFF debugging output, we start numbering the blocks
4672 from 1 within each function, rather than keeping a running
4673 count. */
4674 #if defined (SDB_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
4675 if (write_symbols == SDB_DEBUG || write_symbols == XCOFF_DEBUG)
4676 next_block_index = 1;
4677 #endif
4678
4679 block_vector = get_block_vector (DECL_INITIAL (fn), &n_blocks);
4680
4681 /* The top-level BLOCK isn't numbered at all. */
4682 for (i = 1; i < n_blocks; ++i)
4683 /* We number the blocks from two. */
4684 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4685
4686 free (block_vector);
4687
4688 return;
4689 }
4690
4691 /* If VAR is present in a subblock of BLOCK, return the subblock. */
4692
4693 DEBUG_FUNCTION tree
4694 debug_find_var_in_block_tree (tree var, tree block)
4695 {
4696 tree t;
4697
4698 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4699 if (t == var)
4700 return block;
4701
4702 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4703 {
4704 tree ret = debug_find_var_in_block_tree (var, t);
4705 if (ret)
4706 return ret;
4707 }
4708
4709 return NULL_TREE;
4710 }
4711 \f
4712 /* Keep track of whether we're in a dummy function context. If we are,
4713 we don't want to invoke the set_current_function hook, because we'll
4714 get into trouble if the hook calls target_reinit () recursively or
4715 when the initial initialization is not yet complete. */
4716
4717 static bool in_dummy_function;
4718
4719 /* Invoke the target hook when setting cfun. Update the optimization options
4720 if the function uses different options than the default. */
4721
4722 static void
4723 invoke_set_current_function_hook (tree fndecl)
4724 {
4725 if (!in_dummy_function)
4726 {
4727 tree opts = ((fndecl)
4728 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4729 : optimization_default_node);
4730
4731 if (!opts)
4732 opts = optimization_default_node;
4733
4734 /* Change optimization options if needed. */
4735 if (optimization_current_node != opts)
4736 {
4737 optimization_current_node = opts;
4738 cl_optimization_restore (&global_options, TREE_OPTIMIZATION (opts));
4739 }
4740
4741 targetm.set_current_function (fndecl);
4742 this_fn_optabs = this_target_optabs;
4743
4744 if (opts != optimization_default_node)
4745 {
4746 init_tree_optimization_optabs (opts);
4747 if (TREE_OPTIMIZATION_OPTABS (opts))
4748 this_fn_optabs = (struct target_optabs *)
4749 TREE_OPTIMIZATION_OPTABS (opts);
4750 }
4751 }
4752 }
4753
4754 /* cfun should never be set directly; use this function. */
4755
4756 void
4757 set_cfun (struct function *new_cfun)
4758 {
4759 if (cfun != new_cfun)
4760 {
4761 cfun = new_cfun;
4762 invoke_set_current_function_hook (new_cfun ? new_cfun->decl : NULL_TREE);
4763 }
4764 }
4765
4766 /* Initialized with NOGC, making this poisonous to the garbage collector. */
4767
4768 static vec<function *> cfun_stack;
4769
4770 /* Push the current cfun onto the stack, and set cfun to new_cfun. Also set
4771 current_function_decl accordingly. */
4772
4773 void
4774 push_cfun (struct function *new_cfun)
4775 {
4776 gcc_assert ((!cfun && !current_function_decl)
4777 || (cfun && current_function_decl == cfun->decl));
4778 cfun_stack.safe_push (cfun);
4779 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4780 set_cfun (new_cfun);
4781 }
4782
4783 /* Pop cfun from the stack. Also set current_function_decl accordingly. */
4784
4785 void
4786 pop_cfun (void)
4787 {
4788 struct function *new_cfun = cfun_stack.pop ();
4789 /* When in_dummy_function, we do have a cfun but current_function_decl is
4790 NULL. We also allow pushing NULL cfun and subsequently changing
4791 current_function_decl to something else and have both restored by
4792 pop_cfun. */
4793 gcc_checking_assert (in_dummy_function
4794 || !cfun
4795 || current_function_decl == cfun->decl);
4796 set_cfun (new_cfun);
4797 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4798 }
4799
4800 /* Return value of funcdef and increase it. */
4801 int
4802 get_next_funcdef_no (void)
4803 {
4804 return funcdef_no++;
4805 }
4806
4807 /* Return value of funcdef. */
4808 int
4809 get_last_funcdef_no (void)
4810 {
4811 return funcdef_no;
4812 }
4813
4814 /* Allocate a function structure for FNDECL and set its contents
4815 to the defaults. Set cfun to the newly-allocated object.
4816 Some of the helper functions invoked during initialization assume
4817 that cfun has already been set. Therefore, assign the new object
4818 directly into cfun and invoke the back end hook explicitly at the
4819 very end, rather than initializing a temporary and calling set_cfun
4820 on it.
4821
4822 ABSTRACT_P is true if this is a function that will never be seen by
4823 the middle-end. Such functions are front-end concepts (like C++
4824 function templates) that do not correspond directly to functions
4825 placed in object files. */
4826
4827 void
4828 allocate_struct_function (tree fndecl, bool abstract_p)
4829 {
4830 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4831
4832 cfun = ggc_cleared_alloc<function> ();
4833
4834 init_eh_for_function ();
4835
4836 if (init_machine_status)
4837 cfun->machine = (*init_machine_status) ();
4838
4839 #ifdef OVERRIDE_ABI_FORMAT
4840 OVERRIDE_ABI_FORMAT (fndecl);
4841 #endif
4842
4843 if (fndecl != NULL_TREE)
4844 {
4845 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4846 cfun->decl = fndecl;
4847 current_function_funcdef_no = get_next_funcdef_no ();
4848 }
4849
4850 invoke_set_current_function_hook (fndecl);
4851
4852 if (fndecl != NULL_TREE)
4853 {
4854 tree result = DECL_RESULT (fndecl);
4855
4856 if (!abstract_p)
4857 {
4858 /* Now that we have activated any function-specific attributes
4859 that might affect layout, particularly vector modes, relayout
4860 each of the parameters and the result. */
4861 relayout_decl (result);
4862 for (tree parm = DECL_ARGUMENTS (fndecl); parm;
4863 parm = DECL_CHAIN (parm))
4864 relayout_decl (parm);
4865
4866 /* Similarly relayout the function decl. */
4867 targetm.target_option.relayout_function (fndecl);
4868 }
4869
4870 if (!abstract_p && aggregate_value_p (result, fndecl))
4871 {
4872 #ifdef PCC_STATIC_STRUCT_RETURN
4873 cfun->returns_pcc_struct = 1;
4874 #endif
4875 cfun->returns_struct = 1;
4876 }
4877
4878 cfun->stdarg = stdarg_p (fntype);
4879
4880 /* Assume all registers in stdarg functions need to be saved. */
4881 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4882 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4883
4884 /* ??? This could be set on a per-function basis by the front-end
4885 but is this worth the hassle? */
4886 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4887 cfun->can_delete_dead_exceptions = flag_delete_dead_exceptions;
4888
4889 if (!profile_flag && !flag_instrument_function_entry_exit)
4890 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl) = 1;
4891 }
4892 }
4893
4894 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4895 instead of just setting it. */
4896
4897 void
4898 push_struct_function (tree fndecl)
4899 {
4900 /* When in_dummy_function we might be in the middle of a pop_cfun and
4901 current_function_decl and cfun may not match. */
4902 gcc_assert (in_dummy_function
4903 || (!cfun && !current_function_decl)
4904 || (cfun && current_function_decl == cfun->decl));
4905 cfun_stack.safe_push (cfun);
4906 current_function_decl = fndecl;
4907 allocate_struct_function (fndecl, false);
4908 }
4909
4910 /* Reset crtl and other non-struct-function variables to defaults as
4911 appropriate for emitting rtl at the start of a function. */
4912
4913 static void
4914 prepare_function_start (void)
4915 {
4916 gcc_assert (!get_last_insn ());
4917 init_temp_slots ();
4918 init_emit ();
4919 init_varasm_status ();
4920 init_expr ();
4921 default_rtl_profile ();
4922
4923 if (flag_stack_usage_info)
4924 {
4925 cfun->su = ggc_cleared_alloc<stack_usage> ();
4926 cfun->su->static_stack_size = -1;
4927 }
4928
4929 cse_not_expected = ! optimize;
4930
4931 /* Caller save not needed yet. */
4932 caller_save_needed = 0;
4933
4934 /* We haven't done register allocation yet. */
4935 reg_renumber = 0;
4936
4937 /* Indicate that we have not instantiated virtual registers yet. */
4938 virtuals_instantiated = 0;
4939
4940 /* Indicate that we want CONCATs now. */
4941 generating_concat_p = 1;
4942
4943 /* Indicate we have no need of a frame pointer yet. */
4944 frame_pointer_needed = 0;
4945 }
4946
4947 void
4948 push_dummy_function (bool with_decl)
4949 {
4950 tree fn_decl, fn_type, fn_result_decl;
4951
4952 gcc_assert (!in_dummy_function);
4953 in_dummy_function = true;
4954
4955 if (with_decl)
4956 {
4957 fn_type = build_function_type_list (void_type_node, NULL_TREE);
4958 fn_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
4959 fn_type);
4960 fn_result_decl = build_decl (UNKNOWN_LOCATION, RESULT_DECL,
4961 NULL_TREE, void_type_node);
4962 DECL_RESULT (fn_decl) = fn_result_decl;
4963 }
4964 else
4965 fn_decl = NULL_TREE;
4966
4967 push_struct_function (fn_decl);
4968 }
4969
4970 /* Initialize the rtl expansion mechanism so that we can do simple things
4971 like generate sequences. This is used to provide a context during global
4972 initialization of some passes. You must call expand_dummy_function_end
4973 to exit this context. */
4974
4975 void
4976 init_dummy_function_start (void)
4977 {
4978 push_dummy_function (false);
4979 prepare_function_start ();
4980 }
4981
4982 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4983 and initialize static variables for generating RTL for the statements
4984 of the function. */
4985
4986 void
4987 init_function_start (tree subr)
4988 {
4989 /* Initialize backend, if needed. */
4990 initialize_rtl ();
4991
4992 prepare_function_start ();
4993 decide_function_section (subr);
4994
4995 /* Warn if this value is an aggregate type,
4996 regardless of which calling convention we are using for it. */
4997 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4998 warning (OPT_Waggregate_return, "function returns an aggregate");
4999 }
5000
5001 /* Expand code to verify the stack_protect_guard. This is invoked at
5002 the end of a function to be protected. */
5003
5004 void
5005 stack_protect_epilogue (void)
5006 {
5007 tree guard_decl = targetm.stack_protect_guard ();
5008 rtx_code_label *label = gen_label_rtx ();
5009 rtx x, y;
5010 rtx_insn *seq;
5011
5012 x = expand_normal (crtl->stack_protect_guard);
5013 y = expand_normal (guard_decl);
5014
5015 /* Allow the target to compare Y with X without leaking either into
5016 a register. */
5017 if (targetm.have_stack_protect_test ()
5018 && ((seq = targetm.gen_stack_protect_test (x, y, label)) != NULL_RTX))
5019 emit_insn (seq);
5020 else
5021 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
5022
5023 /* The noreturn predictor has been moved to the tree level. The rtl-level
5024 predictors estimate this branch about 20%, which isn't enough to get
5025 things moved out of line. Since this is the only extant case of adding
5026 a noreturn function at the rtl level, it doesn't seem worth doing ought
5027 except adding the prediction by hand. */
5028 rtx_insn *tmp = get_last_insn ();
5029 if (JUMP_P (tmp))
5030 predict_insn_def (tmp, PRED_NORETURN, TAKEN);
5031
5032 expand_call (targetm.stack_protect_fail (), NULL_RTX, /*ignore=*/true);
5033 free_temp_slots ();
5034 emit_label (label);
5035 }
5036 \f
5037 /* Start the RTL for a new function, and set variables used for
5038 emitting RTL.
5039 SUBR is the FUNCTION_DECL node.
5040 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
5041 the function's parameters, which must be run at any return statement. */
5042
5043 void
5044 expand_function_start (tree subr)
5045 {
5046 /* Make sure volatile mem refs aren't considered
5047 valid operands of arithmetic insns. */
5048 init_recog_no_volatile ();
5049
5050 crtl->profile
5051 = (profile_flag
5052 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
5053
5054 crtl->limit_stack
5055 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
5056
5057 /* Make the label for return statements to jump to. Do not special
5058 case machines with special return instructions -- they will be
5059 handled later during jump, ifcvt, or epilogue creation. */
5060 return_label = gen_label_rtx ();
5061
5062 /* Initialize rtx used to return the value. */
5063 /* Do this before assign_parms so that we copy the struct value address
5064 before any library calls that assign parms might generate. */
5065
5066 /* Decide whether to return the value in memory or in a register. */
5067 tree res = DECL_RESULT (subr);
5068 if (aggregate_value_p (res, subr))
5069 {
5070 /* Returning something that won't go in a register. */
5071 rtx value_address = 0;
5072
5073 #ifdef PCC_STATIC_STRUCT_RETURN
5074 if (cfun->returns_pcc_struct)
5075 {
5076 int size = int_size_in_bytes (TREE_TYPE (res));
5077 value_address = assemble_static_space (size);
5078 }
5079 else
5080 #endif
5081 {
5082 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
5083 /* Expect to be passed the address of a place to store the value.
5084 If it is passed as an argument, assign_parms will take care of
5085 it. */
5086 if (sv)
5087 {
5088 value_address = gen_reg_rtx (Pmode);
5089 emit_move_insn (value_address, sv);
5090 }
5091 }
5092 if (value_address)
5093 {
5094 rtx x = value_address;
5095 if (!DECL_BY_REFERENCE (res))
5096 {
5097 x = gen_rtx_MEM (DECL_MODE (res), x);
5098 set_mem_attributes (x, res, 1);
5099 }
5100 set_parm_rtl (res, x);
5101 }
5102 }
5103 else if (DECL_MODE (res) == VOIDmode)
5104 /* If return mode is void, this decl rtl should not be used. */
5105 set_parm_rtl (res, NULL_RTX);
5106 else
5107 {
5108 /* Compute the return values into a pseudo reg, which we will copy
5109 into the true return register after the cleanups are done. */
5110 tree return_type = TREE_TYPE (res);
5111 /* If we may coalesce this result, make sure it has the expected
5112 mode. */
5113 if (flag_tree_coalesce_vars && is_gimple_reg (res))
5114 {
5115 tree def = ssa_default_def (cfun, res);
5116 gcc_assert (def);
5117 machine_mode mode = promote_ssa_mode (def, NULL);
5118 set_parm_rtl (res, gen_reg_rtx (mode));
5119 }
5120 else if (TYPE_MODE (return_type) != BLKmode
5121 && targetm.calls.return_in_msb (return_type))
5122 /* expand_function_end will insert the appropriate padding in
5123 this case. Use the return value's natural (unpadded) mode
5124 within the function proper. */
5125 set_parm_rtl (res, gen_reg_rtx (TYPE_MODE (return_type)));
5126 else
5127 {
5128 /* In order to figure out what mode to use for the pseudo, we
5129 figure out what the mode of the eventual return register will
5130 actually be, and use that. */
5131 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
5132
5133 /* Structures that are returned in registers are not
5134 aggregate_value_p, so we may see a PARALLEL or a REG. */
5135 if (REG_P (hard_reg))
5136 set_parm_rtl (res, gen_reg_rtx (GET_MODE (hard_reg)));
5137 else
5138 {
5139 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
5140 set_parm_rtl (res, gen_group_rtx (hard_reg));
5141 }
5142 }
5143
5144 /* Set DECL_REGISTER flag so that expand_function_end will copy the
5145 result to the real return register(s). */
5146 DECL_REGISTER (res) = 1;
5147
5148 if (chkp_function_instrumented_p (current_function_decl))
5149 {
5150 tree return_type = TREE_TYPE (res);
5151 rtx bounds = targetm.calls.chkp_function_value_bounds (return_type,
5152 subr, 1);
5153 SET_DECL_BOUNDS_RTL (res, bounds);
5154 }
5155 }
5156
5157 /* Initialize rtx for parameters and local variables.
5158 In some cases this requires emitting insns. */
5159 assign_parms (subr);
5160
5161 /* If function gets a static chain arg, store it. */
5162 if (cfun->static_chain_decl)
5163 {
5164 tree parm = cfun->static_chain_decl;
5165 rtx local, chain;
5166 rtx_insn *insn;
5167 int unsignedp;
5168
5169 local = gen_reg_rtx (promote_decl_mode (parm, &unsignedp));
5170 chain = targetm.calls.static_chain (current_function_decl, true);
5171
5172 set_decl_incoming_rtl (parm, chain, false);
5173 set_parm_rtl (parm, local);
5174 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
5175
5176 if (GET_MODE (local) != GET_MODE (chain))
5177 {
5178 convert_move (local, chain, unsignedp);
5179 insn = get_last_insn ();
5180 }
5181 else
5182 insn = emit_move_insn (local, chain);
5183
5184 /* Mark the register as eliminable, similar to parameters. */
5185 if (MEM_P (chain)
5186 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
5187 set_dst_reg_note (insn, REG_EQUIV, chain, local);
5188
5189 /* If we aren't optimizing, save the static chain onto the stack. */
5190 if (!optimize)
5191 {
5192 tree saved_static_chain_decl
5193 = build_decl (DECL_SOURCE_LOCATION (parm), VAR_DECL,
5194 DECL_NAME (parm), TREE_TYPE (parm));
5195 rtx saved_static_chain_rtx
5196 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5197 SET_DECL_RTL (saved_static_chain_decl, saved_static_chain_rtx);
5198 emit_move_insn (saved_static_chain_rtx, chain);
5199 SET_DECL_VALUE_EXPR (parm, saved_static_chain_decl);
5200 DECL_HAS_VALUE_EXPR_P (parm) = 1;
5201 }
5202 }
5203
5204 /* If the function receives a non-local goto, then store the
5205 bits we need to restore the frame pointer. */
5206 if (cfun->nonlocal_goto_save_area)
5207 {
5208 tree t_save;
5209 rtx r_save;
5210
5211 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
5212 gcc_assert (DECL_RTL_SET_P (var));
5213
5214 t_save = build4 (ARRAY_REF,
5215 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
5216 cfun->nonlocal_goto_save_area,
5217 integer_zero_node, NULL_TREE, NULL_TREE);
5218 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
5219 gcc_assert (GET_MODE (r_save) == Pmode);
5220
5221 emit_move_insn (r_save, targetm.builtin_setjmp_frame_value ());
5222 update_nonlocal_goto_save_area ();
5223 }
5224
5225 /* The following was moved from init_function_start.
5226 The move is supposed to make sdb output more accurate. */
5227 /* Indicate the beginning of the function body,
5228 as opposed to parm setup. */
5229 emit_note (NOTE_INSN_FUNCTION_BEG);
5230
5231 gcc_assert (NOTE_P (get_last_insn ()));
5232
5233 parm_birth_insn = get_last_insn ();
5234
5235 if (crtl->profile)
5236 {
5237 #ifdef PROFILE_HOOK
5238 PROFILE_HOOK (current_function_funcdef_no);
5239 #endif
5240 }
5241
5242 /* If we are doing generic stack checking, the probe should go here. */
5243 if (flag_stack_check == GENERIC_STACK_CHECK)
5244 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
5245 }
5246 \f
5247 void
5248 pop_dummy_function (void)
5249 {
5250 pop_cfun ();
5251 in_dummy_function = false;
5252 }
5253
5254 /* Undo the effects of init_dummy_function_start. */
5255 void
5256 expand_dummy_function_end (void)
5257 {
5258 gcc_assert (in_dummy_function);
5259
5260 /* End any sequences that failed to be closed due to syntax errors. */
5261 while (in_sequence_p ())
5262 end_sequence ();
5263
5264 /* Outside function body, can't compute type's actual size
5265 until next function's body starts. */
5266
5267 free_after_parsing (cfun);
5268 free_after_compilation (cfun);
5269 pop_dummy_function ();
5270 }
5271
5272 /* Helper for diddle_return_value. */
5273
5274 void
5275 diddle_return_value_1 (void (*doit) (rtx, void *), void *arg, rtx outgoing)
5276 {
5277 if (! outgoing)
5278 return;
5279
5280 if (REG_P (outgoing))
5281 (*doit) (outgoing, arg);
5282 else if (GET_CODE (outgoing) == PARALLEL)
5283 {
5284 int i;
5285
5286 for (i = 0; i < XVECLEN (outgoing, 0); i++)
5287 {
5288 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
5289
5290 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
5291 (*doit) (x, arg);
5292 }
5293 }
5294 }
5295
5296 /* Call DOIT for each hard register used as a return value from
5297 the current function. */
5298
5299 void
5300 diddle_return_value (void (*doit) (rtx, void *), void *arg)
5301 {
5302 diddle_return_value_1 (doit, arg, crtl->return_bnd);
5303 diddle_return_value_1 (doit, arg, crtl->return_rtx);
5304 }
5305
5306 static void
5307 do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5308 {
5309 emit_clobber (reg);
5310 }
5311
5312 void
5313 clobber_return_register (void)
5314 {
5315 diddle_return_value (do_clobber_return_reg, NULL);
5316
5317 /* In case we do use pseudo to return value, clobber it too. */
5318 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5319 {
5320 tree decl_result = DECL_RESULT (current_function_decl);
5321 rtx decl_rtl = DECL_RTL (decl_result);
5322 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
5323 {
5324 do_clobber_return_reg (decl_rtl, NULL);
5325 }
5326 }
5327 }
5328
5329 static void
5330 do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5331 {
5332 emit_use (reg);
5333 }
5334
5335 static void
5336 use_return_register (void)
5337 {
5338 diddle_return_value (do_use_return_reg, NULL);
5339 }
5340
5341 /* Set the location of the insn chain starting at INSN to LOC. */
5342
5343 static void
5344 set_insn_locations (rtx_insn *insn, int loc)
5345 {
5346 while (insn != NULL)
5347 {
5348 if (INSN_P (insn))
5349 INSN_LOCATION (insn) = loc;
5350 insn = NEXT_INSN (insn);
5351 }
5352 }
5353
5354 /* Generate RTL for the end of the current function. */
5355
5356 void
5357 expand_function_end (void)
5358 {
5359 /* If arg_pointer_save_area was referenced only from a nested
5360 function, we will not have initialized it yet. Do that now. */
5361 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
5362 get_arg_pointer_save_area ();
5363
5364 /* If we are doing generic stack checking and this function makes calls,
5365 do a stack probe at the start of the function to ensure we have enough
5366 space for another stack frame. */
5367 if (flag_stack_check == GENERIC_STACK_CHECK)
5368 {
5369 rtx_insn *insn, *seq;
5370
5371 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5372 if (CALL_P (insn))
5373 {
5374 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
5375 start_sequence ();
5376 if (STACK_CHECK_MOVING_SP)
5377 anti_adjust_stack_and_probe (max_frame_size, true);
5378 else
5379 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
5380 seq = get_insns ();
5381 end_sequence ();
5382 set_insn_locations (seq, prologue_location);
5383 emit_insn_before (seq, stack_check_probe_note);
5384 break;
5385 }
5386 }
5387
5388 /* End any sequences that failed to be closed due to syntax errors. */
5389 while (in_sequence_p ())
5390 end_sequence ();
5391
5392 clear_pending_stack_adjust ();
5393 do_pending_stack_adjust ();
5394
5395 /* Output a linenumber for the end of the function.
5396 SDB depends on this. */
5397 set_curr_insn_location (input_location);
5398
5399 /* Before the return label (if any), clobber the return
5400 registers so that they are not propagated live to the rest of
5401 the function. This can only happen with functions that drop
5402 through; if there had been a return statement, there would
5403 have either been a return rtx, or a jump to the return label.
5404
5405 We delay actual code generation after the current_function_value_rtx
5406 is computed. */
5407 rtx_insn *clobber_after = get_last_insn ();
5408
5409 /* Output the label for the actual return from the function. */
5410 emit_label (return_label);
5411
5412 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
5413 {
5414 /* Let except.c know where it should emit the call to unregister
5415 the function context for sjlj exceptions. */
5416 if (flag_exceptions)
5417 sjlj_emit_function_exit_after (get_last_insn ());
5418 }
5419 else
5420 {
5421 /* We want to ensure that instructions that may trap are not
5422 moved into the epilogue by scheduling, because we don't
5423 always emit unwind information for the epilogue. */
5424 if (cfun->can_throw_non_call_exceptions)
5425 emit_insn (gen_blockage ());
5426 }
5427
5428 /* If this is an implementation of throw, do what's necessary to
5429 communicate between __builtin_eh_return and the epilogue. */
5430 expand_eh_return ();
5431
5432 /* If scalar return value was computed in a pseudo-reg, or was a named
5433 return value that got dumped to the stack, copy that to the hard
5434 return register. */
5435 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5436 {
5437 tree decl_result = DECL_RESULT (current_function_decl);
5438 rtx decl_rtl = DECL_RTL (decl_result);
5439
5440 if (REG_P (decl_rtl)
5441 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5442 : DECL_REGISTER (decl_result))
5443 {
5444 rtx real_decl_rtl = crtl->return_rtx;
5445
5446 /* This should be set in assign_parms. */
5447 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5448
5449 /* If this is a BLKmode structure being returned in registers,
5450 then use the mode computed in expand_return. Note that if
5451 decl_rtl is memory, then its mode may have been changed,
5452 but that crtl->return_rtx has not. */
5453 if (GET_MODE (real_decl_rtl) == BLKmode)
5454 PUT_MODE (real_decl_rtl, GET_MODE (decl_rtl));
5455
5456 /* If a non-BLKmode return value should be padded at the least
5457 significant end of the register, shift it left by the appropriate
5458 amount. BLKmode results are handled using the group load/store
5459 machinery. */
5460 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5461 && REG_P (real_decl_rtl)
5462 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5463 {
5464 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5465 REGNO (real_decl_rtl)),
5466 decl_rtl);
5467 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5468 }
5469 else if (GET_CODE (real_decl_rtl) == PARALLEL)
5470 {
5471 /* If expand_function_start has created a PARALLEL for decl_rtl,
5472 move the result to the real return registers. Otherwise, do
5473 a group load from decl_rtl for a named return. */
5474 if (GET_CODE (decl_rtl) == PARALLEL)
5475 emit_group_move (real_decl_rtl, decl_rtl);
5476 else
5477 emit_group_load (real_decl_rtl, decl_rtl,
5478 TREE_TYPE (decl_result),
5479 int_size_in_bytes (TREE_TYPE (decl_result)));
5480 }
5481 /* In the case of complex integer modes smaller than a word, we'll
5482 need to generate some non-trivial bitfield insertions. Do that
5483 on a pseudo and not the hard register. */
5484 else if (GET_CODE (decl_rtl) == CONCAT
5485 && GET_MODE_CLASS (GET_MODE (decl_rtl)) == MODE_COMPLEX_INT
5486 && GET_MODE_BITSIZE (GET_MODE (decl_rtl)) <= BITS_PER_WORD)
5487 {
5488 int old_generating_concat_p;
5489 rtx tmp;
5490
5491 old_generating_concat_p = generating_concat_p;
5492 generating_concat_p = 0;
5493 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5494 generating_concat_p = old_generating_concat_p;
5495
5496 emit_move_insn (tmp, decl_rtl);
5497 emit_move_insn (real_decl_rtl, tmp);
5498 }
5499 /* If a named return value dumped decl_return to memory, then
5500 we may need to re-do the PROMOTE_MODE signed/unsigned
5501 extension. */
5502 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5503 {
5504 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5505 promote_function_mode (TREE_TYPE (decl_result),
5506 GET_MODE (decl_rtl), &unsignedp,
5507 TREE_TYPE (current_function_decl), 1);
5508
5509 convert_move (real_decl_rtl, decl_rtl, unsignedp);
5510 }
5511 else
5512 emit_move_insn (real_decl_rtl, decl_rtl);
5513 }
5514 }
5515
5516 /* If returning a structure, arrange to return the address of the value
5517 in a place where debuggers expect to find it.
5518
5519 If returning a structure PCC style,
5520 the caller also depends on this value.
5521 And cfun->returns_pcc_struct is not necessarily set. */
5522 if ((cfun->returns_struct || cfun->returns_pcc_struct)
5523 && !targetm.calls.omit_struct_return_reg)
5524 {
5525 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5526 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5527 rtx outgoing;
5528
5529 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5530 type = TREE_TYPE (type);
5531 else
5532 value_address = XEXP (value_address, 0);
5533
5534 outgoing = targetm.calls.function_value (build_pointer_type (type),
5535 current_function_decl, true);
5536
5537 /* Mark this as a function return value so integrate will delete the
5538 assignment and USE below when inlining this function. */
5539 REG_FUNCTION_VALUE_P (outgoing) = 1;
5540
5541 /* The address may be ptr_mode and OUTGOING may be Pmode. */
5542 value_address = convert_memory_address (GET_MODE (outgoing),
5543 value_address);
5544
5545 emit_move_insn (outgoing, value_address);
5546
5547 /* Show return register used to hold result (in this case the address
5548 of the result. */
5549 crtl->return_rtx = outgoing;
5550 }
5551
5552 /* Emit the actual code to clobber return register. Don't emit
5553 it if clobber_after is a barrier, then the previous basic block
5554 certainly doesn't fall thru into the exit block. */
5555 if (!BARRIER_P (clobber_after))
5556 {
5557 start_sequence ();
5558 clobber_return_register ();
5559 rtx_insn *seq = get_insns ();
5560 end_sequence ();
5561
5562 emit_insn_after (seq, clobber_after);
5563 }
5564
5565 /* Output the label for the naked return from the function. */
5566 if (naked_return_label)
5567 emit_label (naked_return_label);
5568
5569 /* @@@ This is a kludge. We want to ensure that instructions that
5570 may trap are not moved into the epilogue by scheduling, because
5571 we don't always emit unwind information for the epilogue. */
5572 if (cfun->can_throw_non_call_exceptions
5573 && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5574 emit_insn (gen_blockage ());
5575
5576 /* If stack protection is enabled for this function, check the guard. */
5577 if (crtl->stack_protect_guard)
5578 stack_protect_epilogue ();
5579
5580 /* If we had calls to alloca, and this machine needs
5581 an accurate stack pointer to exit the function,
5582 insert some code to save and restore the stack pointer. */
5583 if (! EXIT_IGNORE_STACK
5584 && cfun->calls_alloca)
5585 {
5586 rtx tem = 0;
5587
5588 start_sequence ();
5589 emit_stack_save (SAVE_FUNCTION, &tem);
5590 rtx_insn *seq = get_insns ();
5591 end_sequence ();
5592 emit_insn_before (seq, parm_birth_insn);
5593
5594 emit_stack_restore (SAVE_FUNCTION, tem);
5595 }
5596
5597 /* ??? This should no longer be necessary since stupid is no longer with
5598 us, but there are some parts of the compiler (eg reload_combine, and
5599 sh mach_dep_reorg) that still try and compute their own lifetime info
5600 instead of using the general framework. */
5601 use_return_register ();
5602 }
5603
5604 rtx
5605 get_arg_pointer_save_area (void)
5606 {
5607 rtx ret = arg_pointer_save_area;
5608
5609 if (! ret)
5610 {
5611 ret = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5612 arg_pointer_save_area = ret;
5613 }
5614
5615 if (! crtl->arg_pointer_save_area_init)
5616 {
5617 /* Save the arg pointer at the beginning of the function. The
5618 generated stack slot may not be a valid memory address, so we
5619 have to check it and fix it if necessary. */
5620 start_sequence ();
5621 emit_move_insn (validize_mem (copy_rtx (ret)),
5622 crtl->args.internal_arg_pointer);
5623 rtx_insn *seq = get_insns ();
5624 end_sequence ();
5625
5626 push_topmost_sequence ();
5627 emit_insn_after (seq, entry_of_function ());
5628 pop_topmost_sequence ();
5629
5630 crtl->arg_pointer_save_area_init = true;
5631 }
5632
5633 return ret;
5634 }
5635 \f
5636 /* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5637 for the first time. */
5638
5639 static void
5640 record_insns (rtx_insn *insns, rtx end, hash_table<insn_cache_hasher> **hashp)
5641 {
5642 rtx_insn *tmp;
5643 hash_table<insn_cache_hasher> *hash = *hashp;
5644
5645 if (hash == NULL)
5646 *hashp = hash = hash_table<insn_cache_hasher>::create_ggc (17);
5647
5648 for (tmp = insns; tmp != end; tmp = NEXT_INSN (tmp))
5649 {
5650 rtx *slot = hash->find_slot (tmp, INSERT);
5651 gcc_assert (*slot == NULL);
5652 *slot = tmp;
5653 }
5654 }
5655
5656 /* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5657 basic block, splitting or peepholes. If INSN is a prologue or epilogue
5658 insn, then record COPY as well. */
5659
5660 void
5661 maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5662 {
5663 hash_table<insn_cache_hasher> *hash;
5664 rtx *slot;
5665
5666 hash = epilogue_insn_hash;
5667 if (!hash || !hash->find (insn))
5668 {
5669 hash = prologue_insn_hash;
5670 if (!hash || !hash->find (insn))
5671 return;
5672 }
5673
5674 slot = hash->find_slot (copy, INSERT);
5675 gcc_assert (*slot == NULL);
5676 *slot = copy;
5677 }
5678
5679 /* Determine if any INSNs in HASH are, or are part of, INSN. Because
5680 we can be running after reorg, SEQUENCE rtl is possible. */
5681
5682 static bool
5683 contains (const_rtx insn, hash_table<insn_cache_hasher> *hash)
5684 {
5685 if (hash == NULL)
5686 return false;
5687
5688 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5689 {
5690 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
5691 int i;
5692 for (i = seq->len () - 1; i >= 0; i--)
5693 if (hash->find (seq->element (i)))
5694 return true;
5695 return false;
5696 }
5697
5698 return hash->find (const_cast<rtx> (insn)) != NULL;
5699 }
5700
5701 int
5702 prologue_epilogue_contains (const_rtx insn)
5703 {
5704 if (contains (insn, prologue_insn_hash))
5705 return 1;
5706 if (contains (insn, epilogue_insn_hash))
5707 return 1;
5708 return 0;
5709 }
5710
5711 /* Insert use of return register before the end of BB. */
5712
5713 static void
5714 emit_use_return_register_into_block (basic_block bb)
5715 {
5716 start_sequence ();
5717 use_return_register ();
5718 rtx_insn *seq = get_insns ();
5719 end_sequence ();
5720 rtx_insn *insn = BB_END (bb);
5721 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
5722 insn = prev_cc0_setter (insn);
5723
5724 emit_insn_before (seq, insn);
5725 }
5726
5727
5728 /* Create a return pattern, either simple_return or return, depending on
5729 simple_p. */
5730
5731 static rtx_insn *
5732 gen_return_pattern (bool simple_p)
5733 {
5734 return (simple_p
5735 ? targetm.gen_simple_return ()
5736 : targetm.gen_return ());
5737 }
5738
5739 /* Insert an appropriate return pattern at the end of block BB. This
5740 also means updating block_for_insn appropriately. SIMPLE_P is
5741 the same as in gen_return_pattern and passed to it. */
5742
5743 void
5744 emit_return_into_block (bool simple_p, basic_block bb)
5745 {
5746 rtx_jump_insn *jump = emit_jump_insn_after (gen_return_pattern (simple_p),
5747 BB_END (bb));
5748 rtx pat = PATTERN (jump);
5749 if (GET_CODE (pat) == PARALLEL)
5750 pat = XVECEXP (pat, 0, 0);
5751 gcc_assert (ANY_RETURN_P (pat));
5752 JUMP_LABEL (jump) = pat;
5753 }
5754
5755 /* Set JUMP_LABEL for a return insn. */
5756
5757 void
5758 set_return_jump_label (rtx_insn *returnjump)
5759 {
5760 rtx pat = PATTERN (returnjump);
5761 if (GET_CODE (pat) == PARALLEL)
5762 pat = XVECEXP (pat, 0, 0);
5763 if (ANY_RETURN_P (pat))
5764 JUMP_LABEL (returnjump) = pat;
5765 else
5766 JUMP_LABEL (returnjump) = ret_rtx;
5767 }
5768
5769 /* Return true if there are any active insns between HEAD and TAIL. */
5770 bool
5771 active_insn_between (rtx_insn *head, rtx_insn *tail)
5772 {
5773 while (tail)
5774 {
5775 if (active_insn_p (tail))
5776 return true;
5777 if (tail == head)
5778 return false;
5779 tail = PREV_INSN (tail);
5780 }
5781 return false;
5782 }
5783
5784 /* LAST_BB is a block that exits, and empty of active instructions.
5785 Examine its predecessors for jumps that can be converted to
5786 (conditional) returns. */
5787 vec<edge>
5788 convert_jumps_to_returns (basic_block last_bb, bool simple_p,
5789 vec<edge> unconverted ATTRIBUTE_UNUSED)
5790 {
5791 int i;
5792 basic_block bb;
5793 edge_iterator ei;
5794 edge e;
5795 auto_vec<basic_block> src_bbs (EDGE_COUNT (last_bb->preds));
5796
5797 FOR_EACH_EDGE (e, ei, last_bb->preds)
5798 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
5799 src_bbs.quick_push (e->src);
5800
5801 rtx_insn *label = BB_HEAD (last_bb);
5802
5803 FOR_EACH_VEC_ELT (src_bbs, i, bb)
5804 {
5805 rtx_insn *jump = BB_END (bb);
5806
5807 if (!JUMP_P (jump) || JUMP_LABEL (jump) != label)
5808 continue;
5809
5810 e = find_edge (bb, last_bb);
5811
5812 /* If we have an unconditional jump, we can replace that
5813 with a simple return instruction. */
5814 if (simplejump_p (jump))
5815 {
5816 /* The use of the return register might be present in the exit
5817 fallthru block. Either:
5818 - removing the use is safe, and we should remove the use in
5819 the exit fallthru block, or
5820 - removing the use is not safe, and we should add it here.
5821 For now, we conservatively choose the latter. Either of the
5822 2 helps in crossjumping. */
5823 emit_use_return_register_into_block (bb);
5824
5825 emit_return_into_block (simple_p, bb);
5826 delete_insn (jump);
5827 }
5828
5829 /* If we have a conditional jump branching to the last
5830 block, we can try to replace that with a conditional
5831 return instruction. */
5832 else if (condjump_p (jump))
5833 {
5834 rtx dest;
5835
5836 if (simple_p)
5837 dest = simple_return_rtx;
5838 else
5839 dest = ret_rtx;
5840 if (!redirect_jump (as_a <rtx_jump_insn *> (jump), dest, 0))
5841 {
5842 if (targetm.have_simple_return () && simple_p)
5843 {
5844 if (dump_file)
5845 fprintf (dump_file,
5846 "Failed to redirect bb %d branch.\n", bb->index);
5847 unconverted.safe_push (e);
5848 }
5849 continue;
5850 }
5851
5852 /* See comment in simplejump_p case above. */
5853 emit_use_return_register_into_block (bb);
5854
5855 /* If this block has only one successor, it both jumps
5856 and falls through to the fallthru block, so we can't
5857 delete the edge. */
5858 if (single_succ_p (bb))
5859 continue;
5860 }
5861 else
5862 {
5863 if (targetm.have_simple_return () && simple_p)
5864 {
5865 if (dump_file)
5866 fprintf (dump_file,
5867 "Failed to redirect bb %d branch.\n", bb->index);
5868 unconverted.safe_push (e);
5869 }
5870 continue;
5871 }
5872
5873 /* Fix up the CFG for the successful change we just made. */
5874 redirect_edge_succ (e, EXIT_BLOCK_PTR_FOR_FN (cfun));
5875 e->flags &= ~EDGE_CROSSING;
5876 }
5877 src_bbs.release ();
5878 return unconverted;
5879 }
5880
5881 /* Emit a return insn for the exit fallthru block. */
5882 basic_block
5883 emit_return_for_exit (edge exit_fallthru_edge, bool simple_p)
5884 {
5885 basic_block last_bb = exit_fallthru_edge->src;
5886
5887 if (JUMP_P (BB_END (last_bb)))
5888 {
5889 last_bb = split_edge (exit_fallthru_edge);
5890 exit_fallthru_edge = single_succ_edge (last_bb);
5891 }
5892 emit_barrier_after (BB_END (last_bb));
5893 emit_return_into_block (simple_p, last_bb);
5894 exit_fallthru_edge->flags &= ~EDGE_FALLTHRU;
5895 return last_bb;
5896 }
5897
5898
5899 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
5900 this into place with notes indicating where the prologue ends and where
5901 the epilogue begins. Update the basic block information when possible.
5902
5903 Notes on epilogue placement:
5904 There are several kinds of edges to the exit block:
5905 * a single fallthru edge from LAST_BB
5906 * possibly, edges from blocks containing sibcalls
5907 * possibly, fake edges from infinite loops
5908
5909 The epilogue is always emitted on the fallthru edge from the last basic
5910 block in the function, LAST_BB, into the exit block.
5911
5912 If LAST_BB is empty except for a label, it is the target of every
5913 other basic block in the function that ends in a return. If a
5914 target has a return or simple_return pattern (possibly with
5915 conditional variants), these basic blocks can be changed so that a
5916 return insn is emitted into them, and their target is adjusted to
5917 the real exit block.
5918
5919 Notes on shrink wrapping: We implement a fairly conservative
5920 version of shrink-wrapping rather than the textbook one. We only
5921 generate a single prologue and a single epilogue. This is
5922 sufficient to catch a number of interesting cases involving early
5923 exits.
5924
5925 First, we identify the blocks that require the prologue to occur before
5926 them. These are the ones that modify a call-saved register, or reference
5927 any of the stack or frame pointer registers. To simplify things, we then
5928 mark everything reachable from these blocks as also requiring a prologue.
5929 This takes care of loops automatically, and avoids the need to examine
5930 whether MEMs reference the frame, since it is sufficient to check for
5931 occurrences of the stack or frame pointer.
5932
5933 We then compute the set of blocks for which the need for a prologue
5934 is anticipatable (borrowing terminology from the shrink-wrapping
5935 description in Muchnick's book). These are the blocks which either
5936 require a prologue themselves, or those that have only successors
5937 where the prologue is anticipatable. The prologue needs to be
5938 inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
5939 is not. For the moment, we ensure that only one such edge exists.
5940
5941 The epilogue is placed as described above, but we make a
5942 distinction between inserting return and simple_return patterns
5943 when modifying other blocks that end in a return. Blocks that end
5944 in a sibcall omit the sibcall_epilogue if the block is not in
5945 ANTIC. */
5946
5947 void
5948 thread_prologue_and_epilogue_insns (void)
5949 {
5950 bool inserted;
5951 vec<edge> unconverted_simple_returns = vNULL;
5952 bitmap_head bb_flags;
5953 rtx_insn *returnjump;
5954 rtx_insn *epilogue_end ATTRIBUTE_UNUSED;
5955 rtx_insn *prologue_seq ATTRIBUTE_UNUSED, *split_prologue_seq ATTRIBUTE_UNUSED;
5956 edge e, entry_edge, orig_entry_edge, exit_fallthru_edge;
5957 edge_iterator ei;
5958
5959 df_analyze ();
5960
5961 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5962
5963 inserted = false;
5964 epilogue_end = NULL;
5965 returnjump = NULL;
5966
5967 /* Can't deal with multiple successors of the entry block at the
5968 moment. Function should always have at least one entry
5969 point. */
5970 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5971 entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5972 orig_entry_edge = entry_edge;
5973
5974 split_prologue_seq = NULL;
5975 if (flag_split_stack
5976 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
5977 == NULL))
5978 {
5979 start_sequence ();
5980 emit_insn (targetm.gen_split_stack_prologue ());
5981 split_prologue_seq = get_insns ();
5982 end_sequence ();
5983
5984 record_insns (split_prologue_seq, NULL, &prologue_insn_hash);
5985 set_insn_locations (split_prologue_seq, prologue_location);
5986 }
5987
5988 prologue_seq = NULL;
5989 if (targetm.have_prologue ())
5990 {
5991 start_sequence ();
5992 rtx_insn *seq = targetm.gen_prologue ();
5993 emit_insn (seq);
5994
5995 /* Insert an explicit USE for the frame pointer
5996 if the profiling is on and the frame pointer is required. */
5997 if (crtl->profile && frame_pointer_needed)
5998 emit_use (hard_frame_pointer_rtx);
5999
6000 /* Retain a map of the prologue insns. */
6001 record_insns (seq, NULL, &prologue_insn_hash);
6002 emit_note (NOTE_INSN_PROLOGUE_END);
6003
6004 /* Ensure that instructions are not moved into the prologue when
6005 profiling is on. The call to the profiling routine can be
6006 emitted within the live range of a call-clobbered register. */
6007 if (!targetm.profile_before_prologue () && crtl->profile)
6008 emit_insn (gen_blockage ());
6009
6010 prologue_seq = get_insns ();
6011 end_sequence ();
6012 set_insn_locations (prologue_seq, prologue_location);
6013 }
6014
6015 bitmap_initialize (&bb_flags, &bitmap_default_obstack);
6016
6017 /* Try to perform a kind of shrink-wrapping, making sure the
6018 prologue/epilogue is emitted only around those parts of the
6019 function that require it. */
6020
6021 try_shrink_wrapping (&entry_edge, &bb_flags, prologue_seq);
6022
6023 if (split_prologue_seq != NULL_RTX)
6024 {
6025 insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
6026 inserted = true;
6027 }
6028 if (prologue_seq != NULL_RTX)
6029 {
6030 insert_insn_on_edge (prologue_seq, entry_edge);
6031 inserted = true;
6032 }
6033
6034 /* If the exit block has no non-fake predecessors, we don't need
6035 an epilogue. */
6036 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6037 if ((e->flags & EDGE_FAKE) == 0)
6038 break;
6039 if (e == NULL)
6040 goto epilogue_done;
6041
6042 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
6043
6044 exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
6045
6046 if (targetm.have_simple_return () && entry_edge != orig_entry_edge)
6047 exit_fallthru_edge
6048 = get_unconverted_simple_return (exit_fallthru_edge, bb_flags,
6049 &unconverted_simple_returns,
6050 &returnjump);
6051 if (targetm.have_return ())
6052 {
6053 if (exit_fallthru_edge == NULL)
6054 goto epilogue_done;
6055
6056 if (optimize)
6057 {
6058 basic_block last_bb = exit_fallthru_edge->src;
6059
6060 if (LABEL_P (BB_HEAD (last_bb))
6061 && !active_insn_between (BB_HEAD (last_bb), BB_END (last_bb)))
6062 convert_jumps_to_returns (last_bb, false, vNULL);
6063
6064 if (EDGE_COUNT (last_bb->preds) != 0
6065 && single_succ_p (last_bb))
6066 {
6067 last_bb = emit_return_for_exit (exit_fallthru_edge, false);
6068 epilogue_end = returnjump = BB_END (last_bb);
6069
6070 /* Emitting the return may add a basic block.
6071 Fix bb_flags for the added block. */
6072 if (targetm.have_simple_return ()
6073 && last_bb != exit_fallthru_edge->src)
6074 bitmap_set_bit (&bb_flags, last_bb->index);
6075
6076 goto epilogue_done;
6077 }
6078 }
6079 }
6080
6081 /* A small fib -- epilogue is not yet completed, but we wish to re-use
6082 this marker for the splits of EH_RETURN patterns, and nothing else
6083 uses the flag in the meantime. */
6084 epilogue_completed = 1;
6085
6086 /* Find non-fallthru edges that end with EH_RETURN instructions. On
6087 some targets, these get split to a special version of the epilogue
6088 code. In order to be able to properly annotate these with unwind
6089 info, try to split them now. If we get a valid split, drop an
6090 EPILOGUE_BEG note and mark the insns as epilogue insns. */
6091 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6092 {
6093 rtx_insn *prev, *last, *trial;
6094
6095 if (e->flags & EDGE_FALLTHRU)
6096 continue;
6097 last = BB_END (e->src);
6098 if (!eh_returnjump_p (last))
6099 continue;
6100
6101 prev = PREV_INSN (last);
6102 trial = try_split (PATTERN (last), last, 1);
6103 if (trial == last)
6104 continue;
6105
6106 record_insns (NEXT_INSN (prev), NEXT_INSN (trial), &epilogue_insn_hash);
6107 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
6108 }
6109
6110 /* If nothing falls through into the exit block, we don't need an
6111 epilogue. */
6112
6113 if (exit_fallthru_edge == NULL)
6114 goto epilogue_done;
6115
6116 if (targetm.have_epilogue ())
6117 {
6118 start_sequence ();
6119 epilogue_end = emit_note (NOTE_INSN_EPILOGUE_BEG);
6120 rtx_insn *seq = targetm.gen_epilogue ();
6121 if (seq)
6122 emit_jump_insn (seq);
6123
6124 /* Retain a map of the epilogue insns. */
6125 record_insns (seq, NULL, &epilogue_insn_hash);
6126 set_insn_locations (seq, epilogue_location);
6127
6128 seq = get_insns ();
6129 returnjump = get_last_insn ();
6130 end_sequence ();
6131
6132 insert_insn_on_edge (seq, exit_fallthru_edge);
6133 inserted = true;
6134
6135 if (JUMP_P (returnjump))
6136 set_return_jump_label (returnjump);
6137 }
6138 else
6139 {
6140 basic_block cur_bb;
6141
6142 if (! next_active_insn (BB_END (exit_fallthru_edge->src)))
6143 goto epilogue_done;
6144 /* We have a fall-through edge to the exit block, the source is not
6145 at the end of the function, and there will be an assembler epilogue
6146 at the end of the function.
6147 We can't use force_nonfallthru here, because that would try to
6148 use return. Inserting a jump 'by hand' is extremely messy, so
6149 we take advantage of cfg_layout_finalize using
6150 fixup_fallthru_exit_predecessor. */
6151 cfg_layout_initialize (0);
6152 FOR_EACH_BB_FN (cur_bb, cfun)
6153 if (cur_bb->index >= NUM_FIXED_BLOCKS
6154 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
6155 cur_bb->aux = cur_bb->next_bb;
6156 cfg_layout_finalize ();
6157 }
6158
6159 epilogue_done:
6160
6161 default_rtl_profile ();
6162
6163 if (inserted)
6164 {
6165 sbitmap blocks;
6166
6167 commit_edge_insertions ();
6168
6169 /* Look for basic blocks within the prologue insns. */
6170 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
6171 bitmap_clear (blocks);
6172 bitmap_set_bit (blocks, entry_edge->dest->index);
6173 bitmap_set_bit (blocks, orig_entry_edge->dest->index);
6174 find_many_sub_basic_blocks (blocks);
6175 sbitmap_free (blocks);
6176
6177 /* The epilogue insns we inserted may cause the exit edge to no longer
6178 be fallthru. */
6179 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6180 {
6181 if (((e->flags & EDGE_FALLTHRU) != 0)
6182 && returnjump_p (BB_END (e->src)))
6183 e->flags &= ~EDGE_FALLTHRU;
6184 }
6185 }
6186
6187 if (targetm.have_simple_return ())
6188 convert_to_simple_return (entry_edge, orig_entry_edge, bb_flags,
6189 returnjump, unconverted_simple_returns);
6190
6191 /* Emit sibling epilogues before any sibling call sites. */
6192 for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds); (e =
6193 ei_safe_edge (ei));
6194 )
6195 {
6196 basic_block bb = e->src;
6197 rtx_insn *insn = BB_END (bb);
6198
6199 if (!CALL_P (insn)
6200 || ! SIBLING_CALL_P (insn)
6201 || (targetm.have_simple_return ()
6202 && entry_edge != orig_entry_edge
6203 && !bitmap_bit_p (&bb_flags, bb->index)))
6204 {
6205 ei_next (&ei);
6206 continue;
6207 }
6208
6209 if (rtx_insn *ep_seq = targetm.gen_sibcall_epilogue ())
6210 {
6211 start_sequence ();
6212 emit_note (NOTE_INSN_EPILOGUE_BEG);
6213 emit_insn (ep_seq);
6214 rtx_insn *seq = get_insns ();
6215 end_sequence ();
6216
6217 /* Retain a map of the epilogue insns. Used in life analysis to
6218 avoid getting rid of sibcall epilogue insns. Do this before we
6219 actually emit the sequence. */
6220 record_insns (seq, NULL, &epilogue_insn_hash);
6221 set_insn_locations (seq, epilogue_location);
6222
6223 emit_insn_before (seq, insn);
6224 }
6225 ei_next (&ei);
6226 }
6227
6228 if (epilogue_end)
6229 {
6230 rtx_insn *insn, *next;
6231
6232 /* Similarly, move any line notes that appear after the epilogue.
6233 There is no need, however, to be quite so anal about the existence
6234 of such a note. Also possibly move
6235 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
6236 info generation. */
6237 for (insn = epilogue_end; insn; insn = next)
6238 {
6239 next = NEXT_INSN (insn);
6240 if (NOTE_P (insn)
6241 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
6242 reorder_insns (insn, insn, PREV_INSN (epilogue_end));
6243 }
6244 }
6245
6246 bitmap_clear (&bb_flags);
6247
6248 /* Threading the prologue and epilogue changes the artificial refs
6249 in the entry and exit blocks. */
6250 epilogue_completed = 1;
6251 df_update_entry_exit_and_calls ();
6252 }
6253
6254 /* Reposition the prologue-end and epilogue-begin notes after
6255 instruction scheduling. */
6256
6257 void
6258 reposition_prologue_and_epilogue_notes (void)
6259 {
6260 if (!targetm.have_prologue ()
6261 && !targetm.have_epilogue ()
6262 && !targetm.have_sibcall_epilogue ())
6263 return;
6264
6265 /* Since the hash table is created on demand, the fact that it is
6266 non-null is a signal that it is non-empty. */
6267 if (prologue_insn_hash != NULL)
6268 {
6269 size_t len = prologue_insn_hash->elements ();
6270 rtx_insn *insn, *last = NULL, *note = NULL;
6271
6272 /* Scan from the beginning until we reach the last prologue insn. */
6273 /* ??? While we do have the CFG intact, there are two problems:
6274 (1) The prologue can contain loops (typically probing the stack),
6275 which means that the end of the prologue isn't in the first bb.
6276 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
6277 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6278 {
6279 if (NOTE_P (insn))
6280 {
6281 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
6282 note = insn;
6283 }
6284 else if (contains (insn, prologue_insn_hash))
6285 {
6286 last = insn;
6287 if (--len == 0)
6288 break;
6289 }
6290 }
6291
6292 if (last)
6293 {
6294 if (note == NULL)
6295 {
6296 /* Scan forward looking for the PROLOGUE_END note. It should
6297 be right at the beginning of the block, possibly with other
6298 insn notes that got moved there. */
6299 for (note = NEXT_INSN (last); ; note = NEXT_INSN (note))
6300 {
6301 if (NOTE_P (note)
6302 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
6303 break;
6304 }
6305 }
6306
6307 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
6308 if (LABEL_P (last))
6309 last = NEXT_INSN (last);
6310 reorder_insns (note, note, last);
6311 }
6312 }
6313
6314 if (epilogue_insn_hash != NULL)
6315 {
6316 edge_iterator ei;
6317 edge e;
6318
6319 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6320 {
6321 rtx_insn *insn, *first = NULL, *note = NULL;
6322 basic_block bb = e->src;
6323
6324 /* Scan from the beginning until we reach the first epilogue insn. */
6325 FOR_BB_INSNS (bb, insn)
6326 {
6327 if (NOTE_P (insn))
6328 {
6329 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
6330 {
6331 note = insn;
6332 if (first != NULL)
6333 break;
6334 }
6335 }
6336 else if (first == NULL && contains (insn, epilogue_insn_hash))
6337 {
6338 first = insn;
6339 if (note != NULL)
6340 break;
6341 }
6342 }
6343
6344 if (note)
6345 {
6346 /* If the function has a single basic block, and no real
6347 epilogue insns (e.g. sibcall with no cleanup), the
6348 epilogue note can get scheduled before the prologue
6349 note. If we have frame related prologue insns, having
6350 them scanned during the epilogue will result in a crash.
6351 In this case re-order the epilogue note to just before
6352 the last insn in the block. */
6353 if (first == NULL)
6354 first = BB_END (bb);
6355
6356 if (PREV_INSN (first) != note)
6357 reorder_insns (note, note, PREV_INSN (first));
6358 }
6359 }
6360 }
6361 }
6362
6363 /* Returns the name of function declared by FNDECL. */
6364 const char *
6365 fndecl_name (tree fndecl)
6366 {
6367 if (fndecl == NULL)
6368 return "(nofn)";
6369 return lang_hooks.decl_printable_name (fndecl, 2);
6370 }
6371
6372 /* Returns the name of function FN. */
6373 const char *
6374 function_name (struct function *fn)
6375 {
6376 tree fndecl = (fn == NULL) ? NULL : fn->decl;
6377 return fndecl_name (fndecl);
6378 }
6379
6380 /* Returns the name of the current function. */
6381 const char *
6382 current_function_name (void)
6383 {
6384 return function_name (cfun);
6385 }
6386 \f
6387
6388 static unsigned int
6389 rest_of_handle_check_leaf_regs (void)
6390 {
6391 #ifdef LEAF_REGISTERS
6392 crtl->uses_only_leaf_regs
6393 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6394 #endif
6395 return 0;
6396 }
6397
6398 /* Insert a TYPE into the used types hash table of CFUN. */
6399
6400 static void
6401 used_types_insert_helper (tree type, struct function *func)
6402 {
6403 if (type != NULL && func != NULL)
6404 {
6405 if (func->used_types_hash == NULL)
6406 func->used_types_hash = hash_set<tree>::create_ggc (37);
6407
6408 func->used_types_hash->add (type);
6409 }
6410 }
6411
6412 /* Given a type, insert it into the used hash table in cfun. */
6413 void
6414 used_types_insert (tree t)
6415 {
6416 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6417 if (TYPE_NAME (t))
6418 break;
6419 else
6420 t = TREE_TYPE (t);
6421 if (TREE_CODE (t) == ERROR_MARK)
6422 return;
6423 if (TYPE_NAME (t) == NULL_TREE
6424 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6425 t = TYPE_MAIN_VARIANT (t);
6426 if (debug_info_level > DINFO_LEVEL_NONE)
6427 {
6428 if (cfun)
6429 used_types_insert_helper (t, cfun);
6430 else
6431 {
6432 /* So this might be a type referenced by a global variable.
6433 Record that type so that we can later decide to emit its
6434 debug information. */
6435 vec_safe_push (types_used_by_cur_var_decl, t);
6436 }
6437 }
6438 }
6439
6440 /* Helper to Hash a struct types_used_by_vars_entry. */
6441
6442 static hashval_t
6443 hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6444 {
6445 gcc_assert (entry && entry->var_decl && entry->type);
6446
6447 return iterative_hash_object (entry->type,
6448 iterative_hash_object (entry->var_decl, 0));
6449 }
6450
6451 /* Hash function of the types_used_by_vars_entry hash table. */
6452
6453 hashval_t
6454 used_type_hasher::hash (types_used_by_vars_entry *entry)
6455 {
6456 return hash_types_used_by_vars_entry (entry);
6457 }
6458
6459 /*Equality function of the types_used_by_vars_entry hash table. */
6460
6461 bool
6462 used_type_hasher::equal (types_used_by_vars_entry *e1,
6463 types_used_by_vars_entry *e2)
6464 {
6465 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6466 }
6467
6468 /* Inserts an entry into the types_used_by_vars_hash hash table. */
6469
6470 void
6471 types_used_by_var_decl_insert (tree type, tree var_decl)
6472 {
6473 if (type != NULL && var_decl != NULL)
6474 {
6475 types_used_by_vars_entry **slot;
6476 struct types_used_by_vars_entry e;
6477 e.var_decl = var_decl;
6478 e.type = type;
6479 if (types_used_by_vars_hash == NULL)
6480 types_used_by_vars_hash
6481 = hash_table<used_type_hasher>::create_ggc (37);
6482
6483 slot = types_used_by_vars_hash->find_slot (&e, INSERT);
6484 if (*slot == NULL)
6485 {
6486 struct types_used_by_vars_entry *entry;
6487 entry = ggc_alloc<types_used_by_vars_entry> ();
6488 entry->type = type;
6489 entry->var_decl = var_decl;
6490 *slot = entry;
6491 }
6492 }
6493 }
6494
6495 namespace {
6496
6497 const pass_data pass_data_leaf_regs =
6498 {
6499 RTL_PASS, /* type */
6500 "*leaf_regs", /* name */
6501 OPTGROUP_NONE, /* optinfo_flags */
6502 TV_NONE, /* tv_id */
6503 0, /* properties_required */
6504 0, /* properties_provided */
6505 0, /* properties_destroyed */
6506 0, /* todo_flags_start */
6507 0, /* todo_flags_finish */
6508 };
6509
6510 class pass_leaf_regs : public rtl_opt_pass
6511 {
6512 public:
6513 pass_leaf_regs (gcc::context *ctxt)
6514 : rtl_opt_pass (pass_data_leaf_regs, ctxt)
6515 {}
6516
6517 /* opt_pass methods: */
6518 virtual unsigned int execute (function *)
6519 {
6520 return rest_of_handle_check_leaf_regs ();
6521 }
6522
6523 }; // class pass_leaf_regs
6524
6525 } // anon namespace
6526
6527 rtl_opt_pass *
6528 make_pass_leaf_regs (gcc::context *ctxt)
6529 {
6530 return new pass_leaf_regs (ctxt);
6531 }
6532
6533 static unsigned int
6534 rest_of_handle_thread_prologue_and_epilogue (void)
6535 {
6536 if (optimize)
6537 cleanup_cfg (CLEANUP_EXPENSIVE);
6538
6539 /* On some machines, the prologue and epilogue code, or parts thereof,
6540 can be represented as RTL. Doing so lets us schedule insns between
6541 it and the rest of the code and also allows delayed branch
6542 scheduling to operate in the epilogue. */
6543 thread_prologue_and_epilogue_insns ();
6544
6545 /* Some non-cold blocks may now be only reachable from cold blocks.
6546 Fix that up. */
6547 fixup_partitions ();
6548
6549 /* Shrink-wrapping can result in unreachable edges in the epilogue,
6550 see PR57320. */
6551 cleanup_cfg (0);
6552
6553 /* The stack usage info is finalized during prologue expansion. */
6554 if (flag_stack_usage_info)
6555 output_stack_usage ();
6556
6557 return 0;
6558 }
6559
6560 namespace {
6561
6562 const pass_data pass_data_thread_prologue_and_epilogue =
6563 {
6564 RTL_PASS, /* type */
6565 "pro_and_epilogue", /* name */
6566 OPTGROUP_NONE, /* optinfo_flags */
6567 TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
6568 0, /* properties_required */
6569 0, /* properties_provided */
6570 0, /* properties_destroyed */
6571 0, /* todo_flags_start */
6572 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
6573 };
6574
6575 class pass_thread_prologue_and_epilogue : public rtl_opt_pass
6576 {
6577 public:
6578 pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6579 : rtl_opt_pass (pass_data_thread_prologue_and_epilogue, ctxt)
6580 {}
6581
6582 /* opt_pass methods: */
6583 virtual unsigned int execute (function *)
6584 {
6585 return rest_of_handle_thread_prologue_and_epilogue ();
6586 }
6587
6588 }; // class pass_thread_prologue_and_epilogue
6589
6590 } // anon namespace
6591
6592 rtl_opt_pass *
6593 make_pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6594 {
6595 return new pass_thread_prologue_and_epilogue (ctxt);
6596 }
6597 \f
6598
6599 /* This mini-pass fixes fall-out from SSA in asm statements that have
6600 in-out constraints. Say you start with
6601
6602 orig = inout;
6603 asm ("": "+mr" (inout));
6604 use (orig);
6605
6606 which is transformed very early to use explicit output and match operands:
6607
6608 orig = inout;
6609 asm ("": "=mr" (inout) : "0" (inout));
6610 use (orig);
6611
6612 Or, after SSA and copyprop,
6613
6614 asm ("": "=mr" (inout_2) : "0" (inout_1));
6615 use (inout_1);
6616
6617 Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
6618 they represent two separate values, so they will get different pseudo
6619 registers during expansion. Then, since the two operands need to match
6620 per the constraints, but use different pseudo registers, reload can
6621 only register a reload for these operands. But reloads can only be
6622 satisfied by hardregs, not by memory, so we need a register for this
6623 reload, just because we are presented with non-matching operands.
6624 So, even though we allow memory for this operand, no memory can be
6625 used for it, just because the two operands don't match. This can
6626 cause reload failures on register-starved targets.
6627
6628 So it's a symptom of reload not being able to use memory for reloads
6629 or, alternatively it's also a symptom of both operands not coming into
6630 reload as matching (in which case the pseudo could go to memory just
6631 fine, as the alternative allows it, and no reload would be necessary).
6632 We fix the latter problem here, by transforming
6633
6634 asm ("": "=mr" (inout_2) : "0" (inout_1));
6635
6636 back to
6637
6638 inout_2 = inout_1;
6639 asm ("": "=mr" (inout_2) : "0" (inout_2)); */
6640
6641 static void
6642 match_asm_constraints_1 (rtx_insn *insn, rtx *p_sets, int noutputs)
6643 {
6644 int i;
6645 bool changed = false;
6646 rtx op = SET_SRC (p_sets[0]);
6647 int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
6648 rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
6649 bool *output_matched = XALLOCAVEC (bool, noutputs);
6650
6651 memset (output_matched, 0, noutputs * sizeof (bool));
6652 for (i = 0; i < ninputs; i++)
6653 {
6654 rtx input, output;
6655 rtx_insn *insns;
6656 const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
6657 char *end;
6658 int match, j;
6659
6660 if (*constraint == '%')
6661 constraint++;
6662
6663 match = strtoul (constraint, &end, 10);
6664 if (end == constraint)
6665 continue;
6666
6667 gcc_assert (match < noutputs);
6668 output = SET_DEST (p_sets[match]);
6669 input = RTVEC_ELT (inputs, i);
6670 /* Only do the transformation for pseudos. */
6671 if (! REG_P (output)
6672 || rtx_equal_p (output, input)
6673 || (GET_MODE (input) != VOIDmode
6674 && GET_MODE (input) != GET_MODE (output)))
6675 continue;
6676
6677 /* We can't do anything if the output is also used as input,
6678 as we're going to overwrite it. */
6679 for (j = 0; j < ninputs; j++)
6680 if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
6681 break;
6682 if (j != ninputs)
6683 continue;
6684
6685 /* Avoid changing the same input several times. For
6686 asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
6687 only change in once (to out1), rather than changing it
6688 first to out1 and afterwards to out2. */
6689 if (i > 0)
6690 {
6691 for (j = 0; j < noutputs; j++)
6692 if (output_matched[j] && input == SET_DEST (p_sets[j]))
6693 break;
6694 if (j != noutputs)
6695 continue;
6696 }
6697 output_matched[match] = true;
6698
6699 start_sequence ();
6700 emit_move_insn (output, input);
6701 insns = get_insns ();
6702 end_sequence ();
6703 emit_insn_before (insns, insn);
6704
6705 /* Now replace all mentions of the input with output. We can't
6706 just replace the occurrence in inputs[i], as the register might
6707 also be used in some other input (or even in an address of an
6708 output), which would mean possibly increasing the number of
6709 inputs by one (namely 'output' in addition), which might pose
6710 a too complicated problem for reload to solve. E.g. this situation:
6711
6712 asm ("" : "=r" (output), "=m" (input) : "0" (input))
6713
6714 Here 'input' is used in two occurrences as input (once for the
6715 input operand, once for the address in the second output operand).
6716 If we would replace only the occurrence of the input operand (to
6717 make the matching) we would be left with this:
6718
6719 output = input
6720 asm ("" : "=r" (output), "=m" (input) : "0" (output))
6721
6722 Now we suddenly have two different input values (containing the same
6723 value, but different pseudos) where we formerly had only one.
6724 With more complicated asms this might lead to reload failures
6725 which wouldn't have happen without this pass. So, iterate over
6726 all operands and replace all occurrences of the register used. */
6727 for (j = 0; j < noutputs; j++)
6728 if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
6729 && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
6730 SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
6731 input, output);
6732 for (j = 0; j < ninputs; j++)
6733 if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
6734 RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
6735 input, output);
6736
6737 changed = true;
6738 }
6739
6740 if (changed)
6741 df_insn_rescan (insn);
6742 }
6743
6744 /* Add the decl D to the local_decls list of FUN. */
6745
6746 void
6747 add_local_decl (struct function *fun, tree d)
6748 {
6749 gcc_assert (TREE_CODE (d) == VAR_DECL);
6750 vec_safe_push (fun->local_decls, d);
6751 }
6752
6753 namespace {
6754
6755 const pass_data pass_data_match_asm_constraints =
6756 {
6757 RTL_PASS, /* type */
6758 "asmcons", /* name */
6759 OPTGROUP_NONE, /* optinfo_flags */
6760 TV_NONE, /* tv_id */
6761 0, /* properties_required */
6762 0, /* properties_provided */
6763 0, /* properties_destroyed */
6764 0, /* todo_flags_start */
6765 0, /* todo_flags_finish */
6766 };
6767
6768 class pass_match_asm_constraints : public rtl_opt_pass
6769 {
6770 public:
6771 pass_match_asm_constraints (gcc::context *ctxt)
6772 : rtl_opt_pass (pass_data_match_asm_constraints, ctxt)
6773 {}
6774
6775 /* opt_pass methods: */
6776 virtual unsigned int execute (function *);
6777
6778 }; // class pass_match_asm_constraints
6779
6780 unsigned
6781 pass_match_asm_constraints::execute (function *fun)
6782 {
6783 basic_block bb;
6784 rtx_insn *insn;
6785 rtx pat, *p_sets;
6786 int noutputs;
6787
6788 if (!crtl->has_asm_statement)
6789 return 0;
6790
6791 df_set_flags (DF_DEFER_INSN_RESCAN);
6792 FOR_EACH_BB_FN (bb, fun)
6793 {
6794 FOR_BB_INSNS (bb, insn)
6795 {
6796 if (!INSN_P (insn))
6797 continue;
6798
6799 pat = PATTERN (insn);
6800 if (GET_CODE (pat) == PARALLEL)
6801 p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
6802 else if (GET_CODE (pat) == SET)
6803 p_sets = &PATTERN (insn), noutputs = 1;
6804 else
6805 continue;
6806
6807 if (GET_CODE (*p_sets) == SET
6808 && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
6809 match_asm_constraints_1 (insn, p_sets, noutputs);
6810 }
6811 }
6812
6813 return TODO_df_finish;
6814 }
6815
6816 } // anon namespace
6817
6818 rtl_opt_pass *
6819 make_pass_match_asm_constraints (gcc::context *ctxt)
6820 {
6821 return new pass_match_asm_constraints (ctxt);
6822 }
6823
6824
6825 #include "gt-function.h"