gcc/
[official-gcc.git] / gcc / cfgexpand.c
blobe9840b100049dd7d6d99c8c405ccea433538514c
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 "hash-set.h"
27 #include "vec.h"
28 #include "input.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "inchash.h"
32 #include "tree.h"
33 #include "fold-const.h"
34 #include "stringpool.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "stmt.h"
38 #include "print-tree.h"
39 #include "tm_p.h"
40 #include "predict.h"
41 #include "hashtab.h"
42 #include "function.h"
43 #include "dominance.h"
44 #include "cfg.h"
45 #include "cfgrtl.h"
46 #include "cfganal.h"
47 #include "cfgbuild.h"
48 #include "cfgcleanup.h"
49 #include "basic-block.h"
50 #include "insn-codes.h"
51 #include "optabs.h"
52 #include "flags.h"
53 #include "statistics.h"
54 #include "insn-config.h"
55 #include "expmed.h"
56 #include "dojump.h"
57 #include "explow.h"
58 #include "calls.h"
59 #include "emit-rtl.h"
60 #include "expr.h"
61 #include "langhooks.h"
62 #include "bitmap.h"
63 #include "tree-ssa-alias.h"
64 #include "internal-fn.h"
65 #include "tree-eh.h"
66 #include "gimple-expr.h"
67 #include "is-a.h"
68 #include "gimple.h"
69 #include "gimple-iterator.h"
70 #include "gimple-walk.h"
71 #include "gimple-ssa.h"
72 #include "hash-map.h"
73 #include "plugin-api.h"
74 #include "ipa-ref.h"
75 #include "cgraph.h"
76 #include "tree-cfg.h"
77 #include "tree-phinodes.h"
78 #include "ssa-iterators.h"
79 #include "tree-ssanames.h"
80 #include "tree-dfa.h"
81 #include "tree-ssa.h"
82 #include "tree-pass.h"
83 #include "except.h"
84 #include "diagnostic.h"
85 #include "gimple-pretty-print.h"
86 #include "toplev.h"
87 #include "debug.h"
88 #include "params.h"
89 #include "tree-inline.h"
90 #include "value-prof.h"
91 #include "target.h"
92 #include "tree-ssa-live.h"
93 #include "tree-outof-ssa.h"
94 #include "sbitmap.h"
95 #include "cfgloop.h"
96 #include "regs.h" /* For reg_renumber. */
97 #include "insn-attr.h" /* For INSN_SCHEDULING. */
98 #include "asan.h"
99 #include "tree-ssa-address.h"
100 #include "recog.h"
101 #include "output.h"
102 #include "builtins.h"
103 #include "tree-chkp.h"
104 #include "rtl-chkp.h"
106 /* Some systems use __main in a way incompatible with its use in gcc, in these
107 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
108 give the same symbol without quotes for an alternative entry point. You
109 must define both, or neither. */
110 #ifndef NAME__MAIN
111 #define NAME__MAIN "__main"
112 #endif
114 /* This variable holds information helping the rewriting of SSA trees
115 into RTL. */
116 struct ssaexpand SA;
118 /* This variable holds the currently expanded gimple statement for purposes
119 of comminucating the profile info to the builtin expanders. */
120 gimple currently_expanding_gimple_stmt;
122 static rtx expand_debug_expr (tree);
124 /* Return an expression tree corresponding to the RHS of GIMPLE
125 statement STMT. */
127 tree
128 gimple_assign_rhs_to_tree (gimple stmt)
130 tree t;
131 enum gimple_rhs_class grhs_class;
133 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
135 if (grhs_class == GIMPLE_TERNARY_RHS)
136 t = build3 (gimple_assign_rhs_code (stmt),
137 TREE_TYPE (gimple_assign_lhs (stmt)),
138 gimple_assign_rhs1 (stmt),
139 gimple_assign_rhs2 (stmt),
140 gimple_assign_rhs3 (stmt));
141 else if (grhs_class == GIMPLE_BINARY_RHS)
142 t = build2 (gimple_assign_rhs_code (stmt),
143 TREE_TYPE (gimple_assign_lhs (stmt)),
144 gimple_assign_rhs1 (stmt),
145 gimple_assign_rhs2 (stmt));
146 else if (grhs_class == GIMPLE_UNARY_RHS)
147 t = build1 (gimple_assign_rhs_code (stmt),
148 TREE_TYPE (gimple_assign_lhs (stmt)),
149 gimple_assign_rhs1 (stmt));
150 else if (grhs_class == GIMPLE_SINGLE_RHS)
152 t = gimple_assign_rhs1 (stmt);
153 /* Avoid modifying this tree in place below. */
154 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
155 && gimple_location (stmt) != EXPR_LOCATION (t))
156 || (gimple_block (stmt)
157 && currently_expanding_to_rtl
158 && EXPR_P (t)))
159 t = copy_node (t);
161 else
162 gcc_unreachable ();
164 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
165 SET_EXPR_LOCATION (t, gimple_location (stmt));
167 return t;
171 #ifndef STACK_ALIGNMENT_NEEDED
172 #define STACK_ALIGNMENT_NEEDED 1
173 #endif
175 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
177 /* Associate declaration T with storage space X. If T is no
178 SSA name this is exactly SET_DECL_RTL, otherwise make the
179 partition of T associated with X. */
180 static inline void
181 set_rtl (tree t, rtx x)
183 if (TREE_CODE (t) == SSA_NAME)
185 SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
186 if (x && !MEM_P (x))
187 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
188 /* For the benefit of debug information at -O0 (where vartracking
189 doesn't run) record the place also in the base DECL if it's
190 a normal variable (not a parameter). */
191 if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
193 tree var = SSA_NAME_VAR (t);
194 /* If we don't yet have something recorded, just record it now. */
195 if (!DECL_RTL_SET_P (var))
196 SET_DECL_RTL (var, x);
197 /* If we have it set already to "multiple places" don't
198 change this. */
199 else if (DECL_RTL (var) == pc_rtx)
201 /* If we have something recorded and it's not the same place
202 as we want to record now, we have multiple partitions for the
203 same base variable, with different places. We can't just
204 randomly chose one, hence we have to say that we don't know.
205 This only happens with optimization, and there var-tracking
206 will figure out the right thing. */
207 else if (DECL_RTL (var) != x)
208 SET_DECL_RTL (var, pc_rtx);
211 else
212 SET_DECL_RTL (t, x);
215 /* This structure holds data relevant to one variable that will be
216 placed in a stack slot. */
217 struct stack_var
219 /* The Variable. */
220 tree decl;
222 /* Initially, the size of the variable. Later, the size of the partition,
223 if this variable becomes it's partition's representative. */
224 HOST_WIDE_INT size;
226 /* The *byte* alignment required for this variable. Or as, with the
227 size, the alignment for this partition. */
228 unsigned int alignb;
230 /* The partition representative. */
231 size_t representative;
233 /* The next stack variable in the partition, or EOC. */
234 size_t next;
236 /* The numbers of conflicting stack variables. */
237 bitmap conflicts;
240 #define EOC ((size_t)-1)
242 /* We have an array of such objects while deciding allocation. */
243 static struct stack_var *stack_vars;
244 static size_t stack_vars_alloc;
245 static size_t stack_vars_num;
246 static hash_map<tree, size_t> *decl_to_stack_part;
248 /* Conflict bitmaps go on this obstack. This allows us to destroy
249 all of them in one big sweep. */
250 static bitmap_obstack stack_var_bitmap_obstack;
252 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
253 is non-decreasing. */
254 static size_t *stack_vars_sorted;
256 /* The phase of the stack frame. This is the known misalignment of
257 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
258 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
259 static int frame_phase;
261 /* Used during expand_used_vars to remember if we saw any decls for
262 which we'd like to enable stack smashing protection. */
263 static bool has_protected_decls;
265 /* Used during expand_used_vars. Remember if we say a character buffer
266 smaller than our cutoff threshold. Used for -Wstack-protector. */
267 static bool has_short_buffer;
269 /* Compute the byte alignment to use for DECL. Ignore alignment
270 we can't do with expected alignment of the stack boundary. */
272 static unsigned int
273 align_local_variable (tree decl)
275 unsigned int align = LOCAL_DECL_ALIGNMENT (decl);
276 DECL_ALIGN (decl) = align;
277 return align / BITS_PER_UNIT;
280 /* Align given offset BASE with ALIGN. Truncate up if ALIGN_UP is true,
281 down otherwise. Return truncated BASE value. */
283 static inline unsigned HOST_WIDE_INT
284 align_base (HOST_WIDE_INT base, unsigned HOST_WIDE_INT align, bool align_up)
286 return align_up ? (base + align - 1) & -align : base & -align;
289 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
290 Return the frame offset. */
292 static HOST_WIDE_INT
293 alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
295 HOST_WIDE_INT offset, new_frame_offset;
297 if (FRAME_GROWS_DOWNWARD)
299 new_frame_offset
300 = align_base (frame_offset - frame_phase - size,
301 align, false) + frame_phase;
302 offset = new_frame_offset;
304 else
306 new_frame_offset
307 = align_base (frame_offset - frame_phase, align, true) + frame_phase;
308 offset = new_frame_offset;
309 new_frame_offset += size;
311 frame_offset = new_frame_offset;
313 if (frame_offset_overflow (frame_offset, cfun->decl))
314 frame_offset = offset = 0;
316 return offset;
319 /* Accumulate DECL into STACK_VARS. */
321 static void
322 add_stack_var (tree decl)
324 struct stack_var *v;
326 if (stack_vars_num >= stack_vars_alloc)
328 if (stack_vars_alloc)
329 stack_vars_alloc = stack_vars_alloc * 3 / 2;
330 else
331 stack_vars_alloc = 32;
332 stack_vars
333 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
335 if (!decl_to_stack_part)
336 decl_to_stack_part = new hash_map<tree, size_t>;
338 v = &stack_vars[stack_vars_num];
339 decl_to_stack_part->put (decl, stack_vars_num);
341 v->decl = decl;
342 v->size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (decl)));
343 /* Ensure that all variables have size, so that &a != &b for any two
344 variables that are simultaneously live. */
345 if (v->size == 0)
346 v->size = 1;
347 v->alignb = align_local_variable (SSAVAR (decl));
348 /* An alignment of zero can mightily confuse us later. */
349 gcc_assert (v->alignb != 0);
351 /* All variables are initially in their own partition. */
352 v->representative = stack_vars_num;
353 v->next = EOC;
355 /* All variables initially conflict with no other. */
356 v->conflicts = NULL;
358 /* Ensure that this decl doesn't get put onto the list twice. */
359 set_rtl (decl, pc_rtx);
361 stack_vars_num++;
364 /* Make the decls associated with luid's X and Y conflict. */
366 static void
367 add_stack_var_conflict (size_t x, size_t y)
369 struct stack_var *a = &stack_vars[x];
370 struct stack_var *b = &stack_vars[y];
371 if (!a->conflicts)
372 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
373 if (!b->conflicts)
374 b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
375 bitmap_set_bit (a->conflicts, y);
376 bitmap_set_bit (b->conflicts, x);
379 /* Check whether the decls associated with luid's X and Y conflict. */
381 static bool
382 stack_var_conflict_p (size_t x, size_t y)
384 struct stack_var *a = &stack_vars[x];
385 struct stack_var *b = &stack_vars[y];
386 if (x == y)
387 return false;
388 /* Partitions containing an SSA name result from gimple registers
389 with things like unsupported modes. They are top-level and
390 hence conflict with everything else. */
391 if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
392 return true;
394 if (!a->conflicts || !b->conflicts)
395 return false;
396 return bitmap_bit_p (a->conflicts, y);
399 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
400 enter its partition number into bitmap DATA. */
402 static bool
403 visit_op (gimple, tree op, tree, void *data)
405 bitmap active = (bitmap)data;
406 op = get_base_address (op);
407 if (op
408 && DECL_P (op)
409 && DECL_RTL_IF_SET (op) == pc_rtx)
411 size_t *v = decl_to_stack_part->get (op);
412 if (v)
413 bitmap_set_bit (active, *v);
415 return false;
418 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
419 record conflicts between it and all currently active other partitions
420 from bitmap DATA. */
422 static bool
423 visit_conflict (gimple, tree op, tree, void *data)
425 bitmap active = (bitmap)data;
426 op = get_base_address (op);
427 if (op
428 && DECL_P (op)
429 && DECL_RTL_IF_SET (op) == pc_rtx)
431 size_t *v = decl_to_stack_part->get (op);
432 if (v && bitmap_set_bit (active, *v))
434 size_t num = *v;
435 bitmap_iterator bi;
436 unsigned i;
437 gcc_assert (num < stack_vars_num);
438 EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
439 add_stack_var_conflict (num, i);
442 return false;
445 /* Helper routine for add_scope_conflicts, calculating the active partitions
446 at the end of BB, leaving the result in WORK. We're called to generate
447 conflicts when FOR_CONFLICT is true, otherwise we're just tracking
448 liveness. */
450 static void
451 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
453 edge e;
454 edge_iterator ei;
455 gimple_stmt_iterator gsi;
456 walk_stmt_load_store_addr_fn visit;
458 bitmap_clear (work);
459 FOR_EACH_EDGE (e, ei, bb->preds)
460 bitmap_ior_into (work, (bitmap)e->src->aux);
462 visit = visit_op;
464 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
466 gimple stmt = gsi_stmt (gsi);
467 walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
469 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
471 gimple stmt = gsi_stmt (gsi);
473 if (gimple_clobber_p (stmt))
475 tree lhs = gimple_assign_lhs (stmt);
476 size_t *v;
477 /* Nested function lowering might introduce LHSs
478 that are COMPONENT_REFs. */
479 if (TREE_CODE (lhs) != VAR_DECL)
480 continue;
481 if (DECL_RTL_IF_SET (lhs) == pc_rtx
482 && (v = decl_to_stack_part->get (lhs)))
483 bitmap_clear_bit (work, *v);
485 else if (!is_gimple_debug (stmt))
487 if (for_conflict
488 && visit == visit_op)
490 /* If this is the first real instruction in this BB we need
491 to add conflicts for everything live at this point now.
492 Unlike classical liveness for named objects we can't
493 rely on seeing a def/use of the names we're interested in.
494 There might merely be indirect loads/stores. We'd not add any
495 conflicts for such partitions. */
496 bitmap_iterator bi;
497 unsigned i;
498 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
500 struct stack_var *a = &stack_vars[i];
501 if (!a->conflicts)
502 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
503 bitmap_ior_into (a->conflicts, work);
505 visit = visit_conflict;
507 walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
512 /* Generate stack partition conflicts between all partitions that are
513 simultaneously live. */
515 static void
516 add_scope_conflicts (void)
518 basic_block bb;
519 bool changed;
520 bitmap work = BITMAP_ALLOC (NULL);
521 int *rpo;
522 int n_bbs;
524 /* We approximate the live range of a stack variable by taking the first
525 mention of its name as starting point(s), and by the end-of-scope
526 death clobber added by gimplify as ending point(s) of the range.
527 This overapproximates in the case we for instance moved an address-taken
528 operation upward, without also moving a dereference to it upwards.
529 But it's conservatively correct as a variable never can hold values
530 before its name is mentioned at least once.
532 We then do a mostly classical bitmap liveness algorithm. */
534 FOR_ALL_BB_FN (bb, cfun)
535 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
537 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
538 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
540 changed = true;
541 while (changed)
543 int i;
544 changed = false;
545 for (i = 0; i < n_bbs; i++)
547 bitmap active;
548 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
549 active = (bitmap)bb->aux;
550 add_scope_conflicts_1 (bb, work, false);
551 if (bitmap_ior_into (active, work))
552 changed = true;
556 FOR_EACH_BB_FN (bb, cfun)
557 add_scope_conflicts_1 (bb, work, true);
559 free (rpo);
560 BITMAP_FREE (work);
561 FOR_ALL_BB_FN (bb, cfun)
562 BITMAP_FREE (bb->aux);
565 /* A subroutine of partition_stack_vars. A comparison function for qsort,
566 sorting an array of indices by the properties of the object. */
568 static int
569 stack_var_cmp (const void *a, const void *b)
571 size_t ia = *(const size_t *)a;
572 size_t ib = *(const size_t *)b;
573 unsigned int aligna = stack_vars[ia].alignb;
574 unsigned int alignb = stack_vars[ib].alignb;
575 HOST_WIDE_INT sizea = stack_vars[ia].size;
576 HOST_WIDE_INT sizeb = stack_vars[ib].size;
577 tree decla = stack_vars[ia].decl;
578 tree declb = stack_vars[ib].decl;
579 bool largea, largeb;
580 unsigned int uida, uidb;
582 /* Primary compare on "large" alignment. Large comes first. */
583 largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
584 largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
585 if (largea != largeb)
586 return (int)largeb - (int)largea;
588 /* Secondary compare on size, decreasing */
589 if (sizea > sizeb)
590 return -1;
591 if (sizea < sizeb)
592 return 1;
594 /* Tertiary compare on true alignment, decreasing. */
595 if (aligna < alignb)
596 return -1;
597 if (aligna > alignb)
598 return 1;
600 /* Final compare on ID for sort stability, increasing.
601 Two SSA names are compared by their version, SSA names come before
602 non-SSA names, and two normal decls are compared by their DECL_UID. */
603 if (TREE_CODE (decla) == SSA_NAME)
605 if (TREE_CODE (declb) == SSA_NAME)
606 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
607 else
608 return -1;
610 else if (TREE_CODE (declb) == SSA_NAME)
611 return 1;
612 else
613 uida = DECL_UID (decla), uidb = DECL_UID (declb);
614 if (uida < uidb)
615 return 1;
616 if (uida > uidb)
617 return -1;
618 return 0;
621 struct part_traits : default_hashmap_traits
623 template<typename T>
624 static bool
625 is_deleted (T &e)
626 { return e.m_value == reinterpret_cast<void *> (1); }
628 template<typename T> static bool is_empty (T &e) { return e.m_value == NULL; }
629 template<typename T>
630 static void
631 mark_deleted (T &e)
632 { e.m_value = reinterpret_cast<T> (1); }
634 template<typename T>
635 static void
636 mark_empty (T &e)
637 { e.m_value = NULL; }
640 typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
642 /* If the points-to solution *PI points to variables that are in a partition
643 together with other variables add all partition members to the pointed-to
644 variables bitmap. */
646 static void
647 add_partitioned_vars_to_ptset (struct pt_solution *pt,
648 part_hashmap *decls_to_partitions,
649 hash_set<bitmap> *visited, bitmap temp)
651 bitmap_iterator bi;
652 unsigned i;
653 bitmap *part;
655 if (pt->anything
656 || pt->vars == NULL
657 /* The pointed-to vars bitmap is shared, it is enough to
658 visit it once. */
659 || visited->add (pt->vars))
660 return;
662 bitmap_clear (temp);
664 /* By using a temporary bitmap to store all members of the partitions
665 we have to add we make sure to visit each of the partitions only
666 once. */
667 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
668 if ((!temp
669 || !bitmap_bit_p (temp, i))
670 && (part = decls_to_partitions->get (i)))
671 bitmap_ior_into (temp, *part);
672 if (!bitmap_empty_p (temp))
673 bitmap_ior_into (pt->vars, temp);
676 /* Update points-to sets based on partition info, so we can use them on RTL.
677 The bitmaps representing stack partitions will be saved until expand,
678 where partitioned decls used as bases in memory expressions will be
679 rewritten. */
681 static void
682 update_alias_info_with_stack_vars (void)
684 part_hashmap *decls_to_partitions = NULL;
685 size_t i, j;
686 tree var = NULL_TREE;
688 for (i = 0; i < stack_vars_num; i++)
690 bitmap part = NULL;
691 tree name;
692 struct ptr_info_def *pi;
694 /* Not interested in partitions with single variable. */
695 if (stack_vars[i].representative != i
696 || stack_vars[i].next == EOC)
697 continue;
699 if (!decls_to_partitions)
701 decls_to_partitions = new part_hashmap;
702 cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
705 /* Create an SSA_NAME that points to the partition for use
706 as base during alias-oracle queries on RTL for bases that
707 have been partitioned. */
708 if (var == NULL_TREE)
709 var = create_tmp_var (ptr_type_node);
710 name = make_ssa_name (var);
712 /* Create bitmaps representing partitions. They will be used for
713 points-to sets later, so use GGC alloc. */
714 part = BITMAP_GGC_ALLOC ();
715 for (j = i; j != EOC; j = stack_vars[j].next)
717 tree decl = stack_vars[j].decl;
718 unsigned int uid = DECL_PT_UID (decl);
719 bitmap_set_bit (part, uid);
720 decls_to_partitions->put (uid, part);
721 cfun->gimple_df->decls_to_pointers->put (decl, name);
722 if (TREE_ADDRESSABLE (decl))
723 TREE_ADDRESSABLE (name) = 1;
726 /* Make the SSA name point to all partition members. */
727 pi = get_ptr_info (name);
728 pt_solution_set (&pi->pt, part, false);
731 /* Make all points-to sets that contain one member of a partition
732 contain all members of the partition. */
733 if (decls_to_partitions)
735 unsigned i;
736 hash_set<bitmap> visited;
737 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
739 for (i = 1; i < num_ssa_names; i++)
741 tree name = ssa_name (i);
742 struct ptr_info_def *pi;
744 if (name
745 && POINTER_TYPE_P (TREE_TYPE (name))
746 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
747 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
748 &visited, temp);
751 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
752 decls_to_partitions, &visited, temp);
754 delete decls_to_partitions;
755 BITMAP_FREE (temp);
759 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
760 partitioning algorithm. Partitions A and B are known to be non-conflicting.
761 Merge them into a single partition A. */
763 static void
764 union_stack_vars (size_t a, size_t b)
766 struct stack_var *vb = &stack_vars[b];
767 bitmap_iterator bi;
768 unsigned u;
770 gcc_assert (stack_vars[b].next == EOC);
771 /* Add B to A's partition. */
772 stack_vars[b].next = stack_vars[a].next;
773 stack_vars[b].representative = a;
774 stack_vars[a].next = b;
776 /* Update the required alignment of partition A to account for B. */
777 if (stack_vars[a].alignb < stack_vars[b].alignb)
778 stack_vars[a].alignb = stack_vars[b].alignb;
780 /* Update the interference graph and merge the conflicts. */
781 if (vb->conflicts)
783 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
784 add_stack_var_conflict (a, stack_vars[u].representative);
785 BITMAP_FREE (vb->conflicts);
789 /* A subroutine of expand_used_vars. Binpack the variables into
790 partitions constrained by the interference graph. The overall
791 algorithm used is as follows:
793 Sort the objects by size in descending order.
794 For each object A {
795 S = size(A)
796 O = 0
797 loop {
798 Look for the largest non-conflicting object B with size <= S.
799 UNION (A, B)
804 static void
805 partition_stack_vars (void)
807 size_t si, sj, n = stack_vars_num;
809 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
810 for (si = 0; si < n; ++si)
811 stack_vars_sorted[si] = si;
813 if (n == 1)
814 return;
816 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
818 for (si = 0; si < n; ++si)
820 size_t i = stack_vars_sorted[si];
821 unsigned int ialign = stack_vars[i].alignb;
822 HOST_WIDE_INT isize = stack_vars[i].size;
824 /* Ignore objects that aren't partition representatives. If we
825 see a var that is not a partition representative, it must
826 have been merged earlier. */
827 if (stack_vars[i].representative != i)
828 continue;
830 for (sj = si + 1; sj < n; ++sj)
832 size_t j = stack_vars_sorted[sj];
833 unsigned int jalign = stack_vars[j].alignb;
834 HOST_WIDE_INT jsize = stack_vars[j].size;
836 /* Ignore objects that aren't partition representatives. */
837 if (stack_vars[j].representative != j)
838 continue;
840 /* Do not mix objects of "small" (supported) alignment
841 and "large" (unsupported) alignment. */
842 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
843 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
844 break;
846 /* For Address Sanitizer do not mix objects with different
847 sizes, as the shorter vars wouldn't be adequately protected.
848 Don't do that for "large" (unsupported) alignment objects,
849 those aren't protected anyway. */
850 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && isize != jsize
851 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
852 break;
854 /* Ignore conflicting objects. */
855 if (stack_var_conflict_p (i, j))
856 continue;
858 /* UNION the objects, placing J at OFFSET. */
859 union_stack_vars (i, j);
863 update_alias_info_with_stack_vars ();
866 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
868 static void
869 dump_stack_var_partition (void)
871 size_t si, i, j, n = stack_vars_num;
873 for (si = 0; si < n; ++si)
875 i = stack_vars_sorted[si];
877 /* Skip variables that aren't partition representatives, for now. */
878 if (stack_vars[i].representative != i)
879 continue;
881 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
882 " align %u\n", (unsigned long) i, stack_vars[i].size,
883 stack_vars[i].alignb);
885 for (j = i; j != EOC; j = stack_vars[j].next)
887 fputc ('\t', dump_file);
888 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
890 fputc ('\n', dump_file);
894 /* Assign rtl to DECL at BASE + OFFSET. */
896 static void
897 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
898 HOST_WIDE_INT offset)
900 unsigned align;
901 rtx x;
903 /* If this fails, we've overflowed the stack frame. Error nicely? */
904 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
906 x = plus_constant (Pmode, base, offset);
907 x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
909 if (TREE_CODE (decl) != SSA_NAME)
911 /* Set alignment we actually gave this decl if it isn't an SSA name.
912 If it is we generate stack slots only accidentally so it isn't as
913 important, we'll simply use the alignment that is already set. */
914 if (base == virtual_stack_vars_rtx)
915 offset -= frame_phase;
916 align = offset & -offset;
917 align *= BITS_PER_UNIT;
918 if (align == 0 || align > base_align)
919 align = base_align;
921 /* One would think that we could assert that we're not decreasing
922 alignment here, but (at least) the i386 port does exactly this
923 via the MINIMUM_ALIGNMENT hook. */
925 DECL_ALIGN (decl) = align;
926 DECL_USER_ALIGN (decl) = 0;
929 set_mem_attributes (x, SSAVAR (decl), true);
930 set_rtl (decl, x);
933 struct stack_vars_data
935 /* Vector of offset pairs, always end of some padding followed
936 by start of the padding that needs Address Sanitizer protection.
937 The vector is in reversed, highest offset pairs come first. */
938 vec<HOST_WIDE_INT> asan_vec;
940 /* Vector of partition representative decls in between the paddings. */
941 vec<tree> asan_decl_vec;
943 /* Base pseudo register for Address Sanitizer protected automatic vars. */
944 rtx asan_base;
946 /* Alignment needed for the Address Sanitizer protected automatic vars. */
947 unsigned int asan_alignb;
950 /* A subroutine of expand_used_vars. Give each partition representative
951 a unique location within the stack frame. Update each partition member
952 with that location. */
954 static void
955 expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
957 size_t si, i, j, n = stack_vars_num;
958 HOST_WIDE_INT large_size = 0, large_alloc = 0;
959 rtx large_base = NULL;
960 unsigned large_align = 0;
961 tree decl;
963 /* Determine if there are any variables requiring "large" alignment.
964 Since these are dynamically allocated, we only process these if
965 no predicate involved. */
966 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
967 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
969 /* Find the total size of these variables. */
970 for (si = 0; si < n; ++si)
972 unsigned alignb;
974 i = stack_vars_sorted[si];
975 alignb = stack_vars[i].alignb;
977 /* All "large" alignment decls come before all "small" alignment
978 decls, but "large" alignment decls are not sorted based on
979 their alignment. Increase large_align to track the largest
980 required alignment. */
981 if ((alignb * BITS_PER_UNIT) > large_align)
982 large_align = alignb * BITS_PER_UNIT;
984 /* Stop when we get to the first decl with "small" alignment. */
985 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
986 break;
988 /* Skip variables that aren't partition representatives. */
989 if (stack_vars[i].representative != i)
990 continue;
992 /* Skip variables that have already had rtl assigned. See also
993 add_stack_var where we perpetrate this pc_rtx hack. */
994 decl = stack_vars[i].decl;
995 if ((TREE_CODE (decl) == SSA_NAME
996 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
997 : DECL_RTL (decl)) != pc_rtx)
998 continue;
1000 large_size += alignb - 1;
1001 large_size &= -(HOST_WIDE_INT)alignb;
1002 large_size += stack_vars[i].size;
1005 /* If there were any, allocate space. */
1006 if (large_size > 0)
1007 large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
1008 large_align, true);
1011 for (si = 0; si < n; ++si)
1013 rtx base;
1014 unsigned base_align, alignb;
1015 HOST_WIDE_INT offset;
1017 i = stack_vars_sorted[si];
1019 /* Skip variables that aren't partition representatives, for now. */
1020 if (stack_vars[i].representative != i)
1021 continue;
1023 /* Skip variables that have already had rtl assigned. See also
1024 add_stack_var where we perpetrate this pc_rtx hack. */
1025 decl = stack_vars[i].decl;
1026 if ((TREE_CODE (decl) == SSA_NAME
1027 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
1028 : DECL_RTL (decl)) != pc_rtx)
1029 continue;
1031 /* Check the predicate to see whether this variable should be
1032 allocated in this pass. */
1033 if (pred && !pred (i))
1034 continue;
1036 alignb = stack_vars[i].alignb;
1037 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1039 base = virtual_stack_vars_rtx;
1040 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && pred)
1042 HOST_WIDE_INT prev_offset
1043 = align_base (frame_offset,
1044 MAX (alignb, ASAN_RED_ZONE_SIZE),
1045 FRAME_GROWS_DOWNWARD);
1046 tree repr_decl = NULL_TREE;
1047 offset
1048 = alloc_stack_frame_space (stack_vars[i].size
1049 + ASAN_RED_ZONE_SIZE,
1050 MAX (alignb, ASAN_RED_ZONE_SIZE));
1052 data->asan_vec.safe_push (prev_offset);
1053 data->asan_vec.safe_push (offset + stack_vars[i].size);
1054 /* Find best representative of the partition.
1055 Prefer those with DECL_NAME, even better
1056 satisfying asan_protect_stack_decl predicate. */
1057 for (j = i; j != EOC; j = stack_vars[j].next)
1058 if (asan_protect_stack_decl (stack_vars[j].decl)
1059 && DECL_NAME (stack_vars[j].decl))
1061 repr_decl = stack_vars[j].decl;
1062 break;
1064 else if (repr_decl == NULL_TREE
1065 && DECL_P (stack_vars[j].decl)
1066 && DECL_NAME (stack_vars[j].decl))
1067 repr_decl = stack_vars[j].decl;
1068 if (repr_decl == NULL_TREE)
1069 repr_decl = stack_vars[i].decl;
1070 data->asan_decl_vec.safe_push (repr_decl);
1071 data->asan_alignb = MAX (data->asan_alignb, alignb);
1072 if (data->asan_base == NULL)
1073 data->asan_base = gen_reg_rtx (Pmode);
1074 base = data->asan_base;
1076 if (!STRICT_ALIGNMENT)
1077 base_align = crtl->max_used_stack_slot_alignment;
1078 else
1079 base_align = MAX (crtl->max_used_stack_slot_alignment,
1080 GET_MODE_ALIGNMENT (SImode)
1081 << ASAN_SHADOW_SHIFT);
1083 else
1085 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1086 base_align = crtl->max_used_stack_slot_alignment;
1089 else
1091 /* Large alignment is only processed in the last pass. */
1092 if (pred)
1093 continue;
1094 gcc_assert (large_base != NULL);
1096 large_alloc += alignb - 1;
1097 large_alloc &= -(HOST_WIDE_INT)alignb;
1098 offset = large_alloc;
1099 large_alloc += stack_vars[i].size;
1101 base = large_base;
1102 base_align = large_align;
1105 /* Create rtl for each variable based on their location within the
1106 partition. */
1107 for (j = i; j != EOC; j = stack_vars[j].next)
1109 expand_one_stack_var_at (stack_vars[j].decl,
1110 base, base_align,
1111 offset);
1115 gcc_assert (large_alloc == large_size);
1118 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1119 static HOST_WIDE_INT
1120 account_stack_vars (void)
1122 size_t si, j, i, n = stack_vars_num;
1123 HOST_WIDE_INT size = 0;
1125 for (si = 0; si < n; ++si)
1127 i = stack_vars_sorted[si];
1129 /* Skip variables that aren't partition representatives, for now. */
1130 if (stack_vars[i].representative != i)
1131 continue;
1133 size += stack_vars[i].size;
1134 for (j = i; j != EOC; j = stack_vars[j].next)
1135 set_rtl (stack_vars[j].decl, NULL);
1137 return size;
1140 /* A subroutine of expand_one_var. Called to immediately assign rtl
1141 to a variable to be allocated in the stack frame. */
1143 static void
1144 expand_one_stack_var (tree var)
1146 HOST_WIDE_INT size, offset;
1147 unsigned byte_align;
1149 size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (var)));
1150 byte_align = align_local_variable (SSAVAR (var));
1152 /* We handle highly aligned variables in expand_stack_vars. */
1153 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1155 offset = alloc_stack_frame_space (size, byte_align);
1157 expand_one_stack_var_at (var, virtual_stack_vars_rtx,
1158 crtl->max_used_stack_slot_alignment, offset);
1161 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1162 that will reside in a hard register. */
1164 static void
1165 expand_one_hard_reg_var (tree var)
1167 rest_of_decl_compilation (var, 0, 0);
1170 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1171 that will reside in a pseudo register. */
1173 static void
1174 expand_one_register_var (tree var)
1176 tree decl = SSAVAR (var);
1177 tree type = TREE_TYPE (decl);
1178 machine_mode reg_mode = promote_decl_mode (decl, NULL);
1179 rtx x = gen_reg_rtx (reg_mode);
1181 set_rtl (var, x);
1183 /* Note if the object is a user variable. */
1184 if (!DECL_ARTIFICIAL (decl))
1185 mark_user_reg (x);
1187 if (POINTER_TYPE_P (type))
1188 mark_reg_pointer (x, get_pointer_alignment (var));
1191 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1192 has some associated error, e.g. its type is error-mark. We just need
1193 to pick something that won't crash the rest of the compiler. */
1195 static void
1196 expand_one_error_var (tree var)
1198 machine_mode mode = DECL_MODE (var);
1199 rtx x;
1201 if (mode == BLKmode)
1202 x = gen_rtx_MEM (BLKmode, const0_rtx);
1203 else if (mode == VOIDmode)
1204 x = const0_rtx;
1205 else
1206 x = gen_reg_rtx (mode);
1208 SET_DECL_RTL (var, x);
1211 /* A subroutine of expand_one_var. VAR is a variable that will be
1212 allocated to the local stack frame. Return true if we wish to
1213 add VAR to STACK_VARS so that it will be coalesced with other
1214 variables. Return false to allocate VAR immediately.
1216 This function is used to reduce the number of variables considered
1217 for coalescing, which reduces the size of the quadratic problem. */
1219 static bool
1220 defer_stack_allocation (tree var, bool toplevel)
1222 /* Whether the variable is small enough for immediate allocation not to be
1223 a problem with regard to the frame size. */
1224 bool smallish
1225 = ((HOST_WIDE_INT) tree_to_uhwi (DECL_SIZE_UNIT (var))
1226 < PARAM_VALUE (PARAM_MIN_SIZE_FOR_STACK_SHARING));
1228 /* If stack protection is enabled, *all* stack variables must be deferred,
1229 so that we can re-order the strings to the top of the frame.
1230 Similarly for Address Sanitizer. */
1231 if (flag_stack_protect || ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK))
1232 return true;
1234 /* We handle "large" alignment via dynamic allocation. We want to handle
1235 this extra complication in only one place, so defer them. */
1236 if (DECL_ALIGN (var) > MAX_SUPPORTED_STACK_ALIGNMENT)
1237 return true;
1239 /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1240 might be detached from their block and appear at toplevel when we reach
1241 here. We want to coalesce them with variables from other blocks when
1242 the immediate contribution to the frame size would be noticeable. */
1243 if (toplevel && optimize > 0 && DECL_IGNORED_P (var) && !smallish)
1244 return true;
1246 /* Variables declared in the outermost scope automatically conflict
1247 with every other variable. The only reason to want to defer them
1248 at all is that, after sorting, we can more efficiently pack
1249 small variables in the stack frame. Continue to defer at -O2. */
1250 if (toplevel && optimize < 2)
1251 return false;
1253 /* Without optimization, *most* variables are allocated from the
1254 stack, which makes the quadratic problem large exactly when we
1255 want compilation to proceed as quickly as possible. On the
1256 other hand, we don't want the function's stack frame size to
1257 get completely out of hand. So we avoid adding scalars and
1258 "small" aggregates to the list at all. */
1259 if (optimize == 0 && smallish)
1260 return false;
1262 return true;
1265 /* A subroutine of expand_used_vars. Expand one variable according to
1266 its flavor. Variables to be placed on the stack are not actually
1267 expanded yet, merely recorded.
1268 When REALLY_EXPAND is false, only add stack values to be allocated.
1269 Return stack usage this variable is supposed to take.
1272 static HOST_WIDE_INT
1273 expand_one_var (tree var, bool toplevel, bool really_expand)
1275 unsigned int align = BITS_PER_UNIT;
1276 tree origvar = var;
1278 var = SSAVAR (var);
1280 if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
1282 /* Because we don't know if VAR will be in register or on stack,
1283 we conservatively assume it will be on stack even if VAR is
1284 eventually put into register after RA pass. For non-automatic
1285 variables, which won't be on stack, we collect alignment of
1286 type and ignore user specified alignment. Similarly for
1287 SSA_NAMEs for which use_register_for_decl returns true. */
1288 if (TREE_STATIC (var)
1289 || DECL_EXTERNAL (var)
1290 || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1291 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1292 TYPE_MODE (TREE_TYPE (var)),
1293 TYPE_ALIGN (TREE_TYPE (var)));
1294 else if (DECL_HAS_VALUE_EXPR_P (var)
1295 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1296 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1297 or variables which were assigned a stack slot already by
1298 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1299 changed from the offset chosen to it. */
1300 align = crtl->stack_alignment_estimated;
1301 else
1302 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1304 /* If the variable alignment is very large we'll dynamicaly allocate
1305 it, which means that in-frame portion is just a pointer. */
1306 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1307 align = POINTER_SIZE;
1310 if (SUPPORTS_STACK_ALIGNMENT
1311 && crtl->stack_alignment_estimated < align)
1313 /* stack_alignment_estimated shouldn't change after stack
1314 realign decision made */
1315 gcc_assert (!crtl->stack_realign_processed);
1316 crtl->stack_alignment_estimated = align;
1319 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1320 So here we only make sure stack_alignment_needed >= align. */
1321 if (crtl->stack_alignment_needed < align)
1322 crtl->stack_alignment_needed = align;
1323 if (crtl->max_used_stack_slot_alignment < align)
1324 crtl->max_used_stack_slot_alignment = align;
1326 if (TREE_CODE (origvar) == SSA_NAME)
1328 gcc_assert (TREE_CODE (var) != VAR_DECL
1329 || (!DECL_EXTERNAL (var)
1330 && !DECL_HAS_VALUE_EXPR_P (var)
1331 && !TREE_STATIC (var)
1332 && TREE_TYPE (var) != error_mark_node
1333 && !DECL_HARD_REGISTER (var)
1334 && really_expand));
1336 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1338 else if (DECL_EXTERNAL (var))
1340 else if (DECL_HAS_VALUE_EXPR_P (var))
1342 else if (TREE_STATIC (var))
1344 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1346 else if (TREE_TYPE (var) == error_mark_node)
1348 if (really_expand)
1349 expand_one_error_var (var);
1351 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1353 if (really_expand)
1355 expand_one_hard_reg_var (var);
1356 if (!DECL_HARD_REGISTER (var))
1357 /* Invalid register specification. */
1358 expand_one_error_var (var);
1361 else if (use_register_for_decl (var))
1363 if (really_expand)
1364 expand_one_register_var (origvar);
1366 else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
1368 /* Reject variables which cover more than half of the address-space. */
1369 if (really_expand)
1371 error ("size of variable %q+D is too large", var);
1372 expand_one_error_var (var);
1375 else if (defer_stack_allocation (var, toplevel))
1376 add_stack_var (origvar);
1377 else
1379 if (really_expand)
1380 expand_one_stack_var (origvar);
1381 return tree_to_uhwi (DECL_SIZE_UNIT (var));
1383 return 0;
1386 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1387 expanding variables. Those variables that can be put into registers
1388 are allocated pseudos; those that can't are put on the stack.
1390 TOPLEVEL is true if this is the outermost BLOCK. */
1392 static void
1393 expand_used_vars_for_block (tree block, bool toplevel)
1395 tree t;
1397 /* Expand all variables at this level. */
1398 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1399 if (TREE_USED (t)
1400 && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1401 || !DECL_NONSHAREABLE (t)))
1402 expand_one_var (t, toplevel, true);
1404 /* Expand all variables at containing levels. */
1405 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1406 expand_used_vars_for_block (t, false);
1409 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1410 and clear TREE_USED on all local variables. */
1412 static void
1413 clear_tree_used (tree block)
1415 tree t;
1417 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1418 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1419 if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1420 || !DECL_NONSHAREABLE (t))
1421 TREE_USED (t) = 0;
1423 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1424 clear_tree_used (t);
1427 enum {
1428 SPCT_FLAG_DEFAULT = 1,
1429 SPCT_FLAG_ALL = 2,
1430 SPCT_FLAG_STRONG = 3,
1431 SPCT_FLAG_EXPLICIT = 4
1434 /* Examine TYPE and determine a bit mask of the following features. */
1436 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1437 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1438 #define SPCT_HAS_ARRAY 4
1439 #define SPCT_HAS_AGGREGATE 8
1441 static unsigned int
1442 stack_protect_classify_type (tree type)
1444 unsigned int ret = 0;
1445 tree t;
1447 switch (TREE_CODE (type))
1449 case ARRAY_TYPE:
1450 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1451 if (t == char_type_node
1452 || t == signed_char_type_node
1453 || t == unsigned_char_type_node)
1455 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1456 unsigned HOST_WIDE_INT len;
1458 if (!TYPE_SIZE_UNIT (type)
1459 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1460 len = max;
1461 else
1462 len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1464 if (len < max)
1465 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1466 else
1467 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1469 else
1470 ret = SPCT_HAS_ARRAY;
1471 break;
1473 case UNION_TYPE:
1474 case QUAL_UNION_TYPE:
1475 case RECORD_TYPE:
1476 ret = SPCT_HAS_AGGREGATE;
1477 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1478 if (TREE_CODE (t) == FIELD_DECL)
1479 ret |= stack_protect_classify_type (TREE_TYPE (t));
1480 break;
1482 default:
1483 break;
1486 return ret;
1489 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1490 part of the local stack frame. Remember if we ever return nonzero for
1491 any variable in this function. The return value is the phase number in
1492 which the variable should be allocated. */
1494 static int
1495 stack_protect_decl_phase (tree decl)
1497 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1498 int ret = 0;
1500 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1501 has_short_buffer = true;
1503 if (flag_stack_protect == SPCT_FLAG_ALL
1504 || flag_stack_protect == SPCT_FLAG_STRONG
1505 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1506 && lookup_attribute ("stack_protect",
1507 DECL_ATTRIBUTES (current_function_decl))))
1509 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1510 && !(bits & SPCT_HAS_AGGREGATE))
1511 ret = 1;
1512 else if (bits & SPCT_HAS_ARRAY)
1513 ret = 2;
1515 else
1516 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1518 if (ret)
1519 has_protected_decls = true;
1521 return ret;
1524 /* Two helper routines that check for phase 1 and phase 2. These are used
1525 as callbacks for expand_stack_vars. */
1527 static bool
1528 stack_protect_decl_phase_1 (size_t i)
1530 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
1533 static bool
1534 stack_protect_decl_phase_2 (size_t i)
1536 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
1539 /* And helper function that checks for asan phase (with stack protector
1540 it is phase 3). This is used as callback for expand_stack_vars.
1541 Returns true if any of the vars in the partition need to be protected. */
1543 static bool
1544 asan_decl_phase_3 (size_t i)
1546 while (i != EOC)
1548 if (asan_protect_stack_decl (stack_vars[i].decl))
1549 return true;
1550 i = stack_vars[i].next;
1552 return false;
1555 /* Ensure that variables in different stack protection phases conflict
1556 so that they are not merged and share the same stack slot. */
1558 static void
1559 add_stack_protection_conflicts (void)
1561 size_t i, j, n = stack_vars_num;
1562 unsigned char *phase;
1564 phase = XNEWVEC (unsigned char, n);
1565 for (i = 0; i < n; ++i)
1566 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1568 for (i = 0; i < n; ++i)
1570 unsigned char ph_i = phase[i];
1571 for (j = i + 1; j < n; ++j)
1572 if (ph_i != phase[j])
1573 add_stack_var_conflict (i, j);
1576 XDELETEVEC (phase);
1579 /* Create a decl for the guard at the top of the stack frame. */
1581 static void
1582 create_stack_guard (void)
1584 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1585 VAR_DECL, NULL, ptr_type_node);
1586 TREE_THIS_VOLATILE (guard) = 1;
1587 TREE_USED (guard) = 1;
1588 expand_one_stack_var (guard);
1589 crtl->stack_protect_guard = guard;
1592 /* Prepare for expanding variables. */
1593 static void
1594 init_vars_expansion (void)
1596 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
1597 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
1599 /* A map from decl to stack partition. */
1600 decl_to_stack_part = new hash_map<tree, size_t>;
1602 /* Initialize local stack smashing state. */
1603 has_protected_decls = false;
1604 has_short_buffer = false;
1607 /* Free up stack variable graph data. */
1608 static void
1609 fini_vars_expansion (void)
1611 bitmap_obstack_release (&stack_var_bitmap_obstack);
1612 if (stack_vars)
1613 XDELETEVEC (stack_vars);
1614 if (stack_vars_sorted)
1615 XDELETEVEC (stack_vars_sorted);
1616 stack_vars = NULL;
1617 stack_vars_sorted = NULL;
1618 stack_vars_alloc = stack_vars_num = 0;
1619 delete decl_to_stack_part;
1620 decl_to_stack_part = NULL;
1623 /* Make a fair guess for the size of the stack frame of the function
1624 in NODE. This doesn't have to be exact, the result is only used in
1625 the inline heuristics. So we don't want to run the full stack var
1626 packing algorithm (which is quadratic in the number of stack vars).
1627 Instead, we calculate the total size of all stack vars. This turns
1628 out to be a pretty fair estimate -- packing of stack vars doesn't
1629 happen very often. */
1631 HOST_WIDE_INT
1632 estimated_stack_frame_size (struct cgraph_node *node)
1634 HOST_WIDE_INT size = 0;
1635 size_t i;
1636 tree var;
1637 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1639 push_cfun (fn);
1641 init_vars_expansion ();
1643 FOR_EACH_LOCAL_DECL (fn, i, var)
1644 if (auto_var_in_fn_p (var, fn->decl))
1645 size += expand_one_var (var, true, false);
1647 if (stack_vars_num > 0)
1649 /* Fake sorting the stack vars for account_stack_vars (). */
1650 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1651 for (i = 0; i < stack_vars_num; ++i)
1652 stack_vars_sorted[i] = i;
1653 size += account_stack_vars ();
1656 fini_vars_expansion ();
1657 pop_cfun ();
1658 return size;
1661 /* Helper routine to check if a record or union contains an array field. */
1663 static int
1664 record_or_union_type_has_array_p (const_tree tree_type)
1666 tree fields = TYPE_FIELDS (tree_type);
1667 tree f;
1669 for (f = fields; f; f = DECL_CHAIN (f))
1670 if (TREE_CODE (f) == FIELD_DECL)
1672 tree field_type = TREE_TYPE (f);
1673 if (RECORD_OR_UNION_TYPE_P (field_type)
1674 && record_or_union_type_has_array_p (field_type))
1675 return 1;
1676 if (TREE_CODE (field_type) == ARRAY_TYPE)
1677 return 1;
1679 return 0;
1682 /* Check if the current function has local referenced variables that
1683 have their addresses taken, contain an array, or are arrays. */
1685 static bool
1686 stack_protect_decl_p ()
1688 unsigned i;
1689 tree var;
1691 FOR_EACH_LOCAL_DECL (cfun, i, var)
1692 if (!is_global_var (var))
1694 tree var_type = TREE_TYPE (var);
1695 if (TREE_CODE (var) == VAR_DECL
1696 && (TREE_CODE (var_type) == ARRAY_TYPE
1697 || TREE_ADDRESSABLE (var)
1698 || (RECORD_OR_UNION_TYPE_P (var_type)
1699 && record_or_union_type_has_array_p (var_type))))
1700 return true;
1702 return false;
1705 /* Check if the current function has calls that use a return slot. */
1707 static bool
1708 stack_protect_return_slot_p ()
1710 basic_block bb;
1712 FOR_ALL_BB_FN (bb, cfun)
1713 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1714 !gsi_end_p (gsi); gsi_next (&gsi))
1716 gimple stmt = gsi_stmt (gsi);
1717 /* This assumes that calls to internal-only functions never
1718 use a return slot. */
1719 if (is_gimple_call (stmt)
1720 && !gimple_call_internal_p (stmt)
1721 && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
1722 gimple_call_fndecl (stmt)))
1723 return true;
1725 return false;
1728 /* Expand all variables used in the function. */
1730 static rtx_insn *
1731 expand_used_vars (void)
1733 tree var, outer_block = DECL_INITIAL (current_function_decl);
1734 vec<tree> maybe_local_decls = vNULL;
1735 rtx_insn *var_end_seq = NULL;
1736 unsigned i;
1737 unsigned len;
1738 bool gen_stack_protect_signal = false;
1740 /* Compute the phase of the stack frame for this function. */
1742 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1743 int off = STARTING_FRAME_OFFSET % align;
1744 frame_phase = off ? align - off : 0;
1747 /* Set TREE_USED on all variables in the local_decls. */
1748 FOR_EACH_LOCAL_DECL (cfun, i, var)
1749 TREE_USED (var) = 1;
1750 /* Clear TREE_USED on all variables associated with a block scope. */
1751 clear_tree_used (DECL_INITIAL (current_function_decl));
1753 init_vars_expansion ();
1755 if (targetm.use_pseudo_pic_reg ())
1756 pic_offset_table_rtx = gen_reg_rtx (Pmode);
1758 hash_map<tree, tree> ssa_name_decls;
1759 for (i = 0; i < SA.map->num_partitions; i++)
1761 tree var = partition_to_var (SA.map, i);
1763 gcc_assert (!virtual_operand_p (var));
1765 /* Assign decls to each SSA name partition, share decls for partitions
1766 we could have coalesced (those with the same type). */
1767 if (SSA_NAME_VAR (var) == NULL_TREE)
1769 tree *slot = &ssa_name_decls.get_or_insert (TREE_TYPE (var));
1770 if (!*slot)
1771 *slot = create_tmp_reg (TREE_TYPE (var));
1772 replace_ssa_name_symbol (var, *slot);
1775 /* Always allocate space for partitions based on VAR_DECLs. But for
1776 those based on PARM_DECLs or RESULT_DECLs and which matter for the
1777 debug info, there is no need to do so if optimization is disabled
1778 because all the SSA_NAMEs based on these DECLs have been coalesced
1779 into a single partition, which is thus assigned the canonical RTL
1780 location of the DECLs. If in_lto_p, we can't rely on optimize,
1781 a function could be compiled with -O1 -flto first and only the
1782 link performed at -O0. */
1783 if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1784 expand_one_var (var, true, true);
1785 else if (DECL_IGNORED_P (SSA_NAME_VAR (var)) || optimize || in_lto_p)
1787 /* This is a PARM_DECL or RESULT_DECL. For those partitions that
1788 contain the default def (representing the parm or result itself)
1789 we don't do anything here. But those which don't contain the
1790 default def (representing a temporary based on the parm/result)
1791 we need to allocate space just like for normal VAR_DECLs. */
1792 if (!bitmap_bit_p (SA.partition_has_default_def, i))
1794 expand_one_var (var, true, true);
1795 gcc_assert (SA.partition_to_pseudo[i]);
1800 if (flag_stack_protect == SPCT_FLAG_STRONG)
1801 gen_stack_protect_signal
1802 = stack_protect_decl_p () || stack_protect_return_slot_p ();
1804 /* At this point all variables on the local_decls with TREE_USED
1805 set are not associated with any block scope. Lay them out. */
1807 len = vec_safe_length (cfun->local_decls);
1808 FOR_EACH_LOCAL_DECL (cfun, i, var)
1810 bool expand_now = false;
1812 /* Expanded above already. */
1813 if (is_gimple_reg (var))
1815 TREE_USED (var) = 0;
1816 goto next;
1818 /* We didn't set a block for static or extern because it's hard
1819 to tell the difference between a global variable (re)declared
1820 in a local scope, and one that's really declared there to
1821 begin with. And it doesn't really matter much, since we're
1822 not giving them stack space. Expand them now. */
1823 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1824 expand_now = true;
1826 /* Expand variables not associated with any block now. Those created by
1827 the optimizers could be live anywhere in the function. Those that
1828 could possibly have been scoped originally and detached from their
1829 block will have their allocation deferred so we coalesce them with
1830 others when optimization is enabled. */
1831 else if (TREE_USED (var))
1832 expand_now = true;
1834 /* Finally, mark all variables on the list as used. We'll use
1835 this in a moment when we expand those associated with scopes. */
1836 TREE_USED (var) = 1;
1838 if (expand_now)
1839 expand_one_var (var, true, true);
1841 next:
1842 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1844 rtx rtl = DECL_RTL_IF_SET (var);
1846 /* Keep artificial non-ignored vars in cfun->local_decls
1847 chain until instantiate_decls. */
1848 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1849 add_local_decl (cfun, var);
1850 else if (rtl == NULL_RTX)
1851 /* If rtl isn't set yet, which can happen e.g. with
1852 -fstack-protector, retry before returning from this
1853 function. */
1854 maybe_local_decls.safe_push (var);
1858 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
1860 +-----------------+-----------------+
1861 | ...processed... | ...duplicates...|
1862 +-----------------+-----------------+
1864 +-- LEN points here.
1866 We just want the duplicates, as those are the artificial
1867 non-ignored vars that we want to keep until instantiate_decls.
1868 Move them down and truncate the array. */
1869 if (!vec_safe_is_empty (cfun->local_decls))
1870 cfun->local_decls->block_remove (0, len);
1872 /* At this point, all variables within the block tree with TREE_USED
1873 set are actually used by the optimized function. Lay them out. */
1874 expand_used_vars_for_block (outer_block, true);
1876 if (stack_vars_num > 0)
1878 add_scope_conflicts ();
1880 /* If stack protection is enabled, we don't share space between
1881 vulnerable data and non-vulnerable data. */
1882 if (flag_stack_protect != 0
1883 && (flag_stack_protect != SPCT_FLAG_EXPLICIT
1884 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1885 && lookup_attribute ("stack_protect",
1886 DECL_ATTRIBUTES (current_function_decl)))))
1887 add_stack_protection_conflicts ();
1889 /* Now that we have collected all stack variables, and have computed a
1890 minimal interference graph, attempt to save some stack space. */
1891 partition_stack_vars ();
1892 if (dump_file)
1893 dump_stack_var_partition ();
1896 switch (flag_stack_protect)
1898 case SPCT_FLAG_ALL:
1899 create_stack_guard ();
1900 break;
1902 case SPCT_FLAG_STRONG:
1903 if (gen_stack_protect_signal
1904 || cfun->calls_alloca || has_protected_decls
1905 || lookup_attribute ("stack_protect",
1906 DECL_ATTRIBUTES (current_function_decl)))
1907 create_stack_guard ();
1908 break;
1910 case SPCT_FLAG_DEFAULT:
1911 if (cfun->calls_alloca || has_protected_decls
1912 || lookup_attribute ("stack_protect",
1913 DECL_ATTRIBUTES (current_function_decl)))
1914 create_stack_guard ();
1915 break;
1917 case SPCT_FLAG_EXPLICIT:
1918 if (lookup_attribute ("stack_protect",
1919 DECL_ATTRIBUTES (current_function_decl)))
1920 create_stack_guard ();
1921 break;
1922 default:
1926 /* Assign rtl to each variable based on these partitions. */
1927 if (stack_vars_num > 0)
1929 struct stack_vars_data data;
1931 data.asan_vec = vNULL;
1932 data.asan_decl_vec = vNULL;
1933 data.asan_base = NULL_RTX;
1934 data.asan_alignb = 0;
1936 /* Reorder decls to be protected by iterating over the variables
1937 array multiple times, and allocating out of each phase in turn. */
1938 /* ??? We could probably integrate this into the qsort we did
1939 earlier, such that we naturally see these variables first,
1940 and thus naturally allocate things in the right order. */
1941 if (has_protected_decls)
1943 /* Phase 1 contains only character arrays. */
1944 expand_stack_vars (stack_protect_decl_phase_1, &data);
1946 /* Phase 2 contains other kinds of arrays. */
1947 if (flag_stack_protect == SPCT_FLAG_ALL
1948 || flag_stack_protect == SPCT_FLAG_STRONG
1949 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1950 && lookup_attribute ("stack_protect",
1951 DECL_ATTRIBUTES (current_function_decl))))
1952 expand_stack_vars (stack_protect_decl_phase_2, &data);
1955 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK)
1956 /* Phase 3, any partitions that need asan protection
1957 in addition to phase 1 and 2. */
1958 expand_stack_vars (asan_decl_phase_3, &data);
1960 if (!data.asan_vec.is_empty ())
1962 HOST_WIDE_INT prev_offset = frame_offset;
1963 HOST_WIDE_INT offset, sz, redzonesz;
1964 redzonesz = ASAN_RED_ZONE_SIZE;
1965 sz = data.asan_vec[0] - prev_offset;
1966 if (data.asan_alignb > ASAN_RED_ZONE_SIZE
1967 && data.asan_alignb <= 4096
1968 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
1969 redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
1970 & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
1971 offset
1972 = alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE);
1973 data.asan_vec.safe_push (prev_offset);
1974 data.asan_vec.safe_push (offset);
1975 /* Leave space for alignment if STRICT_ALIGNMENT. */
1976 if (STRICT_ALIGNMENT)
1977 alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
1978 << ASAN_SHADOW_SHIFT)
1979 / BITS_PER_UNIT, 1);
1981 var_end_seq
1982 = asan_emit_stack_protection (virtual_stack_vars_rtx,
1983 data.asan_base,
1984 data.asan_alignb,
1985 data.asan_vec.address (),
1986 data.asan_decl_vec.address (),
1987 data.asan_vec.length ());
1990 expand_stack_vars (NULL, &data);
1992 data.asan_vec.release ();
1993 data.asan_decl_vec.release ();
1996 fini_vars_expansion ();
1998 /* If there were any artificial non-ignored vars without rtl
1999 found earlier, see if deferred stack allocation hasn't assigned
2000 rtl to them. */
2001 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
2003 rtx rtl = DECL_RTL_IF_SET (var);
2005 /* Keep artificial non-ignored vars in cfun->local_decls
2006 chain until instantiate_decls. */
2007 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2008 add_local_decl (cfun, var);
2010 maybe_local_decls.release ();
2012 /* If the target requires that FRAME_OFFSET be aligned, do it. */
2013 if (STACK_ALIGNMENT_NEEDED)
2015 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2016 if (!FRAME_GROWS_DOWNWARD)
2017 frame_offset += align - 1;
2018 frame_offset &= -align;
2021 return var_end_seq;
2025 /* If we need to produce a detailed dump, print the tree representation
2026 for STMT to the dump file. SINCE is the last RTX after which the RTL
2027 generated for STMT should have been appended. */
2029 static void
2030 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx_insn *since)
2032 if (dump_file && (dump_flags & TDF_DETAILS))
2034 fprintf (dump_file, "\n;; ");
2035 print_gimple_stmt (dump_file, stmt, 0,
2036 TDF_SLIM | (dump_flags & TDF_LINENO));
2037 fprintf (dump_file, "\n");
2039 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
2043 /* Maps the blocks that do not contain tree labels to rtx labels. */
2045 static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
2047 /* Returns the label_rtx expression for a label starting basic block BB. */
2049 static rtx_code_label *
2050 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
2052 gimple_stmt_iterator gsi;
2053 tree lab;
2055 if (bb->flags & BB_RTL)
2056 return block_label (bb);
2058 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
2059 if (elt)
2060 return *elt;
2062 /* Find the tree label if it is present. */
2064 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2066 glabel *lab_stmt;
2068 lab_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
2069 if (!lab_stmt)
2070 break;
2072 lab = gimple_label_label (lab_stmt);
2073 if (DECL_NONLOCAL (lab))
2074 break;
2076 return jump_target_rtx (lab);
2079 rtx_code_label *l = gen_label_rtx ();
2080 lab_rtx_for_bb->put (bb, l);
2081 return l;
2085 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2086 of a basic block where we just expanded the conditional at the end,
2087 possibly clean up the CFG and instruction sequence. LAST is the
2088 last instruction before the just emitted jump sequence. */
2090 static void
2091 maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2093 /* Special case: when jumpif decides that the condition is
2094 trivial it emits an unconditional jump (and the necessary
2095 barrier). But we still have two edges, the fallthru one is
2096 wrong. purge_dead_edges would clean this up later. Unfortunately
2097 we have to insert insns (and split edges) before
2098 find_many_sub_basic_blocks and hence before purge_dead_edges.
2099 But splitting edges might create new blocks which depend on the
2100 fact that if there are two edges there's no barrier. So the
2101 barrier would get lost and verify_flow_info would ICE. Instead
2102 of auditing all edge splitters to care for the barrier (which
2103 normally isn't there in a cleaned CFG), fix it here. */
2104 if (BARRIER_P (get_last_insn ()))
2106 rtx_insn *insn;
2107 remove_edge (e);
2108 /* Now, we have a single successor block, if we have insns to
2109 insert on the remaining edge we potentially will insert
2110 it at the end of this block (if the dest block isn't feasible)
2111 in order to avoid splitting the edge. This insertion will take
2112 place in front of the last jump. But we might have emitted
2113 multiple jumps (conditional and one unconditional) to the
2114 same destination. Inserting in front of the last one then
2115 is a problem. See PR 40021. We fix this by deleting all
2116 jumps except the last unconditional one. */
2117 insn = PREV_INSN (get_last_insn ());
2118 /* Make sure we have an unconditional jump. Otherwise we're
2119 confused. */
2120 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2121 for (insn = PREV_INSN (insn); insn != last;)
2123 insn = PREV_INSN (insn);
2124 if (JUMP_P (NEXT_INSN (insn)))
2126 if (!any_condjump_p (NEXT_INSN (insn)))
2128 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2129 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2131 delete_insn (NEXT_INSN (insn));
2137 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2138 Returns a new basic block if we've terminated the current basic
2139 block and created a new one. */
2141 static basic_block
2142 expand_gimple_cond (basic_block bb, gcond *stmt)
2144 basic_block new_bb, dest;
2145 edge new_edge;
2146 edge true_edge;
2147 edge false_edge;
2148 rtx_insn *last2, *last;
2149 enum tree_code code;
2150 tree op0, op1;
2152 code = gimple_cond_code (stmt);
2153 op0 = gimple_cond_lhs (stmt);
2154 op1 = gimple_cond_rhs (stmt);
2155 /* We're sometimes presented with such code:
2156 D.123_1 = x < y;
2157 if (D.123_1 != 0)
2159 This would expand to two comparisons which then later might
2160 be cleaned up by combine. But some pattern matchers like if-conversion
2161 work better when there's only one compare, so make up for this
2162 here as special exception if TER would have made the same change. */
2163 if (SA.values
2164 && TREE_CODE (op0) == SSA_NAME
2165 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2166 && TREE_CODE (op1) == INTEGER_CST
2167 && ((gimple_cond_code (stmt) == NE_EXPR
2168 && integer_zerop (op1))
2169 || (gimple_cond_code (stmt) == EQ_EXPR
2170 && integer_onep (op1)))
2171 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2173 gimple second = SSA_NAME_DEF_STMT (op0);
2174 if (gimple_code (second) == GIMPLE_ASSIGN)
2176 enum tree_code code2 = gimple_assign_rhs_code (second);
2177 if (TREE_CODE_CLASS (code2) == tcc_comparison)
2179 code = code2;
2180 op0 = gimple_assign_rhs1 (second);
2181 op1 = gimple_assign_rhs2 (second);
2183 /* If jumps are cheap and the target does not support conditional
2184 compare, turn some more codes into jumpy sequences. */
2185 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4
2186 && targetm.gen_ccmp_first == NULL)
2188 if ((code2 == BIT_AND_EXPR
2189 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2190 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2191 || code2 == TRUTH_AND_EXPR)
2193 code = TRUTH_ANDIF_EXPR;
2194 op0 = gimple_assign_rhs1 (second);
2195 op1 = gimple_assign_rhs2 (second);
2197 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2199 code = TRUTH_ORIF_EXPR;
2200 op0 = gimple_assign_rhs1 (second);
2201 op1 = gimple_assign_rhs2 (second);
2207 last2 = last = get_last_insn ();
2209 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2210 set_curr_insn_location (gimple_location (stmt));
2212 /* These flags have no purpose in RTL land. */
2213 true_edge->flags &= ~EDGE_TRUE_VALUE;
2214 false_edge->flags &= ~EDGE_FALSE_VALUE;
2216 /* We can either have a pure conditional jump with one fallthru edge or
2217 two-way jump that needs to be decomposed into two basic blocks. */
2218 if (false_edge->dest == bb->next_bb)
2220 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2221 true_edge->probability);
2222 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2223 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2224 set_curr_insn_location (true_edge->goto_locus);
2225 false_edge->flags |= EDGE_FALLTHRU;
2226 maybe_cleanup_end_of_block (false_edge, last);
2227 return NULL;
2229 if (true_edge->dest == bb->next_bb)
2231 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2232 false_edge->probability);
2233 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2234 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2235 set_curr_insn_location (false_edge->goto_locus);
2236 true_edge->flags |= EDGE_FALLTHRU;
2237 maybe_cleanup_end_of_block (true_edge, last);
2238 return NULL;
2241 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2242 true_edge->probability);
2243 last = get_last_insn ();
2244 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2245 set_curr_insn_location (false_edge->goto_locus);
2246 emit_jump (label_rtx_for_bb (false_edge->dest));
2248 BB_END (bb) = last;
2249 if (BARRIER_P (BB_END (bb)))
2250 BB_END (bb) = PREV_INSN (BB_END (bb));
2251 update_bb_for_insn (bb);
2253 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2254 dest = false_edge->dest;
2255 redirect_edge_succ (false_edge, new_bb);
2256 false_edge->flags |= EDGE_FALLTHRU;
2257 new_bb->count = false_edge->count;
2258 new_bb->frequency = EDGE_FREQUENCY (false_edge);
2259 add_bb_to_loop (new_bb, bb->loop_father);
2260 new_edge = make_edge (new_bb, dest, 0);
2261 new_edge->probability = REG_BR_PROB_BASE;
2262 new_edge->count = new_bb->count;
2263 if (BARRIER_P (BB_END (new_bb)))
2264 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2265 update_bb_for_insn (new_bb);
2267 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2269 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2271 set_curr_insn_location (true_edge->goto_locus);
2272 true_edge->goto_locus = curr_insn_location ();
2275 return new_bb;
2278 /* Mark all calls that can have a transaction restart. */
2280 static void
2281 mark_transaction_restart_calls (gimple stmt)
2283 struct tm_restart_node dummy;
2284 tm_restart_node **slot;
2286 if (!cfun->gimple_df->tm_restart)
2287 return;
2289 dummy.stmt = stmt;
2290 slot = cfun->gimple_df->tm_restart->find_slot (&dummy, NO_INSERT);
2291 if (slot)
2293 struct tm_restart_node *n = *slot;
2294 tree list = n->label_or_list;
2295 rtx_insn *insn;
2297 for (insn = next_real_insn (get_last_insn ());
2298 !CALL_P (insn);
2299 insn = next_real_insn (insn))
2300 continue;
2302 if (TREE_CODE (list) == LABEL_DECL)
2303 add_reg_note (insn, REG_TM, label_rtx (list));
2304 else
2305 for (; list ; list = TREE_CHAIN (list))
2306 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2310 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2311 statement STMT. */
2313 static void
2314 expand_call_stmt (gcall *stmt)
2316 tree exp, decl, lhs;
2317 bool builtin_p;
2318 size_t i;
2320 if (gimple_call_internal_p (stmt))
2322 expand_internal_call (stmt);
2323 return;
2326 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2328 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2329 decl = gimple_call_fndecl (stmt);
2330 builtin_p = decl && DECL_BUILT_IN (decl);
2332 /* If this is not a builtin function, the function type through which the
2333 call is made may be different from the type of the function. */
2334 if (!builtin_p)
2335 CALL_EXPR_FN (exp)
2336 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2337 CALL_EXPR_FN (exp));
2339 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2340 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2342 for (i = 0; i < gimple_call_num_args (stmt); i++)
2344 tree arg = gimple_call_arg (stmt, i);
2345 gimple def;
2346 /* TER addresses into arguments of builtin functions so we have a
2347 chance to infer more correct alignment information. See PR39954. */
2348 if (builtin_p
2349 && TREE_CODE (arg) == SSA_NAME
2350 && (def = get_gimple_for_ssa_name (arg))
2351 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2352 arg = gimple_assign_rhs1 (def);
2353 CALL_EXPR_ARG (exp, i) = arg;
2356 if (gimple_has_side_effects (stmt))
2357 TREE_SIDE_EFFECTS (exp) = 1;
2359 if (gimple_call_nothrow_p (stmt))
2360 TREE_NOTHROW (exp) = 1;
2362 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2363 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2364 if (decl
2365 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2366 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2367 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2368 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2369 else
2370 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2371 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2372 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2373 CALL_WITH_BOUNDS_P (exp) = gimple_call_with_bounds_p (stmt);
2375 /* Ensure RTL is created for debug args. */
2376 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2378 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2379 unsigned int ix;
2380 tree dtemp;
2382 if (debug_args)
2383 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2385 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2386 expand_debug_expr (dtemp);
2390 lhs = gimple_call_lhs (stmt);
2391 if (lhs)
2392 expand_assignment (lhs, exp, false);
2393 else
2394 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2396 mark_transaction_restart_calls (stmt);
2400 /* Generate RTL for an asm statement (explicit assembler code).
2401 STRING is a STRING_CST node containing the assembler code text,
2402 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
2403 insn is volatile; don't optimize it. */
2405 static void
2406 expand_asm_loc (tree string, int vol, location_t locus)
2408 rtx body;
2410 if (TREE_CODE (string) == ADDR_EXPR)
2411 string = TREE_OPERAND (string, 0);
2413 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2414 ggc_strdup (TREE_STRING_POINTER (string)),
2415 locus);
2417 MEM_VOLATILE_P (body) = vol;
2419 emit_insn (body);
2422 /* Return the number of times character C occurs in string S. */
2423 static int
2424 n_occurrences (int c, const char *s)
2426 int n = 0;
2427 while (*s)
2428 n += (*s++ == c);
2429 return n;
2432 /* A subroutine of expand_asm_operands. Check that all operands have
2433 the same number of alternatives. Return true if so. */
2435 static bool
2436 check_operand_nalternatives (const vec<const char *> &constraints)
2438 unsigned len = constraints.length();
2439 if (len > 0)
2441 int nalternatives = n_occurrences (',', constraints[0]);
2443 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2445 error ("too many alternatives in %<asm%>");
2446 return false;
2449 for (unsigned i = 1; i < len; ++i)
2450 if (n_occurrences (',', constraints[i]) != nalternatives)
2452 error ("operand constraints for %<asm%> differ "
2453 "in number of alternatives");
2454 return false;
2457 return true;
2460 /* Check for overlap between registers marked in CLOBBERED_REGS and
2461 anything inappropriate in T. Emit error and return the register
2462 variable definition for error, NULL_TREE for ok. */
2464 static bool
2465 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
2467 /* Conflicts between asm-declared register variables and the clobber
2468 list are not allowed. */
2469 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2471 if (overlap)
2473 error ("asm-specifier for variable %qE conflicts with asm clobber list",
2474 DECL_NAME (overlap));
2476 /* Reset registerness to stop multiple errors emitted for a single
2477 variable. */
2478 DECL_REGISTER (overlap) = 0;
2479 return true;
2482 return false;
2485 /* Generate RTL for an asm statement with arguments.
2486 STRING is the instruction template.
2487 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
2488 Each output or input has an expression in the TREE_VALUE and
2489 a tree list in TREE_PURPOSE which in turn contains a constraint
2490 name in TREE_VALUE (or NULL_TREE) and a constraint string
2491 in TREE_PURPOSE.
2492 CLOBBERS is a list of STRING_CST nodes each naming a hard register
2493 that is clobbered by this insn.
2495 LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
2496 should be the fallthru basic block of the asm goto.
2498 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
2499 Some elements of OUTPUTS may be replaced with trees representing temporary
2500 values. The caller should copy those temporary values to the originally
2501 specified lvalues.
2503 VOL nonzero means the insn is volatile; don't optimize it. */
2505 static void
2506 expand_asm_stmt (gasm *stmt)
2508 class save_input_location
2510 location_t old;
2512 public:
2513 explicit save_input_location(location_t where)
2515 old = input_location;
2516 input_location = where;
2519 ~save_input_location()
2521 input_location = old;
2525 location_t locus = gimple_location (stmt);
2527 if (gimple_asm_input_p (stmt))
2529 const char *s = gimple_asm_string (stmt);
2530 tree string = build_string (strlen (s), s);
2531 expand_asm_loc (string, gimple_asm_volatile_p (stmt), locus);
2532 return;
2535 /* There are some legacy diagnostics in here, and also avoids a
2536 sixth parameger to targetm.md_asm_adjust. */
2537 save_input_location s_i_l(locus);
2539 unsigned noutputs = gimple_asm_noutputs (stmt);
2540 unsigned ninputs = gimple_asm_ninputs (stmt);
2541 unsigned nlabels = gimple_asm_nlabels (stmt);
2542 unsigned i;
2544 /* ??? Diagnose during gimplification? */
2545 if (ninputs + noutputs + nlabels > MAX_RECOG_OPERANDS)
2547 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
2548 return;
2551 auto_vec<tree, MAX_RECOG_OPERANDS> output_tvec;
2552 auto_vec<tree, MAX_RECOG_OPERANDS> input_tvec;
2553 auto_vec<const char *, MAX_RECOG_OPERANDS> constraints;
2555 /* Copy the gimple vectors into new vectors that we can manipulate. */
2557 output_tvec.safe_grow (noutputs);
2558 input_tvec.safe_grow (ninputs);
2559 constraints.safe_grow (noutputs + ninputs);
2561 for (i = 0; i < noutputs; ++i)
2563 tree t = gimple_asm_output_op (stmt, i);
2564 output_tvec[i] = TREE_VALUE (t);
2565 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2567 for (i = 0; i < ninputs; i++)
2569 tree t = gimple_asm_input_op (stmt, i);
2570 input_tvec[i] = TREE_VALUE (t);
2571 constraints[i + noutputs]
2572 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2575 /* ??? Diagnose during gimplification? */
2576 if (! check_operand_nalternatives (constraints))
2577 return;
2579 /* Count the number of meaningful clobbered registers, ignoring what
2580 we would ignore later. */
2581 auto_vec<rtx> clobber_rvec;
2582 HARD_REG_SET clobbered_regs;
2583 CLEAR_HARD_REG_SET (clobbered_regs);
2585 if (unsigned n = gimple_asm_nclobbers (stmt))
2587 clobber_rvec.reserve (n);
2588 for (i = 0; i < n; i++)
2590 tree t = gimple_asm_clobber_op (stmt, i);
2591 const char *regname = TREE_STRING_POINTER (TREE_VALUE (t));
2592 int nregs, j;
2594 j = decode_reg_name_and_count (regname, &nregs);
2595 if (j < 0)
2597 if (j == -2)
2599 /* ??? Diagnose during gimplification? */
2600 error ("unknown register name %qs in %<asm%>", regname);
2602 else if (j == -4)
2604 rtx x = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2605 clobber_rvec.safe_push (x);
2607 else
2609 /* Otherwise we should have -1 == empty string
2610 or -3 == cc, which is not a register. */
2611 gcc_assert (j == -1 || j == -3);
2614 else
2615 for (int reg = j; reg < j + nregs; reg++)
2617 /* Clobbering the PIC register is an error. */
2618 if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
2620 /* ??? Diagnose during gimplification? */
2621 error ("PIC register clobbered by %qs in %<asm%>",
2622 regname);
2623 return;
2626 SET_HARD_REG_BIT (clobbered_regs, reg);
2627 rtx x = gen_rtx_REG (reg_raw_mode[reg], reg);
2628 clobber_rvec.safe_push (x);
2632 unsigned nclobbers = clobber_rvec.length();
2634 /* First pass over inputs and outputs checks validity and sets
2635 mark_addressable if needed. */
2636 /* ??? Diagnose during gimplification? */
2638 for (i = 0; i < noutputs; ++i)
2640 tree val = output_tvec[i];
2641 tree type = TREE_TYPE (val);
2642 const char *constraint;
2643 bool is_inout;
2644 bool allows_reg;
2645 bool allows_mem;
2647 /* Try to parse the output constraint. If that fails, there's
2648 no point in going further. */
2649 constraint = constraints[i];
2650 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
2651 &allows_mem, &allows_reg, &is_inout))
2652 return;
2654 if (! allows_reg
2655 && (allows_mem
2656 || is_inout
2657 || (DECL_P (val)
2658 && REG_P (DECL_RTL (val))
2659 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
2660 mark_addressable (val);
2663 for (i = 0; i < ninputs; ++i)
2665 bool allows_reg, allows_mem;
2666 const char *constraint;
2668 constraint = constraints[i + noutputs];
2669 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2670 constraints.address (),
2671 &allows_mem, &allows_reg))
2672 return;
2674 if (! allows_reg && allows_mem)
2675 mark_addressable (input_tvec[i]);
2678 /* Second pass evaluates arguments. */
2680 /* Make sure stack is consistent for asm goto. */
2681 if (nlabels > 0)
2682 do_pending_stack_adjust ();
2683 int old_generating_concat_p = generating_concat_p;
2685 /* Vector of RTX's of evaluated output operands. */
2686 auto_vec<rtx, MAX_RECOG_OPERANDS> output_rvec;
2687 auto_vec<int, MAX_RECOG_OPERANDS> inout_opnum;
2688 rtx_insn *after_rtl_seq = NULL, *after_rtl_end = NULL;
2690 output_rvec.safe_grow (noutputs);
2692 for (i = 0; i < noutputs; ++i)
2694 tree val = output_tvec[i];
2695 tree type = TREE_TYPE (val);
2696 bool is_inout, allows_reg, allows_mem, ok;
2697 rtx op;
2699 ok = parse_output_constraint (&constraints[i], i, ninputs,
2700 noutputs, &allows_mem, &allows_reg,
2701 &is_inout);
2702 gcc_assert (ok);
2704 /* If an output operand is not a decl or indirect ref and our constraint
2705 allows a register, make a temporary to act as an intermediate.
2706 Make the asm insn write into that, then we will copy it to
2707 the real output operand. Likewise for promoted variables. */
2709 generating_concat_p = 0;
2711 if ((TREE_CODE (val) == INDIRECT_REF
2712 && allows_mem)
2713 || (DECL_P (val)
2714 && (allows_mem || REG_P (DECL_RTL (val)))
2715 && ! (REG_P (DECL_RTL (val))
2716 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
2717 || ! allows_reg
2718 || is_inout)
2720 op = expand_expr (val, NULL_RTX, VOIDmode,
2721 !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
2722 if (MEM_P (op))
2723 op = validize_mem (op);
2725 if (! allows_reg && !MEM_P (op))
2726 error ("output number %d not directly addressable", i);
2727 if ((! allows_mem && MEM_P (op))
2728 || GET_CODE (op) == CONCAT)
2730 rtx old_op = op;
2731 op = gen_reg_rtx (GET_MODE (op));
2733 generating_concat_p = old_generating_concat_p;
2735 if (is_inout)
2736 emit_move_insn (op, old_op);
2738 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2739 emit_move_insn (old_op, op);
2740 after_rtl_seq = get_insns ();
2741 after_rtl_end = get_last_insn ();
2742 end_sequence ();
2745 else
2747 op = assign_temp (type, 0, 1);
2748 op = validize_mem (op);
2749 if (!MEM_P (op) && TREE_CODE (val) == SSA_NAME)
2750 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (val), op);
2752 generating_concat_p = old_generating_concat_p;
2754 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2755 expand_assignment (val, make_tree (type, op), false);
2756 after_rtl_seq = get_insns ();
2757 after_rtl_end = get_last_insn ();
2758 end_sequence ();
2760 output_rvec[i] = op;
2762 if (is_inout)
2763 inout_opnum.safe_push (i);
2766 auto_vec<rtx, MAX_RECOG_OPERANDS> input_rvec;
2767 auto_vec<machine_mode, MAX_RECOG_OPERANDS> input_mode;
2769 input_rvec.safe_grow (ninputs);
2770 input_mode.safe_grow (ninputs);
2772 generating_concat_p = 0;
2774 for (i = 0; i < ninputs; ++i)
2776 tree val = input_tvec[i];
2777 tree type = TREE_TYPE (val);
2778 bool allows_reg, allows_mem, ok;
2779 const char *constraint;
2780 rtx op;
2782 constraint = constraints[i + noutputs];
2783 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2784 constraints.address (),
2785 &allows_mem, &allows_reg);
2786 gcc_assert (ok);
2788 /* EXPAND_INITIALIZER will not generate code for valid initializer
2789 constants, but will still generate code for other types of operand.
2790 This is the behavior we want for constant constraints. */
2791 op = expand_expr (val, NULL_RTX, VOIDmode,
2792 allows_reg ? EXPAND_NORMAL
2793 : allows_mem ? EXPAND_MEMORY
2794 : EXPAND_INITIALIZER);
2796 /* Never pass a CONCAT to an ASM. */
2797 if (GET_CODE (op) == CONCAT)
2798 op = force_reg (GET_MODE (op), op);
2799 else if (MEM_P (op))
2800 op = validize_mem (op);
2802 if (asm_operand_ok (op, constraint, NULL) <= 0)
2804 if (allows_reg && TYPE_MODE (type) != BLKmode)
2805 op = force_reg (TYPE_MODE (type), op);
2806 else if (!allows_mem)
2807 warning (0, "asm operand %d probably doesn%'t match constraints",
2808 i + noutputs);
2809 else if (MEM_P (op))
2811 /* We won't recognize either volatile memory or memory
2812 with a queued address as available a memory_operand
2813 at this point. Ignore it: clearly this *is* a memory. */
2815 else
2816 gcc_unreachable ();
2818 input_rvec[i] = op;
2819 input_mode[i] = TYPE_MODE (type);
2822 /* For in-out operands, copy output rtx to input rtx. */
2823 unsigned ninout = inout_opnum.length();
2824 for (i = 0; i < ninout; i++)
2826 int j = inout_opnum[i];
2827 rtx o = output_rvec[j];
2829 input_rvec.safe_push (o);
2830 input_mode.safe_push (GET_MODE (o));
2832 char buffer[16];
2833 sprintf (buffer, "%d", j);
2834 constraints.safe_push (ggc_strdup (buffer));
2836 ninputs += ninout;
2838 /* Sometimes we wish to automatically clobber registers across an asm.
2839 Case in point is when the i386 backend moved from cc0 to a hard reg --
2840 maintaining source-level compatibility means automatically clobbering
2841 the flags register. */
2842 rtx_insn *after_md_seq = NULL;
2843 if (targetm.md_asm_adjust)
2844 after_md_seq = targetm.md_asm_adjust (output_rvec, input_rvec,
2845 constraints, clobber_rvec,
2846 clobbered_regs);
2848 /* Do not allow the hook to change the output and input count,
2849 lest it mess up the operand numbering. */
2850 gcc_assert (output_rvec.length() == noutputs);
2851 gcc_assert (input_rvec.length() == ninputs);
2852 gcc_assert (constraints.length() == noutputs + ninputs);
2854 /* But it certainly can adjust the clobbers. */
2855 nclobbers = clobber_rvec.length();
2857 /* Third pass checks for easy conflicts. */
2858 /* ??? Why are we doing this on trees instead of rtx. */
2860 bool clobber_conflict_found = 0;
2861 for (i = 0; i < noutputs; ++i)
2862 if (tree_conflicts_with_clobbers_p (output_tvec[i], &clobbered_regs))
2863 clobber_conflict_found = 1;
2864 for (i = 0; i < ninputs - ninout; ++i)
2865 if (tree_conflicts_with_clobbers_p (input_tvec[i], &clobbered_regs))
2866 clobber_conflict_found = 1;
2868 /* Make vectors for the expression-rtx, constraint strings,
2869 and named operands. */
2871 rtvec argvec = rtvec_alloc (ninputs);
2872 rtvec constraintvec = rtvec_alloc (ninputs);
2873 rtvec labelvec = rtvec_alloc (nlabels);
2875 rtx body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
2876 : GET_MODE (output_rvec[0])),
2877 ggc_strdup (gimple_asm_string (stmt)),
2878 empty_string, 0, argvec, constraintvec,
2879 labelvec, locus);
2880 MEM_VOLATILE_P (body) = gimple_asm_volatile_p (stmt);
2882 for (i = 0; i < ninputs; ++i)
2884 ASM_OPERANDS_INPUT (body, i) = input_rvec[i];
2885 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
2886 = gen_rtx_ASM_INPUT_loc (input_mode[i],
2887 constraints[i + noutputs],
2888 locus);
2891 /* Copy labels to the vector. */
2892 rtx_code_label *fallthru_label = NULL;
2893 if (nlabels > 0)
2895 basic_block fallthru_bb = NULL;
2896 edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
2897 if (fallthru)
2898 fallthru_bb = fallthru->dest;
2900 for (i = 0; i < nlabels; ++i)
2902 tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
2903 rtx_insn *r;
2904 /* If asm goto has any labels in the fallthru basic block, use
2905 a label that we emit immediately after the asm goto. Expansion
2906 may insert further instructions into the same basic block after
2907 asm goto and if we don't do this, insertion of instructions on
2908 the fallthru edge might misbehave. See PR58670. */
2909 if (fallthru_bb && label_to_block_fn (cfun, label) == fallthru_bb)
2911 if (fallthru_label == NULL_RTX)
2912 fallthru_label = gen_label_rtx ();
2913 r = fallthru_label;
2915 else
2916 r = label_rtx (label);
2917 ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
2921 /* Now, for each output, construct an rtx
2922 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
2923 ARGVEC CONSTRAINTS OPNAMES))
2924 If there is more than one, put them inside a PARALLEL. */
2926 if (nlabels > 0 && nclobbers == 0)
2928 gcc_assert (noutputs == 0);
2929 emit_jump_insn (body);
2931 else if (noutputs == 0 && nclobbers == 0)
2933 /* No output operands: put in a raw ASM_OPERANDS rtx. */
2934 emit_insn (body);
2936 else if (noutputs == 1 && nclobbers == 0)
2938 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
2939 emit_insn (gen_rtx_SET (output_rvec[0], body));
2941 else
2943 rtx obody = body;
2944 int num = noutputs;
2946 if (num == 0)
2947 num = 1;
2949 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
2951 /* For each output operand, store a SET. */
2952 for (i = 0; i < noutputs; ++i)
2954 rtx src, o = output_rvec[i];
2955 if (i == 0)
2957 ASM_OPERANDS_OUTPUT_CONSTRAINT (obody) = constraints[0];
2958 src = obody;
2960 else
2962 src = gen_rtx_ASM_OPERANDS (GET_MODE (o),
2963 ASM_OPERANDS_TEMPLATE (obody),
2964 constraints[i], i, argvec,
2965 constraintvec, labelvec, locus);
2966 MEM_VOLATILE_P (src) = gimple_asm_volatile_p (stmt);
2968 XVECEXP (body, 0, i) = gen_rtx_SET (o, src);
2971 /* If there are no outputs (but there are some clobbers)
2972 store the bare ASM_OPERANDS into the PARALLEL. */
2973 if (i == 0)
2974 XVECEXP (body, 0, i++) = obody;
2976 /* Store (clobber REG) for each clobbered register specified. */
2977 for (unsigned j = 0; j < nclobbers; ++j)
2979 rtx clobbered_reg = clobber_rvec[j];
2981 /* Do sanity check for overlap between clobbers and respectively
2982 input and outputs that hasn't been handled. Such overlap
2983 should have been detected and reported above. */
2984 if (!clobber_conflict_found && REG_P (clobbered_reg))
2986 /* We test the old body (obody) contents to avoid
2987 tripping over the under-construction body. */
2988 for (unsigned k = 0; k < noutputs; ++k)
2989 if (reg_overlap_mentioned_p (clobbered_reg, output_rvec[k]))
2990 internal_error ("asm clobber conflict with output operand");
2992 for (unsigned k = 0; k < ninputs - ninout; ++k)
2993 if (reg_overlap_mentioned_p (clobbered_reg, input_rvec[k]))
2994 internal_error ("asm clobber conflict with input operand");
2997 XVECEXP (body, 0, i++) = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
3000 if (nlabels > 0)
3001 emit_jump_insn (body);
3002 else
3003 emit_insn (body);
3006 generating_concat_p = old_generating_concat_p;
3008 if (fallthru_label)
3009 emit_label (fallthru_label);
3011 if (after_md_seq)
3012 emit_insn (after_md_seq);
3013 if (after_rtl_seq)
3014 emit_insn (after_rtl_seq);
3016 free_temp_slots ();
3017 crtl->has_asm_statement = 1;
3020 /* Emit code to jump to the address
3021 specified by the pointer expression EXP. */
3023 static void
3024 expand_computed_goto (tree exp)
3026 rtx x = expand_normal (exp);
3028 do_pending_stack_adjust ();
3029 emit_indirect_jump (x);
3032 /* Generate RTL code for a `goto' statement with target label LABEL.
3033 LABEL should be a LABEL_DECL tree node that was or will later be
3034 defined with `expand_label'. */
3036 static void
3037 expand_goto (tree label)
3039 #ifdef ENABLE_CHECKING
3040 /* Check for a nonlocal goto to a containing function. Should have
3041 gotten translated to __builtin_nonlocal_goto. */
3042 tree context = decl_function_context (label);
3043 gcc_assert (!context || context == current_function_decl);
3044 #endif
3046 emit_jump (jump_target_rtx (label));
3049 /* Output a return with no value. */
3051 static void
3052 expand_null_return_1 (void)
3054 clear_pending_stack_adjust ();
3055 do_pending_stack_adjust ();
3056 emit_jump (return_label);
3059 /* Generate RTL to return from the current function, with no value.
3060 (That is, we do not do anything about returning any value.) */
3062 void
3063 expand_null_return (void)
3065 /* If this function was declared to return a value, but we
3066 didn't, clobber the return registers so that they are not
3067 propagated live to the rest of the function. */
3068 clobber_return_register ();
3070 expand_null_return_1 ();
3073 /* Generate RTL to return from the current function, with value VAL. */
3075 static void
3076 expand_value_return (rtx val)
3078 /* Copy the value to the return location unless it's already there. */
3080 tree decl = DECL_RESULT (current_function_decl);
3081 rtx return_reg = DECL_RTL (decl);
3082 if (return_reg != val)
3084 tree funtype = TREE_TYPE (current_function_decl);
3085 tree type = TREE_TYPE (decl);
3086 int unsignedp = TYPE_UNSIGNED (type);
3087 machine_mode old_mode = DECL_MODE (decl);
3088 machine_mode mode;
3089 if (DECL_BY_REFERENCE (decl))
3090 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3091 else
3092 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3094 if (mode != old_mode)
3095 val = convert_modes (mode, old_mode, val, unsignedp);
3097 if (GET_CODE (return_reg) == PARALLEL)
3098 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3099 else
3100 emit_move_insn (return_reg, val);
3103 expand_null_return_1 ();
3106 /* Generate RTL to evaluate the expression RETVAL and return it
3107 from the current function. */
3109 static void
3110 expand_return (tree retval, tree bounds)
3112 rtx result_rtl;
3113 rtx val = 0;
3114 tree retval_rhs;
3115 rtx bounds_rtl;
3117 /* If function wants no value, give it none. */
3118 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3120 expand_normal (retval);
3121 expand_null_return ();
3122 return;
3125 if (retval == error_mark_node)
3127 /* Treat this like a return of no value from a function that
3128 returns a value. */
3129 expand_null_return ();
3130 return;
3132 else if ((TREE_CODE (retval) == MODIFY_EXPR
3133 || TREE_CODE (retval) == INIT_EXPR)
3134 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3135 retval_rhs = TREE_OPERAND (retval, 1);
3136 else
3137 retval_rhs = retval;
3139 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3141 /* Put returned bounds to the right place. */
3142 bounds_rtl = DECL_BOUNDS_RTL (DECL_RESULT (current_function_decl));
3143 if (bounds_rtl)
3145 rtx addr, bnd;
3147 if (bounds)
3149 bnd = expand_normal (bounds);
3150 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3152 else if (REG_P (bounds_rtl))
3154 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3155 addr = gen_rtx_MEM (Pmode, addr);
3156 bnd = targetm.calls.load_bounds_for_arg (addr, NULL, NULL);
3157 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3159 else
3161 int n;
3163 gcc_assert (GET_CODE (bounds_rtl) == PARALLEL);
3165 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3166 addr = gen_rtx_MEM (Pmode, addr);
3168 for (n = 0; n < XVECLEN (bounds_rtl, 0); n++)
3170 rtx offs = XEXP (XVECEXP (bounds_rtl, 0, n), 1);
3171 rtx slot = XEXP (XVECEXP (bounds_rtl, 0, n), 0);
3172 rtx from = adjust_address (addr, Pmode, INTVAL (offs));
3173 rtx bnd = targetm.calls.load_bounds_for_arg (from, NULL, NULL);
3174 targetm.calls.store_returned_bounds (slot, bnd);
3178 else if (chkp_function_instrumented_p (current_function_decl)
3179 && !BOUNDED_P (retval_rhs)
3180 && chkp_type_has_pointer (TREE_TYPE (retval_rhs))
3181 && TREE_CODE (retval_rhs) != RESULT_DECL)
3183 rtx addr = expand_normal (build_fold_addr_expr (retval_rhs));
3184 addr = gen_rtx_MEM (Pmode, addr);
3186 gcc_assert (MEM_P (result_rtl));
3188 chkp_copy_bounds_for_stack_parm (result_rtl, addr, TREE_TYPE (retval_rhs));
3191 /* If we are returning the RESULT_DECL, then the value has already
3192 been stored into it, so we don't have to do anything special. */
3193 if (TREE_CODE (retval_rhs) == RESULT_DECL)
3194 expand_value_return (result_rtl);
3196 /* If the result is an aggregate that is being returned in one (or more)
3197 registers, load the registers here. */
3199 else if (retval_rhs != 0
3200 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3201 && REG_P (result_rtl))
3203 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3204 if (val)
3206 /* Use the mode of the result value on the return register. */
3207 PUT_MODE (result_rtl, GET_MODE (val));
3208 expand_value_return (val);
3210 else
3211 expand_null_return ();
3213 else if (retval_rhs != 0
3214 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3215 && (REG_P (result_rtl)
3216 || (GET_CODE (result_rtl) == PARALLEL)))
3218 /* Compute the return value into a temporary (usually a pseudo reg). */
3220 = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3221 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3222 val = force_not_mem (val);
3223 expand_value_return (val);
3225 else
3227 /* No hard reg used; calculate value into hard return reg. */
3228 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3229 expand_value_return (result_rtl);
3233 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3234 STMT that doesn't require special handling for outgoing edges. That
3235 is no tailcalls and no GIMPLE_COND. */
3237 static void
3238 expand_gimple_stmt_1 (gimple stmt)
3240 tree op0;
3242 set_curr_insn_location (gimple_location (stmt));
3244 switch (gimple_code (stmt))
3246 case GIMPLE_GOTO:
3247 op0 = gimple_goto_dest (stmt);
3248 if (TREE_CODE (op0) == LABEL_DECL)
3249 expand_goto (op0);
3250 else
3251 expand_computed_goto (op0);
3252 break;
3253 case GIMPLE_LABEL:
3254 expand_label (gimple_label_label (as_a <glabel *> (stmt)));
3255 break;
3256 case GIMPLE_NOP:
3257 case GIMPLE_PREDICT:
3258 break;
3259 case GIMPLE_SWITCH:
3260 expand_case (as_a <gswitch *> (stmt));
3261 break;
3262 case GIMPLE_ASM:
3263 expand_asm_stmt (as_a <gasm *> (stmt));
3264 break;
3265 case GIMPLE_CALL:
3266 expand_call_stmt (as_a <gcall *> (stmt));
3267 break;
3269 case GIMPLE_RETURN:
3270 op0 = gimple_return_retval (as_a <greturn *> (stmt));
3272 if (op0 && op0 != error_mark_node)
3274 tree result = DECL_RESULT (current_function_decl);
3276 /* If we are not returning the current function's RESULT_DECL,
3277 build an assignment to it. */
3278 if (op0 != result)
3280 /* I believe that a function's RESULT_DECL is unique. */
3281 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3283 /* ??? We'd like to use simply expand_assignment here,
3284 but this fails if the value is of BLKmode but the return
3285 decl is a register. expand_return has special handling
3286 for this combination, which eventually should move
3287 to common code. See comments there. Until then, let's
3288 build a modify expression :-/ */
3289 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3290 result, op0);
3293 if (!op0)
3294 expand_null_return ();
3295 else
3296 expand_return (op0, gimple_return_retbnd (stmt));
3297 break;
3299 case GIMPLE_ASSIGN:
3301 gassign *assign_stmt = as_a <gassign *> (stmt);
3302 tree lhs = gimple_assign_lhs (assign_stmt);
3304 /* Tree expand used to fiddle with |= and &= of two bitfield
3305 COMPONENT_REFs here. This can't happen with gimple, the LHS
3306 of binary assigns must be a gimple reg. */
3308 if (TREE_CODE (lhs) != SSA_NAME
3309 || get_gimple_rhs_class (gimple_expr_code (stmt))
3310 == GIMPLE_SINGLE_RHS)
3312 tree rhs = gimple_assign_rhs1 (assign_stmt);
3313 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
3314 == GIMPLE_SINGLE_RHS);
3315 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
3316 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3317 if (TREE_CLOBBER_P (rhs))
3318 /* This is a clobber to mark the going out of scope for
3319 this LHS. */
3321 else
3322 expand_assignment (lhs, rhs,
3323 gimple_assign_nontemporal_move_p (
3324 assign_stmt));
3326 else
3328 rtx target, temp;
3329 bool nontemporal = gimple_assign_nontemporal_move_p (assign_stmt);
3330 struct separate_ops ops;
3331 bool promoted = false;
3333 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3334 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
3335 promoted = true;
3337 ops.code = gimple_assign_rhs_code (assign_stmt);
3338 ops.type = TREE_TYPE (lhs);
3339 switch (get_gimple_rhs_class (ops.code))
3341 case GIMPLE_TERNARY_RHS:
3342 ops.op2 = gimple_assign_rhs3 (assign_stmt);
3343 /* Fallthru */
3344 case GIMPLE_BINARY_RHS:
3345 ops.op1 = gimple_assign_rhs2 (assign_stmt);
3346 /* Fallthru */
3347 case GIMPLE_UNARY_RHS:
3348 ops.op0 = gimple_assign_rhs1 (assign_stmt);
3349 break;
3350 default:
3351 gcc_unreachable ();
3353 ops.location = gimple_location (stmt);
3355 /* If we want to use a nontemporal store, force the value to
3356 register first. If we store into a promoted register,
3357 don't directly expand to target. */
3358 temp = nontemporal || promoted ? NULL_RTX : target;
3359 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
3360 EXPAND_NORMAL);
3362 if (temp == target)
3364 else if (promoted)
3366 int unsignedp = SUBREG_PROMOTED_SIGN (target);
3367 /* If TEMP is a VOIDmode constant, use convert_modes to make
3368 sure that we properly convert it. */
3369 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
3371 temp = convert_modes (GET_MODE (target),
3372 TYPE_MODE (ops.type),
3373 temp, unsignedp);
3374 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
3375 GET_MODE (target), temp, unsignedp);
3378 convert_move (SUBREG_REG (target), temp, unsignedp);
3380 else if (nontemporal && emit_storent_insn (target, temp))
3382 else
3384 temp = force_operand (temp, target);
3385 if (temp != target)
3386 emit_move_insn (target, temp);
3390 break;
3392 default:
3393 gcc_unreachable ();
3397 /* Expand one gimple statement STMT and return the last RTL instruction
3398 before any of the newly generated ones.
3400 In addition to generating the necessary RTL instructions this also
3401 sets REG_EH_REGION notes if necessary and sets the current source
3402 location for diagnostics. */
3404 static rtx_insn *
3405 expand_gimple_stmt (gimple stmt)
3407 location_t saved_location = input_location;
3408 rtx_insn *last = get_last_insn ();
3409 int lp_nr;
3411 gcc_assert (cfun);
3413 /* We need to save and restore the current source location so that errors
3414 discovered during expansion are emitted with the right location. But
3415 it would be better if the diagnostic routines used the source location
3416 embedded in the tree nodes rather than globals. */
3417 if (gimple_has_location (stmt))
3418 input_location = gimple_location (stmt);
3420 expand_gimple_stmt_1 (stmt);
3422 /* Free any temporaries used to evaluate this statement. */
3423 free_temp_slots ();
3425 input_location = saved_location;
3427 /* Mark all insns that may trap. */
3428 lp_nr = lookup_stmt_eh_lp (stmt);
3429 if (lp_nr)
3431 rtx_insn *insn;
3432 for (insn = next_real_insn (last); insn;
3433 insn = next_real_insn (insn))
3435 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
3436 /* If we want exceptions for non-call insns, any
3437 may_trap_p instruction may throw. */
3438 && GET_CODE (PATTERN (insn)) != CLOBBER
3439 && GET_CODE (PATTERN (insn)) != USE
3440 && insn_could_throw_p (insn))
3441 make_reg_eh_region_note (insn, 0, lp_nr);
3445 return last;
3448 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
3449 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
3450 generated a tail call (something that might be denied by the ABI
3451 rules governing the call; see calls.c).
3453 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
3454 can still reach the rest of BB. The case here is __builtin_sqrt,
3455 where the NaN result goes through the external function (with a
3456 tailcall) and the normal result happens via a sqrt instruction. */
3458 static basic_block
3459 expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
3461 rtx_insn *last2, *last;
3462 edge e;
3463 edge_iterator ei;
3464 int probability;
3465 gcov_type count;
3467 last2 = last = expand_gimple_stmt (stmt);
3469 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
3470 if (CALL_P (last) && SIBLING_CALL_P (last))
3471 goto found;
3473 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3475 *can_fallthru = true;
3476 return NULL;
3478 found:
3479 /* ??? Wouldn't it be better to just reset any pending stack adjust?
3480 Any instructions emitted here are about to be deleted. */
3481 do_pending_stack_adjust ();
3483 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
3484 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
3485 EH or abnormal edges, we shouldn't have created a tail call in
3486 the first place. So it seems to me we should just be removing
3487 all edges here, or redirecting the existing fallthru edge to
3488 the exit block. */
3490 probability = 0;
3491 count = 0;
3493 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3495 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
3497 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3499 e->dest->count -= e->count;
3500 e->dest->frequency -= EDGE_FREQUENCY (e);
3501 if (e->dest->count < 0)
3502 e->dest->count = 0;
3503 if (e->dest->frequency < 0)
3504 e->dest->frequency = 0;
3506 count += e->count;
3507 probability += e->probability;
3508 remove_edge (e);
3510 else
3511 ei_next (&ei);
3514 /* This is somewhat ugly: the call_expr expander often emits instructions
3515 after the sibcall (to perform the function return). These confuse the
3516 find_many_sub_basic_blocks code, so we need to get rid of these. */
3517 last = NEXT_INSN (last);
3518 gcc_assert (BARRIER_P (last));
3520 *can_fallthru = false;
3521 while (NEXT_INSN (last))
3523 /* For instance an sqrt builtin expander expands if with
3524 sibcall in the then and label for `else`. */
3525 if (LABEL_P (NEXT_INSN (last)))
3527 *can_fallthru = true;
3528 break;
3530 delete_insn (NEXT_INSN (last));
3533 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
3534 | EDGE_SIBCALL);
3535 e->probability += probability;
3536 e->count += count;
3537 BB_END (bb) = last;
3538 update_bb_for_insn (bb);
3540 if (NEXT_INSN (last))
3542 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
3544 last = BB_END (bb);
3545 if (BARRIER_P (last))
3546 BB_END (bb) = PREV_INSN (last);
3549 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3551 return bb;
3554 /* Return the difference between the floor and the truncated result of
3555 a signed division by OP1 with remainder MOD. */
3556 static rtx
3557 floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3559 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
3560 return gen_rtx_IF_THEN_ELSE
3561 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3562 gen_rtx_IF_THEN_ELSE
3563 (mode, gen_rtx_LT (BImode,
3564 gen_rtx_DIV (mode, op1, mod),
3565 const0_rtx),
3566 constm1_rtx, const0_rtx),
3567 const0_rtx);
3570 /* Return the difference between the ceil and the truncated result of
3571 a signed division by OP1 with remainder MOD. */
3572 static rtx
3573 ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3575 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
3576 return gen_rtx_IF_THEN_ELSE
3577 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3578 gen_rtx_IF_THEN_ELSE
3579 (mode, gen_rtx_GT (BImode,
3580 gen_rtx_DIV (mode, op1, mod),
3581 const0_rtx),
3582 const1_rtx, const0_rtx),
3583 const0_rtx);
3586 /* Return the difference between the ceil and the truncated result of
3587 an unsigned division by OP1 with remainder MOD. */
3588 static rtx
3589 ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
3591 /* (mod != 0 ? 1 : 0) */
3592 return gen_rtx_IF_THEN_ELSE
3593 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3594 const1_rtx, const0_rtx);
3597 /* Return the difference between the rounded and the truncated result
3598 of a signed division by OP1 with remainder MOD. Halfway cases are
3599 rounded away from zero, rather than to the nearest even number. */
3600 static rtx
3601 round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3603 /* (abs (mod) >= abs (op1) - abs (mod)
3604 ? (op1 / mod > 0 ? 1 : -1)
3605 : 0) */
3606 return gen_rtx_IF_THEN_ELSE
3607 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
3608 gen_rtx_MINUS (mode,
3609 gen_rtx_ABS (mode, op1),
3610 gen_rtx_ABS (mode, mod))),
3611 gen_rtx_IF_THEN_ELSE
3612 (mode, gen_rtx_GT (BImode,
3613 gen_rtx_DIV (mode, op1, mod),
3614 const0_rtx),
3615 const1_rtx, constm1_rtx),
3616 const0_rtx);
3619 /* Return the difference between the rounded and the truncated result
3620 of a unsigned division by OP1 with remainder MOD. Halfway cases
3621 are rounded away from zero, rather than to the nearest even
3622 number. */
3623 static rtx
3624 round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
3626 /* (mod >= op1 - mod ? 1 : 0) */
3627 return gen_rtx_IF_THEN_ELSE
3628 (mode, gen_rtx_GE (BImode, mod,
3629 gen_rtx_MINUS (mode, op1, mod)),
3630 const1_rtx, const0_rtx);
3633 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
3634 any rtl. */
3636 static rtx
3637 convert_debug_memory_address (machine_mode mode, rtx x,
3638 addr_space_t as)
3640 machine_mode xmode = GET_MODE (x);
3642 #ifndef POINTERS_EXTEND_UNSIGNED
3643 gcc_assert (mode == Pmode
3644 || mode == targetm.addr_space.address_mode (as));
3645 gcc_assert (xmode == mode || xmode == VOIDmode);
3646 #else
3647 rtx temp;
3649 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
3651 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
3652 return x;
3654 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
3655 x = simplify_gen_subreg (mode, x, xmode,
3656 subreg_lowpart_offset
3657 (mode, xmode));
3658 else if (POINTERS_EXTEND_UNSIGNED > 0)
3659 x = gen_rtx_ZERO_EXTEND (mode, x);
3660 else if (!POINTERS_EXTEND_UNSIGNED)
3661 x = gen_rtx_SIGN_EXTEND (mode, x);
3662 else
3664 switch (GET_CODE (x))
3666 case SUBREG:
3667 if ((SUBREG_PROMOTED_VAR_P (x)
3668 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
3669 || (GET_CODE (SUBREG_REG (x)) == PLUS
3670 && REG_P (XEXP (SUBREG_REG (x), 0))
3671 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
3672 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
3673 && GET_MODE (SUBREG_REG (x)) == mode)
3674 return SUBREG_REG (x);
3675 break;
3676 case LABEL_REF:
3677 temp = gen_rtx_LABEL_REF (mode, LABEL_REF_LABEL (x));
3678 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
3679 return temp;
3680 case SYMBOL_REF:
3681 temp = shallow_copy_rtx (x);
3682 PUT_MODE (temp, mode);
3683 return temp;
3684 case CONST:
3685 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3686 if (temp)
3687 temp = gen_rtx_CONST (mode, temp);
3688 return temp;
3689 case PLUS:
3690 case MINUS:
3691 if (CONST_INT_P (XEXP (x, 1)))
3693 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3694 if (temp)
3695 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
3697 break;
3698 default:
3699 break;
3701 /* Don't know how to express ptr_extend as operation in debug info. */
3702 return NULL;
3704 #endif /* POINTERS_EXTEND_UNSIGNED */
3706 return x;
3709 /* Map from SSA_NAMEs to corresponding DEBUG_EXPR_DECLs created
3710 by avoid_deep_ter_for_debug. */
3712 static hash_map<tree, tree> *deep_ter_debug_map;
3714 /* Split too deep TER chains for debug stmts using debug temporaries. */
3716 static void
3717 avoid_deep_ter_for_debug (gimple stmt, int depth)
3719 use_operand_p use_p;
3720 ssa_op_iter iter;
3721 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
3723 tree use = USE_FROM_PTR (use_p);
3724 if (TREE_CODE (use) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (use))
3725 continue;
3726 gimple g = get_gimple_for_ssa_name (use);
3727 if (g == NULL)
3728 continue;
3729 if (depth > 6 && !stmt_ends_bb_p (g))
3731 if (deep_ter_debug_map == NULL)
3732 deep_ter_debug_map = new hash_map<tree, tree>;
3734 tree &vexpr = deep_ter_debug_map->get_or_insert (use);
3735 if (vexpr != NULL)
3736 continue;
3737 vexpr = make_node (DEBUG_EXPR_DECL);
3738 gimple def_temp = gimple_build_debug_bind (vexpr, use, g);
3739 DECL_ARTIFICIAL (vexpr) = 1;
3740 TREE_TYPE (vexpr) = TREE_TYPE (use);
3741 DECL_MODE (vexpr) = TYPE_MODE (TREE_TYPE (use));
3742 gimple_stmt_iterator gsi = gsi_for_stmt (g);
3743 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
3744 avoid_deep_ter_for_debug (def_temp, 0);
3746 else
3747 avoid_deep_ter_for_debug (g, depth + 1);
3751 /* Return an RTX equivalent to the value of the parameter DECL. */
3753 static rtx
3754 expand_debug_parm_decl (tree decl)
3756 rtx incoming = DECL_INCOMING_RTL (decl);
3758 if (incoming
3759 && GET_MODE (incoming) != BLKmode
3760 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
3761 || (MEM_P (incoming)
3762 && REG_P (XEXP (incoming, 0))
3763 && HARD_REGISTER_P (XEXP (incoming, 0)))))
3765 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
3767 #ifdef HAVE_window_save
3768 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
3769 If the target machine has an explicit window save instruction, the
3770 actual entry value is the corresponding OUTGOING_REGNO instead. */
3771 if (REG_P (incoming)
3772 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
3773 incoming
3774 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
3775 OUTGOING_REGNO (REGNO (incoming)), 0);
3776 else if (MEM_P (incoming))
3778 rtx reg = XEXP (incoming, 0);
3779 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
3781 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
3782 incoming = replace_equiv_address_nv (incoming, reg);
3784 else
3785 incoming = copy_rtx (incoming);
3787 #endif
3789 ENTRY_VALUE_EXP (rtl) = incoming;
3790 return rtl;
3793 if (incoming
3794 && GET_MODE (incoming) != BLKmode
3795 && !TREE_ADDRESSABLE (decl)
3796 && MEM_P (incoming)
3797 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
3798 || (GET_CODE (XEXP (incoming, 0)) == PLUS
3799 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
3800 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
3801 return copy_rtx (incoming);
3803 return NULL_RTX;
3806 /* Return an RTX equivalent to the value of the tree expression EXP. */
3808 static rtx
3809 expand_debug_expr (tree exp)
3811 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
3812 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3813 machine_mode inner_mode = VOIDmode;
3814 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
3815 addr_space_t as;
3817 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
3819 case tcc_expression:
3820 switch (TREE_CODE (exp))
3822 case COND_EXPR:
3823 case DOT_PROD_EXPR:
3824 case SAD_EXPR:
3825 case WIDEN_MULT_PLUS_EXPR:
3826 case WIDEN_MULT_MINUS_EXPR:
3827 case FMA_EXPR:
3828 goto ternary;
3830 case TRUTH_ANDIF_EXPR:
3831 case TRUTH_ORIF_EXPR:
3832 case TRUTH_AND_EXPR:
3833 case TRUTH_OR_EXPR:
3834 case TRUTH_XOR_EXPR:
3835 goto binary;
3837 case TRUTH_NOT_EXPR:
3838 goto unary;
3840 default:
3841 break;
3843 break;
3845 ternary:
3846 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
3847 if (!op2)
3848 return NULL_RTX;
3849 /* Fall through. */
3851 binary:
3852 case tcc_binary:
3853 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
3854 if (!op1)
3855 return NULL_RTX;
3856 switch (TREE_CODE (exp))
3858 case LSHIFT_EXPR:
3859 case RSHIFT_EXPR:
3860 case LROTATE_EXPR:
3861 case RROTATE_EXPR:
3862 case WIDEN_LSHIFT_EXPR:
3863 /* Ensure second operand isn't wider than the first one. */
3864 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 1)));
3865 if (SCALAR_INT_MODE_P (inner_mode))
3867 machine_mode opmode = mode;
3868 if (VECTOR_MODE_P (mode))
3869 opmode = GET_MODE_INNER (mode);
3870 if (SCALAR_INT_MODE_P (opmode)
3871 && (GET_MODE_PRECISION (opmode)
3872 < GET_MODE_PRECISION (inner_mode)))
3873 op1 = simplify_gen_subreg (opmode, op1, inner_mode,
3874 subreg_lowpart_offset (opmode,
3875 inner_mode));
3877 break;
3878 default:
3879 break;
3881 /* Fall through. */
3883 unary:
3884 case tcc_unary:
3885 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3886 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3887 if (!op0)
3888 return NULL_RTX;
3889 break;
3891 case tcc_comparison:
3892 unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
3893 goto binary;
3895 case tcc_type:
3896 case tcc_statement:
3897 gcc_unreachable ();
3899 case tcc_constant:
3900 case tcc_exceptional:
3901 case tcc_declaration:
3902 case tcc_reference:
3903 case tcc_vl_exp:
3904 break;
3907 switch (TREE_CODE (exp))
3909 case STRING_CST:
3910 if (!lookup_constant_def (exp))
3912 if (strlen (TREE_STRING_POINTER (exp)) + 1
3913 != (size_t) TREE_STRING_LENGTH (exp))
3914 return NULL_RTX;
3915 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
3916 op0 = gen_rtx_MEM (BLKmode, op0);
3917 set_mem_attributes (op0, exp, 0);
3918 return op0;
3920 /* Fall through... */
3922 case INTEGER_CST:
3923 case REAL_CST:
3924 case FIXED_CST:
3925 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
3926 return op0;
3928 case COMPLEX_CST:
3929 gcc_assert (COMPLEX_MODE_P (mode));
3930 op0 = expand_debug_expr (TREE_REALPART (exp));
3931 op1 = expand_debug_expr (TREE_IMAGPART (exp));
3932 return gen_rtx_CONCAT (mode, op0, op1);
3934 case DEBUG_EXPR_DECL:
3935 op0 = DECL_RTL_IF_SET (exp);
3937 if (op0)
3938 return op0;
3940 op0 = gen_rtx_DEBUG_EXPR (mode);
3941 DEBUG_EXPR_TREE_DECL (op0) = exp;
3942 SET_DECL_RTL (exp, op0);
3944 return op0;
3946 case VAR_DECL:
3947 case PARM_DECL:
3948 case FUNCTION_DECL:
3949 case LABEL_DECL:
3950 case CONST_DECL:
3951 case RESULT_DECL:
3952 op0 = DECL_RTL_IF_SET (exp);
3954 /* This decl was probably optimized away. */
3955 if (!op0)
3957 if (TREE_CODE (exp) != VAR_DECL
3958 || DECL_EXTERNAL (exp)
3959 || !TREE_STATIC (exp)
3960 || !DECL_NAME (exp)
3961 || DECL_HARD_REGISTER (exp)
3962 || DECL_IN_CONSTANT_POOL (exp)
3963 || mode == VOIDmode)
3964 return NULL;
3966 op0 = make_decl_rtl_for_debug (exp);
3967 if (!MEM_P (op0)
3968 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
3969 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
3970 return NULL;
3972 else
3973 op0 = copy_rtx (op0);
3975 if (GET_MODE (op0) == BLKmode
3976 /* If op0 is not BLKmode, but mode is, adjust_mode
3977 below would ICE. While it is likely a FE bug,
3978 try to be robust here. See PR43166. */
3979 || mode == BLKmode
3980 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
3982 gcc_assert (MEM_P (op0));
3983 op0 = adjust_address_nv (op0, mode, 0);
3984 return op0;
3987 /* Fall through. */
3989 adjust_mode:
3990 case PAREN_EXPR:
3991 CASE_CONVERT:
3993 inner_mode = GET_MODE (op0);
3995 if (mode == inner_mode)
3996 return op0;
3998 if (inner_mode == VOIDmode)
4000 if (TREE_CODE (exp) == SSA_NAME)
4001 inner_mode = TYPE_MODE (TREE_TYPE (exp));
4002 else
4003 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4004 if (mode == inner_mode)
4005 return op0;
4008 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4010 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4011 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4012 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4013 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4014 else
4015 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4017 else if (FLOAT_MODE_P (mode))
4019 gcc_assert (TREE_CODE (exp) != SSA_NAME);
4020 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4021 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
4022 else
4023 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
4025 else if (FLOAT_MODE_P (inner_mode))
4027 if (unsignedp)
4028 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4029 else
4030 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4032 else if (CONSTANT_P (op0)
4033 || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
4034 op0 = simplify_gen_subreg (mode, op0, inner_mode,
4035 subreg_lowpart_offset (mode,
4036 inner_mode));
4037 else if (UNARY_CLASS_P (exp)
4038 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
4039 : unsignedp)
4040 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4041 else
4042 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4044 return op0;
4047 case MEM_REF:
4048 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4050 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
4051 TREE_OPERAND (exp, 0),
4052 TREE_OPERAND (exp, 1));
4053 if (newexp)
4054 return expand_debug_expr (newexp);
4056 /* FALLTHROUGH */
4057 case INDIRECT_REF:
4058 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4059 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4060 if (!op0)
4061 return NULL;
4063 if (TREE_CODE (exp) == MEM_REF)
4065 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4066 || (GET_CODE (op0) == PLUS
4067 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
4068 /* (mem (debug_implicit_ptr)) might confuse aliasing.
4069 Instead just use get_inner_reference. */
4070 goto component_ref;
4072 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4073 if (!op1 || !CONST_INT_P (op1))
4074 return NULL;
4076 op0 = plus_constant (inner_mode, op0, INTVAL (op1));
4079 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4081 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4082 op0, as);
4083 if (op0 == NULL_RTX)
4084 return NULL;
4086 op0 = gen_rtx_MEM (mode, op0);
4087 set_mem_attributes (op0, exp, 0);
4088 if (TREE_CODE (exp) == MEM_REF
4089 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4090 set_mem_expr (op0, NULL_TREE);
4091 set_mem_addr_space (op0, as);
4093 return op0;
4095 case TARGET_MEM_REF:
4096 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
4097 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
4098 return NULL;
4100 op0 = expand_debug_expr
4101 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
4102 if (!op0)
4103 return NULL;
4105 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4106 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4107 op0, as);
4108 if (op0 == NULL_RTX)
4109 return NULL;
4111 op0 = gen_rtx_MEM (mode, op0);
4113 set_mem_attributes (op0, exp, 0);
4114 set_mem_addr_space (op0, as);
4116 return op0;
4118 component_ref:
4119 case ARRAY_REF:
4120 case ARRAY_RANGE_REF:
4121 case COMPONENT_REF:
4122 case BIT_FIELD_REF:
4123 case REALPART_EXPR:
4124 case IMAGPART_EXPR:
4125 case VIEW_CONVERT_EXPR:
4127 machine_mode mode1;
4128 HOST_WIDE_INT bitsize, bitpos;
4129 tree offset;
4130 int volatilep = 0;
4131 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
4132 &mode1, &unsignedp, &volatilep, false);
4133 rtx orig_op0;
4135 if (bitsize == 0)
4136 return NULL;
4138 orig_op0 = op0 = expand_debug_expr (tem);
4140 if (!op0)
4141 return NULL;
4143 if (offset)
4145 machine_mode addrmode, offmode;
4147 if (!MEM_P (op0))
4148 return NULL;
4150 op0 = XEXP (op0, 0);
4151 addrmode = GET_MODE (op0);
4152 if (addrmode == VOIDmode)
4153 addrmode = Pmode;
4155 op1 = expand_debug_expr (offset);
4156 if (!op1)
4157 return NULL;
4159 offmode = GET_MODE (op1);
4160 if (offmode == VOIDmode)
4161 offmode = TYPE_MODE (TREE_TYPE (offset));
4163 if (addrmode != offmode)
4164 op1 = simplify_gen_subreg (addrmode, op1, offmode,
4165 subreg_lowpart_offset (addrmode,
4166 offmode));
4168 /* Don't use offset_address here, we don't need a
4169 recognizable address, and we don't want to generate
4170 code. */
4171 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4172 op0, op1));
4175 if (MEM_P (op0))
4177 if (mode1 == VOIDmode)
4178 /* Bitfield. */
4179 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
4180 if (bitpos >= BITS_PER_UNIT)
4182 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
4183 bitpos %= BITS_PER_UNIT;
4185 else if (bitpos < 0)
4187 HOST_WIDE_INT units
4188 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4189 op0 = adjust_address_nv (op0, mode1, units);
4190 bitpos += units * BITS_PER_UNIT;
4192 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
4193 op0 = adjust_address_nv (op0, mode, 0);
4194 else if (GET_MODE (op0) != mode1)
4195 op0 = adjust_address_nv (op0, mode1, 0);
4196 else
4197 op0 = copy_rtx (op0);
4198 if (op0 == orig_op0)
4199 op0 = shallow_copy_rtx (op0);
4200 set_mem_attributes (op0, exp, 0);
4203 if (bitpos == 0 && mode == GET_MODE (op0))
4204 return op0;
4206 if (bitpos < 0)
4207 return NULL;
4209 if (GET_MODE (op0) == BLKmode)
4210 return NULL;
4212 if ((bitpos % BITS_PER_UNIT) == 0
4213 && bitsize == GET_MODE_BITSIZE (mode1))
4215 machine_mode opmode = GET_MODE (op0);
4217 if (opmode == VOIDmode)
4218 opmode = TYPE_MODE (TREE_TYPE (tem));
4220 /* This condition may hold if we're expanding the address
4221 right past the end of an array that turned out not to
4222 be addressable (i.e., the address was only computed in
4223 debug stmts). The gen_subreg below would rightfully
4224 crash, and the address doesn't really exist, so just
4225 drop it. */
4226 if (bitpos >= GET_MODE_BITSIZE (opmode))
4227 return NULL;
4229 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
4230 return simplify_gen_subreg (mode, op0, opmode,
4231 bitpos / BITS_PER_UNIT);
4234 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4235 && TYPE_UNSIGNED (TREE_TYPE (exp))
4236 ? SIGN_EXTRACT
4237 : ZERO_EXTRACT, mode,
4238 GET_MODE (op0) != VOIDmode
4239 ? GET_MODE (op0)
4240 : TYPE_MODE (TREE_TYPE (tem)),
4241 op0, GEN_INT (bitsize), GEN_INT (bitpos));
4244 case ABS_EXPR:
4245 return simplify_gen_unary (ABS, mode, op0, mode);
4247 case NEGATE_EXPR:
4248 return simplify_gen_unary (NEG, mode, op0, mode);
4250 case BIT_NOT_EXPR:
4251 return simplify_gen_unary (NOT, mode, op0, mode);
4253 case FLOAT_EXPR:
4254 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4255 0)))
4256 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4257 inner_mode);
4259 case FIX_TRUNC_EXPR:
4260 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4261 inner_mode);
4263 case POINTER_PLUS_EXPR:
4264 /* For the rare target where pointers are not the same size as
4265 size_t, we need to check for mis-matched modes and correct
4266 the addend. */
4267 if (op0 && op1
4268 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
4269 && GET_MODE (op0) != GET_MODE (op1))
4271 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1))
4272 /* If OP0 is a partial mode, then we must truncate, even if it has
4273 the same bitsize as OP1 as GCC's representation of partial modes
4274 is opaque. */
4275 || (GET_MODE_CLASS (GET_MODE (op0)) == MODE_PARTIAL_INT
4276 && GET_MODE_BITSIZE (GET_MODE (op0)) == GET_MODE_BITSIZE (GET_MODE (op1))))
4277 op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
4278 GET_MODE (op1));
4279 else
4280 /* We always sign-extend, regardless of the signedness of
4281 the operand, because the operand is always unsigned
4282 here even if the original C expression is signed. */
4283 op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
4284 GET_MODE (op1));
4286 /* Fall through. */
4287 case PLUS_EXPR:
4288 return simplify_gen_binary (PLUS, mode, op0, op1);
4290 case MINUS_EXPR:
4291 return simplify_gen_binary (MINUS, mode, op0, op1);
4293 case MULT_EXPR:
4294 return simplify_gen_binary (MULT, mode, op0, op1);
4296 case RDIV_EXPR:
4297 case TRUNC_DIV_EXPR:
4298 case EXACT_DIV_EXPR:
4299 if (unsignedp)
4300 return simplify_gen_binary (UDIV, mode, op0, op1);
4301 else
4302 return simplify_gen_binary (DIV, mode, op0, op1);
4304 case TRUNC_MOD_EXPR:
4305 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4307 case FLOOR_DIV_EXPR:
4308 if (unsignedp)
4309 return simplify_gen_binary (UDIV, mode, op0, op1);
4310 else
4312 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4313 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4314 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4315 return simplify_gen_binary (PLUS, mode, div, adj);
4318 case FLOOR_MOD_EXPR:
4319 if (unsignedp)
4320 return simplify_gen_binary (UMOD, mode, op0, op1);
4321 else
4323 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4324 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4325 adj = simplify_gen_unary (NEG, mode,
4326 simplify_gen_binary (MULT, mode, adj, op1),
4327 mode);
4328 return simplify_gen_binary (PLUS, mode, mod, adj);
4331 case CEIL_DIV_EXPR:
4332 if (unsignedp)
4334 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4335 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4336 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4337 return simplify_gen_binary (PLUS, mode, div, adj);
4339 else
4341 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4342 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4343 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4344 return simplify_gen_binary (PLUS, mode, div, adj);
4347 case CEIL_MOD_EXPR:
4348 if (unsignedp)
4350 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4351 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4352 adj = simplify_gen_unary (NEG, mode,
4353 simplify_gen_binary (MULT, mode, adj, op1),
4354 mode);
4355 return simplify_gen_binary (PLUS, mode, mod, adj);
4357 else
4359 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4360 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4361 adj = simplify_gen_unary (NEG, mode,
4362 simplify_gen_binary (MULT, mode, adj, op1),
4363 mode);
4364 return simplify_gen_binary (PLUS, mode, mod, adj);
4367 case ROUND_DIV_EXPR:
4368 if (unsignedp)
4370 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4371 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4372 rtx adj = round_udiv_adjust (mode, mod, op1);
4373 return simplify_gen_binary (PLUS, mode, div, adj);
4375 else
4377 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4378 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4379 rtx adj = round_sdiv_adjust (mode, mod, op1);
4380 return simplify_gen_binary (PLUS, mode, div, adj);
4383 case ROUND_MOD_EXPR:
4384 if (unsignedp)
4386 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4387 rtx adj = round_udiv_adjust (mode, mod, op1);
4388 adj = simplify_gen_unary (NEG, mode,
4389 simplify_gen_binary (MULT, mode, adj, op1),
4390 mode);
4391 return simplify_gen_binary (PLUS, mode, mod, adj);
4393 else
4395 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4396 rtx adj = round_sdiv_adjust (mode, mod, op1);
4397 adj = simplify_gen_unary (NEG, mode,
4398 simplify_gen_binary (MULT, mode, adj, op1),
4399 mode);
4400 return simplify_gen_binary (PLUS, mode, mod, adj);
4403 case LSHIFT_EXPR:
4404 return simplify_gen_binary (ASHIFT, mode, op0, op1);
4406 case RSHIFT_EXPR:
4407 if (unsignedp)
4408 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
4409 else
4410 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
4412 case LROTATE_EXPR:
4413 return simplify_gen_binary (ROTATE, mode, op0, op1);
4415 case RROTATE_EXPR:
4416 return simplify_gen_binary (ROTATERT, mode, op0, op1);
4418 case MIN_EXPR:
4419 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
4421 case MAX_EXPR:
4422 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
4424 case BIT_AND_EXPR:
4425 case TRUTH_AND_EXPR:
4426 return simplify_gen_binary (AND, mode, op0, op1);
4428 case BIT_IOR_EXPR:
4429 case TRUTH_OR_EXPR:
4430 return simplify_gen_binary (IOR, mode, op0, op1);
4432 case BIT_XOR_EXPR:
4433 case TRUTH_XOR_EXPR:
4434 return simplify_gen_binary (XOR, mode, op0, op1);
4436 case TRUTH_ANDIF_EXPR:
4437 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
4439 case TRUTH_ORIF_EXPR:
4440 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
4442 case TRUTH_NOT_EXPR:
4443 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
4445 case LT_EXPR:
4446 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
4447 op0, op1);
4449 case LE_EXPR:
4450 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
4451 op0, op1);
4453 case GT_EXPR:
4454 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
4455 op0, op1);
4457 case GE_EXPR:
4458 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
4459 op0, op1);
4461 case EQ_EXPR:
4462 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
4464 case NE_EXPR:
4465 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
4467 case UNORDERED_EXPR:
4468 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
4470 case ORDERED_EXPR:
4471 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
4473 case UNLT_EXPR:
4474 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
4476 case UNLE_EXPR:
4477 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
4479 case UNGT_EXPR:
4480 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
4482 case UNGE_EXPR:
4483 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
4485 case UNEQ_EXPR:
4486 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
4488 case LTGT_EXPR:
4489 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
4491 case COND_EXPR:
4492 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
4494 case COMPLEX_EXPR:
4495 gcc_assert (COMPLEX_MODE_P (mode));
4496 if (GET_MODE (op0) == VOIDmode)
4497 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
4498 if (GET_MODE (op1) == VOIDmode)
4499 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
4500 return gen_rtx_CONCAT (mode, op0, op1);
4502 case CONJ_EXPR:
4503 if (GET_CODE (op0) == CONCAT)
4504 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
4505 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
4506 XEXP (op0, 1),
4507 GET_MODE_INNER (mode)));
4508 else
4510 machine_mode imode = GET_MODE_INNER (mode);
4511 rtx re, im;
4513 if (MEM_P (op0))
4515 re = adjust_address_nv (op0, imode, 0);
4516 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
4518 else
4520 machine_mode ifmode = int_mode_for_mode (mode);
4521 machine_mode ihmode = int_mode_for_mode (imode);
4522 rtx halfsize;
4523 if (ifmode == BLKmode || ihmode == BLKmode)
4524 return NULL;
4525 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
4526 re = op0;
4527 if (mode != ifmode)
4528 re = gen_rtx_SUBREG (ifmode, re, 0);
4529 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
4530 if (imode != ihmode)
4531 re = gen_rtx_SUBREG (imode, re, 0);
4532 im = copy_rtx (op0);
4533 if (mode != ifmode)
4534 im = gen_rtx_SUBREG (ifmode, im, 0);
4535 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
4536 if (imode != ihmode)
4537 im = gen_rtx_SUBREG (imode, im, 0);
4539 im = gen_rtx_NEG (imode, im);
4540 return gen_rtx_CONCAT (mode, re, im);
4543 case ADDR_EXPR:
4544 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4545 if (!op0 || !MEM_P (op0))
4547 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
4548 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
4549 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
4550 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
4551 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
4552 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
4554 if (handled_component_p (TREE_OPERAND (exp, 0)))
4556 HOST_WIDE_INT bitoffset, bitsize, maxsize;
4557 tree decl
4558 = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
4559 &bitoffset, &bitsize, &maxsize);
4560 if ((TREE_CODE (decl) == VAR_DECL
4561 || TREE_CODE (decl) == PARM_DECL
4562 || TREE_CODE (decl) == RESULT_DECL)
4563 && (!TREE_ADDRESSABLE (decl)
4564 || target_for_debug_bind (decl))
4565 && (bitoffset % BITS_PER_UNIT) == 0
4566 && bitsize > 0
4567 && bitsize == maxsize)
4569 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
4570 return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
4574 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
4575 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4576 == ADDR_EXPR)
4578 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4579 0));
4580 if (op0 != NULL
4581 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4582 || (GET_CODE (op0) == PLUS
4583 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
4584 && CONST_INT_P (XEXP (op0, 1)))))
4586 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4587 1));
4588 if (!op1 || !CONST_INT_P (op1))
4589 return NULL;
4591 return plus_constant (mode, op0, INTVAL (op1));
4595 return NULL;
4598 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
4599 op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
4601 return op0;
4603 case VECTOR_CST:
4605 unsigned i;
4607 op0 = gen_rtx_CONCATN
4608 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4610 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
4612 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
4613 if (!op1)
4614 return NULL;
4615 XVECEXP (op0, 0, i) = op1;
4618 return op0;
4621 case CONSTRUCTOR:
4622 if (TREE_CLOBBER_P (exp))
4623 return NULL;
4624 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
4626 unsigned i;
4627 tree val;
4629 op0 = gen_rtx_CONCATN
4630 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4632 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
4634 op1 = expand_debug_expr (val);
4635 if (!op1)
4636 return NULL;
4637 XVECEXP (op0, 0, i) = op1;
4640 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
4642 op1 = expand_debug_expr
4643 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
4645 if (!op1)
4646 return NULL;
4648 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
4649 XVECEXP (op0, 0, i) = op1;
4652 return op0;
4654 else
4655 goto flag_unsupported;
4657 case CALL_EXPR:
4658 /* ??? Maybe handle some builtins? */
4659 return NULL;
4661 case SSA_NAME:
4663 gimple g = get_gimple_for_ssa_name (exp);
4664 if (g)
4666 tree t = NULL_TREE;
4667 if (deep_ter_debug_map)
4669 tree *slot = deep_ter_debug_map->get (exp);
4670 if (slot)
4671 t = *slot;
4673 if (t == NULL_TREE)
4674 t = gimple_assign_rhs_to_tree (g);
4675 op0 = expand_debug_expr (t);
4676 if (!op0)
4677 return NULL;
4679 else
4681 int part = var_to_partition (SA.map, exp);
4683 if (part == NO_PARTITION)
4685 /* If this is a reference to an incoming value of parameter
4686 that is never used in the code or where the incoming
4687 value is never used in the code, use PARM_DECL's
4688 DECL_RTL if set. */
4689 if (SSA_NAME_IS_DEFAULT_DEF (exp)
4690 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL)
4692 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
4693 if (op0)
4694 goto adjust_mode;
4695 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
4696 if (op0)
4697 goto adjust_mode;
4699 return NULL;
4702 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
4704 op0 = copy_rtx (SA.partition_to_pseudo[part]);
4706 goto adjust_mode;
4709 case ERROR_MARK:
4710 return NULL;
4712 /* Vector stuff. For most of the codes we don't have rtl codes. */
4713 case REALIGN_LOAD_EXPR:
4714 case REDUC_MAX_EXPR:
4715 case REDUC_MIN_EXPR:
4716 case REDUC_PLUS_EXPR:
4717 case VEC_COND_EXPR:
4718 case VEC_PACK_FIX_TRUNC_EXPR:
4719 case VEC_PACK_SAT_EXPR:
4720 case VEC_PACK_TRUNC_EXPR:
4721 case VEC_UNPACK_FLOAT_HI_EXPR:
4722 case VEC_UNPACK_FLOAT_LO_EXPR:
4723 case VEC_UNPACK_HI_EXPR:
4724 case VEC_UNPACK_LO_EXPR:
4725 case VEC_WIDEN_MULT_HI_EXPR:
4726 case VEC_WIDEN_MULT_LO_EXPR:
4727 case VEC_WIDEN_MULT_EVEN_EXPR:
4728 case VEC_WIDEN_MULT_ODD_EXPR:
4729 case VEC_WIDEN_LSHIFT_HI_EXPR:
4730 case VEC_WIDEN_LSHIFT_LO_EXPR:
4731 case VEC_PERM_EXPR:
4732 return NULL;
4734 /* Misc codes. */
4735 case ADDR_SPACE_CONVERT_EXPR:
4736 case FIXED_CONVERT_EXPR:
4737 case OBJ_TYPE_REF:
4738 case WITH_SIZE_EXPR:
4739 return NULL;
4741 case DOT_PROD_EXPR:
4742 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4743 && SCALAR_INT_MODE_P (mode))
4746 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4747 0)))
4748 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4749 inner_mode);
4751 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4752 1)))
4753 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
4754 inner_mode);
4755 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4756 return simplify_gen_binary (PLUS, mode, op0, op2);
4758 return NULL;
4760 case WIDEN_MULT_EXPR:
4761 case WIDEN_MULT_PLUS_EXPR:
4762 case WIDEN_MULT_MINUS_EXPR:
4763 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4764 && SCALAR_INT_MODE_P (mode))
4766 inner_mode = GET_MODE (op0);
4767 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4768 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4769 else
4770 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4771 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
4772 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
4773 else
4774 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
4775 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4776 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
4777 return op0;
4778 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
4779 return simplify_gen_binary (PLUS, mode, op0, op2);
4780 else
4781 return simplify_gen_binary (MINUS, mode, op2, op0);
4783 return NULL;
4785 case MULT_HIGHPART_EXPR:
4786 /* ??? Similar to the above. */
4787 return NULL;
4789 case WIDEN_SUM_EXPR:
4790 case WIDEN_LSHIFT_EXPR:
4791 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4792 && SCALAR_INT_MODE_P (mode))
4795 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4796 0)))
4797 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4798 inner_mode);
4799 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
4800 ? ASHIFT : PLUS, mode, op0, op1);
4802 return NULL;
4804 case FMA_EXPR:
4805 return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
4807 default:
4808 flag_unsupported:
4809 #ifdef ENABLE_CHECKING
4810 debug_tree (exp);
4811 gcc_unreachable ();
4812 #else
4813 return NULL;
4814 #endif
4818 /* Return an RTX equivalent to the source bind value of the tree expression
4819 EXP. */
4821 static rtx
4822 expand_debug_source_expr (tree exp)
4824 rtx op0 = NULL_RTX;
4825 machine_mode mode = VOIDmode, inner_mode;
4827 switch (TREE_CODE (exp))
4829 case PARM_DECL:
4831 mode = DECL_MODE (exp);
4832 op0 = expand_debug_parm_decl (exp);
4833 if (op0)
4834 break;
4835 /* See if this isn't an argument that has been completely
4836 optimized out. */
4837 if (!DECL_RTL_SET_P (exp)
4838 && !DECL_INCOMING_RTL (exp)
4839 && DECL_ABSTRACT_ORIGIN (current_function_decl))
4841 tree aexp = DECL_ORIGIN (exp);
4842 if (DECL_CONTEXT (aexp)
4843 == DECL_ABSTRACT_ORIGIN (current_function_decl))
4845 vec<tree, va_gc> **debug_args;
4846 unsigned int ix;
4847 tree ddecl;
4848 debug_args = decl_debug_args_lookup (current_function_decl);
4849 if (debug_args != NULL)
4851 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
4852 ix += 2)
4853 if (ddecl == aexp)
4854 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
4858 break;
4860 default:
4861 break;
4864 if (op0 == NULL_RTX)
4865 return NULL_RTX;
4867 inner_mode = GET_MODE (op0);
4868 if (mode == inner_mode)
4869 return op0;
4871 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4873 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4874 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4875 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4876 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4877 else
4878 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4880 else if (FLOAT_MODE_P (mode))
4881 gcc_unreachable ();
4882 else if (FLOAT_MODE_P (inner_mode))
4884 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4885 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4886 else
4887 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4889 else if (CONSTANT_P (op0)
4890 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
4891 op0 = simplify_gen_subreg (mode, op0, inner_mode,
4892 subreg_lowpart_offset (mode, inner_mode));
4893 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4894 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4895 else
4896 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4898 return op0;
4901 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
4902 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
4903 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
4905 static void
4906 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
4908 rtx exp = *exp_p;
4910 if (exp == NULL_RTX)
4911 return;
4913 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
4914 return;
4916 if (depth == 4)
4918 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
4919 rtx dval = make_debug_expr_from_rtl (exp);
4921 /* Emit a debug bind insn before INSN. */
4922 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
4923 DEBUG_EXPR_TREE_DECL (dval), exp,
4924 VAR_INIT_STATUS_INITIALIZED);
4926 emit_debug_insn_before (bind, insn);
4927 *exp_p = dval;
4928 return;
4931 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
4932 int i, j;
4933 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4934 switch (*format_ptr++)
4936 case 'e':
4937 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
4938 break;
4940 case 'E':
4941 case 'V':
4942 for (j = 0; j < XVECLEN (exp, i); j++)
4943 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
4944 break;
4946 default:
4947 break;
4951 /* Expand the _LOCs in debug insns. We run this after expanding all
4952 regular insns, so that any variables referenced in the function
4953 will have their DECL_RTLs set. */
4955 static void
4956 expand_debug_locations (void)
4958 rtx_insn *insn;
4959 rtx_insn *last = get_last_insn ();
4960 int save_strict_alias = flag_strict_aliasing;
4962 /* New alias sets while setting up memory attributes cause
4963 -fcompare-debug failures, even though it doesn't bring about any
4964 codegen changes. */
4965 flag_strict_aliasing = 0;
4967 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4968 if (DEBUG_INSN_P (insn))
4970 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
4971 rtx val;
4972 rtx_insn *prev_insn, *insn2;
4973 machine_mode mode;
4975 if (value == NULL_TREE)
4976 val = NULL_RTX;
4977 else
4979 if (INSN_VAR_LOCATION_STATUS (insn)
4980 == VAR_INIT_STATUS_UNINITIALIZED)
4981 val = expand_debug_source_expr (value);
4982 /* The avoid_deep_ter_for_debug function inserts
4983 debug bind stmts after SSA_NAME definition, with the
4984 SSA_NAME as the whole bind location. Disable temporarily
4985 expansion of that SSA_NAME into the DEBUG_EXPR_DECL
4986 being defined in this DEBUG_INSN. */
4987 else if (deep_ter_debug_map && TREE_CODE (value) == SSA_NAME)
4989 tree *slot = deep_ter_debug_map->get (value);
4990 if (slot)
4992 if (*slot == INSN_VAR_LOCATION_DECL (insn))
4993 *slot = NULL_TREE;
4994 else
4995 slot = NULL;
4997 val = expand_debug_expr (value);
4998 if (slot)
4999 *slot = INSN_VAR_LOCATION_DECL (insn);
5001 else
5002 val = expand_debug_expr (value);
5003 gcc_assert (last == get_last_insn ());
5006 if (!val)
5007 val = gen_rtx_UNKNOWN_VAR_LOC ();
5008 else
5010 mode = GET_MODE (INSN_VAR_LOCATION (insn));
5012 gcc_assert (mode == GET_MODE (val)
5013 || (GET_MODE (val) == VOIDmode
5014 && (CONST_SCALAR_INT_P (val)
5015 || GET_CODE (val) == CONST_FIXED
5016 || GET_CODE (val) == LABEL_REF)));
5019 INSN_VAR_LOCATION_LOC (insn) = val;
5020 prev_insn = PREV_INSN (insn);
5021 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
5022 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
5025 flag_strict_aliasing = save_strict_alias;
5028 /* Performs swapping operands of commutative operations to expand
5029 the expensive one first. */
5031 static void
5032 reorder_operands (basic_block bb)
5034 unsigned int *lattice; /* Hold cost of each statement. */
5035 unsigned int i = 0, n = 0;
5036 gimple_stmt_iterator gsi;
5037 gimple_seq stmts;
5038 gimple stmt;
5039 bool swap;
5040 tree op0, op1;
5041 ssa_op_iter iter;
5042 use_operand_p use_p;
5043 gimple def0, def1;
5045 /* Compute cost of each statement using estimate_num_insns. */
5046 stmts = bb_seq (bb);
5047 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5049 stmt = gsi_stmt (gsi);
5050 if (!is_gimple_debug (stmt))
5051 gimple_set_uid (stmt, n++);
5053 lattice = XNEWVEC (unsigned int, n);
5054 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5056 unsigned cost;
5057 stmt = gsi_stmt (gsi);
5058 if (is_gimple_debug (stmt))
5059 continue;
5060 cost = estimate_num_insns (stmt, &eni_size_weights);
5061 lattice[i] = cost;
5062 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
5064 tree use = USE_FROM_PTR (use_p);
5065 gimple def_stmt;
5066 if (TREE_CODE (use) != SSA_NAME)
5067 continue;
5068 def_stmt = get_gimple_for_ssa_name (use);
5069 if (!def_stmt)
5070 continue;
5071 lattice[i] += lattice[gimple_uid (def_stmt)];
5073 i++;
5074 if (!is_gimple_assign (stmt)
5075 || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
5076 continue;
5077 op0 = gimple_op (stmt, 1);
5078 op1 = gimple_op (stmt, 2);
5079 if (TREE_CODE (op0) != SSA_NAME
5080 || TREE_CODE (op1) != SSA_NAME)
5081 continue;
5082 /* Swap operands if the second one is more expensive. */
5083 def0 = get_gimple_for_ssa_name (op0);
5084 def1 = get_gimple_for_ssa_name (op1);
5085 if (!def1)
5086 continue;
5087 swap = false;
5088 if (!def0 || lattice[gimple_uid (def1)] > lattice[gimple_uid (def0)])
5089 swap = true;
5090 if (swap)
5092 if (dump_file && (dump_flags & TDF_DETAILS))
5094 fprintf (dump_file, "Swap operands in stmt:\n");
5095 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5096 fprintf (dump_file, "Cost left opnd=%d, right opnd=%d\n",
5097 def0 ? lattice[gimple_uid (def0)] : 0,
5098 lattice[gimple_uid (def1)]);
5100 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
5101 gimple_assign_rhs2_ptr (stmt));
5104 XDELETE (lattice);
5107 /* Expand basic block BB from GIMPLE trees to RTL. */
5109 static basic_block
5110 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
5112 gimple_stmt_iterator gsi;
5113 gimple_seq stmts;
5114 gimple stmt = NULL;
5115 rtx_note *note;
5116 rtx_insn *last;
5117 edge e;
5118 edge_iterator ei;
5120 if (dump_file)
5121 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
5122 bb->index);
5124 /* Note that since we are now transitioning from GIMPLE to RTL, we
5125 cannot use the gsi_*_bb() routines because they expect the basic
5126 block to be in GIMPLE, instead of RTL. Therefore, we need to
5127 access the BB sequence directly. */
5128 if (optimize)
5129 reorder_operands (bb);
5130 stmts = bb_seq (bb);
5131 bb->il.gimple.seq = NULL;
5132 bb->il.gimple.phi_nodes = NULL;
5133 rtl_profile_for_bb (bb);
5134 init_rtl_bb_info (bb);
5135 bb->flags |= BB_RTL;
5137 /* Remove the RETURN_EXPR if we may fall though to the exit
5138 instead. */
5139 gsi = gsi_last (stmts);
5140 if (!gsi_end_p (gsi)
5141 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
5143 greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
5145 gcc_assert (single_succ_p (bb));
5146 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
5148 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5149 && !gimple_return_retval (ret_stmt))
5151 gsi_remove (&gsi, false);
5152 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
5156 gsi = gsi_start (stmts);
5157 if (!gsi_end_p (gsi))
5159 stmt = gsi_stmt (gsi);
5160 if (gimple_code (stmt) != GIMPLE_LABEL)
5161 stmt = NULL;
5164 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
5166 if (stmt || elt)
5168 last = get_last_insn ();
5170 if (stmt)
5172 expand_gimple_stmt (stmt);
5173 gsi_next (&gsi);
5176 if (elt)
5177 emit_label (*elt);
5179 /* Java emits line number notes in the top of labels.
5180 ??? Make this go away once line number notes are obsoleted. */
5181 BB_HEAD (bb) = NEXT_INSN (last);
5182 if (NOTE_P (BB_HEAD (bb)))
5183 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
5184 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
5186 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5188 else
5189 BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
5191 NOTE_BASIC_BLOCK (note) = bb;
5193 for (; !gsi_end_p (gsi); gsi_next (&gsi))
5195 basic_block new_bb;
5197 stmt = gsi_stmt (gsi);
5199 /* If this statement is a non-debug one, and we generate debug
5200 insns, then this one might be the last real use of a TERed
5201 SSA_NAME, but where there are still some debug uses further
5202 down. Expanding the current SSA name in such further debug
5203 uses by their RHS might lead to wrong debug info, as coalescing
5204 might make the operands of such RHS be placed into the same
5205 pseudo as something else. Like so:
5206 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
5207 use(a_1);
5208 a_2 = ...
5209 #DEBUG ... => a_1
5210 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
5211 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
5212 the write to a_2 would actually have clobbered the place which
5213 formerly held a_0.
5215 So, instead of that, we recognize the situation, and generate
5216 debug temporaries at the last real use of TERed SSA names:
5217 a_1 = a_0 + 1;
5218 #DEBUG #D1 => a_1
5219 use(a_1);
5220 a_2 = ...
5221 #DEBUG ... => #D1
5223 if (MAY_HAVE_DEBUG_INSNS
5224 && SA.values
5225 && !is_gimple_debug (stmt))
5227 ssa_op_iter iter;
5228 tree op;
5229 gimple def;
5231 location_t sloc = curr_insn_location ();
5233 /* Look for SSA names that have their last use here (TERed
5234 names always have only one real use). */
5235 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5236 if ((def = get_gimple_for_ssa_name (op)))
5238 imm_use_iterator imm_iter;
5239 use_operand_p use_p;
5240 bool have_debug_uses = false;
5242 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5244 if (gimple_debug_bind_p (USE_STMT (use_p)))
5246 have_debug_uses = true;
5247 break;
5251 if (have_debug_uses)
5253 /* OP is a TERed SSA name, with DEF its defining
5254 statement, and where OP is used in further debug
5255 instructions. Generate a debug temporary, and
5256 replace all uses of OP in debug insns with that
5257 temporary. */
5258 gimple debugstmt;
5259 tree value = gimple_assign_rhs_to_tree (def);
5260 tree vexpr = make_node (DEBUG_EXPR_DECL);
5261 rtx val;
5262 machine_mode mode;
5264 set_curr_insn_location (gimple_location (def));
5266 DECL_ARTIFICIAL (vexpr) = 1;
5267 TREE_TYPE (vexpr) = TREE_TYPE (value);
5268 if (DECL_P (value))
5269 mode = DECL_MODE (value);
5270 else
5271 mode = TYPE_MODE (TREE_TYPE (value));
5272 DECL_MODE (vexpr) = mode;
5274 val = gen_rtx_VAR_LOCATION
5275 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5277 emit_debug_insn (val);
5279 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5281 if (!gimple_debug_bind_p (debugstmt))
5282 continue;
5284 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5285 SET_USE (use_p, vexpr);
5287 update_stmt (debugstmt);
5291 set_curr_insn_location (sloc);
5294 currently_expanding_gimple_stmt = stmt;
5296 /* Expand this statement, then evaluate the resulting RTL and
5297 fixup the CFG accordingly. */
5298 if (gimple_code (stmt) == GIMPLE_COND)
5300 new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
5301 if (new_bb)
5302 return new_bb;
5304 else if (gimple_debug_bind_p (stmt))
5306 location_t sloc = curr_insn_location ();
5307 gimple_stmt_iterator nsi = gsi;
5309 for (;;)
5311 tree var = gimple_debug_bind_get_var (stmt);
5312 tree value;
5313 rtx val;
5314 machine_mode mode;
5316 if (TREE_CODE (var) != DEBUG_EXPR_DECL
5317 && TREE_CODE (var) != LABEL_DECL
5318 && !target_for_debug_bind (var))
5319 goto delink_debug_stmt;
5321 if (gimple_debug_bind_has_value_p (stmt))
5322 value = gimple_debug_bind_get_value (stmt);
5323 else
5324 value = NULL_TREE;
5326 last = get_last_insn ();
5328 set_curr_insn_location (gimple_location (stmt));
5330 if (DECL_P (var))
5331 mode = DECL_MODE (var);
5332 else
5333 mode = TYPE_MODE (TREE_TYPE (var));
5335 val = gen_rtx_VAR_LOCATION
5336 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5338 emit_debug_insn (val);
5340 if (dump_file && (dump_flags & TDF_DETAILS))
5342 /* We can't dump the insn with a TREE where an RTX
5343 is expected. */
5344 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5345 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5346 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5349 delink_debug_stmt:
5350 /* In order not to generate too many debug temporaries,
5351 we delink all uses of debug statements we already expanded.
5352 Therefore debug statements between definition and real
5353 use of TERed SSA names will continue to use the SSA name,
5354 and not be replaced with debug temps. */
5355 delink_stmt_imm_use (stmt);
5357 gsi = nsi;
5358 gsi_next (&nsi);
5359 if (gsi_end_p (nsi))
5360 break;
5361 stmt = gsi_stmt (nsi);
5362 if (!gimple_debug_bind_p (stmt))
5363 break;
5366 set_curr_insn_location (sloc);
5368 else if (gimple_debug_source_bind_p (stmt))
5370 location_t sloc = curr_insn_location ();
5371 tree var = gimple_debug_source_bind_get_var (stmt);
5372 tree value = gimple_debug_source_bind_get_value (stmt);
5373 rtx val;
5374 machine_mode mode;
5376 last = get_last_insn ();
5378 set_curr_insn_location (gimple_location (stmt));
5380 mode = DECL_MODE (var);
5382 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
5383 VAR_INIT_STATUS_UNINITIALIZED);
5385 emit_debug_insn (val);
5387 if (dump_file && (dump_flags & TDF_DETAILS))
5389 /* We can't dump the insn with a TREE where an RTX
5390 is expected. */
5391 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5392 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5393 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5396 set_curr_insn_location (sloc);
5398 else
5400 gcall *call_stmt = dyn_cast <gcall *> (stmt);
5401 if (call_stmt
5402 && gimple_call_tail_p (call_stmt)
5403 && disable_tail_calls)
5404 gimple_call_set_tail (call_stmt, false);
5406 if (call_stmt && gimple_call_tail_p (call_stmt))
5408 bool can_fallthru;
5409 new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
5410 if (new_bb)
5412 if (can_fallthru)
5413 bb = new_bb;
5414 else
5415 return new_bb;
5418 else
5420 def_operand_p def_p;
5421 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
5423 if (def_p != NULL)
5425 /* Ignore this stmt if it is in the list of
5426 replaceable expressions. */
5427 if (SA.values
5428 && bitmap_bit_p (SA.values,
5429 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
5430 continue;
5432 last = expand_gimple_stmt (stmt);
5433 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5438 currently_expanding_gimple_stmt = NULL;
5440 /* Expand implicit goto and convert goto_locus. */
5441 FOR_EACH_EDGE (e, ei, bb->succs)
5443 if (e->goto_locus != UNKNOWN_LOCATION)
5444 set_curr_insn_location (e->goto_locus);
5445 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
5447 emit_jump (label_rtx_for_bb (e->dest));
5448 e->flags &= ~EDGE_FALLTHRU;
5452 /* Expanded RTL can create a jump in the last instruction of block.
5453 This later might be assumed to be a jump to successor and break edge insertion.
5454 We need to insert dummy move to prevent this. PR41440. */
5455 if (single_succ_p (bb)
5456 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
5457 && (last = get_last_insn ())
5458 && JUMP_P (last))
5460 rtx dummy = gen_reg_rtx (SImode);
5461 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
5464 do_pending_stack_adjust ();
5466 /* Find the block tail. The last insn in the block is the insn
5467 before a barrier and/or table jump insn. */
5468 last = get_last_insn ();
5469 if (BARRIER_P (last))
5470 last = PREV_INSN (last);
5471 if (JUMP_TABLE_DATA_P (last))
5472 last = PREV_INSN (PREV_INSN (last));
5473 BB_END (bb) = last;
5475 update_bb_for_insn (bb);
5477 return bb;
5481 /* Create a basic block for initialization code. */
5483 static basic_block
5484 construct_init_block (void)
5486 basic_block init_block, first_block;
5487 edge e = NULL;
5488 int flags;
5490 /* Multiple entry points not supported yet. */
5491 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
5492 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5493 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
5494 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5495 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5497 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
5499 /* When entry edge points to first basic block, we don't need jump,
5500 otherwise we have to jump into proper target. */
5501 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
5503 tree label = gimple_block_label (e->dest);
5505 emit_jump (jump_target_rtx (label));
5506 flags = 0;
5508 else
5509 flags = EDGE_FALLTHRU;
5511 init_block = create_basic_block (NEXT_INSN (get_insns ()),
5512 get_last_insn (),
5513 ENTRY_BLOCK_PTR_FOR_FN (cfun));
5514 init_block->frequency = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
5515 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5516 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5517 if (e)
5519 first_block = e->dest;
5520 redirect_edge_succ (e, init_block);
5521 e = make_edge (init_block, first_block, flags);
5523 else
5524 e = make_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5525 e->probability = REG_BR_PROB_BASE;
5526 e->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5528 update_bb_for_insn (init_block);
5529 return init_block;
5532 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
5533 found in the block tree. */
5535 static void
5536 set_block_levels (tree block, int level)
5538 while (block)
5540 BLOCK_NUMBER (block) = level;
5541 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
5542 block = BLOCK_CHAIN (block);
5546 /* Create a block containing landing pads and similar stuff. */
5548 static void
5549 construct_exit_block (void)
5551 rtx_insn *head = get_last_insn ();
5552 rtx_insn *end;
5553 basic_block exit_block;
5554 edge e, e2;
5555 unsigned ix;
5556 edge_iterator ei;
5557 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
5558 rtx_insn *orig_end = BB_END (prev_bb);
5560 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5562 /* Make sure the locus is set to the end of the function, so that
5563 epilogue line numbers and warnings are set properly. */
5564 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
5565 input_location = cfun->function_end_locus;
5567 /* Generate rtl for function exit. */
5568 expand_function_end ();
5570 end = get_last_insn ();
5571 if (head == end)
5572 return;
5573 /* While emitting the function end we could move end of the last basic
5574 block. */
5575 BB_END (prev_bb) = orig_end;
5576 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
5577 head = NEXT_INSN (head);
5578 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
5579 bb frequency counting will be confused. Any instructions before that
5580 label are emitted for the case where PREV_BB falls through into the
5581 exit block, so append those instructions to prev_bb in that case. */
5582 if (NEXT_INSN (head) != return_label)
5584 while (NEXT_INSN (head) != return_label)
5586 if (!NOTE_P (NEXT_INSN (head)))
5587 BB_END (prev_bb) = NEXT_INSN (head);
5588 head = NEXT_INSN (head);
5591 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
5592 exit_block->frequency = EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency;
5593 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5594 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5596 ix = 0;
5597 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
5599 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
5600 if (!(e->flags & EDGE_ABNORMAL))
5601 redirect_edge_succ (e, exit_block);
5602 else
5603 ix++;
5606 e = make_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5607 e->probability = REG_BR_PROB_BASE;
5608 e->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5609 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5610 if (e2 != e)
5612 e->count -= e2->count;
5613 exit_block->count -= e2->count;
5614 exit_block->frequency -= EDGE_FREQUENCY (e2);
5616 if (e->count < 0)
5617 e->count = 0;
5618 if (exit_block->count < 0)
5619 exit_block->count = 0;
5620 if (exit_block->frequency < 0)
5621 exit_block->frequency = 0;
5622 update_bb_for_insn (exit_block);
5625 /* Helper function for discover_nonconstant_array_refs.
5626 Look for ARRAY_REF nodes with non-constant indexes and mark them
5627 addressable. */
5629 static tree
5630 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
5631 void *data ATTRIBUTE_UNUSED)
5633 tree t = *tp;
5635 if (IS_TYPE_OR_DECL_P (t))
5636 *walk_subtrees = 0;
5637 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5639 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5640 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
5641 && (!TREE_OPERAND (t, 2)
5642 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5643 || (TREE_CODE (t) == COMPONENT_REF
5644 && (!TREE_OPERAND (t,2)
5645 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5646 || TREE_CODE (t) == BIT_FIELD_REF
5647 || TREE_CODE (t) == REALPART_EXPR
5648 || TREE_CODE (t) == IMAGPART_EXPR
5649 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5650 || CONVERT_EXPR_P (t))
5651 t = TREE_OPERAND (t, 0);
5653 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5655 t = get_base_address (t);
5656 if (t && DECL_P (t)
5657 && DECL_MODE (t) != BLKmode)
5658 TREE_ADDRESSABLE (t) = 1;
5661 *walk_subtrees = 0;
5664 return NULL_TREE;
5667 /* RTL expansion is not able to compile array references with variable
5668 offsets for arrays stored in single register. Discover such
5669 expressions and mark variables as addressable to avoid this
5670 scenario. */
5672 static void
5673 discover_nonconstant_array_refs (void)
5675 basic_block bb;
5676 gimple_stmt_iterator gsi;
5678 FOR_EACH_BB_FN (bb, cfun)
5679 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5681 gimple stmt = gsi_stmt (gsi);
5682 if (!is_gimple_debug (stmt))
5683 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
5687 /* This function sets crtl->args.internal_arg_pointer to a virtual
5688 register if DRAP is needed. Local register allocator will replace
5689 virtual_incoming_args_rtx with the virtual register. */
5691 static void
5692 expand_stack_alignment (void)
5694 rtx drap_rtx;
5695 unsigned int preferred_stack_boundary;
5697 if (! SUPPORTS_STACK_ALIGNMENT)
5698 return;
5700 if (cfun->calls_alloca
5701 || cfun->has_nonlocal_label
5702 || crtl->has_nonlocal_goto)
5703 crtl->need_drap = true;
5705 /* Call update_stack_boundary here again to update incoming stack
5706 boundary. It may set incoming stack alignment to a different
5707 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
5708 use the minimum incoming stack alignment to check if it is OK
5709 to perform sibcall optimization since sibcall optimization will
5710 only align the outgoing stack to incoming stack boundary. */
5711 if (targetm.calls.update_stack_boundary)
5712 targetm.calls.update_stack_boundary ();
5714 /* The incoming stack frame has to be aligned at least at
5715 parm_stack_boundary. */
5716 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
5718 /* Update crtl->stack_alignment_estimated and use it later to align
5719 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
5720 exceptions since callgraph doesn't collect incoming stack alignment
5721 in this case. */
5722 if (cfun->can_throw_non_call_exceptions
5723 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
5724 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
5725 else
5726 preferred_stack_boundary = crtl->preferred_stack_boundary;
5727 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
5728 crtl->stack_alignment_estimated = preferred_stack_boundary;
5729 if (preferred_stack_boundary > crtl->stack_alignment_needed)
5730 crtl->stack_alignment_needed = preferred_stack_boundary;
5732 gcc_assert (crtl->stack_alignment_needed
5733 <= crtl->stack_alignment_estimated);
5735 crtl->stack_realign_needed
5736 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
5737 crtl->stack_realign_tried = crtl->stack_realign_needed;
5739 crtl->stack_realign_processed = true;
5741 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
5742 alignment. */
5743 gcc_assert (targetm.calls.get_drap_rtx != NULL);
5744 drap_rtx = targetm.calls.get_drap_rtx ();
5746 /* stack_realign_drap and drap_rtx must match. */
5747 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
5749 /* Do nothing if NULL is returned, which means DRAP is not needed. */
5750 if (NULL != drap_rtx)
5752 crtl->args.internal_arg_pointer = drap_rtx;
5754 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
5755 needed. */
5756 fixup_tail_calls ();
5761 static void
5762 expand_main_function (void)
5764 #if (defined(INVOKE__main) \
5765 || (!defined(HAS_INIT_SECTION) \
5766 && !defined(INIT_SECTION_ASM_OP) \
5767 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
5768 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
5769 #endif
5773 /* Expand code to initialize the stack_protect_guard. This is invoked at
5774 the beginning of a function to be protected. */
5776 #ifndef HAVE_stack_protect_set
5777 # define HAVE_stack_protect_set 0
5778 # define gen_stack_protect_set(x,y) (gcc_unreachable (), NULL_RTX)
5779 #endif
5781 static void
5782 stack_protect_prologue (void)
5784 tree guard_decl = targetm.stack_protect_guard ();
5785 rtx x, y;
5787 x = expand_normal (crtl->stack_protect_guard);
5788 y = expand_normal (guard_decl);
5790 /* Allow the target to copy from Y to X without leaking Y into a
5791 register. */
5792 if (HAVE_stack_protect_set)
5794 rtx insn = gen_stack_protect_set (x, y);
5795 if (insn)
5797 emit_insn (insn);
5798 return;
5802 /* Otherwise do a straight move. */
5803 emit_move_insn (x, y);
5806 /* Translate the intermediate representation contained in the CFG
5807 from GIMPLE trees to RTL.
5809 We do conversion per basic block and preserve/update the tree CFG.
5810 This implies we have to do some magic as the CFG can simultaneously
5811 consist of basic blocks containing RTL and GIMPLE trees. This can
5812 confuse the CFG hooks, so be careful to not manipulate CFG during
5813 the expansion. */
5815 namespace {
5817 const pass_data pass_data_expand =
5819 RTL_PASS, /* type */
5820 "expand", /* name */
5821 OPTGROUP_NONE, /* optinfo_flags */
5822 TV_EXPAND, /* tv_id */
5823 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
5824 | PROP_gimple_lcx
5825 | PROP_gimple_lvec
5826 | PROP_gimple_lva), /* properties_required */
5827 PROP_rtl, /* properties_provided */
5828 ( PROP_ssa | PROP_trees ), /* properties_destroyed */
5829 0, /* todo_flags_start */
5830 0, /* todo_flags_finish */
5833 class pass_expand : public rtl_opt_pass
5835 public:
5836 pass_expand (gcc::context *ctxt)
5837 : rtl_opt_pass (pass_data_expand, ctxt)
5840 /* opt_pass methods: */
5841 virtual unsigned int execute (function *);
5843 }; // class pass_expand
5845 unsigned int
5846 pass_expand::execute (function *fun)
5848 basic_block bb, init_block;
5849 sbitmap blocks;
5850 edge_iterator ei;
5851 edge e;
5852 rtx_insn *var_seq, *var_ret_seq;
5853 unsigned i;
5855 timevar_push (TV_OUT_OF_SSA);
5856 rewrite_out_of_ssa (&SA);
5857 timevar_pop (TV_OUT_OF_SSA);
5858 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
5860 if (MAY_HAVE_DEBUG_STMTS && flag_tree_ter)
5862 gimple_stmt_iterator gsi;
5863 FOR_EACH_BB_FN (bb, cfun)
5864 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5865 if (gimple_debug_bind_p (gsi_stmt (gsi)))
5866 avoid_deep_ter_for_debug (gsi_stmt (gsi), 0);
5869 /* Make sure all values used by the optimization passes have sane
5870 defaults. */
5871 reg_renumber = 0;
5873 /* Some backends want to know that we are expanding to RTL. */
5874 currently_expanding_to_rtl = 1;
5875 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
5876 free_dominance_info (CDI_DOMINATORS);
5878 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
5880 if (chkp_function_instrumented_p (current_function_decl))
5881 chkp_reset_rtl_bounds ();
5883 insn_locations_init ();
5884 if (!DECL_IS_BUILTIN (current_function_decl))
5886 /* Eventually, all FEs should explicitly set function_start_locus. */
5887 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
5888 set_curr_insn_location
5889 (DECL_SOURCE_LOCATION (current_function_decl));
5890 else
5891 set_curr_insn_location (fun->function_start_locus);
5893 else
5894 set_curr_insn_location (UNKNOWN_LOCATION);
5895 prologue_location = curr_insn_location ();
5897 #ifdef INSN_SCHEDULING
5898 init_sched_attrs ();
5899 #endif
5901 /* Make sure first insn is a note even if we don't want linenums.
5902 This makes sure the first insn will never be deleted.
5903 Also, final expects a note to appear there. */
5904 emit_note (NOTE_INSN_DELETED);
5906 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
5907 discover_nonconstant_array_refs ();
5909 targetm.expand_to_rtl_hook ();
5910 crtl->stack_alignment_needed = STACK_BOUNDARY;
5911 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
5912 crtl->stack_alignment_estimated = 0;
5913 crtl->preferred_stack_boundary = STACK_BOUNDARY;
5914 fun->cfg->max_jumptable_ents = 0;
5916 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
5917 of the function section at exapnsion time to predict distance of calls. */
5918 resolve_unique_section (current_function_decl, 0, flag_function_sections);
5920 /* Expand the variables recorded during gimple lowering. */
5921 timevar_push (TV_VAR_EXPAND);
5922 start_sequence ();
5924 var_ret_seq = expand_used_vars ();
5926 var_seq = get_insns ();
5927 end_sequence ();
5928 timevar_pop (TV_VAR_EXPAND);
5930 /* Honor stack protection warnings. */
5931 if (warn_stack_protect)
5933 if (fun->calls_alloca)
5934 warning (OPT_Wstack_protector,
5935 "stack protector not protecting local variables: "
5936 "variable length buffer");
5937 if (has_short_buffer && !crtl->stack_protect_guard)
5938 warning (OPT_Wstack_protector,
5939 "stack protector not protecting function: "
5940 "all local arrays are less than %d bytes long",
5941 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
5944 /* Set up parameters and prepare for return, for the function. */
5945 expand_function_start (current_function_decl);
5947 /* If we emitted any instructions for setting up the variables,
5948 emit them before the FUNCTION_START note. */
5949 if (var_seq)
5951 emit_insn_before (var_seq, parm_birth_insn);
5953 /* In expand_function_end we'll insert the alloca save/restore
5954 before parm_birth_insn. We've just insertted an alloca call.
5955 Adjust the pointer to match. */
5956 parm_birth_insn = var_seq;
5959 /* Now that we also have the parameter RTXs, copy them over to our
5960 partitions. */
5961 for (i = 0; i < SA.map->num_partitions; i++)
5963 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
5965 if (TREE_CODE (var) != VAR_DECL
5966 && !SA.partition_to_pseudo[i])
5967 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
5968 gcc_assert (SA.partition_to_pseudo[i]);
5970 /* If this decl was marked as living in multiple places, reset
5971 this now to NULL. */
5972 if (DECL_RTL_IF_SET (var) == pc_rtx)
5973 SET_DECL_RTL (var, NULL);
5975 /* Some RTL parts really want to look at DECL_RTL(x) when x
5976 was a decl marked in REG_ATTR or MEM_ATTR. We could use
5977 SET_DECL_RTL here making this available, but that would mean
5978 to select one of the potentially many RTLs for one DECL. Instead
5979 of doing that we simply reset the MEM_EXPR of the RTL in question,
5980 then nobody can get at it and hence nobody can call DECL_RTL on it. */
5981 if (!DECL_RTL_SET_P (var))
5983 if (MEM_P (SA.partition_to_pseudo[i]))
5984 set_mem_expr (SA.partition_to_pseudo[i], NULL);
5988 /* If we have a class containing differently aligned pointers
5989 we need to merge those into the corresponding RTL pointer
5990 alignment. */
5991 for (i = 1; i < num_ssa_names; i++)
5993 tree name = ssa_name (i);
5994 int part;
5995 rtx r;
5997 if (!name
5998 /* We might have generated new SSA names in
5999 update_alias_info_with_stack_vars. They will have a NULL
6000 defining statements, and won't be part of the partitioning,
6001 so ignore those. */
6002 || !SSA_NAME_DEF_STMT (name))
6003 continue;
6004 part = var_to_partition (SA.map, name);
6005 if (part == NO_PARTITION)
6006 continue;
6008 /* Adjust all partition members to get the underlying decl of
6009 the representative which we might have created in expand_one_var. */
6010 if (SSA_NAME_VAR (name) == NULL_TREE)
6012 tree leader = partition_to_var (SA.map, part);
6013 gcc_assert (SSA_NAME_VAR (leader) != NULL_TREE);
6014 replace_ssa_name_symbol (name, SSA_NAME_VAR (leader));
6016 if (!POINTER_TYPE_P (TREE_TYPE (name)))
6017 continue;
6019 r = SA.partition_to_pseudo[part];
6020 if (REG_P (r))
6021 mark_reg_pointer (r, get_pointer_alignment (name));
6024 /* If this function is `main', emit a call to `__main'
6025 to run global initializers, etc. */
6026 if (DECL_NAME (current_function_decl)
6027 && MAIN_NAME_P (DECL_NAME (current_function_decl))
6028 && DECL_FILE_SCOPE_P (current_function_decl))
6029 expand_main_function ();
6031 /* Initialize the stack_protect_guard field. This must happen after the
6032 call to __main (if any) so that the external decl is initialized. */
6033 if (crtl->stack_protect_guard)
6034 stack_protect_prologue ();
6036 expand_phi_nodes (&SA);
6038 /* Register rtl specific functions for cfg. */
6039 rtl_register_cfg_hooks ();
6041 init_block = construct_init_block ();
6043 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
6044 remaining edges later. */
6045 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
6046 e->flags &= ~EDGE_EXECUTABLE;
6048 lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
6049 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
6050 next_bb)
6051 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
6053 if (MAY_HAVE_DEBUG_INSNS)
6054 expand_debug_locations ();
6056 if (deep_ter_debug_map)
6058 delete deep_ter_debug_map;
6059 deep_ter_debug_map = NULL;
6062 /* Free stuff we no longer need after GIMPLE optimizations. */
6063 free_dominance_info (CDI_DOMINATORS);
6064 free_dominance_info (CDI_POST_DOMINATORS);
6065 delete_tree_cfg_annotations ();
6067 timevar_push (TV_OUT_OF_SSA);
6068 finish_out_of_ssa (&SA);
6069 timevar_pop (TV_OUT_OF_SSA);
6071 timevar_push (TV_POST_EXPAND);
6072 /* We are no longer in SSA form. */
6073 fun->gimple_df->in_ssa_p = false;
6074 loops_state_clear (LOOP_CLOSED_SSA);
6076 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
6077 conservatively to true until they are all profile aware. */
6078 delete lab_rtx_for_bb;
6079 free_histograms ();
6081 construct_exit_block ();
6082 insn_locations_finalize ();
6084 if (var_ret_seq)
6086 rtx_insn *after = return_label;
6087 rtx_insn *next = NEXT_INSN (after);
6088 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
6089 after = next;
6090 emit_insn_after (var_ret_seq, after);
6093 /* Zap the tree EH table. */
6094 set_eh_throw_stmt_table (fun, NULL);
6096 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
6097 split edges which edge insertions might do. */
6098 rebuild_jump_labels (get_insns ());
6100 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun),
6101 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6103 edge e;
6104 edge_iterator ei;
6105 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6107 if (e->insns.r)
6109 rebuild_jump_labels_chain (e->insns.r);
6110 /* Put insns after parm birth, but before
6111 NOTE_INSNS_FUNCTION_BEG. */
6112 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (fun)
6113 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
6115 rtx_insn *insns = e->insns.r;
6116 e->insns.r = NULL;
6117 if (NOTE_P (parm_birth_insn)
6118 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
6119 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
6120 else
6121 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
6123 else
6124 commit_one_edge_insertion (e);
6126 else
6127 ei_next (&ei);
6131 /* We're done expanding trees to RTL. */
6132 currently_expanding_to_rtl = 0;
6134 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
6135 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6137 edge e;
6138 edge_iterator ei;
6139 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6141 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
6142 e->flags &= ~EDGE_EXECUTABLE;
6144 /* At the moment not all abnormal edges match the RTL
6145 representation. It is safe to remove them here as
6146 find_many_sub_basic_blocks will rediscover them.
6147 In the future we should get this fixed properly. */
6148 if ((e->flags & EDGE_ABNORMAL)
6149 && !(e->flags & EDGE_SIBCALL))
6150 remove_edge (e);
6151 else
6152 ei_next (&ei);
6156 blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
6157 bitmap_ones (blocks);
6158 find_many_sub_basic_blocks (blocks);
6159 sbitmap_free (blocks);
6160 purge_all_dead_edges ();
6162 expand_stack_alignment ();
6164 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
6165 function. */
6166 if (crtl->tail_call_emit)
6167 fixup_tail_calls ();
6169 /* After initial rtl generation, call back to finish generating
6170 exception support code. We need to do this before cleaning up
6171 the CFG as the code does not expect dead landing pads. */
6172 if (fun->eh->region_tree != NULL)
6173 finish_eh_generation ();
6175 /* Remove unreachable blocks, otherwise we cannot compute dominators
6176 which are needed for loop state verification. As a side-effect
6177 this also compacts blocks.
6178 ??? We cannot remove trivially dead insns here as for example
6179 the DRAP reg on i?86 is not magically live at this point.
6180 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
6181 cleanup_cfg (CLEANUP_NO_INSN_DEL);
6183 #ifdef ENABLE_CHECKING
6184 verify_flow_info ();
6185 #endif
6187 /* Initialize pseudos allocated for hard registers. */
6188 emit_initial_value_sets ();
6190 /* And finally unshare all RTL. */
6191 unshare_all_rtl ();
6193 /* There's no need to defer outputting this function any more; we
6194 know we want to output it. */
6195 DECL_DEFER_OUTPUT (current_function_decl) = 0;
6197 /* Now that we're done expanding trees to RTL, we shouldn't have any
6198 more CONCATs anywhere. */
6199 generating_concat_p = 0;
6201 if (dump_file)
6203 fprintf (dump_file,
6204 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
6205 /* And the pass manager will dump RTL for us. */
6208 /* If we're emitting a nested function, make sure its parent gets
6209 emitted as well. Doing otherwise confuses debug info. */
6211 tree parent;
6212 for (parent = DECL_CONTEXT (current_function_decl);
6213 parent != NULL_TREE;
6214 parent = get_containing_scope (parent))
6215 if (TREE_CODE (parent) == FUNCTION_DECL)
6216 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
6219 /* We are now committed to emitting code for this function. Do any
6220 preparation, such as emitting abstract debug info for the inline
6221 before it gets mangled by optimization. */
6222 if (cgraph_function_possibly_inlined_p (current_function_decl))
6223 (*debug_hooks->outlining_inline_function) (current_function_decl);
6225 TREE_ASM_WRITTEN (current_function_decl) = 1;
6227 /* After expanding, the return labels are no longer needed. */
6228 return_label = NULL;
6229 naked_return_label = NULL;
6231 /* After expanding, the tm_restart map is no longer needed. */
6232 if (fun->gimple_df->tm_restart)
6233 fun->gimple_df->tm_restart = NULL;
6235 /* Tag the blocks with a depth number so that change_scope can find
6236 the common parent easily. */
6237 set_block_levels (DECL_INITIAL (fun->decl), 0);
6238 default_rtl_profile ();
6240 timevar_pop (TV_POST_EXPAND);
6242 return 0;
6245 } // anon namespace
6247 rtl_opt_pass *
6248 make_pass_expand (gcc::context *ctxt)
6250 return new pass_expand (ctxt);