1 /* A pass for lowering trees to RTL.
2    Copyright (C) 2004-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
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10 
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "cfghooks.h"
29 #include "tree-pass.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "ssa.h"
33 #include "optabs.h"
34 #include "regs.h" /* For reg_renumber.  */
35 #include "emit-rtl.h"
36 #include "recog.h"
37 #include "cgraph.h"
38 #include "diagnostic.h"
39 #include "fold-const.h"
40 #include "varasm.h"
41 #include "stor-layout.h"
42 #include "stmt.h"
43 #include "print-tree.h"
44 #include "cfgrtl.h"
45 #include "cfganal.h"
46 #include "cfgbuild.h"
47 #include "cfgcleanup.h"
48 #include "dojump.h"
49 #include "explow.h"
50 #include "calls.h"
51 #include "expr.h"
52 #include "internal-fn.h"
53 #include "tree-eh.h"
54 #include "gimple-iterator.h"
55 #include "gimple-expr.h"
56 #include "gimple-walk.h"
57 #include "tree-cfg.h"
58 #include "tree-dfa.h"
59 #include "tree-ssa.h"
60 #include "except.h"
61 #include "gimple-pretty-print.h"
62 #include "toplev.h"
63 #include "debug.h"
64 #include "tree-inline.h"
65 #include "value-prof.h"
66 #include "tree-ssa-live.h"
67 #include "tree-outof-ssa.h"
68 #include "cfgloop.h"
69 #include "insn-attr.h" /* For INSN_SCHEDULING.  */
70 #include "stringpool.h"
71 #include "attribs.h"
72 #include "asan.h"
73 #include "tree-ssa-address.h"
74 #include "output.h"
75 #include "builtins.h"
76 #include "opts.h"
77 
78 /* Some systems use __main in a way incompatible with its use in gcc, in these
79    cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
80    give the same symbol without quotes for an alternative entry point.  You
81    must define both, or neither.  */
82 #ifndef NAME__MAIN
83 #define NAME__MAIN "__main"
84 #endif
85 
86 /* This variable holds information helping the rewriting of SSA trees
87    into RTL.  */
88 struct ssaexpand SA;
89 
90 /* This variable holds the currently expanded gimple statement for purposes
91    of comminucating the profile info to the builtin expanders.  */
92 gimple *currently_expanding_gimple_stmt;
93 
94 static rtx expand_debug_expr (tree);
95 
96 static bool defer_stack_allocation (tree, bool);
97 
98 static void record_alignment_for_reg_var (unsigned int);
99 
100 /* Return an expression tree corresponding to the RHS of GIMPLE
101    statement STMT.  */
102 
103 tree
gimple_assign_rhs_to_tree(gimple * stmt)104 gimple_assign_rhs_to_tree (gimple *stmt)
105 {
106   tree t;
107   switch (gimple_assign_rhs_class (stmt))
108     {
109     case GIMPLE_TERNARY_RHS:
110       t = build3 (gimple_assign_rhs_code (stmt),
111                       TREE_TYPE (gimple_assign_lhs (stmt)),
112                       gimple_assign_rhs1 (stmt), gimple_assign_rhs2 (stmt),
113                       gimple_assign_rhs3 (stmt));
114       break;
115     case GIMPLE_BINARY_RHS:
116       t = build2 (gimple_assign_rhs_code (stmt),
117                       TREE_TYPE (gimple_assign_lhs (stmt)),
118                       gimple_assign_rhs1 (stmt), gimple_assign_rhs2 (stmt));
119       break;
120     case GIMPLE_UNARY_RHS:
121       t = build1 (gimple_assign_rhs_code (stmt),
122                       TREE_TYPE (gimple_assign_lhs (stmt)),
123                       gimple_assign_rhs1 (stmt));
124       break;
125     case GIMPLE_SINGLE_RHS:
126       {
127           t = gimple_assign_rhs1 (stmt);
128           /* Avoid modifying this tree in place below.  */
129           if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
130                && gimple_location (stmt) != EXPR_LOCATION (t))
131               || (gimple_block (stmt) && currently_expanding_to_rtl
132                     && EXPR_P (t)))
133             t = copy_node (t);
134           break;
135       }
136     default:
137       gcc_unreachable ();
138     }
139 
140   if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
141     SET_EXPR_LOCATION (t, gimple_location (stmt));
142 
143   return t;
144 }
145 
146 
147 #ifndef STACK_ALIGNMENT_NEEDED
148 #define STACK_ALIGNMENT_NEEDED 1
149 #endif
150 
151 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
152 
153 /* Choose either CUR or NEXT as the leader DECL for a partition.
154    Prefer ignored decls, to simplify debug dumps and reduce ambiguity
155    out of the same user variable being in multiple partitions (this is
156    less likely for compiler-introduced temps).  */
157 
158 static tree
leader_merge(tree cur,tree next)159 leader_merge (tree cur, tree next)
160 {
161   if (cur == NULL || cur == next)
162     return next;
163 
164   if (DECL_P (cur) && DECL_IGNORED_P (cur))
165     return cur;
166 
167   if (DECL_P (next) && DECL_IGNORED_P (next))
168     return next;
169 
170   return cur;
171 }
172 
173 /* Associate declaration T with storage space X.  If T is no
174    SSA name this is exactly SET_DECL_RTL, otherwise make the
175    partition of T associated with X.  */
176 static inline void
set_rtl(tree t,rtx x)177 set_rtl (tree t, rtx x)
178 {
179   gcc_checking_assert (!x
180                            || !(TREE_CODE (t) == SSA_NAME || is_gimple_reg (t))
181                            || (use_register_for_decl (t)
182                                  ? (REG_P (x)
183                                     || (GET_CODE (x) == CONCAT
184                                           && (REG_P (XEXP (x, 0))
185                                               || SUBREG_P (XEXP (x, 0)))
186                                           && (REG_P (XEXP (x, 1))
187                                               || SUBREG_P (XEXP (x, 1))))
188                                     /* We need to accept PARALLELs for RESUT_DECLs
189                                          because of vector types with BLKmode returned
190                                          in multiple registers, but they are supposed
191                                          to be uncoalesced.  */
192                                     || (GET_CODE (x) == PARALLEL
193                                           && SSAVAR (t)
194                                           && TREE_CODE (SSAVAR (t)) == RESULT_DECL
195                                           && (GET_MODE (x) == BLKmode
196                                               || !flag_tree_coalesce_vars)))
197                                  : (MEM_P (x) || x == pc_rtx
198                                     || (GET_CODE (x) == CONCAT
199                                           && MEM_P (XEXP (x, 0))
200                                           && MEM_P (XEXP (x, 1))))));
201   /* Check that the RTL for SSA_NAMEs and gimple-reg PARM_DECLs and
202      RESULT_DECLs has the expected mode.  For memory, we accept
203      unpromoted modes, since that's what we're likely to get.  For
204      PARM_DECLs and RESULT_DECLs, we'll have been called by
205      set_parm_rtl, which will give us the default def, so we don't
206      have to compute it ourselves.  For RESULT_DECLs, we accept mode
207      mismatches too, as long as we have BLKmode or are not coalescing
208      across variables, so that we don't reject BLKmode PARALLELs or
209      unpromoted REGs.  */
210   gcc_checking_assert (!x || x == pc_rtx || TREE_CODE (t) != SSA_NAME
211                            || (SSAVAR (t)
212                                  && TREE_CODE (SSAVAR (t)) == RESULT_DECL
213                                  && (promote_ssa_mode (t, NULL) == BLKmode
214                                      || !flag_tree_coalesce_vars))
215                            || !use_register_for_decl (t)
216                            || GET_MODE (x) == promote_ssa_mode (t, NULL));
217 
218   if (x)
219     {
220       bool skip = false;
221       tree cur = NULL_TREE;
222       rtx xm = x;
223 
224     retry:
225       if (MEM_P (xm))
226           cur = MEM_EXPR (xm);
227       else if (REG_P (xm))
228           cur = REG_EXPR (xm);
229       else if (SUBREG_P (xm))
230           {
231             gcc_assert (subreg_lowpart_p (xm));
232             xm = SUBREG_REG (xm);
233             goto retry;
234           }
235       else if (GET_CODE (xm) == CONCAT)
236           {
237             xm = XEXP (xm, 0);
238             goto retry;
239           }
240       else if (GET_CODE (xm) == PARALLEL)
241           {
242             xm = XVECEXP (xm, 0, 0);
243             gcc_assert (GET_CODE (xm) == EXPR_LIST);
244             xm = XEXP (xm, 0);
245             goto retry;
246           }
247       else if (xm == pc_rtx)
248           skip = true;
249       else
250           gcc_unreachable ();
251 
252       tree next = skip ? cur : leader_merge (cur, SSAVAR (t) ? SSAVAR (t) : t);
253 
254       if (cur != next)
255           {
256             if (MEM_P (x))
257               set_mem_attributes (x,
258                                         next && TREE_CODE (next) == SSA_NAME
259                                         ? TREE_TYPE (next)
260                                         : next, true);
261             else
262               set_reg_attrs_for_decl_rtl (next, x);
263           }
264     }
265 
266   if (TREE_CODE (t) == SSA_NAME)
267     {
268       int part = var_to_partition (SA.map, t);
269       if (part != NO_PARTITION)
270           {
271             if (SA.partition_to_pseudo[part])
272               gcc_assert (SA.partition_to_pseudo[part] == x);
273             else if (x != pc_rtx)
274               SA.partition_to_pseudo[part] = x;
275           }
276       /* For the benefit of debug information at -O0 (where
277          vartracking doesn't run) record the place also in the base
278          DECL.  For PARMs and RESULTs, do so only when setting the
279          default def.  */
280       if (x && x != pc_rtx && SSA_NAME_VAR (t)
281             && (VAR_P (SSA_NAME_VAR (t))
282                 || SSA_NAME_IS_DEFAULT_DEF (t)))
283           {
284             tree var = SSA_NAME_VAR (t);
285             /* If we don't yet have something recorded, just record it now.  */
286             if (!DECL_RTL_SET_P (var))
287               SET_DECL_RTL (var, x);
288             /* If we have it set already to "multiple places" don't
289                change this.  */
290             else if (DECL_RTL (var) == pc_rtx)
291               ;
292             /* If we have something recorded and it's not the same place
293                as we want to record now, we have multiple partitions for the
294                same base variable, with different places.  We can't just
295                randomly chose one, hence we have to say that we don't know.
296                This only happens with optimization, and there var-tracking
297                will figure out the right thing.  */
298             else if (DECL_RTL (var) != x)
299               SET_DECL_RTL (var, pc_rtx);
300           }
301     }
302   else
303     SET_DECL_RTL (t, x);
304 }
305 
306 /* This structure holds data relevant to one variable that will be
307    placed in a stack slot.  */
308 class stack_var
309 {
310 public:
311   /* The Variable.  */
312   tree decl;
313 
314   /* Initially, the size of the variable.  Later, the size of the partition,
315      if this variable becomes it's partition's representative.  */
316   poly_uint64 size;
317 
318   /* The *byte* alignment required for this variable.  Or as, with the
319      size, the alignment for this partition.  */
320   unsigned int alignb;
321 
322   /* The partition representative.  */
323   size_t representative;
324 
325   /* The next stack variable in the partition, or EOC.  */
326   size_t next;
327 
328   /* The numbers of conflicting stack variables.  */
329   bitmap conflicts;
330 };
331 
332 #define EOC  ((size_t)-1)
333 
334 /* We have an array of such objects while deciding allocation.  */
335 static class stack_var *stack_vars;
336 static size_t stack_vars_alloc;
337 static size_t stack_vars_num;
338 static hash_map<tree, size_t> *decl_to_stack_part;
339 
340 /* Conflict bitmaps go on this obstack.  This allows us to destroy
341    all of them in one big sweep.  */
342 static bitmap_obstack stack_var_bitmap_obstack;
343 
344 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
345    is non-decreasing.  */
346 static size_t *stack_vars_sorted;
347 
348 /* The phase of the stack frame.  This is the known misalignment of
349    virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY.  That is,
350    (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0.  */
351 static int frame_phase;
352 
353 /* Used during expand_used_vars to remember if we saw any decls for
354    which we'd like to enable stack smashing protection.  */
355 static bool has_protected_decls;
356 
357 /* Used during expand_used_vars.  Remember if we say a character buffer
358    smaller than our cutoff threshold.  Used for -Wstack-protector.  */
359 static bool has_short_buffer;
360 
361 /* Compute the byte alignment to use for DECL.  Ignore alignment
362    we can't do with expected alignment of the stack boundary.  */
363 
364 static unsigned int
align_local_variable(tree decl,bool really_expand)365 align_local_variable (tree decl, bool really_expand)
366 {
367   unsigned int align;
368 
369   if (TREE_CODE (decl) == SSA_NAME)
370     {
371       tree type = TREE_TYPE (decl);
372       machine_mode mode = TYPE_MODE (type);
373 
374       align = TYPE_ALIGN (type);
375       if (mode != BLKmode
376             && align < GET_MODE_ALIGNMENT (mode))
377           align = GET_MODE_ALIGNMENT (mode);
378     }
379   else
380     align = LOCAL_DECL_ALIGNMENT (decl);
381 
382   if (hwasan_sanitize_stack_p ())
383     align = MAX (align, (unsigned) HWASAN_TAG_GRANULE_SIZE * BITS_PER_UNIT);
384 
385   if (TREE_CODE (decl) != SSA_NAME && really_expand)
386     /* Don't change DECL_ALIGN when called from estimated_stack_frame_size.
387        That is done before IPA and could bump alignment based on host
388        backend even for offloaded code which wants different
389        LOCAL_DECL_ALIGNMENT.  */
390     SET_DECL_ALIGN (decl, align);
391 
392   return align / BITS_PER_UNIT;
393 }
394 
395 /* Align given offset BASE with ALIGN.  Truncate up if ALIGN_UP is true,
396    down otherwise.  Return truncated BASE value.  */
397 
398 static inline unsigned HOST_WIDE_INT
align_base(HOST_WIDE_INT base,unsigned HOST_WIDE_INT align,bool align_up)399 align_base (HOST_WIDE_INT base, unsigned HOST_WIDE_INT align, bool align_up)
400 {
401   return align_up ? (base + align - 1) & -align : base & -align;
402 }
403 
404 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
405    Return the frame offset.  */
406 
407 static poly_int64
alloc_stack_frame_space(poly_int64 size,unsigned HOST_WIDE_INT align)408 alloc_stack_frame_space (poly_int64 size, unsigned HOST_WIDE_INT align)
409 {
410   poly_int64 offset, new_frame_offset;
411 
412   if (FRAME_GROWS_DOWNWARD)
413     {
414       new_frame_offset
415           = aligned_lower_bound (frame_offset - frame_phase - size,
416                                      align) + frame_phase;
417       offset = new_frame_offset;
418     }
419   else
420     {
421       new_frame_offset
422           = aligned_upper_bound (frame_offset - frame_phase,
423                                      align) + frame_phase;
424       offset = new_frame_offset;
425       new_frame_offset += size;
426     }
427   frame_offset = new_frame_offset;
428 
429   if (frame_offset_overflow (frame_offset, cfun->decl))
430     frame_offset = offset = 0;
431 
432   return offset;
433 }
434 
435 /* Ensure that the stack is aligned to ALIGN bytes.
436    Return the new frame offset.  */
437 static poly_int64
align_frame_offset(unsigned HOST_WIDE_INT align)438 align_frame_offset (unsigned HOST_WIDE_INT align)
439 {
440   return alloc_stack_frame_space (0, align);
441 }
442 
443 /* Accumulate DECL into STACK_VARS.  */
444 
445 static void
add_stack_var(tree decl,bool really_expand)446 add_stack_var (tree decl, bool really_expand)
447 {
448   class stack_var *v;
449 
450   if (stack_vars_num >= stack_vars_alloc)
451     {
452       if (stack_vars_alloc)
453           stack_vars_alloc = stack_vars_alloc * 3 / 2;
454       else
455           stack_vars_alloc = 32;
456       stack_vars
457           = XRESIZEVEC (class stack_var, stack_vars, stack_vars_alloc);
458     }
459   if (!decl_to_stack_part)
460     decl_to_stack_part = new hash_map<tree, size_t>;
461 
462   v = &stack_vars[stack_vars_num];
463   decl_to_stack_part->put (decl, stack_vars_num);
464 
465   v->decl = decl;
466   tree size = TREE_CODE (decl) == SSA_NAME
467     ? TYPE_SIZE_UNIT (TREE_TYPE (decl))
468     : DECL_SIZE_UNIT (decl);
469   v->size = tree_to_poly_uint64 (size);
470   /* Ensure that all variables have size, so that &a != &b for any two
471      variables that are simultaneously live.  */
472   if (known_eq (v->size, 0U))
473     v->size = 1;
474   v->alignb = align_local_variable (decl, really_expand);
475   /* An alignment of zero can mightily confuse us later.  */
476   gcc_assert (v->alignb != 0);
477 
478   /* All variables are initially in their own partition.  */
479   v->representative = stack_vars_num;
480   v->next = EOC;
481 
482   /* All variables initially conflict with no other.  */
483   v->conflicts = NULL;
484 
485   /* Ensure that this decl doesn't get put onto the list twice.  */
486   set_rtl (decl, pc_rtx);
487 
488   stack_vars_num++;
489 }
490 
491 /* Make the decls associated with luid's X and Y conflict.  */
492 
493 static void
add_stack_var_conflict(size_t x,size_t y)494 add_stack_var_conflict (size_t x, size_t y)
495 {
496   class stack_var *a = &stack_vars[x];
497   class stack_var *b = &stack_vars[y];
498   if (x == y)
499     return;
500   if (!a->conflicts)
501     a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
502   if (!b->conflicts)
503     b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
504   bitmap_set_bit (a->conflicts, y);
505   bitmap_set_bit (b->conflicts, x);
506 }
507 
508 /* Check whether the decls associated with luid's X and Y conflict.  */
509 
510 static bool
stack_var_conflict_p(size_t x,size_t y)511 stack_var_conflict_p (size_t x, size_t y)
512 {
513   class stack_var *a = &stack_vars[x];
514   class stack_var *b = &stack_vars[y];
515   if (x == y)
516     return false;
517   /* Partitions containing an SSA name result from gimple registers
518      with things like unsupported modes.  They are top-level and
519      hence conflict with everything else.  */
520   if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
521     return true;
522 
523   if (!a->conflicts || !b->conflicts)
524     return false;
525   return bitmap_bit_p (a->conflicts, y);
526 }
527 
528 /* Callback for walk_stmt_ops.  If OP is a decl touched by add_stack_var
529    enter its partition number into bitmap DATA.  */
530 
531 static bool
visit_op(gimple *,tree op,tree,void * data)532 visit_op (gimple *, tree op, tree, void *data)
533 {
534   bitmap active = (bitmap)data;
535   op = get_base_address (op);
536   if (op
537       && DECL_P (op)
538       && DECL_RTL_IF_SET (op) == pc_rtx)
539     {
540       size_t *v = decl_to_stack_part->get (op);
541       if (v)
542           bitmap_set_bit (active, *v);
543     }
544   return false;
545 }
546 
547 /* Callback for walk_stmt_ops.  If OP is a decl touched by add_stack_var
548    record conflicts between it and all currently active other partitions
549    from bitmap DATA.  */
550 
551 static bool
visit_conflict(gimple *,tree op,tree,void * data)552 visit_conflict (gimple *, tree op, tree, void *data)
553 {
554   bitmap active = (bitmap)data;
555   op = get_base_address (op);
556   if (op
557       && DECL_P (op)
558       && DECL_RTL_IF_SET (op) == pc_rtx)
559     {
560       size_t *v = decl_to_stack_part->get (op);
561       if (v && bitmap_set_bit (active, *v))
562           {
563             size_t num = *v;
564             bitmap_iterator bi;
565             unsigned i;
566             gcc_assert (num < stack_vars_num);
567             EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
568               add_stack_var_conflict (num, i);
569           }
570     }
571   return false;
572 }
573 
574 /* Helper function for add_scope_conflicts_1.  For USE on
575    a stmt, if it is a SSA_NAME and in its SSA_NAME_DEF_STMT is known to be
576    based on some ADDR_EXPR, invoke VISIT on that ADDR_EXPR.  */
577 
578 static inline void
add_scope_conflicts_2(tree use,bitmap work,walk_stmt_load_store_addr_fn visit)579 add_scope_conflicts_2 (tree use, bitmap work,
580                            walk_stmt_load_store_addr_fn visit)
581 {
582   if (TREE_CODE (use) == SSA_NAME
583       && (POINTER_TYPE_P (TREE_TYPE (use))
584             || INTEGRAL_TYPE_P (TREE_TYPE (use))))
585     {
586       gimple *g = SSA_NAME_DEF_STMT (use);
587       if (is_gimple_assign (g))
588           if (tree op = gimple_assign_rhs1 (g))
589             if (TREE_CODE (op) == ADDR_EXPR)
590               visit (g, TREE_OPERAND (op, 0), op, work);
591     }
592 }
593 
594 /* Helper routine for add_scope_conflicts, calculating the active partitions
595    at the end of BB, leaving the result in WORK.  We're called to generate
596    conflicts when FOR_CONFLICT is true, otherwise we're just tracking
597    liveness.  */
598 
599 static void
add_scope_conflicts_1(basic_block bb,bitmap work,bool for_conflict)600 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
601 {
602   edge e;
603   edge_iterator ei;
604   gimple_stmt_iterator gsi;
605   walk_stmt_load_store_addr_fn visit;
606   use_operand_p use_p;
607   ssa_op_iter iter;
608 
609   bitmap_clear (work);
610   FOR_EACH_EDGE (e, ei, bb->preds)
611     bitmap_ior_into (work, (bitmap)e->src->aux);
612 
613   visit = visit_op;
614 
615   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
616     {
617       gimple *stmt = gsi_stmt (gsi);
618       gphi *phi = as_a <gphi *> (stmt);
619       walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
620       FOR_EACH_PHI_ARG (use_p, phi, iter, SSA_OP_USE)
621           add_scope_conflicts_2 (USE_FROM_PTR (use_p), work, visit);
622     }
623   for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
624     {
625       gimple *stmt = gsi_stmt (gsi);
626 
627       if (gimple_clobber_p (stmt))
628           {
629             tree lhs = gimple_assign_lhs (stmt);
630             size_t *v;
631             /* Nested function lowering might introduce LHSs
632                that are COMPONENT_REFs.  */
633             if (!VAR_P (lhs))
634               continue;
635             if (DECL_RTL_IF_SET (lhs) == pc_rtx
636                 && (v = decl_to_stack_part->get (lhs)))
637               bitmap_clear_bit (work, *v);
638           }
639       else if (!is_gimple_debug (stmt))
640           {
641             if (for_conflict && visit == visit_op)
642               {
643                 /* If this is the first real instruction in this BB we need
644                    to add conflicts for everything live at this point now.
645                      Unlike classical liveness for named objects we can't
646                      rely on seeing a def/use of the names we're interested in.
647                      There might merely be indirect loads/stores.  We'd not add any
648                      conflicts for such partitions.  */
649                 bitmap_iterator bi;
650                 unsigned i;
651                 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
652                     {
653                       class stack_var *a = &stack_vars[i];
654                       if (!a->conflicts)
655                         a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
656                       bitmap_ior_into (a->conflicts, work);
657                     }
658                 visit = visit_conflict;
659               }
660             walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
661             FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
662               add_scope_conflicts_2 (USE_FROM_PTR (use_p), work, visit);
663           }
664     }
665 }
666 
667 /* Generate stack partition conflicts between all partitions that are
668    simultaneously live.  */
669 
670 static void
add_scope_conflicts(void)671 add_scope_conflicts (void)
672 {
673   basic_block bb;
674   bool changed;
675   bitmap work = BITMAP_ALLOC (NULL);
676   int *rpo;
677   int n_bbs;
678 
679   /* We approximate the live range of a stack variable by taking the first
680      mention of its name as starting point(s), and by the end-of-scope
681      death clobber added by gimplify as ending point(s) of the range.
682      This overapproximates in the case we for instance moved an address-taken
683      operation upward, without also moving a dereference to it upwards.
684      But it's conservatively correct as a variable never can hold values
685      before its name is mentioned at least once.
686 
687      We then do a mostly classical bitmap liveness algorithm.  */
688 
689   FOR_ALL_BB_FN (bb, cfun)
690     bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
691 
692   rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
693   n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
694 
695   changed = true;
696   while (changed)
697     {
698       int i;
699       changed = false;
700       for (i = 0; i < n_bbs; i++)
701           {
702             bitmap active;
703             bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
704             active = (bitmap)bb->aux;
705             add_scope_conflicts_1 (bb, work, false);
706             if (bitmap_ior_into (active, work))
707               changed = true;
708           }
709     }
710 
711   FOR_EACH_BB_FN (bb, cfun)
712     add_scope_conflicts_1 (bb, work, true);
713 
714   free (rpo);
715   BITMAP_FREE (work);
716   FOR_ALL_BB_FN (bb, cfun)
717     BITMAP_FREE (bb->aux);
718 }
719 
720 /* A subroutine of partition_stack_vars.  A comparison function for qsort,
721    sorting an array of indices by the properties of the object.  */
722 
723 static int
stack_var_cmp(const void * a,const void * b)724 stack_var_cmp (const void *a, const void *b)
725 {
726   size_t ia = *(const size_t *)a;
727   size_t ib = *(const size_t *)b;
728   unsigned int aligna = stack_vars[ia].alignb;
729   unsigned int alignb = stack_vars[ib].alignb;
730   poly_int64 sizea = stack_vars[ia].size;
731   poly_int64 sizeb = stack_vars[ib].size;
732   tree decla = stack_vars[ia].decl;
733   tree declb = stack_vars[ib].decl;
734   bool largea, largeb;
735   unsigned int uida, uidb;
736 
737   /* Primary compare on "large" alignment.  Large comes first.  */
738   largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
739   largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
740   if (largea != largeb)
741     return (int)largeb - (int)largea;
742 
743   /* Secondary compare on size, decreasing  */
744   int diff = compare_sizes_for_sort (sizeb, sizea);
745   if (diff != 0)
746     return diff;
747 
748   /* Tertiary compare on true alignment, decreasing.  */
749   if (aligna < alignb)
750     return -1;
751   if (aligna > alignb)
752     return 1;
753 
754   /* Final compare on ID for sort stability, increasing.
755      Two SSA names are compared by their version, SSA names come before
756      non-SSA names, and two normal decls are compared by their DECL_UID.  */
757   if (TREE_CODE (decla) == SSA_NAME)
758     {
759       if (TREE_CODE (declb) == SSA_NAME)
760           uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
761       else
762           return -1;
763     }
764   else if (TREE_CODE (declb) == SSA_NAME)
765     return 1;
766   else
767     uida = DECL_UID (decla), uidb = DECL_UID (declb);
768   if (uida < uidb)
769     return 1;
770   if (uida > uidb)
771     return -1;
772   return 0;
773 }
774 
775 struct part_traits : unbounded_int_hashmap_traits <size_t, bitmap> {};
776 typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
777 
778 /* If the points-to solution *PI points to variables that are in a partition
779    together with other variables add all partition members to the pointed-to
780    variables bitmap.  */
781 
782 static void
add_partitioned_vars_to_ptset(struct pt_solution * pt,part_hashmap * decls_to_partitions,hash_set<bitmap> * visited,bitmap temp)783 add_partitioned_vars_to_ptset (struct pt_solution *pt,
784                                      part_hashmap *decls_to_partitions,
785                                      hash_set<bitmap> *visited, bitmap temp)
786 {
787   bitmap_iterator bi;
788   unsigned i;
789   bitmap *part;
790 
791   if (pt->anything
792       || pt->vars == NULL
793       /* The pointed-to vars bitmap is shared, it is enough to
794            visit it once.  */
795       || visited->add (pt->vars))
796     return;
797 
798   bitmap_clear (temp);
799 
800   /* By using a temporary bitmap to store all members of the partitions
801      we have to add we make sure to visit each of the partitions only
802      once.  */
803   EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
804     if ((!temp
805            || !bitmap_bit_p (temp, i))
806           && (part = decls_to_partitions->get (i)))
807       bitmap_ior_into (temp, *part);
808   if (!bitmap_empty_p (temp))
809     bitmap_ior_into (pt->vars, temp);
810 }
811 
812 /* Update points-to sets based on partition info, so we can use them on RTL.
813    The bitmaps representing stack partitions will be saved until expand,
814    where partitioned decls used as bases in memory expressions will be
815    rewritten.  */
816 
817 static void
update_alias_info_with_stack_vars(void)818 update_alias_info_with_stack_vars (void)
819 {
820   part_hashmap *decls_to_partitions = NULL;
821   size_t i, j;
822   tree var = NULL_TREE;
823 
824   for (i = 0; i < stack_vars_num; i++)
825     {
826       bitmap part = NULL;
827       tree name;
828       struct ptr_info_def *pi;
829 
830       /* Not interested in partitions with single variable.  */
831       if (stack_vars[i].representative != i
832           || stack_vars[i].next == EOC)
833         continue;
834 
835       if (!decls_to_partitions)
836           {
837             decls_to_partitions = new part_hashmap;
838             cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
839           }
840 
841       /* Create an SSA_NAME that points to the partition for use
842          as base during alias-oracle queries on RTL for bases that
843            have been partitioned.  */
844       if (var == NULL_TREE)
845           var = create_tmp_var (ptr_type_node);
846       name = make_ssa_name (var);
847 
848       /* Create bitmaps representing partitions.  They will be used for
849          points-to sets later, so use GGC alloc.  */
850       part = BITMAP_GGC_ALLOC ();
851       for (j = i; j != EOC; j = stack_vars[j].next)
852           {
853             tree decl = stack_vars[j].decl;
854             unsigned int uid = DECL_PT_UID (decl);
855             bitmap_set_bit (part, uid);
856             decls_to_partitions->put (uid, part);
857             cfun->gimple_df->decls_to_pointers->put (decl, name);
858             if (TREE_ADDRESSABLE (decl))
859               TREE_ADDRESSABLE (name) = 1;
860           }
861 
862       /* Make the SSA name point to all partition members.  */
863       pi = get_ptr_info (name);
864       pt_solution_set (&pi->pt, part, false);
865     }
866 
867   /* Make all points-to sets that contain one member of a partition
868      contain all members of the partition.  */
869   if (decls_to_partitions)
870     {
871       unsigned i;
872       tree name;
873       hash_set<bitmap> visited;
874       bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
875 
876       FOR_EACH_SSA_NAME (i, name, cfun)
877           {
878             struct ptr_info_def *pi;
879 
880             if (POINTER_TYPE_P (TREE_TYPE (name))
881                 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
882               add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
883                                                      &visited, temp);
884           }
885 
886       add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
887                                              decls_to_partitions, &visited, temp);
888 
889       delete decls_to_partitions;
890       BITMAP_FREE (temp);
891     }
892 }
893 
894 /* A subroutine of partition_stack_vars.  The UNION portion of a UNION/FIND
895    partitioning algorithm.  Partitions A and B are known to be non-conflicting.
896    Merge them into a single partition A.  */
897 
898 static void
union_stack_vars(size_t a,size_t b)899 union_stack_vars (size_t a, size_t b)
900 {
901   class stack_var *vb = &stack_vars[b];
902   bitmap_iterator bi;
903   unsigned u;
904 
905   gcc_assert (stack_vars[b].next == EOC);
906    /* Add B to A's partition.  */
907   stack_vars[b].next = stack_vars[a].next;
908   stack_vars[b].representative = a;
909   stack_vars[a].next = b;
910 
911   /* Make sure A is big enough to hold B.  */
912   stack_vars[a].size = upper_bound (stack_vars[a].size, stack_vars[b].size);
913 
914   /* Update the required alignment of partition A to account for B.  */
915   if (stack_vars[a].alignb < stack_vars[b].alignb)
916     stack_vars[a].alignb = stack_vars[b].alignb;
917 
918   /* Update the interference graph and merge the conflicts.  */
919   if (vb->conflicts)
920     {
921       EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
922           add_stack_var_conflict (a, stack_vars[u].representative);
923       BITMAP_FREE (vb->conflicts);
924     }
925 }
926 
927 /* A subroutine of expand_used_vars.  Binpack the variables into
928    partitions constrained by the interference graph.  The overall
929    algorithm used is as follows:
930 
931           Sort the objects by size in descending order.
932           For each object A {
933             S = size(A)
934             O = 0
935             loop {
936               Look for the largest non-conflicting object B with size <= S.
937               UNION (A, B)
938             }
939           }
940 */
941 
942 static void
partition_stack_vars(void)943 partition_stack_vars (void)
944 {
945   size_t si, sj, n = stack_vars_num;
946 
947   stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
948   for (si = 0; si < n; ++si)
949     stack_vars_sorted[si] = si;
950 
951   if (n == 1)
952     return;
953 
954   qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
955 
956   for (si = 0; si < n; ++si)
957     {
958       size_t i = stack_vars_sorted[si];
959       unsigned int ialign = stack_vars[i].alignb;
960       poly_int64 isize = stack_vars[i].size;
961 
962       /* Ignore objects that aren't partition representatives. If we
963          see a var that is not a partition representative, it must
964          have been merged earlier.  */
965       if (stack_vars[i].representative != i)
966         continue;
967 
968       for (sj = si + 1; sj < n; ++sj)
969           {
970             size_t j = stack_vars_sorted[sj];
971             unsigned int jalign = stack_vars[j].alignb;
972             poly_int64 jsize = stack_vars[j].size;
973 
974             /* Ignore objects that aren't partition representatives.  */
975             if (stack_vars[j].representative != j)
976               continue;
977 
978             /* Do not mix objects of "small" (supported) alignment
979                and "large" (unsupported) alignment.  */
980             if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
981                 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
982               break;
983 
984             /* For Address Sanitizer do not mix objects with different
985                sizes, as the shorter vars wouldn't be adequately protected.
986                Don't do that for "large" (unsupported) alignment objects,
987                those aren't protected anyway.  */
988             if (asan_sanitize_stack_p ()
989                 && maybe_ne (isize, jsize)
990                 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
991               break;
992 
993             /* Ignore conflicting objects.  */
994             if (stack_var_conflict_p (i, j))
995               continue;
996 
997             /* UNION the objects, placing J at OFFSET.  */
998             union_stack_vars (i, j);
999           }
1000     }
1001 
1002   update_alias_info_with_stack_vars ();
1003 }
1004 
1005 /* A debugging aid for expand_used_vars.  Dump the generated partitions.  */
1006 
1007 static void
dump_stack_var_partition(void)1008 dump_stack_var_partition (void)
1009 {
1010   size_t si, i, j, n = stack_vars_num;
1011 
1012   for (si = 0; si < n; ++si)
1013     {
1014       i = stack_vars_sorted[si];
1015 
1016       /* Skip variables that aren't partition representatives, for now.  */
1017       if (stack_vars[i].representative != i)
1018           continue;
1019 
1020       fprintf (dump_file, "Partition %lu: size ", (unsigned long) i);
1021       print_dec (stack_vars[i].size, dump_file);
1022       fprintf (dump_file, " align %u\n", stack_vars[i].alignb);
1023 
1024       for (j = i; j != EOC; j = stack_vars[j].next)
1025           {
1026             fputc ('\t', dump_file);
1027             print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
1028           }
1029       fputc ('\n', dump_file);
1030     }
1031 }
1032 
1033 /* Assign rtl to DECL at BASE + OFFSET.  */
1034 
1035 static void
expand_one_stack_var_at(tree decl,rtx base,unsigned base_align,poly_int64 offset)1036 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
1037                                poly_int64 offset)
1038 {
1039   unsigned align;
1040   rtx x;
1041 
1042   /* If this fails, we've overflowed the stack frame.  Error nicely?  */
1043   gcc_assert (known_eq (offset, trunc_int_for_mode (offset, Pmode)));
1044 
1045   if (hwasan_sanitize_stack_p ())
1046     x = targetm.memtag.add_tag (base, offset,
1047                                         hwasan_current_frame_tag ());
1048   else
1049     x = plus_constant (Pmode, base, offset);
1050 
1051   x = gen_rtx_MEM (TREE_CODE (decl) == SSA_NAME
1052                        ? TYPE_MODE (TREE_TYPE (decl))
1053                        : DECL_MODE (decl), x);
1054 
1055   /* Set alignment we actually gave this decl if it isn't an SSA name.
1056      If it is we generate stack slots only accidentally so it isn't as
1057      important, we'll simply set the alignment directly on the MEM.  */
1058 
1059   if (stack_vars_base_reg_p (base))
1060     offset -= frame_phase;
1061   align = known_alignment (offset);
1062   align *= BITS_PER_UNIT;
1063   if (align == 0 || align > base_align)
1064     align = base_align;
1065 
1066   if (TREE_CODE (decl) != SSA_NAME)
1067     {
1068       /* One would think that we could assert that we're not decreasing
1069            alignment here, but (at least) the i386 port does exactly this
1070            via the MINIMUM_ALIGNMENT hook.  */
1071 
1072       SET_DECL_ALIGN (decl, align);
1073       DECL_USER_ALIGN (decl) = 0;
1074     }
1075 
1076   set_rtl (decl, x);
1077 
1078   set_mem_align (x, align);
1079 }
1080 
1081 class stack_vars_data
1082 {
1083 public:
1084   /* Vector of offset pairs, always end of some padding followed
1085      by start of the padding that needs Address Sanitizer protection.
1086      The vector is in reversed, highest offset pairs come first.  */
1087   auto_vec<HOST_WIDE_INT> asan_vec;
1088 
1089   /* Vector of partition representative decls in between the paddings.  */
1090   auto_vec<tree> asan_decl_vec;
1091 
1092   /* Base pseudo register for Address Sanitizer protected automatic vars.  */
1093   rtx asan_base;
1094 
1095   /* Alignment needed for the Address Sanitizer protected automatic vars.  */
1096   unsigned int asan_alignb;
1097 };
1098 
1099 /* A subroutine of expand_used_vars.  Give each partition representative
1100    a unique location within the stack frame.  Update each partition member
1101    with that location.  */
1102 static void
expand_stack_vars(bool (* pred)(size_t),class stack_vars_data * data)1103 expand_stack_vars (bool (*pred) (size_t), class stack_vars_data *data)
1104 {
1105   size_t si, i, j, n = stack_vars_num;
1106   poly_uint64 large_size = 0, large_alloc = 0;
1107   rtx large_base = NULL;
1108   rtx large_untagged_base = NULL;
1109   unsigned large_align = 0;
1110   bool large_allocation_done = false;
1111   tree decl;
1112 
1113   /* Determine if there are any variables requiring "large" alignment.
1114      Since these are dynamically allocated, we only process these if
1115      no predicate involved.  */
1116   large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
1117   if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
1118     {
1119       /* Find the total size of these variables.  */
1120       for (si = 0; si < n; ++si)
1121           {
1122             unsigned alignb;
1123 
1124             i = stack_vars_sorted[si];
1125             alignb = stack_vars[i].alignb;
1126 
1127             /* All "large" alignment decls come before all "small" alignment
1128                decls, but "large" alignment decls are not sorted based on
1129                their alignment.  Increase large_align to track the largest
1130                required alignment.  */
1131             if ((alignb * BITS_PER_UNIT) > large_align)
1132               large_align = alignb * BITS_PER_UNIT;
1133 
1134             /* Stop when we get to the first decl with "small" alignment.  */
1135             if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1136               break;
1137 
1138             /* Skip variables that aren't partition representatives.  */
1139             if (stack_vars[i].representative != i)
1140               continue;
1141 
1142             /* Skip variables that have already had rtl assigned.  See also
1143                add_stack_var where we perpetrate this pc_rtx hack.  */
1144             decl = stack_vars[i].decl;
1145             if (TREE_CODE (decl) == SSA_NAME
1146                 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)] != NULL_RTX
1147                 : DECL_RTL (decl) != pc_rtx)
1148               continue;
1149 
1150             large_size = aligned_upper_bound (large_size, alignb);
1151             large_size += stack_vars[i].size;
1152           }
1153     }
1154 
1155   for (si = 0; si < n; ++si)
1156     {
1157       rtx base;
1158       unsigned base_align, alignb;
1159       poly_int64 offset = 0;
1160 
1161       i = stack_vars_sorted[si];
1162 
1163       /* Skip variables that aren't partition representatives, for now.  */
1164       if (stack_vars[i].representative != i)
1165           continue;
1166 
1167       /* Skip variables that have already had rtl assigned.  See also
1168            add_stack_var where we perpetrate this pc_rtx hack.  */
1169       decl = stack_vars[i].decl;
1170       if (TREE_CODE (decl) == SSA_NAME
1171             ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)] != NULL_RTX
1172             : DECL_RTL (decl) != pc_rtx)
1173           continue;
1174 
1175       /* Check the predicate to see whether this variable should be
1176            allocated in this pass.  */
1177       if (pred && !pred (i))
1178           continue;
1179 
1180       base = (hwasan_sanitize_stack_p ()
1181                 ? hwasan_frame_base ()
1182                 : virtual_stack_vars_rtx);
1183       alignb = stack_vars[i].alignb;
1184       if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1185           {
1186             poly_int64 hwasan_orig_offset;
1187             if (hwasan_sanitize_stack_p ())
1188               {
1189                 /* There must be no tag granule "shared" between different
1190                      objects.  This means that no HWASAN_TAG_GRANULE_SIZE byte
1191                      chunk can have more than one object in it.
1192 
1193                      We ensure this by forcing the end of the last bit of data to
1194                      be aligned to HWASAN_TAG_GRANULE_SIZE bytes here, and setting
1195                      the start of each variable to be aligned to
1196                      HWASAN_TAG_GRANULE_SIZE bytes in `align_local_variable`.
1197 
1198                      We can't align just one of the start or end, since there are
1199                      untagged things stored on the stack which we do not align to
1200                      HWASAN_TAG_GRANULE_SIZE bytes.  If we only aligned the start
1201                      or the end of tagged objects then untagged objects could end
1202                      up sharing the first granule of a tagged object or sharing the
1203                      last granule of a tagged object respectively.  */
1204                 hwasan_orig_offset = align_frame_offset (HWASAN_TAG_GRANULE_SIZE);
1205                 gcc_assert (stack_vars[i].alignb >= HWASAN_TAG_GRANULE_SIZE);
1206               }
1207             /* ASAN description strings don't yet have a syntax for expressing
1208                polynomial offsets.  */
1209             HOST_WIDE_INT prev_offset;
1210             if (asan_sanitize_stack_p ()
1211                 && pred
1212                 && frame_offset.is_constant (&prev_offset)
1213                 && stack_vars[i].size.is_constant ())
1214               {
1215                 if (data->asan_vec.is_empty ())
1216                     {
1217                       align_frame_offset (ASAN_RED_ZONE_SIZE);
1218                       prev_offset = frame_offset.to_constant ();
1219                     }
1220                 prev_offset = align_base (prev_offset,
1221                                                   ASAN_MIN_RED_ZONE_SIZE,
1222                                                   !FRAME_GROWS_DOWNWARD);
1223                 tree repr_decl = NULL_TREE;
1224                 unsigned HOST_WIDE_INT size
1225                     = asan_var_and_redzone_size (stack_vars[i].size.to_constant ());
1226                 if (data->asan_vec.is_empty ())
1227                     size = MAX (size, ASAN_RED_ZONE_SIZE);
1228 
1229                 unsigned HOST_WIDE_INT alignment = MAX (alignb,
1230                                                                   ASAN_MIN_RED_ZONE_SIZE);
1231                 offset = alloc_stack_frame_space (size, alignment);
1232 
1233                 data->asan_vec.safe_push (prev_offset);
1234                 /* Allocating a constant amount of space from a constant
1235                      starting offset must give a constant result.  */
1236                 data->asan_vec.safe_push ((offset + stack_vars[i].size)
1237                                                   .to_constant ());
1238                 /* Find best representative of the partition.
1239                      Prefer those with DECL_NAME, even better
1240                      satisfying asan_protect_stack_decl predicate.  */
1241                 for (j = i; j != EOC; j = stack_vars[j].next)
1242                     if (asan_protect_stack_decl (stack_vars[j].decl)
1243                         && DECL_NAME (stack_vars[j].decl))
1244                       {
1245                         repr_decl = stack_vars[j].decl;
1246                         break;
1247                       }
1248                     else if (repr_decl == NULL_TREE
1249                                && DECL_P (stack_vars[j].decl)
1250                                && DECL_NAME (stack_vars[j].decl))
1251                       repr_decl = stack_vars[j].decl;
1252                 if (repr_decl == NULL_TREE)
1253                     repr_decl = stack_vars[i].decl;
1254                 data->asan_decl_vec.safe_push (repr_decl);
1255 
1256                 /* Make sure a representative is unpoison if another
1257                      variable in the partition is handled by
1258                      use-after-scope sanitization.  */
1259                 if (asan_handled_variables != NULL
1260                       && !asan_handled_variables->contains (repr_decl))
1261                     {
1262                       for (j = i; j != EOC; j = stack_vars[j].next)
1263                         if (asan_handled_variables->contains (stack_vars[j].decl))
1264                           break;
1265                       if (j != EOC)
1266                         asan_handled_variables->add (repr_decl);
1267                     }
1268 
1269                 data->asan_alignb = MAX (data->asan_alignb, alignb);
1270                 if (data->asan_base == NULL)
1271                     data->asan_base = gen_reg_rtx (Pmode);
1272                 base = data->asan_base;
1273 
1274                 if (!STRICT_ALIGNMENT)
1275                     base_align = crtl->max_used_stack_slot_alignment;
1276                 else
1277                     base_align = MAX (crtl->max_used_stack_slot_alignment,
1278                                           GET_MODE_ALIGNMENT (SImode)
1279                                           << ASAN_SHADOW_SHIFT);
1280               }
1281             else
1282               {
1283                 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1284                 base_align = crtl->max_used_stack_slot_alignment;
1285 
1286                 if (hwasan_sanitize_stack_p ())
1287                     {
1288                       /* Align again since the point of this alignment is to handle
1289                          the "end" of the object (i.e. smallest address after the
1290                          stack object).  For FRAME_GROWS_DOWNWARD that requires
1291                          aligning the stack before allocating, but for a frame that
1292                          grows upwards that requires aligning the stack after
1293                          allocation.
1294 
1295                          Use `frame_offset` to record the offset value rather than
1296                          `offset` since the `frame_offset` describes the extent
1297                          allocated for this particular variable while `offset`
1298                          describes the address that this variable starts at.  */
1299                       align_frame_offset (HWASAN_TAG_GRANULE_SIZE);
1300                       hwasan_record_stack_var (virtual_stack_vars_rtx, base,
1301                                                      hwasan_orig_offset, frame_offset);
1302                     }
1303               }
1304           }
1305       else
1306           {
1307             /* Large alignment is only processed in the last pass.  */
1308             if (pred)
1309               continue;
1310 
1311             /* If there were any variables requiring "large" alignment, allocate
1312                space.  */
1313             if (maybe_ne (large_size, 0U) && ! large_allocation_done)
1314               {
1315                 poly_int64 loffset;
1316                 rtx large_allocsize;
1317 
1318                 large_allocsize = gen_int_mode (large_size, Pmode);
1319                 get_dynamic_stack_size (&large_allocsize, 0, large_align, NULL);
1320                 loffset = alloc_stack_frame_space
1321                     (rtx_to_poly_int64 (large_allocsize),
1322                      PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
1323                 large_base = get_dynamic_stack_base (loffset, large_align, base);
1324                 large_allocation_done = true;
1325               }
1326 
1327             gcc_assert (large_base != NULL);
1328             large_alloc = aligned_upper_bound (large_alloc, alignb);
1329             offset = large_alloc;
1330             large_alloc += stack_vars[i].size;
1331             if (hwasan_sanitize_stack_p ())
1332               {
1333                 /* An object with a large alignment requirement means that the
1334                      alignment requirement is greater than the required alignment
1335                      for tags.  */
1336                 if (!large_untagged_base)
1337                     large_untagged_base
1338                       = targetm.memtag.untagged_pointer (large_base, NULL_RTX);
1339                 /* Ensure the end of the variable is also aligned correctly.  */
1340                 poly_int64 align_again
1341                     = aligned_upper_bound (large_alloc, HWASAN_TAG_GRANULE_SIZE);
1342                 /* For large allocations we always allocate a chunk of space
1343                      (which is addressed by large_untagged_base/large_base) and
1344                      then use positive offsets from that.  Hence the farthest
1345                      offset is `align_again` and the nearest offset from the base
1346                      is `offset`.  */
1347                 hwasan_record_stack_var (large_untagged_base, large_base,
1348                                                offset, align_again);
1349               }
1350 
1351             base = large_base;
1352             base_align = large_align;
1353           }
1354 
1355       /* Create rtl for each variable based on their location within the
1356            partition.  */
1357       for (j = i; j != EOC; j = stack_vars[j].next)
1358           {
1359             expand_one_stack_var_at (stack_vars[j].decl,
1360                                            base, base_align, offset);
1361           }
1362       if (hwasan_sanitize_stack_p ())
1363           hwasan_increment_frame_tag ();
1364     }
1365 
1366   gcc_assert (known_eq (large_alloc, large_size));
1367 }
1368 
1369 /* Take into account all sizes of partitions and reset DECL_RTLs.  */
1370 static poly_uint64
account_stack_vars(void)1371 account_stack_vars (void)
1372 {
1373   size_t si, j, i, n = stack_vars_num;
1374   poly_uint64 size = 0;
1375 
1376   for (si = 0; si < n; ++si)
1377     {
1378       i = stack_vars_sorted[si];
1379 
1380       /* Skip variables that aren't partition representatives, for now.  */
1381       if (stack_vars[i].representative != i)
1382           continue;
1383 
1384       size += stack_vars[i].size;
1385       for (j = i; j != EOC; j = stack_vars[j].next)
1386           set_rtl (stack_vars[j].decl, NULL);
1387     }
1388   return size;
1389 }
1390 
1391 /* Record the RTL assignment X for the default def of PARM.  */
1392 
1393 extern void
set_parm_rtl(tree parm,rtx x)1394 set_parm_rtl (tree parm, rtx x)
1395 {
1396   gcc_assert (TREE_CODE (parm) == PARM_DECL
1397                 || TREE_CODE (parm) == RESULT_DECL);
1398 
1399   if (x && !MEM_P (x))
1400     {
1401       unsigned int align = MINIMUM_ALIGNMENT (TREE_TYPE (parm),
1402                                                         TYPE_MODE (TREE_TYPE (parm)),
1403                                                         TYPE_ALIGN (TREE_TYPE (parm)));
1404 
1405       /* If the variable alignment is very large we'll dynamicaly
1406            allocate it, which means that in-frame portion is just a
1407            pointer.  ??? We've got a pseudo for sure here, do we
1408            actually dynamically allocate its spilling area if needed?
1409            ??? Isn't it a problem when Pmode alignment also exceeds
1410            MAX_SUPPORTED_STACK_ALIGNMENT, as can happen on cris and lm32?  */
1411       if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1412           align = GET_MODE_ALIGNMENT (Pmode);
1413 
1414       record_alignment_for_reg_var (align);
1415     }
1416 
1417   tree ssa = ssa_default_def (cfun, parm);
1418   if (!ssa)
1419     return set_rtl (parm, x);
1420 
1421   int part = var_to_partition (SA.map, ssa);
1422   gcc_assert (part != NO_PARTITION);
1423 
1424   bool changed = bitmap_bit_p (SA.partitions_for_parm_default_defs, part);
1425   gcc_assert (changed);
1426 
1427   set_rtl (ssa, x);
1428   gcc_assert (DECL_RTL (parm) == x);
1429 }
1430 
1431 /* A subroutine of expand_one_var.  Called to immediately assign rtl
1432    to a variable to be allocated in the stack frame.  */
1433 
1434 static void
expand_one_stack_var_1(tree var)1435 expand_one_stack_var_1 (tree var)
1436 {
1437   poly_uint64 size;
1438   poly_int64 offset;
1439   unsigned byte_align;
1440 
1441   if (TREE_CODE (var) == SSA_NAME)
1442     {
1443       tree type = TREE_TYPE (var);
1444       size = tree_to_poly_uint64 (TYPE_SIZE_UNIT (type));
1445     }
1446   else
1447     size = tree_to_poly_uint64 (DECL_SIZE_UNIT (var));
1448 
1449   byte_align = align_local_variable (var, true);
1450 
1451   /* We handle highly aligned variables in expand_stack_vars.  */
1452   gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1453 
1454   rtx base;
1455   if (hwasan_sanitize_stack_p ())
1456     {
1457       /* Allocate zero bytes to align the stack.  */
1458       poly_int64 hwasan_orig_offset
1459           = align_frame_offset (HWASAN_TAG_GRANULE_SIZE);
1460       offset = alloc_stack_frame_space (size, byte_align);
1461       align_frame_offset (HWASAN_TAG_GRANULE_SIZE);
1462       base = hwasan_frame_base ();
1463       /* Use `frame_offset` to automatically account for machines where the
1464            frame grows upwards.
1465 
1466            `offset` will always point to the "start" of the stack object, which
1467            will be the smallest address, for ! FRAME_GROWS_DOWNWARD this is *not*
1468            the "furthest" offset from the base delimiting the current stack
1469            object.  `frame_offset` will always delimit the extent that the frame.
1470            */
1471       hwasan_record_stack_var (virtual_stack_vars_rtx, base,
1472                                      hwasan_orig_offset, frame_offset);
1473     }
1474   else
1475     {
1476       offset = alloc_stack_frame_space (size, byte_align);
1477       base = virtual_stack_vars_rtx;
1478     }
1479 
1480   expand_one_stack_var_at (var, base,
1481                                  crtl->max_used_stack_slot_alignment, offset);
1482 
1483   if (hwasan_sanitize_stack_p ())
1484     hwasan_increment_frame_tag ();
1485 }
1486 
1487 /* Wrapper for expand_one_stack_var_1 that checks SSA_NAMEs are
1488    already assigned some MEM.  */
1489 
1490 static void
expand_one_stack_var(tree var)1491 expand_one_stack_var (tree var)
1492 {
1493   if (TREE_CODE (var) == SSA_NAME)
1494     {
1495       int part = var_to_partition (SA.map, var);
1496       if (part != NO_PARTITION)
1497           {
1498             rtx x = SA.partition_to_pseudo[part];
1499             gcc_assert (x);
1500             gcc_assert (MEM_P (x));
1501             return;
1502           }
1503     }
1504 
1505   return expand_one_stack_var_1 (var);
1506 }
1507 
1508 /* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL
1509    that will reside in a hard register.  */
1510 
1511 static void
expand_one_hard_reg_var(tree var)1512 expand_one_hard_reg_var (tree var)
1513 {
1514   rest_of_decl_compilation (var, 0, 0);
1515 }
1516 
1517 /* Record the alignment requirements of some variable assigned to a
1518    pseudo.  */
1519 
1520 static void
record_alignment_for_reg_var(unsigned int align)1521 record_alignment_for_reg_var (unsigned int align)
1522 {
1523   if (SUPPORTS_STACK_ALIGNMENT
1524       && crtl->stack_alignment_estimated < align)
1525     {
1526       /* stack_alignment_estimated shouldn't change after stack
1527          realign decision made */
1528       gcc_assert (!crtl->stack_realign_processed);
1529       crtl->stack_alignment_estimated = align;
1530     }
1531 
1532   /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1533      So here we only make sure stack_alignment_needed >= align.  */
1534   if (crtl->stack_alignment_needed < align)
1535     crtl->stack_alignment_needed = align;
1536   if (crtl->max_used_stack_slot_alignment < align)
1537     crtl->max_used_stack_slot_alignment = align;
1538 }
1539 
1540 /* Create RTL for an SSA partition.  */
1541 
1542 static void
expand_one_ssa_partition(tree var)1543 expand_one_ssa_partition (tree var)
1544 {
1545   int part = var_to_partition (SA.map, var);
1546   gcc_assert (part != NO_PARTITION);
1547 
1548   if (SA.partition_to_pseudo[part])
1549     return;
1550 
1551   unsigned int align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1552                                                     TYPE_MODE (TREE_TYPE (var)),
1553                                                     TYPE_ALIGN (TREE_TYPE (var)));
1554 
1555   /* If the variable alignment is very large we'll dynamicaly allocate
1556      it, which means that in-frame portion is just a pointer.  */
1557   if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1558     align = GET_MODE_ALIGNMENT (Pmode);
1559 
1560   record_alignment_for_reg_var (align);
1561 
1562   if (!use_register_for_decl (var))
1563     {
1564       if (defer_stack_allocation (var, true))
1565           add_stack_var (var, true);
1566       else
1567           expand_one_stack_var_1 (var);
1568       return;
1569     }
1570 
1571   machine_mode reg_mode = promote_ssa_mode (var, NULL);
1572   rtx x = gen_reg_rtx (reg_mode);
1573 
1574   set_rtl (var, x);
1575 
1576   /* For a promoted variable, X will not be used directly but wrapped in a
1577      SUBREG with SUBREG_PROMOTED_VAR_P set, which means that the RTL land
1578      will assume that its upper bits can be inferred from its lower bits.
1579      Therefore, if X isn't initialized on every path from the entry, then
1580      we must do it manually in order to fulfill the above assumption.  */
1581   if (reg_mode != TYPE_MODE (TREE_TYPE (var))
1582       && bitmap_bit_p (SA.partitions_for_undefined_values, part))
1583     emit_move_insn (x, CONST0_RTX (reg_mode));
1584 }
1585 
1586 /* Record the association between the RTL generated for partition PART
1587    and the underlying variable of the SSA_NAME VAR.  */
1588 
1589 static void
adjust_one_expanded_partition_var(tree var)1590 adjust_one_expanded_partition_var (tree var)
1591 {
1592   if (!var)
1593     return;
1594 
1595   tree decl = SSA_NAME_VAR (var);
1596 
1597   int part = var_to_partition (SA.map, var);
1598   if (part == NO_PARTITION)
1599     return;
1600 
1601   rtx x = SA.partition_to_pseudo[part];
1602 
1603   gcc_assert (x);
1604 
1605   set_rtl (var, x);
1606 
1607   if (!REG_P (x))
1608     return;
1609 
1610   /* Note if the object is a user variable.  */
1611   if (decl && !DECL_ARTIFICIAL (decl))
1612     mark_user_reg (x);
1613 
1614   if (POINTER_TYPE_P (decl ? TREE_TYPE (decl) : TREE_TYPE (var)))
1615     mark_reg_pointer (x, get_pointer_alignment (var));
1616 }
1617 
1618 /* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL
1619    that will reside in a pseudo register.  */
1620 
1621 static void
expand_one_register_var(tree var)1622 expand_one_register_var (tree var)
1623 {
1624   if (TREE_CODE (var) == SSA_NAME)
1625     {
1626       int part = var_to_partition (SA.map, var);
1627       if (part != NO_PARTITION)
1628           {
1629             rtx x = SA.partition_to_pseudo[part];
1630             gcc_assert (x);
1631             gcc_assert (REG_P (x));
1632             return;
1633           }
1634       gcc_unreachable ();
1635     }
1636 
1637   tree decl = var;
1638   tree type = TREE_TYPE (decl);
1639   machine_mode reg_mode = promote_decl_mode (decl, NULL);
1640   rtx x = gen_reg_rtx (reg_mode);
1641 
1642   set_rtl (var, x);
1643 
1644   /* Note if the object is a user variable.  */
1645   if (!DECL_ARTIFICIAL (decl))
1646     mark_user_reg (x);
1647 
1648   if (POINTER_TYPE_P (type))
1649     mark_reg_pointer (x, get_pointer_alignment (var));
1650 }
1651 
1652 /* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL that
1653    has some associated error, e.g. its type is error-mark.  We just need
1654    to pick something that won't crash the rest of the compiler.  */
1655 
1656 static void
expand_one_error_var(tree var)1657 expand_one_error_var (tree var)
1658 {
1659   machine_mode mode = DECL_MODE (var);
1660   rtx x;
1661 
1662   if (mode == BLKmode)
1663     x = gen_rtx_MEM (BLKmode, const0_rtx);
1664   else if (mode == VOIDmode)
1665     x = const0_rtx;
1666   else
1667     x = gen_reg_rtx (mode);
1668 
1669   SET_DECL_RTL (var, x);
1670 }
1671 
1672 /* A subroutine of expand_one_var.  VAR is a variable that will be
1673    allocated to the local stack frame.  Return true if we wish to
1674    add VAR to STACK_VARS so that it will be coalesced with other
1675    variables.  Return false to allocate VAR immediately.
1676 
1677    This function is used to reduce the number of variables considered
1678    for coalescing, which reduces the size of the quadratic problem.  */
1679 
1680 static bool
defer_stack_allocation(tree var,bool toplevel)1681 defer_stack_allocation (tree var, bool toplevel)
1682 {
1683   tree size_unit = TREE_CODE (var) == SSA_NAME
1684     ? TYPE_SIZE_UNIT (TREE_TYPE (var))
1685     : DECL_SIZE_UNIT (var);
1686   poly_uint64 size;
1687 
1688   /* Whether the variable is small enough for immediate allocation not to be
1689      a problem with regard to the frame size.  */
1690   bool smallish
1691     = (poly_int_tree_p (size_unit, &size)
1692        && (estimated_poly_value (size)
1693              < param_min_size_for_stack_sharing));
1694 
1695   /* If stack protection is enabled, *all* stack variables must be deferred,
1696      so that we can re-order the strings to the top of the frame.
1697      Similarly for Address Sanitizer.  */
1698   if (flag_stack_protect || asan_sanitize_stack_p ())
1699     return true;
1700 
1701   unsigned int align = TREE_CODE (var) == SSA_NAME
1702     ? TYPE_ALIGN (TREE_TYPE (var))
1703     : DECL_ALIGN (var);
1704 
1705   /* We handle "large" alignment via dynamic allocation.  We want to handle
1706      this extra complication in only one place, so defer them.  */
1707   if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1708     return true;
1709 
1710   bool ignored = TREE_CODE (var) == SSA_NAME
1711     ? !SSAVAR (var) || DECL_IGNORED_P (SSA_NAME_VAR (var))
1712     : DECL_IGNORED_P (var);
1713 
1714   /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1715      might be detached from their block and appear at toplevel when we reach
1716      here.  We want to coalesce them with variables from other blocks when
1717      the immediate contribution to the frame size would be noticeable.  */
1718   if (toplevel && optimize > 0 && ignored && !smallish)
1719     return true;
1720 
1721   /* Variables declared in the outermost scope automatically conflict
1722      with every other variable.  The only reason to want to defer them
1723      at all is that, after sorting, we can more efficiently pack
1724      small variables in the stack frame.  Continue to defer at -O2.  */
1725   if (toplevel && optimize < 2)
1726     return false;
1727 
1728   /* Without optimization, *most* variables are allocated from the
1729      stack, which makes the quadratic problem large exactly when we
1730      want compilation to proceed as quickly as possible.  On the
1731      other hand, we don't want the function's stack frame size to
1732      get completely out of hand.  So we avoid adding scalars and
1733      "small" aggregates to the list at all.  */
1734   if (optimize == 0 && smallish)
1735     return false;
1736 
1737   return true;
1738 }
1739 
1740 /* A subroutine of expand_used_vars.  Expand one variable according to
1741    its flavor.  Variables to be placed on the stack are not actually
1742    expanded yet, merely recorded.
1743    When REALLY_EXPAND is false, only add stack values to be allocated.
1744    Return stack usage this variable is supposed to take.
1745 */
1746 
1747 static poly_uint64
expand_one_var(tree var,bool toplevel,bool really_expand,bitmap forced_stack_var=NULL)1748 expand_one_var (tree var, bool toplevel, bool really_expand,
1749                     bitmap forced_stack_var = NULL)
1750 {
1751   unsigned int align = BITS_PER_UNIT;
1752   tree origvar = var;
1753 
1754   var = SSAVAR (var);
1755 
1756   if (TREE_TYPE (var) != error_mark_node && VAR_P (var))
1757     {
1758       if (is_global_var (var))
1759           return 0;
1760 
1761       /* Because we don't know if VAR will be in register or on stack,
1762            we conservatively assume it will be on stack even if VAR is
1763            eventually put into register after RA pass.  For non-automatic
1764            variables, which won't be on stack, we collect alignment of
1765            type and ignore user specified alignment.  Similarly for
1766            SSA_NAMEs for which use_register_for_decl returns true.  */
1767       if (TREE_STATIC (var)
1768             || DECL_EXTERNAL (var)
1769             || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1770           align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1771                                            TYPE_MODE (TREE_TYPE (var)),
1772                                            TYPE_ALIGN (TREE_TYPE (var)));
1773       else if (DECL_HAS_VALUE_EXPR_P (var)
1774                  || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1775           /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1776              or variables which were assigned a stack slot already by
1777              expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1778              changed from the offset chosen to it.  */
1779           align = crtl->stack_alignment_estimated;
1780       else
1781           align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1782 
1783       /* If the variable alignment is very large we'll dynamicaly allocate
1784            it, which means that in-frame portion is just a pointer.  */
1785       if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1786           align = GET_MODE_ALIGNMENT (Pmode);
1787     }
1788 
1789   record_alignment_for_reg_var (align);
1790 
1791   poly_uint64 size;
1792   if (TREE_CODE (origvar) == SSA_NAME)
1793     {
1794       gcc_assert (!VAR_P (var)
1795                       || (!DECL_EXTERNAL (var)
1796                           && !DECL_HAS_VALUE_EXPR_P (var)
1797                           && !TREE_STATIC (var)
1798                           && TREE_TYPE (var) != error_mark_node
1799                           && !DECL_HARD_REGISTER (var)
1800                           && really_expand));
1801     }
1802   if (!VAR_P (var) && TREE_CODE (origvar) != SSA_NAME)
1803     ;
1804   else if (DECL_EXTERNAL (var))
1805     ;
1806   else if (DECL_HAS_VALUE_EXPR_P (var))
1807     ;
1808   else if (TREE_STATIC (var))
1809     ;
1810   else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1811     ;
1812   else if (TREE_TYPE (var) == error_mark_node)
1813     {
1814       if (really_expand)
1815         expand_one_error_var (var);
1816     }
1817   else if (VAR_P (var) && DECL_HARD_REGISTER (var))
1818     {
1819       if (really_expand)
1820           {
1821             expand_one_hard_reg_var (var);
1822             if (!DECL_HARD_REGISTER (var))
1823               /* Invalid register specification.  */
1824               expand_one_error_var (var);
1825           }
1826     }
1827   else if (use_register_for_decl (var)
1828              && (!forced_stack_var
1829                  || !bitmap_bit_p (forced_stack_var, DECL_UID (var))))
1830     {
1831       if (really_expand)
1832         expand_one_register_var (origvar);
1833     }
1834   else if (!poly_int_tree_p (DECL_SIZE_UNIT (var), &size)
1835              || !valid_constant_size_p (DECL_SIZE_UNIT (var)))
1836     {
1837       /* Reject variables which cover more than half of the address-space.  */
1838       if (really_expand)
1839           {
1840             if (DECL_NONLOCAL_FRAME (var))
1841               error_at (DECL_SOURCE_LOCATION (current_function_decl),
1842                           "total size of local objects is too large");
1843             else
1844               error_at (DECL_SOURCE_LOCATION (var),
1845                           "size of variable %q+D is too large", var);
1846             expand_one_error_var (var);
1847           }
1848     }
1849   else if (defer_stack_allocation (var, toplevel))
1850     add_stack_var (origvar, really_expand);
1851   else
1852     {
1853       if (really_expand)
1854         {
1855           if (lookup_attribute ("naked",
1856                                 DECL_ATTRIBUTES (current_function_decl)))
1857               error ("cannot allocate stack for variable %q+D, naked function",
1858                    var);
1859 
1860           expand_one_stack_var (origvar);
1861         }
1862       return size;
1863     }
1864   return 0;
1865 }
1866 
1867 /* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
1868    expanding variables.  Those variables that can be put into registers
1869    are allocated pseudos; those that can't are put on the stack.
1870 
1871    TOPLEVEL is true if this is the outermost BLOCK.  */
1872 
1873 static void
expand_used_vars_for_block(tree block,bool toplevel,bitmap forced_stack_vars)1874 expand_used_vars_for_block (tree block, bool toplevel, bitmap forced_stack_vars)
1875 {
1876   tree t;
1877 
1878   /* Expand all variables at this level.  */
1879   for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1880     if (TREE_USED (t)
1881         && ((!VAR_P (t) && TREE_CODE (t) != RESULT_DECL)
1882               || !DECL_NONSHAREABLE (t)))
1883       expand_one_var (t, toplevel, true, forced_stack_vars);
1884 
1885   /* Expand all variables at containing levels.  */
1886   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1887     expand_used_vars_for_block (t, false, forced_stack_vars);
1888 }
1889 
1890 /* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
1891    and clear TREE_USED on all local variables.  */
1892 
1893 static void
clear_tree_used(tree block)1894 clear_tree_used (tree block)
1895 {
1896   tree t;
1897 
1898   for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1899     /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1900     if ((!VAR_P (t) && TREE_CODE (t) != RESULT_DECL)
1901           || !DECL_NONSHAREABLE (t))
1902       TREE_USED (t) = 0;
1903 
1904   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1905     clear_tree_used (t);
1906 }
1907 
1908 /* Examine TYPE and determine a bit mask of the following features.  */
1909 
1910 #define SPCT_HAS_LARGE_CHAR_ARRAY       1
1911 #define SPCT_HAS_SMALL_CHAR_ARRAY       2
1912 #define SPCT_HAS_ARRAY                            4
1913 #define SPCT_HAS_AGGREGATE              8
1914 
1915 static unsigned int
stack_protect_classify_type(tree type)1916 stack_protect_classify_type (tree type)
1917 {
1918   unsigned int ret = 0;
1919   tree t;
1920 
1921   switch (TREE_CODE (type))
1922     {
1923     case ARRAY_TYPE:
1924       t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1925       if (t == char_type_node
1926             || t == signed_char_type_node
1927             || t == unsigned_char_type_node)
1928           {
1929             unsigned HOST_WIDE_INT max = param_ssp_buffer_size;
1930             unsigned HOST_WIDE_INT len;
1931 
1932             if (!TYPE_SIZE_UNIT (type)
1933                 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1934               len = max;
1935             else
1936               len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1937 
1938             if (len == 0)
1939               ret = SPCT_HAS_ARRAY;
1940             else if (len < max)
1941               ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1942             else
1943               ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1944           }
1945       else
1946           ret = SPCT_HAS_ARRAY;
1947       break;
1948 
1949     case UNION_TYPE:
1950     case QUAL_UNION_TYPE:
1951     case RECORD_TYPE:
1952       ret = SPCT_HAS_AGGREGATE;
1953       for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1954           if (TREE_CODE (t) == FIELD_DECL)
1955             ret |= stack_protect_classify_type (TREE_TYPE (t));
1956       break;
1957 
1958     default:
1959       break;
1960     }
1961 
1962   return ret;
1963 }
1964 
1965 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1966    part of the local stack frame.  Remember if we ever return nonzero for
1967    any variable in this function.  The return value is the phase number in
1968    which the variable should be allocated.  */
1969 
1970 static int
stack_protect_decl_phase(tree decl)1971 stack_protect_decl_phase (tree decl)
1972 {
1973   unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1974   int ret = 0;
1975 
1976   if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1977     has_short_buffer = true;
1978 
1979   tree attribs = DECL_ATTRIBUTES (current_function_decl);
1980   if (!lookup_attribute ("no_stack_protector", attribs)
1981       && (flag_stack_protect == SPCT_FLAG_ALL
1982             || flag_stack_protect == SPCT_FLAG_STRONG
1983             || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1984                 && lookup_attribute ("stack_protect", attribs))))
1985     {
1986       if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1987             && !(bits & SPCT_HAS_AGGREGATE))
1988           ret = 1;
1989       else if (bits & SPCT_HAS_ARRAY)
1990           ret = 2;
1991     }
1992   else
1993     ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1994 
1995   if (ret)
1996     has_protected_decls = true;
1997 
1998   return ret;
1999 }
2000 
2001 /* Two helper routines that check for phase 1 and phase 2.  These are used
2002    as callbacks for expand_stack_vars.  */
2003 
2004 static bool
stack_protect_decl_phase_1(size_t i)2005 stack_protect_decl_phase_1 (size_t i)
2006 {
2007   return stack_protect_decl_phase (stack_vars[i].decl) == 1;
2008 }
2009 
2010 static bool
stack_protect_decl_phase_2(size_t i)2011 stack_protect_decl_phase_2 (size_t i)
2012 {
2013   return stack_protect_decl_phase (stack_vars[i].decl) == 2;
2014 }
2015 
2016 /* And helper function that checks for asan phase (with stack protector
2017    it is phase 3).  This is used as callback for expand_stack_vars.
2018    Returns true if any of the vars in the partition need to be protected.  */
2019 
2020 static bool
asan_decl_phase_3(size_t i)2021 asan_decl_phase_3 (size_t i)
2022 {
2023   while (i != EOC)
2024     {
2025       if (asan_protect_stack_decl (stack_vars[i].decl))
2026           return true;
2027       i = stack_vars[i].next;
2028     }
2029   return false;
2030 }
2031 
2032 /* Ensure that variables in different stack protection phases conflict
2033    so that they are not merged and share the same stack slot.
2034    Return true if there are any address taken variables.  */
2035 
2036 static bool
add_stack_protection_conflicts(void)2037 add_stack_protection_conflicts (void)
2038 {
2039   size_t i, j, n = stack_vars_num;
2040   unsigned char *phase;
2041   bool ret = false;
2042 
2043   phase = XNEWVEC (unsigned char, n);
2044   for (i = 0; i < n; ++i)
2045     {
2046       phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
2047       if (TREE_ADDRESSABLE (stack_vars[i].decl))
2048           ret = true;
2049     }
2050 
2051   for (i = 0; i < n; ++i)
2052     {
2053       unsigned char ph_i = phase[i];
2054       for (j = i + 1; j < n; ++j)
2055           if (ph_i != phase[j])
2056             add_stack_var_conflict (i, j);
2057     }
2058 
2059   XDELETEVEC (phase);
2060   return ret;
2061 }
2062 
2063 /* Create a decl for the guard at the top of the stack frame.  */
2064 
2065 static void
create_stack_guard(void)2066 create_stack_guard (void)
2067 {
2068   tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
2069                                  VAR_DECL, NULL, ptr_type_node);
2070   TREE_THIS_VOLATILE (guard) = 1;
2071   TREE_USED (guard) = 1;
2072   expand_one_stack_var (guard);
2073   crtl->stack_protect_guard = guard;
2074 }
2075 
2076 /* Prepare for expanding variables.  */
2077 static void
init_vars_expansion(void)2078 init_vars_expansion (void)
2079 {
2080   /* Conflict bitmaps, and a few related temporary bitmaps, go here.  */
2081   bitmap_obstack_initialize (&stack_var_bitmap_obstack);
2082 
2083   /* A map from decl to stack partition.  */
2084   decl_to_stack_part = new hash_map<tree, size_t>;
2085 
2086   /* Initialize local stack smashing state.  */
2087   has_protected_decls = false;
2088   has_short_buffer = false;
2089   if (hwasan_sanitize_stack_p ())
2090     hwasan_record_frame_init ();
2091 }
2092 
2093 /* Free up stack variable graph data.  */
2094 static void
fini_vars_expansion(void)2095 fini_vars_expansion (void)
2096 {
2097   bitmap_obstack_release (&stack_var_bitmap_obstack);
2098   if (stack_vars)
2099     XDELETEVEC (stack_vars);
2100   if (stack_vars_sorted)
2101     XDELETEVEC (stack_vars_sorted);
2102   stack_vars = NULL;
2103   stack_vars_sorted = NULL;
2104   stack_vars_alloc = stack_vars_num = 0;
2105   delete decl_to_stack_part;
2106   decl_to_stack_part = NULL;
2107 }
2108 
2109 /* Make a fair guess for the size of the stack frame of the function
2110    in NODE.  This doesn't have to be exact, the result is only used in
2111    the inline heuristics.  So we don't want to run the full stack var
2112    packing algorithm (which is quadratic in the number of stack vars).
2113    Instead, we calculate the total size of all stack vars.  This turns
2114    out to be a pretty fair estimate -- packing of stack vars doesn't
2115    happen very often.  */
2116 
2117 HOST_WIDE_INT
estimated_stack_frame_size(struct cgraph_node * node)2118 estimated_stack_frame_size (struct cgraph_node *node)
2119 {
2120   poly_int64 size = 0;
2121   size_t i;
2122   tree var;
2123   struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
2124 
2125   push_cfun (fn);
2126 
2127   init_vars_expansion ();
2128 
2129   FOR_EACH_LOCAL_DECL (fn, i, var)
2130     if (auto_var_in_fn_p (var, fn->decl))
2131       size += expand_one_var (var, true, false);
2132 
2133   if (stack_vars_num > 0)
2134     {
2135       /* Fake sorting the stack vars for account_stack_vars ().  */
2136       stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
2137       for (i = 0; i < stack_vars_num; ++i)
2138           stack_vars_sorted[i] = i;
2139       size += account_stack_vars ();
2140     }
2141 
2142   fini_vars_expansion ();
2143   pop_cfun ();
2144   return estimated_poly_value (size);
2145 }
2146 
2147 /* Check if the current function has calls that use a return slot.  */
2148 
2149 static bool
stack_protect_return_slot_p()2150 stack_protect_return_slot_p ()
2151 {
2152   basic_block bb;
2153 
2154   FOR_ALL_BB_FN (bb, cfun)
2155     for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
2156            !gsi_end_p (gsi); gsi_next (&gsi))
2157       {
2158           gimple *stmt = gsi_stmt (gsi);
2159           /* This assumes that calls to internal-only functions never
2160              use a return slot.  */
2161           if (is_gimple_call (stmt)
2162               && !gimple_call_internal_p (stmt)
2163               && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
2164                                           gimple_call_fndecl (stmt)))
2165             return true;
2166       }
2167   return false;
2168 }
2169 
2170 /* Expand all variables used in the function.  */
2171 
2172 static rtx_insn *
expand_used_vars(bitmap forced_stack_vars)2173 expand_used_vars (bitmap forced_stack_vars)
2174 {
2175   tree var, outer_block = DECL_INITIAL (current_function_decl);
2176   auto_vec<tree> maybe_local_decls;
2177   rtx_insn *var_end_seq = NULL;
2178   unsigned i;
2179   unsigned len;
2180   bool gen_stack_protect_signal = false;
2181 
2182   /* Compute the phase of the stack frame for this function.  */
2183   {
2184     int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2185     int off = targetm.starting_frame_offset () % align;
2186     frame_phase = off ? align - off : 0;
2187   }
2188 
2189   /* Set TREE_USED on all variables in the local_decls.  */
2190   FOR_EACH_LOCAL_DECL (cfun, i, var)
2191     TREE_USED (var) = 1;
2192   /* Clear TREE_USED on all variables associated with a block scope.  */
2193   clear_tree_used (DECL_INITIAL (current_function_decl));
2194 
2195   init_vars_expansion ();
2196 
2197   if (targetm.use_pseudo_pic_reg ())
2198     pic_offset_table_rtx = gen_reg_rtx (Pmode);
2199 
2200   for (i = 0; i < SA.map->num_partitions; i++)
2201     {
2202       if (bitmap_bit_p (SA.partitions_for_parm_default_defs, i))
2203           continue;
2204 
2205       tree var = partition_to_var (SA.map, i);
2206 
2207       gcc_assert (!virtual_operand_p (var));
2208 
2209       expand_one_ssa_partition (var);
2210     }
2211 
2212   if (flag_stack_protect == SPCT_FLAG_STRONG)
2213     gen_stack_protect_signal = stack_protect_return_slot_p ();
2214 
2215   /* At this point all variables on the local_decls with TREE_USED
2216      set are not associated with any block scope.  Lay them out.  */
2217 
2218   len = vec_safe_length (cfun->local_decls);
2219   FOR_EACH_LOCAL_DECL (cfun, i, var)
2220     {
2221       bool expand_now = false;
2222 
2223       /* Expanded above already.  */
2224       if (is_gimple_reg (var))
2225           {
2226             TREE_USED (var) = 0;
2227             goto next;
2228           }
2229       /* We didn't set a block for static or extern because it's hard
2230            to tell the difference between a global variable (re)declared
2231            in a local scope, and one that's really declared there to
2232            begin with.  And it doesn't really matter much, since we're
2233            not giving them stack space.  Expand them now.  */
2234       else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
2235           expand_now = true;
2236 
2237       /* Expand variables not associated with any block now.  Those created by
2238            the optimizers could be live anywhere in the function.  Those that
2239            could possibly have been scoped originally and detached from their
2240            block will have their allocation deferred so we coalesce them with
2241            others when optimization is enabled.  */
2242       else if (TREE_USED (var))
2243           expand_now = true;
2244 
2245       /* Finally, mark all variables on the list as used.  We'll use
2246            this in a moment when we expand those associated with scopes.  */
2247       TREE_USED (var) = 1;
2248 
2249       if (expand_now)
2250           expand_one_var (var, true, true, forced_stack_vars);
2251 
2252     next:
2253       if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
2254           {
2255             rtx rtl = DECL_RTL_IF_SET (var);
2256 
2257             /* Keep artificial non-ignored vars in cfun->local_decls
2258                chain until instantiate_decls.  */
2259             if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2260               add_local_decl (cfun, var);
2261             else if (rtl == NULL_RTX)
2262               /* If rtl isn't set yet, which can happen e.g. with
2263                  -fstack-protector, retry before returning from this
2264                  function.  */
2265               maybe_local_decls.safe_push (var);
2266           }
2267     }
2268 
2269   /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
2270 
2271      +-----------------+-----------------+
2272      | ...processed... | ...duplicates...|
2273      +-----------------+-----------------+
2274                        ^
2275                            +-- LEN points here.
2276 
2277      We just want the duplicates, as those are the artificial
2278      non-ignored vars that we want to keep until instantiate_decls.
2279      Move them down and truncate the array.  */
2280   if (!vec_safe_is_empty (cfun->local_decls))
2281     cfun->local_decls->block_remove (0, len);
2282 
2283   /* At this point, all variables within the block tree with TREE_USED
2284      set are actually used by the optimized function.  Lay them out.  */
2285   expand_used_vars_for_block (outer_block, true, forced_stack_vars);
2286 
2287   tree attribs = DECL_ATTRIBUTES (current_function_decl);
2288   if (stack_vars_num > 0)
2289     {
2290       bool has_addressable_vars = false;
2291 
2292       add_scope_conflicts ();
2293 
2294       /* If stack protection is enabled, we don't share space between
2295            vulnerable data and non-vulnerable data.  */
2296       if (flag_stack_protect != 0
2297             && !lookup_attribute ("no_stack_protector", attribs)
2298             && (flag_stack_protect != SPCT_FLAG_EXPLICIT
2299                 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
2300                       && lookup_attribute ("stack_protect", attribs))))
2301           has_addressable_vars = add_stack_protection_conflicts ();
2302 
2303       if (flag_stack_protect == SPCT_FLAG_STRONG && has_addressable_vars)
2304           gen_stack_protect_signal = true;
2305 
2306       /* Now that we have collected all stack variables, and have computed a
2307            minimal interference graph, attempt to save some stack space.  */
2308       partition_stack_vars ();
2309       if (dump_file)
2310           dump_stack_var_partition ();
2311     }
2312 
2313 
2314   if (!lookup_attribute ("no_stack_protector", attribs))
2315     switch (flag_stack_protect)
2316       {
2317       case SPCT_FLAG_ALL:
2318           create_stack_guard ();
2319           break;
2320 
2321       case SPCT_FLAG_STRONG:
2322           if (gen_stack_protect_signal
2323               || cfun->calls_alloca
2324               || has_protected_decls
2325               || lookup_attribute ("stack_protect", attribs))
2326             create_stack_guard ();
2327           break;
2328 
2329       case SPCT_FLAG_DEFAULT:
2330           if (cfun->calls_alloca
2331               || has_protected_decls
2332               || lookup_attribute ("stack_protect", attribs))
2333             create_stack_guard ();
2334           break;
2335 
2336       case SPCT_FLAG_EXPLICIT:
2337           if (lookup_attribute ("stack_protect", attribs))
2338             create_stack_guard ();
2339           break;
2340 
2341       default:
2342           break;
2343       }
2344 
2345   /* Assign rtl to each variable based on these partitions.  */
2346   if (stack_vars_num > 0)
2347     {
2348       class stack_vars_data data;
2349 
2350       data.asan_base = NULL_RTX;
2351       data.asan_alignb = 0;
2352 
2353       /* Reorder decls to be protected by iterating over the variables
2354            array multiple times, and allocating out of each phase in turn.  */
2355       /* ??? We could probably integrate this into the qsort we did
2356            earlier, such that we naturally see these variables first,
2357            and thus naturally allocate things in the right order.  */
2358       if (has_protected_decls)
2359           {
2360             /* Phase 1 contains only character arrays.  */
2361             expand_stack_vars (stack_protect_decl_phase_1, &data);
2362 
2363             /* Phase 2 contains other kinds of arrays.  */
2364             if (!lookup_attribute ("no_stack_protector", attribs)
2365                 && (flag_stack_protect == SPCT_FLAG_ALL
2366                       || flag_stack_protect == SPCT_FLAG_STRONG
2367                       || (flag_stack_protect == SPCT_FLAG_EXPLICIT
2368                           && lookup_attribute ("stack_protect", attribs))))
2369               expand_stack_vars (stack_protect_decl_phase_2, &data);
2370           }
2371 
2372       if (asan_sanitize_stack_p ())
2373           /* Phase 3, any partitions that need asan protection
2374              in addition to phase 1 and 2.  */
2375           expand_stack_vars (asan_decl_phase_3, &data);
2376 
2377       /* ASAN description strings don't yet have a syntax for expressing
2378            polynomial offsets.  */
2379       HOST_WIDE_INT prev_offset;
2380       if (!data.asan_vec.is_empty ()
2381             && frame_offset.is_constant (&prev_offset))
2382           {
2383             HOST_WIDE_INT offset, sz, redzonesz;
2384             redzonesz = ASAN_RED_ZONE_SIZE;
2385             sz = data.asan_vec[0] - prev_offset;
2386             if (data.asan_alignb > ASAN_RED_ZONE_SIZE
2387                 && data.asan_alignb <= 4096
2388                 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
2389               redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
2390                                & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
2391             /* Allocating a constant amount of space from a constant
2392                starting offset must give a constant result.  */
2393             offset = (alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE)
2394                         .to_constant ());
2395             data.asan_vec.safe_push (prev_offset);
2396             data.asan_vec.safe_push (offset);
2397             /* Leave space for alignment if STRICT_ALIGNMENT.  */
2398             if (STRICT_ALIGNMENT)
2399               alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
2400                                               << ASAN_SHADOW_SHIFT)
2401                                              / BITS_PER_UNIT, 1);
2402 
2403             var_end_seq
2404               = asan_emit_stack_protection (virtual_stack_vars_rtx,
2405                                                     data.asan_base,
2406                                                     data.asan_alignb,
2407                                                     data.asan_vec.address (),
2408                                                     data.asan_decl_vec.address (),
2409                                                     data.asan_vec.length ());
2410           }
2411 
2412       expand_stack_vars (NULL, &data);
2413     }
2414 
2415   if (hwasan_sanitize_stack_p ())
2416     hwasan_emit_prologue ();
2417   if (asan_sanitize_allocas_p () && cfun->calls_alloca)
2418     var_end_seq = asan_emit_allocas_unpoison (virtual_stack_dynamic_rtx,
2419                                                         virtual_stack_vars_rtx,
2420                                                         var_end_seq);
2421   else if (hwasan_sanitize_allocas_p () && cfun->calls_alloca)
2422     /* When using out-of-line instrumentation we only want to emit one function
2423        call for clearing the tags in a region of shadow stack.  When there are
2424        alloca calls in this frame we want to emit a call using the
2425        virtual_stack_dynamic_rtx, but when not we use the hwasan_frame_extent
2426        rtx we created in expand_stack_vars.  */
2427     var_end_seq = hwasan_emit_untag_frame (virtual_stack_dynamic_rtx,
2428                                                      virtual_stack_vars_rtx);
2429   else if (hwasan_sanitize_stack_p ())
2430     /* If no variables were stored on the stack, `hwasan_get_frame_extent`
2431        will return NULL_RTX and hence `hwasan_emit_untag_frame` will return
2432        NULL (i.e. an empty sequence).  */
2433     var_end_seq = hwasan_emit_untag_frame (hwasan_get_frame_extent (),
2434                                                      virtual_stack_vars_rtx);
2435 
2436   fini_vars_expansion ();
2437 
2438   /* If there were any artificial non-ignored vars without rtl
2439      found earlier, see if deferred stack allocation hasn't assigned
2440      rtl to them.  */
2441   FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
2442     {
2443       rtx rtl = DECL_RTL_IF_SET (var);
2444 
2445       /* Keep artificial non-ignored vars in cfun->local_decls
2446            chain until instantiate_decls.  */
2447       if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2448           add_local_decl (cfun, var);
2449     }
2450 
2451   /* If the target requires that FRAME_OFFSET be aligned, do it.  */
2452   if (STACK_ALIGNMENT_NEEDED)
2453     {
2454       HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2455       if (FRAME_GROWS_DOWNWARD)
2456           frame_offset = aligned_lower_bound (frame_offset, align);
2457       else
2458           frame_offset = aligned_upper_bound (frame_offset, align);
2459     }
2460 
2461   return var_end_seq;
2462 }
2463 
2464 
2465 /* If we need to produce a detailed dump, print the tree representation
2466    for STMT to the dump file.  SINCE is the last RTX after which the RTL
2467    generated for STMT should have been appended.  */
2468 
2469 static void
maybe_dump_rtl_for_gimple_stmt(gimple * stmt,rtx_insn * since)2470 maybe_dump_rtl_for_gimple_stmt (gimple *stmt, rtx_insn *since)
2471 {
2472   if (dump_file && (dump_flags & TDF_DETAILS))
2473     {
2474       fprintf (dump_file, "\n;; ");
2475       print_gimple_stmt (dump_file, stmt, 0,
2476                                TDF_SLIM | (dump_flags & TDF_LINENO));
2477       fprintf (dump_file, "\n");
2478 
2479       print_rtl (dump_file, since ? NEXT_INSN (since) : since);
2480     }
2481 }
2482 
2483 /* Maps the blocks that do not contain tree labels to rtx labels.  */
2484 
2485 static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
2486 
2487 /* Returns the label_rtx expression for a label starting basic block BB.  */
2488 
2489 static rtx_code_label *
label_rtx_for_bb(basic_block bb ATTRIBUTE_UNUSED)2490 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
2491 {
2492   if (bb->flags & BB_RTL)
2493     return block_label (bb);
2494 
2495   rtx_code_label **elt = lab_rtx_for_bb->get (bb);
2496   if (elt)
2497     return *elt;
2498 
2499   /* Find the tree label if it is present.  */
2500   gimple_stmt_iterator gsi = gsi_start_bb (bb);
2501   glabel *lab_stmt;
2502   if (!gsi_end_p (gsi)
2503       && (lab_stmt = dyn_cast <glabel *> (gsi_stmt (gsi)))
2504       && !DECL_NONLOCAL (gimple_label_label (lab_stmt)))
2505     return jump_target_rtx (gimple_label_label (lab_stmt));
2506 
2507   rtx_code_label *l = gen_label_rtx ();
2508   lab_rtx_for_bb->put (bb, l);
2509   return l;
2510 }
2511 
2512 
2513 /* A subroutine of expand_gimple_cond.  Given E, a fallthrough edge
2514    of a basic block where we just expanded the conditional at the end,
2515    possibly clean up the CFG and instruction sequence.  LAST is the
2516    last instruction before the just emitted jump sequence.  */
2517 
2518 static void
maybe_cleanup_end_of_block(edge e,rtx_insn * last)2519 maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2520 {
2521   /* Special case: when jumpif decides that the condition is
2522      trivial it emits an unconditional jump (and the necessary
2523      barrier).  But we still have two edges, the fallthru one is
2524      wrong.  purge_dead_edges would clean this up later.  Unfortunately
2525      we have to insert insns (and split edges) before
2526      find_many_sub_basic_blocks and hence before purge_dead_edges.
2527      But splitting edges might create new blocks which depend on the
2528      fact that if there are two edges there's no barrier.  So the
2529      barrier would get lost and verify_flow_info would ICE.  Instead
2530      of auditing all edge splitters to care for the barrier (which
2531      normally isn't there in a cleaned CFG), fix it here.  */
2532   if (BARRIER_P (get_last_insn ()))
2533     {
2534       rtx_insn *insn;
2535       remove_edge (e);
2536       /* Now, we have a single successor block, if we have insns to
2537            insert on the remaining edge we potentially will insert
2538            it at the end of this block (if the dest block isn't feasible)
2539            in order to avoid splitting the edge.  This insertion will take
2540            place in front of the last jump.  But we might have emitted
2541            multiple jumps (conditional and one unconditional) to the
2542            same destination.  Inserting in front of the last one then
2543            is a problem.  See PR 40021.  We fix this by deleting all
2544            jumps except the last unconditional one.  */
2545       insn = PREV_INSN (get_last_insn ());
2546       /* Make sure we have an unconditional jump.  Otherwise we're
2547            confused.  */
2548       gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2549       for (insn = PREV_INSN (insn); insn != last;)
2550           {
2551             insn = PREV_INSN (insn);
2552             if (JUMP_P (NEXT_INSN (insn)))
2553               {
2554                 if (!any_condjump_p (NEXT_INSN (insn)))
2555                     {
2556                       gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2557                       delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2558                     }
2559                 delete_insn (NEXT_INSN (insn));
2560               }
2561           }
2562     }
2563 }
2564 
2565 /* A subroutine of expand_gimple_basic_block.  Expand one GIMPLE_COND.
2566    Returns a new basic block if we've terminated the current basic
2567    block and created a new one.  */
2568 
2569 static basic_block
expand_gimple_cond(basic_block bb,gcond * stmt)2570 expand_gimple_cond (basic_block bb, gcond *stmt)
2571 {
2572   basic_block new_bb, dest;
2573   edge true_edge;
2574   edge false_edge;
2575   rtx_insn *last2, *last;
2576   enum tree_code code;
2577   tree op0, op1;
2578 
2579   code = gimple_cond_code (stmt);
2580   op0 = gimple_cond_lhs (stmt);
2581   op1 = gimple_cond_rhs (stmt);
2582   /* We're sometimes presented with such code:
2583        D.123_1 = x < y;
2584        if (D.123_1 != 0)
2585          ...
2586      This would expand to two comparisons which then later might
2587      be cleaned up by combine.  But some pattern matchers like if-conversion
2588      work better when there's only one compare, so make up for this
2589      here as special exception if TER would have made the same change.  */
2590   if (SA.values
2591       && TREE_CODE (op0) == SSA_NAME
2592       && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2593       && TREE_CODE (op1) == INTEGER_CST
2594       && ((gimple_cond_code (stmt) == NE_EXPR
2595              && integer_zerop (op1))
2596             || (gimple_cond_code (stmt) == EQ_EXPR
2597                 && integer_onep (op1)))
2598       && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2599     {
2600       gimple *second = SSA_NAME_DEF_STMT (op0);
2601       if (gimple_code (second) == GIMPLE_ASSIGN)
2602           {
2603             enum tree_code code2 = gimple_assign_rhs_code (second);
2604             if (TREE_CODE_CLASS (code2) == tcc_comparison)
2605               {
2606                 code = code2;
2607                 op0 = gimple_assign_rhs1 (second);
2608                 op1 = gimple_assign_rhs2 (second);
2609               }
2610             /* If jumps are cheap and the target does not support conditional
2611                compare, turn some more codes into jumpy sequences.  */
2612             else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4
2613                        && targetm.gen_ccmp_first == NULL)
2614               {
2615                 if ((code2 == BIT_AND_EXPR
2616                        && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2617                        && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2618                       || code2 == TRUTH_AND_EXPR)
2619                     {
2620                       code = TRUTH_ANDIF_EXPR;
2621                       op0 = gimple_assign_rhs1 (second);
2622                       op1 = gimple_assign_rhs2 (second);
2623                     }
2624                 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2625                     {
2626                       code = TRUTH_ORIF_EXPR;
2627                       op0 = gimple_assign_rhs1 (second);
2628                       op1 = gimple_assign_rhs2 (second);
2629                     }
2630               }
2631           }
2632     }
2633 
2634   /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
2635      into (x - C2) * C3 < C4.  */
2636   if ((code == EQ_EXPR || code == NE_EXPR)
2637       && TREE_CODE (op0) == SSA_NAME
2638       && TREE_CODE (op1) == INTEGER_CST)
2639     code = maybe_optimize_mod_cmp (code, &op0, &op1);
2640 
2641   /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow.  */
2642   if (!TYPE_UNSIGNED (TREE_TYPE (op0))
2643       && (code == LT_EXPR || code == LE_EXPR
2644             || code == GT_EXPR || code == GE_EXPR)
2645       && integer_zerop (op1)
2646       && TREE_CODE (op0) == SSA_NAME)
2647     maybe_optimize_sub_cmp_0 (code, &op0, &op1);
2648 
2649   last2 = last = get_last_insn ();
2650 
2651   extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2652   set_curr_insn_location (gimple_location (stmt));
2653 
2654   /* These flags have no purpose in RTL land.  */
2655   true_edge->flags &= ~EDGE_TRUE_VALUE;
2656   false_edge->flags &= ~EDGE_FALSE_VALUE;
2657 
2658   /* We can either have a pure conditional jump with one fallthru edge or
2659      two-way jump that needs to be decomposed into two basic blocks.  */
2660   if (false_edge->dest == bb->next_bb)
2661     {
2662       jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2663                     true_edge->probability);
2664       maybe_dump_rtl_for_gimple_stmt (stmt, last);
2665       if (true_edge->goto_locus != UNKNOWN_LOCATION)
2666           set_curr_insn_location (true_edge->goto_locus);
2667       false_edge->flags |= EDGE_FALLTHRU;
2668       maybe_cleanup_end_of_block (false_edge, last);
2669       return NULL;
2670     }
2671   if (true_edge->dest == bb->next_bb)
2672     {
2673       jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2674                        false_edge->probability);
2675       maybe_dump_rtl_for_gimple_stmt (stmt, last);
2676       if (false_edge->goto_locus != UNKNOWN_LOCATION)
2677           set_curr_insn_location (false_edge->goto_locus);
2678       true_edge->flags |= EDGE_FALLTHRU;
2679       maybe_cleanup_end_of_block (true_edge, last);
2680       return NULL;
2681     }
2682 
2683   jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2684               true_edge->probability);
2685   last = get_last_insn ();
2686   if (false_edge->goto_locus != UNKNOWN_LOCATION)
2687     set_curr_insn_location (false_edge->goto_locus);
2688   emit_jump (label_rtx_for_bb (false_edge->dest));
2689 
2690   BB_END (bb) = last;
2691   if (BARRIER_P (BB_END (bb)))
2692     BB_END (bb) = PREV_INSN (BB_END (bb));
2693   update_bb_for_insn (bb);
2694 
2695   new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2696   dest = false_edge->dest;
2697   redirect_edge_succ (false_edge, new_bb);
2698   false_edge->flags |= EDGE_FALLTHRU;
2699   new_bb->count = false_edge->count ();
2700   loop_p loop = find_common_loop (bb->loop_father, dest->loop_father);
2701   add_bb_to_loop (new_bb, loop);
2702   if (loop->latch == bb
2703       && loop->header == dest)
2704     loop->latch = new_bb;
2705   make_single_succ_edge (new_bb, dest, 0);
2706   if (BARRIER_P (BB_END (new_bb)))
2707     BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2708   update_bb_for_insn (new_bb);
2709 
2710   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2711 
2712   if (true_edge->goto_locus != UNKNOWN_LOCATION)
2713     {
2714       set_curr_insn_location (true_edge->goto_locus);
2715       true_edge->goto_locus = curr_insn_location ();
2716     }
2717 
2718   return new_bb;
2719 }
2720 
2721 /* Mark all calls that can have a transaction restart.  */
2722 
2723 static void
mark_transaction_restart_calls(gimple * stmt)2724 mark_transaction_restart_calls (gimple *stmt)
2725 {
2726   struct tm_restart_node dummy;
2727   tm_restart_node **slot;
2728 
2729   if (!cfun->gimple_df->tm_restart)
2730     return;
2731 
2732   dummy.stmt = stmt;
2733   slot = cfun->gimple_df->tm_restart->find_slot (&dummy, NO_INSERT);
2734   if (slot)
2735     {
2736       struct tm_restart_node *n = *slot;
2737       tree list = n->label_or_list;
2738       rtx_insn *insn;
2739 
2740       for (insn = next_real_insn (get_last_insn ());
2741              !CALL_P (insn);
2742              insn = next_real_insn (insn))
2743           continue;
2744 
2745       if (TREE_CODE (list) == LABEL_DECL)
2746           add_reg_note (insn, REG_TM, label_rtx (list));
2747       else
2748           for (; list ; list = TREE_CHAIN (list))
2749             add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2750     }
2751 }
2752 
2753 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2754    statement STMT.  */
2755 
2756 static void
expand_call_stmt(gcall * stmt)2757 expand_call_stmt (gcall *stmt)
2758 {
2759   tree exp, decl, lhs;
2760   bool builtin_p;
2761   size_t i;
2762 
2763   if (gimple_call_internal_p (stmt))
2764     {
2765       expand_internal_call (stmt);
2766       return;
2767     }
2768 
2769   /* If this is a call to a built-in function and it has no effect other
2770      than setting the lhs, try to implement it using an internal function
2771      instead.  */
2772   decl = gimple_call_fndecl (stmt);
2773   if (gimple_call_lhs (stmt)
2774       && !gimple_has_side_effects (stmt)
2775       && (optimize || (decl && called_as_built_in (decl))))
2776     {
2777       internal_fn ifn = replacement_internal_fn (stmt);
2778       if (ifn != IFN_LAST)
2779           {
2780             expand_internal_call (ifn, stmt);
2781             return;
2782           }
2783     }
2784 
2785   exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2786 
2787   CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2788   builtin_p = decl && fndecl_built_in_p (decl);
2789 
2790   /* If this is not a builtin function, the function type through which the
2791      call is made may be different from the type of the function.  */
2792   if (!builtin_p)
2793     CALL_EXPR_FN (exp)
2794       = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2795                           CALL_EXPR_FN (exp));
2796 
2797   TREE_TYPE (exp) = gimple_call_return_type (stmt);
2798   CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2799 
2800   for (i = 0; i < gimple_call_num_args (stmt); i++)
2801     {
2802       tree arg = gimple_call_arg (stmt, i);
2803       gimple *def;
2804       /* TER addresses into arguments of builtin functions so we have a
2805            chance to infer more correct alignment information.  See PR39954.  */
2806       if (builtin_p
2807             && TREE_CODE (arg) == SSA_NAME
2808             && (def = get_gimple_for_ssa_name (arg))
2809             && gimple_assign_rhs_code (def) == ADDR_EXPR)
2810           arg = gimple_assign_rhs1 (def);
2811       CALL_EXPR_ARG (exp, i) = arg;
2812     }
2813 
2814   if (gimple_has_side_effects (stmt)
2815       /* ???  Downstream in expand_expr_real_1 we assume that expressions
2816            w/o side-effects do not throw so work around this here.  */
2817       || stmt_could_throw_p (cfun, stmt))
2818     TREE_SIDE_EFFECTS (exp) = 1;
2819 
2820   if (gimple_call_nothrow_p (stmt))
2821     TREE_NOTHROW (exp) = 1;
2822 
2823   CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2824   CALL_EXPR_MUST_TAIL_CALL (exp) = gimple_call_must_tail_p (stmt);
2825   CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2826   if (decl
2827       && fndecl_built_in_p (decl, BUILT_IN_NORMAL)
2828       && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2829     CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2830   else
2831     CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2832   CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2833   CALL_EXPR_BY_DESCRIPTOR (exp) = gimple_call_by_descriptor_p (stmt);
2834   SET_EXPR_LOCATION (exp, gimple_location (stmt));
2835 
2836   /* Must come after copying location.  */
2837   copy_warning (exp, stmt);
2838 
2839   /* Ensure RTL is created for debug args.  */
2840   if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2841     {
2842       vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2843       unsigned int ix;
2844       tree dtemp;
2845 
2846       if (debug_args)
2847           for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2848             {
2849               gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2850               expand_debug_expr (dtemp);
2851             }
2852     }
2853 
2854   rtx_insn *before_call = get_last_insn ();
2855   lhs = gimple_call_lhs (stmt);
2856   if (lhs)
2857     expand_assignment (lhs, exp, false);
2858   else
2859     expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2860 
2861   /* If the gimple call is an indirect call and has 'nocf_check'
2862      attribute find a generated CALL insn to mark it as no
2863      control-flow verification is needed.  */
2864   if (gimple_call_nocf_check_p (stmt)
2865       && !gimple_call_fndecl (stmt))
2866     {
2867       rtx_insn *last = get_last_insn ();
2868       while (!CALL_P (last)
2869                && last != before_call)
2870           last = PREV_INSN (last);
2871 
2872       if (last != before_call)
2873           add_reg_note (last, REG_CALL_NOCF_CHECK, const0_rtx);
2874     }
2875 
2876   mark_transaction_restart_calls (stmt);
2877 }
2878 
2879 
2880 /* Generate RTL for an asm statement (explicit assembler code).
2881    STRING is a STRING_CST node containing the assembler code text,
2882    or an ADDR_EXPR containing a STRING_CST.  VOL nonzero means the
2883    insn is volatile; don't optimize it.  */
2884 
2885 static void
expand_asm_loc(tree string,int vol,location_t locus)2886 expand_asm_loc (tree string, int vol, location_t locus)
2887 {
2888   rtx body;
2889 
2890   body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2891                                         ggc_strdup (TREE_STRING_POINTER (string)),
2892                                         locus);
2893 
2894   MEM_VOLATILE_P (body) = vol;
2895 
2896   /* Non-empty basic ASM implicitly clobbers memory.  */
2897   if (TREE_STRING_LENGTH (string) != 0)
2898     {
2899       rtx asm_op, clob;
2900       unsigned i, nclobbers;
2901       auto_vec<rtx> input_rvec, output_rvec;
2902       auto_vec<machine_mode> input_mode;
2903       auto_vec<const char *> constraints;
2904       auto_vec<rtx> clobber_rvec;
2905       HARD_REG_SET clobbered_regs;
2906       CLEAR_HARD_REG_SET (clobbered_regs);
2907 
2908       clob = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2909       clobber_rvec.safe_push (clob);
2910 
2911       if (targetm.md_asm_adjust)
2912           targetm.md_asm_adjust (output_rvec, input_rvec, input_mode,
2913                                      constraints, clobber_rvec, clobbered_regs,
2914                                      locus);
2915 
2916       asm_op = body;
2917       nclobbers = clobber_rvec.length ();
2918       body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (1 + nclobbers));
2919 
2920       XVECEXP (body, 0, 0) = asm_op;
2921       for (i = 0; i < nclobbers; i++)
2922           XVECEXP (body, 0, i + 1) = gen_rtx_CLOBBER (VOIDmode, clobber_rvec[i]);
2923     }
2924 
2925   emit_insn (body);
2926 }
2927 
2928 /* Return the number of times character C occurs in string S.  */
2929 static int
n_occurrences(int c,const char * s)2930 n_occurrences (int c, const char *s)
2931 {
2932   int n = 0;
2933   while (*s)
2934     n += (*s++ == c);
2935   return n;
2936 }
2937 
2938 /* A subroutine of expand_asm_operands.  Check that all operands have
2939    the same number of alternatives.  Return true if so.  */
2940 
2941 static bool
check_operand_nalternatives(const vec<const char * > & constraints)2942 check_operand_nalternatives (const vec<const char *> &constraints)
2943 {
2944   unsigned len = constraints.length();
2945   if (len > 0)
2946     {
2947       int nalternatives = n_occurrences (',', constraints[0]);
2948 
2949       if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2950           {
2951             error ("too many alternatives in %<asm%>");
2952             return false;
2953           }
2954 
2955       for (unsigned i = 1; i < len; ++i)
2956           if (n_occurrences (',', constraints[i]) != nalternatives)
2957             {
2958               error ("operand constraints for %<asm%> differ "
2959                        "in number of alternatives");
2960               return false;
2961             }
2962     }
2963   return true;
2964 }
2965 
2966 /* Check for overlap between registers marked in CLOBBERED_REGS and
2967    anything inappropriate in T.  Emit error and return the register
2968    variable definition for error, NULL_TREE for ok.  */
2969 
2970 static bool
tree_conflicts_with_clobbers_p(tree t,HARD_REG_SET * clobbered_regs,location_t loc)2971 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs,
2972                                         location_t loc)
2973 {
2974   /* Conflicts between asm-declared register variables and the clobber
2975      list are not allowed.  */
2976   tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2977 
2978   if (overlap)
2979     {
2980       error_at (loc, "%<asm%> specifier for variable %qE conflicts with "
2981                     "%<asm%> clobber list", DECL_NAME (overlap));
2982 
2983       /* Reset registerness to stop multiple errors emitted for a single
2984            variable.  */
2985       DECL_REGISTER (overlap) = 0;
2986       return true;
2987     }
2988 
2989   return false;
2990 }
2991 
2992 /* Check that the given REGNO spanning NREGS is a valid
2993    asm clobber operand.  Some HW registers cannot be
2994    saved/restored, hence they should not be clobbered by
2995    asm statements.  */
2996 static bool
asm_clobber_reg_is_valid(int regno,int nregs,const char * regname)2997 asm_clobber_reg_is_valid (int regno, int nregs, const char *regname)
2998 {
2999   bool is_valid = true;
3000   HARD_REG_SET regset;
3001 
3002   CLEAR_HARD_REG_SET (regset);
3003 
3004   add_range_to_hard_reg_set (&regset, regno, nregs);
3005 
3006   /* Clobbering the PIC register is an error.  */
3007   if (PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3008       && overlaps_hard_reg_set_p (regset, Pmode, PIC_OFFSET_TABLE_REGNUM))
3009     {
3010       /* ??? Diagnose during gimplification?  */
3011       error ("PIC register clobbered by %qs in %<asm%>", regname);
3012       is_valid = false;
3013     }
3014   else if (!in_hard_reg_set_p
3015              (accessible_reg_set, reg_raw_mode[regno], regno))
3016     {
3017       /* ??? Diagnose during gimplification?  */
3018       error ("the register %qs cannot be clobbered in %<asm%>"
3019                " for the current target", regname);
3020       is_valid = false;
3021     }
3022 
3023   /* Clobbering the stack pointer register is deprecated.  GCC expects
3024      the value of the stack pointer after an asm statement to be the same
3025      as it was before, so no asm can validly clobber the stack pointer in
3026      the usual sense.  Adding the stack pointer to the clobber list has
3027      traditionally had some undocumented and somewhat obscure side-effects.  */
3028   if (overlaps_hard_reg_set_p (regset, Pmode, STACK_POINTER_REGNUM))
3029     {
3030       crtl->sp_is_clobbered_by_asm = true;
3031       if (warning (OPT_Wdeprecated, "listing the stack pointer register"
3032                        " %qs in a clobber list is deprecated", regname))
3033           inform (input_location, "the value of the stack pointer after"
3034                     " an %<asm%> statement must be the same as it was before"
3035                     " the statement");
3036     }
3037 
3038   return is_valid;
3039 }
3040 
3041 /* Generate RTL for an asm statement with arguments.
3042    STRING is the instruction template.
3043    OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
3044    Each output or input has an expression in the TREE_VALUE and
3045    a tree list in TREE_PURPOSE which in turn contains a constraint
3046    name in TREE_VALUE (or NULL_TREE) and a constraint string
3047    in TREE_PURPOSE.
3048    CLOBBERS is a list of STRING_CST nodes each naming a hard register
3049    that is clobbered by this insn.
3050 
3051    LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
3052    should be the fallthru basic block of the asm goto.
3053 
3054    Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
3055    Some elements of OUTPUTS may be replaced with trees representing temporary
3056    values.  The caller should copy those temporary values to the originally
3057    specified lvalues.
3058 
3059    VOL nonzero means the insn is volatile; don't optimize it.  */
3060 
3061 static void
expand_asm_stmt(gasm * stmt)3062 expand_asm_stmt (gasm *stmt)
3063 {
3064   class save_input_location
3065   {
3066     location_t old;
3067 
3068   public:
3069     explicit save_input_location(location_t where)
3070     {
3071       old = input_location;
3072       input_location = where;
3073     }
3074 
3075     ~save_input_location()
3076     {
3077       input_location = old;
3078     }
3079   };
3080 
3081   location_t locus = gimple_location (stmt);
3082 
3083   if (gimple_asm_input_p (stmt))
3084     {
3085       const char *s = gimple_asm_string (stmt);
3086       tree string = build_string (strlen (s), s);
3087       expand_asm_loc (string, gimple_asm_volatile_p (stmt), locus);
3088       return;
3089     }
3090 
3091   /* There are some legacy diagnostics in here.  */
3092   save_input_location s_i_l(locus);
3093 
3094   unsigned noutputs = gimple_asm_noutputs (stmt);
3095   unsigned ninputs = gimple_asm_ninputs (stmt);
3096   unsigned nlabels = gimple_asm_nlabels (stmt);
3097   unsigned i;
3098   bool error_seen = false;
3099 
3100   /* ??? Diagnose during gimplification?  */
3101   if (ninputs + noutputs + nlabels > MAX_RECOG_OPERANDS)
3102     {
3103       error_at (locus, "more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
3104       return;
3105     }
3106 
3107   auto_vec<tree, MAX_RECOG_OPERANDS> output_tvec;
3108   auto_vec<tree, MAX_RECOG_OPERANDS> input_tvec;
3109   auto_vec<const char *, MAX_RECOG_OPERANDS> constraints;
3110 
3111   /* Copy the gimple vectors into new vectors that we can manipulate.  */
3112 
3113   output_tvec.safe_grow (noutputs, true);
3114   input_tvec.safe_grow (ninputs, true);
3115   constraints.safe_grow (noutputs + ninputs, true);
3116 
3117   for (i = 0; i < noutputs; ++i)
3118     {
3119       tree t = gimple_asm_output_op (stmt, i);
3120       output_tvec[i] = TREE_VALUE (t);
3121       constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
3122     }
3123   for (i = 0; i < ninputs; i++)
3124     {
3125       tree t = gimple_asm_input_op (stmt, i);
3126       input_tvec[i] = TREE_VALUE (t);
3127       constraints[i + noutputs]
3128           = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
3129     }
3130 
3131   /* ??? Diagnose during gimplification?  */
3132   if (! check_operand_nalternatives (constraints))
3133     return;
3134 
3135   /* Count the number of meaningful clobbered registers, ignoring what
3136      we would ignore later.  */
3137   auto_vec<rtx> clobber_rvec;
3138   HARD_REG_SET clobbered_regs;
3139   CLEAR_HARD_REG_SET (clobbered_regs);
3140 
3141   if (unsigned n = gimple_asm_nclobbers (stmt))
3142     {
3143       clobber_rvec.reserve (n);
3144       for (i = 0; i < n; i++)
3145           {
3146             tree t = gimple_asm_clobber_op (stmt, i);
3147           const char *regname = TREE_STRING_POINTER (TREE_VALUE (t));
3148             int nregs, j;
3149 
3150             j = decode_reg_name_and_count (regname, &nregs);
3151             if (j < 0)
3152               {
3153                 if (j == -2)
3154                     {
3155                       /* ??? Diagnose during gimplification?  */
3156                       error_at (locus, "unknown register name %qs in %<asm%>",
3157                                   regname);
3158                       error_seen = true;
3159                     }
3160                 else if (j == -4)
3161                     {
3162                       rtx x = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
3163                       clobber_rvec.safe_push (x);
3164                     }
3165                 else
3166                     {
3167                       /* Otherwise we should have -1 == empty string
3168                          or -3 == cc, which is not a register.  */
3169                       gcc_assert (j == -1 || j == -3);
3170                     }
3171               }
3172             else
3173               for (int reg = j; reg < j + nregs; reg++)
3174                 {
3175                     if (!asm_clobber_reg_is_valid (reg, nregs, regname))
3176                       return;
3177 
3178                   SET_HARD_REG_BIT (clobbered_regs, reg);
3179                   rtx x = gen_rtx_REG (reg_raw_mode[reg], reg);
3180                     clobber_rvec.safe_push (x);
3181                 }
3182           }
3183     }
3184 
3185   /* First pass over inputs and outputs checks validity and sets
3186      mark_addressable if needed.  */
3187   /* ??? Diagnose during gimplification?  */
3188 
3189   for (i = 0; i < noutputs; ++i)
3190     {
3191       tree val = output_tvec[i];
3192       tree type = TREE_TYPE (val);
3193       const char *constraint;
3194       bool is_inout;
3195       bool allows_reg;
3196       bool allows_mem;
3197 
3198       /* Try to parse the output constraint.  If that fails, there's
3199            no point in going further.  */
3200       constraint = constraints[i];
3201       if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
3202                                             &allows_mem, &allows_reg, &is_inout))
3203           return;
3204 
3205       /* If the output is a hard register, verify it doesn't conflict with
3206            any other operand's possible hard register use.  */
3207       if (DECL_P (val)
3208             && REG_P (DECL_RTL (val))
3209             && HARD_REGISTER_P (DECL_RTL (val)))
3210           {
3211             unsigned j, output_hregno = REGNO (DECL_RTL (val));
3212             bool early_clobber_p = strchr (constraints[i], '&') != NULL;
3213             unsigned long match;
3214 
3215             /* Verify the other outputs do not use the same hard register.  */
3216             for (j = i + 1; j < noutputs; ++j)
3217               if (DECL_P (output_tvec[j])
3218                     && REG_P (DECL_RTL (output_tvec[j]))
3219                     && HARD_REGISTER_P (DECL_RTL (output_tvec[j]))
3220                     && output_hregno == REGNO (DECL_RTL (output_tvec[j])))
3221                 {
3222                     error_at (locus, "invalid hard register usage between output "
3223                                 "operands");
3224                     error_seen = true;
3225                 }
3226 
3227             /* Verify matching constraint operands use the same hard register
3228                and that the non-matching constraint operands do not use the same
3229                hard register if the output is an early clobber operand.  */
3230             for (j = 0; j < ninputs; ++j)
3231               if (DECL_P (input_tvec[j])
3232                     && REG_P (DECL_RTL (input_tvec[j]))
3233                     && HARD_REGISTER_P (DECL_RTL (input_tvec[j])))
3234                 {
3235                     unsigned input_hregno = REGNO (DECL_RTL (input_tvec[j]));
3236                     switch (*constraints[j + noutputs])
3237                       {
3238                       case '0':  case '1':  case '2':  case '3':  case '4':
3239                       case '5':  case '6':  case '7':  case '8':  case '9':
3240                         match = strtoul (constraints[j + noutputs], NULL, 10);
3241                         break;
3242                       default:
3243                         match = ULONG_MAX;
3244                         break;
3245                       }
3246                     if (i == match
3247                         && output_hregno != input_hregno)
3248                       {
3249                         error_at (locus, "invalid hard register usage between "
3250                                     "output operand and matching constraint operand");
3251                         error_seen = true;
3252                       }
3253                     else if (early_clobber_p
3254                                && i != match
3255                                && output_hregno == input_hregno)
3256                       {
3257                         error_at (locus, "invalid hard register usage between "
3258                                     "earlyclobber operand and input operand");
3259                         error_seen = true;
3260                       }
3261                 }
3262           }
3263 
3264       if (! allows_reg
3265             && (allows_mem
3266                 || is_inout
3267                 || (DECL_P (val)
3268                       && REG_P (DECL_RTL (val))
3269                       && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
3270           mark_addressable (val);
3271     }
3272 
3273   for (i = 0; i < ninputs; ++i)
3274     {
3275       bool allows_reg, allows_mem;
3276       const char *constraint;
3277 
3278       constraint = constraints[i + noutputs];
3279       if (! parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
3280                                             constraints.address (),
3281                                             &allows_mem, &allows_reg))
3282           return;
3283 
3284       if (! allows_reg && allows_mem)
3285           mark_addressable (input_tvec[i]);
3286     }
3287 
3288   /* Second pass evaluates arguments.  */
3289 
3290   /* Make sure stack is consistent for asm goto.  */
3291   if (nlabels > 0)
3292     do_pending_stack_adjust ();
3293   int old_generating_concat_p = generating_concat_p;
3294 
3295   /* Vector of RTX's of evaluated output operands.  */
3296   auto_vec<rtx, MAX_RECOG_OPERANDS> output_rvec;
3297   auto_vec<int, MAX_RECOG_OPERANDS> inout_opnum;
3298   rtx_insn *after_rtl_seq = NULL, *after_rtl_end = NULL;
3299 
3300   output_rvec.safe_grow (noutputs, true);
3301 
3302   for (i = 0; i < noutputs; ++i)
3303     {
3304       tree val = output_tvec[i];
3305       tree type = TREE_TYPE (val);
3306       bool is_inout, allows_reg, allows_mem, ok;
3307       rtx op;
3308 
3309       ok = parse_output_constraint (&constraints[i], i, ninputs,
3310                                             noutputs, &allows_mem, &allows_reg,
3311                                             &is_inout);
3312       gcc_assert (ok);
3313 
3314       /* If an output operand is not a decl or indirect ref and our constraint
3315            allows a register, make a temporary to act as an intermediate.
3316            Make the asm insn write into that, then we will copy it to
3317            the real output operand.  Likewise for promoted variables.  */
3318 
3319       generating_concat_p = 0;
3320 
3321       gcc_assert (TREE_CODE (val) != INDIRECT_REF);
3322       if (((TREE_CODE (val) == MEM_REF
3323               && TREE_CODE (TREE_OPERAND (val, 0)) != ADDR_EXPR)
3324              && allows_mem)
3325             || (DECL_P (val)
3326                 && (allows_mem || REG_P (DECL_RTL (val)))
3327                 && ! (REG_P (DECL_RTL (val))
3328                         && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
3329             || ! allows_reg
3330             || is_inout
3331             || TREE_ADDRESSABLE (type)
3332             || (!tree_fits_poly_int64_p (TYPE_SIZE (type))
3333                 && !known_size_p (max_int_size_in_bytes (type))))
3334           {
3335             op = expand_expr (val, NULL_RTX, VOIDmode,
3336                                   !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
3337             if (MEM_P (op))
3338               op = validize_mem (op);
3339 
3340             if (! allows_reg && !MEM_P (op))
3341               {
3342                 error_at (locus, "output number %d not directly addressable", i);
3343                 error_seen = true;
3344               }
3345             if ((! allows_mem && MEM_P (op) && GET_MODE (op) != BLKmode)
3346                 || GET_CODE (op) == CONCAT)
3347               {
3348                 rtx old_op = op;
3349                 op = gen_reg_rtx (GET_MODE (op));
3350 
3351                 generating_concat_p = old_generating_concat_p;
3352 
3353                 if (is_inout)
3354                     emit_move_insn (op, old_op);
3355 
3356                 push_to_sequence2 (after_rtl_seq, after_rtl_end);
3357                 emit_move_insn (old_op, op);
3358                 after_rtl_seq = get_insns ();
3359                 after_rtl_end = get_last_insn ();
3360                 end_sequence ();
3361               }
3362           }
3363       else
3364           {
3365             op = assign_temp (type, 0, 1);
3366             op = validize_mem (op);
3367             if (!MEM_P (op) && TREE_CODE (val) == SSA_NAME)
3368               set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (val), op);
3369 
3370             generating_concat_p = old_generating_concat_p;
3371 
3372             push_to_sequence2 (after_rtl_seq, after_rtl_end);
3373             expand_assignment (val, make_tree (type, op), false);
3374             after_rtl_seq = get_insns ();
3375             after_rtl_end = get_last_insn ();
3376             end_sequence ();
3377           }
3378       output_rvec[i] = op;
3379 
3380       if (is_inout)
3381           inout_opnum.safe_push (i);
3382     }
3383 
3384   const char *str = gimple_asm_string (stmt);
3385   if (error_seen)
3386     {
3387       ninputs = 0;
3388       noutputs = 0;
3389       inout_opnum.truncate (0);
3390       output_rvec.truncate (0);
3391       clobber_rvec.truncate (0);
3392       constraints.truncate (0);
3393       CLEAR_HARD_REG_SET (clobbered_regs);
3394       str = "";
3395     }
3396 
3397   auto_vec<rtx, MAX_RECOG_OPERANDS> input_rvec;
3398   auto_vec<machine_mode, MAX_RECOG_OPERANDS> input_mode;
3399 
3400   input_rvec.safe_grow (ninputs, true);
3401   input_mode.safe_grow (ninputs, true);
3402 
3403   generating_concat_p = 0;
3404 
3405   for (i = 0; i < ninputs; ++i)
3406     {
3407       tree val = input_tvec[i];
3408       tree type = TREE_TYPE (val);
3409       bool allows_reg, allows_mem, ok;
3410       const char *constraint;
3411       rtx op;
3412 
3413       constraint = constraints[i + noutputs];
3414       ok = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
3415                                            constraints.address (),
3416                                            &allows_mem, &allows_reg);
3417       gcc_assert (ok);
3418 
3419       /* EXPAND_INITIALIZER will not generate code for valid initializer
3420            constants, but will still generate code for other types of operand.
3421            This is the behavior we want for constant constraints.  */
3422       op = expand_expr (val, NULL_RTX, VOIDmode,
3423                               allows_reg ? EXPAND_NORMAL
3424                               : allows_mem ? EXPAND_MEMORY
3425                               : EXPAND_INITIALIZER);
3426 
3427       /* Never pass a CONCAT to an ASM.  */
3428       if (GET_CODE (op) == CONCAT)
3429           op = force_reg (GET_MODE (op), op);
3430       else if (MEM_P (op))
3431           op = validize_mem (op);
3432 
3433       if (asm_operand_ok (op, constraint, NULL) <= 0)
3434           {
3435             if (allows_reg && TYPE_MODE (type) != BLKmode)
3436               op = force_reg (TYPE_MODE (type), op);
3437             else if (!allows_mem)
3438               warning_at (locus, 0, "%<asm%> operand %d probably does not match "
3439                               "constraints", i + noutputs);
3440             else if (MEM_P (op))
3441               {
3442                 /* We won't recognize either volatile memory or memory
3443                      with a queued address as available a memory_operand
3444                      at this point.  Ignore it: clearly this *is* a memory.  */
3445               }
3446             else
3447               gcc_unreachable ();
3448           }
3449       input_rvec[i] = op;
3450       input_mode[i] = TYPE_MODE (type);
3451     }
3452 
3453   /* For in-out operands, copy output rtx to input rtx.  */
3454   unsigned ninout = inout_opnum.length ();
3455   for (i = 0; i < ninout; i++)
3456     {
3457       int j = inout_opnum[i];
3458       rtx o = output_rvec[j];
3459 
3460       input_rvec.safe_push (o);
3461       input_mode.safe_push (GET_MODE (o));
3462 
3463       char buffer[16];
3464       sprintf (buffer, "%d", j);
3465       constraints.safe_push (ggc_strdup (buffer));
3466     }
3467   ninputs += ninout;
3468 
3469   /* Sometimes we wish to automatically clobber registers across an asm.
3470      Case in point is when the i386 backend moved from cc0 to a hard reg --
3471      maintaining source-level compatibility means automatically clobbering
3472      the flags register.  */
3473   rtx_insn *after_md_seq = NULL;
3474   if (targetm.md_asm_adjust)
3475     after_md_seq
3476           = targetm.md_asm_adjust (output_rvec, input_rvec, input_mode,
3477                                          constraints, clobber_rvec, clobbered_regs,
3478                                          locus);
3479 
3480   /* Do not allow the hook to change the output and input count,
3481      lest it mess up the operand numbering.  */
3482   gcc_assert (output_rvec.length() == noutputs);
3483   gcc_assert (input_rvec.length() == ninputs);
3484   gcc_assert (constraints.length() == noutputs + ninputs);
3485 
3486   /* But it certainly can adjust the clobbers.  */
3487   unsigned nclobbers = clobber_rvec.length ();
3488 
3489   /* Third pass checks for easy conflicts.  */
3490   /* ??? Why are we doing this on trees instead of rtx.  */
3491 
3492   bool clobber_conflict_found = 0;
3493   for (i = 0; i < noutputs; ++i)
3494     if (tree_conflicts_with_clobbers_p (output_tvec[i], &clobbered_regs, locus))
3495           clobber_conflict_found = 1;
3496   for (i = 0; i < ninputs - ninout; ++i)
3497     if (tree_conflicts_with_clobbers_p (input_tvec[i], &clobbered_regs, locus))
3498           clobber_conflict_found = 1;
3499 
3500   /* Make vectors for the expression-rtx, constraint strings,
3501      and named operands.  */
3502 
3503   rtvec argvec = rtvec_alloc (ninputs);
3504   rtvec constraintvec = rtvec_alloc (ninputs);
3505   rtvec labelvec = rtvec_alloc (nlabels);
3506 
3507   rtx body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
3508                                             : GET_MODE (output_rvec[0])),
3509                                            ggc_strdup (str),
3510                                            "", 0, argvec, constraintvec,
3511                                            labelvec, locus);
3512   MEM_VOLATILE_P (body) = gimple_asm_volatile_p (stmt);
3513 
3514   for (i = 0; i < ninputs; ++i)
3515     {
3516       ASM_OPERANDS_INPUT (body, i) = input_rvec[i];
3517       ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
3518           = gen_rtx_ASM_INPUT_loc (input_mode[i],
3519                                          constraints[i + noutputs],
3520                                          locus);
3521     }
3522 
3523   /* Copy labels to the vector.  */
3524   rtx_code_label *fallthru_label = NULL;
3525   if (nlabels > 0)
3526     {
3527       basic_block fallthru_bb = NULL;
3528       edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
3529       if (fallthru)
3530           fallthru_bb = fallthru->dest;
3531 
3532       for (i = 0; i < nlabels; ++i)
3533           {
3534             tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
3535             rtx_insn *r;
3536             /* If asm goto has any labels in the fallthru basic block, use
3537                a label that we emit immediately after the asm goto.  Expansion
3538                may insert further instructions into the same basic block after
3539                asm goto and if we don't do this, insertion of instructions on
3540                the fallthru edge might misbehave.  See PR58670.  */
3541             if (fallthru_bb && label_to_block (cfun, label) == fallthru_bb)
3542               {
3543                 if (fallthru_label == NULL_RTX)
3544                   fallthru_label = gen_label_rtx ();
3545                 r = fallthru_label;
3546               }
3547             else
3548               r = label_rtx (label);
3549             ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
3550           }
3551     }
3552 
3553   /* Now, for each output, construct an rtx
3554      (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
3555                                      ARGVEC CONSTRAINTS OPNAMES))
3556      If there is more than one, put them inside a PARALLEL.  */
3557 
3558   if (noutputs == 0 && nclobbers == 0)
3559     {
3560       /* No output operands: put in a raw ASM_OPERANDS rtx.  */
3561       if (nlabels > 0)
3562           emit_jump_insn (body);
3563       else
3564           emit_insn (body);
3565     }
3566   else if (noutputs == 1 && nclobbers == 0)
3567     {
3568       ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
3569       if (nlabels > 0)
3570           emit_jump_insn (gen_rtx_SET (output_rvec[0], body));
3571       else
3572           emit_insn (gen_rtx_SET (output_rvec[0], body));
3573     }
3574   else
3575     {
3576       rtx obody = body;
3577       int num = noutputs;
3578 
3579       if (num == 0)
3580           num = 1;
3581 
3582       body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
3583 
3584       /* For each output operand, store a SET.  */
3585       for (i = 0; i < noutputs; ++i)
3586           {
3587             rtx src, o = output_rvec[i];
3588             if (i == 0)
3589               {
3590                 ASM_OPERANDS_OUTPUT_CONSTRAINT (obody) = constraints[0];
3591                 src = obody;
3592               }
3593             else
3594               {
3595                 src = gen_rtx_ASM_OPERANDS (GET_MODE (o),
3596                                                     ASM_OPERANDS_TEMPLATE (obody),
3597                                                     constraints[i], i, argvec,
3598                                                     constraintvec, labelvec, locus);
3599                 MEM_VOLATILE_P (src) = gimple_asm_volatile_p (stmt);
3600               }
3601             XVECEXP (body, 0, i) = gen_rtx_SET (o, src);
3602           }
3603 
3604       /* If there are no outputs (but there are some clobbers)
3605            store the bare ASM_OPERANDS into the PARALLEL.  */
3606       if (i == 0)
3607           XVECEXP (body, 0, i++) = obody;
3608 
3609       /* Store (clobber REG) for each clobbered register specified.  */
3610       for (unsigned j = 0; j < nclobbers; ++j)
3611           {
3612             rtx clobbered_reg = clobber_rvec[j];
3613 
3614             /* Do sanity check for overlap between clobbers and respectively
3615                input and outputs that hasn't been handled.  Such overlap
3616                should have been detected and reported above.  */
3617             if (!clobber_conflict_found && REG_P (clobbered_reg))
3618               {
3619                 /* We test the old body (obody) contents to avoid
3620                      tripping over the under-construction body.  */
3621                 for (unsigned k = 0; k < noutputs; ++k)
3622                     if (reg_overlap_mentioned_p (clobbered_reg, output_rvec[k]))
3623                       internal_error ("%<asm%> clobber conflict with "
3624                                           "output operand");
3625 
3626                 for (unsigned k = 0; k < ninputs - ninout; ++k)
3627                     if (reg_overlap_mentioned_p (clobbered_reg, input_rvec[k]))
3628                       internal_error ("%<asm%> clobber conflict with "
3629                                           "input operand");
3630               }
3631 
3632             XVECEXP (body, 0, i++) = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
3633           }
3634 
3635       if (nlabels > 0)
3636           emit_jump_insn (body);
3637       else
3638           emit_insn (body);
3639     }
3640 
3641   generating_concat_p = old_generating_concat_p;
3642 
3643   if (fallthru_label)
3644     emit_label (fallthru_label);
3645 
3646   if (after_md_seq)
3647     emit_insn (after_md_seq);
3648   if (after_rtl_seq)
3649     {
3650       if (nlabels == 0)
3651           emit_insn (after_rtl_seq);
3652       else
3653           {
3654             edge e;
3655             edge_iterator ei;
3656 
3657             FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
3658               {
3659                 start_sequence ();
3660                 for (rtx_insn *curr = after_rtl_seq;
3661                        curr != NULL_RTX;
3662                        curr = NEXT_INSN (curr))
3663                     emit_insn (copy_insn (PATTERN (curr)));
3664                 rtx_insn *copy = get_insns ();
3665                 end_sequence ();
3666                 prepend_insn_to_edge (copy, e);
3667               }
3668           }
3669     }
3670 
3671   free_temp_slots ();
3672   crtl->has_asm_statement = 1;
3673 }
3674 
3675 /* Emit code to jump to the address
3676    specified by the pointer expression EXP.  */
3677 
3678 static void
expand_computed_goto(tree exp)3679 expand_computed_goto (tree exp)
3680 {
3681   rtx x = expand_normal (exp);
3682 
3683   do_pending_stack_adjust ();
3684   emit_indirect_jump (x);
3685 }
3686 
3687 /* Generate RTL code for a `goto' statement with target label LABEL.
3688    LABEL should be a LABEL_DECL tree node that was or will later be
3689    defined with `expand_label'.  */
3690 
3691 static void
expand_goto(tree label)3692 expand_goto (tree label)
3693 {
3694   if (flag_checking)
3695     {
3696       /* Check for a nonlocal goto to a containing function.  Should have
3697            gotten translated to __builtin_nonlocal_goto.  */
3698       tree context = decl_function_context (label);
3699       gcc_assert (!context || context == current_function_decl);
3700     }
3701 
3702   emit_jump (jump_target_rtx (label));
3703 }
3704 
3705 /* Output a return with no value.  */
3706 
3707 static void
expand_null_return_1(void)3708 expand_null_return_1 (void)
3709 {
3710   clear_pending_stack_adjust ();
3711   do_pending_stack_adjust ();
3712   emit_jump (return_label);
3713 }
3714 
3715 /* Generate RTL to return from the current function, with no value.
3716    (That is, we do not do anything about returning any value.)  */
3717 
3718 void
expand_null_return(void)3719 expand_null_return (void)
3720 {
3721   /* If this function was declared to return a value, but we
3722      didn't, clobber the return registers so that they are not
3723      propagated live to the rest of the function.  */
3724   clobber_return_register ();
3725 
3726   expand_null_return_1 ();
3727 }
3728 
3729 /* Generate RTL to return from the current function, with value VAL.  */
3730 
3731 static void
expand_value_return(rtx val)3732 expand_value_return (rtx val)
3733 {
3734   /* Copy the value to the return location unless it's already there.  */
3735 
3736   tree decl = DECL_RESULT (current_function_decl);
3737   rtx return_reg = DECL_RTL (decl);
3738   if (return_reg != val)
3739     {
3740       tree funtype = TREE_TYPE (current_function_decl);
3741       tree type = TREE_TYPE (decl);
3742       int unsignedp = TYPE_UNSIGNED (type);
3743       machine_mode old_mode = DECL_MODE (decl);
3744       machine_mode mode;
3745       if (DECL_BY_REFERENCE (decl))
3746         mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3747       else
3748         mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3749 
3750       if (mode != old_mode)
3751           val = convert_modes (mode, old_mode, val, unsignedp);
3752 
3753       if (GET_CODE (return_reg) == PARALLEL)
3754           emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3755       else
3756           emit_move_insn (return_reg, val);
3757     }
3758 
3759   expand_null_return_1 ();
3760 }
3761 
3762 /* Generate RTL to evaluate the expression RETVAL and return it
3763    from the current function.  */
3764 
3765 static void
expand_return(tree retval)3766 expand_return (tree retval)
3767 {
3768   rtx result_rtl;
3769   rtx val = 0;
3770   tree retval_rhs;
3771 
3772   /* If function wants no value, give it none.  */
3773   if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3774     {
3775       expand_normal (retval);
3776       expand_null_return ();
3777       return;
3778     }
3779 
3780   if (retval == error_mark_node)
3781     {
3782       /* Treat this like a return of no value from a function that
3783            returns a value.  */
3784       expand_null_return ();
3785       return;
3786     }
3787   else if ((TREE_CODE (retval) == MODIFY_EXPR
3788               || TREE_CODE (retval) == INIT_EXPR)
3789              && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3790     retval_rhs = TREE_OPERAND (retval, 1);
3791   else
3792     retval_rhs = retval;
3793 
3794   result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3795 
3796   /* If we are returning the RESULT_DECL, then the value has already
3797      been stored into it, so we don't have to do anything special.  */
3798   if (TREE_CODE (retval_rhs) == RESULT_DECL)
3799     expand_value_return (result_rtl);
3800 
3801   /* If the result is an aggregate that is being returned in one (or more)
3802      registers, load the registers here.  */
3803 
3804   else if (retval_rhs != 0
3805              && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3806              && REG_P (result_rtl))
3807     {
3808       val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3809       if (val)
3810           {
3811             /* Use the mode of the result value on the return register.  */
3812             PUT_MODE (result_rtl, GET_MODE (val));
3813             expand_value_return (val);
3814           }
3815       else
3816           expand_null_return ();
3817     }
3818   else if (retval_rhs != 0
3819              && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3820              && (REG_P (result_rtl)
3821                  || (GET_CODE (result_rtl) == PARALLEL)))
3822     {
3823       /* Compute the return value into a temporary (usually a pseudo reg).  */
3824       val
3825           = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3826       val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3827       val = force_not_mem (val);
3828       expand_value_return (val);
3829     }
3830   else
3831     {
3832       /* No hard reg used; calculate value into hard return reg.  */
3833       expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3834       expand_value_return (result_rtl);
3835     }
3836 }
3837 
3838 /* Expand a clobber of LHS.  If LHS is stored it in a multi-part
3839    register, tell the rtl optimizers that its value is no longer
3840    needed.  */
3841 
3842 static void
expand_clobber(tree lhs)3843 expand_clobber (tree lhs)
3844 {
3845   if (DECL_P (lhs))
3846     {
3847       rtx decl_rtl = DECL_RTL_IF_SET (lhs);
3848       if (decl_rtl && REG_P (decl_rtl))
3849           {
3850             machine_mode decl_mode = GET_MODE (decl_rtl);
3851             if (maybe_gt (GET_MODE_SIZE (decl_mode),
3852                               REGMODE_NATURAL_SIZE (decl_mode)))
3853               emit_clobber (decl_rtl);
3854           }
3855     }
3856 }
3857 
3858 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3859    STMT that doesn't require special handling for outgoing edges.  That
3860    is no tailcalls and no GIMPLE_COND.  */
3861 
3862 static void
expand_gimple_stmt_1(gimple * stmt)3863 expand_gimple_stmt_1 (gimple *stmt)
3864 {
3865   tree op0;
3866 
3867   set_curr_insn_location (gimple_location (stmt));
3868 
3869   switch (gimple_code (stmt))
3870     {
3871     case GIMPLE_GOTO:
3872       op0 = gimple_goto_dest (stmt);
3873       if (TREE_CODE (op0) == LABEL_DECL)
3874           expand_goto (op0);
3875       else
3876           expand_computed_goto (op0);
3877       break;
3878     case GIMPLE_LABEL:
3879       expand_label (gimple_label_label (as_a <glabel *> (stmt)));
3880       break;
3881     case GIMPLE_NOP:
3882     case GIMPLE_PREDICT:
3883       break;
3884     case GIMPLE_SWITCH:
3885       {
3886           gswitch *swtch = as_a <gswitch *> (stmt);
3887           if (gimple_switch_num_labels (swtch) == 1)
3888             expand_goto (CASE_LABEL (gimple_switch_default_label (swtch)));
3889           else
3890             expand_case (swtch);
3891       }
3892       break;
3893     case GIMPLE_ASM:
3894       expand_asm_stmt (as_a <gasm *> (stmt));
3895       break;
3896     case GIMPLE_CALL:
3897       expand_call_stmt (as_a <gcall *> (stmt));
3898       break;
3899 
3900     case GIMPLE_RETURN:
3901       {
3902           op0 = gimple_return_retval (as_a <greturn *> (stmt));
3903 
3904           /* If a return doesn't have a location, it very likely represents
3905              multiple user returns so we cannot let it inherit the location
3906              of the last statement of the previous basic block in RTL.  */
3907           if (!gimple_has_location (stmt))
3908             set_curr_insn_location (cfun->function_end_locus);
3909 
3910           if (op0 && op0 != error_mark_node)
3911             {
3912               tree result = DECL_RESULT (current_function_decl);
3913 
3914               /* If we are not returning the current function's RESULT_DECL,
3915                  build an assignment to it.  */
3916               if (op0 != result)
3917                 {
3918                     /* I believe that a function's RESULT_DECL is unique.  */
3919                     gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3920 
3921                     /* ??? We'd like to use simply expand_assignment here,
3922                        but this fails if the value is of BLKmode but the return
3923                        decl is a register.  expand_return has special handling
3924                        for this combination, which eventually should move
3925                        to common code.  See comments there.  Until then, let's
3926                        build a modify expression :-/  */
3927                     op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3928                                     result, op0);
3929                 }
3930             }
3931 
3932           if (!op0)
3933             expand_null_return ();
3934           else
3935             expand_return (op0);
3936       }
3937       break;
3938 
3939     case GIMPLE_ASSIGN:
3940       {
3941           gassign *assign_stmt = as_a <gassign *> (stmt);
3942           tree lhs = gimple_assign_lhs (assign_stmt);
3943 
3944           /* Tree expand used to fiddle with |= and &= of two bitfield
3945              COMPONENT_REFs here.  This can't happen with gimple, the LHS
3946              of binary assigns must be a gimple reg.  */
3947 
3948           if (TREE_CODE (lhs) != SSA_NAME
3949               || gimple_assign_rhs_class (assign_stmt) == GIMPLE_SINGLE_RHS)
3950             {
3951               tree rhs = gimple_assign_rhs1 (assign_stmt);
3952               gcc_assert (gimple_assign_rhs_class (assign_stmt)
3953                               == GIMPLE_SINGLE_RHS);
3954               if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs)
3955                     /* Do not put locations on possibly shared trees.  */
3956                     && !is_gimple_min_invariant (rhs))
3957                 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3958               if (TREE_CLOBBER_P (rhs))
3959                 /* This is a clobber to mark the going out of scope for
3960                      this LHS.  */
3961                 expand_clobber (lhs);
3962               else
3963                 expand_assignment (lhs, rhs,
3964                                          gimple_assign_nontemporal_move_p (
3965                                            assign_stmt));
3966             }
3967           else
3968             {
3969               rtx target, temp;
3970               bool nontemporal = gimple_assign_nontemporal_move_p (assign_stmt);
3971               struct separate_ops ops;
3972               bool promoted = false;
3973 
3974               target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3975               if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
3976                 promoted = true;
3977 
3978               ops.code = gimple_assign_rhs_code (assign_stmt);
3979               ops.type = TREE_TYPE (lhs);
3980               switch (get_gimple_rhs_class (ops.code))
3981                 {
3982                     case GIMPLE_TERNARY_RHS:
3983                       ops.op2 = gimple_assign_rhs3 (assign_stmt);
3984                       /* Fallthru */
3985                     case GIMPLE_BINARY_RHS:
3986                       ops.op1 = gimple_assign_rhs2 (assign_stmt);
3987                       /* Fallthru */
3988                     case GIMPLE_UNARY_RHS:
3989                       ops.op0 = gimple_assign_rhs1 (assign_stmt);
3990                       break;
3991                     default:
3992                       gcc_unreachable ();
3993                 }
3994               ops.location = gimple_location (stmt);
3995 
3996               /* If we want to use a nontemporal store, force the value to
3997                  register first.  If we store into a promoted register,
3998                  don't directly expand to target.  */
3999               temp = nontemporal || promoted ? NULL_RTX : target;
4000               temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
4001                                                EXPAND_NORMAL);
4002 
4003               if (temp == target)
4004                 ;
4005               else if (promoted)
4006                 {
4007                     int unsignedp = SUBREG_PROMOTED_SIGN (target);
4008                     /* If TEMP is a VOIDmode constant, use convert_modes to make
4009                        sure that we properly convert it.  */
4010                     if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
4011                       {
4012                         temp = convert_modes (GET_MODE (target),
4013                                                     TYPE_MODE (ops.type),
4014                                                     temp, unsignedp);
4015                         temp = convert_modes (GET_MODE (SUBREG_REG (target)),
4016                                                     GET_MODE (target), temp, unsignedp);
4017                       }
4018 
4019                     convert_move (SUBREG_REG (target), temp, unsignedp);
4020                 }
4021               else if (nontemporal && emit_storent_insn (target, temp))
4022                 ;
4023               else
4024                 {
4025                     temp = force_operand (temp, target);
4026                     if (temp != target)
4027                       emit_move_insn (target, temp);
4028                 }
4029             }
4030       }
4031       break;
4032 
4033     default:
4034       gcc_unreachable ();
4035     }
4036 }
4037 
4038 /* Expand one gimple statement STMT and return the last RTL instruction
4039    before any of the newly generated ones.
4040 
4041    In addition to generating the necessary RTL instructions this also
4042    sets REG_EH_REGION notes if necessary and sets the current source
4043    location for diagnostics.  */
4044 
4045 static rtx_insn *
expand_gimple_stmt(gimple * stmt)4046 expand_gimple_stmt (gimple *stmt)
4047 {
4048   location_t saved_location = input_location;
4049   rtx_insn *last = get_last_insn ();
4050   int lp_nr;
4051 
4052   gcc_assert (cfun);
4053 
4054   /* We need to save and restore the current source location so that errors
4055      discovered during expansion are emitted with the right location.  But
4056      it would be better if the diagnostic routines used the source location
4057      embedded in the tree nodes rather than globals.  */
4058   if (gimple_has_location (stmt))
4059     input_location = gimple_location (stmt);
4060 
4061   expand_gimple_stmt_1 (stmt);
4062 
4063   /* Free any temporaries used to evaluate this statement.  */
4064   free_temp_slots ();
4065 
4066   input_location = saved_location;
4067 
4068   /* Mark all insns that may trap.  */
4069   lp_nr = lookup_stmt_eh_lp (stmt);
4070   if (lp_nr)
4071     {
4072       rtx_insn *insn;
4073       for (insn = next_real_insn (last); insn;
4074              insn = next_real_insn (insn))
4075           {
4076             if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
4077                 /* If we want exceptions for non-call insns, any
4078                      may_trap_p instruction may throw.  */
4079                 && GET_CODE (PATTERN (insn)) != CLOBBER
4080                 && GET_CODE (PATTERN (insn)) != USE
4081                 && insn_could_throw_p (insn))
4082               make_reg_eh_region_note (insn, 0, lp_nr);
4083           }
4084     }
4085 
4086   return last;
4087 }
4088 
4089 /* A subroutine of expand_gimple_basic_block.  Expand one GIMPLE_CALL
4090    that has CALL_EXPR_TAILCALL set.  Returns non-null if we actually
4091    generated a tail call (something that might be denied by the ABI
4092    rules governing the call; see calls.cc).
4093 
4094    Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
4095    can still reach the rest of BB.  The case here is __builtin_sqrt,
4096    where the NaN result goes through the external function (with a
4097    tailcall) and the normal result happens via a sqrt instruction.  */
4098 
4099 static basic_block
expand_gimple_tailcall(basic_block bb,gcall * stmt,bool * can_fallthru)4100 expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
4101 {
4102   rtx_insn *last2, *last;
4103   edge e;
4104   edge_iterator ei;
4105   profile_probability probability;
4106 
4107   last2 = last = expand_gimple_stmt (stmt);
4108 
4109   for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
4110     if (CALL_P (last) && SIBLING_CALL_P (last))
4111       goto found;
4112 
4113   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
4114 
4115   *can_fallthru = true;
4116   return NULL;
4117 
4118  found:
4119   /* ??? Wouldn't it be better to just reset any pending stack adjust?
4120      Any instructions emitted here are about to be deleted.  */
4121   do_pending_stack_adjust ();
4122 
4123   /* Remove any non-eh, non-abnormal edges that don't go to exit.  */
4124   /* ??? I.e. the fallthrough edge.  HOWEVER!  If there were to be
4125      EH or abnormal edges, we shouldn't have created a tail call in
4126      the first place.  So it seems to me we should just be removing
4127      all edges here, or redirecting the existing fallthru edge to
4128      the exit block.  */
4129 
4130   probability = profile_probability::never ();
4131 
4132   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
4133     {
4134       if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
4135           {
4136             if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
4137               e->dest->count -= e->count ();
4138             probability += e->probability;
4139             remove_edge (e);
4140           }
4141       else
4142           ei_next (&ei);
4143     }
4144 
4145   /* This is somewhat ugly: the call_expr expander often emits instructions
4146      after the sibcall (to perform the function return).  These confuse the
4147      find_many_sub_basic_blocks code, so we need to get rid of these.  */
4148   last = NEXT_INSN (last);
4149   gcc_assert (BARRIER_P (last));
4150 
4151   *can_fallthru = false;
4152   while (NEXT_INSN (last))
4153     {
4154       /* For instance an sqrt builtin expander expands if with
4155            sibcall in the then and label for `else`.  */
4156       if (LABEL_P (NEXT_INSN (last)))
4157           {
4158             *can_fallthru = true;
4159             break;
4160           }
4161       delete_insn (NEXT_INSN (last));
4162     }
4163 
4164   e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
4165                      | EDGE_SIBCALL);
4166   e->probability = probability;
4167   BB_END (bb) = last;
4168   update_bb_for_insn (bb);
4169 
4170   if (NEXT_INSN (last))
4171     {
4172       bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
4173 
4174       last = BB_END (bb);
4175       if (BARRIER_P (last))
4176           BB_END (bb) = PREV_INSN (last);
4177     }
4178 
4179   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
4180 
4181   return bb;
4182 }
4183 
4184 /* Return the difference between the floor and the truncated result of
4185    a signed division by OP1 with remainder MOD.  */
4186 static rtx
floor_sdiv_adjust(machine_mode mode,rtx mod,rtx op1)4187 floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
4188 {
4189   /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
4190   return gen_rtx_IF_THEN_ELSE
4191     (mode, gen_rtx_NE (BImode, mod, const0_rtx),
4192      gen_rtx_IF_THEN_ELSE
4193      (mode, gen_rtx_LT (BImode,
4194                               gen_rtx_DIV (mode, op1, mod),
4195                               const0_rtx),
4196       constm1_rtx, const0_rtx),
4197      const0_rtx);
4198 }
4199 
4200 /* Return the difference between the ceil and the truncated result of
4201    a signed division by OP1 with remainder MOD.  */
4202 static rtx
ceil_sdiv_adjust(machine_mode mode,rtx mod,rtx op1)4203 ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
4204 {
4205   /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
4206   return gen_rtx_IF_THEN_ELSE
4207     (mode, gen_rtx_NE (BImode, mod, const0_rtx),
4208      gen_rtx_IF_THEN_ELSE
4209      (mode, gen_rtx_GT (BImode,
4210                               gen_rtx_DIV (mode, op1, mod),
4211                               const0_rtx),
4212       const1_rtx, const0_rtx),
4213      const0_rtx);
4214 }
4215 
4216 /* Return the difference between the ceil and the truncated result of
4217    an unsigned division by OP1 with remainder MOD.  */
4218 static rtx
ceil_udiv_adjust(machine_mode mode,rtx mod,rtx op1 ATTRIBUTE_UNUSED)4219 ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
4220 {
4221   /* (mod != 0 ? 1 : 0) */
4222   return gen_rtx_IF_THEN_ELSE
4223     (mode, gen_rtx_NE (BImode, mod, const0_rtx),
4224      const1_rtx, const0_rtx);
4225 }
4226 
4227 /* Return the difference between the rounded and the truncated result
4228    of a signed division by OP1 with remainder MOD.  Halfway cases are
4229    rounded away from zero, rather than to the nearest even number.  */
4230 static rtx
round_sdiv_adjust(machine_mode mode,rtx mod,rtx op1)4231 round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
4232 {
4233   /* (abs (mod) >= abs (op1) - abs (mod)
4234       ? (op1 / mod > 0 ? 1 : -1)
4235       : 0) */
4236   return gen_rtx_IF_THEN_ELSE
4237     (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
4238                            gen_rtx_MINUS (mode,
4239                                               gen_rtx_ABS (mode, op1),
4240                                               gen_rtx_ABS (mode, mod))),
4241      gen_rtx_IF_THEN_ELSE
4242      (mode, gen_rtx_GT (BImode,
4243                               gen_rtx_DIV (mode, op1, mod),
4244                               const0_rtx),
4245       const1_rtx, constm1_rtx),
4246      const0_rtx);
4247 }
4248 
4249 /* Return the difference between the rounded and the truncated result
4250    of a unsigned division by OP1 with remainder MOD.  Halfway cases
4251    are rounded away from zero, rather than to the nearest even
4252    number.  */
4253 static rtx
round_udiv_adjust(machine_mode mode,rtx mod,rtx op1)4254 round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
4255 {
4256   /* (mod >= op1 - mod ? 1 : 0) */
4257   return gen_rtx_IF_THEN_ELSE
4258     (mode, gen_rtx_GE (BImode, mod,
4259                            gen_rtx_MINUS (mode, op1, mod)),
4260      const1_rtx, const0_rtx);
4261 }
4262 
4263 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
4264    any rtl.  */
4265 
4266 static rtx
convert_debug_memory_address(scalar_int_mode mode,rtx x,addr_space_t as)4267 convert_debug_memory_address (scalar_int_mode mode, rtx x,
4268                                     addr_space_t as)
4269 {
4270 #ifndef POINTERS_EXTEND_UNSIGNED
4271   gcc_assert (mode == Pmode
4272                 || mode == targetm.addr_space.address_mode (as));
4273   gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
4274 #else
4275   rtx temp;
4276 
4277   gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
4278 
4279   if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
4280     return x;
4281 
4282   /* X must have some form of address mode already.  */
4283   scalar_int_mode xmode = as_a <scalar_int_mode> (GET_MODE (x));
4284   if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
4285     x = lowpart_subreg (mode, x, xmode);
4286   else if (POINTERS_EXTEND_UNSIGNED > 0)
4287     x = gen_rtx_ZERO_EXTEND (mode, x);
4288   else if (!POINTERS_EXTEND_UNSIGNED)
4289     x = gen_rtx_SIGN_EXTEND (mode, x);
4290   else
4291     {
4292       switch (GET_CODE (x))
4293           {
4294           case SUBREG:
4295             if ((SUBREG_PROMOTED_VAR_P (x)
4296                  || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
4297                  || (GET_CODE (SUBREG_REG (x)) == PLUS
4298                        && REG_P (XEXP (SUBREG_REG (x), 0))
4299                        && REG_POINTER (XEXP (SUBREG_REG (x), 0))
4300                        && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
4301                 && GET_MODE (SUBREG_REG (x)) == mode)
4302               return SUBREG_REG (x);
4303             break;
4304           case LABEL_REF:
4305             temp = gen_rtx_LABEL_REF (mode, label_ref_label (x));
4306             LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
4307             return temp;
4308           case SYMBOL_REF:
4309             temp = shallow_copy_rtx (x);
4310             PUT_MODE (temp, mode);
4311             return temp;
4312           case CONST:
4313             temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
4314             if (temp)
4315               temp = gen_rtx_CONST (mode, temp);
4316             return temp;
4317           case PLUS:
4318           case MINUS:
4319             if (CONST_INT_P (XEXP (x, 1)))
4320               {
4321                 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
4322                 if (temp)
4323                     return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
4324               }
4325             break;
4326           default:
4327             break;
4328           }
4329       /* Don't know how to express ptr_extend as operation in debug info.  */
4330       return NULL;
4331     }
4332 #endif /* POINTERS_EXTEND_UNSIGNED */
4333 
4334   return x;
4335 }
4336 
4337 /* Map from SSA_NAMEs to corresponding DEBUG_EXPR_DECLs created
4338    by avoid_deep_ter_for_debug.  */
4339 
4340 static hash_map<tree, tree> *deep_ter_debug_map;
4341 
4342 /* Split too deep TER chains for debug stmts using debug temporaries.  */
4343 
4344 static void
avoid_deep_ter_for_debug(gimple * stmt,int depth)4345 avoid_deep_ter_for_debug (gimple *stmt, int depth)
4346 {
4347   use_operand_p use_p;
4348   ssa_op_iter iter;
4349   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
4350     {
4351       tree use = USE_FROM_PTR (use_p);
4352       if (TREE_CODE (use) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (use))
4353           continue;
4354       gimple *g = get_gimple_for_ssa_name (use);
4355       if (g == NULL)
4356           continue;
4357       if (depth > 6 && !stmt_ends_bb_p (g))
4358           {
4359             if (deep_ter_debug_map == NULL)
4360               deep_ter_debug_map = new hash_map<tree, tree>;
4361 
4362             tree &vexpr = deep_ter_debug_map->get_or_insert (use);
4363             if (vexpr != NULL)
4364               continue;
4365             vexpr = build_debug_expr_decl (TREE_TYPE (use));
4366             gimple *def_temp = gimple_build_debug_bind (vexpr, use, g);
4367             gimple_stmt_iterator gsi = gsi_for_stmt (g);
4368             gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
4369             avoid_deep_ter_for_debug (def_temp, 0);
4370           }
4371       else
4372           avoid_deep_ter_for_debug (g, depth + 1);
4373     }
4374 }
4375 
4376 /* Return an RTX equivalent to the value of the parameter DECL.  */
4377 
4378 static rtx
expand_debug_parm_decl(tree decl)4379 expand_debug_parm_decl (tree decl)
4380 {
4381   rtx incoming = DECL_INCOMING_RTL (decl);
4382 
4383   if (incoming
4384       && GET_MODE (incoming) != BLKmode
4385       && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
4386             || (MEM_P (incoming)
4387                 && REG_P (XEXP (incoming, 0))
4388                 && HARD_REGISTER_P (XEXP (incoming, 0)))))
4389     {
4390       rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
4391 
4392 #ifdef HAVE_window_save
4393       /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
4394            If the target machine has an explicit window save instruction, the
4395            actual entry value is the corresponding OUTGOING_REGNO instead.  */
4396       if (REG_P (incoming)
4397             && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
4398           incoming
4399             = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
4400                                         OUTGOING_REGNO (REGNO (incoming)), 0);
4401       else if (MEM_P (incoming))
4402           {
4403             rtx reg = XEXP (incoming, 0);
4404             if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
4405               {
4406                 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
4407                 incoming = replace_equiv_address_nv (incoming, reg);
4408               }
4409             else
4410               incoming = copy_rtx (incoming);
4411           }
4412 #endif
4413 
4414       ENTRY_VALUE_EXP (rtl) = incoming;
4415       return rtl;
4416     }
4417 
4418   if (incoming
4419       && GET_MODE (incoming) != BLKmode
4420       && !TREE_ADDRESSABLE (decl)
4421       && MEM_P (incoming)
4422       && (XEXP (incoming, 0) == virtual_incoming_args_rtx
4423             || (GET_CODE (XEXP (incoming, 0)) == PLUS
4424                 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
4425                 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
4426     return copy_rtx (incoming);
4427 
4428   return NULL_RTX;
4429 }
4430 
4431 /* Return an RTX equivalent to the value of the tree expression EXP.  */
4432 
4433 static rtx
expand_debug_expr(tree exp)4434 expand_debug_expr (tree exp)
4435 {
4436   rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
4437   machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4438   machine_mode inner_mode = VOIDmode;
4439   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
4440   addr_space_t as;
4441   scalar_int_mode op0_mode, op1_mode, addr_mode;
4442 
4443   switch (TREE_CODE_CLASS (TREE_CODE (exp)))
4444     {
4445     case tcc_expression:
4446       switch (TREE_CODE (exp))
4447           {
4448           case COND_EXPR:
4449           case DOT_PROD_EXPR:
4450           case SAD_EXPR:
4451           case WIDEN_MULT_PLUS_EXPR:
4452           case WIDEN_MULT_MINUS_EXPR:
4453             goto ternary;
4454 
4455           case TRUTH_ANDIF_EXPR:
4456           case TRUTH_ORIF_EXPR:
4457           case TRUTH_AND_EXPR:
4458           case TRUTH_OR_EXPR:
4459           case TRUTH_XOR_EXPR:
4460             goto binary;
4461 
4462           case TRUTH_NOT_EXPR:
4463             goto unary;
4464 
4465           default:
4466             break;
4467           }
4468       break;
4469 
4470     ternary:
4471       op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
4472       if (!op2)
4473           return NULL_RTX;
4474       /* Fall through.  */
4475 
4476     binary:
4477     case tcc_binary:
4478       if (mode == BLKmode)
4479           return NULL_RTX;
4480       op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4481       if (!op1)
4482           return NULL_RTX;
4483       switch (TREE_CODE (exp))
4484           {
4485           case LSHIFT_EXPR:
4486           case RSHIFT_EXPR:
4487           case LROTATE_EXPR:
4488           case RROTATE_EXPR:
4489           case WIDEN_LSHIFT_EXPR:
4490             /* Ensure second operand isn't wider than the first one.  */
4491             inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 1)));
4492             if (is_a <scalar_int_mode> (inner_mode, &op1_mode)
4493                 && (GET_MODE_UNIT_PRECISION (mode)
4494                       < GET_MODE_PRECISION (op1_mode)))
4495               op1 = lowpart_subreg (GET_MODE_INNER (mode), op1, op1_mode);
4496             break;
4497           default:
4498             break;
4499           }
4500       /* Fall through.  */
4501 
4502     unary:
4503     case tcc_unary:
4504       if (mode == BLKmode)
4505           return NULL_RTX;
4506       inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4507       op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4508       if (!op0)
4509           return NULL_RTX;
4510       break;
4511 
4512     case tcc_comparison:
4513       unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
4514       goto binary;
4515 
4516     case tcc_type:
4517     case tcc_statement:
4518       gcc_unreachable ();
4519 
4520     case tcc_constant:
4521     case tcc_exceptional:
4522     case tcc_declaration:
4523     case tcc_reference:
4524     case tcc_vl_exp:
4525       break;
4526     }
4527 
4528   switch (TREE_CODE (exp))
4529     {
4530     case STRING_CST:
4531       if (!lookup_constant_def (exp))
4532           {
4533             if (strlen (TREE_STRING_POINTER (exp)) + 1
4534                 != (size_t) TREE_STRING_LENGTH (exp))
4535               return NULL_RTX;
4536             op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
4537             op0 = gen_rtx_MEM (BLKmode, op0);
4538             set_mem_attributes (op0, exp, 0);
4539             return op0;
4540           }
4541       /* Fall through.  */
4542 
4543     case INTEGER_CST:
4544     case REAL_CST:
4545     case FIXED_CST:
4546       op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
4547       return op0;
4548 
4549     case POLY_INT_CST:
4550       return immed_wide_int_const (poly_int_cst_value (exp), mode);
4551 
4552     case COMPLEX_CST:
4553       gcc_assert (COMPLEX_MODE_P (mode));
4554       op0 = expand_debug_expr (TREE_REALPART (exp));
4555       op1 = expand_debug_expr (TREE_IMAGPART (exp));
4556       return gen_rtx_CONCAT (mode, op0, op1);
4557 
4558     case DEBUG_EXPR_DECL:
4559       op0 = DECL_RTL_IF_SET (exp);
4560 
4561       if (op0)
4562           {
4563             if (GET_MODE (op0) != mode)
4564               gcc_assert (VECTOR_TYPE_P (TREE_TYPE (exp)));
4565             else
4566               return op0;
4567           }
4568 
4569       op0 = gen_rtx_DEBUG_EXPR (mode);
4570       DEBUG_EXPR_TREE_DECL (op0) = exp;
4571       SET_DECL_RTL (exp, op0);
4572 
4573       return op0;
4574 
4575     case VAR_DECL:
4576     case PARM_DECL:
4577     case FUNCTION_DECL:
4578     case LABEL_DECL:
4579     case CONST_DECL:
4580     case RESULT_DECL:
4581       op0 = DECL_RTL_IF_SET (exp);
4582 
4583       /* This decl was probably optimized away.  */
4584       if (!op0
4585             /* At least label RTXen are sometimes replaced by
4586                NOTE_INSN_DELETED_LABEL.  Any notes here are not
4587                handled by copy_rtx.  */
4588             || NOTE_P (op0))
4589           {
4590             if (!VAR_P (exp)
4591                 || DECL_EXTERNAL (exp)
4592                 || !TREE_STATIC (exp)
4593                 || !DECL_NAME (exp)
4594                 || DECL_HARD_REGISTER (exp)
4595                 || DECL_IN_CONSTANT_POOL (exp)
4596                 || mode == VOIDmode)
4597               return NULL;
4598 
4599             op0 = make_decl_rtl_for_debug (exp);
4600             if (!MEM_P (op0)
4601                 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
4602                 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
4603               return NULL;
4604           }
4605       else
4606           op0 = copy_rtx (op0);
4607 
4608       if (GET_MODE (op0) == BLKmode
4609             /* If op0 is not BLKmode, but mode is, adjust_mode
4610                below would ICE.  While it is likely a FE bug,
4611                try to be robust here.  See PR43166.  */
4612             || mode == BLKmode
4613             || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
4614           {
4615             gcc_assert (MEM_P (op0));
4616             op0 = adjust_address_nv (op0, mode, 0);
4617             return op0;
4618           }
4619 
4620       /* Fall through.  */
4621 
4622     adjust_mode:
4623     case PAREN_EXPR:
4624     CASE_CONVERT:
4625       {
4626           inner_mode = GET_MODE (op0);
4627 
4628           if (mode == inner_mode)
4629             return op0;
4630 
4631           if (inner_mode == VOIDmode)
4632             {
4633               if (TREE_CODE (exp) == SSA_NAME)
4634                 inner_mode = TYPE_MODE (TREE_TYPE (exp));
4635               else
4636                 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4637               if (mode == inner_mode)
4638                 return op0;
4639             }
4640 
4641           if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4642             {
4643               if (GET_MODE_UNIT_BITSIZE (mode)
4644                     == GET_MODE_UNIT_BITSIZE (inner_mode))
4645                 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4646               else if (GET_MODE_UNIT_BITSIZE (mode)
4647                          < GET_MODE_UNIT_BITSIZE (inner_mode))
4648                 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4649               else
4650                 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4651             }
4652           else if (FLOAT_MODE_P (mode))
4653             {
4654               gcc_assert (TREE_CODE (exp) != SSA_NAME);
4655               if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4656                 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
4657               else
4658                 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
4659             }
4660           else if (FLOAT_MODE_P (inner_mode))
4661             {
4662               if (unsignedp)
4663                 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4664               else
4665                 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4666             }
4667           else if (GET_MODE_UNIT_PRECISION (mode)
4668                      == GET_MODE_UNIT_PRECISION (inner_mode))
4669             op0 = lowpart_subreg (mode, op0, inner_mode);
4670           else if (GET_MODE_UNIT_PRECISION (mode)
4671                      < GET_MODE_UNIT_PRECISION (inner_mode))
4672             op0 = simplify_gen_unary (TRUNCATE, mode, op0, inner_mode);
4673           else if (UNARY_CLASS_P (exp)
4674                      ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
4675                      : unsignedp)
4676             op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4677           else
4678             op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4679 
4680           return op0;
4681       }
4682 
4683     case MEM_REF:
4684       if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4685           {
4686             tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
4687                                              TREE_OPERAND (exp, 0),
4688                                              TREE_OPERAND (exp, 1));
4689             if (newexp)
4690               return expand_debug_expr (newexp);
4691           }
4692       /* FALLTHROUGH */
4693     case INDIRECT_REF:
4694       inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4695       op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4696       if (!op0)
4697           return NULL;
4698 
4699       if (TREE_CODE (exp) == MEM_REF)
4700           {
4701             if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4702                 || (GET_CODE (op0) == PLUS
4703                       && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
4704               /* (mem (debug_implicit_ptr)) might confuse aliasing.
4705                  Instead just use get_inner_reference.  */
4706               goto component_ref;
4707 
4708             op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4709             poly_int64 offset;
4710             if (!op1 || !poly_int_rtx_p (op1, &offset))
4711               return NULL;
4712 
4713             op0 = plus_constant (inner_mode, op0, offset);
4714           }
4715 
4716       as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4717 
4718       op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4719                                                     op0, as);
4720       if (op0 == NULL_RTX)
4721           return NULL;
4722 
4723       op0 = gen_rtx_MEM (mode, op0);
4724       set_mem_attributes (op0, exp, 0);
4725       if (TREE_CODE (exp) == MEM_REF
4726             && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4727           set_mem_expr (op0, NULL_TREE);
4728       set_mem_addr_space (op0, as);
4729 
4730       return op0;
4731 
4732     case TARGET_MEM_REF:
4733       if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
4734             && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
4735           return NULL;
4736 
4737       op0 = expand_debug_expr
4738               (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
4739       if (!op0)
4740           return NULL;
4741 
4742       as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4743       op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4744                                                     op0, as);
4745       if (op0 == NULL_RTX)
4746           return NULL;
4747 
4748       op0 = gen_rtx_MEM (mode, op0);
4749 
4750       set_mem_attributes (op0, exp, 0);
4751       set_mem_addr_space (op0, as);
4752 
4753       return op0;
4754 
4755     component_ref:
4756     case ARRAY_REF:
4757     case ARRAY_RANGE_REF:
4758     case COMPONENT_REF:
4759     case BIT_FIELD_REF:
4760     case REALPART_EXPR:
4761     case IMAGPART_EXPR:
4762     case VIEW_CONVERT_EXPR:
4763       {
4764           machine_mode mode1;
4765           poly_int64 bitsize, bitpos;
4766           tree offset;
4767           int reversep, volatilep = 0;
4768           tree tem
4769             = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
4770                                          &unsignedp, &reversep, &volatilep);
4771           rtx orig_op0;
4772 
4773           if (known_eq (bitsize, 0))
4774             return NULL;
4775 
4776           orig_op0 = op0 = expand_debug_expr (tem);
4777 
4778           if (!op0)
4779             return NULL;
4780 
4781           if (offset)
4782             {
4783               machine_mode addrmode, offmode;
4784 
4785               if (!MEM_P (op0))
4786                 return NULL;
4787 
4788               op0 = XEXP (op0, 0);
4789               addrmode = GET_MODE (op0);
4790               if (addrmode == VOIDmode)
4791                 addrmode = Pmode;
4792 
4793               op1 = expand_debug_expr (offset);
4794               if (!op1)
4795                 return NULL;
4796 
4797               offmode = GET_MODE (op1);
4798               if (offmode == VOIDmode)
4799                 offmode = TYPE_MODE (TREE_TYPE (offset));
4800 
4801               if (addrmode != offmode)
4802                 op1 = lowpart_subreg (addrmode, op1, offmode);
4803 
4804               /* Don't use offset_address here, we don't need a
4805                  recognizable address, and we don't want to generate
4806                  code.  */
4807               op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4808                                                                         op0, op1));
4809             }
4810 
4811           if (MEM_P (op0))
4812             {
4813               if (mode1 == VOIDmode)
4814                 {
4815                     if (maybe_gt (bitsize, MAX_BITSIZE_MODE_ANY_INT))
4816                       return NULL;
4817                     /* Bitfield.  */
4818                     mode1 = smallest_int_mode_for_size (bitsize);
4819                 }
4820               poly_int64 bytepos = bits_to_bytes_round_down (bitpos);
4821               if (maybe_ne (bytepos, 0))
4822                 {
4823                     op0 = adjust_address_nv (op0, mode1, bytepos);
4824                     bitpos = num_trailing_bits (bitpos);
4825                 }
4826               else if (known_eq (bitpos, 0)
4827                          && known_eq (bitsize, GET_MODE_BITSIZE (mode)))
4828                 op0 = adjust_address_nv (op0, mode, 0);
4829               else if (GET_MODE (op0) != mode1)
4830                 op0 = adjust_address_nv (op0, mode1, 0);
4831               else
4832                 op0 = copy_rtx (op0);
4833               if (op0 == orig_op0)
4834                 op0 = shallow_copy_rtx (op0);
4835               if (TREE_CODE (tem) != SSA_NAME)
4836                 set_mem_attributes (op0, exp, 0);
4837             }
4838 
4839           if (known_eq (bitpos, 0) && mode == GET_MODE (op0))
4840             return op0;
4841 
4842           if (maybe_lt (bitpos, 0))
4843           return NULL;
4844 
4845           if (GET_MODE (op0) == BLKmode || mode == BLKmode)
4846             return NULL;
4847 
4848           poly_int64 bytepos;
4849           if (multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
4850               && known_eq (bitsize, GET_MODE_BITSIZE (mode1)))
4851             {
4852               machine_mode opmode = GET_MODE (op0);
4853 
4854               if (opmode == VOIDmode)
4855                 opmode = TYPE_MODE (TREE_TYPE (tem));
4856 
4857               /* This condition may hold if we're expanding the address
4858                  right past the end of an array that turned out not to
4859                  be addressable (i.e., the address was only computed in
4860                  debug stmts).  The gen_subreg below would rightfully
4861                  crash, and the address doesn't really exist, so just
4862                  drop it.  */
4863               if (known_ge (bitpos, GET_MODE_BITSIZE (opmode)))
4864                 return NULL;
4865 
4866               if (multiple_p (bitpos, GET_MODE_BITSIZE (mode)))
4867                 return simplify_gen_subreg (mode, op0, opmode, bytepos);
4868             }
4869 
4870           return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4871                                              && TYPE_UNSIGNED (TREE_TYPE (exp))
4872                                              ? SIGN_EXTRACT
4873                                              : ZERO_EXTRACT, mode,
4874                                              GET_MODE (op0) != VOIDmode
4875                                              ? GET_MODE (op0)
4876                                              : TYPE_MODE (TREE_TYPE (tem)),
4877                                              op0, gen_int_mode (bitsize, word_mode),
4878                                              gen_int_mode (bitpos, word_mode));
4879       }
4880 
4881     case ABS_EXPR:
4882     case ABSU_EXPR:
4883       return simplify_gen_unary (ABS, mode, op0, mode);
4884 
4885     case NEGATE_EXPR:
4886       return simplify_gen_unary (NEG, mode, op0, mode);
4887 
4888     case BIT_NOT_EXPR:
4889       return simplify_gen_unary (NOT, mode, op0, mode);
4890 
4891     case FLOAT_EXPR:
4892       return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4893                                                                                            0)))
4894                                          ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4895                                          inner_mode);
4896 
4897     case FIX_TRUNC_EXPR:
4898       return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4899                                          inner_mode);
4900 
4901     case POINTER_PLUS_EXPR:
4902       /* For the rare target where pointers are not the same size as
4903            size_t, we need to check for mis-matched modes and correct
4904            the addend.  */
4905       if (op0 && op1
4906             && is_a <scalar_int_mode> (GET_MODE (op0), &op0_mode)
4907             && is_a <scalar_int_mode> (GET_MODE (op1), &op1_mode)
4908             && op0_mode != op1_mode)
4909           {
4910             if (GET_MODE_BITSIZE (op0_mode) < GET_MODE_BITSIZE (op1_mode)
4911                 /* If OP0 is a partial mode, then we must truncate, even
4912                      if it has the same bitsize as OP1 as GCC's
4913                      representation of partial modes is opaque.  */
4914                 || (GET_MODE_CLASS (op0_mode) == MODE_PARTIAL_INT
4915                       && (GET_MODE_BITSIZE (op0_mode)
4916                           == GET_MODE_BITSIZE (op1_mode))))
4917               op1 = simplify_gen_unary (TRUNCATE, op0_mode, op1, op1_mode);
4918             else
4919               /* We always sign-extend, regardless of the signedness of
4920                  the operand, because the operand is always unsigned
4921                  here even if the original C expression is signed.  */
4922               op1 = simplify_gen_unary (SIGN_EXTEND, op0_mode, op1, op1_mode);
4923           }
4924       /* Fall through.  */
4925     case PLUS_EXPR:
4926       return simplify_gen_binary (PLUS, mode, op0, op1);
4927 
4928     case MINUS_EXPR:
4929     case POINTER_DIFF_EXPR:
4930       return simplify_gen_binary (MINUS, mode, op0, op1);
4931 
4932     case MULT_EXPR:
4933       return simplify_gen_binary (MULT, mode, op0, op1);
4934 
4935     case RDIV_EXPR:
4936     case TRUNC_DIV_EXPR:
4937     case EXACT_DIV_EXPR:
4938       if (unsignedp)
4939           return simplify_gen_binary (UDIV, mode, op0, op1);
4940       else
4941           return simplify_gen_binary (DIV, mode, op0, op1);
4942 
4943     case TRUNC_MOD_EXPR:
4944       return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4945 
4946     case FLOOR_DIV_EXPR:
4947       if (unsignedp)
4948           return simplify_gen_binary (UDIV, mode, op0, op1);
4949       else
4950           {
4951             rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4952             rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4953             rtx adj = floor_sdiv_adjust (mode, mod, op1);
4954             return simplify_gen_binary (PLUS, mode, div, adj);
4955           }
4956 
4957     case FLOOR_MOD_EXPR:
4958       if (unsignedp)
4959           return simplify_gen_binary (UMOD, mode, op0, op1);
4960       else
4961           {
4962             rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4963             rtx adj = floor_sdiv_adjust (mode, mod, op1);
4964             adj = simplify_gen_unary (NEG, mode,
4965                                             simplify_gen_binary (MULT, mode, adj, op1),
4966                                             mode);
4967             return simplify_gen_binary (PLUS, mode, mod, adj);
4968           }
4969 
4970     case CEIL_DIV_EXPR:
4971       if (unsignedp)
4972           {
4973             rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4974             rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4975             rtx adj = ceil_udiv_adjust (mode, mod, op1);
4976             return simplify_gen_binary (PLUS, mode, div, adj);
4977           }
4978       else
4979           {
4980             rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4981             rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4982             rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4983             return simplify_gen_binary (PLUS, mode, div, adj);
4984           }
4985 
4986     case CEIL_MOD_EXPR:
4987       if (unsignedp)
4988           {
4989             rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4990             rtx adj = ceil_udiv_adjust (mode, mod, op1);
4991             adj = simplify_gen_unary (NEG, mode,
4992                                             simplify_gen_binary (MULT, mode, adj, op1),
4993                                             mode);
4994             return simplify_gen_binary (PLUS, mode, mod, adj);
4995           }
4996       else
4997           {
4998             rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4999             rtx adj = ceil_sdiv_adjust (mode, mod, op1);
5000             adj = simplify_gen_unary (NEG, mode,
5001                                             simplify_gen_binary (MULT, mode, adj, op1),
5002                                             mode);
5003             return simplify_gen_binary (PLUS, mode, mod, adj);
5004           }
5005 
5006     case ROUND_DIV_EXPR:
5007       if (unsignedp)
5008           {
5009             rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
5010             rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5011             rtx adj = round_udiv_adjust (mode, mod, op1);
5012             return simplify_gen_binary (PLUS, mode, div, adj);
5013           }
5014       else
5015           {
5016             rtx div = simplify_gen_binary (DIV, mode, op0, op1);
5017             rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5018             rtx adj = round_sdiv_adjust (mode, mod, op1);
5019             return simplify_gen_binary (PLUS, mode, div, adj);
5020           }
5021 
5022     case ROUND_MOD_EXPR:
5023       if (unsignedp)
5024           {
5025             rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5026             rtx adj = round_udiv_adjust (mode, mod, op1);
5027             adj = simplify_gen_unary (NEG, mode,
5028                                             simplify_gen_binary (MULT, mode, adj, op1),
5029                                             mode);
5030             return simplify_gen_binary (PLUS, mode, mod, adj);
5031           }
5032       else
5033           {
5034             rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5035             rtx adj = round_sdiv_adjust (mode, mod, op1);
5036             adj = simplify_gen_unary (NEG, mode,
5037                                             simplify_gen_binary (MULT, mode, adj, op1),
5038                                             mode);
5039             return simplify_gen_binary (PLUS, mode, mod, adj);
5040           }
5041 
5042     case LSHIFT_EXPR:
5043       return simplify_gen_binary (ASHIFT, mode, op0, op1);
5044 
5045     case RSHIFT_EXPR:
5046       if (unsignedp)
5047           return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
5048       else
5049           return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
5050 
5051     case LROTATE_EXPR:
5052       return simplify_gen_binary (ROTATE, mode, op0, op1);
5053 
5054     case RROTATE_EXPR:
5055       return simplify_gen_binary (ROTATERT, mode, op0, op1);
5056 
5057     case MIN_EXPR:
5058       return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
5059 
5060     case MAX_EXPR:
5061       return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
5062 
5063     case BIT_AND_EXPR:
5064     case TRUTH_AND_EXPR:
5065       return simplify_gen_binary (AND, mode, op0, op1);
5066 
5067     case BIT_IOR_EXPR:
5068     case TRUTH_OR_EXPR:
5069       return simplify_gen_binary (IOR, mode, op0, op1);
5070 
5071     case BIT_XOR_EXPR:
5072     case TRUTH_XOR_EXPR:
5073       return simplify_gen_binary (XOR, mode, op0, op1);
5074 
5075     case TRUTH_ANDIF_EXPR:
5076       return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
5077 
5078     case TRUTH_ORIF_EXPR:
5079       return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
5080 
5081     case TRUTH_NOT_EXPR:
5082       return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
5083 
5084     case LT_EXPR:
5085       return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
5086                                               op0, op1);
5087 
5088     case LE_EXPR:
5089       return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
5090                                               op0, op1);
5091 
5092     case GT_EXPR:
5093       return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
5094                                               op0, op1);
5095 
5096     case GE_EXPR:
5097       return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
5098                                               op0, op1);
5099 
5100     case EQ_EXPR:
5101       return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
5102 
5103     case NE_EXPR:
5104       return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
5105 
5106     case UNORDERED_EXPR:
5107       return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
5108 
5109     case ORDERED_EXPR:
5110       return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
5111 
5112     case UNLT_EXPR:
5113       return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
5114 
5115     case UNLE_EXPR:
5116       return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
5117 
5118     case UNGT_EXPR:
5119       return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
5120 
5121     case UNGE_EXPR:
5122       return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
5123 
5124     case UNEQ_EXPR:
5125       return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
5126 
5127     case LTGT_EXPR:
5128       return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
5129 
5130     case COND_EXPR:
5131       return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
5132 
5133     case COMPLEX_EXPR:
5134       gcc_assert (COMPLEX_MODE_P (mode));
5135       if (GET_MODE (op0) == VOIDmode)
5136           op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
5137       if (GET_MODE (op1) == VOIDmode)
5138           op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
5139       return gen_rtx_CONCAT (mode, op0, op1);
5140 
5141     case CONJ_EXPR:
5142       if (GET_CODE (op0) == CONCAT)
5143           return gen_rtx_CONCAT (mode, XEXP (op0, 0),
5144                                      simplify_gen_unary (NEG, GET_MODE_INNER (mode),
5145                                                                XEXP (op0, 1),
5146                                                                GET_MODE_INNER (mode)));
5147       else
5148           {
5149             scalar_mode imode = GET_MODE_INNER (mode);
5150             rtx re, im;
5151 
5152             if (MEM_P (op0))
5153               {
5154                 re = adjust_address_nv (op0, imode, 0);
5155                 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
5156               }
5157             else
5158               {
5159                 scalar_int_mode ifmode;
5160                 scalar_int_mode ihmode;
5161                 rtx halfsize;
5162                 if (!int_mode_for_mode (mode).exists (&ifmode)
5163                       || !int_mode_for_mode (imode).exists (&ihmode))
5164                     return NULL;
5165                 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
5166                 re = op0;
5167                 if (mode != ifmode)
5168                     re = gen_rtx_SUBREG (ifmode, re, 0);
5169                 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
5170                 if (imode != ihmode)
5171                     re = gen_rtx_SUBREG (imode, re, 0);
5172                 im = copy_rtx (op0);
5173                 if (mode != ifmode)
5174                     im = gen_rtx_SUBREG (ifmode, im, 0);
5175                 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
5176                 if (imode != ihmode)
5177                     im = gen_rtx_SUBREG (imode, im, 0);
5178               }
5179             im = gen_rtx_NEG (imode, im);
5180             return gen_rtx_CONCAT (mode, re, im);
5181           }
5182 
5183     case ADDR_EXPR:
5184       op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
5185       if (!op0 || !MEM_P (op0))
5186           {
5187             if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
5188                  || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
5189                  || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
5190                 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
5191                       || target_for_debug_bind (TREE_OPERAND (exp, 0))))
5192               return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
5193 
5194             if (handled_component_p (TREE_OPERAND (exp, 0)))
5195               {
5196                 poly_int64 bitoffset, bitsize, maxsize, byteoffset;
5197                 bool reverse;
5198                 tree decl
5199                     = get_ref_base_and_extent (TREE_OPERAND (exp, 0), &bitoffset,
5200                                                      &bitsize, &maxsize, &reverse);
5201                 if ((VAR_P (decl)
5202                        || TREE_CODE (decl) == PARM_DECL
5203                        || TREE_CODE (decl) == RESULT_DECL)
5204                       && (!TREE_ADDRESSABLE (decl)
5205                           || target_for_debug_bind (decl))
5206                       && multiple_p (bitoffset, BITS_PER_UNIT, &byteoffset)
5207                       && known_gt (bitsize, 0)
5208                       && known_eq (bitsize, maxsize))
5209                     {
5210                       rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
5211                       return plus_constant (mode, base, byteoffset);
5212                     }
5213               }
5214 
5215             if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
5216                 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5217                      == ADDR_EXPR)
5218               {
5219                 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
5220                                                                  0));
5221                 if (op0 != NULL
5222                       && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
5223                           || (GET_CODE (op0) == PLUS
5224                                 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
5225                                 && CONST_INT_P (XEXP (op0, 1)))))
5226                     {
5227                       op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
5228                                                                        1));
5229                       poly_int64 offset;
5230                       if (!op1 || !poly_int_rtx_p (op1, &offset))
5231                         return NULL;
5232 
5233                       return plus_constant (mode, op0, offset);
5234                     }
5235               }
5236 
5237             return NULL;
5238           }
5239 
5240       as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
5241       addr_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
5242       op0 = convert_debug_memory_address (addr_mode, XEXP (op0, 0), as);
5243 
5244       return op0;
5245 
5246     case VECTOR_CST:
5247       {
5248           unsigned HOST_WIDE_INT i, nelts;
5249 
5250           if (!VECTOR_CST_NELTS (exp).is_constant (&nelts))
5251             return NULL;
5252 
5253           op0 = gen_rtx_CONCATN (mode, rtvec_alloc (nelts));
5254 
5255           for (i = 0; i < nelts; ++i)
5256             {
5257               op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
5258               if (!op1)
5259                 return NULL;
5260               XVECEXP (op0, 0, i) = op1;
5261             }
5262 
5263           return op0;
5264       }
5265 
5266     case CONSTRUCTOR:
5267       if (TREE_CLOBBER_P (exp))
5268           return NULL;
5269       else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
5270           {
5271             unsigned i;
5272             unsigned HOST_WIDE_INT nelts;
5273             tree val;
5274 
5275             if (!TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)).is_constant (&nelts))
5276               goto flag_unsupported;
5277 
5278             op0 = gen_rtx_CONCATN (mode, rtvec_alloc (nelts));
5279 
5280             FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
5281               {
5282                 op1 = expand_debug_expr (val);
5283                 if (!op1)
5284                     return NULL;
5285                 XVECEXP (op0, 0, i) = op1;
5286               }
5287 
5288             if (i < nelts)
5289               {
5290                 op1 = expand_debug_expr
5291                     (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
5292 
5293                 if (!op1)
5294                     return NULL;
5295 
5296                 for (; i < nelts; i++)
5297                     XVECEXP (op0, 0, i) = op1;
5298               }
5299 
5300             return op0;
5301           }
5302       else
5303           goto flag_unsupported;
5304 
5305     case CALL_EXPR:
5306       /* ??? Maybe handle some builtins?  */
5307       return NULL;
5308 
5309     case SSA_NAME:
5310       {
5311           gimple *g = get_gimple_for_ssa_name (exp);
5312           if (g)
5313             {
5314               tree t = NULL_TREE;
5315               if (deep_ter_debug_map)
5316                 {
5317                     tree *slot = deep_ter_debug_map->get (exp);
5318                     if (slot)
5319                       t = *slot;
5320                 }
5321               if (t == NULL_TREE)
5322                 t = gimple_assign_rhs_to_tree (g);
5323               op0 = expand_debug_expr (t);
5324               if (!op0)
5325                 return NULL;
5326             }
5327           else
5328             {
5329               /* If this is a reference to an incoming value of
5330                  parameter that is never used in the code or where the
5331                  incoming value is never used in the code, use
5332                  PARM_DECL's DECL_RTL if set.  */
5333               if (SSA_NAME_IS_DEFAULT_DEF (exp)
5334                     && SSA_NAME_VAR (exp)
5335                     && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL
5336                     && has_zero_uses (exp))
5337                 {
5338                     op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
5339                     if (op0)
5340                       goto adjust_mode;
5341                     op0 = expand_debug_expr (SSA_NAME_VAR (exp));
5342                     if (op0)
5343                       goto adjust_mode;
5344                 }
5345 
5346               int part = var_to_partition (SA.map, exp);
5347 
5348               if (part == NO_PARTITION)
5349                 return NULL;
5350 
5351               gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
5352 
5353               op0 = copy_rtx (SA.partition_to_pseudo[part]);
5354             }
5355           goto adjust_mode;
5356       }
5357 
5358     case ERROR_MARK:
5359       return NULL;
5360 
5361     /* Vector stuff.  For most of the codes we don't have rtl codes.  */
5362     case REALIGN_LOAD_EXPR:
5363     case VEC_COND_EXPR:
5364     case VEC_PACK_FIX_TRUNC_EXPR:
5365     case VEC_PACK_FLOAT_EXPR:
5366     case VEC_PACK_SAT_EXPR:
5367     case VEC_PACK_TRUNC_EXPR:
5368     case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
5369     case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
5370     case VEC_UNPACK_FLOAT_HI_EXPR:
5371     case VEC_UNPACK_FLOAT_LO_EXPR:
5372     case VEC_UNPACK_HI_EXPR:
5373     case VEC_UNPACK_LO_EXPR:
5374     case VEC_WIDEN_MULT_HI_EXPR:
5375     case VEC_WIDEN_MULT_LO_EXPR:
5376     case VEC_WIDEN_MULT_EVEN_EXPR:
5377     case VEC_WIDEN_MULT_ODD_EXPR:
5378     case VEC_WIDEN_LSHIFT_HI_EXPR:
5379     case VEC_WIDEN_LSHIFT_LO_EXPR:
5380     case VEC_WIDEN_PLUS_HI_EXPR:
5381     case VEC_WIDEN_PLUS_LO_EXPR:
5382     case VEC_WIDEN_MINUS_HI_EXPR:
5383     case VEC_WIDEN_MINUS_LO_EXPR:
5384     case VEC_PERM_EXPR:
5385     case VEC_DUPLICATE_EXPR:
5386     case VEC_SERIES_EXPR:
5387     case SAD_EXPR:
5388       return NULL;
5389 
5390     /* Misc codes.  */
5391     case ADDR_SPACE_CONVERT_EXPR:
5392     case FIXED_CONVERT_EXPR:
5393     case OBJ_TYPE_REF:
5394     case WITH_SIZE_EXPR:
5395     case BIT_INSERT_EXPR:
5396       return NULL;
5397 
5398     case DOT_PROD_EXPR:
5399       if (SCALAR_INT_MODE_P (GET_MODE (op0))
5400             && SCALAR_INT_MODE_P (mode))
5401           {
5402             op0
5403               = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5404                                                                                             0)))
5405                                           ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
5406                                           inner_mode);
5407             op1
5408               = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5409                                                                                             1)))
5410                                           ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
5411                                           inner_mode);
5412             op0 = simplify_gen_binary (MULT, mode, op0, op1);
5413             return simplify_gen_binary (PLUS, mode, op0, op2);
5414           }
5415       return NULL;
5416 
5417     case WIDEN_MULT_EXPR:
5418     case WIDEN_MULT_PLUS_EXPR:
5419     case WIDEN_MULT_MINUS_EXPR:
5420     case WIDEN_PLUS_EXPR:
5421     case WIDEN_MINUS_EXPR:
5422       if (SCALAR_INT_MODE_P (GET_MODE (op0))
5423             && SCALAR_INT_MODE_P (mode))
5424           {
5425             inner_mode = GET_MODE (op0);
5426             if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
5427               op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
5428             else
5429               op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
5430             if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
5431               op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
5432             else
5433               op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
5434             if (TREE_CODE (exp) == WIDEN_PLUS_EXPR)
5435               return simplify_gen_binary (PLUS, mode, op0, op1);
5436             else if (TREE_CODE (exp) == WIDEN_MINUS_EXPR)
5437               return simplify_gen_binary (MINUS, mode, op0, op1);
5438             op0 = simplify_gen_binary (MULT, mode, op0, op1);
5439             if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
5440               return op0;
5441             else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
5442               return simplify_gen_binary (PLUS, mode, op0, op2);
5443             else
5444               return simplify_gen_binary (MINUS, mode, op2, op0);
5445           }
5446       return NULL;
5447 
5448     case MULT_HIGHPART_EXPR:
5449       /* ??? Similar to the above.  */
5450       return NULL;
5451 
5452     case WIDEN_SUM_EXPR:
5453     case WIDEN_LSHIFT_EXPR:
5454       if (SCALAR_INT_MODE_P (GET_MODE (op0))
5455             && SCALAR_INT_MODE_P (mode))
5456           {
5457             op0
5458               = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5459                                                                                             0)))
5460                                           ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
5461                                           inner_mode);
5462             return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
5463                                               ? ASHIFT : PLUS, mode, op0, op1);
5464           }
5465       return NULL;
5466 
5467     default:
5468     flag_unsupported:
5469       if (flag_checking)
5470           {
5471             debug_tree (exp);
5472             gcc_unreachable ();
5473           }
5474       return NULL;
5475     }
5476 }
5477 
5478 /* Return an RTX equivalent to the source bind value of the tree expression
5479    EXP.  */
5480 
5481 static rtx
expand_debug_source_expr(tree exp)5482 expand_debug_source_expr (tree exp)
5483 {
5484   rtx op0 = NULL_RTX;
5485   machine_mode mode = VOIDmode, inner_mode;
5486 
5487   switch (TREE_CODE (exp))
5488     {
5489     case VAR_DECL:
5490       if (DECL_ABSTRACT_ORIGIN (exp))
5491           return expand_debug_source_expr (DECL_ABSTRACT_ORIGIN (exp));
5492       break;
5493     case PARM_DECL:
5494       {
5495           mode = DECL_MODE (exp);
5496           op0 = expand_debug_parm_decl (exp);
5497           if (op0)
5498              break;
5499           /* See if this isn't an argument that has been completely
5500              optimized out.  */
5501           if (!DECL_RTL_SET_P (exp)
5502               && !DECL_INCOMING_RTL (exp)
5503               && DECL_ABSTRACT_ORIGIN (current_function_decl))
5504             {
5505               tree aexp = DECL_ORIGIN (exp);
5506               if (DECL_CONTEXT (aexp)
5507                     == DECL_ABSTRACT_ORIGIN (current_function_decl))
5508                 {
5509                     vec<tree, va_gc> **debug_args;
5510                     unsigned int ix;
5511                     tree ddecl;
5512                     debug_args = decl_debug_args_lookup (current_function_decl);
5513                     if (debug_args != NULL)
5514                       {
5515                         for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
5516                                ix += 2)
5517                           if (ddecl == aexp)
5518                               return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
5519                       }
5520                 }
5521             }
5522           break;
5523       }
5524     default:
5525       break;
5526     }
5527 
5528   if (op0 == NULL_RTX)
5529     return NULL_RTX;
5530 
5531   inner_mode = GET_MODE (op0);
5532   if (mode == inner_mode)
5533     return op0;
5534 
5535   if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
5536     {
5537       if (GET_MODE_UNIT_BITSIZE (mode)
5538             == GET_MODE_UNIT_BITSIZE (inner_mode))
5539           op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
5540       else if (GET_MODE_UNIT_BITSIZE (mode)
5541                  < GET_MODE_UNIT_BITSIZE (inner_mode))
5542           op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
5543       else
5544           op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
5545     }
5546   else if (FLOAT_MODE_P (mode))
5547     gcc_unreachable ();
5548   else if (FLOAT_MODE_P (inner_mode))
5549     {
5550       if (TYPE_UNSIGNED (TREE_TYPE (exp)))
5551           op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
5552       else
5553           op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
5554     }
5555   else if (GET_MODE_UNIT_PRECISION (mode)
5556              == GET_MODE_UNIT_PRECISION (inner_mode))
5557     op0 = lowpart_subreg (mode, op0, inner_mode);
5558   else if (GET_MODE_UNIT_PRECISION (mode)
5559              < GET_MODE_UNIT_PRECISION (inner_mode))
5560     op0 = simplify_gen_unary (TRUNCATE, mode, op0, inner_mode);
5561   else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
5562     op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
5563   else
5564     op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
5565 
5566   return op0;
5567 }
5568 
5569 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
5570    Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
5571    deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN.  */
5572 
5573 static void
avoid_complex_debug_insns(rtx_insn * insn,rtx * exp_p,int depth)5574 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
5575 {
5576   rtx exp = *exp_p;
5577 
5578   if (exp == NULL_RTX)
5579     return;
5580 
5581   if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
5582     return;
5583 
5584   if (depth == 4)
5585     {
5586       /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL).  */
5587       rtx dval = make_debug_expr_from_rtl (exp);
5588 
5589       /* Emit a debug bind insn before INSN.  */
5590       rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
5591                                                DEBUG_EXPR_TREE_DECL (dval), exp,
5592                                                VAR_INIT_STATUS_INITIALIZED);
5593 
5594       emit_debug_insn_before (bind, insn);
5595       *exp_p = dval;
5596       return;
5597     }
5598 
5599   const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
5600   int i, j;
5601   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
5602     switch (*format_ptr++)
5603       {
5604       case 'e':
5605           avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
5606           break;
5607 
5608       case 'E':
5609       case 'V':
5610           for (j = 0; j < XVECLEN (exp, i); j++)
5611             avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
5612           break;
5613 
5614       default:
5615           break;
5616       }
5617 }
5618 
5619 /* Expand the _LOCs in debug insns.  We run this after expanding all
5620    regular insns, so that any variables referenced in the function
5621    will have their DECL_RTLs set.  */
5622 
5623 static void
expand_debug_locations(void)5624 expand_debug_locations (void)
5625 {
5626   rtx_insn *insn;
5627   rtx_insn *last = get_last_insn ();
5628   int save_strict_alias = flag_strict_aliasing;
5629 
5630   /* New alias sets while setting up memory attributes cause
5631      -fcompare-debug failures, even though it doesn't bring about any
5632      codegen changes.  */
5633   flag_strict_aliasing = 0;
5634 
5635   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5636     if (DEBUG_BIND_INSN_P (insn))
5637       {
5638           tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
5639           rtx val;
5640           rtx_insn *prev_insn, *insn2;
5641           machine_mode mode;
5642 
5643           if (value == NULL_TREE)
5644             val = NULL_RTX;
5645           else
5646             {
5647               if (INSN_VAR_LOCATION_STATUS (insn)
5648                     == VAR_INIT_STATUS_UNINITIALIZED)
5649                 val = expand_debug_source_expr (value);
5650               /* The avoid_deep_ter_for_debug function inserts
5651                  debug bind stmts after SSA_NAME definition, with the
5652                  SSA_NAME as the whole bind location.  Disable temporarily
5653                  expansion of that SSA_NAME into the DEBUG_EXPR_DECL
5654                  being defined in this DEBUG_INSN.  */
5655               else if (deep_ter_debug_map && TREE_CODE (value) == SSA_NAME)
5656                 {
5657                     tree *slot = deep_ter_debug_map->get (value);
5658                     if (slot)
5659                       {
5660                         if (*slot == INSN_VAR_LOCATION_DECL (insn))
5661                           *slot = NULL_TREE;
5662                         else
5663                           slot = NULL;
5664                       }
5665                     val = expand_debug_expr (value);
5666                     if (slot)
5667                       *slot = INSN_VAR_LOCATION_DECL (insn);
5668                 }
5669               else
5670                 val = expand_debug_expr (value);
5671               gcc_assert (last == get_last_insn ());
5672             }
5673 
5674           if (!val)
5675             val = gen_rtx_UNKNOWN_VAR_LOC ();
5676           else
5677             {
5678               mode = GET_MODE (INSN_VAR_LOCATION (insn));
5679 
5680               gcc_assert (mode == GET_MODE (val)
5681                               || (GET_MODE (val) == VOIDmode
5682                                   && (CONST_SCALAR_INT_P (val)
5683                                         || GET_CODE (val) == CONST_FIXED
5684                                         || GET_CODE (val) == LABEL_REF)));
5685             }
5686 
5687           INSN_VAR_LOCATION_LOC (insn) = val;
5688           prev_insn = PREV_INSN (insn);
5689           for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
5690             avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
5691       }
5692 
5693   flag_strict_aliasing = save_strict_alias;
5694 }
5695 
5696 /* Performs swapping operands of commutative operations to expand
5697    the expensive one first.  */
5698 
5699 static void
reorder_operands(basic_block bb)5700 reorder_operands (basic_block bb)
5701 {
5702   unsigned int *lattice;  /* Hold cost of each statement.  */
5703   unsigned int i = 0, n = 0;
5704   gimple_stmt_iterator gsi;
5705   gimple_seq stmts;
5706   gimple *stmt;
5707   bool swap;
5708   tree op0, op1;
5709   ssa_op_iter iter;
5710   use_operand_p use_p;
5711   gimple *def0, *def1;
5712 
5713   /* Compute cost of each statement using estimate_num_insns.  */
5714   stmts = bb_seq (bb);
5715   for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5716     {
5717       stmt = gsi_stmt (gsi);
5718       if (!is_gimple_debug (stmt))
5719         gimple_set_uid (stmt, n++);
5720     }
5721   lattice = XNEWVEC (unsigned int, n);
5722   for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5723     {
5724       unsigned cost;
5725       stmt = gsi_stmt (gsi);
5726       if (is_gimple_debug (stmt))
5727           continue;
5728       cost = estimate_num_insns (stmt, &eni_size_weights);
5729       lattice[i] = cost;
5730       FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
5731           {
5732             tree use = USE_FROM_PTR (use_p);
5733             gimple *def_stmt;
5734             if (TREE_CODE (use) != SSA_NAME)
5735               continue;
5736             def_stmt = get_gimple_for_ssa_name (use);
5737             if (!def_stmt)
5738               continue;
5739             lattice[i] += lattice[gimple_uid (def_stmt)];
5740           }
5741       i++;
5742       if (!is_gimple_assign (stmt)
5743             || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
5744           continue;
5745       op0 = gimple_op (stmt, 1);
5746       op1 = gimple_op (stmt, 2);
5747       if (TREE_CODE (op0) != SSA_NAME
5748             || TREE_CODE (op1) != SSA_NAME)
5749           continue;
5750       /* Swap operands if the second one is more expensive.  */
5751       def0 = get_gimple_for_ssa_name (op0);
5752       def1 = get_gimple_for_ssa_name (op1);
5753       if (!def1)
5754           continue;
5755       swap = false;
5756       if (!def0 || lattice[gimple_uid (def1)] > lattice[gimple_uid (def0)])
5757           swap = true;
5758       if (swap)
5759           {
5760             if (dump_file && (dump_flags & TDF_DETAILS))
5761               {
5762                 fprintf (dump_file, "Swap operands in stmt:\n");
5763                 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5764                 fprintf (dump_file, "Cost left opnd=%d, right opnd=%d\n",
5765                            def0 ? lattice[gimple_uid (def0)] : 0,
5766                            lattice[gimple_uid (def1)]);
5767               }
5768             swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
5769                                    gimple_assign_rhs2_ptr (stmt));
5770           }
5771     }
5772   XDELETE (lattice);
5773 }
5774 
5775 /* Expand basic block BB from GIMPLE trees to RTL.  */
5776 
5777 static basic_block
expand_gimple_basic_block(basic_block bb,bool disable_tail_calls)5778 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
5779 {
5780   gimple_stmt_iterator gsi;
5781   gimple_seq stmts;
5782   gimple *stmt = NULL;
5783   rtx_note *note = NULL;
5784   rtx_insn *last;
5785   edge e;
5786   edge_iterator ei;
5787   bool nondebug_stmt_seen = false;
5788 
5789   if (dump_file)
5790     fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
5791                bb->index);
5792 
5793   /* Note that since we are now transitioning from GIMPLE to RTL, we
5794      cannot use the gsi_*_bb() routines because they expect the basic
5795      block to be in GIMPLE, instead of RTL.  Therefore, we need to
5796      access the BB sequence directly.  */
5797   if (optimize)
5798     reorder_operands (bb);
5799   stmts = bb_seq (bb);
5800   bb->il.gimple.seq = NULL;
5801   bb->il.gimple.phi_nodes = NULL;
5802   rtl_profile_for_bb (bb);
5803   init_rtl_bb_info (bb);
5804   bb->flags |= BB_RTL;
5805 
5806   /* Remove the RETURN_EXPR if we may fall though to the exit
5807      instead.  */
5808   gsi = gsi_last (stmts);
5809   if (!gsi_end_p (gsi)
5810       && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
5811     {
5812       greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
5813 
5814       gcc_assert (single_succ_p (bb));
5815       gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
5816 
5817       if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5818             && !gimple_return_retval (ret_stmt))
5819           {
5820             gsi_remove (&gsi, false);
5821             single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
5822           }
5823     }
5824 
5825   gsi = gsi_start (stmts);
5826   if (!gsi_end_p (gsi))
5827     {
5828       stmt = gsi_stmt (gsi);
5829       if (gimple_code (stmt) != GIMPLE_LABEL)
5830           stmt = NULL;
5831     }
5832 
5833   rtx_code_label **elt = lab_rtx_for_bb->get (bb);
5834 
5835   if (stmt || elt)
5836     {
5837       gcc_checking_assert (!note);
5838       last = get_last_insn ();
5839 
5840       if (stmt)
5841           {
5842             expand_gimple_stmt (stmt);
5843             gsi_next (&gsi);
5844           }
5845 
5846       if (elt)
5847           emit_label (*elt);
5848 
5849       BB_HEAD (bb) = NEXT_INSN (last);
5850       if (NOTE_P (BB_HEAD (bb)))
5851           BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
5852       gcc_assert (LABEL_P (BB_HEAD (bb)));
5853       note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
5854 
5855       maybe_dump_rtl_for_gimple_stmt (stmt, last);
5856     }
5857   else
5858     BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
5859 
5860   if (note)
5861     NOTE_BASIC_BLOCK (note) = bb;
5862 
5863   for (; !gsi_end_p (gsi); gsi_next (&gsi))
5864     {
5865       basic_block new_bb;
5866 
5867       stmt = gsi_stmt (gsi);
5868       if (!is_gimple_debug (stmt))
5869           nondebug_stmt_seen = true;
5870 
5871       /* If this statement is a non-debug one, and we generate debug
5872            insns, then this one might be the last real use of a TERed
5873            SSA_NAME, but where there are still some debug uses further
5874            down.  Expanding the current SSA name in such further debug
5875            uses by their RHS might lead to wrong debug info, as coalescing
5876            might make the operands of such RHS be placed into the same
5877            pseudo as something else.  Like so:
5878              a_1 = a_0 + 1;   // Assume a_1 is TERed and a_0 is dead
5879              use(a_1);
5880              a_2 = ...
5881            #DEBUG ... => a_1
5882            As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
5883            If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
5884            the write to a_2 would actually have clobbered the place which
5885            formerly held a_0.
5886 
5887            So, instead of that, we recognize the situation, and generate
5888            debug temporaries at the last real use of TERed SSA names:
5889              a_1 = a_0 + 1;
5890            #DEBUG #D1 => a_1
5891              use(a_1);
5892              a_2 = ...
5893            #DEBUG ... => #D1
5894            */
5895       if (MAY_HAVE_DEBUG_BIND_INSNS
5896             && SA.values
5897             && !is_gimple_debug (stmt))
5898           {
5899             ssa_op_iter iter;
5900             tree op;
5901             gimple *def;
5902 
5903             location_t sloc = curr_insn_location ();
5904 
5905             /* Look for SSA names that have their last use here (TERed
5906                names always have only one real use).  */
5907             FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5908               if ((def = get_gimple_for_ssa_name (op)))
5909                 {
5910                     imm_use_iterator imm_iter;
5911                     use_operand_p use_p;
5912                     bool have_debug_uses = false;
5913 
5914                     FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5915                       {
5916                         if (gimple_debug_bind_p (USE_STMT (use_p)))
5917                           {
5918                               have_debug_uses = true;
5919                               break;
5920                           }
5921                       }
5922 
5923                     if (have_debug_uses)
5924                       {
5925                         /* OP is a TERed SSA name, with DEF its defining
5926                            statement, and where OP is used in further debug
5927                            instructions.  Generate a debug temporary, and
5928                            replace all uses of OP in debug insns with that
5929                            temporary.  */
5930                         gimple *debugstmt;
5931                         tree value = gimple_assign_rhs_to_tree (def);
5932                         tree vexpr = build_debug_expr_decl (TREE_TYPE (value));
5933                         rtx val;
5934                         machine_mode mode;
5935 
5936                         set_curr_insn_location (gimple_location (def));
5937 
5938                         if (DECL_P (value))
5939                           mode = DECL_MODE (value);
5940                         else
5941                           mode = TYPE_MODE (TREE_TYPE (value));
5942                         /* FIXME: Is setting the mode really necessary? */
5943                         SET_DECL_MODE (vexpr, mode);
5944 
5945                         val = gen_rtx_VAR_LOCATION
5946                               (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5947 
5948                         emit_debug_insn (val);
5949 
5950                         FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5951                           {
5952                               if (!gimple_debug_bind_p (debugstmt))
5953                                 continue;
5954 
5955                               FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5956                                 SET_USE (use_p, vexpr);
5957 
5958                               update_stmt (debugstmt);
5959                           }
5960                       }
5961                 }
5962             set_curr_insn_location (sloc);
5963           }
5964 
5965       currently_expanding_gimple_stmt = stmt;
5966 
5967       /* Expand this statement, then evaluate the resulting RTL and
5968            fixup the CFG accordingly.  */
5969       if (gimple_code (stmt) == GIMPLE_COND)
5970           {
5971             new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
5972             if (new_bb)
5973               {
5974                 currently_expanding_gimple_stmt = NULL;
5975                 return new_bb;
5976               }
5977           }
5978       else if (is_gimple_debug (stmt))
5979           {
5980             location_t sloc = curr_insn_location ();
5981             gimple_stmt_iterator nsi = gsi;
5982 
5983             for (;;)
5984               {
5985                 tree var;
5986                 tree value = NULL_TREE;
5987                 rtx val = NULL_RTX;
5988                 machine_mode mode;
5989 
5990                 if (!gimple_debug_nonbind_marker_p (stmt))
5991                     {
5992                       if (gimple_debug_bind_p (stmt))
5993                         {
5994                           var = gimple_debug_bind_get_var (stmt);
5995 
5996                           if (TREE_CODE (var) != DEBUG_EXPR_DECL
5997                                 && TREE_CODE (var) != LABEL_DECL
5998                                 && !target_for_debug_bind (var))
5999                               goto delink_debug_stmt;
6000 
6001                           if (DECL_P (var) && !VECTOR_TYPE_P (TREE_TYPE (var)))
6002                               mode = DECL_MODE (var);
6003                           else
6004                               mode = TYPE_MODE (TREE_TYPE (var));
6005 
6006                           if (gimple_debug_bind_has_value_p (stmt))
6007                               value = gimple_debug_bind_get_value (stmt);
6008 
6009                           val = gen_rtx_VAR_LOCATION
6010                               (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
6011                         }
6012                       else if (gimple_debug_source_bind_p (stmt))
6013                         {
6014                           var = gimple_debug_source_bind_get_var (stmt);
6015 
6016                           value = gimple_debug_source_bind_get_value (stmt);
6017 
6018                           if (!VECTOR_TYPE_P (TREE_TYPE (var)))
6019                               mode = DECL_MODE (var);
6020                           else
6021                               mode = TYPE_MODE (TREE_TYPE (var));
6022 
6023                           val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
6024                                                               VAR_INIT_STATUS_UNINITIALIZED);
6025                         }
6026                       else
6027                         gcc_unreachable ();
6028                     }
6029                 /* If this function was first compiled with markers
6030                      enabled, but they're now disable (e.g. LTO), drop
6031                      them on the floor.  */
6032                 else if (gimple_debug_nonbind_marker_p (stmt)
6033                            && !MAY_HAVE_DEBUG_MARKER_INSNS)
6034                     goto delink_debug_stmt;
6035                 else if (gimple_debug_begin_stmt_p (stmt))
6036                     val = GEN_RTX_DEBUG_MARKER_BEGIN_STMT_PAT ();
6037                 else if (gimple_debug_inline_entry_p (stmt))
6038                     val = GEN_RTX_DEBUG_MARKER_INLINE_ENTRY_PAT ();
6039                 else
6040                     gcc_unreachable ();
6041 
6042                 last = get_last_insn ();
6043 
6044                 set_curr_insn_location (gimple_location (stmt));
6045 
6046                 emit_debug_insn (val);
6047 
6048                 if (dump_file && (dump_flags & TDF_DETAILS))
6049                     {
6050                       /* We can't dump the insn with a TREE where an RTX
6051                          is expected.  */
6052                       if (GET_CODE (val) == VAR_LOCATION)
6053                         {
6054                           gcc_checking_assert (PAT_VAR_LOCATION_LOC (val) == (rtx)value);
6055                           PAT_VAR_LOCATION_LOC (val) = const0_rtx;
6056                         }
6057                       maybe_dump_rtl_for_gimple_stmt (stmt, last);
6058                       if (GET_CODE (val) == VAR_LOCATION)
6059                         PAT_VAR_LOCATION_LOC (val) = (rtx)value;
6060                     }
6061 
6062               delink_debug_stmt:
6063                 /* In order not to generate too many debug temporaries,
6064                    we delink all uses of debug statements we already expanded.
6065                      Therefore debug statements between definition and real
6066                      use of TERed SSA names will continue to use the SSA name,
6067                      and not be replaced with debug temps.  */
6068                 delink_stmt_imm_use (stmt);
6069 
6070                 gsi = nsi;
6071                 gsi_next (&nsi);
6072                 if (gsi_end_p (nsi))
6073                     break;
6074                 stmt = gsi_stmt (nsi);
6075                 if (!is_gimple_debug (stmt))
6076                     break;
6077               }
6078 
6079             set_curr_insn_location (sloc);
6080           }
6081       else
6082           {
6083             gcall *call_stmt = dyn_cast <gcall *> (stmt);
6084             if (call_stmt
6085                 && gimple_call_tail_p (call_stmt)
6086                 && disable_tail_calls)
6087               gimple_call_set_tail (call_stmt, false);
6088 
6089             if (call_stmt && gimple_call_tail_p (call_stmt))
6090               {
6091                 bool can_fallthru;
6092                 new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
6093                 if (new_bb)
6094                     {
6095                       if (can_fallthru)
6096                         bb = new_bb;
6097                       else
6098                         {
6099                           currently_expanding_gimple_stmt = NULL;
6100                           return new_bb;
6101                         }
6102                     }
6103               }
6104             else
6105               {
6106                 def_operand_p def_p;
6107                 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
6108 
6109                 if (def_p != NULL)
6110                     {
6111                       /* Ignore this stmt if it is in the list of
6112                          replaceable expressions.  */
6113                       if (SA.values
6114                           && bitmap_bit_p (SA.values,
6115                                                SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
6116                         continue;
6117                     }
6118                 last = expand_gimple_stmt (stmt);
6119                 maybe_dump_rtl_for_gimple_stmt (stmt, last);
6120               }
6121           }
6122     }
6123 
6124   currently_expanding_gimple_stmt = NULL;
6125 
6126   /* Expand implicit goto and convert goto_locus.  */
6127   FOR_EACH_EDGE (e, ei, bb->succs)
6128     {
6129       if (e->goto_locus != UNKNOWN_LOCATION || !nondebug_stmt_seen)
6130           set_curr_insn_location (e->goto_locus);
6131       if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
6132           {
6133             emit_jump (label_rtx_for_bb (e->dest));
6134             e->flags &= ~EDGE_FALLTHRU;
6135           }
6136     }
6137 
6138   /* Expanded RTL can create a jump in the last instruction of block.
6139      This later might be assumed to be a jump to successor and break edge insertion.
6140      We need to insert dummy move to prevent this. PR41440. */
6141   if (single_succ_p (bb)
6142       && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
6143       && (last = get_last_insn ())
6144       && (JUMP_P (last)
6145             || (DEBUG_INSN_P (last)
6146                 && JUMP_P (prev_nondebug_insn (last)))))
6147     {
6148       rtx dummy = gen_reg_rtx (SImode);
6149       emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
6150     }
6151 
6152   do_pending_stack_adjust ();
6153 
6154   /* Find the block tail.  The last insn in the block is the insn
6155      before a barrier and/or table jump insn.  */
6156   last = get_last_insn ();
6157   if (BARRIER_P (last))
6158     last = PREV_INSN (last);
6159   if (JUMP_TABLE_DATA_P (last))
6160     last = PREV_INSN (PREV_INSN (last));
6161   if (BARRIER_P (last))
6162     last = PREV_INSN (last);
6163   BB_END (bb) = last;
6164 
6165   update_bb_for_insn (bb);
6166 
6167   return bb;
6168 }
6169 
6170 
6171 /* Create a basic block for initialization code.  */
6172 
6173 static basic_block
construct_init_block(void)6174 construct_init_block (void)
6175 {
6176   basic_block init_block, first_block;
6177   edge e = NULL;
6178   int flags;
6179 
6180   /* Multiple entry points not supported yet.  */
6181   gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
6182   init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6183   init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
6184   ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
6185   EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
6186 
6187   e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
6188 
6189   /* When entry edge points to first basic block, we don't need jump,
6190      otherwise we have to jump into proper target.  */
6191   if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
6192     {
6193       tree label = gimple_block_label (e->dest);
6194 
6195       emit_jump (jump_target_rtx (label));
6196       flags = 0;
6197     }
6198   else
6199     flags = EDGE_FALLTHRU;
6200 
6201   init_block = create_basic_block (NEXT_INSN (get_insns ()),
6202                                            get_last_insn (),
6203                                            ENTRY_BLOCK_PTR_FOR_FN (cfun));
6204   init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
6205   add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
6206   if (e)
6207     {
6208       first_block = e->dest;
6209       redirect_edge_succ (e, init_block);
6210       make_single_succ_edge (init_block, first_block, flags);
6211     }
6212   else
6213     make_single_succ_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun),
6214                                  EDGE_FALLTHRU);
6215 
6216   update_bb_for_insn (init_block);
6217   return init_block;
6218 }
6219 
6220 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
6221    found in the block tree.  */
6222 
6223 static void
set_block_levels(tree block,int level)6224 set_block_levels (tree block, int level)
6225 {
6226   while (block)
6227     {
6228       BLOCK_NUMBER (block) = level;
6229       set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
6230       block = BLOCK_CHAIN (block);
6231     }
6232 }
6233 
6234 /* Create a block containing landing pads and similar stuff.  */
6235 
6236 static void
construct_exit_block(void)6237 construct_exit_block (void)
6238 {
6239   rtx_insn *head = get_last_insn ();
6240   rtx_insn *end;
6241   basic_block exit_block;
6242   edge e, e2;
6243   unsigned ix;
6244   edge_iterator ei;
6245   basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
6246   rtx_insn *orig_end = BB_END (prev_bb);
6247 
6248   rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
6249 
6250   /* Make sure the locus is set to the end of the function, so that
6251      epilogue line numbers and warnings are set properly.  */
6252   if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
6253     input_location = cfun->function_end_locus;
6254 
6255   /* Generate rtl for function exit.  */
6256   expand_function_end ();
6257 
6258   end = get_last_insn ();
6259   if (head == end)
6260     return;
6261   /* While emitting the function end we could move end of the last basic
6262      block.  */
6263   BB_END (prev_bb) = orig_end;
6264   while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
6265     head = NEXT_INSN (head);
6266   /* But make sure exit_block starts with RETURN_LABEL, otherwise the
6267      bb count counting will be confused.  Any instructions before that
6268      label are emitted for the case where PREV_BB falls through into the
6269      exit block, so append those instructions to prev_bb in that case.  */
6270   if (NEXT_INSN (head) != return_label)
6271     {
6272       while (NEXT_INSN (head) != return_label)
6273           {
6274             if (!NOTE_P (NEXT_INSN (head)))
6275               BB_END (prev_bb) = NEXT_INSN (head);
6276             head = NEXT_INSN (head);
6277           }
6278     }
6279   exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
6280   exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
6281   add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
6282 
6283   ix = 0;
6284   while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
6285     {
6286       e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
6287       if (!(e->flags & EDGE_ABNORMAL))
6288           redirect_edge_succ (e, exit_block);
6289       else
6290           ix++;
6291     }
6292 
6293   e = make_single_succ_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun),
6294                                    EDGE_FALLTHRU);
6295   FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6296     if (e2 != e)
6297       {
6298           exit_block->count -= e2->count ();
6299       }
6300   update_bb_for_insn (exit_block);
6301 }
6302 
6303 /* Helper function for discover_nonconstant_array_refs.
6304    Look for ARRAY_REF nodes with non-constant indexes and mark them
6305    addressable.  */
6306 
6307 static tree
discover_nonconstant_array_refs_r(tree * tp,int * walk_subtrees,void * data)6308 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
6309                                            void *data)
6310 {
6311   tree t = *tp;
6312   bitmap forced_stack_vars = (bitmap)((walk_stmt_info *)data)->info;
6313 
6314   if (IS_TYPE_OR_DECL_P (t))
6315     *walk_subtrees = 0;
6316   else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6317     {
6318       while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6319                 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
6320                 && (!TREE_OPERAND (t, 2)
6321                       || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
6322                || (TREE_CODE (t) == COMPONENT_REF
6323                      && (!TREE_OPERAND (t,2)
6324                          || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
6325                || TREE_CODE (t) == BIT_FIELD_REF
6326                || TREE_CODE (t) == REALPART_EXPR
6327                || TREE_CODE (t) == IMAGPART_EXPR
6328                || TREE_CODE (t) == VIEW_CONVERT_EXPR
6329                || CONVERT_EXPR_P (t))
6330           t = TREE_OPERAND (t, 0);
6331 
6332       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6333           {
6334             t = get_base_address (t);
6335             if (t && DECL_P (t)
6336                 && DECL_MODE (t) != BLKmode
6337                 && !TREE_ADDRESSABLE (t))
6338               bitmap_set_bit (forced_stack_vars, DECL_UID (t));
6339           }
6340 
6341       *walk_subtrees = 0;
6342     }
6343   /* References of size POLY_INT_CST to a fixed-size object must go
6344      through memory.  It's more efficient to force that here than
6345      to create temporary slots on the fly.
6346      RTL expansion expectes TARGET_MEM_REF to always address actual memory.  */
6347   else if (TREE_CODE (t) == TARGET_MEM_REF
6348              || (TREE_CODE (t) == MEM_REF
6349                  && TYPE_SIZE (TREE_TYPE (t))
6350                  && POLY_INT_CST_P (TYPE_SIZE (TREE_TYPE (t)))))
6351     {
6352       tree base = get_base_address (t);
6353       if (base
6354             && DECL_P (base)
6355             && !TREE_ADDRESSABLE (base)
6356             && DECL_MODE (base) != BLKmode
6357             && GET_MODE_SIZE (DECL_MODE (base)).is_constant ())
6358           bitmap_set_bit (forced_stack_vars, DECL_UID (base));
6359       *walk_subtrees = 0;
6360     }
6361 
6362   return NULL_TREE;
6363 }
6364 
6365 /* If there's a chance to get a pseudo for t then if it would be of float mode
6366    and the actual access is via an integer mode (lowered memcpy or similar
6367    access) then avoid the register expansion if the mode likely is not storage
6368    suitable for raw bits processing (like XFmode on i?86).  */
6369 
6370 static void
avoid_type_punning_on_regs(tree t,bitmap forced_stack_vars)6371 avoid_type_punning_on_regs (tree t, bitmap forced_stack_vars)
6372 {
6373   machine_mode access_mode = TYPE_MODE (TREE_TYPE (t));
6374   if (access_mode != BLKmode
6375       && !SCALAR_INT_MODE_P (access_mode))
6376     return;
6377   tree base = get_base_address (t);
6378   if (DECL_P (base)
6379       && !TREE_ADDRESSABLE (base)
6380       && FLOAT_MODE_P (DECL_MODE (base))
6381       && maybe_lt (GET_MODE_PRECISION (DECL_MODE (base)),
6382                        GET_MODE_BITSIZE (GET_MODE_INNER (DECL_MODE (base))))
6383       /* Double check in the expensive way we really would get a pseudo.  */
6384       && use_register_for_decl (base))
6385     bitmap_set_bit (forced_stack_vars, DECL_UID (base));
6386 }
6387 
6388 /* RTL expansion is not able to compile array references with variable
6389    offsets for arrays stored in single register.  Discover such
6390    expressions and mark variables as addressable to avoid this
6391    scenario.  */
6392 
6393 static void
discover_nonconstant_array_refs(bitmap forced_stack_vars)6394 discover_nonconstant_array_refs (bitmap forced_stack_vars)
6395 {
6396   basic_block bb;
6397   gimple_stmt_iterator gsi;
6398 
6399   walk_stmt_info wi = {};
6400   wi.info = forced_stack_vars;
6401   FOR_EACH_BB_FN (bb, cfun)
6402     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6403       {
6404           gimple *stmt = gsi_stmt (gsi);
6405           if (!is_gimple_debug (stmt))
6406             {
6407               walk_gimple_op (stmt, discover_nonconstant_array_refs_r, &wi);
6408               gcall *call = dyn_cast <gcall *> (stmt);
6409               if (call && gimple_call_internal_p (call))
6410                 {
6411                     tree cand = NULL_TREE;
6412                     switch (gimple_call_internal_fn (call))
6413                       {
6414                       case IFN_LOAD_LANES:
6415                         /* The source must be a MEM.  */
6416                         cand = gimple_call_arg (call, 0);
6417                         break;
6418                       case IFN_STORE_LANES:
6419                         /* The destination must be a MEM.  */
6420                         cand = gimple_call_lhs (call);
6421                         break;
6422                       default:
6423                         break;
6424                       }
6425                     if (cand)
6426                       cand = get_base_address (cand);
6427                     if (cand
6428                         && DECL_P (cand)
6429                         && use_register_for_decl (cand))
6430                       bitmap_set_bit (forced_stack_vars, DECL_UID (cand));
6431                 }
6432               if (gimple_vdef (stmt))
6433                 {
6434                     tree t = gimple_get_lhs (stmt);
6435                     if (t && REFERENCE_CLASS_P (t))
6436                       avoid_type_punning_on_regs (t, forced_stack_vars);
6437                 }
6438             }
6439       }
6440 }
6441 
6442 /* This function sets crtl->args.internal_arg_pointer to a virtual
6443    register if DRAP is needed.  Local register allocator will replace
6444    virtual_incoming_args_rtx with the virtual register.  */
6445 
6446 static void
expand_stack_alignment(void)6447 expand_stack_alignment (void)
6448 {
6449   rtx drap_rtx;
6450   unsigned int preferred_stack_boundary;
6451 
6452   if (! SUPPORTS_STACK_ALIGNMENT)
6453     return;
6454 
6455   if (cfun->calls_alloca
6456       || cfun->has_nonlocal_label
6457       || crtl->has_nonlocal_goto)
6458     crtl->need_drap = true;
6459 
6460   /* Call update_stack_boundary here again to update incoming stack
6461      boundary.  It may set incoming stack alignment to a different
6462      value after RTL expansion.  TARGET_FUNCTION_OK_FOR_SIBCALL may
6463      use the minimum incoming stack alignment to check if it is OK
6464      to perform sibcall optimization since sibcall optimization will
6465      only align the outgoing stack to incoming stack boundary.  */
6466   if (targetm.calls.update_stack_boundary)
6467     targetm.calls.update_stack_boundary ();
6468 
6469   /* The incoming stack frame has to be aligned at least at
6470      parm_stack_boundary.  */
6471   gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
6472 
6473   /* Update crtl->stack_alignment_estimated and use it later to align
6474      stack.  We check PREFERRED_STACK_BOUNDARY if there may be non-call
6475      exceptions since callgraph doesn't collect incoming stack alignment
6476      in this case.  */
6477   if (cfun->can_throw_non_call_exceptions
6478       && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
6479     preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
6480   else
6481     preferred_stack_boundary = crtl->preferred_stack_boundary;
6482   if (preferred_stack_boundary > crtl->stack_alignment_estimated)
6483     crtl->stack_alignment_estimated = preferred_stack_boundary;
6484   if (preferred_stack_boundary > crtl->stack_alignment_needed)
6485     crtl->stack_alignment_needed = preferred_stack_boundary;
6486 
6487   gcc_assert (crtl->stack_alignment_needed
6488                 <= crtl->stack_alignment_estimated);
6489 
6490   crtl->stack_realign_needed
6491     = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
6492   crtl->stack_realign_tried = crtl->stack_realign_needed;
6493 
6494   crtl->stack_realign_processed = true;
6495 
6496   /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
6497      alignment.  */
6498   gcc_assert (targetm.calls.get_drap_rtx != NULL);
6499   drap_rtx = targetm.calls.get_drap_rtx ();
6500 
6501   /* stack_realign_drap and drap_rtx must match.  */
6502   gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
6503 
6504   /* Do nothing if NULL is returned, which means DRAP is not needed.  */
6505   if (drap_rtx != NULL)
6506     {
6507       crtl->args.internal_arg_pointer = drap_rtx;
6508 
6509       /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
6510          needed. */
6511       fixup_tail_calls ();
6512     }
6513 }
6514 
6515 
6516 static void
expand_main_function(void)6517 expand_main_function (void)
6518 {
6519 #if (defined(INVOKE__main)                                  \
6520      || (!defined(HAS_INIT_SECTION)                         \
6521            && !defined(INIT_SECTION_ASM_OP)                 \
6522            && !defined(INIT_ARRAY_SECTION_ASM_OP)))
6523   emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode);
6524 #endif
6525 }
6526 
6527 
6528 /* Expand code to initialize the stack_protect_guard.  This is invoked at
6529    the beginning of a function to be protected.  */
6530 
6531 static void
stack_protect_prologue(void)6532 stack_protect_prologue (void)
6533 {
6534   tree guard_decl = targetm.stack_protect_guard ();
6535   rtx x, y;
6536 
6537   crtl->stack_protect_guard_decl = guard_decl;
6538   x = expand_normal (crtl->stack_protect_guard);
6539 
6540   if (targetm.have_stack_protect_combined_set () && guard_decl)
6541     {
6542       gcc_assert (DECL_P (guard_decl));
6543       y = DECL_RTL (guard_decl);
6544 
6545       /* Allow the target to compute address of Y and copy it to X without
6546            leaking Y into a register.  This combined address + copy pattern
6547            allows the target to prevent spilling of any intermediate results by
6548            splitting it after register allocator.  */
6549       if (rtx_insn *insn = targetm.gen_stack_protect_combined_set (x, y))
6550           {
6551             emit_insn (insn);
6552             return;
6553           }
6554     }
6555 
6556   if (guard_decl)
6557     y = expand_normal (guard_decl);
6558   else
6559     y = const0_rtx;
6560 
6561   /* Allow the target to copy from Y to X without leaking Y into a
6562      register.  */
6563   if (targetm.have_stack_protect_set ())
6564     if (rtx_insn *insn = targetm.gen_stack_protect_set (x, y))
6565       {
6566           emit_insn (insn);
6567           return;
6568       }
6569 
6570   /* Otherwise do a straight move.  */
6571   emit_move_insn (x, y);
6572 }
6573 
6574 /* Translate the intermediate representation contained in the CFG
6575    from GIMPLE trees to RTL.
6576 
6577    We do conversion per basic block and preserve/update the tree CFG.
6578    This implies we have to do some magic as the CFG can simultaneously
6579    consist of basic blocks containing RTL and GIMPLE trees.  This can
6580    confuse the CFG hooks, so be careful to not manipulate CFG during
6581    the expansion.  */
6582 
6583 namespace {
6584 
6585 const pass_data pass_data_expand =
6586 {
6587   RTL_PASS, /* type */
6588   "expand", /* name */
6589   OPTGROUP_NONE, /* optinfo_flags */
6590   TV_EXPAND, /* tv_id */
6591   ( PROP_ssa | PROP_gimple_leh | PROP_cfg
6592     | PROP_gimple_lcx
6593     | PROP_gimple_lvec
6594     | PROP_gimple_lva), /* properties_required */
6595   PROP_rtl, /* properties_provided */
6596   ( PROP_ssa | PROP_gimple ), /* properties_destroyed */
6597   0, /* todo_flags_start */
6598   0, /* todo_flags_finish */
6599 };
6600 
6601 class pass_expand : public rtl_opt_pass
6602 {
6603 public:
pass_expand(gcc::context * ctxt)6604   pass_expand (gcc::context *ctxt)
6605     : rtl_opt_pass (pass_data_expand, ctxt)
6606   {}
6607 
6608   /* opt_pass methods: */
6609   virtual unsigned int execute (function *);
6610 
6611 }; // class pass_expand
6612 
6613 unsigned int
execute(function * fun)6614 pass_expand::execute (function *fun)
6615 {
6616   basic_block bb, init_block;
6617   edge_iterator ei;
6618   edge e;
6619   rtx_insn *var_seq, *var_ret_seq;
6620   unsigned i;
6621 
6622   timevar_push (TV_OUT_OF_SSA);
6623   rewrite_out_of_ssa (&SA);
6624   timevar_pop (TV_OUT_OF_SSA);
6625   SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
6626 
6627   if (MAY_HAVE_DEBUG_BIND_STMTS && flag_tree_ter)
6628     {
6629       gimple_stmt_iterator gsi;
6630       FOR_EACH_BB_FN (bb, cfun)
6631           for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6632             if (gimple_debug_bind_p (gsi_stmt (gsi)))
6633               avoid_deep_ter_for_debug (gsi_stmt (gsi), 0);
6634     }
6635 
6636   /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE.  */
6637   auto_bitmap forced_stack_vars;
6638   discover_nonconstant_array_refs (forced_stack_vars);
6639 
6640   /* Make sure all values used by the optimization passes have sane
6641      defaults.  */
6642   reg_renumber = 0;
6643 
6644   /* Some backends want to know that we are expanding to RTL.  */
6645   currently_expanding_to_rtl = 1;
6646   /* Dominators are not kept up-to-date as we may create new basic-blocks.  */
6647   free_dominance_info (CDI_DOMINATORS);
6648 
6649   rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
6650 
6651   insn_locations_init ();
6652   if (!DECL_IS_UNDECLARED_BUILTIN (current_function_decl))
6653     {
6654       /* Eventually, all FEs should explicitly set function_start_locus.  */
6655       if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
6656           set_curr_insn_location
6657             (DECL_SOURCE_LOCATION (current_function_decl));
6658       else
6659           set_curr_insn_location (fun->function_start_locus);
6660     }
6661   else
6662     set_curr_insn_location (UNKNOWN_LOCATION);
6663   prologue_location = curr_insn_location ();
6664 
6665 #ifdef INSN_SCHEDULING
6666   init_sched_attrs ();
6667 #endif
6668 
6669   /* Make sure first insn is a note even if we don't want linenums.
6670      This makes sure the first insn will never be deleted.
6671      Also, final expects a note to appear there.  */
6672   emit_note (NOTE_INSN_DELETED);
6673 
6674   targetm.expand_to_rtl_hook ();
6675   crtl->init_stack_alignment ();
6676   fun->cfg->max_jumptable_ents = 0;
6677 
6678   /* Resovle the function section.  Some targets, like ARM EABI rely on knowledge
6679      of the function section at exapnsion time to predict distance of calls.  */
6680   resolve_unique_section (current_function_decl, 0, flag_function_sections);
6681 
6682   /* Expand the variables recorded during gimple lowering.  */
6683   timevar_push (TV_VAR_EXPAND);
6684   start_sequence ();
6685 
6686   var_ret_seq = expand_used_vars (forced_stack_vars);
6687 
6688   var_seq = get_insns ();
6689   end_sequence ();
6690   timevar_pop (TV_VAR_EXPAND);
6691 
6692   /* Honor stack protection warnings.  */
6693   if (warn_stack_protect)
6694     {
6695       if (fun->calls_alloca)
6696           warning (OPT_Wstack_protector,
6697                      "stack protector not protecting local variables: "
6698                      "variable length buffer");
6699       if (has_short_buffer && !crtl->stack_protect_guard)
6700           warning (OPT_Wstack_protector,
6701                      "stack protector not protecting function: "
6702                      "all local arrays are less than %d bytes long",
6703                      (int) param_ssp_buffer_size);
6704     }
6705 
6706   /* Temporarily mark PARM_DECLs and RESULT_DECLs we need to expand to
6707      memory addressable so expand_function_start can emit the required
6708      copies.  */
6709   auto_vec<tree, 16> marked_parms;
6710   for (tree parm = DECL_ARGUMENTS (current_function_decl); parm;
6711        parm = DECL_CHAIN (parm))
6712     if (!TREE_ADDRESSABLE (parm)
6713           && bitmap_bit_p (forced_stack_vars, DECL_UID (parm)))
6714       {
6715           TREE_ADDRESSABLE (parm) = 1;
6716           marked_parms.safe_push (parm);
6717       }
6718   if (DECL_RESULT (current_function_decl)
6719       && !TREE_ADDRESSABLE (DECL_RESULT (current_function_decl))
6720       && bitmap_bit_p (forced_stack_vars,
6721                            DECL_UID (DECL_RESULT (current_function_decl))))
6722     {
6723       TREE_ADDRESSABLE (DECL_RESULT (current_function_decl)) = 1;
6724       marked_parms.safe_push (DECL_RESULT (current_function_decl));
6725     }
6726 
6727   /* Set up parameters and prepare for return, for the function.  */
6728   expand_function_start (current_function_decl);
6729 
6730   /* Clear TREE_ADDRESSABLE again.  */
6731   while (!marked_parms.is_empty ())
6732     TREE_ADDRESSABLE (marked_parms.pop ()) = 0;
6733 
6734   /* If we emitted any instructions for setting up the variables,
6735      emit them before the FUNCTION_START note.  */
6736   if (var_seq)
6737     {
6738       emit_insn_before (var_seq, parm_birth_insn);
6739 
6740       /* In expand_function_end we'll insert the alloca save/restore
6741            before parm_birth_insn.  We've just insertted an alloca call.
6742            Adjust the pointer to match.  */
6743       parm_birth_insn = var_seq;
6744     }
6745 
6746   /* Now propagate the RTL assignment of each partition to the
6747      underlying var of each SSA_NAME.  */
6748   tree name;
6749 
6750   FOR_EACH_SSA_NAME (i, name, cfun)
6751     {
6752       /* We might have generated new SSA names in
6753            update_alias_info_with_stack_vars.  They will have a NULL
6754            defining statements, and won't be part of the partitioning,
6755            so ignore those.  */
6756       if (!SSA_NAME_DEF_STMT (name))
6757           continue;
6758 
6759       adjust_one_expanded_partition_var (name);
6760     }
6761 
6762   /* Clean up RTL of variables that straddle across multiple
6763      partitions, and check that the rtl of any PARM_DECLs that are not
6764      cleaned up is that of their default defs.  */
6765   FOR_EACH_SSA_NAME (i, name, cfun)
6766     {
6767       int part;
6768 
6769       /* We might have generated new SSA names in
6770            update_alias_info_with_stack_vars.  They will have a NULL
6771            defining statements, and won't be part of the partitioning,
6772            so ignore those.  */
6773       if (!SSA_NAME_DEF_STMT (name))
6774           continue;
6775       part = var_to_partition (SA.map, name);
6776       if (part == NO_PARTITION)
6777           continue;
6778 
6779       /* If this decl was marked as living in multiple places, reset
6780            this now to NULL.  */
6781       tree var = SSA_NAME_VAR (name);
6782       if (var && DECL_RTL_IF_SET (var) == pc_rtx)
6783           SET_DECL_RTL (var, NULL);
6784       /* Check that the pseudos chosen by assign_parms are those of
6785            the corresponding default defs.  */
6786       else if (SSA_NAME_IS_DEFAULT_DEF (name)
6787                  && (TREE_CODE (var) == PARM_DECL
6788                        || TREE_CODE (var) == RESULT_DECL))
6789           {
6790             rtx in = DECL_RTL_IF_SET (var);
6791             gcc_assert (in);
6792             rtx out = SA.partition_to_pseudo[part];
6793             gcc_assert (in == out);
6794 
6795             /* Now reset VAR's RTL to IN, so that the _EXPR attrs match
6796                those expected by debug backends for each parm and for
6797                the result.  This is particularly important for stabs,
6798                whose register elimination from parm's DECL_RTL may cause
6799                -fcompare-debug differences as SET_DECL_RTL changes reg's
6800                attrs.  So, make sure the RTL already has the parm as the
6801                EXPR, so that it won't change.  */
6802             SET_DECL_RTL (var, NULL_RTX);
6803             if (MEM_P (in))
6804               set_mem_attributes (in, var, true);
6805             SET_DECL_RTL (var, in);
6806           }
6807     }
6808 
6809   /* If this function is `main', emit a call to `__main'
6810      to run global initializers, etc.  */
6811   if (DECL_NAME (current_function_decl)
6812       && MAIN_NAME_P (DECL_NAME (current_function_decl))
6813       && DECL_FILE_SCOPE_P (current_function_decl))
6814     expand_main_function ();
6815 
6816   /* Initialize the stack_protect_guard field.  This must happen after the
6817      call to __main (if any) so that the external decl is initialized.  */
6818   if (crtl->stack_protect_guard && targetm.stack_protect_runtime_enabled_p ())
6819     stack_protect_prologue ();
6820 
6821   expand_phi_nodes (&SA);
6822 
6823   /* Release any stale SSA redirection data.  */
6824   redirect_edge_var_map_empty ();
6825 
6826   /* Register rtl specific functions for cfg.  */
6827   rtl_register_cfg_hooks ();
6828 
6829   init_block = construct_init_block ();
6830 
6831   /* Clear EDGE_EXECUTABLE on the entry edge(s).  It is cleaned from the
6832      remaining edges later.  */
6833   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
6834     e->flags &= ~EDGE_EXECUTABLE;
6835 
6836   /* If the function has too many markers, drop them while expanding.  */
6837   if (cfun->debug_marker_count
6838       >= param_max_debug_marker_count)
6839     cfun->debug_nonbind_markers = false;
6840 
6841   lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
6842   FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
6843                       next_bb)
6844     bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
6845 
6846   if (MAY_HAVE_DEBUG_BIND_INSNS)
6847     expand_debug_locations ();
6848 
6849   if (deep_ter_debug_map)
6850     {
6851       delete deep_ter_debug_map;
6852       deep_ter_debug_map = NULL;
6853     }
6854 
6855   /* Free stuff we no longer need after GIMPLE optimizations.  */
6856   free_dominance_info (CDI_DOMINATORS);
6857   free_dominance_info (CDI_POST_DOMINATORS);
6858   delete_tree_cfg_annotations (fun);
6859 
6860   timevar_push (TV_OUT_OF_SSA);
6861   finish_out_of_ssa (&SA);
6862   timevar_pop (TV_OUT_OF_SSA);
6863 
6864   timevar_push (TV_POST_EXPAND);
6865   /* We are no longer in SSA form.  */
6866   fun->gimple_df->in_ssa_p = false;
6867   loops_state_clear (LOOP_CLOSED_SSA);
6868 
6869   /* Expansion is used by optimization passes too, set maybe_hot_insn_p
6870      conservatively to true until they are all profile aware.  */
6871   delete lab_rtx_for_bb;
6872   free_histograms (fun);
6873 
6874   construct_exit_block ();
6875   insn_locations_finalize ();
6876 
6877   if (var_ret_seq)
6878     {
6879       rtx_insn *after = return_label;
6880       rtx_insn *next = NEXT_INSN (after);
6881       if (next && NOTE_INSN_BASIC_BLOCK_P (next))
6882           after = next;
6883       emit_insn_after (var_ret_seq, after);
6884     }
6885 
6886   if (hwasan_sanitize_stack_p ())
6887     hwasan_maybe_emit_frame_base_init ();
6888 
6889   /* Zap the tree EH table.  */
6890   set_eh_throw_stmt_table (fun, NULL);
6891 
6892   /* We need JUMP_LABEL be set in order to redirect jumps, and hence
6893      split edges which edge insertions might do.  */
6894   rebuild_jump_labels (get_insns ());
6895 
6896   /* If we have a single successor to the entry block, put the pending insns
6897      after parm birth, but before NOTE_INSNS_FUNCTION_BEG.  */
6898   if (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
6899     {
6900       edge e = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fun));
6901       if (e->insns.r)
6902           {
6903             rtx_insn *insns = e->insns.r;
6904             e->insns.r = NULL;
6905             rebuild_jump_labels_chain (insns);
6906             if (NOTE_P (parm_birth_insn)
6907                 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
6908               emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
6909             else
6910               emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
6911           }
6912     }
6913 
6914   /* Otherwise, as well as for other edges, take the usual way.  */
6915   commit_edge_insertions ();
6916 
6917   /* We're done expanding trees to RTL.  */
6918   currently_expanding_to_rtl = 0;
6919 
6920   flush_mark_addressable_queue ();
6921 
6922   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
6923                       EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6924     {
6925       edge e;
6926       edge_iterator ei;
6927       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6928           {
6929             /* Clear EDGE_EXECUTABLE.  This flag is never used in the backend.  */
6930             e->flags &= ~EDGE_EXECUTABLE;
6931 
6932             /* At the moment not all abnormal edges match the RTL
6933                representation.  It is safe to remove them here as
6934                find_many_sub_basic_blocks will rediscover them.
6935                In the future we should get this fixed properly.  */
6936             if ((e->flags & EDGE_ABNORMAL)
6937                 && !(e->flags & EDGE_SIBCALL))
6938               remove_edge (e);
6939             else
6940               ei_next (&ei);
6941           }
6942     }
6943 
6944   auto_sbitmap blocks (last_basic_block_for_fn (fun));
6945   bitmap_ones (blocks);
6946   find_many_sub_basic_blocks (blocks);
6947   purge_all_dead_edges ();
6948 
6949   /* After initial rtl generation, call back to finish generating
6950      exception support code.  We need to do this before cleaning up
6951      the CFG as the code does not expect dead landing pads.  */
6952   if (fun->eh->region_tree != NULL)
6953     finish_eh_generation ();
6954 
6955   /* Call expand_stack_alignment after finishing all
6956      updates to crtl->preferred_stack_boundary.  */
6957   expand_stack_alignment ();
6958 
6959   /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
6960      function.  */
6961   if (crtl->tail_call_emit)
6962     fixup_tail_calls ();
6963 
6964   HOST_WIDE_INT patch_area_size, patch_area_entry;
6965   parse_and_check_patch_area (flag_patchable_function_entry, false,
6966                                     &patch_area_size, &patch_area_entry);
6967 
6968   tree patchable_function_entry_attr
6969     = lookup_attribute ("patchable_function_entry",
6970                               DECL_ATTRIBUTES (cfun->decl));
6971   if (patchable_function_entry_attr)
6972     {
6973       tree pp_val = TREE_VALUE (patchable_function_entry_attr);
6974       tree patchable_function_entry_value1 = TREE_VALUE (pp_val);
6975 
6976       patch_area_size = tree_to_uhwi (patchable_function_entry_value1);
6977       patch_area_entry = 0;
6978       if (TREE_CHAIN (pp_val) != NULL_TREE)
6979           {
6980             tree patchable_function_entry_value2
6981               = TREE_VALUE (TREE_CHAIN (pp_val));
6982             patch_area_entry = tree_to_uhwi (patchable_function_entry_value2);
6983           }
6984     }
6985 
6986   if (patch_area_entry > patch_area_size)
6987     {
6988       if (patch_area_size > 0)
6989           warning (OPT_Wattributes,
6990                      "patchable function entry %wu exceeds size %wu",
6991                      patch_area_entry, patch_area_size);
6992       patch_area_entry = 0;
6993     }
6994 
6995   crtl->patch_area_size = patch_area_size;
6996   crtl->patch_area_entry = patch_area_entry;
6997 
6998   /* BB subdivision may have created basic blocks that are only reachable
6999      from unlikely bbs but not marked as such in the profile.  */
7000   if (optimize)
7001     propagate_unlikely_bbs_forward ();
7002 
7003   /* Remove unreachable blocks, otherwise we cannot compute dominators
7004      which are needed for loop state verification.  As a side-effect
7005      this also compacts blocks.
7006      ???  We cannot remove trivially dead insns here as for example
7007      the DRAP reg on i?86 is not magically live at this point.
7008      gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise.  */
7009   cleanup_cfg (CLEANUP_NO_INSN_DEL);
7010 
7011   checking_verify_flow_info ();
7012 
7013   /* Initialize pseudos allocated for hard registers.  */
7014   emit_initial_value_sets ();
7015 
7016   /* And finally unshare all RTL.  */
7017   unshare_all_rtl ();
7018 
7019   /* There's no need to defer outputting this function any more; we
7020      know we want to output it.  */
7021   DECL_DEFER_OUTPUT (current_function_decl) = 0;
7022 
7023   /* Now that we're done expanding trees to RTL, we shouldn't have any
7024      more CONCATs anywhere.  */
7025   generating_concat_p = 0;
7026 
7027   if (dump_file)
7028     {
7029       fprintf (dump_file,
7030                  "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
7031       /* And the pass manager will dump RTL for us.  */
7032     }
7033 
7034   /* If we're emitting a nested function, make sure its parent gets
7035      emitted as well.  Doing otherwise confuses debug info.  */
7036     {
7037       tree parent;
7038       for (parent = DECL_CONTEXT (current_function_decl);
7039              parent != NULL_TREE;
7040              parent = get_containing_scope (parent))
7041           if (TREE_CODE (parent) == FUNCTION_DECL)
7042             TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
7043     }
7044 
7045   TREE_ASM_WRITTEN (current_function_decl) = 1;
7046 
7047   /* After expanding, the return labels are no longer needed. */
7048   return_label = NULL;
7049   naked_return_label = NULL;
7050 
7051   /* After expanding, the tm_restart map is no longer needed.  */
7052   if (fun->gimple_df->tm_restart)
7053     fun->gimple_df->tm_restart = NULL;
7054 
7055   /* Tag the blocks with a depth number so that change_scope can find
7056      the common parent easily.  */
7057   set_block_levels (DECL_INITIAL (fun->decl), 0);
7058   default_rtl_profile ();
7059 
7060   /* For -dx discard loops now, otherwise IL verify in clean_state will
7061      ICE.  */
7062   if (rtl_dump_and_exit)
7063     {
7064       cfun->curr_properties &= ~PROP_loops;
7065       loop_optimizer_finalize ();
7066     }
7067 
7068   timevar_pop (TV_POST_EXPAND);
7069 
7070   return 0;
7071 }
7072 
7073 } // anon namespace
7074 
7075 rtl_opt_pass *
make_pass_expand(gcc::context * ctxt)7076 make_pass_expand (gcc::context *ctxt)
7077 {
7078   return new pass_expand (ctxt);
7079 }
7080