PR target/37072
[official-gcc.git] / gcc / cfgexpand.c
blob05eb2ad513540694215bdbf9b92601b50e179057
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 "tm.h"
24 #include "rtl.h"
25 #include "hard-reg-set.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "stringpool.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 "predict.h"
37 #include "function.h"
38 #include "dominance.h"
39 #include "cfg.h"
40 #include "cfgrtl.h"
41 #include "cfganal.h"
42 #include "cfgbuild.h"
43 #include "cfgcleanup.h"
44 #include "basic-block.h"
45 #include "insn-codes.h"
46 #include "optabs.h"
47 #include "flags.h"
48 #include "insn-config.h"
49 #include "expmed.h"
50 #include "dojump.h"
51 #include "explow.h"
52 #include "calls.h"
53 #include "emit-rtl.h"
54 #include "expr.h"
55 #include "langhooks.h"
56 #include "bitmap.h"
57 #include "tree-ssa-alias.h"
58 #include "internal-fn.h"
59 #include "tree-eh.h"
60 #include "gimple-expr.h"
61 #include "gimple.h"
62 #include "gimple-iterator.h"
63 #include "gimple-walk.h"
64 #include "gimple-ssa.h"
65 #include "cgraph.h"
66 #include "tree-cfg.h"
67 #include "tree-phinodes.h"
68 #include "ssa-iterators.h"
69 #include "tree-ssanames.h"
70 #include "tree-dfa.h"
71 #include "tree-ssa.h"
72 #include "tree-pass.h"
73 #include "except.h"
74 #include "diagnostic.h"
75 #include "gimple-pretty-print.h"
76 #include "toplev.h"
77 #include "debug.h"
78 #include "params.h"
79 #include "tree-inline.h"
80 #include "value-prof.h"
81 #include "target.h"
82 #include "tree-ssa-live.h"
83 #include "tree-outof-ssa.h"
84 #include "sbitmap.h"
85 #include "cfgloop.h"
86 #include "regs.h" /* For reg_renumber. */
87 #include "insn-attr.h" /* For INSN_SCHEDULING. */
88 #include "asan.h"
89 #include "tree-ssa-address.h"
90 #include "recog.h"
91 #include "output.h"
92 #include "builtins.h"
93 #include "tree-chkp.h"
94 #include "rtl-chkp.h"
96 /* Some systems use __main in a way incompatible with its use in gcc, in these
97 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
98 give the same symbol without quotes for an alternative entry point. You
99 must define both, or neither. */
100 #ifndef NAME__MAIN
101 #define NAME__MAIN "__main"
102 #endif
104 /* This variable holds information helping the rewriting of SSA trees
105 into RTL. */
106 struct ssaexpand SA;
108 /* This variable holds the currently expanded gimple statement for purposes
109 of comminucating the profile info to the builtin expanders. */
110 gimple currently_expanding_gimple_stmt;
112 static rtx expand_debug_expr (tree);
114 /* Return an expression tree corresponding to the RHS of GIMPLE
115 statement STMT. */
117 tree
118 gimple_assign_rhs_to_tree (gimple stmt)
120 tree t;
121 enum gimple_rhs_class grhs_class;
123 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
125 if (grhs_class == GIMPLE_TERNARY_RHS)
126 t = build3 (gimple_assign_rhs_code (stmt),
127 TREE_TYPE (gimple_assign_lhs (stmt)),
128 gimple_assign_rhs1 (stmt),
129 gimple_assign_rhs2 (stmt),
130 gimple_assign_rhs3 (stmt));
131 else if (grhs_class == GIMPLE_BINARY_RHS)
132 t = build2 (gimple_assign_rhs_code (stmt),
133 TREE_TYPE (gimple_assign_lhs (stmt)),
134 gimple_assign_rhs1 (stmt),
135 gimple_assign_rhs2 (stmt));
136 else if (grhs_class == GIMPLE_UNARY_RHS)
137 t = build1 (gimple_assign_rhs_code (stmt),
138 TREE_TYPE (gimple_assign_lhs (stmt)),
139 gimple_assign_rhs1 (stmt));
140 else if (grhs_class == GIMPLE_SINGLE_RHS)
142 t = gimple_assign_rhs1 (stmt);
143 /* Avoid modifying this tree in place below. */
144 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
145 && gimple_location (stmt) != EXPR_LOCATION (t))
146 || (gimple_block (stmt)
147 && currently_expanding_to_rtl
148 && EXPR_P (t)))
149 t = copy_node (t);
151 else
152 gcc_unreachable ();
154 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
155 SET_EXPR_LOCATION (t, gimple_location (stmt));
157 return t;
161 #ifndef STACK_ALIGNMENT_NEEDED
162 #define STACK_ALIGNMENT_NEEDED 1
163 #endif
165 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
167 /* Associate declaration T with storage space X. If T is no
168 SSA name this is exactly SET_DECL_RTL, otherwise make the
169 partition of T associated with X. */
170 static inline void
171 set_rtl (tree t, rtx x)
173 if (TREE_CODE (t) == SSA_NAME)
175 SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
176 if (x && !MEM_P (x))
177 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
178 /* For the benefit of debug information at -O0 (where vartracking
179 doesn't run) record the place also in the base DECL if it's
180 a normal variable (not a parameter). */
181 if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
183 tree var = SSA_NAME_VAR (t);
184 /* If we don't yet have something recorded, just record it now. */
185 if (!DECL_RTL_SET_P (var))
186 SET_DECL_RTL (var, x);
187 /* If we have it set already to "multiple places" don't
188 change this. */
189 else if (DECL_RTL (var) == pc_rtx)
191 /* If we have something recorded and it's not the same place
192 as we want to record now, we have multiple partitions for the
193 same base variable, with different places. We can't just
194 randomly chose one, hence we have to say that we don't know.
195 This only happens with optimization, and there var-tracking
196 will figure out the right thing. */
197 else if (DECL_RTL (var) != x)
198 SET_DECL_RTL (var, pc_rtx);
201 else
202 SET_DECL_RTL (t, x);
205 /* This structure holds data relevant to one variable that will be
206 placed in a stack slot. */
207 struct stack_var
209 /* The Variable. */
210 tree decl;
212 /* Initially, the size of the variable. Later, the size of the partition,
213 if this variable becomes it's partition's representative. */
214 HOST_WIDE_INT size;
216 /* The *byte* alignment required for this variable. Or as, with the
217 size, the alignment for this partition. */
218 unsigned int alignb;
220 /* The partition representative. */
221 size_t representative;
223 /* The next stack variable in the partition, or EOC. */
224 size_t next;
226 /* The numbers of conflicting stack variables. */
227 bitmap conflicts;
230 #define EOC ((size_t)-1)
232 /* We have an array of such objects while deciding allocation. */
233 static struct stack_var *stack_vars;
234 static size_t stack_vars_alloc;
235 static size_t stack_vars_num;
236 static hash_map<tree, size_t> *decl_to_stack_part;
238 /* Conflict bitmaps go on this obstack. This allows us to destroy
239 all of them in one big sweep. */
240 static bitmap_obstack stack_var_bitmap_obstack;
242 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
243 is non-decreasing. */
244 static size_t *stack_vars_sorted;
246 /* The phase of the stack frame. This is the known misalignment of
247 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
248 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
249 static int frame_phase;
251 /* Used during expand_used_vars to remember if we saw any decls for
252 which we'd like to enable stack smashing protection. */
253 static bool has_protected_decls;
255 /* Used during expand_used_vars. Remember if we say a character buffer
256 smaller than our cutoff threshold. Used for -Wstack-protector. */
257 static bool has_short_buffer;
259 /* Compute the byte alignment to use for DECL. Ignore alignment
260 we can't do with expected alignment of the stack boundary. */
262 static unsigned int
263 align_local_variable (tree decl)
265 unsigned int align = LOCAL_DECL_ALIGNMENT (decl);
266 DECL_ALIGN (decl) = align;
267 return align / BITS_PER_UNIT;
270 /* Align given offset BASE with ALIGN. Truncate up if ALIGN_UP is true,
271 down otherwise. Return truncated BASE value. */
273 static inline unsigned HOST_WIDE_INT
274 align_base (HOST_WIDE_INT base, unsigned HOST_WIDE_INT align, bool align_up)
276 return align_up ? (base + align - 1) & -align : base & -align;
279 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
280 Return the frame offset. */
282 static HOST_WIDE_INT
283 alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
285 HOST_WIDE_INT offset, new_frame_offset;
287 if (FRAME_GROWS_DOWNWARD)
289 new_frame_offset
290 = align_base (frame_offset - frame_phase - size,
291 align, false) + frame_phase;
292 offset = new_frame_offset;
294 else
296 new_frame_offset
297 = align_base (frame_offset - frame_phase, align, true) + frame_phase;
298 offset = new_frame_offset;
299 new_frame_offset += size;
301 frame_offset = new_frame_offset;
303 if (frame_offset_overflow (frame_offset, cfun->decl))
304 frame_offset = offset = 0;
306 return offset;
309 /* Accumulate DECL into STACK_VARS. */
311 static void
312 add_stack_var (tree decl)
314 struct stack_var *v;
316 if (stack_vars_num >= stack_vars_alloc)
318 if (stack_vars_alloc)
319 stack_vars_alloc = stack_vars_alloc * 3 / 2;
320 else
321 stack_vars_alloc = 32;
322 stack_vars
323 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
325 if (!decl_to_stack_part)
326 decl_to_stack_part = new hash_map<tree, size_t>;
328 v = &stack_vars[stack_vars_num];
329 decl_to_stack_part->put (decl, stack_vars_num);
331 v->decl = decl;
332 v->size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (decl)));
333 /* Ensure that all variables have size, so that &a != &b for any two
334 variables that are simultaneously live. */
335 if (v->size == 0)
336 v->size = 1;
337 v->alignb = align_local_variable (SSAVAR (decl));
338 /* An alignment of zero can mightily confuse us later. */
339 gcc_assert (v->alignb != 0);
341 /* All variables are initially in their own partition. */
342 v->representative = stack_vars_num;
343 v->next = EOC;
345 /* All variables initially conflict with no other. */
346 v->conflicts = NULL;
348 /* Ensure that this decl doesn't get put onto the list twice. */
349 set_rtl (decl, pc_rtx);
351 stack_vars_num++;
354 /* Make the decls associated with luid's X and Y conflict. */
356 static void
357 add_stack_var_conflict (size_t x, size_t y)
359 struct stack_var *a = &stack_vars[x];
360 struct stack_var *b = &stack_vars[y];
361 if (!a->conflicts)
362 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
363 if (!b->conflicts)
364 b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
365 bitmap_set_bit (a->conflicts, y);
366 bitmap_set_bit (b->conflicts, x);
369 /* Check whether the decls associated with luid's X and Y conflict. */
371 static bool
372 stack_var_conflict_p (size_t x, size_t y)
374 struct stack_var *a = &stack_vars[x];
375 struct stack_var *b = &stack_vars[y];
376 if (x == y)
377 return false;
378 /* Partitions containing an SSA name result from gimple registers
379 with things like unsupported modes. They are top-level and
380 hence conflict with everything else. */
381 if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
382 return true;
384 if (!a->conflicts || !b->conflicts)
385 return false;
386 return bitmap_bit_p (a->conflicts, y);
389 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
390 enter its partition number into bitmap DATA. */
392 static bool
393 visit_op (gimple, tree op, tree, void *data)
395 bitmap active = (bitmap)data;
396 op = get_base_address (op);
397 if (op
398 && DECL_P (op)
399 && DECL_RTL_IF_SET (op) == pc_rtx)
401 size_t *v = decl_to_stack_part->get (op);
402 if (v)
403 bitmap_set_bit (active, *v);
405 return false;
408 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
409 record conflicts between it and all currently active other partitions
410 from bitmap DATA. */
412 static bool
413 visit_conflict (gimple, tree op, tree, void *data)
415 bitmap active = (bitmap)data;
416 op = get_base_address (op);
417 if (op
418 && DECL_P (op)
419 && DECL_RTL_IF_SET (op) == pc_rtx)
421 size_t *v = decl_to_stack_part->get (op);
422 if (v && bitmap_set_bit (active, *v))
424 size_t num = *v;
425 bitmap_iterator bi;
426 unsigned i;
427 gcc_assert (num < stack_vars_num);
428 EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
429 add_stack_var_conflict (num, i);
432 return false;
435 /* Helper routine for add_scope_conflicts, calculating the active partitions
436 at the end of BB, leaving the result in WORK. We're called to generate
437 conflicts when FOR_CONFLICT is true, otherwise we're just tracking
438 liveness. */
440 static void
441 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
443 edge e;
444 edge_iterator ei;
445 gimple_stmt_iterator gsi;
446 walk_stmt_load_store_addr_fn visit;
448 bitmap_clear (work);
449 FOR_EACH_EDGE (e, ei, bb->preds)
450 bitmap_ior_into (work, (bitmap)e->src->aux);
452 visit = visit_op;
454 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
456 gimple stmt = gsi_stmt (gsi);
457 walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
459 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
461 gimple stmt = gsi_stmt (gsi);
463 if (gimple_clobber_p (stmt))
465 tree lhs = gimple_assign_lhs (stmt);
466 size_t *v;
467 /* Nested function lowering might introduce LHSs
468 that are COMPONENT_REFs. */
469 if (TREE_CODE (lhs) != VAR_DECL)
470 continue;
471 if (DECL_RTL_IF_SET (lhs) == pc_rtx
472 && (v = decl_to_stack_part->get (lhs)))
473 bitmap_clear_bit (work, *v);
475 else if (!is_gimple_debug (stmt))
477 if (for_conflict
478 && visit == visit_op)
480 /* If this is the first real instruction in this BB we need
481 to add conflicts for everything live at this point now.
482 Unlike classical liveness for named objects we can't
483 rely on seeing a def/use of the names we're interested in.
484 There might merely be indirect loads/stores. We'd not add any
485 conflicts for such partitions. */
486 bitmap_iterator bi;
487 unsigned i;
488 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
490 struct stack_var *a = &stack_vars[i];
491 if (!a->conflicts)
492 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
493 bitmap_ior_into (a->conflicts, work);
495 visit = visit_conflict;
497 walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
502 /* Generate stack partition conflicts between all partitions that are
503 simultaneously live. */
505 static void
506 add_scope_conflicts (void)
508 basic_block bb;
509 bool changed;
510 bitmap work = BITMAP_ALLOC (NULL);
511 int *rpo;
512 int n_bbs;
514 /* We approximate the live range of a stack variable by taking the first
515 mention of its name as starting point(s), and by the end-of-scope
516 death clobber added by gimplify as ending point(s) of the range.
517 This overapproximates in the case we for instance moved an address-taken
518 operation upward, without also moving a dereference to it upwards.
519 But it's conservatively correct as a variable never can hold values
520 before its name is mentioned at least once.
522 We then do a mostly classical bitmap liveness algorithm. */
524 FOR_ALL_BB_FN (bb, cfun)
525 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
527 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
528 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
530 changed = true;
531 while (changed)
533 int i;
534 changed = false;
535 for (i = 0; i < n_bbs; i++)
537 bitmap active;
538 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
539 active = (bitmap)bb->aux;
540 add_scope_conflicts_1 (bb, work, false);
541 if (bitmap_ior_into (active, work))
542 changed = true;
546 FOR_EACH_BB_FN (bb, cfun)
547 add_scope_conflicts_1 (bb, work, true);
549 free (rpo);
550 BITMAP_FREE (work);
551 FOR_ALL_BB_FN (bb, cfun)
552 BITMAP_FREE (bb->aux);
555 /* A subroutine of partition_stack_vars. A comparison function for qsort,
556 sorting an array of indices by the properties of the object. */
558 static int
559 stack_var_cmp (const void *a, const void *b)
561 size_t ia = *(const size_t *)a;
562 size_t ib = *(const size_t *)b;
563 unsigned int aligna = stack_vars[ia].alignb;
564 unsigned int alignb = stack_vars[ib].alignb;
565 HOST_WIDE_INT sizea = stack_vars[ia].size;
566 HOST_WIDE_INT sizeb = stack_vars[ib].size;
567 tree decla = stack_vars[ia].decl;
568 tree declb = stack_vars[ib].decl;
569 bool largea, largeb;
570 unsigned int uida, uidb;
572 /* Primary compare on "large" alignment. Large comes first. */
573 largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
574 largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
575 if (largea != largeb)
576 return (int)largeb - (int)largea;
578 /* Secondary compare on size, decreasing */
579 if (sizea > sizeb)
580 return -1;
581 if (sizea < sizeb)
582 return 1;
584 /* Tertiary compare on true alignment, decreasing. */
585 if (aligna < alignb)
586 return -1;
587 if (aligna > alignb)
588 return 1;
590 /* Final compare on ID for sort stability, increasing.
591 Two SSA names are compared by their version, SSA names come before
592 non-SSA names, and two normal decls are compared by their DECL_UID. */
593 if (TREE_CODE (decla) == SSA_NAME)
595 if (TREE_CODE (declb) == SSA_NAME)
596 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
597 else
598 return -1;
600 else if (TREE_CODE (declb) == SSA_NAME)
601 return 1;
602 else
603 uida = DECL_UID (decla), uidb = DECL_UID (declb);
604 if (uida < uidb)
605 return 1;
606 if (uida > uidb)
607 return -1;
608 return 0;
611 struct part_traits : unbounded_int_hashmap_traits <size_t, bitmap> {};
612 typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
614 /* If the points-to solution *PI points to variables that are in a partition
615 together with other variables add all partition members to the pointed-to
616 variables bitmap. */
618 static void
619 add_partitioned_vars_to_ptset (struct pt_solution *pt,
620 part_hashmap *decls_to_partitions,
621 hash_set<bitmap> *visited, bitmap temp)
623 bitmap_iterator bi;
624 unsigned i;
625 bitmap *part;
627 if (pt->anything
628 || pt->vars == NULL
629 /* The pointed-to vars bitmap is shared, it is enough to
630 visit it once. */
631 || visited->add (pt->vars))
632 return;
634 bitmap_clear (temp);
636 /* By using a temporary bitmap to store all members of the partitions
637 we have to add we make sure to visit each of the partitions only
638 once. */
639 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
640 if ((!temp
641 || !bitmap_bit_p (temp, i))
642 && (part = decls_to_partitions->get (i)))
643 bitmap_ior_into (temp, *part);
644 if (!bitmap_empty_p (temp))
645 bitmap_ior_into (pt->vars, temp);
648 /* Update points-to sets based on partition info, so we can use them on RTL.
649 The bitmaps representing stack partitions will be saved until expand,
650 where partitioned decls used as bases in memory expressions will be
651 rewritten. */
653 static void
654 update_alias_info_with_stack_vars (void)
656 part_hashmap *decls_to_partitions = NULL;
657 size_t i, j;
658 tree var = NULL_TREE;
660 for (i = 0; i < stack_vars_num; i++)
662 bitmap part = NULL;
663 tree name;
664 struct ptr_info_def *pi;
666 /* Not interested in partitions with single variable. */
667 if (stack_vars[i].representative != i
668 || stack_vars[i].next == EOC)
669 continue;
671 if (!decls_to_partitions)
673 decls_to_partitions = new part_hashmap;
674 cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
677 /* Create an SSA_NAME that points to the partition for use
678 as base during alias-oracle queries on RTL for bases that
679 have been partitioned. */
680 if (var == NULL_TREE)
681 var = create_tmp_var (ptr_type_node);
682 name = make_ssa_name (var);
684 /* Create bitmaps representing partitions. They will be used for
685 points-to sets later, so use GGC alloc. */
686 part = BITMAP_GGC_ALLOC ();
687 for (j = i; j != EOC; j = stack_vars[j].next)
689 tree decl = stack_vars[j].decl;
690 unsigned int uid = DECL_PT_UID (decl);
691 bitmap_set_bit (part, uid);
692 decls_to_partitions->put (uid, part);
693 cfun->gimple_df->decls_to_pointers->put (decl, name);
694 if (TREE_ADDRESSABLE (decl))
695 TREE_ADDRESSABLE (name) = 1;
698 /* Make the SSA name point to all partition members. */
699 pi = get_ptr_info (name);
700 pt_solution_set (&pi->pt, part, false);
703 /* Make all points-to sets that contain one member of a partition
704 contain all members of the partition. */
705 if (decls_to_partitions)
707 unsigned i;
708 hash_set<bitmap> visited;
709 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
711 for (i = 1; i < num_ssa_names; i++)
713 tree name = ssa_name (i);
714 struct ptr_info_def *pi;
716 if (name
717 && POINTER_TYPE_P (TREE_TYPE (name))
718 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
719 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
720 &visited, temp);
723 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
724 decls_to_partitions, &visited, temp);
726 delete decls_to_partitions;
727 BITMAP_FREE (temp);
731 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
732 partitioning algorithm. Partitions A and B are known to be non-conflicting.
733 Merge them into a single partition A. */
735 static void
736 union_stack_vars (size_t a, size_t b)
738 struct stack_var *vb = &stack_vars[b];
739 bitmap_iterator bi;
740 unsigned u;
742 gcc_assert (stack_vars[b].next == EOC);
743 /* Add B to A's partition. */
744 stack_vars[b].next = stack_vars[a].next;
745 stack_vars[b].representative = a;
746 stack_vars[a].next = b;
748 /* Update the required alignment of partition A to account for B. */
749 if (stack_vars[a].alignb < stack_vars[b].alignb)
750 stack_vars[a].alignb = stack_vars[b].alignb;
752 /* Update the interference graph and merge the conflicts. */
753 if (vb->conflicts)
755 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
756 add_stack_var_conflict (a, stack_vars[u].representative);
757 BITMAP_FREE (vb->conflicts);
761 /* A subroutine of expand_used_vars. Binpack the variables into
762 partitions constrained by the interference graph. The overall
763 algorithm used is as follows:
765 Sort the objects by size in descending order.
766 For each object A {
767 S = size(A)
768 O = 0
769 loop {
770 Look for the largest non-conflicting object B with size <= S.
771 UNION (A, B)
776 static void
777 partition_stack_vars (void)
779 size_t si, sj, n = stack_vars_num;
781 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
782 for (si = 0; si < n; ++si)
783 stack_vars_sorted[si] = si;
785 if (n == 1)
786 return;
788 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
790 for (si = 0; si < n; ++si)
792 size_t i = stack_vars_sorted[si];
793 unsigned int ialign = stack_vars[i].alignb;
794 HOST_WIDE_INT isize = stack_vars[i].size;
796 /* Ignore objects that aren't partition representatives. If we
797 see a var that is not a partition representative, it must
798 have been merged earlier. */
799 if (stack_vars[i].representative != i)
800 continue;
802 for (sj = si + 1; sj < n; ++sj)
804 size_t j = stack_vars_sorted[sj];
805 unsigned int jalign = stack_vars[j].alignb;
806 HOST_WIDE_INT jsize = stack_vars[j].size;
808 /* Ignore objects that aren't partition representatives. */
809 if (stack_vars[j].representative != j)
810 continue;
812 /* Do not mix objects of "small" (supported) alignment
813 and "large" (unsupported) alignment. */
814 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
815 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
816 break;
818 /* For Address Sanitizer do not mix objects with different
819 sizes, as the shorter vars wouldn't be adequately protected.
820 Don't do that for "large" (unsupported) alignment objects,
821 those aren't protected anyway. */
822 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && isize != jsize
823 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
824 break;
826 /* Ignore conflicting objects. */
827 if (stack_var_conflict_p (i, j))
828 continue;
830 /* UNION the objects, placing J at OFFSET. */
831 union_stack_vars (i, j);
835 update_alias_info_with_stack_vars ();
838 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
840 static void
841 dump_stack_var_partition (void)
843 size_t si, i, j, n = stack_vars_num;
845 for (si = 0; si < n; ++si)
847 i = stack_vars_sorted[si];
849 /* Skip variables that aren't partition representatives, for now. */
850 if (stack_vars[i].representative != i)
851 continue;
853 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
854 " align %u\n", (unsigned long) i, stack_vars[i].size,
855 stack_vars[i].alignb);
857 for (j = i; j != EOC; j = stack_vars[j].next)
859 fputc ('\t', dump_file);
860 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
862 fputc ('\n', dump_file);
866 /* Assign rtl to DECL at BASE + OFFSET. */
868 static void
869 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
870 HOST_WIDE_INT offset)
872 unsigned align;
873 rtx x;
875 /* If this fails, we've overflowed the stack frame. Error nicely? */
876 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
878 x = plus_constant (Pmode, base, offset);
879 x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
881 if (TREE_CODE (decl) != SSA_NAME)
883 /* Set alignment we actually gave this decl if it isn't an SSA name.
884 If it is we generate stack slots only accidentally so it isn't as
885 important, we'll simply use the alignment that is already set. */
886 if (base == virtual_stack_vars_rtx)
887 offset -= frame_phase;
888 align = offset & -offset;
889 align *= BITS_PER_UNIT;
890 if (align == 0 || align > base_align)
891 align = base_align;
893 /* One would think that we could assert that we're not decreasing
894 alignment here, but (at least) the i386 port does exactly this
895 via the MINIMUM_ALIGNMENT hook. */
897 DECL_ALIGN (decl) = align;
898 DECL_USER_ALIGN (decl) = 0;
901 set_mem_attributes (x, SSAVAR (decl), true);
902 set_rtl (decl, x);
905 struct stack_vars_data
907 /* Vector of offset pairs, always end of some padding followed
908 by start of the padding that needs Address Sanitizer protection.
909 The vector is in reversed, highest offset pairs come first. */
910 vec<HOST_WIDE_INT> asan_vec;
912 /* Vector of partition representative decls in between the paddings. */
913 vec<tree> asan_decl_vec;
915 /* Base pseudo register for Address Sanitizer protected automatic vars. */
916 rtx asan_base;
918 /* Alignment needed for the Address Sanitizer protected automatic vars. */
919 unsigned int asan_alignb;
922 /* A subroutine of expand_used_vars. Give each partition representative
923 a unique location within the stack frame. Update each partition member
924 with that location. */
926 static void
927 expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
929 size_t si, i, j, n = stack_vars_num;
930 HOST_WIDE_INT large_size = 0, large_alloc = 0;
931 rtx large_base = NULL;
932 unsigned large_align = 0;
933 tree decl;
935 /* Determine if there are any variables requiring "large" alignment.
936 Since these are dynamically allocated, we only process these if
937 no predicate involved. */
938 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
939 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
941 /* Find the total size of these variables. */
942 for (si = 0; si < n; ++si)
944 unsigned alignb;
946 i = stack_vars_sorted[si];
947 alignb = stack_vars[i].alignb;
949 /* All "large" alignment decls come before all "small" alignment
950 decls, but "large" alignment decls are not sorted based on
951 their alignment. Increase large_align to track the largest
952 required alignment. */
953 if ((alignb * BITS_PER_UNIT) > large_align)
954 large_align = alignb * BITS_PER_UNIT;
956 /* Stop when we get to the first decl with "small" alignment. */
957 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
958 break;
960 /* Skip variables that aren't partition representatives. */
961 if (stack_vars[i].representative != i)
962 continue;
964 /* Skip variables that have already had rtl assigned. See also
965 add_stack_var where we perpetrate this pc_rtx hack. */
966 decl = stack_vars[i].decl;
967 if ((TREE_CODE (decl) == SSA_NAME
968 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
969 : DECL_RTL (decl)) != pc_rtx)
970 continue;
972 large_size += alignb - 1;
973 large_size &= -(HOST_WIDE_INT)alignb;
974 large_size += stack_vars[i].size;
977 /* If there were any, allocate space. */
978 if (large_size > 0)
979 large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
980 large_align, true);
983 for (si = 0; si < n; ++si)
985 rtx base;
986 unsigned base_align, alignb;
987 HOST_WIDE_INT offset;
989 i = stack_vars_sorted[si];
991 /* Skip variables that aren't partition representatives, for now. */
992 if (stack_vars[i].representative != i)
993 continue;
995 /* Skip variables that have already had rtl assigned. See also
996 add_stack_var where we perpetrate this pc_rtx hack. */
997 decl = stack_vars[i].decl;
998 if ((TREE_CODE (decl) == SSA_NAME
999 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
1000 : DECL_RTL (decl)) != pc_rtx)
1001 continue;
1003 /* Check the predicate to see whether this variable should be
1004 allocated in this pass. */
1005 if (pred && !pred (i))
1006 continue;
1008 alignb = stack_vars[i].alignb;
1009 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1011 base = virtual_stack_vars_rtx;
1012 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && pred)
1014 HOST_WIDE_INT prev_offset
1015 = align_base (frame_offset,
1016 MAX (alignb, ASAN_RED_ZONE_SIZE),
1017 FRAME_GROWS_DOWNWARD);
1018 tree repr_decl = NULL_TREE;
1019 offset
1020 = alloc_stack_frame_space (stack_vars[i].size
1021 + ASAN_RED_ZONE_SIZE,
1022 MAX (alignb, ASAN_RED_ZONE_SIZE));
1024 data->asan_vec.safe_push (prev_offset);
1025 data->asan_vec.safe_push (offset + stack_vars[i].size);
1026 /* Find best representative of the partition.
1027 Prefer those with DECL_NAME, even better
1028 satisfying asan_protect_stack_decl predicate. */
1029 for (j = i; j != EOC; j = stack_vars[j].next)
1030 if (asan_protect_stack_decl (stack_vars[j].decl)
1031 && DECL_NAME (stack_vars[j].decl))
1033 repr_decl = stack_vars[j].decl;
1034 break;
1036 else if (repr_decl == NULL_TREE
1037 && DECL_P (stack_vars[j].decl)
1038 && DECL_NAME (stack_vars[j].decl))
1039 repr_decl = stack_vars[j].decl;
1040 if (repr_decl == NULL_TREE)
1041 repr_decl = stack_vars[i].decl;
1042 data->asan_decl_vec.safe_push (repr_decl);
1043 data->asan_alignb = MAX (data->asan_alignb, alignb);
1044 if (data->asan_base == NULL)
1045 data->asan_base = gen_reg_rtx (Pmode);
1046 base = data->asan_base;
1048 if (!STRICT_ALIGNMENT)
1049 base_align = crtl->max_used_stack_slot_alignment;
1050 else
1051 base_align = MAX (crtl->max_used_stack_slot_alignment,
1052 GET_MODE_ALIGNMENT (SImode)
1053 << ASAN_SHADOW_SHIFT);
1055 else
1057 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1058 base_align = crtl->max_used_stack_slot_alignment;
1061 else
1063 /* Large alignment is only processed in the last pass. */
1064 if (pred)
1065 continue;
1066 gcc_assert (large_base != NULL);
1068 large_alloc += alignb - 1;
1069 large_alloc &= -(HOST_WIDE_INT)alignb;
1070 offset = large_alloc;
1071 large_alloc += stack_vars[i].size;
1073 base = large_base;
1074 base_align = large_align;
1077 /* Create rtl for each variable based on their location within the
1078 partition. */
1079 for (j = i; j != EOC; j = stack_vars[j].next)
1081 expand_one_stack_var_at (stack_vars[j].decl,
1082 base, base_align,
1083 offset);
1087 gcc_assert (large_alloc == large_size);
1090 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1091 static HOST_WIDE_INT
1092 account_stack_vars (void)
1094 size_t si, j, i, n = stack_vars_num;
1095 HOST_WIDE_INT size = 0;
1097 for (si = 0; si < n; ++si)
1099 i = stack_vars_sorted[si];
1101 /* Skip variables that aren't partition representatives, for now. */
1102 if (stack_vars[i].representative != i)
1103 continue;
1105 size += stack_vars[i].size;
1106 for (j = i; j != EOC; j = stack_vars[j].next)
1107 set_rtl (stack_vars[j].decl, NULL);
1109 return size;
1112 /* A subroutine of expand_one_var. Called to immediately assign rtl
1113 to a variable to be allocated in the stack frame. */
1115 static void
1116 expand_one_stack_var (tree var)
1118 HOST_WIDE_INT size, offset;
1119 unsigned byte_align;
1121 size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (var)));
1122 byte_align = align_local_variable (SSAVAR (var));
1124 /* We handle highly aligned variables in expand_stack_vars. */
1125 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1127 offset = alloc_stack_frame_space (size, byte_align);
1129 expand_one_stack_var_at (var, virtual_stack_vars_rtx,
1130 crtl->max_used_stack_slot_alignment, offset);
1133 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1134 that will reside in a hard register. */
1136 static void
1137 expand_one_hard_reg_var (tree var)
1139 rest_of_decl_compilation (var, 0, 0);
1142 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1143 that will reside in a pseudo register. */
1145 static void
1146 expand_one_register_var (tree var)
1148 tree decl = SSAVAR (var);
1149 tree type = TREE_TYPE (decl);
1150 machine_mode reg_mode = promote_decl_mode (decl, NULL);
1151 rtx x = gen_reg_rtx (reg_mode);
1153 set_rtl (var, x);
1155 /* Note if the object is a user variable. */
1156 if (!DECL_ARTIFICIAL (decl))
1157 mark_user_reg (x);
1159 if (POINTER_TYPE_P (type))
1160 mark_reg_pointer (x, get_pointer_alignment (var));
1163 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1164 has some associated error, e.g. its type is error-mark. We just need
1165 to pick something that won't crash the rest of the compiler. */
1167 static void
1168 expand_one_error_var (tree var)
1170 machine_mode mode = DECL_MODE (var);
1171 rtx x;
1173 if (mode == BLKmode)
1174 x = gen_rtx_MEM (BLKmode, const0_rtx);
1175 else if (mode == VOIDmode)
1176 x = const0_rtx;
1177 else
1178 x = gen_reg_rtx (mode);
1180 SET_DECL_RTL (var, x);
1183 /* A subroutine of expand_one_var. VAR is a variable that will be
1184 allocated to the local stack frame. Return true if we wish to
1185 add VAR to STACK_VARS so that it will be coalesced with other
1186 variables. Return false to allocate VAR immediately.
1188 This function is used to reduce the number of variables considered
1189 for coalescing, which reduces the size of the quadratic problem. */
1191 static bool
1192 defer_stack_allocation (tree var, bool toplevel)
1194 /* Whether the variable is small enough for immediate allocation not to be
1195 a problem with regard to the frame size. */
1196 bool smallish
1197 = ((HOST_WIDE_INT) tree_to_uhwi (DECL_SIZE_UNIT (var))
1198 < PARAM_VALUE (PARAM_MIN_SIZE_FOR_STACK_SHARING));
1200 /* If stack protection is enabled, *all* stack variables must be deferred,
1201 so that we can re-order the strings to the top of the frame.
1202 Similarly for Address Sanitizer. */
1203 if (flag_stack_protect || ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK))
1204 return true;
1206 /* We handle "large" alignment via dynamic allocation. We want to handle
1207 this extra complication in only one place, so defer them. */
1208 if (DECL_ALIGN (var) > MAX_SUPPORTED_STACK_ALIGNMENT)
1209 return true;
1211 /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1212 might be detached from their block and appear at toplevel when we reach
1213 here. We want to coalesce them with variables from other blocks when
1214 the immediate contribution to the frame size would be noticeable. */
1215 if (toplevel && optimize > 0 && DECL_IGNORED_P (var) && !smallish)
1216 return true;
1218 /* Variables declared in the outermost scope automatically conflict
1219 with every other variable. The only reason to want to defer them
1220 at all is that, after sorting, we can more efficiently pack
1221 small variables in the stack frame. Continue to defer at -O2. */
1222 if (toplevel && optimize < 2)
1223 return false;
1225 /* Without optimization, *most* variables are allocated from the
1226 stack, which makes the quadratic problem large exactly when we
1227 want compilation to proceed as quickly as possible. On the
1228 other hand, we don't want the function's stack frame size to
1229 get completely out of hand. So we avoid adding scalars and
1230 "small" aggregates to the list at all. */
1231 if (optimize == 0 && smallish)
1232 return false;
1234 return true;
1237 /* A subroutine of expand_used_vars. Expand one variable according to
1238 its flavor. Variables to be placed on the stack are not actually
1239 expanded yet, merely recorded.
1240 When REALLY_EXPAND is false, only add stack values to be allocated.
1241 Return stack usage this variable is supposed to take.
1244 static HOST_WIDE_INT
1245 expand_one_var (tree var, bool toplevel, bool really_expand)
1247 unsigned int align = BITS_PER_UNIT;
1248 tree origvar = var;
1250 var = SSAVAR (var);
1252 if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
1254 /* Because we don't know if VAR will be in register or on stack,
1255 we conservatively assume it will be on stack even if VAR is
1256 eventually put into register after RA pass. For non-automatic
1257 variables, which won't be on stack, we collect alignment of
1258 type and ignore user specified alignment. Similarly for
1259 SSA_NAMEs for which use_register_for_decl returns true. */
1260 if (TREE_STATIC (var)
1261 || DECL_EXTERNAL (var)
1262 || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1263 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1264 TYPE_MODE (TREE_TYPE (var)),
1265 TYPE_ALIGN (TREE_TYPE (var)));
1266 else if (DECL_HAS_VALUE_EXPR_P (var)
1267 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1268 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1269 or variables which were assigned a stack slot already by
1270 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1271 changed from the offset chosen to it. */
1272 align = crtl->stack_alignment_estimated;
1273 else
1274 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1276 /* If the variable alignment is very large we'll dynamicaly allocate
1277 it, which means that in-frame portion is just a pointer. */
1278 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1279 align = POINTER_SIZE;
1282 if (SUPPORTS_STACK_ALIGNMENT
1283 && crtl->stack_alignment_estimated < align)
1285 /* stack_alignment_estimated shouldn't change after stack
1286 realign decision made */
1287 gcc_assert (!crtl->stack_realign_processed);
1288 crtl->stack_alignment_estimated = align;
1291 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1292 So here we only make sure stack_alignment_needed >= align. */
1293 if (crtl->stack_alignment_needed < align)
1294 crtl->stack_alignment_needed = align;
1295 if (crtl->max_used_stack_slot_alignment < align)
1296 crtl->max_used_stack_slot_alignment = align;
1298 if (TREE_CODE (origvar) == SSA_NAME)
1300 gcc_assert (TREE_CODE (var) != VAR_DECL
1301 || (!DECL_EXTERNAL (var)
1302 && !DECL_HAS_VALUE_EXPR_P (var)
1303 && !TREE_STATIC (var)
1304 && TREE_TYPE (var) != error_mark_node
1305 && !DECL_HARD_REGISTER (var)
1306 && really_expand));
1308 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1310 else if (DECL_EXTERNAL (var))
1312 else if (DECL_HAS_VALUE_EXPR_P (var))
1314 else if (TREE_STATIC (var))
1316 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1318 else if (TREE_TYPE (var) == error_mark_node)
1320 if (really_expand)
1321 expand_one_error_var (var);
1323 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1325 if (really_expand)
1327 expand_one_hard_reg_var (var);
1328 if (!DECL_HARD_REGISTER (var))
1329 /* Invalid register specification. */
1330 expand_one_error_var (var);
1333 else if (use_register_for_decl (var))
1335 if (really_expand)
1336 expand_one_register_var (origvar);
1338 else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
1340 /* Reject variables which cover more than half of the address-space. */
1341 if (really_expand)
1343 error ("size of variable %q+D is too large", var);
1344 expand_one_error_var (var);
1347 else if (defer_stack_allocation (var, toplevel))
1348 add_stack_var (origvar);
1349 else
1351 if (really_expand)
1352 expand_one_stack_var (origvar);
1353 return tree_to_uhwi (DECL_SIZE_UNIT (var));
1355 return 0;
1358 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1359 expanding variables. Those variables that can be put into registers
1360 are allocated pseudos; those that can't are put on the stack.
1362 TOPLEVEL is true if this is the outermost BLOCK. */
1364 static void
1365 expand_used_vars_for_block (tree block, bool toplevel)
1367 tree t;
1369 /* Expand all variables at this level. */
1370 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1371 if (TREE_USED (t)
1372 && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1373 || !DECL_NONSHAREABLE (t)))
1374 expand_one_var (t, toplevel, true);
1376 /* Expand all variables at containing levels. */
1377 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1378 expand_used_vars_for_block (t, false);
1381 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1382 and clear TREE_USED on all local variables. */
1384 static void
1385 clear_tree_used (tree block)
1387 tree t;
1389 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1390 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1391 if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1392 || !DECL_NONSHAREABLE (t))
1393 TREE_USED (t) = 0;
1395 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1396 clear_tree_used (t);
1399 enum {
1400 SPCT_FLAG_DEFAULT = 1,
1401 SPCT_FLAG_ALL = 2,
1402 SPCT_FLAG_STRONG = 3,
1403 SPCT_FLAG_EXPLICIT = 4
1406 /* Examine TYPE and determine a bit mask of the following features. */
1408 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1409 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1410 #define SPCT_HAS_ARRAY 4
1411 #define SPCT_HAS_AGGREGATE 8
1413 static unsigned int
1414 stack_protect_classify_type (tree type)
1416 unsigned int ret = 0;
1417 tree t;
1419 switch (TREE_CODE (type))
1421 case ARRAY_TYPE:
1422 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1423 if (t == char_type_node
1424 || t == signed_char_type_node
1425 || t == unsigned_char_type_node)
1427 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1428 unsigned HOST_WIDE_INT len;
1430 if (!TYPE_SIZE_UNIT (type)
1431 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1432 len = max;
1433 else
1434 len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1436 if (len < max)
1437 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1438 else
1439 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1441 else
1442 ret = SPCT_HAS_ARRAY;
1443 break;
1445 case UNION_TYPE:
1446 case QUAL_UNION_TYPE:
1447 case RECORD_TYPE:
1448 ret = SPCT_HAS_AGGREGATE;
1449 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1450 if (TREE_CODE (t) == FIELD_DECL)
1451 ret |= stack_protect_classify_type (TREE_TYPE (t));
1452 break;
1454 default:
1455 break;
1458 return ret;
1461 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1462 part of the local stack frame. Remember if we ever return nonzero for
1463 any variable in this function. The return value is the phase number in
1464 which the variable should be allocated. */
1466 static int
1467 stack_protect_decl_phase (tree decl)
1469 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1470 int ret = 0;
1472 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1473 has_short_buffer = true;
1475 if (flag_stack_protect == SPCT_FLAG_ALL
1476 || flag_stack_protect == SPCT_FLAG_STRONG
1477 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1478 && lookup_attribute ("stack_protect",
1479 DECL_ATTRIBUTES (current_function_decl))))
1481 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1482 && !(bits & SPCT_HAS_AGGREGATE))
1483 ret = 1;
1484 else if (bits & SPCT_HAS_ARRAY)
1485 ret = 2;
1487 else
1488 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1490 if (ret)
1491 has_protected_decls = true;
1493 return ret;
1496 /* Two helper routines that check for phase 1 and phase 2. These are used
1497 as callbacks for expand_stack_vars. */
1499 static bool
1500 stack_protect_decl_phase_1 (size_t i)
1502 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
1505 static bool
1506 stack_protect_decl_phase_2 (size_t i)
1508 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
1511 /* And helper function that checks for asan phase (with stack protector
1512 it is phase 3). This is used as callback for expand_stack_vars.
1513 Returns true if any of the vars in the partition need to be protected. */
1515 static bool
1516 asan_decl_phase_3 (size_t i)
1518 while (i != EOC)
1520 if (asan_protect_stack_decl (stack_vars[i].decl))
1521 return true;
1522 i = stack_vars[i].next;
1524 return false;
1527 /* Ensure that variables in different stack protection phases conflict
1528 so that they are not merged and share the same stack slot. */
1530 static void
1531 add_stack_protection_conflicts (void)
1533 size_t i, j, n = stack_vars_num;
1534 unsigned char *phase;
1536 phase = XNEWVEC (unsigned char, n);
1537 for (i = 0; i < n; ++i)
1538 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1540 for (i = 0; i < n; ++i)
1542 unsigned char ph_i = phase[i];
1543 for (j = i + 1; j < n; ++j)
1544 if (ph_i != phase[j])
1545 add_stack_var_conflict (i, j);
1548 XDELETEVEC (phase);
1551 /* Create a decl for the guard at the top of the stack frame. */
1553 static void
1554 create_stack_guard (void)
1556 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1557 VAR_DECL, NULL, ptr_type_node);
1558 TREE_THIS_VOLATILE (guard) = 1;
1559 TREE_USED (guard) = 1;
1560 expand_one_stack_var (guard);
1561 crtl->stack_protect_guard = guard;
1564 /* Prepare for expanding variables. */
1565 static void
1566 init_vars_expansion (void)
1568 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
1569 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
1571 /* A map from decl to stack partition. */
1572 decl_to_stack_part = new hash_map<tree, size_t>;
1574 /* Initialize local stack smashing state. */
1575 has_protected_decls = false;
1576 has_short_buffer = false;
1579 /* Free up stack variable graph data. */
1580 static void
1581 fini_vars_expansion (void)
1583 bitmap_obstack_release (&stack_var_bitmap_obstack);
1584 if (stack_vars)
1585 XDELETEVEC (stack_vars);
1586 if (stack_vars_sorted)
1587 XDELETEVEC (stack_vars_sorted);
1588 stack_vars = NULL;
1589 stack_vars_sorted = NULL;
1590 stack_vars_alloc = stack_vars_num = 0;
1591 delete decl_to_stack_part;
1592 decl_to_stack_part = NULL;
1595 /* Make a fair guess for the size of the stack frame of the function
1596 in NODE. This doesn't have to be exact, the result is only used in
1597 the inline heuristics. So we don't want to run the full stack var
1598 packing algorithm (which is quadratic in the number of stack vars).
1599 Instead, we calculate the total size of all stack vars. This turns
1600 out to be a pretty fair estimate -- packing of stack vars doesn't
1601 happen very often. */
1603 HOST_WIDE_INT
1604 estimated_stack_frame_size (struct cgraph_node *node)
1606 HOST_WIDE_INT size = 0;
1607 size_t i;
1608 tree var;
1609 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1611 push_cfun (fn);
1613 init_vars_expansion ();
1615 FOR_EACH_LOCAL_DECL (fn, i, var)
1616 if (auto_var_in_fn_p (var, fn->decl))
1617 size += expand_one_var (var, true, false);
1619 if (stack_vars_num > 0)
1621 /* Fake sorting the stack vars for account_stack_vars (). */
1622 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1623 for (i = 0; i < stack_vars_num; ++i)
1624 stack_vars_sorted[i] = i;
1625 size += account_stack_vars ();
1628 fini_vars_expansion ();
1629 pop_cfun ();
1630 return size;
1633 /* Helper routine to check if a record or union contains an array field. */
1635 static int
1636 record_or_union_type_has_array_p (const_tree tree_type)
1638 tree fields = TYPE_FIELDS (tree_type);
1639 tree f;
1641 for (f = fields; f; f = DECL_CHAIN (f))
1642 if (TREE_CODE (f) == FIELD_DECL)
1644 tree field_type = TREE_TYPE (f);
1645 if (RECORD_OR_UNION_TYPE_P (field_type)
1646 && record_or_union_type_has_array_p (field_type))
1647 return 1;
1648 if (TREE_CODE (field_type) == ARRAY_TYPE)
1649 return 1;
1651 return 0;
1654 /* Check if the current function has local referenced variables that
1655 have their addresses taken, contain an array, or are arrays. */
1657 static bool
1658 stack_protect_decl_p ()
1660 unsigned i;
1661 tree var;
1663 FOR_EACH_LOCAL_DECL (cfun, i, var)
1664 if (!is_global_var (var))
1666 tree var_type = TREE_TYPE (var);
1667 if (TREE_CODE (var) == VAR_DECL
1668 && (TREE_CODE (var_type) == ARRAY_TYPE
1669 || TREE_ADDRESSABLE (var)
1670 || (RECORD_OR_UNION_TYPE_P (var_type)
1671 && record_or_union_type_has_array_p (var_type))))
1672 return true;
1674 return false;
1677 /* Check if the current function has calls that use a return slot. */
1679 static bool
1680 stack_protect_return_slot_p ()
1682 basic_block bb;
1684 FOR_ALL_BB_FN (bb, cfun)
1685 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1686 !gsi_end_p (gsi); gsi_next (&gsi))
1688 gimple stmt = gsi_stmt (gsi);
1689 /* This assumes that calls to internal-only functions never
1690 use a return slot. */
1691 if (is_gimple_call (stmt)
1692 && !gimple_call_internal_p (stmt)
1693 && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
1694 gimple_call_fndecl (stmt)))
1695 return true;
1697 return false;
1700 /* Expand all variables used in the function. */
1702 static rtx_insn *
1703 expand_used_vars (void)
1705 tree var, outer_block = DECL_INITIAL (current_function_decl);
1706 vec<tree> maybe_local_decls = vNULL;
1707 rtx_insn *var_end_seq = NULL;
1708 unsigned i;
1709 unsigned len;
1710 bool gen_stack_protect_signal = false;
1712 /* Compute the phase of the stack frame for this function. */
1714 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1715 int off = STARTING_FRAME_OFFSET % align;
1716 frame_phase = off ? align - off : 0;
1719 /* Set TREE_USED on all variables in the local_decls. */
1720 FOR_EACH_LOCAL_DECL (cfun, i, var)
1721 TREE_USED (var) = 1;
1722 /* Clear TREE_USED on all variables associated with a block scope. */
1723 clear_tree_used (DECL_INITIAL (current_function_decl));
1725 init_vars_expansion ();
1727 if (targetm.use_pseudo_pic_reg ())
1728 pic_offset_table_rtx = gen_reg_rtx (Pmode);
1730 hash_map<tree, tree> ssa_name_decls;
1731 for (i = 0; i < SA.map->num_partitions; i++)
1733 tree var = partition_to_var (SA.map, i);
1735 gcc_assert (!virtual_operand_p (var));
1737 /* Assign decls to each SSA name partition, share decls for partitions
1738 we could have coalesced (those with the same type). */
1739 if (SSA_NAME_VAR (var) == NULL_TREE)
1741 tree *slot = &ssa_name_decls.get_or_insert (TREE_TYPE (var));
1742 if (!*slot)
1743 *slot = create_tmp_reg (TREE_TYPE (var));
1744 replace_ssa_name_symbol (var, *slot);
1747 /* Always allocate space for partitions based on VAR_DECLs. But for
1748 those based on PARM_DECLs or RESULT_DECLs and which matter for the
1749 debug info, there is no need to do so if optimization is disabled
1750 because all the SSA_NAMEs based on these DECLs have been coalesced
1751 into a single partition, which is thus assigned the canonical RTL
1752 location of the DECLs. If in_lto_p, we can't rely on optimize,
1753 a function could be compiled with -O1 -flto first and only the
1754 link performed at -O0. */
1755 if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1756 expand_one_var (var, true, true);
1757 else if (DECL_IGNORED_P (SSA_NAME_VAR (var)) || optimize || in_lto_p)
1759 /* This is a PARM_DECL or RESULT_DECL. For those partitions that
1760 contain the default def (representing the parm or result itself)
1761 we don't do anything here. But those which don't contain the
1762 default def (representing a temporary based on the parm/result)
1763 we need to allocate space just like for normal VAR_DECLs. */
1764 if (!bitmap_bit_p (SA.partition_has_default_def, i))
1766 expand_one_var (var, true, true);
1767 gcc_assert (SA.partition_to_pseudo[i]);
1772 if (flag_stack_protect == SPCT_FLAG_STRONG)
1773 gen_stack_protect_signal
1774 = stack_protect_decl_p () || stack_protect_return_slot_p ();
1776 /* At this point all variables on the local_decls with TREE_USED
1777 set are not associated with any block scope. Lay them out. */
1779 len = vec_safe_length (cfun->local_decls);
1780 FOR_EACH_LOCAL_DECL (cfun, i, var)
1782 bool expand_now = false;
1784 /* Expanded above already. */
1785 if (is_gimple_reg (var))
1787 TREE_USED (var) = 0;
1788 goto next;
1790 /* We didn't set a block for static or extern because it's hard
1791 to tell the difference between a global variable (re)declared
1792 in a local scope, and one that's really declared there to
1793 begin with. And it doesn't really matter much, since we're
1794 not giving them stack space. Expand them now. */
1795 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1796 expand_now = true;
1798 /* Expand variables not associated with any block now. Those created by
1799 the optimizers could be live anywhere in the function. Those that
1800 could possibly have been scoped originally and detached from their
1801 block will have their allocation deferred so we coalesce them with
1802 others when optimization is enabled. */
1803 else if (TREE_USED (var))
1804 expand_now = true;
1806 /* Finally, mark all variables on the list as used. We'll use
1807 this in a moment when we expand those associated with scopes. */
1808 TREE_USED (var) = 1;
1810 if (expand_now)
1811 expand_one_var (var, true, true);
1813 next:
1814 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1816 rtx rtl = DECL_RTL_IF_SET (var);
1818 /* Keep artificial non-ignored vars in cfun->local_decls
1819 chain until instantiate_decls. */
1820 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1821 add_local_decl (cfun, var);
1822 else if (rtl == NULL_RTX)
1823 /* If rtl isn't set yet, which can happen e.g. with
1824 -fstack-protector, retry before returning from this
1825 function. */
1826 maybe_local_decls.safe_push (var);
1830 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
1832 +-----------------+-----------------+
1833 | ...processed... | ...duplicates...|
1834 +-----------------+-----------------+
1836 +-- LEN points here.
1838 We just want the duplicates, as those are the artificial
1839 non-ignored vars that we want to keep until instantiate_decls.
1840 Move them down and truncate the array. */
1841 if (!vec_safe_is_empty (cfun->local_decls))
1842 cfun->local_decls->block_remove (0, len);
1844 /* At this point, all variables within the block tree with TREE_USED
1845 set are actually used by the optimized function. Lay them out. */
1846 expand_used_vars_for_block (outer_block, true);
1848 if (stack_vars_num > 0)
1850 add_scope_conflicts ();
1852 /* If stack protection is enabled, we don't share space between
1853 vulnerable data and non-vulnerable data. */
1854 if (flag_stack_protect != 0
1855 && (flag_stack_protect != SPCT_FLAG_EXPLICIT
1856 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1857 && lookup_attribute ("stack_protect",
1858 DECL_ATTRIBUTES (current_function_decl)))))
1859 add_stack_protection_conflicts ();
1861 /* Now that we have collected all stack variables, and have computed a
1862 minimal interference graph, attempt to save some stack space. */
1863 partition_stack_vars ();
1864 if (dump_file)
1865 dump_stack_var_partition ();
1868 switch (flag_stack_protect)
1870 case SPCT_FLAG_ALL:
1871 create_stack_guard ();
1872 break;
1874 case SPCT_FLAG_STRONG:
1875 if (gen_stack_protect_signal
1876 || cfun->calls_alloca || has_protected_decls
1877 || lookup_attribute ("stack_protect",
1878 DECL_ATTRIBUTES (current_function_decl)))
1879 create_stack_guard ();
1880 break;
1882 case SPCT_FLAG_DEFAULT:
1883 if (cfun->calls_alloca || has_protected_decls
1884 || lookup_attribute ("stack_protect",
1885 DECL_ATTRIBUTES (current_function_decl)))
1886 create_stack_guard ();
1887 break;
1889 case SPCT_FLAG_EXPLICIT:
1890 if (lookup_attribute ("stack_protect",
1891 DECL_ATTRIBUTES (current_function_decl)))
1892 create_stack_guard ();
1893 break;
1894 default:
1898 /* Assign rtl to each variable based on these partitions. */
1899 if (stack_vars_num > 0)
1901 struct stack_vars_data data;
1903 data.asan_vec = vNULL;
1904 data.asan_decl_vec = vNULL;
1905 data.asan_base = NULL_RTX;
1906 data.asan_alignb = 0;
1908 /* Reorder decls to be protected by iterating over the variables
1909 array multiple times, and allocating out of each phase in turn. */
1910 /* ??? We could probably integrate this into the qsort we did
1911 earlier, such that we naturally see these variables first,
1912 and thus naturally allocate things in the right order. */
1913 if (has_protected_decls)
1915 /* Phase 1 contains only character arrays. */
1916 expand_stack_vars (stack_protect_decl_phase_1, &data);
1918 /* Phase 2 contains other kinds of arrays. */
1919 if (flag_stack_protect == SPCT_FLAG_ALL
1920 || flag_stack_protect == SPCT_FLAG_STRONG
1921 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1922 && lookup_attribute ("stack_protect",
1923 DECL_ATTRIBUTES (current_function_decl))))
1924 expand_stack_vars (stack_protect_decl_phase_2, &data);
1927 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK)
1928 /* Phase 3, any partitions that need asan protection
1929 in addition to phase 1 and 2. */
1930 expand_stack_vars (asan_decl_phase_3, &data);
1932 if (!data.asan_vec.is_empty ())
1934 HOST_WIDE_INT prev_offset = frame_offset;
1935 HOST_WIDE_INT offset, sz, redzonesz;
1936 redzonesz = ASAN_RED_ZONE_SIZE;
1937 sz = data.asan_vec[0] - prev_offset;
1938 if (data.asan_alignb > ASAN_RED_ZONE_SIZE
1939 && data.asan_alignb <= 4096
1940 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
1941 redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
1942 & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
1943 offset
1944 = alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE);
1945 data.asan_vec.safe_push (prev_offset);
1946 data.asan_vec.safe_push (offset);
1947 /* Leave space for alignment if STRICT_ALIGNMENT. */
1948 if (STRICT_ALIGNMENT)
1949 alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
1950 << ASAN_SHADOW_SHIFT)
1951 / BITS_PER_UNIT, 1);
1953 var_end_seq
1954 = asan_emit_stack_protection (virtual_stack_vars_rtx,
1955 data.asan_base,
1956 data.asan_alignb,
1957 data.asan_vec.address (),
1958 data.asan_decl_vec.address (),
1959 data.asan_vec.length ());
1962 expand_stack_vars (NULL, &data);
1964 data.asan_vec.release ();
1965 data.asan_decl_vec.release ();
1968 fini_vars_expansion ();
1970 /* If there were any artificial non-ignored vars without rtl
1971 found earlier, see if deferred stack allocation hasn't assigned
1972 rtl to them. */
1973 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
1975 rtx rtl = DECL_RTL_IF_SET (var);
1977 /* Keep artificial non-ignored vars in cfun->local_decls
1978 chain until instantiate_decls. */
1979 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1980 add_local_decl (cfun, var);
1982 maybe_local_decls.release ();
1984 /* If the target requires that FRAME_OFFSET be aligned, do it. */
1985 if (STACK_ALIGNMENT_NEEDED)
1987 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1988 if (!FRAME_GROWS_DOWNWARD)
1989 frame_offset += align - 1;
1990 frame_offset &= -align;
1993 return var_end_seq;
1997 /* If we need to produce a detailed dump, print the tree representation
1998 for STMT to the dump file. SINCE is the last RTX after which the RTL
1999 generated for STMT should have been appended. */
2001 static void
2002 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx_insn *since)
2004 if (dump_file && (dump_flags & TDF_DETAILS))
2006 fprintf (dump_file, "\n;; ");
2007 print_gimple_stmt (dump_file, stmt, 0,
2008 TDF_SLIM | (dump_flags & TDF_LINENO));
2009 fprintf (dump_file, "\n");
2011 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
2015 /* Maps the blocks that do not contain tree labels to rtx labels. */
2017 static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
2019 /* Returns the label_rtx expression for a label starting basic block BB. */
2021 static rtx_code_label *
2022 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
2024 gimple_stmt_iterator gsi;
2025 tree lab;
2027 if (bb->flags & BB_RTL)
2028 return block_label (bb);
2030 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
2031 if (elt)
2032 return *elt;
2034 /* Find the tree label if it is present. */
2036 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2038 glabel *lab_stmt;
2040 lab_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
2041 if (!lab_stmt)
2042 break;
2044 lab = gimple_label_label (lab_stmt);
2045 if (DECL_NONLOCAL (lab))
2046 break;
2048 return jump_target_rtx (lab);
2051 rtx_code_label *l = gen_label_rtx ();
2052 lab_rtx_for_bb->put (bb, l);
2053 return l;
2057 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2058 of a basic block where we just expanded the conditional at the end,
2059 possibly clean up the CFG and instruction sequence. LAST is the
2060 last instruction before the just emitted jump sequence. */
2062 static void
2063 maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2065 /* Special case: when jumpif decides that the condition is
2066 trivial it emits an unconditional jump (and the necessary
2067 barrier). But we still have two edges, the fallthru one is
2068 wrong. purge_dead_edges would clean this up later. Unfortunately
2069 we have to insert insns (and split edges) before
2070 find_many_sub_basic_blocks and hence before purge_dead_edges.
2071 But splitting edges might create new blocks which depend on the
2072 fact that if there are two edges there's no barrier. So the
2073 barrier would get lost and verify_flow_info would ICE. Instead
2074 of auditing all edge splitters to care for the barrier (which
2075 normally isn't there in a cleaned CFG), fix it here. */
2076 if (BARRIER_P (get_last_insn ()))
2078 rtx_insn *insn;
2079 remove_edge (e);
2080 /* Now, we have a single successor block, if we have insns to
2081 insert on the remaining edge we potentially will insert
2082 it at the end of this block (if the dest block isn't feasible)
2083 in order to avoid splitting the edge. This insertion will take
2084 place in front of the last jump. But we might have emitted
2085 multiple jumps (conditional and one unconditional) to the
2086 same destination. Inserting in front of the last one then
2087 is a problem. See PR 40021. We fix this by deleting all
2088 jumps except the last unconditional one. */
2089 insn = PREV_INSN (get_last_insn ());
2090 /* Make sure we have an unconditional jump. Otherwise we're
2091 confused. */
2092 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2093 for (insn = PREV_INSN (insn); insn != last;)
2095 insn = PREV_INSN (insn);
2096 if (JUMP_P (NEXT_INSN (insn)))
2098 if (!any_condjump_p (NEXT_INSN (insn)))
2100 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2101 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2103 delete_insn (NEXT_INSN (insn));
2109 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2110 Returns a new basic block if we've terminated the current basic
2111 block and created a new one. */
2113 static basic_block
2114 expand_gimple_cond (basic_block bb, gcond *stmt)
2116 basic_block new_bb, dest;
2117 edge new_edge;
2118 edge true_edge;
2119 edge false_edge;
2120 rtx_insn *last2, *last;
2121 enum tree_code code;
2122 tree op0, op1;
2124 code = gimple_cond_code (stmt);
2125 op0 = gimple_cond_lhs (stmt);
2126 op1 = gimple_cond_rhs (stmt);
2127 /* We're sometimes presented with such code:
2128 D.123_1 = x < y;
2129 if (D.123_1 != 0)
2131 This would expand to two comparisons which then later might
2132 be cleaned up by combine. But some pattern matchers like if-conversion
2133 work better when there's only one compare, so make up for this
2134 here as special exception if TER would have made the same change. */
2135 if (SA.values
2136 && TREE_CODE (op0) == SSA_NAME
2137 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2138 && TREE_CODE (op1) == INTEGER_CST
2139 && ((gimple_cond_code (stmt) == NE_EXPR
2140 && integer_zerop (op1))
2141 || (gimple_cond_code (stmt) == EQ_EXPR
2142 && integer_onep (op1)))
2143 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2145 gimple second = SSA_NAME_DEF_STMT (op0);
2146 if (gimple_code (second) == GIMPLE_ASSIGN)
2148 enum tree_code code2 = gimple_assign_rhs_code (second);
2149 if (TREE_CODE_CLASS (code2) == tcc_comparison)
2151 code = code2;
2152 op0 = gimple_assign_rhs1 (second);
2153 op1 = gimple_assign_rhs2 (second);
2155 /* If jumps are cheap and the target does not support conditional
2156 compare, turn some more codes into jumpy sequences. */
2157 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4
2158 && targetm.gen_ccmp_first == NULL)
2160 if ((code2 == BIT_AND_EXPR
2161 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2162 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2163 || code2 == TRUTH_AND_EXPR)
2165 code = TRUTH_ANDIF_EXPR;
2166 op0 = gimple_assign_rhs1 (second);
2167 op1 = gimple_assign_rhs2 (second);
2169 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2171 code = TRUTH_ORIF_EXPR;
2172 op0 = gimple_assign_rhs1 (second);
2173 op1 = gimple_assign_rhs2 (second);
2179 last2 = last = get_last_insn ();
2181 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2182 set_curr_insn_location (gimple_location (stmt));
2184 /* These flags have no purpose in RTL land. */
2185 true_edge->flags &= ~EDGE_TRUE_VALUE;
2186 false_edge->flags &= ~EDGE_FALSE_VALUE;
2188 /* We can either have a pure conditional jump with one fallthru edge or
2189 two-way jump that needs to be decomposed into two basic blocks. */
2190 if (false_edge->dest == bb->next_bb)
2192 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2193 true_edge->probability);
2194 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2195 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2196 set_curr_insn_location (true_edge->goto_locus);
2197 false_edge->flags |= EDGE_FALLTHRU;
2198 maybe_cleanup_end_of_block (false_edge, last);
2199 return NULL;
2201 if (true_edge->dest == bb->next_bb)
2203 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2204 false_edge->probability);
2205 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2206 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2207 set_curr_insn_location (false_edge->goto_locus);
2208 true_edge->flags |= EDGE_FALLTHRU;
2209 maybe_cleanup_end_of_block (true_edge, last);
2210 return NULL;
2213 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2214 true_edge->probability);
2215 last = get_last_insn ();
2216 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2217 set_curr_insn_location (false_edge->goto_locus);
2218 emit_jump (label_rtx_for_bb (false_edge->dest));
2220 BB_END (bb) = last;
2221 if (BARRIER_P (BB_END (bb)))
2222 BB_END (bb) = PREV_INSN (BB_END (bb));
2223 update_bb_for_insn (bb);
2225 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2226 dest = false_edge->dest;
2227 redirect_edge_succ (false_edge, new_bb);
2228 false_edge->flags |= EDGE_FALLTHRU;
2229 new_bb->count = false_edge->count;
2230 new_bb->frequency = EDGE_FREQUENCY (false_edge);
2231 add_bb_to_loop (new_bb, bb->loop_father);
2232 new_edge = make_edge (new_bb, dest, 0);
2233 new_edge->probability = REG_BR_PROB_BASE;
2234 new_edge->count = new_bb->count;
2235 if (BARRIER_P (BB_END (new_bb)))
2236 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2237 update_bb_for_insn (new_bb);
2239 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2241 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2243 set_curr_insn_location (true_edge->goto_locus);
2244 true_edge->goto_locus = curr_insn_location ();
2247 return new_bb;
2250 /* Mark all calls that can have a transaction restart. */
2252 static void
2253 mark_transaction_restart_calls (gimple stmt)
2255 struct tm_restart_node dummy;
2256 tm_restart_node **slot;
2258 if (!cfun->gimple_df->tm_restart)
2259 return;
2261 dummy.stmt = stmt;
2262 slot = cfun->gimple_df->tm_restart->find_slot (&dummy, NO_INSERT);
2263 if (slot)
2265 struct tm_restart_node *n = *slot;
2266 tree list = n->label_or_list;
2267 rtx_insn *insn;
2269 for (insn = next_real_insn (get_last_insn ());
2270 !CALL_P (insn);
2271 insn = next_real_insn (insn))
2272 continue;
2274 if (TREE_CODE (list) == LABEL_DECL)
2275 add_reg_note (insn, REG_TM, label_rtx (list));
2276 else
2277 for (; list ; list = TREE_CHAIN (list))
2278 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2282 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2283 statement STMT. */
2285 static void
2286 expand_call_stmt (gcall *stmt)
2288 tree exp, decl, lhs;
2289 bool builtin_p;
2290 size_t i;
2292 if (gimple_call_internal_p (stmt))
2294 expand_internal_call (stmt);
2295 return;
2298 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2300 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2301 decl = gimple_call_fndecl (stmt);
2302 builtin_p = decl && DECL_BUILT_IN (decl);
2304 /* If this is not a builtin function, the function type through which the
2305 call is made may be different from the type of the function. */
2306 if (!builtin_p)
2307 CALL_EXPR_FN (exp)
2308 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2309 CALL_EXPR_FN (exp));
2311 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2312 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2314 for (i = 0; i < gimple_call_num_args (stmt); i++)
2316 tree arg = gimple_call_arg (stmt, i);
2317 gimple def;
2318 /* TER addresses into arguments of builtin functions so we have a
2319 chance to infer more correct alignment information. See PR39954. */
2320 if (builtin_p
2321 && TREE_CODE (arg) == SSA_NAME
2322 && (def = get_gimple_for_ssa_name (arg))
2323 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2324 arg = gimple_assign_rhs1 (def);
2325 CALL_EXPR_ARG (exp, i) = arg;
2328 if (gimple_has_side_effects (stmt))
2329 TREE_SIDE_EFFECTS (exp) = 1;
2331 if (gimple_call_nothrow_p (stmt))
2332 TREE_NOTHROW (exp) = 1;
2334 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2335 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2336 if (decl
2337 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2338 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2339 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2340 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2341 else
2342 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2343 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2344 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2345 CALL_WITH_BOUNDS_P (exp) = gimple_call_with_bounds_p (stmt);
2347 /* Ensure RTL is created for debug args. */
2348 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2350 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2351 unsigned int ix;
2352 tree dtemp;
2354 if (debug_args)
2355 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2357 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2358 expand_debug_expr (dtemp);
2362 lhs = gimple_call_lhs (stmt);
2363 if (lhs)
2364 expand_assignment (lhs, exp, false);
2365 else
2366 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2368 mark_transaction_restart_calls (stmt);
2372 /* Generate RTL for an asm statement (explicit assembler code).
2373 STRING is a STRING_CST node containing the assembler code text,
2374 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
2375 insn is volatile; don't optimize it. */
2377 static void
2378 expand_asm_loc (tree string, int vol, location_t locus)
2380 rtx body;
2382 if (TREE_CODE (string) == ADDR_EXPR)
2383 string = TREE_OPERAND (string, 0);
2385 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2386 ggc_strdup (TREE_STRING_POINTER (string)),
2387 locus);
2389 MEM_VOLATILE_P (body) = vol;
2391 emit_insn (body);
2394 /* Return the number of times character C occurs in string S. */
2395 static int
2396 n_occurrences (int c, const char *s)
2398 int n = 0;
2399 while (*s)
2400 n += (*s++ == c);
2401 return n;
2404 /* A subroutine of expand_asm_operands. Check that all operands have
2405 the same number of alternatives. Return true if so. */
2407 static bool
2408 check_operand_nalternatives (const vec<const char *> &constraints)
2410 unsigned len = constraints.length();
2411 if (len > 0)
2413 int nalternatives = n_occurrences (',', constraints[0]);
2415 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2417 error ("too many alternatives in %<asm%>");
2418 return false;
2421 for (unsigned i = 1; i < len; ++i)
2422 if (n_occurrences (',', constraints[i]) != nalternatives)
2424 error ("operand constraints for %<asm%> differ "
2425 "in number of alternatives");
2426 return false;
2429 return true;
2432 /* Check for overlap between registers marked in CLOBBERED_REGS and
2433 anything inappropriate in T. Emit error and return the register
2434 variable definition for error, NULL_TREE for ok. */
2436 static bool
2437 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
2439 /* Conflicts between asm-declared register variables and the clobber
2440 list are not allowed. */
2441 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2443 if (overlap)
2445 error ("asm-specifier for variable %qE conflicts with asm clobber list",
2446 DECL_NAME (overlap));
2448 /* Reset registerness to stop multiple errors emitted for a single
2449 variable. */
2450 DECL_REGISTER (overlap) = 0;
2451 return true;
2454 return false;
2457 /* Generate RTL for an asm statement with arguments.
2458 STRING is the instruction template.
2459 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
2460 Each output or input has an expression in the TREE_VALUE and
2461 a tree list in TREE_PURPOSE which in turn contains a constraint
2462 name in TREE_VALUE (or NULL_TREE) and a constraint string
2463 in TREE_PURPOSE.
2464 CLOBBERS is a list of STRING_CST nodes each naming a hard register
2465 that is clobbered by this insn.
2467 LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
2468 should be the fallthru basic block of the asm goto.
2470 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
2471 Some elements of OUTPUTS may be replaced with trees representing temporary
2472 values. The caller should copy those temporary values to the originally
2473 specified lvalues.
2475 VOL nonzero means the insn is volatile; don't optimize it. */
2477 static void
2478 expand_asm_stmt (gasm *stmt)
2480 class save_input_location
2482 location_t old;
2484 public:
2485 explicit save_input_location(location_t where)
2487 old = input_location;
2488 input_location = where;
2491 ~save_input_location()
2493 input_location = old;
2497 location_t locus = gimple_location (stmt);
2499 if (gimple_asm_input_p (stmt))
2501 const char *s = gimple_asm_string (stmt);
2502 tree string = build_string (strlen (s), s);
2503 expand_asm_loc (string, gimple_asm_volatile_p (stmt), locus);
2504 return;
2507 /* There are some legacy diagnostics in here, and also avoids a
2508 sixth parameger to targetm.md_asm_adjust. */
2509 save_input_location s_i_l(locus);
2511 unsigned noutputs = gimple_asm_noutputs (stmt);
2512 unsigned ninputs = gimple_asm_ninputs (stmt);
2513 unsigned nlabels = gimple_asm_nlabels (stmt);
2514 unsigned i;
2516 /* ??? Diagnose during gimplification? */
2517 if (ninputs + noutputs + nlabels > MAX_RECOG_OPERANDS)
2519 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
2520 return;
2523 auto_vec<tree, MAX_RECOG_OPERANDS> output_tvec;
2524 auto_vec<tree, MAX_RECOG_OPERANDS> input_tvec;
2525 auto_vec<const char *, MAX_RECOG_OPERANDS> constraints;
2527 /* Copy the gimple vectors into new vectors that we can manipulate. */
2529 output_tvec.safe_grow (noutputs);
2530 input_tvec.safe_grow (ninputs);
2531 constraints.safe_grow (noutputs + ninputs);
2533 for (i = 0; i < noutputs; ++i)
2535 tree t = gimple_asm_output_op (stmt, i);
2536 output_tvec[i] = TREE_VALUE (t);
2537 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2539 for (i = 0; i < ninputs; i++)
2541 tree t = gimple_asm_input_op (stmt, i);
2542 input_tvec[i] = TREE_VALUE (t);
2543 constraints[i + noutputs]
2544 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2547 /* ??? Diagnose during gimplification? */
2548 if (! check_operand_nalternatives (constraints))
2549 return;
2551 /* Count the number of meaningful clobbered registers, ignoring what
2552 we would ignore later. */
2553 auto_vec<rtx> clobber_rvec;
2554 HARD_REG_SET clobbered_regs;
2555 CLEAR_HARD_REG_SET (clobbered_regs);
2557 if (unsigned n = gimple_asm_nclobbers (stmt))
2559 clobber_rvec.reserve (n);
2560 for (i = 0; i < n; i++)
2562 tree t = gimple_asm_clobber_op (stmt, i);
2563 const char *regname = TREE_STRING_POINTER (TREE_VALUE (t));
2564 int nregs, j;
2566 j = decode_reg_name_and_count (regname, &nregs);
2567 if (j < 0)
2569 if (j == -2)
2571 /* ??? Diagnose during gimplification? */
2572 error ("unknown register name %qs in %<asm%>", regname);
2574 else if (j == -4)
2576 rtx x = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2577 clobber_rvec.safe_push (x);
2579 else
2581 /* Otherwise we should have -1 == empty string
2582 or -3 == cc, which is not a register. */
2583 gcc_assert (j == -1 || j == -3);
2586 else
2587 for (int reg = j; reg < j + nregs; reg++)
2589 /* Clobbering the PIC register is an error. */
2590 if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
2592 /* ??? Diagnose during gimplification? */
2593 error ("PIC register clobbered by %qs in %<asm%>",
2594 regname);
2595 return;
2598 SET_HARD_REG_BIT (clobbered_regs, reg);
2599 rtx x = gen_rtx_REG (reg_raw_mode[reg], reg);
2600 clobber_rvec.safe_push (x);
2604 unsigned nclobbers = clobber_rvec.length();
2606 /* First pass over inputs and outputs checks validity and sets
2607 mark_addressable if needed. */
2608 /* ??? Diagnose during gimplification? */
2610 for (i = 0; i < noutputs; ++i)
2612 tree val = output_tvec[i];
2613 tree type = TREE_TYPE (val);
2614 const char *constraint;
2615 bool is_inout;
2616 bool allows_reg;
2617 bool allows_mem;
2619 /* Try to parse the output constraint. If that fails, there's
2620 no point in going further. */
2621 constraint = constraints[i];
2622 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
2623 &allows_mem, &allows_reg, &is_inout))
2624 return;
2626 if (! allows_reg
2627 && (allows_mem
2628 || is_inout
2629 || (DECL_P (val)
2630 && REG_P (DECL_RTL (val))
2631 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
2632 mark_addressable (val);
2635 for (i = 0; i < ninputs; ++i)
2637 bool allows_reg, allows_mem;
2638 const char *constraint;
2640 constraint = constraints[i + noutputs];
2641 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2642 constraints.address (),
2643 &allows_mem, &allows_reg))
2644 return;
2646 if (! allows_reg && allows_mem)
2647 mark_addressable (input_tvec[i]);
2650 /* Second pass evaluates arguments. */
2652 /* Make sure stack is consistent for asm goto. */
2653 if (nlabels > 0)
2654 do_pending_stack_adjust ();
2655 int old_generating_concat_p = generating_concat_p;
2657 /* Vector of RTX's of evaluated output operands. */
2658 auto_vec<rtx, MAX_RECOG_OPERANDS> output_rvec;
2659 auto_vec<int, MAX_RECOG_OPERANDS> inout_opnum;
2660 rtx_insn *after_rtl_seq = NULL, *after_rtl_end = NULL;
2662 output_rvec.safe_grow (noutputs);
2664 for (i = 0; i < noutputs; ++i)
2666 tree val = output_tvec[i];
2667 tree type = TREE_TYPE (val);
2668 bool is_inout, allows_reg, allows_mem, ok;
2669 rtx op;
2671 ok = parse_output_constraint (&constraints[i], i, ninputs,
2672 noutputs, &allows_mem, &allows_reg,
2673 &is_inout);
2674 gcc_assert (ok);
2676 /* If an output operand is not a decl or indirect ref and our constraint
2677 allows a register, make a temporary to act as an intermediate.
2678 Make the asm insn write into that, then we will copy it to
2679 the real output operand. Likewise for promoted variables. */
2681 generating_concat_p = 0;
2683 if ((TREE_CODE (val) == INDIRECT_REF
2684 && allows_mem)
2685 || (DECL_P (val)
2686 && (allows_mem || REG_P (DECL_RTL (val)))
2687 && ! (REG_P (DECL_RTL (val))
2688 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
2689 || ! allows_reg
2690 || is_inout)
2692 op = expand_expr (val, NULL_RTX, VOIDmode,
2693 !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
2694 if (MEM_P (op))
2695 op = validize_mem (op);
2697 if (! allows_reg && !MEM_P (op))
2698 error ("output number %d not directly addressable", i);
2699 if ((! allows_mem && MEM_P (op))
2700 || GET_CODE (op) == CONCAT)
2702 rtx old_op = op;
2703 op = gen_reg_rtx (GET_MODE (op));
2705 generating_concat_p = old_generating_concat_p;
2707 if (is_inout)
2708 emit_move_insn (op, old_op);
2710 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2711 emit_move_insn (old_op, op);
2712 after_rtl_seq = get_insns ();
2713 after_rtl_end = get_last_insn ();
2714 end_sequence ();
2717 else
2719 op = assign_temp (type, 0, 1);
2720 op = validize_mem (op);
2721 if (!MEM_P (op) && TREE_CODE (val) == SSA_NAME)
2722 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (val), op);
2724 generating_concat_p = old_generating_concat_p;
2726 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2727 expand_assignment (val, make_tree (type, op), false);
2728 after_rtl_seq = get_insns ();
2729 after_rtl_end = get_last_insn ();
2730 end_sequence ();
2732 output_rvec[i] = op;
2734 if (is_inout)
2735 inout_opnum.safe_push (i);
2738 auto_vec<rtx, MAX_RECOG_OPERANDS> input_rvec;
2739 auto_vec<machine_mode, MAX_RECOG_OPERANDS> input_mode;
2741 input_rvec.safe_grow (ninputs);
2742 input_mode.safe_grow (ninputs);
2744 generating_concat_p = 0;
2746 for (i = 0; i < ninputs; ++i)
2748 tree val = input_tvec[i];
2749 tree type = TREE_TYPE (val);
2750 bool allows_reg, allows_mem, ok;
2751 const char *constraint;
2752 rtx op;
2754 constraint = constraints[i + noutputs];
2755 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2756 constraints.address (),
2757 &allows_mem, &allows_reg);
2758 gcc_assert (ok);
2760 /* EXPAND_INITIALIZER will not generate code for valid initializer
2761 constants, but will still generate code for other types of operand.
2762 This is the behavior we want for constant constraints. */
2763 op = expand_expr (val, NULL_RTX, VOIDmode,
2764 allows_reg ? EXPAND_NORMAL
2765 : allows_mem ? EXPAND_MEMORY
2766 : EXPAND_INITIALIZER);
2768 /* Never pass a CONCAT to an ASM. */
2769 if (GET_CODE (op) == CONCAT)
2770 op = force_reg (GET_MODE (op), op);
2771 else if (MEM_P (op))
2772 op = validize_mem (op);
2774 if (asm_operand_ok (op, constraint, NULL) <= 0)
2776 if (allows_reg && TYPE_MODE (type) != BLKmode)
2777 op = force_reg (TYPE_MODE (type), op);
2778 else if (!allows_mem)
2779 warning (0, "asm operand %d probably doesn%'t match constraints",
2780 i + noutputs);
2781 else if (MEM_P (op))
2783 /* We won't recognize either volatile memory or memory
2784 with a queued address as available a memory_operand
2785 at this point. Ignore it: clearly this *is* a memory. */
2787 else
2788 gcc_unreachable ();
2790 input_rvec[i] = op;
2791 input_mode[i] = TYPE_MODE (type);
2794 /* For in-out operands, copy output rtx to input rtx. */
2795 unsigned ninout = inout_opnum.length();
2796 for (i = 0; i < ninout; i++)
2798 int j = inout_opnum[i];
2799 rtx o = output_rvec[j];
2801 input_rvec.safe_push (o);
2802 input_mode.safe_push (GET_MODE (o));
2804 char buffer[16];
2805 sprintf (buffer, "%d", j);
2806 constraints.safe_push (ggc_strdup (buffer));
2808 ninputs += ninout;
2810 /* Sometimes we wish to automatically clobber registers across an asm.
2811 Case in point is when the i386 backend moved from cc0 to a hard reg --
2812 maintaining source-level compatibility means automatically clobbering
2813 the flags register. */
2814 rtx_insn *after_md_seq = NULL;
2815 if (targetm.md_asm_adjust)
2816 after_md_seq = targetm.md_asm_adjust (output_rvec, input_rvec,
2817 constraints, clobber_rvec,
2818 clobbered_regs);
2820 /* Do not allow the hook to change the output and input count,
2821 lest it mess up the operand numbering. */
2822 gcc_assert (output_rvec.length() == noutputs);
2823 gcc_assert (input_rvec.length() == ninputs);
2824 gcc_assert (constraints.length() == noutputs + ninputs);
2826 /* But it certainly can adjust the clobbers. */
2827 nclobbers = clobber_rvec.length();
2829 /* Third pass checks for easy conflicts. */
2830 /* ??? Why are we doing this on trees instead of rtx. */
2832 bool clobber_conflict_found = 0;
2833 for (i = 0; i < noutputs; ++i)
2834 if (tree_conflicts_with_clobbers_p (output_tvec[i], &clobbered_regs))
2835 clobber_conflict_found = 1;
2836 for (i = 0; i < ninputs - ninout; ++i)
2837 if (tree_conflicts_with_clobbers_p (input_tvec[i], &clobbered_regs))
2838 clobber_conflict_found = 1;
2840 /* Make vectors for the expression-rtx, constraint strings,
2841 and named operands. */
2843 rtvec argvec = rtvec_alloc (ninputs);
2844 rtvec constraintvec = rtvec_alloc (ninputs);
2845 rtvec labelvec = rtvec_alloc (nlabels);
2847 rtx body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
2848 : GET_MODE (output_rvec[0])),
2849 ggc_strdup (gimple_asm_string (stmt)),
2850 empty_string, 0, argvec, constraintvec,
2851 labelvec, locus);
2852 MEM_VOLATILE_P (body) = gimple_asm_volatile_p (stmt);
2854 for (i = 0; i < ninputs; ++i)
2856 ASM_OPERANDS_INPUT (body, i) = input_rvec[i];
2857 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
2858 = gen_rtx_ASM_INPUT_loc (input_mode[i],
2859 constraints[i + noutputs],
2860 locus);
2863 /* Copy labels to the vector. */
2864 rtx_code_label *fallthru_label = NULL;
2865 if (nlabels > 0)
2867 basic_block fallthru_bb = NULL;
2868 edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
2869 if (fallthru)
2870 fallthru_bb = fallthru->dest;
2872 for (i = 0; i < nlabels; ++i)
2874 tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
2875 rtx_insn *r;
2876 /* If asm goto has any labels in the fallthru basic block, use
2877 a label that we emit immediately after the asm goto. Expansion
2878 may insert further instructions into the same basic block after
2879 asm goto and if we don't do this, insertion of instructions on
2880 the fallthru edge might misbehave. See PR58670. */
2881 if (fallthru_bb && label_to_block_fn (cfun, label) == fallthru_bb)
2883 if (fallthru_label == NULL_RTX)
2884 fallthru_label = gen_label_rtx ();
2885 r = fallthru_label;
2887 else
2888 r = label_rtx (label);
2889 ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
2893 /* Now, for each output, construct an rtx
2894 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
2895 ARGVEC CONSTRAINTS OPNAMES))
2896 If there is more than one, put them inside a PARALLEL. */
2898 if (nlabels > 0 && nclobbers == 0)
2900 gcc_assert (noutputs == 0);
2901 emit_jump_insn (body);
2903 else if (noutputs == 0 && nclobbers == 0)
2905 /* No output operands: put in a raw ASM_OPERANDS rtx. */
2906 emit_insn (body);
2908 else if (noutputs == 1 && nclobbers == 0)
2910 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
2911 emit_insn (gen_rtx_SET (output_rvec[0], body));
2913 else
2915 rtx obody = body;
2916 int num = noutputs;
2918 if (num == 0)
2919 num = 1;
2921 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
2923 /* For each output operand, store a SET. */
2924 for (i = 0; i < noutputs; ++i)
2926 rtx src, o = output_rvec[i];
2927 if (i == 0)
2929 ASM_OPERANDS_OUTPUT_CONSTRAINT (obody) = constraints[0];
2930 src = obody;
2932 else
2934 src = gen_rtx_ASM_OPERANDS (GET_MODE (o),
2935 ASM_OPERANDS_TEMPLATE (obody),
2936 constraints[i], i, argvec,
2937 constraintvec, labelvec, locus);
2938 MEM_VOLATILE_P (src) = gimple_asm_volatile_p (stmt);
2940 XVECEXP (body, 0, i) = gen_rtx_SET (o, src);
2943 /* If there are no outputs (but there are some clobbers)
2944 store the bare ASM_OPERANDS into the PARALLEL. */
2945 if (i == 0)
2946 XVECEXP (body, 0, i++) = obody;
2948 /* Store (clobber REG) for each clobbered register specified. */
2949 for (unsigned j = 0; j < nclobbers; ++j)
2951 rtx clobbered_reg = clobber_rvec[j];
2953 /* Do sanity check for overlap between clobbers and respectively
2954 input and outputs that hasn't been handled. Such overlap
2955 should have been detected and reported above. */
2956 if (!clobber_conflict_found && REG_P (clobbered_reg))
2958 /* We test the old body (obody) contents to avoid
2959 tripping over the under-construction body. */
2960 for (unsigned k = 0; k < noutputs; ++k)
2961 if (reg_overlap_mentioned_p (clobbered_reg, output_rvec[k]))
2962 internal_error ("asm clobber conflict with output operand");
2964 for (unsigned k = 0; k < ninputs - ninout; ++k)
2965 if (reg_overlap_mentioned_p (clobbered_reg, input_rvec[k]))
2966 internal_error ("asm clobber conflict with input operand");
2969 XVECEXP (body, 0, i++) = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
2972 if (nlabels > 0)
2973 emit_jump_insn (body);
2974 else
2975 emit_insn (body);
2978 generating_concat_p = old_generating_concat_p;
2980 if (fallthru_label)
2981 emit_label (fallthru_label);
2983 if (after_md_seq)
2984 emit_insn (after_md_seq);
2985 if (after_rtl_seq)
2986 emit_insn (after_rtl_seq);
2988 free_temp_slots ();
2989 crtl->has_asm_statement = 1;
2992 /* Emit code to jump to the address
2993 specified by the pointer expression EXP. */
2995 static void
2996 expand_computed_goto (tree exp)
2998 rtx x = expand_normal (exp);
3000 do_pending_stack_adjust ();
3001 emit_indirect_jump (x);
3004 /* Generate RTL code for a `goto' statement with target label LABEL.
3005 LABEL should be a LABEL_DECL tree node that was or will later be
3006 defined with `expand_label'. */
3008 static void
3009 expand_goto (tree label)
3011 #ifdef ENABLE_CHECKING
3012 /* Check for a nonlocal goto to a containing function. Should have
3013 gotten translated to __builtin_nonlocal_goto. */
3014 tree context = decl_function_context (label);
3015 gcc_assert (!context || context == current_function_decl);
3016 #endif
3018 emit_jump (jump_target_rtx (label));
3021 /* Output a return with no value. */
3023 static void
3024 expand_null_return_1 (void)
3026 clear_pending_stack_adjust ();
3027 do_pending_stack_adjust ();
3028 emit_jump (return_label);
3031 /* Generate RTL to return from the current function, with no value.
3032 (That is, we do not do anything about returning any value.) */
3034 void
3035 expand_null_return (void)
3037 /* If this function was declared to return a value, but we
3038 didn't, clobber the return registers so that they are not
3039 propagated live to the rest of the function. */
3040 clobber_return_register ();
3042 expand_null_return_1 ();
3045 /* Generate RTL to return from the current function, with value VAL. */
3047 static void
3048 expand_value_return (rtx val)
3050 /* Copy the value to the return location unless it's already there. */
3052 tree decl = DECL_RESULT (current_function_decl);
3053 rtx return_reg = DECL_RTL (decl);
3054 if (return_reg != val)
3056 tree funtype = TREE_TYPE (current_function_decl);
3057 tree type = TREE_TYPE (decl);
3058 int unsignedp = TYPE_UNSIGNED (type);
3059 machine_mode old_mode = DECL_MODE (decl);
3060 machine_mode mode;
3061 if (DECL_BY_REFERENCE (decl))
3062 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3063 else
3064 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3066 if (mode != old_mode)
3067 val = convert_modes (mode, old_mode, val, unsignedp);
3069 if (GET_CODE (return_reg) == PARALLEL)
3070 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3071 else
3072 emit_move_insn (return_reg, val);
3075 expand_null_return_1 ();
3078 /* Generate RTL to evaluate the expression RETVAL and return it
3079 from the current function. */
3081 static void
3082 expand_return (tree retval, tree bounds)
3084 rtx result_rtl;
3085 rtx val = 0;
3086 tree retval_rhs;
3087 rtx bounds_rtl;
3089 /* If function wants no value, give it none. */
3090 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3092 expand_normal (retval);
3093 expand_null_return ();
3094 return;
3097 if (retval == error_mark_node)
3099 /* Treat this like a return of no value from a function that
3100 returns a value. */
3101 expand_null_return ();
3102 return;
3104 else if ((TREE_CODE (retval) == MODIFY_EXPR
3105 || TREE_CODE (retval) == INIT_EXPR)
3106 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3107 retval_rhs = TREE_OPERAND (retval, 1);
3108 else
3109 retval_rhs = retval;
3111 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3113 /* Put returned bounds to the right place. */
3114 bounds_rtl = DECL_BOUNDS_RTL (DECL_RESULT (current_function_decl));
3115 if (bounds_rtl)
3117 rtx addr = NULL;
3118 rtx bnd = NULL;
3120 if (bounds && bounds != error_mark_node)
3122 bnd = expand_normal (bounds);
3123 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3125 else if (REG_P (bounds_rtl))
3127 if (bounds)
3128 bnd = chkp_expand_zero_bounds ();
3129 else
3131 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3132 addr = gen_rtx_MEM (Pmode, addr);
3133 bnd = targetm.calls.load_bounds_for_arg (addr, NULL, NULL);
3136 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3138 else
3140 int n;
3142 gcc_assert (GET_CODE (bounds_rtl) == PARALLEL);
3144 if (bounds)
3145 bnd = chkp_expand_zero_bounds ();
3146 else
3148 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3149 addr = gen_rtx_MEM (Pmode, addr);
3152 for (n = 0; n < XVECLEN (bounds_rtl, 0); n++)
3154 rtx slot = XEXP (XVECEXP (bounds_rtl, 0, n), 0);
3155 if (!bounds)
3157 rtx offs = XEXP (XVECEXP (bounds_rtl, 0, n), 1);
3158 rtx from = adjust_address (addr, Pmode, INTVAL (offs));
3159 bnd = targetm.calls.load_bounds_for_arg (from, NULL, NULL);
3161 targetm.calls.store_returned_bounds (slot, bnd);
3165 else if (chkp_function_instrumented_p (current_function_decl)
3166 && !BOUNDED_P (retval_rhs)
3167 && chkp_type_has_pointer (TREE_TYPE (retval_rhs))
3168 && TREE_CODE (retval_rhs) != RESULT_DECL)
3170 rtx addr = expand_normal (build_fold_addr_expr (retval_rhs));
3171 addr = gen_rtx_MEM (Pmode, addr);
3173 gcc_assert (MEM_P (result_rtl));
3175 chkp_copy_bounds_for_stack_parm (result_rtl, addr, TREE_TYPE (retval_rhs));
3178 /* If we are returning the RESULT_DECL, then the value has already
3179 been stored into it, so we don't have to do anything special. */
3180 if (TREE_CODE (retval_rhs) == RESULT_DECL)
3181 expand_value_return (result_rtl);
3183 /* If the result is an aggregate that is being returned in one (or more)
3184 registers, load the registers here. */
3186 else if (retval_rhs != 0
3187 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3188 && REG_P (result_rtl))
3190 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3191 if (val)
3193 /* Use the mode of the result value on the return register. */
3194 PUT_MODE (result_rtl, GET_MODE (val));
3195 expand_value_return (val);
3197 else
3198 expand_null_return ();
3200 else if (retval_rhs != 0
3201 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3202 && (REG_P (result_rtl)
3203 || (GET_CODE (result_rtl) == PARALLEL)))
3205 /* Compute the return value into a temporary (usually a pseudo reg). */
3207 = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3208 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3209 val = force_not_mem (val);
3210 expand_value_return (val);
3212 else
3214 /* No hard reg used; calculate value into hard return reg. */
3215 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3216 expand_value_return (result_rtl);
3220 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3221 STMT that doesn't require special handling for outgoing edges. That
3222 is no tailcalls and no GIMPLE_COND. */
3224 static void
3225 expand_gimple_stmt_1 (gimple stmt)
3227 tree op0;
3229 set_curr_insn_location (gimple_location (stmt));
3231 switch (gimple_code (stmt))
3233 case GIMPLE_GOTO:
3234 op0 = gimple_goto_dest (stmt);
3235 if (TREE_CODE (op0) == LABEL_DECL)
3236 expand_goto (op0);
3237 else
3238 expand_computed_goto (op0);
3239 break;
3240 case GIMPLE_LABEL:
3241 expand_label (gimple_label_label (as_a <glabel *> (stmt)));
3242 break;
3243 case GIMPLE_NOP:
3244 case GIMPLE_PREDICT:
3245 break;
3246 case GIMPLE_SWITCH:
3247 expand_case (as_a <gswitch *> (stmt));
3248 break;
3249 case GIMPLE_ASM:
3250 expand_asm_stmt (as_a <gasm *> (stmt));
3251 break;
3252 case GIMPLE_CALL:
3253 expand_call_stmt (as_a <gcall *> (stmt));
3254 break;
3256 case GIMPLE_RETURN:
3258 tree bnd = gimple_return_retbnd (as_a <greturn *> (stmt));
3259 op0 = gimple_return_retval (as_a <greturn *> (stmt));
3261 if (op0 && op0 != error_mark_node)
3263 tree result = DECL_RESULT (current_function_decl);
3265 /* If we are not returning the current function's RESULT_DECL,
3266 build an assignment to it. */
3267 if (op0 != result)
3269 /* I believe that a function's RESULT_DECL is unique. */
3270 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3272 /* ??? We'd like to use simply expand_assignment here,
3273 but this fails if the value is of BLKmode but the return
3274 decl is a register. expand_return has special handling
3275 for this combination, which eventually should move
3276 to common code. See comments there. Until then, let's
3277 build a modify expression :-/ */
3278 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3279 result, op0);
3281 /* Mark we have return statement with missing bounds. */
3282 if (!bnd && chkp_function_instrumented_p (cfun->decl))
3283 bnd = error_mark_node;
3286 if (!op0)
3287 expand_null_return ();
3288 else
3289 expand_return (op0, bnd);
3291 break;
3293 case GIMPLE_ASSIGN:
3295 gassign *assign_stmt = as_a <gassign *> (stmt);
3296 tree lhs = gimple_assign_lhs (assign_stmt);
3298 /* Tree expand used to fiddle with |= and &= of two bitfield
3299 COMPONENT_REFs here. This can't happen with gimple, the LHS
3300 of binary assigns must be a gimple reg. */
3302 if (TREE_CODE (lhs) != SSA_NAME
3303 || get_gimple_rhs_class (gimple_expr_code (stmt))
3304 == GIMPLE_SINGLE_RHS)
3306 tree rhs = gimple_assign_rhs1 (assign_stmt);
3307 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
3308 == GIMPLE_SINGLE_RHS);
3309 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
3310 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3311 if (TREE_CLOBBER_P (rhs))
3312 /* This is a clobber to mark the going out of scope for
3313 this LHS. */
3315 else
3316 expand_assignment (lhs, rhs,
3317 gimple_assign_nontemporal_move_p (
3318 assign_stmt));
3320 else
3322 rtx target, temp;
3323 bool nontemporal = gimple_assign_nontemporal_move_p (assign_stmt);
3324 struct separate_ops ops;
3325 bool promoted = false;
3327 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3328 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
3329 promoted = true;
3331 ops.code = gimple_assign_rhs_code (assign_stmt);
3332 ops.type = TREE_TYPE (lhs);
3333 switch (get_gimple_rhs_class (ops.code))
3335 case GIMPLE_TERNARY_RHS:
3336 ops.op2 = gimple_assign_rhs3 (assign_stmt);
3337 /* Fallthru */
3338 case GIMPLE_BINARY_RHS:
3339 ops.op1 = gimple_assign_rhs2 (assign_stmt);
3340 /* Fallthru */
3341 case GIMPLE_UNARY_RHS:
3342 ops.op0 = gimple_assign_rhs1 (assign_stmt);
3343 break;
3344 default:
3345 gcc_unreachable ();
3347 ops.location = gimple_location (stmt);
3349 /* If we want to use a nontemporal store, force the value to
3350 register first. If we store into a promoted register,
3351 don't directly expand to target. */
3352 temp = nontemporal || promoted ? NULL_RTX : target;
3353 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
3354 EXPAND_NORMAL);
3356 if (temp == target)
3358 else if (promoted)
3360 int unsignedp = SUBREG_PROMOTED_SIGN (target);
3361 /* If TEMP is a VOIDmode constant, use convert_modes to make
3362 sure that we properly convert it. */
3363 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
3365 temp = convert_modes (GET_MODE (target),
3366 TYPE_MODE (ops.type),
3367 temp, unsignedp);
3368 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
3369 GET_MODE (target), temp, unsignedp);
3372 convert_move (SUBREG_REG (target), temp, unsignedp);
3374 else if (nontemporal && emit_storent_insn (target, temp))
3376 else
3378 temp = force_operand (temp, target);
3379 if (temp != target)
3380 emit_move_insn (target, temp);
3384 break;
3386 default:
3387 gcc_unreachable ();
3391 /* Expand one gimple statement STMT and return the last RTL instruction
3392 before any of the newly generated ones.
3394 In addition to generating the necessary RTL instructions this also
3395 sets REG_EH_REGION notes if necessary and sets the current source
3396 location for diagnostics. */
3398 static rtx_insn *
3399 expand_gimple_stmt (gimple stmt)
3401 location_t saved_location = input_location;
3402 rtx_insn *last = get_last_insn ();
3403 int lp_nr;
3405 gcc_assert (cfun);
3407 /* We need to save and restore the current source location so that errors
3408 discovered during expansion are emitted with the right location. But
3409 it would be better if the diagnostic routines used the source location
3410 embedded in the tree nodes rather than globals. */
3411 if (gimple_has_location (stmt))
3412 input_location = gimple_location (stmt);
3414 expand_gimple_stmt_1 (stmt);
3416 /* Free any temporaries used to evaluate this statement. */
3417 free_temp_slots ();
3419 input_location = saved_location;
3421 /* Mark all insns that may trap. */
3422 lp_nr = lookup_stmt_eh_lp (stmt);
3423 if (lp_nr)
3425 rtx_insn *insn;
3426 for (insn = next_real_insn (last); insn;
3427 insn = next_real_insn (insn))
3429 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
3430 /* If we want exceptions for non-call insns, any
3431 may_trap_p instruction may throw. */
3432 && GET_CODE (PATTERN (insn)) != CLOBBER
3433 && GET_CODE (PATTERN (insn)) != USE
3434 && insn_could_throw_p (insn))
3435 make_reg_eh_region_note (insn, 0, lp_nr);
3439 return last;
3442 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
3443 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
3444 generated a tail call (something that might be denied by the ABI
3445 rules governing the call; see calls.c).
3447 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
3448 can still reach the rest of BB. The case here is __builtin_sqrt,
3449 where the NaN result goes through the external function (with a
3450 tailcall) and the normal result happens via a sqrt instruction. */
3452 static basic_block
3453 expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
3455 rtx_insn *last2, *last;
3456 edge e;
3457 edge_iterator ei;
3458 int probability;
3459 gcov_type count;
3461 last2 = last = expand_gimple_stmt (stmt);
3463 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
3464 if (CALL_P (last) && SIBLING_CALL_P (last))
3465 goto found;
3467 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3469 *can_fallthru = true;
3470 return NULL;
3472 found:
3473 /* ??? Wouldn't it be better to just reset any pending stack adjust?
3474 Any instructions emitted here are about to be deleted. */
3475 do_pending_stack_adjust ();
3477 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
3478 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
3479 EH or abnormal edges, we shouldn't have created a tail call in
3480 the first place. So it seems to me we should just be removing
3481 all edges here, or redirecting the existing fallthru edge to
3482 the exit block. */
3484 probability = 0;
3485 count = 0;
3487 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3489 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
3491 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3493 e->dest->count -= e->count;
3494 e->dest->frequency -= EDGE_FREQUENCY (e);
3495 if (e->dest->count < 0)
3496 e->dest->count = 0;
3497 if (e->dest->frequency < 0)
3498 e->dest->frequency = 0;
3500 count += e->count;
3501 probability += e->probability;
3502 remove_edge (e);
3504 else
3505 ei_next (&ei);
3508 /* This is somewhat ugly: the call_expr expander often emits instructions
3509 after the sibcall (to perform the function return). These confuse the
3510 find_many_sub_basic_blocks code, so we need to get rid of these. */
3511 last = NEXT_INSN (last);
3512 gcc_assert (BARRIER_P (last));
3514 *can_fallthru = false;
3515 while (NEXT_INSN (last))
3517 /* For instance an sqrt builtin expander expands if with
3518 sibcall in the then and label for `else`. */
3519 if (LABEL_P (NEXT_INSN (last)))
3521 *can_fallthru = true;
3522 break;
3524 delete_insn (NEXT_INSN (last));
3527 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
3528 | EDGE_SIBCALL);
3529 e->probability += probability;
3530 e->count += count;
3531 BB_END (bb) = last;
3532 update_bb_for_insn (bb);
3534 if (NEXT_INSN (last))
3536 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
3538 last = BB_END (bb);
3539 if (BARRIER_P (last))
3540 BB_END (bb) = PREV_INSN (last);
3543 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3545 return bb;
3548 /* Return the difference between the floor and the truncated result of
3549 a signed division by OP1 with remainder MOD. */
3550 static rtx
3551 floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3553 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
3554 return gen_rtx_IF_THEN_ELSE
3555 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3556 gen_rtx_IF_THEN_ELSE
3557 (mode, gen_rtx_LT (BImode,
3558 gen_rtx_DIV (mode, op1, mod),
3559 const0_rtx),
3560 constm1_rtx, const0_rtx),
3561 const0_rtx);
3564 /* Return the difference between the ceil and the truncated result of
3565 a signed division by OP1 with remainder MOD. */
3566 static rtx
3567 ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3569 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
3570 return gen_rtx_IF_THEN_ELSE
3571 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3572 gen_rtx_IF_THEN_ELSE
3573 (mode, gen_rtx_GT (BImode,
3574 gen_rtx_DIV (mode, op1, mod),
3575 const0_rtx),
3576 const1_rtx, const0_rtx),
3577 const0_rtx);
3580 /* Return the difference between the ceil and the truncated result of
3581 an unsigned division by OP1 with remainder MOD. */
3582 static rtx
3583 ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
3585 /* (mod != 0 ? 1 : 0) */
3586 return gen_rtx_IF_THEN_ELSE
3587 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3588 const1_rtx, const0_rtx);
3591 /* Return the difference between the rounded and the truncated result
3592 of a signed division by OP1 with remainder MOD. Halfway cases are
3593 rounded away from zero, rather than to the nearest even number. */
3594 static rtx
3595 round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3597 /* (abs (mod) >= abs (op1) - abs (mod)
3598 ? (op1 / mod > 0 ? 1 : -1)
3599 : 0) */
3600 return gen_rtx_IF_THEN_ELSE
3601 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
3602 gen_rtx_MINUS (mode,
3603 gen_rtx_ABS (mode, op1),
3604 gen_rtx_ABS (mode, mod))),
3605 gen_rtx_IF_THEN_ELSE
3606 (mode, gen_rtx_GT (BImode,
3607 gen_rtx_DIV (mode, op1, mod),
3608 const0_rtx),
3609 const1_rtx, constm1_rtx),
3610 const0_rtx);
3613 /* Return the difference between the rounded and the truncated result
3614 of a unsigned division by OP1 with remainder MOD. Halfway cases
3615 are rounded away from zero, rather than to the nearest even
3616 number. */
3617 static rtx
3618 round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
3620 /* (mod >= op1 - mod ? 1 : 0) */
3621 return gen_rtx_IF_THEN_ELSE
3622 (mode, gen_rtx_GE (BImode, mod,
3623 gen_rtx_MINUS (mode, op1, mod)),
3624 const1_rtx, const0_rtx);
3627 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
3628 any rtl. */
3630 static rtx
3631 convert_debug_memory_address (machine_mode mode, rtx x,
3632 addr_space_t as)
3634 machine_mode xmode = GET_MODE (x);
3636 #ifndef POINTERS_EXTEND_UNSIGNED
3637 gcc_assert (mode == Pmode
3638 || mode == targetm.addr_space.address_mode (as));
3639 gcc_assert (xmode == mode || xmode == VOIDmode);
3640 #else
3641 rtx temp;
3643 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
3645 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
3646 return x;
3648 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
3649 x = simplify_gen_subreg (mode, x, xmode,
3650 subreg_lowpart_offset
3651 (mode, xmode));
3652 else if (POINTERS_EXTEND_UNSIGNED > 0)
3653 x = gen_rtx_ZERO_EXTEND (mode, x);
3654 else if (!POINTERS_EXTEND_UNSIGNED)
3655 x = gen_rtx_SIGN_EXTEND (mode, x);
3656 else
3658 switch (GET_CODE (x))
3660 case SUBREG:
3661 if ((SUBREG_PROMOTED_VAR_P (x)
3662 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
3663 || (GET_CODE (SUBREG_REG (x)) == PLUS
3664 && REG_P (XEXP (SUBREG_REG (x), 0))
3665 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
3666 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
3667 && GET_MODE (SUBREG_REG (x)) == mode)
3668 return SUBREG_REG (x);
3669 break;
3670 case LABEL_REF:
3671 temp = gen_rtx_LABEL_REF (mode, LABEL_REF_LABEL (x));
3672 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
3673 return temp;
3674 case SYMBOL_REF:
3675 temp = shallow_copy_rtx (x);
3676 PUT_MODE (temp, mode);
3677 return temp;
3678 case CONST:
3679 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3680 if (temp)
3681 temp = gen_rtx_CONST (mode, temp);
3682 return temp;
3683 case PLUS:
3684 case MINUS:
3685 if (CONST_INT_P (XEXP (x, 1)))
3687 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3688 if (temp)
3689 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
3691 break;
3692 default:
3693 break;
3695 /* Don't know how to express ptr_extend as operation in debug info. */
3696 return NULL;
3698 #endif /* POINTERS_EXTEND_UNSIGNED */
3700 return x;
3703 /* Map from SSA_NAMEs to corresponding DEBUG_EXPR_DECLs created
3704 by avoid_deep_ter_for_debug. */
3706 static hash_map<tree, tree> *deep_ter_debug_map;
3708 /* Split too deep TER chains for debug stmts using debug temporaries. */
3710 static void
3711 avoid_deep_ter_for_debug (gimple stmt, int depth)
3713 use_operand_p use_p;
3714 ssa_op_iter iter;
3715 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
3717 tree use = USE_FROM_PTR (use_p);
3718 if (TREE_CODE (use) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (use))
3719 continue;
3720 gimple g = get_gimple_for_ssa_name (use);
3721 if (g == NULL)
3722 continue;
3723 if (depth > 6 && !stmt_ends_bb_p (g))
3725 if (deep_ter_debug_map == NULL)
3726 deep_ter_debug_map = new hash_map<tree, tree>;
3728 tree &vexpr = deep_ter_debug_map->get_or_insert (use);
3729 if (vexpr != NULL)
3730 continue;
3731 vexpr = make_node (DEBUG_EXPR_DECL);
3732 gimple def_temp = gimple_build_debug_bind (vexpr, use, g);
3733 DECL_ARTIFICIAL (vexpr) = 1;
3734 TREE_TYPE (vexpr) = TREE_TYPE (use);
3735 DECL_MODE (vexpr) = TYPE_MODE (TREE_TYPE (use));
3736 gimple_stmt_iterator gsi = gsi_for_stmt (g);
3737 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
3738 avoid_deep_ter_for_debug (def_temp, 0);
3740 else
3741 avoid_deep_ter_for_debug (g, depth + 1);
3745 /* Return an RTX equivalent to the value of the parameter DECL. */
3747 static rtx
3748 expand_debug_parm_decl (tree decl)
3750 rtx incoming = DECL_INCOMING_RTL (decl);
3752 if (incoming
3753 && GET_MODE (incoming) != BLKmode
3754 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
3755 || (MEM_P (incoming)
3756 && REG_P (XEXP (incoming, 0))
3757 && HARD_REGISTER_P (XEXP (incoming, 0)))))
3759 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
3761 #ifdef HAVE_window_save
3762 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
3763 If the target machine has an explicit window save instruction, the
3764 actual entry value is the corresponding OUTGOING_REGNO instead. */
3765 if (REG_P (incoming)
3766 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
3767 incoming
3768 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
3769 OUTGOING_REGNO (REGNO (incoming)), 0);
3770 else if (MEM_P (incoming))
3772 rtx reg = XEXP (incoming, 0);
3773 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
3775 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
3776 incoming = replace_equiv_address_nv (incoming, reg);
3778 else
3779 incoming = copy_rtx (incoming);
3781 #endif
3783 ENTRY_VALUE_EXP (rtl) = incoming;
3784 return rtl;
3787 if (incoming
3788 && GET_MODE (incoming) != BLKmode
3789 && !TREE_ADDRESSABLE (decl)
3790 && MEM_P (incoming)
3791 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
3792 || (GET_CODE (XEXP (incoming, 0)) == PLUS
3793 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
3794 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
3795 return copy_rtx (incoming);
3797 return NULL_RTX;
3800 /* Return an RTX equivalent to the value of the tree expression EXP. */
3802 static rtx
3803 expand_debug_expr (tree exp)
3805 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
3806 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3807 machine_mode inner_mode = VOIDmode;
3808 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
3809 addr_space_t as;
3811 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
3813 case tcc_expression:
3814 switch (TREE_CODE (exp))
3816 case COND_EXPR:
3817 case DOT_PROD_EXPR:
3818 case SAD_EXPR:
3819 case WIDEN_MULT_PLUS_EXPR:
3820 case WIDEN_MULT_MINUS_EXPR:
3821 case FMA_EXPR:
3822 goto ternary;
3824 case TRUTH_ANDIF_EXPR:
3825 case TRUTH_ORIF_EXPR:
3826 case TRUTH_AND_EXPR:
3827 case TRUTH_OR_EXPR:
3828 case TRUTH_XOR_EXPR:
3829 goto binary;
3831 case TRUTH_NOT_EXPR:
3832 goto unary;
3834 default:
3835 break;
3837 break;
3839 ternary:
3840 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
3841 if (!op2)
3842 return NULL_RTX;
3843 /* Fall through. */
3845 binary:
3846 case tcc_binary:
3847 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
3848 if (!op1)
3849 return NULL_RTX;
3850 switch (TREE_CODE (exp))
3852 case LSHIFT_EXPR:
3853 case RSHIFT_EXPR:
3854 case LROTATE_EXPR:
3855 case RROTATE_EXPR:
3856 case WIDEN_LSHIFT_EXPR:
3857 /* Ensure second operand isn't wider than the first one. */
3858 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 1)));
3859 if (SCALAR_INT_MODE_P (inner_mode))
3861 machine_mode opmode = mode;
3862 if (VECTOR_MODE_P (mode))
3863 opmode = GET_MODE_INNER (mode);
3864 if (SCALAR_INT_MODE_P (opmode)
3865 && (GET_MODE_PRECISION (opmode)
3866 < GET_MODE_PRECISION (inner_mode)))
3867 op1 = simplify_gen_subreg (opmode, op1, inner_mode,
3868 subreg_lowpart_offset (opmode,
3869 inner_mode));
3871 break;
3872 default:
3873 break;
3875 /* Fall through. */
3877 unary:
3878 case tcc_unary:
3879 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3880 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3881 if (!op0)
3882 return NULL_RTX;
3883 break;
3885 case tcc_comparison:
3886 unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
3887 goto binary;
3889 case tcc_type:
3890 case tcc_statement:
3891 gcc_unreachable ();
3893 case tcc_constant:
3894 case tcc_exceptional:
3895 case tcc_declaration:
3896 case tcc_reference:
3897 case tcc_vl_exp:
3898 break;
3901 switch (TREE_CODE (exp))
3903 case STRING_CST:
3904 if (!lookup_constant_def (exp))
3906 if (strlen (TREE_STRING_POINTER (exp)) + 1
3907 != (size_t) TREE_STRING_LENGTH (exp))
3908 return NULL_RTX;
3909 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
3910 op0 = gen_rtx_MEM (BLKmode, op0);
3911 set_mem_attributes (op0, exp, 0);
3912 return op0;
3914 /* Fall through... */
3916 case INTEGER_CST:
3917 case REAL_CST:
3918 case FIXED_CST:
3919 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
3920 return op0;
3922 case COMPLEX_CST:
3923 gcc_assert (COMPLEX_MODE_P (mode));
3924 op0 = expand_debug_expr (TREE_REALPART (exp));
3925 op1 = expand_debug_expr (TREE_IMAGPART (exp));
3926 return gen_rtx_CONCAT (mode, op0, op1);
3928 case DEBUG_EXPR_DECL:
3929 op0 = DECL_RTL_IF_SET (exp);
3931 if (op0)
3932 return op0;
3934 op0 = gen_rtx_DEBUG_EXPR (mode);
3935 DEBUG_EXPR_TREE_DECL (op0) = exp;
3936 SET_DECL_RTL (exp, op0);
3938 return op0;
3940 case VAR_DECL:
3941 case PARM_DECL:
3942 case FUNCTION_DECL:
3943 case LABEL_DECL:
3944 case CONST_DECL:
3945 case RESULT_DECL:
3946 op0 = DECL_RTL_IF_SET (exp);
3948 /* This decl was probably optimized away. */
3949 if (!op0)
3951 if (TREE_CODE (exp) != VAR_DECL
3952 || DECL_EXTERNAL (exp)
3953 || !TREE_STATIC (exp)
3954 || !DECL_NAME (exp)
3955 || DECL_HARD_REGISTER (exp)
3956 || DECL_IN_CONSTANT_POOL (exp)
3957 || mode == VOIDmode)
3958 return NULL;
3960 op0 = make_decl_rtl_for_debug (exp);
3961 if (!MEM_P (op0)
3962 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
3963 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
3964 return NULL;
3966 else
3967 op0 = copy_rtx (op0);
3969 if (GET_MODE (op0) == BLKmode
3970 /* If op0 is not BLKmode, but mode is, adjust_mode
3971 below would ICE. While it is likely a FE bug,
3972 try to be robust here. See PR43166. */
3973 || mode == BLKmode
3974 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
3976 gcc_assert (MEM_P (op0));
3977 op0 = adjust_address_nv (op0, mode, 0);
3978 return op0;
3981 /* Fall through. */
3983 adjust_mode:
3984 case PAREN_EXPR:
3985 CASE_CONVERT:
3987 inner_mode = GET_MODE (op0);
3989 if (mode == inner_mode)
3990 return op0;
3992 if (inner_mode == VOIDmode)
3994 if (TREE_CODE (exp) == SSA_NAME)
3995 inner_mode = TYPE_MODE (TREE_TYPE (exp));
3996 else
3997 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3998 if (mode == inner_mode)
3999 return op0;
4002 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4004 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4005 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4006 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4007 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4008 else
4009 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4011 else if (FLOAT_MODE_P (mode))
4013 gcc_assert (TREE_CODE (exp) != SSA_NAME);
4014 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4015 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
4016 else
4017 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
4019 else if (FLOAT_MODE_P (inner_mode))
4021 if (unsignedp)
4022 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4023 else
4024 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4026 else if (CONSTANT_P (op0)
4027 || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
4028 op0 = simplify_gen_subreg (mode, op0, inner_mode,
4029 subreg_lowpart_offset (mode,
4030 inner_mode));
4031 else if (UNARY_CLASS_P (exp)
4032 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
4033 : unsignedp)
4034 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4035 else
4036 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4038 return op0;
4041 case MEM_REF:
4042 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4044 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
4045 TREE_OPERAND (exp, 0),
4046 TREE_OPERAND (exp, 1));
4047 if (newexp)
4048 return expand_debug_expr (newexp);
4050 /* FALLTHROUGH */
4051 case INDIRECT_REF:
4052 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4053 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4054 if (!op0)
4055 return NULL;
4057 if (TREE_CODE (exp) == MEM_REF)
4059 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4060 || (GET_CODE (op0) == PLUS
4061 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
4062 /* (mem (debug_implicit_ptr)) might confuse aliasing.
4063 Instead just use get_inner_reference. */
4064 goto component_ref;
4066 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4067 if (!op1 || !CONST_INT_P (op1))
4068 return NULL;
4070 op0 = plus_constant (inner_mode, op0, INTVAL (op1));
4073 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4075 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4076 op0, as);
4077 if (op0 == NULL_RTX)
4078 return NULL;
4080 op0 = gen_rtx_MEM (mode, op0);
4081 set_mem_attributes (op0, exp, 0);
4082 if (TREE_CODE (exp) == MEM_REF
4083 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4084 set_mem_expr (op0, NULL_TREE);
4085 set_mem_addr_space (op0, as);
4087 return op0;
4089 case TARGET_MEM_REF:
4090 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
4091 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
4092 return NULL;
4094 op0 = expand_debug_expr
4095 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
4096 if (!op0)
4097 return NULL;
4099 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4100 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4101 op0, as);
4102 if (op0 == NULL_RTX)
4103 return NULL;
4105 op0 = gen_rtx_MEM (mode, op0);
4107 set_mem_attributes (op0, exp, 0);
4108 set_mem_addr_space (op0, as);
4110 return op0;
4112 component_ref:
4113 case ARRAY_REF:
4114 case ARRAY_RANGE_REF:
4115 case COMPONENT_REF:
4116 case BIT_FIELD_REF:
4117 case REALPART_EXPR:
4118 case IMAGPART_EXPR:
4119 case VIEW_CONVERT_EXPR:
4121 machine_mode mode1;
4122 HOST_WIDE_INT bitsize, bitpos;
4123 tree offset;
4124 int volatilep = 0;
4125 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
4126 &mode1, &unsignedp, &volatilep, false);
4127 rtx orig_op0;
4129 if (bitsize == 0)
4130 return NULL;
4132 orig_op0 = op0 = expand_debug_expr (tem);
4134 if (!op0)
4135 return NULL;
4137 if (offset)
4139 machine_mode addrmode, offmode;
4141 if (!MEM_P (op0))
4142 return NULL;
4144 op0 = XEXP (op0, 0);
4145 addrmode = GET_MODE (op0);
4146 if (addrmode == VOIDmode)
4147 addrmode = Pmode;
4149 op1 = expand_debug_expr (offset);
4150 if (!op1)
4151 return NULL;
4153 offmode = GET_MODE (op1);
4154 if (offmode == VOIDmode)
4155 offmode = TYPE_MODE (TREE_TYPE (offset));
4157 if (addrmode != offmode)
4158 op1 = simplify_gen_subreg (addrmode, op1, offmode,
4159 subreg_lowpart_offset (addrmode,
4160 offmode));
4162 /* Don't use offset_address here, we don't need a
4163 recognizable address, and we don't want to generate
4164 code. */
4165 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4166 op0, op1));
4169 if (MEM_P (op0))
4171 if (mode1 == VOIDmode)
4172 /* Bitfield. */
4173 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
4174 if (bitpos >= BITS_PER_UNIT)
4176 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
4177 bitpos %= BITS_PER_UNIT;
4179 else if (bitpos < 0)
4181 HOST_WIDE_INT units
4182 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4183 op0 = adjust_address_nv (op0, mode1, units);
4184 bitpos += units * BITS_PER_UNIT;
4186 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
4187 op0 = adjust_address_nv (op0, mode, 0);
4188 else if (GET_MODE (op0) != mode1)
4189 op0 = adjust_address_nv (op0, mode1, 0);
4190 else
4191 op0 = copy_rtx (op0);
4192 if (op0 == orig_op0)
4193 op0 = shallow_copy_rtx (op0);
4194 set_mem_attributes (op0, exp, 0);
4197 if (bitpos == 0 && mode == GET_MODE (op0))
4198 return op0;
4200 if (bitpos < 0)
4201 return NULL;
4203 if (GET_MODE (op0) == BLKmode)
4204 return NULL;
4206 if ((bitpos % BITS_PER_UNIT) == 0
4207 && bitsize == GET_MODE_BITSIZE (mode1))
4209 machine_mode opmode = GET_MODE (op0);
4211 if (opmode == VOIDmode)
4212 opmode = TYPE_MODE (TREE_TYPE (tem));
4214 /* This condition may hold if we're expanding the address
4215 right past the end of an array that turned out not to
4216 be addressable (i.e., the address was only computed in
4217 debug stmts). The gen_subreg below would rightfully
4218 crash, and the address doesn't really exist, so just
4219 drop it. */
4220 if (bitpos >= GET_MODE_BITSIZE (opmode))
4221 return NULL;
4223 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
4224 return simplify_gen_subreg (mode, op0, opmode,
4225 bitpos / BITS_PER_UNIT);
4228 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4229 && TYPE_UNSIGNED (TREE_TYPE (exp))
4230 ? SIGN_EXTRACT
4231 : ZERO_EXTRACT, mode,
4232 GET_MODE (op0) != VOIDmode
4233 ? GET_MODE (op0)
4234 : TYPE_MODE (TREE_TYPE (tem)),
4235 op0, GEN_INT (bitsize), GEN_INT (bitpos));
4238 case ABS_EXPR:
4239 return simplify_gen_unary (ABS, mode, op0, mode);
4241 case NEGATE_EXPR:
4242 return simplify_gen_unary (NEG, mode, op0, mode);
4244 case BIT_NOT_EXPR:
4245 return simplify_gen_unary (NOT, mode, op0, mode);
4247 case FLOAT_EXPR:
4248 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4249 0)))
4250 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4251 inner_mode);
4253 case FIX_TRUNC_EXPR:
4254 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4255 inner_mode);
4257 case POINTER_PLUS_EXPR:
4258 /* For the rare target where pointers are not the same size as
4259 size_t, we need to check for mis-matched modes and correct
4260 the addend. */
4261 if (op0 && op1
4262 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
4263 && GET_MODE (op0) != GET_MODE (op1))
4265 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1))
4266 /* If OP0 is a partial mode, then we must truncate, even if it has
4267 the same bitsize as OP1 as GCC's representation of partial modes
4268 is opaque. */
4269 || (GET_MODE_CLASS (GET_MODE (op0)) == MODE_PARTIAL_INT
4270 && GET_MODE_BITSIZE (GET_MODE (op0)) == GET_MODE_BITSIZE (GET_MODE (op1))))
4271 op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
4272 GET_MODE (op1));
4273 else
4274 /* We always sign-extend, regardless of the signedness of
4275 the operand, because the operand is always unsigned
4276 here even if the original C expression is signed. */
4277 op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
4278 GET_MODE (op1));
4280 /* Fall through. */
4281 case PLUS_EXPR:
4282 return simplify_gen_binary (PLUS, mode, op0, op1);
4284 case MINUS_EXPR:
4285 return simplify_gen_binary (MINUS, mode, op0, op1);
4287 case MULT_EXPR:
4288 return simplify_gen_binary (MULT, mode, op0, op1);
4290 case RDIV_EXPR:
4291 case TRUNC_DIV_EXPR:
4292 case EXACT_DIV_EXPR:
4293 if (unsignedp)
4294 return simplify_gen_binary (UDIV, mode, op0, op1);
4295 else
4296 return simplify_gen_binary (DIV, mode, op0, op1);
4298 case TRUNC_MOD_EXPR:
4299 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4301 case FLOOR_DIV_EXPR:
4302 if (unsignedp)
4303 return simplify_gen_binary (UDIV, mode, op0, op1);
4304 else
4306 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4307 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4308 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4309 return simplify_gen_binary (PLUS, mode, div, adj);
4312 case FLOOR_MOD_EXPR:
4313 if (unsignedp)
4314 return simplify_gen_binary (UMOD, mode, op0, op1);
4315 else
4317 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4318 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4319 adj = simplify_gen_unary (NEG, mode,
4320 simplify_gen_binary (MULT, mode, adj, op1),
4321 mode);
4322 return simplify_gen_binary (PLUS, mode, mod, adj);
4325 case CEIL_DIV_EXPR:
4326 if (unsignedp)
4328 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4329 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4330 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4331 return simplify_gen_binary (PLUS, mode, div, adj);
4333 else
4335 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4336 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4337 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4338 return simplify_gen_binary (PLUS, mode, div, adj);
4341 case CEIL_MOD_EXPR:
4342 if (unsignedp)
4344 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4345 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4346 adj = simplify_gen_unary (NEG, mode,
4347 simplify_gen_binary (MULT, mode, adj, op1),
4348 mode);
4349 return simplify_gen_binary (PLUS, mode, mod, adj);
4351 else
4353 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4354 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4355 adj = simplify_gen_unary (NEG, mode,
4356 simplify_gen_binary (MULT, mode, adj, op1),
4357 mode);
4358 return simplify_gen_binary (PLUS, mode, mod, adj);
4361 case ROUND_DIV_EXPR:
4362 if (unsignedp)
4364 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4365 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4366 rtx adj = round_udiv_adjust (mode, mod, op1);
4367 return simplify_gen_binary (PLUS, mode, div, adj);
4369 else
4371 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4372 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4373 rtx adj = round_sdiv_adjust (mode, mod, op1);
4374 return simplify_gen_binary (PLUS, mode, div, adj);
4377 case ROUND_MOD_EXPR:
4378 if (unsignedp)
4380 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4381 rtx adj = round_udiv_adjust (mode, mod, op1);
4382 adj = simplify_gen_unary (NEG, mode,
4383 simplify_gen_binary (MULT, mode, adj, op1),
4384 mode);
4385 return simplify_gen_binary (PLUS, mode, mod, adj);
4387 else
4389 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4390 rtx adj = round_sdiv_adjust (mode, mod, op1);
4391 adj = simplify_gen_unary (NEG, mode,
4392 simplify_gen_binary (MULT, mode, adj, op1),
4393 mode);
4394 return simplify_gen_binary (PLUS, mode, mod, adj);
4397 case LSHIFT_EXPR:
4398 return simplify_gen_binary (ASHIFT, mode, op0, op1);
4400 case RSHIFT_EXPR:
4401 if (unsignedp)
4402 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
4403 else
4404 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
4406 case LROTATE_EXPR:
4407 return simplify_gen_binary (ROTATE, mode, op0, op1);
4409 case RROTATE_EXPR:
4410 return simplify_gen_binary (ROTATERT, mode, op0, op1);
4412 case MIN_EXPR:
4413 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
4415 case MAX_EXPR:
4416 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
4418 case BIT_AND_EXPR:
4419 case TRUTH_AND_EXPR:
4420 return simplify_gen_binary (AND, mode, op0, op1);
4422 case BIT_IOR_EXPR:
4423 case TRUTH_OR_EXPR:
4424 return simplify_gen_binary (IOR, mode, op0, op1);
4426 case BIT_XOR_EXPR:
4427 case TRUTH_XOR_EXPR:
4428 return simplify_gen_binary (XOR, mode, op0, op1);
4430 case TRUTH_ANDIF_EXPR:
4431 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
4433 case TRUTH_ORIF_EXPR:
4434 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
4436 case TRUTH_NOT_EXPR:
4437 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
4439 case LT_EXPR:
4440 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
4441 op0, op1);
4443 case LE_EXPR:
4444 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
4445 op0, op1);
4447 case GT_EXPR:
4448 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
4449 op0, op1);
4451 case GE_EXPR:
4452 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
4453 op0, op1);
4455 case EQ_EXPR:
4456 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
4458 case NE_EXPR:
4459 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
4461 case UNORDERED_EXPR:
4462 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
4464 case ORDERED_EXPR:
4465 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
4467 case UNLT_EXPR:
4468 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
4470 case UNLE_EXPR:
4471 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
4473 case UNGT_EXPR:
4474 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
4476 case UNGE_EXPR:
4477 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
4479 case UNEQ_EXPR:
4480 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
4482 case LTGT_EXPR:
4483 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
4485 case COND_EXPR:
4486 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
4488 case COMPLEX_EXPR:
4489 gcc_assert (COMPLEX_MODE_P (mode));
4490 if (GET_MODE (op0) == VOIDmode)
4491 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
4492 if (GET_MODE (op1) == VOIDmode)
4493 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
4494 return gen_rtx_CONCAT (mode, op0, op1);
4496 case CONJ_EXPR:
4497 if (GET_CODE (op0) == CONCAT)
4498 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
4499 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
4500 XEXP (op0, 1),
4501 GET_MODE_INNER (mode)));
4502 else
4504 machine_mode imode = GET_MODE_INNER (mode);
4505 rtx re, im;
4507 if (MEM_P (op0))
4509 re = adjust_address_nv (op0, imode, 0);
4510 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
4512 else
4514 machine_mode ifmode = int_mode_for_mode (mode);
4515 machine_mode ihmode = int_mode_for_mode (imode);
4516 rtx halfsize;
4517 if (ifmode == BLKmode || ihmode == BLKmode)
4518 return NULL;
4519 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
4520 re = op0;
4521 if (mode != ifmode)
4522 re = gen_rtx_SUBREG (ifmode, re, 0);
4523 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
4524 if (imode != ihmode)
4525 re = gen_rtx_SUBREG (imode, re, 0);
4526 im = copy_rtx (op0);
4527 if (mode != ifmode)
4528 im = gen_rtx_SUBREG (ifmode, im, 0);
4529 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
4530 if (imode != ihmode)
4531 im = gen_rtx_SUBREG (imode, im, 0);
4533 im = gen_rtx_NEG (imode, im);
4534 return gen_rtx_CONCAT (mode, re, im);
4537 case ADDR_EXPR:
4538 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4539 if (!op0 || !MEM_P (op0))
4541 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
4542 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
4543 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
4544 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
4545 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
4546 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
4548 if (handled_component_p (TREE_OPERAND (exp, 0)))
4550 HOST_WIDE_INT bitoffset, bitsize, maxsize;
4551 tree decl
4552 = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
4553 &bitoffset, &bitsize, &maxsize);
4554 if ((TREE_CODE (decl) == VAR_DECL
4555 || TREE_CODE (decl) == PARM_DECL
4556 || TREE_CODE (decl) == RESULT_DECL)
4557 && (!TREE_ADDRESSABLE (decl)
4558 || target_for_debug_bind (decl))
4559 && (bitoffset % BITS_PER_UNIT) == 0
4560 && bitsize > 0
4561 && bitsize == maxsize)
4563 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
4564 return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
4568 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
4569 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4570 == ADDR_EXPR)
4572 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4573 0));
4574 if (op0 != NULL
4575 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4576 || (GET_CODE (op0) == PLUS
4577 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
4578 && CONST_INT_P (XEXP (op0, 1)))))
4580 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4581 1));
4582 if (!op1 || !CONST_INT_P (op1))
4583 return NULL;
4585 return plus_constant (mode, op0, INTVAL (op1));
4589 return NULL;
4592 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
4593 op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
4595 return op0;
4597 case VECTOR_CST:
4599 unsigned i;
4601 op0 = gen_rtx_CONCATN
4602 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4604 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
4606 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
4607 if (!op1)
4608 return NULL;
4609 XVECEXP (op0, 0, i) = op1;
4612 return op0;
4615 case CONSTRUCTOR:
4616 if (TREE_CLOBBER_P (exp))
4617 return NULL;
4618 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
4620 unsigned i;
4621 tree val;
4623 op0 = gen_rtx_CONCATN
4624 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4626 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
4628 op1 = expand_debug_expr (val);
4629 if (!op1)
4630 return NULL;
4631 XVECEXP (op0, 0, i) = op1;
4634 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
4636 op1 = expand_debug_expr
4637 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
4639 if (!op1)
4640 return NULL;
4642 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
4643 XVECEXP (op0, 0, i) = op1;
4646 return op0;
4648 else
4649 goto flag_unsupported;
4651 case CALL_EXPR:
4652 /* ??? Maybe handle some builtins? */
4653 return NULL;
4655 case SSA_NAME:
4657 gimple g = get_gimple_for_ssa_name (exp);
4658 if (g)
4660 tree t = NULL_TREE;
4661 if (deep_ter_debug_map)
4663 tree *slot = deep_ter_debug_map->get (exp);
4664 if (slot)
4665 t = *slot;
4667 if (t == NULL_TREE)
4668 t = gimple_assign_rhs_to_tree (g);
4669 op0 = expand_debug_expr (t);
4670 if (!op0)
4671 return NULL;
4673 else
4675 int part = var_to_partition (SA.map, exp);
4677 if (part == NO_PARTITION)
4679 /* If this is a reference to an incoming value of parameter
4680 that is never used in the code or where the incoming
4681 value is never used in the code, use PARM_DECL's
4682 DECL_RTL if set. */
4683 if (SSA_NAME_IS_DEFAULT_DEF (exp)
4684 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL)
4686 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
4687 if (op0)
4688 goto adjust_mode;
4689 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
4690 if (op0)
4691 goto adjust_mode;
4693 return NULL;
4696 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
4698 op0 = copy_rtx (SA.partition_to_pseudo[part]);
4700 goto adjust_mode;
4703 case ERROR_MARK:
4704 return NULL;
4706 /* Vector stuff. For most of the codes we don't have rtl codes. */
4707 case REALIGN_LOAD_EXPR:
4708 case REDUC_MAX_EXPR:
4709 case REDUC_MIN_EXPR:
4710 case REDUC_PLUS_EXPR:
4711 case VEC_COND_EXPR:
4712 case VEC_PACK_FIX_TRUNC_EXPR:
4713 case VEC_PACK_SAT_EXPR:
4714 case VEC_PACK_TRUNC_EXPR:
4715 case VEC_UNPACK_FLOAT_HI_EXPR:
4716 case VEC_UNPACK_FLOAT_LO_EXPR:
4717 case VEC_UNPACK_HI_EXPR:
4718 case VEC_UNPACK_LO_EXPR:
4719 case VEC_WIDEN_MULT_HI_EXPR:
4720 case VEC_WIDEN_MULT_LO_EXPR:
4721 case VEC_WIDEN_MULT_EVEN_EXPR:
4722 case VEC_WIDEN_MULT_ODD_EXPR:
4723 case VEC_WIDEN_LSHIFT_HI_EXPR:
4724 case VEC_WIDEN_LSHIFT_LO_EXPR:
4725 case VEC_PERM_EXPR:
4726 return NULL;
4728 /* Misc codes. */
4729 case ADDR_SPACE_CONVERT_EXPR:
4730 case FIXED_CONVERT_EXPR:
4731 case OBJ_TYPE_REF:
4732 case WITH_SIZE_EXPR:
4733 return NULL;
4735 case DOT_PROD_EXPR:
4736 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4737 && SCALAR_INT_MODE_P (mode))
4740 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4741 0)))
4742 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4743 inner_mode);
4745 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4746 1)))
4747 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
4748 inner_mode);
4749 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4750 return simplify_gen_binary (PLUS, mode, op0, op2);
4752 return NULL;
4754 case WIDEN_MULT_EXPR:
4755 case WIDEN_MULT_PLUS_EXPR:
4756 case WIDEN_MULT_MINUS_EXPR:
4757 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4758 && SCALAR_INT_MODE_P (mode))
4760 inner_mode = GET_MODE (op0);
4761 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4762 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4763 else
4764 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4765 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
4766 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
4767 else
4768 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
4769 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4770 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
4771 return op0;
4772 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
4773 return simplify_gen_binary (PLUS, mode, op0, op2);
4774 else
4775 return simplify_gen_binary (MINUS, mode, op2, op0);
4777 return NULL;
4779 case MULT_HIGHPART_EXPR:
4780 /* ??? Similar to the above. */
4781 return NULL;
4783 case WIDEN_SUM_EXPR:
4784 case WIDEN_LSHIFT_EXPR:
4785 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4786 && SCALAR_INT_MODE_P (mode))
4789 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4790 0)))
4791 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4792 inner_mode);
4793 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
4794 ? ASHIFT : PLUS, mode, op0, op1);
4796 return NULL;
4798 case FMA_EXPR:
4799 return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
4801 default:
4802 flag_unsupported:
4803 #ifdef ENABLE_CHECKING
4804 debug_tree (exp);
4805 gcc_unreachable ();
4806 #else
4807 return NULL;
4808 #endif
4812 /* Return an RTX equivalent to the source bind value of the tree expression
4813 EXP. */
4815 static rtx
4816 expand_debug_source_expr (tree exp)
4818 rtx op0 = NULL_RTX;
4819 machine_mode mode = VOIDmode, inner_mode;
4821 switch (TREE_CODE (exp))
4823 case PARM_DECL:
4825 mode = DECL_MODE (exp);
4826 op0 = expand_debug_parm_decl (exp);
4827 if (op0)
4828 break;
4829 /* See if this isn't an argument that has been completely
4830 optimized out. */
4831 if (!DECL_RTL_SET_P (exp)
4832 && !DECL_INCOMING_RTL (exp)
4833 && DECL_ABSTRACT_ORIGIN (current_function_decl))
4835 tree aexp = DECL_ORIGIN (exp);
4836 if (DECL_CONTEXT (aexp)
4837 == DECL_ABSTRACT_ORIGIN (current_function_decl))
4839 vec<tree, va_gc> **debug_args;
4840 unsigned int ix;
4841 tree ddecl;
4842 debug_args = decl_debug_args_lookup (current_function_decl);
4843 if (debug_args != NULL)
4845 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
4846 ix += 2)
4847 if (ddecl == aexp)
4848 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
4852 break;
4854 default:
4855 break;
4858 if (op0 == NULL_RTX)
4859 return NULL_RTX;
4861 inner_mode = GET_MODE (op0);
4862 if (mode == inner_mode)
4863 return op0;
4865 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4867 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4868 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4869 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4870 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4871 else
4872 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4874 else if (FLOAT_MODE_P (mode))
4875 gcc_unreachable ();
4876 else if (FLOAT_MODE_P (inner_mode))
4878 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4879 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4880 else
4881 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4883 else if (CONSTANT_P (op0)
4884 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
4885 op0 = simplify_gen_subreg (mode, op0, inner_mode,
4886 subreg_lowpart_offset (mode, inner_mode));
4887 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4888 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4889 else
4890 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4892 return op0;
4895 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
4896 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
4897 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
4899 static void
4900 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
4902 rtx exp = *exp_p;
4904 if (exp == NULL_RTX)
4905 return;
4907 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
4908 return;
4910 if (depth == 4)
4912 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
4913 rtx dval = make_debug_expr_from_rtl (exp);
4915 /* Emit a debug bind insn before INSN. */
4916 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
4917 DEBUG_EXPR_TREE_DECL (dval), exp,
4918 VAR_INIT_STATUS_INITIALIZED);
4920 emit_debug_insn_before (bind, insn);
4921 *exp_p = dval;
4922 return;
4925 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
4926 int i, j;
4927 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4928 switch (*format_ptr++)
4930 case 'e':
4931 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
4932 break;
4934 case 'E':
4935 case 'V':
4936 for (j = 0; j < XVECLEN (exp, i); j++)
4937 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
4938 break;
4940 default:
4941 break;
4945 /* Expand the _LOCs in debug insns. We run this after expanding all
4946 regular insns, so that any variables referenced in the function
4947 will have their DECL_RTLs set. */
4949 static void
4950 expand_debug_locations (void)
4952 rtx_insn *insn;
4953 rtx_insn *last = get_last_insn ();
4954 int save_strict_alias = flag_strict_aliasing;
4956 /* New alias sets while setting up memory attributes cause
4957 -fcompare-debug failures, even though it doesn't bring about any
4958 codegen changes. */
4959 flag_strict_aliasing = 0;
4961 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4962 if (DEBUG_INSN_P (insn))
4964 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
4965 rtx val;
4966 rtx_insn *prev_insn, *insn2;
4967 machine_mode mode;
4969 if (value == NULL_TREE)
4970 val = NULL_RTX;
4971 else
4973 if (INSN_VAR_LOCATION_STATUS (insn)
4974 == VAR_INIT_STATUS_UNINITIALIZED)
4975 val = expand_debug_source_expr (value);
4976 /* The avoid_deep_ter_for_debug function inserts
4977 debug bind stmts after SSA_NAME definition, with the
4978 SSA_NAME as the whole bind location. Disable temporarily
4979 expansion of that SSA_NAME into the DEBUG_EXPR_DECL
4980 being defined in this DEBUG_INSN. */
4981 else if (deep_ter_debug_map && TREE_CODE (value) == SSA_NAME)
4983 tree *slot = deep_ter_debug_map->get (value);
4984 if (slot)
4986 if (*slot == INSN_VAR_LOCATION_DECL (insn))
4987 *slot = NULL_TREE;
4988 else
4989 slot = NULL;
4991 val = expand_debug_expr (value);
4992 if (slot)
4993 *slot = INSN_VAR_LOCATION_DECL (insn);
4995 else
4996 val = expand_debug_expr (value);
4997 gcc_assert (last == get_last_insn ());
5000 if (!val)
5001 val = gen_rtx_UNKNOWN_VAR_LOC ();
5002 else
5004 mode = GET_MODE (INSN_VAR_LOCATION (insn));
5006 gcc_assert (mode == GET_MODE (val)
5007 || (GET_MODE (val) == VOIDmode
5008 && (CONST_SCALAR_INT_P (val)
5009 || GET_CODE (val) == CONST_FIXED
5010 || GET_CODE (val) == LABEL_REF)));
5013 INSN_VAR_LOCATION_LOC (insn) = val;
5014 prev_insn = PREV_INSN (insn);
5015 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
5016 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
5019 flag_strict_aliasing = save_strict_alias;
5022 /* Performs swapping operands of commutative operations to expand
5023 the expensive one first. */
5025 static void
5026 reorder_operands (basic_block bb)
5028 unsigned int *lattice; /* Hold cost of each statement. */
5029 unsigned int i = 0, n = 0;
5030 gimple_stmt_iterator gsi;
5031 gimple_seq stmts;
5032 gimple stmt;
5033 bool swap;
5034 tree op0, op1;
5035 ssa_op_iter iter;
5036 use_operand_p use_p;
5037 gimple def0, def1;
5039 /* Compute cost of each statement using estimate_num_insns. */
5040 stmts = bb_seq (bb);
5041 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5043 stmt = gsi_stmt (gsi);
5044 if (!is_gimple_debug (stmt))
5045 gimple_set_uid (stmt, n++);
5047 lattice = XNEWVEC (unsigned int, n);
5048 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5050 unsigned cost;
5051 stmt = gsi_stmt (gsi);
5052 if (is_gimple_debug (stmt))
5053 continue;
5054 cost = estimate_num_insns (stmt, &eni_size_weights);
5055 lattice[i] = cost;
5056 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
5058 tree use = USE_FROM_PTR (use_p);
5059 gimple def_stmt;
5060 if (TREE_CODE (use) != SSA_NAME)
5061 continue;
5062 def_stmt = get_gimple_for_ssa_name (use);
5063 if (!def_stmt)
5064 continue;
5065 lattice[i] += lattice[gimple_uid (def_stmt)];
5067 i++;
5068 if (!is_gimple_assign (stmt)
5069 || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
5070 continue;
5071 op0 = gimple_op (stmt, 1);
5072 op1 = gimple_op (stmt, 2);
5073 if (TREE_CODE (op0) != SSA_NAME
5074 || TREE_CODE (op1) != SSA_NAME)
5075 continue;
5076 /* Swap operands if the second one is more expensive. */
5077 def0 = get_gimple_for_ssa_name (op0);
5078 def1 = get_gimple_for_ssa_name (op1);
5079 if (!def1)
5080 continue;
5081 swap = false;
5082 if (!def0 || lattice[gimple_uid (def1)] > lattice[gimple_uid (def0)])
5083 swap = true;
5084 if (swap)
5086 if (dump_file && (dump_flags & TDF_DETAILS))
5088 fprintf (dump_file, "Swap operands in stmt:\n");
5089 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5090 fprintf (dump_file, "Cost left opnd=%d, right opnd=%d\n",
5091 def0 ? lattice[gimple_uid (def0)] : 0,
5092 lattice[gimple_uid (def1)]);
5094 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
5095 gimple_assign_rhs2_ptr (stmt));
5098 XDELETE (lattice);
5101 /* Expand basic block BB from GIMPLE trees to RTL. */
5103 static basic_block
5104 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
5106 gimple_stmt_iterator gsi;
5107 gimple_seq stmts;
5108 gimple stmt = NULL;
5109 rtx_note *note;
5110 rtx_insn *last;
5111 edge e;
5112 edge_iterator ei;
5114 if (dump_file)
5115 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
5116 bb->index);
5118 /* Note that since we are now transitioning from GIMPLE to RTL, we
5119 cannot use the gsi_*_bb() routines because they expect the basic
5120 block to be in GIMPLE, instead of RTL. Therefore, we need to
5121 access the BB sequence directly. */
5122 if (optimize)
5123 reorder_operands (bb);
5124 stmts = bb_seq (bb);
5125 bb->il.gimple.seq = NULL;
5126 bb->il.gimple.phi_nodes = NULL;
5127 rtl_profile_for_bb (bb);
5128 init_rtl_bb_info (bb);
5129 bb->flags |= BB_RTL;
5131 /* Remove the RETURN_EXPR if we may fall though to the exit
5132 instead. */
5133 gsi = gsi_last (stmts);
5134 if (!gsi_end_p (gsi)
5135 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
5137 greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
5139 gcc_assert (single_succ_p (bb));
5140 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
5142 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5143 && !gimple_return_retval (ret_stmt))
5145 gsi_remove (&gsi, false);
5146 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
5150 gsi = gsi_start (stmts);
5151 if (!gsi_end_p (gsi))
5153 stmt = gsi_stmt (gsi);
5154 if (gimple_code (stmt) != GIMPLE_LABEL)
5155 stmt = NULL;
5158 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
5160 if (stmt || elt)
5162 last = get_last_insn ();
5164 if (stmt)
5166 expand_gimple_stmt (stmt);
5167 gsi_next (&gsi);
5170 if (elt)
5171 emit_label (*elt);
5173 /* Java emits line number notes in the top of labels.
5174 ??? Make this go away once line number notes are obsoleted. */
5175 BB_HEAD (bb) = NEXT_INSN (last);
5176 if (NOTE_P (BB_HEAD (bb)))
5177 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
5178 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
5180 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5182 else
5183 BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
5185 NOTE_BASIC_BLOCK (note) = bb;
5187 for (; !gsi_end_p (gsi); gsi_next (&gsi))
5189 basic_block new_bb;
5191 stmt = gsi_stmt (gsi);
5193 /* If this statement is a non-debug one, and we generate debug
5194 insns, then this one might be the last real use of a TERed
5195 SSA_NAME, but where there are still some debug uses further
5196 down. Expanding the current SSA name in such further debug
5197 uses by their RHS might lead to wrong debug info, as coalescing
5198 might make the operands of such RHS be placed into the same
5199 pseudo as something else. Like so:
5200 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
5201 use(a_1);
5202 a_2 = ...
5203 #DEBUG ... => a_1
5204 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
5205 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
5206 the write to a_2 would actually have clobbered the place which
5207 formerly held a_0.
5209 So, instead of that, we recognize the situation, and generate
5210 debug temporaries at the last real use of TERed SSA names:
5211 a_1 = a_0 + 1;
5212 #DEBUG #D1 => a_1
5213 use(a_1);
5214 a_2 = ...
5215 #DEBUG ... => #D1
5217 if (MAY_HAVE_DEBUG_INSNS
5218 && SA.values
5219 && !is_gimple_debug (stmt))
5221 ssa_op_iter iter;
5222 tree op;
5223 gimple def;
5225 location_t sloc = curr_insn_location ();
5227 /* Look for SSA names that have their last use here (TERed
5228 names always have only one real use). */
5229 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5230 if ((def = get_gimple_for_ssa_name (op)))
5232 imm_use_iterator imm_iter;
5233 use_operand_p use_p;
5234 bool have_debug_uses = false;
5236 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5238 if (gimple_debug_bind_p (USE_STMT (use_p)))
5240 have_debug_uses = true;
5241 break;
5245 if (have_debug_uses)
5247 /* OP is a TERed SSA name, with DEF its defining
5248 statement, and where OP is used in further debug
5249 instructions. Generate a debug temporary, and
5250 replace all uses of OP in debug insns with that
5251 temporary. */
5252 gimple debugstmt;
5253 tree value = gimple_assign_rhs_to_tree (def);
5254 tree vexpr = make_node (DEBUG_EXPR_DECL);
5255 rtx val;
5256 machine_mode mode;
5258 set_curr_insn_location (gimple_location (def));
5260 DECL_ARTIFICIAL (vexpr) = 1;
5261 TREE_TYPE (vexpr) = TREE_TYPE (value);
5262 if (DECL_P (value))
5263 mode = DECL_MODE (value);
5264 else
5265 mode = TYPE_MODE (TREE_TYPE (value));
5266 DECL_MODE (vexpr) = mode;
5268 val = gen_rtx_VAR_LOCATION
5269 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5271 emit_debug_insn (val);
5273 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5275 if (!gimple_debug_bind_p (debugstmt))
5276 continue;
5278 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5279 SET_USE (use_p, vexpr);
5281 update_stmt (debugstmt);
5285 set_curr_insn_location (sloc);
5288 currently_expanding_gimple_stmt = stmt;
5290 /* Expand this statement, then evaluate the resulting RTL and
5291 fixup the CFG accordingly. */
5292 if (gimple_code (stmt) == GIMPLE_COND)
5294 new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
5295 if (new_bb)
5296 return new_bb;
5298 else if (gimple_debug_bind_p (stmt))
5300 location_t sloc = curr_insn_location ();
5301 gimple_stmt_iterator nsi = gsi;
5303 for (;;)
5305 tree var = gimple_debug_bind_get_var (stmt);
5306 tree value;
5307 rtx val;
5308 machine_mode mode;
5310 if (TREE_CODE (var) != DEBUG_EXPR_DECL
5311 && TREE_CODE (var) != LABEL_DECL
5312 && !target_for_debug_bind (var))
5313 goto delink_debug_stmt;
5315 if (gimple_debug_bind_has_value_p (stmt))
5316 value = gimple_debug_bind_get_value (stmt);
5317 else
5318 value = NULL_TREE;
5320 last = get_last_insn ();
5322 set_curr_insn_location (gimple_location (stmt));
5324 if (DECL_P (var))
5325 mode = DECL_MODE (var);
5326 else
5327 mode = TYPE_MODE (TREE_TYPE (var));
5329 val = gen_rtx_VAR_LOCATION
5330 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5332 emit_debug_insn (val);
5334 if (dump_file && (dump_flags & TDF_DETAILS))
5336 /* We can't dump the insn with a TREE where an RTX
5337 is expected. */
5338 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5339 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5340 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5343 delink_debug_stmt:
5344 /* In order not to generate too many debug temporaries,
5345 we delink all uses of debug statements we already expanded.
5346 Therefore debug statements between definition and real
5347 use of TERed SSA names will continue to use the SSA name,
5348 and not be replaced with debug temps. */
5349 delink_stmt_imm_use (stmt);
5351 gsi = nsi;
5352 gsi_next (&nsi);
5353 if (gsi_end_p (nsi))
5354 break;
5355 stmt = gsi_stmt (nsi);
5356 if (!gimple_debug_bind_p (stmt))
5357 break;
5360 set_curr_insn_location (sloc);
5362 else if (gimple_debug_source_bind_p (stmt))
5364 location_t sloc = curr_insn_location ();
5365 tree var = gimple_debug_source_bind_get_var (stmt);
5366 tree value = gimple_debug_source_bind_get_value (stmt);
5367 rtx val;
5368 machine_mode mode;
5370 last = get_last_insn ();
5372 set_curr_insn_location (gimple_location (stmt));
5374 mode = DECL_MODE (var);
5376 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
5377 VAR_INIT_STATUS_UNINITIALIZED);
5379 emit_debug_insn (val);
5381 if (dump_file && (dump_flags & TDF_DETAILS))
5383 /* We can't dump the insn with a TREE where an RTX
5384 is expected. */
5385 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5386 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5387 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5390 set_curr_insn_location (sloc);
5392 else
5394 gcall *call_stmt = dyn_cast <gcall *> (stmt);
5395 if (call_stmt
5396 && gimple_call_tail_p (call_stmt)
5397 && disable_tail_calls)
5398 gimple_call_set_tail (call_stmt, false);
5400 if (call_stmt && gimple_call_tail_p (call_stmt))
5402 bool can_fallthru;
5403 new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
5404 if (new_bb)
5406 if (can_fallthru)
5407 bb = new_bb;
5408 else
5409 return new_bb;
5412 else
5414 def_operand_p def_p;
5415 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
5417 if (def_p != NULL)
5419 /* Ignore this stmt if it is in the list of
5420 replaceable expressions. */
5421 if (SA.values
5422 && bitmap_bit_p (SA.values,
5423 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
5424 continue;
5426 last = expand_gimple_stmt (stmt);
5427 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5432 currently_expanding_gimple_stmt = NULL;
5434 /* Expand implicit goto and convert goto_locus. */
5435 FOR_EACH_EDGE (e, ei, bb->succs)
5437 if (e->goto_locus != UNKNOWN_LOCATION)
5438 set_curr_insn_location (e->goto_locus);
5439 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
5441 emit_jump (label_rtx_for_bb (e->dest));
5442 e->flags &= ~EDGE_FALLTHRU;
5446 /* Expanded RTL can create a jump in the last instruction of block.
5447 This later might be assumed to be a jump to successor and break edge insertion.
5448 We need to insert dummy move to prevent this. PR41440. */
5449 if (single_succ_p (bb)
5450 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
5451 && (last = get_last_insn ())
5452 && JUMP_P (last))
5454 rtx dummy = gen_reg_rtx (SImode);
5455 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
5458 do_pending_stack_adjust ();
5460 /* Find the block tail. The last insn in the block is the insn
5461 before a barrier and/or table jump insn. */
5462 last = get_last_insn ();
5463 if (BARRIER_P (last))
5464 last = PREV_INSN (last);
5465 if (JUMP_TABLE_DATA_P (last))
5466 last = PREV_INSN (PREV_INSN (last));
5467 BB_END (bb) = last;
5469 update_bb_for_insn (bb);
5471 return bb;
5475 /* Create a basic block for initialization code. */
5477 static basic_block
5478 construct_init_block (void)
5480 basic_block init_block, first_block;
5481 edge e = NULL;
5482 int flags;
5484 /* Multiple entry points not supported yet. */
5485 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
5486 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5487 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
5488 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5489 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5491 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
5493 /* When entry edge points to first basic block, we don't need jump,
5494 otherwise we have to jump into proper target. */
5495 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
5497 tree label = gimple_block_label (e->dest);
5499 emit_jump (jump_target_rtx (label));
5500 flags = 0;
5502 else
5503 flags = EDGE_FALLTHRU;
5505 init_block = create_basic_block (NEXT_INSN (get_insns ()),
5506 get_last_insn (),
5507 ENTRY_BLOCK_PTR_FOR_FN (cfun));
5508 init_block->frequency = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
5509 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5510 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5511 if (e)
5513 first_block = e->dest;
5514 redirect_edge_succ (e, init_block);
5515 e = make_edge (init_block, first_block, flags);
5517 else
5518 e = make_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5519 e->probability = REG_BR_PROB_BASE;
5520 e->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5522 update_bb_for_insn (init_block);
5523 return init_block;
5526 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
5527 found in the block tree. */
5529 static void
5530 set_block_levels (tree block, int level)
5532 while (block)
5534 BLOCK_NUMBER (block) = level;
5535 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
5536 block = BLOCK_CHAIN (block);
5540 /* Create a block containing landing pads and similar stuff. */
5542 static void
5543 construct_exit_block (void)
5545 rtx_insn *head = get_last_insn ();
5546 rtx_insn *end;
5547 basic_block exit_block;
5548 edge e, e2;
5549 unsigned ix;
5550 edge_iterator ei;
5551 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
5552 rtx_insn *orig_end = BB_END (prev_bb);
5554 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5556 /* Make sure the locus is set to the end of the function, so that
5557 epilogue line numbers and warnings are set properly. */
5558 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
5559 input_location = cfun->function_end_locus;
5561 /* Generate rtl for function exit. */
5562 expand_function_end ();
5564 end = get_last_insn ();
5565 if (head == end)
5566 return;
5567 /* While emitting the function end we could move end of the last basic
5568 block. */
5569 BB_END (prev_bb) = orig_end;
5570 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
5571 head = NEXT_INSN (head);
5572 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
5573 bb frequency counting will be confused. Any instructions before that
5574 label are emitted for the case where PREV_BB falls through into the
5575 exit block, so append those instructions to prev_bb in that case. */
5576 if (NEXT_INSN (head) != return_label)
5578 while (NEXT_INSN (head) != return_label)
5580 if (!NOTE_P (NEXT_INSN (head)))
5581 BB_END (prev_bb) = NEXT_INSN (head);
5582 head = NEXT_INSN (head);
5585 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
5586 exit_block->frequency = EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency;
5587 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5588 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5590 ix = 0;
5591 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
5593 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
5594 if (!(e->flags & EDGE_ABNORMAL))
5595 redirect_edge_succ (e, exit_block);
5596 else
5597 ix++;
5600 e = make_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5601 e->probability = REG_BR_PROB_BASE;
5602 e->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5603 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5604 if (e2 != e)
5606 e->count -= e2->count;
5607 exit_block->count -= e2->count;
5608 exit_block->frequency -= EDGE_FREQUENCY (e2);
5610 if (e->count < 0)
5611 e->count = 0;
5612 if (exit_block->count < 0)
5613 exit_block->count = 0;
5614 if (exit_block->frequency < 0)
5615 exit_block->frequency = 0;
5616 update_bb_for_insn (exit_block);
5619 /* Helper function for discover_nonconstant_array_refs.
5620 Look for ARRAY_REF nodes with non-constant indexes and mark them
5621 addressable. */
5623 static tree
5624 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
5625 void *data ATTRIBUTE_UNUSED)
5627 tree t = *tp;
5629 if (IS_TYPE_OR_DECL_P (t))
5630 *walk_subtrees = 0;
5631 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5633 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5634 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
5635 && (!TREE_OPERAND (t, 2)
5636 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5637 || (TREE_CODE (t) == COMPONENT_REF
5638 && (!TREE_OPERAND (t,2)
5639 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5640 || TREE_CODE (t) == BIT_FIELD_REF
5641 || TREE_CODE (t) == REALPART_EXPR
5642 || TREE_CODE (t) == IMAGPART_EXPR
5643 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5644 || CONVERT_EXPR_P (t))
5645 t = TREE_OPERAND (t, 0);
5647 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5649 t = get_base_address (t);
5650 if (t && DECL_P (t)
5651 && DECL_MODE (t) != BLKmode)
5652 TREE_ADDRESSABLE (t) = 1;
5655 *walk_subtrees = 0;
5658 return NULL_TREE;
5661 /* RTL expansion is not able to compile array references with variable
5662 offsets for arrays stored in single register. Discover such
5663 expressions and mark variables as addressable to avoid this
5664 scenario. */
5666 static void
5667 discover_nonconstant_array_refs (void)
5669 basic_block bb;
5670 gimple_stmt_iterator gsi;
5672 FOR_EACH_BB_FN (bb, cfun)
5673 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5675 gimple stmt = gsi_stmt (gsi);
5676 if (!is_gimple_debug (stmt))
5677 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
5681 /* This function sets crtl->args.internal_arg_pointer to a virtual
5682 register if DRAP is needed. Local register allocator will replace
5683 virtual_incoming_args_rtx with the virtual register. */
5685 static void
5686 expand_stack_alignment (void)
5688 rtx drap_rtx;
5689 unsigned int preferred_stack_boundary;
5691 if (! SUPPORTS_STACK_ALIGNMENT)
5692 return;
5694 if (cfun->calls_alloca
5695 || cfun->has_nonlocal_label
5696 || crtl->has_nonlocal_goto)
5697 crtl->need_drap = true;
5699 /* Call update_stack_boundary here again to update incoming stack
5700 boundary. It may set incoming stack alignment to a different
5701 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
5702 use the minimum incoming stack alignment to check if it is OK
5703 to perform sibcall optimization since sibcall optimization will
5704 only align the outgoing stack to incoming stack boundary. */
5705 if (targetm.calls.update_stack_boundary)
5706 targetm.calls.update_stack_boundary ();
5708 /* The incoming stack frame has to be aligned at least at
5709 parm_stack_boundary. */
5710 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
5712 /* Update crtl->stack_alignment_estimated and use it later to align
5713 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
5714 exceptions since callgraph doesn't collect incoming stack alignment
5715 in this case. */
5716 if (cfun->can_throw_non_call_exceptions
5717 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
5718 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
5719 else
5720 preferred_stack_boundary = crtl->preferred_stack_boundary;
5721 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
5722 crtl->stack_alignment_estimated = preferred_stack_boundary;
5723 if (preferred_stack_boundary > crtl->stack_alignment_needed)
5724 crtl->stack_alignment_needed = preferred_stack_boundary;
5726 gcc_assert (crtl->stack_alignment_needed
5727 <= crtl->stack_alignment_estimated);
5729 crtl->stack_realign_needed
5730 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
5731 crtl->stack_realign_tried = crtl->stack_realign_needed;
5733 crtl->stack_realign_processed = true;
5735 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
5736 alignment. */
5737 gcc_assert (targetm.calls.get_drap_rtx != NULL);
5738 drap_rtx = targetm.calls.get_drap_rtx ();
5740 /* stack_realign_drap and drap_rtx must match. */
5741 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
5743 /* Do nothing if NULL is returned, which means DRAP is not needed. */
5744 if (NULL != drap_rtx)
5746 crtl->args.internal_arg_pointer = drap_rtx;
5748 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
5749 needed. */
5750 fixup_tail_calls ();
5755 static void
5756 expand_main_function (void)
5758 #if (defined(INVOKE__main) \
5759 || (!defined(HAS_INIT_SECTION) \
5760 && !defined(INIT_SECTION_ASM_OP) \
5761 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
5762 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
5763 #endif
5767 /* Expand code to initialize the stack_protect_guard. This is invoked at
5768 the beginning of a function to be protected. */
5770 #ifndef HAVE_stack_protect_set
5771 # define HAVE_stack_protect_set 0
5772 # define gen_stack_protect_set(x,y) (gcc_unreachable (), NULL_RTX)
5773 #endif
5775 static void
5776 stack_protect_prologue (void)
5778 tree guard_decl = targetm.stack_protect_guard ();
5779 rtx x, y;
5781 x = expand_normal (crtl->stack_protect_guard);
5782 y = expand_normal (guard_decl);
5784 /* Allow the target to copy from Y to X without leaking Y into a
5785 register. */
5786 if (HAVE_stack_protect_set)
5788 rtx insn = gen_stack_protect_set (x, y);
5789 if (insn)
5791 emit_insn (insn);
5792 return;
5796 /* Otherwise do a straight move. */
5797 emit_move_insn (x, y);
5800 /* Translate the intermediate representation contained in the CFG
5801 from GIMPLE trees to RTL.
5803 We do conversion per basic block and preserve/update the tree CFG.
5804 This implies we have to do some magic as the CFG can simultaneously
5805 consist of basic blocks containing RTL and GIMPLE trees. This can
5806 confuse the CFG hooks, so be careful to not manipulate CFG during
5807 the expansion. */
5809 namespace {
5811 const pass_data pass_data_expand =
5813 RTL_PASS, /* type */
5814 "expand", /* name */
5815 OPTGROUP_NONE, /* optinfo_flags */
5816 TV_EXPAND, /* tv_id */
5817 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
5818 | PROP_gimple_lcx
5819 | PROP_gimple_lvec
5820 | PROP_gimple_lva), /* properties_required */
5821 PROP_rtl, /* properties_provided */
5822 ( PROP_ssa | PROP_trees ), /* properties_destroyed */
5823 0, /* todo_flags_start */
5824 0, /* todo_flags_finish */
5827 class pass_expand : public rtl_opt_pass
5829 public:
5830 pass_expand (gcc::context *ctxt)
5831 : rtl_opt_pass (pass_data_expand, ctxt)
5834 /* opt_pass methods: */
5835 virtual unsigned int execute (function *);
5837 }; // class pass_expand
5839 unsigned int
5840 pass_expand::execute (function *fun)
5842 basic_block bb, init_block;
5843 sbitmap blocks;
5844 edge_iterator ei;
5845 edge e;
5846 rtx_insn *var_seq, *var_ret_seq;
5847 unsigned i;
5849 timevar_push (TV_OUT_OF_SSA);
5850 rewrite_out_of_ssa (&SA);
5851 timevar_pop (TV_OUT_OF_SSA);
5852 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
5854 if (MAY_HAVE_DEBUG_STMTS && flag_tree_ter)
5856 gimple_stmt_iterator gsi;
5857 FOR_EACH_BB_FN (bb, cfun)
5858 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5859 if (gimple_debug_bind_p (gsi_stmt (gsi)))
5860 avoid_deep_ter_for_debug (gsi_stmt (gsi), 0);
5863 /* Make sure all values used by the optimization passes have sane
5864 defaults. */
5865 reg_renumber = 0;
5867 /* Some backends want to know that we are expanding to RTL. */
5868 currently_expanding_to_rtl = 1;
5869 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
5870 free_dominance_info (CDI_DOMINATORS);
5872 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
5874 if (chkp_function_instrumented_p (current_function_decl))
5875 chkp_reset_rtl_bounds ();
5877 insn_locations_init ();
5878 if (!DECL_IS_BUILTIN (current_function_decl))
5880 /* Eventually, all FEs should explicitly set function_start_locus. */
5881 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
5882 set_curr_insn_location
5883 (DECL_SOURCE_LOCATION (current_function_decl));
5884 else
5885 set_curr_insn_location (fun->function_start_locus);
5887 else
5888 set_curr_insn_location (UNKNOWN_LOCATION);
5889 prologue_location = curr_insn_location ();
5891 #ifdef INSN_SCHEDULING
5892 init_sched_attrs ();
5893 #endif
5895 /* Make sure first insn is a note even if we don't want linenums.
5896 This makes sure the first insn will never be deleted.
5897 Also, final expects a note to appear there. */
5898 emit_note (NOTE_INSN_DELETED);
5900 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
5901 discover_nonconstant_array_refs ();
5903 targetm.expand_to_rtl_hook ();
5904 crtl->stack_alignment_needed = STACK_BOUNDARY;
5905 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
5906 crtl->stack_alignment_estimated = 0;
5907 crtl->preferred_stack_boundary = STACK_BOUNDARY;
5908 fun->cfg->max_jumptable_ents = 0;
5910 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
5911 of the function section at exapnsion time to predict distance of calls. */
5912 resolve_unique_section (current_function_decl, 0, flag_function_sections);
5914 /* Expand the variables recorded during gimple lowering. */
5915 timevar_push (TV_VAR_EXPAND);
5916 start_sequence ();
5918 var_ret_seq = expand_used_vars ();
5920 var_seq = get_insns ();
5921 end_sequence ();
5922 timevar_pop (TV_VAR_EXPAND);
5924 /* Honor stack protection warnings. */
5925 if (warn_stack_protect)
5927 if (fun->calls_alloca)
5928 warning (OPT_Wstack_protector,
5929 "stack protector not protecting local variables: "
5930 "variable length buffer");
5931 if (has_short_buffer && !crtl->stack_protect_guard)
5932 warning (OPT_Wstack_protector,
5933 "stack protector not protecting function: "
5934 "all local arrays are less than %d bytes long",
5935 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
5938 /* Set up parameters and prepare for return, for the function. */
5939 expand_function_start (current_function_decl);
5941 /* If we emitted any instructions for setting up the variables,
5942 emit them before the FUNCTION_START note. */
5943 if (var_seq)
5945 emit_insn_before (var_seq, parm_birth_insn);
5947 /* In expand_function_end we'll insert the alloca save/restore
5948 before parm_birth_insn. We've just insertted an alloca call.
5949 Adjust the pointer to match. */
5950 parm_birth_insn = var_seq;
5953 /* Now that we also have the parameter RTXs, copy them over to our
5954 partitions. */
5955 for (i = 0; i < SA.map->num_partitions; i++)
5957 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
5959 if (TREE_CODE (var) != VAR_DECL
5960 && !SA.partition_to_pseudo[i])
5961 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
5962 gcc_assert (SA.partition_to_pseudo[i]);
5964 /* If this decl was marked as living in multiple places, reset
5965 this now to NULL. */
5966 if (DECL_RTL_IF_SET (var) == pc_rtx)
5967 SET_DECL_RTL (var, NULL);
5969 /* Some RTL parts really want to look at DECL_RTL(x) when x
5970 was a decl marked in REG_ATTR or MEM_ATTR. We could use
5971 SET_DECL_RTL here making this available, but that would mean
5972 to select one of the potentially many RTLs for one DECL. Instead
5973 of doing that we simply reset the MEM_EXPR of the RTL in question,
5974 then nobody can get at it and hence nobody can call DECL_RTL on it. */
5975 if (!DECL_RTL_SET_P (var))
5977 if (MEM_P (SA.partition_to_pseudo[i]))
5978 set_mem_expr (SA.partition_to_pseudo[i], NULL);
5982 /* If we have a class containing differently aligned pointers
5983 we need to merge those into the corresponding RTL pointer
5984 alignment. */
5985 for (i = 1; i < num_ssa_names; i++)
5987 tree name = ssa_name (i);
5988 int part;
5989 rtx r;
5991 if (!name
5992 /* We might have generated new SSA names in
5993 update_alias_info_with_stack_vars. They will have a NULL
5994 defining statements, and won't be part of the partitioning,
5995 so ignore those. */
5996 || !SSA_NAME_DEF_STMT (name))
5997 continue;
5998 part = var_to_partition (SA.map, name);
5999 if (part == NO_PARTITION)
6000 continue;
6002 /* Adjust all partition members to get the underlying decl of
6003 the representative which we might have created in expand_one_var. */
6004 if (SSA_NAME_VAR (name) == NULL_TREE)
6006 tree leader = partition_to_var (SA.map, part);
6007 gcc_assert (SSA_NAME_VAR (leader) != NULL_TREE);
6008 replace_ssa_name_symbol (name, SSA_NAME_VAR (leader));
6010 if (!POINTER_TYPE_P (TREE_TYPE (name)))
6011 continue;
6013 r = SA.partition_to_pseudo[part];
6014 if (REG_P (r))
6015 mark_reg_pointer (r, get_pointer_alignment (name));
6018 /* If this function is `main', emit a call to `__main'
6019 to run global initializers, etc. */
6020 if (DECL_NAME (current_function_decl)
6021 && MAIN_NAME_P (DECL_NAME (current_function_decl))
6022 && DECL_FILE_SCOPE_P (current_function_decl))
6023 expand_main_function ();
6025 /* Initialize the stack_protect_guard field. This must happen after the
6026 call to __main (if any) so that the external decl is initialized. */
6027 if (crtl->stack_protect_guard)
6028 stack_protect_prologue ();
6030 expand_phi_nodes (&SA);
6032 /* Register rtl specific functions for cfg. */
6033 rtl_register_cfg_hooks ();
6035 init_block = construct_init_block ();
6037 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
6038 remaining edges later. */
6039 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
6040 e->flags &= ~EDGE_EXECUTABLE;
6042 lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
6043 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
6044 next_bb)
6045 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
6047 if (MAY_HAVE_DEBUG_INSNS)
6048 expand_debug_locations ();
6050 if (deep_ter_debug_map)
6052 delete deep_ter_debug_map;
6053 deep_ter_debug_map = NULL;
6056 /* Free stuff we no longer need after GIMPLE optimizations. */
6057 free_dominance_info (CDI_DOMINATORS);
6058 free_dominance_info (CDI_POST_DOMINATORS);
6059 delete_tree_cfg_annotations ();
6061 timevar_push (TV_OUT_OF_SSA);
6062 finish_out_of_ssa (&SA);
6063 timevar_pop (TV_OUT_OF_SSA);
6065 timevar_push (TV_POST_EXPAND);
6066 /* We are no longer in SSA form. */
6067 fun->gimple_df->in_ssa_p = false;
6068 loops_state_clear (LOOP_CLOSED_SSA);
6070 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
6071 conservatively to true until they are all profile aware. */
6072 delete lab_rtx_for_bb;
6073 free_histograms ();
6075 construct_exit_block ();
6076 insn_locations_finalize ();
6078 if (var_ret_seq)
6080 rtx_insn *after = return_label;
6081 rtx_insn *next = NEXT_INSN (after);
6082 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
6083 after = next;
6084 emit_insn_after (var_ret_seq, after);
6087 /* Zap the tree EH table. */
6088 set_eh_throw_stmt_table (fun, NULL);
6090 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
6091 split edges which edge insertions might do. */
6092 rebuild_jump_labels (get_insns ());
6094 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun),
6095 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6097 edge e;
6098 edge_iterator ei;
6099 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6101 if (e->insns.r)
6103 rebuild_jump_labels_chain (e->insns.r);
6104 /* Put insns after parm birth, but before
6105 NOTE_INSNS_FUNCTION_BEG. */
6106 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (fun)
6107 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
6109 rtx_insn *insns = e->insns.r;
6110 e->insns.r = NULL;
6111 if (NOTE_P (parm_birth_insn)
6112 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
6113 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
6114 else
6115 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
6117 else
6118 commit_one_edge_insertion (e);
6120 else
6121 ei_next (&ei);
6125 /* We're done expanding trees to RTL. */
6126 currently_expanding_to_rtl = 0;
6128 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
6129 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6131 edge e;
6132 edge_iterator ei;
6133 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6135 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
6136 e->flags &= ~EDGE_EXECUTABLE;
6138 /* At the moment not all abnormal edges match the RTL
6139 representation. It is safe to remove them here as
6140 find_many_sub_basic_blocks will rediscover them.
6141 In the future we should get this fixed properly. */
6142 if ((e->flags & EDGE_ABNORMAL)
6143 && !(e->flags & EDGE_SIBCALL))
6144 remove_edge (e);
6145 else
6146 ei_next (&ei);
6150 blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
6151 bitmap_ones (blocks);
6152 find_many_sub_basic_blocks (blocks);
6153 sbitmap_free (blocks);
6154 purge_all_dead_edges ();
6156 expand_stack_alignment ();
6158 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
6159 function. */
6160 if (crtl->tail_call_emit)
6161 fixup_tail_calls ();
6163 /* After initial rtl generation, call back to finish generating
6164 exception support code. We need to do this before cleaning up
6165 the CFG as the code does not expect dead landing pads. */
6166 if (fun->eh->region_tree != NULL)
6167 finish_eh_generation ();
6169 /* Remove unreachable blocks, otherwise we cannot compute dominators
6170 which are needed for loop state verification. As a side-effect
6171 this also compacts blocks.
6172 ??? We cannot remove trivially dead insns here as for example
6173 the DRAP reg on i?86 is not magically live at this point.
6174 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
6175 cleanup_cfg (CLEANUP_NO_INSN_DEL);
6177 #ifdef ENABLE_CHECKING
6178 verify_flow_info ();
6179 #endif
6181 /* Initialize pseudos allocated for hard registers. */
6182 emit_initial_value_sets ();
6184 /* And finally unshare all RTL. */
6185 unshare_all_rtl ();
6187 /* There's no need to defer outputting this function any more; we
6188 know we want to output it. */
6189 DECL_DEFER_OUTPUT (current_function_decl) = 0;
6191 /* Now that we're done expanding trees to RTL, we shouldn't have any
6192 more CONCATs anywhere. */
6193 generating_concat_p = 0;
6195 if (dump_file)
6197 fprintf (dump_file,
6198 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
6199 /* And the pass manager will dump RTL for us. */
6202 /* If we're emitting a nested function, make sure its parent gets
6203 emitted as well. Doing otherwise confuses debug info. */
6205 tree parent;
6206 for (parent = DECL_CONTEXT (current_function_decl);
6207 parent != NULL_TREE;
6208 parent = get_containing_scope (parent))
6209 if (TREE_CODE (parent) == FUNCTION_DECL)
6210 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
6213 /* We are now committed to emitting code for this function. Do any
6214 preparation, such as emitting abstract debug info for the inline
6215 before it gets mangled by optimization. */
6216 if (cgraph_function_possibly_inlined_p (current_function_decl))
6217 (*debug_hooks->outlining_inline_function) (current_function_decl);
6219 TREE_ASM_WRITTEN (current_function_decl) = 1;
6221 /* After expanding, the return labels are no longer needed. */
6222 return_label = NULL;
6223 naked_return_label = NULL;
6225 /* After expanding, the tm_restart map is no longer needed. */
6226 if (fun->gimple_df->tm_restart)
6227 fun->gimple_df->tm_restart = NULL;
6229 /* Tag the blocks with a depth number so that change_scope can find
6230 the common parent easily. */
6231 set_block_levels (DECL_INITIAL (fun->decl), 0);
6232 default_rtl_profile ();
6234 timevar_pop (TV_POST_EXPAND);
6236 return 0;
6239 } // anon namespace
6241 rtl_opt_pass *
6242 make_pass_expand (gcc::context *ctxt)
6244 return new pass_expand (ctxt);