xfail scan-tree-dump-not throw in g++.dg/pr99966.C on hppa*64*-*-*
[official-gcc.git] / gcc / cfgexpand.cc
blob9ce01181ebc826546a29e3b47f89e04ac88f5a86
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 struct separate_ops ops;
4001 bool promoted = false;
4003 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
4004 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
4005 promoted = true;
4007 ops.code = gimple_assign_rhs_code (assign_stmt);
4008 ops.type = TREE_TYPE (lhs);
4009 switch (get_gimple_rhs_class (ops.code))
4011 case GIMPLE_TERNARY_RHS:
4012 ops.op2 = gimple_assign_rhs3 (assign_stmt);
4013 /* Fallthru */
4014 case GIMPLE_BINARY_RHS:
4015 ops.op1 = gimple_assign_rhs2 (assign_stmt);
4016 /* Fallthru */
4017 case GIMPLE_UNARY_RHS:
4018 ops.op0 = gimple_assign_rhs1 (assign_stmt);
4019 break;
4020 default:
4021 gcc_unreachable ();
4023 ops.location = gimple_location (stmt);
4025 /* If we want to use a nontemporal store, force the value to
4026 register first. If we store into a promoted register,
4027 don't directly expand to target. */
4028 temp = nontemporal || promoted ? NULL_RTX : target;
4029 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
4030 EXPAND_NORMAL);
4032 if (temp == target)
4034 else if (promoted)
4036 int unsignedp = SUBREG_PROMOTED_SIGN (target);
4037 /* If TEMP is a VOIDmode constant, use convert_modes to make
4038 sure that we properly convert it. */
4039 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
4041 temp = convert_modes (GET_MODE (target),
4042 TYPE_MODE (ops.type),
4043 temp, unsignedp);
4044 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
4045 GET_MODE (target), temp, unsignedp);
4048 convert_move (SUBREG_REG (target), temp, unsignedp);
4050 else if (nontemporal && emit_storent_insn (target, temp))
4052 else
4054 temp = force_operand (temp, target);
4055 if (temp != target)
4056 emit_move_insn (target, temp);
4060 break;
4062 default:
4063 gcc_unreachable ();
4067 /* Expand one gimple statement STMT and return the last RTL instruction
4068 before any of the newly generated ones.
4070 In addition to generating the necessary RTL instructions this also
4071 sets REG_EH_REGION notes if necessary and sets the current source
4072 location for diagnostics. */
4074 static rtx_insn *
4075 expand_gimple_stmt (gimple *stmt)
4077 location_t saved_location = input_location;
4078 rtx_insn *last = get_last_insn ();
4079 int lp_nr;
4081 gcc_assert (cfun);
4083 /* We need to save and restore the current source location so that errors
4084 discovered during expansion are emitted with the right location. But
4085 it would be better if the diagnostic routines used the source location
4086 embedded in the tree nodes rather than globals. */
4087 if (gimple_has_location (stmt))
4088 input_location = gimple_location (stmt);
4090 expand_gimple_stmt_1 (stmt);
4092 /* Free any temporaries used to evaluate this statement. */
4093 free_temp_slots ();
4095 input_location = saved_location;
4097 /* Mark all insns that may trap. */
4098 lp_nr = lookup_stmt_eh_lp (stmt);
4099 if (lp_nr)
4101 rtx_insn *insn;
4102 for (insn = next_real_insn (last); insn;
4103 insn = next_real_insn (insn))
4105 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
4106 /* If we want exceptions for non-call insns, any
4107 may_trap_p instruction may throw. */
4108 && GET_CODE (PATTERN (insn)) != CLOBBER
4109 && GET_CODE (PATTERN (insn)) != USE
4110 && insn_could_throw_p (insn))
4111 make_reg_eh_region_note (insn, 0, lp_nr);
4115 return last;
4118 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
4119 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
4120 generated a tail call (something that might be denied by the ABI
4121 rules governing the call; see calls.cc).
4123 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
4124 can still reach the rest of BB. The case here is __builtin_sqrt,
4125 where the NaN result goes through the external function (with a
4126 tailcall) and the normal result happens via a sqrt instruction. */
4128 static basic_block
4129 expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
4131 rtx_insn *last2, *last;
4132 edge e;
4133 edge_iterator ei;
4134 profile_probability probability;
4136 last2 = last = expand_gimple_stmt (stmt);
4138 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
4139 if (CALL_P (last) && SIBLING_CALL_P (last))
4140 goto found;
4142 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
4144 *can_fallthru = true;
4145 return NULL;
4147 found:
4148 /* ??? Wouldn't it be better to just reset any pending stack adjust?
4149 Any instructions emitted here are about to be deleted. */
4150 do_pending_stack_adjust ();
4152 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
4153 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
4154 EH or abnormal edges, we shouldn't have created a tail call in
4155 the first place. So it seems to me we should just be removing
4156 all edges here, or redirecting the existing fallthru edge to
4157 the exit block. */
4159 probability = profile_probability::never ();
4161 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
4163 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
4165 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
4166 e->dest->count -= e->count ();
4167 probability += e->probability;
4168 remove_edge (e);
4170 else
4171 ei_next (&ei);
4174 /* This is somewhat ugly: the call_expr expander often emits instructions
4175 after the sibcall (to perform the function return). These confuse the
4176 find_many_sub_basic_blocks code, so we need to get rid of these. */
4177 last = NEXT_INSN (last);
4178 gcc_assert (BARRIER_P (last));
4180 *can_fallthru = false;
4181 while (NEXT_INSN (last))
4183 /* For instance an sqrt builtin expander expands if with
4184 sibcall in the then and label for `else`. */
4185 if (LABEL_P (NEXT_INSN (last)))
4187 *can_fallthru = true;
4188 break;
4190 delete_insn (NEXT_INSN (last));
4193 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
4194 | EDGE_SIBCALL);
4195 e->probability = probability;
4196 BB_END (bb) = last;
4197 update_bb_for_insn (bb);
4199 if (NEXT_INSN (last))
4201 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
4203 last = BB_END (bb);
4204 if (BARRIER_P (last))
4205 BB_END (bb) = PREV_INSN (last);
4208 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
4210 return bb;
4213 /* Return the difference between the floor and the truncated result of
4214 a signed division by OP1 with remainder MOD. */
4215 static rtx
4216 floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
4218 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
4219 return gen_rtx_IF_THEN_ELSE
4220 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
4221 gen_rtx_IF_THEN_ELSE
4222 (mode, gen_rtx_LT (BImode,
4223 gen_rtx_DIV (mode, op1, mod),
4224 const0_rtx),
4225 constm1_rtx, const0_rtx),
4226 const0_rtx);
4229 /* Return the difference between the ceil and the truncated result of
4230 a signed division by OP1 with remainder MOD. */
4231 static rtx
4232 ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
4234 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
4235 return gen_rtx_IF_THEN_ELSE
4236 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
4237 gen_rtx_IF_THEN_ELSE
4238 (mode, gen_rtx_GT (BImode,
4239 gen_rtx_DIV (mode, op1, mod),
4240 const0_rtx),
4241 const1_rtx, const0_rtx),
4242 const0_rtx);
4245 /* Return the difference between the ceil and the truncated result of
4246 an unsigned division by OP1 with remainder MOD. */
4247 static rtx
4248 ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
4250 /* (mod != 0 ? 1 : 0) */
4251 return gen_rtx_IF_THEN_ELSE
4252 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
4253 const1_rtx, const0_rtx);
4256 /* Return the difference between the rounded and the truncated result
4257 of a signed division by OP1 with remainder MOD. Halfway cases are
4258 rounded away from zero, rather than to the nearest even number. */
4259 static rtx
4260 round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
4262 /* (abs (mod) >= abs (op1) - abs (mod)
4263 ? (op1 / mod > 0 ? 1 : -1)
4264 : 0) */
4265 return gen_rtx_IF_THEN_ELSE
4266 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
4267 gen_rtx_MINUS (mode,
4268 gen_rtx_ABS (mode, op1),
4269 gen_rtx_ABS (mode, mod))),
4270 gen_rtx_IF_THEN_ELSE
4271 (mode, gen_rtx_GT (BImode,
4272 gen_rtx_DIV (mode, op1, mod),
4273 const0_rtx),
4274 const1_rtx, constm1_rtx),
4275 const0_rtx);
4278 /* Return the difference between the rounded and the truncated result
4279 of a unsigned division by OP1 with remainder MOD. Halfway cases
4280 are rounded away from zero, rather than to the nearest even
4281 number. */
4282 static rtx
4283 round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
4285 /* (mod >= op1 - mod ? 1 : 0) */
4286 return gen_rtx_IF_THEN_ELSE
4287 (mode, gen_rtx_GE (BImode, mod,
4288 gen_rtx_MINUS (mode, op1, mod)),
4289 const1_rtx, const0_rtx);
4292 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
4293 any rtl. */
4295 static rtx
4296 convert_debug_memory_address (scalar_int_mode mode, rtx x,
4297 addr_space_t as)
4299 #ifndef POINTERS_EXTEND_UNSIGNED
4300 gcc_assert (mode == Pmode
4301 || mode == targetm.addr_space.address_mode (as));
4302 gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
4303 #else
4304 rtx temp;
4306 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
4308 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
4309 return x;
4311 /* X must have some form of address mode already. */
4312 scalar_int_mode xmode = as_a <scalar_int_mode> (GET_MODE (x));
4313 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
4314 x = lowpart_subreg (mode, x, xmode);
4315 else if (POINTERS_EXTEND_UNSIGNED > 0)
4316 x = gen_rtx_ZERO_EXTEND (mode, x);
4317 else if (!POINTERS_EXTEND_UNSIGNED)
4318 x = gen_rtx_SIGN_EXTEND (mode, x);
4319 else
4321 switch (GET_CODE (x))
4323 case SUBREG:
4324 if ((SUBREG_PROMOTED_VAR_P (x)
4325 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
4326 || (GET_CODE (SUBREG_REG (x)) == PLUS
4327 && REG_P (XEXP (SUBREG_REG (x), 0))
4328 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
4329 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
4330 && GET_MODE (SUBREG_REG (x)) == mode)
4331 return SUBREG_REG (x);
4332 break;
4333 case LABEL_REF:
4334 temp = gen_rtx_LABEL_REF (mode, label_ref_label (x));
4335 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
4336 return temp;
4337 case SYMBOL_REF:
4338 temp = shallow_copy_rtx (x);
4339 PUT_MODE (temp, mode);
4340 return temp;
4341 case CONST:
4342 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
4343 if (temp)
4344 temp = gen_rtx_CONST (mode, temp);
4345 return temp;
4346 case PLUS:
4347 case MINUS:
4348 if (CONST_INT_P (XEXP (x, 1)))
4350 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
4351 if (temp)
4352 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
4354 break;
4355 default:
4356 break;
4358 /* Don't know how to express ptr_extend as operation in debug info. */
4359 return NULL;
4361 #endif /* POINTERS_EXTEND_UNSIGNED */
4363 return x;
4366 /* Map from SSA_NAMEs to corresponding DEBUG_EXPR_DECLs created
4367 by avoid_deep_ter_for_debug. */
4369 static hash_map<tree, tree> *deep_ter_debug_map;
4371 /* Split too deep TER chains for debug stmts using debug temporaries. */
4373 static void
4374 avoid_deep_ter_for_debug (gimple *stmt, int depth)
4376 use_operand_p use_p;
4377 ssa_op_iter iter;
4378 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
4380 tree use = USE_FROM_PTR (use_p);
4381 if (TREE_CODE (use) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (use))
4382 continue;
4383 gimple *g = get_gimple_for_ssa_name (use);
4384 if (g == NULL)
4385 continue;
4386 if (depth > 6 && !stmt_ends_bb_p (g))
4388 if (deep_ter_debug_map == NULL)
4389 deep_ter_debug_map = new hash_map<tree, tree>;
4391 tree &vexpr = deep_ter_debug_map->get_or_insert (use);
4392 if (vexpr != NULL)
4393 continue;
4394 vexpr = build_debug_expr_decl (TREE_TYPE (use));
4395 gimple *def_temp = gimple_build_debug_bind (vexpr, use, g);
4396 gimple_stmt_iterator gsi = gsi_for_stmt (g);
4397 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
4398 avoid_deep_ter_for_debug (def_temp, 0);
4400 else
4401 avoid_deep_ter_for_debug (g, depth + 1);
4405 /* Return an RTX equivalent to the value of the parameter DECL. */
4407 static rtx
4408 expand_debug_parm_decl (tree decl)
4410 rtx incoming = DECL_INCOMING_RTL (decl);
4412 if (incoming
4413 && GET_MODE (incoming) != BLKmode
4414 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
4415 || (MEM_P (incoming)
4416 && REG_P (XEXP (incoming, 0))
4417 && HARD_REGISTER_P (XEXP (incoming, 0)))))
4419 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
4421 #ifdef HAVE_window_save
4422 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
4423 If the target machine has an explicit window save instruction, the
4424 actual entry value is the corresponding OUTGOING_REGNO instead. */
4425 if (REG_P (incoming)
4426 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
4427 incoming
4428 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
4429 OUTGOING_REGNO (REGNO (incoming)), 0);
4430 else if (MEM_P (incoming))
4432 rtx reg = XEXP (incoming, 0);
4433 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
4435 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
4436 incoming = replace_equiv_address_nv (incoming, reg);
4438 else
4439 incoming = copy_rtx (incoming);
4441 #endif
4443 ENTRY_VALUE_EXP (rtl) = incoming;
4444 return rtl;
4447 if (incoming
4448 && GET_MODE (incoming) != BLKmode
4449 && !TREE_ADDRESSABLE (decl)
4450 && MEM_P (incoming)
4451 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
4452 || (GET_CODE (XEXP (incoming, 0)) == PLUS
4453 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
4454 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
4455 return copy_rtx (incoming);
4457 return NULL_RTX;
4460 /* Return an RTX equivalent to the value of the tree expression EXP. */
4462 static rtx
4463 expand_debug_expr (tree exp)
4465 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
4466 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4467 machine_mode inner_mode = VOIDmode;
4468 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
4469 addr_space_t as;
4470 scalar_int_mode op0_mode, op1_mode, addr_mode;
4472 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
4474 case tcc_expression:
4475 switch (TREE_CODE (exp))
4477 case COND_EXPR:
4478 case DOT_PROD_EXPR:
4479 case SAD_EXPR:
4480 case WIDEN_MULT_PLUS_EXPR:
4481 case WIDEN_MULT_MINUS_EXPR:
4482 goto ternary;
4484 case TRUTH_ANDIF_EXPR:
4485 case TRUTH_ORIF_EXPR:
4486 case TRUTH_AND_EXPR:
4487 case TRUTH_OR_EXPR:
4488 case TRUTH_XOR_EXPR:
4489 goto binary;
4491 case TRUTH_NOT_EXPR:
4492 goto unary;
4494 default:
4495 break;
4497 break;
4499 ternary:
4500 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
4501 if (!op2)
4502 return NULL_RTX;
4503 /* Fall through. */
4505 binary:
4506 case tcc_binary:
4507 if (mode == BLKmode)
4508 return NULL_RTX;
4509 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4510 if (!op1)
4511 return NULL_RTX;
4512 switch (TREE_CODE (exp))
4514 case LSHIFT_EXPR:
4515 case RSHIFT_EXPR:
4516 case LROTATE_EXPR:
4517 case RROTATE_EXPR:
4518 case WIDEN_LSHIFT_EXPR:
4519 /* Ensure second operand isn't wider than the first one. */
4520 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 1)));
4521 if (is_a <scalar_int_mode> (inner_mode, &op1_mode)
4522 && (GET_MODE_UNIT_PRECISION (mode)
4523 < GET_MODE_PRECISION (op1_mode)))
4524 op1 = lowpart_subreg (GET_MODE_INNER (mode), op1, op1_mode);
4525 break;
4526 default:
4527 break;
4529 /* Fall through. */
4531 unary:
4532 case tcc_unary:
4533 if (mode == BLKmode)
4534 return NULL_RTX;
4535 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4536 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4537 if (!op0)
4538 return NULL_RTX;
4539 break;
4541 case tcc_comparison:
4542 unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
4543 goto binary;
4545 case tcc_type:
4546 case tcc_statement:
4547 gcc_unreachable ();
4549 case tcc_constant:
4550 case tcc_exceptional:
4551 case tcc_declaration:
4552 case tcc_reference:
4553 case tcc_vl_exp:
4554 break;
4557 switch (TREE_CODE (exp))
4559 case STRING_CST:
4560 if (!lookup_constant_def (exp))
4562 if (strlen (TREE_STRING_POINTER (exp)) + 1
4563 != (size_t) TREE_STRING_LENGTH (exp))
4564 return NULL_RTX;
4565 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
4566 op0 = gen_rtx_MEM (BLKmode, op0);
4567 set_mem_attributes (op0, exp, 0);
4568 return op0;
4570 /* Fall through. */
4572 case INTEGER_CST:
4573 if (TREE_CODE (TREE_TYPE (exp)) == BITINT_TYPE
4574 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
4575 return NULL;
4576 /* FALLTHRU */
4577 case REAL_CST:
4578 case FIXED_CST:
4579 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
4580 return op0;
4582 case POLY_INT_CST:
4583 return immed_wide_int_const (poly_int_cst_value (exp), mode);
4585 case COMPLEX_CST:
4586 gcc_assert (COMPLEX_MODE_P (mode));
4587 op0 = expand_debug_expr (TREE_REALPART (exp));
4588 op1 = expand_debug_expr (TREE_IMAGPART (exp));
4589 return gen_rtx_CONCAT (mode, op0, op1);
4591 case DEBUG_EXPR_DECL:
4592 op0 = DECL_RTL_IF_SET (exp);
4594 if (op0)
4596 if (GET_MODE (op0) != mode)
4597 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (exp)));
4598 else
4599 return op0;
4602 op0 = gen_rtx_DEBUG_EXPR (mode);
4603 DEBUG_EXPR_TREE_DECL (op0) = exp;
4604 SET_DECL_RTL (exp, op0);
4606 return op0;
4608 case VAR_DECL:
4609 case PARM_DECL:
4610 case FUNCTION_DECL:
4611 case LABEL_DECL:
4612 case CONST_DECL:
4613 case RESULT_DECL:
4614 op0 = DECL_RTL_IF_SET (exp);
4616 /* This decl was probably optimized away. */
4617 if (!op0
4618 /* At least label RTXen are sometimes replaced by
4619 NOTE_INSN_DELETED_LABEL. Any notes here are not
4620 handled by copy_rtx. */
4621 || NOTE_P (op0))
4623 if (!VAR_P (exp)
4624 || DECL_EXTERNAL (exp)
4625 || !TREE_STATIC (exp)
4626 || !DECL_NAME (exp)
4627 || DECL_HARD_REGISTER (exp)
4628 || DECL_IN_CONSTANT_POOL (exp)
4629 || mode == VOIDmode
4630 || symtab_node::get (exp) == NULL)
4631 return NULL;
4633 op0 = make_decl_rtl_for_debug (exp);
4634 if (!MEM_P (op0)
4635 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
4636 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
4637 return NULL;
4639 else if (VAR_P (exp)
4640 && is_global_var (exp)
4641 && symtab_node::get (exp) == NULL)
4642 return NULL;
4643 else
4644 op0 = copy_rtx (op0);
4646 if (GET_MODE (op0) == BLKmode
4647 /* If op0 is not BLKmode, but mode is, adjust_mode
4648 below would ICE. While it is likely a FE bug,
4649 try to be robust here. See PR43166. */
4650 || mode == BLKmode
4651 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
4653 gcc_assert (MEM_P (op0));
4654 op0 = adjust_address_nv (op0, mode, 0);
4655 return op0;
4658 /* Fall through. */
4660 adjust_mode:
4661 case PAREN_EXPR:
4662 CASE_CONVERT:
4664 inner_mode = GET_MODE (op0);
4666 if (mode == inner_mode)
4667 return op0;
4669 if (inner_mode == VOIDmode)
4671 if (TREE_CODE (exp) == SSA_NAME)
4672 inner_mode = TYPE_MODE (TREE_TYPE (exp));
4673 else
4674 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4675 if (mode == inner_mode)
4676 return op0;
4679 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4681 if (GET_MODE_UNIT_BITSIZE (mode)
4682 == GET_MODE_UNIT_BITSIZE (inner_mode))
4683 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4684 else if (GET_MODE_UNIT_BITSIZE (mode)
4685 < GET_MODE_UNIT_BITSIZE (inner_mode))
4686 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4687 else
4688 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4690 else if (FLOAT_MODE_P (mode))
4692 gcc_assert (TREE_CODE (exp) != SSA_NAME);
4693 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4694 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
4695 else
4696 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
4698 else if (FLOAT_MODE_P (inner_mode))
4700 if (unsignedp)
4701 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4702 else
4703 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4705 else if (GET_MODE_UNIT_PRECISION (mode)
4706 == GET_MODE_UNIT_PRECISION (inner_mode))
4707 op0 = lowpart_subreg (mode, op0, inner_mode);
4708 else if (GET_MODE_UNIT_PRECISION (mode)
4709 < GET_MODE_UNIT_PRECISION (inner_mode))
4710 op0 = simplify_gen_unary (TRUNCATE, mode, op0, inner_mode);
4711 else if (UNARY_CLASS_P (exp)
4712 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
4713 : unsignedp)
4714 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4715 else
4716 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4718 return op0;
4721 case MEM_REF:
4722 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4724 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
4725 TREE_OPERAND (exp, 0),
4726 TREE_OPERAND (exp, 1));
4727 if (newexp)
4728 return expand_debug_expr (newexp);
4730 /* FALLTHROUGH */
4731 case INDIRECT_REF:
4732 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4733 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4734 if (!op0)
4735 return NULL;
4737 if (TREE_CODE (exp) == MEM_REF)
4739 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4740 || (GET_CODE (op0) == PLUS
4741 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
4742 /* (mem (debug_implicit_ptr)) might confuse aliasing.
4743 Instead just use get_inner_reference. */
4744 goto component_ref;
4746 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4747 poly_int64 offset;
4748 if (!op1 || !poly_int_rtx_p (op1, &offset))
4749 return NULL;
4751 op0 = plus_constant (inner_mode, op0, offset);
4754 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4756 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4757 op0, as);
4758 if (op0 == NULL_RTX)
4759 return NULL;
4761 op0 = gen_rtx_MEM (mode, op0);
4762 set_mem_attributes (op0, exp, 0);
4763 if (TREE_CODE (exp) == MEM_REF
4764 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4765 set_mem_expr (op0, NULL_TREE);
4766 set_mem_addr_space (op0, as);
4768 return op0;
4770 case TARGET_MEM_REF:
4771 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
4772 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
4773 return NULL;
4775 op0 = expand_debug_expr
4776 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
4777 if (!op0)
4778 return NULL;
4780 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4781 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4782 op0, as);
4783 if (op0 == NULL_RTX)
4784 return NULL;
4786 op0 = gen_rtx_MEM (mode, op0);
4788 set_mem_attributes (op0, exp, 0);
4789 set_mem_addr_space (op0, as);
4791 return op0;
4793 component_ref:
4794 case ARRAY_REF:
4795 case ARRAY_RANGE_REF:
4796 case COMPONENT_REF:
4797 case BIT_FIELD_REF:
4798 case REALPART_EXPR:
4799 case IMAGPART_EXPR:
4800 case VIEW_CONVERT_EXPR:
4802 machine_mode mode1;
4803 poly_int64 bitsize, bitpos;
4804 tree offset;
4805 int reversep, volatilep = 0;
4806 tree tem
4807 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
4808 &unsignedp, &reversep, &volatilep);
4809 rtx orig_op0;
4811 if (known_eq (bitsize, 0))
4812 return NULL;
4814 orig_op0 = op0 = expand_debug_expr (tem);
4816 if (!op0)
4817 return NULL;
4819 if (offset)
4821 machine_mode addrmode, offmode;
4823 if (!MEM_P (op0))
4824 return NULL;
4826 op0 = XEXP (op0, 0);
4827 addrmode = GET_MODE (op0);
4828 if (addrmode == VOIDmode)
4829 addrmode = Pmode;
4831 op1 = expand_debug_expr (offset);
4832 if (!op1)
4833 return NULL;
4835 offmode = GET_MODE (op1);
4836 if (offmode == VOIDmode)
4837 offmode = TYPE_MODE (TREE_TYPE (offset));
4839 if (addrmode != offmode)
4840 op1 = lowpart_subreg (addrmode, op1, offmode);
4842 /* Don't use offset_address here, we don't need a
4843 recognizable address, and we don't want to generate
4844 code. */
4845 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4846 op0, op1));
4849 if (MEM_P (op0))
4851 if (mode1 == VOIDmode)
4853 if (maybe_gt (bitsize, MAX_BITSIZE_MODE_ANY_INT))
4854 return NULL;
4855 /* Bitfield. */
4856 mode1 = smallest_int_mode_for_size (bitsize);
4858 poly_int64 bytepos = bits_to_bytes_round_down (bitpos);
4859 if (maybe_ne (bytepos, 0))
4861 op0 = adjust_address_nv (op0, mode1, bytepos);
4862 bitpos = num_trailing_bits (bitpos);
4864 else if (known_eq (bitpos, 0)
4865 && known_eq (bitsize, GET_MODE_BITSIZE (mode)))
4866 op0 = adjust_address_nv (op0, mode, 0);
4867 else if (GET_MODE (op0) != mode1)
4868 op0 = adjust_address_nv (op0, mode1, 0);
4869 else
4870 op0 = copy_rtx (op0);
4871 if (op0 == orig_op0)
4872 op0 = shallow_copy_rtx (op0);
4873 if (TREE_CODE (tem) != SSA_NAME)
4874 set_mem_attributes (op0, exp, 0);
4877 if (known_eq (bitpos, 0) && mode == GET_MODE (op0))
4878 return op0;
4880 if (maybe_lt (bitpos, 0))
4881 return NULL;
4883 if (GET_MODE (op0) == BLKmode || mode == BLKmode)
4884 return NULL;
4886 poly_int64 bytepos;
4887 if (multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
4888 && known_eq (bitsize, GET_MODE_BITSIZE (mode1)))
4890 machine_mode opmode = GET_MODE (op0);
4892 if (opmode == VOIDmode)
4893 opmode = TYPE_MODE (TREE_TYPE (tem));
4895 /* This condition may hold if we're expanding the address
4896 right past the end of an array that turned out not to
4897 be addressable (i.e., the address was only computed in
4898 debug stmts). The gen_subreg below would rightfully
4899 crash, and the address doesn't really exist, so just
4900 drop it. */
4901 if (known_ge (bitpos, GET_MODE_BITSIZE (opmode)))
4902 return NULL;
4904 if (multiple_p (bitpos, GET_MODE_BITSIZE (mode)))
4905 return simplify_gen_subreg (mode, op0, opmode, bytepos);
4908 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4909 && TYPE_UNSIGNED (TREE_TYPE (exp))
4910 ? SIGN_EXTRACT
4911 : ZERO_EXTRACT, mode,
4912 GET_MODE (op0) != VOIDmode
4913 ? GET_MODE (op0)
4914 : TYPE_MODE (TREE_TYPE (tem)),
4915 op0, gen_int_mode (bitsize, word_mode),
4916 gen_int_mode (bitpos, word_mode));
4919 case ABS_EXPR:
4920 case ABSU_EXPR:
4921 return simplify_gen_unary (ABS, mode, op0, mode);
4923 case NEGATE_EXPR:
4924 return simplify_gen_unary (NEG, mode, op0, mode);
4926 case BIT_NOT_EXPR:
4927 return simplify_gen_unary (NOT, mode, op0, mode);
4929 case FLOAT_EXPR:
4930 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4931 0)))
4932 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4933 inner_mode);
4935 case FIX_TRUNC_EXPR:
4936 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4937 inner_mode);
4939 case POINTER_PLUS_EXPR:
4940 /* For the rare target where pointers are not the same size as
4941 size_t, we need to check for mis-matched modes and correct
4942 the addend. */
4943 if (op0 && op1
4944 && is_a <scalar_int_mode> (GET_MODE (op0), &op0_mode)
4945 && is_a <scalar_int_mode> (GET_MODE (op1), &op1_mode)
4946 && op0_mode != op1_mode)
4948 if (GET_MODE_BITSIZE (op0_mode) < GET_MODE_BITSIZE (op1_mode)
4949 /* If OP0 is a partial mode, then we must truncate, even
4950 if it has the same bitsize as OP1 as GCC's
4951 representation of partial modes is opaque. */
4952 || (GET_MODE_CLASS (op0_mode) == MODE_PARTIAL_INT
4953 && (GET_MODE_BITSIZE (op0_mode)
4954 == GET_MODE_BITSIZE (op1_mode))))
4955 op1 = simplify_gen_unary (TRUNCATE, op0_mode, op1, op1_mode);
4956 else
4957 /* We always sign-extend, regardless of the signedness of
4958 the operand, because the operand is always unsigned
4959 here even if the original C expression is signed. */
4960 op1 = simplify_gen_unary (SIGN_EXTEND, op0_mode, op1, op1_mode);
4962 /* Fall through. */
4963 case PLUS_EXPR:
4964 return simplify_gen_binary (PLUS, mode, op0, op1);
4966 case MINUS_EXPR:
4967 case POINTER_DIFF_EXPR:
4968 return simplify_gen_binary (MINUS, mode, op0, op1);
4970 case MULT_EXPR:
4971 return simplify_gen_binary (MULT, mode, op0, op1);
4973 case RDIV_EXPR:
4974 case TRUNC_DIV_EXPR:
4975 case EXACT_DIV_EXPR:
4976 if (unsignedp)
4977 return simplify_gen_binary (UDIV, mode, op0, op1);
4978 else
4979 return simplify_gen_binary (DIV, mode, op0, op1);
4981 case TRUNC_MOD_EXPR:
4982 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4984 case FLOOR_DIV_EXPR:
4985 if (unsignedp)
4986 return simplify_gen_binary (UDIV, mode, op0, op1);
4987 else
4989 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4990 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4991 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4992 return simplify_gen_binary (PLUS, mode, div, adj);
4995 case FLOOR_MOD_EXPR:
4996 if (unsignedp)
4997 return simplify_gen_binary (UMOD, mode, op0, op1);
4998 else
5000 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5001 rtx adj = floor_sdiv_adjust (mode, mod, op1);
5002 adj = simplify_gen_unary (NEG, mode,
5003 simplify_gen_binary (MULT, mode, adj, op1),
5004 mode);
5005 return simplify_gen_binary (PLUS, mode, mod, adj);
5008 case CEIL_DIV_EXPR:
5009 if (unsignedp)
5011 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
5012 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5013 rtx adj = ceil_udiv_adjust (mode, mod, op1);
5014 return simplify_gen_binary (PLUS, mode, div, adj);
5016 else
5018 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
5019 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5020 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
5021 return simplify_gen_binary (PLUS, mode, div, adj);
5024 case CEIL_MOD_EXPR:
5025 if (unsignedp)
5027 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5028 rtx adj = ceil_udiv_adjust (mode, mod, op1);
5029 adj = simplify_gen_unary (NEG, mode,
5030 simplify_gen_binary (MULT, mode, adj, op1),
5031 mode);
5032 return simplify_gen_binary (PLUS, mode, mod, adj);
5034 else
5036 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5037 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
5038 adj = simplify_gen_unary (NEG, mode,
5039 simplify_gen_binary (MULT, mode, adj, op1),
5040 mode);
5041 return simplify_gen_binary (PLUS, mode, mod, adj);
5044 case ROUND_DIV_EXPR:
5045 if (unsignedp)
5047 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
5048 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5049 rtx adj = round_udiv_adjust (mode, mod, op1);
5050 return simplify_gen_binary (PLUS, mode, div, adj);
5052 else
5054 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
5055 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5056 rtx adj = round_sdiv_adjust (mode, mod, op1);
5057 return simplify_gen_binary (PLUS, mode, div, adj);
5060 case ROUND_MOD_EXPR:
5061 if (unsignedp)
5063 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
5064 rtx adj = round_udiv_adjust (mode, mod, op1);
5065 adj = simplify_gen_unary (NEG, mode,
5066 simplify_gen_binary (MULT, mode, adj, op1),
5067 mode);
5068 return simplify_gen_binary (PLUS, mode, mod, adj);
5070 else
5072 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
5073 rtx adj = round_sdiv_adjust (mode, mod, op1);
5074 adj = simplify_gen_unary (NEG, mode,
5075 simplify_gen_binary (MULT, mode, adj, op1),
5076 mode);
5077 return simplify_gen_binary (PLUS, mode, mod, adj);
5080 case LSHIFT_EXPR:
5081 return simplify_gen_binary (ASHIFT, mode, op0, op1);
5083 case RSHIFT_EXPR:
5084 if (unsignedp)
5085 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
5086 else
5087 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
5089 case LROTATE_EXPR:
5090 return simplify_gen_binary (ROTATE, mode, op0, op1);
5092 case RROTATE_EXPR:
5093 return simplify_gen_binary (ROTATERT, mode, op0, op1);
5095 case MIN_EXPR:
5096 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
5098 case MAX_EXPR:
5099 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
5101 case BIT_AND_EXPR:
5102 case TRUTH_AND_EXPR:
5103 return simplify_gen_binary (AND, mode, op0, op1);
5105 case BIT_IOR_EXPR:
5106 case TRUTH_OR_EXPR:
5107 return simplify_gen_binary (IOR, mode, op0, op1);
5109 case BIT_XOR_EXPR:
5110 case TRUTH_XOR_EXPR:
5111 return simplify_gen_binary (XOR, mode, op0, op1);
5113 case TRUTH_ANDIF_EXPR:
5114 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
5116 case TRUTH_ORIF_EXPR:
5117 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
5119 case TRUTH_NOT_EXPR:
5120 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
5122 case LT_EXPR:
5123 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
5124 op0, op1);
5126 case LE_EXPR:
5127 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
5128 op0, op1);
5130 case GT_EXPR:
5131 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
5132 op0, op1);
5134 case GE_EXPR:
5135 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
5136 op0, op1);
5138 case EQ_EXPR:
5139 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
5141 case NE_EXPR:
5142 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
5144 case UNORDERED_EXPR:
5145 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
5147 case ORDERED_EXPR:
5148 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
5150 case UNLT_EXPR:
5151 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
5153 case UNLE_EXPR:
5154 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
5156 case UNGT_EXPR:
5157 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
5159 case UNGE_EXPR:
5160 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
5162 case UNEQ_EXPR:
5163 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
5165 case LTGT_EXPR:
5166 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
5168 case COND_EXPR:
5169 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
5171 case COMPLEX_EXPR:
5172 gcc_assert (COMPLEX_MODE_P (mode));
5173 if (GET_MODE (op0) == VOIDmode)
5174 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
5175 if (GET_MODE (op1) == VOIDmode)
5176 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
5177 return gen_rtx_CONCAT (mode, op0, op1);
5179 case CONJ_EXPR:
5180 if (GET_CODE (op0) == CONCAT)
5181 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
5182 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
5183 XEXP (op0, 1),
5184 GET_MODE_INNER (mode)));
5185 else
5187 scalar_mode imode = GET_MODE_INNER (mode);
5188 rtx re, im;
5190 if (MEM_P (op0))
5192 re = adjust_address_nv (op0, imode, 0);
5193 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
5195 else
5197 scalar_int_mode ifmode;
5198 scalar_int_mode ihmode;
5199 rtx halfsize;
5200 if (!int_mode_for_mode (mode).exists (&ifmode)
5201 || !int_mode_for_mode (imode).exists (&ihmode))
5202 return NULL;
5203 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
5204 re = op0;
5205 if (mode != ifmode)
5206 re = gen_rtx_SUBREG (ifmode, re, 0);
5207 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
5208 if (imode != ihmode)
5209 re = gen_rtx_SUBREG (imode, re, 0);
5210 im = copy_rtx (op0);
5211 if (mode != ifmode)
5212 im = gen_rtx_SUBREG (ifmode, im, 0);
5213 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
5214 if (imode != ihmode)
5215 im = gen_rtx_SUBREG (imode, im, 0);
5217 im = gen_rtx_NEG (imode, im);
5218 return gen_rtx_CONCAT (mode, re, im);
5221 case ADDR_EXPR:
5222 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
5223 if (!op0 || !MEM_P (op0))
5225 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
5226 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
5227 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
5228 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
5229 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
5230 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
5232 if (handled_component_p (TREE_OPERAND (exp, 0)))
5234 poly_int64 bitoffset, bitsize, maxsize, byteoffset;
5235 bool reverse;
5236 tree decl
5237 = get_ref_base_and_extent (TREE_OPERAND (exp, 0), &bitoffset,
5238 &bitsize, &maxsize, &reverse);
5239 if ((VAR_P (decl)
5240 || TREE_CODE (decl) == PARM_DECL
5241 || TREE_CODE (decl) == RESULT_DECL)
5242 && (!TREE_ADDRESSABLE (decl)
5243 || target_for_debug_bind (decl))
5244 && multiple_p (bitoffset, BITS_PER_UNIT, &byteoffset)
5245 && known_gt (bitsize, 0)
5246 && known_eq (bitsize, maxsize))
5248 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
5249 return plus_constant (mode, base, byteoffset);
5253 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
5254 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5255 == ADDR_EXPR)
5257 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
5258 0));
5259 if (op0 != NULL
5260 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
5261 || (GET_CODE (op0) == PLUS
5262 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
5263 && CONST_INT_P (XEXP (op0, 1)))))
5265 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
5266 1));
5267 poly_int64 offset;
5268 if (!op1 || !poly_int_rtx_p (op1, &offset))
5269 return NULL;
5271 return plus_constant (mode, op0, offset);
5275 return NULL;
5278 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
5279 addr_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
5280 op0 = convert_debug_memory_address (addr_mode, XEXP (op0, 0), as);
5282 return op0;
5284 case VECTOR_CST:
5286 unsigned HOST_WIDE_INT i, nelts;
5288 if (!VECTOR_CST_NELTS (exp).is_constant (&nelts))
5289 return NULL;
5291 op0 = gen_rtx_CONCATN (mode, rtvec_alloc (nelts));
5293 for (i = 0; i < nelts; ++i)
5295 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
5296 if (!op1)
5297 return NULL;
5298 XVECEXP (op0, 0, i) = op1;
5301 return op0;
5304 case CONSTRUCTOR:
5305 if (TREE_CLOBBER_P (exp))
5306 return NULL;
5307 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
5309 unsigned i;
5310 unsigned HOST_WIDE_INT nelts;
5311 tree val;
5313 if (!TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)).is_constant (&nelts))
5314 goto flag_unsupported;
5316 op0 = gen_rtx_CONCATN (mode, rtvec_alloc (nelts));
5318 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
5320 op1 = expand_debug_expr (val);
5321 if (!op1)
5322 return NULL;
5323 XVECEXP (op0, 0, i) = op1;
5326 if (i < nelts)
5328 op1 = expand_debug_expr
5329 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
5331 if (!op1)
5332 return NULL;
5334 for (; i < nelts; i++)
5335 XVECEXP (op0, 0, i) = op1;
5338 return op0;
5340 else
5341 goto flag_unsupported;
5343 case CALL_EXPR:
5344 /* ??? Maybe handle some builtins? */
5345 return NULL;
5347 case SSA_NAME:
5349 gimple *g = get_gimple_for_ssa_name (exp);
5350 if (g)
5352 tree t = NULL_TREE;
5353 if (deep_ter_debug_map)
5355 tree *slot = deep_ter_debug_map->get (exp);
5356 if (slot)
5357 t = *slot;
5359 if (t == NULL_TREE)
5360 t = gimple_assign_rhs_to_tree (g);
5361 op0 = expand_debug_expr (t);
5362 if (!op0)
5363 return NULL;
5365 else
5367 /* If this is a reference to an incoming value of
5368 parameter that is never used in the code or where the
5369 incoming value is never used in the code, use
5370 PARM_DECL's DECL_RTL if set. */
5371 if (SSA_NAME_IS_DEFAULT_DEF (exp)
5372 && SSA_NAME_VAR (exp)
5373 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL
5374 && has_zero_uses (exp))
5376 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
5377 if (op0)
5378 goto adjust_mode;
5379 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
5380 if (op0)
5381 goto adjust_mode;
5384 int part = var_to_partition (SA.map, exp);
5386 if (part == NO_PARTITION)
5387 return NULL;
5389 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
5391 op0 = copy_rtx (SA.partition_to_pseudo[part]);
5393 goto adjust_mode;
5396 case ERROR_MARK:
5397 return NULL;
5399 /* Vector stuff. For most of the codes we don't have rtl codes. */
5400 case REALIGN_LOAD_EXPR:
5401 case VEC_COND_EXPR:
5402 case VEC_PACK_FIX_TRUNC_EXPR:
5403 case VEC_PACK_FLOAT_EXPR:
5404 case VEC_PACK_SAT_EXPR:
5405 case VEC_PACK_TRUNC_EXPR:
5406 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
5407 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
5408 case VEC_UNPACK_FLOAT_HI_EXPR:
5409 case VEC_UNPACK_FLOAT_LO_EXPR:
5410 case VEC_UNPACK_HI_EXPR:
5411 case VEC_UNPACK_LO_EXPR:
5412 case VEC_WIDEN_MULT_HI_EXPR:
5413 case VEC_WIDEN_MULT_LO_EXPR:
5414 case VEC_WIDEN_MULT_EVEN_EXPR:
5415 case VEC_WIDEN_MULT_ODD_EXPR:
5416 case VEC_WIDEN_LSHIFT_HI_EXPR:
5417 case VEC_WIDEN_LSHIFT_LO_EXPR:
5418 case VEC_PERM_EXPR:
5419 case VEC_DUPLICATE_EXPR:
5420 case VEC_SERIES_EXPR:
5421 case SAD_EXPR:
5422 return NULL;
5424 /* Misc codes. */
5425 case ADDR_SPACE_CONVERT_EXPR:
5426 case FIXED_CONVERT_EXPR:
5427 case OBJ_TYPE_REF:
5428 case WITH_SIZE_EXPR:
5429 case BIT_INSERT_EXPR:
5430 return NULL;
5432 case DOT_PROD_EXPR:
5433 if (SCALAR_INT_MODE_P (GET_MODE (op0))
5434 && SCALAR_INT_MODE_P (mode))
5437 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5438 0)))
5439 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
5440 inner_mode);
5442 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5443 1)))
5444 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
5445 inner_mode);
5446 op0 = simplify_gen_binary (MULT, mode, op0, op1);
5447 return simplify_gen_binary (PLUS, mode, op0, op2);
5449 return NULL;
5451 case WIDEN_MULT_EXPR:
5452 case WIDEN_MULT_PLUS_EXPR:
5453 case WIDEN_MULT_MINUS_EXPR:
5454 if (SCALAR_INT_MODE_P (GET_MODE (op0))
5455 && SCALAR_INT_MODE_P (mode))
5457 inner_mode = GET_MODE (op0);
5458 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
5459 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
5460 else
5461 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
5462 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
5463 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
5464 else
5465 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
5466 op0 = simplify_gen_binary (MULT, mode, op0, op1);
5467 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
5468 return op0;
5469 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
5470 return simplify_gen_binary (PLUS, mode, op0, op2);
5471 else
5472 return simplify_gen_binary (MINUS, mode, op2, op0);
5474 return NULL;
5476 case MULT_HIGHPART_EXPR:
5477 /* ??? Similar to the above. */
5478 return NULL;
5480 case WIDEN_SUM_EXPR:
5481 case WIDEN_LSHIFT_EXPR:
5482 if (SCALAR_INT_MODE_P (GET_MODE (op0))
5483 && SCALAR_INT_MODE_P (mode))
5486 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5487 0)))
5488 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
5489 inner_mode);
5490 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
5491 ? ASHIFT : PLUS, mode, op0, op1);
5493 return NULL;
5495 default:
5496 flag_unsupported:
5497 if (flag_checking)
5499 debug_tree (exp);
5500 gcc_unreachable ();
5502 return NULL;
5506 /* Return an RTX equivalent to the source bind value of the tree expression
5507 EXP. */
5509 static rtx
5510 expand_debug_source_expr (tree exp)
5512 rtx op0 = NULL_RTX;
5513 machine_mode mode = VOIDmode, inner_mode;
5515 switch (TREE_CODE (exp))
5517 case VAR_DECL:
5518 if (DECL_ABSTRACT_ORIGIN (exp))
5519 return expand_debug_source_expr (DECL_ABSTRACT_ORIGIN (exp));
5520 break;
5521 case PARM_DECL:
5523 mode = DECL_MODE (exp);
5524 op0 = expand_debug_parm_decl (exp);
5525 if (op0)
5526 break;
5527 /* See if this isn't an argument that has been completely
5528 optimized out. */
5529 if (!DECL_RTL_SET_P (exp)
5530 && !DECL_INCOMING_RTL (exp)
5531 && DECL_ABSTRACT_ORIGIN (current_function_decl))
5533 tree aexp = DECL_ORIGIN (exp);
5534 if (DECL_CONTEXT (aexp)
5535 == DECL_ABSTRACT_ORIGIN (current_function_decl))
5537 vec<tree, va_gc> **debug_args;
5538 unsigned int ix;
5539 tree ddecl;
5540 debug_args = decl_debug_args_lookup (current_function_decl);
5541 if (debug_args != NULL)
5543 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
5544 ix += 2)
5545 if (ddecl == aexp)
5546 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
5550 break;
5552 default:
5553 break;
5556 if (op0 == NULL_RTX)
5557 return NULL_RTX;
5559 inner_mode = GET_MODE (op0);
5560 if (mode == inner_mode)
5561 return op0;
5563 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
5565 if (GET_MODE_UNIT_BITSIZE (mode)
5566 == GET_MODE_UNIT_BITSIZE (inner_mode))
5567 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
5568 else if (GET_MODE_UNIT_BITSIZE (mode)
5569 < GET_MODE_UNIT_BITSIZE (inner_mode))
5570 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
5571 else
5572 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
5574 else if (FLOAT_MODE_P (mode))
5575 gcc_unreachable ();
5576 else if (FLOAT_MODE_P (inner_mode))
5578 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
5579 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
5580 else
5581 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
5583 else if (GET_MODE_UNIT_PRECISION (mode)
5584 == GET_MODE_UNIT_PRECISION (inner_mode))
5585 op0 = lowpart_subreg (mode, op0, inner_mode);
5586 else if (GET_MODE_UNIT_PRECISION (mode)
5587 < GET_MODE_UNIT_PRECISION (inner_mode))
5588 op0 = simplify_gen_unary (TRUNCATE, mode, op0, inner_mode);
5589 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
5590 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
5591 else
5592 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
5594 return op0;
5597 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
5598 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
5599 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
5601 static void
5602 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
5604 rtx exp = *exp_p;
5606 if (exp == NULL_RTX)
5607 return;
5609 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
5610 return;
5612 if (depth == 4)
5614 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
5615 rtx dval = make_debug_expr_from_rtl (exp);
5617 /* Emit a debug bind insn before INSN. */
5618 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
5619 DEBUG_EXPR_TREE_DECL (dval), exp,
5620 VAR_INIT_STATUS_INITIALIZED);
5622 emit_debug_insn_before (bind, insn);
5623 *exp_p = dval;
5624 return;
5627 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
5628 int i, j;
5629 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
5630 switch (*format_ptr++)
5632 case 'e':
5633 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
5634 break;
5636 case 'E':
5637 case 'V':
5638 for (j = 0; j < XVECLEN (exp, i); j++)
5639 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
5640 break;
5642 default:
5643 break;
5647 /* Expand the _LOCs in debug insns. We run this after expanding all
5648 regular insns, so that any variables referenced in the function
5649 will have their DECL_RTLs set. */
5651 static void
5652 expand_debug_locations (void)
5654 rtx_insn *insn;
5655 rtx_insn *last = get_last_insn ();
5656 int save_strict_alias = flag_strict_aliasing;
5658 /* New alias sets while setting up memory attributes cause
5659 -fcompare-debug failures, even though it doesn't bring about any
5660 codegen changes. */
5661 flag_strict_aliasing = 0;
5663 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5664 if (DEBUG_BIND_INSN_P (insn))
5666 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
5667 rtx val;
5668 rtx_insn *prev_insn, *insn2;
5669 machine_mode mode;
5671 if (value == NULL_TREE)
5672 val = NULL_RTX;
5673 else
5675 if (INSN_VAR_LOCATION_STATUS (insn)
5676 == VAR_INIT_STATUS_UNINITIALIZED)
5677 val = expand_debug_source_expr (value);
5678 /* The avoid_deep_ter_for_debug function inserts
5679 debug bind stmts after SSA_NAME definition, with the
5680 SSA_NAME as the whole bind location. Disable temporarily
5681 expansion of that SSA_NAME into the DEBUG_EXPR_DECL
5682 being defined in this DEBUG_INSN. */
5683 else if (deep_ter_debug_map && TREE_CODE (value) == SSA_NAME)
5685 tree *slot = deep_ter_debug_map->get (value);
5686 if (slot)
5688 if (*slot == INSN_VAR_LOCATION_DECL (insn))
5689 *slot = NULL_TREE;
5690 else
5691 slot = NULL;
5693 val = expand_debug_expr (value);
5694 if (slot)
5695 *slot = INSN_VAR_LOCATION_DECL (insn);
5697 else
5698 val = expand_debug_expr (value);
5699 gcc_assert (last == get_last_insn ());
5702 if (!val)
5703 val = gen_rtx_UNKNOWN_VAR_LOC ();
5704 else
5706 mode = GET_MODE (INSN_VAR_LOCATION (insn));
5708 gcc_assert (mode == GET_MODE (val)
5709 || (GET_MODE (val) == VOIDmode
5710 && (CONST_SCALAR_INT_P (val)
5711 || GET_CODE (val) == CONST_FIXED
5712 || GET_CODE (val) == LABEL_REF)));
5715 INSN_VAR_LOCATION_LOC (insn) = val;
5716 prev_insn = PREV_INSN (insn);
5717 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
5718 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
5721 flag_strict_aliasing = save_strict_alias;
5724 /* Performs swapping operands of commutative operations to expand
5725 the expensive one first. */
5727 static void
5728 reorder_operands (basic_block bb)
5730 unsigned int *lattice; /* Hold cost of each statement. */
5731 unsigned int i = 0, n = 0;
5732 gimple_stmt_iterator gsi;
5733 gimple_seq stmts;
5734 gimple *stmt;
5735 bool swap;
5736 tree op0, op1;
5737 ssa_op_iter iter;
5738 use_operand_p use_p;
5739 gimple *def0, *def1;
5741 /* Compute cost of each statement using estimate_num_insns. */
5742 stmts = bb_seq (bb);
5743 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5745 stmt = gsi_stmt (gsi);
5746 if (!is_gimple_debug (stmt))
5747 gimple_set_uid (stmt, n++);
5749 lattice = XNEWVEC (unsigned int, n);
5750 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5752 unsigned cost;
5753 stmt = gsi_stmt (gsi);
5754 if (is_gimple_debug (stmt))
5755 continue;
5756 cost = estimate_num_insns (stmt, &eni_size_weights);
5757 lattice[i] = cost;
5758 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
5760 tree use = USE_FROM_PTR (use_p);
5761 gimple *def_stmt;
5762 if (TREE_CODE (use) != SSA_NAME)
5763 continue;
5764 def_stmt = get_gimple_for_ssa_name (use);
5765 if (!def_stmt)
5766 continue;
5767 lattice[i] += lattice[gimple_uid (def_stmt)];
5769 i++;
5770 if (!is_gimple_assign (stmt)
5771 || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
5772 continue;
5773 op0 = gimple_op (stmt, 1);
5774 op1 = gimple_op (stmt, 2);
5775 if (TREE_CODE (op0) != SSA_NAME
5776 || TREE_CODE (op1) != SSA_NAME)
5777 continue;
5778 /* Swap operands if the second one is more expensive. */
5779 def0 = get_gimple_for_ssa_name (op0);
5780 def1 = get_gimple_for_ssa_name (op1);
5781 if (!def1)
5782 continue;
5783 swap = false;
5784 if (!def0 || lattice[gimple_uid (def1)] > lattice[gimple_uid (def0)])
5785 swap = true;
5786 if (swap)
5788 if (dump_file && (dump_flags & TDF_DETAILS))
5790 fprintf (dump_file, "Swap operands in stmt:\n");
5791 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5792 fprintf (dump_file, "Cost left opnd=%d, right opnd=%d\n",
5793 def0 ? lattice[gimple_uid (def0)] : 0,
5794 lattice[gimple_uid (def1)]);
5796 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
5797 gimple_assign_rhs2_ptr (stmt));
5800 XDELETE (lattice);
5803 /* Expand basic block BB from GIMPLE trees to RTL. */
5805 static basic_block
5806 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
5808 gimple_stmt_iterator gsi;
5809 gimple_seq stmts;
5810 gimple *stmt = NULL;
5811 rtx_note *note = NULL;
5812 rtx_insn *last;
5813 edge e;
5814 edge_iterator ei;
5815 bool nondebug_stmt_seen = false;
5817 if (dump_file)
5818 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
5819 bb->index);
5821 /* Note that since we are now transitioning from GIMPLE to RTL, we
5822 cannot use the gsi_*_bb() routines because they expect the basic
5823 block to be in GIMPLE, instead of RTL. Therefore, we need to
5824 access the BB sequence directly. */
5825 if (optimize)
5826 reorder_operands (bb);
5827 stmts = bb_seq (bb);
5828 bb->il.gimple.seq = NULL;
5829 bb->il.gimple.phi_nodes = NULL;
5830 rtl_profile_for_bb (bb);
5831 init_rtl_bb_info (bb);
5832 bb->flags |= BB_RTL;
5834 /* Remove the RETURN_EXPR if we may fall though to the exit
5835 instead. */
5836 gsi = gsi_last (stmts);
5837 if (!gsi_end_p (gsi)
5838 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
5840 greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
5842 gcc_assert (single_succ_p (bb));
5843 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
5845 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5846 && !gimple_return_retval (ret_stmt))
5848 gsi_remove (&gsi, false);
5849 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
5853 gsi = gsi_start (stmts);
5854 if (!gsi_end_p (gsi))
5856 stmt = gsi_stmt (gsi);
5857 if (gimple_code (stmt) != GIMPLE_LABEL)
5858 stmt = NULL;
5861 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
5863 if (stmt || elt)
5865 gcc_checking_assert (!note);
5866 last = get_last_insn ();
5868 if (stmt)
5870 expand_gimple_stmt (stmt);
5871 gsi_next (&gsi);
5874 if (elt)
5875 emit_label (*elt);
5877 BB_HEAD (bb) = NEXT_INSN (last);
5878 if (NOTE_P (BB_HEAD (bb)))
5879 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
5880 gcc_assert (LABEL_P (BB_HEAD (bb)));
5881 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
5883 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5885 else
5886 BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
5888 if (note)
5889 NOTE_BASIC_BLOCK (note) = bb;
5891 for (; !gsi_end_p (gsi); gsi_next (&gsi))
5893 basic_block new_bb;
5895 stmt = gsi_stmt (gsi);
5896 if (!is_gimple_debug (stmt))
5897 nondebug_stmt_seen = true;
5899 /* If this statement is a non-debug one, and we generate debug
5900 insns, then this one might be the last real use of a TERed
5901 SSA_NAME, but where there are still some debug uses further
5902 down. Expanding the current SSA name in such further debug
5903 uses by their RHS might lead to wrong debug info, as coalescing
5904 might make the operands of such RHS be placed into the same
5905 pseudo as something else. Like so:
5906 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
5907 use(a_1);
5908 a_2 = ...
5909 #DEBUG ... => a_1
5910 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
5911 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
5912 the write to a_2 would actually have clobbered the place which
5913 formerly held a_0.
5915 So, instead of that, we recognize the situation, and generate
5916 debug temporaries at the last real use of TERed SSA names:
5917 a_1 = a_0 + 1;
5918 #DEBUG #D1 => a_1
5919 use(a_1);
5920 a_2 = ...
5921 #DEBUG ... => #D1
5923 if (MAY_HAVE_DEBUG_BIND_INSNS
5924 && SA.values
5925 && !is_gimple_debug (stmt))
5927 ssa_op_iter iter;
5928 tree op;
5929 gimple *def;
5931 location_t sloc = curr_insn_location ();
5933 /* Look for SSA names that have their last use here (TERed
5934 names always have only one real use). */
5935 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5936 if ((def = get_gimple_for_ssa_name (op)))
5938 imm_use_iterator imm_iter;
5939 use_operand_p use_p;
5940 bool have_debug_uses = false;
5942 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5944 if (gimple_debug_bind_p (USE_STMT (use_p)))
5946 have_debug_uses = true;
5947 break;
5951 if (have_debug_uses)
5953 /* OP is a TERed SSA name, with DEF its defining
5954 statement, and where OP is used in further debug
5955 instructions. Generate a debug temporary, and
5956 replace all uses of OP in debug insns with that
5957 temporary. */
5958 gimple *debugstmt;
5959 tree value = gimple_assign_rhs_to_tree (def);
5960 tree vexpr = build_debug_expr_decl (TREE_TYPE (value));
5961 rtx val;
5962 machine_mode mode;
5964 set_curr_insn_location (gimple_location (def));
5966 if (DECL_P (value))
5967 mode = DECL_MODE (value);
5968 else
5969 mode = TYPE_MODE (TREE_TYPE (value));
5970 /* FIXME: Is setting the mode really necessary? */
5971 SET_DECL_MODE (vexpr, mode);
5973 val = gen_rtx_VAR_LOCATION
5974 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5976 emit_debug_insn (val);
5978 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5980 if (!gimple_debug_bind_p (debugstmt))
5981 continue;
5983 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5984 SET_USE (use_p, vexpr);
5986 update_stmt (debugstmt);
5990 set_curr_insn_location (sloc);
5993 currently_expanding_gimple_stmt = stmt;
5995 /* Expand this statement, then evaluate the resulting RTL and
5996 fixup the CFG accordingly. */
5997 if (gimple_code (stmt) == GIMPLE_COND)
5999 new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
6000 if (new_bb)
6002 currently_expanding_gimple_stmt = NULL;
6003 return new_bb;
6006 else if (is_gimple_debug (stmt))
6008 location_t sloc = curr_insn_location ();
6009 gimple_stmt_iterator nsi = gsi;
6011 for (;;)
6013 tree var;
6014 tree value = NULL_TREE;
6015 rtx val = NULL_RTX;
6016 machine_mode mode;
6018 if (!gimple_debug_nonbind_marker_p (stmt))
6020 if (gimple_debug_bind_p (stmt))
6022 var = gimple_debug_bind_get_var (stmt);
6024 if (TREE_CODE (var) != DEBUG_EXPR_DECL
6025 && TREE_CODE (var) != LABEL_DECL
6026 && !target_for_debug_bind (var))
6027 goto delink_debug_stmt;
6029 if (DECL_P (var) && !VECTOR_TYPE_P (TREE_TYPE (var)))
6030 mode = DECL_MODE (var);
6031 else
6032 mode = TYPE_MODE (TREE_TYPE (var));
6034 if (gimple_debug_bind_has_value_p (stmt))
6035 value = gimple_debug_bind_get_value (stmt);
6037 val = gen_rtx_VAR_LOCATION
6038 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
6040 else if (gimple_debug_source_bind_p (stmt))
6042 var = gimple_debug_source_bind_get_var (stmt);
6044 value = gimple_debug_source_bind_get_value (stmt);
6046 if (!VECTOR_TYPE_P (TREE_TYPE (var)))
6047 mode = DECL_MODE (var);
6048 else
6049 mode = TYPE_MODE (TREE_TYPE (var));
6051 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
6052 VAR_INIT_STATUS_UNINITIALIZED);
6054 else
6055 gcc_unreachable ();
6057 /* If this function was first compiled with markers
6058 enabled, but they're now disable (e.g. LTO), drop
6059 them on the floor. */
6060 else if (gimple_debug_nonbind_marker_p (stmt)
6061 && !MAY_HAVE_DEBUG_MARKER_INSNS)
6062 goto delink_debug_stmt;
6063 else if (gimple_debug_begin_stmt_p (stmt))
6064 val = GEN_RTX_DEBUG_MARKER_BEGIN_STMT_PAT ();
6065 else if (gimple_debug_inline_entry_p (stmt))
6066 val = GEN_RTX_DEBUG_MARKER_INLINE_ENTRY_PAT ();
6067 else
6068 gcc_unreachable ();
6070 last = get_last_insn ();
6072 set_curr_insn_location (gimple_location (stmt));
6074 emit_debug_insn (val);
6076 if (dump_file && (dump_flags & TDF_DETAILS))
6078 /* We can't dump the insn with a TREE where an RTX
6079 is expected. */
6080 if (GET_CODE (val) == VAR_LOCATION)
6082 gcc_checking_assert (PAT_VAR_LOCATION_LOC (val) == (rtx)value);
6083 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
6085 maybe_dump_rtl_for_gimple_stmt (stmt, last);
6086 if (GET_CODE (val) == VAR_LOCATION)
6087 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
6090 delink_debug_stmt:
6091 /* In order not to generate too many debug temporaries,
6092 we delink all uses of debug statements we already expanded.
6093 Therefore debug statements between definition and real
6094 use of TERed SSA names will continue to use the SSA name,
6095 and not be replaced with debug temps. */
6096 delink_stmt_imm_use (stmt);
6098 gsi = nsi;
6099 gsi_next (&nsi);
6100 if (gsi_end_p (nsi))
6101 break;
6102 stmt = gsi_stmt (nsi);
6103 if (!is_gimple_debug (stmt))
6104 break;
6107 set_curr_insn_location (sloc);
6109 else
6111 gcall *call_stmt = dyn_cast <gcall *> (stmt);
6112 if (call_stmt
6113 && gimple_call_tail_p (call_stmt)
6114 && disable_tail_calls)
6115 gimple_call_set_tail (call_stmt, false);
6117 if (call_stmt && gimple_call_tail_p (call_stmt))
6119 bool can_fallthru;
6120 new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
6121 if (new_bb)
6123 if (can_fallthru)
6124 bb = new_bb;
6125 else
6127 currently_expanding_gimple_stmt = NULL;
6128 return new_bb;
6132 else
6134 def_operand_p def_p;
6135 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
6137 if (def_p != NULL)
6139 /* Ignore this stmt if it is in the list of
6140 replaceable expressions. */
6141 if (SA.values
6142 && bitmap_bit_p (SA.values,
6143 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
6144 continue;
6146 last = expand_gimple_stmt (stmt);
6147 maybe_dump_rtl_for_gimple_stmt (stmt, last);
6152 currently_expanding_gimple_stmt = NULL;
6154 /* Expand implicit goto and convert goto_locus. */
6155 FOR_EACH_EDGE (e, ei, bb->succs)
6157 if (e->goto_locus != UNKNOWN_LOCATION || !nondebug_stmt_seen)
6158 set_curr_insn_location (e->goto_locus);
6159 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
6161 emit_jump (label_rtx_for_bb (e->dest));
6162 e->flags &= ~EDGE_FALLTHRU;
6166 /* Expanded RTL can create a jump in the last instruction of block.
6167 This later might be assumed to be a jump to successor and break edge insertion.
6168 We need to insert dummy move to prevent this. PR41440. */
6169 if (single_succ_p (bb)
6170 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
6171 && (last = get_last_insn ())
6172 && (JUMP_P (last)
6173 || (DEBUG_INSN_P (last)
6174 && JUMP_P (prev_nondebug_insn (last)))))
6176 rtx dummy = gen_reg_rtx (SImode);
6177 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
6180 do_pending_stack_adjust ();
6182 /* Find the block tail. The last insn in the block is the insn
6183 before a barrier and/or table jump insn. */
6184 last = get_last_insn ();
6185 if (BARRIER_P (last))
6186 last = PREV_INSN (last);
6187 if (JUMP_TABLE_DATA_P (last))
6188 last = PREV_INSN (PREV_INSN (last));
6189 if (BARRIER_P (last))
6190 last = PREV_INSN (last);
6191 BB_END (bb) = last;
6193 update_bb_for_insn (bb);
6195 return bb;
6199 /* Create a basic block for initialization code. */
6201 static basic_block
6202 construct_init_block (void)
6204 basic_block init_block, first_block;
6205 edge e = NULL;
6206 int flags;
6208 /* Multiple entry points not supported yet. */
6209 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
6210 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6211 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
6212 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
6213 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
6215 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
6217 /* When entry edge points to first basic block, we don't need jump,
6218 otherwise we have to jump into proper target. */
6219 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
6221 tree label = gimple_block_label (e->dest);
6223 emit_jump (jump_target_rtx (label));
6224 flags = 0;
6226 else
6227 flags = EDGE_FALLTHRU;
6229 init_block = create_basic_block (NEXT_INSN (get_insns ()),
6230 get_last_insn (),
6231 ENTRY_BLOCK_PTR_FOR_FN (cfun));
6232 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
6233 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
6234 if (e)
6236 first_block = e->dest;
6237 redirect_edge_succ (e, init_block);
6238 make_single_succ_edge (init_block, first_block, flags);
6240 else
6241 make_single_succ_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun),
6242 EDGE_FALLTHRU);
6244 update_bb_for_insn (init_block);
6245 return init_block;
6248 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
6249 found in the block tree. */
6251 static void
6252 set_block_levels (tree block, int level)
6254 while (block)
6256 BLOCK_NUMBER (block) = level;
6257 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
6258 block = BLOCK_CHAIN (block);
6262 /* Create a block containing landing pads and similar stuff. */
6264 static void
6265 construct_exit_block (void)
6267 rtx_insn *head = get_last_insn ();
6268 rtx_insn *end;
6269 basic_block exit_block;
6270 edge e, e2;
6271 unsigned ix;
6272 edge_iterator ei;
6273 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
6274 rtx_insn *orig_end = BB_END (prev_bb);
6276 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
6278 /* Make sure the locus is set to the end of the function, so that
6279 epilogue line numbers and warnings are set properly. */
6280 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
6281 input_location = cfun->function_end_locus;
6283 /* Generate rtl for function exit. */
6284 expand_function_end ();
6286 end = get_last_insn ();
6287 if (head == end)
6288 return;
6289 /* While emitting the function end we could move end of the last basic
6290 block. */
6291 BB_END (prev_bb) = orig_end;
6292 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
6293 head = NEXT_INSN (head);
6294 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
6295 bb count counting will be confused. Any instructions before that
6296 label are emitted for the case where PREV_BB falls through into the
6297 exit block, so append those instructions to prev_bb in that case. */
6298 if (NEXT_INSN (head) != return_label)
6300 while (NEXT_INSN (head) != return_label)
6302 if (!NOTE_P (NEXT_INSN (head)))
6303 BB_END (prev_bb) = NEXT_INSN (head);
6304 head = NEXT_INSN (head);
6307 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
6308 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
6309 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
6311 ix = 0;
6312 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
6314 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
6315 if (!(e->flags & EDGE_ABNORMAL))
6316 redirect_edge_succ (e, exit_block);
6317 else
6318 ix++;
6321 e = make_single_succ_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun),
6322 EDGE_FALLTHRU);
6323 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6324 if (e2 != e)
6326 exit_block->count -= e2->count ();
6328 update_bb_for_insn (exit_block);
6331 /* Helper function for discover_nonconstant_array_refs.
6332 Look for ARRAY_REF nodes with non-constant indexes and mark them
6333 addressable. */
6335 static tree
6336 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
6337 void *data)
6339 tree t = *tp;
6340 bitmap forced_stack_vars = (bitmap)((walk_stmt_info *)data)->info;
6342 if (IS_TYPE_OR_DECL_P (t))
6343 *walk_subtrees = 0;
6344 else if (REFERENCE_CLASS_P (t) && TREE_THIS_VOLATILE (t))
6346 t = get_base_address (t);
6347 if (t && DECL_P (t)
6348 && DECL_MODE (t) != BLKmode
6349 && !TREE_ADDRESSABLE (t))
6350 bitmap_set_bit (forced_stack_vars, DECL_UID (t));
6351 *walk_subtrees = 0;
6353 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6355 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6356 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
6357 && (!TREE_OPERAND (t, 2)
6358 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
6359 || (TREE_CODE (t) == COMPONENT_REF
6360 && (!TREE_OPERAND (t,2)
6361 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
6362 || TREE_CODE (t) == BIT_FIELD_REF
6363 || TREE_CODE (t) == REALPART_EXPR
6364 || TREE_CODE (t) == IMAGPART_EXPR
6365 || TREE_CODE (t) == VIEW_CONVERT_EXPR
6366 || CONVERT_EXPR_P (t))
6367 t = TREE_OPERAND (t, 0);
6369 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6371 t = get_base_address (t);
6372 if (t && DECL_P (t)
6373 && DECL_MODE (t) != BLKmode
6374 && !TREE_ADDRESSABLE (t))
6375 bitmap_set_bit (forced_stack_vars, DECL_UID (t));
6378 *walk_subtrees = 0;
6380 /* References of size POLY_INT_CST to a fixed-size object must go
6381 through memory. It's more efficient to force that here than
6382 to create temporary slots on the fly.
6383 RTL expansion expectes TARGET_MEM_REF to always address actual memory. */
6384 else if (TREE_CODE (t) == TARGET_MEM_REF
6385 || (TREE_CODE (t) == MEM_REF
6386 && TYPE_SIZE (TREE_TYPE (t))
6387 && POLY_INT_CST_P (TYPE_SIZE (TREE_TYPE (t)))))
6389 tree base = get_base_address (t);
6390 if (base
6391 && DECL_P (base)
6392 && !TREE_ADDRESSABLE (base)
6393 && DECL_MODE (base) != BLKmode
6394 && GET_MODE_SIZE (DECL_MODE (base)).is_constant ())
6395 bitmap_set_bit (forced_stack_vars, DECL_UID (base));
6396 *walk_subtrees = 0;
6399 return NULL_TREE;
6402 /* If there's a chance to get a pseudo for t then if it would be of float mode
6403 and the actual access is via an integer mode (lowered memcpy or similar
6404 access) then avoid the register expansion if the mode likely is not storage
6405 suitable for raw bits processing (like XFmode on i?86). */
6407 static void
6408 avoid_type_punning_on_regs (tree t, bitmap forced_stack_vars)
6410 machine_mode access_mode = TYPE_MODE (TREE_TYPE (t));
6411 if (access_mode != BLKmode
6412 && !SCALAR_INT_MODE_P (access_mode))
6413 return;
6414 tree base = get_base_address (t);
6415 if (DECL_P (base)
6416 && !TREE_ADDRESSABLE (base)
6417 && FLOAT_MODE_P (DECL_MODE (base))
6418 && maybe_lt (GET_MODE_PRECISION (DECL_MODE (base)),
6419 GET_MODE_BITSIZE (GET_MODE_INNER (DECL_MODE (base))))
6420 /* Double check in the expensive way we really would get a pseudo. */
6421 && use_register_for_decl (base))
6422 bitmap_set_bit (forced_stack_vars, DECL_UID (base));
6425 /* RTL expansion is not able to compile array references with variable
6426 offsets for arrays stored in single register. Discover such
6427 expressions and mark variables as addressable to avoid this
6428 scenario. */
6430 static void
6431 discover_nonconstant_array_refs (bitmap forced_stack_vars)
6433 basic_block bb;
6434 gimple_stmt_iterator gsi;
6436 walk_stmt_info wi = {};
6437 wi.info = forced_stack_vars;
6438 FOR_EACH_BB_FN (bb, cfun)
6439 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6441 gimple *stmt = gsi_stmt (gsi);
6442 if (!is_gimple_debug (stmt))
6444 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, &wi);
6445 gcall *call = dyn_cast <gcall *> (stmt);
6446 if (call && gimple_call_internal_p (call))
6448 tree cand = NULL_TREE;
6449 switch (gimple_call_internal_fn (call))
6451 case IFN_LOAD_LANES:
6452 /* The source must be a MEM. */
6453 cand = gimple_call_arg (call, 0);
6454 break;
6455 case IFN_STORE_LANES:
6456 /* The destination must be a MEM. */
6457 cand = gimple_call_lhs (call);
6458 break;
6459 default:
6460 break;
6462 if (cand)
6463 cand = get_base_address (cand);
6464 if (cand
6465 && DECL_P (cand)
6466 && use_register_for_decl (cand))
6467 bitmap_set_bit (forced_stack_vars, DECL_UID (cand));
6469 if (gimple_vdef (stmt))
6471 tree t = gimple_get_lhs (stmt);
6472 if (t && REFERENCE_CLASS_P (t))
6473 avoid_type_punning_on_regs (t, forced_stack_vars);
6479 /* This function sets crtl->args.internal_arg_pointer to a virtual
6480 register if DRAP is needed. Local register allocator will replace
6481 virtual_incoming_args_rtx with the virtual register. */
6483 static void
6484 expand_stack_alignment (void)
6486 rtx drap_rtx;
6487 unsigned int preferred_stack_boundary;
6489 if (! SUPPORTS_STACK_ALIGNMENT)
6490 return;
6492 if (cfun->calls_alloca
6493 || cfun->has_nonlocal_label
6494 || crtl->has_nonlocal_goto)
6495 crtl->need_drap = true;
6497 /* Call update_stack_boundary here again to update incoming stack
6498 boundary. It may set incoming stack alignment to a different
6499 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
6500 use the minimum incoming stack alignment to check if it is OK
6501 to perform sibcall optimization since sibcall optimization will
6502 only align the outgoing stack to incoming stack boundary. */
6503 if (targetm.calls.update_stack_boundary)
6504 targetm.calls.update_stack_boundary ();
6506 /* The incoming stack frame has to be aligned at least at
6507 parm_stack_boundary. */
6508 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
6510 /* Update crtl->stack_alignment_estimated and use it later to align
6511 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
6512 exceptions since callgraph doesn't collect incoming stack alignment
6513 in this case. */
6514 if (cfun->can_throw_non_call_exceptions
6515 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
6516 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
6517 else
6518 preferred_stack_boundary = crtl->preferred_stack_boundary;
6519 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
6520 crtl->stack_alignment_estimated = preferred_stack_boundary;
6521 if (preferred_stack_boundary > crtl->stack_alignment_needed)
6522 crtl->stack_alignment_needed = preferred_stack_boundary;
6524 gcc_assert (crtl->stack_alignment_needed
6525 <= crtl->stack_alignment_estimated);
6527 crtl->stack_realign_needed
6528 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
6529 crtl->stack_realign_tried = crtl->stack_realign_needed;
6531 crtl->stack_realign_processed = true;
6533 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
6534 alignment. */
6535 gcc_assert (targetm.calls.get_drap_rtx != NULL);
6536 drap_rtx = targetm.calls.get_drap_rtx ();
6538 /* stack_realign_drap and drap_rtx must match. */
6539 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
6541 /* Do nothing if NULL is returned, which means DRAP is not needed. */
6542 if (drap_rtx != NULL)
6544 crtl->args.internal_arg_pointer = drap_rtx;
6546 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
6547 needed. */
6548 fixup_tail_calls ();
6553 static void
6554 expand_main_function (void)
6556 #if (defined(INVOKE__main) \
6557 || (!defined(HAS_INIT_SECTION) \
6558 && !defined(INIT_SECTION_ASM_OP) \
6559 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
6560 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode);
6561 #endif
6565 /* Expand code to initialize the stack_protect_guard. This is invoked at
6566 the beginning of a function to be protected. */
6568 static void
6569 stack_protect_prologue (void)
6571 tree guard_decl = targetm.stack_protect_guard ();
6572 rtx x, y;
6574 crtl->stack_protect_guard_decl = guard_decl;
6575 x = expand_normal (crtl->stack_protect_guard);
6577 if (targetm.have_stack_protect_combined_set () && guard_decl)
6579 gcc_assert (DECL_P (guard_decl));
6580 y = DECL_RTL (guard_decl);
6582 /* Allow the target to compute address of Y and copy it to X without
6583 leaking Y into a register. This combined address + copy pattern
6584 allows the target to prevent spilling of any intermediate results by
6585 splitting it after register allocator. */
6586 if (rtx_insn *insn = targetm.gen_stack_protect_combined_set (x, y))
6588 emit_insn (insn);
6589 return;
6593 if (guard_decl)
6594 y = expand_normal (guard_decl);
6595 else
6596 y = const0_rtx;
6598 /* Allow the target to copy from Y to X without leaking Y into a
6599 register. */
6600 if (targetm.have_stack_protect_set ())
6601 if (rtx_insn *insn = targetm.gen_stack_protect_set (x, y))
6603 emit_insn (insn);
6604 return;
6607 /* Otherwise do a straight move. */
6608 emit_move_insn (x, y);
6611 /* Translate the intermediate representation contained in the CFG
6612 from GIMPLE trees to RTL.
6614 We do conversion per basic block and preserve/update the tree CFG.
6615 This implies we have to do some magic as the CFG can simultaneously
6616 consist of basic blocks containing RTL and GIMPLE trees. This can
6617 confuse the CFG hooks, so be careful to not manipulate CFG during
6618 the expansion. */
6620 namespace {
6622 const pass_data pass_data_expand =
6624 RTL_PASS, /* type */
6625 "expand", /* name */
6626 OPTGROUP_NONE, /* optinfo_flags */
6627 TV_EXPAND, /* tv_id */
6628 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
6629 | PROP_gimple_lcx
6630 | PROP_gimple_lvec
6631 | PROP_gimple_lva), /* properties_required */
6632 PROP_rtl, /* properties_provided */
6633 ( PROP_ssa | PROP_gimple ), /* properties_destroyed */
6634 0, /* todo_flags_start */
6635 0, /* todo_flags_finish */
6638 class pass_expand : public rtl_opt_pass
6640 public:
6641 pass_expand (gcc::context *ctxt)
6642 : rtl_opt_pass (pass_data_expand, ctxt)
6645 /* opt_pass methods: */
6646 unsigned int execute (function *) final override;
6648 }; // class pass_expand
6650 unsigned int
6651 pass_expand::execute (function *fun)
6653 basic_block bb, init_block;
6654 edge_iterator ei;
6655 edge e;
6656 rtx_insn *var_seq, *var_ret_seq;
6657 unsigned i;
6659 timevar_push (TV_OUT_OF_SSA);
6660 rewrite_out_of_ssa (&SA);
6661 timevar_pop (TV_OUT_OF_SSA);
6662 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
6664 if (MAY_HAVE_DEBUG_BIND_STMTS && flag_tree_ter)
6666 gimple_stmt_iterator gsi;
6667 FOR_EACH_BB_FN (bb, cfun)
6668 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6669 if (gimple_debug_bind_p (gsi_stmt (gsi)))
6670 avoid_deep_ter_for_debug (gsi_stmt (gsi), 0);
6673 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
6674 auto_bitmap forced_stack_vars;
6675 discover_nonconstant_array_refs (forced_stack_vars);
6677 /* Make sure all values used by the optimization passes have sane
6678 defaults. */
6679 reg_renumber = 0;
6681 /* Some backends want to know that we are expanding to RTL. */
6682 currently_expanding_to_rtl = 1;
6683 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
6684 free_dominance_info (CDI_DOMINATORS);
6686 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
6688 insn_locations_init ();
6689 if (!DECL_IS_UNDECLARED_BUILTIN (current_function_decl))
6691 /* Eventually, all FEs should explicitly set function_start_locus. */
6692 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
6693 set_curr_insn_location
6694 (DECL_SOURCE_LOCATION (current_function_decl));
6695 else
6696 set_curr_insn_location (fun->function_start_locus);
6698 else
6699 set_curr_insn_location (UNKNOWN_LOCATION);
6700 prologue_location = curr_insn_location ();
6702 #ifdef INSN_SCHEDULING
6703 init_sched_attrs ();
6704 #endif
6706 /* Make sure first insn is a note even if we don't want linenums.
6707 This makes sure the first insn will never be deleted.
6708 Also, final expects a note to appear there. */
6709 emit_note (NOTE_INSN_DELETED);
6711 targetm.expand_to_rtl_hook ();
6712 crtl->init_stack_alignment ();
6713 fun->cfg->max_jumptable_ents = 0;
6715 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
6716 of the function section at exapnsion time to predict distance of calls. */
6717 resolve_unique_section (current_function_decl, 0, flag_function_sections);
6719 /* Expand the variables recorded during gimple lowering. */
6720 timevar_push (TV_VAR_EXPAND);
6721 start_sequence ();
6723 var_ret_seq = expand_used_vars (forced_stack_vars);
6725 var_seq = get_insns ();
6726 end_sequence ();
6727 timevar_pop (TV_VAR_EXPAND);
6729 /* Honor stack protection warnings. */
6730 if (warn_stack_protect)
6732 if (fun->calls_alloca)
6733 warning (OPT_Wstack_protector,
6734 "stack protector not protecting local variables: "
6735 "variable length buffer");
6736 if (has_short_buffer && !crtl->stack_protect_guard)
6737 warning (OPT_Wstack_protector,
6738 "stack protector not protecting function: "
6739 "all local arrays are less than %d bytes long",
6740 (int) param_ssp_buffer_size);
6743 /* Temporarily mark PARM_DECLs and RESULT_DECLs we need to expand to
6744 memory addressable so expand_function_start can emit the required
6745 copies. */
6746 auto_vec<tree, 16> marked_parms;
6747 for (tree parm = DECL_ARGUMENTS (current_function_decl); parm;
6748 parm = DECL_CHAIN (parm))
6749 if (!TREE_ADDRESSABLE (parm)
6750 && bitmap_bit_p (forced_stack_vars, DECL_UID (parm)))
6752 TREE_ADDRESSABLE (parm) = 1;
6753 marked_parms.safe_push (parm);
6755 if (DECL_RESULT (current_function_decl)
6756 && !TREE_ADDRESSABLE (DECL_RESULT (current_function_decl))
6757 && bitmap_bit_p (forced_stack_vars,
6758 DECL_UID (DECL_RESULT (current_function_decl))))
6760 TREE_ADDRESSABLE (DECL_RESULT (current_function_decl)) = 1;
6761 marked_parms.safe_push (DECL_RESULT (current_function_decl));
6764 /* Set up parameters and prepare for return, for the function. */
6765 expand_function_start (current_function_decl);
6767 /* Clear TREE_ADDRESSABLE again. */
6768 while (!marked_parms.is_empty ())
6769 TREE_ADDRESSABLE (marked_parms.pop ()) = 0;
6771 /* If we emitted any instructions for setting up the variables,
6772 emit them before the FUNCTION_START note. */
6773 if (var_seq)
6775 emit_insn_before (var_seq, parm_birth_insn);
6777 /* In expand_function_end we'll insert the alloca save/restore
6778 before parm_birth_insn. We've just insertted an alloca call.
6779 Adjust the pointer to match. */
6780 parm_birth_insn = var_seq;
6783 /* Now propagate the RTL assignment of each partition to the
6784 underlying var of each SSA_NAME. */
6785 tree name;
6787 FOR_EACH_SSA_NAME (i, name, cfun)
6789 /* We might have generated new SSA names in
6790 update_alias_info_with_stack_vars. They will have a NULL
6791 defining statements, and won't be part of the partitioning,
6792 so ignore those. */
6793 if (!SSA_NAME_DEF_STMT (name))
6794 continue;
6796 adjust_one_expanded_partition_var (name);
6799 /* Clean up RTL of variables that straddle across multiple
6800 partitions, and check that the rtl of any PARM_DECLs that are not
6801 cleaned up is that of their default defs. */
6802 FOR_EACH_SSA_NAME (i, name, cfun)
6804 int part;
6806 /* We might have generated new SSA names in
6807 update_alias_info_with_stack_vars. They will have a NULL
6808 defining statements, and won't be part of the partitioning,
6809 so ignore those. */
6810 if (!SSA_NAME_DEF_STMT (name))
6811 continue;
6812 part = var_to_partition (SA.map, name);
6813 if (part == NO_PARTITION)
6814 continue;
6816 /* If this decl was marked as living in multiple places, reset
6817 this now to NULL. */
6818 tree var = SSA_NAME_VAR (name);
6819 if (var && DECL_RTL_IF_SET (var) == pc_rtx)
6820 SET_DECL_RTL (var, NULL);
6821 /* Check that the pseudos chosen by assign_parms are those of
6822 the corresponding default defs. */
6823 else if (SSA_NAME_IS_DEFAULT_DEF (name)
6824 && (TREE_CODE (var) == PARM_DECL
6825 || TREE_CODE (var) == RESULT_DECL))
6827 rtx in = DECL_RTL_IF_SET (var);
6828 gcc_assert (in);
6829 rtx out = SA.partition_to_pseudo[part];
6830 gcc_assert (in == out);
6832 /* Now reset VAR's RTL to IN, so that the _EXPR attrs match
6833 those expected by debug backends for each parm and for
6834 the result. This is particularly important for stabs,
6835 whose register elimination from parm's DECL_RTL may cause
6836 -fcompare-debug differences as SET_DECL_RTL changes reg's
6837 attrs. So, make sure the RTL already has the parm as the
6838 EXPR, so that it won't change. */
6839 SET_DECL_RTL (var, NULL_RTX);
6840 if (MEM_P (in))
6841 set_mem_attributes (in, var, true);
6842 SET_DECL_RTL (var, in);
6846 /* If this function is `main', emit a call to `__main'
6847 to run global initializers, etc. */
6848 if (DECL_NAME (current_function_decl)
6849 && MAIN_NAME_P (DECL_NAME (current_function_decl))
6850 && DECL_FILE_SCOPE_P (current_function_decl))
6851 expand_main_function ();
6853 /* Initialize the stack_protect_guard field. This must happen after the
6854 call to __main (if any) so that the external decl is initialized. */
6855 if (crtl->stack_protect_guard && targetm.stack_protect_runtime_enabled_p ())
6856 stack_protect_prologue ();
6858 expand_phi_nodes (&SA);
6860 /* Release any stale SSA redirection data. */
6861 redirect_edge_var_map_empty ();
6863 /* Register rtl specific functions for cfg. */
6864 rtl_register_cfg_hooks ();
6866 init_block = construct_init_block ();
6868 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
6869 remaining edges later. */
6870 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
6871 e->flags &= ~EDGE_EXECUTABLE;
6873 /* If the function has too many markers, drop them while expanding. */
6874 if (cfun->debug_marker_count
6875 >= param_max_debug_marker_count)
6876 cfun->debug_nonbind_markers = false;
6878 lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
6879 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
6880 next_bb)
6881 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
6883 if (MAY_HAVE_DEBUG_BIND_INSNS)
6884 expand_debug_locations ();
6886 if (deep_ter_debug_map)
6888 delete deep_ter_debug_map;
6889 deep_ter_debug_map = NULL;
6892 /* Free stuff we no longer need after GIMPLE optimizations. */
6893 free_dominance_info (CDI_DOMINATORS);
6894 free_dominance_info (CDI_POST_DOMINATORS);
6895 delete_tree_cfg_annotations (fun);
6897 timevar_push (TV_OUT_OF_SSA);
6898 finish_out_of_ssa (&SA);
6899 timevar_pop (TV_OUT_OF_SSA);
6901 timevar_push (TV_POST_EXPAND);
6902 /* We are no longer in SSA form. */
6903 fun->gimple_df->in_ssa_p = false;
6904 loops_state_clear (LOOP_CLOSED_SSA);
6906 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
6907 conservatively to true until they are all profile aware. */
6908 delete lab_rtx_for_bb;
6909 free_histograms (fun);
6911 construct_exit_block ();
6912 insn_locations_finalize ();
6914 if (var_ret_seq)
6916 rtx_insn *after = return_label;
6917 rtx_insn *next = NEXT_INSN (after);
6918 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
6919 after = next;
6920 emit_insn_after (var_ret_seq, after);
6923 if (hwasan_sanitize_stack_p ())
6924 hwasan_maybe_emit_frame_base_init ();
6926 /* Zap the tree EH table. */
6927 set_eh_throw_stmt_table (fun, NULL);
6929 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
6930 split edges which edge insertions might do. */
6931 rebuild_jump_labels (get_insns ());
6933 /* If we have a single successor to the entry block, put the pending insns
6934 after parm birth, but before NOTE_INSNS_FUNCTION_BEG. */
6935 if (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
6937 edge e = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fun));
6938 if (e->insns.r)
6940 rtx_insn *insns = e->insns.r;
6941 e->insns.r = NULL;
6942 rebuild_jump_labels_chain (insns);
6943 if (NOTE_P (parm_birth_insn)
6944 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
6945 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
6946 else
6947 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
6951 /* Otherwise, as well as for other edges, take the usual way. */
6952 commit_edge_insertions ();
6954 /* We're done expanding trees to RTL. */
6955 currently_expanding_to_rtl = 0;
6957 flush_mark_addressable_queue ();
6959 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
6960 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6962 edge e;
6963 edge_iterator ei;
6964 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6966 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
6967 e->flags &= ~EDGE_EXECUTABLE;
6969 /* At the moment not all abnormal edges match the RTL
6970 representation. It is safe to remove them here as
6971 find_many_sub_basic_blocks will rediscover them.
6972 In the future we should get this fixed properly. */
6973 if ((e->flags & EDGE_ABNORMAL)
6974 && !(e->flags & EDGE_SIBCALL))
6975 remove_edge (e);
6976 else
6977 ei_next (&ei);
6981 auto_sbitmap blocks (last_basic_block_for_fn (fun));
6982 bitmap_ones (blocks);
6983 find_many_sub_basic_blocks (blocks);
6984 purge_all_dead_edges ();
6986 /* After initial rtl generation, call back to finish generating
6987 exception support code. We need to do this before cleaning up
6988 the CFG as the code does not expect dead landing pads. */
6989 if (fun->eh->region_tree != NULL)
6990 finish_eh_generation ();
6992 /* Call expand_stack_alignment after finishing all
6993 updates to crtl->preferred_stack_boundary. */
6994 expand_stack_alignment ();
6996 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
6997 function. */
6998 if (crtl->tail_call_emit)
6999 fixup_tail_calls ();
7001 HOST_WIDE_INT patch_area_size, patch_area_entry;
7002 parse_and_check_patch_area (flag_patchable_function_entry, false,
7003 &patch_area_size, &patch_area_entry);
7005 tree patchable_function_entry_attr
7006 = lookup_attribute ("patchable_function_entry",
7007 DECL_ATTRIBUTES (cfun->decl));
7008 if (patchable_function_entry_attr)
7010 tree pp_val = TREE_VALUE (patchable_function_entry_attr);
7011 tree patchable_function_entry_value1 = TREE_VALUE (pp_val);
7013 patch_area_size = tree_to_uhwi (patchable_function_entry_value1);
7014 patch_area_entry = 0;
7015 if (TREE_CHAIN (pp_val) != NULL_TREE)
7017 tree patchable_function_entry_value2
7018 = TREE_VALUE (TREE_CHAIN (pp_val));
7019 patch_area_entry = tree_to_uhwi (patchable_function_entry_value2);
7023 if (patch_area_entry > patch_area_size)
7025 if (patch_area_size > 0)
7026 warning (OPT_Wattributes,
7027 "patchable function entry %wu exceeds size %wu",
7028 patch_area_entry, patch_area_size);
7029 patch_area_entry = 0;
7032 crtl->patch_area_size = patch_area_size;
7033 crtl->patch_area_entry = patch_area_entry;
7035 /* BB subdivision may have created basic blocks that are only reachable
7036 from unlikely bbs but not marked as such in the profile. */
7037 if (optimize)
7038 propagate_unlikely_bbs_forward ();
7040 /* Remove unreachable blocks, otherwise we cannot compute dominators
7041 which are needed for loop state verification. As a side-effect
7042 this also compacts blocks.
7043 ??? We cannot remove trivially dead insns here as for example
7044 the DRAP reg on i?86 is not magically live at this point.
7045 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
7046 cleanup_cfg (CLEANUP_NO_INSN_DEL);
7048 checking_verify_flow_info ();
7050 /* Initialize pseudos allocated for hard registers. */
7051 emit_initial_value_sets ();
7053 /* And finally unshare all RTL. */
7054 unshare_all_rtl ();
7056 /* There's no need to defer outputting this function any more; we
7057 know we want to output it. */
7058 DECL_DEFER_OUTPUT (current_function_decl) = 0;
7060 /* Now that we're done expanding trees to RTL, we shouldn't have any
7061 more CONCATs anywhere. */
7062 generating_concat_p = 0;
7064 if (dump_file)
7066 fprintf (dump_file,
7067 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
7068 /* And the pass manager will dump RTL for us. */
7071 /* If we're emitting a nested function, make sure its parent gets
7072 emitted as well. Doing otherwise confuses debug info. */
7074 tree parent;
7075 for (parent = DECL_CONTEXT (current_function_decl);
7076 parent != NULL_TREE;
7077 parent = get_containing_scope (parent))
7078 if (TREE_CODE (parent) == FUNCTION_DECL)
7079 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
7082 TREE_ASM_WRITTEN (current_function_decl) = 1;
7084 /* After expanding, the return labels are no longer needed. */
7085 return_label = NULL;
7086 naked_return_label = NULL;
7088 /* After expanding, the tm_restart map is no longer needed. */
7089 if (fun->gimple_df->tm_restart)
7090 fun->gimple_df->tm_restart = NULL;
7092 /* Tag the blocks with a depth number so that change_scope can find
7093 the common parent easily. */
7094 set_block_levels (DECL_INITIAL (fun->decl), 0);
7095 default_rtl_profile ();
7097 /* For -dx discard loops now, otherwise IL verify in clean_state will
7098 ICE. */
7099 if (rtl_dump_and_exit)
7101 cfun->curr_properties &= ~PROP_loops;
7102 loop_optimizer_finalize ();
7105 timevar_pop (TV_POST_EXPAND);
7107 return 0;
7110 } // anon namespace
7112 rtl_opt_pass *
7113 make_pass_expand (gcc::context *ctxt)
7115 return new pass_expand (ctxt);