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