* gcc-interface/trans.c (gigi): Fix initialization order.
[official-gcc.git] / gcc / cfgexpand.c
blob1990e107db60ad80ac26c824e3f24bf777275037
1 /* A pass for lowering trees to RTL.
2 Copyright (C) 2004-2015 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 "tm_p.h"
31 #include "ssa.h"
32 #include "optabs.h"
33 #include "regs.h" /* For reg_renumber. */
34 #include "emit-rtl.h"
35 #include "recog.h"
36 #include "cgraph.h"
37 #include "diagnostic.h"
38 #include "fold-const.h"
39 #include "varasm.h"
40 #include "stor-layout.h"
41 #include "stmt.h"
42 #include "print-tree.h"
43 #include "cfgrtl.h"
44 #include "cfganal.h"
45 #include "cfgbuild.h"
46 #include "cfgcleanup.h"
47 #include "dojump.h"
48 #include "explow.h"
49 #include "calls.h"
50 #include "expr.h"
51 #include "internal-fn.h"
52 #include "tree-eh.h"
53 #include "gimple-iterator.h"
54 #include "gimple-expr.h"
55 #include "gimple-walk.h"
56 #include "tree-cfg.h"
57 #include "tree-dfa.h"
58 #include "tree-ssa.h"
59 #include "except.h"
60 #include "gimple-pretty-print.h"
61 #include "toplev.h"
62 #include "debug.h"
63 #include "params.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 "asan.h"
71 #include "tree-ssa-address.h"
72 #include "output.h"
73 #include "builtins.h"
74 #include "tree-chkp.h"
75 #include "rtl-chkp.h"
77 /* Some systems use __main in a way incompatible with its use in gcc, in these
78 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
79 give the same symbol without quotes for an alternative entry point. You
80 must define both, or neither. */
81 #ifndef NAME__MAIN
82 #define NAME__MAIN "__main"
83 #endif
85 /* This variable holds information helping the rewriting of SSA trees
86 into RTL. */
87 struct ssaexpand SA;
89 /* This variable holds the currently expanded gimple statement for purposes
90 of comminucating the profile info to the builtin expanders. */
91 gimple *currently_expanding_gimple_stmt;
93 static rtx expand_debug_expr (tree);
95 static bool defer_stack_allocation (tree, bool);
97 static void record_alignment_for_reg_var (unsigned int);
99 /* Return an expression tree corresponding to the RHS of GIMPLE
100 statement STMT. */
102 tree
103 gimple_assign_rhs_to_tree (gimple *stmt)
105 tree t;
106 enum gimple_rhs_class grhs_class;
108 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
110 if (grhs_class == GIMPLE_TERNARY_RHS)
111 t = build3 (gimple_assign_rhs_code (stmt),
112 TREE_TYPE (gimple_assign_lhs (stmt)),
113 gimple_assign_rhs1 (stmt),
114 gimple_assign_rhs2 (stmt),
115 gimple_assign_rhs3 (stmt));
116 else if (grhs_class == GIMPLE_BINARY_RHS)
117 t = build2 (gimple_assign_rhs_code (stmt),
118 TREE_TYPE (gimple_assign_lhs (stmt)),
119 gimple_assign_rhs1 (stmt),
120 gimple_assign_rhs2 (stmt));
121 else if (grhs_class == GIMPLE_UNARY_RHS)
122 t = build1 (gimple_assign_rhs_code (stmt),
123 TREE_TYPE (gimple_assign_lhs (stmt)),
124 gimple_assign_rhs1 (stmt));
125 else if (grhs_class == 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)
132 && currently_expanding_to_rtl
133 && EXPR_P (t)))
134 t = copy_node (t);
136 else
137 gcc_unreachable ();
139 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
140 SET_EXPR_LOCATION (t, gimple_location (stmt));
142 return t;
146 #ifndef STACK_ALIGNMENT_NEEDED
147 #define STACK_ALIGNMENT_NEEDED 1
148 #endif
150 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
152 /* Choose either CUR or NEXT as the leader DECL for a partition.
153 Prefer ignored decls, to simplify debug dumps and reduce ambiguity
154 out of the same user variable being in multiple partitions (this is
155 less likely for compiler-introduced temps). */
157 static tree
158 leader_merge (tree cur, tree next)
160 if (cur == NULL || cur == next)
161 return next;
163 if (DECL_P (cur) && DECL_IGNORED_P (cur))
164 return cur;
166 if (DECL_P (next) && DECL_IGNORED_P (next))
167 return next;
169 return cur;
172 /* Associate declaration T with storage space X. If T is no
173 SSA name this is exactly SET_DECL_RTL, otherwise make the
174 partition of T associated with X. */
175 static inline void
176 set_rtl (tree t, rtx x)
178 gcc_checking_assert (!x
179 || !(TREE_CODE (t) == SSA_NAME || is_gimple_reg (t))
180 || (use_register_for_decl (t)
181 ? (REG_P (x)
182 || (GET_CODE (x) == CONCAT
183 && (REG_P (XEXP (x, 0))
184 || SUBREG_P (XEXP (x, 0)))
185 && (REG_P (XEXP (x, 1))
186 || SUBREG_P (XEXP (x, 1))))
187 || (GET_CODE (x) == PARALLEL
188 && SSAVAR (t)
189 && TREE_CODE (SSAVAR (t)) == RESULT_DECL
190 && !flag_tree_coalesce_vars))
191 : (MEM_P (x) || x == pc_rtx
192 || (GET_CODE (x) == CONCAT
193 && MEM_P (XEXP (x, 0))
194 && MEM_P (XEXP (x, 1))))));
195 /* Check that the RTL for SSA_NAMEs and gimple-reg PARM_DECLs and
196 RESULT_DECLs has the expected mode. For memory, we accept
197 unpromoted modes, since that's what we're likely to get. For
198 PARM_DECLs and RESULT_DECLs, we'll have been called by
199 set_parm_rtl, which will give us the default def, so we don't
200 have to compute it ourselves. For RESULT_DECLs, we accept mode
201 mismatches too, as long as we're not coalescing across variables,
202 so that we don't reject BLKmode PARALLELs or unpromoted REGs. */
203 gcc_checking_assert (!x || x == pc_rtx || TREE_CODE (t) != SSA_NAME
204 || (SSAVAR (t) && TREE_CODE (SSAVAR (t)) == RESULT_DECL
205 && !flag_tree_coalesce_vars)
206 || !use_register_for_decl (t)
207 || GET_MODE (x) == promote_ssa_mode (t, NULL));
209 if (x)
211 bool skip = false;
212 tree cur = NULL_TREE;
213 rtx xm = x;
215 retry:
216 if (MEM_P (xm))
217 cur = MEM_EXPR (xm);
218 else if (REG_P (xm))
219 cur = REG_EXPR (xm);
220 else if (SUBREG_P (xm))
222 gcc_assert (subreg_lowpart_p (xm));
223 xm = SUBREG_REG (xm);
224 goto retry;
226 else if (GET_CODE (xm) == CONCAT)
228 xm = XEXP (xm, 0);
229 goto retry;
231 else if (GET_CODE (xm) == PARALLEL)
233 xm = XVECEXP (xm, 0, 0);
234 gcc_assert (GET_CODE (xm) == EXPR_LIST);
235 xm = XEXP (xm, 0);
236 goto retry;
238 else if (xm == pc_rtx)
239 skip = true;
240 else
241 gcc_unreachable ();
243 tree next = skip ? cur : leader_merge (cur, SSAVAR (t) ? SSAVAR (t) : t);
245 if (cur != next)
247 if (MEM_P (x))
248 set_mem_attributes (x,
249 next && TREE_CODE (next) == SSA_NAME
250 ? TREE_TYPE (next)
251 : next, true);
252 else
253 set_reg_attrs_for_decl_rtl (next, x);
257 if (TREE_CODE (t) == SSA_NAME)
259 int part = var_to_partition (SA.map, t);
260 if (part != NO_PARTITION)
262 if (SA.partition_to_pseudo[part])
263 gcc_assert (SA.partition_to_pseudo[part] == x);
264 else if (x != pc_rtx)
265 SA.partition_to_pseudo[part] = x;
267 /* For the benefit of debug information at -O0 (where
268 vartracking doesn't run) record the place also in the base
269 DECL. For PARMs and RESULTs, do so only when setting the
270 default def. */
271 if (x && x != pc_rtx && SSA_NAME_VAR (t)
272 && (VAR_P (SSA_NAME_VAR (t))
273 || SSA_NAME_IS_DEFAULT_DEF (t)))
275 tree var = SSA_NAME_VAR (t);
276 /* If we don't yet have something recorded, just record it now. */
277 if (!DECL_RTL_SET_P (var))
278 SET_DECL_RTL (var, x);
279 /* If we have it set already to "multiple places" don't
280 change this. */
281 else if (DECL_RTL (var) == pc_rtx)
283 /* If we have something recorded and it's not the same place
284 as we want to record now, we have multiple partitions for the
285 same base variable, with different places. We can't just
286 randomly chose one, hence we have to say that we don't know.
287 This only happens with optimization, and there var-tracking
288 will figure out the right thing. */
289 else if (DECL_RTL (var) != x)
290 SET_DECL_RTL (var, pc_rtx);
293 else
294 SET_DECL_RTL (t, x);
297 /* This structure holds data relevant to one variable that will be
298 placed in a stack slot. */
299 struct stack_var
301 /* The Variable. */
302 tree decl;
304 /* Initially, the size of the variable. Later, the size of the partition,
305 if this variable becomes it's partition's representative. */
306 HOST_WIDE_INT size;
308 /* The *byte* alignment required for this variable. Or as, with the
309 size, the alignment for this partition. */
310 unsigned int alignb;
312 /* The partition representative. */
313 size_t representative;
315 /* The next stack variable in the partition, or EOC. */
316 size_t next;
318 /* The numbers of conflicting stack variables. */
319 bitmap conflicts;
322 #define EOC ((size_t)-1)
324 /* We have an array of such objects while deciding allocation. */
325 static struct stack_var *stack_vars;
326 static size_t stack_vars_alloc;
327 static size_t stack_vars_num;
328 static hash_map<tree, size_t> *decl_to_stack_part;
330 /* Conflict bitmaps go on this obstack. This allows us to destroy
331 all of them in one big sweep. */
332 static bitmap_obstack stack_var_bitmap_obstack;
334 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
335 is non-decreasing. */
336 static size_t *stack_vars_sorted;
338 /* The phase of the stack frame. This is the known misalignment of
339 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
340 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
341 static int frame_phase;
343 /* Used during expand_used_vars to remember if we saw any decls for
344 which we'd like to enable stack smashing protection. */
345 static bool has_protected_decls;
347 /* Used during expand_used_vars. Remember if we say a character buffer
348 smaller than our cutoff threshold. Used for -Wstack-protector. */
349 static bool has_short_buffer;
351 /* Compute the byte alignment to use for DECL. Ignore alignment
352 we can't do with expected alignment of the stack boundary. */
354 static unsigned int
355 align_local_variable (tree decl)
357 unsigned int align;
359 if (TREE_CODE (decl) == SSA_NAME)
360 align = TYPE_ALIGN (TREE_TYPE (decl));
361 else
363 align = LOCAL_DECL_ALIGNMENT (decl);
364 DECL_ALIGN (decl) = align;
366 return align / BITS_PER_UNIT;
369 /* Align given offset BASE with ALIGN. Truncate up if ALIGN_UP is true,
370 down otherwise. Return truncated BASE value. */
372 static inline unsigned HOST_WIDE_INT
373 align_base (HOST_WIDE_INT base, unsigned HOST_WIDE_INT align, bool align_up)
375 return align_up ? (base + align - 1) & -align : base & -align;
378 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
379 Return the frame offset. */
381 static HOST_WIDE_INT
382 alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
384 HOST_WIDE_INT offset, new_frame_offset;
386 if (FRAME_GROWS_DOWNWARD)
388 new_frame_offset
389 = align_base (frame_offset - frame_phase - size,
390 align, false) + frame_phase;
391 offset = new_frame_offset;
393 else
395 new_frame_offset
396 = align_base (frame_offset - frame_phase, align, true) + frame_phase;
397 offset = new_frame_offset;
398 new_frame_offset += size;
400 frame_offset = new_frame_offset;
402 if (frame_offset_overflow (frame_offset, cfun->decl))
403 frame_offset = offset = 0;
405 return offset;
408 /* Accumulate DECL into STACK_VARS. */
410 static void
411 add_stack_var (tree decl)
413 struct stack_var *v;
415 if (stack_vars_num >= stack_vars_alloc)
417 if (stack_vars_alloc)
418 stack_vars_alloc = stack_vars_alloc * 3 / 2;
419 else
420 stack_vars_alloc = 32;
421 stack_vars
422 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
424 if (!decl_to_stack_part)
425 decl_to_stack_part = new hash_map<tree, size_t>;
427 v = &stack_vars[stack_vars_num];
428 decl_to_stack_part->put (decl, stack_vars_num);
430 v->decl = decl;
431 tree size = TREE_CODE (decl) == SSA_NAME
432 ? TYPE_SIZE_UNIT (TREE_TYPE (decl))
433 : DECL_SIZE_UNIT (decl);
434 v->size = tree_to_uhwi (size);
435 /* Ensure that all variables have size, so that &a != &b for any two
436 variables that are simultaneously live. */
437 if (v->size == 0)
438 v->size = 1;
439 v->alignb = align_local_variable (decl);
440 /* An alignment of zero can mightily confuse us later. */
441 gcc_assert (v->alignb != 0);
443 /* All variables are initially in their own partition. */
444 v->representative = stack_vars_num;
445 v->next = EOC;
447 /* All variables initially conflict with no other. */
448 v->conflicts = NULL;
450 /* Ensure that this decl doesn't get put onto the list twice. */
451 set_rtl (decl, pc_rtx);
453 stack_vars_num++;
456 /* Make the decls associated with luid's X and Y conflict. */
458 static void
459 add_stack_var_conflict (size_t x, size_t y)
461 struct stack_var *a = &stack_vars[x];
462 struct stack_var *b = &stack_vars[y];
463 if (!a->conflicts)
464 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
465 if (!b->conflicts)
466 b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
467 bitmap_set_bit (a->conflicts, y);
468 bitmap_set_bit (b->conflicts, x);
471 /* Check whether the decls associated with luid's X and Y conflict. */
473 static bool
474 stack_var_conflict_p (size_t x, size_t y)
476 struct stack_var *a = &stack_vars[x];
477 struct stack_var *b = &stack_vars[y];
478 if (x == y)
479 return false;
480 /* Partitions containing an SSA name result from gimple registers
481 with things like unsupported modes. They are top-level and
482 hence conflict with everything else. */
483 if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
484 return true;
486 if (!a->conflicts || !b->conflicts)
487 return false;
488 return bitmap_bit_p (a->conflicts, y);
491 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
492 enter its partition number into bitmap DATA. */
494 static bool
495 visit_op (gimple *, tree op, tree, void *data)
497 bitmap active = (bitmap)data;
498 op = get_base_address (op);
499 if (op
500 && DECL_P (op)
501 && DECL_RTL_IF_SET (op) == pc_rtx)
503 size_t *v = decl_to_stack_part->get (op);
504 if (v)
505 bitmap_set_bit (active, *v);
507 return false;
510 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
511 record conflicts between it and all currently active other partitions
512 from bitmap DATA. */
514 static bool
515 visit_conflict (gimple *, tree op, tree, void *data)
517 bitmap active = (bitmap)data;
518 op = get_base_address (op);
519 if (op
520 && DECL_P (op)
521 && DECL_RTL_IF_SET (op) == pc_rtx)
523 size_t *v = decl_to_stack_part->get (op);
524 if (v && bitmap_set_bit (active, *v))
526 size_t num = *v;
527 bitmap_iterator bi;
528 unsigned i;
529 gcc_assert (num < stack_vars_num);
530 EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
531 add_stack_var_conflict (num, i);
534 return false;
537 /* Helper routine for add_scope_conflicts, calculating the active partitions
538 at the end of BB, leaving the result in WORK. We're called to generate
539 conflicts when FOR_CONFLICT is true, otherwise we're just tracking
540 liveness. */
542 static void
543 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
545 edge e;
546 edge_iterator ei;
547 gimple_stmt_iterator gsi;
548 walk_stmt_load_store_addr_fn visit;
550 bitmap_clear (work);
551 FOR_EACH_EDGE (e, ei, bb->preds)
552 bitmap_ior_into (work, (bitmap)e->src->aux);
554 visit = visit_op;
556 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
558 gimple *stmt = gsi_stmt (gsi);
559 walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
561 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
563 gimple *stmt = gsi_stmt (gsi);
565 if (gimple_clobber_p (stmt))
567 tree lhs = gimple_assign_lhs (stmt);
568 size_t *v;
569 /* Nested function lowering might introduce LHSs
570 that are COMPONENT_REFs. */
571 if (TREE_CODE (lhs) != VAR_DECL)
572 continue;
573 if (DECL_RTL_IF_SET (lhs) == pc_rtx
574 && (v = decl_to_stack_part->get (lhs)))
575 bitmap_clear_bit (work, *v);
577 else if (!is_gimple_debug (stmt))
579 if (for_conflict
580 && visit == visit_op)
582 /* If this is the first real instruction in this BB we need
583 to add conflicts for everything live at this point now.
584 Unlike classical liveness for named objects we can't
585 rely on seeing a def/use of the names we're interested in.
586 There might merely be indirect loads/stores. We'd not add any
587 conflicts for such partitions. */
588 bitmap_iterator bi;
589 unsigned i;
590 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
592 struct stack_var *a = &stack_vars[i];
593 if (!a->conflicts)
594 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
595 bitmap_ior_into (a->conflicts, work);
597 visit = visit_conflict;
599 walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
604 /* Generate stack partition conflicts between all partitions that are
605 simultaneously live. */
607 static void
608 add_scope_conflicts (void)
610 basic_block bb;
611 bool changed;
612 bitmap work = BITMAP_ALLOC (NULL);
613 int *rpo;
614 int n_bbs;
616 /* We approximate the live range of a stack variable by taking the first
617 mention of its name as starting point(s), and by the end-of-scope
618 death clobber added by gimplify as ending point(s) of the range.
619 This overapproximates in the case we for instance moved an address-taken
620 operation upward, without also moving a dereference to it upwards.
621 But it's conservatively correct as a variable never can hold values
622 before its name is mentioned at least once.
624 We then do a mostly classical bitmap liveness algorithm. */
626 FOR_ALL_BB_FN (bb, cfun)
627 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
629 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
630 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
632 changed = true;
633 while (changed)
635 int i;
636 changed = false;
637 for (i = 0; i < n_bbs; i++)
639 bitmap active;
640 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
641 active = (bitmap)bb->aux;
642 add_scope_conflicts_1 (bb, work, false);
643 if (bitmap_ior_into (active, work))
644 changed = true;
648 FOR_EACH_BB_FN (bb, cfun)
649 add_scope_conflicts_1 (bb, work, true);
651 free (rpo);
652 BITMAP_FREE (work);
653 FOR_ALL_BB_FN (bb, cfun)
654 BITMAP_FREE (bb->aux);
657 /* A subroutine of partition_stack_vars. A comparison function for qsort,
658 sorting an array of indices by the properties of the object. */
660 static int
661 stack_var_cmp (const void *a, const void *b)
663 size_t ia = *(const size_t *)a;
664 size_t ib = *(const size_t *)b;
665 unsigned int aligna = stack_vars[ia].alignb;
666 unsigned int alignb = stack_vars[ib].alignb;
667 HOST_WIDE_INT sizea = stack_vars[ia].size;
668 HOST_WIDE_INT sizeb = stack_vars[ib].size;
669 tree decla = stack_vars[ia].decl;
670 tree declb = stack_vars[ib].decl;
671 bool largea, largeb;
672 unsigned int uida, uidb;
674 /* Primary compare on "large" alignment. Large comes first. */
675 largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
676 largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
677 if (largea != largeb)
678 return (int)largeb - (int)largea;
680 /* Secondary compare on size, decreasing */
681 if (sizea > sizeb)
682 return -1;
683 if (sizea < sizeb)
684 return 1;
686 /* Tertiary compare on true alignment, decreasing. */
687 if (aligna < alignb)
688 return -1;
689 if (aligna > alignb)
690 return 1;
692 /* Final compare on ID for sort stability, increasing.
693 Two SSA names are compared by their version, SSA names come before
694 non-SSA names, and two normal decls are compared by their DECL_UID. */
695 if (TREE_CODE (decla) == SSA_NAME)
697 if (TREE_CODE (declb) == SSA_NAME)
698 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
699 else
700 return -1;
702 else if (TREE_CODE (declb) == SSA_NAME)
703 return 1;
704 else
705 uida = DECL_UID (decla), uidb = DECL_UID (declb);
706 if (uida < uidb)
707 return 1;
708 if (uida > uidb)
709 return -1;
710 return 0;
713 struct part_traits : unbounded_int_hashmap_traits <size_t, bitmap> {};
714 typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
716 /* If the points-to solution *PI points to variables that are in a partition
717 together with other variables add all partition members to the pointed-to
718 variables bitmap. */
720 static void
721 add_partitioned_vars_to_ptset (struct pt_solution *pt,
722 part_hashmap *decls_to_partitions,
723 hash_set<bitmap> *visited, bitmap temp)
725 bitmap_iterator bi;
726 unsigned i;
727 bitmap *part;
729 if (pt->anything
730 || pt->vars == NULL
731 /* The pointed-to vars bitmap is shared, it is enough to
732 visit it once. */
733 || visited->add (pt->vars))
734 return;
736 bitmap_clear (temp);
738 /* By using a temporary bitmap to store all members of the partitions
739 we have to add we make sure to visit each of the partitions only
740 once. */
741 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
742 if ((!temp
743 || !bitmap_bit_p (temp, i))
744 && (part = decls_to_partitions->get (i)))
745 bitmap_ior_into (temp, *part);
746 if (!bitmap_empty_p (temp))
747 bitmap_ior_into (pt->vars, temp);
750 /* Update points-to sets based on partition info, so we can use them on RTL.
751 The bitmaps representing stack partitions will be saved until expand,
752 where partitioned decls used as bases in memory expressions will be
753 rewritten. */
755 static void
756 update_alias_info_with_stack_vars (void)
758 part_hashmap *decls_to_partitions = NULL;
759 size_t i, j;
760 tree var = NULL_TREE;
762 for (i = 0; i < stack_vars_num; i++)
764 bitmap part = NULL;
765 tree name;
766 struct ptr_info_def *pi;
768 /* Not interested in partitions with single variable. */
769 if (stack_vars[i].representative != i
770 || stack_vars[i].next == EOC)
771 continue;
773 if (!decls_to_partitions)
775 decls_to_partitions = new part_hashmap;
776 cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
779 /* Create an SSA_NAME that points to the partition for use
780 as base during alias-oracle queries on RTL for bases that
781 have been partitioned. */
782 if (var == NULL_TREE)
783 var = create_tmp_var (ptr_type_node);
784 name = make_ssa_name (var);
786 /* Create bitmaps representing partitions. They will be used for
787 points-to sets later, so use GGC alloc. */
788 part = BITMAP_GGC_ALLOC ();
789 for (j = i; j != EOC; j = stack_vars[j].next)
791 tree decl = stack_vars[j].decl;
792 unsigned int uid = DECL_PT_UID (decl);
793 bitmap_set_bit (part, uid);
794 decls_to_partitions->put (uid, part);
795 cfun->gimple_df->decls_to_pointers->put (decl, name);
796 if (TREE_ADDRESSABLE (decl))
797 TREE_ADDRESSABLE (name) = 1;
800 /* Make the SSA name point to all partition members. */
801 pi = get_ptr_info (name);
802 pt_solution_set (&pi->pt, part, false);
805 /* Make all points-to sets that contain one member of a partition
806 contain all members of the partition. */
807 if (decls_to_partitions)
809 unsigned i;
810 hash_set<bitmap> visited;
811 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
813 for (i = 1; i < num_ssa_names; i++)
815 tree name = ssa_name (i);
816 struct ptr_info_def *pi;
818 if (name
819 && POINTER_TYPE_P (TREE_TYPE (name))
820 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
821 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
822 &visited, temp);
825 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
826 decls_to_partitions, &visited, temp);
828 delete decls_to_partitions;
829 BITMAP_FREE (temp);
833 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
834 partitioning algorithm. Partitions A and B are known to be non-conflicting.
835 Merge them into a single partition A. */
837 static void
838 union_stack_vars (size_t a, size_t b)
840 struct stack_var *vb = &stack_vars[b];
841 bitmap_iterator bi;
842 unsigned u;
844 gcc_assert (stack_vars[b].next == EOC);
845 /* Add B to A's partition. */
846 stack_vars[b].next = stack_vars[a].next;
847 stack_vars[b].representative = a;
848 stack_vars[a].next = b;
850 /* Update the required alignment of partition A to account for B. */
851 if (stack_vars[a].alignb < stack_vars[b].alignb)
852 stack_vars[a].alignb = stack_vars[b].alignb;
854 /* Update the interference graph and merge the conflicts. */
855 if (vb->conflicts)
857 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
858 add_stack_var_conflict (a, stack_vars[u].representative);
859 BITMAP_FREE (vb->conflicts);
863 /* A subroutine of expand_used_vars. Binpack the variables into
864 partitions constrained by the interference graph. The overall
865 algorithm used is as follows:
867 Sort the objects by size in descending order.
868 For each object A {
869 S = size(A)
870 O = 0
871 loop {
872 Look for the largest non-conflicting object B with size <= S.
873 UNION (A, B)
878 static void
879 partition_stack_vars (void)
881 size_t si, sj, n = stack_vars_num;
883 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
884 for (si = 0; si < n; ++si)
885 stack_vars_sorted[si] = si;
887 if (n == 1)
888 return;
890 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
892 for (si = 0; si < n; ++si)
894 size_t i = stack_vars_sorted[si];
895 unsigned int ialign = stack_vars[i].alignb;
896 HOST_WIDE_INT isize = stack_vars[i].size;
898 /* Ignore objects that aren't partition representatives. If we
899 see a var that is not a partition representative, it must
900 have been merged earlier. */
901 if (stack_vars[i].representative != i)
902 continue;
904 for (sj = si + 1; sj < n; ++sj)
906 size_t j = stack_vars_sorted[sj];
907 unsigned int jalign = stack_vars[j].alignb;
908 HOST_WIDE_INT jsize = stack_vars[j].size;
910 /* Ignore objects that aren't partition representatives. */
911 if (stack_vars[j].representative != j)
912 continue;
914 /* Do not mix objects of "small" (supported) alignment
915 and "large" (unsupported) alignment. */
916 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
917 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
918 break;
920 /* For Address Sanitizer do not mix objects with different
921 sizes, as the shorter vars wouldn't be adequately protected.
922 Don't do that for "large" (unsupported) alignment objects,
923 those aren't protected anyway. */
924 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && isize != jsize
925 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
926 break;
928 /* Ignore conflicting objects. */
929 if (stack_var_conflict_p (i, j))
930 continue;
932 /* UNION the objects, placing J at OFFSET. */
933 union_stack_vars (i, j);
937 update_alias_info_with_stack_vars ();
940 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
942 static void
943 dump_stack_var_partition (void)
945 size_t si, i, j, n = stack_vars_num;
947 for (si = 0; si < n; ++si)
949 i = stack_vars_sorted[si];
951 /* Skip variables that aren't partition representatives, for now. */
952 if (stack_vars[i].representative != i)
953 continue;
955 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
956 " align %u\n", (unsigned long) i, stack_vars[i].size,
957 stack_vars[i].alignb);
959 for (j = i; j != EOC; j = stack_vars[j].next)
961 fputc ('\t', dump_file);
962 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
964 fputc ('\n', dump_file);
968 /* Assign rtl to DECL at BASE + OFFSET. */
970 static void
971 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
972 HOST_WIDE_INT offset)
974 unsigned align;
975 rtx x;
977 /* If this fails, we've overflowed the stack frame. Error nicely? */
978 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
980 x = plus_constant (Pmode, base, offset);
981 x = gen_rtx_MEM (TREE_CODE (decl) == SSA_NAME
982 ? TYPE_MODE (TREE_TYPE (decl))
983 : DECL_MODE (SSAVAR (decl)), x);
985 if (TREE_CODE (decl) != SSA_NAME)
987 /* Set alignment we actually gave this decl if it isn't an SSA name.
988 If it is we generate stack slots only accidentally so it isn't as
989 important, we'll simply use the alignment that is already set. */
990 if (base == virtual_stack_vars_rtx)
991 offset -= frame_phase;
992 align = offset & -offset;
993 align *= BITS_PER_UNIT;
994 if (align == 0 || align > base_align)
995 align = base_align;
997 /* One would think that we could assert that we're not decreasing
998 alignment here, but (at least) the i386 port does exactly this
999 via the MINIMUM_ALIGNMENT hook. */
1001 DECL_ALIGN (decl) = align;
1002 DECL_USER_ALIGN (decl) = 0;
1005 set_rtl (decl, x);
1008 struct stack_vars_data
1010 /* Vector of offset pairs, always end of some padding followed
1011 by start of the padding that needs Address Sanitizer protection.
1012 The vector is in reversed, highest offset pairs come first. */
1013 vec<HOST_WIDE_INT> asan_vec;
1015 /* Vector of partition representative decls in between the paddings. */
1016 vec<tree> asan_decl_vec;
1018 /* Base pseudo register for Address Sanitizer protected automatic vars. */
1019 rtx asan_base;
1021 /* Alignment needed for the Address Sanitizer protected automatic vars. */
1022 unsigned int asan_alignb;
1025 /* A subroutine of expand_used_vars. Give each partition representative
1026 a unique location within the stack frame. Update each partition member
1027 with that location. */
1029 static void
1030 expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
1032 size_t si, i, j, n = stack_vars_num;
1033 HOST_WIDE_INT large_size = 0, large_alloc = 0;
1034 rtx large_base = NULL;
1035 unsigned large_align = 0;
1036 tree decl;
1038 /* Determine if there are any variables requiring "large" alignment.
1039 Since these are dynamically allocated, we only process these if
1040 no predicate involved. */
1041 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
1042 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
1044 /* Find the total size of these variables. */
1045 for (si = 0; si < n; ++si)
1047 unsigned alignb;
1049 i = stack_vars_sorted[si];
1050 alignb = stack_vars[i].alignb;
1052 /* All "large" alignment decls come before all "small" alignment
1053 decls, but "large" alignment decls are not sorted based on
1054 their alignment. Increase large_align to track the largest
1055 required alignment. */
1056 if ((alignb * BITS_PER_UNIT) > large_align)
1057 large_align = alignb * BITS_PER_UNIT;
1059 /* Stop when we get to the first decl with "small" alignment. */
1060 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1061 break;
1063 /* Skip variables that aren't partition representatives. */
1064 if (stack_vars[i].representative != i)
1065 continue;
1067 /* Skip variables that have already had rtl assigned. See also
1068 add_stack_var where we perpetrate this pc_rtx hack. */
1069 decl = stack_vars[i].decl;
1070 if (TREE_CODE (decl) == SSA_NAME
1071 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)] != NULL_RTX
1072 : DECL_RTL (decl) != pc_rtx)
1073 continue;
1075 large_size += alignb - 1;
1076 large_size &= -(HOST_WIDE_INT)alignb;
1077 large_size += stack_vars[i].size;
1080 /* If there were any, allocate space. */
1081 if (large_size > 0)
1082 large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
1083 large_align, true);
1086 for (si = 0; si < n; ++si)
1088 rtx base;
1089 unsigned base_align, alignb;
1090 HOST_WIDE_INT offset;
1092 i = stack_vars_sorted[si];
1094 /* Skip variables that aren't partition representatives, for now. */
1095 if (stack_vars[i].representative != i)
1096 continue;
1098 /* Skip variables that have already had rtl assigned. See also
1099 add_stack_var where we perpetrate this pc_rtx hack. */
1100 decl = stack_vars[i].decl;
1101 if (TREE_CODE (decl) == SSA_NAME
1102 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)] != NULL_RTX
1103 : DECL_RTL (decl) != pc_rtx)
1104 continue;
1106 /* Check the predicate to see whether this variable should be
1107 allocated in this pass. */
1108 if (pred && !pred (i))
1109 continue;
1111 alignb = stack_vars[i].alignb;
1112 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1114 base = virtual_stack_vars_rtx;
1115 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && pred)
1117 HOST_WIDE_INT prev_offset
1118 = align_base (frame_offset,
1119 MAX (alignb, ASAN_RED_ZONE_SIZE),
1120 FRAME_GROWS_DOWNWARD);
1121 tree repr_decl = NULL_TREE;
1122 offset
1123 = alloc_stack_frame_space (stack_vars[i].size
1124 + ASAN_RED_ZONE_SIZE,
1125 MAX (alignb, ASAN_RED_ZONE_SIZE));
1127 data->asan_vec.safe_push (prev_offset);
1128 data->asan_vec.safe_push (offset + stack_vars[i].size);
1129 /* Find best representative of the partition.
1130 Prefer those with DECL_NAME, even better
1131 satisfying asan_protect_stack_decl predicate. */
1132 for (j = i; j != EOC; j = stack_vars[j].next)
1133 if (asan_protect_stack_decl (stack_vars[j].decl)
1134 && DECL_NAME (stack_vars[j].decl))
1136 repr_decl = stack_vars[j].decl;
1137 break;
1139 else if (repr_decl == NULL_TREE
1140 && DECL_P (stack_vars[j].decl)
1141 && DECL_NAME (stack_vars[j].decl))
1142 repr_decl = stack_vars[j].decl;
1143 if (repr_decl == NULL_TREE)
1144 repr_decl = stack_vars[i].decl;
1145 data->asan_decl_vec.safe_push (repr_decl);
1146 data->asan_alignb = MAX (data->asan_alignb, alignb);
1147 if (data->asan_base == NULL)
1148 data->asan_base = gen_reg_rtx (Pmode);
1149 base = data->asan_base;
1151 if (!STRICT_ALIGNMENT)
1152 base_align = crtl->max_used_stack_slot_alignment;
1153 else
1154 base_align = MAX (crtl->max_used_stack_slot_alignment,
1155 GET_MODE_ALIGNMENT (SImode)
1156 << ASAN_SHADOW_SHIFT);
1158 else
1160 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1161 base_align = crtl->max_used_stack_slot_alignment;
1164 else
1166 /* Large alignment is only processed in the last pass. */
1167 if (pred)
1168 continue;
1169 gcc_assert (large_base != NULL);
1171 large_alloc += alignb - 1;
1172 large_alloc &= -(HOST_WIDE_INT)alignb;
1173 offset = large_alloc;
1174 large_alloc += stack_vars[i].size;
1176 base = large_base;
1177 base_align = large_align;
1180 /* Create rtl for each variable based on their location within the
1181 partition. */
1182 for (j = i; j != EOC; j = stack_vars[j].next)
1184 expand_one_stack_var_at (stack_vars[j].decl,
1185 base, base_align,
1186 offset);
1190 gcc_assert (large_alloc == large_size);
1193 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1194 static HOST_WIDE_INT
1195 account_stack_vars (void)
1197 size_t si, j, i, n = stack_vars_num;
1198 HOST_WIDE_INT size = 0;
1200 for (si = 0; si < n; ++si)
1202 i = stack_vars_sorted[si];
1204 /* Skip variables that aren't partition representatives, for now. */
1205 if (stack_vars[i].representative != i)
1206 continue;
1208 size += stack_vars[i].size;
1209 for (j = i; j != EOC; j = stack_vars[j].next)
1210 set_rtl (stack_vars[j].decl, NULL);
1212 return size;
1215 /* Record the RTL assignment X for the default def of PARM. */
1217 extern void
1218 set_parm_rtl (tree parm, rtx x)
1220 gcc_assert (TREE_CODE (parm) == PARM_DECL
1221 || TREE_CODE (parm) == RESULT_DECL);
1223 if (x && !MEM_P (x))
1225 unsigned int align = MINIMUM_ALIGNMENT (TREE_TYPE (parm),
1226 TYPE_MODE (TREE_TYPE (parm)),
1227 TYPE_ALIGN (TREE_TYPE (parm)));
1229 /* If the variable alignment is very large we'll dynamicaly
1230 allocate it, which means that in-frame portion is just a
1231 pointer. ??? We've got a pseudo for sure here, do we
1232 actually dynamically allocate its spilling area if needed?
1233 ??? Isn't it a problem when POINTER_SIZE also exceeds
1234 MAX_SUPPORTED_STACK_ALIGNMENT, as on cris and lm32? */
1235 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1236 align = POINTER_SIZE;
1238 record_alignment_for_reg_var (align);
1241 tree ssa = ssa_default_def (cfun, parm);
1242 if (!ssa)
1243 return set_rtl (parm, x);
1245 int part = var_to_partition (SA.map, ssa);
1246 gcc_assert (part != NO_PARTITION);
1248 bool changed = bitmap_bit_p (SA.partitions_for_parm_default_defs, part);
1249 gcc_assert (changed);
1251 set_rtl (ssa, x);
1252 gcc_assert (DECL_RTL (parm) == x);
1255 /* A subroutine of expand_one_var. Called to immediately assign rtl
1256 to a variable to be allocated in the stack frame. */
1258 static void
1259 expand_one_stack_var_1 (tree var)
1261 HOST_WIDE_INT size, offset;
1262 unsigned byte_align;
1264 if (TREE_CODE (var) == SSA_NAME)
1266 tree type = TREE_TYPE (var);
1267 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1268 byte_align = TYPE_ALIGN_UNIT (type);
1270 else
1272 size = tree_to_uhwi (DECL_SIZE_UNIT (var));
1273 byte_align = align_local_variable (var);
1276 /* We handle highly aligned variables in expand_stack_vars. */
1277 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1279 offset = alloc_stack_frame_space (size, byte_align);
1281 expand_one_stack_var_at (var, virtual_stack_vars_rtx,
1282 crtl->max_used_stack_slot_alignment, offset);
1285 /* Wrapper for expand_one_stack_var_1 that checks SSA_NAMEs are
1286 already assigned some MEM. */
1288 static void
1289 expand_one_stack_var (tree var)
1291 if (TREE_CODE (var) == SSA_NAME)
1293 int part = var_to_partition (SA.map, var);
1294 if (part != NO_PARTITION)
1296 rtx x = SA.partition_to_pseudo[part];
1297 gcc_assert (x);
1298 gcc_assert (MEM_P (x));
1299 return;
1303 return expand_one_stack_var_1 (var);
1306 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1307 that will reside in a hard register. */
1309 static void
1310 expand_one_hard_reg_var (tree var)
1312 rest_of_decl_compilation (var, 0, 0);
1315 /* Record the alignment requirements of some variable assigned to a
1316 pseudo. */
1318 static void
1319 record_alignment_for_reg_var (unsigned int align)
1321 if (SUPPORTS_STACK_ALIGNMENT
1322 && crtl->stack_alignment_estimated < align)
1324 /* stack_alignment_estimated shouldn't change after stack
1325 realign decision made */
1326 gcc_assert (!crtl->stack_realign_processed);
1327 crtl->stack_alignment_estimated = align;
1330 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1331 So here we only make sure stack_alignment_needed >= align. */
1332 if (crtl->stack_alignment_needed < align)
1333 crtl->stack_alignment_needed = align;
1334 if (crtl->max_used_stack_slot_alignment < align)
1335 crtl->max_used_stack_slot_alignment = align;
1338 /* Create RTL for an SSA partition. */
1340 static void
1341 expand_one_ssa_partition (tree var)
1343 int part = var_to_partition (SA.map, var);
1344 gcc_assert (part != NO_PARTITION);
1346 if (SA.partition_to_pseudo[part])
1347 return;
1349 unsigned int align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1350 TYPE_MODE (TREE_TYPE (var)),
1351 TYPE_ALIGN (TREE_TYPE (var)));
1353 /* If the variable alignment is very large we'll dynamicaly allocate
1354 it, which means that in-frame portion is just a pointer. */
1355 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1356 align = POINTER_SIZE;
1358 record_alignment_for_reg_var (align);
1360 if (!use_register_for_decl (var))
1362 if (defer_stack_allocation (var, true))
1363 add_stack_var (var);
1364 else
1365 expand_one_stack_var_1 (var);
1366 return;
1369 machine_mode reg_mode = promote_ssa_mode (var, NULL);
1371 rtx x = gen_reg_rtx (reg_mode);
1373 set_rtl (var, x);
1376 /* Record the association between the RTL generated for partition PART
1377 and the underlying variable of the SSA_NAME VAR. */
1379 static void
1380 adjust_one_expanded_partition_var (tree var)
1382 if (!var)
1383 return;
1385 tree decl = SSA_NAME_VAR (var);
1387 int part = var_to_partition (SA.map, var);
1388 if (part == NO_PARTITION)
1389 return;
1391 rtx x = SA.partition_to_pseudo[part];
1393 gcc_assert (x);
1395 set_rtl (var, x);
1397 if (!REG_P (x))
1398 return;
1400 /* Note if the object is a user variable. */
1401 if (decl && !DECL_ARTIFICIAL (decl))
1402 mark_user_reg (x);
1404 if (POINTER_TYPE_P (decl ? TREE_TYPE (decl) : TREE_TYPE (var)))
1405 mark_reg_pointer (x, get_pointer_alignment (var));
1408 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1409 that will reside in a pseudo register. */
1411 static void
1412 expand_one_register_var (tree var)
1414 if (TREE_CODE (var) == SSA_NAME)
1416 int part = var_to_partition (SA.map, var);
1417 if (part != NO_PARTITION)
1419 rtx x = SA.partition_to_pseudo[part];
1420 gcc_assert (x);
1421 gcc_assert (REG_P (x));
1422 return;
1424 gcc_unreachable ();
1427 tree decl = var;
1428 tree type = TREE_TYPE (decl);
1429 machine_mode reg_mode = promote_decl_mode (decl, NULL);
1430 rtx x = gen_reg_rtx (reg_mode);
1432 set_rtl (var, x);
1434 /* Note if the object is a user variable. */
1435 if (!DECL_ARTIFICIAL (decl))
1436 mark_user_reg (x);
1438 if (POINTER_TYPE_P (type))
1439 mark_reg_pointer (x, get_pointer_alignment (var));
1442 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1443 has some associated error, e.g. its type is error-mark. We just need
1444 to pick something that won't crash the rest of the compiler. */
1446 static void
1447 expand_one_error_var (tree var)
1449 machine_mode mode = DECL_MODE (var);
1450 rtx x;
1452 if (mode == BLKmode)
1453 x = gen_rtx_MEM (BLKmode, const0_rtx);
1454 else if (mode == VOIDmode)
1455 x = const0_rtx;
1456 else
1457 x = gen_reg_rtx (mode);
1459 SET_DECL_RTL (var, x);
1462 /* A subroutine of expand_one_var. VAR is a variable that will be
1463 allocated to the local stack frame. Return true if we wish to
1464 add VAR to STACK_VARS so that it will be coalesced with other
1465 variables. Return false to allocate VAR immediately.
1467 This function is used to reduce the number of variables considered
1468 for coalescing, which reduces the size of the quadratic problem. */
1470 static bool
1471 defer_stack_allocation (tree var, bool toplevel)
1473 tree size_unit = TREE_CODE (var) == SSA_NAME
1474 ? TYPE_SIZE_UNIT (TREE_TYPE (var))
1475 : DECL_SIZE_UNIT (var);
1477 /* Whether the variable is small enough for immediate allocation not to be
1478 a problem with regard to the frame size. */
1479 bool smallish
1480 = ((HOST_WIDE_INT) tree_to_uhwi (size_unit)
1481 < PARAM_VALUE (PARAM_MIN_SIZE_FOR_STACK_SHARING));
1483 /* If stack protection is enabled, *all* stack variables must be deferred,
1484 so that we can re-order the strings to the top of the frame.
1485 Similarly for Address Sanitizer. */
1486 if (flag_stack_protect || ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK))
1487 return true;
1489 unsigned int align = TREE_CODE (var) == SSA_NAME
1490 ? TYPE_ALIGN (TREE_TYPE (var))
1491 : DECL_ALIGN (var);
1493 /* We handle "large" alignment via dynamic allocation. We want to handle
1494 this extra complication in only one place, so defer them. */
1495 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1496 return true;
1498 bool ignored = TREE_CODE (var) == SSA_NAME
1499 ? !SSAVAR (var) || DECL_IGNORED_P (SSA_NAME_VAR (var))
1500 : DECL_IGNORED_P (var);
1502 /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1503 might be detached from their block and appear at toplevel when we reach
1504 here. We want to coalesce them with variables from other blocks when
1505 the immediate contribution to the frame size would be noticeable. */
1506 if (toplevel && optimize > 0 && ignored && !smallish)
1507 return true;
1509 /* Variables declared in the outermost scope automatically conflict
1510 with every other variable. The only reason to want to defer them
1511 at all is that, after sorting, we can more efficiently pack
1512 small variables in the stack frame. Continue to defer at -O2. */
1513 if (toplevel && optimize < 2)
1514 return false;
1516 /* Without optimization, *most* variables are allocated from the
1517 stack, which makes the quadratic problem large exactly when we
1518 want compilation to proceed as quickly as possible. On the
1519 other hand, we don't want the function's stack frame size to
1520 get completely out of hand. So we avoid adding scalars and
1521 "small" aggregates to the list at all. */
1522 if (optimize == 0 && smallish)
1523 return false;
1525 return true;
1528 /* A subroutine of expand_used_vars. Expand one variable according to
1529 its flavor. Variables to be placed on the stack are not actually
1530 expanded yet, merely recorded.
1531 When REALLY_EXPAND is false, only add stack values to be allocated.
1532 Return stack usage this variable is supposed to take.
1535 static HOST_WIDE_INT
1536 expand_one_var (tree var, bool toplevel, bool really_expand)
1538 unsigned int align = BITS_PER_UNIT;
1539 tree origvar = var;
1541 var = SSAVAR (var);
1543 if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
1545 /* Because we don't know if VAR will be in register or on stack,
1546 we conservatively assume it will be on stack even if VAR is
1547 eventually put into register after RA pass. For non-automatic
1548 variables, which won't be on stack, we collect alignment of
1549 type and ignore user specified alignment. Similarly for
1550 SSA_NAMEs for which use_register_for_decl returns true. */
1551 if (TREE_STATIC (var)
1552 || DECL_EXTERNAL (var)
1553 || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1554 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1555 TYPE_MODE (TREE_TYPE (var)),
1556 TYPE_ALIGN (TREE_TYPE (var)));
1557 else if (DECL_HAS_VALUE_EXPR_P (var)
1558 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1559 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1560 or variables which were assigned a stack slot already by
1561 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1562 changed from the offset chosen to it. */
1563 align = crtl->stack_alignment_estimated;
1564 else
1565 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1567 /* If the variable alignment is very large we'll dynamicaly allocate
1568 it, which means that in-frame portion is just a pointer. */
1569 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1570 align = POINTER_SIZE;
1573 record_alignment_for_reg_var (align);
1575 if (TREE_CODE (origvar) == SSA_NAME)
1577 gcc_assert (TREE_CODE (var) != VAR_DECL
1578 || (!DECL_EXTERNAL (var)
1579 && !DECL_HAS_VALUE_EXPR_P (var)
1580 && !TREE_STATIC (var)
1581 && TREE_TYPE (var) != error_mark_node
1582 && !DECL_HARD_REGISTER (var)
1583 && really_expand));
1585 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1587 else if (DECL_EXTERNAL (var))
1589 else if (DECL_HAS_VALUE_EXPR_P (var))
1591 else if (TREE_STATIC (var))
1593 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1595 else if (TREE_TYPE (var) == error_mark_node)
1597 if (really_expand)
1598 expand_one_error_var (var);
1600 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1602 if (really_expand)
1604 expand_one_hard_reg_var (var);
1605 if (!DECL_HARD_REGISTER (var))
1606 /* Invalid register specification. */
1607 expand_one_error_var (var);
1610 else if (use_register_for_decl (var))
1612 if (really_expand)
1613 expand_one_register_var (origvar);
1615 else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
1617 /* Reject variables which cover more than half of the address-space. */
1618 if (really_expand)
1620 error ("size of variable %q+D is too large", var);
1621 expand_one_error_var (var);
1624 else if (defer_stack_allocation (var, toplevel))
1625 add_stack_var (origvar);
1626 else
1628 if (really_expand)
1630 if (lookup_attribute ("naked",
1631 DECL_ATTRIBUTES (current_function_decl)))
1632 error ("cannot allocate stack for variable %q+D, naked function.",
1633 var);
1635 expand_one_stack_var (origvar);
1639 return tree_to_uhwi (DECL_SIZE_UNIT (var));
1641 return 0;
1644 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1645 expanding variables. Those variables that can be put into registers
1646 are allocated pseudos; those that can't are put on the stack.
1648 TOPLEVEL is true if this is the outermost BLOCK. */
1650 static void
1651 expand_used_vars_for_block (tree block, bool toplevel)
1653 tree t;
1655 /* Expand all variables at this level. */
1656 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1657 if (TREE_USED (t)
1658 && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1659 || !DECL_NONSHAREABLE (t)))
1660 expand_one_var (t, toplevel, true);
1662 /* Expand all variables at containing levels. */
1663 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1664 expand_used_vars_for_block (t, false);
1667 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1668 and clear TREE_USED on all local variables. */
1670 static void
1671 clear_tree_used (tree block)
1673 tree t;
1675 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1676 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1677 if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1678 || !DECL_NONSHAREABLE (t))
1679 TREE_USED (t) = 0;
1681 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1682 clear_tree_used (t);
1685 enum {
1686 SPCT_FLAG_DEFAULT = 1,
1687 SPCT_FLAG_ALL = 2,
1688 SPCT_FLAG_STRONG = 3,
1689 SPCT_FLAG_EXPLICIT = 4
1692 /* Examine TYPE and determine a bit mask of the following features. */
1694 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1695 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1696 #define SPCT_HAS_ARRAY 4
1697 #define SPCT_HAS_AGGREGATE 8
1699 static unsigned int
1700 stack_protect_classify_type (tree type)
1702 unsigned int ret = 0;
1703 tree t;
1705 switch (TREE_CODE (type))
1707 case ARRAY_TYPE:
1708 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1709 if (t == char_type_node
1710 || t == signed_char_type_node
1711 || t == unsigned_char_type_node)
1713 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1714 unsigned HOST_WIDE_INT len;
1716 if (!TYPE_SIZE_UNIT (type)
1717 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1718 len = max;
1719 else
1720 len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1722 if (len < max)
1723 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1724 else
1725 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1727 else
1728 ret = SPCT_HAS_ARRAY;
1729 break;
1731 case UNION_TYPE:
1732 case QUAL_UNION_TYPE:
1733 case RECORD_TYPE:
1734 ret = SPCT_HAS_AGGREGATE;
1735 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1736 if (TREE_CODE (t) == FIELD_DECL)
1737 ret |= stack_protect_classify_type (TREE_TYPE (t));
1738 break;
1740 default:
1741 break;
1744 return ret;
1747 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1748 part of the local stack frame. Remember if we ever return nonzero for
1749 any variable in this function. The return value is the phase number in
1750 which the variable should be allocated. */
1752 static int
1753 stack_protect_decl_phase (tree decl)
1755 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1756 int ret = 0;
1758 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1759 has_short_buffer = true;
1761 if (flag_stack_protect == SPCT_FLAG_ALL
1762 || flag_stack_protect == SPCT_FLAG_STRONG
1763 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1764 && lookup_attribute ("stack_protect",
1765 DECL_ATTRIBUTES (current_function_decl))))
1767 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1768 && !(bits & SPCT_HAS_AGGREGATE))
1769 ret = 1;
1770 else if (bits & SPCT_HAS_ARRAY)
1771 ret = 2;
1773 else
1774 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1776 if (ret)
1777 has_protected_decls = true;
1779 return ret;
1782 /* Two helper routines that check for phase 1 and phase 2. These are used
1783 as callbacks for expand_stack_vars. */
1785 static bool
1786 stack_protect_decl_phase_1 (size_t i)
1788 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
1791 static bool
1792 stack_protect_decl_phase_2 (size_t i)
1794 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
1797 /* And helper function that checks for asan phase (with stack protector
1798 it is phase 3). This is used as callback for expand_stack_vars.
1799 Returns true if any of the vars in the partition need to be protected. */
1801 static bool
1802 asan_decl_phase_3 (size_t i)
1804 while (i != EOC)
1806 if (asan_protect_stack_decl (stack_vars[i].decl))
1807 return true;
1808 i = stack_vars[i].next;
1810 return false;
1813 /* Ensure that variables in different stack protection phases conflict
1814 so that they are not merged and share the same stack slot. */
1816 static void
1817 add_stack_protection_conflicts (void)
1819 size_t i, j, n = stack_vars_num;
1820 unsigned char *phase;
1822 phase = XNEWVEC (unsigned char, n);
1823 for (i = 0; i < n; ++i)
1824 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1826 for (i = 0; i < n; ++i)
1828 unsigned char ph_i = phase[i];
1829 for (j = i + 1; j < n; ++j)
1830 if (ph_i != phase[j])
1831 add_stack_var_conflict (i, j);
1834 XDELETEVEC (phase);
1837 /* Create a decl for the guard at the top of the stack frame. */
1839 static void
1840 create_stack_guard (void)
1842 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1843 VAR_DECL, NULL, ptr_type_node);
1844 TREE_THIS_VOLATILE (guard) = 1;
1845 TREE_USED (guard) = 1;
1846 expand_one_stack_var (guard);
1847 crtl->stack_protect_guard = guard;
1850 /* Prepare for expanding variables. */
1851 static void
1852 init_vars_expansion (void)
1854 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
1855 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
1857 /* A map from decl to stack partition. */
1858 decl_to_stack_part = new hash_map<tree, size_t>;
1860 /* Initialize local stack smashing state. */
1861 has_protected_decls = false;
1862 has_short_buffer = false;
1865 /* Free up stack variable graph data. */
1866 static void
1867 fini_vars_expansion (void)
1869 bitmap_obstack_release (&stack_var_bitmap_obstack);
1870 if (stack_vars)
1871 XDELETEVEC (stack_vars);
1872 if (stack_vars_sorted)
1873 XDELETEVEC (stack_vars_sorted);
1874 stack_vars = NULL;
1875 stack_vars_sorted = NULL;
1876 stack_vars_alloc = stack_vars_num = 0;
1877 delete decl_to_stack_part;
1878 decl_to_stack_part = NULL;
1881 /* Make a fair guess for the size of the stack frame of the function
1882 in NODE. This doesn't have to be exact, the result is only used in
1883 the inline heuristics. So we don't want to run the full stack var
1884 packing algorithm (which is quadratic in the number of stack vars).
1885 Instead, we calculate the total size of all stack vars. This turns
1886 out to be a pretty fair estimate -- packing of stack vars doesn't
1887 happen very often. */
1889 HOST_WIDE_INT
1890 estimated_stack_frame_size (struct cgraph_node *node)
1892 HOST_WIDE_INT size = 0;
1893 size_t i;
1894 tree var;
1895 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1897 push_cfun (fn);
1899 init_vars_expansion ();
1901 FOR_EACH_LOCAL_DECL (fn, i, var)
1902 if (auto_var_in_fn_p (var, fn->decl))
1903 size += expand_one_var (var, true, false);
1905 if (stack_vars_num > 0)
1907 /* Fake sorting the stack vars for account_stack_vars (). */
1908 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1909 for (i = 0; i < stack_vars_num; ++i)
1910 stack_vars_sorted[i] = i;
1911 size += account_stack_vars ();
1914 fini_vars_expansion ();
1915 pop_cfun ();
1916 return size;
1919 /* Helper routine to check if a record or union contains an array field. */
1921 static int
1922 record_or_union_type_has_array_p (const_tree tree_type)
1924 tree fields = TYPE_FIELDS (tree_type);
1925 tree f;
1927 for (f = fields; f; f = DECL_CHAIN (f))
1928 if (TREE_CODE (f) == FIELD_DECL)
1930 tree field_type = TREE_TYPE (f);
1931 if (RECORD_OR_UNION_TYPE_P (field_type)
1932 && record_or_union_type_has_array_p (field_type))
1933 return 1;
1934 if (TREE_CODE (field_type) == ARRAY_TYPE)
1935 return 1;
1937 return 0;
1940 /* Check if the current function has local referenced variables that
1941 have their addresses taken, contain an array, or are arrays. */
1943 static bool
1944 stack_protect_decl_p ()
1946 unsigned i;
1947 tree var;
1949 FOR_EACH_LOCAL_DECL (cfun, i, var)
1950 if (!is_global_var (var))
1952 tree var_type = TREE_TYPE (var);
1953 if (TREE_CODE (var) == VAR_DECL
1954 && (TREE_CODE (var_type) == ARRAY_TYPE
1955 || TREE_ADDRESSABLE (var)
1956 || (RECORD_OR_UNION_TYPE_P (var_type)
1957 && record_or_union_type_has_array_p (var_type))))
1958 return true;
1960 return false;
1963 /* Check if the current function has calls that use a return slot. */
1965 static bool
1966 stack_protect_return_slot_p ()
1968 basic_block bb;
1970 FOR_ALL_BB_FN (bb, cfun)
1971 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1972 !gsi_end_p (gsi); gsi_next (&gsi))
1974 gimple *stmt = gsi_stmt (gsi);
1975 /* This assumes that calls to internal-only functions never
1976 use a return slot. */
1977 if (is_gimple_call (stmt)
1978 && !gimple_call_internal_p (stmt)
1979 && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
1980 gimple_call_fndecl (stmt)))
1981 return true;
1983 return false;
1986 /* Expand all variables used in the function. */
1988 static rtx_insn *
1989 expand_used_vars (void)
1991 tree var, outer_block = DECL_INITIAL (current_function_decl);
1992 vec<tree> maybe_local_decls = vNULL;
1993 rtx_insn *var_end_seq = NULL;
1994 unsigned i;
1995 unsigned len;
1996 bool gen_stack_protect_signal = false;
1998 /* Compute the phase of the stack frame for this function. */
2000 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2001 int off = STARTING_FRAME_OFFSET % align;
2002 frame_phase = off ? align - off : 0;
2005 /* Set TREE_USED on all variables in the local_decls. */
2006 FOR_EACH_LOCAL_DECL (cfun, i, var)
2007 TREE_USED (var) = 1;
2008 /* Clear TREE_USED on all variables associated with a block scope. */
2009 clear_tree_used (DECL_INITIAL (current_function_decl));
2011 init_vars_expansion ();
2013 if (targetm.use_pseudo_pic_reg ())
2014 pic_offset_table_rtx = gen_reg_rtx (Pmode);
2016 for (i = 0; i < SA.map->num_partitions; i++)
2018 if (bitmap_bit_p (SA.partitions_for_parm_default_defs, i))
2019 continue;
2021 tree var = partition_to_var (SA.map, i);
2023 gcc_assert (!virtual_operand_p (var));
2025 expand_one_ssa_partition (var);
2028 if (flag_stack_protect == SPCT_FLAG_STRONG)
2029 gen_stack_protect_signal
2030 = stack_protect_decl_p () || stack_protect_return_slot_p ();
2032 /* At this point all variables on the local_decls with TREE_USED
2033 set are not associated with any block scope. Lay them out. */
2035 len = vec_safe_length (cfun->local_decls);
2036 FOR_EACH_LOCAL_DECL (cfun, i, var)
2038 bool expand_now = false;
2040 /* Expanded above already. */
2041 if (is_gimple_reg (var))
2043 TREE_USED (var) = 0;
2044 goto next;
2046 /* We didn't set a block for static or extern because it's hard
2047 to tell the difference between a global variable (re)declared
2048 in a local scope, and one that's really declared there to
2049 begin with. And it doesn't really matter much, since we're
2050 not giving them stack space. Expand them now. */
2051 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
2052 expand_now = true;
2054 /* Expand variables not associated with any block now. Those created by
2055 the optimizers could be live anywhere in the function. Those that
2056 could possibly have been scoped originally and detached from their
2057 block will have their allocation deferred so we coalesce them with
2058 others when optimization is enabled. */
2059 else if (TREE_USED (var))
2060 expand_now = true;
2062 /* Finally, mark all variables on the list as used. We'll use
2063 this in a moment when we expand those associated with scopes. */
2064 TREE_USED (var) = 1;
2066 if (expand_now)
2067 expand_one_var (var, true, true);
2069 next:
2070 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
2072 rtx rtl = DECL_RTL_IF_SET (var);
2074 /* Keep artificial non-ignored vars in cfun->local_decls
2075 chain until instantiate_decls. */
2076 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2077 add_local_decl (cfun, var);
2078 else if (rtl == NULL_RTX)
2079 /* If rtl isn't set yet, which can happen e.g. with
2080 -fstack-protector, retry before returning from this
2081 function. */
2082 maybe_local_decls.safe_push (var);
2086 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
2088 +-----------------+-----------------+
2089 | ...processed... | ...duplicates...|
2090 +-----------------+-----------------+
2092 +-- LEN points here.
2094 We just want the duplicates, as those are the artificial
2095 non-ignored vars that we want to keep until instantiate_decls.
2096 Move them down and truncate the array. */
2097 if (!vec_safe_is_empty (cfun->local_decls))
2098 cfun->local_decls->block_remove (0, len);
2100 /* At this point, all variables within the block tree with TREE_USED
2101 set are actually used by the optimized function. Lay them out. */
2102 expand_used_vars_for_block (outer_block, true);
2104 if (stack_vars_num > 0)
2106 add_scope_conflicts ();
2108 /* If stack protection is enabled, we don't share space between
2109 vulnerable data and non-vulnerable data. */
2110 if (flag_stack_protect != 0
2111 && (flag_stack_protect != SPCT_FLAG_EXPLICIT
2112 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
2113 && lookup_attribute ("stack_protect",
2114 DECL_ATTRIBUTES (current_function_decl)))))
2115 add_stack_protection_conflicts ();
2117 /* Now that we have collected all stack variables, and have computed a
2118 minimal interference graph, attempt to save some stack space. */
2119 partition_stack_vars ();
2120 if (dump_file)
2121 dump_stack_var_partition ();
2124 switch (flag_stack_protect)
2126 case SPCT_FLAG_ALL:
2127 create_stack_guard ();
2128 break;
2130 case SPCT_FLAG_STRONG:
2131 if (gen_stack_protect_signal
2132 || cfun->calls_alloca || has_protected_decls
2133 || lookup_attribute ("stack_protect",
2134 DECL_ATTRIBUTES (current_function_decl)))
2135 create_stack_guard ();
2136 break;
2138 case SPCT_FLAG_DEFAULT:
2139 if (cfun->calls_alloca || has_protected_decls
2140 || lookup_attribute ("stack_protect",
2141 DECL_ATTRIBUTES (current_function_decl)))
2142 create_stack_guard ();
2143 break;
2145 case SPCT_FLAG_EXPLICIT:
2146 if (lookup_attribute ("stack_protect",
2147 DECL_ATTRIBUTES (current_function_decl)))
2148 create_stack_guard ();
2149 break;
2150 default:
2154 /* Assign rtl to each variable based on these partitions. */
2155 if (stack_vars_num > 0)
2157 struct stack_vars_data data;
2159 data.asan_vec = vNULL;
2160 data.asan_decl_vec = vNULL;
2161 data.asan_base = NULL_RTX;
2162 data.asan_alignb = 0;
2164 /* Reorder decls to be protected by iterating over the variables
2165 array multiple times, and allocating out of each phase in turn. */
2166 /* ??? We could probably integrate this into the qsort we did
2167 earlier, such that we naturally see these variables first,
2168 and thus naturally allocate things in the right order. */
2169 if (has_protected_decls)
2171 /* Phase 1 contains only character arrays. */
2172 expand_stack_vars (stack_protect_decl_phase_1, &data);
2174 /* Phase 2 contains other kinds of arrays. */
2175 if (flag_stack_protect == SPCT_FLAG_ALL
2176 || flag_stack_protect == SPCT_FLAG_STRONG
2177 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
2178 && lookup_attribute ("stack_protect",
2179 DECL_ATTRIBUTES (current_function_decl))))
2180 expand_stack_vars (stack_protect_decl_phase_2, &data);
2183 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK)
2184 /* Phase 3, any partitions that need asan protection
2185 in addition to phase 1 and 2. */
2186 expand_stack_vars (asan_decl_phase_3, &data);
2188 if (!data.asan_vec.is_empty ())
2190 HOST_WIDE_INT prev_offset = frame_offset;
2191 HOST_WIDE_INT offset, sz, redzonesz;
2192 redzonesz = ASAN_RED_ZONE_SIZE;
2193 sz = data.asan_vec[0] - prev_offset;
2194 if (data.asan_alignb > ASAN_RED_ZONE_SIZE
2195 && data.asan_alignb <= 4096
2196 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
2197 redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
2198 & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
2199 offset
2200 = alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE);
2201 data.asan_vec.safe_push (prev_offset);
2202 data.asan_vec.safe_push (offset);
2203 /* Leave space for alignment if STRICT_ALIGNMENT. */
2204 if (STRICT_ALIGNMENT)
2205 alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
2206 << ASAN_SHADOW_SHIFT)
2207 / BITS_PER_UNIT, 1);
2209 var_end_seq
2210 = asan_emit_stack_protection (virtual_stack_vars_rtx,
2211 data.asan_base,
2212 data.asan_alignb,
2213 data.asan_vec.address (),
2214 data.asan_decl_vec.address (),
2215 data.asan_vec.length ());
2218 expand_stack_vars (NULL, &data);
2220 data.asan_vec.release ();
2221 data.asan_decl_vec.release ();
2224 fini_vars_expansion ();
2226 /* If there were any artificial non-ignored vars without rtl
2227 found earlier, see if deferred stack allocation hasn't assigned
2228 rtl to them. */
2229 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
2231 rtx rtl = DECL_RTL_IF_SET (var);
2233 /* Keep artificial non-ignored vars in cfun->local_decls
2234 chain until instantiate_decls. */
2235 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2236 add_local_decl (cfun, var);
2238 maybe_local_decls.release ();
2240 /* If the target requires that FRAME_OFFSET be aligned, do it. */
2241 if (STACK_ALIGNMENT_NEEDED)
2243 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2244 if (!FRAME_GROWS_DOWNWARD)
2245 frame_offset += align - 1;
2246 frame_offset &= -align;
2249 return var_end_seq;
2253 /* If we need to produce a detailed dump, print the tree representation
2254 for STMT to the dump file. SINCE is the last RTX after which the RTL
2255 generated for STMT should have been appended. */
2257 static void
2258 maybe_dump_rtl_for_gimple_stmt (gimple *stmt, rtx_insn *since)
2260 if (dump_file && (dump_flags & TDF_DETAILS))
2262 fprintf (dump_file, "\n;; ");
2263 print_gimple_stmt (dump_file, stmt, 0,
2264 TDF_SLIM | (dump_flags & TDF_LINENO));
2265 fprintf (dump_file, "\n");
2267 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
2271 /* Maps the blocks that do not contain tree labels to rtx labels. */
2273 static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
2275 /* Returns the label_rtx expression for a label starting basic block BB. */
2277 static rtx_code_label *
2278 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
2280 gimple_stmt_iterator gsi;
2281 tree lab;
2283 if (bb->flags & BB_RTL)
2284 return block_label (bb);
2286 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
2287 if (elt)
2288 return *elt;
2290 /* Find the tree label if it is present. */
2292 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2294 glabel *lab_stmt;
2296 lab_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
2297 if (!lab_stmt)
2298 break;
2300 lab = gimple_label_label (lab_stmt);
2301 if (DECL_NONLOCAL (lab))
2302 break;
2304 return jump_target_rtx (lab);
2307 rtx_code_label *l = gen_label_rtx ();
2308 lab_rtx_for_bb->put (bb, l);
2309 return l;
2313 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2314 of a basic block where we just expanded the conditional at the end,
2315 possibly clean up the CFG and instruction sequence. LAST is the
2316 last instruction before the just emitted jump sequence. */
2318 static void
2319 maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2321 /* Special case: when jumpif decides that the condition is
2322 trivial it emits an unconditional jump (and the necessary
2323 barrier). But we still have two edges, the fallthru one is
2324 wrong. purge_dead_edges would clean this up later. Unfortunately
2325 we have to insert insns (and split edges) before
2326 find_many_sub_basic_blocks and hence before purge_dead_edges.
2327 But splitting edges might create new blocks which depend on the
2328 fact that if there are two edges there's no barrier. So the
2329 barrier would get lost and verify_flow_info would ICE. Instead
2330 of auditing all edge splitters to care for the barrier (which
2331 normally isn't there in a cleaned CFG), fix it here. */
2332 if (BARRIER_P (get_last_insn ()))
2334 rtx_insn *insn;
2335 remove_edge (e);
2336 /* Now, we have a single successor block, if we have insns to
2337 insert on the remaining edge we potentially will insert
2338 it at the end of this block (if the dest block isn't feasible)
2339 in order to avoid splitting the edge. This insertion will take
2340 place in front of the last jump. But we might have emitted
2341 multiple jumps (conditional and one unconditional) to the
2342 same destination. Inserting in front of the last one then
2343 is a problem. See PR 40021. We fix this by deleting all
2344 jumps except the last unconditional one. */
2345 insn = PREV_INSN (get_last_insn ());
2346 /* Make sure we have an unconditional jump. Otherwise we're
2347 confused. */
2348 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2349 for (insn = PREV_INSN (insn); insn != last;)
2351 insn = PREV_INSN (insn);
2352 if (JUMP_P (NEXT_INSN (insn)))
2354 if (!any_condjump_p (NEXT_INSN (insn)))
2356 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2357 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2359 delete_insn (NEXT_INSN (insn));
2365 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2366 Returns a new basic block if we've terminated the current basic
2367 block and created a new one. */
2369 static basic_block
2370 expand_gimple_cond (basic_block bb, gcond *stmt)
2372 basic_block new_bb, dest;
2373 edge new_edge;
2374 edge true_edge;
2375 edge false_edge;
2376 rtx_insn *last2, *last;
2377 enum tree_code code;
2378 tree op0, op1;
2380 code = gimple_cond_code (stmt);
2381 op0 = gimple_cond_lhs (stmt);
2382 op1 = gimple_cond_rhs (stmt);
2383 /* We're sometimes presented with such code:
2384 D.123_1 = x < y;
2385 if (D.123_1 != 0)
2387 This would expand to two comparisons which then later might
2388 be cleaned up by combine. But some pattern matchers like if-conversion
2389 work better when there's only one compare, so make up for this
2390 here as special exception if TER would have made the same change. */
2391 if (SA.values
2392 && TREE_CODE (op0) == SSA_NAME
2393 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2394 && TREE_CODE (op1) == INTEGER_CST
2395 && ((gimple_cond_code (stmt) == NE_EXPR
2396 && integer_zerop (op1))
2397 || (gimple_cond_code (stmt) == EQ_EXPR
2398 && integer_onep (op1)))
2399 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2401 gimple *second = SSA_NAME_DEF_STMT (op0);
2402 if (gimple_code (second) == GIMPLE_ASSIGN)
2404 enum tree_code code2 = gimple_assign_rhs_code (second);
2405 if (TREE_CODE_CLASS (code2) == tcc_comparison)
2407 code = code2;
2408 op0 = gimple_assign_rhs1 (second);
2409 op1 = gimple_assign_rhs2 (second);
2411 /* If jumps are cheap and the target does not support conditional
2412 compare, turn some more codes into jumpy sequences. */
2413 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4
2414 && targetm.gen_ccmp_first == NULL)
2416 if ((code2 == BIT_AND_EXPR
2417 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2418 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2419 || code2 == TRUTH_AND_EXPR)
2421 code = TRUTH_ANDIF_EXPR;
2422 op0 = gimple_assign_rhs1 (second);
2423 op1 = gimple_assign_rhs2 (second);
2425 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2427 code = TRUTH_ORIF_EXPR;
2428 op0 = gimple_assign_rhs1 (second);
2429 op1 = gimple_assign_rhs2 (second);
2435 last2 = last = get_last_insn ();
2437 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2438 set_curr_insn_location (gimple_location (stmt));
2440 /* These flags have no purpose in RTL land. */
2441 true_edge->flags &= ~EDGE_TRUE_VALUE;
2442 false_edge->flags &= ~EDGE_FALSE_VALUE;
2444 /* We can either have a pure conditional jump with one fallthru edge or
2445 two-way jump that needs to be decomposed into two basic blocks. */
2446 if (false_edge->dest == bb->next_bb)
2448 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2449 true_edge->probability);
2450 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2451 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2452 set_curr_insn_location (true_edge->goto_locus);
2453 false_edge->flags |= EDGE_FALLTHRU;
2454 maybe_cleanup_end_of_block (false_edge, last);
2455 return NULL;
2457 if (true_edge->dest == bb->next_bb)
2459 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2460 false_edge->probability);
2461 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2462 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2463 set_curr_insn_location (false_edge->goto_locus);
2464 true_edge->flags |= EDGE_FALLTHRU;
2465 maybe_cleanup_end_of_block (true_edge, last);
2466 return NULL;
2469 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2470 true_edge->probability);
2471 last = get_last_insn ();
2472 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2473 set_curr_insn_location (false_edge->goto_locus);
2474 emit_jump (label_rtx_for_bb (false_edge->dest));
2476 BB_END (bb) = last;
2477 if (BARRIER_P (BB_END (bb)))
2478 BB_END (bb) = PREV_INSN (BB_END (bb));
2479 update_bb_for_insn (bb);
2481 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2482 dest = false_edge->dest;
2483 redirect_edge_succ (false_edge, new_bb);
2484 false_edge->flags |= EDGE_FALLTHRU;
2485 new_bb->count = false_edge->count;
2486 new_bb->frequency = EDGE_FREQUENCY (false_edge);
2487 add_bb_to_loop (new_bb, bb->loop_father);
2488 new_edge = make_edge (new_bb, dest, 0);
2489 new_edge->probability = REG_BR_PROB_BASE;
2490 new_edge->count = new_bb->count;
2491 if (BARRIER_P (BB_END (new_bb)))
2492 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2493 update_bb_for_insn (new_bb);
2495 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2497 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2499 set_curr_insn_location (true_edge->goto_locus);
2500 true_edge->goto_locus = curr_insn_location ();
2503 return new_bb;
2506 /* Mark all calls that can have a transaction restart. */
2508 static void
2509 mark_transaction_restart_calls (gimple *stmt)
2511 struct tm_restart_node dummy;
2512 tm_restart_node **slot;
2514 if (!cfun->gimple_df->tm_restart)
2515 return;
2517 dummy.stmt = stmt;
2518 slot = cfun->gimple_df->tm_restart->find_slot (&dummy, NO_INSERT);
2519 if (slot)
2521 struct tm_restart_node *n = *slot;
2522 tree list = n->label_or_list;
2523 rtx_insn *insn;
2525 for (insn = next_real_insn (get_last_insn ());
2526 !CALL_P (insn);
2527 insn = next_real_insn (insn))
2528 continue;
2530 if (TREE_CODE (list) == LABEL_DECL)
2531 add_reg_note (insn, REG_TM, label_rtx (list));
2532 else
2533 for (; list ; list = TREE_CHAIN (list))
2534 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2538 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2539 statement STMT. */
2541 static void
2542 expand_call_stmt (gcall *stmt)
2544 tree exp, decl, lhs;
2545 bool builtin_p;
2546 size_t i;
2548 if (gimple_call_internal_p (stmt))
2550 expand_internal_call (stmt);
2551 return;
2554 /* If this is a call to a built-in function and it has no effect other
2555 than setting the lhs, try to implement it using an internal function
2556 instead. */
2557 decl = gimple_call_fndecl (stmt);
2558 if (gimple_call_lhs (stmt)
2559 && !gimple_has_side_effects (stmt)
2560 && (optimize || (decl && called_as_built_in (decl))))
2562 internal_fn ifn = replacement_internal_fn (stmt);
2563 if (ifn != IFN_LAST)
2565 expand_internal_call (ifn, stmt);
2566 return;
2570 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2572 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2573 builtin_p = decl && DECL_BUILT_IN (decl);
2575 /* If this is not a builtin function, the function type through which the
2576 call is made may be different from the type of the function. */
2577 if (!builtin_p)
2578 CALL_EXPR_FN (exp)
2579 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2580 CALL_EXPR_FN (exp));
2582 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2583 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2585 for (i = 0; i < gimple_call_num_args (stmt); i++)
2587 tree arg = gimple_call_arg (stmt, i);
2588 gimple *def;
2589 /* TER addresses into arguments of builtin functions so we have a
2590 chance to infer more correct alignment information. See PR39954. */
2591 if (builtin_p
2592 && TREE_CODE (arg) == SSA_NAME
2593 && (def = get_gimple_for_ssa_name (arg))
2594 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2595 arg = gimple_assign_rhs1 (def);
2596 CALL_EXPR_ARG (exp, i) = arg;
2599 if (gimple_has_side_effects (stmt))
2600 TREE_SIDE_EFFECTS (exp) = 1;
2602 if (gimple_call_nothrow_p (stmt))
2603 TREE_NOTHROW (exp) = 1;
2605 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2606 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2607 if (decl
2608 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2609 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2610 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2611 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2612 else
2613 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2614 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2615 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2616 CALL_WITH_BOUNDS_P (exp) = gimple_call_with_bounds_p (stmt);
2618 /* Ensure RTL is created for debug args. */
2619 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2621 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2622 unsigned int ix;
2623 tree dtemp;
2625 if (debug_args)
2626 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2628 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2629 expand_debug_expr (dtemp);
2633 lhs = gimple_call_lhs (stmt);
2634 if (lhs)
2635 expand_assignment (lhs, exp, false);
2636 else
2637 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2639 mark_transaction_restart_calls (stmt);
2643 /* Generate RTL for an asm statement (explicit assembler code).
2644 STRING is a STRING_CST node containing the assembler code text,
2645 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
2646 insn is volatile; don't optimize it. */
2648 static void
2649 expand_asm_loc (tree string, int vol, location_t locus)
2651 rtx body;
2653 if (TREE_CODE (string) == ADDR_EXPR)
2654 string = TREE_OPERAND (string, 0);
2656 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2657 ggc_strdup (TREE_STRING_POINTER (string)),
2658 locus);
2660 MEM_VOLATILE_P (body) = vol;
2662 emit_insn (body);
2665 /* Return the number of times character C occurs in string S. */
2666 static int
2667 n_occurrences (int c, const char *s)
2669 int n = 0;
2670 while (*s)
2671 n += (*s++ == c);
2672 return n;
2675 /* A subroutine of expand_asm_operands. Check that all operands have
2676 the same number of alternatives. Return true if so. */
2678 static bool
2679 check_operand_nalternatives (const vec<const char *> &constraints)
2681 unsigned len = constraints.length();
2682 if (len > 0)
2684 int nalternatives = n_occurrences (',', constraints[0]);
2686 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2688 error ("too many alternatives in %<asm%>");
2689 return false;
2692 for (unsigned i = 1; i < len; ++i)
2693 if (n_occurrences (',', constraints[i]) != nalternatives)
2695 error ("operand constraints for %<asm%> differ "
2696 "in number of alternatives");
2697 return false;
2700 return true;
2703 /* Check for overlap between registers marked in CLOBBERED_REGS and
2704 anything inappropriate in T. Emit error and return the register
2705 variable definition for error, NULL_TREE for ok. */
2707 static bool
2708 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
2710 /* Conflicts between asm-declared register variables and the clobber
2711 list are not allowed. */
2712 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2714 if (overlap)
2716 error ("asm-specifier for variable %qE conflicts with asm clobber list",
2717 DECL_NAME (overlap));
2719 /* Reset registerness to stop multiple errors emitted for a single
2720 variable. */
2721 DECL_REGISTER (overlap) = 0;
2722 return true;
2725 return false;
2728 /* Generate RTL for an asm statement with arguments.
2729 STRING is the instruction template.
2730 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
2731 Each output or input has an expression in the TREE_VALUE and
2732 a tree list in TREE_PURPOSE which in turn contains a constraint
2733 name in TREE_VALUE (or NULL_TREE) and a constraint string
2734 in TREE_PURPOSE.
2735 CLOBBERS is a list of STRING_CST nodes each naming a hard register
2736 that is clobbered by this insn.
2738 LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
2739 should be the fallthru basic block of the asm goto.
2741 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
2742 Some elements of OUTPUTS may be replaced with trees representing temporary
2743 values. The caller should copy those temporary values to the originally
2744 specified lvalues.
2746 VOL nonzero means the insn is volatile; don't optimize it. */
2748 static void
2749 expand_asm_stmt (gasm *stmt)
2751 class save_input_location
2753 location_t old;
2755 public:
2756 explicit save_input_location(location_t where)
2758 old = input_location;
2759 input_location = where;
2762 ~save_input_location()
2764 input_location = old;
2768 location_t locus = gimple_location (stmt);
2770 if (gimple_asm_input_p (stmt))
2772 const char *s = gimple_asm_string (stmt);
2773 tree string = build_string (strlen (s), s);
2774 expand_asm_loc (string, gimple_asm_volatile_p (stmt), locus);
2775 return;
2778 /* There are some legacy diagnostics in here, and also avoids a
2779 sixth parameger to targetm.md_asm_adjust. */
2780 save_input_location s_i_l(locus);
2782 unsigned noutputs = gimple_asm_noutputs (stmt);
2783 unsigned ninputs = gimple_asm_ninputs (stmt);
2784 unsigned nlabels = gimple_asm_nlabels (stmt);
2785 unsigned i;
2787 /* ??? Diagnose during gimplification? */
2788 if (ninputs + noutputs + nlabels > MAX_RECOG_OPERANDS)
2790 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
2791 return;
2794 auto_vec<tree, MAX_RECOG_OPERANDS> output_tvec;
2795 auto_vec<tree, MAX_RECOG_OPERANDS> input_tvec;
2796 auto_vec<const char *, MAX_RECOG_OPERANDS> constraints;
2798 /* Copy the gimple vectors into new vectors that we can manipulate. */
2800 output_tvec.safe_grow (noutputs);
2801 input_tvec.safe_grow (ninputs);
2802 constraints.safe_grow (noutputs + ninputs);
2804 for (i = 0; i < noutputs; ++i)
2806 tree t = gimple_asm_output_op (stmt, i);
2807 output_tvec[i] = TREE_VALUE (t);
2808 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2810 for (i = 0; i < ninputs; i++)
2812 tree t = gimple_asm_input_op (stmt, i);
2813 input_tvec[i] = TREE_VALUE (t);
2814 constraints[i + noutputs]
2815 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2818 /* ??? Diagnose during gimplification? */
2819 if (! check_operand_nalternatives (constraints))
2820 return;
2822 /* Count the number of meaningful clobbered registers, ignoring what
2823 we would ignore later. */
2824 auto_vec<rtx> clobber_rvec;
2825 HARD_REG_SET clobbered_regs;
2826 CLEAR_HARD_REG_SET (clobbered_regs);
2828 if (unsigned n = gimple_asm_nclobbers (stmt))
2830 clobber_rvec.reserve (n);
2831 for (i = 0; i < n; i++)
2833 tree t = gimple_asm_clobber_op (stmt, i);
2834 const char *regname = TREE_STRING_POINTER (TREE_VALUE (t));
2835 int nregs, j;
2837 j = decode_reg_name_and_count (regname, &nregs);
2838 if (j < 0)
2840 if (j == -2)
2842 /* ??? Diagnose during gimplification? */
2843 error ("unknown register name %qs in %<asm%>", regname);
2845 else if (j == -4)
2847 rtx x = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2848 clobber_rvec.safe_push (x);
2850 else
2852 /* Otherwise we should have -1 == empty string
2853 or -3 == cc, which is not a register. */
2854 gcc_assert (j == -1 || j == -3);
2857 else
2858 for (int reg = j; reg < j + nregs; reg++)
2860 /* Clobbering the PIC register is an error. */
2861 if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
2863 /* ??? Diagnose during gimplification? */
2864 error ("PIC register clobbered by %qs in %<asm%>",
2865 regname);
2866 return;
2869 SET_HARD_REG_BIT (clobbered_regs, reg);
2870 rtx x = gen_rtx_REG (reg_raw_mode[reg], reg);
2871 clobber_rvec.safe_push (x);
2875 unsigned nclobbers = clobber_rvec.length();
2877 /* First pass over inputs and outputs checks validity and sets
2878 mark_addressable if needed. */
2879 /* ??? Diagnose during gimplification? */
2881 for (i = 0; i < noutputs; ++i)
2883 tree val = output_tvec[i];
2884 tree type = TREE_TYPE (val);
2885 const char *constraint;
2886 bool is_inout;
2887 bool allows_reg;
2888 bool allows_mem;
2890 /* Try to parse the output constraint. If that fails, there's
2891 no point in going further. */
2892 constraint = constraints[i];
2893 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
2894 &allows_mem, &allows_reg, &is_inout))
2895 return;
2897 if (! allows_reg
2898 && (allows_mem
2899 || is_inout
2900 || (DECL_P (val)
2901 && REG_P (DECL_RTL (val))
2902 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
2903 mark_addressable (val);
2906 for (i = 0; i < ninputs; ++i)
2908 bool allows_reg, allows_mem;
2909 const char *constraint;
2911 constraint = constraints[i + noutputs];
2912 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2913 constraints.address (),
2914 &allows_mem, &allows_reg))
2915 return;
2917 if (! allows_reg && allows_mem)
2918 mark_addressable (input_tvec[i]);
2921 /* Second pass evaluates arguments. */
2923 /* Make sure stack is consistent for asm goto. */
2924 if (nlabels > 0)
2925 do_pending_stack_adjust ();
2926 int old_generating_concat_p = generating_concat_p;
2928 /* Vector of RTX's of evaluated output operands. */
2929 auto_vec<rtx, MAX_RECOG_OPERANDS> output_rvec;
2930 auto_vec<int, MAX_RECOG_OPERANDS> inout_opnum;
2931 rtx_insn *after_rtl_seq = NULL, *after_rtl_end = NULL;
2933 output_rvec.safe_grow (noutputs);
2935 for (i = 0; i < noutputs; ++i)
2937 tree val = output_tvec[i];
2938 tree type = TREE_TYPE (val);
2939 bool is_inout, allows_reg, allows_mem, ok;
2940 rtx op;
2942 ok = parse_output_constraint (&constraints[i], i, ninputs,
2943 noutputs, &allows_mem, &allows_reg,
2944 &is_inout);
2945 gcc_assert (ok);
2947 /* If an output operand is not a decl or indirect ref and our constraint
2948 allows a register, make a temporary to act as an intermediate.
2949 Make the asm insn write into that, then we will copy it to
2950 the real output operand. Likewise for promoted variables. */
2952 generating_concat_p = 0;
2954 if ((TREE_CODE (val) == INDIRECT_REF
2955 && allows_mem)
2956 || (DECL_P (val)
2957 && (allows_mem || REG_P (DECL_RTL (val)))
2958 && ! (REG_P (DECL_RTL (val))
2959 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
2960 || ! allows_reg
2961 || is_inout)
2963 op = expand_expr (val, NULL_RTX, VOIDmode,
2964 !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
2965 if (MEM_P (op))
2966 op = validize_mem (op);
2968 if (! allows_reg && !MEM_P (op))
2969 error ("output number %d not directly addressable", i);
2970 if ((! allows_mem && MEM_P (op))
2971 || GET_CODE (op) == CONCAT)
2973 rtx old_op = op;
2974 op = gen_reg_rtx (GET_MODE (op));
2976 generating_concat_p = old_generating_concat_p;
2978 if (is_inout)
2979 emit_move_insn (op, old_op);
2981 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2982 emit_move_insn (old_op, op);
2983 after_rtl_seq = get_insns ();
2984 after_rtl_end = get_last_insn ();
2985 end_sequence ();
2988 else
2990 op = assign_temp (type, 0, 1);
2991 op = validize_mem (op);
2992 if (!MEM_P (op) && TREE_CODE (val) == SSA_NAME)
2993 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (val), op);
2995 generating_concat_p = old_generating_concat_p;
2997 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2998 expand_assignment (val, make_tree (type, op), false);
2999 after_rtl_seq = get_insns ();
3000 after_rtl_end = get_last_insn ();
3001 end_sequence ();
3003 output_rvec[i] = op;
3005 if (is_inout)
3006 inout_opnum.safe_push (i);
3009 auto_vec<rtx, MAX_RECOG_OPERANDS> input_rvec;
3010 auto_vec<machine_mode, MAX_RECOG_OPERANDS> input_mode;
3012 input_rvec.safe_grow (ninputs);
3013 input_mode.safe_grow (ninputs);
3015 generating_concat_p = 0;
3017 for (i = 0; i < ninputs; ++i)
3019 tree val = input_tvec[i];
3020 tree type = TREE_TYPE (val);
3021 bool allows_reg, allows_mem, ok;
3022 const char *constraint;
3023 rtx op;
3025 constraint = constraints[i + noutputs];
3026 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
3027 constraints.address (),
3028 &allows_mem, &allows_reg);
3029 gcc_assert (ok);
3031 /* EXPAND_INITIALIZER will not generate code for valid initializer
3032 constants, but will still generate code for other types of operand.
3033 This is the behavior we want for constant constraints. */
3034 op = expand_expr (val, NULL_RTX, VOIDmode,
3035 allows_reg ? EXPAND_NORMAL
3036 : allows_mem ? EXPAND_MEMORY
3037 : EXPAND_INITIALIZER);
3039 /* Never pass a CONCAT to an ASM. */
3040 if (GET_CODE (op) == CONCAT)
3041 op = force_reg (GET_MODE (op), op);
3042 else if (MEM_P (op))
3043 op = validize_mem (op);
3045 if (asm_operand_ok (op, constraint, NULL) <= 0)
3047 if (allows_reg && TYPE_MODE (type) != BLKmode)
3048 op = force_reg (TYPE_MODE (type), op);
3049 else if (!allows_mem)
3050 warning (0, "asm operand %d probably doesn%'t match constraints",
3051 i + noutputs);
3052 else if (MEM_P (op))
3054 /* We won't recognize either volatile memory or memory
3055 with a queued address as available a memory_operand
3056 at this point. Ignore it: clearly this *is* a memory. */
3058 else
3059 gcc_unreachable ();
3061 input_rvec[i] = op;
3062 input_mode[i] = TYPE_MODE (type);
3065 /* For in-out operands, copy output rtx to input rtx. */
3066 unsigned ninout = inout_opnum.length();
3067 for (i = 0; i < ninout; i++)
3069 int j = inout_opnum[i];
3070 rtx o = output_rvec[j];
3072 input_rvec.safe_push (o);
3073 input_mode.safe_push (GET_MODE (o));
3075 char buffer[16];
3076 sprintf (buffer, "%d", j);
3077 constraints.safe_push (ggc_strdup (buffer));
3079 ninputs += ninout;
3081 /* Sometimes we wish to automatically clobber registers across an asm.
3082 Case in point is when the i386 backend moved from cc0 to a hard reg --
3083 maintaining source-level compatibility means automatically clobbering
3084 the flags register. */
3085 rtx_insn *after_md_seq = NULL;
3086 if (targetm.md_asm_adjust)
3087 after_md_seq = targetm.md_asm_adjust (output_rvec, input_rvec,
3088 constraints, clobber_rvec,
3089 clobbered_regs);
3091 /* Do not allow the hook to change the output and input count,
3092 lest it mess up the operand numbering. */
3093 gcc_assert (output_rvec.length() == noutputs);
3094 gcc_assert (input_rvec.length() == ninputs);
3095 gcc_assert (constraints.length() == noutputs + ninputs);
3097 /* But it certainly can adjust the clobbers. */
3098 nclobbers = clobber_rvec.length();
3100 /* Third pass checks for easy conflicts. */
3101 /* ??? Why are we doing this on trees instead of rtx. */
3103 bool clobber_conflict_found = 0;
3104 for (i = 0; i < noutputs; ++i)
3105 if (tree_conflicts_with_clobbers_p (output_tvec[i], &clobbered_regs))
3106 clobber_conflict_found = 1;
3107 for (i = 0; i < ninputs - ninout; ++i)
3108 if (tree_conflicts_with_clobbers_p (input_tvec[i], &clobbered_regs))
3109 clobber_conflict_found = 1;
3111 /* Make vectors for the expression-rtx, constraint strings,
3112 and named operands. */
3114 rtvec argvec = rtvec_alloc (ninputs);
3115 rtvec constraintvec = rtvec_alloc (ninputs);
3116 rtvec labelvec = rtvec_alloc (nlabels);
3118 rtx body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
3119 : GET_MODE (output_rvec[0])),
3120 ggc_strdup (gimple_asm_string (stmt)),
3121 empty_string, 0, argvec, constraintvec,
3122 labelvec, locus);
3123 MEM_VOLATILE_P (body) = gimple_asm_volatile_p (stmt);
3125 for (i = 0; i < ninputs; ++i)
3127 ASM_OPERANDS_INPUT (body, i) = input_rvec[i];
3128 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
3129 = gen_rtx_ASM_INPUT_loc (input_mode[i],
3130 constraints[i + noutputs],
3131 locus);
3134 /* Copy labels to the vector. */
3135 rtx_code_label *fallthru_label = NULL;
3136 if (nlabels > 0)
3138 basic_block fallthru_bb = NULL;
3139 edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
3140 if (fallthru)
3141 fallthru_bb = fallthru->dest;
3143 for (i = 0; i < nlabels; ++i)
3145 tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
3146 rtx_insn *r;
3147 /* If asm goto has any labels in the fallthru basic block, use
3148 a label that we emit immediately after the asm goto. Expansion
3149 may insert further instructions into the same basic block after
3150 asm goto and if we don't do this, insertion of instructions on
3151 the fallthru edge might misbehave. See PR58670. */
3152 if (fallthru_bb && label_to_block_fn (cfun, label) == fallthru_bb)
3154 if (fallthru_label == NULL_RTX)
3155 fallthru_label = gen_label_rtx ();
3156 r = fallthru_label;
3158 else
3159 r = label_rtx (label);
3160 ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
3164 /* Now, for each output, construct an rtx
3165 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
3166 ARGVEC CONSTRAINTS OPNAMES))
3167 If there is more than one, put them inside a PARALLEL. */
3169 if (nlabels > 0 && nclobbers == 0)
3171 gcc_assert (noutputs == 0);
3172 emit_jump_insn (body);
3174 else if (noutputs == 0 && nclobbers == 0)
3176 /* No output operands: put in a raw ASM_OPERANDS rtx. */
3177 emit_insn (body);
3179 else if (noutputs == 1 && nclobbers == 0)
3181 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
3182 emit_insn (gen_rtx_SET (output_rvec[0], body));
3184 else
3186 rtx obody = body;
3187 int num = noutputs;
3189 if (num == 0)
3190 num = 1;
3192 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
3194 /* For each output operand, store a SET. */
3195 for (i = 0; i < noutputs; ++i)
3197 rtx src, o = output_rvec[i];
3198 if (i == 0)
3200 ASM_OPERANDS_OUTPUT_CONSTRAINT (obody) = constraints[0];
3201 src = obody;
3203 else
3205 src = gen_rtx_ASM_OPERANDS (GET_MODE (o),
3206 ASM_OPERANDS_TEMPLATE (obody),
3207 constraints[i], i, argvec,
3208 constraintvec, labelvec, locus);
3209 MEM_VOLATILE_P (src) = gimple_asm_volatile_p (stmt);
3211 XVECEXP (body, 0, i) = gen_rtx_SET (o, src);
3214 /* If there are no outputs (but there are some clobbers)
3215 store the bare ASM_OPERANDS into the PARALLEL. */
3216 if (i == 0)
3217 XVECEXP (body, 0, i++) = obody;
3219 /* Store (clobber REG) for each clobbered register specified. */
3220 for (unsigned j = 0; j < nclobbers; ++j)
3222 rtx clobbered_reg = clobber_rvec[j];
3224 /* Do sanity check for overlap between clobbers and respectively
3225 input and outputs that hasn't been handled. Such overlap
3226 should have been detected and reported above. */
3227 if (!clobber_conflict_found && REG_P (clobbered_reg))
3229 /* We test the old body (obody) contents to avoid
3230 tripping over the under-construction body. */
3231 for (unsigned k = 0; k < noutputs; ++k)
3232 if (reg_overlap_mentioned_p (clobbered_reg, output_rvec[k]))
3233 internal_error ("asm clobber conflict with output operand");
3235 for (unsigned k = 0; k < ninputs - ninout; ++k)
3236 if (reg_overlap_mentioned_p (clobbered_reg, input_rvec[k]))
3237 internal_error ("asm clobber conflict with input operand");
3240 XVECEXP (body, 0, i++) = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
3243 if (nlabels > 0)
3244 emit_jump_insn (body);
3245 else
3246 emit_insn (body);
3249 generating_concat_p = old_generating_concat_p;
3251 if (fallthru_label)
3252 emit_label (fallthru_label);
3254 if (after_md_seq)
3255 emit_insn (after_md_seq);
3256 if (after_rtl_seq)
3257 emit_insn (after_rtl_seq);
3259 free_temp_slots ();
3260 crtl->has_asm_statement = 1;
3263 /* Emit code to jump to the address
3264 specified by the pointer expression EXP. */
3266 static void
3267 expand_computed_goto (tree exp)
3269 rtx x = expand_normal (exp);
3271 do_pending_stack_adjust ();
3272 emit_indirect_jump (x);
3275 /* Generate RTL code for a `goto' statement with target label LABEL.
3276 LABEL should be a LABEL_DECL tree node that was or will later be
3277 defined with `expand_label'. */
3279 static void
3280 expand_goto (tree label)
3282 if (flag_checking)
3284 /* Check for a nonlocal goto to a containing function. Should have
3285 gotten translated to __builtin_nonlocal_goto. */
3286 tree context = decl_function_context (label);
3287 gcc_assert (!context || context == current_function_decl);
3290 emit_jump (jump_target_rtx (label));
3293 /* Output a return with no value. */
3295 static void
3296 expand_null_return_1 (void)
3298 clear_pending_stack_adjust ();
3299 do_pending_stack_adjust ();
3300 emit_jump (return_label);
3303 /* Generate RTL to return from the current function, with no value.
3304 (That is, we do not do anything about returning any value.) */
3306 void
3307 expand_null_return (void)
3309 /* If this function was declared to return a value, but we
3310 didn't, clobber the return registers so that they are not
3311 propagated live to the rest of the function. */
3312 clobber_return_register ();
3314 expand_null_return_1 ();
3317 /* Generate RTL to return from the current function, with value VAL. */
3319 static void
3320 expand_value_return (rtx val)
3322 /* Copy the value to the return location unless it's already there. */
3324 tree decl = DECL_RESULT (current_function_decl);
3325 rtx return_reg = DECL_RTL (decl);
3326 if (return_reg != val)
3328 tree funtype = TREE_TYPE (current_function_decl);
3329 tree type = TREE_TYPE (decl);
3330 int unsignedp = TYPE_UNSIGNED (type);
3331 machine_mode old_mode = DECL_MODE (decl);
3332 machine_mode mode;
3333 if (DECL_BY_REFERENCE (decl))
3334 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3335 else
3336 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3338 if (mode != old_mode)
3339 val = convert_modes (mode, old_mode, val, unsignedp);
3341 if (GET_CODE (return_reg) == PARALLEL)
3342 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3343 else
3344 emit_move_insn (return_reg, val);
3347 expand_null_return_1 ();
3350 /* Generate RTL to evaluate the expression RETVAL and return it
3351 from the current function. */
3353 static void
3354 expand_return (tree retval, tree bounds)
3356 rtx result_rtl;
3357 rtx val = 0;
3358 tree retval_rhs;
3359 rtx bounds_rtl;
3361 /* If function wants no value, give it none. */
3362 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3364 expand_normal (retval);
3365 expand_null_return ();
3366 return;
3369 if (retval == error_mark_node)
3371 /* Treat this like a return of no value from a function that
3372 returns a value. */
3373 expand_null_return ();
3374 return;
3376 else if ((TREE_CODE (retval) == MODIFY_EXPR
3377 || TREE_CODE (retval) == INIT_EXPR)
3378 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3379 retval_rhs = TREE_OPERAND (retval, 1);
3380 else
3381 retval_rhs = retval;
3383 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3385 /* Put returned bounds to the right place. */
3386 bounds_rtl = DECL_BOUNDS_RTL (DECL_RESULT (current_function_decl));
3387 if (bounds_rtl)
3389 rtx addr = NULL;
3390 rtx bnd = NULL;
3392 if (bounds && bounds != error_mark_node)
3394 bnd = expand_normal (bounds);
3395 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3397 else if (REG_P (bounds_rtl))
3399 if (bounds)
3400 bnd = chkp_expand_zero_bounds ();
3401 else
3403 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3404 addr = gen_rtx_MEM (Pmode, addr);
3405 bnd = targetm.calls.load_bounds_for_arg (addr, NULL, NULL);
3408 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3410 else
3412 int n;
3414 gcc_assert (GET_CODE (bounds_rtl) == PARALLEL);
3416 if (bounds)
3417 bnd = chkp_expand_zero_bounds ();
3418 else
3420 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3421 addr = gen_rtx_MEM (Pmode, addr);
3424 for (n = 0; n < XVECLEN (bounds_rtl, 0); n++)
3426 rtx slot = XEXP (XVECEXP (bounds_rtl, 0, n), 0);
3427 if (!bounds)
3429 rtx offs = XEXP (XVECEXP (bounds_rtl, 0, n), 1);
3430 rtx from = adjust_address (addr, Pmode, INTVAL (offs));
3431 bnd = targetm.calls.load_bounds_for_arg (from, NULL, NULL);
3433 targetm.calls.store_returned_bounds (slot, bnd);
3437 else if (chkp_function_instrumented_p (current_function_decl)
3438 && !BOUNDED_P (retval_rhs)
3439 && chkp_type_has_pointer (TREE_TYPE (retval_rhs))
3440 && TREE_CODE (retval_rhs) != RESULT_DECL)
3442 rtx addr = expand_normal (build_fold_addr_expr (retval_rhs));
3443 addr = gen_rtx_MEM (Pmode, addr);
3445 gcc_assert (MEM_P (result_rtl));
3447 chkp_copy_bounds_for_stack_parm (result_rtl, addr, TREE_TYPE (retval_rhs));
3450 /* If we are returning the RESULT_DECL, then the value has already
3451 been stored into it, so we don't have to do anything special. */
3452 if (TREE_CODE (retval_rhs) == RESULT_DECL)
3453 expand_value_return (result_rtl);
3455 /* If the result is an aggregate that is being returned in one (or more)
3456 registers, load the registers here. */
3458 else if (retval_rhs != 0
3459 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3460 && REG_P (result_rtl))
3462 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3463 if (val)
3465 /* Use the mode of the result value on the return register. */
3466 PUT_MODE (result_rtl, GET_MODE (val));
3467 expand_value_return (val);
3469 else
3470 expand_null_return ();
3472 else if (retval_rhs != 0
3473 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3474 && (REG_P (result_rtl)
3475 || (GET_CODE (result_rtl) == PARALLEL)))
3477 /* Compute the return value into a temporary (usually a pseudo reg). */
3479 = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3480 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3481 val = force_not_mem (val);
3482 expand_value_return (val);
3484 else
3486 /* No hard reg used; calculate value into hard return reg. */
3487 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3488 expand_value_return (result_rtl);
3492 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3493 STMT that doesn't require special handling for outgoing edges. That
3494 is no tailcalls and no GIMPLE_COND. */
3496 static void
3497 expand_gimple_stmt_1 (gimple *stmt)
3499 tree op0;
3501 set_curr_insn_location (gimple_location (stmt));
3503 switch (gimple_code (stmt))
3505 case GIMPLE_GOTO:
3506 op0 = gimple_goto_dest (stmt);
3507 if (TREE_CODE (op0) == LABEL_DECL)
3508 expand_goto (op0);
3509 else
3510 expand_computed_goto (op0);
3511 break;
3512 case GIMPLE_LABEL:
3513 expand_label (gimple_label_label (as_a <glabel *> (stmt)));
3514 break;
3515 case GIMPLE_NOP:
3516 case GIMPLE_PREDICT:
3517 break;
3518 case GIMPLE_SWITCH:
3519 expand_case (as_a <gswitch *> (stmt));
3520 break;
3521 case GIMPLE_ASM:
3522 expand_asm_stmt (as_a <gasm *> (stmt));
3523 break;
3524 case GIMPLE_CALL:
3525 expand_call_stmt (as_a <gcall *> (stmt));
3526 break;
3528 case GIMPLE_RETURN:
3530 tree bnd = gimple_return_retbnd (as_a <greturn *> (stmt));
3531 op0 = gimple_return_retval (as_a <greturn *> (stmt));
3533 if (op0 && op0 != error_mark_node)
3535 tree result = DECL_RESULT (current_function_decl);
3537 /* If we are not returning the current function's RESULT_DECL,
3538 build an assignment to it. */
3539 if (op0 != result)
3541 /* I believe that a function's RESULT_DECL is unique. */
3542 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3544 /* ??? We'd like to use simply expand_assignment here,
3545 but this fails if the value is of BLKmode but the return
3546 decl is a register. expand_return has special handling
3547 for this combination, which eventually should move
3548 to common code. See comments there. Until then, let's
3549 build a modify expression :-/ */
3550 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3551 result, op0);
3553 /* Mark we have return statement with missing bounds. */
3554 if (!bnd && chkp_function_instrumented_p (cfun->decl))
3555 bnd = error_mark_node;
3558 if (!op0)
3559 expand_null_return ();
3560 else
3561 expand_return (op0, bnd);
3563 break;
3565 case GIMPLE_ASSIGN:
3567 gassign *assign_stmt = as_a <gassign *> (stmt);
3568 tree lhs = gimple_assign_lhs (assign_stmt);
3570 /* Tree expand used to fiddle with |= and &= of two bitfield
3571 COMPONENT_REFs here. This can't happen with gimple, the LHS
3572 of binary assigns must be a gimple reg. */
3574 if (TREE_CODE (lhs) != SSA_NAME
3575 || get_gimple_rhs_class (gimple_expr_code (stmt))
3576 == GIMPLE_SINGLE_RHS)
3578 tree rhs = gimple_assign_rhs1 (assign_stmt);
3579 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
3580 == GIMPLE_SINGLE_RHS);
3581 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs)
3582 /* Do not put locations on possibly shared trees. */
3583 && !is_gimple_min_invariant (rhs))
3584 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3585 if (TREE_CLOBBER_P (rhs))
3586 /* This is a clobber to mark the going out of scope for
3587 this LHS. */
3589 else
3590 expand_assignment (lhs, rhs,
3591 gimple_assign_nontemporal_move_p (
3592 assign_stmt));
3594 else
3596 rtx target, temp;
3597 bool nontemporal = gimple_assign_nontemporal_move_p (assign_stmt);
3598 struct separate_ops ops;
3599 bool promoted = false;
3601 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3602 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
3603 promoted = true;
3605 ops.code = gimple_assign_rhs_code (assign_stmt);
3606 ops.type = TREE_TYPE (lhs);
3607 switch (get_gimple_rhs_class (ops.code))
3609 case GIMPLE_TERNARY_RHS:
3610 ops.op2 = gimple_assign_rhs3 (assign_stmt);
3611 /* Fallthru */
3612 case GIMPLE_BINARY_RHS:
3613 ops.op1 = gimple_assign_rhs2 (assign_stmt);
3614 /* Fallthru */
3615 case GIMPLE_UNARY_RHS:
3616 ops.op0 = gimple_assign_rhs1 (assign_stmt);
3617 break;
3618 default:
3619 gcc_unreachable ();
3621 ops.location = gimple_location (stmt);
3623 /* If we want to use a nontemporal store, force the value to
3624 register first. If we store into a promoted register,
3625 don't directly expand to target. */
3626 temp = nontemporal || promoted ? NULL_RTX : target;
3627 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
3628 EXPAND_NORMAL);
3630 if (temp == target)
3632 else if (promoted)
3634 int unsignedp = SUBREG_PROMOTED_SIGN (target);
3635 /* If TEMP is a VOIDmode constant, use convert_modes to make
3636 sure that we properly convert it. */
3637 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
3639 temp = convert_modes (GET_MODE (target),
3640 TYPE_MODE (ops.type),
3641 temp, unsignedp);
3642 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
3643 GET_MODE (target), temp, unsignedp);
3646 convert_move (SUBREG_REG (target), temp, unsignedp);
3648 else if (nontemporal && emit_storent_insn (target, temp))
3650 else
3652 temp = force_operand (temp, target);
3653 if (temp != target)
3654 emit_move_insn (target, temp);
3658 break;
3660 default:
3661 gcc_unreachable ();
3665 /* Expand one gimple statement STMT and return the last RTL instruction
3666 before any of the newly generated ones.
3668 In addition to generating the necessary RTL instructions this also
3669 sets REG_EH_REGION notes if necessary and sets the current source
3670 location for diagnostics. */
3672 static rtx_insn *
3673 expand_gimple_stmt (gimple *stmt)
3675 location_t saved_location = input_location;
3676 rtx_insn *last = get_last_insn ();
3677 int lp_nr;
3679 gcc_assert (cfun);
3681 /* We need to save and restore the current source location so that errors
3682 discovered during expansion are emitted with the right location. But
3683 it would be better if the diagnostic routines used the source location
3684 embedded in the tree nodes rather than globals. */
3685 if (gimple_has_location (stmt))
3686 input_location = gimple_location (stmt);
3688 expand_gimple_stmt_1 (stmt);
3690 /* Free any temporaries used to evaluate this statement. */
3691 free_temp_slots ();
3693 input_location = saved_location;
3695 /* Mark all insns that may trap. */
3696 lp_nr = lookup_stmt_eh_lp (stmt);
3697 if (lp_nr)
3699 rtx_insn *insn;
3700 for (insn = next_real_insn (last); insn;
3701 insn = next_real_insn (insn))
3703 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
3704 /* If we want exceptions for non-call insns, any
3705 may_trap_p instruction may throw. */
3706 && GET_CODE (PATTERN (insn)) != CLOBBER
3707 && GET_CODE (PATTERN (insn)) != USE
3708 && insn_could_throw_p (insn))
3709 make_reg_eh_region_note (insn, 0, lp_nr);
3713 return last;
3716 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
3717 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
3718 generated a tail call (something that might be denied by the ABI
3719 rules governing the call; see calls.c).
3721 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
3722 can still reach the rest of BB. The case here is __builtin_sqrt,
3723 where the NaN result goes through the external function (with a
3724 tailcall) and the normal result happens via a sqrt instruction. */
3726 static basic_block
3727 expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
3729 rtx_insn *last2, *last;
3730 edge e;
3731 edge_iterator ei;
3732 int probability;
3733 gcov_type count;
3735 last2 = last = expand_gimple_stmt (stmt);
3737 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
3738 if (CALL_P (last) && SIBLING_CALL_P (last))
3739 goto found;
3741 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3743 *can_fallthru = true;
3744 return NULL;
3746 found:
3747 /* ??? Wouldn't it be better to just reset any pending stack adjust?
3748 Any instructions emitted here are about to be deleted. */
3749 do_pending_stack_adjust ();
3751 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
3752 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
3753 EH or abnormal edges, we shouldn't have created a tail call in
3754 the first place. So it seems to me we should just be removing
3755 all edges here, or redirecting the existing fallthru edge to
3756 the exit block. */
3758 probability = 0;
3759 count = 0;
3761 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3763 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
3765 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3767 e->dest->count -= e->count;
3768 e->dest->frequency -= EDGE_FREQUENCY (e);
3769 if (e->dest->count < 0)
3770 e->dest->count = 0;
3771 if (e->dest->frequency < 0)
3772 e->dest->frequency = 0;
3774 count += e->count;
3775 probability += e->probability;
3776 remove_edge (e);
3778 else
3779 ei_next (&ei);
3782 /* This is somewhat ugly: the call_expr expander often emits instructions
3783 after the sibcall (to perform the function return). These confuse the
3784 find_many_sub_basic_blocks code, so we need to get rid of these. */
3785 last = NEXT_INSN (last);
3786 gcc_assert (BARRIER_P (last));
3788 *can_fallthru = false;
3789 while (NEXT_INSN (last))
3791 /* For instance an sqrt builtin expander expands if with
3792 sibcall in the then and label for `else`. */
3793 if (LABEL_P (NEXT_INSN (last)))
3795 *can_fallthru = true;
3796 break;
3798 delete_insn (NEXT_INSN (last));
3801 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
3802 | EDGE_SIBCALL);
3803 e->probability += probability;
3804 e->count += count;
3805 BB_END (bb) = last;
3806 update_bb_for_insn (bb);
3808 if (NEXT_INSN (last))
3810 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
3812 last = BB_END (bb);
3813 if (BARRIER_P (last))
3814 BB_END (bb) = PREV_INSN (last);
3817 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3819 return bb;
3822 /* Return the difference between the floor and the truncated result of
3823 a signed division by OP1 with remainder MOD. */
3824 static rtx
3825 floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3827 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
3828 return gen_rtx_IF_THEN_ELSE
3829 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3830 gen_rtx_IF_THEN_ELSE
3831 (mode, gen_rtx_LT (BImode,
3832 gen_rtx_DIV (mode, op1, mod),
3833 const0_rtx),
3834 constm1_rtx, const0_rtx),
3835 const0_rtx);
3838 /* Return the difference between the ceil and the truncated result of
3839 a signed division by OP1 with remainder MOD. */
3840 static rtx
3841 ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3843 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
3844 return gen_rtx_IF_THEN_ELSE
3845 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3846 gen_rtx_IF_THEN_ELSE
3847 (mode, gen_rtx_GT (BImode,
3848 gen_rtx_DIV (mode, op1, mod),
3849 const0_rtx),
3850 const1_rtx, const0_rtx),
3851 const0_rtx);
3854 /* Return the difference between the ceil and the truncated result of
3855 an unsigned division by OP1 with remainder MOD. */
3856 static rtx
3857 ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
3859 /* (mod != 0 ? 1 : 0) */
3860 return gen_rtx_IF_THEN_ELSE
3861 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3862 const1_rtx, const0_rtx);
3865 /* Return the difference between the rounded and the truncated result
3866 of a signed division by OP1 with remainder MOD. Halfway cases are
3867 rounded away from zero, rather than to the nearest even number. */
3868 static rtx
3869 round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3871 /* (abs (mod) >= abs (op1) - abs (mod)
3872 ? (op1 / mod > 0 ? 1 : -1)
3873 : 0) */
3874 return gen_rtx_IF_THEN_ELSE
3875 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
3876 gen_rtx_MINUS (mode,
3877 gen_rtx_ABS (mode, op1),
3878 gen_rtx_ABS (mode, mod))),
3879 gen_rtx_IF_THEN_ELSE
3880 (mode, gen_rtx_GT (BImode,
3881 gen_rtx_DIV (mode, op1, mod),
3882 const0_rtx),
3883 const1_rtx, constm1_rtx),
3884 const0_rtx);
3887 /* Return the difference between the rounded and the truncated result
3888 of a unsigned division by OP1 with remainder MOD. Halfway cases
3889 are rounded away from zero, rather than to the nearest even
3890 number. */
3891 static rtx
3892 round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
3894 /* (mod >= op1 - mod ? 1 : 0) */
3895 return gen_rtx_IF_THEN_ELSE
3896 (mode, gen_rtx_GE (BImode, mod,
3897 gen_rtx_MINUS (mode, op1, mod)),
3898 const1_rtx, const0_rtx);
3901 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
3902 any rtl. */
3904 static rtx
3905 convert_debug_memory_address (machine_mode mode, rtx x,
3906 addr_space_t as)
3908 machine_mode xmode = GET_MODE (x);
3910 #ifndef POINTERS_EXTEND_UNSIGNED
3911 gcc_assert (mode == Pmode
3912 || mode == targetm.addr_space.address_mode (as));
3913 gcc_assert (xmode == mode || xmode == VOIDmode);
3914 #else
3915 rtx temp;
3917 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
3919 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
3920 return x;
3922 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
3923 x = lowpart_subreg (mode, x, xmode);
3924 else if (POINTERS_EXTEND_UNSIGNED > 0)
3925 x = gen_rtx_ZERO_EXTEND (mode, x);
3926 else if (!POINTERS_EXTEND_UNSIGNED)
3927 x = gen_rtx_SIGN_EXTEND (mode, x);
3928 else
3930 switch (GET_CODE (x))
3932 case SUBREG:
3933 if ((SUBREG_PROMOTED_VAR_P (x)
3934 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
3935 || (GET_CODE (SUBREG_REG (x)) == PLUS
3936 && REG_P (XEXP (SUBREG_REG (x), 0))
3937 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
3938 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
3939 && GET_MODE (SUBREG_REG (x)) == mode)
3940 return SUBREG_REG (x);
3941 break;
3942 case LABEL_REF:
3943 temp = gen_rtx_LABEL_REF (mode, LABEL_REF_LABEL (x));
3944 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
3945 return temp;
3946 case SYMBOL_REF:
3947 temp = shallow_copy_rtx (x);
3948 PUT_MODE (temp, mode);
3949 return temp;
3950 case CONST:
3951 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3952 if (temp)
3953 temp = gen_rtx_CONST (mode, temp);
3954 return temp;
3955 case PLUS:
3956 case MINUS:
3957 if (CONST_INT_P (XEXP (x, 1)))
3959 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3960 if (temp)
3961 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
3963 break;
3964 default:
3965 break;
3967 /* Don't know how to express ptr_extend as operation in debug info. */
3968 return NULL;
3970 #endif /* POINTERS_EXTEND_UNSIGNED */
3972 return x;
3975 /* Map from SSA_NAMEs to corresponding DEBUG_EXPR_DECLs created
3976 by avoid_deep_ter_for_debug. */
3978 static hash_map<tree, tree> *deep_ter_debug_map;
3980 /* Split too deep TER chains for debug stmts using debug temporaries. */
3982 static void
3983 avoid_deep_ter_for_debug (gimple *stmt, int depth)
3985 use_operand_p use_p;
3986 ssa_op_iter iter;
3987 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
3989 tree use = USE_FROM_PTR (use_p);
3990 if (TREE_CODE (use) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (use))
3991 continue;
3992 gimple *g = get_gimple_for_ssa_name (use);
3993 if (g == NULL)
3994 continue;
3995 if (depth > 6 && !stmt_ends_bb_p (g))
3997 if (deep_ter_debug_map == NULL)
3998 deep_ter_debug_map = new hash_map<tree, tree>;
4000 tree &vexpr = deep_ter_debug_map->get_or_insert (use);
4001 if (vexpr != NULL)
4002 continue;
4003 vexpr = make_node (DEBUG_EXPR_DECL);
4004 gimple *def_temp = gimple_build_debug_bind (vexpr, use, g);
4005 DECL_ARTIFICIAL (vexpr) = 1;
4006 TREE_TYPE (vexpr) = TREE_TYPE (use);
4007 DECL_MODE (vexpr) = TYPE_MODE (TREE_TYPE (use));
4008 gimple_stmt_iterator gsi = gsi_for_stmt (g);
4009 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
4010 avoid_deep_ter_for_debug (def_temp, 0);
4012 else
4013 avoid_deep_ter_for_debug (g, depth + 1);
4017 /* Return an RTX equivalent to the value of the parameter DECL. */
4019 static rtx
4020 expand_debug_parm_decl (tree decl)
4022 rtx incoming = DECL_INCOMING_RTL (decl);
4024 if (incoming
4025 && GET_MODE (incoming) != BLKmode
4026 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
4027 || (MEM_P (incoming)
4028 && REG_P (XEXP (incoming, 0))
4029 && HARD_REGISTER_P (XEXP (incoming, 0)))))
4031 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
4033 #ifdef HAVE_window_save
4034 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
4035 If the target machine has an explicit window save instruction, the
4036 actual entry value is the corresponding OUTGOING_REGNO instead. */
4037 if (REG_P (incoming)
4038 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
4039 incoming
4040 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
4041 OUTGOING_REGNO (REGNO (incoming)), 0);
4042 else if (MEM_P (incoming))
4044 rtx reg = XEXP (incoming, 0);
4045 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
4047 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
4048 incoming = replace_equiv_address_nv (incoming, reg);
4050 else
4051 incoming = copy_rtx (incoming);
4053 #endif
4055 ENTRY_VALUE_EXP (rtl) = incoming;
4056 return rtl;
4059 if (incoming
4060 && GET_MODE (incoming) != BLKmode
4061 && !TREE_ADDRESSABLE (decl)
4062 && MEM_P (incoming)
4063 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
4064 || (GET_CODE (XEXP (incoming, 0)) == PLUS
4065 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
4066 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
4067 return copy_rtx (incoming);
4069 return NULL_RTX;
4072 /* Return an RTX equivalent to the value of the tree expression EXP. */
4074 static rtx
4075 expand_debug_expr (tree exp)
4077 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
4078 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4079 machine_mode inner_mode = VOIDmode;
4080 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
4081 addr_space_t as;
4083 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
4085 case tcc_expression:
4086 switch (TREE_CODE (exp))
4088 case COND_EXPR:
4089 case DOT_PROD_EXPR:
4090 case SAD_EXPR:
4091 case WIDEN_MULT_PLUS_EXPR:
4092 case WIDEN_MULT_MINUS_EXPR:
4093 case FMA_EXPR:
4094 goto ternary;
4096 case TRUTH_ANDIF_EXPR:
4097 case TRUTH_ORIF_EXPR:
4098 case TRUTH_AND_EXPR:
4099 case TRUTH_OR_EXPR:
4100 case TRUTH_XOR_EXPR:
4101 goto binary;
4103 case TRUTH_NOT_EXPR:
4104 goto unary;
4106 default:
4107 break;
4109 break;
4111 ternary:
4112 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
4113 if (!op2)
4114 return NULL_RTX;
4115 /* Fall through. */
4117 binary:
4118 case tcc_binary:
4119 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4120 if (!op1)
4121 return NULL_RTX;
4122 switch (TREE_CODE (exp))
4124 case LSHIFT_EXPR:
4125 case RSHIFT_EXPR:
4126 case LROTATE_EXPR:
4127 case RROTATE_EXPR:
4128 case WIDEN_LSHIFT_EXPR:
4129 /* Ensure second operand isn't wider than the first one. */
4130 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 1)));
4131 if (SCALAR_INT_MODE_P (inner_mode))
4133 machine_mode opmode = mode;
4134 if (VECTOR_MODE_P (mode))
4135 opmode = GET_MODE_INNER (mode);
4136 if (SCALAR_INT_MODE_P (opmode)
4137 && (GET_MODE_PRECISION (opmode)
4138 < GET_MODE_PRECISION (inner_mode)))
4139 op1 = lowpart_subreg (opmode, op1, inner_mode);
4141 break;
4142 default:
4143 break;
4145 /* Fall through. */
4147 unary:
4148 case tcc_unary:
4149 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4150 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4151 if (!op0)
4152 return NULL_RTX;
4153 break;
4155 case tcc_comparison:
4156 unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
4157 goto binary;
4159 case tcc_type:
4160 case tcc_statement:
4161 gcc_unreachable ();
4163 case tcc_constant:
4164 case tcc_exceptional:
4165 case tcc_declaration:
4166 case tcc_reference:
4167 case tcc_vl_exp:
4168 break;
4171 switch (TREE_CODE (exp))
4173 case STRING_CST:
4174 if (!lookup_constant_def (exp))
4176 if (strlen (TREE_STRING_POINTER (exp)) + 1
4177 != (size_t) TREE_STRING_LENGTH (exp))
4178 return NULL_RTX;
4179 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
4180 op0 = gen_rtx_MEM (BLKmode, op0);
4181 set_mem_attributes (op0, exp, 0);
4182 return op0;
4184 /* Fall through... */
4186 case INTEGER_CST:
4187 case REAL_CST:
4188 case FIXED_CST:
4189 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
4190 return op0;
4192 case COMPLEX_CST:
4193 gcc_assert (COMPLEX_MODE_P (mode));
4194 op0 = expand_debug_expr (TREE_REALPART (exp));
4195 op1 = expand_debug_expr (TREE_IMAGPART (exp));
4196 return gen_rtx_CONCAT (mode, op0, op1);
4198 case DEBUG_EXPR_DECL:
4199 op0 = DECL_RTL_IF_SET (exp);
4201 if (op0)
4202 return op0;
4204 op0 = gen_rtx_DEBUG_EXPR (mode);
4205 DEBUG_EXPR_TREE_DECL (op0) = exp;
4206 SET_DECL_RTL (exp, op0);
4208 return op0;
4210 case VAR_DECL:
4211 case PARM_DECL:
4212 case FUNCTION_DECL:
4213 case LABEL_DECL:
4214 case CONST_DECL:
4215 case RESULT_DECL:
4216 op0 = DECL_RTL_IF_SET (exp);
4218 /* This decl was probably optimized away. */
4219 if (!op0)
4221 if (TREE_CODE (exp) != VAR_DECL
4222 || DECL_EXTERNAL (exp)
4223 || !TREE_STATIC (exp)
4224 || !DECL_NAME (exp)
4225 || DECL_HARD_REGISTER (exp)
4226 || DECL_IN_CONSTANT_POOL (exp)
4227 || mode == VOIDmode)
4228 return NULL;
4230 op0 = make_decl_rtl_for_debug (exp);
4231 if (!MEM_P (op0)
4232 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
4233 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
4234 return NULL;
4236 else
4237 op0 = copy_rtx (op0);
4239 if (GET_MODE (op0) == BLKmode
4240 /* If op0 is not BLKmode, but mode is, adjust_mode
4241 below would ICE. While it is likely a FE bug,
4242 try to be robust here. See PR43166. */
4243 || mode == BLKmode
4244 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
4246 gcc_assert (MEM_P (op0));
4247 op0 = adjust_address_nv (op0, mode, 0);
4248 return op0;
4251 /* Fall through. */
4253 adjust_mode:
4254 case PAREN_EXPR:
4255 CASE_CONVERT:
4257 inner_mode = GET_MODE (op0);
4259 if (mode == inner_mode)
4260 return op0;
4262 if (inner_mode == VOIDmode)
4264 if (TREE_CODE (exp) == SSA_NAME)
4265 inner_mode = TYPE_MODE (TREE_TYPE (exp));
4266 else
4267 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4268 if (mode == inner_mode)
4269 return op0;
4272 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4274 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4275 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4276 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4277 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4278 else
4279 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4281 else if (FLOAT_MODE_P (mode))
4283 gcc_assert (TREE_CODE (exp) != SSA_NAME);
4284 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4285 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
4286 else
4287 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
4289 else if (FLOAT_MODE_P (inner_mode))
4291 if (unsignedp)
4292 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4293 else
4294 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4296 else if (CONSTANT_P (op0)
4297 || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
4298 op0 = lowpart_subreg (mode, op0, inner_mode);
4299 else if (UNARY_CLASS_P (exp)
4300 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
4301 : unsignedp)
4302 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4303 else
4304 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4306 return op0;
4309 case MEM_REF:
4310 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4312 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
4313 TREE_OPERAND (exp, 0),
4314 TREE_OPERAND (exp, 1));
4315 if (newexp)
4316 return expand_debug_expr (newexp);
4318 /* FALLTHROUGH */
4319 case INDIRECT_REF:
4320 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4321 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4322 if (!op0)
4323 return NULL;
4325 if (TREE_CODE (exp) == MEM_REF)
4327 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4328 || (GET_CODE (op0) == PLUS
4329 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
4330 /* (mem (debug_implicit_ptr)) might confuse aliasing.
4331 Instead just use get_inner_reference. */
4332 goto component_ref;
4334 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4335 if (!op1 || !CONST_INT_P (op1))
4336 return NULL;
4338 op0 = plus_constant (inner_mode, op0, INTVAL (op1));
4341 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4343 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4344 op0, as);
4345 if (op0 == NULL_RTX)
4346 return NULL;
4348 op0 = gen_rtx_MEM (mode, op0);
4349 set_mem_attributes (op0, exp, 0);
4350 if (TREE_CODE (exp) == MEM_REF
4351 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4352 set_mem_expr (op0, NULL_TREE);
4353 set_mem_addr_space (op0, as);
4355 return op0;
4357 case TARGET_MEM_REF:
4358 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
4359 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
4360 return NULL;
4362 op0 = expand_debug_expr
4363 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
4364 if (!op0)
4365 return NULL;
4367 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4368 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4369 op0, as);
4370 if (op0 == NULL_RTX)
4371 return NULL;
4373 op0 = gen_rtx_MEM (mode, op0);
4375 set_mem_attributes (op0, exp, 0);
4376 set_mem_addr_space (op0, as);
4378 return op0;
4380 component_ref:
4381 case ARRAY_REF:
4382 case ARRAY_RANGE_REF:
4383 case COMPONENT_REF:
4384 case BIT_FIELD_REF:
4385 case REALPART_EXPR:
4386 case IMAGPART_EXPR:
4387 case VIEW_CONVERT_EXPR:
4389 machine_mode mode1;
4390 HOST_WIDE_INT bitsize, bitpos;
4391 tree offset;
4392 int reversep, volatilep = 0;
4393 tree tem
4394 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
4395 &unsignedp, &reversep, &volatilep, false);
4396 rtx orig_op0;
4398 if (bitsize == 0)
4399 return NULL;
4401 orig_op0 = op0 = expand_debug_expr (tem);
4403 if (!op0)
4404 return NULL;
4406 if (offset)
4408 machine_mode addrmode, offmode;
4410 if (!MEM_P (op0))
4411 return NULL;
4413 op0 = XEXP (op0, 0);
4414 addrmode = GET_MODE (op0);
4415 if (addrmode == VOIDmode)
4416 addrmode = Pmode;
4418 op1 = expand_debug_expr (offset);
4419 if (!op1)
4420 return NULL;
4422 offmode = GET_MODE (op1);
4423 if (offmode == VOIDmode)
4424 offmode = TYPE_MODE (TREE_TYPE (offset));
4426 if (addrmode != offmode)
4427 op1 = lowpart_subreg (addrmode, op1, offmode);
4429 /* Don't use offset_address here, we don't need a
4430 recognizable address, and we don't want to generate
4431 code. */
4432 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4433 op0, op1));
4436 if (MEM_P (op0))
4438 if (mode1 == VOIDmode)
4439 /* Bitfield. */
4440 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
4441 if (bitpos >= BITS_PER_UNIT)
4443 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
4444 bitpos %= BITS_PER_UNIT;
4446 else if (bitpos < 0)
4448 HOST_WIDE_INT units
4449 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4450 op0 = adjust_address_nv (op0, mode1, units);
4451 bitpos += units * BITS_PER_UNIT;
4453 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
4454 op0 = adjust_address_nv (op0, mode, 0);
4455 else if (GET_MODE (op0) != mode1)
4456 op0 = adjust_address_nv (op0, mode1, 0);
4457 else
4458 op0 = copy_rtx (op0);
4459 if (op0 == orig_op0)
4460 op0 = shallow_copy_rtx (op0);
4461 set_mem_attributes (op0, exp, 0);
4464 if (bitpos == 0 && mode == GET_MODE (op0))
4465 return op0;
4467 if (bitpos < 0)
4468 return NULL;
4470 if (GET_MODE (op0) == BLKmode)
4471 return NULL;
4473 if ((bitpos % BITS_PER_UNIT) == 0
4474 && bitsize == GET_MODE_BITSIZE (mode1))
4476 machine_mode opmode = GET_MODE (op0);
4478 if (opmode == VOIDmode)
4479 opmode = TYPE_MODE (TREE_TYPE (tem));
4481 /* This condition may hold if we're expanding the address
4482 right past the end of an array that turned out not to
4483 be addressable (i.e., the address was only computed in
4484 debug stmts). The gen_subreg below would rightfully
4485 crash, and the address doesn't really exist, so just
4486 drop it. */
4487 if (bitpos >= GET_MODE_BITSIZE (opmode))
4488 return NULL;
4490 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
4491 return simplify_gen_subreg (mode, op0, opmode,
4492 bitpos / BITS_PER_UNIT);
4495 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4496 && TYPE_UNSIGNED (TREE_TYPE (exp))
4497 ? SIGN_EXTRACT
4498 : ZERO_EXTRACT, mode,
4499 GET_MODE (op0) != VOIDmode
4500 ? GET_MODE (op0)
4501 : TYPE_MODE (TREE_TYPE (tem)),
4502 op0, GEN_INT (bitsize), GEN_INT (bitpos));
4505 case ABS_EXPR:
4506 return simplify_gen_unary (ABS, mode, op0, mode);
4508 case NEGATE_EXPR:
4509 return simplify_gen_unary (NEG, mode, op0, mode);
4511 case BIT_NOT_EXPR:
4512 return simplify_gen_unary (NOT, mode, op0, mode);
4514 case FLOAT_EXPR:
4515 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4516 0)))
4517 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4518 inner_mode);
4520 case FIX_TRUNC_EXPR:
4521 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4522 inner_mode);
4524 case POINTER_PLUS_EXPR:
4525 /* For the rare target where pointers are not the same size as
4526 size_t, we need to check for mis-matched modes and correct
4527 the addend. */
4528 if (op0 && op1
4529 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
4530 && GET_MODE (op0) != GET_MODE (op1))
4532 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1))
4533 /* If OP0 is a partial mode, then we must truncate, even if it has
4534 the same bitsize as OP1 as GCC's representation of partial modes
4535 is opaque. */
4536 || (GET_MODE_CLASS (GET_MODE (op0)) == MODE_PARTIAL_INT
4537 && GET_MODE_BITSIZE (GET_MODE (op0)) == GET_MODE_BITSIZE (GET_MODE (op1))))
4538 op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
4539 GET_MODE (op1));
4540 else
4541 /* We always sign-extend, regardless of the signedness of
4542 the operand, because the operand is always unsigned
4543 here even if the original C expression is signed. */
4544 op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
4545 GET_MODE (op1));
4547 /* Fall through. */
4548 case PLUS_EXPR:
4549 return simplify_gen_binary (PLUS, mode, op0, op1);
4551 case MINUS_EXPR:
4552 return simplify_gen_binary (MINUS, mode, op0, op1);
4554 case MULT_EXPR:
4555 return simplify_gen_binary (MULT, mode, op0, op1);
4557 case RDIV_EXPR:
4558 case TRUNC_DIV_EXPR:
4559 case EXACT_DIV_EXPR:
4560 if (unsignedp)
4561 return simplify_gen_binary (UDIV, mode, op0, op1);
4562 else
4563 return simplify_gen_binary (DIV, mode, op0, op1);
4565 case TRUNC_MOD_EXPR:
4566 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4568 case FLOOR_DIV_EXPR:
4569 if (unsignedp)
4570 return simplify_gen_binary (UDIV, mode, op0, op1);
4571 else
4573 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4574 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4575 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4576 return simplify_gen_binary (PLUS, mode, div, adj);
4579 case FLOOR_MOD_EXPR:
4580 if (unsignedp)
4581 return simplify_gen_binary (UMOD, mode, op0, op1);
4582 else
4584 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4585 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4586 adj = simplify_gen_unary (NEG, mode,
4587 simplify_gen_binary (MULT, mode, adj, op1),
4588 mode);
4589 return simplify_gen_binary (PLUS, mode, mod, adj);
4592 case CEIL_DIV_EXPR:
4593 if (unsignedp)
4595 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4596 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4597 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4598 return simplify_gen_binary (PLUS, mode, div, adj);
4600 else
4602 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4603 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4604 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4605 return simplify_gen_binary (PLUS, mode, div, adj);
4608 case CEIL_MOD_EXPR:
4609 if (unsignedp)
4611 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4612 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4613 adj = simplify_gen_unary (NEG, mode,
4614 simplify_gen_binary (MULT, mode, adj, op1),
4615 mode);
4616 return simplify_gen_binary (PLUS, mode, mod, adj);
4618 else
4620 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4621 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4622 adj = simplify_gen_unary (NEG, mode,
4623 simplify_gen_binary (MULT, mode, adj, op1),
4624 mode);
4625 return simplify_gen_binary (PLUS, mode, mod, adj);
4628 case ROUND_DIV_EXPR:
4629 if (unsignedp)
4631 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4632 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4633 rtx adj = round_udiv_adjust (mode, mod, op1);
4634 return simplify_gen_binary (PLUS, mode, div, adj);
4636 else
4638 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4639 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4640 rtx adj = round_sdiv_adjust (mode, mod, op1);
4641 return simplify_gen_binary (PLUS, mode, div, adj);
4644 case ROUND_MOD_EXPR:
4645 if (unsignedp)
4647 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4648 rtx adj = round_udiv_adjust (mode, mod, op1);
4649 adj = simplify_gen_unary (NEG, mode,
4650 simplify_gen_binary (MULT, mode, adj, op1),
4651 mode);
4652 return simplify_gen_binary (PLUS, mode, mod, adj);
4654 else
4656 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4657 rtx adj = round_sdiv_adjust (mode, mod, op1);
4658 adj = simplify_gen_unary (NEG, mode,
4659 simplify_gen_binary (MULT, mode, adj, op1),
4660 mode);
4661 return simplify_gen_binary (PLUS, mode, mod, adj);
4664 case LSHIFT_EXPR:
4665 return simplify_gen_binary (ASHIFT, mode, op0, op1);
4667 case RSHIFT_EXPR:
4668 if (unsignedp)
4669 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
4670 else
4671 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
4673 case LROTATE_EXPR:
4674 return simplify_gen_binary (ROTATE, mode, op0, op1);
4676 case RROTATE_EXPR:
4677 return simplify_gen_binary (ROTATERT, mode, op0, op1);
4679 case MIN_EXPR:
4680 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
4682 case MAX_EXPR:
4683 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
4685 case BIT_AND_EXPR:
4686 case TRUTH_AND_EXPR:
4687 return simplify_gen_binary (AND, mode, op0, op1);
4689 case BIT_IOR_EXPR:
4690 case TRUTH_OR_EXPR:
4691 return simplify_gen_binary (IOR, mode, op0, op1);
4693 case BIT_XOR_EXPR:
4694 case TRUTH_XOR_EXPR:
4695 return simplify_gen_binary (XOR, mode, op0, op1);
4697 case TRUTH_ANDIF_EXPR:
4698 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
4700 case TRUTH_ORIF_EXPR:
4701 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
4703 case TRUTH_NOT_EXPR:
4704 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
4706 case LT_EXPR:
4707 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
4708 op0, op1);
4710 case LE_EXPR:
4711 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
4712 op0, op1);
4714 case GT_EXPR:
4715 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
4716 op0, op1);
4718 case GE_EXPR:
4719 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
4720 op0, op1);
4722 case EQ_EXPR:
4723 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
4725 case NE_EXPR:
4726 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
4728 case UNORDERED_EXPR:
4729 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
4731 case ORDERED_EXPR:
4732 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
4734 case UNLT_EXPR:
4735 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
4737 case UNLE_EXPR:
4738 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
4740 case UNGT_EXPR:
4741 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
4743 case UNGE_EXPR:
4744 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
4746 case UNEQ_EXPR:
4747 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
4749 case LTGT_EXPR:
4750 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
4752 case COND_EXPR:
4753 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
4755 case COMPLEX_EXPR:
4756 gcc_assert (COMPLEX_MODE_P (mode));
4757 if (GET_MODE (op0) == VOIDmode)
4758 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
4759 if (GET_MODE (op1) == VOIDmode)
4760 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
4761 return gen_rtx_CONCAT (mode, op0, op1);
4763 case CONJ_EXPR:
4764 if (GET_CODE (op0) == CONCAT)
4765 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
4766 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
4767 XEXP (op0, 1),
4768 GET_MODE_INNER (mode)));
4769 else
4771 machine_mode imode = GET_MODE_INNER (mode);
4772 rtx re, im;
4774 if (MEM_P (op0))
4776 re = adjust_address_nv (op0, imode, 0);
4777 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
4779 else
4781 machine_mode ifmode = int_mode_for_mode (mode);
4782 machine_mode ihmode = int_mode_for_mode (imode);
4783 rtx halfsize;
4784 if (ifmode == BLKmode || ihmode == BLKmode)
4785 return NULL;
4786 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
4787 re = op0;
4788 if (mode != ifmode)
4789 re = gen_rtx_SUBREG (ifmode, re, 0);
4790 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
4791 if (imode != ihmode)
4792 re = gen_rtx_SUBREG (imode, re, 0);
4793 im = copy_rtx (op0);
4794 if (mode != ifmode)
4795 im = gen_rtx_SUBREG (ifmode, im, 0);
4796 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
4797 if (imode != ihmode)
4798 im = gen_rtx_SUBREG (imode, im, 0);
4800 im = gen_rtx_NEG (imode, im);
4801 return gen_rtx_CONCAT (mode, re, im);
4804 case ADDR_EXPR:
4805 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4806 if (!op0 || !MEM_P (op0))
4808 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
4809 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
4810 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
4811 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
4812 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
4813 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
4815 if (handled_component_p (TREE_OPERAND (exp, 0)))
4817 HOST_WIDE_INT bitoffset, bitsize, maxsize;
4818 bool reverse;
4819 tree decl
4820 = get_ref_base_and_extent (TREE_OPERAND (exp, 0), &bitoffset,
4821 &bitsize, &maxsize, &reverse);
4822 if ((TREE_CODE (decl) == VAR_DECL
4823 || TREE_CODE (decl) == PARM_DECL
4824 || TREE_CODE (decl) == RESULT_DECL)
4825 && (!TREE_ADDRESSABLE (decl)
4826 || target_for_debug_bind (decl))
4827 && (bitoffset % BITS_PER_UNIT) == 0
4828 && bitsize > 0
4829 && bitsize == maxsize)
4831 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
4832 return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
4836 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
4837 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4838 == ADDR_EXPR)
4840 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4841 0));
4842 if (op0 != NULL
4843 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4844 || (GET_CODE (op0) == PLUS
4845 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
4846 && CONST_INT_P (XEXP (op0, 1)))))
4848 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4849 1));
4850 if (!op1 || !CONST_INT_P (op1))
4851 return NULL;
4853 return plus_constant (mode, op0, INTVAL (op1));
4857 return NULL;
4860 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
4861 op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
4863 return op0;
4865 case VECTOR_CST:
4867 unsigned i;
4869 op0 = gen_rtx_CONCATN
4870 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4872 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
4874 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
4875 if (!op1)
4876 return NULL;
4877 XVECEXP (op0, 0, i) = op1;
4880 return op0;
4883 case CONSTRUCTOR:
4884 if (TREE_CLOBBER_P (exp))
4885 return NULL;
4886 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
4888 unsigned i;
4889 tree val;
4891 op0 = gen_rtx_CONCATN
4892 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4894 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
4896 op1 = expand_debug_expr (val);
4897 if (!op1)
4898 return NULL;
4899 XVECEXP (op0, 0, i) = op1;
4902 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
4904 op1 = expand_debug_expr
4905 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
4907 if (!op1)
4908 return NULL;
4910 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
4911 XVECEXP (op0, 0, i) = op1;
4914 return op0;
4916 else
4917 goto flag_unsupported;
4919 case CALL_EXPR:
4920 /* ??? Maybe handle some builtins? */
4921 return NULL;
4923 case SSA_NAME:
4925 gimple *g = get_gimple_for_ssa_name (exp);
4926 if (g)
4928 tree t = NULL_TREE;
4929 if (deep_ter_debug_map)
4931 tree *slot = deep_ter_debug_map->get (exp);
4932 if (slot)
4933 t = *slot;
4935 if (t == NULL_TREE)
4936 t = gimple_assign_rhs_to_tree (g);
4937 op0 = expand_debug_expr (t);
4938 if (!op0)
4939 return NULL;
4941 else
4943 /* If this is a reference to an incoming value of
4944 parameter that is never used in the code or where the
4945 incoming value is never used in the code, use
4946 PARM_DECL's DECL_RTL if set. */
4947 if (SSA_NAME_IS_DEFAULT_DEF (exp)
4948 && SSA_NAME_VAR (exp)
4949 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL
4950 && has_zero_uses (exp))
4952 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
4953 if (op0)
4954 goto adjust_mode;
4955 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
4956 if (op0)
4957 goto adjust_mode;
4960 int part = var_to_partition (SA.map, exp);
4962 if (part == NO_PARTITION)
4963 return NULL;
4965 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
4967 op0 = copy_rtx (SA.partition_to_pseudo[part]);
4969 goto adjust_mode;
4972 case ERROR_MARK:
4973 return NULL;
4975 /* Vector stuff. For most of the codes we don't have rtl codes. */
4976 case REALIGN_LOAD_EXPR:
4977 case REDUC_MAX_EXPR:
4978 case REDUC_MIN_EXPR:
4979 case REDUC_PLUS_EXPR:
4980 case VEC_COND_EXPR:
4981 case VEC_PACK_FIX_TRUNC_EXPR:
4982 case VEC_PACK_SAT_EXPR:
4983 case VEC_PACK_TRUNC_EXPR:
4984 case VEC_UNPACK_FLOAT_HI_EXPR:
4985 case VEC_UNPACK_FLOAT_LO_EXPR:
4986 case VEC_UNPACK_HI_EXPR:
4987 case VEC_UNPACK_LO_EXPR:
4988 case VEC_WIDEN_MULT_HI_EXPR:
4989 case VEC_WIDEN_MULT_LO_EXPR:
4990 case VEC_WIDEN_MULT_EVEN_EXPR:
4991 case VEC_WIDEN_MULT_ODD_EXPR:
4992 case VEC_WIDEN_LSHIFT_HI_EXPR:
4993 case VEC_WIDEN_LSHIFT_LO_EXPR:
4994 case VEC_PERM_EXPR:
4995 return NULL;
4997 /* Misc codes. */
4998 case ADDR_SPACE_CONVERT_EXPR:
4999 case FIXED_CONVERT_EXPR:
5000 case OBJ_TYPE_REF:
5001 case WITH_SIZE_EXPR:
5002 return NULL;
5004 case DOT_PROD_EXPR:
5005 if (SCALAR_INT_MODE_P (GET_MODE (op0))
5006 && SCALAR_INT_MODE_P (mode))
5009 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5010 0)))
5011 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
5012 inner_mode);
5014 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5015 1)))
5016 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
5017 inner_mode);
5018 op0 = simplify_gen_binary (MULT, mode, op0, op1);
5019 return simplify_gen_binary (PLUS, mode, op0, op2);
5021 return NULL;
5023 case WIDEN_MULT_EXPR:
5024 case WIDEN_MULT_PLUS_EXPR:
5025 case WIDEN_MULT_MINUS_EXPR:
5026 if (SCALAR_INT_MODE_P (GET_MODE (op0))
5027 && SCALAR_INT_MODE_P (mode))
5029 inner_mode = GET_MODE (op0);
5030 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
5031 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
5032 else
5033 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
5034 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
5035 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
5036 else
5037 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
5038 op0 = simplify_gen_binary (MULT, mode, op0, op1);
5039 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
5040 return op0;
5041 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
5042 return simplify_gen_binary (PLUS, mode, op0, op2);
5043 else
5044 return simplify_gen_binary (MINUS, mode, op2, op0);
5046 return NULL;
5048 case MULT_HIGHPART_EXPR:
5049 /* ??? Similar to the above. */
5050 return NULL;
5052 case WIDEN_SUM_EXPR:
5053 case WIDEN_LSHIFT_EXPR:
5054 if (SCALAR_INT_MODE_P (GET_MODE (op0))
5055 && SCALAR_INT_MODE_P (mode))
5058 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5059 0)))
5060 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
5061 inner_mode);
5062 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
5063 ? ASHIFT : PLUS, mode, op0, op1);
5065 return NULL;
5067 case FMA_EXPR:
5068 return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
5070 default:
5071 flag_unsupported:
5072 if (flag_checking)
5074 debug_tree (exp);
5075 gcc_unreachable ();
5077 return NULL;
5081 /* Return an RTX equivalent to the source bind value of the tree expression
5082 EXP. */
5084 static rtx
5085 expand_debug_source_expr (tree exp)
5087 rtx op0 = NULL_RTX;
5088 machine_mode mode = VOIDmode, inner_mode;
5090 switch (TREE_CODE (exp))
5092 case PARM_DECL:
5094 mode = DECL_MODE (exp);
5095 op0 = expand_debug_parm_decl (exp);
5096 if (op0)
5097 break;
5098 /* See if this isn't an argument that has been completely
5099 optimized out. */
5100 if (!DECL_RTL_SET_P (exp)
5101 && !DECL_INCOMING_RTL (exp)
5102 && DECL_ABSTRACT_ORIGIN (current_function_decl))
5104 tree aexp = DECL_ORIGIN (exp);
5105 if (DECL_CONTEXT (aexp)
5106 == DECL_ABSTRACT_ORIGIN (current_function_decl))
5108 vec<tree, va_gc> **debug_args;
5109 unsigned int ix;
5110 tree ddecl;
5111 debug_args = decl_debug_args_lookup (current_function_decl);
5112 if (debug_args != NULL)
5114 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
5115 ix += 2)
5116 if (ddecl == aexp)
5117 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
5121 break;
5123 default:
5124 break;
5127 if (op0 == NULL_RTX)
5128 return NULL_RTX;
5130 inner_mode = GET_MODE (op0);
5131 if (mode == inner_mode)
5132 return op0;
5134 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
5136 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
5137 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
5138 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
5139 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
5140 else
5141 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
5143 else if (FLOAT_MODE_P (mode))
5144 gcc_unreachable ();
5145 else if (FLOAT_MODE_P (inner_mode))
5147 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
5148 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
5149 else
5150 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
5152 else if (CONSTANT_P (op0)
5153 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
5154 op0 = lowpart_subreg (mode, op0, inner_mode);
5155 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
5156 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
5157 else
5158 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
5160 return op0;
5163 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
5164 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
5165 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
5167 static void
5168 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
5170 rtx exp = *exp_p;
5172 if (exp == NULL_RTX)
5173 return;
5175 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
5176 return;
5178 if (depth == 4)
5180 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
5181 rtx dval = make_debug_expr_from_rtl (exp);
5183 /* Emit a debug bind insn before INSN. */
5184 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
5185 DEBUG_EXPR_TREE_DECL (dval), exp,
5186 VAR_INIT_STATUS_INITIALIZED);
5188 emit_debug_insn_before (bind, insn);
5189 *exp_p = dval;
5190 return;
5193 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
5194 int i, j;
5195 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
5196 switch (*format_ptr++)
5198 case 'e':
5199 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
5200 break;
5202 case 'E':
5203 case 'V':
5204 for (j = 0; j < XVECLEN (exp, i); j++)
5205 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
5206 break;
5208 default:
5209 break;
5213 /* Expand the _LOCs in debug insns. We run this after expanding all
5214 regular insns, so that any variables referenced in the function
5215 will have their DECL_RTLs set. */
5217 static void
5218 expand_debug_locations (void)
5220 rtx_insn *insn;
5221 rtx_insn *last = get_last_insn ();
5222 int save_strict_alias = flag_strict_aliasing;
5224 /* New alias sets while setting up memory attributes cause
5225 -fcompare-debug failures, even though it doesn't bring about any
5226 codegen changes. */
5227 flag_strict_aliasing = 0;
5229 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5230 if (DEBUG_INSN_P (insn))
5232 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
5233 rtx val;
5234 rtx_insn *prev_insn, *insn2;
5235 machine_mode mode;
5237 if (value == NULL_TREE)
5238 val = NULL_RTX;
5239 else
5241 if (INSN_VAR_LOCATION_STATUS (insn)
5242 == VAR_INIT_STATUS_UNINITIALIZED)
5243 val = expand_debug_source_expr (value);
5244 /* The avoid_deep_ter_for_debug function inserts
5245 debug bind stmts after SSA_NAME definition, with the
5246 SSA_NAME as the whole bind location. Disable temporarily
5247 expansion of that SSA_NAME into the DEBUG_EXPR_DECL
5248 being defined in this DEBUG_INSN. */
5249 else if (deep_ter_debug_map && TREE_CODE (value) == SSA_NAME)
5251 tree *slot = deep_ter_debug_map->get (value);
5252 if (slot)
5254 if (*slot == INSN_VAR_LOCATION_DECL (insn))
5255 *slot = NULL_TREE;
5256 else
5257 slot = NULL;
5259 val = expand_debug_expr (value);
5260 if (slot)
5261 *slot = INSN_VAR_LOCATION_DECL (insn);
5263 else
5264 val = expand_debug_expr (value);
5265 gcc_assert (last == get_last_insn ());
5268 if (!val)
5269 val = gen_rtx_UNKNOWN_VAR_LOC ();
5270 else
5272 mode = GET_MODE (INSN_VAR_LOCATION (insn));
5274 gcc_assert (mode == GET_MODE (val)
5275 || (GET_MODE (val) == VOIDmode
5276 && (CONST_SCALAR_INT_P (val)
5277 || GET_CODE (val) == CONST_FIXED
5278 || GET_CODE (val) == LABEL_REF)));
5281 INSN_VAR_LOCATION_LOC (insn) = val;
5282 prev_insn = PREV_INSN (insn);
5283 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
5284 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
5287 flag_strict_aliasing = save_strict_alias;
5290 /* Performs swapping operands of commutative operations to expand
5291 the expensive one first. */
5293 static void
5294 reorder_operands (basic_block bb)
5296 unsigned int *lattice; /* Hold cost of each statement. */
5297 unsigned int i = 0, n = 0;
5298 gimple_stmt_iterator gsi;
5299 gimple_seq stmts;
5300 gimple *stmt;
5301 bool swap;
5302 tree op0, op1;
5303 ssa_op_iter iter;
5304 use_operand_p use_p;
5305 gimple *def0, *def1;
5307 /* Compute cost of each statement using estimate_num_insns. */
5308 stmts = bb_seq (bb);
5309 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5311 stmt = gsi_stmt (gsi);
5312 if (!is_gimple_debug (stmt))
5313 gimple_set_uid (stmt, n++);
5315 lattice = XNEWVEC (unsigned int, n);
5316 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5318 unsigned cost;
5319 stmt = gsi_stmt (gsi);
5320 if (is_gimple_debug (stmt))
5321 continue;
5322 cost = estimate_num_insns (stmt, &eni_size_weights);
5323 lattice[i] = cost;
5324 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
5326 tree use = USE_FROM_PTR (use_p);
5327 gimple *def_stmt;
5328 if (TREE_CODE (use) != SSA_NAME)
5329 continue;
5330 def_stmt = get_gimple_for_ssa_name (use);
5331 if (!def_stmt)
5332 continue;
5333 lattice[i] += lattice[gimple_uid (def_stmt)];
5335 i++;
5336 if (!is_gimple_assign (stmt)
5337 || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
5338 continue;
5339 op0 = gimple_op (stmt, 1);
5340 op1 = gimple_op (stmt, 2);
5341 if (TREE_CODE (op0) != SSA_NAME
5342 || TREE_CODE (op1) != SSA_NAME)
5343 continue;
5344 /* Swap operands if the second one is more expensive. */
5345 def0 = get_gimple_for_ssa_name (op0);
5346 def1 = get_gimple_for_ssa_name (op1);
5347 if (!def1)
5348 continue;
5349 swap = false;
5350 if (!def0 || lattice[gimple_uid (def1)] > lattice[gimple_uid (def0)])
5351 swap = true;
5352 if (swap)
5354 if (dump_file && (dump_flags & TDF_DETAILS))
5356 fprintf (dump_file, "Swap operands in stmt:\n");
5357 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5358 fprintf (dump_file, "Cost left opnd=%d, right opnd=%d\n",
5359 def0 ? lattice[gimple_uid (def0)] : 0,
5360 lattice[gimple_uid (def1)]);
5362 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
5363 gimple_assign_rhs2_ptr (stmt));
5366 XDELETE (lattice);
5369 /* Expand basic block BB from GIMPLE trees to RTL. */
5371 static basic_block
5372 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
5374 gimple_stmt_iterator gsi;
5375 gimple_seq stmts;
5376 gimple *stmt = NULL;
5377 rtx_note *note;
5378 rtx_insn *last;
5379 edge e;
5380 edge_iterator ei;
5382 if (dump_file)
5383 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
5384 bb->index);
5386 /* Note that since we are now transitioning from GIMPLE to RTL, we
5387 cannot use the gsi_*_bb() routines because they expect the basic
5388 block to be in GIMPLE, instead of RTL. Therefore, we need to
5389 access the BB sequence directly. */
5390 if (optimize)
5391 reorder_operands (bb);
5392 stmts = bb_seq (bb);
5393 bb->il.gimple.seq = NULL;
5394 bb->il.gimple.phi_nodes = NULL;
5395 rtl_profile_for_bb (bb);
5396 init_rtl_bb_info (bb);
5397 bb->flags |= BB_RTL;
5399 /* Remove the RETURN_EXPR if we may fall though to the exit
5400 instead. */
5401 gsi = gsi_last (stmts);
5402 if (!gsi_end_p (gsi)
5403 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
5405 greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
5407 gcc_assert (single_succ_p (bb));
5408 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
5410 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5411 && !gimple_return_retval (ret_stmt))
5413 gsi_remove (&gsi, false);
5414 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
5418 gsi = gsi_start (stmts);
5419 if (!gsi_end_p (gsi))
5421 stmt = gsi_stmt (gsi);
5422 if (gimple_code (stmt) != GIMPLE_LABEL)
5423 stmt = NULL;
5426 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
5428 if (stmt || elt)
5430 last = get_last_insn ();
5432 if (stmt)
5434 expand_gimple_stmt (stmt);
5435 gsi_next (&gsi);
5438 if (elt)
5439 emit_label (*elt);
5441 /* Java emits line number notes in the top of labels.
5442 ??? Make this go away once line number notes are obsoleted. */
5443 BB_HEAD (bb) = NEXT_INSN (last);
5444 if (NOTE_P (BB_HEAD (bb)))
5445 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
5446 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
5448 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5450 else
5451 BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
5453 NOTE_BASIC_BLOCK (note) = bb;
5455 for (; !gsi_end_p (gsi); gsi_next (&gsi))
5457 basic_block new_bb;
5459 stmt = gsi_stmt (gsi);
5461 /* If this statement is a non-debug one, and we generate debug
5462 insns, then this one might be the last real use of a TERed
5463 SSA_NAME, but where there are still some debug uses further
5464 down. Expanding the current SSA name in such further debug
5465 uses by their RHS might lead to wrong debug info, as coalescing
5466 might make the operands of such RHS be placed into the same
5467 pseudo as something else. Like so:
5468 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
5469 use(a_1);
5470 a_2 = ...
5471 #DEBUG ... => a_1
5472 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
5473 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
5474 the write to a_2 would actually have clobbered the place which
5475 formerly held a_0.
5477 So, instead of that, we recognize the situation, and generate
5478 debug temporaries at the last real use of TERed SSA names:
5479 a_1 = a_0 + 1;
5480 #DEBUG #D1 => a_1
5481 use(a_1);
5482 a_2 = ...
5483 #DEBUG ... => #D1
5485 if (MAY_HAVE_DEBUG_INSNS
5486 && SA.values
5487 && !is_gimple_debug (stmt))
5489 ssa_op_iter iter;
5490 tree op;
5491 gimple *def;
5493 location_t sloc = curr_insn_location ();
5495 /* Look for SSA names that have their last use here (TERed
5496 names always have only one real use). */
5497 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5498 if ((def = get_gimple_for_ssa_name (op)))
5500 imm_use_iterator imm_iter;
5501 use_operand_p use_p;
5502 bool have_debug_uses = false;
5504 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5506 if (gimple_debug_bind_p (USE_STMT (use_p)))
5508 have_debug_uses = true;
5509 break;
5513 if (have_debug_uses)
5515 /* OP is a TERed SSA name, with DEF its defining
5516 statement, and where OP is used in further debug
5517 instructions. Generate a debug temporary, and
5518 replace all uses of OP in debug insns with that
5519 temporary. */
5520 gimple *debugstmt;
5521 tree value = gimple_assign_rhs_to_tree (def);
5522 tree vexpr = make_node (DEBUG_EXPR_DECL);
5523 rtx val;
5524 machine_mode mode;
5526 set_curr_insn_location (gimple_location (def));
5528 DECL_ARTIFICIAL (vexpr) = 1;
5529 TREE_TYPE (vexpr) = TREE_TYPE (value);
5530 if (DECL_P (value))
5531 mode = DECL_MODE (value);
5532 else
5533 mode = TYPE_MODE (TREE_TYPE (value));
5534 DECL_MODE (vexpr) = mode;
5536 val = gen_rtx_VAR_LOCATION
5537 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5539 emit_debug_insn (val);
5541 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5543 if (!gimple_debug_bind_p (debugstmt))
5544 continue;
5546 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5547 SET_USE (use_p, vexpr);
5549 update_stmt (debugstmt);
5553 set_curr_insn_location (sloc);
5556 currently_expanding_gimple_stmt = stmt;
5558 /* Expand this statement, then evaluate the resulting RTL and
5559 fixup the CFG accordingly. */
5560 if (gimple_code (stmt) == GIMPLE_COND)
5562 new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
5563 if (new_bb)
5564 return new_bb;
5566 else if (gimple_debug_bind_p (stmt))
5568 location_t sloc = curr_insn_location ();
5569 gimple_stmt_iterator nsi = gsi;
5571 for (;;)
5573 tree var = gimple_debug_bind_get_var (stmt);
5574 tree value;
5575 rtx val;
5576 machine_mode mode;
5578 if (TREE_CODE (var) != DEBUG_EXPR_DECL
5579 && TREE_CODE (var) != LABEL_DECL
5580 && !target_for_debug_bind (var))
5581 goto delink_debug_stmt;
5583 if (gimple_debug_bind_has_value_p (stmt))
5584 value = gimple_debug_bind_get_value (stmt);
5585 else
5586 value = NULL_TREE;
5588 last = get_last_insn ();
5590 set_curr_insn_location (gimple_location (stmt));
5592 if (DECL_P (var))
5593 mode = DECL_MODE (var);
5594 else
5595 mode = TYPE_MODE (TREE_TYPE (var));
5597 val = gen_rtx_VAR_LOCATION
5598 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5600 emit_debug_insn (val);
5602 if (dump_file && (dump_flags & TDF_DETAILS))
5604 /* We can't dump the insn with a TREE where an RTX
5605 is expected. */
5606 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5607 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5608 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5611 delink_debug_stmt:
5612 /* In order not to generate too many debug temporaries,
5613 we delink all uses of debug statements we already expanded.
5614 Therefore debug statements between definition and real
5615 use of TERed SSA names will continue to use the SSA name,
5616 and not be replaced with debug temps. */
5617 delink_stmt_imm_use (stmt);
5619 gsi = nsi;
5620 gsi_next (&nsi);
5621 if (gsi_end_p (nsi))
5622 break;
5623 stmt = gsi_stmt (nsi);
5624 if (!gimple_debug_bind_p (stmt))
5625 break;
5628 set_curr_insn_location (sloc);
5630 else if (gimple_debug_source_bind_p (stmt))
5632 location_t sloc = curr_insn_location ();
5633 tree var = gimple_debug_source_bind_get_var (stmt);
5634 tree value = gimple_debug_source_bind_get_value (stmt);
5635 rtx val;
5636 machine_mode mode;
5638 last = get_last_insn ();
5640 set_curr_insn_location (gimple_location (stmt));
5642 mode = DECL_MODE (var);
5644 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
5645 VAR_INIT_STATUS_UNINITIALIZED);
5647 emit_debug_insn (val);
5649 if (dump_file && (dump_flags & TDF_DETAILS))
5651 /* We can't dump the insn with a TREE where an RTX
5652 is expected. */
5653 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5654 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5655 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5658 set_curr_insn_location (sloc);
5660 else
5662 gcall *call_stmt = dyn_cast <gcall *> (stmt);
5663 if (call_stmt
5664 && gimple_call_tail_p (call_stmt)
5665 && disable_tail_calls)
5666 gimple_call_set_tail (call_stmt, false);
5668 if (call_stmt && gimple_call_tail_p (call_stmt))
5670 bool can_fallthru;
5671 new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
5672 if (new_bb)
5674 if (can_fallthru)
5675 bb = new_bb;
5676 else
5677 return new_bb;
5680 else
5682 def_operand_p def_p;
5683 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
5685 if (def_p != NULL)
5687 /* Ignore this stmt if it is in the list of
5688 replaceable expressions. */
5689 if (SA.values
5690 && bitmap_bit_p (SA.values,
5691 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
5692 continue;
5694 last = expand_gimple_stmt (stmt);
5695 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5700 currently_expanding_gimple_stmt = NULL;
5702 /* Expand implicit goto and convert goto_locus. */
5703 FOR_EACH_EDGE (e, ei, bb->succs)
5705 if (e->goto_locus != UNKNOWN_LOCATION)
5706 set_curr_insn_location (e->goto_locus);
5707 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
5709 emit_jump (label_rtx_for_bb (e->dest));
5710 e->flags &= ~EDGE_FALLTHRU;
5714 /* Expanded RTL can create a jump in the last instruction of block.
5715 This later might be assumed to be a jump to successor and break edge insertion.
5716 We need to insert dummy move to prevent this. PR41440. */
5717 if (single_succ_p (bb)
5718 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
5719 && (last = get_last_insn ())
5720 && JUMP_P (last))
5722 rtx dummy = gen_reg_rtx (SImode);
5723 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
5726 do_pending_stack_adjust ();
5728 /* Find the block tail. The last insn in the block is the insn
5729 before a barrier and/or table jump insn. */
5730 last = get_last_insn ();
5731 if (BARRIER_P (last))
5732 last = PREV_INSN (last);
5733 if (JUMP_TABLE_DATA_P (last))
5734 last = PREV_INSN (PREV_INSN (last));
5735 BB_END (bb) = last;
5737 update_bb_for_insn (bb);
5739 return bb;
5743 /* Create a basic block for initialization code. */
5745 static basic_block
5746 construct_init_block (void)
5748 basic_block init_block, first_block;
5749 edge e = NULL;
5750 int flags;
5752 /* Multiple entry points not supported yet. */
5753 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
5754 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5755 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
5756 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5757 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5759 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
5761 /* When entry edge points to first basic block, we don't need jump,
5762 otherwise we have to jump into proper target. */
5763 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
5765 tree label = gimple_block_label (e->dest);
5767 emit_jump (jump_target_rtx (label));
5768 flags = 0;
5770 else
5771 flags = EDGE_FALLTHRU;
5773 init_block = create_basic_block (NEXT_INSN (get_insns ()),
5774 get_last_insn (),
5775 ENTRY_BLOCK_PTR_FOR_FN (cfun));
5776 init_block->frequency = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
5777 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5778 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5779 if (e)
5781 first_block = e->dest;
5782 redirect_edge_succ (e, init_block);
5783 e = make_edge (init_block, first_block, flags);
5785 else
5786 e = make_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5787 e->probability = REG_BR_PROB_BASE;
5788 e->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5790 update_bb_for_insn (init_block);
5791 return init_block;
5794 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
5795 found in the block tree. */
5797 static void
5798 set_block_levels (tree block, int level)
5800 while (block)
5802 BLOCK_NUMBER (block) = level;
5803 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
5804 block = BLOCK_CHAIN (block);
5808 /* Create a block containing landing pads and similar stuff. */
5810 static void
5811 construct_exit_block (void)
5813 rtx_insn *head = get_last_insn ();
5814 rtx_insn *end;
5815 basic_block exit_block;
5816 edge e, e2;
5817 unsigned ix;
5818 edge_iterator ei;
5819 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
5820 rtx_insn *orig_end = BB_END (prev_bb);
5822 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5824 /* Make sure the locus is set to the end of the function, so that
5825 epilogue line numbers and warnings are set properly. */
5826 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
5827 input_location = cfun->function_end_locus;
5829 /* Generate rtl for function exit. */
5830 expand_function_end ();
5832 end = get_last_insn ();
5833 if (head == end)
5834 return;
5835 /* While emitting the function end we could move end of the last basic
5836 block. */
5837 BB_END (prev_bb) = orig_end;
5838 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
5839 head = NEXT_INSN (head);
5840 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
5841 bb frequency counting will be confused. Any instructions before that
5842 label are emitted for the case where PREV_BB falls through into the
5843 exit block, so append those instructions to prev_bb in that case. */
5844 if (NEXT_INSN (head) != return_label)
5846 while (NEXT_INSN (head) != return_label)
5848 if (!NOTE_P (NEXT_INSN (head)))
5849 BB_END (prev_bb) = NEXT_INSN (head);
5850 head = NEXT_INSN (head);
5853 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
5854 exit_block->frequency = EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency;
5855 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5856 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5858 ix = 0;
5859 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
5861 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
5862 if (!(e->flags & EDGE_ABNORMAL))
5863 redirect_edge_succ (e, exit_block);
5864 else
5865 ix++;
5868 e = make_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5869 e->probability = REG_BR_PROB_BASE;
5870 e->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5871 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5872 if (e2 != e)
5874 e->count -= e2->count;
5875 exit_block->count -= e2->count;
5876 exit_block->frequency -= EDGE_FREQUENCY (e2);
5878 if (e->count < 0)
5879 e->count = 0;
5880 if (exit_block->count < 0)
5881 exit_block->count = 0;
5882 if (exit_block->frequency < 0)
5883 exit_block->frequency = 0;
5884 update_bb_for_insn (exit_block);
5887 /* Helper function for discover_nonconstant_array_refs.
5888 Look for ARRAY_REF nodes with non-constant indexes and mark them
5889 addressable. */
5891 static tree
5892 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
5893 void *data ATTRIBUTE_UNUSED)
5895 tree t = *tp;
5897 if (IS_TYPE_OR_DECL_P (t))
5898 *walk_subtrees = 0;
5899 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5901 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5902 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
5903 && (!TREE_OPERAND (t, 2)
5904 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5905 || (TREE_CODE (t) == COMPONENT_REF
5906 && (!TREE_OPERAND (t,2)
5907 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5908 || TREE_CODE (t) == BIT_FIELD_REF
5909 || TREE_CODE (t) == REALPART_EXPR
5910 || TREE_CODE (t) == IMAGPART_EXPR
5911 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5912 || CONVERT_EXPR_P (t))
5913 t = TREE_OPERAND (t, 0);
5915 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5917 t = get_base_address (t);
5918 if (t && DECL_P (t)
5919 && DECL_MODE (t) != BLKmode)
5920 TREE_ADDRESSABLE (t) = 1;
5923 *walk_subtrees = 0;
5926 return NULL_TREE;
5929 /* RTL expansion is not able to compile array references with variable
5930 offsets for arrays stored in single register. Discover such
5931 expressions and mark variables as addressable to avoid this
5932 scenario. */
5934 static void
5935 discover_nonconstant_array_refs (void)
5937 basic_block bb;
5938 gimple_stmt_iterator gsi;
5940 FOR_EACH_BB_FN (bb, cfun)
5941 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5943 gimple *stmt = gsi_stmt (gsi);
5944 if (!is_gimple_debug (stmt))
5945 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
5949 /* This function sets crtl->args.internal_arg_pointer to a virtual
5950 register if DRAP is needed. Local register allocator will replace
5951 virtual_incoming_args_rtx with the virtual register. */
5953 static void
5954 expand_stack_alignment (void)
5956 rtx drap_rtx;
5957 unsigned int preferred_stack_boundary;
5959 if (! SUPPORTS_STACK_ALIGNMENT)
5960 return;
5962 if (cfun->calls_alloca
5963 || cfun->has_nonlocal_label
5964 || crtl->has_nonlocal_goto)
5965 crtl->need_drap = true;
5967 /* Call update_stack_boundary here again to update incoming stack
5968 boundary. It may set incoming stack alignment to a different
5969 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
5970 use the minimum incoming stack alignment to check if it is OK
5971 to perform sibcall optimization since sibcall optimization will
5972 only align the outgoing stack to incoming stack boundary. */
5973 if (targetm.calls.update_stack_boundary)
5974 targetm.calls.update_stack_boundary ();
5976 /* The incoming stack frame has to be aligned at least at
5977 parm_stack_boundary. */
5978 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
5980 /* Update crtl->stack_alignment_estimated and use it later to align
5981 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
5982 exceptions since callgraph doesn't collect incoming stack alignment
5983 in this case. */
5984 if (cfun->can_throw_non_call_exceptions
5985 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
5986 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
5987 else
5988 preferred_stack_boundary = crtl->preferred_stack_boundary;
5989 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
5990 crtl->stack_alignment_estimated = preferred_stack_boundary;
5991 if (preferred_stack_boundary > crtl->stack_alignment_needed)
5992 crtl->stack_alignment_needed = preferred_stack_boundary;
5994 gcc_assert (crtl->stack_alignment_needed
5995 <= crtl->stack_alignment_estimated);
5997 crtl->stack_realign_needed
5998 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
5999 crtl->stack_realign_tried = crtl->stack_realign_needed;
6001 crtl->stack_realign_processed = true;
6003 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
6004 alignment. */
6005 gcc_assert (targetm.calls.get_drap_rtx != NULL);
6006 drap_rtx = targetm.calls.get_drap_rtx ();
6008 /* stack_realign_drap and drap_rtx must match. */
6009 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
6011 /* Do nothing if NULL is returned, which means DRAP is not needed. */
6012 if (NULL != drap_rtx)
6014 crtl->args.internal_arg_pointer = drap_rtx;
6016 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
6017 needed. */
6018 fixup_tail_calls ();
6023 static void
6024 expand_main_function (void)
6026 #if (defined(INVOKE__main) \
6027 || (!defined(HAS_INIT_SECTION) \
6028 && !defined(INIT_SECTION_ASM_OP) \
6029 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
6030 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
6031 #endif
6035 /* Expand code to initialize the stack_protect_guard. This is invoked at
6036 the beginning of a function to be protected. */
6038 static void
6039 stack_protect_prologue (void)
6041 tree guard_decl = targetm.stack_protect_guard ();
6042 rtx x, y;
6044 x = expand_normal (crtl->stack_protect_guard);
6045 y = expand_normal (guard_decl);
6047 /* Allow the target to copy from Y to X without leaking Y into a
6048 register. */
6049 if (targetm.have_stack_protect_set ())
6050 if (rtx_insn *insn = targetm.gen_stack_protect_set (x, y))
6052 emit_insn (insn);
6053 return;
6056 /* Otherwise do a straight move. */
6057 emit_move_insn (x, y);
6060 /* Translate the intermediate representation contained in the CFG
6061 from GIMPLE trees to RTL.
6063 We do conversion per basic block and preserve/update the tree CFG.
6064 This implies we have to do some magic as the CFG can simultaneously
6065 consist of basic blocks containing RTL and GIMPLE trees. This can
6066 confuse the CFG hooks, so be careful to not manipulate CFG during
6067 the expansion. */
6069 namespace {
6071 const pass_data pass_data_expand =
6073 RTL_PASS, /* type */
6074 "expand", /* name */
6075 OPTGROUP_NONE, /* optinfo_flags */
6076 TV_EXPAND, /* tv_id */
6077 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
6078 | PROP_gimple_lcx
6079 | PROP_gimple_lvec
6080 | PROP_gimple_lva), /* properties_required */
6081 PROP_rtl, /* properties_provided */
6082 ( PROP_ssa | PROP_trees ), /* properties_destroyed */
6083 0, /* todo_flags_start */
6084 0, /* todo_flags_finish */
6087 class pass_expand : public rtl_opt_pass
6089 public:
6090 pass_expand (gcc::context *ctxt)
6091 : rtl_opt_pass (pass_data_expand, ctxt)
6094 /* opt_pass methods: */
6095 virtual unsigned int execute (function *);
6097 }; // class pass_expand
6099 unsigned int
6100 pass_expand::execute (function *fun)
6102 basic_block bb, init_block;
6103 sbitmap blocks;
6104 edge_iterator ei;
6105 edge e;
6106 rtx_insn *var_seq, *var_ret_seq;
6107 unsigned i;
6109 timevar_push (TV_OUT_OF_SSA);
6110 rewrite_out_of_ssa (&SA);
6111 timevar_pop (TV_OUT_OF_SSA);
6112 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
6114 if (MAY_HAVE_DEBUG_STMTS && flag_tree_ter)
6116 gimple_stmt_iterator gsi;
6117 FOR_EACH_BB_FN (bb, cfun)
6118 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6119 if (gimple_debug_bind_p (gsi_stmt (gsi)))
6120 avoid_deep_ter_for_debug (gsi_stmt (gsi), 0);
6123 /* Make sure all values used by the optimization passes have sane
6124 defaults. */
6125 reg_renumber = 0;
6127 /* Some backends want to know that we are expanding to RTL. */
6128 currently_expanding_to_rtl = 1;
6129 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
6130 free_dominance_info (CDI_DOMINATORS);
6132 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
6134 if (chkp_function_instrumented_p (current_function_decl))
6135 chkp_reset_rtl_bounds ();
6137 insn_locations_init ();
6138 if (!DECL_IS_BUILTIN (current_function_decl))
6140 /* Eventually, all FEs should explicitly set function_start_locus. */
6141 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
6142 set_curr_insn_location
6143 (DECL_SOURCE_LOCATION (current_function_decl));
6144 else
6145 set_curr_insn_location (fun->function_start_locus);
6147 else
6148 set_curr_insn_location (UNKNOWN_LOCATION);
6149 prologue_location = curr_insn_location ();
6151 #ifdef INSN_SCHEDULING
6152 init_sched_attrs ();
6153 #endif
6155 /* Make sure first insn is a note even if we don't want linenums.
6156 This makes sure the first insn will never be deleted.
6157 Also, final expects a note to appear there. */
6158 emit_note (NOTE_INSN_DELETED);
6160 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
6161 discover_nonconstant_array_refs ();
6163 targetm.expand_to_rtl_hook ();
6164 crtl->stack_alignment_needed = STACK_BOUNDARY;
6165 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
6166 crtl->stack_alignment_estimated = 0;
6167 crtl->preferred_stack_boundary = STACK_BOUNDARY;
6168 fun->cfg->max_jumptable_ents = 0;
6170 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
6171 of the function section at exapnsion time to predict distance of calls. */
6172 resolve_unique_section (current_function_decl, 0, flag_function_sections);
6174 /* Expand the variables recorded during gimple lowering. */
6175 timevar_push (TV_VAR_EXPAND);
6176 start_sequence ();
6178 var_ret_seq = expand_used_vars ();
6180 var_seq = get_insns ();
6181 end_sequence ();
6182 timevar_pop (TV_VAR_EXPAND);
6184 /* Honor stack protection warnings. */
6185 if (warn_stack_protect)
6187 if (fun->calls_alloca)
6188 warning (OPT_Wstack_protector,
6189 "stack protector not protecting local variables: "
6190 "variable length buffer");
6191 if (has_short_buffer && !crtl->stack_protect_guard)
6192 warning (OPT_Wstack_protector,
6193 "stack protector not protecting function: "
6194 "all local arrays are less than %d bytes long",
6195 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
6198 /* Set up parameters and prepare for return, for the function. */
6199 expand_function_start (current_function_decl);
6201 /* If we emitted any instructions for setting up the variables,
6202 emit them before the FUNCTION_START note. */
6203 if (var_seq)
6205 emit_insn_before (var_seq, parm_birth_insn);
6207 /* In expand_function_end we'll insert the alloca save/restore
6208 before parm_birth_insn. We've just insertted an alloca call.
6209 Adjust the pointer to match. */
6210 parm_birth_insn = var_seq;
6213 /* Now propagate the RTL assignment of each partition to the
6214 underlying var of each SSA_NAME. */
6215 for (i = 1; i < num_ssa_names; i++)
6217 tree name = ssa_name (i);
6219 if (!name
6220 /* We might have generated new SSA names in
6221 update_alias_info_with_stack_vars. They will have a NULL
6222 defining statements, and won't be part of the partitioning,
6223 so ignore those. */
6224 || !SSA_NAME_DEF_STMT (name))
6225 continue;
6227 adjust_one_expanded_partition_var (name);
6230 /* Clean up RTL of variables that straddle across multiple
6231 partitions, and check that the rtl of any PARM_DECLs that are not
6232 cleaned up is that of their default defs. */
6233 for (i = 1; i < num_ssa_names; i++)
6235 tree name = ssa_name (i);
6236 int part;
6238 if (!name
6239 /* We might have generated new SSA names in
6240 update_alias_info_with_stack_vars. They will have a NULL
6241 defining statements, and won't be part of the partitioning,
6242 so ignore those. */
6243 || !SSA_NAME_DEF_STMT (name))
6244 continue;
6245 part = var_to_partition (SA.map, name);
6246 if (part == NO_PARTITION)
6247 continue;
6249 /* If this decl was marked as living in multiple places, reset
6250 this now to NULL. */
6251 tree var = SSA_NAME_VAR (name);
6252 if (var && DECL_RTL_IF_SET (var) == pc_rtx)
6253 SET_DECL_RTL (var, NULL);
6254 /* Check that the pseudos chosen by assign_parms are those of
6255 the corresponding default defs. */
6256 else if (SSA_NAME_IS_DEFAULT_DEF (name)
6257 && (TREE_CODE (var) == PARM_DECL
6258 || TREE_CODE (var) == RESULT_DECL))
6260 rtx in = DECL_RTL_IF_SET (var);
6261 gcc_assert (in);
6262 rtx out = SA.partition_to_pseudo[part];
6263 gcc_assert (in == out);
6265 /* Now reset VAR's RTL to IN, so that the _EXPR attrs match
6266 those expected by debug backends for each parm and for
6267 the result. This is particularly important for stabs,
6268 whose register elimination from parm's DECL_RTL may cause
6269 -fcompare-debug differences as SET_DECL_RTL changes reg's
6270 attrs. So, make sure the RTL already has the parm as the
6271 EXPR, so that it won't change. */
6272 SET_DECL_RTL (var, NULL_RTX);
6273 if (MEM_P (in))
6274 set_mem_attributes (in, var, true);
6275 SET_DECL_RTL (var, in);
6279 /* If this function is `main', emit a call to `__main'
6280 to run global initializers, etc. */
6281 if (DECL_NAME (current_function_decl)
6282 && MAIN_NAME_P (DECL_NAME (current_function_decl))
6283 && DECL_FILE_SCOPE_P (current_function_decl))
6284 expand_main_function ();
6286 /* Initialize the stack_protect_guard field. This must happen after the
6287 call to __main (if any) so that the external decl is initialized. */
6288 if (crtl->stack_protect_guard)
6289 stack_protect_prologue ();
6291 expand_phi_nodes (&SA);
6293 /* Release any stale SSA redirection data. */
6294 redirect_edge_var_map_destroy ();
6296 /* Register rtl specific functions for cfg. */
6297 rtl_register_cfg_hooks ();
6299 init_block = construct_init_block ();
6301 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
6302 remaining edges later. */
6303 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
6304 e->flags &= ~EDGE_EXECUTABLE;
6306 lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
6307 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
6308 next_bb)
6309 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
6311 if (MAY_HAVE_DEBUG_INSNS)
6312 expand_debug_locations ();
6314 if (deep_ter_debug_map)
6316 delete deep_ter_debug_map;
6317 deep_ter_debug_map = NULL;
6320 /* Free stuff we no longer need after GIMPLE optimizations. */
6321 free_dominance_info (CDI_DOMINATORS);
6322 free_dominance_info (CDI_POST_DOMINATORS);
6323 delete_tree_cfg_annotations (fun);
6325 timevar_push (TV_OUT_OF_SSA);
6326 finish_out_of_ssa (&SA);
6327 timevar_pop (TV_OUT_OF_SSA);
6329 timevar_push (TV_POST_EXPAND);
6330 /* We are no longer in SSA form. */
6331 fun->gimple_df->in_ssa_p = false;
6332 loops_state_clear (LOOP_CLOSED_SSA);
6334 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
6335 conservatively to true until they are all profile aware. */
6336 delete lab_rtx_for_bb;
6337 free_histograms (fun);
6339 construct_exit_block ();
6340 insn_locations_finalize ();
6342 if (var_ret_seq)
6344 rtx_insn *after = return_label;
6345 rtx_insn *next = NEXT_INSN (after);
6346 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
6347 after = next;
6348 emit_insn_after (var_ret_seq, after);
6351 /* Zap the tree EH table. */
6352 set_eh_throw_stmt_table (fun, NULL);
6354 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
6355 split edges which edge insertions might do. */
6356 rebuild_jump_labels (get_insns ());
6358 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun),
6359 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6361 edge e;
6362 edge_iterator ei;
6363 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6365 if (e->insns.r)
6367 rebuild_jump_labels_chain (e->insns.r);
6368 /* Put insns after parm birth, but before
6369 NOTE_INSNS_FUNCTION_BEG. */
6370 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (fun)
6371 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
6373 rtx_insn *insns = e->insns.r;
6374 e->insns.r = NULL;
6375 if (NOTE_P (parm_birth_insn)
6376 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
6377 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
6378 else
6379 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
6381 else
6382 commit_one_edge_insertion (e);
6384 else
6385 ei_next (&ei);
6389 /* We're done expanding trees to RTL. */
6390 currently_expanding_to_rtl = 0;
6392 flush_mark_addressable_queue ();
6394 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
6395 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6397 edge e;
6398 edge_iterator ei;
6399 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6401 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
6402 e->flags &= ~EDGE_EXECUTABLE;
6404 /* At the moment not all abnormal edges match the RTL
6405 representation. It is safe to remove them here as
6406 find_many_sub_basic_blocks will rediscover them.
6407 In the future we should get this fixed properly. */
6408 if ((e->flags & EDGE_ABNORMAL)
6409 && !(e->flags & EDGE_SIBCALL))
6410 remove_edge (e);
6411 else
6412 ei_next (&ei);
6416 blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
6417 bitmap_ones (blocks);
6418 find_many_sub_basic_blocks (blocks);
6419 sbitmap_free (blocks);
6420 purge_all_dead_edges ();
6422 expand_stack_alignment ();
6424 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
6425 function. */
6426 if (crtl->tail_call_emit)
6427 fixup_tail_calls ();
6429 /* After initial rtl generation, call back to finish generating
6430 exception support code. We need to do this before cleaning up
6431 the CFG as the code does not expect dead landing pads. */
6432 if (fun->eh->region_tree != NULL)
6433 finish_eh_generation ();
6435 /* Remove unreachable blocks, otherwise we cannot compute dominators
6436 which are needed for loop state verification. As a side-effect
6437 this also compacts blocks.
6438 ??? We cannot remove trivially dead insns here as for example
6439 the DRAP reg on i?86 is not magically live at this point.
6440 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
6441 cleanup_cfg (CLEANUP_NO_INSN_DEL);
6443 checking_verify_flow_info ();
6445 /* Initialize pseudos allocated for hard registers. */
6446 emit_initial_value_sets ();
6448 /* And finally unshare all RTL. */
6449 unshare_all_rtl ();
6451 /* There's no need to defer outputting this function any more; we
6452 know we want to output it. */
6453 DECL_DEFER_OUTPUT (current_function_decl) = 0;
6455 /* Now that we're done expanding trees to RTL, we shouldn't have any
6456 more CONCATs anywhere. */
6457 generating_concat_p = 0;
6459 if (dump_file)
6461 fprintf (dump_file,
6462 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
6463 /* And the pass manager will dump RTL for us. */
6466 /* If we're emitting a nested function, make sure its parent gets
6467 emitted as well. Doing otherwise confuses debug info. */
6469 tree parent;
6470 for (parent = DECL_CONTEXT (current_function_decl);
6471 parent != NULL_TREE;
6472 parent = get_containing_scope (parent))
6473 if (TREE_CODE (parent) == FUNCTION_DECL)
6474 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
6477 /* We are now committed to emitting code for this function. Do any
6478 preparation, such as emitting abstract debug info for the inline
6479 before it gets mangled by optimization. */
6480 if (cgraph_function_possibly_inlined_p (current_function_decl))
6481 (*debug_hooks->outlining_inline_function) (current_function_decl);
6483 TREE_ASM_WRITTEN (current_function_decl) = 1;
6485 /* After expanding, the return labels are no longer needed. */
6486 return_label = NULL;
6487 naked_return_label = NULL;
6489 /* After expanding, the tm_restart map is no longer needed. */
6490 if (fun->gimple_df->tm_restart)
6491 fun->gimple_df->tm_restart = NULL;
6493 /* Tag the blocks with a depth number so that change_scope can find
6494 the common parent easily. */
6495 set_block_levels (DECL_INITIAL (fun->decl), 0);
6496 default_rtl_profile ();
6498 timevar_pop (TV_POST_EXPAND);
6500 return 0;
6503 } // anon namespace
6505 rtl_opt_pass *
6506 make_pass_expand (gcc::context *ctxt)
6508 return new pass_expand (ctxt);