[PR67828] don't unswitch on default defs of non-parms
[official-gcc.git] / gcc / cfgexpand.c
blob58e55d26341ae7196fdf8276b8e0024075cd9fd7
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 "cfghooks.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "rtl.h"
28 #include "ssa.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "varasm.h"
32 #include "stor-layout.h"
33 #include "stmt.h"
34 #include "print-tree.h"
35 #include "tm_p.h"
36 #include "cfgrtl.h"
37 #include "cfganal.h"
38 #include "cfgbuild.h"
39 #include "cfgcleanup.h"
40 #include "insn-codes.h"
41 #include "optabs.h"
42 #include "flags.h"
43 #include "insn-config.h"
44 #include "expmed.h"
45 #include "dojump.h"
46 #include "explow.h"
47 #include "calls.h"
48 #include "emit-rtl.h"
49 #include "expr.h"
50 #include "langhooks.h"
51 #include "internal-fn.h"
52 #include "tree-eh.h"
53 #include "gimple-iterator.h"
54 #include "gimple-walk.h"
55 #include "cgraph.h"
56 #include "tree-cfg.h"
57 #include "tree-dfa.h"
58 #include "tree-ssa.h"
59 #include "tree-pass.h"
60 #include "except.h"
61 #include "diagnostic.h"
62 #include "gimple-pretty-print.h"
63 #include "toplev.h"
64 #include "debug.h"
65 #include "params.h"
66 #include "tree-inline.h"
67 #include "value-prof.h"
68 #include "target.h"
69 #include "tree-ssa-live.h"
70 #include "tree-outof-ssa.h"
71 #include "cfgloop.h"
72 #include "regs.h" /* For reg_renumber. */
73 #include "insn-attr.h" /* For INSN_SCHEDULING. */
74 #include "asan.h"
75 #include "tree-ssa-address.h"
76 #include "recog.h"
77 #include "output.h"
78 #include "builtins.h"
79 #include "tree-chkp.h"
80 #include "rtl-chkp.h"
82 /* Some systems use __main in a way incompatible with its use in gcc, in these
83 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
84 give the same symbol without quotes for an alternative entry point. You
85 must define both, or neither. */
86 #ifndef NAME__MAIN
87 #define NAME__MAIN "__main"
88 #endif
90 /* This variable holds information helping the rewriting of SSA trees
91 into RTL. */
92 struct ssaexpand SA;
94 /* This variable holds the currently expanded gimple statement for purposes
95 of comminucating the profile info to the builtin expanders. */
96 gimple *currently_expanding_gimple_stmt;
98 static rtx expand_debug_expr (tree);
100 static bool defer_stack_allocation (tree, bool);
102 static void record_alignment_for_reg_var (unsigned int);
104 /* Return an expression tree corresponding to the RHS of GIMPLE
105 statement STMT. */
107 tree
108 gimple_assign_rhs_to_tree (gimple *stmt)
110 tree t;
111 enum gimple_rhs_class grhs_class;
113 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
115 if (grhs_class == GIMPLE_TERNARY_RHS)
116 t = build3 (gimple_assign_rhs_code (stmt),
117 TREE_TYPE (gimple_assign_lhs (stmt)),
118 gimple_assign_rhs1 (stmt),
119 gimple_assign_rhs2 (stmt),
120 gimple_assign_rhs3 (stmt));
121 else if (grhs_class == GIMPLE_BINARY_RHS)
122 t = build2 (gimple_assign_rhs_code (stmt),
123 TREE_TYPE (gimple_assign_lhs (stmt)),
124 gimple_assign_rhs1 (stmt),
125 gimple_assign_rhs2 (stmt));
126 else if (grhs_class == GIMPLE_UNARY_RHS)
127 t = build1 (gimple_assign_rhs_code (stmt),
128 TREE_TYPE (gimple_assign_lhs (stmt)),
129 gimple_assign_rhs1 (stmt));
130 else if (grhs_class == GIMPLE_SINGLE_RHS)
132 t = gimple_assign_rhs1 (stmt);
133 /* Avoid modifying this tree in place below. */
134 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
135 && gimple_location (stmt) != EXPR_LOCATION (t))
136 || (gimple_block (stmt)
137 && currently_expanding_to_rtl
138 && EXPR_P (t)))
139 t = copy_node (t);
141 else
142 gcc_unreachable ();
144 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
145 SET_EXPR_LOCATION (t, gimple_location (stmt));
147 return t;
151 #ifndef STACK_ALIGNMENT_NEEDED
152 #define STACK_ALIGNMENT_NEEDED 1
153 #endif
155 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
157 /* Choose either CUR or NEXT as the leader DECL for a partition.
158 Prefer ignored decls, to simplify debug dumps and reduce ambiguity
159 out of the same user variable being in multiple partitions (this is
160 less likely for compiler-introduced temps). */
162 static tree
163 leader_merge (tree cur, tree next)
165 if (cur == NULL || cur == next)
166 return next;
168 if (DECL_P (cur) && DECL_IGNORED_P (cur))
169 return cur;
171 if (DECL_P (next) && DECL_IGNORED_P (next))
172 return next;
174 return cur;
177 /* Associate declaration T with storage space X. If T is no
178 SSA name this is exactly SET_DECL_RTL, otherwise make the
179 partition of T associated with X. */
180 static inline void
181 set_rtl (tree t, rtx x)
183 gcc_checking_assert (!x
184 || !(TREE_CODE (t) == SSA_NAME || is_gimple_reg (t))
185 || (use_register_for_decl (t)
186 ? (REG_P (x)
187 || (GET_CODE (x) == CONCAT
188 && (REG_P (XEXP (x, 0))
189 || SUBREG_P (XEXP (x, 0)))
190 && (REG_P (XEXP (x, 1))
191 || SUBREG_P (XEXP (x, 1))))
192 || (GET_CODE (x) == PARALLEL
193 && SSAVAR (t)
194 && TREE_CODE (SSAVAR (t)) == RESULT_DECL
195 && !flag_tree_coalesce_vars))
196 : (MEM_P (x) || x == pc_rtx
197 || (GET_CODE (x) == CONCAT
198 && MEM_P (XEXP (x, 0))
199 && MEM_P (XEXP (x, 1))))));
200 /* Check that the RTL for SSA_NAMEs and gimple-reg PARM_DECLs and
201 RESULT_DECLs has the expected mode. For memory, we accept
202 unpromoted modes, since that's what we're likely to get. For
203 PARM_DECLs and RESULT_DECLs, we'll have been called by
204 set_parm_rtl, which will give us the default def, so we don't
205 have to compute it ourselves. For RESULT_DECLs, we accept mode
206 mismatches too, as long as we're not coalescing across variables,
207 so that we don't reject BLKmode PARALLELs or unpromoted REGs. */
208 gcc_checking_assert (!x || x == pc_rtx || TREE_CODE (t) != SSA_NAME
209 || (SSAVAR (t) && TREE_CODE (SSAVAR (t)) == RESULT_DECL
210 && !flag_tree_coalesce_vars)
211 || !use_register_for_decl (t)
212 || GET_MODE (x) == promote_ssa_mode (t, NULL));
214 if (x)
216 bool skip = false;
217 tree cur = NULL_TREE;
218 rtx xm = x;
220 retry:
221 if (MEM_P (xm))
222 cur = MEM_EXPR (xm);
223 else if (REG_P (xm))
224 cur = REG_EXPR (xm);
225 else if (SUBREG_P (xm))
227 gcc_assert (subreg_lowpart_p (xm));
228 xm = SUBREG_REG (xm);
229 goto retry;
231 else if (GET_CODE (xm) == CONCAT)
233 xm = XEXP (xm, 0);
234 goto retry;
236 else if (GET_CODE (xm) == PARALLEL)
238 xm = XVECEXP (xm, 0, 0);
239 gcc_assert (GET_CODE (xm) == EXPR_LIST);
240 xm = XEXP (xm, 0);
241 goto retry;
243 else if (xm == pc_rtx)
244 skip = true;
245 else
246 gcc_unreachable ();
248 tree next = skip ? cur : leader_merge (cur, SSAVAR (t) ? SSAVAR (t) : t);
250 if (cur != next)
252 if (MEM_P (x))
253 set_mem_attributes (x,
254 next && TREE_CODE (next) == SSA_NAME
255 ? TREE_TYPE (next)
256 : next, true);
257 else
258 set_reg_attrs_for_decl_rtl (next, x);
262 if (TREE_CODE (t) == SSA_NAME)
264 int part = var_to_partition (SA.map, t);
265 if (part != NO_PARTITION)
267 if (SA.partition_to_pseudo[part])
268 gcc_assert (SA.partition_to_pseudo[part] == x);
269 else if (x != pc_rtx)
270 SA.partition_to_pseudo[part] = x;
272 /* For the benefit of debug information at -O0 (where
273 vartracking doesn't run) record the place also in the base
274 DECL. For PARMs and RESULTs, do so only when setting the
275 default def. */
276 if (x && x != pc_rtx && SSA_NAME_VAR (t)
277 && (VAR_P (SSA_NAME_VAR (t))
278 || SSA_NAME_IS_DEFAULT_DEF (t)))
280 tree var = SSA_NAME_VAR (t);
281 /* If we don't yet have something recorded, just record it now. */
282 if (!DECL_RTL_SET_P (var))
283 SET_DECL_RTL (var, x);
284 /* If we have it set already to "multiple places" don't
285 change this. */
286 else if (DECL_RTL (var) == pc_rtx)
288 /* If we have something recorded and it's not the same place
289 as we want to record now, we have multiple partitions for the
290 same base variable, with different places. We can't just
291 randomly chose one, hence we have to say that we don't know.
292 This only happens with optimization, and there var-tracking
293 will figure out the right thing. */
294 else if (DECL_RTL (var) != x)
295 SET_DECL_RTL (var, pc_rtx);
298 else
299 SET_DECL_RTL (t, x);
302 /* This structure holds data relevant to one variable that will be
303 placed in a stack slot. */
304 struct stack_var
306 /* The Variable. */
307 tree decl;
309 /* Initially, the size of the variable. Later, the size of the partition,
310 if this variable becomes it's partition's representative. */
311 HOST_WIDE_INT size;
313 /* The *byte* alignment required for this variable. Or as, with the
314 size, the alignment for this partition. */
315 unsigned int alignb;
317 /* The partition representative. */
318 size_t representative;
320 /* The next stack variable in the partition, or EOC. */
321 size_t next;
323 /* The numbers of conflicting stack variables. */
324 bitmap conflicts;
327 #define EOC ((size_t)-1)
329 /* We have an array of such objects while deciding allocation. */
330 static struct stack_var *stack_vars;
331 static size_t stack_vars_alloc;
332 static size_t stack_vars_num;
333 static hash_map<tree, size_t> *decl_to_stack_part;
335 /* Conflict bitmaps go on this obstack. This allows us to destroy
336 all of them in one big sweep. */
337 static bitmap_obstack stack_var_bitmap_obstack;
339 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
340 is non-decreasing. */
341 static size_t *stack_vars_sorted;
343 /* The phase of the stack frame. This is the known misalignment of
344 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
345 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
346 static int frame_phase;
348 /* Used during expand_used_vars to remember if we saw any decls for
349 which we'd like to enable stack smashing protection. */
350 static bool has_protected_decls;
352 /* Used during expand_used_vars. Remember if we say a character buffer
353 smaller than our cutoff threshold. Used for -Wstack-protector. */
354 static bool has_short_buffer;
356 /* Compute the byte alignment to use for DECL. Ignore alignment
357 we can't do with expected alignment of the stack boundary. */
359 static unsigned int
360 align_local_variable (tree decl)
362 unsigned int align;
364 if (TREE_CODE (decl) == SSA_NAME)
365 align = TYPE_ALIGN (TREE_TYPE (decl));
366 else
368 align = LOCAL_DECL_ALIGNMENT (decl);
369 DECL_ALIGN (decl) = align;
371 return align / BITS_PER_UNIT;
374 /* Align given offset BASE with ALIGN. Truncate up if ALIGN_UP is true,
375 down otherwise. Return truncated BASE value. */
377 static inline unsigned HOST_WIDE_INT
378 align_base (HOST_WIDE_INT base, unsigned HOST_WIDE_INT align, bool align_up)
380 return align_up ? (base + align - 1) & -align : base & -align;
383 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
384 Return the frame offset. */
386 static HOST_WIDE_INT
387 alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
389 HOST_WIDE_INT offset, new_frame_offset;
391 if (FRAME_GROWS_DOWNWARD)
393 new_frame_offset
394 = align_base (frame_offset - frame_phase - size,
395 align, false) + frame_phase;
396 offset = new_frame_offset;
398 else
400 new_frame_offset
401 = align_base (frame_offset - frame_phase, align, true) + frame_phase;
402 offset = new_frame_offset;
403 new_frame_offset += size;
405 frame_offset = new_frame_offset;
407 if (frame_offset_overflow (frame_offset, cfun->decl))
408 frame_offset = offset = 0;
410 return offset;
413 /* Accumulate DECL into STACK_VARS. */
415 static void
416 add_stack_var (tree decl)
418 struct stack_var *v;
420 if (stack_vars_num >= stack_vars_alloc)
422 if (stack_vars_alloc)
423 stack_vars_alloc = stack_vars_alloc * 3 / 2;
424 else
425 stack_vars_alloc = 32;
426 stack_vars
427 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
429 if (!decl_to_stack_part)
430 decl_to_stack_part = new hash_map<tree, size_t>;
432 v = &stack_vars[stack_vars_num];
433 decl_to_stack_part->put (decl, stack_vars_num);
435 v->decl = decl;
436 tree size = TREE_CODE (decl) == SSA_NAME
437 ? TYPE_SIZE_UNIT (TREE_TYPE (decl))
438 : DECL_SIZE_UNIT (decl);
439 v->size = tree_to_uhwi (size);
440 /* Ensure that all variables have size, so that &a != &b for any two
441 variables that are simultaneously live. */
442 if (v->size == 0)
443 v->size = 1;
444 v->alignb = align_local_variable (decl);
445 /* An alignment of zero can mightily confuse us later. */
446 gcc_assert (v->alignb != 0);
448 /* All variables are initially in their own partition. */
449 v->representative = stack_vars_num;
450 v->next = EOC;
452 /* All variables initially conflict with no other. */
453 v->conflicts = NULL;
455 /* Ensure that this decl doesn't get put onto the list twice. */
456 set_rtl (decl, pc_rtx);
458 stack_vars_num++;
461 /* Make the decls associated with luid's X and Y conflict. */
463 static void
464 add_stack_var_conflict (size_t x, size_t y)
466 struct stack_var *a = &stack_vars[x];
467 struct stack_var *b = &stack_vars[y];
468 if (!a->conflicts)
469 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
470 if (!b->conflicts)
471 b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
472 bitmap_set_bit (a->conflicts, y);
473 bitmap_set_bit (b->conflicts, x);
476 /* Check whether the decls associated with luid's X and Y conflict. */
478 static bool
479 stack_var_conflict_p (size_t x, size_t y)
481 struct stack_var *a = &stack_vars[x];
482 struct stack_var *b = &stack_vars[y];
483 if (x == y)
484 return false;
485 /* Partitions containing an SSA name result from gimple registers
486 with things like unsupported modes. They are top-level and
487 hence conflict with everything else. */
488 if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
489 return true;
491 if (!a->conflicts || !b->conflicts)
492 return false;
493 return bitmap_bit_p (a->conflicts, y);
496 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
497 enter its partition number into bitmap DATA. */
499 static bool
500 visit_op (gimple *, tree op, tree, void *data)
502 bitmap active = (bitmap)data;
503 op = get_base_address (op);
504 if (op
505 && DECL_P (op)
506 && DECL_RTL_IF_SET (op) == pc_rtx)
508 size_t *v = decl_to_stack_part->get (op);
509 if (v)
510 bitmap_set_bit (active, *v);
512 return false;
515 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
516 record conflicts between it and all currently active other partitions
517 from bitmap DATA. */
519 static bool
520 visit_conflict (gimple *, tree op, tree, void *data)
522 bitmap active = (bitmap)data;
523 op = get_base_address (op);
524 if (op
525 && DECL_P (op)
526 && DECL_RTL_IF_SET (op) == pc_rtx)
528 size_t *v = decl_to_stack_part->get (op);
529 if (v && bitmap_set_bit (active, *v))
531 size_t num = *v;
532 bitmap_iterator bi;
533 unsigned i;
534 gcc_assert (num < stack_vars_num);
535 EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
536 add_stack_var_conflict (num, i);
539 return false;
542 /* Helper routine for add_scope_conflicts, calculating the active partitions
543 at the end of BB, leaving the result in WORK. We're called to generate
544 conflicts when FOR_CONFLICT is true, otherwise we're just tracking
545 liveness. */
547 static void
548 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
550 edge e;
551 edge_iterator ei;
552 gimple_stmt_iterator gsi;
553 walk_stmt_load_store_addr_fn visit;
555 bitmap_clear (work);
556 FOR_EACH_EDGE (e, ei, bb->preds)
557 bitmap_ior_into (work, (bitmap)e->src->aux);
559 visit = visit_op;
561 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
563 gimple *stmt = gsi_stmt (gsi);
564 walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
566 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
568 gimple *stmt = gsi_stmt (gsi);
570 if (gimple_clobber_p (stmt))
572 tree lhs = gimple_assign_lhs (stmt);
573 size_t *v;
574 /* Nested function lowering might introduce LHSs
575 that are COMPONENT_REFs. */
576 if (TREE_CODE (lhs) != VAR_DECL)
577 continue;
578 if (DECL_RTL_IF_SET (lhs) == pc_rtx
579 && (v = decl_to_stack_part->get (lhs)))
580 bitmap_clear_bit (work, *v);
582 else if (!is_gimple_debug (stmt))
584 if (for_conflict
585 && visit == visit_op)
587 /* If this is the first real instruction in this BB we need
588 to add conflicts for everything live at this point now.
589 Unlike classical liveness for named objects we can't
590 rely on seeing a def/use of the names we're interested in.
591 There might merely be indirect loads/stores. We'd not add any
592 conflicts for such partitions. */
593 bitmap_iterator bi;
594 unsigned i;
595 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
597 struct stack_var *a = &stack_vars[i];
598 if (!a->conflicts)
599 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
600 bitmap_ior_into (a->conflicts, work);
602 visit = visit_conflict;
604 walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
609 /* Generate stack partition conflicts between all partitions that are
610 simultaneously live. */
612 static void
613 add_scope_conflicts (void)
615 basic_block bb;
616 bool changed;
617 bitmap work = BITMAP_ALLOC (NULL);
618 int *rpo;
619 int n_bbs;
621 /* We approximate the live range of a stack variable by taking the first
622 mention of its name as starting point(s), and by the end-of-scope
623 death clobber added by gimplify as ending point(s) of the range.
624 This overapproximates in the case we for instance moved an address-taken
625 operation upward, without also moving a dereference to it upwards.
626 But it's conservatively correct as a variable never can hold values
627 before its name is mentioned at least once.
629 We then do a mostly classical bitmap liveness algorithm. */
631 FOR_ALL_BB_FN (bb, cfun)
632 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
634 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
635 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
637 changed = true;
638 while (changed)
640 int i;
641 changed = false;
642 for (i = 0; i < n_bbs; i++)
644 bitmap active;
645 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
646 active = (bitmap)bb->aux;
647 add_scope_conflicts_1 (bb, work, false);
648 if (bitmap_ior_into (active, work))
649 changed = true;
653 FOR_EACH_BB_FN (bb, cfun)
654 add_scope_conflicts_1 (bb, work, true);
656 free (rpo);
657 BITMAP_FREE (work);
658 FOR_ALL_BB_FN (bb, cfun)
659 BITMAP_FREE (bb->aux);
662 /* A subroutine of partition_stack_vars. A comparison function for qsort,
663 sorting an array of indices by the properties of the object. */
665 static int
666 stack_var_cmp (const void *a, const void *b)
668 size_t ia = *(const size_t *)a;
669 size_t ib = *(const size_t *)b;
670 unsigned int aligna = stack_vars[ia].alignb;
671 unsigned int alignb = stack_vars[ib].alignb;
672 HOST_WIDE_INT sizea = stack_vars[ia].size;
673 HOST_WIDE_INT sizeb = stack_vars[ib].size;
674 tree decla = stack_vars[ia].decl;
675 tree declb = stack_vars[ib].decl;
676 bool largea, largeb;
677 unsigned int uida, uidb;
679 /* Primary compare on "large" alignment. Large comes first. */
680 largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
681 largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
682 if (largea != largeb)
683 return (int)largeb - (int)largea;
685 /* Secondary compare on size, decreasing */
686 if (sizea > sizeb)
687 return -1;
688 if (sizea < sizeb)
689 return 1;
691 /* Tertiary compare on true alignment, decreasing. */
692 if (aligna < alignb)
693 return -1;
694 if (aligna > alignb)
695 return 1;
697 /* Final compare on ID for sort stability, increasing.
698 Two SSA names are compared by their version, SSA names come before
699 non-SSA names, and two normal decls are compared by their DECL_UID. */
700 if (TREE_CODE (decla) == SSA_NAME)
702 if (TREE_CODE (declb) == SSA_NAME)
703 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
704 else
705 return -1;
707 else if (TREE_CODE (declb) == SSA_NAME)
708 return 1;
709 else
710 uida = DECL_UID (decla), uidb = DECL_UID (declb);
711 if (uida < uidb)
712 return 1;
713 if (uida > uidb)
714 return -1;
715 return 0;
718 struct part_traits : unbounded_int_hashmap_traits <size_t, bitmap> {};
719 typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
721 /* If the points-to solution *PI points to variables that are in a partition
722 together with other variables add all partition members to the pointed-to
723 variables bitmap. */
725 static void
726 add_partitioned_vars_to_ptset (struct pt_solution *pt,
727 part_hashmap *decls_to_partitions,
728 hash_set<bitmap> *visited, bitmap temp)
730 bitmap_iterator bi;
731 unsigned i;
732 bitmap *part;
734 if (pt->anything
735 || pt->vars == NULL
736 /* The pointed-to vars bitmap is shared, it is enough to
737 visit it once. */
738 || visited->add (pt->vars))
739 return;
741 bitmap_clear (temp);
743 /* By using a temporary bitmap to store all members of the partitions
744 we have to add we make sure to visit each of the partitions only
745 once. */
746 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
747 if ((!temp
748 || !bitmap_bit_p (temp, i))
749 && (part = decls_to_partitions->get (i)))
750 bitmap_ior_into (temp, *part);
751 if (!bitmap_empty_p (temp))
752 bitmap_ior_into (pt->vars, temp);
755 /* Update points-to sets based on partition info, so we can use them on RTL.
756 The bitmaps representing stack partitions will be saved until expand,
757 where partitioned decls used as bases in memory expressions will be
758 rewritten. */
760 static void
761 update_alias_info_with_stack_vars (void)
763 part_hashmap *decls_to_partitions = NULL;
764 size_t i, j;
765 tree var = NULL_TREE;
767 for (i = 0; i < stack_vars_num; i++)
769 bitmap part = NULL;
770 tree name;
771 struct ptr_info_def *pi;
773 /* Not interested in partitions with single variable. */
774 if (stack_vars[i].representative != i
775 || stack_vars[i].next == EOC)
776 continue;
778 if (!decls_to_partitions)
780 decls_to_partitions = new part_hashmap;
781 cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
784 /* Create an SSA_NAME that points to the partition for use
785 as base during alias-oracle queries on RTL for bases that
786 have been partitioned. */
787 if (var == NULL_TREE)
788 var = create_tmp_var (ptr_type_node);
789 name = make_ssa_name (var);
791 /* Create bitmaps representing partitions. They will be used for
792 points-to sets later, so use GGC alloc. */
793 part = BITMAP_GGC_ALLOC ();
794 for (j = i; j != EOC; j = stack_vars[j].next)
796 tree decl = stack_vars[j].decl;
797 unsigned int uid = DECL_PT_UID (decl);
798 bitmap_set_bit (part, uid);
799 decls_to_partitions->put (uid, part);
800 cfun->gimple_df->decls_to_pointers->put (decl, name);
801 if (TREE_ADDRESSABLE (decl))
802 TREE_ADDRESSABLE (name) = 1;
805 /* Make the SSA name point to all partition members. */
806 pi = get_ptr_info (name);
807 pt_solution_set (&pi->pt, part, false);
810 /* Make all points-to sets that contain one member of a partition
811 contain all members of the partition. */
812 if (decls_to_partitions)
814 unsigned i;
815 hash_set<bitmap> visited;
816 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
818 for (i = 1; i < num_ssa_names; i++)
820 tree name = ssa_name (i);
821 struct ptr_info_def *pi;
823 if (name
824 && POINTER_TYPE_P (TREE_TYPE (name))
825 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
826 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
827 &visited, temp);
830 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
831 decls_to_partitions, &visited, temp);
833 delete decls_to_partitions;
834 BITMAP_FREE (temp);
838 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
839 partitioning algorithm. Partitions A and B are known to be non-conflicting.
840 Merge them into a single partition A. */
842 static void
843 union_stack_vars (size_t a, size_t b)
845 struct stack_var *vb = &stack_vars[b];
846 bitmap_iterator bi;
847 unsigned u;
849 gcc_assert (stack_vars[b].next == EOC);
850 /* Add B to A's partition. */
851 stack_vars[b].next = stack_vars[a].next;
852 stack_vars[b].representative = a;
853 stack_vars[a].next = b;
855 /* Update the required alignment of partition A to account for B. */
856 if (stack_vars[a].alignb < stack_vars[b].alignb)
857 stack_vars[a].alignb = stack_vars[b].alignb;
859 /* Update the interference graph and merge the conflicts. */
860 if (vb->conflicts)
862 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
863 add_stack_var_conflict (a, stack_vars[u].representative);
864 BITMAP_FREE (vb->conflicts);
868 /* A subroutine of expand_used_vars. Binpack the variables into
869 partitions constrained by the interference graph. The overall
870 algorithm used is as follows:
872 Sort the objects by size in descending order.
873 For each object A {
874 S = size(A)
875 O = 0
876 loop {
877 Look for the largest non-conflicting object B with size <= S.
878 UNION (A, B)
883 static void
884 partition_stack_vars (void)
886 size_t si, sj, n = stack_vars_num;
888 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
889 for (si = 0; si < n; ++si)
890 stack_vars_sorted[si] = si;
892 if (n == 1)
893 return;
895 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
897 for (si = 0; si < n; ++si)
899 size_t i = stack_vars_sorted[si];
900 unsigned int ialign = stack_vars[i].alignb;
901 HOST_WIDE_INT isize = stack_vars[i].size;
903 /* Ignore objects that aren't partition representatives. If we
904 see a var that is not a partition representative, it must
905 have been merged earlier. */
906 if (stack_vars[i].representative != i)
907 continue;
909 for (sj = si + 1; sj < n; ++sj)
911 size_t j = stack_vars_sorted[sj];
912 unsigned int jalign = stack_vars[j].alignb;
913 HOST_WIDE_INT jsize = stack_vars[j].size;
915 /* Ignore objects that aren't partition representatives. */
916 if (stack_vars[j].representative != j)
917 continue;
919 /* Do not mix objects of "small" (supported) alignment
920 and "large" (unsupported) alignment. */
921 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
922 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
923 break;
925 /* For Address Sanitizer do not mix objects with different
926 sizes, as the shorter vars wouldn't be adequately protected.
927 Don't do that for "large" (unsupported) alignment objects,
928 those aren't protected anyway. */
929 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && isize != jsize
930 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
931 break;
933 /* Ignore conflicting objects. */
934 if (stack_var_conflict_p (i, j))
935 continue;
937 /* UNION the objects, placing J at OFFSET. */
938 union_stack_vars (i, j);
942 update_alias_info_with_stack_vars ();
945 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
947 static void
948 dump_stack_var_partition (void)
950 size_t si, i, j, n = stack_vars_num;
952 for (si = 0; si < n; ++si)
954 i = stack_vars_sorted[si];
956 /* Skip variables that aren't partition representatives, for now. */
957 if (stack_vars[i].representative != i)
958 continue;
960 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
961 " align %u\n", (unsigned long) i, stack_vars[i].size,
962 stack_vars[i].alignb);
964 for (j = i; j != EOC; j = stack_vars[j].next)
966 fputc ('\t', dump_file);
967 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
969 fputc ('\n', dump_file);
973 /* Assign rtl to DECL at BASE + OFFSET. */
975 static void
976 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
977 HOST_WIDE_INT offset)
979 unsigned align;
980 rtx x;
982 /* If this fails, we've overflowed the stack frame. Error nicely? */
983 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
985 x = plus_constant (Pmode, base, offset);
986 x = gen_rtx_MEM (TREE_CODE (decl) == SSA_NAME
987 ? TYPE_MODE (TREE_TYPE (decl))
988 : DECL_MODE (SSAVAR (decl)), x);
990 if (TREE_CODE (decl) != SSA_NAME)
992 /* Set alignment we actually gave this decl if it isn't an SSA name.
993 If it is we generate stack slots only accidentally so it isn't as
994 important, we'll simply use the alignment that is already set. */
995 if (base == virtual_stack_vars_rtx)
996 offset -= frame_phase;
997 align = offset & -offset;
998 align *= BITS_PER_UNIT;
999 if (align == 0 || align > base_align)
1000 align = base_align;
1002 /* One would think that we could assert that we're not decreasing
1003 alignment here, but (at least) the i386 port does exactly this
1004 via the MINIMUM_ALIGNMENT hook. */
1006 DECL_ALIGN (decl) = align;
1007 DECL_USER_ALIGN (decl) = 0;
1010 set_rtl (decl, x);
1013 struct stack_vars_data
1015 /* Vector of offset pairs, always end of some padding followed
1016 by start of the padding that needs Address Sanitizer protection.
1017 The vector is in reversed, highest offset pairs come first. */
1018 vec<HOST_WIDE_INT> asan_vec;
1020 /* Vector of partition representative decls in between the paddings. */
1021 vec<tree> asan_decl_vec;
1023 /* Base pseudo register for Address Sanitizer protected automatic vars. */
1024 rtx asan_base;
1026 /* Alignment needed for the Address Sanitizer protected automatic vars. */
1027 unsigned int asan_alignb;
1030 /* A subroutine of expand_used_vars. Give each partition representative
1031 a unique location within the stack frame. Update each partition member
1032 with that location. */
1034 static void
1035 expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
1037 size_t si, i, j, n = stack_vars_num;
1038 HOST_WIDE_INT large_size = 0, large_alloc = 0;
1039 rtx large_base = NULL;
1040 unsigned large_align = 0;
1041 tree decl;
1043 /* Determine if there are any variables requiring "large" alignment.
1044 Since these are dynamically allocated, we only process these if
1045 no predicate involved. */
1046 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
1047 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
1049 /* Find the total size of these variables. */
1050 for (si = 0; si < n; ++si)
1052 unsigned alignb;
1054 i = stack_vars_sorted[si];
1055 alignb = stack_vars[i].alignb;
1057 /* All "large" alignment decls come before all "small" alignment
1058 decls, but "large" alignment decls are not sorted based on
1059 their alignment. Increase large_align to track the largest
1060 required alignment. */
1061 if ((alignb * BITS_PER_UNIT) > large_align)
1062 large_align = alignb * BITS_PER_UNIT;
1064 /* Stop when we get to the first decl with "small" alignment. */
1065 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1066 break;
1068 /* Skip variables that aren't partition representatives. */
1069 if (stack_vars[i].representative != i)
1070 continue;
1072 /* Skip variables that have already had rtl assigned. See also
1073 add_stack_var where we perpetrate this pc_rtx hack. */
1074 decl = stack_vars[i].decl;
1075 if (TREE_CODE (decl) == SSA_NAME
1076 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)] != NULL_RTX
1077 : DECL_RTL (decl) != pc_rtx)
1078 continue;
1080 large_size += alignb - 1;
1081 large_size &= -(HOST_WIDE_INT)alignb;
1082 large_size += stack_vars[i].size;
1085 /* If there were any, allocate space. */
1086 if (large_size > 0)
1087 large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
1088 large_align, true);
1091 for (si = 0; si < n; ++si)
1093 rtx base;
1094 unsigned base_align, alignb;
1095 HOST_WIDE_INT offset;
1097 i = stack_vars_sorted[si];
1099 /* Skip variables that aren't partition representatives, for now. */
1100 if (stack_vars[i].representative != i)
1101 continue;
1103 /* Skip variables that have already had rtl assigned. See also
1104 add_stack_var where we perpetrate this pc_rtx hack. */
1105 decl = stack_vars[i].decl;
1106 if (TREE_CODE (decl) == SSA_NAME
1107 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)] != NULL_RTX
1108 : DECL_RTL (decl) != pc_rtx)
1109 continue;
1111 /* Check the predicate to see whether this variable should be
1112 allocated in this pass. */
1113 if (pred && !pred (i))
1114 continue;
1116 alignb = stack_vars[i].alignb;
1117 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1119 base = virtual_stack_vars_rtx;
1120 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && pred)
1122 HOST_WIDE_INT prev_offset
1123 = align_base (frame_offset,
1124 MAX (alignb, ASAN_RED_ZONE_SIZE),
1125 FRAME_GROWS_DOWNWARD);
1126 tree repr_decl = NULL_TREE;
1127 offset
1128 = alloc_stack_frame_space (stack_vars[i].size
1129 + ASAN_RED_ZONE_SIZE,
1130 MAX (alignb, ASAN_RED_ZONE_SIZE));
1132 data->asan_vec.safe_push (prev_offset);
1133 data->asan_vec.safe_push (offset + stack_vars[i].size);
1134 /* Find best representative of the partition.
1135 Prefer those with DECL_NAME, even better
1136 satisfying asan_protect_stack_decl predicate. */
1137 for (j = i; j != EOC; j = stack_vars[j].next)
1138 if (asan_protect_stack_decl (stack_vars[j].decl)
1139 && DECL_NAME (stack_vars[j].decl))
1141 repr_decl = stack_vars[j].decl;
1142 break;
1144 else if (repr_decl == NULL_TREE
1145 && DECL_P (stack_vars[j].decl)
1146 && DECL_NAME (stack_vars[j].decl))
1147 repr_decl = stack_vars[j].decl;
1148 if (repr_decl == NULL_TREE)
1149 repr_decl = stack_vars[i].decl;
1150 data->asan_decl_vec.safe_push (repr_decl);
1151 data->asan_alignb = MAX (data->asan_alignb, alignb);
1152 if (data->asan_base == NULL)
1153 data->asan_base = gen_reg_rtx (Pmode);
1154 base = data->asan_base;
1156 if (!STRICT_ALIGNMENT)
1157 base_align = crtl->max_used_stack_slot_alignment;
1158 else
1159 base_align = MAX (crtl->max_used_stack_slot_alignment,
1160 GET_MODE_ALIGNMENT (SImode)
1161 << ASAN_SHADOW_SHIFT);
1163 else
1165 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1166 base_align = crtl->max_used_stack_slot_alignment;
1169 else
1171 /* Large alignment is only processed in the last pass. */
1172 if (pred)
1173 continue;
1174 gcc_assert (large_base != NULL);
1176 large_alloc += alignb - 1;
1177 large_alloc &= -(HOST_WIDE_INT)alignb;
1178 offset = large_alloc;
1179 large_alloc += stack_vars[i].size;
1181 base = large_base;
1182 base_align = large_align;
1185 /* Create rtl for each variable based on their location within the
1186 partition. */
1187 for (j = i; j != EOC; j = stack_vars[j].next)
1189 expand_one_stack_var_at (stack_vars[j].decl,
1190 base, base_align,
1191 offset);
1195 gcc_assert (large_alloc == large_size);
1198 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1199 static HOST_WIDE_INT
1200 account_stack_vars (void)
1202 size_t si, j, i, n = stack_vars_num;
1203 HOST_WIDE_INT size = 0;
1205 for (si = 0; si < n; ++si)
1207 i = stack_vars_sorted[si];
1209 /* Skip variables that aren't partition representatives, for now. */
1210 if (stack_vars[i].representative != i)
1211 continue;
1213 size += stack_vars[i].size;
1214 for (j = i; j != EOC; j = stack_vars[j].next)
1215 set_rtl (stack_vars[j].decl, NULL);
1217 return size;
1220 /* Record the RTL assignment X for the default def of PARM. */
1222 extern void
1223 set_parm_rtl (tree parm, rtx x)
1225 gcc_assert (TREE_CODE (parm) == PARM_DECL
1226 || TREE_CODE (parm) == RESULT_DECL);
1228 if (x && !MEM_P (x))
1230 unsigned int align = MINIMUM_ALIGNMENT (TREE_TYPE (parm),
1231 TYPE_MODE (TREE_TYPE (parm)),
1232 TYPE_ALIGN (TREE_TYPE (parm)));
1234 /* If the variable alignment is very large we'll dynamicaly
1235 allocate it, which means that in-frame portion is just a
1236 pointer. ??? We've got a pseudo for sure here, do we
1237 actually dynamically allocate its spilling area if needed?
1238 ??? Isn't it a problem when POINTER_SIZE also exceeds
1239 MAX_SUPPORTED_STACK_ALIGNMENT, as on cris and lm32? */
1240 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1241 align = POINTER_SIZE;
1243 record_alignment_for_reg_var (align);
1246 if (!is_gimple_reg (parm))
1247 return set_rtl (parm, x);
1249 tree ssa = ssa_default_def (cfun, parm);
1250 if (!ssa)
1251 return set_rtl (parm, x);
1253 int part = var_to_partition (SA.map, ssa);
1254 gcc_assert (part != NO_PARTITION);
1256 bool changed = bitmap_bit_p (SA.partitions_for_parm_default_defs, part);
1257 gcc_assert (changed);
1259 set_rtl (ssa, x);
1260 gcc_assert (DECL_RTL (parm) == x);
1263 /* A subroutine of expand_one_var. Called to immediately assign rtl
1264 to a variable to be allocated in the stack frame. */
1266 static void
1267 expand_one_stack_var_1 (tree var)
1269 HOST_WIDE_INT size, offset;
1270 unsigned byte_align;
1272 if (TREE_CODE (var) == SSA_NAME)
1274 tree type = TREE_TYPE (var);
1275 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1276 byte_align = TYPE_ALIGN_UNIT (type);
1278 else
1280 size = tree_to_uhwi (DECL_SIZE_UNIT (var));
1281 byte_align = align_local_variable (var);
1284 /* We handle highly aligned variables in expand_stack_vars. */
1285 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1287 offset = alloc_stack_frame_space (size, byte_align);
1289 expand_one_stack_var_at (var, virtual_stack_vars_rtx,
1290 crtl->max_used_stack_slot_alignment, offset);
1293 /* Wrapper for expand_one_stack_var_1 that checks SSA_NAMEs are
1294 already assigned some MEM. */
1296 static void
1297 expand_one_stack_var (tree var)
1299 if (TREE_CODE (var) == SSA_NAME)
1301 int part = var_to_partition (SA.map, var);
1302 if (part != NO_PARTITION)
1304 rtx x = SA.partition_to_pseudo[part];
1305 gcc_assert (x);
1306 gcc_assert (MEM_P (x));
1307 return;
1311 return expand_one_stack_var_1 (var);
1314 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1315 that will reside in a hard register. */
1317 static void
1318 expand_one_hard_reg_var (tree var)
1320 rest_of_decl_compilation (var, 0, 0);
1323 /* Record the alignment requirements of some variable assigned to a
1324 pseudo. */
1326 static void
1327 record_alignment_for_reg_var (unsigned int align)
1329 if (SUPPORTS_STACK_ALIGNMENT
1330 && crtl->stack_alignment_estimated < align)
1332 /* stack_alignment_estimated shouldn't change after stack
1333 realign decision made */
1334 gcc_assert (!crtl->stack_realign_processed);
1335 crtl->stack_alignment_estimated = align;
1338 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1339 So here we only make sure stack_alignment_needed >= align. */
1340 if (crtl->stack_alignment_needed < align)
1341 crtl->stack_alignment_needed = align;
1342 if (crtl->max_used_stack_slot_alignment < align)
1343 crtl->max_used_stack_slot_alignment = align;
1346 /* Create RTL for an SSA partition. */
1348 static void
1349 expand_one_ssa_partition (tree var)
1351 int part = var_to_partition (SA.map, var);
1352 gcc_assert (part != NO_PARTITION);
1354 if (SA.partition_to_pseudo[part])
1355 return;
1357 unsigned int align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1358 TYPE_MODE (TREE_TYPE (var)),
1359 TYPE_ALIGN (TREE_TYPE (var)));
1361 /* If the variable alignment is very large we'll dynamicaly allocate
1362 it, which means that in-frame portion is just a pointer. */
1363 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1364 align = POINTER_SIZE;
1366 record_alignment_for_reg_var (align);
1368 if (!use_register_for_decl (var))
1370 if (defer_stack_allocation (var, true))
1371 add_stack_var (var);
1372 else
1373 expand_one_stack_var_1 (var);
1374 return;
1377 machine_mode reg_mode = promote_ssa_mode (var, NULL);
1379 rtx x = gen_reg_rtx (reg_mode);
1381 set_rtl (var, x);
1384 /* Record the association between the RTL generated for partition PART
1385 and the underlying variable of the SSA_NAME VAR. */
1387 static void
1388 adjust_one_expanded_partition_var (tree var)
1390 if (!var)
1391 return;
1393 tree decl = SSA_NAME_VAR (var);
1395 int part = var_to_partition (SA.map, var);
1396 if (part == NO_PARTITION)
1397 return;
1399 rtx x = SA.partition_to_pseudo[part];
1401 gcc_assert (x);
1403 set_rtl (var, x);
1405 if (!REG_P (x))
1406 return;
1408 /* Note if the object is a user variable. */
1409 if (decl && !DECL_ARTIFICIAL (decl))
1410 mark_user_reg (x);
1412 if (POINTER_TYPE_P (decl ? TREE_TYPE (decl) : TREE_TYPE (var)))
1413 mark_reg_pointer (x, get_pointer_alignment (var));
1416 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1417 that will reside in a pseudo register. */
1419 static void
1420 expand_one_register_var (tree var)
1422 if (TREE_CODE (var) == SSA_NAME)
1424 int part = var_to_partition (SA.map, var);
1425 if (part != NO_PARTITION)
1427 rtx x = SA.partition_to_pseudo[part];
1428 gcc_assert (x);
1429 gcc_assert (REG_P (x));
1430 return;
1432 gcc_unreachable ();
1435 tree decl = var;
1436 tree type = TREE_TYPE (decl);
1437 machine_mode reg_mode = promote_decl_mode (decl, NULL);
1438 rtx x = gen_reg_rtx (reg_mode);
1440 set_rtl (var, x);
1442 /* Note if the object is a user variable. */
1443 if (!DECL_ARTIFICIAL (decl))
1444 mark_user_reg (x);
1446 if (POINTER_TYPE_P (type))
1447 mark_reg_pointer (x, get_pointer_alignment (var));
1450 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1451 has some associated error, e.g. its type is error-mark. We just need
1452 to pick something that won't crash the rest of the compiler. */
1454 static void
1455 expand_one_error_var (tree var)
1457 machine_mode mode = DECL_MODE (var);
1458 rtx x;
1460 if (mode == BLKmode)
1461 x = gen_rtx_MEM (BLKmode, const0_rtx);
1462 else if (mode == VOIDmode)
1463 x = const0_rtx;
1464 else
1465 x = gen_reg_rtx (mode);
1467 SET_DECL_RTL (var, x);
1470 /* A subroutine of expand_one_var. VAR is a variable that will be
1471 allocated to the local stack frame. Return true if we wish to
1472 add VAR to STACK_VARS so that it will be coalesced with other
1473 variables. Return false to allocate VAR immediately.
1475 This function is used to reduce the number of variables considered
1476 for coalescing, which reduces the size of the quadratic problem. */
1478 static bool
1479 defer_stack_allocation (tree var, bool toplevel)
1481 tree size_unit = TREE_CODE (var) == SSA_NAME
1482 ? TYPE_SIZE_UNIT (TREE_TYPE (var))
1483 : DECL_SIZE_UNIT (var);
1485 /* Whether the variable is small enough for immediate allocation not to be
1486 a problem with regard to the frame size. */
1487 bool smallish
1488 = ((HOST_WIDE_INT) tree_to_uhwi (size_unit)
1489 < PARAM_VALUE (PARAM_MIN_SIZE_FOR_STACK_SHARING));
1491 /* If stack protection is enabled, *all* stack variables must be deferred,
1492 so that we can re-order the strings to the top of the frame.
1493 Similarly for Address Sanitizer. */
1494 if (flag_stack_protect || ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK))
1495 return true;
1497 unsigned int align = TREE_CODE (var) == SSA_NAME
1498 ? TYPE_ALIGN (TREE_TYPE (var))
1499 : DECL_ALIGN (var);
1501 /* We handle "large" alignment via dynamic allocation. We want to handle
1502 this extra complication in only one place, so defer them. */
1503 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1504 return true;
1506 bool ignored = TREE_CODE (var) == SSA_NAME
1507 ? !SSAVAR (var) || DECL_IGNORED_P (SSA_NAME_VAR (var))
1508 : DECL_IGNORED_P (var);
1510 /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1511 might be detached from their block and appear at toplevel when we reach
1512 here. We want to coalesce them with variables from other blocks when
1513 the immediate contribution to the frame size would be noticeable. */
1514 if (toplevel && optimize > 0 && ignored && !smallish)
1515 return true;
1517 /* Variables declared in the outermost scope automatically conflict
1518 with every other variable. The only reason to want to defer them
1519 at all is that, after sorting, we can more efficiently pack
1520 small variables in the stack frame. Continue to defer at -O2. */
1521 if (toplevel && optimize < 2)
1522 return false;
1524 /* Without optimization, *most* variables are allocated from the
1525 stack, which makes the quadratic problem large exactly when we
1526 want compilation to proceed as quickly as possible. On the
1527 other hand, we don't want the function's stack frame size to
1528 get completely out of hand. So we avoid adding scalars and
1529 "small" aggregates to the list at all. */
1530 if (optimize == 0 && smallish)
1531 return false;
1533 return true;
1536 /* A subroutine of expand_used_vars. Expand one variable according to
1537 its flavor. Variables to be placed on the stack are not actually
1538 expanded yet, merely recorded.
1539 When REALLY_EXPAND is false, only add stack values to be allocated.
1540 Return stack usage this variable is supposed to take.
1543 static HOST_WIDE_INT
1544 expand_one_var (tree var, bool toplevel, bool really_expand)
1546 unsigned int align = BITS_PER_UNIT;
1547 tree origvar = var;
1549 var = SSAVAR (var);
1551 if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
1553 /* Because we don't know if VAR will be in register or on stack,
1554 we conservatively assume it will be on stack even if VAR is
1555 eventually put into register after RA pass. For non-automatic
1556 variables, which won't be on stack, we collect alignment of
1557 type and ignore user specified alignment. Similarly for
1558 SSA_NAMEs for which use_register_for_decl returns true. */
1559 if (TREE_STATIC (var)
1560 || DECL_EXTERNAL (var)
1561 || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1562 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1563 TYPE_MODE (TREE_TYPE (var)),
1564 TYPE_ALIGN (TREE_TYPE (var)));
1565 else if (DECL_HAS_VALUE_EXPR_P (var)
1566 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1567 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1568 or variables which were assigned a stack slot already by
1569 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1570 changed from the offset chosen to it. */
1571 align = crtl->stack_alignment_estimated;
1572 else
1573 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1575 /* If the variable alignment is very large we'll dynamicaly allocate
1576 it, which means that in-frame portion is just a pointer. */
1577 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1578 align = POINTER_SIZE;
1581 record_alignment_for_reg_var (align);
1583 if (TREE_CODE (origvar) == SSA_NAME)
1585 gcc_assert (TREE_CODE (var) != VAR_DECL
1586 || (!DECL_EXTERNAL (var)
1587 && !DECL_HAS_VALUE_EXPR_P (var)
1588 && !TREE_STATIC (var)
1589 && TREE_TYPE (var) != error_mark_node
1590 && !DECL_HARD_REGISTER (var)
1591 && really_expand));
1593 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1595 else if (DECL_EXTERNAL (var))
1597 else if (DECL_HAS_VALUE_EXPR_P (var))
1599 else if (TREE_STATIC (var))
1601 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1603 else if (TREE_TYPE (var) == error_mark_node)
1605 if (really_expand)
1606 expand_one_error_var (var);
1608 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1610 if (really_expand)
1612 expand_one_hard_reg_var (var);
1613 if (!DECL_HARD_REGISTER (var))
1614 /* Invalid register specification. */
1615 expand_one_error_var (var);
1618 else if (use_register_for_decl (var))
1620 if (really_expand)
1621 expand_one_register_var (origvar);
1623 else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
1625 /* Reject variables which cover more than half of the address-space. */
1626 if (really_expand)
1628 error ("size of variable %q+D is too large", var);
1629 expand_one_error_var (var);
1632 else if (defer_stack_allocation (var, toplevel))
1633 add_stack_var (origvar);
1634 else
1636 if (really_expand)
1638 if (lookup_attribute ("naked",
1639 DECL_ATTRIBUTES (current_function_decl)))
1640 error ("cannot allocate stack for variable %q+D, naked function.",
1641 var);
1643 expand_one_stack_var (origvar);
1647 return tree_to_uhwi (DECL_SIZE_UNIT (var));
1649 return 0;
1652 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1653 expanding variables. Those variables that can be put into registers
1654 are allocated pseudos; those that can't are put on the stack.
1656 TOPLEVEL is true if this is the outermost BLOCK. */
1658 static void
1659 expand_used_vars_for_block (tree block, bool toplevel)
1661 tree t;
1663 /* Expand all variables at this level. */
1664 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1665 if (TREE_USED (t)
1666 && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1667 || !DECL_NONSHAREABLE (t)))
1668 expand_one_var (t, toplevel, true);
1670 /* Expand all variables at containing levels. */
1671 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1672 expand_used_vars_for_block (t, false);
1675 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1676 and clear TREE_USED on all local variables. */
1678 static void
1679 clear_tree_used (tree block)
1681 tree t;
1683 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1684 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1685 if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1686 || !DECL_NONSHAREABLE (t))
1687 TREE_USED (t) = 0;
1689 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1690 clear_tree_used (t);
1693 enum {
1694 SPCT_FLAG_DEFAULT = 1,
1695 SPCT_FLAG_ALL = 2,
1696 SPCT_FLAG_STRONG = 3,
1697 SPCT_FLAG_EXPLICIT = 4
1700 /* Examine TYPE and determine a bit mask of the following features. */
1702 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1703 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1704 #define SPCT_HAS_ARRAY 4
1705 #define SPCT_HAS_AGGREGATE 8
1707 static unsigned int
1708 stack_protect_classify_type (tree type)
1710 unsigned int ret = 0;
1711 tree t;
1713 switch (TREE_CODE (type))
1715 case ARRAY_TYPE:
1716 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1717 if (t == char_type_node
1718 || t == signed_char_type_node
1719 || t == unsigned_char_type_node)
1721 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1722 unsigned HOST_WIDE_INT len;
1724 if (!TYPE_SIZE_UNIT (type)
1725 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1726 len = max;
1727 else
1728 len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1730 if (len < max)
1731 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1732 else
1733 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1735 else
1736 ret = SPCT_HAS_ARRAY;
1737 break;
1739 case UNION_TYPE:
1740 case QUAL_UNION_TYPE:
1741 case RECORD_TYPE:
1742 ret = SPCT_HAS_AGGREGATE;
1743 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1744 if (TREE_CODE (t) == FIELD_DECL)
1745 ret |= stack_protect_classify_type (TREE_TYPE (t));
1746 break;
1748 default:
1749 break;
1752 return ret;
1755 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1756 part of the local stack frame. Remember if we ever return nonzero for
1757 any variable in this function. The return value is the phase number in
1758 which the variable should be allocated. */
1760 static int
1761 stack_protect_decl_phase (tree decl)
1763 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1764 int ret = 0;
1766 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1767 has_short_buffer = true;
1769 if (flag_stack_protect == SPCT_FLAG_ALL
1770 || flag_stack_protect == SPCT_FLAG_STRONG
1771 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1772 && lookup_attribute ("stack_protect",
1773 DECL_ATTRIBUTES (current_function_decl))))
1775 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1776 && !(bits & SPCT_HAS_AGGREGATE))
1777 ret = 1;
1778 else if (bits & SPCT_HAS_ARRAY)
1779 ret = 2;
1781 else
1782 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1784 if (ret)
1785 has_protected_decls = true;
1787 return ret;
1790 /* Two helper routines that check for phase 1 and phase 2. These are used
1791 as callbacks for expand_stack_vars. */
1793 static bool
1794 stack_protect_decl_phase_1 (size_t i)
1796 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
1799 static bool
1800 stack_protect_decl_phase_2 (size_t i)
1802 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
1805 /* And helper function that checks for asan phase (with stack protector
1806 it is phase 3). This is used as callback for expand_stack_vars.
1807 Returns true if any of the vars in the partition need to be protected. */
1809 static bool
1810 asan_decl_phase_3 (size_t i)
1812 while (i != EOC)
1814 if (asan_protect_stack_decl (stack_vars[i].decl))
1815 return true;
1816 i = stack_vars[i].next;
1818 return false;
1821 /* Ensure that variables in different stack protection phases conflict
1822 so that they are not merged and share the same stack slot. */
1824 static void
1825 add_stack_protection_conflicts (void)
1827 size_t i, j, n = stack_vars_num;
1828 unsigned char *phase;
1830 phase = XNEWVEC (unsigned char, n);
1831 for (i = 0; i < n; ++i)
1832 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1834 for (i = 0; i < n; ++i)
1836 unsigned char ph_i = phase[i];
1837 for (j = i + 1; j < n; ++j)
1838 if (ph_i != phase[j])
1839 add_stack_var_conflict (i, j);
1842 XDELETEVEC (phase);
1845 /* Create a decl for the guard at the top of the stack frame. */
1847 static void
1848 create_stack_guard (void)
1850 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1851 VAR_DECL, NULL, ptr_type_node);
1852 TREE_THIS_VOLATILE (guard) = 1;
1853 TREE_USED (guard) = 1;
1854 expand_one_stack_var (guard);
1855 crtl->stack_protect_guard = guard;
1858 /* Prepare for expanding variables. */
1859 static void
1860 init_vars_expansion (void)
1862 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
1863 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
1865 /* A map from decl to stack partition. */
1866 decl_to_stack_part = new hash_map<tree, size_t>;
1868 /* Initialize local stack smashing state. */
1869 has_protected_decls = false;
1870 has_short_buffer = false;
1873 /* Free up stack variable graph data. */
1874 static void
1875 fini_vars_expansion (void)
1877 bitmap_obstack_release (&stack_var_bitmap_obstack);
1878 if (stack_vars)
1879 XDELETEVEC (stack_vars);
1880 if (stack_vars_sorted)
1881 XDELETEVEC (stack_vars_sorted);
1882 stack_vars = NULL;
1883 stack_vars_sorted = NULL;
1884 stack_vars_alloc = stack_vars_num = 0;
1885 delete decl_to_stack_part;
1886 decl_to_stack_part = NULL;
1889 /* Make a fair guess for the size of the stack frame of the function
1890 in NODE. This doesn't have to be exact, the result is only used in
1891 the inline heuristics. So we don't want to run the full stack var
1892 packing algorithm (which is quadratic in the number of stack vars).
1893 Instead, we calculate the total size of all stack vars. This turns
1894 out to be a pretty fair estimate -- packing of stack vars doesn't
1895 happen very often. */
1897 HOST_WIDE_INT
1898 estimated_stack_frame_size (struct cgraph_node *node)
1900 HOST_WIDE_INT size = 0;
1901 size_t i;
1902 tree var;
1903 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1905 push_cfun (fn);
1907 init_vars_expansion ();
1909 FOR_EACH_LOCAL_DECL (fn, i, var)
1910 if (auto_var_in_fn_p (var, fn->decl))
1911 size += expand_one_var (var, true, false);
1913 if (stack_vars_num > 0)
1915 /* Fake sorting the stack vars for account_stack_vars (). */
1916 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1917 for (i = 0; i < stack_vars_num; ++i)
1918 stack_vars_sorted[i] = i;
1919 size += account_stack_vars ();
1922 fini_vars_expansion ();
1923 pop_cfun ();
1924 return size;
1927 /* Helper routine to check if a record or union contains an array field. */
1929 static int
1930 record_or_union_type_has_array_p (const_tree tree_type)
1932 tree fields = TYPE_FIELDS (tree_type);
1933 tree f;
1935 for (f = fields; f; f = DECL_CHAIN (f))
1936 if (TREE_CODE (f) == FIELD_DECL)
1938 tree field_type = TREE_TYPE (f);
1939 if (RECORD_OR_UNION_TYPE_P (field_type)
1940 && record_or_union_type_has_array_p (field_type))
1941 return 1;
1942 if (TREE_CODE (field_type) == ARRAY_TYPE)
1943 return 1;
1945 return 0;
1948 /* Check if the current function has local referenced variables that
1949 have their addresses taken, contain an array, or are arrays. */
1951 static bool
1952 stack_protect_decl_p ()
1954 unsigned i;
1955 tree var;
1957 FOR_EACH_LOCAL_DECL (cfun, i, var)
1958 if (!is_global_var (var))
1960 tree var_type = TREE_TYPE (var);
1961 if (TREE_CODE (var) == VAR_DECL
1962 && (TREE_CODE (var_type) == ARRAY_TYPE
1963 || TREE_ADDRESSABLE (var)
1964 || (RECORD_OR_UNION_TYPE_P (var_type)
1965 && record_or_union_type_has_array_p (var_type))))
1966 return true;
1968 return false;
1971 /* Check if the current function has calls that use a return slot. */
1973 static bool
1974 stack_protect_return_slot_p ()
1976 basic_block bb;
1978 FOR_ALL_BB_FN (bb, cfun)
1979 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1980 !gsi_end_p (gsi); gsi_next (&gsi))
1982 gimple *stmt = gsi_stmt (gsi);
1983 /* This assumes that calls to internal-only functions never
1984 use a return slot. */
1985 if (is_gimple_call (stmt)
1986 && !gimple_call_internal_p (stmt)
1987 && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
1988 gimple_call_fndecl (stmt)))
1989 return true;
1991 return false;
1994 /* Expand all variables used in the function. */
1996 static rtx_insn *
1997 expand_used_vars (void)
1999 tree var, outer_block = DECL_INITIAL (current_function_decl);
2000 vec<tree> maybe_local_decls = vNULL;
2001 rtx_insn *var_end_seq = NULL;
2002 unsigned i;
2003 unsigned len;
2004 bool gen_stack_protect_signal = false;
2006 /* Compute the phase of the stack frame for this function. */
2008 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2009 int off = STARTING_FRAME_OFFSET % align;
2010 frame_phase = off ? align - off : 0;
2013 /* Set TREE_USED on all variables in the local_decls. */
2014 FOR_EACH_LOCAL_DECL (cfun, i, var)
2015 TREE_USED (var) = 1;
2016 /* Clear TREE_USED on all variables associated with a block scope. */
2017 clear_tree_used (DECL_INITIAL (current_function_decl));
2019 init_vars_expansion ();
2021 if (targetm.use_pseudo_pic_reg ())
2022 pic_offset_table_rtx = gen_reg_rtx (Pmode);
2024 for (i = 0; i < SA.map->num_partitions; i++)
2026 if (bitmap_bit_p (SA.partitions_for_parm_default_defs, i))
2027 continue;
2029 tree var = partition_to_var (SA.map, i);
2031 gcc_assert (!virtual_operand_p (var));
2033 expand_one_ssa_partition (var);
2036 if (flag_stack_protect == SPCT_FLAG_STRONG)
2037 gen_stack_protect_signal
2038 = stack_protect_decl_p () || stack_protect_return_slot_p ();
2040 /* At this point all variables on the local_decls with TREE_USED
2041 set are not associated with any block scope. Lay them out. */
2043 len = vec_safe_length (cfun->local_decls);
2044 FOR_EACH_LOCAL_DECL (cfun, i, var)
2046 bool expand_now = false;
2048 /* Expanded above already. */
2049 if (is_gimple_reg (var))
2051 TREE_USED (var) = 0;
2052 goto next;
2054 /* We didn't set a block for static or extern because it's hard
2055 to tell the difference between a global variable (re)declared
2056 in a local scope, and one that's really declared there to
2057 begin with. And it doesn't really matter much, since we're
2058 not giving them stack space. Expand them now. */
2059 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
2060 expand_now = true;
2062 /* Expand variables not associated with any block now. Those created by
2063 the optimizers could be live anywhere in the function. Those that
2064 could possibly have been scoped originally and detached from their
2065 block will have their allocation deferred so we coalesce them with
2066 others when optimization is enabled. */
2067 else if (TREE_USED (var))
2068 expand_now = true;
2070 /* Finally, mark all variables on the list as used. We'll use
2071 this in a moment when we expand those associated with scopes. */
2072 TREE_USED (var) = 1;
2074 if (expand_now)
2075 expand_one_var (var, true, true);
2077 next:
2078 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
2080 rtx rtl = DECL_RTL_IF_SET (var);
2082 /* Keep artificial non-ignored vars in cfun->local_decls
2083 chain until instantiate_decls. */
2084 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2085 add_local_decl (cfun, var);
2086 else if (rtl == NULL_RTX)
2087 /* If rtl isn't set yet, which can happen e.g. with
2088 -fstack-protector, retry before returning from this
2089 function. */
2090 maybe_local_decls.safe_push (var);
2094 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
2096 +-----------------+-----------------+
2097 | ...processed... | ...duplicates...|
2098 +-----------------+-----------------+
2100 +-- LEN points here.
2102 We just want the duplicates, as those are the artificial
2103 non-ignored vars that we want to keep until instantiate_decls.
2104 Move them down and truncate the array. */
2105 if (!vec_safe_is_empty (cfun->local_decls))
2106 cfun->local_decls->block_remove (0, len);
2108 /* At this point, all variables within the block tree with TREE_USED
2109 set are actually used by the optimized function. Lay them out. */
2110 expand_used_vars_for_block (outer_block, true);
2112 if (stack_vars_num > 0)
2114 add_scope_conflicts ();
2116 /* If stack protection is enabled, we don't share space between
2117 vulnerable data and non-vulnerable data. */
2118 if (flag_stack_protect != 0
2119 && (flag_stack_protect != SPCT_FLAG_EXPLICIT
2120 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
2121 && lookup_attribute ("stack_protect",
2122 DECL_ATTRIBUTES (current_function_decl)))))
2123 add_stack_protection_conflicts ();
2125 /* Now that we have collected all stack variables, and have computed a
2126 minimal interference graph, attempt to save some stack space. */
2127 partition_stack_vars ();
2128 if (dump_file)
2129 dump_stack_var_partition ();
2132 switch (flag_stack_protect)
2134 case SPCT_FLAG_ALL:
2135 create_stack_guard ();
2136 break;
2138 case SPCT_FLAG_STRONG:
2139 if (gen_stack_protect_signal
2140 || cfun->calls_alloca || has_protected_decls
2141 || lookup_attribute ("stack_protect",
2142 DECL_ATTRIBUTES (current_function_decl)))
2143 create_stack_guard ();
2144 break;
2146 case SPCT_FLAG_DEFAULT:
2147 if (cfun->calls_alloca || has_protected_decls
2148 || lookup_attribute ("stack_protect",
2149 DECL_ATTRIBUTES (current_function_decl)))
2150 create_stack_guard ();
2151 break;
2153 case SPCT_FLAG_EXPLICIT:
2154 if (lookup_attribute ("stack_protect",
2155 DECL_ATTRIBUTES (current_function_decl)))
2156 create_stack_guard ();
2157 break;
2158 default:
2162 /* Assign rtl to each variable based on these partitions. */
2163 if (stack_vars_num > 0)
2165 struct stack_vars_data data;
2167 data.asan_vec = vNULL;
2168 data.asan_decl_vec = vNULL;
2169 data.asan_base = NULL_RTX;
2170 data.asan_alignb = 0;
2172 /* Reorder decls to be protected by iterating over the variables
2173 array multiple times, and allocating out of each phase in turn. */
2174 /* ??? We could probably integrate this into the qsort we did
2175 earlier, such that we naturally see these variables first,
2176 and thus naturally allocate things in the right order. */
2177 if (has_protected_decls)
2179 /* Phase 1 contains only character arrays. */
2180 expand_stack_vars (stack_protect_decl_phase_1, &data);
2182 /* Phase 2 contains other kinds of arrays. */
2183 if (flag_stack_protect == SPCT_FLAG_ALL
2184 || flag_stack_protect == SPCT_FLAG_STRONG
2185 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
2186 && lookup_attribute ("stack_protect",
2187 DECL_ATTRIBUTES (current_function_decl))))
2188 expand_stack_vars (stack_protect_decl_phase_2, &data);
2191 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK)
2192 /* Phase 3, any partitions that need asan protection
2193 in addition to phase 1 and 2. */
2194 expand_stack_vars (asan_decl_phase_3, &data);
2196 if (!data.asan_vec.is_empty ())
2198 HOST_WIDE_INT prev_offset = frame_offset;
2199 HOST_WIDE_INT offset, sz, redzonesz;
2200 redzonesz = ASAN_RED_ZONE_SIZE;
2201 sz = data.asan_vec[0] - prev_offset;
2202 if (data.asan_alignb > ASAN_RED_ZONE_SIZE
2203 && data.asan_alignb <= 4096
2204 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
2205 redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
2206 & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
2207 offset
2208 = alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE);
2209 data.asan_vec.safe_push (prev_offset);
2210 data.asan_vec.safe_push (offset);
2211 /* Leave space for alignment if STRICT_ALIGNMENT. */
2212 if (STRICT_ALIGNMENT)
2213 alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
2214 << ASAN_SHADOW_SHIFT)
2215 / BITS_PER_UNIT, 1);
2217 var_end_seq
2218 = asan_emit_stack_protection (virtual_stack_vars_rtx,
2219 data.asan_base,
2220 data.asan_alignb,
2221 data.asan_vec.address (),
2222 data.asan_decl_vec.address (),
2223 data.asan_vec.length ());
2226 expand_stack_vars (NULL, &data);
2228 data.asan_vec.release ();
2229 data.asan_decl_vec.release ();
2232 fini_vars_expansion ();
2234 /* If there were any artificial non-ignored vars without rtl
2235 found earlier, see if deferred stack allocation hasn't assigned
2236 rtl to them. */
2237 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
2239 rtx rtl = DECL_RTL_IF_SET (var);
2241 /* Keep artificial non-ignored vars in cfun->local_decls
2242 chain until instantiate_decls. */
2243 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2244 add_local_decl (cfun, var);
2246 maybe_local_decls.release ();
2248 /* If the target requires that FRAME_OFFSET be aligned, do it. */
2249 if (STACK_ALIGNMENT_NEEDED)
2251 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2252 if (!FRAME_GROWS_DOWNWARD)
2253 frame_offset += align - 1;
2254 frame_offset &= -align;
2257 return var_end_seq;
2261 /* If we need to produce a detailed dump, print the tree representation
2262 for STMT to the dump file. SINCE is the last RTX after which the RTL
2263 generated for STMT should have been appended. */
2265 static void
2266 maybe_dump_rtl_for_gimple_stmt (gimple *stmt, rtx_insn *since)
2268 if (dump_file && (dump_flags & TDF_DETAILS))
2270 fprintf (dump_file, "\n;; ");
2271 print_gimple_stmt (dump_file, stmt, 0,
2272 TDF_SLIM | (dump_flags & TDF_LINENO));
2273 fprintf (dump_file, "\n");
2275 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
2279 /* Maps the blocks that do not contain tree labels to rtx labels. */
2281 static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
2283 /* Returns the label_rtx expression for a label starting basic block BB. */
2285 static rtx_code_label *
2286 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
2288 gimple_stmt_iterator gsi;
2289 tree lab;
2291 if (bb->flags & BB_RTL)
2292 return block_label (bb);
2294 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
2295 if (elt)
2296 return *elt;
2298 /* Find the tree label if it is present. */
2300 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2302 glabel *lab_stmt;
2304 lab_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
2305 if (!lab_stmt)
2306 break;
2308 lab = gimple_label_label (lab_stmt);
2309 if (DECL_NONLOCAL (lab))
2310 break;
2312 return jump_target_rtx (lab);
2315 rtx_code_label *l = gen_label_rtx ();
2316 lab_rtx_for_bb->put (bb, l);
2317 return l;
2321 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2322 of a basic block where we just expanded the conditional at the end,
2323 possibly clean up the CFG and instruction sequence. LAST is the
2324 last instruction before the just emitted jump sequence. */
2326 static void
2327 maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2329 /* Special case: when jumpif decides that the condition is
2330 trivial it emits an unconditional jump (and the necessary
2331 barrier). But we still have two edges, the fallthru one is
2332 wrong. purge_dead_edges would clean this up later. Unfortunately
2333 we have to insert insns (and split edges) before
2334 find_many_sub_basic_blocks and hence before purge_dead_edges.
2335 But splitting edges might create new blocks which depend on the
2336 fact that if there are two edges there's no barrier. So the
2337 barrier would get lost and verify_flow_info would ICE. Instead
2338 of auditing all edge splitters to care for the barrier (which
2339 normally isn't there in a cleaned CFG), fix it here. */
2340 if (BARRIER_P (get_last_insn ()))
2342 rtx_insn *insn;
2343 remove_edge (e);
2344 /* Now, we have a single successor block, if we have insns to
2345 insert on the remaining edge we potentially will insert
2346 it at the end of this block (if the dest block isn't feasible)
2347 in order to avoid splitting the edge. This insertion will take
2348 place in front of the last jump. But we might have emitted
2349 multiple jumps (conditional and one unconditional) to the
2350 same destination. Inserting in front of the last one then
2351 is a problem. See PR 40021. We fix this by deleting all
2352 jumps except the last unconditional one. */
2353 insn = PREV_INSN (get_last_insn ());
2354 /* Make sure we have an unconditional jump. Otherwise we're
2355 confused. */
2356 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2357 for (insn = PREV_INSN (insn); insn != last;)
2359 insn = PREV_INSN (insn);
2360 if (JUMP_P (NEXT_INSN (insn)))
2362 if (!any_condjump_p (NEXT_INSN (insn)))
2364 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2365 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2367 delete_insn (NEXT_INSN (insn));
2373 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2374 Returns a new basic block if we've terminated the current basic
2375 block and created a new one. */
2377 static basic_block
2378 expand_gimple_cond (basic_block bb, gcond *stmt)
2380 basic_block new_bb, dest;
2381 edge new_edge;
2382 edge true_edge;
2383 edge false_edge;
2384 rtx_insn *last2, *last;
2385 enum tree_code code;
2386 tree op0, op1;
2388 code = gimple_cond_code (stmt);
2389 op0 = gimple_cond_lhs (stmt);
2390 op1 = gimple_cond_rhs (stmt);
2391 /* We're sometimes presented with such code:
2392 D.123_1 = x < y;
2393 if (D.123_1 != 0)
2395 This would expand to two comparisons which then later might
2396 be cleaned up by combine. But some pattern matchers like if-conversion
2397 work better when there's only one compare, so make up for this
2398 here as special exception if TER would have made the same change. */
2399 if (SA.values
2400 && TREE_CODE (op0) == SSA_NAME
2401 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2402 && TREE_CODE (op1) == INTEGER_CST
2403 && ((gimple_cond_code (stmt) == NE_EXPR
2404 && integer_zerop (op1))
2405 || (gimple_cond_code (stmt) == EQ_EXPR
2406 && integer_onep (op1)))
2407 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2409 gimple *second = SSA_NAME_DEF_STMT (op0);
2410 if (gimple_code (second) == GIMPLE_ASSIGN)
2412 enum tree_code code2 = gimple_assign_rhs_code (second);
2413 if (TREE_CODE_CLASS (code2) == tcc_comparison)
2415 code = code2;
2416 op0 = gimple_assign_rhs1 (second);
2417 op1 = gimple_assign_rhs2 (second);
2419 /* If jumps are cheap and the target does not support conditional
2420 compare, turn some more codes into jumpy sequences. */
2421 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4
2422 && targetm.gen_ccmp_first == NULL)
2424 if ((code2 == BIT_AND_EXPR
2425 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2426 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2427 || code2 == TRUTH_AND_EXPR)
2429 code = TRUTH_ANDIF_EXPR;
2430 op0 = gimple_assign_rhs1 (second);
2431 op1 = gimple_assign_rhs2 (second);
2433 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2435 code = TRUTH_ORIF_EXPR;
2436 op0 = gimple_assign_rhs1 (second);
2437 op1 = gimple_assign_rhs2 (second);
2443 last2 = last = get_last_insn ();
2445 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2446 set_curr_insn_location (gimple_location (stmt));
2448 /* These flags have no purpose in RTL land. */
2449 true_edge->flags &= ~EDGE_TRUE_VALUE;
2450 false_edge->flags &= ~EDGE_FALSE_VALUE;
2452 /* We can either have a pure conditional jump with one fallthru edge or
2453 two-way jump that needs to be decomposed into two basic blocks. */
2454 if (false_edge->dest == bb->next_bb)
2456 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2457 true_edge->probability);
2458 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2459 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2460 set_curr_insn_location (true_edge->goto_locus);
2461 false_edge->flags |= EDGE_FALLTHRU;
2462 maybe_cleanup_end_of_block (false_edge, last);
2463 return NULL;
2465 if (true_edge->dest == bb->next_bb)
2467 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2468 false_edge->probability);
2469 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2470 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2471 set_curr_insn_location (false_edge->goto_locus);
2472 true_edge->flags |= EDGE_FALLTHRU;
2473 maybe_cleanup_end_of_block (true_edge, last);
2474 return NULL;
2477 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2478 true_edge->probability);
2479 last = get_last_insn ();
2480 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2481 set_curr_insn_location (false_edge->goto_locus);
2482 emit_jump (label_rtx_for_bb (false_edge->dest));
2484 BB_END (bb) = last;
2485 if (BARRIER_P (BB_END (bb)))
2486 BB_END (bb) = PREV_INSN (BB_END (bb));
2487 update_bb_for_insn (bb);
2489 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2490 dest = false_edge->dest;
2491 redirect_edge_succ (false_edge, new_bb);
2492 false_edge->flags |= EDGE_FALLTHRU;
2493 new_bb->count = false_edge->count;
2494 new_bb->frequency = EDGE_FREQUENCY (false_edge);
2495 add_bb_to_loop (new_bb, bb->loop_father);
2496 new_edge = make_edge (new_bb, dest, 0);
2497 new_edge->probability = REG_BR_PROB_BASE;
2498 new_edge->count = new_bb->count;
2499 if (BARRIER_P (BB_END (new_bb)))
2500 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2501 update_bb_for_insn (new_bb);
2503 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2505 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2507 set_curr_insn_location (true_edge->goto_locus);
2508 true_edge->goto_locus = curr_insn_location ();
2511 return new_bb;
2514 /* Mark all calls that can have a transaction restart. */
2516 static void
2517 mark_transaction_restart_calls (gimple *stmt)
2519 struct tm_restart_node dummy;
2520 tm_restart_node **slot;
2522 if (!cfun->gimple_df->tm_restart)
2523 return;
2525 dummy.stmt = stmt;
2526 slot = cfun->gimple_df->tm_restart->find_slot (&dummy, NO_INSERT);
2527 if (slot)
2529 struct tm_restart_node *n = *slot;
2530 tree list = n->label_or_list;
2531 rtx_insn *insn;
2533 for (insn = next_real_insn (get_last_insn ());
2534 !CALL_P (insn);
2535 insn = next_real_insn (insn))
2536 continue;
2538 if (TREE_CODE (list) == LABEL_DECL)
2539 add_reg_note (insn, REG_TM, label_rtx (list));
2540 else
2541 for (; list ; list = TREE_CHAIN (list))
2542 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2546 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2547 statement STMT. */
2549 static void
2550 expand_call_stmt (gcall *stmt)
2552 tree exp, decl, lhs;
2553 bool builtin_p;
2554 size_t i;
2556 if (gimple_call_internal_p (stmt))
2558 expand_internal_call (stmt);
2559 return;
2562 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2564 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2565 decl = gimple_call_fndecl (stmt);
2566 builtin_p = decl && DECL_BUILT_IN (decl);
2568 /* If this is not a builtin function, the function type through which the
2569 call is made may be different from the type of the function. */
2570 if (!builtin_p)
2571 CALL_EXPR_FN (exp)
2572 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2573 CALL_EXPR_FN (exp));
2575 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2576 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2578 for (i = 0; i < gimple_call_num_args (stmt); i++)
2580 tree arg = gimple_call_arg (stmt, i);
2581 gimple *def;
2582 /* TER addresses into arguments of builtin functions so we have a
2583 chance to infer more correct alignment information. See PR39954. */
2584 if (builtin_p
2585 && TREE_CODE (arg) == SSA_NAME
2586 && (def = get_gimple_for_ssa_name (arg))
2587 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2588 arg = gimple_assign_rhs1 (def);
2589 CALL_EXPR_ARG (exp, i) = arg;
2592 if (gimple_has_side_effects (stmt))
2593 TREE_SIDE_EFFECTS (exp) = 1;
2595 if (gimple_call_nothrow_p (stmt))
2596 TREE_NOTHROW (exp) = 1;
2598 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2599 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2600 if (decl
2601 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2602 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2603 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2604 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2605 else
2606 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2607 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2608 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2609 CALL_WITH_BOUNDS_P (exp) = gimple_call_with_bounds_p (stmt);
2611 /* Ensure RTL is created for debug args. */
2612 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2614 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2615 unsigned int ix;
2616 tree dtemp;
2618 if (debug_args)
2619 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2621 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2622 expand_debug_expr (dtemp);
2626 lhs = gimple_call_lhs (stmt);
2627 if (lhs)
2628 expand_assignment (lhs, exp, false);
2629 else
2630 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2632 mark_transaction_restart_calls (stmt);
2636 /* Generate RTL for an asm statement (explicit assembler code).
2637 STRING is a STRING_CST node containing the assembler code text,
2638 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
2639 insn is volatile; don't optimize it. */
2641 static void
2642 expand_asm_loc (tree string, int vol, location_t locus)
2644 rtx body;
2646 if (TREE_CODE (string) == ADDR_EXPR)
2647 string = TREE_OPERAND (string, 0);
2649 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2650 ggc_strdup (TREE_STRING_POINTER (string)),
2651 locus);
2653 MEM_VOLATILE_P (body) = vol;
2655 emit_insn (body);
2658 /* Return the number of times character C occurs in string S. */
2659 static int
2660 n_occurrences (int c, const char *s)
2662 int n = 0;
2663 while (*s)
2664 n += (*s++ == c);
2665 return n;
2668 /* A subroutine of expand_asm_operands. Check that all operands have
2669 the same number of alternatives. Return true if so. */
2671 static bool
2672 check_operand_nalternatives (const vec<const char *> &constraints)
2674 unsigned len = constraints.length();
2675 if (len > 0)
2677 int nalternatives = n_occurrences (',', constraints[0]);
2679 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2681 error ("too many alternatives in %<asm%>");
2682 return false;
2685 for (unsigned i = 1; i < len; ++i)
2686 if (n_occurrences (',', constraints[i]) != nalternatives)
2688 error ("operand constraints for %<asm%> differ "
2689 "in number of alternatives");
2690 return false;
2693 return true;
2696 /* Check for overlap between registers marked in CLOBBERED_REGS and
2697 anything inappropriate in T. Emit error and return the register
2698 variable definition for error, NULL_TREE for ok. */
2700 static bool
2701 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
2703 /* Conflicts between asm-declared register variables and the clobber
2704 list are not allowed. */
2705 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2707 if (overlap)
2709 error ("asm-specifier for variable %qE conflicts with asm clobber list",
2710 DECL_NAME (overlap));
2712 /* Reset registerness to stop multiple errors emitted for a single
2713 variable. */
2714 DECL_REGISTER (overlap) = 0;
2715 return true;
2718 return false;
2721 /* Generate RTL for an asm statement with arguments.
2722 STRING is the instruction template.
2723 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
2724 Each output or input has an expression in the TREE_VALUE and
2725 a tree list in TREE_PURPOSE which in turn contains a constraint
2726 name in TREE_VALUE (or NULL_TREE) and a constraint string
2727 in TREE_PURPOSE.
2728 CLOBBERS is a list of STRING_CST nodes each naming a hard register
2729 that is clobbered by this insn.
2731 LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
2732 should be the fallthru basic block of the asm goto.
2734 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
2735 Some elements of OUTPUTS may be replaced with trees representing temporary
2736 values. The caller should copy those temporary values to the originally
2737 specified lvalues.
2739 VOL nonzero means the insn is volatile; don't optimize it. */
2741 static void
2742 expand_asm_stmt (gasm *stmt)
2744 class save_input_location
2746 location_t old;
2748 public:
2749 explicit save_input_location(location_t where)
2751 old = input_location;
2752 input_location = where;
2755 ~save_input_location()
2757 input_location = old;
2761 location_t locus = gimple_location (stmt);
2763 if (gimple_asm_input_p (stmt))
2765 const char *s = gimple_asm_string (stmt);
2766 tree string = build_string (strlen (s), s);
2767 expand_asm_loc (string, gimple_asm_volatile_p (stmt), locus);
2768 return;
2771 /* There are some legacy diagnostics in here, and also avoids a
2772 sixth parameger to targetm.md_asm_adjust. */
2773 save_input_location s_i_l(locus);
2775 unsigned noutputs = gimple_asm_noutputs (stmt);
2776 unsigned ninputs = gimple_asm_ninputs (stmt);
2777 unsigned nlabels = gimple_asm_nlabels (stmt);
2778 unsigned i;
2780 /* ??? Diagnose during gimplification? */
2781 if (ninputs + noutputs + nlabels > MAX_RECOG_OPERANDS)
2783 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
2784 return;
2787 auto_vec<tree, MAX_RECOG_OPERANDS> output_tvec;
2788 auto_vec<tree, MAX_RECOG_OPERANDS> input_tvec;
2789 auto_vec<const char *, MAX_RECOG_OPERANDS> constraints;
2791 /* Copy the gimple vectors into new vectors that we can manipulate. */
2793 output_tvec.safe_grow (noutputs);
2794 input_tvec.safe_grow (ninputs);
2795 constraints.safe_grow (noutputs + ninputs);
2797 for (i = 0; i < noutputs; ++i)
2799 tree t = gimple_asm_output_op (stmt, i);
2800 output_tvec[i] = TREE_VALUE (t);
2801 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2803 for (i = 0; i < ninputs; i++)
2805 tree t = gimple_asm_input_op (stmt, i);
2806 input_tvec[i] = TREE_VALUE (t);
2807 constraints[i + noutputs]
2808 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2811 /* ??? Diagnose during gimplification? */
2812 if (! check_operand_nalternatives (constraints))
2813 return;
2815 /* Count the number of meaningful clobbered registers, ignoring what
2816 we would ignore later. */
2817 auto_vec<rtx> clobber_rvec;
2818 HARD_REG_SET clobbered_regs;
2819 CLEAR_HARD_REG_SET (clobbered_regs);
2821 if (unsigned n = gimple_asm_nclobbers (stmt))
2823 clobber_rvec.reserve (n);
2824 for (i = 0; i < n; i++)
2826 tree t = gimple_asm_clobber_op (stmt, i);
2827 const char *regname = TREE_STRING_POINTER (TREE_VALUE (t));
2828 int nregs, j;
2830 j = decode_reg_name_and_count (regname, &nregs);
2831 if (j < 0)
2833 if (j == -2)
2835 /* ??? Diagnose during gimplification? */
2836 error ("unknown register name %qs in %<asm%>", regname);
2838 else if (j == -4)
2840 rtx x = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2841 clobber_rvec.safe_push (x);
2843 else
2845 /* Otherwise we should have -1 == empty string
2846 or -3 == cc, which is not a register. */
2847 gcc_assert (j == -1 || j == -3);
2850 else
2851 for (int reg = j; reg < j + nregs; reg++)
2853 /* Clobbering the PIC register is an error. */
2854 if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
2856 /* ??? Diagnose during gimplification? */
2857 error ("PIC register clobbered by %qs in %<asm%>",
2858 regname);
2859 return;
2862 SET_HARD_REG_BIT (clobbered_regs, reg);
2863 rtx x = gen_rtx_REG (reg_raw_mode[reg], reg);
2864 clobber_rvec.safe_push (x);
2868 unsigned nclobbers = clobber_rvec.length();
2870 /* First pass over inputs and outputs checks validity and sets
2871 mark_addressable if needed. */
2872 /* ??? Diagnose during gimplification? */
2874 for (i = 0; i < noutputs; ++i)
2876 tree val = output_tvec[i];
2877 tree type = TREE_TYPE (val);
2878 const char *constraint;
2879 bool is_inout;
2880 bool allows_reg;
2881 bool allows_mem;
2883 /* Try to parse the output constraint. If that fails, there's
2884 no point in going further. */
2885 constraint = constraints[i];
2886 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
2887 &allows_mem, &allows_reg, &is_inout))
2888 return;
2890 if (! allows_reg
2891 && (allows_mem
2892 || is_inout
2893 || (DECL_P (val)
2894 && REG_P (DECL_RTL (val))
2895 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
2896 mark_addressable (val);
2899 for (i = 0; i < ninputs; ++i)
2901 bool allows_reg, allows_mem;
2902 const char *constraint;
2904 constraint = constraints[i + noutputs];
2905 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2906 constraints.address (),
2907 &allows_mem, &allows_reg))
2908 return;
2910 if (! allows_reg && allows_mem)
2911 mark_addressable (input_tvec[i]);
2914 /* Second pass evaluates arguments. */
2916 /* Make sure stack is consistent for asm goto. */
2917 if (nlabels > 0)
2918 do_pending_stack_adjust ();
2919 int old_generating_concat_p = generating_concat_p;
2921 /* Vector of RTX's of evaluated output operands. */
2922 auto_vec<rtx, MAX_RECOG_OPERANDS> output_rvec;
2923 auto_vec<int, MAX_RECOG_OPERANDS> inout_opnum;
2924 rtx_insn *after_rtl_seq = NULL, *after_rtl_end = NULL;
2926 output_rvec.safe_grow (noutputs);
2928 for (i = 0; i < noutputs; ++i)
2930 tree val = output_tvec[i];
2931 tree type = TREE_TYPE (val);
2932 bool is_inout, allows_reg, allows_mem, ok;
2933 rtx op;
2935 ok = parse_output_constraint (&constraints[i], i, ninputs,
2936 noutputs, &allows_mem, &allows_reg,
2937 &is_inout);
2938 gcc_assert (ok);
2940 /* If an output operand is not a decl or indirect ref and our constraint
2941 allows a register, make a temporary to act as an intermediate.
2942 Make the asm insn write into that, then we will copy it to
2943 the real output operand. Likewise for promoted variables. */
2945 generating_concat_p = 0;
2947 if ((TREE_CODE (val) == INDIRECT_REF
2948 && allows_mem)
2949 || (DECL_P (val)
2950 && (allows_mem || REG_P (DECL_RTL (val)))
2951 && ! (REG_P (DECL_RTL (val))
2952 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
2953 || ! allows_reg
2954 || is_inout)
2956 op = expand_expr (val, NULL_RTX, VOIDmode,
2957 !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
2958 if (MEM_P (op))
2959 op = validize_mem (op);
2961 if (! allows_reg && !MEM_P (op))
2962 error ("output number %d not directly addressable", i);
2963 if ((! allows_mem && MEM_P (op))
2964 || GET_CODE (op) == CONCAT)
2966 rtx old_op = op;
2967 op = gen_reg_rtx (GET_MODE (op));
2969 generating_concat_p = old_generating_concat_p;
2971 if (is_inout)
2972 emit_move_insn (op, old_op);
2974 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2975 emit_move_insn (old_op, op);
2976 after_rtl_seq = get_insns ();
2977 after_rtl_end = get_last_insn ();
2978 end_sequence ();
2981 else
2983 op = assign_temp (type, 0, 1);
2984 op = validize_mem (op);
2985 if (!MEM_P (op) && TREE_CODE (val) == SSA_NAME)
2986 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (val), op);
2988 generating_concat_p = old_generating_concat_p;
2990 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2991 expand_assignment (val, make_tree (type, op), false);
2992 after_rtl_seq = get_insns ();
2993 after_rtl_end = get_last_insn ();
2994 end_sequence ();
2996 output_rvec[i] = op;
2998 if (is_inout)
2999 inout_opnum.safe_push (i);
3002 auto_vec<rtx, MAX_RECOG_OPERANDS> input_rvec;
3003 auto_vec<machine_mode, MAX_RECOG_OPERANDS> input_mode;
3005 input_rvec.safe_grow (ninputs);
3006 input_mode.safe_grow (ninputs);
3008 generating_concat_p = 0;
3010 for (i = 0; i < ninputs; ++i)
3012 tree val = input_tvec[i];
3013 tree type = TREE_TYPE (val);
3014 bool allows_reg, allows_mem, ok;
3015 const char *constraint;
3016 rtx op;
3018 constraint = constraints[i + noutputs];
3019 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
3020 constraints.address (),
3021 &allows_mem, &allows_reg);
3022 gcc_assert (ok);
3024 /* EXPAND_INITIALIZER will not generate code for valid initializer
3025 constants, but will still generate code for other types of operand.
3026 This is the behavior we want for constant constraints. */
3027 op = expand_expr (val, NULL_RTX, VOIDmode,
3028 allows_reg ? EXPAND_NORMAL
3029 : allows_mem ? EXPAND_MEMORY
3030 : EXPAND_INITIALIZER);
3032 /* Never pass a CONCAT to an ASM. */
3033 if (GET_CODE (op) == CONCAT)
3034 op = force_reg (GET_MODE (op), op);
3035 else if (MEM_P (op))
3036 op = validize_mem (op);
3038 if (asm_operand_ok (op, constraint, NULL) <= 0)
3040 if (allows_reg && TYPE_MODE (type) != BLKmode)
3041 op = force_reg (TYPE_MODE (type), op);
3042 else if (!allows_mem)
3043 warning (0, "asm operand %d probably doesn%'t match constraints",
3044 i + noutputs);
3045 else if (MEM_P (op))
3047 /* We won't recognize either volatile memory or memory
3048 with a queued address as available a memory_operand
3049 at this point. Ignore it: clearly this *is* a memory. */
3051 else
3052 gcc_unreachable ();
3054 input_rvec[i] = op;
3055 input_mode[i] = TYPE_MODE (type);
3058 /* For in-out operands, copy output rtx to input rtx. */
3059 unsigned ninout = inout_opnum.length();
3060 for (i = 0; i < ninout; i++)
3062 int j = inout_opnum[i];
3063 rtx o = output_rvec[j];
3065 input_rvec.safe_push (o);
3066 input_mode.safe_push (GET_MODE (o));
3068 char buffer[16];
3069 sprintf (buffer, "%d", j);
3070 constraints.safe_push (ggc_strdup (buffer));
3072 ninputs += ninout;
3074 /* Sometimes we wish to automatically clobber registers across an asm.
3075 Case in point is when the i386 backend moved from cc0 to a hard reg --
3076 maintaining source-level compatibility means automatically clobbering
3077 the flags register. */
3078 rtx_insn *after_md_seq = NULL;
3079 if (targetm.md_asm_adjust)
3080 after_md_seq = targetm.md_asm_adjust (output_rvec, input_rvec,
3081 constraints, clobber_rvec,
3082 clobbered_regs);
3084 /* Do not allow the hook to change the output and input count,
3085 lest it mess up the operand numbering. */
3086 gcc_assert (output_rvec.length() == noutputs);
3087 gcc_assert (input_rvec.length() == ninputs);
3088 gcc_assert (constraints.length() == noutputs + ninputs);
3090 /* But it certainly can adjust the clobbers. */
3091 nclobbers = clobber_rvec.length();
3093 /* Third pass checks for easy conflicts. */
3094 /* ??? Why are we doing this on trees instead of rtx. */
3096 bool clobber_conflict_found = 0;
3097 for (i = 0; i < noutputs; ++i)
3098 if (tree_conflicts_with_clobbers_p (output_tvec[i], &clobbered_regs))
3099 clobber_conflict_found = 1;
3100 for (i = 0; i < ninputs - ninout; ++i)
3101 if (tree_conflicts_with_clobbers_p (input_tvec[i], &clobbered_regs))
3102 clobber_conflict_found = 1;
3104 /* Make vectors for the expression-rtx, constraint strings,
3105 and named operands. */
3107 rtvec argvec = rtvec_alloc (ninputs);
3108 rtvec constraintvec = rtvec_alloc (ninputs);
3109 rtvec labelvec = rtvec_alloc (nlabels);
3111 rtx body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
3112 : GET_MODE (output_rvec[0])),
3113 ggc_strdup (gimple_asm_string (stmt)),
3114 empty_string, 0, argvec, constraintvec,
3115 labelvec, locus);
3116 MEM_VOLATILE_P (body) = gimple_asm_volatile_p (stmt);
3118 for (i = 0; i < ninputs; ++i)
3120 ASM_OPERANDS_INPUT (body, i) = input_rvec[i];
3121 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
3122 = gen_rtx_ASM_INPUT_loc (input_mode[i],
3123 constraints[i + noutputs],
3124 locus);
3127 /* Copy labels to the vector. */
3128 rtx_code_label *fallthru_label = NULL;
3129 if (nlabels > 0)
3131 basic_block fallthru_bb = NULL;
3132 edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
3133 if (fallthru)
3134 fallthru_bb = fallthru->dest;
3136 for (i = 0; i < nlabels; ++i)
3138 tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
3139 rtx_insn *r;
3140 /* If asm goto has any labels in the fallthru basic block, use
3141 a label that we emit immediately after the asm goto. Expansion
3142 may insert further instructions into the same basic block after
3143 asm goto and if we don't do this, insertion of instructions on
3144 the fallthru edge might misbehave. See PR58670. */
3145 if (fallthru_bb && label_to_block_fn (cfun, label) == fallthru_bb)
3147 if (fallthru_label == NULL_RTX)
3148 fallthru_label = gen_label_rtx ();
3149 r = fallthru_label;
3151 else
3152 r = label_rtx (label);
3153 ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
3157 /* Now, for each output, construct an rtx
3158 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
3159 ARGVEC CONSTRAINTS OPNAMES))
3160 If there is more than one, put them inside a PARALLEL. */
3162 if (nlabels > 0 && nclobbers == 0)
3164 gcc_assert (noutputs == 0);
3165 emit_jump_insn (body);
3167 else if (noutputs == 0 && nclobbers == 0)
3169 /* No output operands: put in a raw ASM_OPERANDS rtx. */
3170 emit_insn (body);
3172 else if (noutputs == 1 && nclobbers == 0)
3174 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
3175 emit_insn (gen_rtx_SET (output_rvec[0], body));
3177 else
3179 rtx obody = body;
3180 int num = noutputs;
3182 if (num == 0)
3183 num = 1;
3185 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
3187 /* For each output operand, store a SET. */
3188 for (i = 0; i < noutputs; ++i)
3190 rtx src, o = output_rvec[i];
3191 if (i == 0)
3193 ASM_OPERANDS_OUTPUT_CONSTRAINT (obody) = constraints[0];
3194 src = obody;
3196 else
3198 src = gen_rtx_ASM_OPERANDS (GET_MODE (o),
3199 ASM_OPERANDS_TEMPLATE (obody),
3200 constraints[i], i, argvec,
3201 constraintvec, labelvec, locus);
3202 MEM_VOLATILE_P (src) = gimple_asm_volatile_p (stmt);
3204 XVECEXP (body, 0, i) = gen_rtx_SET (o, src);
3207 /* If there are no outputs (but there are some clobbers)
3208 store the bare ASM_OPERANDS into the PARALLEL. */
3209 if (i == 0)
3210 XVECEXP (body, 0, i++) = obody;
3212 /* Store (clobber REG) for each clobbered register specified. */
3213 for (unsigned j = 0; j < nclobbers; ++j)
3215 rtx clobbered_reg = clobber_rvec[j];
3217 /* Do sanity check for overlap between clobbers and respectively
3218 input and outputs that hasn't been handled. Such overlap
3219 should have been detected and reported above. */
3220 if (!clobber_conflict_found && REG_P (clobbered_reg))
3222 /* We test the old body (obody) contents to avoid
3223 tripping over the under-construction body. */
3224 for (unsigned k = 0; k < noutputs; ++k)
3225 if (reg_overlap_mentioned_p (clobbered_reg, output_rvec[k]))
3226 internal_error ("asm clobber conflict with output operand");
3228 for (unsigned k = 0; k < ninputs - ninout; ++k)
3229 if (reg_overlap_mentioned_p (clobbered_reg, input_rvec[k]))
3230 internal_error ("asm clobber conflict with input operand");
3233 XVECEXP (body, 0, i++) = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
3236 if (nlabels > 0)
3237 emit_jump_insn (body);
3238 else
3239 emit_insn (body);
3242 generating_concat_p = old_generating_concat_p;
3244 if (fallthru_label)
3245 emit_label (fallthru_label);
3247 if (after_md_seq)
3248 emit_insn (after_md_seq);
3249 if (after_rtl_seq)
3250 emit_insn (after_rtl_seq);
3252 free_temp_slots ();
3253 crtl->has_asm_statement = 1;
3256 /* Emit code to jump to the address
3257 specified by the pointer expression EXP. */
3259 static void
3260 expand_computed_goto (tree exp)
3262 rtx x = expand_normal (exp);
3264 do_pending_stack_adjust ();
3265 emit_indirect_jump (x);
3268 /* Generate RTL code for a `goto' statement with target label LABEL.
3269 LABEL should be a LABEL_DECL tree node that was or will later be
3270 defined with `expand_label'. */
3272 static void
3273 expand_goto (tree label)
3275 #ifdef ENABLE_CHECKING
3276 /* Check for a nonlocal goto to a containing function. Should have
3277 gotten translated to __builtin_nonlocal_goto. */
3278 tree context = decl_function_context (label);
3279 gcc_assert (!context || context == current_function_decl);
3280 #endif
3282 emit_jump (jump_target_rtx (label));
3285 /* Output a return with no value. */
3287 static void
3288 expand_null_return_1 (void)
3290 clear_pending_stack_adjust ();
3291 do_pending_stack_adjust ();
3292 emit_jump (return_label);
3295 /* Generate RTL to return from the current function, with no value.
3296 (That is, we do not do anything about returning any value.) */
3298 void
3299 expand_null_return (void)
3301 /* If this function was declared to return a value, but we
3302 didn't, clobber the return registers so that they are not
3303 propagated live to the rest of the function. */
3304 clobber_return_register ();
3306 expand_null_return_1 ();
3309 /* Generate RTL to return from the current function, with value VAL. */
3311 static void
3312 expand_value_return (rtx val)
3314 /* Copy the value to the return location unless it's already there. */
3316 tree decl = DECL_RESULT (current_function_decl);
3317 rtx return_reg = DECL_RTL (decl);
3318 if (return_reg != val)
3320 tree funtype = TREE_TYPE (current_function_decl);
3321 tree type = TREE_TYPE (decl);
3322 int unsignedp = TYPE_UNSIGNED (type);
3323 machine_mode old_mode = DECL_MODE (decl);
3324 machine_mode mode;
3325 if (DECL_BY_REFERENCE (decl))
3326 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3327 else
3328 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3330 if (mode != old_mode)
3331 val = convert_modes (mode, old_mode, val, unsignedp);
3333 if (GET_CODE (return_reg) == PARALLEL)
3334 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3335 else
3336 emit_move_insn (return_reg, val);
3339 expand_null_return_1 ();
3342 /* Generate RTL to evaluate the expression RETVAL and return it
3343 from the current function. */
3345 static void
3346 expand_return (tree retval, tree bounds)
3348 rtx result_rtl;
3349 rtx val = 0;
3350 tree retval_rhs;
3351 rtx bounds_rtl;
3353 /* If function wants no value, give it none. */
3354 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3356 expand_normal (retval);
3357 expand_null_return ();
3358 return;
3361 if (retval == error_mark_node)
3363 /* Treat this like a return of no value from a function that
3364 returns a value. */
3365 expand_null_return ();
3366 return;
3368 else if ((TREE_CODE (retval) == MODIFY_EXPR
3369 || TREE_CODE (retval) == INIT_EXPR)
3370 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3371 retval_rhs = TREE_OPERAND (retval, 1);
3372 else
3373 retval_rhs = retval;
3375 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3377 /* Put returned bounds to the right place. */
3378 bounds_rtl = DECL_BOUNDS_RTL (DECL_RESULT (current_function_decl));
3379 if (bounds_rtl)
3381 rtx addr = NULL;
3382 rtx bnd = NULL;
3384 if (bounds && bounds != error_mark_node)
3386 bnd = expand_normal (bounds);
3387 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3389 else if (REG_P (bounds_rtl))
3391 if (bounds)
3392 bnd = chkp_expand_zero_bounds ();
3393 else
3395 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3396 addr = gen_rtx_MEM (Pmode, addr);
3397 bnd = targetm.calls.load_bounds_for_arg (addr, NULL, NULL);
3400 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3402 else
3404 int n;
3406 gcc_assert (GET_CODE (bounds_rtl) == PARALLEL);
3408 if (bounds)
3409 bnd = chkp_expand_zero_bounds ();
3410 else
3412 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3413 addr = gen_rtx_MEM (Pmode, addr);
3416 for (n = 0; n < XVECLEN (bounds_rtl, 0); n++)
3418 rtx slot = XEXP (XVECEXP (bounds_rtl, 0, n), 0);
3419 if (!bounds)
3421 rtx offs = XEXP (XVECEXP (bounds_rtl, 0, n), 1);
3422 rtx from = adjust_address (addr, Pmode, INTVAL (offs));
3423 bnd = targetm.calls.load_bounds_for_arg (from, NULL, NULL);
3425 targetm.calls.store_returned_bounds (slot, bnd);
3429 else if (chkp_function_instrumented_p (current_function_decl)
3430 && !BOUNDED_P (retval_rhs)
3431 && chkp_type_has_pointer (TREE_TYPE (retval_rhs))
3432 && TREE_CODE (retval_rhs) != RESULT_DECL)
3434 rtx addr = expand_normal (build_fold_addr_expr (retval_rhs));
3435 addr = gen_rtx_MEM (Pmode, addr);
3437 gcc_assert (MEM_P (result_rtl));
3439 chkp_copy_bounds_for_stack_parm (result_rtl, addr, TREE_TYPE (retval_rhs));
3442 /* If we are returning the RESULT_DECL, then the value has already
3443 been stored into it, so we don't have to do anything special. */
3444 if (TREE_CODE (retval_rhs) == RESULT_DECL)
3445 expand_value_return (result_rtl);
3447 /* If the result is an aggregate that is being returned in one (or more)
3448 registers, load the registers here. */
3450 else if (retval_rhs != 0
3451 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3452 && REG_P (result_rtl))
3454 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3455 if (val)
3457 /* Use the mode of the result value on the return register. */
3458 PUT_MODE (result_rtl, GET_MODE (val));
3459 expand_value_return (val);
3461 else
3462 expand_null_return ();
3464 else if (retval_rhs != 0
3465 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3466 && (REG_P (result_rtl)
3467 || (GET_CODE (result_rtl) == PARALLEL)))
3469 /* Compute the return value into a temporary (usually a pseudo reg). */
3471 = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3472 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3473 val = force_not_mem (val);
3474 expand_value_return (val);
3476 else
3478 /* No hard reg used; calculate value into hard return reg. */
3479 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3480 expand_value_return (result_rtl);
3484 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3485 STMT that doesn't require special handling for outgoing edges. That
3486 is no tailcalls and no GIMPLE_COND. */
3488 static void
3489 expand_gimple_stmt_1 (gimple *stmt)
3491 tree op0;
3493 set_curr_insn_location (gimple_location (stmt));
3495 switch (gimple_code (stmt))
3497 case GIMPLE_GOTO:
3498 op0 = gimple_goto_dest (stmt);
3499 if (TREE_CODE (op0) == LABEL_DECL)
3500 expand_goto (op0);
3501 else
3502 expand_computed_goto (op0);
3503 break;
3504 case GIMPLE_LABEL:
3505 expand_label (gimple_label_label (as_a <glabel *> (stmt)));
3506 break;
3507 case GIMPLE_NOP:
3508 case GIMPLE_PREDICT:
3509 break;
3510 case GIMPLE_SWITCH:
3511 expand_case (as_a <gswitch *> (stmt));
3512 break;
3513 case GIMPLE_ASM:
3514 expand_asm_stmt (as_a <gasm *> (stmt));
3515 break;
3516 case GIMPLE_CALL:
3517 expand_call_stmt (as_a <gcall *> (stmt));
3518 break;
3520 case GIMPLE_RETURN:
3522 tree bnd = gimple_return_retbnd (as_a <greturn *> (stmt));
3523 op0 = gimple_return_retval (as_a <greturn *> (stmt));
3525 if (op0 && op0 != error_mark_node)
3527 tree result = DECL_RESULT (current_function_decl);
3529 /* If we are not returning the current function's RESULT_DECL,
3530 build an assignment to it. */
3531 if (op0 != result)
3533 /* I believe that a function's RESULT_DECL is unique. */
3534 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3536 /* ??? We'd like to use simply expand_assignment here,
3537 but this fails if the value is of BLKmode but the return
3538 decl is a register. expand_return has special handling
3539 for this combination, which eventually should move
3540 to common code. See comments there. Until then, let's
3541 build a modify expression :-/ */
3542 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3543 result, op0);
3545 /* Mark we have return statement with missing bounds. */
3546 if (!bnd && chkp_function_instrumented_p (cfun->decl))
3547 bnd = error_mark_node;
3550 if (!op0)
3551 expand_null_return ();
3552 else
3553 expand_return (op0, bnd);
3555 break;
3557 case GIMPLE_ASSIGN:
3559 gassign *assign_stmt = as_a <gassign *> (stmt);
3560 tree lhs = gimple_assign_lhs (assign_stmt);
3562 /* Tree expand used to fiddle with |= and &= of two bitfield
3563 COMPONENT_REFs here. This can't happen with gimple, the LHS
3564 of binary assigns must be a gimple reg. */
3566 if (TREE_CODE (lhs) != SSA_NAME
3567 || get_gimple_rhs_class (gimple_expr_code (stmt))
3568 == GIMPLE_SINGLE_RHS)
3570 tree rhs = gimple_assign_rhs1 (assign_stmt);
3571 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
3572 == GIMPLE_SINGLE_RHS);
3573 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs)
3574 /* Do not put locations on possibly shared trees. */
3575 && !is_gimple_min_invariant (rhs))
3576 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3577 if (TREE_CLOBBER_P (rhs))
3578 /* This is a clobber to mark the going out of scope for
3579 this LHS. */
3581 else
3582 expand_assignment (lhs, rhs,
3583 gimple_assign_nontemporal_move_p (
3584 assign_stmt));
3586 else
3588 rtx target, temp;
3589 bool nontemporal = gimple_assign_nontemporal_move_p (assign_stmt);
3590 struct separate_ops ops;
3591 bool promoted = false;
3593 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3594 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
3595 promoted = true;
3597 ops.code = gimple_assign_rhs_code (assign_stmt);
3598 ops.type = TREE_TYPE (lhs);
3599 switch (get_gimple_rhs_class (ops.code))
3601 case GIMPLE_TERNARY_RHS:
3602 ops.op2 = gimple_assign_rhs3 (assign_stmt);
3603 /* Fallthru */
3604 case GIMPLE_BINARY_RHS:
3605 ops.op1 = gimple_assign_rhs2 (assign_stmt);
3606 /* Fallthru */
3607 case GIMPLE_UNARY_RHS:
3608 ops.op0 = gimple_assign_rhs1 (assign_stmt);
3609 break;
3610 default:
3611 gcc_unreachable ();
3613 ops.location = gimple_location (stmt);
3615 /* If we want to use a nontemporal store, force the value to
3616 register first. If we store into a promoted register,
3617 don't directly expand to target. */
3618 temp = nontemporal || promoted ? NULL_RTX : target;
3619 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
3620 EXPAND_NORMAL);
3622 if (temp == target)
3624 else if (promoted)
3626 int unsignedp = SUBREG_PROMOTED_SIGN (target);
3627 /* If TEMP is a VOIDmode constant, use convert_modes to make
3628 sure that we properly convert it. */
3629 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
3631 temp = convert_modes (GET_MODE (target),
3632 TYPE_MODE (ops.type),
3633 temp, unsignedp);
3634 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
3635 GET_MODE (target), temp, unsignedp);
3638 convert_move (SUBREG_REG (target), temp, unsignedp);
3640 else if (nontemporal && emit_storent_insn (target, temp))
3642 else
3644 temp = force_operand (temp, target);
3645 if (temp != target)
3646 emit_move_insn (target, temp);
3650 break;
3652 default:
3653 gcc_unreachable ();
3657 /* Expand one gimple statement STMT and return the last RTL instruction
3658 before any of the newly generated ones.
3660 In addition to generating the necessary RTL instructions this also
3661 sets REG_EH_REGION notes if necessary and sets the current source
3662 location for diagnostics. */
3664 static rtx_insn *
3665 expand_gimple_stmt (gimple *stmt)
3667 location_t saved_location = input_location;
3668 rtx_insn *last = get_last_insn ();
3669 int lp_nr;
3671 gcc_assert (cfun);
3673 /* We need to save and restore the current source location so that errors
3674 discovered during expansion are emitted with the right location. But
3675 it would be better if the diagnostic routines used the source location
3676 embedded in the tree nodes rather than globals. */
3677 if (gimple_has_location (stmt))
3678 input_location = gimple_location (stmt);
3680 expand_gimple_stmt_1 (stmt);
3682 /* Free any temporaries used to evaluate this statement. */
3683 free_temp_slots ();
3685 input_location = saved_location;
3687 /* Mark all insns that may trap. */
3688 lp_nr = lookup_stmt_eh_lp (stmt);
3689 if (lp_nr)
3691 rtx_insn *insn;
3692 for (insn = next_real_insn (last); insn;
3693 insn = next_real_insn (insn))
3695 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
3696 /* If we want exceptions for non-call insns, any
3697 may_trap_p instruction may throw. */
3698 && GET_CODE (PATTERN (insn)) != CLOBBER
3699 && GET_CODE (PATTERN (insn)) != USE
3700 && insn_could_throw_p (insn))
3701 make_reg_eh_region_note (insn, 0, lp_nr);
3705 return last;
3708 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
3709 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
3710 generated a tail call (something that might be denied by the ABI
3711 rules governing the call; see calls.c).
3713 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
3714 can still reach the rest of BB. The case here is __builtin_sqrt,
3715 where the NaN result goes through the external function (with a
3716 tailcall) and the normal result happens via a sqrt instruction. */
3718 static basic_block
3719 expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
3721 rtx_insn *last2, *last;
3722 edge e;
3723 edge_iterator ei;
3724 int probability;
3725 gcov_type count;
3727 last2 = last = expand_gimple_stmt (stmt);
3729 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
3730 if (CALL_P (last) && SIBLING_CALL_P (last))
3731 goto found;
3733 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3735 *can_fallthru = true;
3736 return NULL;
3738 found:
3739 /* ??? Wouldn't it be better to just reset any pending stack adjust?
3740 Any instructions emitted here are about to be deleted. */
3741 do_pending_stack_adjust ();
3743 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
3744 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
3745 EH or abnormal edges, we shouldn't have created a tail call in
3746 the first place. So it seems to me we should just be removing
3747 all edges here, or redirecting the existing fallthru edge to
3748 the exit block. */
3750 probability = 0;
3751 count = 0;
3753 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3755 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
3757 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3759 e->dest->count -= e->count;
3760 e->dest->frequency -= EDGE_FREQUENCY (e);
3761 if (e->dest->count < 0)
3762 e->dest->count = 0;
3763 if (e->dest->frequency < 0)
3764 e->dest->frequency = 0;
3766 count += e->count;
3767 probability += e->probability;
3768 remove_edge (e);
3770 else
3771 ei_next (&ei);
3774 /* This is somewhat ugly: the call_expr expander often emits instructions
3775 after the sibcall (to perform the function return). These confuse the
3776 find_many_sub_basic_blocks code, so we need to get rid of these. */
3777 last = NEXT_INSN (last);
3778 gcc_assert (BARRIER_P (last));
3780 *can_fallthru = false;
3781 while (NEXT_INSN (last))
3783 /* For instance an sqrt builtin expander expands if with
3784 sibcall in the then and label for `else`. */
3785 if (LABEL_P (NEXT_INSN (last)))
3787 *can_fallthru = true;
3788 break;
3790 delete_insn (NEXT_INSN (last));
3793 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
3794 | EDGE_SIBCALL);
3795 e->probability += probability;
3796 e->count += count;
3797 BB_END (bb) = last;
3798 update_bb_for_insn (bb);
3800 if (NEXT_INSN (last))
3802 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
3804 last = BB_END (bb);
3805 if (BARRIER_P (last))
3806 BB_END (bb) = PREV_INSN (last);
3809 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3811 return bb;
3814 /* Return the difference between the floor and the truncated result of
3815 a signed division by OP1 with remainder MOD. */
3816 static rtx
3817 floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3819 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
3820 return gen_rtx_IF_THEN_ELSE
3821 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3822 gen_rtx_IF_THEN_ELSE
3823 (mode, gen_rtx_LT (BImode,
3824 gen_rtx_DIV (mode, op1, mod),
3825 const0_rtx),
3826 constm1_rtx, const0_rtx),
3827 const0_rtx);
3830 /* Return the difference between the ceil and the truncated result of
3831 a signed division by OP1 with remainder MOD. */
3832 static rtx
3833 ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3835 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
3836 return gen_rtx_IF_THEN_ELSE
3837 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3838 gen_rtx_IF_THEN_ELSE
3839 (mode, gen_rtx_GT (BImode,
3840 gen_rtx_DIV (mode, op1, mod),
3841 const0_rtx),
3842 const1_rtx, const0_rtx),
3843 const0_rtx);
3846 /* Return the difference between the ceil and the truncated result of
3847 an unsigned division by OP1 with remainder MOD. */
3848 static rtx
3849 ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
3851 /* (mod != 0 ? 1 : 0) */
3852 return gen_rtx_IF_THEN_ELSE
3853 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3854 const1_rtx, const0_rtx);
3857 /* Return the difference between the rounded and the truncated result
3858 of a signed division by OP1 with remainder MOD. Halfway cases are
3859 rounded away from zero, rather than to the nearest even number. */
3860 static rtx
3861 round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3863 /* (abs (mod) >= abs (op1) - abs (mod)
3864 ? (op1 / mod > 0 ? 1 : -1)
3865 : 0) */
3866 return gen_rtx_IF_THEN_ELSE
3867 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
3868 gen_rtx_MINUS (mode,
3869 gen_rtx_ABS (mode, op1),
3870 gen_rtx_ABS (mode, mod))),
3871 gen_rtx_IF_THEN_ELSE
3872 (mode, gen_rtx_GT (BImode,
3873 gen_rtx_DIV (mode, op1, mod),
3874 const0_rtx),
3875 const1_rtx, constm1_rtx),
3876 const0_rtx);
3879 /* Return the difference between the rounded and the truncated result
3880 of a unsigned division by OP1 with remainder MOD. Halfway cases
3881 are rounded away from zero, rather than to the nearest even
3882 number. */
3883 static rtx
3884 round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
3886 /* (mod >= op1 - mod ? 1 : 0) */
3887 return gen_rtx_IF_THEN_ELSE
3888 (mode, gen_rtx_GE (BImode, mod,
3889 gen_rtx_MINUS (mode, op1, mod)),
3890 const1_rtx, const0_rtx);
3893 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
3894 any rtl. */
3896 static rtx
3897 convert_debug_memory_address (machine_mode mode, rtx x,
3898 addr_space_t as)
3900 machine_mode xmode = GET_MODE (x);
3902 #ifndef POINTERS_EXTEND_UNSIGNED
3903 gcc_assert (mode == Pmode
3904 || mode == targetm.addr_space.address_mode (as));
3905 gcc_assert (xmode == mode || xmode == VOIDmode);
3906 #else
3907 rtx temp;
3909 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
3911 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
3912 return x;
3914 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
3915 x = lowpart_subreg (mode, x, xmode);
3916 else if (POINTERS_EXTEND_UNSIGNED > 0)
3917 x = gen_rtx_ZERO_EXTEND (mode, x);
3918 else if (!POINTERS_EXTEND_UNSIGNED)
3919 x = gen_rtx_SIGN_EXTEND (mode, x);
3920 else
3922 switch (GET_CODE (x))
3924 case SUBREG:
3925 if ((SUBREG_PROMOTED_VAR_P (x)
3926 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
3927 || (GET_CODE (SUBREG_REG (x)) == PLUS
3928 && REG_P (XEXP (SUBREG_REG (x), 0))
3929 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
3930 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
3931 && GET_MODE (SUBREG_REG (x)) == mode)
3932 return SUBREG_REG (x);
3933 break;
3934 case LABEL_REF:
3935 temp = gen_rtx_LABEL_REF (mode, LABEL_REF_LABEL (x));
3936 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
3937 return temp;
3938 case SYMBOL_REF:
3939 temp = shallow_copy_rtx (x);
3940 PUT_MODE (temp, mode);
3941 return temp;
3942 case CONST:
3943 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3944 if (temp)
3945 temp = gen_rtx_CONST (mode, temp);
3946 return temp;
3947 case PLUS:
3948 case MINUS:
3949 if (CONST_INT_P (XEXP (x, 1)))
3951 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3952 if (temp)
3953 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
3955 break;
3956 default:
3957 break;
3959 /* Don't know how to express ptr_extend as operation in debug info. */
3960 return NULL;
3962 #endif /* POINTERS_EXTEND_UNSIGNED */
3964 return x;
3967 /* Map from SSA_NAMEs to corresponding DEBUG_EXPR_DECLs created
3968 by avoid_deep_ter_for_debug. */
3970 static hash_map<tree, tree> *deep_ter_debug_map;
3972 /* Split too deep TER chains for debug stmts using debug temporaries. */
3974 static void
3975 avoid_deep_ter_for_debug (gimple *stmt, int depth)
3977 use_operand_p use_p;
3978 ssa_op_iter iter;
3979 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
3981 tree use = USE_FROM_PTR (use_p);
3982 if (TREE_CODE (use) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (use))
3983 continue;
3984 gimple *g = get_gimple_for_ssa_name (use);
3985 if (g == NULL)
3986 continue;
3987 if (depth > 6 && !stmt_ends_bb_p (g))
3989 if (deep_ter_debug_map == NULL)
3990 deep_ter_debug_map = new hash_map<tree, tree>;
3992 tree &vexpr = deep_ter_debug_map->get_or_insert (use);
3993 if (vexpr != NULL)
3994 continue;
3995 vexpr = make_node (DEBUG_EXPR_DECL);
3996 gimple *def_temp = gimple_build_debug_bind (vexpr, use, g);
3997 DECL_ARTIFICIAL (vexpr) = 1;
3998 TREE_TYPE (vexpr) = TREE_TYPE (use);
3999 DECL_MODE (vexpr) = TYPE_MODE (TREE_TYPE (use));
4000 gimple_stmt_iterator gsi = gsi_for_stmt (g);
4001 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
4002 avoid_deep_ter_for_debug (def_temp, 0);
4004 else
4005 avoid_deep_ter_for_debug (g, depth + 1);
4009 /* Return an RTX equivalent to the value of the parameter DECL. */
4011 static rtx
4012 expand_debug_parm_decl (tree decl)
4014 rtx incoming = DECL_INCOMING_RTL (decl);
4016 if (incoming
4017 && GET_MODE (incoming) != BLKmode
4018 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
4019 || (MEM_P (incoming)
4020 && REG_P (XEXP (incoming, 0))
4021 && HARD_REGISTER_P (XEXP (incoming, 0)))))
4023 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
4025 #ifdef HAVE_window_save
4026 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
4027 If the target machine has an explicit window save instruction, the
4028 actual entry value is the corresponding OUTGOING_REGNO instead. */
4029 if (REG_P (incoming)
4030 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
4031 incoming
4032 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
4033 OUTGOING_REGNO (REGNO (incoming)), 0);
4034 else if (MEM_P (incoming))
4036 rtx reg = XEXP (incoming, 0);
4037 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
4039 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
4040 incoming = replace_equiv_address_nv (incoming, reg);
4042 else
4043 incoming = copy_rtx (incoming);
4045 #endif
4047 ENTRY_VALUE_EXP (rtl) = incoming;
4048 return rtl;
4051 if (incoming
4052 && GET_MODE (incoming) != BLKmode
4053 && !TREE_ADDRESSABLE (decl)
4054 && MEM_P (incoming)
4055 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
4056 || (GET_CODE (XEXP (incoming, 0)) == PLUS
4057 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
4058 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
4059 return copy_rtx (incoming);
4061 return NULL_RTX;
4064 /* Return an RTX equivalent to the value of the tree expression EXP. */
4066 static rtx
4067 expand_debug_expr (tree exp)
4069 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
4070 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4071 machine_mode inner_mode = VOIDmode;
4072 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
4073 addr_space_t as;
4075 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
4077 case tcc_expression:
4078 switch (TREE_CODE (exp))
4080 case COND_EXPR:
4081 case DOT_PROD_EXPR:
4082 case SAD_EXPR:
4083 case WIDEN_MULT_PLUS_EXPR:
4084 case WIDEN_MULT_MINUS_EXPR:
4085 case FMA_EXPR:
4086 goto ternary;
4088 case TRUTH_ANDIF_EXPR:
4089 case TRUTH_ORIF_EXPR:
4090 case TRUTH_AND_EXPR:
4091 case TRUTH_OR_EXPR:
4092 case TRUTH_XOR_EXPR:
4093 goto binary;
4095 case TRUTH_NOT_EXPR:
4096 goto unary;
4098 default:
4099 break;
4101 break;
4103 ternary:
4104 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
4105 if (!op2)
4106 return NULL_RTX;
4107 /* Fall through. */
4109 binary:
4110 case tcc_binary:
4111 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4112 if (!op1)
4113 return NULL_RTX;
4114 switch (TREE_CODE (exp))
4116 case LSHIFT_EXPR:
4117 case RSHIFT_EXPR:
4118 case LROTATE_EXPR:
4119 case RROTATE_EXPR:
4120 case WIDEN_LSHIFT_EXPR:
4121 /* Ensure second operand isn't wider than the first one. */
4122 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 1)));
4123 if (SCALAR_INT_MODE_P (inner_mode))
4125 machine_mode opmode = mode;
4126 if (VECTOR_MODE_P (mode))
4127 opmode = GET_MODE_INNER (mode);
4128 if (SCALAR_INT_MODE_P (opmode)
4129 && (GET_MODE_PRECISION (opmode)
4130 < GET_MODE_PRECISION (inner_mode)))
4131 op1 = lowpart_subreg (opmode, op1, inner_mode);
4133 break;
4134 default:
4135 break;
4137 /* Fall through. */
4139 unary:
4140 case tcc_unary:
4141 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4142 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4143 if (!op0)
4144 return NULL_RTX;
4145 break;
4147 case tcc_comparison:
4148 unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
4149 goto binary;
4151 case tcc_type:
4152 case tcc_statement:
4153 gcc_unreachable ();
4155 case tcc_constant:
4156 case tcc_exceptional:
4157 case tcc_declaration:
4158 case tcc_reference:
4159 case tcc_vl_exp:
4160 break;
4163 switch (TREE_CODE (exp))
4165 case STRING_CST:
4166 if (!lookup_constant_def (exp))
4168 if (strlen (TREE_STRING_POINTER (exp)) + 1
4169 != (size_t) TREE_STRING_LENGTH (exp))
4170 return NULL_RTX;
4171 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
4172 op0 = gen_rtx_MEM (BLKmode, op0);
4173 set_mem_attributes (op0, exp, 0);
4174 return op0;
4176 /* Fall through... */
4178 case INTEGER_CST:
4179 case REAL_CST:
4180 case FIXED_CST:
4181 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
4182 return op0;
4184 case COMPLEX_CST:
4185 gcc_assert (COMPLEX_MODE_P (mode));
4186 op0 = expand_debug_expr (TREE_REALPART (exp));
4187 op1 = expand_debug_expr (TREE_IMAGPART (exp));
4188 return gen_rtx_CONCAT (mode, op0, op1);
4190 case DEBUG_EXPR_DECL:
4191 op0 = DECL_RTL_IF_SET (exp);
4193 if (op0)
4194 return op0;
4196 op0 = gen_rtx_DEBUG_EXPR (mode);
4197 DEBUG_EXPR_TREE_DECL (op0) = exp;
4198 SET_DECL_RTL (exp, op0);
4200 return op0;
4202 case VAR_DECL:
4203 case PARM_DECL:
4204 case FUNCTION_DECL:
4205 case LABEL_DECL:
4206 case CONST_DECL:
4207 case RESULT_DECL:
4208 op0 = DECL_RTL_IF_SET (exp);
4210 /* This decl was probably optimized away. */
4211 if (!op0)
4213 if (TREE_CODE (exp) != VAR_DECL
4214 || DECL_EXTERNAL (exp)
4215 || !TREE_STATIC (exp)
4216 || !DECL_NAME (exp)
4217 || DECL_HARD_REGISTER (exp)
4218 || DECL_IN_CONSTANT_POOL (exp)
4219 || mode == VOIDmode)
4220 return NULL;
4222 op0 = make_decl_rtl_for_debug (exp);
4223 if (!MEM_P (op0)
4224 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
4225 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
4226 return NULL;
4228 else
4229 op0 = copy_rtx (op0);
4231 if (GET_MODE (op0) == BLKmode
4232 /* If op0 is not BLKmode, but mode is, adjust_mode
4233 below would ICE. While it is likely a FE bug,
4234 try to be robust here. See PR43166. */
4235 || mode == BLKmode
4236 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
4238 gcc_assert (MEM_P (op0));
4239 op0 = adjust_address_nv (op0, mode, 0);
4240 return op0;
4243 /* Fall through. */
4245 adjust_mode:
4246 case PAREN_EXPR:
4247 CASE_CONVERT:
4249 inner_mode = GET_MODE (op0);
4251 if (mode == inner_mode)
4252 return op0;
4254 if (inner_mode == VOIDmode)
4256 if (TREE_CODE (exp) == SSA_NAME)
4257 inner_mode = TYPE_MODE (TREE_TYPE (exp));
4258 else
4259 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4260 if (mode == inner_mode)
4261 return op0;
4264 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4266 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4267 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4268 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4269 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4270 else
4271 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4273 else if (FLOAT_MODE_P (mode))
4275 gcc_assert (TREE_CODE (exp) != SSA_NAME);
4276 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4277 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
4278 else
4279 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
4281 else if (FLOAT_MODE_P (inner_mode))
4283 if (unsignedp)
4284 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4285 else
4286 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4288 else if (CONSTANT_P (op0)
4289 || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
4290 op0 = lowpart_subreg (mode, op0, inner_mode);
4291 else if (UNARY_CLASS_P (exp)
4292 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
4293 : unsignedp)
4294 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4295 else
4296 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4298 return op0;
4301 case MEM_REF:
4302 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4304 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
4305 TREE_OPERAND (exp, 0),
4306 TREE_OPERAND (exp, 1));
4307 if (newexp)
4308 return expand_debug_expr (newexp);
4310 /* FALLTHROUGH */
4311 case INDIRECT_REF:
4312 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4313 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4314 if (!op0)
4315 return NULL;
4317 if (TREE_CODE (exp) == MEM_REF)
4319 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4320 || (GET_CODE (op0) == PLUS
4321 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
4322 /* (mem (debug_implicit_ptr)) might confuse aliasing.
4323 Instead just use get_inner_reference. */
4324 goto component_ref;
4326 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4327 if (!op1 || !CONST_INT_P (op1))
4328 return NULL;
4330 op0 = plus_constant (inner_mode, op0, INTVAL (op1));
4333 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4335 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4336 op0, as);
4337 if (op0 == NULL_RTX)
4338 return NULL;
4340 op0 = gen_rtx_MEM (mode, op0);
4341 set_mem_attributes (op0, exp, 0);
4342 if (TREE_CODE (exp) == MEM_REF
4343 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4344 set_mem_expr (op0, NULL_TREE);
4345 set_mem_addr_space (op0, as);
4347 return op0;
4349 case TARGET_MEM_REF:
4350 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
4351 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
4352 return NULL;
4354 op0 = expand_debug_expr
4355 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
4356 if (!op0)
4357 return NULL;
4359 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4360 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4361 op0, as);
4362 if (op0 == NULL_RTX)
4363 return NULL;
4365 op0 = gen_rtx_MEM (mode, op0);
4367 set_mem_attributes (op0, exp, 0);
4368 set_mem_addr_space (op0, as);
4370 return op0;
4372 component_ref:
4373 case ARRAY_REF:
4374 case ARRAY_RANGE_REF:
4375 case COMPONENT_REF:
4376 case BIT_FIELD_REF:
4377 case REALPART_EXPR:
4378 case IMAGPART_EXPR:
4379 case VIEW_CONVERT_EXPR:
4381 machine_mode mode1;
4382 HOST_WIDE_INT bitsize, bitpos;
4383 tree offset;
4384 int volatilep = 0;
4385 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
4386 &mode1, &unsignedp, &volatilep, false);
4387 rtx orig_op0;
4389 if (bitsize == 0)
4390 return NULL;
4392 orig_op0 = op0 = expand_debug_expr (tem);
4394 if (!op0)
4395 return NULL;
4397 if (offset)
4399 machine_mode addrmode, offmode;
4401 if (!MEM_P (op0))
4402 return NULL;
4404 op0 = XEXP (op0, 0);
4405 addrmode = GET_MODE (op0);
4406 if (addrmode == VOIDmode)
4407 addrmode = Pmode;
4409 op1 = expand_debug_expr (offset);
4410 if (!op1)
4411 return NULL;
4413 offmode = GET_MODE (op1);
4414 if (offmode == VOIDmode)
4415 offmode = TYPE_MODE (TREE_TYPE (offset));
4417 if (addrmode != offmode)
4418 op1 = lowpart_subreg (addrmode, op1, offmode);
4420 /* Don't use offset_address here, we don't need a
4421 recognizable address, and we don't want to generate
4422 code. */
4423 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4424 op0, op1));
4427 if (MEM_P (op0))
4429 if (mode1 == VOIDmode)
4430 /* Bitfield. */
4431 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
4432 if (bitpos >= BITS_PER_UNIT)
4434 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
4435 bitpos %= BITS_PER_UNIT;
4437 else if (bitpos < 0)
4439 HOST_WIDE_INT units
4440 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4441 op0 = adjust_address_nv (op0, mode1, units);
4442 bitpos += units * BITS_PER_UNIT;
4444 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
4445 op0 = adjust_address_nv (op0, mode, 0);
4446 else if (GET_MODE (op0) != mode1)
4447 op0 = adjust_address_nv (op0, mode1, 0);
4448 else
4449 op0 = copy_rtx (op0);
4450 if (op0 == orig_op0)
4451 op0 = shallow_copy_rtx (op0);
4452 set_mem_attributes (op0, exp, 0);
4455 if (bitpos == 0 && mode == GET_MODE (op0))
4456 return op0;
4458 if (bitpos < 0)
4459 return NULL;
4461 if (GET_MODE (op0) == BLKmode)
4462 return NULL;
4464 if ((bitpos % BITS_PER_UNIT) == 0
4465 && bitsize == GET_MODE_BITSIZE (mode1))
4467 machine_mode opmode = GET_MODE (op0);
4469 if (opmode == VOIDmode)
4470 opmode = TYPE_MODE (TREE_TYPE (tem));
4472 /* This condition may hold if we're expanding the address
4473 right past the end of an array that turned out not to
4474 be addressable (i.e., the address was only computed in
4475 debug stmts). The gen_subreg below would rightfully
4476 crash, and the address doesn't really exist, so just
4477 drop it. */
4478 if (bitpos >= GET_MODE_BITSIZE (opmode))
4479 return NULL;
4481 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
4482 return simplify_gen_subreg (mode, op0, opmode,
4483 bitpos / BITS_PER_UNIT);
4486 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4487 && TYPE_UNSIGNED (TREE_TYPE (exp))
4488 ? SIGN_EXTRACT
4489 : ZERO_EXTRACT, mode,
4490 GET_MODE (op0) != VOIDmode
4491 ? GET_MODE (op0)
4492 : TYPE_MODE (TREE_TYPE (tem)),
4493 op0, GEN_INT (bitsize), GEN_INT (bitpos));
4496 case ABS_EXPR:
4497 return simplify_gen_unary (ABS, mode, op0, mode);
4499 case NEGATE_EXPR:
4500 return simplify_gen_unary (NEG, mode, op0, mode);
4502 case BIT_NOT_EXPR:
4503 return simplify_gen_unary (NOT, mode, op0, mode);
4505 case FLOAT_EXPR:
4506 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4507 0)))
4508 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4509 inner_mode);
4511 case FIX_TRUNC_EXPR:
4512 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4513 inner_mode);
4515 case POINTER_PLUS_EXPR:
4516 /* For the rare target where pointers are not the same size as
4517 size_t, we need to check for mis-matched modes and correct
4518 the addend. */
4519 if (op0 && op1
4520 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
4521 && GET_MODE (op0) != GET_MODE (op1))
4523 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1))
4524 /* If OP0 is a partial mode, then we must truncate, even if it has
4525 the same bitsize as OP1 as GCC's representation of partial modes
4526 is opaque. */
4527 || (GET_MODE_CLASS (GET_MODE (op0)) == MODE_PARTIAL_INT
4528 && GET_MODE_BITSIZE (GET_MODE (op0)) == GET_MODE_BITSIZE (GET_MODE (op1))))
4529 op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
4530 GET_MODE (op1));
4531 else
4532 /* We always sign-extend, regardless of the signedness of
4533 the operand, because the operand is always unsigned
4534 here even if the original C expression is signed. */
4535 op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
4536 GET_MODE (op1));
4538 /* Fall through. */
4539 case PLUS_EXPR:
4540 return simplify_gen_binary (PLUS, mode, op0, op1);
4542 case MINUS_EXPR:
4543 return simplify_gen_binary (MINUS, mode, op0, op1);
4545 case MULT_EXPR:
4546 return simplify_gen_binary (MULT, mode, op0, op1);
4548 case RDIV_EXPR:
4549 case TRUNC_DIV_EXPR:
4550 case EXACT_DIV_EXPR:
4551 if (unsignedp)
4552 return simplify_gen_binary (UDIV, mode, op0, op1);
4553 else
4554 return simplify_gen_binary (DIV, mode, op0, op1);
4556 case TRUNC_MOD_EXPR:
4557 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4559 case FLOOR_DIV_EXPR:
4560 if (unsignedp)
4561 return simplify_gen_binary (UDIV, mode, op0, op1);
4562 else
4564 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4565 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4566 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4567 return simplify_gen_binary (PLUS, mode, div, adj);
4570 case FLOOR_MOD_EXPR:
4571 if (unsignedp)
4572 return simplify_gen_binary (UMOD, mode, op0, op1);
4573 else
4575 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4576 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4577 adj = simplify_gen_unary (NEG, mode,
4578 simplify_gen_binary (MULT, mode, adj, op1),
4579 mode);
4580 return simplify_gen_binary (PLUS, mode, mod, adj);
4583 case CEIL_DIV_EXPR:
4584 if (unsignedp)
4586 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4587 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4588 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4589 return simplify_gen_binary (PLUS, mode, div, adj);
4591 else
4593 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4594 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4595 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4596 return simplify_gen_binary (PLUS, mode, div, adj);
4599 case CEIL_MOD_EXPR:
4600 if (unsignedp)
4602 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4603 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4604 adj = simplify_gen_unary (NEG, mode,
4605 simplify_gen_binary (MULT, mode, adj, op1),
4606 mode);
4607 return simplify_gen_binary (PLUS, mode, mod, adj);
4609 else
4611 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4612 rtx adj = ceil_sdiv_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);
4619 case ROUND_DIV_EXPR:
4620 if (unsignedp)
4622 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4623 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4624 rtx adj = round_udiv_adjust (mode, mod, op1);
4625 return simplify_gen_binary (PLUS, mode, div, adj);
4627 else
4629 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4630 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4631 rtx adj = round_sdiv_adjust (mode, mod, op1);
4632 return simplify_gen_binary (PLUS, mode, div, adj);
4635 case ROUND_MOD_EXPR:
4636 if (unsignedp)
4638 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4639 rtx adj = round_udiv_adjust (mode, mod, op1);
4640 adj = simplify_gen_unary (NEG, mode,
4641 simplify_gen_binary (MULT, mode, adj, op1),
4642 mode);
4643 return simplify_gen_binary (PLUS, mode, mod, adj);
4645 else
4647 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4648 rtx adj = round_sdiv_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);
4655 case LSHIFT_EXPR:
4656 return simplify_gen_binary (ASHIFT, mode, op0, op1);
4658 case RSHIFT_EXPR:
4659 if (unsignedp)
4660 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
4661 else
4662 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
4664 case LROTATE_EXPR:
4665 return simplify_gen_binary (ROTATE, mode, op0, op1);
4667 case RROTATE_EXPR:
4668 return simplify_gen_binary (ROTATERT, mode, op0, op1);
4670 case MIN_EXPR:
4671 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
4673 case MAX_EXPR:
4674 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
4676 case BIT_AND_EXPR:
4677 case TRUTH_AND_EXPR:
4678 return simplify_gen_binary (AND, mode, op0, op1);
4680 case BIT_IOR_EXPR:
4681 case TRUTH_OR_EXPR:
4682 return simplify_gen_binary (IOR, mode, op0, op1);
4684 case BIT_XOR_EXPR:
4685 case TRUTH_XOR_EXPR:
4686 return simplify_gen_binary (XOR, mode, op0, op1);
4688 case TRUTH_ANDIF_EXPR:
4689 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
4691 case TRUTH_ORIF_EXPR:
4692 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
4694 case TRUTH_NOT_EXPR:
4695 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
4697 case LT_EXPR:
4698 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
4699 op0, op1);
4701 case LE_EXPR:
4702 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
4703 op0, op1);
4705 case GT_EXPR:
4706 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
4707 op0, op1);
4709 case GE_EXPR:
4710 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
4711 op0, op1);
4713 case EQ_EXPR:
4714 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
4716 case NE_EXPR:
4717 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
4719 case UNORDERED_EXPR:
4720 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
4722 case ORDERED_EXPR:
4723 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
4725 case UNLT_EXPR:
4726 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
4728 case UNLE_EXPR:
4729 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
4731 case UNGT_EXPR:
4732 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
4734 case UNGE_EXPR:
4735 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
4737 case UNEQ_EXPR:
4738 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
4740 case LTGT_EXPR:
4741 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
4743 case COND_EXPR:
4744 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
4746 case COMPLEX_EXPR:
4747 gcc_assert (COMPLEX_MODE_P (mode));
4748 if (GET_MODE (op0) == VOIDmode)
4749 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
4750 if (GET_MODE (op1) == VOIDmode)
4751 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
4752 return gen_rtx_CONCAT (mode, op0, op1);
4754 case CONJ_EXPR:
4755 if (GET_CODE (op0) == CONCAT)
4756 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
4757 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
4758 XEXP (op0, 1),
4759 GET_MODE_INNER (mode)));
4760 else
4762 machine_mode imode = GET_MODE_INNER (mode);
4763 rtx re, im;
4765 if (MEM_P (op0))
4767 re = adjust_address_nv (op0, imode, 0);
4768 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
4770 else
4772 machine_mode ifmode = int_mode_for_mode (mode);
4773 machine_mode ihmode = int_mode_for_mode (imode);
4774 rtx halfsize;
4775 if (ifmode == BLKmode || ihmode == BLKmode)
4776 return NULL;
4777 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
4778 re = op0;
4779 if (mode != ifmode)
4780 re = gen_rtx_SUBREG (ifmode, re, 0);
4781 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
4782 if (imode != ihmode)
4783 re = gen_rtx_SUBREG (imode, re, 0);
4784 im = copy_rtx (op0);
4785 if (mode != ifmode)
4786 im = gen_rtx_SUBREG (ifmode, im, 0);
4787 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
4788 if (imode != ihmode)
4789 im = gen_rtx_SUBREG (imode, im, 0);
4791 im = gen_rtx_NEG (imode, im);
4792 return gen_rtx_CONCAT (mode, re, im);
4795 case ADDR_EXPR:
4796 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4797 if (!op0 || !MEM_P (op0))
4799 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
4800 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
4801 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
4802 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
4803 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
4804 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
4806 if (handled_component_p (TREE_OPERAND (exp, 0)))
4808 HOST_WIDE_INT bitoffset, bitsize, maxsize;
4809 tree decl
4810 = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
4811 &bitoffset, &bitsize, &maxsize);
4812 if ((TREE_CODE (decl) == VAR_DECL
4813 || TREE_CODE (decl) == PARM_DECL
4814 || TREE_CODE (decl) == RESULT_DECL)
4815 && (!TREE_ADDRESSABLE (decl)
4816 || target_for_debug_bind (decl))
4817 && (bitoffset % BITS_PER_UNIT) == 0
4818 && bitsize > 0
4819 && bitsize == maxsize)
4821 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
4822 return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
4826 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
4827 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4828 == ADDR_EXPR)
4830 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4831 0));
4832 if (op0 != NULL
4833 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4834 || (GET_CODE (op0) == PLUS
4835 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
4836 && CONST_INT_P (XEXP (op0, 1)))))
4838 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4839 1));
4840 if (!op1 || !CONST_INT_P (op1))
4841 return NULL;
4843 return plus_constant (mode, op0, INTVAL (op1));
4847 return NULL;
4850 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
4851 op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
4853 return op0;
4855 case VECTOR_CST:
4857 unsigned i;
4859 op0 = gen_rtx_CONCATN
4860 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4862 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
4864 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
4865 if (!op1)
4866 return NULL;
4867 XVECEXP (op0, 0, i) = op1;
4870 return op0;
4873 case CONSTRUCTOR:
4874 if (TREE_CLOBBER_P (exp))
4875 return NULL;
4876 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
4878 unsigned i;
4879 tree val;
4881 op0 = gen_rtx_CONCATN
4882 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4884 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
4886 op1 = expand_debug_expr (val);
4887 if (!op1)
4888 return NULL;
4889 XVECEXP (op0, 0, i) = op1;
4892 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
4894 op1 = expand_debug_expr
4895 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
4897 if (!op1)
4898 return NULL;
4900 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
4901 XVECEXP (op0, 0, i) = op1;
4904 return op0;
4906 else
4907 goto flag_unsupported;
4909 case CALL_EXPR:
4910 /* ??? Maybe handle some builtins? */
4911 return NULL;
4913 case SSA_NAME:
4915 gimple *g = get_gimple_for_ssa_name (exp);
4916 if (g)
4918 tree t = NULL_TREE;
4919 if (deep_ter_debug_map)
4921 tree *slot = deep_ter_debug_map->get (exp);
4922 if (slot)
4923 t = *slot;
4925 if (t == NULL_TREE)
4926 t = gimple_assign_rhs_to_tree (g);
4927 op0 = expand_debug_expr (t);
4928 if (!op0)
4929 return NULL;
4931 else
4933 /* If this is a reference to an incoming value of
4934 parameter that is never used in the code or where the
4935 incoming value is never used in the code, use
4936 PARM_DECL's DECL_RTL if set. */
4937 if (SSA_NAME_IS_DEFAULT_DEF (exp)
4938 && SSA_NAME_VAR (exp)
4939 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL
4940 && has_zero_uses (exp))
4942 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
4943 if (op0)
4944 goto adjust_mode;
4945 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
4946 if (op0)
4947 goto adjust_mode;
4950 int part = var_to_partition (SA.map, exp);
4952 if (part == NO_PARTITION)
4953 return NULL;
4955 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
4957 op0 = copy_rtx (SA.partition_to_pseudo[part]);
4959 goto adjust_mode;
4962 case ERROR_MARK:
4963 return NULL;
4965 /* Vector stuff. For most of the codes we don't have rtl codes. */
4966 case REALIGN_LOAD_EXPR:
4967 case REDUC_MAX_EXPR:
4968 case REDUC_MIN_EXPR:
4969 case REDUC_PLUS_EXPR:
4970 case VEC_COND_EXPR:
4971 case VEC_PACK_FIX_TRUNC_EXPR:
4972 case VEC_PACK_SAT_EXPR:
4973 case VEC_PACK_TRUNC_EXPR:
4974 case VEC_UNPACK_FLOAT_HI_EXPR:
4975 case VEC_UNPACK_FLOAT_LO_EXPR:
4976 case VEC_UNPACK_HI_EXPR:
4977 case VEC_UNPACK_LO_EXPR:
4978 case VEC_WIDEN_MULT_HI_EXPR:
4979 case VEC_WIDEN_MULT_LO_EXPR:
4980 case VEC_WIDEN_MULT_EVEN_EXPR:
4981 case VEC_WIDEN_MULT_ODD_EXPR:
4982 case VEC_WIDEN_LSHIFT_HI_EXPR:
4983 case VEC_WIDEN_LSHIFT_LO_EXPR:
4984 case VEC_PERM_EXPR:
4985 return NULL;
4987 /* Misc codes. */
4988 case ADDR_SPACE_CONVERT_EXPR:
4989 case FIXED_CONVERT_EXPR:
4990 case OBJ_TYPE_REF:
4991 case WITH_SIZE_EXPR:
4992 return NULL;
4994 case DOT_PROD_EXPR:
4995 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4996 && SCALAR_INT_MODE_P (mode))
4999 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5000 0)))
5001 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
5002 inner_mode);
5004 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5005 1)))
5006 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
5007 inner_mode);
5008 op0 = simplify_gen_binary (MULT, mode, op0, op1);
5009 return simplify_gen_binary (PLUS, mode, op0, op2);
5011 return NULL;
5013 case WIDEN_MULT_EXPR:
5014 case WIDEN_MULT_PLUS_EXPR:
5015 case WIDEN_MULT_MINUS_EXPR:
5016 if (SCALAR_INT_MODE_P (GET_MODE (op0))
5017 && SCALAR_INT_MODE_P (mode))
5019 inner_mode = GET_MODE (op0);
5020 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
5021 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
5022 else
5023 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
5024 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
5025 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
5026 else
5027 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
5028 op0 = simplify_gen_binary (MULT, mode, op0, op1);
5029 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
5030 return op0;
5031 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
5032 return simplify_gen_binary (PLUS, mode, op0, op2);
5033 else
5034 return simplify_gen_binary (MINUS, mode, op2, op0);
5036 return NULL;
5038 case MULT_HIGHPART_EXPR:
5039 /* ??? Similar to the above. */
5040 return NULL;
5042 case WIDEN_SUM_EXPR:
5043 case WIDEN_LSHIFT_EXPR:
5044 if (SCALAR_INT_MODE_P (GET_MODE (op0))
5045 && SCALAR_INT_MODE_P (mode))
5048 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
5049 0)))
5050 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
5051 inner_mode);
5052 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
5053 ? ASHIFT : PLUS, mode, op0, op1);
5055 return NULL;
5057 case FMA_EXPR:
5058 return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
5060 default:
5061 flag_unsupported:
5062 #ifdef ENABLE_CHECKING
5063 debug_tree (exp);
5064 gcc_unreachable ();
5065 #else
5066 return NULL;
5067 #endif
5071 /* Return an RTX equivalent to the source bind value of the tree expression
5072 EXP. */
5074 static rtx
5075 expand_debug_source_expr (tree exp)
5077 rtx op0 = NULL_RTX;
5078 machine_mode mode = VOIDmode, inner_mode;
5080 switch (TREE_CODE (exp))
5082 case PARM_DECL:
5084 mode = DECL_MODE (exp);
5085 op0 = expand_debug_parm_decl (exp);
5086 if (op0)
5087 break;
5088 /* See if this isn't an argument that has been completely
5089 optimized out. */
5090 if (!DECL_RTL_SET_P (exp)
5091 && !DECL_INCOMING_RTL (exp)
5092 && DECL_ABSTRACT_ORIGIN (current_function_decl))
5094 tree aexp = DECL_ORIGIN (exp);
5095 if (DECL_CONTEXT (aexp)
5096 == DECL_ABSTRACT_ORIGIN (current_function_decl))
5098 vec<tree, va_gc> **debug_args;
5099 unsigned int ix;
5100 tree ddecl;
5101 debug_args = decl_debug_args_lookup (current_function_decl);
5102 if (debug_args != NULL)
5104 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
5105 ix += 2)
5106 if (ddecl == aexp)
5107 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
5111 break;
5113 default:
5114 break;
5117 if (op0 == NULL_RTX)
5118 return NULL_RTX;
5120 inner_mode = GET_MODE (op0);
5121 if (mode == inner_mode)
5122 return op0;
5124 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
5126 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
5127 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
5128 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
5129 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
5130 else
5131 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
5133 else if (FLOAT_MODE_P (mode))
5134 gcc_unreachable ();
5135 else if (FLOAT_MODE_P (inner_mode))
5137 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
5138 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
5139 else
5140 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
5142 else if (CONSTANT_P (op0)
5143 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
5144 op0 = lowpart_subreg (mode, op0, inner_mode);
5145 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
5146 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
5147 else
5148 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
5150 return op0;
5153 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
5154 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
5155 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
5157 static void
5158 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
5160 rtx exp = *exp_p;
5162 if (exp == NULL_RTX)
5163 return;
5165 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
5166 return;
5168 if (depth == 4)
5170 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
5171 rtx dval = make_debug_expr_from_rtl (exp);
5173 /* Emit a debug bind insn before INSN. */
5174 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
5175 DEBUG_EXPR_TREE_DECL (dval), exp,
5176 VAR_INIT_STATUS_INITIALIZED);
5178 emit_debug_insn_before (bind, insn);
5179 *exp_p = dval;
5180 return;
5183 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
5184 int i, j;
5185 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
5186 switch (*format_ptr++)
5188 case 'e':
5189 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
5190 break;
5192 case 'E':
5193 case 'V':
5194 for (j = 0; j < XVECLEN (exp, i); j++)
5195 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
5196 break;
5198 default:
5199 break;
5203 /* Expand the _LOCs in debug insns. We run this after expanding all
5204 regular insns, so that any variables referenced in the function
5205 will have their DECL_RTLs set. */
5207 static void
5208 expand_debug_locations (void)
5210 rtx_insn *insn;
5211 rtx_insn *last = get_last_insn ();
5212 int save_strict_alias = flag_strict_aliasing;
5214 /* New alias sets while setting up memory attributes cause
5215 -fcompare-debug failures, even though it doesn't bring about any
5216 codegen changes. */
5217 flag_strict_aliasing = 0;
5219 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5220 if (DEBUG_INSN_P (insn))
5222 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
5223 rtx val;
5224 rtx_insn *prev_insn, *insn2;
5225 machine_mode mode;
5227 if (value == NULL_TREE)
5228 val = NULL_RTX;
5229 else
5231 if (INSN_VAR_LOCATION_STATUS (insn)
5232 == VAR_INIT_STATUS_UNINITIALIZED)
5233 val = expand_debug_source_expr (value);
5234 /* The avoid_deep_ter_for_debug function inserts
5235 debug bind stmts after SSA_NAME definition, with the
5236 SSA_NAME as the whole bind location. Disable temporarily
5237 expansion of that SSA_NAME into the DEBUG_EXPR_DECL
5238 being defined in this DEBUG_INSN. */
5239 else if (deep_ter_debug_map && TREE_CODE (value) == SSA_NAME)
5241 tree *slot = deep_ter_debug_map->get (value);
5242 if (slot)
5244 if (*slot == INSN_VAR_LOCATION_DECL (insn))
5245 *slot = NULL_TREE;
5246 else
5247 slot = NULL;
5249 val = expand_debug_expr (value);
5250 if (slot)
5251 *slot = INSN_VAR_LOCATION_DECL (insn);
5253 else
5254 val = expand_debug_expr (value);
5255 gcc_assert (last == get_last_insn ());
5258 if (!val)
5259 val = gen_rtx_UNKNOWN_VAR_LOC ();
5260 else
5262 mode = GET_MODE (INSN_VAR_LOCATION (insn));
5264 gcc_assert (mode == GET_MODE (val)
5265 || (GET_MODE (val) == VOIDmode
5266 && (CONST_SCALAR_INT_P (val)
5267 || GET_CODE (val) == CONST_FIXED
5268 || GET_CODE (val) == LABEL_REF)));
5271 INSN_VAR_LOCATION_LOC (insn) = val;
5272 prev_insn = PREV_INSN (insn);
5273 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
5274 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
5277 flag_strict_aliasing = save_strict_alias;
5280 /* Performs swapping operands of commutative operations to expand
5281 the expensive one first. */
5283 static void
5284 reorder_operands (basic_block bb)
5286 unsigned int *lattice; /* Hold cost of each statement. */
5287 unsigned int i = 0, n = 0;
5288 gimple_stmt_iterator gsi;
5289 gimple_seq stmts;
5290 gimple *stmt;
5291 bool swap;
5292 tree op0, op1;
5293 ssa_op_iter iter;
5294 use_operand_p use_p;
5295 gimple *def0, *def1;
5297 /* Compute cost of each statement using estimate_num_insns. */
5298 stmts = bb_seq (bb);
5299 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5301 stmt = gsi_stmt (gsi);
5302 if (!is_gimple_debug (stmt))
5303 gimple_set_uid (stmt, n++);
5305 lattice = XNEWVEC (unsigned int, n);
5306 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5308 unsigned cost;
5309 stmt = gsi_stmt (gsi);
5310 if (is_gimple_debug (stmt))
5311 continue;
5312 cost = estimate_num_insns (stmt, &eni_size_weights);
5313 lattice[i] = cost;
5314 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
5316 tree use = USE_FROM_PTR (use_p);
5317 gimple *def_stmt;
5318 if (TREE_CODE (use) != SSA_NAME)
5319 continue;
5320 def_stmt = get_gimple_for_ssa_name (use);
5321 if (!def_stmt)
5322 continue;
5323 lattice[i] += lattice[gimple_uid (def_stmt)];
5325 i++;
5326 if (!is_gimple_assign (stmt)
5327 || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
5328 continue;
5329 op0 = gimple_op (stmt, 1);
5330 op1 = gimple_op (stmt, 2);
5331 if (TREE_CODE (op0) != SSA_NAME
5332 || TREE_CODE (op1) != SSA_NAME)
5333 continue;
5334 /* Swap operands if the second one is more expensive. */
5335 def0 = get_gimple_for_ssa_name (op0);
5336 def1 = get_gimple_for_ssa_name (op1);
5337 if (!def1)
5338 continue;
5339 swap = false;
5340 if (!def0 || lattice[gimple_uid (def1)] > lattice[gimple_uid (def0)])
5341 swap = true;
5342 if (swap)
5344 if (dump_file && (dump_flags & TDF_DETAILS))
5346 fprintf (dump_file, "Swap operands in stmt:\n");
5347 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5348 fprintf (dump_file, "Cost left opnd=%d, right opnd=%d\n",
5349 def0 ? lattice[gimple_uid (def0)] : 0,
5350 lattice[gimple_uid (def1)]);
5352 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
5353 gimple_assign_rhs2_ptr (stmt));
5356 XDELETE (lattice);
5359 /* Expand basic block BB from GIMPLE trees to RTL. */
5361 static basic_block
5362 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
5364 gimple_stmt_iterator gsi;
5365 gimple_seq stmts;
5366 gimple *stmt = NULL;
5367 rtx_note *note;
5368 rtx_insn *last;
5369 edge e;
5370 edge_iterator ei;
5372 if (dump_file)
5373 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
5374 bb->index);
5376 /* Note that since we are now transitioning from GIMPLE to RTL, we
5377 cannot use the gsi_*_bb() routines because they expect the basic
5378 block to be in GIMPLE, instead of RTL. Therefore, we need to
5379 access the BB sequence directly. */
5380 if (optimize)
5381 reorder_operands (bb);
5382 stmts = bb_seq (bb);
5383 bb->il.gimple.seq = NULL;
5384 bb->il.gimple.phi_nodes = NULL;
5385 rtl_profile_for_bb (bb);
5386 init_rtl_bb_info (bb);
5387 bb->flags |= BB_RTL;
5389 /* Remove the RETURN_EXPR if we may fall though to the exit
5390 instead. */
5391 gsi = gsi_last (stmts);
5392 if (!gsi_end_p (gsi)
5393 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
5395 greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
5397 gcc_assert (single_succ_p (bb));
5398 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
5400 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5401 && !gimple_return_retval (ret_stmt))
5403 gsi_remove (&gsi, false);
5404 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
5408 gsi = gsi_start (stmts);
5409 if (!gsi_end_p (gsi))
5411 stmt = gsi_stmt (gsi);
5412 if (gimple_code (stmt) != GIMPLE_LABEL)
5413 stmt = NULL;
5416 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
5418 if (stmt || elt)
5420 last = get_last_insn ();
5422 if (stmt)
5424 expand_gimple_stmt (stmt);
5425 gsi_next (&gsi);
5428 if (elt)
5429 emit_label (*elt);
5431 /* Java emits line number notes in the top of labels.
5432 ??? Make this go away once line number notes are obsoleted. */
5433 BB_HEAD (bb) = NEXT_INSN (last);
5434 if (NOTE_P (BB_HEAD (bb)))
5435 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
5436 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
5438 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5440 else
5441 BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
5443 NOTE_BASIC_BLOCK (note) = bb;
5445 for (; !gsi_end_p (gsi); gsi_next (&gsi))
5447 basic_block new_bb;
5449 stmt = gsi_stmt (gsi);
5451 /* If this statement is a non-debug one, and we generate debug
5452 insns, then this one might be the last real use of a TERed
5453 SSA_NAME, but where there are still some debug uses further
5454 down. Expanding the current SSA name in such further debug
5455 uses by their RHS might lead to wrong debug info, as coalescing
5456 might make the operands of such RHS be placed into the same
5457 pseudo as something else. Like so:
5458 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
5459 use(a_1);
5460 a_2 = ...
5461 #DEBUG ... => a_1
5462 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
5463 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
5464 the write to a_2 would actually have clobbered the place which
5465 formerly held a_0.
5467 So, instead of that, we recognize the situation, and generate
5468 debug temporaries at the last real use of TERed SSA names:
5469 a_1 = a_0 + 1;
5470 #DEBUG #D1 => a_1
5471 use(a_1);
5472 a_2 = ...
5473 #DEBUG ... => #D1
5475 if (MAY_HAVE_DEBUG_INSNS
5476 && SA.values
5477 && !is_gimple_debug (stmt))
5479 ssa_op_iter iter;
5480 tree op;
5481 gimple *def;
5483 location_t sloc = curr_insn_location ();
5485 /* Look for SSA names that have their last use here (TERed
5486 names always have only one real use). */
5487 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5488 if ((def = get_gimple_for_ssa_name (op)))
5490 imm_use_iterator imm_iter;
5491 use_operand_p use_p;
5492 bool have_debug_uses = false;
5494 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5496 if (gimple_debug_bind_p (USE_STMT (use_p)))
5498 have_debug_uses = true;
5499 break;
5503 if (have_debug_uses)
5505 /* OP is a TERed SSA name, with DEF its defining
5506 statement, and where OP is used in further debug
5507 instructions. Generate a debug temporary, and
5508 replace all uses of OP in debug insns with that
5509 temporary. */
5510 gimple *debugstmt;
5511 tree value = gimple_assign_rhs_to_tree (def);
5512 tree vexpr = make_node (DEBUG_EXPR_DECL);
5513 rtx val;
5514 machine_mode mode;
5516 set_curr_insn_location (gimple_location (def));
5518 DECL_ARTIFICIAL (vexpr) = 1;
5519 TREE_TYPE (vexpr) = TREE_TYPE (value);
5520 if (DECL_P (value))
5521 mode = DECL_MODE (value);
5522 else
5523 mode = TYPE_MODE (TREE_TYPE (value));
5524 DECL_MODE (vexpr) = mode;
5526 val = gen_rtx_VAR_LOCATION
5527 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5529 emit_debug_insn (val);
5531 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5533 if (!gimple_debug_bind_p (debugstmt))
5534 continue;
5536 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5537 SET_USE (use_p, vexpr);
5539 update_stmt (debugstmt);
5543 set_curr_insn_location (sloc);
5546 currently_expanding_gimple_stmt = stmt;
5548 /* Expand this statement, then evaluate the resulting RTL and
5549 fixup the CFG accordingly. */
5550 if (gimple_code (stmt) == GIMPLE_COND)
5552 new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
5553 if (new_bb)
5554 return new_bb;
5556 else if (gimple_debug_bind_p (stmt))
5558 location_t sloc = curr_insn_location ();
5559 gimple_stmt_iterator nsi = gsi;
5561 for (;;)
5563 tree var = gimple_debug_bind_get_var (stmt);
5564 tree value;
5565 rtx val;
5566 machine_mode mode;
5568 if (TREE_CODE (var) != DEBUG_EXPR_DECL
5569 && TREE_CODE (var) != LABEL_DECL
5570 && !target_for_debug_bind (var))
5571 goto delink_debug_stmt;
5573 if (gimple_debug_bind_has_value_p (stmt))
5574 value = gimple_debug_bind_get_value (stmt);
5575 else
5576 value = NULL_TREE;
5578 last = get_last_insn ();
5580 set_curr_insn_location (gimple_location (stmt));
5582 if (DECL_P (var))
5583 mode = DECL_MODE (var);
5584 else
5585 mode = TYPE_MODE (TREE_TYPE (var));
5587 val = gen_rtx_VAR_LOCATION
5588 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5590 emit_debug_insn (val);
5592 if (dump_file && (dump_flags & TDF_DETAILS))
5594 /* We can't dump the insn with a TREE where an RTX
5595 is expected. */
5596 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5597 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5598 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5601 delink_debug_stmt:
5602 /* In order not to generate too many debug temporaries,
5603 we delink all uses of debug statements we already expanded.
5604 Therefore debug statements between definition and real
5605 use of TERed SSA names will continue to use the SSA name,
5606 and not be replaced with debug temps. */
5607 delink_stmt_imm_use (stmt);
5609 gsi = nsi;
5610 gsi_next (&nsi);
5611 if (gsi_end_p (nsi))
5612 break;
5613 stmt = gsi_stmt (nsi);
5614 if (!gimple_debug_bind_p (stmt))
5615 break;
5618 set_curr_insn_location (sloc);
5620 else if (gimple_debug_source_bind_p (stmt))
5622 location_t sloc = curr_insn_location ();
5623 tree var = gimple_debug_source_bind_get_var (stmt);
5624 tree value = gimple_debug_source_bind_get_value (stmt);
5625 rtx val;
5626 machine_mode mode;
5628 last = get_last_insn ();
5630 set_curr_insn_location (gimple_location (stmt));
5632 mode = DECL_MODE (var);
5634 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
5635 VAR_INIT_STATUS_UNINITIALIZED);
5637 emit_debug_insn (val);
5639 if (dump_file && (dump_flags & TDF_DETAILS))
5641 /* We can't dump the insn with a TREE where an RTX
5642 is expected. */
5643 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5644 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5645 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5648 set_curr_insn_location (sloc);
5650 else
5652 gcall *call_stmt = dyn_cast <gcall *> (stmt);
5653 if (call_stmt
5654 && gimple_call_tail_p (call_stmt)
5655 && disable_tail_calls)
5656 gimple_call_set_tail (call_stmt, false);
5658 if (call_stmt && gimple_call_tail_p (call_stmt))
5660 bool can_fallthru;
5661 new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
5662 if (new_bb)
5664 if (can_fallthru)
5665 bb = new_bb;
5666 else
5667 return new_bb;
5670 else
5672 def_operand_p def_p;
5673 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
5675 if (def_p != NULL)
5677 /* Ignore this stmt if it is in the list of
5678 replaceable expressions. */
5679 if (SA.values
5680 && bitmap_bit_p (SA.values,
5681 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
5682 continue;
5684 last = expand_gimple_stmt (stmt);
5685 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5690 currently_expanding_gimple_stmt = NULL;
5692 /* Expand implicit goto and convert goto_locus. */
5693 FOR_EACH_EDGE (e, ei, bb->succs)
5695 if (e->goto_locus != UNKNOWN_LOCATION)
5696 set_curr_insn_location (e->goto_locus);
5697 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
5699 emit_jump (label_rtx_for_bb (e->dest));
5700 e->flags &= ~EDGE_FALLTHRU;
5704 /* Expanded RTL can create a jump in the last instruction of block.
5705 This later might be assumed to be a jump to successor and break edge insertion.
5706 We need to insert dummy move to prevent this. PR41440. */
5707 if (single_succ_p (bb)
5708 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
5709 && (last = get_last_insn ())
5710 && JUMP_P (last))
5712 rtx dummy = gen_reg_rtx (SImode);
5713 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
5716 do_pending_stack_adjust ();
5718 /* Find the block tail. The last insn in the block is the insn
5719 before a barrier and/or table jump insn. */
5720 last = get_last_insn ();
5721 if (BARRIER_P (last))
5722 last = PREV_INSN (last);
5723 if (JUMP_TABLE_DATA_P (last))
5724 last = PREV_INSN (PREV_INSN (last));
5725 BB_END (bb) = last;
5727 update_bb_for_insn (bb);
5729 return bb;
5733 /* Create a basic block for initialization code. */
5735 static basic_block
5736 construct_init_block (void)
5738 basic_block init_block, first_block;
5739 edge e = NULL;
5740 int flags;
5742 /* Multiple entry points not supported yet. */
5743 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
5744 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5745 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
5746 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5747 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5749 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
5751 /* When entry edge points to first basic block, we don't need jump,
5752 otherwise we have to jump into proper target. */
5753 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
5755 tree label = gimple_block_label (e->dest);
5757 emit_jump (jump_target_rtx (label));
5758 flags = 0;
5760 else
5761 flags = EDGE_FALLTHRU;
5763 init_block = create_basic_block (NEXT_INSN (get_insns ()),
5764 get_last_insn (),
5765 ENTRY_BLOCK_PTR_FOR_FN (cfun));
5766 init_block->frequency = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
5767 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5768 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5769 if (e)
5771 first_block = e->dest;
5772 redirect_edge_succ (e, init_block);
5773 e = make_edge (init_block, first_block, flags);
5775 else
5776 e = make_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5777 e->probability = REG_BR_PROB_BASE;
5778 e->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5780 update_bb_for_insn (init_block);
5781 return init_block;
5784 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
5785 found in the block tree. */
5787 static void
5788 set_block_levels (tree block, int level)
5790 while (block)
5792 BLOCK_NUMBER (block) = level;
5793 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
5794 block = BLOCK_CHAIN (block);
5798 /* Create a block containing landing pads and similar stuff. */
5800 static void
5801 construct_exit_block (void)
5803 rtx_insn *head = get_last_insn ();
5804 rtx_insn *end;
5805 basic_block exit_block;
5806 edge e, e2;
5807 unsigned ix;
5808 edge_iterator ei;
5809 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
5810 rtx_insn *orig_end = BB_END (prev_bb);
5812 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5814 /* Make sure the locus is set to the end of the function, so that
5815 epilogue line numbers and warnings are set properly. */
5816 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
5817 input_location = cfun->function_end_locus;
5819 /* Generate rtl for function exit. */
5820 expand_function_end ();
5822 end = get_last_insn ();
5823 if (head == end)
5824 return;
5825 /* While emitting the function end we could move end of the last basic
5826 block. */
5827 BB_END (prev_bb) = orig_end;
5828 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
5829 head = NEXT_INSN (head);
5830 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
5831 bb frequency counting will be confused. Any instructions before that
5832 label are emitted for the case where PREV_BB falls through into the
5833 exit block, so append those instructions to prev_bb in that case. */
5834 if (NEXT_INSN (head) != return_label)
5836 while (NEXT_INSN (head) != return_label)
5838 if (!NOTE_P (NEXT_INSN (head)))
5839 BB_END (prev_bb) = NEXT_INSN (head);
5840 head = NEXT_INSN (head);
5843 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
5844 exit_block->frequency = EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency;
5845 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5846 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5848 ix = 0;
5849 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
5851 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
5852 if (!(e->flags & EDGE_ABNORMAL))
5853 redirect_edge_succ (e, exit_block);
5854 else
5855 ix++;
5858 e = make_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5859 e->probability = REG_BR_PROB_BASE;
5860 e->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5861 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5862 if (e2 != e)
5864 e->count -= e2->count;
5865 exit_block->count -= e2->count;
5866 exit_block->frequency -= EDGE_FREQUENCY (e2);
5868 if (e->count < 0)
5869 e->count = 0;
5870 if (exit_block->count < 0)
5871 exit_block->count = 0;
5872 if (exit_block->frequency < 0)
5873 exit_block->frequency = 0;
5874 update_bb_for_insn (exit_block);
5877 /* Helper function for discover_nonconstant_array_refs.
5878 Look for ARRAY_REF nodes with non-constant indexes and mark them
5879 addressable. */
5881 static tree
5882 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
5883 void *data ATTRIBUTE_UNUSED)
5885 tree t = *tp;
5887 if (IS_TYPE_OR_DECL_P (t))
5888 *walk_subtrees = 0;
5889 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5891 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5892 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
5893 && (!TREE_OPERAND (t, 2)
5894 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5895 || (TREE_CODE (t) == COMPONENT_REF
5896 && (!TREE_OPERAND (t,2)
5897 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5898 || TREE_CODE (t) == BIT_FIELD_REF
5899 || TREE_CODE (t) == REALPART_EXPR
5900 || TREE_CODE (t) == IMAGPART_EXPR
5901 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5902 || CONVERT_EXPR_P (t))
5903 t = TREE_OPERAND (t, 0);
5905 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5907 t = get_base_address (t);
5908 if (t && DECL_P (t)
5909 && DECL_MODE (t) != BLKmode)
5910 TREE_ADDRESSABLE (t) = 1;
5913 *walk_subtrees = 0;
5916 return NULL_TREE;
5919 /* RTL expansion is not able to compile array references with variable
5920 offsets for arrays stored in single register. Discover such
5921 expressions and mark variables as addressable to avoid this
5922 scenario. */
5924 static void
5925 discover_nonconstant_array_refs (void)
5927 basic_block bb;
5928 gimple_stmt_iterator gsi;
5930 FOR_EACH_BB_FN (bb, cfun)
5931 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5933 gimple *stmt = gsi_stmt (gsi);
5934 if (!is_gimple_debug (stmt))
5935 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
5939 /* This function sets crtl->args.internal_arg_pointer to a virtual
5940 register if DRAP is needed. Local register allocator will replace
5941 virtual_incoming_args_rtx with the virtual register. */
5943 static void
5944 expand_stack_alignment (void)
5946 rtx drap_rtx;
5947 unsigned int preferred_stack_boundary;
5949 if (! SUPPORTS_STACK_ALIGNMENT)
5950 return;
5952 if (cfun->calls_alloca
5953 || cfun->has_nonlocal_label
5954 || crtl->has_nonlocal_goto)
5955 crtl->need_drap = true;
5957 /* Call update_stack_boundary here again to update incoming stack
5958 boundary. It may set incoming stack alignment to a different
5959 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
5960 use the minimum incoming stack alignment to check if it is OK
5961 to perform sibcall optimization since sibcall optimization will
5962 only align the outgoing stack to incoming stack boundary. */
5963 if (targetm.calls.update_stack_boundary)
5964 targetm.calls.update_stack_boundary ();
5966 /* The incoming stack frame has to be aligned at least at
5967 parm_stack_boundary. */
5968 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
5970 /* Update crtl->stack_alignment_estimated and use it later to align
5971 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
5972 exceptions since callgraph doesn't collect incoming stack alignment
5973 in this case. */
5974 if (cfun->can_throw_non_call_exceptions
5975 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
5976 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
5977 else
5978 preferred_stack_boundary = crtl->preferred_stack_boundary;
5979 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
5980 crtl->stack_alignment_estimated = preferred_stack_boundary;
5981 if (preferred_stack_boundary > crtl->stack_alignment_needed)
5982 crtl->stack_alignment_needed = preferred_stack_boundary;
5984 gcc_assert (crtl->stack_alignment_needed
5985 <= crtl->stack_alignment_estimated);
5987 crtl->stack_realign_needed
5988 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
5989 crtl->stack_realign_tried = crtl->stack_realign_needed;
5991 crtl->stack_realign_processed = true;
5993 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
5994 alignment. */
5995 gcc_assert (targetm.calls.get_drap_rtx != NULL);
5996 drap_rtx = targetm.calls.get_drap_rtx ();
5998 /* stack_realign_drap and drap_rtx must match. */
5999 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
6001 /* Do nothing if NULL is returned, which means DRAP is not needed. */
6002 if (NULL != drap_rtx)
6004 crtl->args.internal_arg_pointer = drap_rtx;
6006 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
6007 needed. */
6008 fixup_tail_calls ();
6013 static void
6014 expand_main_function (void)
6016 #if (defined(INVOKE__main) \
6017 || (!defined(HAS_INIT_SECTION) \
6018 && !defined(INIT_SECTION_ASM_OP) \
6019 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
6020 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
6021 #endif
6025 /* Expand code to initialize the stack_protect_guard. This is invoked at
6026 the beginning of a function to be protected. */
6028 static void
6029 stack_protect_prologue (void)
6031 tree guard_decl = targetm.stack_protect_guard ();
6032 rtx x, y;
6034 x = expand_normal (crtl->stack_protect_guard);
6035 y = expand_normal (guard_decl);
6037 /* Allow the target to copy from Y to X without leaking Y into a
6038 register. */
6039 if (targetm.have_stack_protect_set ())
6040 if (rtx_insn *insn = targetm.gen_stack_protect_set (x, y))
6042 emit_insn (insn);
6043 return;
6046 /* Otherwise do a straight move. */
6047 emit_move_insn (x, y);
6050 /* Translate the intermediate representation contained in the CFG
6051 from GIMPLE trees to RTL.
6053 We do conversion per basic block and preserve/update the tree CFG.
6054 This implies we have to do some magic as the CFG can simultaneously
6055 consist of basic blocks containing RTL and GIMPLE trees. This can
6056 confuse the CFG hooks, so be careful to not manipulate CFG during
6057 the expansion. */
6059 namespace {
6061 const pass_data pass_data_expand =
6063 RTL_PASS, /* type */
6064 "expand", /* name */
6065 OPTGROUP_NONE, /* optinfo_flags */
6066 TV_EXPAND, /* tv_id */
6067 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
6068 | PROP_gimple_lcx
6069 | PROP_gimple_lvec
6070 | PROP_gimple_lva), /* properties_required */
6071 PROP_rtl, /* properties_provided */
6072 ( PROP_ssa | PROP_trees ), /* properties_destroyed */
6073 0, /* todo_flags_start */
6074 0, /* todo_flags_finish */
6077 class pass_expand : public rtl_opt_pass
6079 public:
6080 pass_expand (gcc::context *ctxt)
6081 : rtl_opt_pass (pass_data_expand, ctxt)
6084 /* opt_pass methods: */
6085 virtual unsigned int execute (function *);
6087 }; // class pass_expand
6089 unsigned int
6090 pass_expand::execute (function *fun)
6092 basic_block bb, init_block;
6093 sbitmap blocks;
6094 edge_iterator ei;
6095 edge e;
6096 rtx_insn *var_seq, *var_ret_seq;
6097 unsigned i;
6099 timevar_push (TV_OUT_OF_SSA);
6100 rewrite_out_of_ssa (&SA);
6101 timevar_pop (TV_OUT_OF_SSA);
6102 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
6104 if (MAY_HAVE_DEBUG_STMTS && flag_tree_ter)
6106 gimple_stmt_iterator gsi;
6107 FOR_EACH_BB_FN (bb, cfun)
6108 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6109 if (gimple_debug_bind_p (gsi_stmt (gsi)))
6110 avoid_deep_ter_for_debug (gsi_stmt (gsi), 0);
6113 /* Make sure all values used by the optimization passes have sane
6114 defaults. */
6115 reg_renumber = 0;
6117 /* Some backends want to know that we are expanding to RTL. */
6118 currently_expanding_to_rtl = 1;
6119 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
6120 free_dominance_info (CDI_DOMINATORS);
6122 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
6124 if (chkp_function_instrumented_p (current_function_decl))
6125 chkp_reset_rtl_bounds ();
6127 insn_locations_init ();
6128 if (!DECL_IS_BUILTIN (current_function_decl))
6130 /* Eventually, all FEs should explicitly set function_start_locus. */
6131 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
6132 set_curr_insn_location
6133 (DECL_SOURCE_LOCATION (current_function_decl));
6134 else
6135 set_curr_insn_location (fun->function_start_locus);
6137 else
6138 set_curr_insn_location (UNKNOWN_LOCATION);
6139 prologue_location = curr_insn_location ();
6141 #ifdef INSN_SCHEDULING
6142 init_sched_attrs ();
6143 #endif
6145 /* Make sure first insn is a note even if we don't want linenums.
6146 This makes sure the first insn will never be deleted.
6147 Also, final expects a note to appear there. */
6148 emit_note (NOTE_INSN_DELETED);
6150 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
6151 discover_nonconstant_array_refs ();
6153 targetm.expand_to_rtl_hook ();
6154 crtl->stack_alignment_needed = STACK_BOUNDARY;
6155 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
6156 crtl->stack_alignment_estimated = 0;
6157 crtl->preferred_stack_boundary = STACK_BOUNDARY;
6158 fun->cfg->max_jumptable_ents = 0;
6160 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
6161 of the function section at exapnsion time to predict distance of calls. */
6162 resolve_unique_section (current_function_decl, 0, flag_function_sections);
6164 /* Expand the variables recorded during gimple lowering. */
6165 timevar_push (TV_VAR_EXPAND);
6166 start_sequence ();
6168 var_ret_seq = expand_used_vars ();
6170 var_seq = get_insns ();
6171 end_sequence ();
6172 timevar_pop (TV_VAR_EXPAND);
6174 /* Honor stack protection warnings. */
6175 if (warn_stack_protect)
6177 if (fun->calls_alloca)
6178 warning (OPT_Wstack_protector,
6179 "stack protector not protecting local variables: "
6180 "variable length buffer");
6181 if (has_short_buffer && !crtl->stack_protect_guard)
6182 warning (OPT_Wstack_protector,
6183 "stack protector not protecting function: "
6184 "all local arrays are less than %d bytes long",
6185 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
6188 /* Set up parameters and prepare for return, for the function. */
6189 expand_function_start (current_function_decl);
6191 /* If we emitted any instructions for setting up the variables,
6192 emit them before the FUNCTION_START note. */
6193 if (var_seq)
6195 emit_insn_before (var_seq, parm_birth_insn);
6197 /* In expand_function_end we'll insert the alloca save/restore
6198 before parm_birth_insn. We've just insertted an alloca call.
6199 Adjust the pointer to match. */
6200 parm_birth_insn = var_seq;
6203 /* Now propagate the RTL assignment of each partition to the
6204 underlying var of each SSA_NAME. */
6205 for (i = 1; i < num_ssa_names; i++)
6207 tree name = ssa_name (i);
6209 if (!name
6210 /* We might have generated new SSA names in
6211 update_alias_info_with_stack_vars. They will have a NULL
6212 defining statements, and won't be part of the partitioning,
6213 so ignore those. */
6214 || !SSA_NAME_DEF_STMT (name))
6215 continue;
6217 adjust_one_expanded_partition_var (name);
6220 /* Clean up RTL of variables that straddle across multiple
6221 partitions, and check that the rtl of any PARM_DECLs that are not
6222 cleaned up is that of their default defs. */
6223 for (i = 1; i < num_ssa_names; i++)
6225 tree name = ssa_name (i);
6226 int part;
6228 if (!name
6229 /* We might have generated new SSA names in
6230 update_alias_info_with_stack_vars. They will have a NULL
6231 defining statements, and won't be part of the partitioning,
6232 so ignore those. */
6233 || !SSA_NAME_DEF_STMT (name))
6234 continue;
6235 part = var_to_partition (SA.map, name);
6236 if (part == NO_PARTITION)
6237 continue;
6239 /* If this decl was marked as living in multiple places, reset
6240 this now to NULL. */
6241 tree var = SSA_NAME_VAR (name);
6242 if (var && DECL_RTL_IF_SET (var) == pc_rtx)
6243 SET_DECL_RTL (var, NULL);
6244 /* Check that the pseudos chosen by assign_parms are those of
6245 the corresponding default defs. */
6246 else if (SSA_NAME_IS_DEFAULT_DEF (name)
6247 && (TREE_CODE (var) == PARM_DECL
6248 || TREE_CODE (var) == RESULT_DECL))
6250 rtx in = DECL_RTL_IF_SET (var);
6251 gcc_assert (in);
6252 rtx out = SA.partition_to_pseudo[part];
6253 gcc_assert (in == out);
6255 /* Now reset VAR's RTL to IN, so that the _EXPR attrs match
6256 those expected by debug backends for each parm and for
6257 the result. This is particularly important for stabs,
6258 whose register elimination from parm's DECL_RTL may cause
6259 -fcompare-debug differences as SET_DECL_RTL changes reg's
6260 attrs. So, make sure the RTL already has the parm as the
6261 EXPR, so that it won't change. */
6262 SET_DECL_RTL (var, NULL_RTX);
6263 if (MEM_P (in))
6264 set_mem_attributes (in, var, true);
6265 SET_DECL_RTL (var, in);
6269 /* If this function is `main', emit a call to `__main'
6270 to run global initializers, etc. */
6271 if (DECL_NAME (current_function_decl)
6272 && MAIN_NAME_P (DECL_NAME (current_function_decl))
6273 && DECL_FILE_SCOPE_P (current_function_decl))
6274 expand_main_function ();
6276 /* Initialize the stack_protect_guard field. This must happen after the
6277 call to __main (if any) so that the external decl is initialized. */
6278 if (crtl->stack_protect_guard)
6279 stack_protect_prologue ();
6281 expand_phi_nodes (&SA);
6283 /* Register rtl specific functions for cfg. */
6284 rtl_register_cfg_hooks ();
6286 init_block = construct_init_block ();
6288 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
6289 remaining edges later. */
6290 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
6291 e->flags &= ~EDGE_EXECUTABLE;
6293 lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
6294 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
6295 next_bb)
6296 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
6298 if (MAY_HAVE_DEBUG_INSNS)
6299 expand_debug_locations ();
6301 if (deep_ter_debug_map)
6303 delete deep_ter_debug_map;
6304 deep_ter_debug_map = NULL;
6307 /* Free stuff we no longer need after GIMPLE optimizations. */
6308 free_dominance_info (CDI_DOMINATORS);
6309 free_dominance_info (CDI_POST_DOMINATORS);
6310 delete_tree_cfg_annotations ();
6312 timevar_push (TV_OUT_OF_SSA);
6313 finish_out_of_ssa (&SA);
6314 timevar_pop (TV_OUT_OF_SSA);
6316 timevar_push (TV_POST_EXPAND);
6317 /* We are no longer in SSA form. */
6318 fun->gimple_df->in_ssa_p = false;
6319 loops_state_clear (LOOP_CLOSED_SSA);
6321 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
6322 conservatively to true until they are all profile aware. */
6323 delete lab_rtx_for_bb;
6324 free_histograms ();
6326 construct_exit_block ();
6327 insn_locations_finalize ();
6329 if (var_ret_seq)
6331 rtx_insn *after = return_label;
6332 rtx_insn *next = NEXT_INSN (after);
6333 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
6334 after = next;
6335 emit_insn_after (var_ret_seq, after);
6338 /* Zap the tree EH table. */
6339 set_eh_throw_stmt_table (fun, NULL);
6341 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
6342 split edges which edge insertions might do. */
6343 rebuild_jump_labels (get_insns ());
6345 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun),
6346 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6348 edge e;
6349 edge_iterator ei;
6350 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6352 if (e->insns.r)
6354 rebuild_jump_labels_chain (e->insns.r);
6355 /* Put insns after parm birth, but before
6356 NOTE_INSNS_FUNCTION_BEG. */
6357 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (fun)
6358 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
6360 rtx_insn *insns = e->insns.r;
6361 e->insns.r = NULL;
6362 if (NOTE_P (parm_birth_insn)
6363 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
6364 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
6365 else
6366 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
6368 else
6369 commit_one_edge_insertion (e);
6371 else
6372 ei_next (&ei);
6376 /* We're done expanding trees to RTL. */
6377 currently_expanding_to_rtl = 0;
6379 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
6380 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6382 edge e;
6383 edge_iterator ei;
6384 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6386 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
6387 e->flags &= ~EDGE_EXECUTABLE;
6389 /* At the moment not all abnormal edges match the RTL
6390 representation. It is safe to remove them here as
6391 find_many_sub_basic_blocks will rediscover them.
6392 In the future we should get this fixed properly. */
6393 if ((e->flags & EDGE_ABNORMAL)
6394 && !(e->flags & EDGE_SIBCALL))
6395 remove_edge (e);
6396 else
6397 ei_next (&ei);
6401 blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
6402 bitmap_ones (blocks);
6403 find_many_sub_basic_blocks (blocks);
6404 sbitmap_free (blocks);
6405 purge_all_dead_edges ();
6407 expand_stack_alignment ();
6409 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
6410 function. */
6411 if (crtl->tail_call_emit)
6412 fixup_tail_calls ();
6414 /* After initial rtl generation, call back to finish generating
6415 exception support code. We need to do this before cleaning up
6416 the CFG as the code does not expect dead landing pads. */
6417 if (fun->eh->region_tree != NULL)
6418 finish_eh_generation ();
6420 /* Remove unreachable blocks, otherwise we cannot compute dominators
6421 which are needed for loop state verification. As a side-effect
6422 this also compacts blocks.
6423 ??? We cannot remove trivially dead insns here as for example
6424 the DRAP reg on i?86 is not magically live at this point.
6425 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
6426 cleanup_cfg (CLEANUP_NO_INSN_DEL);
6428 #ifdef ENABLE_CHECKING
6429 verify_flow_info ();
6430 #endif
6432 /* Initialize pseudos allocated for hard registers. */
6433 emit_initial_value_sets ();
6435 /* And finally unshare all RTL. */
6436 unshare_all_rtl ();
6438 /* There's no need to defer outputting this function any more; we
6439 know we want to output it. */
6440 DECL_DEFER_OUTPUT (current_function_decl) = 0;
6442 /* Now that we're done expanding trees to RTL, we shouldn't have any
6443 more CONCATs anywhere. */
6444 generating_concat_p = 0;
6446 if (dump_file)
6448 fprintf (dump_file,
6449 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
6450 /* And the pass manager will dump RTL for us. */
6453 /* If we're emitting a nested function, make sure its parent gets
6454 emitted as well. Doing otherwise confuses debug info. */
6456 tree parent;
6457 for (parent = DECL_CONTEXT (current_function_decl);
6458 parent != NULL_TREE;
6459 parent = get_containing_scope (parent))
6460 if (TREE_CODE (parent) == FUNCTION_DECL)
6461 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
6464 /* We are now committed to emitting code for this function. Do any
6465 preparation, such as emitting abstract debug info for the inline
6466 before it gets mangled by optimization. */
6467 if (cgraph_function_possibly_inlined_p (current_function_decl))
6468 (*debug_hooks->outlining_inline_function) (current_function_decl);
6470 TREE_ASM_WRITTEN (current_function_decl) = 1;
6472 /* After expanding, the return labels are no longer needed. */
6473 return_label = NULL;
6474 naked_return_label = NULL;
6476 /* After expanding, the tm_restart map is no longer needed. */
6477 if (fun->gimple_df->tm_restart)
6478 fun->gimple_df->tm_restart = NULL;
6480 /* Tag the blocks with a depth number so that change_scope can find
6481 the common parent easily. */
6482 set_block_levels (DECL_INITIAL (fun->decl), 0);
6483 default_rtl_profile ();
6485 timevar_pop (TV_POST_EXPAND);
6487 return 0;
6490 } // anon namespace
6492 rtl_opt_pass *
6493 make_pass_expand (gcc::context *ctxt)
6495 return new pass_expand (ctxt);