2015-08-04 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / cfgexpand.c
blob7df9d06fab33d0be288b226aa412f250853fbd44
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 /* Return an expression tree corresponding to the RHS of GIMPLE
101 statement STMT. */
103 tree
104 gimple_assign_rhs_to_tree (gimple stmt)
106 tree t;
107 enum gimple_rhs_class grhs_class;
109 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
111 if (grhs_class == GIMPLE_TERNARY_RHS)
112 t = build3 (gimple_assign_rhs_code (stmt),
113 TREE_TYPE (gimple_assign_lhs (stmt)),
114 gimple_assign_rhs1 (stmt),
115 gimple_assign_rhs2 (stmt),
116 gimple_assign_rhs3 (stmt));
117 else if (grhs_class == GIMPLE_BINARY_RHS)
118 t = build2 (gimple_assign_rhs_code (stmt),
119 TREE_TYPE (gimple_assign_lhs (stmt)),
120 gimple_assign_rhs1 (stmt),
121 gimple_assign_rhs2 (stmt));
122 else if (grhs_class == GIMPLE_UNARY_RHS)
123 t = build1 (gimple_assign_rhs_code (stmt),
124 TREE_TYPE (gimple_assign_lhs (stmt)),
125 gimple_assign_rhs1 (stmt));
126 else if (grhs_class == GIMPLE_SINGLE_RHS)
128 t = gimple_assign_rhs1 (stmt);
129 /* Avoid modifying this tree in place below. */
130 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
131 && gimple_location (stmt) != EXPR_LOCATION (t))
132 || (gimple_block (stmt)
133 && currently_expanding_to_rtl
134 && EXPR_P (t)))
135 t = copy_node (t);
137 else
138 gcc_unreachable ();
140 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
141 SET_EXPR_LOCATION (t, gimple_location (stmt));
143 return t;
147 #ifndef STACK_ALIGNMENT_NEEDED
148 #define STACK_ALIGNMENT_NEEDED 1
149 #endif
151 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
153 /* Associate declaration T with storage space X. If T is no
154 SSA name this is exactly SET_DECL_RTL, otherwise make the
155 partition of T associated with X. */
156 static inline void
157 set_rtl (tree t, rtx x)
159 if (TREE_CODE (t) == SSA_NAME)
161 SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
162 if (x && !MEM_P (x))
163 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
164 /* For the benefit of debug information at -O0 (where vartracking
165 doesn't run) record the place also in the base DECL if it's
166 a normal variable (not a parameter). */
167 if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
169 tree var = SSA_NAME_VAR (t);
170 /* If we don't yet have something recorded, just record it now. */
171 if (!DECL_RTL_SET_P (var))
172 SET_DECL_RTL (var, x);
173 /* If we have it set already to "multiple places" don't
174 change this. */
175 else if (DECL_RTL (var) == pc_rtx)
177 /* If we have something recorded and it's not the same place
178 as we want to record now, we have multiple partitions for the
179 same base variable, with different places. We can't just
180 randomly chose one, hence we have to say that we don't know.
181 This only happens with optimization, and there var-tracking
182 will figure out the right thing. */
183 else if (DECL_RTL (var) != x)
184 SET_DECL_RTL (var, pc_rtx);
187 else
188 SET_DECL_RTL (t, x);
191 /* This structure holds data relevant to one variable that will be
192 placed in a stack slot. */
193 struct stack_var
195 /* The Variable. */
196 tree decl;
198 /* Initially, the size of the variable. Later, the size of the partition,
199 if this variable becomes it's partition's representative. */
200 HOST_WIDE_INT size;
202 /* The *byte* alignment required for this variable. Or as, with the
203 size, the alignment for this partition. */
204 unsigned int alignb;
206 /* The partition representative. */
207 size_t representative;
209 /* The next stack variable in the partition, or EOC. */
210 size_t next;
212 /* The numbers of conflicting stack variables. */
213 bitmap conflicts;
216 #define EOC ((size_t)-1)
218 /* We have an array of such objects while deciding allocation. */
219 static struct stack_var *stack_vars;
220 static size_t stack_vars_alloc;
221 static size_t stack_vars_num;
222 static hash_map<tree, size_t> *decl_to_stack_part;
224 /* Conflict bitmaps go on this obstack. This allows us to destroy
225 all of them in one big sweep. */
226 static bitmap_obstack stack_var_bitmap_obstack;
228 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
229 is non-decreasing. */
230 static size_t *stack_vars_sorted;
232 /* The phase of the stack frame. This is the known misalignment of
233 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
234 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
235 static int frame_phase;
237 /* Used during expand_used_vars to remember if we saw any decls for
238 which we'd like to enable stack smashing protection. */
239 static bool has_protected_decls;
241 /* Used during expand_used_vars. Remember if we say a character buffer
242 smaller than our cutoff threshold. Used for -Wstack-protector. */
243 static bool has_short_buffer;
245 /* Compute the byte alignment to use for DECL. Ignore alignment
246 we can't do with expected alignment of the stack boundary. */
248 static unsigned int
249 align_local_variable (tree decl)
251 unsigned int align = LOCAL_DECL_ALIGNMENT (decl);
252 DECL_ALIGN (decl) = align;
253 return align / BITS_PER_UNIT;
256 /* Align given offset BASE with ALIGN. Truncate up if ALIGN_UP is true,
257 down otherwise. Return truncated BASE value. */
259 static inline unsigned HOST_WIDE_INT
260 align_base (HOST_WIDE_INT base, unsigned HOST_WIDE_INT align, bool align_up)
262 return align_up ? (base + align - 1) & -align : base & -align;
265 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
266 Return the frame offset. */
268 static HOST_WIDE_INT
269 alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
271 HOST_WIDE_INT offset, new_frame_offset;
273 if (FRAME_GROWS_DOWNWARD)
275 new_frame_offset
276 = align_base (frame_offset - frame_phase - size,
277 align, false) + frame_phase;
278 offset = new_frame_offset;
280 else
282 new_frame_offset
283 = align_base (frame_offset - frame_phase, align, true) + frame_phase;
284 offset = new_frame_offset;
285 new_frame_offset += size;
287 frame_offset = new_frame_offset;
289 if (frame_offset_overflow (frame_offset, cfun->decl))
290 frame_offset = offset = 0;
292 return offset;
295 /* Accumulate DECL into STACK_VARS. */
297 static void
298 add_stack_var (tree decl)
300 struct stack_var *v;
302 if (stack_vars_num >= stack_vars_alloc)
304 if (stack_vars_alloc)
305 stack_vars_alloc = stack_vars_alloc * 3 / 2;
306 else
307 stack_vars_alloc = 32;
308 stack_vars
309 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
311 if (!decl_to_stack_part)
312 decl_to_stack_part = new hash_map<tree, size_t>;
314 v = &stack_vars[stack_vars_num];
315 decl_to_stack_part->put (decl, stack_vars_num);
317 v->decl = decl;
318 v->size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (decl)));
319 /* Ensure that all variables have size, so that &a != &b for any two
320 variables that are simultaneously live. */
321 if (v->size == 0)
322 v->size = 1;
323 v->alignb = align_local_variable (SSAVAR (decl));
324 /* An alignment of zero can mightily confuse us later. */
325 gcc_assert (v->alignb != 0);
327 /* All variables are initially in their own partition. */
328 v->representative = stack_vars_num;
329 v->next = EOC;
331 /* All variables initially conflict with no other. */
332 v->conflicts = NULL;
334 /* Ensure that this decl doesn't get put onto the list twice. */
335 set_rtl (decl, pc_rtx);
337 stack_vars_num++;
340 /* Make the decls associated with luid's X and Y conflict. */
342 static void
343 add_stack_var_conflict (size_t x, size_t y)
345 struct stack_var *a = &stack_vars[x];
346 struct stack_var *b = &stack_vars[y];
347 if (!a->conflicts)
348 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
349 if (!b->conflicts)
350 b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
351 bitmap_set_bit (a->conflicts, y);
352 bitmap_set_bit (b->conflicts, x);
355 /* Check whether the decls associated with luid's X and Y conflict. */
357 static bool
358 stack_var_conflict_p (size_t x, size_t y)
360 struct stack_var *a = &stack_vars[x];
361 struct stack_var *b = &stack_vars[y];
362 if (x == y)
363 return false;
364 /* Partitions containing an SSA name result from gimple registers
365 with things like unsupported modes. They are top-level and
366 hence conflict with everything else. */
367 if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
368 return true;
370 if (!a->conflicts || !b->conflicts)
371 return false;
372 return bitmap_bit_p (a->conflicts, y);
375 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
376 enter its partition number into bitmap DATA. */
378 static bool
379 visit_op (gimple, tree op, tree, void *data)
381 bitmap active = (bitmap)data;
382 op = get_base_address (op);
383 if (op
384 && DECL_P (op)
385 && DECL_RTL_IF_SET (op) == pc_rtx)
387 size_t *v = decl_to_stack_part->get (op);
388 if (v)
389 bitmap_set_bit (active, *v);
391 return false;
394 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
395 record conflicts between it and all currently active other partitions
396 from bitmap DATA. */
398 static bool
399 visit_conflict (gimple, tree op, tree, void *data)
401 bitmap active = (bitmap)data;
402 op = get_base_address (op);
403 if (op
404 && DECL_P (op)
405 && DECL_RTL_IF_SET (op) == pc_rtx)
407 size_t *v = decl_to_stack_part->get (op);
408 if (v && bitmap_set_bit (active, *v))
410 size_t num = *v;
411 bitmap_iterator bi;
412 unsigned i;
413 gcc_assert (num < stack_vars_num);
414 EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
415 add_stack_var_conflict (num, i);
418 return false;
421 /* Helper routine for add_scope_conflicts, calculating the active partitions
422 at the end of BB, leaving the result in WORK. We're called to generate
423 conflicts when FOR_CONFLICT is true, otherwise we're just tracking
424 liveness. */
426 static void
427 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
429 edge e;
430 edge_iterator ei;
431 gimple_stmt_iterator gsi;
432 walk_stmt_load_store_addr_fn visit;
434 bitmap_clear (work);
435 FOR_EACH_EDGE (e, ei, bb->preds)
436 bitmap_ior_into (work, (bitmap)e->src->aux);
438 visit = visit_op;
440 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
442 gimple stmt = gsi_stmt (gsi);
443 walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
445 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
447 gimple stmt = gsi_stmt (gsi);
449 if (gimple_clobber_p (stmt))
451 tree lhs = gimple_assign_lhs (stmt);
452 size_t *v;
453 /* Nested function lowering might introduce LHSs
454 that are COMPONENT_REFs. */
455 if (TREE_CODE (lhs) != VAR_DECL)
456 continue;
457 if (DECL_RTL_IF_SET (lhs) == pc_rtx
458 && (v = decl_to_stack_part->get (lhs)))
459 bitmap_clear_bit (work, *v);
461 else if (!is_gimple_debug (stmt))
463 if (for_conflict
464 && visit == visit_op)
466 /* If this is the first real instruction in this BB we need
467 to add conflicts for everything live at this point now.
468 Unlike classical liveness for named objects we can't
469 rely on seeing a def/use of the names we're interested in.
470 There might merely be indirect loads/stores. We'd not add any
471 conflicts for such partitions. */
472 bitmap_iterator bi;
473 unsigned i;
474 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
476 struct stack_var *a = &stack_vars[i];
477 if (!a->conflicts)
478 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
479 bitmap_ior_into (a->conflicts, work);
481 visit = visit_conflict;
483 walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
488 /* Generate stack partition conflicts between all partitions that are
489 simultaneously live. */
491 static void
492 add_scope_conflicts (void)
494 basic_block bb;
495 bool changed;
496 bitmap work = BITMAP_ALLOC (NULL);
497 int *rpo;
498 int n_bbs;
500 /* We approximate the live range of a stack variable by taking the first
501 mention of its name as starting point(s), and by the end-of-scope
502 death clobber added by gimplify as ending point(s) of the range.
503 This overapproximates in the case we for instance moved an address-taken
504 operation upward, without also moving a dereference to it upwards.
505 But it's conservatively correct as a variable never can hold values
506 before its name is mentioned at least once.
508 We then do a mostly classical bitmap liveness algorithm. */
510 FOR_ALL_BB_FN (bb, cfun)
511 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
513 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
514 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
516 changed = true;
517 while (changed)
519 int i;
520 changed = false;
521 for (i = 0; i < n_bbs; i++)
523 bitmap active;
524 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
525 active = (bitmap)bb->aux;
526 add_scope_conflicts_1 (bb, work, false);
527 if (bitmap_ior_into (active, work))
528 changed = true;
532 FOR_EACH_BB_FN (bb, cfun)
533 add_scope_conflicts_1 (bb, work, true);
535 free (rpo);
536 BITMAP_FREE (work);
537 FOR_ALL_BB_FN (bb, cfun)
538 BITMAP_FREE (bb->aux);
541 /* A subroutine of partition_stack_vars. A comparison function for qsort,
542 sorting an array of indices by the properties of the object. */
544 static int
545 stack_var_cmp (const void *a, const void *b)
547 size_t ia = *(const size_t *)a;
548 size_t ib = *(const size_t *)b;
549 unsigned int aligna = stack_vars[ia].alignb;
550 unsigned int alignb = stack_vars[ib].alignb;
551 HOST_WIDE_INT sizea = stack_vars[ia].size;
552 HOST_WIDE_INT sizeb = stack_vars[ib].size;
553 tree decla = stack_vars[ia].decl;
554 tree declb = stack_vars[ib].decl;
555 bool largea, largeb;
556 unsigned int uida, uidb;
558 /* Primary compare on "large" alignment. Large comes first. */
559 largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
560 largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
561 if (largea != largeb)
562 return (int)largeb - (int)largea;
564 /* Secondary compare on size, decreasing */
565 if (sizea > sizeb)
566 return -1;
567 if (sizea < sizeb)
568 return 1;
570 /* Tertiary compare on true alignment, decreasing. */
571 if (aligna < alignb)
572 return -1;
573 if (aligna > alignb)
574 return 1;
576 /* Final compare on ID for sort stability, increasing.
577 Two SSA names are compared by their version, SSA names come before
578 non-SSA names, and two normal decls are compared by their DECL_UID. */
579 if (TREE_CODE (decla) == SSA_NAME)
581 if (TREE_CODE (declb) == SSA_NAME)
582 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
583 else
584 return -1;
586 else if (TREE_CODE (declb) == SSA_NAME)
587 return 1;
588 else
589 uida = DECL_UID (decla), uidb = DECL_UID (declb);
590 if (uida < uidb)
591 return 1;
592 if (uida > uidb)
593 return -1;
594 return 0;
597 struct part_traits : unbounded_int_hashmap_traits <size_t, bitmap> {};
598 typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
600 /* If the points-to solution *PI points to variables that are in a partition
601 together with other variables add all partition members to the pointed-to
602 variables bitmap. */
604 static void
605 add_partitioned_vars_to_ptset (struct pt_solution *pt,
606 part_hashmap *decls_to_partitions,
607 hash_set<bitmap> *visited, bitmap temp)
609 bitmap_iterator bi;
610 unsigned i;
611 bitmap *part;
613 if (pt->anything
614 || pt->vars == NULL
615 /* The pointed-to vars bitmap is shared, it is enough to
616 visit it once. */
617 || visited->add (pt->vars))
618 return;
620 bitmap_clear (temp);
622 /* By using a temporary bitmap to store all members of the partitions
623 we have to add we make sure to visit each of the partitions only
624 once. */
625 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
626 if ((!temp
627 || !bitmap_bit_p (temp, i))
628 && (part = decls_to_partitions->get (i)))
629 bitmap_ior_into (temp, *part);
630 if (!bitmap_empty_p (temp))
631 bitmap_ior_into (pt->vars, temp);
634 /* Update points-to sets based on partition info, so we can use them on RTL.
635 The bitmaps representing stack partitions will be saved until expand,
636 where partitioned decls used as bases in memory expressions will be
637 rewritten. */
639 static void
640 update_alias_info_with_stack_vars (void)
642 part_hashmap *decls_to_partitions = NULL;
643 size_t i, j;
644 tree var = NULL_TREE;
646 for (i = 0; i < stack_vars_num; i++)
648 bitmap part = NULL;
649 tree name;
650 struct ptr_info_def *pi;
652 /* Not interested in partitions with single variable. */
653 if (stack_vars[i].representative != i
654 || stack_vars[i].next == EOC)
655 continue;
657 if (!decls_to_partitions)
659 decls_to_partitions = new part_hashmap;
660 cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
663 /* Create an SSA_NAME that points to the partition for use
664 as base during alias-oracle queries on RTL for bases that
665 have been partitioned. */
666 if (var == NULL_TREE)
667 var = create_tmp_var (ptr_type_node);
668 name = make_ssa_name (var);
670 /* Create bitmaps representing partitions. They will be used for
671 points-to sets later, so use GGC alloc. */
672 part = BITMAP_GGC_ALLOC ();
673 for (j = i; j != EOC; j = stack_vars[j].next)
675 tree decl = stack_vars[j].decl;
676 unsigned int uid = DECL_PT_UID (decl);
677 bitmap_set_bit (part, uid);
678 decls_to_partitions->put (uid, part);
679 cfun->gimple_df->decls_to_pointers->put (decl, name);
680 if (TREE_ADDRESSABLE (decl))
681 TREE_ADDRESSABLE (name) = 1;
684 /* Make the SSA name point to all partition members. */
685 pi = get_ptr_info (name);
686 pt_solution_set (&pi->pt, part, false);
689 /* Make all points-to sets that contain one member of a partition
690 contain all members of the partition. */
691 if (decls_to_partitions)
693 unsigned i;
694 hash_set<bitmap> visited;
695 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
697 for (i = 1; i < num_ssa_names; i++)
699 tree name = ssa_name (i);
700 struct ptr_info_def *pi;
702 if (name
703 && POINTER_TYPE_P (TREE_TYPE (name))
704 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
705 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
706 &visited, temp);
709 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
710 decls_to_partitions, &visited, temp);
712 delete decls_to_partitions;
713 BITMAP_FREE (temp);
717 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
718 partitioning algorithm. Partitions A and B are known to be non-conflicting.
719 Merge them into a single partition A. */
721 static void
722 union_stack_vars (size_t a, size_t b)
724 struct stack_var *vb = &stack_vars[b];
725 bitmap_iterator bi;
726 unsigned u;
728 gcc_assert (stack_vars[b].next == EOC);
729 /* Add B to A's partition. */
730 stack_vars[b].next = stack_vars[a].next;
731 stack_vars[b].representative = a;
732 stack_vars[a].next = b;
734 /* Update the required alignment of partition A to account for B. */
735 if (stack_vars[a].alignb < stack_vars[b].alignb)
736 stack_vars[a].alignb = stack_vars[b].alignb;
738 /* Update the interference graph and merge the conflicts. */
739 if (vb->conflicts)
741 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
742 add_stack_var_conflict (a, stack_vars[u].representative);
743 BITMAP_FREE (vb->conflicts);
747 /* A subroutine of expand_used_vars. Binpack the variables into
748 partitions constrained by the interference graph. The overall
749 algorithm used is as follows:
751 Sort the objects by size in descending order.
752 For each object A {
753 S = size(A)
754 O = 0
755 loop {
756 Look for the largest non-conflicting object B with size <= S.
757 UNION (A, B)
762 static void
763 partition_stack_vars (void)
765 size_t si, sj, n = stack_vars_num;
767 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
768 for (si = 0; si < n; ++si)
769 stack_vars_sorted[si] = si;
771 if (n == 1)
772 return;
774 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
776 for (si = 0; si < n; ++si)
778 size_t i = stack_vars_sorted[si];
779 unsigned int ialign = stack_vars[i].alignb;
780 HOST_WIDE_INT isize = stack_vars[i].size;
782 /* Ignore objects that aren't partition representatives. If we
783 see a var that is not a partition representative, it must
784 have been merged earlier. */
785 if (stack_vars[i].representative != i)
786 continue;
788 for (sj = si + 1; sj < n; ++sj)
790 size_t j = stack_vars_sorted[sj];
791 unsigned int jalign = stack_vars[j].alignb;
792 HOST_WIDE_INT jsize = stack_vars[j].size;
794 /* Ignore objects that aren't partition representatives. */
795 if (stack_vars[j].representative != j)
796 continue;
798 /* Do not mix objects of "small" (supported) alignment
799 and "large" (unsupported) alignment. */
800 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
801 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
802 break;
804 /* For Address Sanitizer do not mix objects with different
805 sizes, as the shorter vars wouldn't be adequately protected.
806 Don't do that for "large" (unsupported) alignment objects,
807 those aren't protected anyway. */
808 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && isize != jsize
809 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
810 break;
812 /* Ignore conflicting objects. */
813 if (stack_var_conflict_p (i, j))
814 continue;
816 /* UNION the objects, placing J at OFFSET. */
817 union_stack_vars (i, j);
821 update_alias_info_with_stack_vars ();
824 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
826 static void
827 dump_stack_var_partition (void)
829 size_t si, i, j, n = stack_vars_num;
831 for (si = 0; si < n; ++si)
833 i = stack_vars_sorted[si];
835 /* Skip variables that aren't partition representatives, for now. */
836 if (stack_vars[i].representative != i)
837 continue;
839 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
840 " align %u\n", (unsigned long) i, stack_vars[i].size,
841 stack_vars[i].alignb);
843 for (j = i; j != EOC; j = stack_vars[j].next)
845 fputc ('\t', dump_file);
846 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
848 fputc ('\n', dump_file);
852 /* Assign rtl to DECL at BASE + OFFSET. */
854 static void
855 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
856 HOST_WIDE_INT offset)
858 unsigned align;
859 rtx x;
861 /* If this fails, we've overflowed the stack frame. Error nicely? */
862 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
864 x = plus_constant (Pmode, base, offset);
865 x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
867 if (TREE_CODE (decl) != SSA_NAME)
869 /* Set alignment we actually gave this decl if it isn't an SSA name.
870 If it is we generate stack slots only accidentally so it isn't as
871 important, we'll simply use the alignment that is already set. */
872 if (base == virtual_stack_vars_rtx)
873 offset -= frame_phase;
874 align = offset & -offset;
875 align *= BITS_PER_UNIT;
876 if (align == 0 || align > base_align)
877 align = base_align;
879 /* One would think that we could assert that we're not decreasing
880 alignment here, but (at least) the i386 port does exactly this
881 via the MINIMUM_ALIGNMENT hook. */
883 DECL_ALIGN (decl) = align;
884 DECL_USER_ALIGN (decl) = 0;
887 set_mem_attributes (x, SSAVAR (decl), true);
888 set_rtl (decl, x);
891 struct stack_vars_data
893 /* Vector of offset pairs, always end of some padding followed
894 by start of the padding that needs Address Sanitizer protection.
895 The vector is in reversed, highest offset pairs come first. */
896 vec<HOST_WIDE_INT> asan_vec;
898 /* Vector of partition representative decls in between the paddings. */
899 vec<tree> asan_decl_vec;
901 /* Base pseudo register for Address Sanitizer protected automatic vars. */
902 rtx asan_base;
904 /* Alignment needed for the Address Sanitizer protected automatic vars. */
905 unsigned int asan_alignb;
908 /* A subroutine of expand_used_vars. Give each partition representative
909 a unique location within the stack frame. Update each partition member
910 with that location. */
912 static void
913 expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
915 size_t si, i, j, n = stack_vars_num;
916 HOST_WIDE_INT large_size = 0, large_alloc = 0;
917 rtx large_base = NULL;
918 unsigned large_align = 0;
919 tree decl;
921 /* Determine if there are any variables requiring "large" alignment.
922 Since these are dynamically allocated, we only process these if
923 no predicate involved. */
924 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
925 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
927 /* Find the total size of these variables. */
928 for (si = 0; si < n; ++si)
930 unsigned alignb;
932 i = stack_vars_sorted[si];
933 alignb = stack_vars[i].alignb;
935 /* All "large" alignment decls come before all "small" alignment
936 decls, but "large" alignment decls are not sorted based on
937 their alignment. Increase large_align to track the largest
938 required alignment. */
939 if ((alignb * BITS_PER_UNIT) > large_align)
940 large_align = alignb * BITS_PER_UNIT;
942 /* Stop when we get to the first decl with "small" alignment. */
943 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
944 break;
946 /* Skip variables that aren't partition representatives. */
947 if (stack_vars[i].representative != i)
948 continue;
950 /* Skip variables that have already had rtl assigned. See also
951 add_stack_var where we perpetrate this pc_rtx hack. */
952 decl = stack_vars[i].decl;
953 if ((TREE_CODE (decl) == SSA_NAME
954 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
955 : DECL_RTL (decl)) != pc_rtx)
956 continue;
958 large_size += alignb - 1;
959 large_size &= -(HOST_WIDE_INT)alignb;
960 large_size += stack_vars[i].size;
963 /* If there were any, allocate space. */
964 if (large_size > 0)
965 large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
966 large_align, true);
969 for (si = 0; si < n; ++si)
971 rtx base;
972 unsigned base_align, alignb;
973 HOST_WIDE_INT offset;
975 i = stack_vars_sorted[si];
977 /* Skip variables that aren't partition representatives, for now. */
978 if (stack_vars[i].representative != i)
979 continue;
981 /* Skip variables that have already had rtl assigned. See also
982 add_stack_var where we perpetrate this pc_rtx hack. */
983 decl = stack_vars[i].decl;
984 if ((TREE_CODE (decl) == SSA_NAME
985 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
986 : DECL_RTL (decl)) != pc_rtx)
987 continue;
989 /* Check the predicate to see whether this variable should be
990 allocated in this pass. */
991 if (pred && !pred (i))
992 continue;
994 alignb = stack_vars[i].alignb;
995 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
997 base = virtual_stack_vars_rtx;
998 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && pred)
1000 HOST_WIDE_INT prev_offset
1001 = align_base (frame_offset,
1002 MAX (alignb, ASAN_RED_ZONE_SIZE),
1003 FRAME_GROWS_DOWNWARD);
1004 tree repr_decl = NULL_TREE;
1005 offset
1006 = alloc_stack_frame_space (stack_vars[i].size
1007 + ASAN_RED_ZONE_SIZE,
1008 MAX (alignb, ASAN_RED_ZONE_SIZE));
1010 data->asan_vec.safe_push (prev_offset);
1011 data->asan_vec.safe_push (offset + stack_vars[i].size);
1012 /* Find best representative of the partition.
1013 Prefer those with DECL_NAME, even better
1014 satisfying asan_protect_stack_decl predicate. */
1015 for (j = i; j != EOC; j = stack_vars[j].next)
1016 if (asan_protect_stack_decl (stack_vars[j].decl)
1017 && DECL_NAME (stack_vars[j].decl))
1019 repr_decl = stack_vars[j].decl;
1020 break;
1022 else if (repr_decl == NULL_TREE
1023 && DECL_P (stack_vars[j].decl)
1024 && DECL_NAME (stack_vars[j].decl))
1025 repr_decl = stack_vars[j].decl;
1026 if (repr_decl == NULL_TREE)
1027 repr_decl = stack_vars[i].decl;
1028 data->asan_decl_vec.safe_push (repr_decl);
1029 data->asan_alignb = MAX (data->asan_alignb, alignb);
1030 if (data->asan_base == NULL)
1031 data->asan_base = gen_reg_rtx (Pmode);
1032 base = data->asan_base;
1034 if (!STRICT_ALIGNMENT)
1035 base_align = crtl->max_used_stack_slot_alignment;
1036 else
1037 base_align = MAX (crtl->max_used_stack_slot_alignment,
1038 GET_MODE_ALIGNMENT (SImode)
1039 << ASAN_SHADOW_SHIFT);
1041 else
1043 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1044 base_align = crtl->max_used_stack_slot_alignment;
1047 else
1049 /* Large alignment is only processed in the last pass. */
1050 if (pred)
1051 continue;
1052 gcc_assert (large_base != NULL);
1054 large_alloc += alignb - 1;
1055 large_alloc &= -(HOST_WIDE_INT)alignb;
1056 offset = large_alloc;
1057 large_alloc += stack_vars[i].size;
1059 base = large_base;
1060 base_align = large_align;
1063 /* Create rtl for each variable based on their location within the
1064 partition. */
1065 for (j = i; j != EOC; j = stack_vars[j].next)
1067 expand_one_stack_var_at (stack_vars[j].decl,
1068 base, base_align,
1069 offset);
1073 gcc_assert (large_alloc == large_size);
1076 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1077 static HOST_WIDE_INT
1078 account_stack_vars (void)
1080 size_t si, j, i, n = stack_vars_num;
1081 HOST_WIDE_INT size = 0;
1083 for (si = 0; si < n; ++si)
1085 i = stack_vars_sorted[si];
1087 /* Skip variables that aren't partition representatives, for now. */
1088 if (stack_vars[i].representative != i)
1089 continue;
1091 size += stack_vars[i].size;
1092 for (j = i; j != EOC; j = stack_vars[j].next)
1093 set_rtl (stack_vars[j].decl, NULL);
1095 return size;
1098 /* A subroutine of expand_one_var. Called to immediately assign rtl
1099 to a variable to be allocated in the stack frame. */
1101 static void
1102 expand_one_stack_var (tree var)
1104 HOST_WIDE_INT size, offset;
1105 unsigned byte_align;
1107 size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (var)));
1108 byte_align = align_local_variable (SSAVAR (var));
1110 /* We handle highly aligned variables in expand_stack_vars. */
1111 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1113 offset = alloc_stack_frame_space (size, byte_align);
1115 expand_one_stack_var_at (var, virtual_stack_vars_rtx,
1116 crtl->max_used_stack_slot_alignment, offset);
1119 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1120 that will reside in a hard register. */
1122 static void
1123 expand_one_hard_reg_var (tree var)
1125 rest_of_decl_compilation (var, 0, 0);
1128 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1129 that will reside in a pseudo register. */
1131 static void
1132 expand_one_register_var (tree var)
1134 tree decl = SSAVAR (var);
1135 tree type = TREE_TYPE (decl);
1136 machine_mode reg_mode = promote_decl_mode (decl, NULL);
1137 rtx x = gen_reg_rtx (reg_mode);
1139 set_rtl (var, x);
1141 /* Note if the object is a user variable. */
1142 if (!DECL_ARTIFICIAL (decl))
1143 mark_user_reg (x);
1145 if (POINTER_TYPE_P (type))
1146 mark_reg_pointer (x, get_pointer_alignment (var));
1149 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1150 has some associated error, e.g. its type is error-mark. We just need
1151 to pick something that won't crash the rest of the compiler. */
1153 static void
1154 expand_one_error_var (tree var)
1156 machine_mode mode = DECL_MODE (var);
1157 rtx x;
1159 if (mode == BLKmode)
1160 x = gen_rtx_MEM (BLKmode, const0_rtx);
1161 else if (mode == VOIDmode)
1162 x = const0_rtx;
1163 else
1164 x = gen_reg_rtx (mode);
1166 SET_DECL_RTL (var, x);
1169 /* A subroutine of expand_one_var. VAR is a variable that will be
1170 allocated to the local stack frame. Return true if we wish to
1171 add VAR to STACK_VARS so that it will be coalesced with other
1172 variables. Return false to allocate VAR immediately.
1174 This function is used to reduce the number of variables considered
1175 for coalescing, which reduces the size of the quadratic problem. */
1177 static bool
1178 defer_stack_allocation (tree var, bool toplevel)
1180 /* Whether the variable is small enough for immediate allocation not to be
1181 a problem with regard to the frame size. */
1182 bool smallish
1183 = ((HOST_WIDE_INT) tree_to_uhwi (DECL_SIZE_UNIT (var))
1184 < PARAM_VALUE (PARAM_MIN_SIZE_FOR_STACK_SHARING));
1186 /* If stack protection is enabled, *all* stack variables must be deferred,
1187 so that we can re-order the strings to the top of the frame.
1188 Similarly for Address Sanitizer. */
1189 if (flag_stack_protect || ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK))
1190 return true;
1192 /* We handle "large" alignment via dynamic allocation. We want to handle
1193 this extra complication in only one place, so defer them. */
1194 if (DECL_ALIGN (var) > MAX_SUPPORTED_STACK_ALIGNMENT)
1195 return true;
1197 /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1198 might be detached from their block and appear at toplevel when we reach
1199 here. We want to coalesce them with variables from other blocks when
1200 the immediate contribution to the frame size would be noticeable. */
1201 if (toplevel && optimize > 0 && DECL_IGNORED_P (var) && !smallish)
1202 return true;
1204 /* Variables declared in the outermost scope automatically conflict
1205 with every other variable. The only reason to want to defer them
1206 at all is that, after sorting, we can more efficiently pack
1207 small variables in the stack frame. Continue to defer at -O2. */
1208 if (toplevel && optimize < 2)
1209 return false;
1211 /* Without optimization, *most* variables are allocated from the
1212 stack, which makes the quadratic problem large exactly when we
1213 want compilation to proceed as quickly as possible. On the
1214 other hand, we don't want the function's stack frame size to
1215 get completely out of hand. So we avoid adding scalars and
1216 "small" aggregates to the list at all. */
1217 if (optimize == 0 && smallish)
1218 return false;
1220 return true;
1223 /* A subroutine of expand_used_vars. Expand one variable according to
1224 its flavor. Variables to be placed on the stack are not actually
1225 expanded yet, merely recorded.
1226 When REALLY_EXPAND is false, only add stack values to be allocated.
1227 Return stack usage this variable is supposed to take.
1230 static HOST_WIDE_INT
1231 expand_one_var (tree var, bool toplevel, bool really_expand)
1233 unsigned int align = BITS_PER_UNIT;
1234 tree origvar = var;
1236 var = SSAVAR (var);
1238 if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
1240 /* Because we don't know if VAR will be in register or on stack,
1241 we conservatively assume it will be on stack even if VAR is
1242 eventually put into register after RA pass. For non-automatic
1243 variables, which won't be on stack, we collect alignment of
1244 type and ignore user specified alignment. Similarly for
1245 SSA_NAMEs for which use_register_for_decl returns true. */
1246 if (TREE_STATIC (var)
1247 || DECL_EXTERNAL (var)
1248 || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1249 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1250 TYPE_MODE (TREE_TYPE (var)),
1251 TYPE_ALIGN (TREE_TYPE (var)));
1252 else if (DECL_HAS_VALUE_EXPR_P (var)
1253 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1254 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1255 or variables which were assigned a stack slot already by
1256 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1257 changed from the offset chosen to it. */
1258 align = crtl->stack_alignment_estimated;
1259 else
1260 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1262 /* If the variable alignment is very large we'll dynamicaly allocate
1263 it, which means that in-frame portion is just a pointer. */
1264 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1265 align = POINTER_SIZE;
1268 if (SUPPORTS_STACK_ALIGNMENT
1269 && crtl->stack_alignment_estimated < align)
1271 /* stack_alignment_estimated shouldn't change after stack
1272 realign decision made */
1273 gcc_assert (!crtl->stack_realign_processed);
1274 crtl->stack_alignment_estimated = align;
1277 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1278 So here we only make sure stack_alignment_needed >= align. */
1279 if (crtl->stack_alignment_needed < align)
1280 crtl->stack_alignment_needed = align;
1281 if (crtl->max_used_stack_slot_alignment < align)
1282 crtl->max_used_stack_slot_alignment = align;
1284 if (TREE_CODE (origvar) == SSA_NAME)
1286 gcc_assert (TREE_CODE (var) != VAR_DECL
1287 || (!DECL_EXTERNAL (var)
1288 && !DECL_HAS_VALUE_EXPR_P (var)
1289 && !TREE_STATIC (var)
1290 && TREE_TYPE (var) != error_mark_node
1291 && !DECL_HARD_REGISTER (var)
1292 && really_expand));
1294 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1296 else if (DECL_EXTERNAL (var))
1298 else if (DECL_HAS_VALUE_EXPR_P (var))
1300 else if (TREE_STATIC (var))
1302 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1304 else if (TREE_TYPE (var) == error_mark_node)
1306 if (really_expand)
1307 expand_one_error_var (var);
1309 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1311 if (really_expand)
1313 expand_one_hard_reg_var (var);
1314 if (!DECL_HARD_REGISTER (var))
1315 /* Invalid register specification. */
1316 expand_one_error_var (var);
1319 else if (use_register_for_decl (var))
1321 if (really_expand)
1322 expand_one_register_var (origvar);
1324 else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
1326 /* Reject variables which cover more than half of the address-space. */
1327 if (really_expand)
1329 error ("size of variable %q+D is too large", var);
1330 expand_one_error_var (var);
1333 else if (defer_stack_allocation (var, toplevel))
1334 add_stack_var (origvar);
1335 else
1337 if (really_expand)
1339 if (lookup_attribute ("naked",
1340 DECL_ATTRIBUTES (current_function_decl)))
1341 error ("cannot allocate stack for variable %q+D, naked function.",
1342 var);
1344 expand_one_stack_var (origvar);
1348 return tree_to_uhwi (DECL_SIZE_UNIT (var));
1350 return 0;
1353 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1354 expanding variables. Those variables that can be put into registers
1355 are allocated pseudos; those that can't are put on the stack.
1357 TOPLEVEL is true if this is the outermost BLOCK. */
1359 static void
1360 expand_used_vars_for_block (tree block, bool toplevel)
1362 tree t;
1364 /* Expand all variables at this level. */
1365 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1366 if (TREE_USED (t)
1367 && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1368 || !DECL_NONSHAREABLE (t)))
1369 expand_one_var (t, toplevel, true);
1371 /* Expand all variables at containing levels. */
1372 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1373 expand_used_vars_for_block (t, false);
1376 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1377 and clear TREE_USED on all local variables. */
1379 static void
1380 clear_tree_used (tree block)
1382 tree t;
1384 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1385 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1386 if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1387 || !DECL_NONSHAREABLE (t))
1388 TREE_USED (t) = 0;
1390 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1391 clear_tree_used (t);
1394 enum {
1395 SPCT_FLAG_DEFAULT = 1,
1396 SPCT_FLAG_ALL = 2,
1397 SPCT_FLAG_STRONG = 3,
1398 SPCT_FLAG_EXPLICIT = 4
1401 /* Examine TYPE and determine a bit mask of the following features. */
1403 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1404 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1405 #define SPCT_HAS_ARRAY 4
1406 #define SPCT_HAS_AGGREGATE 8
1408 static unsigned int
1409 stack_protect_classify_type (tree type)
1411 unsigned int ret = 0;
1412 tree t;
1414 switch (TREE_CODE (type))
1416 case ARRAY_TYPE:
1417 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1418 if (t == char_type_node
1419 || t == signed_char_type_node
1420 || t == unsigned_char_type_node)
1422 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1423 unsigned HOST_WIDE_INT len;
1425 if (!TYPE_SIZE_UNIT (type)
1426 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1427 len = max;
1428 else
1429 len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1431 if (len < max)
1432 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1433 else
1434 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1436 else
1437 ret = SPCT_HAS_ARRAY;
1438 break;
1440 case UNION_TYPE:
1441 case QUAL_UNION_TYPE:
1442 case RECORD_TYPE:
1443 ret = SPCT_HAS_AGGREGATE;
1444 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1445 if (TREE_CODE (t) == FIELD_DECL)
1446 ret |= stack_protect_classify_type (TREE_TYPE (t));
1447 break;
1449 default:
1450 break;
1453 return ret;
1456 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1457 part of the local stack frame. Remember if we ever return nonzero for
1458 any variable in this function. The return value is the phase number in
1459 which the variable should be allocated. */
1461 static int
1462 stack_protect_decl_phase (tree decl)
1464 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1465 int ret = 0;
1467 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1468 has_short_buffer = true;
1470 if (flag_stack_protect == SPCT_FLAG_ALL
1471 || flag_stack_protect == SPCT_FLAG_STRONG
1472 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1473 && lookup_attribute ("stack_protect",
1474 DECL_ATTRIBUTES (current_function_decl))))
1476 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1477 && !(bits & SPCT_HAS_AGGREGATE))
1478 ret = 1;
1479 else if (bits & SPCT_HAS_ARRAY)
1480 ret = 2;
1482 else
1483 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1485 if (ret)
1486 has_protected_decls = true;
1488 return ret;
1491 /* Two helper routines that check for phase 1 and phase 2. These are used
1492 as callbacks for expand_stack_vars. */
1494 static bool
1495 stack_protect_decl_phase_1 (size_t i)
1497 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
1500 static bool
1501 stack_protect_decl_phase_2 (size_t i)
1503 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
1506 /* And helper function that checks for asan phase (with stack protector
1507 it is phase 3). This is used as callback for expand_stack_vars.
1508 Returns true if any of the vars in the partition need to be protected. */
1510 static bool
1511 asan_decl_phase_3 (size_t i)
1513 while (i != EOC)
1515 if (asan_protect_stack_decl (stack_vars[i].decl))
1516 return true;
1517 i = stack_vars[i].next;
1519 return false;
1522 /* Ensure that variables in different stack protection phases conflict
1523 so that they are not merged and share the same stack slot. */
1525 static void
1526 add_stack_protection_conflicts (void)
1528 size_t i, j, n = stack_vars_num;
1529 unsigned char *phase;
1531 phase = XNEWVEC (unsigned char, n);
1532 for (i = 0; i < n; ++i)
1533 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1535 for (i = 0; i < n; ++i)
1537 unsigned char ph_i = phase[i];
1538 for (j = i + 1; j < n; ++j)
1539 if (ph_i != phase[j])
1540 add_stack_var_conflict (i, j);
1543 XDELETEVEC (phase);
1546 /* Create a decl for the guard at the top of the stack frame. */
1548 static void
1549 create_stack_guard (void)
1551 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1552 VAR_DECL, NULL, ptr_type_node);
1553 TREE_THIS_VOLATILE (guard) = 1;
1554 TREE_USED (guard) = 1;
1555 expand_one_stack_var (guard);
1556 crtl->stack_protect_guard = guard;
1559 /* Prepare for expanding variables. */
1560 static void
1561 init_vars_expansion (void)
1563 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
1564 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
1566 /* A map from decl to stack partition. */
1567 decl_to_stack_part = new hash_map<tree, size_t>;
1569 /* Initialize local stack smashing state. */
1570 has_protected_decls = false;
1571 has_short_buffer = false;
1574 /* Free up stack variable graph data. */
1575 static void
1576 fini_vars_expansion (void)
1578 bitmap_obstack_release (&stack_var_bitmap_obstack);
1579 if (stack_vars)
1580 XDELETEVEC (stack_vars);
1581 if (stack_vars_sorted)
1582 XDELETEVEC (stack_vars_sorted);
1583 stack_vars = NULL;
1584 stack_vars_sorted = NULL;
1585 stack_vars_alloc = stack_vars_num = 0;
1586 delete decl_to_stack_part;
1587 decl_to_stack_part = NULL;
1590 /* Make a fair guess for the size of the stack frame of the function
1591 in NODE. This doesn't have to be exact, the result is only used in
1592 the inline heuristics. So we don't want to run the full stack var
1593 packing algorithm (which is quadratic in the number of stack vars).
1594 Instead, we calculate the total size of all stack vars. This turns
1595 out to be a pretty fair estimate -- packing of stack vars doesn't
1596 happen very often. */
1598 HOST_WIDE_INT
1599 estimated_stack_frame_size (struct cgraph_node *node)
1601 HOST_WIDE_INT size = 0;
1602 size_t i;
1603 tree var;
1604 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1606 push_cfun (fn);
1608 init_vars_expansion ();
1610 FOR_EACH_LOCAL_DECL (fn, i, var)
1611 if (auto_var_in_fn_p (var, fn->decl))
1612 size += expand_one_var (var, true, false);
1614 if (stack_vars_num > 0)
1616 /* Fake sorting the stack vars for account_stack_vars (). */
1617 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1618 for (i = 0; i < stack_vars_num; ++i)
1619 stack_vars_sorted[i] = i;
1620 size += account_stack_vars ();
1623 fini_vars_expansion ();
1624 pop_cfun ();
1625 return size;
1628 /* Helper routine to check if a record or union contains an array field. */
1630 static int
1631 record_or_union_type_has_array_p (const_tree tree_type)
1633 tree fields = TYPE_FIELDS (tree_type);
1634 tree f;
1636 for (f = fields; f; f = DECL_CHAIN (f))
1637 if (TREE_CODE (f) == FIELD_DECL)
1639 tree field_type = TREE_TYPE (f);
1640 if (RECORD_OR_UNION_TYPE_P (field_type)
1641 && record_or_union_type_has_array_p (field_type))
1642 return 1;
1643 if (TREE_CODE (field_type) == ARRAY_TYPE)
1644 return 1;
1646 return 0;
1649 /* Check if the current function has local referenced variables that
1650 have their addresses taken, contain an array, or are arrays. */
1652 static bool
1653 stack_protect_decl_p ()
1655 unsigned i;
1656 tree var;
1658 FOR_EACH_LOCAL_DECL (cfun, i, var)
1659 if (!is_global_var (var))
1661 tree var_type = TREE_TYPE (var);
1662 if (TREE_CODE (var) == VAR_DECL
1663 && (TREE_CODE (var_type) == ARRAY_TYPE
1664 || TREE_ADDRESSABLE (var)
1665 || (RECORD_OR_UNION_TYPE_P (var_type)
1666 && record_or_union_type_has_array_p (var_type))))
1667 return true;
1669 return false;
1672 /* Check if the current function has calls that use a return slot. */
1674 static bool
1675 stack_protect_return_slot_p ()
1677 basic_block bb;
1679 FOR_ALL_BB_FN (bb, cfun)
1680 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1681 !gsi_end_p (gsi); gsi_next (&gsi))
1683 gimple stmt = gsi_stmt (gsi);
1684 /* This assumes that calls to internal-only functions never
1685 use a return slot. */
1686 if (is_gimple_call (stmt)
1687 && !gimple_call_internal_p (stmt)
1688 && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
1689 gimple_call_fndecl (stmt)))
1690 return true;
1692 return false;
1695 /* Expand all variables used in the function. */
1697 static rtx_insn *
1698 expand_used_vars (void)
1700 tree var, outer_block = DECL_INITIAL (current_function_decl);
1701 vec<tree> maybe_local_decls = vNULL;
1702 rtx_insn *var_end_seq = NULL;
1703 unsigned i;
1704 unsigned len;
1705 bool gen_stack_protect_signal = false;
1707 /* Compute the phase of the stack frame for this function. */
1709 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1710 int off = STARTING_FRAME_OFFSET % align;
1711 frame_phase = off ? align - off : 0;
1714 /* Set TREE_USED on all variables in the local_decls. */
1715 FOR_EACH_LOCAL_DECL (cfun, i, var)
1716 TREE_USED (var) = 1;
1717 /* Clear TREE_USED on all variables associated with a block scope. */
1718 clear_tree_used (DECL_INITIAL (current_function_decl));
1720 init_vars_expansion ();
1722 if (targetm.use_pseudo_pic_reg ())
1723 pic_offset_table_rtx = gen_reg_rtx (Pmode);
1725 hash_map<tree, tree> ssa_name_decls;
1726 for (i = 0; i < SA.map->num_partitions; i++)
1728 tree var = partition_to_var (SA.map, i);
1730 gcc_assert (!virtual_operand_p (var));
1732 /* Assign decls to each SSA name partition, share decls for partitions
1733 we could have coalesced (those with the same type). */
1734 if (SSA_NAME_VAR (var) == NULL_TREE)
1736 tree *slot = &ssa_name_decls.get_or_insert (TREE_TYPE (var));
1737 if (!*slot)
1738 *slot = create_tmp_reg (TREE_TYPE (var));
1739 replace_ssa_name_symbol (var, *slot);
1742 /* Always allocate space for partitions based on VAR_DECLs. But for
1743 those based on PARM_DECLs or RESULT_DECLs and which matter for the
1744 debug info, there is no need to do so if optimization is disabled
1745 because all the SSA_NAMEs based on these DECLs have been coalesced
1746 into a single partition, which is thus assigned the canonical RTL
1747 location of the DECLs. If in_lto_p, we can't rely on optimize,
1748 a function could be compiled with -O1 -flto first and only the
1749 link performed at -O0. */
1750 if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1751 expand_one_var (var, true, true);
1752 else if (DECL_IGNORED_P (SSA_NAME_VAR (var)) || optimize || in_lto_p)
1754 /* This is a PARM_DECL or RESULT_DECL. For those partitions that
1755 contain the default def (representing the parm or result itself)
1756 we don't do anything here. But those which don't contain the
1757 default def (representing a temporary based on the parm/result)
1758 we need to allocate space just like for normal VAR_DECLs. */
1759 if (!bitmap_bit_p (SA.partition_has_default_def, i))
1761 expand_one_var (var, true, true);
1762 gcc_assert (SA.partition_to_pseudo[i]);
1767 if (flag_stack_protect == SPCT_FLAG_STRONG)
1768 gen_stack_protect_signal
1769 = stack_protect_decl_p () || stack_protect_return_slot_p ();
1771 /* At this point all variables on the local_decls with TREE_USED
1772 set are not associated with any block scope. Lay them out. */
1774 len = vec_safe_length (cfun->local_decls);
1775 FOR_EACH_LOCAL_DECL (cfun, i, var)
1777 bool expand_now = false;
1779 /* Expanded above already. */
1780 if (is_gimple_reg (var))
1782 TREE_USED (var) = 0;
1783 goto next;
1785 /* We didn't set a block for static or extern because it's hard
1786 to tell the difference between a global variable (re)declared
1787 in a local scope, and one that's really declared there to
1788 begin with. And it doesn't really matter much, since we're
1789 not giving them stack space. Expand them now. */
1790 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1791 expand_now = true;
1793 /* Expand variables not associated with any block now. Those created by
1794 the optimizers could be live anywhere in the function. Those that
1795 could possibly have been scoped originally and detached from their
1796 block will have their allocation deferred so we coalesce them with
1797 others when optimization is enabled. */
1798 else if (TREE_USED (var))
1799 expand_now = true;
1801 /* Finally, mark all variables on the list as used. We'll use
1802 this in a moment when we expand those associated with scopes. */
1803 TREE_USED (var) = 1;
1805 if (expand_now)
1806 expand_one_var (var, true, true);
1808 next:
1809 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1811 rtx rtl = DECL_RTL_IF_SET (var);
1813 /* Keep artificial non-ignored vars in cfun->local_decls
1814 chain until instantiate_decls. */
1815 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1816 add_local_decl (cfun, var);
1817 else if (rtl == NULL_RTX)
1818 /* If rtl isn't set yet, which can happen e.g. with
1819 -fstack-protector, retry before returning from this
1820 function. */
1821 maybe_local_decls.safe_push (var);
1825 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
1827 +-----------------+-----------------+
1828 | ...processed... | ...duplicates...|
1829 +-----------------+-----------------+
1831 +-- LEN points here.
1833 We just want the duplicates, as those are the artificial
1834 non-ignored vars that we want to keep until instantiate_decls.
1835 Move them down and truncate the array. */
1836 if (!vec_safe_is_empty (cfun->local_decls))
1837 cfun->local_decls->block_remove (0, len);
1839 /* At this point, all variables within the block tree with TREE_USED
1840 set are actually used by the optimized function. Lay them out. */
1841 expand_used_vars_for_block (outer_block, true);
1843 if (stack_vars_num > 0)
1845 add_scope_conflicts ();
1847 /* If stack protection is enabled, we don't share space between
1848 vulnerable data and non-vulnerable data. */
1849 if (flag_stack_protect != 0
1850 && (flag_stack_protect != SPCT_FLAG_EXPLICIT
1851 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1852 && lookup_attribute ("stack_protect",
1853 DECL_ATTRIBUTES (current_function_decl)))))
1854 add_stack_protection_conflicts ();
1856 /* Now that we have collected all stack variables, and have computed a
1857 minimal interference graph, attempt to save some stack space. */
1858 partition_stack_vars ();
1859 if (dump_file)
1860 dump_stack_var_partition ();
1863 switch (flag_stack_protect)
1865 case SPCT_FLAG_ALL:
1866 create_stack_guard ();
1867 break;
1869 case SPCT_FLAG_STRONG:
1870 if (gen_stack_protect_signal
1871 || cfun->calls_alloca || has_protected_decls
1872 || lookup_attribute ("stack_protect",
1873 DECL_ATTRIBUTES (current_function_decl)))
1874 create_stack_guard ();
1875 break;
1877 case SPCT_FLAG_DEFAULT:
1878 if (cfun->calls_alloca || has_protected_decls
1879 || lookup_attribute ("stack_protect",
1880 DECL_ATTRIBUTES (current_function_decl)))
1881 create_stack_guard ();
1882 break;
1884 case SPCT_FLAG_EXPLICIT:
1885 if (lookup_attribute ("stack_protect",
1886 DECL_ATTRIBUTES (current_function_decl)))
1887 create_stack_guard ();
1888 break;
1889 default:
1893 /* Assign rtl to each variable based on these partitions. */
1894 if (stack_vars_num > 0)
1896 struct stack_vars_data data;
1898 data.asan_vec = vNULL;
1899 data.asan_decl_vec = vNULL;
1900 data.asan_base = NULL_RTX;
1901 data.asan_alignb = 0;
1903 /* Reorder decls to be protected by iterating over the variables
1904 array multiple times, and allocating out of each phase in turn. */
1905 /* ??? We could probably integrate this into the qsort we did
1906 earlier, such that we naturally see these variables first,
1907 and thus naturally allocate things in the right order. */
1908 if (has_protected_decls)
1910 /* Phase 1 contains only character arrays. */
1911 expand_stack_vars (stack_protect_decl_phase_1, &data);
1913 /* Phase 2 contains other kinds of arrays. */
1914 if (flag_stack_protect == SPCT_FLAG_ALL
1915 || flag_stack_protect == SPCT_FLAG_STRONG
1916 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1917 && lookup_attribute ("stack_protect",
1918 DECL_ATTRIBUTES (current_function_decl))))
1919 expand_stack_vars (stack_protect_decl_phase_2, &data);
1922 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK)
1923 /* Phase 3, any partitions that need asan protection
1924 in addition to phase 1 and 2. */
1925 expand_stack_vars (asan_decl_phase_3, &data);
1927 if (!data.asan_vec.is_empty ())
1929 HOST_WIDE_INT prev_offset = frame_offset;
1930 HOST_WIDE_INT offset, sz, redzonesz;
1931 redzonesz = ASAN_RED_ZONE_SIZE;
1932 sz = data.asan_vec[0] - prev_offset;
1933 if (data.asan_alignb > ASAN_RED_ZONE_SIZE
1934 && data.asan_alignb <= 4096
1935 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
1936 redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
1937 & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
1938 offset
1939 = alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE);
1940 data.asan_vec.safe_push (prev_offset);
1941 data.asan_vec.safe_push (offset);
1942 /* Leave space for alignment if STRICT_ALIGNMENT. */
1943 if (STRICT_ALIGNMENT)
1944 alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
1945 << ASAN_SHADOW_SHIFT)
1946 / BITS_PER_UNIT, 1);
1948 var_end_seq
1949 = asan_emit_stack_protection (virtual_stack_vars_rtx,
1950 data.asan_base,
1951 data.asan_alignb,
1952 data.asan_vec.address (),
1953 data.asan_decl_vec.address (),
1954 data.asan_vec.length ());
1957 expand_stack_vars (NULL, &data);
1959 data.asan_vec.release ();
1960 data.asan_decl_vec.release ();
1963 fini_vars_expansion ();
1965 /* If there were any artificial non-ignored vars without rtl
1966 found earlier, see if deferred stack allocation hasn't assigned
1967 rtl to them. */
1968 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
1970 rtx rtl = DECL_RTL_IF_SET (var);
1972 /* Keep artificial non-ignored vars in cfun->local_decls
1973 chain until instantiate_decls. */
1974 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1975 add_local_decl (cfun, var);
1977 maybe_local_decls.release ();
1979 /* If the target requires that FRAME_OFFSET be aligned, do it. */
1980 if (STACK_ALIGNMENT_NEEDED)
1982 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1983 if (!FRAME_GROWS_DOWNWARD)
1984 frame_offset += align - 1;
1985 frame_offset &= -align;
1988 return var_end_seq;
1992 /* If we need to produce a detailed dump, print the tree representation
1993 for STMT to the dump file. SINCE is the last RTX after which the RTL
1994 generated for STMT should have been appended. */
1996 static void
1997 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx_insn *since)
1999 if (dump_file && (dump_flags & TDF_DETAILS))
2001 fprintf (dump_file, "\n;; ");
2002 print_gimple_stmt (dump_file, stmt, 0,
2003 TDF_SLIM | (dump_flags & TDF_LINENO));
2004 fprintf (dump_file, "\n");
2006 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
2010 /* Maps the blocks that do not contain tree labels to rtx labels. */
2012 static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
2014 /* Returns the label_rtx expression for a label starting basic block BB. */
2016 static rtx_code_label *
2017 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
2019 gimple_stmt_iterator gsi;
2020 tree lab;
2022 if (bb->flags & BB_RTL)
2023 return block_label (bb);
2025 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
2026 if (elt)
2027 return *elt;
2029 /* Find the tree label if it is present. */
2031 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2033 glabel *lab_stmt;
2035 lab_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
2036 if (!lab_stmt)
2037 break;
2039 lab = gimple_label_label (lab_stmt);
2040 if (DECL_NONLOCAL (lab))
2041 break;
2043 return jump_target_rtx (lab);
2046 rtx_code_label *l = gen_label_rtx ();
2047 lab_rtx_for_bb->put (bb, l);
2048 return l;
2052 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2053 of a basic block where we just expanded the conditional at the end,
2054 possibly clean up the CFG and instruction sequence. LAST is the
2055 last instruction before the just emitted jump sequence. */
2057 static void
2058 maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2060 /* Special case: when jumpif decides that the condition is
2061 trivial it emits an unconditional jump (and the necessary
2062 barrier). But we still have two edges, the fallthru one is
2063 wrong. purge_dead_edges would clean this up later. Unfortunately
2064 we have to insert insns (and split edges) before
2065 find_many_sub_basic_blocks and hence before purge_dead_edges.
2066 But splitting edges might create new blocks which depend on the
2067 fact that if there are two edges there's no barrier. So the
2068 barrier would get lost and verify_flow_info would ICE. Instead
2069 of auditing all edge splitters to care for the barrier (which
2070 normally isn't there in a cleaned CFG), fix it here. */
2071 if (BARRIER_P (get_last_insn ()))
2073 rtx_insn *insn;
2074 remove_edge (e);
2075 /* Now, we have a single successor block, if we have insns to
2076 insert on the remaining edge we potentially will insert
2077 it at the end of this block (if the dest block isn't feasible)
2078 in order to avoid splitting the edge. This insertion will take
2079 place in front of the last jump. But we might have emitted
2080 multiple jumps (conditional and one unconditional) to the
2081 same destination. Inserting in front of the last one then
2082 is a problem. See PR 40021. We fix this by deleting all
2083 jumps except the last unconditional one. */
2084 insn = PREV_INSN (get_last_insn ());
2085 /* Make sure we have an unconditional jump. Otherwise we're
2086 confused. */
2087 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2088 for (insn = PREV_INSN (insn); insn != last;)
2090 insn = PREV_INSN (insn);
2091 if (JUMP_P (NEXT_INSN (insn)))
2093 if (!any_condjump_p (NEXT_INSN (insn)))
2095 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2096 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2098 delete_insn (NEXT_INSN (insn));
2104 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2105 Returns a new basic block if we've terminated the current basic
2106 block and created a new one. */
2108 static basic_block
2109 expand_gimple_cond (basic_block bb, gcond *stmt)
2111 basic_block new_bb, dest;
2112 edge new_edge;
2113 edge true_edge;
2114 edge false_edge;
2115 rtx_insn *last2, *last;
2116 enum tree_code code;
2117 tree op0, op1;
2119 code = gimple_cond_code (stmt);
2120 op0 = gimple_cond_lhs (stmt);
2121 op1 = gimple_cond_rhs (stmt);
2122 /* We're sometimes presented with such code:
2123 D.123_1 = x < y;
2124 if (D.123_1 != 0)
2126 This would expand to two comparisons which then later might
2127 be cleaned up by combine. But some pattern matchers like if-conversion
2128 work better when there's only one compare, so make up for this
2129 here as special exception if TER would have made the same change. */
2130 if (SA.values
2131 && TREE_CODE (op0) == SSA_NAME
2132 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2133 && TREE_CODE (op1) == INTEGER_CST
2134 && ((gimple_cond_code (stmt) == NE_EXPR
2135 && integer_zerop (op1))
2136 || (gimple_cond_code (stmt) == EQ_EXPR
2137 && integer_onep (op1)))
2138 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2140 gimple second = SSA_NAME_DEF_STMT (op0);
2141 if (gimple_code (second) == GIMPLE_ASSIGN)
2143 enum tree_code code2 = gimple_assign_rhs_code (second);
2144 if (TREE_CODE_CLASS (code2) == tcc_comparison)
2146 code = code2;
2147 op0 = gimple_assign_rhs1 (second);
2148 op1 = gimple_assign_rhs2 (second);
2150 /* If jumps are cheap and the target does not support conditional
2151 compare, turn some more codes into jumpy sequences. */
2152 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4
2153 && targetm.gen_ccmp_first == NULL)
2155 if ((code2 == BIT_AND_EXPR
2156 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2157 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2158 || code2 == TRUTH_AND_EXPR)
2160 code = TRUTH_ANDIF_EXPR;
2161 op0 = gimple_assign_rhs1 (second);
2162 op1 = gimple_assign_rhs2 (second);
2164 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2166 code = TRUTH_ORIF_EXPR;
2167 op0 = gimple_assign_rhs1 (second);
2168 op1 = gimple_assign_rhs2 (second);
2174 last2 = last = get_last_insn ();
2176 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2177 set_curr_insn_location (gimple_location (stmt));
2179 /* These flags have no purpose in RTL land. */
2180 true_edge->flags &= ~EDGE_TRUE_VALUE;
2181 false_edge->flags &= ~EDGE_FALSE_VALUE;
2183 /* We can either have a pure conditional jump with one fallthru edge or
2184 two-way jump that needs to be decomposed into two basic blocks. */
2185 if (false_edge->dest == bb->next_bb)
2187 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2188 true_edge->probability);
2189 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2190 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2191 set_curr_insn_location (true_edge->goto_locus);
2192 false_edge->flags |= EDGE_FALLTHRU;
2193 maybe_cleanup_end_of_block (false_edge, last);
2194 return NULL;
2196 if (true_edge->dest == bb->next_bb)
2198 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2199 false_edge->probability);
2200 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2201 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2202 set_curr_insn_location (false_edge->goto_locus);
2203 true_edge->flags |= EDGE_FALLTHRU;
2204 maybe_cleanup_end_of_block (true_edge, last);
2205 return NULL;
2208 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2209 true_edge->probability);
2210 last = get_last_insn ();
2211 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2212 set_curr_insn_location (false_edge->goto_locus);
2213 emit_jump (label_rtx_for_bb (false_edge->dest));
2215 BB_END (bb) = last;
2216 if (BARRIER_P (BB_END (bb)))
2217 BB_END (bb) = PREV_INSN (BB_END (bb));
2218 update_bb_for_insn (bb);
2220 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2221 dest = false_edge->dest;
2222 redirect_edge_succ (false_edge, new_bb);
2223 false_edge->flags |= EDGE_FALLTHRU;
2224 new_bb->count = false_edge->count;
2225 new_bb->frequency = EDGE_FREQUENCY (false_edge);
2226 add_bb_to_loop (new_bb, bb->loop_father);
2227 new_edge = make_edge (new_bb, dest, 0);
2228 new_edge->probability = REG_BR_PROB_BASE;
2229 new_edge->count = new_bb->count;
2230 if (BARRIER_P (BB_END (new_bb)))
2231 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2232 update_bb_for_insn (new_bb);
2234 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2236 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2238 set_curr_insn_location (true_edge->goto_locus);
2239 true_edge->goto_locus = curr_insn_location ();
2242 return new_bb;
2245 /* Mark all calls that can have a transaction restart. */
2247 static void
2248 mark_transaction_restart_calls (gimple stmt)
2250 struct tm_restart_node dummy;
2251 tm_restart_node **slot;
2253 if (!cfun->gimple_df->tm_restart)
2254 return;
2256 dummy.stmt = stmt;
2257 slot = cfun->gimple_df->tm_restart->find_slot (&dummy, NO_INSERT);
2258 if (slot)
2260 struct tm_restart_node *n = *slot;
2261 tree list = n->label_or_list;
2262 rtx_insn *insn;
2264 for (insn = next_real_insn (get_last_insn ());
2265 !CALL_P (insn);
2266 insn = next_real_insn (insn))
2267 continue;
2269 if (TREE_CODE (list) == LABEL_DECL)
2270 add_reg_note (insn, REG_TM, label_rtx (list));
2271 else
2272 for (; list ; list = TREE_CHAIN (list))
2273 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2277 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2278 statement STMT. */
2280 static void
2281 expand_call_stmt (gcall *stmt)
2283 tree exp, decl, lhs;
2284 bool builtin_p;
2285 size_t i;
2287 if (gimple_call_internal_p (stmt))
2289 expand_internal_call (stmt);
2290 return;
2293 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2295 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2296 decl = gimple_call_fndecl (stmt);
2297 builtin_p = decl && DECL_BUILT_IN (decl);
2299 /* If this is not a builtin function, the function type through which the
2300 call is made may be different from the type of the function. */
2301 if (!builtin_p)
2302 CALL_EXPR_FN (exp)
2303 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2304 CALL_EXPR_FN (exp));
2306 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2307 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2309 for (i = 0; i < gimple_call_num_args (stmt); i++)
2311 tree arg = gimple_call_arg (stmt, i);
2312 gimple def;
2313 /* TER addresses into arguments of builtin functions so we have a
2314 chance to infer more correct alignment information. See PR39954. */
2315 if (builtin_p
2316 && TREE_CODE (arg) == SSA_NAME
2317 && (def = get_gimple_for_ssa_name (arg))
2318 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2319 arg = gimple_assign_rhs1 (def);
2320 CALL_EXPR_ARG (exp, i) = arg;
2323 if (gimple_has_side_effects (stmt))
2324 TREE_SIDE_EFFECTS (exp) = 1;
2326 if (gimple_call_nothrow_p (stmt))
2327 TREE_NOTHROW (exp) = 1;
2329 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2330 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2331 if (decl
2332 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2333 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2334 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2335 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2336 else
2337 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2338 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2339 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2340 CALL_WITH_BOUNDS_P (exp) = gimple_call_with_bounds_p (stmt);
2342 /* Ensure RTL is created for debug args. */
2343 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2345 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2346 unsigned int ix;
2347 tree dtemp;
2349 if (debug_args)
2350 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2352 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2353 expand_debug_expr (dtemp);
2357 lhs = gimple_call_lhs (stmt);
2358 if (lhs)
2359 expand_assignment (lhs, exp, false);
2360 else
2361 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2363 mark_transaction_restart_calls (stmt);
2367 /* Generate RTL for an asm statement (explicit assembler code).
2368 STRING is a STRING_CST node containing the assembler code text,
2369 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
2370 insn is volatile; don't optimize it. */
2372 static void
2373 expand_asm_loc (tree string, int vol, location_t locus)
2375 rtx body;
2377 if (TREE_CODE (string) == ADDR_EXPR)
2378 string = TREE_OPERAND (string, 0);
2380 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2381 ggc_strdup (TREE_STRING_POINTER (string)),
2382 locus);
2384 MEM_VOLATILE_P (body) = vol;
2386 emit_insn (body);
2389 /* Return the number of times character C occurs in string S. */
2390 static int
2391 n_occurrences (int c, const char *s)
2393 int n = 0;
2394 while (*s)
2395 n += (*s++ == c);
2396 return n;
2399 /* A subroutine of expand_asm_operands. Check that all operands have
2400 the same number of alternatives. Return true if so. */
2402 static bool
2403 check_operand_nalternatives (const vec<const char *> &constraints)
2405 unsigned len = constraints.length();
2406 if (len > 0)
2408 int nalternatives = n_occurrences (',', constraints[0]);
2410 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2412 error ("too many alternatives in %<asm%>");
2413 return false;
2416 for (unsigned i = 1; i < len; ++i)
2417 if (n_occurrences (',', constraints[i]) != nalternatives)
2419 error ("operand constraints for %<asm%> differ "
2420 "in number of alternatives");
2421 return false;
2424 return true;
2427 /* Check for overlap between registers marked in CLOBBERED_REGS and
2428 anything inappropriate in T. Emit error and return the register
2429 variable definition for error, NULL_TREE for ok. */
2431 static bool
2432 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
2434 /* Conflicts between asm-declared register variables and the clobber
2435 list are not allowed. */
2436 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2438 if (overlap)
2440 error ("asm-specifier for variable %qE conflicts with asm clobber list",
2441 DECL_NAME (overlap));
2443 /* Reset registerness to stop multiple errors emitted for a single
2444 variable. */
2445 DECL_REGISTER (overlap) = 0;
2446 return true;
2449 return false;
2452 /* Generate RTL for an asm statement with arguments.
2453 STRING is the instruction template.
2454 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
2455 Each output or input has an expression in the TREE_VALUE and
2456 a tree list in TREE_PURPOSE which in turn contains a constraint
2457 name in TREE_VALUE (or NULL_TREE) and a constraint string
2458 in TREE_PURPOSE.
2459 CLOBBERS is a list of STRING_CST nodes each naming a hard register
2460 that is clobbered by this insn.
2462 LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
2463 should be the fallthru basic block of the asm goto.
2465 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
2466 Some elements of OUTPUTS may be replaced with trees representing temporary
2467 values. The caller should copy those temporary values to the originally
2468 specified lvalues.
2470 VOL nonzero means the insn is volatile; don't optimize it. */
2472 static void
2473 expand_asm_stmt (gasm *stmt)
2475 class save_input_location
2477 location_t old;
2479 public:
2480 explicit save_input_location(location_t where)
2482 old = input_location;
2483 input_location = where;
2486 ~save_input_location()
2488 input_location = old;
2492 location_t locus = gimple_location (stmt);
2494 if (gimple_asm_input_p (stmt))
2496 const char *s = gimple_asm_string (stmt);
2497 tree string = build_string (strlen (s), s);
2498 expand_asm_loc (string, gimple_asm_volatile_p (stmt), locus);
2499 return;
2502 /* There are some legacy diagnostics in here, and also avoids a
2503 sixth parameger to targetm.md_asm_adjust. */
2504 save_input_location s_i_l(locus);
2506 unsigned noutputs = gimple_asm_noutputs (stmt);
2507 unsigned ninputs = gimple_asm_ninputs (stmt);
2508 unsigned nlabels = gimple_asm_nlabels (stmt);
2509 unsigned i;
2511 /* ??? Diagnose during gimplification? */
2512 if (ninputs + noutputs + nlabels > MAX_RECOG_OPERANDS)
2514 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
2515 return;
2518 auto_vec<tree, MAX_RECOG_OPERANDS> output_tvec;
2519 auto_vec<tree, MAX_RECOG_OPERANDS> input_tvec;
2520 auto_vec<const char *, MAX_RECOG_OPERANDS> constraints;
2522 /* Copy the gimple vectors into new vectors that we can manipulate. */
2524 output_tvec.safe_grow (noutputs);
2525 input_tvec.safe_grow (ninputs);
2526 constraints.safe_grow (noutputs + ninputs);
2528 for (i = 0; i < noutputs; ++i)
2530 tree t = gimple_asm_output_op (stmt, i);
2531 output_tvec[i] = TREE_VALUE (t);
2532 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2534 for (i = 0; i < ninputs; i++)
2536 tree t = gimple_asm_input_op (stmt, i);
2537 input_tvec[i] = TREE_VALUE (t);
2538 constraints[i + noutputs]
2539 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2542 /* ??? Diagnose during gimplification? */
2543 if (! check_operand_nalternatives (constraints))
2544 return;
2546 /* Count the number of meaningful clobbered registers, ignoring what
2547 we would ignore later. */
2548 auto_vec<rtx> clobber_rvec;
2549 HARD_REG_SET clobbered_regs;
2550 CLEAR_HARD_REG_SET (clobbered_regs);
2552 if (unsigned n = gimple_asm_nclobbers (stmt))
2554 clobber_rvec.reserve (n);
2555 for (i = 0; i < n; i++)
2557 tree t = gimple_asm_clobber_op (stmt, i);
2558 const char *regname = TREE_STRING_POINTER (TREE_VALUE (t));
2559 int nregs, j;
2561 j = decode_reg_name_and_count (regname, &nregs);
2562 if (j < 0)
2564 if (j == -2)
2566 /* ??? Diagnose during gimplification? */
2567 error ("unknown register name %qs in %<asm%>", regname);
2569 else if (j == -4)
2571 rtx x = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2572 clobber_rvec.safe_push (x);
2574 else
2576 /* Otherwise we should have -1 == empty string
2577 or -3 == cc, which is not a register. */
2578 gcc_assert (j == -1 || j == -3);
2581 else
2582 for (int reg = j; reg < j + nregs; reg++)
2584 /* Clobbering the PIC register is an error. */
2585 if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
2587 /* ??? Diagnose during gimplification? */
2588 error ("PIC register clobbered by %qs in %<asm%>",
2589 regname);
2590 return;
2593 SET_HARD_REG_BIT (clobbered_regs, reg);
2594 rtx x = gen_rtx_REG (reg_raw_mode[reg], reg);
2595 clobber_rvec.safe_push (x);
2599 unsigned nclobbers = clobber_rvec.length();
2601 /* First pass over inputs and outputs checks validity and sets
2602 mark_addressable if needed. */
2603 /* ??? Diagnose during gimplification? */
2605 for (i = 0; i < noutputs; ++i)
2607 tree val = output_tvec[i];
2608 tree type = TREE_TYPE (val);
2609 const char *constraint;
2610 bool is_inout;
2611 bool allows_reg;
2612 bool allows_mem;
2614 /* Try to parse the output constraint. If that fails, there's
2615 no point in going further. */
2616 constraint = constraints[i];
2617 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
2618 &allows_mem, &allows_reg, &is_inout))
2619 return;
2621 if (! allows_reg
2622 && (allows_mem
2623 || is_inout
2624 || (DECL_P (val)
2625 && REG_P (DECL_RTL (val))
2626 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
2627 mark_addressable (val);
2630 for (i = 0; i < ninputs; ++i)
2632 bool allows_reg, allows_mem;
2633 const char *constraint;
2635 constraint = constraints[i + noutputs];
2636 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2637 constraints.address (),
2638 &allows_mem, &allows_reg))
2639 return;
2641 if (! allows_reg && allows_mem)
2642 mark_addressable (input_tvec[i]);
2645 /* Second pass evaluates arguments. */
2647 /* Make sure stack is consistent for asm goto. */
2648 if (nlabels > 0)
2649 do_pending_stack_adjust ();
2650 int old_generating_concat_p = generating_concat_p;
2652 /* Vector of RTX's of evaluated output operands. */
2653 auto_vec<rtx, MAX_RECOG_OPERANDS> output_rvec;
2654 auto_vec<int, MAX_RECOG_OPERANDS> inout_opnum;
2655 rtx_insn *after_rtl_seq = NULL, *after_rtl_end = NULL;
2657 output_rvec.safe_grow (noutputs);
2659 for (i = 0; i < noutputs; ++i)
2661 tree val = output_tvec[i];
2662 tree type = TREE_TYPE (val);
2663 bool is_inout, allows_reg, allows_mem, ok;
2664 rtx op;
2666 ok = parse_output_constraint (&constraints[i], i, ninputs,
2667 noutputs, &allows_mem, &allows_reg,
2668 &is_inout);
2669 gcc_assert (ok);
2671 /* If an output operand is not a decl or indirect ref and our constraint
2672 allows a register, make a temporary to act as an intermediate.
2673 Make the asm insn write into that, then we will copy it to
2674 the real output operand. Likewise for promoted variables. */
2676 generating_concat_p = 0;
2678 if ((TREE_CODE (val) == INDIRECT_REF
2679 && allows_mem)
2680 || (DECL_P (val)
2681 && (allows_mem || REG_P (DECL_RTL (val)))
2682 && ! (REG_P (DECL_RTL (val))
2683 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
2684 || ! allows_reg
2685 || is_inout)
2687 op = expand_expr (val, NULL_RTX, VOIDmode,
2688 !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
2689 if (MEM_P (op))
2690 op = validize_mem (op);
2692 if (! allows_reg && !MEM_P (op))
2693 error ("output number %d not directly addressable", i);
2694 if ((! allows_mem && MEM_P (op))
2695 || GET_CODE (op) == CONCAT)
2697 rtx old_op = op;
2698 op = gen_reg_rtx (GET_MODE (op));
2700 generating_concat_p = old_generating_concat_p;
2702 if (is_inout)
2703 emit_move_insn (op, old_op);
2705 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2706 emit_move_insn (old_op, op);
2707 after_rtl_seq = get_insns ();
2708 after_rtl_end = get_last_insn ();
2709 end_sequence ();
2712 else
2714 op = assign_temp (type, 0, 1);
2715 op = validize_mem (op);
2716 if (!MEM_P (op) && TREE_CODE (val) == SSA_NAME)
2717 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (val), op);
2719 generating_concat_p = old_generating_concat_p;
2721 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2722 expand_assignment (val, make_tree (type, op), false);
2723 after_rtl_seq = get_insns ();
2724 after_rtl_end = get_last_insn ();
2725 end_sequence ();
2727 output_rvec[i] = op;
2729 if (is_inout)
2730 inout_opnum.safe_push (i);
2733 auto_vec<rtx, MAX_RECOG_OPERANDS> input_rvec;
2734 auto_vec<machine_mode, MAX_RECOG_OPERANDS> input_mode;
2736 input_rvec.safe_grow (ninputs);
2737 input_mode.safe_grow (ninputs);
2739 generating_concat_p = 0;
2741 for (i = 0; i < ninputs; ++i)
2743 tree val = input_tvec[i];
2744 tree type = TREE_TYPE (val);
2745 bool allows_reg, allows_mem, ok;
2746 const char *constraint;
2747 rtx op;
2749 constraint = constraints[i + noutputs];
2750 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2751 constraints.address (),
2752 &allows_mem, &allows_reg);
2753 gcc_assert (ok);
2755 /* EXPAND_INITIALIZER will not generate code for valid initializer
2756 constants, but will still generate code for other types of operand.
2757 This is the behavior we want for constant constraints. */
2758 op = expand_expr (val, NULL_RTX, VOIDmode,
2759 allows_reg ? EXPAND_NORMAL
2760 : allows_mem ? EXPAND_MEMORY
2761 : EXPAND_INITIALIZER);
2763 /* Never pass a CONCAT to an ASM. */
2764 if (GET_CODE (op) == CONCAT)
2765 op = force_reg (GET_MODE (op), op);
2766 else if (MEM_P (op))
2767 op = validize_mem (op);
2769 if (asm_operand_ok (op, constraint, NULL) <= 0)
2771 if (allows_reg && TYPE_MODE (type) != BLKmode)
2772 op = force_reg (TYPE_MODE (type), op);
2773 else if (!allows_mem)
2774 warning (0, "asm operand %d probably doesn%'t match constraints",
2775 i + noutputs);
2776 else if (MEM_P (op))
2778 /* We won't recognize either volatile memory or memory
2779 with a queued address as available a memory_operand
2780 at this point. Ignore it: clearly this *is* a memory. */
2782 else
2783 gcc_unreachable ();
2785 input_rvec[i] = op;
2786 input_mode[i] = TYPE_MODE (type);
2789 /* For in-out operands, copy output rtx to input rtx. */
2790 unsigned ninout = inout_opnum.length();
2791 for (i = 0; i < ninout; i++)
2793 int j = inout_opnum[i];
2794 rtx o = output_rvec[j];
2796 input_rvec.safe_push (o);
2797 input_mode.safe_push (GET_MODE (o));
2799 char buffer[16];
2800 sprintf (buffer, "%d", j);
2801 constraints.safe_push (ggc_strdup (buffer));
2803 ninputs += ninout;
2805 /* Sometimes we wish to automatically clobber registers across an asm.
2806 Case in point is when the i386 backend moved from cc0 to a hard reg --
2807 maintaining source-level compatibility means automatically clobbering
2808 the flags register. */
2809 rtx_insn *after_md_seq = NULL;
2810 if (targetm.md_asm_adjust)
2811 after_md_seq = targetm.md_asm_adjust (output_rvec, input_rvec,
2812 constraints, clobber_rvec,
2813 clobbered_regs);
2815 /* Do not allow the hook to change the output and input count,
2816 lest it mess up the operand numbering. */
2817 gcc_assert (output_rvec.length() == noutputs);
2818 gcc_assert (input_rvec.length() == ninputs);
2819 gcc_assert (constraints.length() == noutputs + ninputs);
2821 /* But it certainly can adjust the clobbers. */
2822 nclobbers = clobber_rvec.length();
2824 /* Third pass checks for easy conflicts. */
2825 /* ??? Why are we doing this on trees instead of rtx. */
2827 bool clobber_conflict_found = 0;
2828 for (i = 0; i < noutputs; ++i)
2829 if (tree_conflicts_with_clobbers_p (output_tvec[i], &clobbered_regs))
2830 clobber_conflict_found = 1;
2831 for (i = 0; i < ninputs - ninout; ++i)
2832 if (tree_conflicts_with_clobbers_p (input_tvec[i], &clobbered_regs))
2833 clobber_conflict_found = 1;
2835 /* Make vectors for the expression-rtx, constraint strings,
2836 and named operands. */
2838 rtvec argvec = rtvec_alloc (ninputs);
2839 rtvec constraintvec = rtvec_alloc (ninputs);
2840 rtvec labelvec = rtvec_alloc (nlabels);
2842 rtx body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
2843 : GET_MODE (output_rvec[0])),
2844 ggc_strdup (gimple_asm_string (stmt)),
2845 empty_string, 0, argvec, constraintvec,
2846 labelvec, locus);
2847 MEM_VOLATILE_P (body) = gimple_asm_volatile_p (stmt);
2849 for (i = 0; i < ninputs; ++i)
2851 ASM_OPERANDS_INPUT (body, i) = input_rvec[i];
2852 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
2853 = gen_rtx_ASM_INPUT_loc (input_mode[i],
2854 constraints[i + noutputs],
2855 locus);
2858 /* Copy labels to the vector. */
2859 rtx_code_label *fallthru_label = NULL;
2860 if (nlabels > 0)
2862 basic_block fallthru_bb = NULL;
2863 edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
2864 if (fallthru)
2865 fallthru_bb = fallthru->dest;
2867 for (i = 0; i < nlabels; ++i)
2869 tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
2870 rtx_insn *r;
2871 /* If asm goto has any labels in the fallthru basic block, use
2872 a label that we emit immediately after the asm goto. Expansion
2873 may insert further instructions into the same basic block after
2874 asm goto and if we don't do this, insertion of instructions on
2875 the fallthru edge might misbehave. See PR58670. */
2876 if (fallthru_bb && label_to_block_fn (cfun, label) == fallthru_bb)
2878 if (fallthru_label == NULL_RTX)
2879 fallthru_label = gen_label_rtx ();
2880 r = fallthru_label;
2882 else
2883 r = label_rtx (label);
2884 ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
2888 /* Now, for each output, construct an rtx
2889 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
2890 ARGVEC CONSTRAINTS OPNAMES))
2891 If there is more than one, put them inside a PARALLEL. */
2893 if (nlabels > 0 && nclobbers == 0)
2895 gcc_assert (noutputs == 0);
2896 emit_jump_insn (body);
2898 else if (noutputs == 0 && nclobbers == 0)
2900 /* No output operands: put in a raw ASM_OPERANDS rtx. */
2901 emit_insn (body);
2903 else if (noutputs == 1 && nclobbers == 0)
2905 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
2906 emit_insn (gen_rtx_SET (output_rvec[0], body));
2908 else
2910 rtx obody = body;
2911 int num = noutputs;
2913 if (num == 0)
2914 num = 1;
2916 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
2918 /* For each output operand, store a SET. */
2919 for (i = 0; i < noutputs; ++i)
2921 rtx src, o = output_rvec[i];
2922 if (i == 0)
2924 ASM_OPERANDS_OUTPUT_CONSTRAINT (obody) = constraints[0];
2925 src = obody;
2927 else
2929 src = gen_rtx_ASM_OPERANDS (GET_MODE (o),
2930 ASM_OPERANDS_TEMPLATE (obody),
2931 constraints[i], i, argvec,
2932 constraintvec, labelvec, locus);
2933 MEM_VOLATILE_P (src) = gimple_asm_volatile_p (stmt);
2935 XVECEXP (body, 0, i) = gen_rtx_SET (o, src);
2938 /* If there are no outputs (but there are some clobbers)
2939 store the bare ASM_OPERANDS into the PARALLEL. */
2940 if (i == 0)
2941 XVECEXP (body, 0, i++) = obody;
2943 /* Store (clobber REG) for each clobbered register specified. */
2944 for (unsigned j = 0; j < nclobbers; ++j)
2946 rtx clobbered_reg = clobber_rvec[j];
2948 /* Do sanity check for overlap between clobbers and respectively
2949 input and outputs that hasn't been handled. Such overlap
2950 should have been detected and reported above. */
2951 if (!clobber_conflict_found && REG_P (clobbered_reg))
2953 /* We test the old body (obody) contents to avoid
2954 tripping over the under-construction body. */
2955 for (unsigned k = 0; k < noutputs; ++k)
2956 if (reg_overlap_mentioned_p (clobbered_reg, output_rvec[k]))
2957 internal_error ("asm clobber conflict with output operand");
2959 for (unsigned k = 0; k < ninputs - ninout; ++k)
2960 if (reg_overlap_mentioned_p (clobbered_reg, input_rvec[k]))
2961 internal_error ("asm clobber conflict with input operand");
2964 XVECEXP (body, 0, i++) = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
2967 if (nlabels > 0)
2968 emit_jump_insn (body);
2969 else
2970 emit_insn (body);
2973 generating_concat_p = old_generating_concat_p;
2975 if (fallthru_label)
2976 emit_label (fallthru_label);
2978 if (after_md_seq)
2979 emit_insn (after_md_seq);
2980 if (after_rtl_seq)
2981 emit_insn (after_rtl_seq);
2983 free_temp_slots ();
2984 crtl->has_asm_statement = 1;
2987 /* Emit code to jump to the address
2988 specified by the pointer expression EXP. */
2990 static void
2991 expand_computed_goto (tree exp)
2993 rtx x = expand_normal (exp);
2995 do_pending_stack_adjust ();
2996 emit_indirect_jump (x);
2999 /* Generate RTL code for a `goto' statement with target label LABEL.
3000 LABEL should be a LABEL_DECL tree node that was or will later be
3001 defined with `expand_label'. */
3003 static void
3004 expand_goto (tree label)
3006 #ifdef ENABLE_CHECKING
3007 /* Check for a nonlocal goto to a containing function. Should have
3008 gotten translated to __builtin_nonlocal_goto. */
3009 tree context = decl_function_context (label);
3010 gcc_assert (!context || context == current_function_decl);
3011 #endif
3013 emit_jump (jump_target_rtx (label));
3016 /* Output a return with no value. */
3018 static void
3019 expand_null_return_1 (void)
3021 clear_pending_stack_adjust ();
3022 do_pending_stack_adjust ();
3023 emit_jump (return_label);
3026 /* Generate RTL to return from the current function, with no value.
3027 (That is, we do not do anything about returning any value.) */
3029 void
3030 expand_null_return (void)
3032 /* If this function was declared to return a value, but we
3033 didn't, clobber the return registers so that they are not
3034 propagated live to the rest of the function. */
3035 clobber_return_register ();
3037 expand_null_return_1 ();
3040 /* Generate RTL to return from the current function, with value VAL. */
3042 static void
3043 expand_value_return (rtx val)
3045 /* Copy the value to the return location unless it's already there. */
3047 tree decl = DECL_RESULT (current_function_decl);
3048 rtx return_reg = DECL_RTL (decl);
3049 if (return_reg != val)
3051 tree funtype = TREE_TYPE (current_function_decl);
3052 tree type = TREE_TYPE (decl);
3053 int unsignedp = TYPE_UNSIGNED (type);
3054 machine_mode old_mode = DECL_MODE (decl);
3055 machine_mode mode;
3056 if (DECL_BY_REFERENCE (decl))
3057 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3058 else
3059 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3061 if (mode != old_mode)
3062 val = convert_modes (mode, old_mode, val, unsignedp);
3064 if (GET_CODE (return_reg) == PARALLEL)
3065 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3066 else
3067 emit_move_insn (return_reg, val);
3070 expand_null_return_1 ();
3073 /* Generate RTL to evaluate the expression RETVAL and return it
3074 from the current function. */
3076 static void
3077 expand_return (tree retval, tree bounds)
3079 rtx result_rtl;
3080 rtx val = 0;
3081 tree retval_rhs;
3082 rtx bounds_rtl;
3084 /* If function wants no value, give it none. */
3085 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3087 expand_normal (retval);
3088 expand_null_return ();
3089 return;
3092 if (retval == error_mark_node)
3094 /* Treat this like a return of no value from a function that
3095 returns a value. */
3096 expand_null_return ();
3097 return;
3099 else if ((TREE_CODE (retval) == MODIFY_EXPR
3100 || TREE_CODE (retval) == INIT_EXPR)
3101 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3102 retval_rhs = TREE_OPERAND (retval, 1);
3103 else
3104 retval_rhs = retval;
3106 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3108 /* Put returned bounds to the right place. */
3109 bounds_rtl = DECL_BOUNDS_RTL (DECL_RESULT (current_function_decl));
3110 if (bounds_rtl)
3112 rtx addr = NULL;
3113 rtx bnd = NULL;
3115 if (bounds && bounds != error_mark_node)
3117 bnd = expand_normal (bounds);
3118 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3120 else if (REG_P (bounds_rtl))
3122 if (bounds)
3123 bnd = chkp_expand_zero_bounds ();
3124 else
3126 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3127 addr = gen_rtx_MEM (Pmode, addr);
3128 bnd = targetm.calls.load_bounds_for_arg (addr, NULL, NULL);
3131 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3133 else
3135 int n;
3137 gcc_assert (GET_CODE (bounds_rtl) == PARALLEL);
3139 if (bounds)
3140 bnd = chkp_expand_zero_bounds ();
3141 else
3143 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3144 addr = gen_rtx_MEM (Pmode, addr);
3147 for (n = 0; n < XVECLEN (bounds_rtl, 0); n++)
3149 rtx slot = XEXP (XVECEXP (bounds_rtl, 0, n), 0);
3150 if (!bounds)
3152 rtx offs = XEXP (XVECEXP (bounds_rtl, 0, n), 1);
3153 rtx from = adjust_address (addr, Pmode, INTVAL (offs));
3154 bnd = targetm.calls.load_bounds_for_arg (from, NULL, NULL);
3156 targetm.calls.store_returned_bounds (slot, bnd);
3160 else if (chkp_function_instrumented_p (current_function_decl)
3161 && !BOUNDED_P (retval_rhs)
3162 && chkp_type_has_pointer (TREE_TYPE (retval_rhs))
3163 && TREE_CODE (retval_rhs) != RESULT_DECL)
3165 rtx addr = expand_normal (build_fold_addr_expr (retval_rhs));
3166 addr = gen_rtx_MEM (Pmode, addr);
3168 gcc_assert (MEM_P (result_rtl));
3170 chkp_copy_bounds_for_stack_parm (result_rtl, addr, TREE_TYPE (retval_rhs));
3173 /* If we are returning the RESULT_DECL, then the value has already
3174 been stored into it, so we don't have to do anything special. */
3175 if (TREE_CODE (retval_rhs) == RESULT_DECL)
3176 expand_value_return (result_rtl);
3178 /* If the result is an aggregate that is being returned in one (or more)
3179 registers, load the registers here. */
3181 else if (retval_rhs != 0
3182 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3183 && REG_P (result_rtl))
3185 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3186 if (val)
3188 /* Use the mode of the result value on the return register. */
3189 PUT_MODE (result_rtl, GET_MODE (val));
3190 expand_value_return (val);
3192 else
3193 expand_null_return ();
3195 else if (retval_rhs != 0
3196 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3197 && (REG_P (result_rtl)
3198 || (GET_CODE (result_rtl) == PARALLEL)))
3200 /* Compute the return value into a temporary (usually a pseudo reg). */
3202 = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3203 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3204 val = force_not_mem (val);
3205 expand_value_return (val);
3207 else
3209 /* No hard reg used; calculate value into hard return reg. */
3210 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3211 expand_value_return (result_rtl);
3215 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3216 STMT that doesn't require special handling for outgoing edges. That
3217 is no tailcalls and no GIMPLE_COND. */
3219 static void
3220 expand_gimple_stmt_1 (gimple stmt)
3222 tree op0;
3224 set_curr_insn_location (gimple_location (stmt));
3226 switch (gimple_code (stmt))
3228 case GIMPLE_GOTO:
3229 op0 = gimple_goto_dest (stmt);
3230 if (TREE_CODE (op0) == LABEL_DECL)
3231 expand_goto (op0);
3232 else
3233 expand_computed_goto (op0);
3234 break;
3235 case GIMPLE_LABEL:
3236 expand_label (gimple_label_label (as_a <glabel *> (stmt)));
3237 break;
3238 case GIMPLE_NOP:
3239 case GIMPLE_PREDICT:
3240 break;
3241 case GIMPLE_SWITCH:
3242 expand_case (as_a <gswitch *> (stmt));
3243 break;
3244 case GIMPLE_ASM:
3245 expand_asm_stmt (as_a <gasm *> (stmt));
3246 break;
3247 case GIMPLE_CALL:
3248 expand_call_stmt (as_a <gcall *> (stmt));
3249 break;
3251 case GIMPLE_RETURN:
3253 tree bnd = gimple_return_retbnd (as_a <greturn *> (stmt));
3254 op0 = gimple_return_retval (as_a <greturn *> (stmt));
3256 if (op0 && op0 != error_mark_node)
3258 tree result = DECL_RESULT (current_function_decl);
3260 /* If we are not returning the current function's RESULT_DECL,
3261 build an assignment to it. */
3262 if (op0 != result)
3264 /* I believe that a function's RESULT_DECL is unique. */
3265 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3267 /* ??? We'd like to use simply expand_assignment here,
3268 but this fails if the value is of BLKmode but the return
3269 decl is a register. expand_return has special handling
3270 for this combination, which eventually should move
3271 to common code. See comments there. Until then, let's
3272 build a modify expression :-/ */
3273 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3274 result, op0);
3276 /* Mark we have return statement with missing bounds. */
3277 if (!bnd && chkp_function_instrumented_p (cfun->decl))
3278 bnd = error_mark_node;
3281 if (!op0)
3282 expand_null_return ();
3283 else
3284 expand_return (op0, bnd);
3286 break;
3288 case GIMPLE_ASSIGN:
3290 gassign *assign_stmt = as_a <gassign *> (stmt);
3291 tree lhs = gimple_assign_lhs (assign_stmt);
3293 /* Tree expand used to fiddle with |= and &= of two bitfield
3294 COMPONENT_REFs here. This can't happen with gimple, the LHS
3295 of binary assigns must be a gimple reg. */
3297 if (TREE_CODE (lhs) != SSA_NAME
3298 || get_gimple_rhs_class (gimple_expr_code (stmt))
3299 == GIMPLE_SINGLE_RHS)
3301 tree rhs = gimple_assign_rhs1 (assign_stmt);
3302 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
3303 == GIMPLE_SINGLE_RHS);
3304 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
3305 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3306 if (TREE_CLOBBER_P (rhs))
3307 /* This is a clobber to mark the going out of scope for
3308 this LHS. */
3310 else
3311 expand_assignment (lhs, rhs,
3312 gimple_assign_nontemporal_move_p (
3313 assign_stmt));
3315 else
3317 rtx target, temp;
3318 bool nontemporal = gimple_assign_nontemporal_move_p (assign_stmt);
3319 struct separate_ops ops;
3320 bool promoted = false;
3322 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3323 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
3324 promoted = true;
3326 ops.code = gimple_assign_rhs_code (assign_stmt);
3327 ops.type = TREE_TYPE (lhs);
3328 switch (get_gimple_rhs_class (ops.code))
3330 case GIMPLE_TERNARY_RHS:
3331 ops.op2 = gimple_assign_rhs3 (assign_stmt);
3332 /* Fallthru */
3333 case GIMPLE_BINARY_RHS:
3334 ops.op1 = gimple_assign_rhs2 (assign_stmt);
3335 /* Fallthru */
3336 case GIMPLE_UNARY_RHS:
3337 ops.op0 = gimple_assign_rhs1 (assign_stmt);
3338 break;
3339 default:
3340 gcc_unreachable ();
3342 ops.location = gimple_location (stmt);
3344 /* If we want to use a nontemporal store, force the value to
3345 register first. If we store into a promoted register,
3346 don't directly expand to target. */
3347 temp = nontemporal || promoted ? NULL_RTX : target;
3348 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
3349 EXPAND_NORMAL);
3351 if (temp == target)
3353 else if (promoted)
3355 int unsignedp = SUBREG_PROMOTED_SIGN (target);
3356 /* If TEMP is a VOIDmode constant, use convert_modes to make
3357 sure that we properly convert it. */
3358 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
3360 temp = convert_modes (GET_MODE (target),
3361 TYPE_MODE (ops.type),
3362 temp, unsignedp);
3363 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
3364 GET_MODE (target), temp, unsignedp);
3367 convert_move (SUBREG_REG (target), temp, unsignedp);
3369 else if (nontemporal && emit_storent_insn (target, temp))
3371 else
3373 temp = force_operand (temp, target);
3374 if (temp != target)
3375 emit_move_insn (target, temp);
3379 break;
3381 default:
3382 gcc_unreachable ();
3386 /* Expand one gimple statement STMT and return the last RTL instruction
3387 before any of the newly generated ones.
3389 In addition to generating the necessary RTL instructions this also
3390 sets REG_EH_REGION notes if necessary and sets the current source
3391 location for diagnostics. */
3393 static rtx_insn *
3394 expand_gimple_stmt (gimple stmt)
3396 location_t saved_location = input_location;
3397 rtx_insn *last = get_last_insn ();
3398 int lp_nr;
3400 gcc_assert (cfun);
3402 /* We need to save and restore the current source location so that errors
3403 discovered during expansion are emitted with the right location. But
3404 it would be better if the diagnostic routines used the source location
3405 embedded in the tree nodes rather than globals. */
3406 if (gimple_has_location (stmt))
3407 input_location = gimple_location (stmt);
3409 expand_gimple_stmt_1 (stmt);
3411 /* Free any temporaries used to evaluate this statement. */
3412 free_temp_slots ();
3414 input_location = saved_location;
3416 /* Mark all insns that may trap. */
3417 lp_nr = lookup_stmt_eh_lp (stmt);
3418 if (lp_nr)
3420 rtx_insn *insn;
3421 for (insn = next_real_insn (last); insn;
3422 insn = next_real_insn (insn))
3424 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
3425 /* If we want exceptions for non-call insns, any
3426 may_trap_p instruction may throw. */
3427 && GET_CODE (PATTERN (insn)) != CLOBBER
3428 && GET_CODE (PATTERN (insn)) != USE
3429 && insn_could_throw_p (insn))
3430 make_reg_eh_region_note (insn, 0, lp_nr);
3434 return last;
3437 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
3438 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
3439 generated a tail call (something that might be denied by the ABI
3440 rules governing the call; see calls.c).
3442 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
3443 can still reach the rest of BB. The case here is __builtin_sqrt,
3444 where the NaN result goes through the external function (with a
3445 tailcall) and the normal result happens via a sqrt instruction. */
3447 static basic_block
3448 expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
3450 rtx_insn *last2, *last;
3451 edge e;
3452 edge_iterator ei;
3453 int probability;
3454 gcov_type count;
3456 last2 = last = expand_gimple_stmt (stmt);
3458 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
3459 if (CALL_P (last) && SIBLING_CALL_P (last))
3460 goto found;
3462 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3464 *can_fallthru = true;
3465 return NULL;
3467 found:
3468 /* ??? Wouldn't it be better to just reset any pending stack adjust?
3469 Any instructions emitted here are about to be deleted. */
3470 do_pending_stack_adjust ();
3472 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
3473 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
3474 EH or abnormal edges, we shouldn't have created a tail call in
3475 the first place. So it seems to me we should just be removing
3476 all edges here, or redirecting the existing fallthru edge to
3477 the exit block. */
3479 probability = 0;
3480 count = 0;
3482 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3484 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
3486 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3488 e->dest->count -= e->count;
3489 e->dest->frequency -= EDGE_FREQUENCY (e);
3490 if (e->dest->count < 0)
3491 e->dest->count = 0;
3492 if (e->dest->frequency < 0)
3493 e->dest->frequency = 0;
3495 count += e->count;
3496 probability += e->probability;
3497 remove_edge (e);
3499 else
3500 ei_next (&ei);
3503 /* This is somewhat ugly: the call_expr expander often emits instructions
3504 after the sibcall (to perform the function return). These confuse the
3505 find_many_sub_basic_blocks code, so we need to get rid of these. */
3506 last = NEXT_INSN (last);
3507 gcc_assert (BARRIER_P (last));
3509 *can_fallthru = false;
3510 while (NEXT_INSN (last))
3512 /* For instance an sqrt builtin expander expands if with
3513 sibcall in the then and label for `else`. */
3514 if (LABEL_P (NEXT_INSN (last)))
3516 *can_fallthru = true;
3517 break;
3519 delete_insn (NEXT_INSN (last));
3522 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
3523 | EDGE_SIBCALL);
3524 e->probability += probability;
3525 e->count += count;
3526 BB_END (bb) = last;
3527 update_bb_for_insn (bb);
3529 if (NEXT_INSN (last))
3531 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
3533 last = BB_END (bb);
3534 if (BARRIER_P (last))
3535 BB_END (bb) = PREV_INSN (last);
3538 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3540 return bb;
3543 /* Return the difference between the floor and the truncated result of
3544 a signed division by OP1 with remainder MOD. */
3545 static rtx
3546 floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3548 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
3549 return gen_rtx_IF_THEN_ELSE
3550 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3551 gen_rtx_IF_THEN_ELSE
3552 (mode, gen_rtx_LT (BImode,
3553 gen_rtx_DIV (mode, op1, mod),
3554 const0_rtx),
3555 constm1_rtx, const0_rtx),
3556 const0_rtx);
3559 /* Return the difference between the ceil and the truncated result of
3560 a signed division by OP1 with remainder MOD. */
3561 static rtx
3562 ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3564 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
3565 return gen_rtx_IF_THEN_ELSE
3566 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3567 gen_rtx_IF_THEN_ELSE
3568 (mode, gen_rtx_GT (BImode,
3569 gen_rtx_DIV (mode, op1, mod),
3570 const0_rtx),
3571 const1_rtx, const0_rtx),
3572 const0_rtx);
3575 /* Return the difference between the ceil and the truncated result of
3576 an unsigned division by OP1 with remainder MOD. */
3577 static rtx
3578 ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
3580 /* (mod != 0 ? 1 : 0) */
3581 return gen_rtx_IF_THEN_ELSE
3582 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3583 const1_rtx, const0_rtx);
3586 /* Return the difference between the rounded and the truncated result
3587 of a signed division by OP1 with remainder MOD. Halfway cases are
3588 rounded away from zero, rather than to the nearest even number. */
3589 static rtx
3590 round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3592 /* (abs (mod) >= abs (op1) - abs (mod)
3593 ? (op1 / mod > 0 ? 1 : -1)
3594 : 0) */
3595 return gen_rtx_IF_THEN_ELSE
3596 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
3597 gen_rtx_MINUS (mode,
3598 gen_rtx_ABS (mode, op1),
3599 gen_rtx_ABS (mode, mod))),
3600 gen_rtx_IF_THEN_ELSE
3601 (mode, gen_rtx_GT (BImode,
3602 gen_rtx_DIV (mode, op1, mod),
3603 const0_rtx),
3604 const1_rtx, constm1_rtx),
3605 const0_rtx);
3608 /* Return the difference between the rounded and the truncated result
3609 of a unsigned division by OP1 with remainder MOD. Halfway cases
3610 are rounded away from zero, rather than to the nearest even
3611 number. */
3612 static rtx
3613 round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
3615 /* (mod >= op1 - mod ? 1 : 0) */
3616 return gen_rtx_IF_THEN_ELSE
3617 (mode, gen_rtx_GE (BImode, mod,
3618 gen_rtx_MINUS (mode, op1, mod)),
3619 const1_rtx, const0_rtx);
3622 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
3623 any rtl. */
3625 static rtx
3626 convert_debug_memory_address (machine_mode mode, rtx x,
3627 addr_space_t as)
3629 machine_mode xmode = GET_MODE (x);
3631 #ifndef POINTERS_EXTEND_UNSIGNED
3632 gcc_assert (mode == Pmode
3633 || mode == targetm.addr_space.address_mode (as));
3634 gcc_assert (xmode == mode || xmode == VOIDmode);
3635 #else
3636 rtx temp;
3638 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
3640 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
3641 return x;
3643 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
3644 x = lowpart_subreg (mode, x, xmode);
3645 else if (POINTERS_EXTEND_UNSIGNED > 0)
3646 x = gen_rtx_ZERO_EXTEND (mode, x);
3647 else if (!POINTERS_EXTEND_UNSIGNED)
3648 x = gen_rtx_SIGN_EXTEND (mode, x);
3649 else
3651 switch (GET_CODE (x))
3653 case SUBREG:
3654 if ((SUBREG_PROMOTED_VAR_P (x)
3655 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
3656 || (GET_CODE (SUBREG_REG (x)) == PLUS
3657 && REG_P (XEXP (SUBREG_REG (x), 0))
3658 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
3659 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
3660 && GET_MODE (SUBREG_REG (x)) == mode)
3661 return SUBREG_REG (x);
3662 break;
3663 case LABEL_REF:
3664 temp = gen_rtx_LABEL_REF (mode, LABEL_REF_LABEL (x));
3665 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
3666 return temp;
3667 case SYMBOL_REF:
3668 temp = shallow_copy_rtx (x);
3669 PUT_MODE (temp, mode);
3670 return temp;
3671 case CONST:
3672 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3673 if (temp)
3674 temp = gen_rtx_CONST (mode, temp);
3675 return temp;
3676 case PLUS:
3677 case MINUS:
3678 if (CONST_INT_P (XEXP (x, 1)))
3680 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3681 if (temp)
3682 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
3684 break;
3685 default:
3686 break;
3688 /* Don't know how to express ptr_extend as operation in debug info. */
3689 return NULL;
3691 #endif /* POINTERS_EXTEND_UNSIGNED */
3693 return x;
3696 /* Map from SSA_NAMEs to corresponding DEBUG_EXPR_DECLs created
3697 by avoid_deep_ter_for_debug. */
3699 static hash_map<tree, tree> *deep_ter_debug_map;
3701 /* Split too deep TER chains for debug stmts using debug temporaries. */
3703 static void
3704 avoid_deep_ter_for_debug (gimple stmt, int depth)
3706 use_operand_p use_p;
3707 ssa_op_iter iter;
3708 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
3710 tree use = USE_FROM_PTR (use_p);
3711 if (TREE_CODE (use) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (use))
3712 continue;
3713 gimple g = get_gimple_for_ssa_name (use);
3714 if (g == NULL)
3715 continue;
3716 if (depth > 6 && !stmt_ends_bb_p (g))
3718 if (deep_ter_debug_map == NULL)
3719 deep_ter_debug_map = new hash_map<tree, tree>;
3721 tree &vexpr = deep_ter_debug_map->get_or_insert (use);
3722 if (vexpr != NULL)
3723 continue;
3724 vexpr = make_node (DEBUG_EXPR_DECL);
3725 gimple def_temp = gimple_build_debug_bind (vexpr, use, g);
3726 DECL_ARTIFICIAL (vexpr) = 1;
3727 TREE_TYPE (vexpr) = TREE_TYPE (use);
3728 DECL_MODE (vexpr) = TYPE_MODE (TREE_TYPE (use));
3729 gimple_stmt_iterator gsi = gsi_for_stmt (g);
3730 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
3731 avoid_deep_ter_for_debug (def_temp, 0);
3733 else
3734 avoid_deep_ter_for_debug (g, depth + 1);
3738 /* Return an RTX equivalent to the value of the parameter DECL. */
3740 static rtx
3741 expand_debug_parm_decl (tree decl)
3743 rtx incoming = DECL_INCOMING_RTL (decl);
3745 if (incoming
3746 && GET_MODE (incoming) != BLKmode
3747 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
3748 || (MEM_P (incoming)
3749 && REG_P (XEXP (incoming, 0))
3750 && HARD_REGISTER_P (XEXP (incoming, 0)))))
3752 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
3754 #ifdef HAVE_window_save
3755 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
3756 If the target machine has an explicit window save instruction, the
3757 actual entry value is the corresponding OUTGOING_REGNO instead. */
3758 if (REG_P (incoming)
3759 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
3760 incoming
3761 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
3762 OUTGOING_REGNO (REGNO (incoming)), 0);
3763 else if (MEM_P (incoming))
3765 rtx reg = XEXP (incoming, 0);
3766 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
3768 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
3769 incoming = replace_equiv_address_nv (incoming, reg);
3771 else
3772 incoming = copy_rtx (incoming);
3774 #endif
3776 ENTRY_VALUE_EXP (rtl) = incoming;
3777 return rtl;
3780 if (incoming
3781 && GET_MODE (incoming) != BLKmode
3782 && !TREE_ADDRESSABLE (decl)
3783 && MEM_P (incoming)
3784 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
3785 || (GET_CODE (XEXP (incoming, 0)) == PLUS
3786 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
3787 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
3788 return copy_rtx (incoming);
3790 return NULL_RTX;
3793 /* Return an RTX equivalent to the value of the tree expression EXP. */
3795 static rtx
3796 expand_debug_expr (tree exp)
3798 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
3799 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3800 machine_mode inner_mode = VOIDmode;
3801 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
3802 addr_space_t as;
3804 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
3806 case tcc_expression:
3807 switch (TREE_CODE (exp))
3809 case COND_EXPR:
3810 case DOT_PROD_EXPR:
3811 case SAD_EXPR:
3812 case WIDEN_MULT_PLUS_EXPR:
3813 case WIDEN_MULT_MINUS_EXPR:
3814 case FMA_EXPR:
3815 goto ternary;
3817 case TRUTH_ANDIF_EXPR:
3818 case TRUTH_ORIF_EXPR:
3819 case TRUTH_AND_EXPR:
3820 case TRUTH_OR_EXPR:
3821 case TRUTH_XOR_EXPR:
3822 goto binary;
3824 case TRUTH_NOT_EXPR:
3825 goto unary;
3827 default:
3828 break;
3830 break;
3832 ternary:
3833 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
3834 if (!op2)
3835 return NULL_RTX;
3836 /* Fall through. */
3838 binary:
3839 case tcc_binary:
3840 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
3841 if (!op1)
3842 return NULL_RTX;
3843 switch (TREE_CODE (exp))
3845 case LSHIFT_EXPR:
3846 case RSHIFT_EXPR:
3847 case LROTATE_EXPR:
3848 case RROTATE_EXPR:
3849 case WIDEN_LSHIFT_EXPR:
3850 /* Ensure second operand isn't wider than the first one. */
3851 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 1)));
3852 if (SCALAR_INT_MODE_P (inner_mode))
3854 machine_mode opmode = mode;
3855 if (VECTOR_MODE_P (mode))
3856 opmode = GET_MODE_INNER (mode);
3857 if (SCALAR_INT_MODE_P (opmode)
3858 && (GET_MODE_PRECISION (opmode)
3859 < GET_MODE_PRECISION (inner_mode)))
3860 op1 = lowpart_subreg (opmode, op1, inner_mode);
3862 break;
3863 default:
3864 break;
3866 /* Fall through. */
3868 unary:
3869 case tcc_unary:
3870 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3871 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3872 if (!op0)
3873 return NULL_RTX;
3874 break;
3876 case tcc_comparison:
3877 unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
3878 goto binary;
3880 case tcc_type:
3881 case tcc_statement:
3882 gcc_unreachable ();
3884 case tcc_constant:
3885 case tcc_exceptional:
3886 case tcc_declaration:
3887 case tcc_reference:
3888 case tcc_vl_exp:
3889 break;
3892 switch (TREE_CODE (exp))
3894 case STRING_CST:
3895 if (!lookup_constant_def (exp))
3897 if (strlen (TREE_STRING_POINTER (exp)) + 1
3898 != (size_t) TREE_STRING_LENGTH (exp))
3899 return NULL_RTX;
3900 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
3901 op0 = gen_rtx_MEM (BLKmode, op0);
3902 set_mem_attributes (op0, exp, 0);
3903 return op0;
3905 /* Fall through... */
3907 case INTEGER_CST:
3908 case REAL_CST:
3909 case FIXED_CST:
3910 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
3911 return op0;
3913 case COMPLEX_CST:
3914 gcc_assert (COMPLEX_MODE_P (mode));
3915 op0 = expand_debug_expr (TREE_REALPART (exp));
3916 op1 = expand_debug_expr (TREE_IMAGPART (exp));
3917 return gen_rtx_CONCAT (mode, op0, op1);
3919 case DEBUG_EXPR_DECL:
3920 op0 = DECL_RTL_IF_SET (exp);
3922 if (op0)
3923 return op0;
3925 op0 = gen_rtx_DEBUG_EXPR (mode);
3926 DEBUG_EXPR_TREE_DECL (op0) = exp;
3927 SET_DECL_RTL (exp, op0);
3929 return op0;
3931 case VAR_DECL:
3932 case PARM_DECL:
3933 case FUNCTION_DECL:
3934 case LABEL_DECL:
3935 case CONST_DECL:
3936 case RESULT_DECL:
3937 op0 = DECL_RTL_IF_SET (exp);
3939 /* This decl was probably optimized away. */
3940 if (!op0)
3942 if (TREE_CODE (exp) != VAR_DECL
3943 || DECL_EXTERNAL (exp)
3944 || !TREE_STATIC (exp)
3945 || !DECL_NAME (exp)
3946 || DECL_HARD_REGISTER (exp)
3947 || DECL_IN_CONSTANT_POOL (exp)
3948 || mode == VOIDmode)
3949 return NULL;
3951 op0 = make_decl_rtl_for_debug (exp);
3952 if (!MEM_P (op0)
3953 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
3954 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
3955 return NULL;
3957 else
3958 op0 = copy_rtx (op0);
3960 if (GET_MODE (op0) == BLKmode
3961 /* If op0 is not BLKmode, but mode is, adjust_mode
3962 below would ICE. While it is likely a FE bug,
3963 try to be robust here. See PR43166. */
3964 || mode == BLKmode
3965 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
3967 gcc_assert (MEM_P (op0));
3968 op0 = adjust_address_nv (op0, mode, 0);
3969 return op0;
3972 /* Fall through. */
3974 adjust_mode:
3975 case PAREN_EXPR:
3976 CASE_CONVERT:
3978 inner_mode = GET_MODE (op0);
3980 if (mode == inner_mode)
3981 return op0;
3983 if (inner_mode == VOIDmode)
3985 if (TREE_CODE (exp) == SSA_NAME)
3986 inner_mode = TYPE_MODE (TREE_TYPE (exp));
3987 else
3988 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3989 if (mode == inner_mode)
3990 return op0;
3993 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
3995 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
3996 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
3997 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
3998 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
3999 else
4000 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4002 else if (FLOAT_MODE_P (mode))
4004 gcc_assert (TREE_CODE (exp) != SSA_NAME);
4005 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4006 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
4007 else
4008 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
4010 else if (FLOAT_MODE_P (inner_mode))
4012 if (unsignedp)
4013 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4014 else
4015 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4017 else if (CONSTANT_P (op0)
4018 || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
4019 op0 = lowpart_subreg (mode, op0, inner_mode);
4020 else if (UNARY_CLASS_P (exp)
4021 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
4022 : unsignedp)
4023 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4024 else
4025 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4027 return op0;
4030 case MEM_REF:
4031 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4033 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
4034 TREE_OPERAND (exp, 0),
4035 TREE_OPERAND (exp, 1));
4036 if (newexp)
4037 return expand_debug_expr (newexp);
4039 /* FALLTHROUGH */
4040 case INDIRECT_REF:
4041 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4042 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4043 if (!op0)
4044 return NULL;
4046 if (TREE_CODE (exp) == MEM_REF)
4048 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4049 || (GET_CODE (op0) == PLUS
4050 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
4051 /* (mem (debug_implicit_ptr)) might confuse aliasing.
4052 Instead just use get_inner_reference. */
4053 goto component_ref;
4055 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4056 if (!op1 || !CONST_INT_P (op1))
4057 return NULL;
4059 op0 = plus_constant (inner_mode, op0, INTVAL (op1));
4062 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4064 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4065 op0, as);
4066 if (op0 == NULL_RTX)
4067 return NULL;
4069 op0 = gen_rtx_MEM (mode, op0);
4070 set_mem_attributes (op0, exp, 0);
4071 if (TREE_CODE (exp) == MEM_REF
4072 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4073 set_mem_expr (op0, NULL_TREE);
4074 set_mem_addr_space (op0, as);
4076 return op0;
4078 case TARGET_MEM_REF:
4079 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
4080 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
4081 return NULL;
4083 op0 = expand_debug_expr
4084 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
4085 if (!op0)
4086 return NULL;
4088 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4089 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4090 op0, as);
4091 if (op0 == NULL_RTX)
4092 return NULL;
4094 op0 = gen_rtx_MEM (mode, op0);
4096 set_mem_attributes (op0, exp, 0);
4097 set_mem_addr_space (op0, as);
4099 return op0;
4101 component_ref:
4102 case ARRAY_REF:
4103 case ARRAY_RANGE_REF:
4104 case COMPONENT_REF:
4105 case BIT_FIELD_REF:
4106 case REALPART_EXPR:
4107 case IMAGPART_EXPR:
4108 case VIEW_CONVERT_EXPR:
4110 machine_mode mode1;
4111 HOST_WIDE_INT bitsize, bitpos;
4112 tree offset;
4113 int volatilep = 0;
4114 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
4115 &mode1, &unsignedp, &volatilep, false);
4116 rtx orig_op0;
4118 if (bitsize == 0)
4119 return NULL;
4121 orig_op0 = op0 = expand_debug_expr (tem);
4123 if (!op0)
4124 return NULL;
4126 if (offset)
4128 machine_mode addrmode, offmode;
4130 if (!MEM_P (op0))
4131 return NULL;
4133 op0 = XEXP (op0, 0);
4134 addrmode = GET_MODE (op0);
4135 if (addrmode == VOIDmode)
4136 addrmode = Pmode;
4138 op1 = expand_debug_expr (offset);
4139 if (!op1)
4140 return NULL;
4142 offmode = GET_MODE (op1);
4143 if (offmode == VOIDmode)
4144 offmode = TYPE_MODE (TREE_TYPE (offset));
4146 if (addrmode != offmode)
4147 op1 = lowpart_subreg (addrmode, op1, offmode);
4149 /* Don't use offset_address here, we don't need a
4150 recognizable address, and we don't want to generate
4151 code. */
4152 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4153 op0, op1));
4156 if (MEM_P (op0))
4158 if (mode1 == VOIDmode)
4159 /* Bitfield. */
4160 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
4161 if (bitpos >= BITS_PER_UNIT)
4163 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
4164 bitpos %= BITS_PER_UNIT;
4166 else if (bitpos < 0)
4168 HOST_WIDE_INT units
4169 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4170 op0 = adjust_address_nv (op0, mode1, units);
4171 bitpos += units * BITS_PER_UNIT;
4173 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
4174 op0 = adjust_address_nv (op0, mode, 0);
4175 else if (GET_MODE (op0) != mode1)
4176 op0 = adjust_address_nv (op0, mode1, 0);
4177 else
4178 op0 = copy_rtx (op0);
4179 if (op0 == orig_op0)
4180 op0 = shallow_copy_rtx (op0);
4181 set_mem_attributes (op0, exp, 0);
4184 if (bitpos == 0 && mode == GET_MODE (op0))
4185 return op0;
4187 if (bitpos < 0)
4188 return NULL;
4190 if (GET_MODE (op0) == BLKmode)
4191 return NULL;
4193 if ((bitpos % BITS_PER_UNIT) == 0
4194 && bitsize == GET_MODE_BITSIZE (mode1))
4196 machine_mode opmode = GET_MODE (op0);
4198 if (opmode == VOIDmode)
4199 opmode = TYPE_MODE (TREE_TYPE (tem));
4201 /* This condition may hold if we're expanding the address
4202 right past the end of an array that turned out not to
4203 be addressable (i.e., the address was only computed in
4204 debug stmts). The gen_subreg below would rightfully
4205 crash, and the address doesn't really exist, so just
4206 drop it. */
4207 if (bitpos >= GET_MODE_BITSIZE (opmode))
4208 return NULL;
4210 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
4211 return simplify_gen_subreg (mode, op0, opmode,
4212 bitpos / BITS_PER_UNIT);
4215 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4216 && TYPE_UNSIGNED (TREE_TYPE (exp))
4217 ? SIGN_EXTRACT
4218 : ZERO_EXTRACT, mode,
4219 GET_MODE (op0) != VOIDmode
4220 ? GET_MODE (op0)
4221 : TYPE_MODE (TREE_TYPE (tem)),
4222 op0, GEN_INT (bitsize), GEN_INT (bitpos));
4225 case ABS_EXPR:
4226 return simplify_gen_unary (ABS, mode, op0, mode);
4228 case NEGATE_EXPR:
4229 return simplify_gen_unary (NEG, mode, op0, mode);
4231 case BIT_NOT_EXPR:
4232 return simplify_gen_unary (NOT, mode, op0, mode);
4234 case FLOAT_EXPR:
4235 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4236 0)))
4237 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4238 inner_mode);
4240 case FIX_TRUNC_EXPR:
4241 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4242 inner_mode);
4244 case POINTER_PLUS_EXPR:
4245 /* For the rare target where pointers are not the same size as
4246 size_t, we need to check for mis-matched modes and correct
4247 the addend. */
4248 if (op0 && op1
4249 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
4250 && GET_MODE (op0) != GET_MODE (op1))
4252 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1))
4253 /* If OP0 is a partial mode, then we must truncate, even if it has
4254 the same bitsize as OP1 as GCC's representation of partial modes
4255 is opaque. */
4256 || (GET_MODE_CLASS (GET_MODE (op0)) == MODE_PARTIAL_INT
4257 && GET_MODE_BITSIZE (GET_MODE (op0)) == GET_MODE_BITSIZE (GET_MODE (op1))))
4258 op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
4259 GET_MODE (op1));
4260 else
4261 /* We always sign-extend, regardless of the signedness of
4262 the operand, because the operand is always unsigned
4263 here even if the original C expression is signed. */
4264 op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
4265 GET_MODE (op1));
4267 /* Fall through. */
4268 case PLUS_EXPR:
4269 return simplify_gen_binary (PLUS, mode, op0, op1);
4271 case MINUS_EXPR:
4272 return simplify_gen_binary (MINUS, mode, op0, op1);
4274 case MULT_EXPR:
4275 return simplify_gen_binary (MULT, mode, op0, op1);
4277 case RDIV_EXPR:
4278 case TRUNC_DIV_EXPR:
4279 case EXACT_DIV_EXPR:
4280 if (unsignedp)
4281 return simplify_gen_binary (UDIV, mode, op0, op1);
4282 else
4283 return simplify_gen_binary (DIV, mode, op0, op1);
4285 case TRUNC_MOD_EXPR:
4286 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4288 case FLOOR_DIV_EXPR:
4289 if (unsignedp)
4290 return simplify_gen_binary (UDIV, mode, op0, op1);
4291 else
4293 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4294 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4295 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4296 return simplify_gen_binary (PLUS, mode, div, adj);
4299 case FLOOR_MOD_EXPR:
4300 if (unsignedp)
4301 return simplify_gen_binary (UMOD, mode, op0, op1);
4302 else
4304 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4305 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4306 adj = simplify_gen_unary (NEG, mode,
4307 simplify_gen_binary (MULT, mode, adj, op1),
4308 mode);
4309 return simplify_gen_binary (PLUS, mode, mod, adj);
4312 case CEIL_DIV_EXPR:
4313 if (unsignedp)
4315 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4316 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4317 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4318 return simplify_gen_binary (PLUS, mode, div, adj);
4320 else
4322 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4323 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4324 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4325 return simplify_gen_binary (PLUS, mode, div, adj);
4328 case CEIL_MOD_EXPR:
4329 if (unsignedp)
4331 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4332 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4333 adj = simplify_gen_unary (NEG, mode,
4334 simplify_gen_binary (MULT, mode, adj, op1),
4335 mode);
4336 return simplify_gen_binary (PLUS, mode, mod, adj);
4338 else
4340 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4341 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4342 adj = simplify_gen_unary (NEG, mode,
4343 simplify_gen_binary (MULT, mode, adj, op1),
4344 mode);
4345 return simplify_gen_binary (PLUS, mode, mod, adj);
4348 case ROUND_DIV_EXPR:
4349 if (unsignedp)
4351 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4352 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4353 rtx adj = round_udiv_adjust (mode, mod, op1);
4354 return simplify_gen_binary (PLUS, mode, div, adj);
4356 else
4358 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4359 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4360 rtx adj = round_sdiv_adjust (mode, mod, op1);
4361 return simplify_gen_binary (PLUS, mode, div, adj);
4364 case ROUND_MOD_EXPR:
4365 if (unsignedp)
4367 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4368 rtx adj = round_udiv_adjust (mode, mod, op1);
4369 adj = simplify_gen_unary (NEG, mode,
4370 simplify_gen_binary (MULT, mode, adj, op1),
4371 mode);
4372 return simplify_gen_binary (PLUS, mode, mod, adj);
4374 else
4376 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4377 rtx adj = round_sdiv_adjust (mode, mod, op1);
4378 adj = simplify_gen_unary (NEG, mode,
4379 simplify_gen_binary (MULT, mode, adj, op1),
4380 mode);
4381 return simplify_gen_binary (PLUS, mode, mod, adj);
4384 case LSHIFT_EXPR:
4385 return simplify_gen_binary (ASHIFT, mode, op0, op1);
4387 case RSHIFT_EXPR:
4388 if (unsignedp)
4389 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
4390 else
4391 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
4393 case LROTATE_EXPR:
4394 return simplify_gen_binary (ROTATE, mode, op0, op1);
4396 case RROTATE_EXPR:
4397 return simplify_gen_binary (ROTATERT, mode, op0, op1);
4399 case MIN_EXPR:
4400 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
4402 case MAX_EXPR:
4403 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
4405 case BIT_AND_EXPR:
4406 case TRUTH_AND_EXPR:
4407 return simplify_gen_binary (AND, mode, op0, op1);
4409 case BIT_IOR_EXPR:
4410 case TRUTH_OR_EXPR:
4411 return simplify_gen_binary (IOR, mode, op0, op1);
4413 case BIT_XOR_EXPR:
4414 case TRUTH_XOR_EXPR:
4415 return simplify_gen_binary (XOR, mode, op0, op1);
4417 case TRUTH_ANDIF_EXPR:
4418 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
4420 case TRUTH_ORIF_EXPR:
4421 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
4423 case TRUTH_NOT_EXPR:
4424 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
4426 case LT_EXPR:
4427 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
4428 op0, op1);
4430 case LE_EXPR:
4431 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
4432 op0, op1);
4434 case GT_EXPR:
4435 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
4436 op0, op1);
4438 case GE_EXPR:
4439 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
4440 op0, op1);
4442 case EQ_EXPR:
4443 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
4445 case NE_EXPR:
4446 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
4448 case UNORDERED_EXPR:
4449 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
4451 case ORDERED_EXPR:
4452 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
4454 case UNLT_EXPR:
4455 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
4457 case UNLE_EXPR:
4458 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
4460 case UNGT_EXPR:
4461 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
4463 case UNGE_EXPR:
4464 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
4466 case UNEQ_EXPR:
4467 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
4469 case LTGT_EXPR:
4470 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
4472 case COND_EXPR:
4473 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
4475 case COMPLEX_EXPR:
4476 gcc_assert (COMPLEX_MODE_P (mode));
4477 if (GET_MODE (op0) == VOIDmode)
4478 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
4479 if (GET_MODE (op1) == VOIDmode)
4480 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
4481 return gen_rtx_CONCAT (mode, op0, op1);
4483 case CONJ_EXPR:
4484 if (GET_CODE (op0) == CONCAT)
4485 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
4486 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
4487 XEXP (op0, 1),
4488 GET_MODE_INNER (mode)));
4489 else
4491 machine_mode imode = GET_MODE_INNER (mode);
4492 rtx re, im;
4494 if (MEM_P (op0))
4496 re = adjust_address_nv (op0, imode, 0);
4497 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
4499 else
4501 machine_mode ifmode = int_mode_for_mode (mode);
4502 machine_mode ihmode = int_mode_for_mode (imode);
4503 rtx halfsize;
4504 if (ifmode == BLKmode || ihmode == BLKmode)
4505 return NULL;
4506 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
4507 re = op0;
4508 if (mode != ifmode)
4509 re = gen_rtx_SUBREG (ifmode, re, 0);
4510 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
4511 if (imode != ihmode)
4512 re = gen_rtx_SUBREG (imode, re, 0);
4513 im = copy_rtx (op0);
4514 if (mode != ifmode)
4515 im = gen_rtx_SUBREG (ifmode, im, 0);
4516 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
4517 if (imode != ihmode)
4518 im = gen_rtx_SUBREG (imode, im, 0);
4520 im = gen_rtx_NEG (imode, im);
4521 return gen_rtx_CONCAT (mode, re, im);
4524 case ADDR_EXPR:
4525 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4526 if (!op0 || !MEM_P (op0))
4528 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
4529 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
4530 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
4531 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
4532 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
4533 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
4535 if (handled_component_p (TREE_OPERAND (exp, 0)))
4537 HOST_WIDE_INT bitoffset, bitsize, maxsize;
4538 tree decl
4539 = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
4540 &bitoffset, &bitsize, &maxsize);
4541 if ((TREE_CODE (decl) == VAR_DECL
4542 || TREE_CODE (decl) == PARM_DECL
4543 || TREE_CODE (decl) == RESULT_DECL)
4544 && (!TREE_ADDRESSABLE (decl)
4545 || target_for_debug_bind (decl))
4546 && (bitoffset % BITS_PER_UNIT) == 0
4547 && bitsize > 0
4548 && bitsize == maxsize)
4550 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
4551 return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
4555 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
4556 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4557 == ADDR_EXPR)
4559 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4560 0));
4561 if (op0 != NULL
4562 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4563 || (GET_CODE (op0) == PLUS
4564 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
4565 && CONST_INT_P (XEXP (op0, 1)))))
4567 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4568 1));
4569 if (!op1 || !CONST_INT_P (op1))
4570 return NULL;
4572 return plus_constant (mode, op0, INTVAL (op1));
4576 return NULL;
4579 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
4580 op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
4582 return op0;
4584 case VECTOR_CST:
4586 unsigned i;
4588 op0 = gen_rtx_CONCATN
4589 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4591 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
4593 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
4594 if (!op1)
4595 return NULL;
4596 XVECEXP (op0, 0, i) = op1;
4599 return op0;
4602 case CONSTRUCTOR:
4603 if (TREE_CLOBBER_P (exp))
4604 return NULL;
4605 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
4607 unsigned i;
4608 tree val;
4610 op0 = gen_rtx_CONCATN
4611 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4613 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
4615 op1 = expand_debug_expr (val);
4616 if (!op1)
4617 return NULL;
4618 XVECEXP (op0, 0, i) = op1;
4621 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
4623 op1 = expand_debug_expr
4624 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
4626 if (!op1)
4627 return NULL;
4629 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
4630 XVECEXP (op0, 0, i) = op1;
4633 return op0;
4635 else
4636 goto flag_unsupported;
4638 case CALL_EXPR:
4639 /* ??? Maybe handle some builtins? */
4640 return NULL;
4642 case SSA_NAME:
4644 gimple g = get_gimple_for_ssa_name (exp);
4645 if (g)
4647 tree t = NULL_TREE;
4648 if (deep_ter_debug_map)
4650 tree *slot = deep_ter_debug_map->get (exp);
4651 if (slot)
4652 t = *slot;
4654 if (t == NULL_TREE)
4655 t = gimple_assign_rhs_to_tree (g);
4656 op0 = expand_debug_expr (t);
4657 if (!op0)
4658 return NULL;
4660 else
4662 int part = var_to_partition (SA.map, exp);
4664 if (part == NO_PARTITION)
4666 /* If this is a reference to an incoming value of parameter
4667 that is never used in the code or where the incoming
4668 value is never used in the code, use PARM_DECL's
4669 DECL_RTL if set. */
4670 if (SSA_NAME_IS_DEFAULT_DEF (exp)
4671 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL)
4673 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
4674 if (op0)
4675 goto adjust_mode;
4676 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
4677 if (op0)
4678 goto adjust_mode;
4680 return NULL;
4683 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
4685 op0 = copy_rtx (SA.partition_to_pseudo[part]);
4687 goto adjust_mode;
4690 case ERROR_MARK:
4691 return NULL;
4693 /* Vector stuff. For most of the codes we don't have rtl codes. */
4694 case REALIGN_LOAD_EXPR:
4695 case REDUC_MAX_EXPR:
4696 case REDUC_MIN_EXPR:
4697 case REDUC_PLUS_EXPR:
4698 case VEC_COND_EXPR:
4699 case VEC_PACK_FIX_TRUNC_EXPR:
4700 case VEC_PACK_SAT_EXPR:
4701 case VEC_PACK_TRUNC_EXPR:
4702 case VEC_UNPACK_FLOAT_HI_EXPR:
4703 case VEC_UNPACK_FLOAT_LO_EXPR:
4704 case VEC_UNPACK_HI_EXPR:
4705 case VEC_UNPACK_LO_EXPR:
4706 case VEC_WIDEN_MULT_HI_EXPR:
4707 case VEC_WIDEN_MULT_LO_EXPR:
4708 case VEC_WIDEN_MULT_EVEN_EXPR:
4709 case VEC_WIDEN_MULT_ODD_EXPR:
4710 case VEC_WIDEN_LSHIFT_HI_EXPR:
4711 case VEC_WIDEN_LSHIFT_LO_EXPR:
4712 case VEC_PERM_EXPR:
4713 return NULL;
4715 /* Misc codes. */
4716 case ADDR_SPACE_CONVERT_EXPR:
4717 case FIXED_CONVERT_EXPR:
4718 case OBJ_TYPE_REF:
4719 case WITH_SIZE_EXPR:
4720 return NULL;
4722 case DOT_PROD_EXPR:
4723 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4724 && SCALAR_INT_MODE_P (mode))
4727 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4728 0)))
4729 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4730 inner_mode);
4732 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4733 1)))
4734 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
4735 inner_mode);
4736 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4737 return simplify_gen_binary (PLUS, mode, op0, op2);
4739 return NULL;
4741 case WIDEN_MULT_EXPR:
4742 case WIDEN_MULT_PLUS_EXPR:
4743 case WIDEN_MULT_MINUS_EXPR:
4744 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4745 && SCALAR_INT_MODE_P (mode))
4747 inner_mode = GET_MODE (op0);
4748 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4749 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4750 else
4751 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4752 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
4753 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
4754 else
4755 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
4756 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4757 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
4758 return op0;
4759 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
4760 return simplify_gen_binary (PLUS, mode, op0, op2);
4761 else
4762 return simplify_gen_binary (MINUS, mode, op2, op0);
4764 return NULL;
4766 case MULT_HIGHPART_EXPR:
4767 /* ??? Similar to the above. */
4768 return NULL;
4770 case WIDEN_SUM_EXPR:
4771 case WIDEN_LSHIFT_EXPR:
4772 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4773 && SCALAR_INT_MODE_P (mode))
4776 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4777 0)))
4778 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4779 inner_mode);
4780 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
4781 ? ASHIFT : PLUS, mode, op0, op1);
4783 return NULL;
4785 case FMA_EXPR:
4786 return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
4788 default:
4789 flag_unsupported:
4790 #ifdef ENABLE_CHECKING
4791 debug_tree (exp);
4792 gcc_unreachable ();
4793 #else
4794 return NULL;
4795 #endif
4799 /* Return an RTX equivalent to the source bind value of the tree expression
4800 EXP. */
4802 static rtx
4803 expand_debug_source_expr (tree exp)
4805 rtx op0 = NULL_RTX;
4806 machine_mode mode = VOIDmode, inner_mode;
4808 switch (TREE_CODE (exp))
4810 case PARM_DECL:
4812 mode = DECL_MODE (exp);
4813 op0 = expand_debug_parm_decl (exp);
4814 if (op0)
4815 break;
4816 /* See if this isn't an argument that has been completely
4817 optimized out. */
4818 if (!DECL_RTL_SET_P (exp)
4819 && !DECL_INCOMING_RTL (exp)
4820 && DECL_ABSTRACT_ORIGIN (current_function_decl))
4822 tree aexp = DECL_ORIGIN (exp);
4823 if (DECL_CONTEXT (aexp)
4824 == DECL_ABSTRACT_ORIGIN (current_function_decl))
4826 vec<tree, va_gc> **debug_args;
4827 unsigned int ix;
4828 tree ddecl;
4829 debug_args = decl_debug_args_lookup (current_function_decl);
4830 if (debug_args != NULL)
4832 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
4833 ix += 2)
4834 if (ddecl == aexp)
4835 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
4839 break;
4841 default:
4842 break;
4845 if (op0 == NULL_RTX)
4846 return NULL_RTX;
4848 inner_mode = GET_MODE (op0);
4849 if (mode == inner_mode)
4850 return op0;
4852 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4854 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4855 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4856 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4857 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4858 else
4859 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4861 else if (FLOAT_MODE_P (mode))
4862 gcc_unreachable ();
4863 else if (FLOAT_MODE_P (inner_mode))
4865 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4866 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4867 else
4868 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4870 else if (CONSTANT_P (op0)
4871 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
4872 op0 = lowpart_subreg (mode, op0, inner_mode);
4873 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4874 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4875 else
4876 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4878 return op0;
4881 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
4882 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
4883 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
4885 static void
4886 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
4888 rtx exp = *exp_p;
4890 if (exp == NULL_RTX)
4891 return;
4893 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
4894 return;
4896 if (depth == 4)
4898 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
4899 rtx dval = make_debug_expr_from_rtl (exp);
4901 /* Emit a debug bind insn before INSN. */
4902 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
4903 DEBUG_EXPR_TREE_DECL (dval), exp,
4904 VAR_INIT_STATUS_INITIALIZED);
4906 emit_debug_insn_before (bind, insn);
4907 *exp_p = dval;
4908 return;
4911 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
4912 int i, j;
4913 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4914 switch (*format_ptr++)
4916 case 'e':
4917 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
4918 break;
4920 case 'E':
4921 case 'V':
4922 for (j = 0; j < XVECLEN (exp, i); j++)
4923 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
4924 break;
4926 default:
4927 break;
4931 /* Expand the _LOCs in debug insns. We run this after expanding all
4932 regular insns, so that any variables referenced in the function
4933 will have their DECL_RTLs set. */
4935 static void
4936 expand_debug_locations (void)
4938 rtx_insn *insn;
4939 rtx_insn *last = get_last_insn ();
4940 int save_strict_alias = flag_strict_aliasing;
4942 /* New alias sets while setting up memory attributes cause
4943 -fcompare-debug failures, even though it doesn't bring about any
4944 codegen changes. */
4945 flag_strict_aliasing = 0;
4947 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4948 if (DEBUG_INSN_P (insn))
4950 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
4951 rtx val;
4952 rtx_insn *prev_insn, *insn2;
4953 machine_mode mode;
4955 if (value == NULL_TREE)
4956 val = NULL_RTX;
4957 else
4959 if (INSN_VAR_LOCATION_STATUS (insn)
4960 == VAR_INIT_STATUS_UNINITIALIZED)
4961 val = expand_debug_source_expr (value);
4962 /* The avoid_deep_ter_for_debug function inserts
4963 debug bind stmts after SSA_NAME definition, with the
4964 SSA_NAME as the whole bind location. Disable temporarily
4965 expansion of that SSA_NAME into the DEBUG_EXPR_DECL
4966 being defined in this DEBUG_INSN. */
4967 else if (deep_ter_debug_map && TREE_CODE (value) == SSA_NAME)
4969 tree *slot = deep_ter_debug_map->get (value);
4970 if (slot)
4972 if (*slot == INSN_VAR_LOCATION_DECL (insn))
4973 *slot = NULL_TREE;
4974 else
4975 slot = NULL;
4977 val = expand_debug_expr (value);
4978 if (slot)
4979 *slot = INSN_VAR_LOCATION_DECL (insn);
4981 else
4982 val = expand_debug_expr (value);
4983 gcc_assert (last == get_last_insn ());
4986 if (!val)
4987 val = gen_rtx_UNKNOWN_VAR_LOC ();
4988 else
4990 mode = GET_MODE (INSN_VAR_LOCATION (insn));
4992 gcc_assert (mode == GET_MODE (val)
4993 || (GET_MODE (val) == VOIDmode
4994 && (CONST_SCALAR_INT_P (val)
4995 || GET_CODE (val) == CONST_FIXED
4996 || GET_CODE (val) == LABEL_REF)));
4999 INSN_VAR_LOCATION_LOC (insn) = val;
5000 prev_insn = PREV_INSN (insn);
5001 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
5002 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
5005 flag_strict_aliasing = save_strict_alias;
5008 /* Performs swapping operands of commutative operations to expand
5009 the expensive one first. */
5011 static void
5012 reorder_operands (basic_block bb)
5014 unsigned int *lattice; /* Hold cost of each statement. */
5015 unsigned int i = 0, n = 0;
5016 gimple_stmt_iterator gsi;
5017 gimple_seq stmts;
5018 gimple stmt;
5019 bool swap;
5020 tree op0, op1;
5021 ssa_op_iter iter;
5022 use_operand_p use_p;
5023 gimple def0, def1;
5025 /* Compute cost of each statement using estimate_num_insns. */
5026 stmts = bb_seq (bb);
5027 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5029 stmt = gsi_stmt (gsi);
5030 if (!is_gimple_debug (stmt))
5031 gimple_set_uid (stmt, n++);
5033 lattice = XNEWVEC (unsigned int, n);
5034 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5036 unsigned cost;
5037 stmt = gsi_stmt (gsi);
5038 if (is_gimple_debug (stmt))
5039 continue;
5040 cost = estimate_num_insns (stmt, &eni_size_weights);
5041 lattice[i] = cost;
5042 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
5044 tree use = USE_FROM_PTR (use_p);
5045 gimple def_stmt;
5046 if (TREE_CODE (use) != SSA_NAME)
5047 continue;
5048 def_stmt = get_gimple_for_ssa_name (use);
5049 if (!def_stmt)
5050 continue;
5051 lattice[i] += lattice[gimple_uid (def_stmt)];
5053 i++;
5054 if (!is_gimple_assign (stmt)
5055 || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
5056 continue;
5057 op0 = gimple_op (stmt, 1);
5058 op1 = gimple_op (stmt, 2);
5059 if (TREE_CODE (op0) != SSA_NAME
5060 || TREE_CODE (op1) != SSA_NAME)
5061 continue;
5062 /* Swap operands if the second one is more expensive. */
5063 def0 = get_gimple_for_ssa_name (op0);
5064 def1 = get_gimple_for_ssa_name (op1);
5065 if (!def1)
5066 continue;
5067 swap = false;
5068 if (!def0 || lattice[gimple_uid (def1)] > lattice[gimple_uid (def0)])
5069 swap = true;
5070 if (swap)
5072 if (dump_file && (dump_flags & TDF_DETAILS))
5074 fprintf (dump_file, "Swap operands in stmt:\n");
5075 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5076 fprintf (dump_file, "Cost left opnd=%d, right opnd=%d\n",
5077 def0 ? lattice[gimple_uid (def0)] : 0,
5078 lattice[gimple_uid (def1)]);
5080 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
5081 gimple_assign_rhs2_ptr (stmt));
5084 XDELETE (lattice);
5087 /* Expand basic block BB from GIMPLE trees to RTL. */
5089 static basic_block
5090 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
5092 gimple_stmt_iterator gsi;
5093 gimple_seq stmts;
5094 gimple stmt = NULL;
5095 rtx_note *note;
5096 rtx_insn *last;
5097 edge e;
5098 edge_iterator ei;
5100 if (dump_file)
5101 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
5102 bb->index);
5104 /* Note that since we are now transitioning from GIMPLE to RTL, we
5105 cannot use the gsi_*_bb() routines because they expect the basic
5106 block to be in GIMPLE, instead of RTL. Therefore, we need to
5107 access the BB sequence directly. */
5108 if (optimize)
5109 reorder_operands (bb);
5110 stmts = bb_seq (bb);
5111 bb->il.gimple.seq = NULL;
5112 bb->il.gimple.phi_nodes = NULL;
5113 rtl_profile_for_bb (bb);
5114 init_rtl_bb_info (bb);
5115 bb->flags |= BB_RTL;
5117 /* Remove the RETURN_EXPR if we may fall though to the exit
5118 instead. */
5119 gsi = gsi_last (stmts);
5120 if (!gsi_end_p (gsi)
5121 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
5123 greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
5125 gcc_assert (single_succ_p (bb));
5126 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
5128 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5129 && !gimple_return_retval (ret_stmt))
5131 gsi_remove (&gsi, false);
5132 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
5136 gsi = gsi_start (stmts);
5137 if (!gsi_end_p (gsi))
5139 stmt = gsi_stmt (gsi);
5140 if (gimple_code (stmt) != GIMPLE_LABEL)
5141 stmt = NULL;
5144 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
5146 if (stmt || elt)
5148 last = get_last_insn ();
5150 if (stmt)
5152 expand_gimple_stmt (stmt);
5153 gsi_next (&gsi);
5156 if (elt)
5157 emit_label (*elt);
5159 /* Java emits line number notes in the top of labels.
5160 ??? Make this go away once line number notes are obsoleted. */
5161 BB_HEAD (bb) = NEXT_INSN (last);
5162 if (NOTE_P (BB_HEAD (bb)))
5163 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
5164 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
5166 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5168 else
5169 BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
5171 NOTE_BASIC_BLOCK (note) = bb;
5173 for (; !gsi_end_p (gsi); gsi_next (&gsi))
5175 basic_block new_bb;
5177 stmt = gsi_stmt (gsi);
5179 /* If this statement is a non-debug one, and we generate debug
5180 insns, then this one might be the last real use of a TERed
5181 SSA_NAME, but where there are still some debug uses further
5182 down. Expanding the current SSA name in such further debug
5183 uses by their RHS might lead to wrong debug info, as coalescing
5184 might make the operands of such RHS be placed into the same
5185 pseudo as something else. Like so:
5186 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
5187 use(a_1);
5188 a_2 = ...
5189 #DEBUG ... => a_1
5190 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
5191 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
5192 the write to a_2 would actually have clobbered the place which
5193 formerly held a_0.
5195 So, instead of that, we recognize the situation, and generate
5196 debug temporaries at the last real use of TERed SSA names:
5197 a_1 = a_0 + 1;
5198 #DEBUG #D1 => a_1
5199 use(a_1);
5200 a_2 = ...
5201 #DEBUG ... => #D1
5203 if (MAY_HAVE_DEBUG_INSNS
5204 && SA.values
5205 && !is_gimple_debug (stmt))
5207 ssa_op_iter iter;
5208 tree op;
5209 gimple def;
5211 location_t sloc = curr_insn_location ();
5213 /* Look for SSA names that have their last use here (TERed
5214 names always have only one real use). */
5215 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5216 if ((def = get_gimple_for_ssa_name (op)))
5218 imm_use_iterator imm_iter;
5219 use_operand_p use_p;
5220 bool have_debug_uses = false;
5222 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5224 if (gimple_debug_bind_p (USE_STMT (use_p)))
5226 have_debug_uses = true;
5227 break;
5231 if (have_debug_uses)
5233 /* OP is a TERed SSA name, with DEF its defining
5234 statement, and where OP is used in further debug
5235 instructions. Generate a debug temporary, and
5236 replace all uses of OP in debug insns with that
5237 temporary. */
5238 gimple debugstmt;
5239 tree value = gimple_assign_rhs_to_tree (def);
5240 tree vexpr = make_node (DEBUG_EXPR_DECL);
5241 rtx val;
5242 machine_mode mode;
5244 set_curr_insn_location (gimple_location (def));
5246 DECL_ARTIFICIAL (vexpr) = 1;
5247 TREE_TYPE (vexpr) = TREE_TYPE (value);
5248 if (DECL_P (value))
5249 mode = DECL_MODE (value);
5250 else
5251 mode = TYPE_MODE (TREE_TYPE (value));
5252 DECL_MODE (vexpr) = mode;
5254 val = gen_rtx_VAR_LOCATION
5255 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5257 emit_debug_insn (val);
5259 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5261 if (!gimple_debug_bind_p (debugstmt))
5262 continue;
5264 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5265 SET_USE (use_p, vexpr);
5267 update_stmt (debugstmt);
5271 set_curr_insn_location (sloc);
5274 currently_expanding_gimple_stmt = stmt;
5276 /* Expand this statement, then evaluate the resulting RTL and
5277 fixup the CFG accordingly. */
5278 if (gimple_code (stmt) == GIMPLE_COND)
5280 new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
5281 if (new_bb)
5282 return new_bb;
5284 else if (gimple_debug_bind_p (stmt))
5286 location_t sloc = curr_insn_location ();
5287 gimple_stmt_iterator nsi = gsi;
5289 for (;;)
5291 tree var = gimple_debug_bind_get_var (stmt);
5292 tree value;
5293 rtx val;
5294 machine_mode mode;
5296 if (TREE_CODE (var) != DEBUG_EXPR_DECL
5297 && TREE_CODE (var) != LABEL_DECL
5298 && !target_for_debug_bind (var))
5299 goto delink_debug_stmt;
5301 if (gimple_debug_bind_has_value_p (stmt))
5302 value = gimple_debug_bind_get_value (stmt);
5303 else
5304 value = NULL_TREE;
5306 last = get_last_insn ();
5308 set_curr_insn_location (gimple_location (stmt));
5310 if (DECL_P (var))
5311 mode = DECL_MODE (var);
5312 else
5313 mode = TYPE_MODE (TREE_TYPE (var));
5315 val = gen_rtx_VAR_LOCATION
5316 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5318 emit_debug_insn (val);
5320 if (dump_file && (dump_flags & TDF_DETAILS))
5322 /* We can't dump the insn with a TREE where an RTX
5323 is expected. */
5324 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5325 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5326 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5329 delink_debug_stmt:
5330 /* In order not to generate too many debug temporaries,
5331 we delink all uses of debug statements we already expanded.
5332 Therefore debug statements between definition and real
5333 use of TERed SSA names will continue to use the SSA name,
5334 and not be replaced with debug temps. */
5335 delink_stmt_imm_use (stmt);
5337 gsi = nsi;
5338 gsi_next (&nsi);
5339 if (gsi_end_p (nsi))
5340 break;
5341 stmt = gsi_stmt (nsi);
5342 if (!gimple_debug_bind_p (stmt))
5343 break;
5346 set_curr_insn_location (sloc);
5348 else if (gimple_debug_source_bind_p (stmt))
5350 location_t sloc = curr_insn_location ();
5351 tree var = gimple_debug_source_bind_get_var (stmt);
5352 tree value = gimple_debug_source_bind_get_value (stmt);
5353 rtx val;
5354 machine_mode mode;
5356 last = get_last_insn ();
5358 set_curr_insn_location (gimple_location (stmt));
5360 mode = DECL_MODE (var);
5362 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
5363 VAR_INIT_STATUS_UNINITIALIZED);
5365 emit_debug_insn (val);
5367 if (dump_file && (dump_flags & TDF_DETAILS))
5369 /* We can't dump the insn with a TREE where an RTX
5370 is expected. */
5371 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5372 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5373 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5376 set_curr_insn_location (sloc);
5378 else
5380 gcall *call_stmt = dyn_cast <gcall *> (stmt);
5381 if (call_stmt
5382 && gimple_call_tail_p (call_stmt)
5383 && disable_tail_calls)
5384 gimple_call_set_tail (call_stmt, false);
5386 if (call_stmt && gimple_call_tail_p (call_stmt))
5388 bool can_fallthru;
5389 new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
5390 if (new_bb)
5392 if (can_fallthru)
5393 bb = new_bb;
5394 else
5395 return new_bb;
5398 else
5400 def_operand_p def_p;
5401 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
5403 if (def_p != NULL)
5405 /* Ignore this stmt if it is in the list of
5406 replaceable expressions. */
5407 if (SA.values
5408 && bitmap_bit_p (SA.values,
5409 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
5410 continue;
5412 last = expand_gimple_stmt (stmt);
5413 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5418 currently_expanding_gimple_stmt = NULL;
5420 /* Expand implicit goto and convert goto_locus. */
5421 FOR_EACH_EDGE (e, ei, bb->succs)
5423 if (e->goto_locus != UNKNOWN_LOCATION)
5424 set_curr_insn_location (e->goto_locus);
5425 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
5427 emit_jump (label_rtx_for_bb (e->dest));
5428 e->flags &= ~EDGE_FALLTHRU;
5432 /* Expanded RTL can create a jump in the last instruction of block.
5433 This later might be assumed to be a jump to successor and break edge insertion.
5434 We need to insert dummy move to prevent this. PR41440. */
5435 if (single_succ_p (bb)
5436 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
5437 && (last = get_last_insn ())
5438 && JUMP_P (last))
5440 rtx dummy = gen_reg_rtx (SImode);
5441 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
5444 do_pending_stack_adjust ();
5446 /* Find the block tail. The last insn in the block is the insn
5447 before a barrier and/or table jump insn. */
5448 last = get_last_insn ();
5449 if (BARRIER_P (last))
5450 last = PREV_INSN (last);
5451 if (JUMP_TABLE_DATA_P (last))
5452 last = PREV_INSN (PREV_INSN (last));
5453 BB_END (bb) = last;
5455 update_bb_for_insn (bb);
5457 return bb;
5461 /* Create a basic block for initialization code. */
5463 static basic_block
5464 construct_init_block (void)
5466 basic_block init_block, first_block;
5467 edge e = NULL;
5468 int flags;
5470 /* Multiple entry points not supported yet. */
5471 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
5472 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5473 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
5474 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5475 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5477 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
5479 /* When entry edge points to first basic block, we don't need jump,
5480 otherwise we have to jump into proper target. */
5481 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
5483 tree label = gimple_block_label (e->dest);
5485 emit_jump (jump_target_rtx (label));
5486 flags = 0;
5488 else
5489 flags = EDGE_FALLTHRU;
5491 init_block = create_basic_block (NEXT_INSN (get_insns ()),
5492 get_last_insn (),
5493 ENTRY_BLOCK_PTR_FOR_FN (cfun));
5494 init_block->frequency = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
5495 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5496 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5497 if (e)
5499 first_block = e->dest;
5500 redirect_edge_succ (e, init_block);
5501 e = make_edge (init_block, first_block, flags);
5503 else
5504 e = make_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5505 e->probability = REG_BR_PROB_BASE;
5506 e->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5508 update_bb_for_insn (init_block);
5509 return init_block;
5512 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
5513 found in the block tree. */
5515 static void
5516 set_block_levels (tree block, int level)
5518 while (block)
5520 BLOCK_NUMBER (block) = level;
5521 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
5522 block = BLOCK_CHAIN (block);
5526 /* Create a block containing landing pads and similar stuff. */
5528 static void
5529 construct_exit_block (void)
5531 rtx_insn *head = get_last_insn ();
5532 rtx_insn *end;
5533 basic_block exit_block;
5534 edge e, e2;
5535 unsigned ix;
5536 edge_iterator ei;
5537 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
5538 rtx_insn *orig_end = BB_END (prev_bb);
5540 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5542 /* Make sure the locus is set to the end of the function, so that
5543 epilogue line numbers and warnings are set properly. */
5544 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
5545 input_location = cfun->function_end_locus;
5547 /* Generate rtl for function exit. */
5548 expand_function_end ();
5550 end = get_last_insn ();
5551 if (head == end)
5552 return;
5553 /* While emitting the function end we could move end of the last basic
5554 block. */
5555 BB_END (prev_bb) = orig_end;
5556 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
5557 head = NEXT_INSN (head);
5558 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
5559 bb frequency counting will be confused. Any instructions before that
5560 label are emitted for the case where PREV_BB falls through into the
5561 exit block, so append those instructions to prev_bb in that case. */
5562 if (NEXT_INSN (head) != return_label)
5564 while (NEXT_INSN (head) != return_label)
5566 if (!NOTE_P (NEXT_INSN (head)))
5567 BB_END (prev_bb) = NEXT_INSN (head);
5568 head = NEXT_INSN (head);
5571 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
5572 exit_block->frequency = EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency;
5573 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5574 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5576 ix = 0;
5577 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
5579 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
5580 if (!(e->flags & EDGE_ABNORMAL))
5581 redirect_edge_succ (e, exit_block);
5582 else
5583 ix++;
5586 e = make_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5587 e->probability = REG_BR_PROB_BASE;
5588 e->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5589 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5590 if (e2 != e)
5592 e->count -= e2->count;
5593 exit_block->count -= e2->count;
5594 exit_block->frequency -= EDGE_FREQUENCY (e2);
5596 if (e->count < 0)
5597 e->count = 0;
5598 if (exit_block->count < 0)
5599 exit_block->count = 0;
5600 if (exit_block->frequency < 0)
5601 exit_block->frequency = 0;
5602 update_bb_for_insn (exit_block);
5605 /* Helper function for discover_nonconstant_array_refs.
5606 Look for ARRAY_REF nodes with non-constant indexes and mark them
5607 addressable. */
5609 static tree
5610 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
5611 void *data ATTRIBUTE_UNUSED)
5613 tree t = *tp;
5615 if (IS_TYPE_OR_DECL_P (t))
5616 *walk_subtrees = 0;
5617 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5619 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5620 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
5621 && (!TREE_OPERAND (t, 2)
5622 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5623 || (TREE_CODE (t) == COMPONENT_REF
5624 && (!TREE_OPERAND (t,2)
5625 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5626 || TREE_CODE (t) == BIT_FIELD_REF
5627 || TREE_CODE (t) == REALPART_EXPR
5628 || TREE_CODE (t) == IMAGPART_EXPR
5629 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5630 || CONVERT_EXPR_P (t))
5631 t = TREE_OPERAND (t, 0);
5633 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5635 t = get_base_address (t);
5636 if (t && DECL_P (t)
5637 && DECL_MODE (t) != BLKmode)
5638 TREE_ADDRESSABLE (t) = 1;
5641 *walk_subtrees = 0;
5644 return NULL_TREE;
5647 /* RTL expansion is not able to compile array references with variable
5648 offsets for arrays stored in single register. Discover such
5649 expressions and mark variables as addressable to avoid this
5650 scenario. */
5652 static void
5653 discover_nonconstant_array_refs (void)
5655 basic_block bb;
5656 gimple_stmt_iterator gsi;
5658 FOR_EACH_BB_FN (bb, cfun)
5659 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5661 gimple stmt = gsi_stmt (gsi);
5662 if (!is_gimple_debug (stmt))
5663 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
5667 /* This function sets crtl->args.internal_arg_pointer to a virtual
5668 register if DRAP is needed. Local register allocator will replace
5669 virtual_incoming_args_rtx with the virtual register. */
5671 static void
5672 expand_stack_alignment (void)
5674 rtx drap_rtx;
5675 unsigned int preferred_stack_boundary;
5677 if (! SUPPORTS_STACK_ALIGNMENT)
5678 return;
5680 if (cfun->calls_alloca
5681 || cfun->has_nonlocal_label
5682 || crtl->has_nonlocal_goto)
5683 crtl->need_drap = true;
5685 /* Call update_stack_boundary here again to update incoming stack
5686 boundary. It may set incoming stack alignment to a different
5687 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
5688 use the minimum incoming stack alignment to check if it is OK
5689 to perform sibcall optimization since sibcall optimization will
5690 only align the outgoing stack to incoming stack boundary. */
5691 if (targetm.calls.update_stack_boundary)
5692 targetm.calls.update_stack_boundary ();
5694 /* The incoming stack frame has to be aligned at least at
5695 parm_stack_boundary. */
5696 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
5698 /* Update crtl->stack_alignment_estimated and use it later to align
5699 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
5700 exceptions since callgraph doesn't collect incoming stack alignment
5701 in this case. */
5702 if (cfun->can_throw_non_call_exceptions
5703 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
5704 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
5705 else
5706 preferred_stack_boundary = crtl->preferred_stack_boundary;
5707 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
5708 crtl->stack_alignment_estimated = preferred_stack_boundary;
5709 if (preferred_stack_boundary > crtl->stack_alignment_needed)
5710 crtl->stack_alignment_needed = preferred_stack_boundary;
5712 gcc_assert (crtl->stack_alignment_needed
5713 <= crtl->stack_alignment_estimated);
5715 crtl->stack_realign_needed
5716 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
5717 crtl->stack_realign_tried = crtl->stack_realign_needed;
5719 crtl->stack_realign_processed = true;
5721 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
5722 alignment. */
5723 gcc_assert (targetm.calls.get_drap_rtx != NULL);
5724 drap_rtx = targetm.calls.get_drap_rtx ();
5726 /* stack_realign_drap and drap_rtx must match. */
5727 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
5729 /* Do nothing if NULL is returned, which means DRAP is not needed. */
5730 if (NULL != drap_rtx)
5732 crtl->args.internal_arg_pointer = drap_rtx;
5734 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
5735 needed. */
5736 fixup_tail_calls ();
5741 static void
5742 expand_main_function (void)
5744 #if (defined(INVOKE__main) \
5745 || (!defined(HAS_INIT_SECTION) \
5746 && !defined(INIT_SECTION_ASM_OP) \
5747 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
5748 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
5749 #endif
5753 /* Expand code to initialize the stack_protect_guard. This is invoked at
5754 the beginning of a function to be protected. */
5756 static void
5757 stack_protect_prologue (void)
5759 tree guard_decl = targetm.stack_protect_guard ();
5760 rtx x, y;
5762 x = expand_normal (crtl->stack_protect_guard);
5763 y = expand_normal (guard_decl);
5765 /* Allow the target to copy from Y to X without leaking Y into a
5766 register. */
5767 if (targetm.have_stack_protect_set ())
5768 if (rtx_insn *insn = targetm.gen_stack_protect_set (x, y))
5770 emit_insn (insn);
5771 return;
5774 /* Otherwise do a straight move. */
5775 emit_move_insn (x, y);
5778 /* Translate the intermediate representation contained in the CFG
5779 from GIMPLE trees to RTL.
5781 We do conversion per basic block and preserve/update the tree CFG.
5782 This implies we have to do some magic as the CFG can simultaneously
5783 consist of basic blocks containing RTL and GIMPLE trees. This can
5784 confuse the CFG hooks, so be careful to not manipulate CFG during
5785 the expansion. */
5787 namespace {
5789 const pass_data pass_data_expand =
5791 RTL_PASS, /* type */
5792 "expand", /* name */
5793 OPTGROUP_NONE, /* optinfo_flags */
5794 TV_EXPAND, /* tv_id */
5795 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
5796 | PROP_gimple_lcx
5797 | PROP_gimple_lvec
5798 | PROP_gimple_lva), /* properties_required */
5799 PROP_rtl, /* properties_provided */
5800 ( PROP_ssa | PROP_trees ), /* properties_destroyed */
5801 0, /* todo_flags_start */
5802 0, /* todo_flags_finish */
5805 class pass_expand : public rtl_opt_pass
5807 public:
5808 pass_expand (gcc::context *ctxt)
5809 : rtl_opt_pass (pass_data_expand, ctxt)
5812 /* opt_pass methods: */
5813 virtual unsigned int execute (function *);
5815 }; // class pass_expand
5817 unsigned int
5818 pass_expand::execute (function *fun)
5820 basic_block bb, init_block;
5821 sbitmap blocks;
5822 edge_iterator ei;
5823 edge e;
5824 rtx_insn *var_seq, *var_ret_seq;
5825 unsigned i;
5827 timevar_push (TV_OUT_OF_SSA);
5828 rewrite_out_of_ssa (&SA);
5829 timevar_pop (TV_OUT_OF_SSA);
5830 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
5832 if (MAY_HAVE_DEBUG_STMTS && flag_tree_ter)
5834 gimple_stmt_iterator gsi;
5835 FOR_EACH_BB_FN (bb, cfun)
5836 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5837 if (gimple_debug_bind_p (gsi_stmt (gsi)))
5838 avoid_deep_ter_for_debug (gsi_stmt (gsi), 0);
5841 /* Make sure all values used by the optimization passes have sane
5842 defaults. */
5843 reg_renumber = 0;
5845 /* Some backends want to know that we are expanding to RTL. */
5846 currently_expanding_to_rtl = 1;
5847 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
5848 free_dominance_info (CDI_DOMINATORS);
5850 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
5852 if (chkp_function_instrumented_p (current_function_decl))
5853 chkp_reset_rtl_bounds ();
5855 insn_locations_init ();
5856 if (!DECL_IS_BUILTIN (current_function_decl))
5858 /* Eventually, all FEs should explicitly set function_start_locus. */
5859 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
5860 set_curr_insn_location
5861 (DECL_SOURCE_LOCATION (current_function_decl));
5862 else
5863 set_curr_insn_location (fun->function_start_locus);
5865 else
5866 set_curr_insn_location (UNKNOWN_LOCATION);
5867 prologue_location = curr_insn_location ();
5869 #ifdef INSN_SCHEDULING
5870 init_sched_attrs ();
5871 #endif
5873 /* Make sure first insn is a note even if we don't want linenums.
5874 This makes sure the first insn will never be deleted.
5875 Also, final expects a note to appear there. */
5876 emit_note (NOTE_INSN_DELETED);
5878 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
5879 discover_nonconstant_array_refs ();
5881 targetm.expand_to_rtl_hook ();
5882 crtl->stack_alignment_needed = STACK_BOUNDARY;
5883 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
5884 crtl->stack_alignment_estimated = 0;
5885 crtl->preferred_stack_boundary = STACK_BOUNDARY;
5886 fun->cfg->max_jumptable_ents = 0;
5888 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
5889 of the function section at exapnsion time to predict distance of calls. */
5890 resolve_unique_section (current_function_decl, 0, flag_function_sections);
5892 /* Expand the variables recorded during gimple lowering. */
5893 timevar_push (TV_VAR_EXPAND);
5894 start_sequence ();
5896 var_ret_seq = expand_used_vars ();
5898 var_seq = get_insns ();
5899 end_sequence ();
5900 timevar_pop (TV_VAR_EXPAND);
5902 /* Honor stack protection warnings. */
5903 if (warn_stack_protect)
5905 if (fun->calls_alloca)
5906 warning (OPT_Wstack_protector,
5907 "stack protector not protecting local variables: "
5908 "variable length buffer");
5909 if (has_short_buffer && !crtl->stack_protect_guard)
5910 warning (OPT_Wstack_protector,
5911 "stack protector not protecting function: "
5912 "all local arrays are less than %d bytes long",
5913 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
5916 /* Set up parameters and prepare for return, for the function. */
5917 expand_function_start (current_function_decl);
5919 /* If we emitted any instructions for setting up the variables,
5920 emit them before the FUNCTION_START note. */
5921 if (var_seq)
5923 emit_insn_before (var_seq, parm_birth_insn);
5925 /* In expand_function_end we'll insert the alloca save/restore
5926 before parm_birth_insn. We've just insertted an alloca call.
5927 Adjust the pointer to match. */
5928 parm_birth_insn = var_seq;
5931 /* Now that we also have the parameter RTXs, copy them over to our
5932 partitions. */
5933 for (i = 0; i < SA.map->num_partitions; i++)
5935 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
5937 if (TREE_CODE (var) != VAR_DECL
5938 && !SA.partition_to_pseudo[i])
5939 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
5940 gcc_assert (SA.partition_to_pseudo[i]);
5942 /* If this decl was marked as living in multiple places, reset
5943 this now to NULL. */
5944 if (DECL_RTL_IF_SET (var) == pc_rtx)
5945 SET_DECL_RTL (var, NULL);
5947 /* Some RTL parts really want to look at DECL_RTL(x) when x
5948 was a decl marked in REG_ATTR or MEM_ATTR. We could use
5949 SET_DECL_RTL here making this available, but that would mean
5950 to select one of the potentially many RTLs for one DECL. Instead
5951 of doing that we simply reset the MEM_EXPR of the RTL in question,
5952 then nobody can get at it and hence nobody can call DECL_RTL on it. */
5953 if (!DECL_RTL_SET_P (var))
5955 if (MEM_P (SA.partition_to_pseudo[i]))
5956 set_mem_expr (SA.partition_to_pseudo[i], NULL);
5960 /* If we have a class containing differently aligned pointers
5961 we need to merge those into the corresponding RTL pointer
5962 alignment. */
5963 for (i = 1; i < num_ssa_names; i++)
5965 tree name = ssa_name (i);
5966 int part;
5967 rtx r;
5969 if (!name
5970 /* We might have generated new SSA names in
5971 update_alias_info_with_stack_vars. They will have a NULL
5972 defining statements, and won't be part of the partitioning,
5973 so ignore those. */
5974 || !SSA_NAME_DEF_STMT (name))
5975 continue;
5976 part = var_to_partition (SA.map, name);
5977 if (part == NO_PARTITION)
5978 continue;
5980 /* Adjust all partition members to get the underlying decl of
5981 the representative which we might have created in expand_one_var. */
5982 if (SSA_NAME_VAR (name) == NULL_TREE)
5984 tree leader = partition_to_var (SA.map, part);
5985 gcc_assert (SSA_NAME_VAR (leader) != NULL_TREE);
5986 replace_ssa_name_symbol (name, SSA_NAME_VAR (leader));
5988 if (!POINTER_TYPE_P (TREE_TYPE (name)))
5989 continue;
5991 r = SA.partition_to_pseudo[part];
5992 if (REG_P (r))
5993 mark_reg_pointer (r, get_pointer_alignment (name));
5996 /* If this function is `main', emit a call to `__main'
5997 to run global initializers, etc. */
5998 if (DECL_NAME (current_function_decl)
5999 && MAIN_NAME_P (DECL_NAME (current_function_decl))
6000 && DECL_FILE_SCOPE_P (current_function_decl))
6001 expand_main_function ();
6003 /* Initialize the stack_protect_guard field. This must happen after the
6004 call to __main (if any) so that the external decl is initialized. */
6005 if (crtl->stack_protect_guard)
6006 stack_protect_prologue ();
6008 expand_phi_nodes (&SA);
6010 /* Register rtl specific functions for cfg. */
6011 rtl_register_cfg_hooks ();
6013 init_block = construct_init_block ();
6015 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
6016 remaining edges later. */
6017 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
6018 e->flags &= ~EDGE_EXECUTABLE;
6020 lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
6021 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
6022 next_bb)
6023 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
6025 if (MAY_HAVE_DEBUG_INSNS)
6026 expand_debug_locations ();
6028 if (deep_ter_debug_map)
6030 delete deep_ter_debug_map;
6031 deep_ter_debug_map = NULL;
6034 /* Free stuff we no longer need after GIMPLE optimizations. */
6035 free_dominance_info (CDI_DOMINATORS);
6036 free_dominance_info (CDI_POST_DOMINATORS);
6037 delete_tree_cfg_annotations ();
6039 timevar_push (TV_OUT_OF_SSA);
6040 finish_out_of_ssa (&SA);
6041 timevar_pop (TV_OUT_OF_SSA);
6043 timevar_push (TV_POST_EXPAND);
6044 /* We are no longer in SSA form. */
6045 fun->gimple_df->in_ssa_p = false;
6046 loops_state_clear (LOOP_CLOSED_SSA);
6048 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
6049 conservatively to true until they are all profile aware. */
6050 delete lab_rtx_for_bb;
6051 free_histograms ();
6053 construct_exit_block ();
6054 insn_locations_finalize ();
6056 if (var_ret_seq)
6058 rtx_insn *after = return_label;
6059 rtx_insn *next = NEXT_INSN (after);
6060 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
6061 after = next;
6062 emit_insn_after (var_ret_seq, after);
6065 /* Zap the tree EH table. */
6066 set_eh_throw_stmt_table (fun, NULL);
6068 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
6069 split edges which edge insertions might do. */
6070 rebuild_jump_labels (get_insns ());
6072 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun),
6073 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6075 edge e;
6076 edge_iterator ei;
6077 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6079 if (e->insns.r)
6081 rebuild_jump_labels_chain (e->insns.r);
6082 /* Put insns after parm birth, but before
6083 NOTE_INSNS_FUNCTION_BEG. */
6084 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (fun)
6085 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
6087 rtx_insn *insns = e->insns.r;
6088 e->insns.r = NULL;
6089 if (NOTE_P (parm_birth_insn)
6090 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
6091 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
6092 else
6093 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
6095 else
6096 commit_one_edge_insertion (e);
6098 else
6099 ei_next (&ei);
6103 /* We're done expanding trees to RTL. */
6104 currently_expanding_to_rtl = 0;
6106 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
6107 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6109 edge e;
6110 edge_iterator ei;
6111 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6113 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
6114 e->flags &= ~EDGE_EXECUTABLE;
6116 /* At the moment not all abnormal edges match the RTL
6117 representation. It is safe to remove them here as
6118 find_many_sub_basic_blocks will rediscover them.
6119 In the future we should get this fixed properly. */
6120 if ((e->flags & EDGE_ABNORMAL)
6121 && !(e->flags & EDGE_SIBCALL))
6122 remove_edge (e);
6123 else
6124 ei_next (&ei);
6128 blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
6129 bitmap_ones (blocks);
6130 find_many_sub_basic_blocks (blocks);
6131 sbitmap_free (blocks);
6132 purge_all_dead_edges ();
6134 expand_stack_alignment ();
6136 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
6137 function. */
6138 if (crtl->tail_call_emit)
6139 fixup_tail_calls ();
6141 /* After initial rtl generation, call back to finish generating
6142 exception support code. We need to do this before cleaning up
6143 the CFG as the code does not expect dead landing pads. */
6144 if (fun->eh->region_tree != NULL)
6145 finish_eh_generation ();
6147 /* Remove unreachable blocks, otherwise we cannot compute dominators
6148 which are needed for loop state verification. As a side-effect
6149 this also compacts blocks.
6150 ??? We cannot remove trivially dead insns here as for example
6151 the DRAP reg on i?86 is not magically live at this point.
6152 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
6153 cleanup_cfg (CLEANUP_NO_INSN_DEL);
6155 #ifdef ENABLE_CHECKING
6156 verify_flow_info ();
6157 #endif
6159 /* Initialize pseudos allocated for hard registers. */
6160 emit_initial_value_sets ();
6162 /* And finally unshare all RTL. */
6163 unshare_all_rtl ();
6165 /* There's no need to defer outputting this function any more; we
6166 know we want to output it. */
6167 DECL_DEFER_OUTPUT (current_function_decl) = 0;
6169 /* Now that we're done expanding trees to RTL, we shouldn't have any
6170 more CONCATs anywhere. */
6171 generating_concat_p = 0;
6173 if (dump_file)
6175 fprintf (dump_file,
6176 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
6177 /* And the pass manager will dump RTL for us. */
6180 /* If we're emitting a nested function, make sure its parent gets
6181 emitted as well. Doing otherwise confuses debug info. */
6183 tree parent;
6184 for (parent = DECL_CONTEXT (current_function_decl);
6185 parent != NULL_TREE;
6186 parent = get_containing_scope (parent))
6187 if (TREE_CODE (parent) == FUNCTION_DECL)
6188 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
6191 /* We are now committed to emitting code for this function. Do any
6192 preparation, such as emitting abstract debug info for the inline
6193 before it gets mangled by optimization. */
6194 if (cgraph_function_possibly_inlined_p (current_function_decl))
6195 (*debug_hooks->outlining_inline_function) (current_function_decl);
6197 TREE_ASM_WRITTEN (current_function_decl) = 1;
6199 /* After expanding, the return labels are no longer needed. */
6200 return_label = NULL;
6201 naked_return_label = NULL;
6203 /* After expanding, the tm_restart map is no longer needed. */
6204 if (fun->gimple_df->tm_restart)
6205 fun->gimple_df->tm_restart = NULL;
6207 /* Tag the blocks with a depth number so that change_scope can find
6208 the common parent easily. */
6209 set_block_levels (DECL_INITIAL (fun->decl), 0);
6210 default_rtl_profile ();
6212 timevar_pop (TV_POST_EXPAND);
6214 return 0;
6217 } // anon namespace
6219 rtl_opt_pass *
6220 make_pass_expand (gcc::context *ctxt)
6222 return new pass_expand (ctxt);