Fix xfail for 32-bit hppa*-*-* in gcc.dg/pr84877.c
[official-gcc.git] / gcc / cfgexpand.cc
blob316b8830ec006477d500000a7ae1d6dcfdf7665e
1 /* A pass for lowering trees to RTL.
2 Copyright (C) 2004-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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"
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
86 /* This variable holds information helping the rewriting of SSA trees
87 into RTL. */
88 struct ssaexpand SA;
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;
94 static rtx expand_debug_expr (tree);
96 static bool defer_stack_allocation (tree, bool);
98 static void record_alignment_for_reg_var (unsigned int);
100 /* Return an expression tree corresponding to the RHS of GIMPLE
101 statement STMT. */
103 tree
104 gimple_assign_rhs_to_tree (gimple *stmt)
106 tree t;
107 switch (gimple_assign_rhs_class (stmt))
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:
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;
136 default:
137 gcc_unreachable ();
140 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
141 SET_EXPR_LOCATION (t, gimple_location (stmt));
143 return t;
147 #ifndef STACK_ALIGNMENT_NEEDED
148 #define STACK_ALIGNMENT_NEEDED 1
149 #endif
151 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
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). */
158 static tree
159 leader_merge (tree cur, tree next)
161 if (cur == NULL || cur == next)
162 return next;
164 if (DECL_P (cur) && DECL_IGNORED_P (cur))
165 return cur;
167 if (DECL_P (next) && DECL_IGNORED_P (next))
168 return next;
170 return cur;
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
177 set_rtl (tree t, rtx x)
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));
218 if (x)
220 bool skip = false;
221 tree cur = NULL_TREE;
222 rtx xm = x;
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))
231 gcc_assert (subreg_lowpart_p (xm));
232 xm = SUBREG_REG (xm);
233 goto retry;
235 else if (GET_CODE (xm) == CONCAT)
237 xm = XEXP (xm, 0);
238 goto retry;
240 else if (GET_CODE (xm) == PARALLEL)
242 xm = XVECEXP (xm, 0, 0);
243 gcc_assert (GET_CODE (xm) == EXPR_LIST);
244 xm = XEXP (xm, 0);
245 goto retry;
247 else if (xm == pc_rtx)
248 skip = true;
249 else
250 gcc_unreachable ();
252 tree next = skip ? cur : leader_merge (cur, SSAVAR (t) ? SSAVAR (t) : t);
254 if (cur != next)
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);
266 if (TREE_CODE (t) == SSA_NAME)
268 int part = var_to_partition (SA.map, t);
269 if (part != NO_PARTITION)
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;
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)))
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)
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);
302 else
303 SET_DECL_RTL (t, x);
306 /* This structure holds data relevant to one variable that will be
307 placed in a stack slot. */
308 class stack_var
310 public:
311 /* The Variable. */
312 tree decl;
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;
318 /* The *byte* alignment required for this variable. Or as, with the
319 size, the alignment for this partition. */
320 unsigned int alignb;
322 /* The partition representative. */
323 size_t representative;
325 /* The next stack variable in the partition, or EOC. */
326 size_t next;
328 /* The numbers of conflicting stack variables. */
329 bitmap conflicts;
332 #define EOC ((size_t)-1)
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;
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;
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;
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;
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;
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;
361 /* Compute the byte alignment to use for DECL. Ignore alignment
362 we can't do with expected alignment of the stack boundary. */
364 static unsigned int
365 align_local_variable (tree decl, bool really_expand)
367 unsigned int align;
369 if (TREE_CODE (decl) == SSA_NAME)
371 tree type = TREE_TYPE (decl);
372 machine_mode mode = TYPE_MODE (type);
374 align = TYPE_ALIGN (type);
375 if (mode != BLKmode
376 && align < GET_MODE_ALIGNMENT (mode))
377 align = GET_MODE_ALIGNMENT (mode);
379 else
380 align = LOCAL_DECL_ALIGNMENT (decl);
382 if (hwasan_sanitize_stack_p ())
383 align = MAX (align, (unsigned) HWASAN_TAG_GRANULE_SIZE * BITS_PER_UNIT);
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);
392 return align / BITS_PER_UNIT;
395 /* Align given offset BASE with ALIGN. Truncate up if ALIGN_UP is true,
396 down otherwise. Return truncated BASE value. */
398 static inline unsigned HOST_WIDE_INT
399 align_base (HOST_WIDE_INT base, unsigned HOST_WIDE_INT align, bool align_up)
401 return align_up ? (base + align - 1) & -align : base & -align;
404 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
405 Return the frame offset. */
407 static poly_int64
408 alloc_stack_frame_space (poly_int64 size, unsigned HOST_WIDE_INT align)
410 poly_int64 offset, new_frame_offset;
412 if (FRAME_GROWS_DOWNWARD)
414 new_frame_offset
415 = aligned_lower_bound (frame_offset - frame_phase - size,
416 align) + frame_phase;
417 offset = new_frame_offset;
419 else
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;
427 frame_offset = new_frame_offset;
429 if (frame_offset_overflow (frame_offset, cfun->decl))
430 frame_offset = offset = 0;
432 return offset;
435 /* Ensure that the stack is aligned to ALIGN bytes.
436 Return the new frame offset. */
437 static poly_int64
438 align_frame_offset (unsigned HOST_WIDE_INT align)
440 return alloc_stack_frame_space (0, align);
443 /* Accumulate DECL into STACK_VARS. */
445 static void
446 add_stack_var (tree decl, bool really_expand)
448 class stack_var *v;
450 if (stack_vars_num >= stack_vars_alloc)
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);
459 if (!decl_to_stack_part)
460 decl_to_stack_part = new hash_map<tree, size_t>;
462 v = &stack_vars[stack_vars_num];
463 decl_to_stack_part->put (decl, stack_vars_num);
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);
478 /* All variables are initially in their own partition. */
479 v->representative = stack_vars_num;
480 v->next = EOC;
482 /* All variables initially conflict with no other. */
483 v->conflicts = NULL;
485 /* Ensure that this decl doesn't get put onto the list twice. */
486 set_rtl (decl, pc_rtx);
488 stack_vars_num++;
491 /* Make the decls associated with luid's X and Y conflict. */
493 static void
494 add_stack_var_conflict (size_t x, size_t y)
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);
508 /* Check whether the decls associated with luid's X and Y conflict. */
510 static bool
511 stack_var_conflict_p (size_t x, size_t y)
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;
523 if (!a->conflicts || !b->conflicts)
524 return false;
525 return bitmap_bit_p (a->conflicts, y);
528 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
529 enter its partition number into bitmap DATA. */
531 static bool
532 visit_op (gimple *, tree op, tree, void *data)
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)
540 size_t *v = decl_to_stack_part->get (op);
541 if (v)
542 bitmap_set_bit (active, *v);
544 return false;
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. */
551 static bool
552 visit_conflict (gimple *, tree op, tree, void *data)
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)
560 size_t *v = decl_to_stack_part->get (op);
561 if (v && bitmap_set_bit (active, *v))
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);
571 return false;
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. */
578 static inline void
579 add_scope_conflicts_2 (tree use, bitmap work,
580 walk_stmt_load_store_addr_fn visit)
582 if (TREE_CODE (use) == SSA_NAME
583 && (POINTER_TYPE_P (TREE_TYPE (use))
584 || INTEGRAL_TYPE_P (TREE_TYPE (use))))
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);
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. */
599 static void
600 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
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;
609 bitmap_clear (work);
610 FOR_EACH_EDGE (e, ei, bb->preds)
611 bitmap_ior_into (work, (bitmap)e->src->aux);
613 visit = visit_op;
615 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
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);
623 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
625 gimple *stmt = gsi_stmt (gsi);
627 if (gimple_clobber_p (stmt))
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);
639 else if (!is_gimple_debug (stmt))
641 if (for_conflict && visit == visit_op)
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)
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);
658 visit = visit_conflict;
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);
667 /* Generate stack partition conflicts between all partitions that are
668 simultaneously live. */
670 static void
671 add_scope_conflicts (void)
673 basic_block bb;
674 bool changed;
675 bitmap work = BITMAP_ALLOC (NULL);
676 int *rpo;
677 int n_bbs;
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.
687 We then do a mostly classical bitmap liveness algorithm. */
689 FOR_ALL_BB_FN (bb, cfun)
690 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
692 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
693 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
695 changed = true;
696 while (changed)
698 int i;
699 changed = false;
700 for (i = 0; i < n_bbs; i++)
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;
711 FOR_EACH_BB_FN (bb, cfun)
712 add_scope_conflicts_1 (bb, work, true);
714 free (rpo);
715 BITMAP_FREE (work);
716 FOR_ALL_BB_FN (bb, cfun)
717 BITMAP_FREE (bb->aux);
720 /* A subroutine of partition_stack_vars. A comparison function for qsort,
721 sorting an array of indices by the properties of the object. */
723 static int
724 stack_var_cmp (const void *a, const void *b)
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;
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;
743 /* Secondary compare on size, decreasing */
744 int diff = compare_sizes_for_sort (sizeb, sizea);
745 if (diff != 0)
746 return diff;
748 /* Tertiary compare on true alignment, decreasing. */
749 if (aligna < alignb)
750 return -1;
751 if (aligna > alignb)
752 return 1;
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)
759 if (TREE_CODE (declb) == SSA_NAME)
760 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
761 else
762 return -1;
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;
775 struct part_traits : unbounded_int_hashmap_traits <size_t, bitmap> {};
776 typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
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. */
782 static void
783 add_partitioned_vars_to_ptset (struct pt_solution *pt,
784 part_hashmap *decls_to_partitions,
785 hash_set<bitmap> *visited, bitmap temp)
787 bitmap_iterator bi;
788 unsigned i;
789 bitmap *part;
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;
798 bitmap_clear (temp);
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);
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.
817 It is not necessary to update TBAA info on accesses to the coalesced
818 storage since our memory model doesn't allow TBAA to be used for
819 WAW or WAR dependences. For RAW when the write is to an old object
820 the new object would not have been initialized at the point of the
821 read, invoking undefined behavior. */
823 static void
824 update_alias_info_with_stack_vars (void)
826 part_hashmap *decls_to_partitions = NULL;
827 size_t i, j;
828 tree var = NULL_TREE;
830 for (i = 0; i < stack_vars_num; i++)
832 bitmap part = NULL;
833 tree name;
834 struct ptr_info_def *pi;
836 /* Not interested in partitions with single variable. */
837 if (stack_vars[i].representative != i
838 || stack_vars[i].next == EOC)
839 continue;
841 if (!decls_to_partitions)
843 decls_to_partitions = new part_hashmap;
844 cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
847 /* Create an SSA_NAME that points to the partition for use
848 as base during alias-oracle queries on RTL for bases that
849 have been partitioned. */
850 if (var == NULL_TREE)
851 var = create_tmp_var (ptr_type_node);
852 name = make_ssa_name (var);
854 /* Create bitmaps representing partitions. They will be used for
855 points-to sets later, so use GGC alloc. */
856 part = BITMAP_GGC_ALLOC ();
857 for (j = i; j != EOC; j = stack_vars[j].next)
859 tree decl = stack_vars[j].decl;
860 unsigned int uid = DECL_PT_UID (decl);
861 bitmap_set_bit (part, uid);
862 decls_to_partitions->put (uid, part);
863 cfun->gimple_df->decls_to_pointers->put (decl, name);
864 if (TREE_ADDRESSABLE (decl))
865 TREE_ADDRESSABLE (name) = 1;
868 /* Make the SSA name point to all partition members. */
869 pi = get_ptr_info (name);
870 pt_solution_set (&pi->pt, part, false);
873 /* Make all points-to sets that contain one member of a partition
874 contain all members of the partition. */
875 if (decls_to_partitions)
877 unsigned i;
878 tree name;
879 hash_set<bitmap> visited;
880 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
882 FOR_EACH_SSA_NAME (i, name, cfun)
884 struct ptr_info_def *pi;
886 if (POINTER_TYPE_P (TREE_TYPE (name))
887 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
888 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
889 &visited, temp);
892 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
893 decls_to_partitions, &visited, temp);
894 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped_return,
895 decls_to_partitions, &visited, temp);
896 delete decls_to_partitions;
897 BITMAP_FREE (temp);
901 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
902 partitioning algorithm. Partitions A and B are known to be non-conflicting.
903 Merge them into a single partition A. */
905 static void
906 union_stack_vars (size_t a, size_t b)
908 class stack_var *vb = &stack_vars[b];
909 bitmap_iterator bi;
910 unsigned u;
912 gcc_assert (stack_vars[b].next == EOC);
913 /* Add B to A's partition. */
914 stack_vars[b].next = stack_vars[a].next;
915 stack_vars[b].representative = a;
916 stack_vars[a].next = b;
918 /* Make sure A is big enough to hold B. */
919 stack_vars[a].size = upper_bound (stack_vars[a].size, stack_vars[b].size);
921 /* Update the required alignment of partition A to account for B. */
922 if (stack_vars[a].alignb < stack_vars[b].alignb)
923 stack_vars[a].alignb = stack_vars[b].alignb;
925 /* Update the interference graph and merge the conflicts. */
926 if (vb->conflicts)
928 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
929 add_stack_var_conflict (a, stack_vars[u].representative);
930 BITMAP_FREE (vb->conflicts);
934 /* A subroutine of expand_used_vars. Binpack the variables into
935 partitions constrained by the interference graph. The overall
936 algorithm used is as follows:
938 Sort the objects by size in descending order.
939 For each object A {
940 S = size(A)
941 O = 0
942 loop {
943 Look for the largest non-conflicting object B with size <= S.
944 UNION (A, B)
949 static void
950 partition_stack_vars (void)
952 size_t si, sj, n = stack_vars_num;
954 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
955 for (si = 0; si < n; ++si)
956 stack_vars_sorted[si] = si;
958 if (n == 1)
959 return;
961 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
963 for (si = 0; si < n; ++si)
965 size_t i = stack_vars_sorted[si];
966 unsigned int ialign = stack_vars[i].alignb;
967 poly_int64 isize = stack_vars[i].size;
969 /* Ignore objects that aren't partition representatives. If we
970 see a var that is not a partition representative, it must
971 have been merged earlier. */
972 if (stack_vars[i].representative != i)
973 continue;
975 for (sj = si + 1; sj < n; ++sj)
977 size_t j = stack_vars_sorted[sj];
978 unsigned int jalign = stack_vars[j].alignb;
979 poly_int64 jsize = stack_vars[j].size;
981 /* Ignore objects that aren't partition representatives. */
982 if (stack_vars[j].representative != j)
983 continue;
985 /* Do not mix objects of "small" (supported) alignment
986 and "large" (unsupported) alignment. */
987 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
988 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
989 break;
991 /* For Address Sanitizer do not mix objects with different
992 sizes, as the shorter vars wouldn't be adequately protected.
993 Don't do that for "large" (unsupported) alignment objects,
994 those aren't protected anyway. */
995 if (asan_sanitize_stack_p ()
996 && maybe_ne (isize, jsize)
997 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
998 break;
1000 /* Ignore conflicting objects. */
1001 if (stack_var_conflict_p (i, j))
1002 continue;
1004 /* UNION the objects, placing J at OFFSET. */
1005 union_stack_vars (i, j);
1009 update_alias_info_with_stack_vars ();
1012 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
1014 static void
1015 dump_stack_var_partition (void)
1017 size_t si, i, j, n = stack_vars_num;
1019 for (si = 0; si < n; ++si)
1021 i = stack_vars_sorted[si];
1023 /* Skip variables that aren't partition representatives, for now. */
1024 if (stack_vars[i].representative != i)
1025 continue;
1027 fprintf (dump_file, "Partition %lu: size ", (unsigned long) i);
1028 print_dec (stack_vars[i].size, dump_file);
1029 fprintf (dump_file, " align %u\n", stack_vars[i].alignb);
1031 for (j = i; j != EOC; j = stack_vars[j].next)
1033 fputc ('\t', dump_file);
1034 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
1036 fputc ('\n', dump_file);
1040 /* Assign rtl to DECL at BASE + OFFSET. */
1042 static void
1043 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
1044 poly_int64 offset)
1046 unsigned align;
1047 rtx x;
1049 /* If this fails, we've overflowed the stack frame. Error nicely? */
1050 gcc_assert (known_eq (offset, trunc_int_for_mode (offset, Pmode)));
1052 if (hwasan_sanitize_stack_p ())
1053 x = targetm.memtag.add_tag (base, offset,
1054 hwasan_current_frame_tag ());
1055 else
1056 x = plus_constant (Pmode, base, offset);
1058 x = gen_rtx_MEM (TREE_CODE (decl) == SSA_NAME
1059 ? TYPE_MODE (TREE_TYPE (decl))
1060 : DECL_MODE (decl), x);
1062 /* Set alignment we actually gave this decl if it isn't an SSA name.
1063 If it is we generate stack slots only accidentally so it isn't as
1064 important, we'll simply set the alignment directly on the MEM. */
1066 if (stack_vars_base_reg_p (base))
1067 offset -= frame_phase;
1068 align = known_alignment (offset);
1069 align *= BITS_PER_UNIT;
1070 if (align == 0 || align > base_align)
1071 align = base_align;
1073 if (TREE_CODE (decl) != SSA_NAME)
1075 /* One would think that we could assert that we're not decreasing
1076 alignment here, but (at least) the i386 port does exactly this
1077 via the MINIMUM_ALIGNMENT hook. */
1079 SET_DECL_ALIGN (decl, align);
1080 DECL_USER_ALIGN (decl) = 0;
1083 set_rtl (decl, x);
1085 set_mem_align (x, align);
1088 class stack_vars_data
1090 public:
1091 /* Vector of offset pairs, always end of some padding followed
1092 by start of the padding that needs Address Sanitizer protection.
1093 The vector is in reversed, highest offset pairs come first. */
1094 auto_vec<HOST_WIDE_INT> asan_vec;
1096 /* Vector of partition representative decls in between the paddings. */
1097 auto_vec<tree> asan_decl_vec;
1099 /* Base pseudo register for Address Sanitizer protected automatic vars. */
1100 rtx asan_base;
1102 /* Alignment needed for the Address Sanitizer protected automatic vars. */
1103 unsigned int asan_alignb;
1106 /* A subroutine of expand_used_vars. Give each partition representative
1107 a unique location within the stack frame. Update each partition member
1108 with that location. */
1109 static void
1110 expand_stack_vars (bool (*pred) (size_t), class stack_vars_data *data)
1112 size_t si, i, j, n = stack_vars_num;
1113 poly_uint64 large_size = 0, large_alloc = 0;
1114 rtx large_base = NULL;
1115 rtx large_untagged_base = NULL;
1116 unsigned large_align = 0;
1117 bool large_allocation_done = false;
1118 tree decl;
1120 /* Determine if there are any variables requiring "large" alignment.
1121 Since these are dynamically allocated, we only process these if
1122 no predicate involved. */
1123 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
1124 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
1126 /* Find the total size of these variables. */
1127 for (si = 0; si < n; ++si)
1129 unsigned alignb;
1131 i = stack_vars_sorted[si];
1132 alignb = stack_vars[i].alignb;
1134 /* All "large" alignment decls come before all "small" alignment
1135 decls, but "large" alignment decls are not sorted based on
1136 their alignment. Increase large_align to track the largest
1137 required alignment. */
1138 if ((alignb * BITS_PER_UNIT) > large_align)
1139 large_align = alignb * BITS_PER_UNIT;
1141 /* Stop when we get to the first decl with "small" alignment. */
1142 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1143 break;
1145 /* Skip variables that aren't partition representatives. */
1146 if (stack_vars[i].representative != i)
1147 continue;
1149 /* Skip variables that have already had rtl assigned. See also
1150 add_stack_var where we perpetrate this pc_rtx hack. */
1151 decl = stack_vars[i].decl;
1152 if (TREE_CODE (decl) == SSA_NAME
1153 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)] != NULL_RTX
1154 : DECL_RTL (decl) != pc_rtx)
1155 continue;
1157 large_size = aligned_upper_bound (large_size, alignb);
1158 large_size += stack_vars[i].size;
1162 for (si = 0; si < n; ++si)
1164 rtx base;
1165 unsigned base_align, alignb;
1166 poly_int64 offset = 0;
1168 i = stack_vars_sorted[si];
1170 /* Skip variables that aren't partition representatives, for now. */
1171 if (stack_vars[i].representative != i)
1172 continue;
1174 /* Skip variables that have already had rtl assigned. See also
1175 add_stack_var where we perpetrate this pc_rtx hack. */
1176 decl = stack_vars[i].decl;
1177 if (TREE_CODE (decl) == SSA_NAME
1178 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)] != NULL_RTX
1179 : DECL_RTL (decl) != pc_rtx)
1180 continue;
1182 /* Check the predicate to see whether this variable should be
1183 allocated in this pass. */
1184 if (pred && !pred (i))
1185 continue;
1187 base = (hwasan_sanitize_stack_p ()
1188 ? hwasan_frame_base ()
1189 : virtual_stack_vars_rtx);
1190 alignb = stack_vars[i].alignb;
1191 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1193 poly_int64 hwasan_orig_offset;
1194 if (hwasan_sanitize_stack_p ())
1196 /* There must be no tag granule "shared" between different
1197 objects. This means that no HWASAN_TAG_GRANULE_SIZE byte
1198 chunk can have more than one object in it.
1200 We ensure this by forcing the end of the last bit of data to
1201 be aligned to HWASAN_TAG_GRANULE_SIZE bytes here, and setting
1202 the start of each variable to be aligned to
1203 HWASAN_TAG_GRANULE_SIZE bytes in `align_local_variable`.
1205 We can't align just one of the start or end, since there are
1206 untagged things stored on the stack which we do not align to
1207 HWASAN_TAG_GRANULE_SIZE bytes. If we only aligned the start
1208 or the end of tagged objects then untagged objects could end
1209 up sharing the first granule of a tagged object or sharing the
1210 last granule of a tagged object respectively. */
1211 hwasan_orig_offset = align_frame_offset (HWASAN_TAG_GRANULE_SIZE);
1212 gcc_assert (stack_vars[i].alignb >= HWASAN_TAG_GRANULE_SIZE);
1214 /* ASAN description strings don't yet have a syntax for expressing
1215 polynomial offsets. */
1216 HOST_WIDE_INT prev_offset;
1217 if (asan_sanitize_stack_p ()
1218 && pred
1219 && frame_offset.is_constant (&prev_offset)
1220 && stack_vars[i].size.is_constant ())
1222 if (data->asan_vec.is_empty ())
1224 align_frame_offset (ASAN_RED_ZONE_SIZE);
1225 prev_offset = frame_offset.to_constant ();
1227 prev_offset = align_base (prev_offset,
1228 ASAN_MIN_RED_ZONE_SIZE,
1229 !FRAME_GROWS_DOWNWARD);
1230 tree repr_decl = NULL_TREE;
1231 unsigned HOST_WIDE_INT size
1232 = asan_var_and_redzone_size (stack_vars[i].size.to_constant ());
1233 if (data->asan_vec.is_empty ())
1234 size = MAX (size, ASAN_RED_ZONE_SIZE);
1236 unsigned HOST_WIDE_INT alignment = MAX (alignb,
1237 ASAN_MIN_RED_ZONE_SIZE);
1238 offset = alloc_stack_frame_space (size, alignment);
1240 data->asan_vec.safe_push (prev_offset);
1241 /* Allocating a constant amount of space from a constant
1242 starting offset must give a constant result. */
1243 data->asan_vec.safe_push ((offset + stack_vars[i].size)
1244 .to_constant ());
1245 /* Find best representative of the partition.
1246 Prefer those with DECL_NAME, even better
1247 satisfying asan_protect_stack_decl predicate. */
1248 for (j = i; j != EOC; j = stack_vars[j].next)
1249 if (asan_protect_stack_decl (stack_vars[j].decl)
1250 && DECL_NAME (stack_vars[j].decl))
1252 repr_decl = stack_vars[j].decl;
1253 break;
1255 else if (repr_decl == NULL_TREE
1256 && DECL_P (stack_vars[j].decl)
1257 && DECL_NAME (stack_vars[j].decl))
1258 repr_decl = stack_vars[j].decl;
1259 if (repr_decl == NULL_TREE)
1260 repr_decl = stack_vars[i].decl;
1261 data->asan_decl_vec.safe_push (repr_decl);
1263 /* Make sure a representative is unpoison if another
1264 variable in the partition is handled by
1265 use-after-scope sanitization. */
1266 if (asan_handled_variables != NULL
1267 && !asan_handled_variables->contains (repr_decl))
1269 for (j = i; j != EOC; j = stack_vars[j].next)
1270 if (asan_handled_variables->contains (stack_vars[j].decl))
1271 break;
1272 if (j != EOC)
1273 asan_handled_variables->add (repr_decl);
1276 data->asan_alignb = MAX (data->asan_alignb, alignb);
1277 if (data->asan_base == NULL)
1278 data->asan_base = gen_reg_rtx (Pmode);
1279 base = data->asan_base;
1281 if (!STRICT_ALIGNMENT)
1282 base_align = crtl->max_used_stack_slot_alignment;
1283 else
1284 base_align = MAX (crtl->max_used_stack_slot_alignment,
1285 GET_MODE_ALIGNMENT (SImode)
1286 << ASAN_SHADOW_SHIFT);
1288 else
1290 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1291 base_align = crtl->max_used_stack_slot_alignment;
1293 if (hwasan_sanitize_stack_p ())
1295 /* Align again since the point of this alignment is to handle
1296 the "end" of the object (i.e. smallest address after the
1297 stack object). For FRAME_GROWS_DOWNWARD that requires
1298 aligning the stack before allocating, but for a frame that
1299 grows upwards that requires aligning the stack after
1300 allocation.
1302 Use `frame_offset` to record the offset value rather than
1303 `offset` since the `frame_offset` describes the extent
1304 allocated for this particular variable while `offset`
1305 describes the address that this variable starts at. */
1306 align_frame_offset (HWASAN_TAG_GRANULE_SIZE);
1307 hwasan_record_stack_var (virtual_stack_vars_rtx, base,
1308 hwasan_orig_offset, frame_offset);
1312 else
1314 /* Large alignment is only processed in the last pass. */
1315 if (pred)
1316 continue;
1318 /* If there were any variables requiring "large" alignment, allocate
1319 space. */
1320 if (maybe_ne (large_size, 0U) && ! large_allocation_done)
1322 poly_int64 loffset;
1323 rtx large_allocsize;
1325 large_allocsize = gen_int_mode (large_size, Pmode);
1326 get_dynamic_stack_size (&large_allocsize, 0, large_align, NULL);
1327 loffset = alloc_stack_frame_space
1328 (rtx_to_poly_int64 (large_allocsize),
1329 PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
1330 large_base = get_dynamic_stack_base (loffset, large_align, base);
1331 large_allocation_done = true;
1334 gcc_assert (large_base != NULL);
1335 large_alloc = aligned_upper_bound (large_alloc, alignb);
1336 offset = large_alloc;
1337 large_alloc += stack_vars[i].size;
1338 if (hwasan_sanitize_stack_p ())
1340 /* An object with a large alignment requirement means that the
1341 alignment requirement is greater than the required alignment
1342 for tags. */
1343 if (!large_untagged_base)
1344 large_untagged_base
1345 = targetm.memtag.untagged_pointer (large_base, NULL_RTX);
1346 /* Ensure the end of the variable is also aligned correctly. */
1347 poly_int64 align_again
1348 = aligned_upper_bound (large_alloc, HWASAN_TAG_GRANULE_SIZE);
1349 /* For large allocations we always allocate a chunk of space
1350 (which is addressed by large_untagged_base/large_base) and
1351 then use positive offsets from that. Hence the farthest
1352 offset is `align_again` and the nearest offset from the base
1353 is `offset`. */
1354 hwasan_record_stack_var (large_untagged_base, large_base,
1355 offset, align_again);
1358 base = large_base;
1359 base_align = large_align;
1362 /* Create rtl for each variable based on their location within the
1363 partition. */
1364 for (j = i; j != EOC; j = stack_vars[j].next)
1366 expand_one_stack_var_at (stack_vars[j].decl,
1367 base, base_align, offset);
1369 if (hwasan_sanitize_stack_p ())
1370 hwasan_increment_frame_tag ();
1373 gcc_assert (known_eq (large_alloc, large_size));
1376 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1377 static poly_uint64
1378 account_stack_vars (void)
1380 size_t si, j, i, n = stack_vars_num;
1381 poly_uint64 size = 0;
1383 for (si = 0; si < n; ++si)
1385 i = stack_vars_sorted[si];
1387 /* Skip variables that aren't partition representatives, for now. */
1388 if (stack_vars[i].representative != i)
1389 continue;
1391 size += stack_vars[i].size;
1392 for (j = i; j != EOC; j = stack_vars[j].next)
1393 set_rtl (stack_vars[j].decl, NULL);
1395 return size;
1398 /* Record the RTL assignment X for the default def of PARM. */
1400 extern void
1401 set_parm_rtl (tree parm, rtx x)
1403 gcc_assert (TREE_CODE (parm) == PARM_DECL
1404 || TREE_CODE (parm) == RESULT_DECL);
1406 if (x && !MEM_P (x))
1408 unsigned int align = MINIMUM_ALIGNMENT (TREE_TYPE (parm),
1409 TYPE_MODE (TREE_TYPE (parm)),
1410 TYPE_ALIGN (TREE_TYPE (parm)));
1412 /* If the variable alignment is very large we'll dynamicaly
1413 allocate it, which means that in-frame portion is just a
1414 pointer. ??? We've got a pseudo for sure here, do we
1415 actually dynamically allocate its spilling area if needed?
1416 ??? Isn't it a problem when Pmode alignment also exceeds
1417 MAX_SUPPORTED_STACK_ALIGNMENT, as can happen on cris and lm32? */
1418 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1419 align = GET_MODE_ALIGNMENT (Pmode);
1421 record_alignment_for_reg_var (align);
1424 tree ssa = ssa_default_def (cfun, parm);
1425 if (!ssa)
1426 return set_rtl (parm, x);
1428 int part = var_to_partition (SA.map, ssa);
1429 gcc_assert (part != NO_PARTITION);
1431 bool changed = bitmap_bit_p (SA.partitions_for_parm_default_defs, part);
1432 gcc_assert (changed);
1434 set_rtl (ssa, x);
1435 gcc_assert (DECL_RTL (parm) == x);
1438 /* A subroutine of expand_one_var. Called to immediately assign rtl
1439 to a variable to be allocated in the stack frame. */
1441 static void
1442 expand_one_stack_var_1 (tree var)
1444 poly_uint64 size;
1445 poly_int64 offset;
1446 unsigned byte_align;
1448 if (TREE_CODE (var) == SSA_NAME)
1450 tree type = TREE_TYPE (var);
1451 size = tree_to_poly_uint64 (TYPE_SIZE_UNIT (type));
1453 else
1454 size = tree_to_poly_uint64 (DECL_SIZE_UNIT (var));
1456 byte_align = align_local_variable (var, true);
1458 /* We handle highly aligned variables in expand_stack_vars. */
1459 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1461 rtx base;
1462 if (hwasan_sanitize_stack_p ())
1464 /* Allocate zero bytes to align the stack. */
1465 poly_int64 hwasan_orig_offset
1466 = align_frame_offset (HWASAN_TAG_GRANULE_SIZE);
1467 offset = alloc_stack_frame_space (size, byte_align);
1468 align_frame_offset (HWASAN_TAG_GRANULE_SIZE);
1469 base = hwasan_frame_base ();
1470 /* Use `frame_offset` to automatically account for machines where the
1471 frame grows upwards.
1473 `offset` will always point to the "start" of the stack object, which
1474 will be the smallest address, for ! FRAME_GROWS_DOWNWARD this is *not*
1475 the "furthest" offset from the base delimiting the current stack
1476 object. `frame_offset` will always delimit the extent that the frame.
1478 hwasan_record_stack_var (virtual_stack_vars_rtx, base,
1479 hwasan_orig_offset, frame_offset);
1481 else
1483 offset = alloc_stack_frame_space (size, byte_align);
1484 base = virtual_stack_vars_rtx;
1487 expand_one_stack_var_at (var, base,
1488 crtl->max_used_stack_slot_alignment, offset);
1490 if (hwasan_sanitize_stack_p ())
1491 hwasan_increment_frame_tag ();
1494 /* Wrapper for expand_one_stack_var_1 that checks SSA_NAMEs are
1495 already assigned some MEM. */
1497 static void
1498 expand_one_stack_var (tree var)
1500 if (TREE_CODE (var) == SSA_NAME)
1502 int part = var_to_partition (SA.map, var);
1503 if (part != NO_PARTITION)
1505 rtx x = SA.partition_to_pseudo[part];
1506 gcc_assert (x);
1507 gcc_assert (MEM_P (x));
1508 return;
1512 return expand_one_stack_var_1 (var);
1515 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1516 that will reside in a hard register. */
1518 static void
1519 expand_one_hard_reg_var (tree var)
1521 rest_of_decl_compilation (var, 0, 0);
1524 /* Record the alignment requirements of some variable assigned to a
1525 pseudo. */
1527 static void
1528 record_alignment_for_reg_var (unsigned int align)
1530 if (SUPPORTS_STACK_ALIGNMENT
1531 && crtl->stack_alignment_estimated < align)
1533 /* stack_alignment_estimated shouldn't change after stack
1534 realign decision made */
1535 gcc_assert (!crtl->stack_realign_processed);
1536 crtl->stack_alignment_estimated = align;
1539 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1540 So here we only make sure stack_alignment_needed >= align. */
1541 if (crtl->stack_alignment_needed < align)
1542 crtl->stack_alignment_needed = align;
1543 if (crtl->max_used_stack_slot_alignment < align)
1544 crtl->max_used_stack_slot_alignment = align;
1547 /* Create RTL for an SSA partition. */
1549 static void
1550 expand_one_ssa_partition (tree var)
1552 int part = var_to_partition (SA.map, var);
1553 gcc_assert (part != NO_PARTITION);
1555 if (SA.partition_to_pseudo[part])
1556 return;
1558 unsigned int align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1559 TYPE_MODE (TREE_TYPE (var)),
1560 TYPE_ALIGN (TREE_TYPE (var)));
1562 /* If the variable alignment is very large we'll dynamicaly allocate
1563 it, which means that in-frame portion is just a pointer. */
1564 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1565 align = GET_MODE_ALIGNMENT (Pmode);
1567 record_alignment_for_reg_var (align);
1569 if (!use_register_for_decl (var))
1571 if (defer_stack_allocation (var, true))
1572 add_stack_var (var, true);
1573 else
1574 expand_one_stack_var_1 (var);
1575 return;
1578 machine_mode reg_mode = promote_ssa_mode (var, NULL);
1579 rtx x = gen_reg_rtx (reg_mode);
1581 set_rtl (var, x);
1583 /* For a promoted variable, X will not be used directly but wrapped in a
1584 SUBREG with SUBREG_PROMOTED_VAR_P set, which means that the RTL land
1585 will assume that its upper bits can be inferred from its lower bits.
1586 Therefore, if X isn't initialized on every path from the entry, then
1587 we must do it manually in order to fulfill the above assumption. */
1588 if (reg_mode != TYPE_MODE (TREE_TYPE (var))
1589 && bitmap_bit_p (SA.partitions_for_undefined_values, part))
1590 emit_move_insn (x, CONST0_RTX (reg_mode));
1593 /* Record the association between the RTL generated for partition PART
1594 and the underlying variable of the SSA_NAME VAR. */
1596 static void
1597 adjust_one_expanded_partition_var (tree var)
1599 if (!var)
1600 return;
1602 tree decl = SSA_NAME_VAR (var);
1604 int part = var_to_partition (SA.map, var);
1605 if (part == NO_PARTITION)
1606 return;
1608 rtx x = SA.partition_to_pseudo[part];
1610 gcc_assert (x);
1612 set_rtl (var, x);
1614 if (!REG_P (x))
1615 return;
1617 /* Note if the object is a user variable. */
1618 if (decl && !DECL_ARTIFICIAL (decl))
1619 mark_user_reg (x);
1621 if (POINTER_TYPE_P (decl ? TREE_TYPE (decl) : TREE_TYPE (var)))
1622 mark_reg_pointer (x, get_pointer_alignment (var));
1625 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1626 that will reside in a pseudo register. */
1628 static void
1629 expand_one_register_var (tree var)
1631 if (TREE_CODE (var) == SSA_NAME)
1633 int part = var_to_partition (SA.map, var);
1634 if (part != NO_PARTITION)
1636 rtx x = SA.partition_to_pseudo[part];
1637 gcc_assert (x);
1638 gcc_assert (REG_P (x));
1639 return;
1641 gcc_unreachable ();
1644 tree decl = var;
1645 tree type = TREE_TYPE (decl);
1646 machine_mode reg_mode = promote_decl_mode (decl, NULL);
1647 rtx x = gen_reg_rtx (reg_mode);
1649 set_rtl (var, x);
1651 /* Note if the object is a user variable. */
1652 if (!DECL_ARTIFICIAL (decl))
1653 mark_user_reg (x);
1655 if (POINTER_TYPE_P (type))
1656 mark_reg_pointer (x, get_pointer_alignment (var));
1659 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1660 has some associated error, e.g. its type is error-mark. We just need
1661 to pick something that won't crash the rest of the compiler. */
1663 static void
1664 expand_one_error_var (tree var)
1666 machine_mode mode = DECL_MODE (var);
1667 rtx x;
1669 if (mode == BLKmode)
1670 x = gen_rtx_MEM (BLKmode, const0_rtx);
1671 else if (mode == VOIDmode)
1672 x = const0_rtx;
1673 else
1674 x = gen_reg_rtx (mode);
1676 SET_DECL_RTL (var, x);
1679 /* A subroutine of expand_one_var. VAR is a variable that will be
1680 allocated to the local stack frame. Return true if we wish to
1681 add VAR to STACK_VARS so that it will be coalesced with other
1682 variables. Return false to allocate VAR immediately.
1684 This function is used to reduce the number of variables considered
1685 for coalescing, which reduces the size of the quadratic problem. */
1687 static bool
1688 defer_stack_allocation (tree var, bool toplevel)
1690 tree size_unit = TREE_CODE (var) == SSA_NAME
1691 ? TYPE_SIZE_UNIT (TREE_TYPE (var))
1692 : DECL_SIZE_UNIT (var);
1693 poly_uint64 size;
1695 /* Whether the variable is small enough for immediate allocation not to be
1696 a problem with regard to the frame size. */
1697 bool smallish
1698 = (poly_int_tree_p (size_unit, &size)
1699 && (estimated_poly_value (size)
1700 < param_min_size_for_stack_sharing));
1702 /* If stack protection is enabled, *all* stack variables must be deferred,
1703 so that we can re-order the strings to the top of the frame.
1704 Similarly for Address Sanitizer. */
1705 if (flag_stack_protect || asan_sanitize_stack_p ())
1706 return true;
1708 unsigned int align = TREE_CODE (var) == SSA_NAME
1709 ? TYPE_ALIGN (TREE_TYPE (var))
1710 : DECL_ALIGN (var);
1712 /* We handle "large" alignment via dynamic allocation. We want to handle
1713 this extra complication in only one place, so defer them. */
1714 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1715 return true;
1717 bool ignored = TREE_CODE (var) == SSA_NAME
1718 ? !SSAVAR (var) || DECL_IGNORED_P (SSA_NAME_VAR (var))
1719 : DECL_IGNORED_P (var);
1721 /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1722 might be detached from their block and appear at toplevel when we reach
1723 here. We want to coalesce them with variables from other blocks when
1724 the immediate contribution to the frame size would be noticeable. */
1725 if (toplevel && optimize > 0 && ignored && !smallish)
1726 return true;
1728 /* Variables declared in the outermost scope automatically conflict
1729 with every other variable. The only reason to want to defer them
1730 at all is that, after sorting, we can more efficiently pack
1731 small variables in the stack frame. Continue to defer at -O2. */
1732 if (toplevel && optimize < 2)
1733 return false;
1735 /* Without optimization, *most* variables are allocated from the
1736 stack, which makes the quadratic problem large exactly when we
1737 want compilation to proceed as quickly as possible. On the
1738 other hand, we don't want the function's stack frame size to
1739 get completely out of hand. So we avoid adding scalars and
1740 "small" aggregates to the list at all. */
1741 if (optimize == 0 && smallish)
1742 return false;
1744 return true;
1747 /* A subroutine of expand_used_vars. Expand one variable according to
1748 its flavor. Variables to be placed on the stack are not actually
1749 expanded yet, merely recorded.
1750 When REALLY_EXPAND is false, only add stack values to be allocated.
1751 Return stack usage this variable is supposed to take.
1754 static poly_uint64
1755 expand_one_var (tree var, bool toplevel, bool really_expand,
1756 bitmap forced_stack_var = NULL)
1758 unsigned int align = BITS_PER_UNIT;
1759 tree origvar = var;
1761 var = SSAVAR (var);
1763 if (TREE_TYPE (var) != error_mark_node && VAR_P (var))
1765 if (is_global_var (var))
1766 return 0;
1768 /* Because we don't know if VAR will be in register or on stack,
1769 we conservatively assume it will be on stack even if VAR is
1770 eventually put into register after RA pass. For non-automatic
1771 variables, which won't be on stack, we collect alignment of
1772 type and ignore user specified alignment. Similarly for
1773 SSA_NAMEs for which use_register_for_decl returns true. */
1774 if (TREE_STATIC (var)
1775 || DECL_EXTERNAL (var)
1776 || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1777 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1778 TYPE_MODE (TREE_TYPE (var)),
1779 TYPE_ALIGN (TREE_TYPE (var)));
1780 else if (DECL_HAS_VALUE_EXPR_P (var)
1781 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1782 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1783 or variables which were assigned a stack slot already by
1784 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1785 changed from the offset chosen to it. */
1786 align = crtl->stack_alignment_estimated;
1787 else
1788 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1790 /* If the variable alignment is very large we'll dynamicaly allocate
1791 it, which means that in-frame portion is just a pointer. */
1792 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1793 align = GET_MODE_ALIGNMENT (Pmode);
1796 record_alignment_for_reg_var (align);
1798 poly_uint64 size;
1799 if (TREE_CODE (origvar) == SSA_NAME)
1801 gcc_assert (!VAR_P (var)
1802 || (!DECL_EXTERNAL (var)
1803 && !DECL_HAS_VALUE_EXPR_P (var)
1804 && !TREE_STATIC (var)
1805 && TREE_TYPE (var) != error_mark_node
1806 && !DECL_HARD_REGISTER (var)
1807 && really_expand));
1809 if (!VAR_P (var) && TREE_CODE (origvar) != SSA_NAME)
1811 else if (DECL_EXTERNAL (var))
1813 else if (DECL_HAS_VALUE_EXPR_P (var))
1815 else if (TREE_STATIC (var))
1817 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1819 else if (TREE_TYPE (var) == error_mark_node)
1821 if (really_expand)
1822 expand_one_error_var (var);
1824 else if (VAR_P (var) && DECL_HARD_REGISTER (var))
1826 if (really_expand)
1828 expand_one_hard_reg_var (var);
1829 if (!DECL_HARD_REGISTER (var))
1830 /* Invalid register specification. */
1831 expand_one_error_var (var);
1834 else if (use_register_for_decl (var)
1835 && (!forced_stack_var
1836 || !bitmap_bit_p (forced_stack_var, DECL_UID (var))))
1838 if (really_expand)
1839 expand_one_register_var (origvar);
1841 else if (!poly_int_tree_p (DECL_SIZE_UNIT (var), &size)
1842 || !valid_constant_size_p (DECL_SIZE_UNIT (var)))
1844 /* Reject variables which cover more than half of the address-space. */
1845 if (really_expand)
1847 if (DECL_NONLOCAL_FRAME (var))
1848 error_at (DECL_SOURCE_LOCATION (current_function_decl),
1849 "total size of local objects is too large");
1850 else
1851 error_at (DECL_SOURCE_LOCATION (var),
1852 "size of variable %q+D is too large", var);
1853 expand_one_error_var (var);
1856 else if (defer_stack_allocation (var, toplevel))
1857 add_stack_var (origvar, really_expand);
1858 else
1860 if (really_expand)
1862 if (lookup_attribute ("naked",
1863 DECL_ATTRIBUTES (current_function_decl)))
1864 error ("cannot allocate stack for variable %q+D, naked function",
1865 var);
1867 expand_one_stack_var (origvar);
1869 return size;
1871 return 0;
1874 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1875 expanding variables. Those variables that can be put into registers
1876 are allocated pseudos; those that can't are put on the stack.
1878 TOPLEVEL is true if this is the outermost BLOCK. */
1880 static void
1881 expand_used_vars_for_block (tree block, bool toplevel, bitmap forced_stack_vars)
1883 tree t;
1885 /* Expand all variables at this level. */
1886 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1887 if (TREE_USED (t)
1888 && ((!VAR_P (t) && TREE_CODE (t) != RESULT_DECL)
1889 || !DECL_NONSHAREABLE (t)))
1890 expand_one_var (t, toplevel, true, forced_stack_vars);
1892 /* Expand all variables at containing levels. */
1893 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1894 expand_used_vars_for_block (t, false, forced_stack_vars);
1897 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1898 and clear TREE_USED on all local variables. */
1900 static void
1901 clear_tree_used (tree block)
1903 tree t;
1905 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1906 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1907 if ((!VAR_P (t) && TREE_CODE (t) != RESULT_DECL)
1908 || !DECL_NONSHAREABLE (t))
1909 TREE_USED (t) = 0;
1911 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1912 clear_tree_used (t);
1915 /* Examine TYPE and determine a bit mask of the following features. */
1917 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1918 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1919 #define SPCT_HAS_ARRAY 4
1920 #define SPCT_HAS_AGGREGATE 8
1922 static unsigned int
1923 stack_protect_classify_type (tree type)
1925 unsigned int ret = 0;
1926 tree t;
1928 switch (TREE_CODE (type))
1930 case ARRAY_TYPE:
1931 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1932 if (t == char_type_node
1933 || t == signed_char_type_node
1934 || t == unsigned_char_type_node)
1936 unsigned HOST_WIDE_INT max = param_ssp_buffer_size;
1937 unsigned HOST_WIDE_INT len;
1939 if (!TYPE_SIZE_UNIT (type)
1940 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1941 len = max;
1942 else
1943 len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1945 if (len < max)
1946 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1947 else
1948 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1950 else
1951 ret = SPCT_HAS_ARRAY;
1952 break;
1954 case UNION_TYPE:
1955 case QUAL_UNION_TYPE:
1956 case RECORD_TYPE:
1957 ret = SPCT_HAS_AGGREGATE;
1958 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1959 if (TREE_CODE (t) == FIELD_DECL)
1960 ret |= stack_protect_classify_type (TREE_TYPE (t));
1961 break;
1963 default:
1964 break;
1967 return ret;
1970 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1971 part of the local stack frame. Remember if we ever return nonzero for
1972 any variable in this function. The return value is the phase number in
1973 which the variable should be allocated. */
1975 static int
1976 stack_protect_decl_phase (tree decl)
1978 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1979 int ret = 0;
1981 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1982 has_short_buffer = true;
1984 tree attribs = DECL_ATTRIBUTES (current_function_decl);
1985 if (!lookup_attribute ("no_stack_protector", attribs)
1986 && (flag_stack_protect == SPCT_FLAG_ALL
1987 || flag_stack_protect == SPCT_FLAG_STRONG
1988 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1989 && lookup_attribute ("stack_protect", attribs))))
1991 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1992 && !(bits & SPCT_HAS_AGGREGATE))
1993 ret = 1;
1994 else if (bits & SPCT_HAS_ARRAY)
1995 ret = 2;
1997 else
1998 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
2000 if (ret)
2001 has_protected_decls = true;
2003 return ret;
2006 /* Two helper routines that check for phase 1 and phase 2. These are used
2007 as callbacks for expand_stack_vars. */
2009 static bool
2010 stack_protect_decl_phase_1 (size_t i)
2012 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
2015 static bool
2016 stack_protect_decl_phase_2 (size_t i)
2018 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
2021 /* And helper function that checks for asan phase (with stack protector
2022 it is phase 3). This is used as callback for expand_stack_vars.
2023 Returns true if any of the vars in the partition need to be protected. */
2025 static bool
2026 asan_decl_phase_3 (size_t i)
2028 while (i != EOC)
2030 if (asan_protect_stack_decl (stack_vars[i].decl))
2031 return true;
2032 i = stack_vars[i].next;
2034 return false;
2037 /* Ensure that variables in different stack protection phases conflict
2038 so that they are not merged and share the same stack slot.
2039 Return true if there are any address taken variables. */
2041 static bool
2042 add_stack_protection_conflicts (void)
2044 size_t i, j, n = stack_vars_num;
2045 unsigned char *phase;
2046 bool ret = false;
2048 phase = XNEWVEC (unsigned char, n);
2049 for (i = 0; i < n; ++i)
2051 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
2052 if (TREE_ADDRESSABLE (stack_vars[i].decl))
2053 ret = true;
2056 for (i = 0; i < n; ++i)
2058 unsigned char ph_i = phase[i];
2059 for (j = i + 1; j < n; ++j)
2060 if (ph_i != phase[j])
2061 add_stack_var_conflict (i, j);
2064 XDELETEVEC (phase);
2065 return ret;
2068 /* Create a decl for the guard at the top of the stack frame. */
2070 static void
2071 create_stack_guard (void)
2073 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
2074 VAR_DECL, NULL, ptr_type_node);
2075 TREE_THIS_VOLATILE (guard) = 1;
2076 TREE_USED (guard) = 1;
2077 expand_one_stack_var (guard);
2078 crtl->stack_protect_guard = guard;
2081 /* Prepare for expanding variables. */
2082 static void
2083 init_vars_expansion (void)
2085 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
2086 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
2088 /* A map from decl to stack partition. */
2089 decl_to_stack_part = new hash_map<tree, size_t>;
2091 /* Initialize local stack smashing state. */
2092 has_protected_decls = false;
2093 has_short_buffer = false;
2094 if (hwasan_sanitize_stack_p ())
2095 hwasan_record_frame_init ();
2098 /* Free up stack variable graph data. */
2099 static void
2100 fini_vars_expansion (void)
2102 bitmap_obstack_release (&stack_var_bitmap_obstack);
2103 if (stack_vars)
2104 XDELETEVEC (stack_vars);
2105 if (stack_vars_sorted)
2106 XDELETEVEC (stack_vars_sorted);
2107 stack_vars = NULL;
2108 stack_vars_sorted = NULL;
2109 stack_vars_alloc = stack_vars_num = 0;
2110 delete decl_to_stack_part;
2111 decl_to_stack_part = NULL;
2114 /* Make a fair guess for the size of the stack frame of the function
2115 in NODE. This doesn't have to be exact, the result is only used in
2116 the inline heuristics. So we don't want to run the full stack var
2117 packing algorithm (which is quadratic in the number of stack vars).
2118 Instead, we calculate the total size of all stack vars. This turns
2119 out to be a pretty fair estimate -- packing of stack vars doesn't
2120 happen very often. */
2122 HOST_WIDE_INT
2123 estimated_stack_frame_size (struct cgraph_node *node)
2125 poly_int64 size = 0;
2126 size_t i;
2127 tree var;
2128 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
2130 push_cfun (fn);
2132 init_vars_expansion ();
2134 FOR_EACH_LOCAL_DECL (fn, i, var)
2135 if (auto_var_in_fn_p (var, fn->decl))
2136 size += expand_one_var (var, true, false);
2138 if (stack_vars_num > 0)
2140 /* Fake sorting the stack vars for account_stack_vars (). */
2141 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
2142 for (i = 0; i < stack_vars_num; ++i)
2143 stack_vars_sorted[i] = i;
2144 size += account_stack_vars ();
2147 fini_vars_expansion ();
2148 pop_cfun ();
2149 return estimated_poly_value (size);
2152 /* Check if the current function has calls that use a return slot. */
2154 static bool
2155 stack_protect_return_slot_p ()
2157 basic_block bb;
2159 FOR_ALL_BB_FN (bb, cfun)
2160 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
2161 !gsi_end_p (gsi); gsi_next (&gsi))
2163 gimple *stmt = gsi_stmt (gsi);
2164 /* This assumes that calls to internal-only functions never
2165 use a return slot. */
2166 if (is_gimple_call (stmt)
2167 && !gimple_call_internal_p (stmt)
2168 && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
2169 gimple_call_fndecl (stmt)))
2170 return true;
2172 return false;
2175 /* Expand all variables used in the function. */
2177 static rtx_insn *
2178 expand_used_vars (bitmap forced_stack_vars)
2180 tree var, outer_block = DECL_INITIAL (current_function_decl);
2181 auto_vec<tree> maybe_local_decls;
2182 rtx_insn *var_end_seq = NULL;
2183 unsigned i;
2184 unsigned len;
2185 bool gen_stack_protect_signal = false;
2187 /* Compute the phase of the stack frame for this function. */
2189 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2190 int off = targetm.starting_frame_offset () % align;
2191 frame_phase = off ? align - off : 0;
2194 /* Set TREE_USED on all variables in the local_decls. */
2195 FOR_EACH_LOCAL_DECL (cfun, i, var)
2196 TREE_USED (var) = 1;
2197 /* Clear TREE_USED on all variables associated with a block scope. */
2198 clear_tree_used (DECL_INITIAL (current_function_decl));
2200 init_vars_expansion ();
2202 if (targetm.use_pseudo_pic_reg ())
2203 pic_offset_table_rtx = gen_reg_rtx (Pmode);
2205 for (i = 0; i < SA.map->num_partitions; i++)
2207 if (bitmap_bit_p (SA.partitions_for_parm_default_defs, i))
2208 continue;
2210 tree var = partition_to_var (SA.map, i);
2212 gcc_assert (!virtual_operand_p (var));
2214 expand_one_ssa_partition (var);
2217 if (flag_stack_protect == SPCT_FLAG_STRONG)
2218 gen_stack_protect_signal = stack_protect_return_slot_p ();
2220 /* At this point all variables on the local_decls with TREE_USED
2221 set are not associated with any block scope. Lay them out. */
2223 len = vec_safe_length (cfun->local_decls);
2224 FOR_EACH_LOCAL_DECL (cfun, i, var)
2226 bool expand_now = false;
2228 /* Expanded above already. */
2229 if (is_gimple_reg (var))
2231 TREE_USED (var) = 0;
2232 goto next;
2234 /* We didn't set a block for static or extern because it's hard
2235 to tell the difference between a global variable (re)declared
2236 in a local scope, and one that's really declared there to
2237 begin with. And it doesn't really matter much, since we're
2238 not giving them stack space. Expand them now. */
2239 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
2240 expand_now = true;
2242 /* Expand variables not associated with any block now. Those created by
2243 the optimizers could be live anywhere in the function. Those that
2244 could possibly have been scoped originally and detached from their
2245 block will have their allocation deferred so we coalesce them with
2246 others when optimization is enabled. */
2247 else if (TREE_USED (var))
2248 expand_now = true;
2250 /* Finally, mark all variables on the list as used. We'll use
2251 this in a moment when we expand those associated with scopes. */
2252 TREE_USED (var) = 1;
2254 if (expand_now)
2255 expand_one_var (var, true, true, forced_stack_vars);
2257 next:
2258 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
2260 rtx rtl = DECL_RTL_IF_SET (var);
2262 /* Keep artificial non-ignored vars in cfun->local_decls
2263 chain until instantiate_decls. */
2264 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2265 add_local_decl (cfun, var);
2266 else if (rtl == NULL_RTX)
2267 /* If rtl isn't set yet, which can happen e.g. with
2268 -fstack-protector, retry before returning from this
2269 function. */
2270 maybe_local_decls.safe_push (var);
2274 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
2276 +-----------------+-----------------+
2277 | ...processed... | ...duplicates...|
2278 +-----------------+-----------------+
2280 +-- LEN points here.
2282 We just want the duplicates, as those are the artificial
2283 non-ignored vars that we want to keep until instantiate_decls.
2284 Move them down and truncate the array. */
2285 if (!vec_safe_is_empty (cfun->local_decls))
2286 cfun->local_decls->block_remove (0, len);
2288 /* At this point, all variables within the block tree with TREE_USED
2289 set are actually used by the optimized function. Lay them out. */
2290 expand_used_vars_for_block (outer_block, true, forced_stack_vars);
2292 tree attribs = DECL_ATTRIBUTES (current_function_decl);
2293 if (stack_vars_num > 0)
2295 bool has_addressable_vars = false;
2297 add_scope_conflicts ();
2299 /* If stack protection is enabled, we don't share space between
2300 vulnerable data and non-vulnerable data. */
2301 if (flag_stack_protect != 0
2302 && !lookup_attribute ("no_stack_protector", attribs)
2303 && (flag_stack_protect != SPCT_FLAG_EXPLICIT
2304 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
2305 && lookup_attribute ("stack_protect", attribs))))
2306 has_addressable_vars = add_stack_protection_conflicts ();
2308 if (flag_stack_protect == SPCT_FLAG_STRONG && has_addressable_vars)
2309 gen_stack_protect_signal = true;
2311 /* Now that we have collected all stack variables, and have computed a
2312 minimal interference graph, attempt to save some stack space. */
2313 partition_stack_vars ();
2314 if (dump_file)
2315 dump_stack_var_partition ();
2319 if (!lookup_attribute ("no_stack_protector", attribs))
2320 switch (flag_stack_protect)
2322 case SPCT_FLAG_ALL:
2323 create_stack_guard ();
2324 break;
2326 case SPCT_FLAG_STRONG:
2327 if (gen_stack_protect_signal
2328 || cfun->calls_alloca
2329 || has_protected_decls
2330 || lookup_attribute ("stack_protect", attribs))
2331 create_stack_guard ();
2332 break;
2334 case SPCT_FLAG_DEFAULT:
2335 if (cfun->calls_alloca
2336 || has_protected_decls
2337 || lookup_attribute ("stack_protect", attribs))
2338 create_stack_guard ();
2339 break;
2341 case SPCT_FLAG_EXPLICIT:
2342 if (lookup_attribute ("stack_protect", attribs))
2343 create_stack_guard ();
2344 break;
2346 default:
2347 break;
2350 /* Assign rtl to each variable based on these partitions. */
2351 if (stack_vars_num > 0)
2353 class stack_vars_data data;
2355 data.asan_base = NULL_RTX;
2356 data.asan_alignb = 0;
2358 /* Reorder decls to be protected by iterating over the variables
2359 array multiple times, and allocating out of each phase in turn. */
2360 /* ??? We could probably integrate this into the qsort we did
2361 earlier, such that we naturally see these variables first,
2362 and thus naturally allocate things in the right order. */
2363 if (has_protected_decls)
2365 /* Phase 1 contains only character arrays. */
2366 expand_stack_vars (stack_protect_decl_phase_1, &data);
2368 /* Phase 2 contains other kinds of arrays. */
2369 if (!lookup_attribute ("no_stack_protector", attribs)
2370 && (flag_stack_protect == SPCT_FLAG_ALL
2371 || flag_stack_protect == SPCT_FLAG_STRONG
2372 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
2373 && lookup_attribute ("stack_protect", attribs))))
2374 expand_stack_vars (stack_protect_decl_phase_2, &data);
2377 if (asan_sanitize_stack_p ())
2378 /* Phase 3, any partitions that need asan protection
2379 in addition to phase 1 and 2. */
2380 expand_stack_vars (asan_decl_phase_3, &data);
2382 /* ASAN description strings don't yet have a syntax for expressing
2383 polynomial offsets. */
2384 HOST_WIDE_INT prev_offset;
2385 if (!data.asan_vec.is_empty ()
2386 && frame_offset.is_constant (&prev_offset))
2388 HOST_WIDE_INT offset, sz, redzonesz;
2389 redzonesz = ASAN_RED_ZONE_SIZE;
2390 sz = data.asan_vec[0] - prev_offset;
2391 if (data.asan_alignb > ASAN_RED_ZONE_SIZE
2392 && data.asan_alignb <= 4096
2393 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
2394 redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
2395 & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
2396 /* Allocating a constant amount of space from a constant
2397 starting offset must give a constant result. */
2398 offset = (alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE)
2399 .to_constant ());
2400 data.asan_vec.safe_push (prev_offset);
2401 data.asan_vec.safe_push (offset);
2402 /* Leave space for alignment if STRICT_ALIGNMENT. */
2403 if (STRICT_ALIGNMENT)
2404 alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
2405 << ASAN_SHADOW_SHIFT)
2406 / BITS_PER_UNIT, 1);
2408 var_end_seq
2409 = asan_emit_stack_protection (virtual_stack_vars_rtx,
2410 data.asan_base,
2411 data.asan_alignb,
2412 data.asan_vec.address (),
2413 data.asan_decl_vec.address (),
2414 data.asan_vec.length ());
2417 expand_stack_vars (NULL, &data);
2420 if (hwasan_sanitize_stack_p ())
2421 hwasan_emit_prologue ();
2422 if (asan_sanitize_allocas_p () && cfun->calls_alloca)
2423 var_end_seq = asan_emit_allocas_unpoison (virtual_stack_dynamic_rtx,
2424 virtual_stack_vars_rtx,
2425 var_end_seq);
2426 else if (hwasan_sanitize_allocas_p () && cfun->calls_alloca)
2427 /* When using out-of-line instrumentation we only want to emit one function
2428 call for clearing the tags in a region of shadow stack. When there are
2429 alloca calls in this frame we want to emit a call using the
2430 virtual_stack_dynamic_rtx, but when not we use the hwasan_frame_extent
2431 rtx we created in expand_stack_vars. */
2432 var_end_seq = hwasan_emit_untag_frame (virtual_stack_dynamic_rtx,
2433 virtual_stack_vars_rtx);
2434 else if (hwasan_sanitize_stack_p ())
2435 /* If no variables were stored on the stack, `hwasan_get_frame_extent`
2436 will return NULL_RTX and hence `hwasan_emit_untag_frame` will return
2437 NULL (i.e. an empty sequence). */
2438 var_end_seq = hwasan_emit_untag_frame (hwasan_get_frame_extent (),
2439 virtual_stack_vars_rtx);
2441 fini_vars_expansion ();
2443 /* If there were any artificial non-ignored vars without rtl
2444 found earlier, see if deferred stack allocation hasn't assigned
2445 rtl to them. */
2446 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
2448 rtx rtl = DECL_RTL_IF_SET (var);
2450 /* Keep artificial non-ignored vars in cfun->local_decls
2451 chain until instantiate_decls. */
2452 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2453 add_local_decl (cfun, var);
2456 /* If the target requires that FRAME_OFFSET be aligned, do it. */
2457 if (STACK_ALIGNMENT_NEEDED)
2459 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2460 if (FRAME_GROWS_DOWNWARD)
2461 frame_offset = aligned_lower_bound (frame_offset, align);
2462 else
2463 frame_offset = aligned_upper_bound (frame_offset, align);
2466 return var_end_seq;
2470 /* If we need to produce a detailed dump, print the tree representation
2471 for STMT to the dump file. SINCE is the last RTX after which the RTL
2472 generated for STMT should have been appended. */
2474 static void
2475 maybe_dump_rtl_for_gimple_stmt (gimple *stmt, rtx_insn *since)
2477 if (dump_file && (dump_flags & TDF_DETAILS))
2479 fprintf (dump_file, "\n;; ");
2480 print_gimple_stmt (dump_file, stmt, 0,
2481 TDF_SLIM | (dump_flags & TDF_LINENO));
2482 fprintf (dump_file, "\n");
2484 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
2488 /* Maps the blocks that do not contain tree labels to rtx labels. */
2490 static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
2492 /* Returns the label_rtx expression for a label starting basic block BB. */
2494 static rtx_code_label *
2495 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
2497 if (bb->flags & BB_RTL)
2498 return block_label (bb);
2500 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
2501 if (elt)
2502 return *elt;
2504 /* Find the tree label if it is present. */
2505 gimple_stmt_iterator gsi = gsi_start_bb (bb);
2506 glabel *lab_stmt;
2507 if (!gsi_end_p (gsi)
2508 && (lab_stmt = dyn_cast <glabel *> (gsi_stmt (gsi)))
2509 && !DECL_NONLOCAL (gimple_label_label (lab_stmt)))
2510 return jump_target_rtx (gimple_label_label (lab_stmt));
2512 rtx_code_label *l = gen_label_rtx ();
2513 lab_rtx_for_bb->put (bb, l);
2514 return l;
2518 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2519 of a basic block where we just expanded the conditional at the end,
2520 possibly clean up the CFG and instruction sequence. LAST is the
2521 last instruction before the just emitted jump sequence. */
2523 static void
2524 maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2526 /* Special case: when jumpif decides that the condition is
2527 trivial it emits an unconditional jump (and the necessary
2528 barrier). But we still have two edges, the fallthru one is
2529 wrong. purge_dead_edges would clean this up later. Unfortunately
2530 we have to insert insns (and split edges) before
2531 find_many_sub_basic_blocks and hence before purge_dead_edges.
2532 But splitting edges might create new blocks which depend on the
2533 fact that if there are two edges there's no barrier. So the
2534 barrier would get lost and verify_flow_info would ICE. Instead
2535 of auditing all edge splitters to care for the barrier (which
2536 normally isn't there in a cleaned CFG), fix it here. */
2537 if (BARRIER_P (get_last_insn ()))
2539 rtx_insn *insn;
2540 remove_edge (e);
2541 /* Now, we have a single successor block, if we have insns to
2542 insert on the remaining edge we potentially will insert
2543 it at the end of this block (if the dest block isn't feasible)
2544 in order to avoid splitting the edge. This insertion will take
2545 place in front of the last jump. But we might have emitted
2546 multiple jumps (conditional and one unconditional) to the
2547 same destination. Inserting in front of the last one then
2548 is a problem. See PR 40021. We fix this by deleting all
2549 jumps except the last unconditional one. */
2550 insn = PREV_INSN (get_last_insn ());
2551 /* Make sure we have an unconditional jump. Otherwise we're
2552 confused. */
2553 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2554 for (insn = PREV_INSN (insn); insn != last;)
2556 insn = PREV_INSN (insn);
2557 if (JUMP_P (NEXT_INSN (insn)))
2559 if (!any_condjump_p (NEXT_INSN (insn)))
2561 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2562 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2564 delete_insn (NEXT_INSN (insn));
2570 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2571 Returns a new basic block if we've terminated the current basic
2572 block and created a new one. */
2574 static basic_block
2575 expand_gimple_cond (basic_block bb, gcond *stmt)
2577 basic_block new_bb, dest;
2578 edge true_edge;
2579 edge false_edge;
2580 rtx_insn *last2, *last;
2581 enum tree_code code;
2582 tree op0, op1;
2584 code = gimple_cond_code (stmt);
2585 op0 = gimple_cond_lhs (stmt);
2586 op1 = gimple_cond_rhs (stmt);
2587 /* We're sometimes presented with such code:
2588 D.123_1 = x < y;
2589 if (D.123_1 != 0)
2591 This would expand to two comparisons which then later might
2592 be cleaned up by combine. But some pattern matchers like if-conversion
2593 work better when there's only one compare, so make up for this
2594 here as special exception if TER would have made the same change. */
2595 if (SA.values
2596 && TREE_CODE (op0) == SSA_NAME
2597 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2598 && TREE_CODE (op1) == INTEGER_CST
2599 && ((gimple_cond_code (stmt) == NE_EXPR
2600 && integer_zerop (op1))
2601 || (gimple_cond_code (stmt) == EQ_EXPR
2602 && integer_onep (op1)))
2603 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2605 gimple *second = SSA_NAME_DEF_STMT (op0);
2606 if (gimple_code (second) == GIMPLE_ASSIGN)
2608 enum tree_code code2 = gimple_assign_rhs_code (second);
2609 if (TREE_CODE_CLASS (code2) == tcc_comparison)
2611 code = code2;
2612 op0 = gimple_assign_rhs1 (second);
2613 op1 = gimple_assign_rhs2 (second);
2615 /* If jumps are cheap and the target does not support conditional
2616 compare, turn some more codes into jumpy sequences. */
2617 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4
2618 && targetm.gen_ccmp_first == NULL)
2620 if ((code2 == BIT_AND_EXPR
2621 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2622 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2623 || code2 == TRUTH_AND_EXPR)
2625 code = TRUTH_ANDIF_EXPR;
2626 op0 = gimple_assign_rhs1 (second);
2627 op1 = gimple_assign_rhs2 (second);
2629 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2631 code = TRUTH_ORIF_EXPR;
2632 op0 = gimple_assign_rhs1 (second);
2633 op1 = gimple_assign_rhs2 (second);
2639 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
2640 into (x - C2) * C3 < C4. */
2641 if ((code == EQ_EXPR || code == NE_EXPR)
2642 && TREE_CODE (op0) == SSA_NAME
2643 && TREE_CODE (op1) == INTEGER_CST)
2644 code = maybe_optimize_mod_cmp (code, &op0, &op1);
2646 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
2647 if (!TYPE_UNSIGNED (TREE_TYPE (op0))
2648 && (code == LT_EXPR || code == LE_EXPR
2649 || code == GT_EXPR || code == GE_EXPR)
2650 && integer_zerop (op1)
2651 && TREE_CODE (op0) == SSA_NAME)
2652 maybe_optimize_sub_cmp_0 (code, &op0, &op1);
2654 last2 = last = get_last_insn ();
2656 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2657 set_curr_insn_location (gimple_location (stmt));
2659 /* These flags have no purpose in RTL land. */
2660 true_edge->flags &= ~EDGE_TRUE_VALUE;
2661 false_edge->flags &= ~EDGE_FALSE_VALUE;
2663 /* We can either have a pure conditional jump with one fallthru edge or
2664 two-way jump that needs to be decomposed into two basic blocks. */
2665 if (false_edge->dest == bb->next_bb)
2667 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2668 true_edge->probability);
2669 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2670 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2671 set_curr_insn_location (true_edge->goto_locus);
2672 false_edge->flags |= EDGE_FALLTHRU;
2673 maybe_cleanup_end_of_block (false_edge, last);
2674 return NULL;
2676 if (true_edge->dest == bb->next_bb)
2678 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2679 false_edge->probability);
2680 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2681 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2682 set_curr_insn_location (false_edge->goto_locus);
2683 true_edge->flags |= EDGE_FALLTHRU;
2684 maybe_cleanup_end_of_block (true_edge, last);
2685 return NULL;
2688 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2689 true_edge->probability);
2690 last = get_last_insn ();
2691 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2692 set_curr_insn_location (false_edge->goto_locus);
2693 emit_jump (label_rtx_for_bb (false_edge->dest));
2695 BB_END (bb) = last;
2696 if (BARRIER_P (BB_END (bb)))
2697 BB_END (bb) = PREV_INSN (BB_END (bb));
2698 update_bb_for_insn (bb);
2700 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2701 dest = false_edge->dest;
2702 redirect_edge_succ (false_edge, new_bb);
2703 false_edge->flags |= EDGE_FALLTHRU;
2704 new_bb->count = false_edge->count ();
2705 loop_p loop = find_common_loop (bb->loop_father, dest->loop_father);
2706 add_bb_to_loop (new_bb, loop);
2707 if (loop->latch == bb
2708 && loop->header == dest)
2709 loop->latch = new_bb;
2710 make_single_succ_edge (new_bb, dest, 0);
2711 if (BARRIER_P (BB_END (new_bb)))
2712 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2713 update_bb_for_insn (new_bb);
2715 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2717 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2719 set_curr_insn_location (true_edge->goto_locus);
2720 true_edge->goto_locus = curr_insn_location ();
2723 return new_bb;
2726 /* Mark all calls that can have a transaction restart. */
2728 static void
2729 mark_transaction_restart_calls (gimple *stmt)
2731 struct tm_restart_node dummy;
2732 tm_restart_node **slot;
2734 if (!cfun->gimple_df->tm_restart)
2735 return;
2737 dummy.stmt = stmt;
2738 slot = cfun->gimple_df->tm_restart->find_slot (&dummy, NO_INSERT);
2739 if (slot)
2741 struct tm_restart_node *n = *slot;
2742 tree list = n->label_or_list;
2743 rtx_insn *insn;
2745 for (insn = next_real_insn (get_last_insn ());
2746 !CALL_P (insn);
2747 insn = next_real_insn (insn))
2748 continue;
2750 if (TREE_CODE (list) == LABEL_DECL)
2751 add_reg_note (insn, REG_TM, label_rtx (list));
2752 else
2753 for (; list ; list = TREE_CHAIN (list))
2754 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2758 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2759 statement STMT. */
2761 static void
2762 expand_call_stmt (gcall *stmt)
2764 tree exp, decl, lhs;
2765 bool builtin_p;
2766 size_t i;
2768 if (gimple_call_internal_p (stmt))
2770 expand_internal_call (stmt);
2771 return;
2774 /* If this is a call to a built-in function and it has no effect other
2775 than setting the lhs, try to implement it using an internal function
2776 instead. */
2777 decl = gimple_call_fndecl (stmt);
2778 if (gimple_call_lhs (stmt)
2779 && !gimple_has_side_effects (stmt)
2780 && (optimize || (decl && called_as_built_in (decl))))
2782 internal_fn ifn = replacement_internal_fn (stmt);
2783 if (ifn != IFN_LAST)
2785 expand_internal_call (ifn, stmt);
2786 return;
2790 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2792 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2793 builtin_p = decl && fndecl_built_in_p (decl);
2795 /* If this is not a builtin function, the function type through which the
2796 call is made may be different from the type of the function. */
2797 if (!builtin_p)
2798 CALL_EXPR_FN (exp)
2799 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2800 CALL_EXPR_FN (exp));
2802 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2803 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2805 for (i = 0; i < gimple_call_num_args (stmt); i++)
2807 tree arg = gimple_call_arg (stmt, i);
2808 gimple *def;
2809 /* TER addresses into arguments of builtin functions so we have a
2810 chance to infer more correct alignment information. See PR39954. */
2811 if (builtin_p
2812 && TREE_CODE (arg) == SSA_NAME
2813 && (def = get_gimple_for_ssa_name (arg))
2814 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2815 arg = gimple_assign_rhs1 (def);
2816 CALL_EXPR_ARG (exp, i) = arg;
2819 if (gimple_has_side_effects (stmt)
2820 /* ??? Downstream in expand_expr_real_1 we assume that expressions
2821 w/o side-effects do not throw so work around this here. */
2822 || stmt_could_throw_p (cfun, stmt))
2823 TREE_SIDE_EFFECTS (exp) = 1;
2825 if (gimple_call_nothrow_p (stmt))
2826 TREE_NOTHROW (exp) = 1;
2828 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2829 CALL_EXPR_MUST_TAIL_CALL (exp) = gimple_call_must_tail_p (stmt);
2830 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2831 if (decl
2832 && fndecl_built_in_p (decl, BUILT_IN_NORMAL)
2833 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2834 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2835 else
2836 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2837 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2838 CALL_EXPR_BY_DESCRIPTOR (exp) = gimple_call_by_descriptor_p (stmt);
2839 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2841 /* Must come after copying location. */
2842 copy_warning (exp, stmt);
2844 /* Ensure RTL is created for debug args. */
2845 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2847 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2848 unsigned int ix;
2849 tree dtemp;
2851 if (debug_args)
2852 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2854 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2855 expand_debug_expr (dtemp);
2859 rtx_insn *before_call = get_last_insn ();
2860 lhs = gimple_call_lhs (stmt);
2861 if (lhs)
2862 expand_assignment (lhs, exp, false);
2863 else
2864 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2866 /* If the gimple call is an indirect call and has 'nocf_check'
2867 attribute find a generated CALL insn to mark it as no
2868 control-flow verification is needed. */
2869 if (gimple_call_nocf_check_p (stmt)
2870 && !gimple_call_fndecl (stmt))
2872 rtx_insn *last = get_last_insn ();
2873 while (!CALL_P (last)
2874 && last != before_call)
2875 last = PREV_INSN (last);
2877 if (last != before_call)
2878 add_reg_note (last, REG_CALL_NOCF_CHECK, const0_rtx);
2881 mark_transaction_restart_calls (stmt);
2885 /* Generate RTL for an asm statement (explicit assembler code).
2886 STRING is a STRING_CST node containing the assembler code text,
2887 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
2888 insn is volatile; don't optimize it. */
2890 static void
2891 expand_asm_loc (tree string, int vol, location_t locus)
2893 rtx body;
2895 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2896 ggc_strdup (TREE_STRING_POINTER (string)),
2897 locus);
2899 MEM_VOLATILE_P (body) = vol;
2901 /* Non-empty basic ASM implicitly clobbers memory. */
2902 if (TREE_STRING_LENGTH (string) != 0)
2904 rtx asm_op, clob;
2905 unsigned i, nclobbers;
2906 auto_vec<rtx> input_rvec, output_rvec;
2907 auto_vec<machine_mode> input_mode;
2908 auto_vec<const char *> constraints;
2909 auto_vec<rtx> use_rvec;
2910 auto_vec<rtx> clobber_rvec;
2911 HARD_REG_SET clobbered_regs;
2912 CLEAR_HARD_REG_SET (clobbered_regs);
2914 clob = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2915 clobber_rvec.safe_push (clob);
2917 if (targetm.md_asm_adjust)
2918 targetm.md_asm_adjust (output_rvec, input_rvec, input_mode,
2919 constraints, use_rvec, clobber_rvec,
2920 clobbered_regs, locus);
2922 asm_op = body;
2923 nclobbers = clobber_rvec.length ();
2924 auto nuses = use_rvec.length ();
2925 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (1 + nuses + nclobbers));
2927 i = 0;
2928 XVECEXP (body, 0, i++) = asm_op;
2929 for (rtx use : use_rvec)
2930 XVECEXP (body, 0, i++) = gen_rtx_USE (VOIDmode, use);
2931 for (rtx clobber : clobber_rvec)
2932 XVECEXP (body, 0, i++) = gen_rtx_CLOBBER (VOIDmode, clobber);
2935 emit_insn (body);
2938 /* Return the number of times character C occurs in string S. */
2939 static int
2940 n_occurrences (int c, const char *s)
2942 int n = 0;
2943 while (*s)
2944 n += (*s++ == c);
2945 return n;
2948 /* A subroutine of expand_asm_operands. Check that all operands have
2949 the same number of alternatives. Return true if so. */
2951 static bool
2952 check_operand_nalternatives (const vec<const char *> &constraints)
2954 unsigned len = constraints.length();
2955 if (len > 0)
2957 int nalternatives = n_occurrences (',', constraints[0]);
2959 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2961 error ("too many alternatives in %<asm%>");
2962 return false;
2965 for (unsigned i = 1; i < len; ++i)
2966 if (n_occurrences (',', constraints[i]) != nalternatives)
2968 error ("operand constraints for %<asm%> differ "
2969 "in number of alternatives");
2970 return false;
2973 return true;
2976 /* Check for overlap between registers marked in CLOBBERED_REGS and
2977 anything inappropriate in T. Emit error and return the register
2978 variable definition for error, NULL_TREE for ok. */
2980 static bool
2981 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs,
2982 location_t loc)
2984 /* Conflicts between asm-declared register variables and the clobber
2985 list are not allowed. */
2986 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2988 if (overlap)
2990 error_at (loc, "%<asm%> specifier for variable %qE conflicts with "
2991 "%<asm%> clobber list", DECL_NAME (overlap));
2993 /* Reset registerness to stop multiple errors emitted for a single
2994 variable. */
2995 DECL_REGISTER (overlap) = 0;
2996 return true;
2999 return false;
3002 /* Check that the given REGNO spanning NREGS is a valid
3003 asm clobber operand. Some HW registers cannot be
3004 saved/restored, hence they should not be clobbered by
3005 asm statements. */
3006 static bool
3007 asm_clobber_reg_is_valid (int regno, int nregs, const char *regname)
3009 bool is_valid = true;
3010 HARD_REG_SET regset;
3012 CLEAR_HARD_REG_SET (regset);
3014 add_range_to_hard_reg_set (&regset, regno, nregs);
3016 /* Clobbering the PIC register is an error. */
3017 if (PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3018 && overlaps_hard_reg_set_p (regset, Pmode, PIC_OFFSET_TABLE_REGNUM))
3020 /* ??? Diagnose during gimplification? */
3021 error ("PIC register clobbered by %qs in %<asm%>", regname);
3022 is_valid = false;
3024 else if (!in_hard_reg_set_p
3025 (accessible_reg_set, reg_raw_mode[regno], regno))
3027 /* ??? Diagnose during gimplification? */
3028 error ("the register %qs cannot be clobbered in %<asm%>"
3029 " for the current target", regname);
3030 is_valid = false;
3033 /* Clobbering the stack pointer register is deprecated. GCC expects
3034 the value of the stack pointer after an asm statement to be the same
3035 as it was before, so no asm can validly clobber the stack pointer in
3036 the usual sense. Adding the stack pointer to the clobber list has
3037 traditionally had some undocumented and somewhat obscure side-effects. */
3038 if (overlaps_hard_reg_set_p (regset, Pmode, STACK_POINTER_REGNUM))
3040 crtl->sp_is_clobbered_by_asm = true;
3041 if (warning (OPT_Wdeprecated, "listing the stack pointer register"
3042 " %qs in a clobber list is deprecated", regname))
3043 inform (input_location, "the value of the stack pointer after"
3044 " an %<asm%> statement must be the same as it was before"
3045 " the statement");
3048 return is_valid;
3051 /* Generate RTL for an asm statement with arguments.
3052 STRING is the instruction template.
3053 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
3054 Each output or input has an expression in the TREE_VALUE and
3055 a tree list in TREE_PURPOSE which in turn contains a constraint
3056 name in TREE_VALUE (or NULL_TREE) and a constraint string
3057 in TREE_PURPOSE.
3058 CLOBBERS is a list of STRING_CST nodes each naming a hard register
3059 that is clobbered by this insn.
3061 LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
3062 should be the fallthru basic block of the asm goto.
3064 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
3065 Some elements of OUTPUTS may be replaced with trees representing temporary
3066 values. The caller should copy those temporary values to the originally
3067 specified lvalues.
3069 VOL nonzero means the insn is volatile; don't optimize it. */
3071 static void
3072 expand_asm_stmt (gasm *stmt)
3074 class save_input_location
3076 location_t old;
3078 public:
3079 explicit save_input_location(location_t where)
3081 old = input_location;
3082 input_location = where;
3085 ~save_input_location()
3087 input_location = old;
3091 location_t locus = gimple_location (stmt);
3093 if (gimple_asm_input_p (stmt))
3095 const char *s = gimple_asm_string (stmt);
3096 tree string = build_string (strlen (s), s);
3097 expand_asm_loc (string, gimple_asm_volatile_p (stmt), locus);
3098 return;
3101 /* There are some legacy diagnostics in here. */
3102 save_input_location s_i_l(locus);
3104 unsigned noutputs = gimple_asm_noutputs (stmt);
3105 unsigned ninputs = gimple_asm_ninputs (stmt);
3106 unsigned nlabels = gimple_asm_nlabels (stmt);
3107 unsigned i;
3108 bool error_seen = false;
3110 /* ??? Diagnose during gimplification? */
3111 if (ninputs + noutputs + nlabels > MAX_RECOG_OPERANDS)
3113 error_at (locus, "more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
3114 return;
3117 auto_vec<tree, MAX_RECOG_OPERANDS> output_tvec;
3118 auto_vec<tree, MAX_RECOG_OPERANDS> input_tvec;
3119 auto_vec<const char *, MAX_RECOG_OPERANDS> constraints;
3121 /* Copy the gimple vectors into new vectors that we can manipulate. */
3123 output_tvec.safe_grow (noutputs, true);
3124 input_tvec.safe_grow (ninputs, true);
3125 constraints.safe_grow (noutputs + ninputs, true);
3127 for (i = 0; i < noutputs; ++i)
3129 tree t = gimple_asm_output_op (stmt, i);
3130 output_tvec[i] = TREE_VALUE (t);
3131 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
3133 for (i = 0; i < ninputs; i++)
3135 tree t = gimple_asm_input_op (stmt, i);
3136 input_tvec[i] = TREE_VALUE (t);
3137 constraints[i + noutputs]
3138 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
3141 /* ??? Diagnose during gimplification? */
3142 if (! check_operand_nalternatives (constraints))
3143 return;
3145 /* Count the number of meaningful clobbered registers, ignoring what
3146 we would ignore later. */
3147 auto_vec<rtx> clobber_rvec;
3148 HARD_REG_SET clobbered_regs;
3149 CLEAR_HARD_REG_SET (clobbered_regs);
3151 if (unsigned n = gimple_asm_nclobbers (stmt))
3153 clobber_rvec.reserve (n);
3154 for (i = 0; i < n; i++)
3156 tree t = gimple_asm_clobber_op (stmt, i);
3157 const char *regname = TREE_STRING_POINTER (TREE_VALUE (t));
3158 int nregs, j;
3160 j = decode_reg_name_and_count (regname, &nregs);
3161 if (j < 0)
3163 if (j == -2)
3165 /* ??? Diagnose during gimplification? */
3166 error_at (locus, "unknown register name %qs in %<asm%>",
3167 regname);
3168 error_seen = true;
3170 else if (j == -4)
3172 rtx x = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
3173 clobber_rvec.safe_push (x);
3175 else
3177 /* Otherwise we should have -1 == empty string
3178 or -3 == cc, which is not a register. */
3179 gcc_assert (j == -1 || j == -3);
3182 else
3183 for (int reg = j; reg < j + nregs; reg++)
3185 if (!asm_clobber_reg_is_valid (reg, nregs, regname))
3186 return;
3188 SET_HARD_REG_BIT (clobbered_regs, reg);
3189 rtx x = gen_rtx_REG (reg_raw_mode[reg], reg);
3190 clobber_rvec.safe_push (x);
3195 /* First pass over inputs and outputs checks validity and sets
3196 mark_addressable if needed. */
3197 /* ??? Diagnose during gimplification? */
3199 for (i = 0; i < noutputs; ++i)
3201 tree val = output_tvec[i];
3202 tree type = TREE_TYPE (val);
3203 const char *constraint;
3204 bool is_inout;
3205 bool allows_reg;
3206 bool allows_mem;
3208 /* Try to parse the output constraint. If that fails, there's
3209 no point in going further. */
3210 constraint = constraints[i];
3211 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
3212 &allows_mem, &allows_reg, &is_inout))
3213 return;
3215 /* If the output is a hard register, verify it doesn't conflict with
3216 any other operand's possible hard register use. */
3217 if (DECL_P (val)
3218 && REG_P (DECL_RTL (val))
3219 && HARD_REGISTER_P (DECL_RTL (val)))
3221 unsigned j, output_hregno = REGNO (DECL_RTL (val));
3222 bool early_clobber_p = strchr (constraints[i], '&') != NULL;
3223 unsigned long match;
3225 /* Verify the other outputs do not use the same hard register. */
3226 for (j = i + 1; j < noutputs; ++j)
3227 if (DECL_P (output_tvec[j])
3228 && REG_P (DECL_RTL (output_tvec[j]))
3229 && HARD_REGISTER_P (DECL_RTL (output_tvec[j]))
3230 && output_hregno == REGNO (DECL_RTL (output_tvec[j])))
3232 error_at (locus, "invalid hard register usage between output "
3233 "operands");
3234 error_seen = true;
3237 /* Verify matching constraint operands use the same hard register
3238 and that the non-matching constraint operands do not use the same
3239 hard register if the output is an early clobber operand. */
3240 for (j = 0; j < ninputs; ++j)
3241 if (DECL_P (input_tvec[j])
3242 && REG_P (DECL_RTL (input_tvec[j]))
3243 && HARD_REGISTER_P (DECL_RTL (input_tvec[j])))
3245 unsigned input_hregno = REGNO (DECL_RTL (input_tvec[j]));
3246 switch (*constraints[j + noutputs])
3248 case '0': case '1': case '2': case '3': case '4':
3249 case '5': case '6': case '7': case '8': case '9':
3250 match = strtoul (constraints[j + noutputs], NULL, 10);
3251 break;
3252 default:
3253 match = ULONG_MAX;
3254 break;
3256 if (i == match
3257 && output_hregno != input_hregno)
3259 error_at (locus, "invalid hard register usage between "
3260 "output operand and matching constraint operand");
3261 error_seen = true;
3263 else if (early_clobber_p
3264 && i != match
3265 && output_hregno == input_hregno)
3267 error_at (locus, "invalid hard register usage between "
3268 "earlyclobber operand and input operand");
3269 error_seen = true;
3274 if (! allows_reg
3275 && (allows_mem
3276 || is_inout
3277 || (DECL_P (val)
3278 && REG_P (DECL_RTL (val))
3279 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
3280 mark_addressable (val);
3283 for (i = 0; i < ninputs; ++i)
3285 bool allows_reg, allows_mem;
3286 const char *constraint;
3288 constraint = constraints[i + noutputs];
3289 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
3290 constraints.address (),
3291 &allows_mem, &allows_reg))
3292 return;
3294 if (! allows_reg && allows_mem)
3295 mark_addressable (input_tvec[i]);
3298 /* Second pass evaluates arguments. */
3300 /* Make sure stack is consistent for asm goto. */
3301 if (nlabels > 0)
3302 do_pending_stack_adjust ();
3303 int old_generating_concat_p = generating_concat_p;
3305 /* Vector of RTX's of evaluated output operands. */
3306 auto_vec<rtx, MAX_RECOG_OPERANDS> output_rvec;
3307 auto_vec<int, MAX_RECOG_OPERANDS> inout_opnum;
3308 rtx_insn *after_rtl_seq = NULL, *after_rtl_end = NULL;
3310 output_rvec.safe_grow (noutputs, true);
3312 for (i = 0; i < noutputs; ++i)
3314 tree val = output_tvec[i];
3315 tree type = TREE_TYPE (val);
3316 bool is_inout, allows_reg, allows_mem, ok;
3317 rtx op;
3319 ok = parse_output_constraint (&constraints[i], i, ninputs,
3320 noutputs, &allows_mem, &allows_reg,
3321 &is_inout);
3322 gcc_assert (ok);
3324 /* If an output operand is not a decl or indirect ref and our constraint
3325 allows a register, make a temporary to act as an intermediate.
3326 Make the asm insn write into that, then we will copy it to
3327 the real output operand. Likewise for promoted variables. */
3329 generating_concat_p = 0;
3331 gcc_assert (TREE_CODE (val) != INDIRECT_REF);
3332 if (((TREE_CODE (val) == MEM_REF
3333 && TREE_CODE (TREE_OPERAND (val, 0)) != ADDR_EXPR)
3334 && allows_mem)
3335 || (DECL_P (val)
3336 && (allows_mem || REG_P (DECL_RTL (val)))
3337 && ! (REG_P (DECL_RTL (val))
3338 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
3339 || ! allows_reg
3340 || is_inout
3341 || TREE_ADDRESSABLE (type)
3342 || (!tree_fits_poly_int64_p (TYPE_SIZE (type))
3343 && !known_size_p (max_int_size_in_bytes (type))))
3345 op = expand_expr (val, NULL_RTX, VOIDmode,
3346 !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
3347 if (MEM_P (op))
3348 op = validize_mem (op);
3350 if (! allows_reg && !MEM_P (op))
3352 error_at (locus, "output number %d not directly addressable", i);
3353 error_seen = true;
3355 if ((! allows_mem && MEM_P (op) && GET_MODE (op) != BLKmode)
3356 || GET_CODE (op) == CONCAT)
3358 rtx old_op = op;
3359 op = gen_reg_rtx (GET_MODE (op));
3361 generating_concat_p = old_generating_concat_p;
3363 if (is_inout)
3364 emit_move_insn (op, old_op);
3366 push_to_sequence2 (after_rtl_seq, after_rtl_end);
3367 emit_move_insn (old_op, op);
3368 after_rtl_seq = get_insns ();
3369 after_rtl_end = get_last_insn ();
3370 end_sequence ();
3373 else
3375 op = assign_temp (type, 0, 1);
3376 op = validize_mem (op);
3377 if (!MEM_P (op) && TREE_CODE (val) == SSA_NAME)
3378 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (val), op);
3380 generating_concat_p = old_generating_concat_p;
3382 push_to_sequence2 (after_rtl_seq, after_rtl_end);
3383 expand_assignment (val, make_tree (type, op), false);
3384 after_rtl_seq = get_insns ();
3385 after_rtl_end = get_last_insn ();
3386 end_sequence ();
3388 output_rvec[i] = op;
3390 if (is_inout)
3391 inout_opnum.safe_push (i);
3394 const char *str = gimple_asm_string (stmt);
3395 if (error_seen)
3397 ninputs = 0;
3398 noutputs = 0;
3399 inout_opnum.truncate (0);
3400 output_rvec.truncate (0);
3401 clobber_rvec.truncate (0);
3402 constraints.truncate (0);
3403 CLEAR_HARD_REG_SET (clobbered_regs);
3404 str = "";
3407 auto_vec<rtx, MAX_RECOG_OPERANDS> input_rvec;
3408 auto_vec<machine_mode, MAX_RECOG_OPERANDS> input_mode;
3410 input_rvec.safe_grow (ninputs, true);
3411 input_mode.safe_grow (ninputs, true);
3413 generating_concat_p = 0;
3415 for (i = 0; i < ninputs; ++i)
3417 tree val = input_tvec[i];
3418 tree type = TREE_TYPE (val);
3419 bool allows_reg, allows_mem, ok;
3420 const char *constraint;
3421 rtx op;
3423 constraint = constraints[i + noutputs];
3424 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
3425 constraints.address (),
3426 &allows_mem, &allows_reg);
3427 gcc_assert (ok);
3429 /* EXPAND_INITIALIZER will not generate code for valid initializer
3430 constants, but will still generate code for other types of operand.
3431 This is the behavior we want for constant constraints. */
3432 op = expand_expr (val, NULL_RTX, VOIDmode,
3433 allows_reg ? EXPAND_NORMAL
3434 : allows_mem ? EXPAND_MEMORY
3435 : EXPAND_INITIALIZER);
3437 /* Never pass a CONCAT to an ASM. */
3438 if (GET_CODE (op) == CONCAT)
3439 op = force_reg (GET_MODE (op), op);
3440 else if (MEM_P (op))
3441 op = validize_mem (op);
3443 if (asm_operand_ok (op, constraint, NULL) <= 0)
3445 if (allows_reg && TYPE_MODE (type) != BLKmode)
3446 op = force_reg (TYPE_MODE (type), op);
3447 else if (!allows_mem)
3448 warning_at (locus, 0, "%<asm%> operand %d probably does not match "
3449 "constraints", i + noutputs);
3450 else if (MEM_P (op))
3452 /* We won't recognize either volatile memory or memory
3453 with a queued address as available a memory_operand
3454 at this point. Ignore it: clearly this *is* a memory. */
3456 else
3457 gcc_unreachable ();
3459 input_rvec[i] = op;
3460 input_mode[i] = TYPE_MODE (type);
3463 /* For in-out operands, copy output rtx to input rtx. */
3464 unsigned ninout = inout_opnum.length ();
3465 for (i = 0; i < ninout; i++)
3467 int j = inout_opnum[i];
3468 rtx o = output_rvec[j];
3470 input_rvec.safe_push (o);
3471 input_mode.safe_push (GET_MODE (o));
3473 char buffer[16];
3474 sprintf (buffer, "%d", j);
3475 constraints.safe_push (ggc_strdup (buffer));
3477 ninputs += ninout;
3479 /* Sometimes we wish to automatically clobber registers across an asm.
3480 Case in point is when the i386 backend moved from cc0 to a hard reg --
3481 maintaining source-level compatibility means automatically clobbering
3482 the flags register. */
3483 rtx_insn *after_md_seq = NULL;
3484 auto_vec<rtx> use_rvec;
3485 if (targetm.md_asm_adjust)
3486 after_md_seq
3487 = targetm.md_asm_adjust (output_rvec, input_rvec, input_mode,
3488 constraints, use_rvec, clobber_rvec,
3489 clobbered_regs, locus);
3491 /* Do not allow the hook to change the output and input count,
3492 lest it mess up the operand numbering. */
3493 gcc_assert (output_rvec.length() == noutputs);
3494 gcc_assert (input_rvec.length() == ninputs);
3495 gcc_assert (constraints.length() == noutputs + ninputs);
3497 /* But it certainly can adjust the uses and clobbers. */
3498 unsigned nuses = use_rvec.length ();
3499 unsigned nclobbers = clobber_rvec.length ();
3501 /* Third pass checks for easy conflicts. */
3502 /* ??? Why are we doing this on trees instead of rtx. */
3504 bool clobber_conflict_found = 0;
3505 for (i = 0; i < noutputs; ++i)
3506 if (tree_conflicts_with_clobbers_p (output_tvec[i], &clobbered_regs, locus))
3507 clobber_conflict_found = 1;
3508 for (i = 0; i < ninputs - ninout; ++i)
3509 if (tree_conflicts_with_clobbers_p (input_tvec[i], &clobbered_regs, locus))
3510 clobber_conflict_found = 1;
3512 /* Make vectors for the expression-rtx, constraint strings,
3513 and named operands. */
3515 rtvec argvec = rtvec_alloc (ninputs);
3516 rtvec constraintvec = rtvec_alloc (ninputs);
3517 rtvec labelvec = rtvec_alloc (nlabels);
3519 rtx body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
3520 : GET_MODE (output_rvec[0])),
3521 ggc_strdup (str),
3522 "", 0, argvec, constraintvec,
3523 labelvec, locus);
3524 MEM_VOLATILE_P (body) = gimple_asm_volatile_p (stmt);
3526 for (i = 0; i < ninputs; ++i)
3528 ASM_OPERANDS_INPUT (body, i) = input_rvec[i];
3529 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
3530 = gen_rtx_ASM_INPUT_loc (input_mode[i],
3531 constraints[i + noutputs],
3532 locus);
3535 /* Copy labels to the vector. */
3536 rtx_code_label *fallthru_label = NULL;
3537 if (nlabels > 0)
3539 basic_block fallthru_bb = NULL;
3540 edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
3541 if (fallthru)
3542 fallthru_bb = fallthru->dest;
3544 for (i = 0; i < nlabels; ++i)
3546 tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
3547 rtx_insn *r;
3548 /* If asm goto has any labels in the fallthru basic block, use
3549 a label that we emit immediately after the asm goto. Expansion
3550 may insert further instructions into the same basic block after
3551 asm goto and if we don't do this, insertion of instructions on
3552 the fallthru edge might misbehave. See PR58670. */
3553 if (fallthru_bb && label_to_block (cfun, label) == fallthru_bb)
3555 if (fallthru_label == NULL_RTX)
3556 fallthru_label = gen_label_rtx ();
3557 r = fallthru_label;
3559 else
3560 r = label_rtx (label);
3561 ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
3565 /* Now, for each output, construct an rtx
3566 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
3567 ARGVEC CONSTRAINTS OPNAMES))
3568 If there is more than one, put them inside a PARALLEL. */
3570 if (noutputs == 0 && nuses == 0 && nclobbers == 0)
3572 /* No output operands: put in a raw ASM_OPERANDS rtx. */
3573 if (nlabels > 0)
3574 emit_jump_insn (body);
3575 else
3576 emit_insn (body);
3578 else if (noutputs == 1 && nuses == 0 && nclobbers == 0)
3580 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
3581 if (nlabels > 0)
3582 emit_jump_insn (gen_rtx_SET (output_rvec[0], body));
3583 else
3584 emit_insn (gen_rtx_SET (output_rvec[0], body));
3586 else
3588 rtx obody = body;
3589 int num = noutputs;
3591 if (num == 0)
3592 num = 1;
3594 body = gen_rtx_PARALLEL (VOIDmode,
3595 rtvec_alloc (num + nuses + nclobbers));
3597 /* For each output operand, store a SET. */
3598 for (i = 0; i < noutputs; ++i)
3600 rtx src, o = output_rvec[i];
3601 if (i == 0)
3603 ASM_OPERANDS_OUTPUT_CONSTRAINT (obody) = constraints[0];
3604 src = obody;
3606 else
3608 src = gen_rtx_ASM_OPERANDS (GET_MODE (o),
3609 ASM_OPERANDS_TEMPLATE (obody),
3610 constraints[i], i, argvec,
3611 constraintvec, labelvec, locus);
3612 MEM_VOLATILE_P (src) = gimple_asm_volatile_p (stmt);
3614 XVECEXP (body, 0, i) = gen_rtx_SET (o, src);
3617 /* If there are no outputs (but there are some clobbers)
3618 store the bare ASM_OPERANDS into the PARALLEL. */
3619 if (i == 0)
3620 XVECEXP (body, 0, i++) = obody;
3622 /* Add the uses specified by the target hook. No checking should
3623 be needed since this doesn't come directly from user code. */
3624 for (rtx use : use_rvec)
3625 XVECEXP (body, 0, i++) = gen_rtx_USE (VOIDmode, use);
3627 /* Store (clobber REG) for each clobbered register specified. */
3628 for (unsigned j = 0; j < nclobbers; ++j)
3630 rtx clobbered_reg = clobber_rvec[j];
3632 /* Do sanity check for overlap between clobbers and respectively
3633 input and outputs that hasn't been handled. Such overlap
3634 should have been detected and reported above. */
3635 if (!clobber_conflict_found && REG_P (clobbered_reg))
3637 /* We test the old body (obody) contents to avoid
3638 tripping over the under-construction body. */
3639 for (unsigned k = 0; k < noutputs; ++k)
3640 if (reg_overlap_mentioned_p (clobbered_reg, output_rvec[k]))
3641 internal_error ("%<asm%> clobber conflict with "
3642 "output operand");
3644 for (unsigned k = 0; k < ninputs - ninout; ++k)
3645 if (reg_overlap_mentioned_p (clobbered_reg, input_rvec[k]))
3646 internal_error ("%<asm%> clobber conflict with "
3647 "input operand");
3650 XVECEXP (body, 0, i++) = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
3653 if (nlabels > 0)
3654 emit_jump_insn (body);
3655 else
3656 emit_insn (body);
3659 generating_concat_p = old_generating_concat_p;
3661 if (fallthru_label)
3662 emit_label (fallthru_label);
3664 if (after_md_seq)
3665 emit_insn (after_md_seq);
3666 if (after_rtl_seq)
3668 if (nlabels == 0)
3669 emit_insn (after_rtl_seq);
3670 else
3672 edge e;
3673 edge_iterator ei;
3675 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
3677 start_sequence ();
3678 for (rtx_insn *curr = after_rtl_seq;
3679 curr != NULL_RTX;
3680 curr = NEXT_INSN (curr))
3681 emit_insn (copy_insn (PATTERN (curr)));
3682 rtx_insn *copy = get_insns ();
3683 end_sequence ();
3684 insert_insn_on_edge (copy, e);
3689 free_temp_slots ();
3690 crtl->has_asm_statement = 1;
3693 /* Emit code to jump to the address
3694 specified by the pointer expression EXP. */
3696 static void
3697 expand_computed_goto (tree exp)
3699 rtx x = expand_normal (exp);
3701 do_pending_stack_adjust ();
3702 emit_indirect_jump (x);
3705 /* Generate RTL code for a `goto' statement with target label LABEL.
3706 LABEL should be a LABEL_DECL tree node that was or will later be
3707 defined with `expand_label'. */
3709 static void
3710 expand_goto (tree label)
3712 if (flag_checking)
3714 /* Check for a nonlocal goto to a containing function. Should have
3715 gotten translated to __builtin_nonlocal_goto. */
3716 tree context = decl_function_context (label);
3717 gcc_assert (!context || context == current_function_decl);
3720 emit_jump (jump_target_rtx (label));
3723 /* Output a return with no value. */
3725 static void
3726 expand_null_return_1 (void)
3728 clear_pending_stack_adjust ();
3729 do_pending_stack_adjust ();
3730 emit_jump (return_label);
3733 /* Generate RTL to return from the current function, with no value.
3734 (That is, we do not do anything about returning any value.) */
3736 void
3737 expand_null_return (void)
3739 /* If this function was declared to return a value, but we
3740 didn't, clobber the return registers so that they are not
3741 propagated live to the rest of the function. */
3742 clobber_return_register ();
3744 expand_null_return_1 ();
3747 /* Generate RTL to return from the current function, with value VAL. */
3749 static void
3750 expand_value_return (rtx val)
3752 /* Copy the value to the return location unless it's already there. */
3754 tree decl = DECL_RESULT (current_function_decl);
3755 rtx return_reg = DECL_RTL (decl);
3756 if (return_reg != val)
3758 tree funtype = TREE_TYPE (current_function_decl);
3759 tree type = TREE_TYPE (decl);
3760 int unsignedp = TYPE_UNSIGNED (type);
3761 machine_mode old_mode = DECL_MODE (decl);
3762 machine_mode mode;
3763 if (DECL_BY_REFERENCE (decl))
3764 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3765 else
3766 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3768 if (mode != old_mode)
3770 /* Some ABIs require scalar floating point modes to be returned
3771 in a wider scalar integer mode. We need to explicitly
3772 reinterpret to an integer mode of the correct precision
3773 before extending to the desired result. */
3774 if (SCALAR_INT_MODE_P (mode)
3775 && SCALAR_FLOAT_MODE_P (old_mode)
3776 && known_gt (GET_MODE_SIZE (mode), GET_MODE_SIZE (old_mode)))
3777 val = convert_float_to_wider_int (mode, old_mode, val);
3778 else
3779 val = convert_modes (mode, old_mode, val, unsignedp);
3782 if (GET_CODE (return_reg) == PARALLEL)
3783 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3784 else
3785 emit_move_insn (return_reg, val);
3788 expand_null_return_1 ();
3791 /* Generate RTL to evaluate the expression RETVAL and return it
3792 from the current function. */
3794 static void
3795 expand_return (tree retval)
3797 rtx result_rtl;
3798 rtx val = 0;
3799 tree retval_rhs;
3801 /* If function wants no value, give it none. */
3802 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
3804 expand_normal (retval);
3805 expand_null_return ();
3806 return;
3809 if (retval == error_mark_node)
3811 /* Treat this like a return of no value from a function that
3812 returns a value. */
3813 expand_null_return ();
3814 return;
3816 else if ((TREE_CODE (retval) == MODIFY_EXPR
3817 || TREE_CODE (retval) == INIT_EXPR)
3818 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3819 retval_rhs = TREE_OPERAND (retval, 1);
3820 else
3821 retval_rhs = retval;
3823 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3825 /* If we are returning the RESULT_DECL, then the value has already
3826 been stored into it, so we don't have to do anything special. */
3827 if (TREE_CODE (retval_rhs) == RESULT_DECL)
3828 expand_value_return (result_rtl);
3830 /* If the result is an aggregate that is being returned in one (or more)
3831 registers, load the registers here. */
3833 else if (retval_rhs != 0
3834 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3835 && REG_P (result_rtl))
3837 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3838 if (val)
3840 /* Use the mode of the result value on the return register. */
3841 PUT_MODE (result_rtl, GET_MODE (val));
3842 expand_value_return (val);
3844 else
3845 expand_null_return ();
3847 else if (retval_rhs != 0
3848 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3849 && (REG_P (result_rtl)
3850 || (GET_CODE (result_rtl) == PARALLEL)))
3852 /* Compute the return value into a temporary (usually a pseudo reg). */
3854 = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3855 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3856 val = force_not_mem (val);
3857 expand_value_return (val);
3859 else
3861 /* No hard reg used; calculate value into hard return reg. */
3862 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3863 expand_value_return (result_rtl);
3867 /* Expand a clobber of LHS. If LHS is stored it in a multi-part
3868 register, tell the rtl optimizers that its value is no longer
3869 needed. */
3871 static void
3872 expand_clobber (tree lhs)
3874 if (DECL_P (lhs))
3876 rtx decl_rtl = DECL_RTL_IF_SET (lhs);
3877 if (decl_rtl && REG_P (decl_rtl))
3879 machine_mode decl_mode = GET_MODE (decl_rtl);
3880 if (maybe_gt (GET_MODE_SIZE (decl_mode),
3881 REGMODE_NATURAL_SIZE (decl_mode)))
3882 emit_clobber (decl_rtl);
3887 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3888 STMT that doesn't require special handling for outgoing edges. That
3889 is no tailcalls and no GIMPLE_COND. */
3891 static void
3892 expand_gimple_stmt_1 (gimple *stmt)
3894 tree op0;
3896 set_curr_insn_location (gimple_location (stmt));
3898 switch (gimple_code (stmt))
3900 case GIMPLE_GOTO:
3901 op0 = gimple_goto_dest (stmt);
3902 if (TREE_CODE (op0) == LABEL_DECL)
3903 expand_goto (op0);
3904 else
3905 expand_computed_goto (op0);
3906 break;
3907 case GIMPLE_LABEL:
3908 expand_label (gimple_label_label (as_a <glabel *> (stmt)));
3909 break;
3910 case GIMPLE_NOP:
3911 case GIMPLE_PREDICT:
3912 break;
3913 case GIMPLE_SWITCH:
3915 gswitch *swtch = as_a <gswitch *> (stmt);
3916 if (gimple_switch_num_labels (swtch) == 1)
3917 expand_goto (CASE_LABEL (gimple_switch_default_label (swtch)));
3918 else
3919 expand_case (swtch);
3921 break;
3922 case GIMPLE_ASM:
3923 expand_asm_stmt (as_a <gasm *> (stmt));
3924 break;
3925 case GIMPLE_CALL:
3926 expand_call_stmt (as_a <gcall *> (stmt));
3927 break;
3929 case GIMPLE_RETURN:
3931 op0 = gimple_return_retval (as_a <greturn *> (stmt));
3933 /* If a return doesn't have a location, it very likely represents
3934 multiple user returns so we cannot let it inherit the location
3935 of the last statement of the previous basic block in RTL. */
3936 if (!gimple_has_location (stmt))
3937 set_curr_insn_location (cfun->function_end_locus);
3939 if (op0 && op0 != error_mark_node)
3941 tree result = DECL_RESULT (current_function_decl);
3943 /* If we are not returning the current function's RESULT_DECL,
3944 build an assignment to it. */
3945 if (op0 != result)
3947 /* I believe that a function's RESULT_DECL is unique. */
3948 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3950 /* ??? We'd like to use simply expand_assignment here,
3951 but this fails if the value is of BLKmode but the return
3952 decl is a register. expand_return has special handling
3953 for this combination, which eventually should move
3954 to common code. See comments there. Until then, let's
3955 build a modify expression :-/ */
3956 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3957 result, op0);
3961 if (!op0)
3962 expand_null_return ();
3963 else
3964 expand_return (op0);
3966 break;
3968 case GIMPLE_ASSIGN:
3970 gassign *assign_stmt = as_a <gassign *> (stmt);
3971 tree lhs = gimple_assign_lhs (assign_stmt);
3973 /* Tree expand used to fiddle with |= and &= of two bitfield
3974 COMPONENT_REFs here. This can't happen with gimple, the LHS
3975 of binary assigns must be a gimple reg. */
3977 if (TREE_CODE (lhs) != SSA_NAME
3978 || gimple_assign_rhs_class (assign_stmt) == GIMPLE_SINGLE_RHS)
3980 tree rhs = gimple_assign_rhs1 (assign_stmt);
3981 gcc_assert (gimple_assign_rhs_class (assign_stmt)
3982 == GIMPLE_SINGLE_RHS);
3983 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs)
3984 /* Do not put locations on possibly shared trees. */
3985 && !is_gimple_min_invariant (rhs))
3986 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3987 if (TREE_CLOBBER_P (rhs))
3988 /* This is a clobber to mark the going out of scope for
3989 this LHS. */
3990 expand_clobber (lhs);
3991 else
3992 expand_assignment (lhs, rhs,
3993 gimple_assign_nontemporal_move_p (
3994 assign_stmt));
3996 else
3998 rtx target, temp;
3999 bool nontemporal = gimple_assign_nontemporal_move_p (assign_stmt);
4000 bool promoted = false;
4002 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
4003 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
4004 promoted = true;
4006 /* If we want to use a nontemporal store, force the value to
4007 register first. If we store into a promoted register,
4008 don't directly expand to target. */
4009 temp = nontemporal || promoted ? NULL_RTX : target;
4010 temp = expand_expr_real_gassign (assign_stmt, temp,
4011 GET_MODE (target), EXPAND_NORMAL);
4013 if (temp == target)
4015 else if (promoted)
4017 int unsignedp = SUBREG_PROMOTED_SIGN (target);
4018 /* If TEMP is a VOIDmode constant, use convert_modes to make
4019 sure that we properly convert it. */
4020 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
4022 temp = convert_modes (GET_MODE (target),
4023 TYPE_MODE (TREE_TYPE (lhs)),
4024 temp, unsignedp);
4025 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
4026 GET_MODE (target), temp, unsignedp);
4029 convert_move (SUBREG_REG (target), temp, unsignedp);
4031 else if (nontemporal && emit_storent_insn (target, temp))
4033 else
4035 temp = force_operand (temp, target);
4036 if (temp != target)
4037 emit_move_insn (target, temp);
4041 break;
4043 default:
4044 gcc_unreachable ();
4048 /* Expand one gimple statement STMT and return the last RTL instruction
4049 before any of the newly generated ones.
4051 In addition to generating the necessary RTL instructions this also
4052 sets REG_EH_REGION notes if necessary and sets the current source
4053 location for diagnostics. */
4055 static rtx_insn *
4056 expand_gimple_stmt (gimple *stmt)
4058 location_t saved_location = input_location;
4059 rtx_insn *last = get_last_insn ();
4060 int lp_nr;
4062 gcc_assert (cfun);
4064 /* We need to save and restore the current source location so that errors
4065 discovered during expansion are emitted with the right location. But
4066 it would be better if the diagnostic routines used the source location
4067 embedded in the tree nodes rather than globals. */
4068 if (gimple_has_location (stmt))
4069 input_location = gimple_location (stmt);
4071 expand_gimple_stmt_1 (stmt);
4073 /* Free any temporaries used to evaluate this statement. */
4074 free_temp_slots ();
4076 input_location = saved_location;
4078 /* Mark all insns that may trap. */
4079 lp_nr = lookup_stmt_eh_lp (stmt);
4080 if (lp_nr)
4082 rtx_insn *insn;
4083 for (insn = next_real_insn (last); insn;
4084 insn = next_real_insn (insn))
4086 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
4087 /* If we want exceptions for non-call insns, any
4088 may_trap_p instruction may throw. */
4089 && GET_CODE (PATTERN (insn)) != CLOBBER
4090 && GET_CODE (PATTERN (insn)) != USE
4091 && insn_could_throw_p (insn))
4092 make_reg_eh_region_note (insn, 0, lp_nr);
4096 return last;
4099 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
4100 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
4101 generated a tail call (something that might be denied by the ABI
4102 rules governing the call; see calls.cc).
4104 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
4105 can still reach the rest of BB. The case here is __builtin_sqrt,
4106 where the NaN result goes through the external function (with a
4107 tailcall) and the normal result happens via a sqrt instruction. */
4109 static basic_block
4110 expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
4112 rtx_insn *last2, *last;
4113 edge e;
4114 edge_iterator ei;
4115 profile_probability probability;
4117 last2 = last = expand_gimple_stmt (stmt);
4119 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
4120 if (CALL_P (last) && SIBLING_CALL_P (last))
4121 goto found;
4123 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
4125 *can_fallthru = true;
4126 return NULL;
4128 found:
4129 /* ??? Wouldn't it be better to just reset any pending stack adjust?
4130 Any instructions emitted here are about to be deleted. */
4131 do_pending_stack_adjust ();
4133 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
4134 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
4135 EH or abnormal edges, we shouldn't have created a tail call in
4136 the first place. So it seems to me we should just be removing
4137 all edges here, or redirecting the existing fallthru edge to
4138 the exit block. */
4140 probability = profile_probability::never ();
4142 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
4144 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
4146 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
4147 e->dest->count -= e->count ();
4148 probability += e->probability;
4149 remove_edge (e);
4151 else
4152 ei_next (&ei);
4155 /* This is somewhat ugly: the call_expr expander often emits instructions
4156 after the sibcall (to perform the function return). These confuse the
4157 find_many_sub_basic_blocks code, so we need to get rid of these. */
4158 last = NEXT_INSN (last);
4159 gcc_assert (BARRIER_P (last));
4161 *can_fallthru = false;
4162 while (NEXT_INSN (last))
4164 /* For instance an sqrt builtin expander expands if with
4165 sibcall in the then and label for `else`. */
4166 if (LABEL_P (NEXT_INSN (last)))
4168 *can_fallthru = true;
4169 break;
4171 delete_insn (NEXT_INSN (last));
4174 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
4175 | EDGE_SIBCALL);
4176 e->probability = probability;
4177 BB_END (bb) = last;
4178 update_bb_for_insn (bb);
4180 if (NEXT_INSN (last))
4182 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
4184 last = BB_END (bb);
4185 if (BARRIER_P (last))
4186 BB_END (bb) = PREV_INSN (last);
4189 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
4191 return bb;
4194 /* Return the difference between the floor and the truncated result of
4195 a signed division by OP1 with remainder MOD. */
4196 static rtx
4197 floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
4199 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
4200 return gen_rtx_IF_THEN_ELSE
4201 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
4202 gen_rtx_IF_THEN_ELSE
4203 (mode, gen_rtx_LT (BImode,
4204 gen_rtx_DIV (mode, op1, mod),
4205 const0_rtx),
4206 constm1_rtx, const0_rtx),
4207 const0_rtx);
4210 /* Return the difference between the ceil and the truncated result of
4211 a signed division by OP1 with remainder MOD. */
4212 static rtx
4213 ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
4215 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
4216 return gen_rtx_IF_THEN_ELSE
4217 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
4218 gen_rtx_IF_THEN_ELSE
4219 (mode, gen_rtx_GT (BImode,
4220 gen_rtx_DIV (mode, op1, mod),
4221 const0_rtx),
4222 const1_rtx, const0_rtx),
4223 const0_rtx);
4226 /* Return the difference between the ceil and the truncated result of
4227 an unsigned division by OP1 with remainder MOD. */
4228 static rtx
4229 ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
4231 /* (mod != 0 ? 1 : 0) */
4232 return gen_rtx_IF_THEN_ELSE
4233 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
4234 const1_rtx, const0_rtx);
4237 /* Return the difference between the rounded and the truncated result
4238 of a signed division by OP1 with remainder MOD. Halfway cases are
4239 rounded away from zero, rather than to the nearest even number. */
4240 static rtx
4241 round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
4243 /* (abs (mod) >= abs (op1) - abs (mod)
4244 ? (op1 / mod > 0 ? 1 : -1)
4245 : 0) */
4246 return gen_rtx_IF_THEN_ELSE
4247 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
4248 gen_rtx_MINUS (mode,
4249 gen_rtx_ABS (mode, op1),
4250 gen_rtx_ABS (mode, mod))),
4251 gen_rtx_IF_THEN_ELSE
4252 (mode, gen_rtx_GT (BImode,
4253 gen_rtx_DIV (mode, op1, mod),
4254 const0_rtx),
4255 const1_rtx, constm1_rtx),
4256 const0_rtx);
4259 /* Return the difference between the rounded and the truncated result
4260 of a unsigned division by OP1 with remainder MOD. Halfway cases
4261 are rounded away from zero, rather than to the nearest even
4262 number. */
4263 static rtx
4264 round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
4266 /* (mod >= op1 - mod ? 1 : 0) */
4267 return gen_rtx_IF_THEN_ELSE
4268 (mode, gen_rtx_GE (BImode, mod,
4269 gen_rtx_MINUS (mode, op1, mod)),
4270 const1_rtx, const0_rtx);
4273 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
4274 any rtl. */
4276 static rtx
4277 convert_debug_memory_address (scalar_int_mode mode, rtx x,
4278 addr_space_t as)
4280 #ifndef POINTERS_EXTEND_UNSIGNED
4281 gcc_assert (mode == Pmode
4282 || mode == targetm.addr_space.address_mode (as));
4283 gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
4284 #else
4285 rtx temp;
4287 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
4289 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
4290 return x;
4292 /* X must have some form of address mode already. */
4293 scalar_int_mode xmode = as_a <scalar_int_mode> (GET_MODE (x));
4294 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
4295 x = lowpart_subreg (mode, x, xmode);
4296 else if (POINTERS_EXTEND_UNSIGNED > 0)
4297 x = gen_rtx_ZERO_EXTEND (mode, x);
4298 else if (!POINTERS_EXTEND_UNSIGNED)
4299 x = gen_rtx_SIGN_EXTEND (mode, x);
4300 else
4302 switch (GET_CODE (x))
4304 case SUBREG:
4305 if ((SUBREG_PROMOTED_VAR_P (x)
4306 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
4307 || (GET_CODE (SUBREG_REG (x)) == PLUS
4308 && REG_P (XEXP (SUBREG_REG (x), 0))
4309 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
4310 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
4311 && GET_MODE (SUBREG_REG (x)) == mode)
4312 return SUBREG_REG (x);
4313 break;
4314 case LABEL_REF:
4315 temp = gen_rtx_LABEL_REF (mode, label_ref_label (x));
4316 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
4317 return temp;
4318 case SYMBOL_REF:
4319 temp = shallow_copy_rtx (x);
4320 PUT_MODE (temp, mode);
4321 return temp;
4322 case CONST:
4323 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
4324 if (temp)
4325 temp = gen_rtx_CONST (mode, temp);
4326 return temp;
4327 case PLUS:
4328 case MINUS:
4329 if (CONST_INT_P (XEXP (x, 1)))
4331 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
4332 if (temp)
4333 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
4335 break;
4336 default:
4337 break;
4339 /* Don't know how to express ptr_extend as operation in debug info. */
4340 return NULL;
4342 #endif /* POINTERS_EXTEND_UNSIGNED */
4344 return x;
4347 /* Map from SSA_NAMEs to corresponding DEBUG_EXPR_DECLs created
4348 by avoid_deep_ter_for_debug. */
4350 static hash_map<tree, tree> *deep_ter_debug_map;
4352 /* Split too deep TER chains for debug stmts using debug temporaries. */
4354 static void
4355 avoid_deep_ter_for_debug (gimple *stmt, int depth)
4357 use_operand_p use_p;
4358 ssa_op_iter iter;
4359 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
4361 tree use = USE_FROM_PTR (use_p);
4362 if (TREE_CODE (use) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (use))
4363 continue;
4364 gimple *g = get_gimple_for_ssa_name (use);
4365 if (g == NULL)
4366 continue;
4367 if (depth > 6 && !stmt_ends_bb_p (g))
4369 if (deep_ter_debug_map == NULL)
4370 deep_ter_debug_map = new hash_map<tree, tree>;
4372 tree &vexpr = deep_ter_debug_map->get_or_insert (use);
4373 if (vexpr != NULL)
4374 continue;
4375 vexpr = build_debug_expr_decl (TREE_TYPE (use));
4376 gimple *def_temp = gimple_build_debug_bind (vexpr, use, g);
4377 gimple_stmt_iterator gsi = gsi_for_stmt (g);
4378 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
4379 avoid_deep_ter_for_debug (def_temp, 0);
4381 else
4382 avoid_deep_ter_for_debug (g, depth + 1);
4386 /* Return an RTX equivalent to the value of the parameter DECL. */
4388 static rtx
4389 expand_debug_parm_decl (tree decl)
4391 rtx incoming = DECL_INCOMING_RTL (decl);
4393 if (incoming
4394 && GET_MODE (incoming) != BLKmode
4395 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
4396 || (MEM_P (incoming)
4397 && REG_P (XEXP (incoming, 0))
4398 && HARD_REGISTER_P (XEXP (incoming, 0)))))
4400 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
4402 #ifdef HAVE_window_save
4403 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
4404 If the target machine has an explicit window save instruction, the
4405 actual entry value is the corresponding OUTGOING_REGNO instead. */
4406 if (REG_P (incoming)
4407 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
4408 incoming
4409 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
4410 OUTGOING_REGNO (REGNO (incoming)), 0);
4411 else if (MEM_P (incoming))
4413 rtx reg = XEXP (incoming, 0);
4414 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
4416 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
4417 incoming = replace_equiv_address_nv (incoming, reg);
4419 else
4420 incoming = copy_rtx (incoming);
4422 #endif
4424 ENTRY_VALUE_EXP (rtl) = incoming;
4425 return rtl;
4428 if (incoming
4429 && GET_MODE (incoming) != BLKmode
4430 && !TREE_ADDRESSABLE (decl)
4431 && MEM_P (incoming)
4432 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
4433 || (GET_CODE (XEXP (incoming, 0)) == PLUS
4434 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
4435 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
4436 return copy_rtx (incoming);
4438 return NULL_RTX;
4441 /* Return an RTX equivalent to the value of the tree expression EXP. */
4443 static rtx
4444 expand_debug_expr (tree exp)
4446 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
4447 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4448 machine_mode inner_mode = VOIDmode;
4449 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
4450 addr_space_t as;
4451 scalar_int_mode op0_mode, op1_mode, addr_mode;
4453 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
4455 case tcc_expression:
4456 switch (TREE_CODE (exp))
4458 case COND_EXPR:
4459 case DOT_PROD_EXPR:
4460 case SAD_EXPR:
4461 case WIDEN_MULT_PLUS_EXPR:
4462 case WIDEN_MULT_MINUS_EXPR:
4463 goto ternary;
4465 case TRUTH_ANDIF_EXPR:
4466 case TRUTH_ORIF_EXPR:
4467 case TRUTH_AND_EXPR:
4468 case TRUTH_OR_EXPR:
4469 case TRUTH_XOR_EXPR:
4470 goto binary;
4472 case TRUTH_NOT_EXPR:
4473 goto unary;
4475 default:
4476 break;
4478 break;
4480 ternary:
4481 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
4482 if (!op2)
4483 return NULL_RTX;
4484 /* Fall through. */
4486 binary:
4487 case tcc_binary:
4488 if (mode == BLKmode)
4489 return NULL_RTX;
4490 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4491 if (!op1)
4492 return NULL_RTX;
4493 switch (TREE_CODE (exp))
4495 case LSHIFT_EXPR:
4496 case RSHIFT_EXPR:
4497 case LROTATE_EXPR:
4498 case RROTATE_EXPR:
4499 case WIDEN_LSHIFT_EXPR:
4500 /* Ensure second operand isn't wider than the first one. */
4501 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 1)));
4502 if (is_a <scalar_int_mode> (inner_mode, &op1_mode)
4503 && (GET_MODE_UNIT_PRECISION (mode)
4504 < GET_MODE_PRECISION (op1_mode)))
4505 op1 = lowpart_subreg (GET_MODE_INNER (mode), op1, op1_mode);
4506 break;
4507 default:
4508 break;
4510 /* Fall through. */
4512 unary:
4513 case tcc_unary:
4514 if (mode == BLKmode)
4515 return NULL_RTX;
4516 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4517 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4518 if (!op0)
4519 return NULL_RTX;
4520 break;
4522 case tcc_comparison:
4523 unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
4524 goto binary;
4526 case tcc_type:
4527 case tcc_statement:
4528 gcc_unreachable ();
4530 case tcc_constant:
4531 case tcc_exceptional:
4532 case tcc_declaration:
4533 case tcc_reference:
4534 case tcc_vl_exp:
4535 break;
4538 switch (TREE_CODE (exp))
4540 case STRING_CST:
4541 if (!lookup_constant_def (exp))
4543 if (strlen (TREE_STRING_POINTER (exp)) + 1
4544 != (size_t) TREE_STRING_LENGTH (exp))
4545 return NULL_RTX;
4546 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
4547 op0 = gen_rtx_MEM (BLKmode, op0);
4548 set_mem_attributes (op0, exp, 0);
4549 return op0;
4551 /* Fall through. */
4553 case INTEGER_CST:
4554 if (TREE_CODE (TREE_TYPE (exp)) == BITINT_TYPE
4555 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
4556 return NULL;
4557 /* FALLTHRU */
4558 case REAL_CST:
4559 case FIXED_CST:
4560 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
4561 return op0;
4563 case POLY_INT_CST:
4564 return immed_wide_int_const (poly_int_cst_value (exp), mode);
4566 case COMPLEX_CST:
4567 gcc_assert (COMPLEX_MODE_P (mode));
4568 op0 = expand_debug_expr (TREE_REALPART (exp));
4569 op1 = expand_debug_expr (TREE_IMAGPART (exp));
4570 return gen_rtx_CONCAT (mode, op0, op1);
4572 case DEBUG_EXPR_DECL:
4573 op0 = DECL_RTL_IF_SET (exp);
4575 if (op0)
4577 if (GET_MODE (op0) != mode)
4578 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (exp)));
4579 else
4580 return op0;
4583 op0 = gen_rtx_DEBUG_EXPR (mode);
4584 DEBUG_EXPR_TREE_DECL (op0) = exp;
4585 SET_DECL_RTL (exp, op0);
4587 return op0;
4589 case VAR_DECL:
4590 case PARM_DECL:
4591 case FUNCTION_DECL:
4592 case LABEL_DECL:
4593 case CONST_DECL:
4594 case RESULT_DECL:
4595 op0 = DECL_RTL_IF_SET (exp);
4597 /* This decl was probably optimized away. */
4598 if (!op0
4599 /* At least label RTXen are sometimes replaced by
4600 NOTE_INSN_DELETED_LABEL. Any notes here are not
4601 handled by copy_rtx. */
4602 || NOTE_P (op0))
4604 if (!VAR_P (exp)
4605 || DECL_EXTERNAL (exp)
4606 || !TREE_STATIC (exp)
4607 || !DECL_NAME (exp)
4608 || DECL_HARD_REGISTER (exp)
4609 || DECL_IN_CONSTANT_POOL (exp)
4610 || mode == VOIDmode
4611 || symtab_node::get (exp) == NULL)
4612 return NULL;
4614 op0 = make_decl_rtl_for_debug (exp);
4615 if (!MEM_P (op0)
4616 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
4617 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
4618 return NULL;
4620 else if (VAR_P (exp)
4621 && is_global_var (exp)
4622 && symtab_node::get (exp) == NULL)
4623 return NULL;
4624 else
4625 op0 = copy_rtx (op0);
4627 if (GET_MODE (op0) == BLKmode
4628 /* If op0 is not BLKmode, but mode is, adjust_mode
4629 below would ICE. While it is likely a FE bug,
4630 try to be robust here. See PR43166. */
4631 || mode == BLKmode
4632 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
4634 gcc_assert (MEM_P (op0));
4635 op0 = adjust_address_nv (op0, mode, 0);
4636 return op0;
4639 /* Fall through. */
4641 adjust_mode:
4642 case PAREN_EXPR:
4643 CASE_CONVERT:
4645 inner_mode = GET_MODE (op0);
4647 if (mode == inner_mode)
4648 return op0;
4650 if (inner_mode == VOIDmode)
4652 if (TREE_CODE (exp) == SSA_NAME)
4653 inner_mode = TYPE_MODE (TREE_TYPE (exp));
4654 else
4655 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4656 if (mode == inner_mode)
4657 return op0;
4660 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4662 if (GET_MODE_UNIT_BITSIZE (mode)
4663 == GET_MODE_UNIT_BITSIZE (inner_mode))
4664 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4665 else if (GET_MODE_UNIT_BITSIZE (mode)
4666 < GET_MODE_UNIT_BITSIZE (inner_mode))
4667 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4668 else
4669 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4671 else if (FLOAT_MODE_P (mode))
4673 gcc_assert (TREE_CODE (exp) != SSA_NAME);
4674 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4675 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
4676 else
4677 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
4679 else if (FLOAT_MODE_P (inner_mode))
4681 if (unsignedp)
4682 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4683 else
4684 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4686 else if (GET_MODE_UNIT_PRECISION (mode)
4687 == GET_MODE_UNIT_PRECISION (inner_mode))
4688 op0 = lowpart_subreg (mode, op0, inner_mode);
4689 else if (GET_MODE_UNIT_PRECISION (mode)
4690 < GET_MODE_UNIT_PRECISION (inner_mode))
4691 op0 = simplify_gen_unary (TRUNCATE, mode, op0, inner_mode);
4692 else if (UNARY_CLASS_P (exp)
4693 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
4694 : unsignedp)
4695 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4696 else
4697 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4699 return op0;
4702 case MEM_REF:
4703 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4705 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
4706 TREE_OPERAND (exp, 0),
4707 TREE_OPERAND (exp, 1));
4708 if (newexp)
4709 return expand_debug_expr (newexp);
4711 /* FALLTHROUGH */
4712 case INDIRECT_REF:
4713 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4714 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4715 if (!op0)
4716 return NULL;
4718 if (TREE_CODE (exp) == MEM_REF)
4720 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4721 || (GET_CODE (op0) == PLUS
4722 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
4723 /* (mem (debug_implicit_ptr)) might confuse aliasing.
4724 Instead just use get_inner_reference. */
4725 goto component_ref;
4727 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4728 poly_int64 offset;
4729 if (!op1 || !poly_int_rtx_p (op1, &offset))
4730 return NULL;
4732 op0 = plus_constant (inner_mode, op0, offset);
4735 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4737 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4738 op0, as);
4739 if (op0 == NULL_RTX)
4740 return NULL;
4742 op0 = gen_rtx_MEM (mode, op0);
4743 set_mem_attributes (op0, exp, 0);
4744 if (TREE_CODE (exp) == MEM_REF
4745 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4746 set_mem_expr (op0, NULL_TREE);
4747 set_mem_addr_space (op0, as);
4749 return op0;
4751 case TARGET_MEM_REF:
4752 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
4753 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
4754 return NULL;
4756 op0 = expand_debug_expr
4757 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
4758 if (!op0)
4759 return NULL;
4761 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4762 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4763 op0, as);
4764 if (op0 == NULL_RTX)
4765 return NULL;
4767 op0 = gen_rtx_MEM (mode, op0);
4769 set_mem_attributes (op0, exp, 0);
4770 set_mem_addr_space (op0, as);
4772 return op0;
4774 component_ref:
4775 case ARRAY_REF:
4776 case ARRAY_RANGE_REF:
4777 case COMPONENT_REF:
4778 case BIT_FIELD_REF:
4779 case REALPART_EXPR:
4780 case IMAGPART_EXPR:
4781 case VIEW_CONVERT_EXPR:
4783 machine_mode mode1;
4784 poly_int64 bitsize, bitpos;
4785 tree offset;
4786 int reversep, volatilep = 0;
4787 tree tem
4788 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
4789 &unsignedp, &reversep, &volatilep);
4790 rtx orig_op0;
4792 if (known_eq (bitsize, 0))
4793 return NULL;
4795 orig_op0 = op0 = expand_debug_expr (tem);
4797 if (!op0)
4798 return NULL;
4800 if (offset)
4802 machine_mode addrmode, offmode;
4804 if (!MEM_P (op0))
4805 return NULL;
4807 op0 = XEXP (op0, 0);
4808 addrmode = GET_MODE (op0);
4809 if (addrmode == VOIDmode)
4810 addrmode = Pmode;
4812 op1 = expand_debug_expr (offset);
4813 if (!op1)
4814 return NULL;
4816 offmode = GET_MODE (op1);
4817 if (offmode == VOIDmode)
4818 offmode = TYPE_MODE (TREE_TYPE (offset));
4820 if (addrmode != offmode)
4821 op1 = lowpart_subreg (addrmode, op1, offmode);
4823 /* Don't use offset_address here, we don't need a
4824 recognizable address, and we don't want to generate
4825 code. */
4826 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4827 op0, op1));
4830 if (MEM_P (op0))
4832 if (mode1 == VOIDmode)
4834 if (maybe_gt (bitsize, MAX_BITSIZE_MODE_ANY_INT))
4835 return NULL;
4836 /* Bitfield. */
4837 mode1 = smallest_int_mode_for_size (bitsize);
4839 poly_int64 bytepos = bits_to_bytes_round_down (bitpos);
4840 if (maybe_ne (bytepos, 0))
4842 op0 = adjust_address_nv (op0, mode1, bytepos);
4843 bitpos = num_trailing_bits (bitpos);
4845 else if (known_eq (bitpos, 0)
4846 && known_eq (bitsize, GET_MODE_BITSIZE (mode)))
4847 op0 = adjust_address_nv (op0, mode, 0);
4848 else if (GET_MODE (op0) != mode1)
4849 op0 = adjust_address_nv (op0, mode1, 0);
4850 else
4851 op0 = copy_rtx (op0);
4852 if (op0 == orig_op0)
4853 op0 = shallow_copy_rtx (op0);
4854 if (TREE_CODE (tem) != SSA_NAME)
4855 set_mem_attributes (op0, exp, 0);
4858 if (known_eq (bitpos, 0) && mode == GET_MODE (op0))
4859 return op0;
4861 if (maybe_lt (bitpos, 0))
4862 return NULL;
4864 if (GET_MODE (op0) == BLKmode || mode == BLKmode)
4865 return NULL;
4867 poly_int64 bytepos;
4868 if (multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
4869 && known_eq (bitsize, GET_MODE_BITSIZE (mode1)))
4871 machine_mode opmode = GET_MODE (op0);
4873 if (opmode == VOIDmode)
4874 opmode = TYPE_MODE (TREE_TYPE (tem));
4876 /* This condition may hold if we're expanding the address
4877 right past the end of an array that turned out not to
4878 be addressable (i.e., the address was only computed in
4879 debug stmts). The gen_subreg below would rightfully
4880 crash, and the address doesn't really exist, so just
4881 drop it. */
4882 if (known_ge (bitpos, GET_MODE_BITSIZE (opmode)))
4883 return NULL;
4885 if (multiple_p (bitpos, GET_MODE_BITSIZE (mode)))
4886 return simplify_gen_subreg (mode, op0, opmode, bytepos);
4889 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4890 && TYPE_UNSIGNED (TREE_TYPE (exp))
4891 ? SIGN_EXTRACT
4892 : ZERO_EXTRACT, mode,
4893 GET_MODE (op0) != VOIDmode
4894 ? GET_MODE (op0)
4895 : TYPE_MODE (TREE_TYPE (tem)),
4896 op0, gen_int_mode (bitsize, word_mode),
4897 gen_int_mode (bitpos, word_mode));
4900 case ABS_EXPR:
4901 case ABSU_EXPR:
4902 return simplify_gen_unary (ABS, mode, op0, mode);
4904 case NEGATE_EXPR:
4905 return simplify_gen_unary (NEG, mode, op0, mode);
4907 case BIT_NOT_EXPR:
4908 return simplify_gen_unary (NOT, mode, op0, mode);
4910 case FLOAT_EXPR:
4911 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4912 0)))
4913 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4914 inner_mode);
4916 case FIX_TRUNC_EXPR:
4917 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4918 inner_mode);
4920 case POINTER_PLUS_EXPR:
4921 /* For the rare target where pointers are not the same size as
4922 size_t, we need to check for mis-matched modes and correct
4923 the addend. */
4924 if (op0 && op1
4925 && is_a <scalar_int_mode> (GET_MODE (op0), &op0_mode)
4926 && is_a <scalar_int_mode> (GET_MODE (op1), &op1_mode)
4927 && op0_mode != op1_mode)
4929 if (GET_MODE_BITSIZE (op0_mode) < GET_MODE_BITSIZE (op1_mode)
4930 /* If OP0 is a partial mode, then we must truncate, even
4931 if it has the same bitsize as OP1 as GCC's
4932 representation of partial modes is opaque. */
4933 || (GET_MODE_CLASS (op0_mode) == MODE_PARTIAL_INT
4934 && (GET_MODE_BITSIZE (op0_mode)
4935 == GET_MODE_BITSIZE (op1_mode))))
4936 op1 = simplify_gen_unary (TRUNCATE, op0_mode, op1, op1_mode);
4937 else
4938 /* We always sign-extend, regardless of the signedness of
4939 the operand, because the operand is always unsigned
4940 here even if the original C expression is signed. */
4941 op1 = simplify_gen_unary (SIGN_EXTEND, op0_mode, op1, op1_mode);
4943 /* Fall through. */
4944 case PLUS_EXPR:
4945 return simplify_gen_binary (PLUS, mode, op0, op1);
4947 case MINUS_EXPR:
4948 case POINTER_DIFF_EXPR:
4949 return simplify_gen_binary (MINUS, mode, op0, op1);
4951 case MULT_EXPR:
4952 return simplify_gen_binary (MULT, mode, op0, op1);
4954 case RDIV_EXPR:
4955 case TRUNC_DIV_EXPR:
4956 case EXACT_DIV_EXPR:
4957 if (unsignedp)
4958 return simplify_gen_binary (UDIV, mode, op0, op1);
4959 else
4960 return simplify_gen_binary (DIV, mode, op0, op1);
4962 case TRUNC_MOD_EXPR:
4963 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4965 case FLOOR_DIV_EXPR:
4966 if (unsignedp)
4967 return simplify_gen_binary (UDIV, mode, op0, op1);
4968 else
4970 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4971 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4972 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4973 return simplify_gen_binary (PLUS, mode, div, adj);
4976 case FLOOR_MOD_EXPR:
4977 if (unsignedp)
4978 return simplify_gen_binary (UMOD, mode, op0, op1);
4979 else
4981 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4982 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4983 adj = simplify_gen_unary (NEG, mode,
4984 simplify_gen_binary (MULT, mode, adj, op1),
4985 mode);
4986 return simplify_gen_binary (PLUS, mode, mod, adj);
4989 case CEIL_DIV_EXPR:
4990 if (unsignedp)
4992 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4993 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4994 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4995 return simplify_gen_binary (PLUS, mode, div, adj);
4997 else
4999 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
5000 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5001 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
5002 return simplify_gen_binary (PLUS, mode, div, adj);
5005 case CEIL_MOD_EXPR:
5006 if (unsignedp)
5008 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5009 rtx adj = ceil_udiv_adjust (mode, mod, op1);
5010 adj = simplify_gen_unary (NEG, mode,
5011 simplify_gen_binary (MULT, mode, adj, op1),
5012 mode);
5013 return simplify_gen_binary (PLUS, mode, mod, adj);
5015 else
5017 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5018 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
5019 adj = simplify_gen_unary (NEG, mode,
5020 simplify_gen_binary (MULT, mode, adj, op1),
5021 mode);
5022 return simplify_gen_binary (PLUS, mode, mod, adj);
5025 case ROUND_DIV_EXPR:
5026 if (unsignedp)
5028 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
5029 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5030 rtx adj = round_udiv_adjust (mode, mod, op1);
5031 return simplify_gen_binary (PLUS, mode, div, adj);
5033 else
5035 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
5036 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5037 rtx adj = round_sdiv_adjust (mode, mod, op1);
5038 return simplify_gen_binary (PLUS, mode, div, adj);
5041 case ROUND_MOD_EXPR:
5042 if (unsignedp)
5044 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5045 rtx adj = round_udiv_adjust (mode, mod, op1);
5046 adj = simplify_gen_unary (NEG, mode,
5047 simplify_gen_binary (MULT, mode, adj, op1),
5048 mode);
5049 return simplify_gen_binary (PLUS, mode, mod, adj);
5051 else
5053 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5054 rtx adj = round_sdiv_adjust (mode, mod, op1);
5055 adj = simplify_gen_unary (NEG, mode,
5056 simplify_gen_binary (MULT, mode, adj, op1),
5057 mode);
5058 return simplify_gen_binary (PLUS, mode, mod, adj);
5061 case LSHIFT_EXPR:
5062 return simplify_gen_binary (ASHIFT, mode, op0, op1);
5064 case RSHIFT_EXPR:
5065 if (unsignedp)
5066 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
5067 else
5068 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
5070 case LROTATE_EXPR:
5071 return simplify_gen_binary (ROTATE, mode, op0, op1);
5073 case RROTATE_EXPR:
5074 return simplify_gen_binary (ROTATERT, mode, op0, op1);
5076 case MIN_EXPR:
5077 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
5079 case MAX_EXPR:
5080 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
5082 case BIT_AND_EXPR:
5083 case TRUTH_AND_EXPR:
5084 return simplify_gen_binary (AND, mode, op0, op1);
5086 case BIT_IOR_EXPR:
5087 case TRUTH_OR_EXPR:
5088 return simplify_gen_binary (IOR, mode, op0, op1);
5090 case BIT_XOR_EXPR:
5091 case TRUTH_XOR_EXPR:
5092 return simplify_gen_binary (XOR, mode, op0, op1);
5094 case TRUTH_ANDIF_EXPR:
5095 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
5097 case TRUTH_ORIF_EXPR:
5098 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
5100 case TRUTH_NOT_EXPR:
5101 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
5103 case LT_EXPR:
5104 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
5105 op0, op1);
5107 case LE_EXPR:
5108 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
5109 op0, op1);
5111 case GT_EXPR:
5112 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
5113 op0, op1);
5115 case GE_EXPR:
5116 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
5117 op0, op1);
5119 case EQ_EXPR:
5120 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
5122 case NE_EXPR:
5123 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
5125 case UNORDERED_EXPR:
5126 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
5128 case ORDERED_EXPR:
5129 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
5131 case UNLT_EXPR:
5132 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
5134 case UNLE_EXPR:
5135 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
5137 case UNGT_EXPR:
5138 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
5140 case UNGE_EXPR:
5141 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
5143 case UNEQ_EXPR:
5144 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
5146 case LTGT_EXPR:
5147 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
5149 case COND_EXPR:
5150 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
5152 case COMPLEX_EXPR:
5153 gcc_assert (COMPLEX_MODE_P (mode));
5154 if (GET_MODE (op0) == VOIDmode)
5155 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
5156 if (GET_MODE (op1) == VOIDmode)
5157 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
5158 return gen_rtx_CONCAT (mode, op0, op1);
5160 case CONJ_EXPR:
5161 if (GET_CODE (op0) == CONCAT)
5162 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
5163 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
5164 XEXP (op0, 1),
5165 GET_MODE_INNER (mode)));
5166 else
5168 scalar_mode imode = GET_MODE_INNER (mode);
5169 rtx re, im;
5171 if (MEM_P (op0))
5173 re = adjust_address_nv (op0, imode, 0);
5174 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
5176 else
5178 scalar_int_mode ifmode;
5179 scalar_int_mode ihmode;
5180 rtx halfsize;
5181 if (!int_mode_for_mode (mode).exists (&ifmode)
5182 || !int_mode_for_mode (imode).exists (&ihmode))
5183 return NULL;
5184 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
5185 re = op0;
5186 if (mode != ifmode)
5187 re = gen_rtx_SUBREG (ifmode, re, 0);
5188 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
5189 if (imode != ihmode)
5190 re = gen_rtx_SUBREG (imode, re, 0);
5191 im = copy_rtx (op0);
5192 if (mode != ifmode)
5193 im = gen_rtx_SUBREG (ifmode, im, 0);
5194 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
5195 if (imode != ihmode)
5196 im = gen_rtx_SUBREG (imode, im, 0);
5198 im = gen_rtx_NEG (imode, im);
5199 return gen_rtx_CONCAT (mode, re, im);
5202 case ADDR_EXPR:
5203 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
5204 if (!op0 || !MEM_P (op0))
5206 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
5207 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
5208 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
5209 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
5210 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
5211 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
5213 if (handled_component_p (TREE_OPERAND (exp, 0)))
5215 poly_int64 bitoffset, bitsize, maxsize, byteoffset;
5216 bool reverse;
5217 tree decl
5218 = get_ref_base_and_extent (TREE_OPERAND (exp, 0), &bitoffset,
5219 &bitsize, &maxsize, &reverse);
5220 if ((VAR_P (decl)
5221 || TREE_CODE (decl) == PARM_DECL
5222 || TREE_CODE (decl) == RESULT_DECL)
5223 && (!TREE_ADDRESSABLE (decl)
5224 || target_for_debug_bind (decl))
5225 && multiple_p (bitoffset, BITS_PER_UNIT, &byteoffset)
5226 && known_gt (bitsize, 0)
5227 && known_eq (bitsize, maxsize))
5229 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
5230 return plus_constant (mode, base, byteoffset);
5234 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
5235 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5236 == ADDR_EXPR)
5238 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
5239 0));
5240 if (op0 != NULL
5241 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
5242 || (GET_CODE (op0) == PLUS
5243 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
5244 && CONST_INT_P (XEXP (op0, 1)))))
5246 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
5247 1));
5248 poly_int64 offset;
5249 if (!op1 || !poly_int_rtx_p (op1, &offset))
5250 return NULL;
5252 return plus_constant (mode, op0, offset);
5256 return NULL;
5259 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
5260 addr_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
5261 op0 = convert_debug_memory_address (addr_mode, XEXP (op0, 0), as);
5263 return op0;
5265 case VECTOR_CST:
5267 unsigned HOST_WIDE_INT i, nelts;
5269 if (!VECTOR_CST_NELTS (exp).is_constant (&nelts))
5270 return NULL;
5272 op0 = gen_rtx_CONCATN (mode, rtvec_alloc (nelts));
5274 for (i = 0; i < nelts; ++i)
5276 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
5277 if (!op1)
5278 return NULL;
5279 XVECEXP (op0, 0, i) = op1;
5282 return op0;
5285 case CONSTRUCTOR:
5286 if (TREE_CLOBBER_P (exp))
5287 return NULL;
5288 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
5290 unsigned i;
5291 unsigned HOST_WIDE_INT nelts;
5292 tree val;
5294 if (!TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)).is_constant (&nelts))
5295 goto flag_unsupported;
5297 op0 = gen_rtx_CONCATN (mode, rtvec_alloc (nelts));
5299 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
5301 op1 = expand_debug_expr (val);
5302 if (!op1)
5303 return NULL;
5304 XVECEXP (op0, 0, i) = op1;
5307 if (i < nelts)
5309 op1 = expand_debug_expr
5310 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
5312 if (!op1)
5313 return NULL;
5315 for (; i < nelts; i++)
5316 XVECEXP (op0, 0, i) = op1;
5319 return op0;
5321 else
5322 goto flag_unsupported;
5324 case CALL_EXPR:
5325 /* ??? Maybe handle some builtins? */
5326 return NULL;
5328 case SSA_NAME:
5330 gimple *g = get_gimple_for_ssa_name (exp);
5331 if (g)
5333 tree t = NULL_TREE;
5334 if (deep_ter_debug_map)
5336 tree *slot = deep_ter_debug_map->get (exp);
5337 if (slot)
5338 t = *slot;
5340 if (t == NULL_TREE)
5341 t = gimple_assign_rhs_to_tree (g);
5342 op0 = expand_debug_expr (t);
5343 if (!op0)
5344 return NULL;
5346 else
5348 /* If this is a reference to an incoming value of
5349 parameter that is never used in the code or where the
5350 incoming value is never used in the code, use
5351 PARM_DECL's DECL_RTL if set. */
5352 if (SSA_NAME_IS_DEFAULT_DEF (exp)
5353 && SSA_NAME_VAR (exp)
5354 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL
5355 && has_zero_uses (exp))
5357 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
5358 if (op0)
5359 goto adjust_mode;
5360 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
5361 if (op0)
5362 goto adjust_mode;
5365 int part = var_to_partition (SA.map, exp);
5367 if (part == NO_PARTITION)
5368 return NULL;
5370 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
5372 op0 = copy_rtx (SA.partition_to_pseudo[part]);
5374 goto adjust_mode;
5377 case ERROR_MARK:
5378 return NULL;
5380 /* Vector stuff. For most of the codes we don't have rtl codes. */
5381 case REALIGN_LOAD_EXPR:
5382 case VEC_COND_EXPR:
5383 case VEC_PACK_FIX_TRUNC_EXPR:
5384 case VEC_PACK_FLOAT_EXPR:
5385 case VEC_PACK_SAT_EXPR:
5386 case VEC_PACK_TRUNC_EXPR:
5387 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
5388 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
5389 case VEC_UNPACK_FLOAT_HI_EXPR:
5390 case VEC_UNPACK_FLOAT_LO_EXPR:
5391 case VEC_UNPACK_HI_EXPR:
5392 case VEC_UNPACK_LO_EXPR:
5393 case VEC_WIDEN_MULT_HI_EXPR:
5394 case VEC_WIDEN_MULT_LO_EXPR:
5395 case VEC_WIDEN_MULT_EVEN_EXPR:
5396 case VEC_WIDEN_MULT_ODD_EXPR:
5397 case VEC_WIDEN_LSHIFT_HI_EXPR:
5398 case VEC_WIDEN_LSHIFT_LO_EXPR:
5399 case VEC_PERM_EXPR:
5400 case VEC_DUPLICATE_EXPR:
5401 case VEC_SERIES_EXPR:
5402 case SAD_EXPR:
5403 return NULL;
5405 /* Misc codes. */
5406 case ADDR_SPACE_CONVERT_EXPR:
5407 case FIXED_CONVERT_EXPR:
5408 case OBJ_TYPE_REF:
5409 case WITH_SIZE_EXPR:
5410 case BIT_INSERT_EXPR:
5411 return NULL;
5413 case DOT_PROD_EXPR:
5414 if (SCALAR_INT_MODE_P (GET_MODE (op0))
5415 && SCALAR_INT_MODE_P (mode))
5418 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5419 0)))
5420 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
5421 inner_mode);
5423 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5424 1)))
5425 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
5426 inner_mode);
5427 op0 = simplify_gen_binary (MULT, mode, op0, op1);
5428 return simplify_gen_binary (PLUS, mode, op0, op2);
5430 return NULL;
5432 case WIDEN_MULT_EXPR:
5433 case WIDEN_MULT_PLUS_EXPR:
5434 case WIDEN_MULT_MINUS_EXPR:
5435 if (SCALAR_INT_MODE_P (GET_MODE (op0))
5436 && SCALAR_INT_MODE_P (mode))
5438 inner_mode = GET_MODE (op0);
5439 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
5440 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
5441 else
5442 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
5443 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
5444 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
5445 else
5446 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
5447 op0 = simplify_gen_binary (MULT, mode, op0, op1);
5448 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
5449 return op0;
5450 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
5451 return simplify_gen_binary (PLUS, mode, op0, op2);
5452 else
5453 return simplify_gen_binary (MINUS, mode, op2, op0);
5455 return NULL;
5457 case MULT_HIGHPART_EXPR:
5458 /* ??? Similar to the above. */
5459 return NULL;
5461 case WIDEN_SUM_EXPR:
5462 case WIDEN_LSHIFT_EXPR:
5463 if (SCALAR_INT_MODE_P (GET_MODE (op0))
5464 && SCALAR_INT_MODE_P (mode))
5467 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5468 0)))
5469 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
5470 inner_mode);
5471 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
5472 ? ASHIFT : PLUS, mode, op0, op1);
5474 return NULL;
5476 default:
5477 flag_unsupported:
5478 if (flag_checking)
5480 debug_tree (exp);
5481 gcc_unreachable ();
5483 return NULL;
5487 /* Return an RTX equivalent to the source bind value of the tree expression
5488 EXP. */
5490 static rtx
5491 expand_debug_source_expr (tree exp)
5493 rtx op0 = NULL_RTX;
5494 machine_mode mode = VOIDmode, inner_mode;
5496 switch (TREE_CODE (exp))
5498 case VAR_DECL:
5499 if (DECL_ABSTRACT_ORIGIN (exp))
5500 return expand_debug_source_expr (DECL_ABSTRACT_ORIGIN (exp));
5501 break;
5502 case PARM_DECL:
5504 mode = DECL_MODE (exp);
5505 op0 = expand_debug_parm_decl (exp);
5506 if (op0)
5507 break;
5508 /* See if this isn't an argument that has been completely
5509 optimized out. */
5510 if (!DECL_RTL_SET_P (exp)
5511 && !DECL_INCOMING_RTL (exp)
5512 && DECL_ABSTRACT_ORIGIN (current_function_decl))
5514 tree aexp = DECL_ORIGIN (exp);
5515 if (DECL_CONTEXT (aexp)
5516 == DECL_ABSTRACT_ORIGIN (current_function_decl))
5518 vec<tree, va_gc> **debug_args;
5519 unsigned int ix;
5520 tree ddecl;
5521 debug_args = decl_debug_args_lookup (current_function_decl);
5522 if (debug_args != NULL)
5524 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
5525 ix += 2)
5526 if (ddecl == aexp)
5527 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
5531 break;
5533 default:
5534 break;
5537 if (op0 == NULL_RTX)
5538 return NULL_RTX;
5540 inner_mode = GET_MODE (op0);
5541 if (mode == inner_mode)
5542 return op0;
5544 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
5546 if (GET_MODE_UNIT_BITSIZE (mode)
5547 == GET_MODE_UNIT_BITSIZE (inner_mode))
5548 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
5549 else if (GET_MODE_UNIT_BITSIZE (mode)
5550 < GET_MODE_UNIT_BITSIZE (inner_mode))
5551 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
5552 else
5553 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
5555 else if (FLOAT_MODE_P (mode))
5556 gcc_unreachable ();
5557 else if (FLOAT_MODE_P (inner_mode))
5559 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
5560 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
5561 else
5562 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
5564 else if (GET_MODE_UNIT_PRECISION (mode)
5565 == GET_MODE_UNIT_PRECISION (inner_mode))
5566 op0 = lowpart_subreg (mode, op0, inner_mode);
5567 else if (GET_MODE_UNIT_PRECISION (mode)
5568 < GET_MODE_UNIT_PRECISION (inner_mode))
5569 op0 = simplify_gen_unary (TRUNCATE, mode, op0, inner_mode);
5570 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
5571 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
5572 else
5573 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
5575 return op0;
5578 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
5579 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
5580 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
5582 static void
5583 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
5585 rtx exp = *exp_p;
5587 if (exp == NULL_RTX)
5588 return;
5590 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
5591 return;
5593 if (depth == 4)
5595 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
5596 rtx dval = make_debug_expr_from_rtl (exp);
5598 /* Emit a debug bind insn before INSN. */
5599 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
5600 DEBUG_EXPR_TREE_DECL (dval), exp,
5601 VAR_INIT_STATUS_INITIALIZED);
5603 emit_debug_insn_before (bind, insn);
5604 *exp_p = dval;
5605 return;
5608 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
5609 int i, j;
5610 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
5611 switch (*format_ptr++)
5613 case 'e':
5614 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
5615 break;
5617 case 'E':
5618 case 'V':
5619 for (j = 0; j < XVECLEN (exp, i); j++)
5620 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
5621 break;
5623 default:
5624 break;
5628 /* Expand the _LOCs in debug insns. We run this after expanding all
5629 regular insns, so that any variables referenced in the function
5630 will have their DECL_RTLs set. */
5632 static void
5633 expand_debug_locations (void)
5635 rtx_insn *insn;
5636 rtx_insn *last = get_last_insn ();
5637 int save_strict_alias = flag_strict_aliasing;
5639 /* New alias sets while setting up memory attributes cause
5640 -fcompare-debug failures, even though it doesn't bring about any
5641 codegen changes. */
5642 flag_strict_aliasing = 0;
5644 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5645 if (DEBUG_BIND_INSN_P (insn))
5647 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
5648 rtx val;
5649 rtx_insn *prev_insn, *insn2;
5650 machine_mode mode;
5652 if (value == NULL_TREE)
5653 val = NULL_RTX;
5654 else
5656 if (INSN_VAR_LOCATION_STATUS (insn)
5657 == VAR_INIT_STATUS_UNINITIALIZED)
5658 val = expand_debug_source_expr (value);
5659 /* The avoid_deep_ter_for_debug function inserts
5660 debug bind stmts after SSA_NAME definition, with the
5661 SSA_NAME as the whole bind location. Disable temporarily
5662 expansion of that SSA_NAME into the DEBUG_EXPR_DECL
5663 being defined in this DEBUG_INSN. */
5664 else if (deep_ter_debug_map && TREE_CODE (value) == SSA_NAME)
5666 tree *slot = deep_ter_debug_map->get (value);
5667 if (slot)
5669 if (*slot == INSN_VAR_LOCATION_DECL (insn))
5670 *slot = NULL_TREE;
5671 else
5672 slot = NULL;
5674 val = expand_debug_expr (value);
5675 if (slot)
5676 *slot = INSN_VAR_LOCATION_DECL (insn);
5678 else
5679 val = expand_debug_expr (value);
5680 gcc_assert (last == get_last_insn ());
5683 if (!val)
5684 val = gen_rtx_UNKNOWN_VAR_LOC ();
5685 else
5687 mode = GET_MODE (INSN_VAR_LOCATION (insn));
5689 gcc_assert (mode == GET_MODE (val)
5690 || (GET_MODE (val) == VOIDmode
5691 && (CONST_SCALAR_INT_P (val)
5692 || GET_CODE (val) == CONST_FIXED
5693 || GET_CODE (val) == LABEL_REF)));
5696 INSN_VAR_LOCATION_LOC (insn) = val;
5697 prev_insn = PREV_INSN (insn);
5698 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
5699 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
5702 flag_strict_aliasing = save_strict_alias;
5705 /* Performs swapping operands of commutative operations to expand
5706 the expensive one first. */
5708 static void
5709 reorder_operands (basic_block bb)
5711 unsigned int *lattice; /* Hold cost of each statement. */
5712 unsigned int i = 0, n = 0;
5713 gimple_stmt_iterator gsi;
5714 gimple_seq stmts;
5715 gimple *stmt;
5716 bool swap;
5717 tree op0, op1;
5718 ssa_op_iter iter;
5719 use_operand_p use_p;
5720 gimple *def0, *def1;
5722 /* Compute cost of each statement using estimate_num_insns. */
5723 stmts = bb_seq (bb);
5724 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5726 stmt = gsi_stmt (gsi);
5727 if (!is_gimple_debug (stmt))
5728 gimple_set_uid (stmt, n++);
5730 lattice = XNEWVEC (unsigned int, n);
5731 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5733 unsigned cost;
5734 stmt = gsi_stmt (gsi);
5735 if (is_gimple_debug (stmt))
5736 continue;
5737 cost = estimate_num_insns (stmt, &eni_size_weights);
5738 lattice[i] = cost;
5739 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
5741 tree use = USE_FROM_PTR (use_p);
5742 gimple *def_stmt;
5743 if (TREE_CODE (use) != SSA_NAME)
5744 continue;
5745 def_stmt = get_gimple_for_ssa_name (use);
5746 if (!def_stmt)
5747 continue;
5748 lattice[i] += lattice[gimple_uid (def_stmt)];
5750 i++;
5751 if (!is_gimple_assign (stmt)
5752 || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
5753 continue;
5754 op0 = gimple_op (stmt, 1);
5755 op1 = gimple_op (stmt, 2);
5756 if (TREE_CODE (op0) != SSA_NAME
5757 || TREE_CODE (op1) != SSA_NAME)
5758 continue;
5759 /* Swap operands if the second one is more expensive. */
5760 def0 = get_gimple_for_ssa_name (op0);
5761 def1 = get_gimple_for_ssa_name (op1);
5762 if (!def1)
5763 continue;
5764 swap = false;
5765 if (!def0 || lattice[gimple_uid (def1)] > lattice[gimple_uid (def0)])
5766 swap = true;
5767 if (swap)
5769 if (dump_file && (dump_flags & TDF_DETAILS))
5771 fprintf (dump_file, "Swap operands in stmt:\n");
5772 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5773 fprintf (dump_file, "Cost left opnd=%d, right opnd=%d\n",
5774 def0 ? lattice[gimple_uid (def0)] : 0,
5775 lattice[gimple_uid (def1)]);
5777 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
5778 gimple_assign_rhs2_ptr (stmt));
5781 XDELETE (lattice);
5784 /* Expand basic block BB from GIMPLE trees to RTL. */
5786 static basic_block
5787 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
5789 gimple_stmt_iterator gsi;
5790 gimple_seq stmts;
5791 gimple *stmt = NULL;
5792 rtx_note *note = NULL;
5793 rtx_insn *last;
5794 edge e;
5795 edge_iterator ei;
5796 bool nondebug_stmt_seen = false;
5798 if (dump_file)
5799 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
5800 bb->index);
5802 /* Note that since we are now transitioning from GIMPLE to RTL, we
5803 cannot use the gsi_*_bb() routines because they expect the basic
5804 block to be in GIMPLE, instead of RTL. Therefore, we need to
5805 access the BB sequence directly. */
5806 if (optimize)
5807 reorder_operands (bb);
5808 stmts = bb_seq (bb);
5809 bb->il.gimple.seq = NULL;
5810 bb->il.gimple.phi_nodes = NULL;
5811 rtl_profile_for_bb (bb);
5812 init_rtl_bb_info (bb);
5813 bb->flags |= BB_RTL;
5815 /* Remove the RETURN_EXPR if we may fall though to the exit
5816 instead. */
5817 gsi = gsi_last (stmts);
5818 if (!gsi_end_p (gsi)
5819 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
5821 greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
5823 gcc_assert (single_succ_p (bb));
5824 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
5826 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5827 && !gimple_return_retval (ret_stmt))
5829 gsi_remove (&gsi, false);
5830 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
5834 gsi = gsi_start (stmts);
5835 if (!gsi_end_p (gsi))
5837 stmt = gsi_stmt (gsi);
5838 if (gimple_code (stmt) != GIMPLE_LABEL)
5839 stmt = NULL;
5842 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
5844 if (stmt || elt)
5846 gcc_checking_assert (!note);
5847 last = get_last_insn ();
5849 if (stmt)
5851 expand_gimple_stmt (stmt);
5852 gsi_next (&gsi);
5855 if (elt)
5856 emit_label (*elt);
5858 BB_HEAD (bb) = NEXT_INSN (last);
5859 if (NOTE_P (BB_HEAD (bb)))
5860 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
5861 gcc_assert (LABEL_P (BB_HEAD (bb)));
5862 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
5864 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5866 else
5867 BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
5869 if (note)
5870 NOTE_BASIC_BLOCK (note) = bb;
5872 for (; !gsi_end_p (gsi); gsi_next (&gsi))
5874 basic_block new_bb;
5876 stmt = gsi_stmt (gsi);
5877 if (!is_gimple_debug (stmt))
5878 nondebug_stmt_seen = true;
5880 /* If this statement is a non-debug one, and we generate debug
5881 insns, then this one might be the last real use of a TERed
5882 SSA_NAME, but where there are still some debug uses further
5883 down. Expanding the current SSA name in such further debug
5884 uses by their RHS might lead to wrong debug info, as coalescing
5885 might make the operands of such RHS be placed into the same
5886 pseudo as something else. Like so:
5887 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
5888 use(a_1);
5889 a_2 = ...
5890 #DEBUG ... => a_1
5891 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
5892 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
5893 the write to a_2 would actually have clobbered the place which
5894 formerly held a_0.
5896 So, instead of that, we recognize the situation, and generate
5897 debug temporaries at the last real use of TERed SSA names:
5898 a_1 = a_0 + 1;
5899 #DEBUG #D1 => a_1
5900 use(a_1);
5901 a_2 = ...
5902 #DEBUG ... => #D1
5904 if (MAY_HAVE_DEBUG_BIND_INSNS
5905 && SA.values
5906 && !is_gimple_debug (stmt))
5908 ssa_op_iter iter;
5909 tree op;
5910 gimple *def;
5912 location_t sloc = curr_insn_location ();
5914 /* Look for SSA names that have their last use here (TERed
5915 names always have only one real use). */
5916 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5917 if ((def = get_gimple_for_ssa_name (op)))
5919 imm_use_iterator imm_iter;
5920 use_operand_p use_p;
5921 bool have_debug_uses = false;
5923 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5925 if (gimple_debug_bind_p (USE_STMT (use_p)))
5927 have_debug_uses = true;
5928 break;
5932 if (have_debug_uses)
5934 /* OP is a TERed SSA name, with DEF its defining
5935 statement, and where OP is used in further debug
5936 instructions. Generate a debug temporary, and
5937 replace all uses of OP in debug insns with that
5938 temporary. */
5939 gimple *debugstmt;
5940 tree value = gimple_assign_rhs_to_tree (def);
5941 tree vexpr = build_debug_expr_decl (TREE_TYPE (value));
5942 rtx val;
5943 machine_mode mode;
5945 set_curr_insn_location (gimple_location (def));
5947 if (DECL_P (value))
5948 mode = DECL_MODE (value);
5949 else
5950 mode = TYPE_MODE (TREE_TYPE (value));
5951 /* FIXME: Is setting the mode really necessary? */
5952 SET_DECL_MODE (vexpr, mode);
5954 val = gen_rtx_VAR_LOCATION
5955 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5957 emit_debug_insn (val);
5959 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5961 if (!gimple_debug_bind_p (debugstmt))
5962 continue;
5964 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5965 SET_USE (use_p, vexpr);
5967 update_stmt (debugstmt);
5971 set_curr_insn_location (sloc);
5974 currently_expanding_gimple_stmt = stmt;
5976 /* Expand this statement, then evaluate the resulting RTL and
5977 fixup the CFG accordingly. */
5978 if (gimple_code (stmt) == GIMPLE_COND)
5980 new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
5981 if (new_bb)
5983 currently_expanding_gimple_stmt = NULL;
5984 return new_bb;
5987 else if (is_gimple_debug (stmt))
5989 location_t sloc = curr_insn_location ();
5990 gimple_stmt_iterator nsi = gsi;
5992 for (;;)
5994 tree var;
5995 tree value = NULL_TREE;
5996 rtx val = NULL_RTX;
5997 machine_mode mode;
5999 if (!gimple_debug_nonbind_marker_p (stmt))
6001 if (gimple_debug_bind_p (stmt))
6003 var = gimple_debug_bind_get_var (stmt);
6005 if (TREE_CODE (var) != DEBUG_EXPR_DECL
6006 && TREE_CODE (var) != LABEL_DECL
6007 && !target_for_debug_bind (var))
6008 goto delink_debug_stmt;
6010 if (DECL_P (var) && !VECTOR_TYPE_P (TREE_TYPE (var)))
6011 mode = DECL_MODE (var);
6012 else
6013 mode = TYPE_MODE (TREE_TYPE (var));
6015 if (gimple_debug_bind_has_value_p (stmt))
6016 value = gimple_debug_bind_get_value (stmt);
6018 val = gen_rtx_VAR_LOCATION
6019 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
6021 else if (gimple_debug_source_bind_p (stmt))
6023 var = gimple_debug_source_bind_get_var (stmt);
6025 value = gimple_debug_source_bind_get_value (stmt);
6027 if (!VECTOR_TYPE_P (TREE_TYPE (var)))
6028 mode = DECL_MODE (var);
6029 else
6030 mode = TYPE_MODE (TREE_TYPE (var));
6032 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
6033 VAR_INIT_STATUS_UNINITIALIZED);
6035 else
6036 gcc_unreachable ();
6038 /* If this function was first compiled with markers
6039 enabled, but they're now disable (e.g. LTO), drop
6040 them on the floor. */
6041 else if (gimple_debug_nonbind_marker_p (stmt)
6042 && !MAY_HAVE_DEBUG_MARKER_INSNS)
6043 goto delink_debug_stmt;
6044 else if (gimple_debug_begin_stmt_p (stmt))
6045 val = GEN_RTX_DEBUG_MARKER_BEGIN_STMT_PAT ();
6046 else if (gimple_debug_inline_entry_p (stmt))
6047 val = GEN_RTX_DEBUG_MARKER_INLINE_ENTRY_PAT ();
6048 else
6049 gcc_unreachable ();
6051 last = get_last_insn ();
6053 set_curr_insn_location (gimple_location (stmt));
6055 emit_debug_insn (val);
6057 if (dump_file && (dump_flags & TDF_DETAILS))
6059 /* We can't dump the insn with a TREE where an RTX
6060 is expected. */
6061 if (GET_CODE (val) == VAR_LOCATION)
6063 gcc_checking_assert (PAT_VAR_LOCATION_LOC (val) == (rtx)value);
6064 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
6066 maybe_dump_rtl_for_gimple_stmt (stmt, last);
6067 if (GET_CODE (val) == VAR_LOCATION)
6068 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
6071 delink_debug_stmt:
6072 /* In order not to generate too many debug temporaries,
6073 we delink all uses of debug statements we already expanded.
6074 Therefore debug statements between definition and real
6075 use of TERed SSA names will continue to use the SSA name,
6076 and not be replaced with debug temps. */
6077 delink_stmt_imm_use (stmt);
6079 gsi = nsi;
6080 gsi_next (&nsi);
6081 if (gsi_end_p (nsi))
6082 break;
6083 stmt = gsi_stmt (nsi);
6084 if (!is_gimple_debug (stmt))
6085 break;
6088 set_curr_insn_location (sloc);
6090 else
6092 gcall *call_stmt = dyn_cast <gcall *> (stmt);
6093 if (call_stmt
6094 && gimple_call_tail_p (call_stmt)
6095 && disable_tail_calls)
6096 gimple_call_set_tail (call_stmt, false);
6098 if (call_stmt && gimple_call_tail_p (call_stmt))
6100 bool can_fallthru;
6101 new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
6102 if (new_bb)
6104 if (can_fallthru)
6105 bb = new_bb;
6106 else
6108 currently_expanding_gimple_stmt = NULL;
6109 return new_bb;
6113 else
6115 def_operand_p def_p;
6116 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
6118 if (def_p != NULL)
6120 /* Ignore this stmt if it is in the list of
6121 replaceable expressions. */
6122 if (SA.values
6123 && bitmap_bit_p (SA.values,
6124 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
6125 continue;
6127 last = expand_gimple_stmt (stmt);
6128 maybe_dump_rtl_for_gimple_stmt (stmt, last);
6133 currently_expanding_gimple_stmt = NULL;
6135 /* Expand implicit goto and convert goto_locus. */
6136 FOR_EACH_EDGE (e, ei, bb->succs)
6138 if (e->goto_locus != UNKNOWN_LOCATION || !nondebug_stmt_seen)
6139 set_curr_insn_location (e->goto_locus);
6140 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
6142 emit_jump (label_rtx_for_bb (e->dest));
6143 e->flags &= ~EDGE_FALLTHRU;
6147 /* Expanded RTL can create a jump in the last instruction of block.
6148 This later might be assumed to be a jump to successor and break edge insertion.
6149 We need to insert dummy move to prevent this. PR41440. */
6150 if (single_succ_p (bb)
6151 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
6152 && (last = get_last_insn ())
6153 && (JUMP_P (last)
6154 || (DEBUG_INSN_P (last)
6155 && JUMP_P (prev_nondebug_insn (last)))))
6157 rtx dummy = gen_reg_rtx (SImode);
6158 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
6161 do_pending_stack_adjust ();
6163 /* Find the block tail. The last insn in the block is the insn
6164 before a barrier and/or table jump insn. */
6165 last = get_last_insn ();
6166 if (BARRIER_P (last))
6167 last = PREV_INSN (last);
6168 if (JUMP_TABLE_DATA_P (last))
6169 last = PREV_INSN (PREV_INSN (last));
6170 if (BARRIER_P (last))
6171 last = PREV_INSN (last);
6172 BB_END (bb) = last;
6174 update_bb_for_insn (bb);
6176 return bb;
6180 /* Create a basic block for initialization code. */
6182 static basic_block
6183 construct_init_block (void)
6185 basic_block init_block, first_block;
6186 edge e = NULL;
6187 int flags;
6189 /* Multiple entry points not supported yet. */
6190 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
6191 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6192 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
6193 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
6194 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
6196 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
6198 /* When entry edge points to first basic block, we don't need jump,
6199 otherwise we have to jump into proper target. */
6200 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
6202 tree label = gimple_block_label (e->dest);
6204 emit_jump (jump_target_rtx (label));
6205 flags = 0;
6207 else
6208 flags = EDGE_FALLTHRU;
6210 init_block = create_basic_block (NEXT_INSN (get_insns ()),
6211 get_last_insn (),
6212 ENTRY_BLOCK_PTR_FOR_FN (cfun));
6213 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
6214 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
6215 if (e)
6217 first_block = e->dest;
6218 redirect_edge_succ (e, init_block);
6219 make_single_succ_edge (init_block, first_block, flags);
6221 else
6222 make_single_succ_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun),
6223 EDGE_FALLTHRU);
6225 update_bb_for_insn (init_block);
6226 return init_block;
6229 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
6230 found in the block tree. */
6232 static void
6233 set_block_levels (tree block, int level)
6235 while (block)
6237 BLOCK_NUMBER (block) = level;
6238 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
6239 block = BLOCK_CHAIN (block);
6243 /* Create a block containing landing pads and similar stuff. */
6245 static void
6246 construct_exit_block (void)
6248 rtx_insn *head = get_last_insn ();
6249 rtx_insn *end;
6250 basic_block exit_block;
6251 edge e, e2;
6252 unsigned ix;
6253 edge_iterator ei;
6254 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
6255 rtx_insn *orig_end = BB_END (prev_bb);
6257 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
6259 /* Make sure the locus is set to the end of the function, so that
6260 epilogue line numbers and warnings are set properly. */
6261 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
6262 input_location = cfun->function_end_locus;
6264 /* Generate rtl for function exit. */
6265 expand_function_end ();
6267 end = get_last_insn ();
6268 if (head == end)
6269 return;
6270 /* While emitting the function end we could move end of the last basic
6271 block. */
6272 BB_END (prev_bb) = orig_end;
6273 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
6274 head = NEXT_INSN (head);
6275 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
6276 bb count counting will be confused. Any instructions before that
6277 label are emitted for the case where PREV_BB falls through into the
6278 exit block, so append those instructions to prev_bb in that case. */
6279 if (NEXT_INSN (head) != return_label)
6281 while (NEXT_INSN (head) != return_label)
6283 if (!NOTE_P (NEXT_INSN (head)))
6284 BB_END (prev_bb) = NEXT_INSN (head);
6285 head = NEXT_INSN (head);
6288 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
6289 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
6290 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
6292 ix = 0;
6293 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
6295 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
6296 if (!(e->flags & EDGE_ABNORMAL))
6297 redirect_edge_succ (e, exit_block);
6298 else
6299 ix++;
6302 e = make_single_succ_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun),
6303 EDGE_FALLTHRU);
6304 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6305 if (e2 != e)
6307 exit_block->count -= e2->count ();
6309 update_bb_for_insn (exit_block);
6312 /* Helper function for discover_nonconstant_array_refs.
6313 Look for ARRAY_REF nodes with non-constant indexes and mark them
6314 addressable. */
6316 static tree
6317 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
6318 void *data)
6320 tree t = *tp;
6321 bitmap forced_stack_vars = (bitmap)((walk_stmt_info *)data)->info;
6323 if (IS_TYPE_OR_DECL_P (t))
6324 *walk_subtrees = 0;
6325 else if (REFERENCE_CLASS_P (t) && TREE_THIS_VOLATILE (t))
6327 t = get_base_address (t);
6328 if (t && DECL_P (t)
6329 && DECL_MODE (t) != BLKmode
6330 && !TREE_ADDRESSABLE (t))
6331 bitmap_set_bit (forced_stack_vars, DECL_UID (t));
6332 *walk_subtrees = 0;
6334 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6336 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6337 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
6338 && (!TREE_OPERAND (t, 2)
6339 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
6340 || (TREE_CODE (t) == COMPONENT_REF
6341 && (!TREE_OPERAND (t,2)
6342 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
6343 || TREE_CODE (t) == BIT_FIELD_REF
6344 || TREE_CODE (t) == REALPART_EXPR
6345 || TREE_CODE (t) == IMAGPART_EXPR
6346 || TREE_CODE (t) == VIEW_CONVERT_EXPR
6347 || CONVERT_EXPR_P (t))
6348 t = TREE_OPERAND (t, 0);
6350 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6352 t = get_base_address (t);
6353 if (t && DECL_P (t)
6354 && DECL_MODE (t) != BLKmode
6355 && !TREE_ADDRESSABLE (t))
6356 bitmap_set_bit (forced_stack_vars, DECL_UID (t));
6359 *walk_subtrees = 0;
6361 /* References of size POLY_INT_CST to a fixed-size object must go
6362 through memory. It's more efficient to force that here than
6363 to create temporary slots on the fly.
6364 RTL expansion expectes TARGET_MEM_REF to always address actual memory.
6365 Also, force to stack non-BLKmode vars accessed through VIEW_CONVERT_EXPR
6366 to BLKmode type. */
6367 else if (TREE_CODE (t) == TARGET_MEM_REF
6368 || (TREE_CODE (t) == MEM_REF
6369 && TYPE_SIZE (TREE_TYPE (t))
6370 && POLY_INT_CST_P (TYPE_SIZE (TREE_TYPE (t))))
6371 || (TREE_CODE (t) == VIEW_CONVERT_EXPR
6372 && TYPE_MODE (TREE_TYPE (t)) == BLKmode))
6374 tree base = get_base_address (t);
6375 if (base
6376 && DECL_P (base)
6377 && !TREE_ADDRESSABLE (base)
6378 && DECL_MODE (base) != BLKmode
6379 && GET_MODE_SIZE (DECL_MODE (base)).is_constant ())
6380 bitmap_set_bit (forced_stack_vars, DECL_UID (base));
6381 *walk_subtrees = 0;
6384 return NULL_TREE;
6387 /* If there's a chance to get a pseudo for t then if it would be of float mode
6388 and the actual access is via an integer mode (lowered memcpy or similar
6389 access) then avoid the register expansion if the mode likely is not storage
6390 suitable for raw bits processing (like XFmode on i?86). */
6392 static void
6393 avoid_type_punning_on_regs (tree t, bitmap forced_stack_vars)
6395 machine_mode access_mode = TYPE_MODE (TREE_TYPE (t));
6396 if (access_mode != BLKmode
6397 && !SCALAR_INT_MODE_P (access_mode))
6398 return;
6399 tree base = get_base_address (t);
6400 if (DECL_P (base)
6401 && !TREE_ADDRESSABLE (base)
6402 && FLOAT_MODE_P (DECL_MODE (base))
6403 && maybe_lt (GET_MODE_PRECISION (DECL_MODE (base)),
6404 GET_MODE_BITSIZE (GET_MODE_INNER (DECL_MODE (base))))
6405 /* Double check in the expensive way we really would get a pseudo. */
6406 && use_register_for_decl (base))
6407 bitmap_set_bit (forced_stack_vars, DECL_UID (base));
6410 /* RTL expansion is not able to compile array references with variable
6411 offsets for arrays stored in single register. Discover such
6412 expressions and mark variables as addressable to avoid this
6413 scenario. */
6415 static void
6416 discover_nonconstant_array_refs (bitmap forced_stack_vars)
6418 basic_block bb;
6419 gimple_stmt_iterator gsi;
6421 walk_stmt_info wi = {};
6422 wi.info = forced_stack_vars;
6423 FOR_EACH_BB_FN (bb, cfun)
6424 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6426 gimple *stmt = gsi_stmt (gsi);
6427 if (!is_gimple_debug (stmt))
6429 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, &wi);
6430 gcall *call = dyn_cast <gcall *> (stmt);
6431 if (call && gimple_call_internal_p (call))
6433 tree cand = NULL_TREE;
6434 switch (gimple_call_internal_fn (call))
6436 case IFN_LOAD_LANES:
6437 /* The source must be a MEM. */
6438 cand = gimple_call_arg (call, 0);
6439 break;
6440 case IFN_STORE_LANES:
6441 /* The destination must be a MEM. */
6442 cand = gimple_call_lhs (call);
6443 break;
6444 default:
6445 break;
6447 if (cand)
6448 cand = get_base_address (cand);
6449 if (cand
6450 && DECL_P (cand)
6451 && use_register_for_decl (cand))
6452 bitmap_set_bit (forced_stack_vars, DECL_UID (cand));
6454 if (gimple_vdef (stmt))
6456 tree t = gimple_get_lhs (stmt);
6457 if (t && REFERENCE_CLASS_P (t))
6458 avoid_type_punning_on_regs (t, forced_stack_vars);
6464 /* This function sets crtl->args.internal_arg_pointer to a virtual
6465 register if DRAP is needed. Local register allocator will replace
6466 virtual_incoming_args_rtx with the virtual register. */
6468 static void
6469 expand_stack_alignment (void)
6471 rtx drap_rtx;
6472 unsigned int preferred_stack_boundary;
6474 if (! SUPPORTS_STACK_ALIGNMENT)
6475 return;
6477 if (cfun->calls_alloca
6478 || cfun->has_nonlocal_label
6479 || crtl->has_nonlocal_goto)
6480 crtl->need_drap = true;
6482 /* Call update_stack_boundary here again to update incoming stack
6483 boundary. It may set incoming stack alignment to a different
6484 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
6485 use the minimum incoming stack alignment to check if it is OK
6486 to perform sibcall optimization since sibcall optimization will
6487 only align the outgoing stack to incoming stack boundary. */
6488 if (targetm.calls.update_stack_boundary)
6489 targetm.calls.update_stack_boundary ();
6491 /* The incoming stack frame has to be aligned at least at
6492 parm_stack_boundary. */
6493 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
6495 /* Update crtl->stack_alignment_estimated and use it later to align
6496 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
6497 exceptions since callgraph doesn't collect incoming stack alignment
6498 in this case. */
6499 if (cfun->can_throw_non_call_exceptions
6500 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
6501 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
6502 else
6503 preferred_stack_boundary = crtl->preferred_stack_boundary;
6504 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
6505 crtl->stack_alignment_estimated = preferred_stack_boundary;
6506 if (preferred_stack_boundary > crtl->stack_alignment_needed)
6507 crtl->stack_alignment_needed = preferred_stack_boundary;
6509 gcc_assert (crtl->stack_alignment_needed
6510 <= crtl->stack_alignment_estimated);
6512 crtl->stack_realign_needed
6513 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
6514 crtl->stack_realign_tried = crtl->stack_realign_needed;
6516 crtl->stack_realign_processed = true;
6518 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
6519 alignment. */
6520 gcc_assert (targetm.calls.get_drap_rtx != NULL);
6521 drap_rtx = targetm.calls.get_drap_rtx ();
6523 /* stack_realign_drap and drap_rtx must match. */
6524 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
6526 /* Do nothing if NULL is returned, which means DRAP is not needed. */
6527 if (drap_rtx != NULL)
6529 crtl->args.internal_arg_pointer = drap_rtx;
6531 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
6532 needed. */
6533 fixup_tail_calls ();
6538 static void
6539 expand_main_function (void)
6541 #if (defined(INVOKE__main) \
6542 || (!defined(HAS_INIT_SECTION) \
6543 && !defined(INIT_SECTION_ASM_OP) \
6544 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
6545 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode);
6546 #endif
6550 /* Expand code to initialize the stack_protect_guard. This is invoked at
6551 the beginning of a function to be protected. */
6553 static void
6554 stack_protect_prologue (void)
6556 tree guard_decl = targetm.stack_protect_guard ();
6557 rtx x, y;
6559 crtl->stack_protect_guard_decl = guard_decl;
6560 x = expand_normal (crtl->stack_protect_guard);
6562 if (targetm.have_stack_protect_combined_set () && guard_decl)
6564 gcc_assert (DECL_P (guard_decl));
6565 y = DECL_RTL (guard_decl);
6567 /* Allow the target to compute address of Y and copy it to X without
6568 leaking Y into a register. This combined address + copy pattern
6569 allows the target to prevent spilling of any intermediate results by
6570 splitting it after register allocator. */
6571 if (rtx_insn *insn = targetm.gen_stack_protect_combined_set (x, y))
6573 emit_insn (insn);
6574 return;
6578 if (guard_decl)
6579 y = expand_normal (guard_decl);
6580 else
6581 y = const0_rtx;
6583 /* Allow the target to copy from Y to X without leaking Y into a
6584 register. */
6585 if (targetm.have_stack_protect_set ())
6586 if (rtx_insn *insn = targetm.gen_stack_protect_set (x, y))
6588 emit_insn (insn);
6589 return;
6592 /* Otherwise do a straight move. */
6593 emit_move_insn (x, y);
6596 /* Translate the intermediate representation contained in the CFG
6597 from GIMPLE trees to RTL.
6599 We do conversion per basic block and preserve/update the tree CFG.
6600 This implies we have to do some magic as the CFG can simultaneously
6601 consist of basic blocks containing RTL and GIMPLE trees. This can
6602 confuse the CFG hooks, so be careful to not manipulate CFG during
6603 the expansion. */
6605 namespace {
6607 const pass_data pass_data_expand =
6609 RTL_PASS, /* type */
6610 "expand", /* name */
6611 OPTGROUP_NONE, /* optinfo_flags */
6612 TV_EXPAND, /* tv_id */
6613 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
6614 | PROP_gimple_lcx
6615 | PROP_gimple_lvec
6616 | PROP_gimple_lva), /* properties_required */
6617 PROP_rtl, /* properties_provided */
6618 ( PROP_ssa | PROP_gimple ), /* properties_destroyed */
6619 0, /* todo_flags_start */
6620 0, /* todo_flags_finish */
6623 class pass_expand : public rtl_opt_pass
6625 public:
6626 pass_expand (gcc::context *ctxt)
6627 : rtl_opt_pass (pass_data_expand, ctxt)
6630 /* opt_pass methods: */
6631 unsigned int execute (function *) final override;
6633 }; // class pass_expand
6635 unsigned int
6636 pass_expand::execute (function *fun)
6638 basic_block bb, init_block;
6639 edge_iterator ei;
6640 edge e;
6641 rtx_insn *var_seq, *var_ret_seq;
6642 unsigned i;
6644 timevar_push (TV_OUT_OF_SSA);
6645 rewrite_out_of_ssa (&SA);
6646 timevar_pop (TV_OUT_OF_SSA);
6647 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
6649 if (MAY_HAVE_DEBUG_BIND_STMTS && flag_tree_ter)
6651 gimple_stmt_iterator gsi;
6652 FOR_EACH_BB_FN (bb, cfun)
6653 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6654 if (gimple_debug_bind_p (gsi_stmt (gsi)))
6655 avoid_deep_ter_for_debug (gsi_stmt (gsi), 0);
6658 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
6659 auto_bitmap forced_stack_vars;
6660 discover_nonconstant_array_refs (forced_stack_vars);
6662 /* Make sure all values used by the optimization passes have sane
6663 defaults. */
6664 reg_renumber = 0;
6666 /* Some backends want to know that we are expanding to RTL. */
6667 currently_expanding_to_rtl = 1;
6668 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
6669 free_dominance_info (CDI_DOMINATORS);
6671 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
6673 insn_locations_init ();
6674 if (!DECL_IS_UNDECLARED_BUILTIN (current_function_decl))
6676 /* Eventually, all FEs should explicitly set function_start_locus. */
6677 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
6678 set_curr_insn_location
6679 (DECL_SOURCE_LOCATION (current_function_decl));
6680 else
6681 set_curr_insn_location (fun->function_start_locus);
6683 else
6684 set_curr_insn_location (UNKNOWN_LOCATION);
6685 prologue_location = curr_insn_location ();
6687 #ifdef INSN_SCHEDULING
6688 init_sched_attrs ();
6689 #endif
6691 /* Make sure first insn is a note even if we don't want linenums.
6692 This makes sure the first insn will never be deleted.
6693 Also, final expects a note to appear there. */
6694 emit_note (NOTE_INSN_DELETED);
6696 targetm.expand_to_rtl_hook ();
6697 crtl->init_stack_alignment ();
6698 fun->cfg->max_jumptable_ents = 0;
6700 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
6701 of the function section at exapnsion time to predict distance of calls. */
6702 resolve_unique_section (current_function_decl, 0, flag_function_sections);
6704 /* Expand the variables recorded during gimple lowering. */
6705 timevar_push (TV_VAR_EXPAND);
6706 start_sequence ();
6708 var_ret_seq = expand_used_vars (forced_stack_vars);
6710 var_seq = get_insns ();
6711 end_sequence ();
6712 timevar_pop (TV_VAR_EXPAND);
6714 /* Honor stack protection warnings. */
6715 if (warn_stack_protect)
6717 if (fun->calls_alloca)
6718 warning (OPT_Wstack_protector,
6719 "stack protector not protecting local variables: "
6720 "variable length buffer");
6721 if (has_short_buffer && !crtl->stack_protect_guard)
6722 warning (OPT_Wstack_protector,
6723 "stack protector not protecting function: "
6724 "all local arrays are less than %d bytes long",
6725 (int) param_ssp_buffer_size);
6728 /* Temporarily mark PARM_DECLs and RESULT_DECLs we need to expand to
6729 memory addressable so expand_function_start can emit the required
6730 copies. */
6731 auto_vec<tree, 16> marked_parms;
6732 for (tree parm = DECL_ARGUMENTS (current_function_decl); parm;
6733 parm = DECL_CHAIN (parm))
6734 if (!TREE_ADDRESSABLE (parm)
6735 && bitmap_bit_p (forced_stack_vars, DECL_UID (parm)))
6737 TREE_ADDRESSABLE (parm) = 1;
6738 marked_parms.safe_push (parm);
6740 if (DECL_RESULT (current_function_decl)
6741 && !TREE_ADDRESSABLE (DECL_RESULT (current_function_decl))
6742 && bitmap_bit_p (forced_stack_vars,
6743 DECL_UID (DECL_RESULT (current_function_decl))))
6745 TREE_ADDRESSABLE (DECL_RESULT (current_function_decl)) = 1;
6746 marked_parms.safe_push (DECL_RESULT (current_function_decl));
6749 /* Set up parameters and prepare for return, for the function. */
6750 expand_function_start (current_function_decl);
6752 /* Clear TREE_ADDRESSABLE again. */
6753 while (!marked_parms.is_empty ())
6754 TREE_ADDRESSABLE (marked_parms.pop ()) = 0;
6756 /* If we emitted any instructions for setting up the variables,
6757 emit them before the FUNCTION_START note. */
6758 if (var_seq)
6760 emit_insn_before (var_seq, parm_birth_insn);
6762 /* In expand_function_end we'll insert the alloca save/restore
6763 before parm_birth_insn. We've just insertted an alloca call.
6764 Adjust the pointer to match. */
6765 parm_birth_insn = var_seq;
6768 /* Now propagate the RTL assignment of each partition to the
6769 underlying var of each SSA_NAME. */
6770 tree name;
6772 FOR_EACH_SSA_NAME (i, name, cfun)
6774 /* We might have generated new SSA names in
6775 update_alias_info_with_stack_vars. They will have a NULL
6776 defining statements, and won't be part of the partitioning,
6777 so ignore those. */
6778 if (!SSA_NAME_DEF_STMT (name))
6779 continue;
6781 adjust_one_expanded_partition_var (name);
6784 /* Clean up RTL of variables that straddle across multiple
6785 partitions, and check that the rtl of any PARM_DECLs that are not
6786 cleaned up is that of their default defs. */
6787 FOR_EACH_SSA_NAME (i, name, cfun)
6789 int part;
6791 /* We might have generated new SSA names in
6792 update_alias_info_with_stack_vars. They will have a NULL
6793 defining statements, and won't be part of the partitioning,
6794 so ignore those. */
6795 if (!SSA_NAME_DEF_STMT (name))
6796 continue;
6797 part = var_to_partition (SA.map, name);
6798 if (part == NO_PARTITION)
6799 continue;
6801 /* If this decl was marked as living in multiple places, reset
6802 this now to NULL. */
6803 tree var = SSA_NAME_VAR (name);
6804 if (var && DECL_RTL_IF_SET (var) == pc_rtx)
6805 SET_DECL_RTL (var, NULL);
6806 /* Check that the pseudos chosen by assign_parms are those of
6807 the corresponding default defs. */
6808 else if (SSA_NAME_IS_DEFAULT_DEF (name)
6809 && (TREE_CODE (var) == PARM_DECL
6810 || TREE_CODE (var) == RESULT_DECL))
6812 rtx in = DECL_RTL_IF_SET (var);
6813 gcc_assert (in);
6814 rtx out = SA.partition_to_pseudo[part];
6815 gcc_assert (in == out);
6817 /* Now reset VAR's RTL to IN, so that the _EXPR attrs match
6818 those expected by debug backends for each parm and for
6819 the result. This is particularly important for stabs,
6820 whose register elimination from parm's DECL_RTL may cause
6821 -fcompare-debug differences as SET_DECL_RTL changes reg's
6822 attrs. So, make sure the RTL already has the parm as the
6823 EXPR, so that it won't change. */
6824 SET_DECL_RTL (var, NULL_RTX);
6825 if (MEM_P (in))
6826 set_mem_attributes (in, var, true);
6827 SET_DECL_RTL (var, in);
6831 /* If this function is `main', emit a call to `__main'
6832 to run global initializers, etc. */
6833 if (DECL_NAME (current_function_decl)
6834 && MAIN_NAME_P (DECL_NAME (current_function_decl))
6835 && DECL_FILE_SCOPE_P (current_function_decl))
6836 expand_main_function ();
6838 /* Initialize the stack_protect_guard field. This must happen after the
6839 call to __main (if any) so that the external decl is initialized. */
6840 if (crtl->stack_protect_guard && targetm.stack_protect_runtime_enabled_p ())
6841 stack_protect_prologue ();
6843 expand_phi_nodes (&SA);
6845 /* Release any stale SSA redirection data. */
6846 redirect_edge_var_map_empty ();
6848 /* Register rtl specific functions for cfg. */
6849 rtl_register_cfg_hooks ();
6851 init_block = construct_init_block ();
6853 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
6854 remaining edges later. */
6855 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
6856 e->flags &= ~EDGE_EXECUTABLE;
6858 /* If the function has too many markers, drop them while expanding. */
6859 if (cfun->debug_marker_count
6860 >= param_max_debug_marker_count)
6861 cfun->debug_nonbind_markers = false;
6863 lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
6864 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
6865 next_bb)
6866 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
6868 if (MAY_HAVE_DEBUG_BIND_INSNS)
6869 expand_debug_locations ();
6871 if (deep_ter_debug_map)
6873 delete deep_ter_debug_map;
6874 deep_ter_debug_map = NULL;
6877 /* Free stuff we no longer need after GIMPLE optimizations. */
6878 free_dominance_info (CDI_DOMINATORS);
6879 free_dominance_info (CDI_POST_DOMINATORS);
6880 delete_tree_cfg_annotations (fun);
6882 timevar_push (TV_OUT_OF_SSA);
6883 finish_out_of_ssa (&SA);
6884 timevar_pop (TV_OUT_OF_SSA);
6886 timevar_push (TV_POST_EXPAND);
6887 /* We are no longer in SSA form. */
6888 fun->gimple_df->in_ssa_p = false;
6889 loops_state_clear (LOOP_CLOSED_SSA);
6891 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
6892 conservatively to true until they are all profile aware. */
6893 delete lab_rtx_for_bb;
6894 free_histograms (fun);
6896 construct_exit_block ();
6897 insn_locations_finalize ();
6899 if (var_ret_seq)
6901 rtx_insn *after = return_label;
6902 rtx_insn *next = NEXT_INSN (after);
6903 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
6904 after = next;
6905 emit_insn_after (var_ret_seq, after);
6908 if (hwasan_sanitize_stack_p ())
6909 hwasan_maybe_emit_frame_base_init ();
6911 /* Zap the tree EH table. */
6912 set_eh_throw_stmt_table (fun, NULL);
6914 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
6915 split edges which edge insertions might do. */
6916 rebuild_jump_labels (get_insns ());
6918 /* If we have a single successor to the entry block, put the pending insns
6919 after parm birth, but before NOTE_INSNS_FUNCTION_BEG. */
6920 if (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
6922 edge e = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fun));
6923 if (e->insns.r)
6925 rtx_insn *insns = e->insns.r;
6926 e->insns.r = NULL;
6927 rebuild_jump_labels_chain (insns);
6928 if (NOTE_P (parm_birth_insn)
6929 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
6930 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
6931 else
6932 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
6936 /* Otherwise, as well as for other edges, take the usual way. */
6937 commit_edge_insertions ();
6939 /* We're done expanding trees to RTL. */
6940 currently_expanding_to_rtl = 0;
6942 flush_mark_addressable_queue ();
6944 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
6945 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6947 edge e;
6948 edge_iterator ei;
6949 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6951 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
6952 e->flags &= ~EDGE_EXECUTABLE;
6954 /* At the moment not all abnormal edges match the RTL
6955 representation. It is safe to remove them here as
6956 find_many_sub_basic_blocks will rediscover them.
6957 In the future we should get this fixed properly. */
6958 if ((e->flags & EDGE_ABNORMAL)
6959 && !(e->flags & EDGE_SIBCALL))
6960 remove_edge (e);
6961 else
6962 ei_next (&ei);
6966 auto_sbitmap blocks (last_basic_block_for_fn (fun));
6967 bitmap_ones (blocks);
6968 find_many_sub_basic_blocks (blocks);
6969 purge_all_dead_edges ();
6971 /* After initial rtl generation, call back to finish generating
6972 exception support code. We need to do this before cleaning up
6973 the CFG as the code does not expect dead landing pads. */
6974 if (fun->eh->region_tree != NULL)
6975 finish_eh_generation ();
6977 /* Call expand_stack_alignment after finishing all
6978 updates to crtl->preferred_stack_boundary. */
6979 expand_stack_alignment ();
6981 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
6982 function. */
6983 if (crtl->tail_call_emit)
6984 fixup_tail_calls ();
6986 HOST_WIDE_INT patch_area_size, patch_area_entry;
6987 parse_and_check_patch_area (flag_patchable_function_entry, false,
6988 &patch_area_size, &patch_area_entry);
6990 tree patchable_function_entry_attr
6991 = lookup_attribute ("patchable_function_entry",
6992 DECL_ATTRIBUTES (cfun->decl));
6993 if (patchable_function_entry_attr)
6995 tree pp_val = TREE_VALUE (patchable_function_entry_attr);
6996 tree patchable_function_entry_value1 = TREE_VALUE (pp_val);
6998 patch_area_size = tree_to_uhwi (patchable_function_entry_value1);
6999 patch_area_entry = 0;
7000 if (TREE_CHAIN (pp_val) != NULL_TREE)
7002 tree patchable_function_entry_value2
7003 = TREE_VALUE (TREE_CHAIN (pp_val));
7004 patch_area_entry = tree_to_uhwi (patchable_function_entry_value2);
7008 if (patch_area_entry > patch_area_size)
7010 if (patch_area_size > 0)
7011 warning (OPT_Wattributes,
7012 "patchable function entry %wu exceeds size %wu",
7013 patch_area_entry, patch_area_size);
7014 patch_area_entry = 0;
7017 crtl->patch_area_size = patch_area_size;
7018 crtl->patch_area_entry = patch_area_entry;
7020 /* BB subdivision may have created basic blocks that are only reachable
7021 from unlikely bbs but not marked as such in the profile. */
7022 if (optimize)
7023 propagate_unlikely_bbs_forward ();
7025 /* Remove unreachable blocks, otherwise we cannot compute dominators
7026 which are needed for loop state verification. As a side-effect
7027 this also compacts blocks.
7028 ??? We cannot remove trivially dead insns here as for example
7029 the DRAP reg on i?86 is not magically live at this point.
7030 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
7031 cleanup_cfg (CLEANUP_NO_INSN_DEL);
7033 checking_verify_flow_info ();
7035 /* Initialize pseudos allocated for hard registers. */
7036 emit_initial_value_sets ();
7038 /* And finally unshare all RTL. */
7039 unshare_all_rtl ();
7041 /* There's no need to defer outputting this function any more; we
7042 know we want to output it. */
7043 DECL_DEFER_OUTPUT (current_function_decl) = 0;
7045 /* Now that we're done expanding trees to RTL, we shouldn't have any
7046 more CONCATs anywhere. */
7047 generating_concat_p = 0;
7049 if (dump_file)
7051 fprintf (dump_file,
7052 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
7053 /* And the pass manager will dump RTL for us. */
7056 /* If we're emitting a nested function, make sure its parent gets
7057 emitted as well. Doing otherwise confuses debug info. */
7059 tree parent;
7060 for (parent = DECL_CONTEXT (current_function_decl);
7061 parent != NULL_TREE;
7062 parent = get_containing_scope (parent))
7063 if (TREE_CODE (parent) == FUNCTION_DECL)
7064 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
7067 TREE_ASM_WRITTEN (current_function_decl) = 1;
7069 /* After expanding, the return labels are no longer needed. */
7070 return_label = NULL;
7071 naked_return_label = NULL;
7073 /* After expanding, the tm_restart map is no longer needed. */
7074 if (fun->gimple_df->tm_restart)
7075 fun->gimple_df->tm_restart = NULL;
7077 /* Tag the blocks with a depth number so that change_scope can find
7078 the common parent easily. */
7079 set_block_levels (DECL_INITIAL (fun->decl), 0);
7080 default_rtl_profile ();
7082 /* For -dx discard loops now, otherwise IL verify in clean_state will
7083 ICE. */
7084 if (rtl_dump_and_exit)
7086 cfun->curr_properties &= ~PROP_loops;
7087 loop_optimizer_finalize ();
7090 timevar_pop (TV_POST_EXPAND);
7092 return 0;
7095 } // anon namespace
7097 rtl_opt_pass *
7098 make_pass_expand (gcc::context *ctxt)
7100 return new pass_expand (ctxt);