2015-05-22 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cfgexpand.c
blobb190f913fd706025624b8aa7561cda249c0b3f6c
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 "machmode.h"
28 #include "vec.h"
29 #include "double-int.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "stringpool.h"
38 #include "varasm.h"
39 #include "stor-layout.h"
40 #include "stmt.h"
41 #include "print-tree.h"
42 #include "tm_p.h"
43 #include "predict.h"
44 #include "hashtab.h"
45 #include "function.h"
46 #include "dominance.h"
47 #include "cfg.h"
48 #include "cfgrtl.h"
49 #include "cfganal.h"
50 #include "cfgbuild.h"
51 #include "cfgcleanup.h"
52 #include "basic-block.h"
53 #include "insn-codes.h"
54 #include "optabs.h"
55 #include "flags.h"
56 #include "statistics.h"
57 #include "real.h"
58 #include "fixed-value.h"
59 #include "insn-config.h"
60 #include "expmed.h"
61 #include "dojump.h"
62 #include "explow.h"
63 #include "calls.h"
64 #include "emit-rtl.h"
65 #include "expr.h"
66 #include "langhooks.h"
67 #include "bitmap.h"
68 #include "tree-ssa-alias.h"
69 #include "internal-fn.h"
70 #include "tree-eh.h"
71 #include "gimple-expr.h"
72 #include "is-a.h"
73 #include "gimple.h"
74 #include "gimple-iterator.h"
75 #include "gimple-walk.h"
76 #include "gimple-ssa.h"
77 #include "hash-map.h"
78 #include "plugin-api.h"
79 #include "ipa-ref.h"
80 #include "cgraph.h"
81 #include "tree-cfg.h"
82 #include "tree-phinodes.h"
83 #include "ssa-iterators.h"
84 #include "tree-ssanames.h"
85 #include "tree-dfa.h"
86 #include "tree-ssa.h"
87 #include "tree-pass.h"
88 #include "except.h"
89 #include "diagnostic.h"
90 #include "gimple-pretty-print.h"
91 #include "toplev.h"
92 #include "debug.h"
93 #include "params.h"
94 #include "tree-inline.h"
95 #include "value-prof.h"
96 #include "target.h"
97 #include "tree-ssa-live.h"
98 #include "tree-outof-ssa.h"
99 #include "sbitmap.h"
100 #include "cfgloop.h"
101 #include "regs.h" /* For reg_renumber. */
102 #include "insn-attr.h" /* For INSN_SCHEDULING. */
103 #include "asan.h"
104 #include "tree-ssa-address.h"
105 #include "recog.h"
106 #include "output.h"
107 #include "builtins.h"
108 #include "tree-chkp.h"
109 #include "rtl-chkp.h"
111 /* Some systems use __main in a way incompatible with its use in gcc, in these
112 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
113 give the same symbol without quotes for an alternative entry point. You
114 must define both, or neither. */
115 #ifndef NAME__MAIN
116 #define NAME__MAIN "__main"
117 #endif
119 /* This variable holds information helping the rewriting of SSA trees
120 into RTL. */
121 struct ssaexpand SA;
123 /* This variable holds the currently expanded gimple statement for purposes
124 of comminucating the profile info to the builtin expanders. */
125 gimple currently_expanding_gimple_stmt;
127 static rtx expand_debug_expr (tree);
129 /* Return an expression tree corresponding to the RHS of GIMPLE
130 statement STMT. */
132 tree
133 gimple_assign_rhs_to_tree (gimple stmt)
135 tree t;
136 enum gimple_rhs_class grhs_class;
138 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
140 if (grhs_class == GIMPLE_TERNARY_RHS)
141 t = build3 (gimple_assign_rhs_code (stmt),
142 TREE_TYPE (gimple_assign_lhs (stmt)),
143 gimple_assign_rhs1 (stmt),
144 gimple_assign_rhs2 (stmt),
145 gimple_assign_rhs3 (stmt));
146 else if (grhs_class == GIMPLE_BINARY_RHS)
147 t = build2 (gimple_assign_rhs_code (stmt),
148 TREE_TYPE (gimple_assign_lhs (stmt)),
149 gimple_assign_rhs1 (stmt),
150 gimple_assign_rhs2 (stmt));
151 else if (grhs_class == GIMPLE_UNARY_RHS)
152 t = build1 (gimple_assign_rhs_code (stmt),
153 TREE_TYPE (gimple_assign_lhs (stmt)),
154 gimple_assign_rhs1 (stmt));
155 else if (grhs_class == GIMPLE_SINGLE_RHS)
157 t = gimple_assign_rhs1 (stmt);
158 /* Avoid modifying this tree in place below. */
159 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
160 && gimple_location (stmt) != EXPR_LOCATION (t))
161 || (gimple_block (stmt)
162 && currently_expanding_to_rtl
163 && EXPR_P (t)))
164 t = copy_node (t);
166 else
167 gcc_unreachable ();
169 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
170 SET_EXPR_LOCATION (t, gimple_location (stmt));
172 return t;
176 #ifndef STACK_ALIGNMENT_NEEDED
177 #define STACK_ALIGNMENT_NEEDED 1
178 #endif
180 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
182 /* Associate declaration T with storage space X. If T is no
183 SSA name this is exactly SET_DECL_RTL, otherwise make the
184 partition of T associated with X. */
185 static inline void
186 set_rtl (tree t, rtx x)
188 if (TREE_CODE (t) == SSA_NAME)
190 SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
191 if (x && !MEM_P (x))
192 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
193 /* For the benefit of debug information at -O0 (where vartracking
194 doesn't run) record the place also in the base DECL if it's
195 a normal variable (not a parameter). */
196 if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
198 tree var = SSA_NAME_VAR (t);
199 /* If we don't yet have something recorded, just record it now. */
200 if (!DECL_RTL_SET_P (var))
201 SET_DECL_RTL (var, x);
202 /* If we have it set already to "multiple places" don't
203 change this. */
204 else if (DECL_RTL (var) == pc_rtx)
206 /* If we have something recorded and it's not the same place
207 as we want to record now, we have multiple partitions for the
208 same base variable, with different places. We can't just
209 randomly chose one, hence we have to say that we don't know.
210 This only happens with optimization, and there var-tracking
211 will figure out the right thing. */
212 else if (DECL_RTL (var) != x)
213 SET_DECL_RTL (var, pc_rtx);
216 else
217 SET_DECL_RTL (t, x);
220 /* This structure holds data relevant to one variable that will be
221 placed in a stack slot. */
222 struct stack_var
224 /* The Variable. */
225 tree decl;
227 /* Initially, the size of the variable. Later, the size of the partition,
228 if this variable becomes it's partition's representative. */
229 HOST_WIDE_INT size;
231 /* The *byte* alignment required for this variable. Or as, with the
232 size, the alignment for this partition. */
233 unsigned int alignb;
235 /* The partition representative. */
236 size_t representative;
238 /* The next stack variable in the partition, or EOC. */
239 size_t next;
241 /* The numbers of conflicting stack variables. */
242 bitmap conflicts;
245 #define EOC ((size_t)-1)
247 /* We have an array of such objects while deciding allocation. */
248 static struct stack_var *stack_vars;
249 static size_t stack_vars_alloc;
250 static size_t stack_vars_num;
251 static hash_map<tree, size_t> *decl_to_stack_part;
253 /* Conflict bitmaps go on this obstack. This allows us to destroy
254 all of them in one big sweep. */
255 static bitmap_obstack stack_var_bitmap_obstack;
257 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
258 is non-decreasing. */
259 static size_t *stack_vars_sorted;
261 /* The phase of the stack frame. This is the known misalignment of
262 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
263 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
264 static int frame_phase;
266 /* Used during expand_used_vars to remember if we saw any decls for
267 which we'd like to enable stack smashing protection. */
268 static bool has_protected_decls;
270 /* Used during expand_used_vars. Remember if we say a character buffer
271 smaller than our cutoff threshold. Used for -Wstack-protector. */
272 static bool has_short_buffer;
274 /* Compute the byte alignment to use for DECL. Ignore alignment
275 we can't do with expected alignment of the stack boundary. */
277 static unsigned int
278 align_local_variable (tree decl)
280 unsigned int align = LOCAL_DECL_ALIGNMENT (decl);
281 DECL_ALIGN (decl) = align;
282 return align / BITS_PER_UNIT;
285 /* Align given offset BASE with ALIGN. Truncate up if ALIGN_UP is true,
286 down otherwise. Return truncated BASE value. */
288 static inline unsigned HOST_WIDE_INT
289 align_base (HOST_WIDE_INT base, unsigned HOST_WIDE_INT align, bool align_up)
291 return align_up ? (base + align - 1) & -align : base & -align;
294 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
295 Return the frame offset. */
297 static HOST_WIDE_INT
298 alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
300 HOST_WIDE_INT offset, new_frame_offset;
302 if (FRAME_GROWS_DOWNWARD)
304 new_frame_offset
305 = align_base (frame_offset - frame_phase - size,
306 align, false) + frame_phase;
307 offset = new_frame_offset;
309 else
311 new_frame_offset
312 = align_base (frame_offset - frame_phase, align, true) + frame_phase;
313 offset = new_frame_offset;
314 new_frame_offset += size;
316 frame_offset = new_frame_offset;
318 if (frame_offset_overflow (frame_offset, cfun->decl))
319 frame_offset = offset = 0;
321 return offset;
324 /* Accumulate DECL into STACK_VARS. */
326 static void
327 add_stack_var (tree decl)
329 struct stack_var *v;
331 if (stack_vars_num >= stack_vars_alloc)
333 if (stack_vars_alloc)
334 stack_vars_alloc = stack_vars_alloc * 3 / 2;
335 else
336 stack_vars_alloc = 32;
337 stack_vars
338 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
340 if (!decl_to_stack_part)
341 decl_to_stack_part = new hash_map<tree, size_t>;
343 v = &stack_vars[stack_vars_num];
344 decl_to_stack_part->put (decl, stack_vars_num);
346 v->decl = decl;
347 v->size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (decl)));
348 /* Ensure that all variables have size, so that &a != &b for any two
349 variables that are simultaneously live. */
350 if (v->size == 0)
351 v->size = 1;
352 v->alignb = align_local_variable (SSAVAR (decl));
353 /* An alignment of zero can mightily confuse us later. */
354 gcc_assert (v->alignb != 0);
356 /* All variables are initially in their own partition. */
357 v->representative = stack_vars_num;
358 v->next = EOC;
360 /* All variables initially conflict with no other. */
361 v->conflicts = NULL;
363 /* Ensure that this decl doesn't get put onto the list twice. */
364 set_rtl (decl, pc_rtx);
366 stack_vars_num++;
369 /* Make the decls associated with luid's X and Y conflict. */
371 static void
372 add_stack_var_conflict (size_t x, size_t y)
374 struct stack_var *a = &stack_vars[x];
375 struct stack_var *b = &stack_vars[y];
376 if (!a->conflicts)
377 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
378 if (!b->conflicts)
379 b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
380 bitmap_set_bit (a->conflicts, y);
381 bitmap_set_bit (b->conflicts, x);
384 /* Check whether the decls associated with luid's X and Y conflict. */
386 static bool
387 stack_var_conflict_p (size_t x, size_t y)
389 struct stack_var *a = &stack_vars[x];
390 struct stack_var *b = &stack_vars[y];
391 if (x == y)
392 return false;
393 /* Partitions containing an SSA name result from gimple registers
394 with things like unsupported modes. They are top-level and
395 hence conflict with everything else. */
396 if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
397 return true;
399 if (!a->conflicts || !b->conflicts)
400 return false;
401 return bitmap_bit_p (a->conflicts, y);
404 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
405 enter its partition number into bitmap DATA. */
407 static bool
408 visit_op (gimple, tree op, tree, void *data)
410 bitmap active = (bitmap)data;
411 op = get_base_address (op);
412 if (op
413 && DECL_P (op)
414 && DECL_RTL_IF_SET (op) == pc_rtx)
416 size_t *v = decl_to_stack_part->get (op);
417 if (v)
418 bitmap_set_bit (active, *v);
420 return false;
423 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
424 record conflicts between it and all currently active other partitions
425 from bitmap DATA. */
427 static bool
428 visit_conflict (gimple, tree op, tree, void *data)
430 bitmap active = (bitmap)data;
431 op = get_base_address (op);
432 if (op
433 && DECL_P (op)
434 && DECL_RTL_IF_SET (op) == pc_rtx)
436 size_t *v = decl_to_stack_part->get (op);
437 if (v && bitmap_set_bit (active, *v))
439 size_t num = *v;
440 bitmap_iterator bi;
441 unsigned i;
442 gcc_assert (num < stack_vars_num);
443 EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
444 add_stack_var_conflict (num, i);
447 return false;
450 /* Helper routine for add_scope_conflicts, calculating the active partitions
451 at the end of BB, leaving the result in WORK. We're called to generate
452 conflicts when FOR_CONFLICT is true, otherwise we're just tracking
453 liveness. */
455 static void
456 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
458 edge e;
459 edge_iterator ei;
460 gimple_stmt_iterator gsi;
461 walk_stmt_load_store_addr_fn visit;
463 bitmap_clear (work);
464 FOR_EACH_EDGE (e, ei, bb->preds)
465 bitmap_ior_into (work, (bitmap)e->src->aux);
467 visit = visit_op;
469 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
471 gimple stmt = gsi_stmt (gsi);
472 walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
474 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
476 gimple stmt = gsi_stmt (gsi);
478 if (gimple_clobber_p (stmt))
480 tree lhs = gimple_assign_lhs (stmt);
481 size_t *v;
482 /* Nested function lowering might introduce LHSs
483 that are COMPONENT_REFs. */
484 if (TREE_CODE (lhs) != VAR_DECL)
485 continue;
486 if (DECL_RTL_IF_SET (lhs) == pc_rtx
487 && (v = decl_to_stack_part->get (lhs)))
488 bitmap_clear_bit (work, *v);
490 else if (!is_gimple_debug (stmt))
492 if (for_conflict
493 && visit == visit_op)
495 /* If this is the first real instruction in this BB we need
496 to add conflicts for everything live at this point now.
497 Unlike classical liveness for named objects we can't
498 rely on seeing a def/use of the names we're interested in.
499 There might merely be indirect loads/stores. We'd not add any
500 conflicts for such partitions. */
501 bitmap_iterator bi;
502 unsigned i;
503 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
505 struct stack_var *a = &stack_vars[i];
506 if (!a->conflicts)
507 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
508 bitmap_ior_into (a->conflicts, work);
510 visit = visit_conflict;
512 walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
517 /* Generate stack partition conflicts between all partitions that are
518 simultaneously live. */
520 static void
521 add_scope_conflicts (void)
523 basic_block bb;
524 bool changed;
525 bitmap work = BITMAP_ALLOC (NULL);
526 int *rpo;
527 int n_bbs;
529 /* We approximate the live range of a stack variable by taking the first
530 mention of its name as starting point(s), and by the end-of-scope
531 death clobber added by gimplify as ending point(s) of the range.
532 This overapproximates in the case we for instance moved an address-taken
533 operation upward, without also moving a dereference to it upwards.
534 But it's conservatively correct as a variable never can hold values
535 before its name is mentioned at least once.
537 We then do a mostly classical bitmap liveness algorithm. */
539 FOR_ALL_BB_FN (bb, cfun)
540 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
542 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
543 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
545 changed = true;
546 while (changed)
548 int i;
549 changed = false;
550 for (i = 0; i < n_bbs; i++)
552 bitmap active;
553 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
554 active = (bitmap)bb->aux;
555 add_scope_conflicts_1 (bb, work, false);
556 if (bitmap_ior_into (active, work))
557 changed = true;
561 FOR_EACH_BB_FN (bb, cfun)
562 add_scope_conflicts_1 (bb, work, true);
564 free (rpo);
565 BITMAP_FREE (work);
566 FOR_ALL_BB_FN (bb, cfun)
567 BITMAP_FREE (bb->aux);
570 /* A subroutine of partition_stack_vars. A comparison function for qsort,
571 sorting an array of indices by the properties of the object. */
573 static int
574 stack_var_cmp (const void *a, const void *b)
576 size_t ia = *(const size_t *)a;
577 size_t ib = *(const size_t *)b;
578 unsigned int aligna = stack_vars[ia].alignb;
579 unsigned int alignb = stack_vars[ib].alignb;
580 HOST_WIDE_INT sizea = stack_vars[ia].size;
581 HOST_WIDE_INT sizeb = stack_vars[ib].size;
582 tree decla = stack_vars[ia].decl;
583 tree declb = stack_vars[ib].decl;
584 bool largea, largeb;
585 unsigned int uida, uidb;
587 /* Primary compare on "large" alignment. Large comes first. */
588 largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
589 largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
590 if (largea != largeb)
591 return (int)largeb - (int)largea;
593 /* Secondary compare on size, decreasing */
594 if (sizea > sizeb)
595 return -1;
596 if (sizea < sizeb)
597 return 1;
599 /* Tertiary compare on true alignment, decreasing. */
600 if (aligna < alignb)
601 return -1;
602 if (aligna > alignb)
603 return 1;
605 /* Final compare on ID for sort stability, increasing.
606 Two SSA names are compared by their version, SSA names come before
607 non-SSA names, and two normal decls are compared by their DECL_UID. */
608 if (TREE_CODE (decla) == SSA_NAME)
610 if (TREE_CODE (declb) == SSA_NAME)
611 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
612 else
613 return -1;
615 else if (TREE_CODE (declb) == SSA_NAME)
616 return 1;
617 else
618 uida = DECL_UID (decla), uidb = DECL_UID (declb);
619 if (uida < uidb)
620 return 1;
621 if (uida > uidb)
622 return -1;
623 return 0;
626 struct part_traits : default_hashmap_traits
628 template<typename T>
629 static bool
630 is_deleted (T &e)
631 { return e.m_value == reinterpret_cast<void *> (1); }
633 template<typename T> static bool is_empty (T &e) { return e.m_value == NULL; }
634 template<typename T>
635 static void
636 mark_deleted (T &e)
637 { e.m_value = reinterpret_cast<T> (1); }
639 template<typename T>
640 static void
641 mark_empty (T &e)
642 { e.m_value = NULL; }
645 typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
647 /* If the points-to solution *PI points to variables that are in a partition
648 together with other variables add all partition members to the pointed-to
649 variables bitmap. */
651 static void
652 add_partitioned_vars_to_ptset (struct pt_solution *pt,
653 part_hashmap *decls_to_partitions,
654 hash_set<bitmap> *visited, bitmap temp)
656 bitmap_iterator bi;
657 unsigned i;
658 bitmap *part;
660 if (pt->anything
661 || pt->vars == NULL
662 /* The pointed-to vars bitmap is shared, it is enough to
663 visit it once. */
664 || visited->add (pt->vars))
665 return;
667 bitmap_clear (temp);
669 /* By using a temporary bitmap to store all members of the partitions
670 we have to add we make sure to visit each of the partitions only
671 once. */
672 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
673 if ((!temp
674 || !bitmap_bit_p (temp, i))
675 && (part = decls_to_partitions->get (i)))
676 bitmap_ior_into (temp, *part);
677 if (!bitmap_empty_p (temp))
678 bitmap_ior_into (pt->vars, temp);
681 /* Update points-to sets based on partition info, so we can use them on RTL.
682 The bitmaps representing stack partitions will be saved until expand,
683 where partitioned decls used as bases in memory expressions will be
684 rewritten. */
686 static void
687 update_alias_info_with_stack_vars (void)
689 part_hashmap *decls_to_partitions = NULL;
690 size_t i, j;
691 tree var = NULL_TREE;
693 for (i = 0; i < stack_vars_num; i++)
695 bitmap part = NULL;
696 tree name;
697 struct ptr_info_def *pi;
699 /* Not interested in partitions with single variable. */
700 if (stack_vars[i].representative != i
701 || stack_vars[i].next == EOC)
702 continue;
704 if (!decls_to_partitions)
706 decls_to_partitions = new part_hashmap;
707 cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
710 /* Create an SSA_NAME that points to the partition for use
711 as base during alias-oracle queries on RTL for bases that
712 have been partitioned. */
713 if (var == NULL_TREE)
714 var = create_tmp_var (ptr_type_node);
715 name = make_ssa_name (var);
717 /* Create bitmaps representing partitions. They will be used for
718 points-to sets later, so use GGC alloc. */
719 part = BITMAP_GGC_ALLOC ();
720 for (j = i; j != EOC; j = stack_vars[j].next)
722 tree decl = stack_vars[j].decl;
723 unsigned int uid = DECL_PT_UID (decl);
724 bitmap_set_bit (part, uid);
725 decls_to_partitions->put (uid, part);
726 cfun->gimple_df->decls_to_pointers->put (decl, name);
727 if (TREE_ADDRESSABLE (decl))
728 TREE_ADDRESSABLE (name) = 1;
731 /* Make the SSA name point to all partition members. */
732 pi = get_ptr_info (name);
733 pt_solution_set (&pi->pt, part, false);
736 /* Make all points-to sets that contain one member of a partition
737 contain all members of the partition. */
738 if (decls_to_partitions)
740 unsigned i;
741 hash_set<bitmap> visited;
742 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
744 for (i = 1; i < num_ssa_names; i++)
746 tree name = ssa_name (i);
747 struct ptr_info_def *pi;
749 if (name
750 && POINTER_TYPE_P (TREE_TYPE (name))
751 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
752 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
753 &visited, temp);
756 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
757 decls_to_partitions, &visited, temp);
759 delete decls_to_partitions;
760 BITMAP_FREE (temp);
764 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
765 partitioning algorithm. Partitions A and B are known to be non-conflicting.
766 Merge them into a single partition A. */
768 static void
769 union_stack_vars (size_t a, size_t b)
771 struct stack_var *vb = &stack_vars[b];
772 bitmap_iterator bi;
773 unsigned u;
775 gcc_assert (stack_vars[b].next == EOC);
776 /* Add B to A's partition. */
777 stack_vars[b].next = stack_vars[a].next;
778 stack_vars[b].representative = a;
779 stack_vars[a].next = b;
781 /* Update the required alignment of partition A to account for B. */
782 if (stack_vars[a].alignb < stack_vars[b].alignb)
783 stack_vars[a].alignb = stack_vars[b].alignb;
785 /* Update the interference graph and merge the conflicts. */
786 if (vb->conflicts)
788 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
789 add_stack_var_conflict (a, stack_vars[u].representative);
790 BITMAP_FREE (vb->conflicts);
794 /* A subroutine of expand_used_vars. Binpack the variables into
795 partitions constrained by the interference graph. The overall
796 algorithm used is as follows:
798 Sort the objects by size in descending order.
799 For each object A {
800 S = size(A)
801 O = 0
802 loop {
803 Look for the largest non-conflicting object B with size <= S.
804 UNION (A, B)
809 static void
810 partition_stack_vars (void)
812 size_t si, sj, n = stack_vars_num;
814 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
815 for (si = 0; si < n; ++si)
816 stack_vars_sorted[si] = si;
818 if (n == 1)
819 return;
821 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
823 for (si = 0; si < n; ++si)
825 size_t i = stack_vars_sorted[si];
826 unsigned int ialign = stack_vars[i].alignb;
827 HOST_WIDE_INT isize = stack_vars[i].size;
829 /* Ignore objects that aren't partition representatives. If we
830 see a var that is not a partition representative, it must
831 have been merged earlier. */
832 if (stack_vars[i].representative != i)
833 continue;
835 for (sj = si + 1; sj < n; ++sj)
837 size_t j = stack_vars_sorted[sj];
838 unsigned int jalign = stack_vars[j].alignb;
839 HOST_WIDE_INT jsize = stack_vars[j].size;
841 /* Ignore objects that aren't partition representatives. */
842 if (stack_vars[j].representative != j)
843 continue;
845 /* Do not mix objects of "small" (supported) alignment
846 and "large" (unsupported) alignment. */
847 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
848 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
849 break;
851 /* For Address Sanitizer do not mix objects with different
852 sizes, as the shorter vars wouldn't be adequately protected.
853 Don't do that for "large" (unsupported) alignment objects,
854 those aren't protected anyway. */
855 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && isize != jsize
856 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
857 break;
859 /* Ignore conflicting objects. */
860 if (stack_var_conflict_p (i, j))
861 continue;
863 /* UNION the objects, placing J at OFFSET. */
864 union_stack_vars (i, j);
868 update_alias_info_with_stack_vars ();
871 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
873 static void
874 dump_stack_var_partition (void)
876 size_t si, i, j, n = stack_vars_num;
878 for (si = 0; si < n; ++si)
880 i = stack_vars_sorted[si];
882 /* Skip variables that aren't partition representatives, for now. */
883 if (stack_vars[i].representative != i)
884 continue;
886 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
887 " align %u\n", (unsigned long) i, stack_vars[i].size,
888 stack_vars[i].alignb);
890 for (j = i; j != EOC; j = stack_vars[j].next)
892 fputc ('\t', dump_file);
893 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
895 fputc ('\n', dump_file);
899 /* Assign rtl to DECL at BASE + OFFSET. */
901 static void
902 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
903 HOST_WIDE_INT offset)
905 unsigned align;
906 rtx x;
908 /* If this fails, we've overflowed the stack frame. Error nicely? */
909 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
911 x = plus_constant (Pmode, base, offset);
912 x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
914 if (TREE_CODE (decl) != SSA_NAME)
916 /* Set alignment we actually gave this decl if it isn't an SSA name.
917 If it is we generate stack slots only accidentally so it isn't as
918 important, we'll simply use the alignment that is already set. */
919 if (base == virtual_stack_vars_rtx)
920 offset -= frame_phase;
921 align = offset & -offset;
922 align *= BITS_PER_UNIT;
923 if (align == 0 || align > base_align)
924 align = base_align;
926 /* One would think that we could assert that we're not decreasing
927 alignment here, but (at least) the i386 port does exactly this
928 via the MINIMUM_ALIGNMENT hook. */
930 DECL_ALIGN (decl) = align;
931 DECL_USER_ALIGN (decl) = 0;
934 set_mem_attributes (x, SSAVAR (decl), true);
935 set_rtl (decl, x);
938 struct stack_vars_data
940 /* Vector of offset pairs, always end of some padding followed
941 by start of the padding that needs Address Sanitizer protection.
942 The vector is in reversed, highest offset pairs come first. */
943 vec<HOST_WIDE_INT> asan_vec;
945 /* Vector of partition representative decls in between the paddings. */
946 vec<tree> asan_decl_vec;
948 /* Base pseudo register for Address Sanitizer protected automatic vars. */
949 rtx asan_base;
951 /* Alignment needed for the Address Sanitizer protected automatic vars. */
952 unsigned int asan_alignb;
955 /* A subroutine of expand_used_vars. Give each partition representative
956 a unique location within the stack frame. Update each partition member
957 with that location. */
959 static void
960 expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
962 size_t si, i, j, n = stack_vars_num;
963 HOST_WIDE_INT large_size = 0, large_alloc = 0;
964 rtx large_base = NULL;
965 unsigned large_align = 0;
966 tree decl;
968 /* Determine if there are any variables requiring "large" alignment.
969 Since these are dynamically allocated, we only process these if
970 no predicate involved. */
971 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
972 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
974 /* Find the total size of these variables. */
975 for (si = 0; si < n; ++si)
977 unsigned alignb;
979 i = stack_vars_sorted[si];
980 alignb = stack_vars[i].alignb;
982 /* All "large" alignment decls come before all "small" alignment
983 decls, but "large" alignment decls are not sorted based on
984 their alignment. Increase large_align to track the largest
985 required alignment. */
986 if ((alignb * BITS_PER_UNIT) > large_align)
987 large_align = alignb * BITS_PER_UNIT;
989 /* Stop when we get to the first decl with "small" alignment. */
990 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
991 break;
993 /* Skip variables that aren't partition representatives. */
994 if (stack_vars[i].representative != i)
995 continue;
997 /* Skip variables that have already had rtl assigned. See also
998 add_stack_var where we perpetrate this pc_rtx hack. */
999 decl = stack_vars[i].decl;
1000 if ((TREE_CODE (decl) == SSA_NAME
1001 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
1002 : DECL_RTL (decl)) != pc_rtx)
1003 continue;
1005 large_size += alignb - 1;
1006 large_size &= -(HOST_WIDE_INT)alignb;
1007 large_size += stack_vars[i].size;
1010 /* If there were any, allocate space. */
1011 if (large_size > 0)
1012 large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
1013 large_align, true);
1016 for (si = 0; si < n; ++si)
1018 rtx base;
1019 unsigned base_align, alignb;
1020 HOST_WIDE_INT offset;
1022 i = stack_vars_sorted[si];
1024 /* Skip variables that aren't partition representatives, for now. */
1025 if (stack_vars[i].representative != i)
1026 continue;
1028 /* Skip variables that have already had rtl assigned. See also
1029 add_stack_var where we perpetrate this pc_rtx hack. */
1030 decl = stack_vars[i].decl;
1031 if ((TREE_CODE (decl) == SSA_NAME
1032 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
1033 : DECL_RTL (decl)) != pc_rtx)
1034 continue;
1036 /* Check the predicate to see whether this variable should be
1037 allocated in this pass. */
1038 if (pred && !pred (i))
1039 continue;
1041 alignb = stack_vars[i].alignb;
1042 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1044 base = virtual_stack_vars_rtx;
1045 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && pred)
1047 HOST_WIDE_INT prev_offset
1048 = align_base (frame_offset,
1049 MAX (alignb, ASAN_RED_ZONE_SIZE),
1050 FRAME_GROWS_DOWNWARD);
1051 tree repr_decl = NULL_TREE;
1052 offset
1053 = alloc_stack_frame_space (stack_vars[i].size
1054 + ASAN_RED_ZONE_SIZE,
1055 MAX (alignb, ASAN_RED_ZONE_SIZE));
1057 data->asan_vec.safe_push (prev_offset);
1058 data->asan_vec.safe_push (offset + stack_vars[i].size);
1059 /* Find best representative of the partition.
1060 Prefer those with DECL_NAME, even better
1061 satisfying asan_protect_stack_decl predicate. */
1062 for (j = i; j != EOC; j = stack_vars[j].next)
1063 if (asan_protect_stack_decl (stack_vars[j].decl)
1064 && DECL_NAME (stack_vars[j].decl))
1066 repr_decl = stack_vars[j].decl;
1067 break;
1069 else if (repr_decl == NULL_TREE
1070 && DECL_P (stack_vars[j].decl)
1071 && DECL_NAME (stack_vars[j].decl))
1072 repr_decl = stack_vars[j].decl;
1073 if (repr_decl == NULL_TREE)
1074 repr_decl = stack_vars[i].decl;
1075 data->asan_decl_vec.safe_push (repr_decl);
1076 data->asan_alignb = MAX (data->asan_alignb, alignb);
1077 if (data->asan_base == NULL)
1078 data->asan_base = gen_reg_rtx (Pmode);
1079 base = data->asan_base;
1081 if (!STRICT_ALIGNMENT)
1082 base_align = crtl->max_used_stack_slot_alignment;
1083 else
1084 base_align = MAX (crtl->max_used_stack_slot_alignment,
1085 GET_MODE_ALIGNMENT (SImode)
1086 << ASAN_SHADOW_SHIFT);
1088 else
1090 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1091 base_align = crtl->max_used_stack_slot_alignment;
1094 else
1096 /* Large alignment is only processed in the last pass. */
1097 if (pred)
1098 continue;
1099 gcc_assert (large_base != NULL);
1101 large_alloc += alignb - 1;
1102 large_alloc &= -(HOST_WIDE_INT)alignb;
1103 offset = large_alloc;
1104 large_alloc += stack_vars[i].size;
1106 base = large_base;
1107 base_align = large_align;
1110 /* Create rtl for each variable based on their location within the
1111 partition. */
1112 for (j = i; j != EOC; j = stack_vars[j].next)
1114 expand_one_stack_var_at (stack_vars[j].decl,
1115 base, base_align,
1116 offset);
1120 gcc_assert (large_alloc == large_size);
1123 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1124 static HOST_WIDE_INT
1125 account_stack_vars (void)
1127 size_t si, j, i, n = stack_vars_num;
1128 HOST_WIDE_INT size = 0;
1130 for (si = 0; si < n; ++si)
1132 i = stack_vars_sorted[si];
1134 /* Skip variables that aren't partition representatives, for now. */
1135 if (stack_vars[i].representative != i)
1136 continue;
1138 size += stack_vars[i].size;
1139 for (j = i; j != EOC; j = stack_vars[j].next)
1140 set_rtl (stack_vars[j].decl, NULL);
1142 return size;
1145 /* A subroutine of expand_one_var. Called to immediately assign rtl
1146 to a variable to be allocated in the stack frame. */
1148 static void
1149 expand_one_stack_var (tree var)
1151 HOST_WIDE_INT size, offset;
1152 unsigned byte_align;
1154 size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (var)));
1155 byte_align = align_local_variable (SSAVAR (var));
1157 /* We handle highly aligned variables in expand_stack_vars. */
1158 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1160 offset = alloc_stack_frame_space (size, byte_align);
1162 expand_one_stack_var_at (var, virtual_stack_vars_rtx,
1163 crtl->max_used_stack_slot_alignment, offset);
1166 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1167 that will reside in a hard register. */
1169 static void
1170 expand_one_hard_reg_var (tree var)
1172 rest_of_decl_compilation (var, 0, 0);
1175 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1176 that will reside in a pseudo register. */
1178 static void
1179 expand_one_register_var (tree var)
1181 tree decl = SSAVAR (var);
1182 tree type = TREE_TYPE (decl);
1183 machine_mode reg_mode = promote_decl_mode (decl, NULL);
1184 rtx x = gen_reg_rtx (reg_mode);
1186 set_rtl (var, x);
1188 /* Note if the object is a user variable. */
1189 if (!DECL_ARTIFICIAL (decl))
1190 mark_user_reg (x);
1192 if (POINTER_TYPE_P (type))
1193 mark_reg_pointer (x, get_pointer_alignment (var));
1196 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1197 has some associated error, e.g. its type is error-mark. We just need
1198 to pick something that won't crash the rest of the compiler. */
1200 static void
1201 expand_one_error_var (tree var)
1203 machine_mode mode = DECL_MODE (var);
1204 rtx x;
1206 if (mode == BLKmode)
1207 x = gen_rtx_MEM (BLKmode, const0_rtx);
1208 else if (mode == VOIDmode)
1209 x = const0_rtx;
1210 else
1211 x = gen_reg_rtx (mode);
1213 SET_DECL_RTL (var, x);
1216 /* A subroutine of expand_one_var. VAR is a variable that will be
1217 allocated to the local stack frame. Return true if we wish to
1218 add VAR to STACK_VARS so that it will be coalesced with other
1219 variables. Return false to allocate VAR immediately.
1221 This function is used to reduce the number of variables considered
1222 for coalescing, which reduces the size of the quadratic problem. */
1224 static bool
1225 defer_stack_allocation (tree var, bool toplevel)
1227 /* Whether the variable is small enough for immediate allocation not to be
1228 a problem with regard to the frame size. */
1229 bool smallish
1230 = ((HOST_WIDE_INT) tree_to_uhwi (DECL_SIZE_UNIT (var))
1231 < PARAM_VALUE (PARAM_MIN_SIZE_FOR_STACK_SHARING));
1233 /* If stack protection is enabled, *all* stack variables must be deferred,
1234 so that we can re-order the strings to the top of the frame.
1235 Similarly for Address Sanitizer. */
1236 if (flag_stack_protect || ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK))
1237 return true;
1239 /* We handle "large" alignment via dynamic allocation. We want to handle
1240 this extra complication in only one place, so defer them. */
1241 if (DECL_ALIGN (var) > MAX_SUPPORTED_STACK_ALIGNMENT)
1242 return true;
1244 /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1245 might be detached from their block and appear at toplevel when we reach
1246 here. We want to coalesce them with variables from other blocks when
1247 the immediate contribution to the frame size would be noticeable. */
1248 if (toplevel && optimize > 0 && DECL_IGNORED_P (var) && !smallish)
1249 return true;
1251 /* Variables declared in the outermost scope automatically conflict
1252 with every other variable. The only reason to want to defer them
1253 at all is that, after sorting, we can more efficiently pack
1254 small variables in the stack frame. Continue to defer at -O2. */
1255 if (toplevel && optimize < 2)
1256 return false;
1258 /* Without optimization, *most* variables are allocated from the
1259 stack, which makes the quadratic problem large exactly when we
1260 want compilation to proceed as quickly as possible. On the
1261 other hand, we don't want the function's stack frame size to
1262 get completely out of hand. So we avoid adding scalars and
1263 "small" aggregates to the list at all. */
1264 if (optimize == 0 && smallish)
1265 return false;
1267 return true;
1270 /* A subroutine of expand_used_vars. Expand one variable according to
1271 its flavor. Variables to be placed on the stack are not actually
1272 expanded yet, merely recorded.
1273 When REALLY_EXPAND is false, only add stack values to be allocated.
1274 Return stack usage this variable is supposed to take.
1277 static HOST_WIDE_INT
1278 expand_one_var (tree var, bool toplevel, bool really_expand)
1280 unsigned int align = BITS_PER_UNIT;
1281 tree origvar = var;
1283 var = SSAVAR (var);
1285 if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
1287 /* Because we don't know if VAR will be in register or on stack,
1288 we conservatively assume it will be on stack even if VAR is
1289 eventually put into register after RA pass. For non-automatic
1290 variables, which won't be on stack, we collect alignment of
1291 type and ignore user specified alignment. Similarly for
1292 SSA_NAMEs for which use_register_for_decl returns true. */
1293 if (TREE_STATIC (var)
1294 || DECL_EXTERNAL (var)
1295 || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1296 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1297 TYPE_MODE (TREE_TYPE (var)),
1298 TYPE_ALIGN (TREE_TYPE (var)));
1299 else if (DECL_HAS_VALUE_EXPR_P (var)
1300 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1301 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1302 or variables which were assigned a stack slot already by
1303 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1304 changed from the offset chosen to it. */
1305 align = crtl->stack_alignment_estimated;
1306 else
1307 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1309 /* If the variable alignment is very large we'll dynamicaly allocate
1310 it, which means that in-frame portion is just a pointer. */
1311 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1312 align = POINTER_SIZE;
1315 if (SUPPORTS_STACK_ALIGNMENT
1316 && crtl->stack_alignment_estimated < align)
1318 /* stack_alignment_estimated shouldn't change after stack
1319 realign decision made */
1320 gcc_assert (!crtl->stack_realign_processed);
1321 crtl->stack_alignment_estimated = align;
1324 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1325 So here we only make sure stack_alignment_needed >= align. */
1326 if (crtl->stack_alignment_needed < align)
1327 crtl->stack_alignment_needed = align;
1328 if (crtl->max_used_stack_slot_alignment < align)
1329 crtl->max_used_stack_slot_alignment = align;
1331 if (TREE_CODE (origvar) == SSA_NAME)
1333 gcc_assert (TREE_CODE (var) != VAR_DECL
1334 || (!DECL_EXTERNAL (var)
1335 && !DECL_HAS_VALUE_EXPR_P (var)
1336 && !TREE_STATIC (var)
1337 && TREE_TYPE (var) != error_mark_node
1338 && !DECL_HARD_REGISTER (var)
1339 && really_expand));
1341 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1343 else if (DECL_EXTERNAL (var))
1345 else if (DECL_HAS_VALUE_EXPR_P (var))
1347 else if (TREE_STATIC (var))
1349 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1351 else if (TREE_TYPE (var) == error_mark_node)
1353 if (really_expand)
1354 expand_one_error_var (var);
1356 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1358 if (really_expand)
1360 expand_one_hard_reg_var (var);
1361 if (!DECL_HARD_REGISTER (var))
1362 /* Invalid register specification. */
1363 expand_one_error_var (var);
1366 else if (use_register_for_decl (var))
1368 if (really_expand)
1369 expand_one_register_var (origvar);
1371 else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
1373 /* Reject variables which cover more than half of the address-space. */
1374 if (really_expand)
1376 error ("size of variable %q+D is too large", var);
1377 expand_one_error_var (var);
1380 else if (defer_stack_allocation (var, toplevel))
1381 add_stack_var (origvar);
1382 else
1384 if (really_expand)
1385 expand_one_stack_var (origvar);
1386 return tree_to_uhwi (DECL_SIZE_UNIT (var));
1388 return 0;
1391 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1392 expanding variables. Those variables that can be put into registers
1393 are allocated pseudos; those that can't are put on the stack.
1395 TOPLEVEL is true if this is the outermost BLOCK. */
1397 static void
1398 expand_used_vars_for_block (tree block, bool toplevel)
1400 tree t;
1402 /* Expand all variables at this level. */
1403 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1404 if (TREE_USED (t)
1405 && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1406 || !DECL_NONSHAREABLE (t)))
1407 expand_one_var (t, toplevel, true);
1409 /* Expand all variables at containing levels. */
1410 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1411 expand_used_vars_for_block (t, false);
1414 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1415 and clear TREE_USED on all local variables. */
1417 static void
1418 clear_tree_used (tree block)
1420 tree t;
1422 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1423 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1424 if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1425 || !DECL_NONSHAREABLE (t))
1426 TREE_USED (t) = 0;
1428 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1429 clear_tree_used (t);
1432 enum {
1433 SPCT_FLAG_DEFAULT = 1,
1434 SPCT_FLAG_ALL = 2,
1435 SPCT_FLAG_STRONG = 3,
1436 SPCT_FLAG_EXPLICIT = 4
1439 /* Examine TYPE and determine a bit mask of the following features. */
1441 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1442 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1443 #define SPCT_HAS_ARRAY 4
1444 #define SPCT_HAS_AGGREGATE 8
1446 static unsigned int
1447 stack_protect_classify_type (tree type)
1449 unsigned int ret = 0;
1450 tree t;
1452 switch (TREE_CODE (type))
1454 case ARRAY_TYPE:
1455 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1456 if (t == char_type_node
1457 || t == signed_char_type_node
1458 || t == unsigned_char_type_node)
1460 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1461 unsigned HOST_WIDE_INT len;
1463 if (!TYPE_SIZE_UNIT (type)
1464 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1465 len = max;
1466 else
1467 len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1469 if (len < max)
1470 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1471 else
1472 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1474 else
1475 ret = SPCT_HAS_ARRAY;
1476 break;
1478 case UNION_TYPE:
1479 case QUAL_UNION_TYPE:
1480 case RECORD_TYPE:
1481 ret = SPCT_HAS_AGGREGATE;
1482 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1483 if (TREE_CODE (t) == FIELD_DECL)
1484 ret |= stack_protect_classify_type (TREE_TYPE (t));
1485 break;
1487 default:
1488 break;
1491 return ret;
1494 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1495 part of the local stack frame. Remember if we ever return nonzero for
1496 any variable in this function. The return value is the phase number in
1497 which the variable should be allocated. */
1499 static int
1500 stack_protect_decl_phase (tree decl)
1502 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1503 int ret = 0;
1505 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1506 has_short_buffer = true;
1508 if (flag_stack_protect == SPCT_FLAG_ALL
1509 || flag_stack_protect == SPCT_FLAG_STRONG
1510 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1511 && lookup_attribute ("stack_protect",
1512 DECL_ATTRIBUTES (current_function_decl))))
1514 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1515 && !(bits & SPCT_HAS_AGGREGATE))
1516 ret = 1;
1517 else if (bits & SPCT_HAS_ARRAY)
1518 ret = 2;
1520 else
1521 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1523 if (ret)
1524 has_protected_decls = true;
1526 return ret;
1529 /* Two helper routines that check for phase 1 and phase 2. These are used
1530 as callbacks for expand_stack_vars. */
1532 static bool
1533 stack_protect_decl_phase_1 (size_t i)
1535 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
1538 static bool
1539 stack_protect_decl_phase_2 (size_t i)
1541 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
1544 /* And helper function that checks for asan phase (with stack protector
1545 it is phase 3). This is used as callback for expand_stack_vars.
1546 Returns true if any of the vars in the partition need to be protected. */
1548 static bool
1549 asan_decl_phase_3 (size_t i)
1551 while (i != EOC)
1553 if (asan_protect_stack_decl (stack_vars[i].decl))
1554 return true;
1555 i = stack_vars[i].next;
1557 return false;
1560 /* Ensure that variables in different stack protection phases conflict
1561 so that they are not merged and share the same stack slot. */
1563 static void
1564 add_stack_protection_conflicts (void)
1566 size_t i, j, n = stack_vars_num;
1567 unsigned char *phase;
1569 phase = XNEWVEC (unsigned char, n);
1570 for (i = 0; i < n; ++i)
1571 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1573 for (i = 0; i < n; ++i)
1575 unsigned char ph_i = phase[i];
1576 for (j = i + 1; j < n; ++j)
1577 if (ph_i != phase[j])
1578 add_stack_var_conflict (i, j);
1581 XDELETEVEC (phase);
1584 /* Create a decl for the guard at the top of the stack frame. */
1586 static void
1587 create_stack_guard (void)
1589 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1590 VAR_DECL, NULL, ptr_type_node);
1591 TREE_THIS_VOLATILE (guard) = 1;
1592 TREE_USED (guard) = 1;
1593 expand_one_stack_var (guard);
1594 crtl->stack_protect_guard = guard;
1597 /* Prepare for expanding variables. */
1598 static void
1599 init_vars_expansion (void)
1601 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
1602 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
1604 /* A map from decl to stack partition. */
1605 decl_to_stack_part = new hash_map<tree, size_t>;
1607 /* Initialize local stack smashing state. */
1608 has_protected_decls = false;
1609 has_short_buffer = false;
1612 /* Free up stack variable graph data. */
1613 static void
1614 fini_vars_expansion (void)
1616 bitmap_obstack_release (&stack_var_bitmap_obstack);
1617 if (stack_vars)
1618 XDELETEVEC (stack_vars);
1619 if (stack_vars_sorted)
1620 XDELETEVEC (stack_vars_sorted);
1621 stack_vars = NULL;
1622 stack_vars_sorted = NULL;
1623 stack_vars_alloc = stack_vars_num = 0;
1624 delete decl_to_stack_part;
1625 decl_to_stack_part = NULL;
1628 /* Make a fair guess for the size of the stack frame of the function
1629 in NODE. This doesn't have to be exact, the result is only used in
1630 the inline heuristics. So we don't want to run the full stack var
1631 packing algorithm (which is quadratic in the number of stack vars).
1632 Instead, we calculate the total size of all stack vars. This turns
1633 out to be a pretty fair estimate -- packing of stack vars doesn't
1634 happen very often. */
1636 HOST_WIDE_INT
1637 estimated_stack_frame_size (struct cgraph_node *node)
1639 HOST_WIDE_INT size = 0;
1640 size_t i;
1641 tree var;
1642 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1644 push_cfun (fn);
1646 init_vars_expansion ();
1648 FOR_EACH_LOCAL_DECL (fn, i, var)
1649 if (auto_var_in_fn_p (var, fn->decl))
1650 size += expand_one_var (var, true, false);
1652 if (stack_vars_num > 0)
1654 /* Fake sorting the stack vars for account_stack_vars (). */
1655 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1656 for (i = 0; i < stack_vars_num; ++i)
1657 stack_vars_sorted[i] = i;
1658 size += account_stack_vars ();
1661 fini_vars_expansion ();
1662 pop_cfun ();
1663 return size;
1666 /* Helper routine to check if a record or union contains an array field. */
1668 static int
1669 record_or_union_type_has_array_p (const_tree tree_type)
1671 tree fields = TYPE_FIELDS (tree_type);
1672 tree f;
1674 for (f = fields; f; f = DECL_CHAIN (f))
1675 if (TREE_CODE (f) == FIELD_DECL)
1677 tree field_type = TREE_TYPE (f);
1678 if (RECORD_OR_UNION_TYPE_P (field_type)
1679 && record_or_union_type_has_array_p (field_type))
1680 return 1;
1681 if (TREE_CODE (field_type) == ARRAY_TYPE)
1682 return 1;
1684 return 0;
1687 /* Check if the current function has local referenced variables that
1688 have their addresses taken, contain an array, or are arrays. */
1690 static bool
1691 stack_protect_decl_p ()
1693 unsigned i;
1694 tree var;
1696 FOR_EACH_LOCAL_DECL (cfun, i, var)
1697 if (!is_global_var (var))
1699 tree var_type = TREE_TYPE (var);
1700 if (TREE_CODE (var) == VAR_DECL
1701 && (TREE_CODE (var_type) == ARRAY_TYPE
1702 || TREE_ADDRESSABLE (var)
1703 || (RECORD_OR_UNION_TYPE_P (var_type)
1704 && record_or_union_type_has_array_p (var_type))))
1705 return true;
1707 return false;
1710 /* Check if the current function has calls that use a return slot. */
1712 static bool
1713 stack_protect_return_slot_p ()
1715 basic_block bb;
1717 FOR_ALL_BB_FN (bb, cfun)
1718 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1719 !gsi_end_p (gsi); gsi_next (&gsi))
1721 gimple stmt = gsi_stmt (gsi);
1722 /* This assumes that calls to internal-only functions never
1723 use a return slot. */
1724 if (is_gimple_call (stmt)
1725 && !gimple_call_internal_p (stmt)
1726 && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
1727 gimple_call_fndecl (stmt)))
1728 return true;
1730 return false;
1733 /* Expand all variables used in the function. */
1735 static rtx_insn *
1736 expand_used_vars (void)
1738 tree var, outer_block = DECL_INITIAL (current_function_decl);
1739 vec<tree> maybe_local_decls = vNULL;
1740 rtx_insn *var_end_seq = NULL;
1741 unsigned i;
1742 unsigned len;
1743 bool gen_stack_protect_signal = false;
1745 /* Compute the phase of the stack frame for this function. */
1747 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1748 int off = STARTING_FRAME_OFFSET % align;
1749 frame_phase = off ? align - off : 0;
1752 /* Set TREE_USED on all variables in the local_decls. */
1753 FOR_EACH_LOCAL_DECL (cfun, i, var)
1754 TREE_USED (var) = 1;
1755 /* Clear TREE_USED on all variables associated with a block scope. */
1756 clear_tree_used (DECL_INITIAL (current_function_decl));
1758 init_vars_expansion ();
1760 if (targetm.use_pseudo_pic_reg ())
1761 pic_offset_table_rtx = gen_reg_rtx (Pmode);
1763 hash_map<tree, tree> ssa_name_decls;
1764 for (i = 0; i < SA.map->num_partitions; i++)
1766 tree var = partition_to_var (SA.map, i);
1768 gcc_assert (!virtual_operand_p (var));
1770 /* Assign decls to each SSA name partition, share decls for partitions
1771 we could have coalesced (those with the same type). */
1772 if (SSA_NAME_VAR (var) == NULL_TREE)
1774 tree *slot = &ssa_name_decls.get_or_insert (TREE_TYPE (var));
1775 if (!*slot)
1776 *slot = create_tmp_reg (TREE_TYPE (var));
1777 replace_ssa_name_symbol (var, *slot);
1780 /* Always allocate space for partitions based on VAR_DECLs. But for
1781 those based on PARM_DECLs or RESULT_DECLs and which matter for the
1782 debug info, there is no need to do so if optimization is disabled
1783 because all the SSA_NAMEs based on these DECLs have been coalesced
1784 into a single partition, which is thus assigned the canonical RTL
1785 location of the DECLs. If in_lto_p, we can't rely on optimize,
1786 a function could be compiled with -O1 -flto first and only the
1787 link performed at -O0. */
1788 if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1789 expand_one_var (var, true, true);
1790 else if (DECL_IGNORED_P (SSA_NAME_VAR (var)) || optimize || in_lto_p)
1792 /* This is a PARM_DECL or RESULT_DECL. For those partitions that
1793 contain the default def (representing the parm or result itself)
1794 we don't do anything here. But those which don't contain the
1795 default def (representing a temporary based on the parm/result)
1796 we need to allocate space just like for normal VAR_DECLs. */
1797 if (!bitmap_bit_p (SA.partition_has_default_def, i))
1799 expand_one_var (var, true, true);
1800 gcc_assert (SA.partition_to_pseudo[i]);
1805 if (flag_stack_protect == SPCT_FLAG_STRONG)
1806 gen_stack_protect_signal
1807 = stack_protect_decl_p () || stack_protect_return_slot_p ();
1809 /* At this point all variables on the local_decls with TREE_USED
1810 set are not associated with any block scope. Lay them out. */
1812 len = vec_safe_length (cfun->local_decls);
1813 FOR_EACH_LOCAL_DECL (cfun, i, var)
1815 bool expand_now = false;
1817 /* Expanded above already. */
1818 if (is_gimple_reg (var))
1820 TREE_USED (var) = 0;
1821 goto next;
1823 /* We didn't set a block for static or extern because it's hard
1824 to tell the difference between a global variable (re)declared
1825 in a local scope, and one that's really declared there to
1826 begin with. And it doesn't really matter much, since we're
1827 not giving them stack space. Expand them now. */
1828 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1829 expand_now = true;
1831 /* Expand variables not associated with any block now. Those created by
1832 the optimizers could be live anywhere in the function. Those that
1833 could possibly have been scoped originally and detached from their
1834 block will have their allocation deferred so we coalesce them with
1835 others when optimization is enabled. */
1836 else if (TREE_USED (var))
1837 expand_now = true;
1839 /* Finally, mark all variables on the list as used. We'll use
1840 this in a moment when we expand those associated with scopes. */
1841 TREE_USED (var) = 1;
1843 if (expand_now)
1844 expand_one_var (var, true, true);
1846 next:
1847 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1849 rtx rtl = DECL_RTL_IF_SET (var);
1851 /* Keep artificial non-ignored vars in cfun->local_decls
1852 chain until instantiate_decls. */
1853 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1854 add_local_decl (cfun, var);
1855 else if (rtl == NULL_RTX)
1856 /* If rtl isn't set yet, which can happen e.g. with
1857 -fstack-protector, retry before returning from this
1858 function. */
1859 maybe_local_decls.safe_push (var);
1863 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
1865 +-----------------+-----------------+
1866 | ...processed... | ...duplicates...|
1867 +-----------------+-----------------+
1869 +-- LEN points here.
1871 We just want the duplicates, as those are the artificial
1872 non-ignored vars that we want to keep until instantiate_decls.
1873 Move them down and truncate the array. */
1874 if (!vec_safe_is_empty (cfun->local_decls))
1875 cfun->local_decls->block_remove (0, len);
1877 /* At this point, all variables within the block tree with TREE_USED
1878 set are actually used by the optimized function. Lay them out. */
1879 expand_used_vars_for_block (outer_block, true);
1881 if (stack_vars_num > 0)
1883 add_scope_conflicts ();
1885 /* If stack protection is enabled, we don't share space between
1886 vulnerable data and non-vulnerable data. */
1887 if (flag_stack_protect != 0
1888 && (flag_stack_protect != SPCT_FLAG_EXPLICIT
1889 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1890 && lookup_attribute ("stack_protect",
1891 DECL_ATTRIBUTES (current_function_decl)))))
1892 add_stack_protection_conflicts ();
1894 /* Now that we have collected all stack variables, and have computed a
1895 minimal interference graph, attempt to save some stack space. */
1896 partition_stack_vars ();
1897 if (dump_file)
1898 dump_stack_var_partition ();
1901 switch (flag_stack_protect)
1903 case SPCT_FLAG_ALL:
1904 create_stack_guard ();
1905 break;
1907 case SPCT_FLAG_STRONG:
1908 if (gen_stack_protect_signal
1909 || cfun->calls_alloca || has_protected_decls
1910 || lookup_attribute ("stack_protect",
1911 DECL_ATTRIBUTES (current_function_decl)))
1912 create_stack_guard ();
1913 break;
1915 case SPCT_FLAG_DEFAULT:
1916 if (cfun->calls_alloca || has_protected_decls
1917 || lookup_attribute ("stack_protect",
1918 DECL_ATTRIBUTES (current_function_decl)))
1919 create_stack_guard ();
1920 break;
1922 case SPCT_FLAG_EXPLICIT:
1923 if (lookup_attribute ("stack_protect",
1924 DECL_ATTRIBUTES (current_function_decl)))
1925 create_stack_guard ();
1926 break;
1927 default:
1931 /* Assign rtl to each variable based on these partitions. */
1932 if (stack_vars_num > 0)
1934 struct stack_vars_data data;
1936 data.asan_vec = vNULL;
1937 data.asan_decl_vec = vNULL;
1938 data.asan_base = NULL_RTX;
1939 data.asan_alignb = 0;
1941 /* Reorder decls to be protected by iterating over the variables
1942 array multiple times, and allocating out of each phase in turn. */
1943 /* ??? We could probably integrate this into the qsort we did
1944 earlier, such that we naturally see these variables first,
1945 and thus naturally allocate things in the right order. */
1946 if (has_protected_decls)
1948 /* Phase 1 contains only character arrays. */
1949 expand_stack_vars (stack_protect_decl_phase_1, &data);
1951 /* Phase 2 contains other kinds of arrays. */
1952 if (flag_stack_protect == SPCT_FLAG_ALL
1953 || flag_stack_protect == SPCT_FLAG_STRONG
1954 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1955 && lookup_attribute ("stack_protect",
1956 DECL_ATTRIBUTES (current_function_decl))))
1957 expand_stack_vars (stack_protect_decl_phase_2, &data);
1960 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK)
1961 /* Phase 3, any partitions that need asan protection
1962 in addition to phase 1 and 2. */
1963 expand_stack_vars (asan_decl_phase_3, &data);
1965 if (!data.asan_vec.is_empty ())
1967 HOST_WIDE_INT prev_offset = frame_offset;
1968 HOST_WIDE_INT offset, sz, redzonesz;
1969 redzonesz = ASAN_RED_ZONE_SIZE;
1970 sz = data.asan_vec[0] - prev_offset;
1971 if (data.asan_alignb > ASAN_RED_ZONE_SIZE
1972 && data.asan_alignb <= 4096
1973 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
1974 redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
1975 & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
1976 offset
1977 = alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE);
1978 data.asan_vec.safe_push (prev_offset);
1979 data.asan_vec.safe_push (offset);
1980 /* Leave space for alignment if STRICT_ALIGNMENT. */
1981 if (STRICT_ALIGNMENT)
1982 alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
1983 << ASAN_SHADOW_SHIFT)
1984 / BITS_PER_UNIT, 1);
1986 var_end_seq
1987 = asan_emit_stack_protection (virtual_stack_vars_rtx,
1988 data.asan_base,
1989 data.asan_alignb,
1990 data.asan_vec.address (),
1991 data.asan_decl_vec.address (),
1992 data.asan_vec.length ());
1995 expand_stack_vars (NULL, &data);
1997 data.asan_vec.release ();
1998 data.asan_decl_vec.release ();
2001 fini_vars_expansion ();
2003 /* If there were any artificial non-ignored vars without rtl
2004 found earlier, see if deferred stack allocation hasn't assigned
2005 rtl to them. */
2006 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
2008 rtx rtl = DECL_RTL_IF_SET (var);
2010 /* Keep artificial non-ignored vars in cfun->local_decls
2011 chain until instantiate_decls. */
2012 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2013 add_local_decl (cfun, var);
2015 maybe_local_decls.release ();
2017 /* If the target requires that FRAME_OFFSET be aligned, do it. */
2018 if (STACK_ALIGNMENT_NEEDED)
2020 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2021 if (!FRAME_GROWS_DOWNWARD)
2022 frame_offset += align - 1;
2023 frame_offset &= -align;
2026 return var_end_seq;
2030 /* If we need to produce a detailed dump, print the tree representation
2031 for STMT to the dump file. SINCE is the last RTX after which the RTL
2032 generated for STMT should have been appended. */
2034 static void
2035 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx_insn *since)
2037 if (dump_file && (dump_flags & TDF_DETAILS))
2039 fprintf (dump_file, "\n;; ");
2040 print_gimple_stmt (dump_file, stmt, 0,
2041 TDF_SLIM | (dump_flags & TDF_LINENO));
2042 fprintf (dump_file, "\n");
2044 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
2048 /* Maps the blocks that do not contain tree labels to rtx labels. */
2050 static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
2052 /* Returns the label_rtx expression for a label starting basic block BB. */
2054 static rtx_code_label *
2055 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
2057 gimple_stmt_iterator gsi;
2058 tree lab;
2060 if (bb->flags & BB_RTL)
2061 return block_label (bb);
2063 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
2064 if (elt)
2065 return *elt;
2067 /* Find the tree label if it is present. */
2069 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2071 glabel *lab_stmt;
2073 lab_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
2074 if (!lab_stmt)
2075 break;
2077 lab = gimple_label_label (lab_stmt);
2078 if (DECL_NONLOCAL (lab))
2079 break;
2081 return jump_target_rtx (lab);
2084 rtx_code_label *l = gen_label_rtx ();
2085 lab_rtx_for_bb->put (bb, l);
2086 return l;
2090 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2091 of a basic block where we just expanded the conditional at the end,
2092 possibly clean up the CFG and instruction sequence. LAST is the
2093 last instruction before the just emitted jump sequence. */
2095 static void
2096 maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2098 /* Special case: when jumpif decides that the condition is
2099 trivial it emits an unconditional jump (and the necessary
2100 barrier). But we still have two edges, the fallthru one is
2101 wrong. purge_dead_edges would clean this up later. Unfortunately
2102 we have to insert insns (and split edges) before
2103 find_many_sub_basic_blocks and hence before purge_dead_edges.
2104 But splitting edges might create new blocks which depend on the
2105 fact that if there are two edges there's no barrier. So the
2106 barrier would get lost and verify_flow_info would ICE. Instead
2107 of auditing all edge splitters to care for the barrier (which
2108 normally isn't there in a cleaned CFG), fix it here. */
2109 if (BARRIER_P (get_last_insn ()))
2111 rtx_insn *insn;
2112 remove_edge (e);
2113 /* Now, we have a single successor block, if we have insns to
2114 insert on the remaining edge we potentially will insert
2115 it at the end of this block (if the dest block isn't feasible)
2116 in order to avoid splitting the edge. This insertion will take
2117 place in front of the last jump. But we might have emitted
2118 multiple jumps (conditional and one unconditional) to the
2119 same destination. Inserting in front of the last one then
2120 is a problem. See PR 40021. We fix this by deleting all
2121 jumps except the last unconditional one. */
2122 insn = PREV_INSN (get_last_insn ());
2123 /* Make sure we have an unconditional jump. Otherwise we're
2124 confused. */
2125 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2126 for (insn = PREV_INSN (insn); insn != last;)
2128 insn = PREV_INSN (insn);
2129 if (JUMP_P (NEXT_INSN (insn)))
2131 if (!any_condjump_p (NEXT_INSN (insn)))
2133 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2134 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2136 delete_insn (NEXT_INSN (insn));
2142 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2143 Returns a new basic block if we've terminated the current basic
2144 block and created a new one. */
2146 static basic_block
2147 expand_gimple_cond (basic_block bb, gcond *stmt)
2149 basic_block new_bb, dest;
2150 edge new_edge;
2151 edge true_edge;
2152 edge false_edge;
2153 rtx_insn *last2, *last;
2154 enum tree_code code;
2155 tree op0, op1;
2157 code = gimple_cond_code (stmt);
2158 op0 = gimple_cond_lhs (stmt);
2159 op1 = gimple_cond_rhs (stmt);
2160 /* We're sometimes presented with such code:
2161 D.123_1 = x < y;
2162 if (D.123_1 != 0)
2164 This would expand to two comparisons which then later might
2165 be cleaned up by combine. But some pattern matchers like if-conversion
2166 work better when there's only one compare, so make up for this
2167 here as special exception if TER would have made the same change. */
2168 if (SA.values
2169 && TREE_CODE (op0) == SSA_NAME
2170 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2171 && TREE_CODE (op1) == INTEGER_CST
2172 && ((gimple_cond_code (stmt) == NE_EXPR
2173 && integer_zerop (op1))
2174 || (gimple_cond_code (stmt) == EQ_EXPR
2175 && integer_onep (op1)))
2176 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2178 gimple second = SSA_NAME_DEF_STMT (op0);
2179 if (gimple_code (second) == GIMPLE_ASSIGN)
2181 enum tree_code code2 = gimple_assign_rhs_code (second);
2182 if (TREE_CODE_CLASS (code2) == tcc_comparison)
2184 code = code2;
2185 op0 = gimple_assign_rhs1 (second);
2186 op1 = gimple_assign_rhs2 (second);
2188 /* If jumps are cheap and the target does not support conditional
2189 compare, turn some more codes into jumpy sequences. */
2190 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4
2191 && targetm.gen_ccmp_first == NULL)
2193 if ((code2 == BIT_AND_EXPR
2194 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2195 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2196 || code2 == TRUTH_AND_EXPR)
2198 code = TRUTH_ANDIF_EXPR;
2199 op0 = gimple_assign_rhs1 (second);
2200 op1 = gimple_assign_rhs2 (second);
2202 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2204 code = TRUTH_ORIF_EXPR;
2205 op0 = gimple_assign_rhs1 (second);
2206 op1 = gimple_assign_rhs2 (second);
2212 last2 = last = get_last_insn ();
2214 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2215 set_curr_insn_location (gimple_location (stmt));
2217 /* These flags have no purpose in RTL land. */
2218 true_edge->flags &= ~EDGE_TRUE_VALUE;
2219 false_edge->flags &= ~EDGE_FALSE_VALUE;
2221 /* We can either have a pure conditional jump with one fallthru edge or
2222 two-way jump that needs to be decomposed into two basic blocks. */
2223 if (false_edge->dest == bb->next_bb)
2225 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2226 true_edge->probability);
2227 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2228 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2229 set_curr_insn_location (true_edge->goto_locus);
2230 false_edge->flags |= EDGE_FALLTHRU;
2231 maybe_cleanup_end_of_block (false_edge, last);
2232 return NULL;
2234 if (true_edge->dest == bb->next_bb)
2236 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2237 false_edge->probability);
2238 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2239 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2240 set_curr_insn_location (false_edge->goto_locus);
2241 true_edge->flags |= EDGE_FALLTHRU;
2242 maybe_cleanup_end_of_block (true_edge, last);
2243 return NULL;
2246 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2247 true_edge->probability);
2248 last = get_last_insn ();
2249 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2250 set_curr_insn_location (false_edge->goto_locus);
2251 emit_jump (label_rtx_for_bb (false_edge->dest));
2253 BB_END (bb) = last;
2254 if (BARRIER_P (BB_END (bb)))
2255 BB_END (bb) = PREV_INSN (BB_END (bb));
2256 update_bb_for_insn (bb);
2258 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2259 dest = false_edge->dest;
2260 redirect_edge_succ (false_edge, new_bb);
2261 false_edge->flags |= EDGE_FALLTHRU;
2262 new_bb->count = false_edge->count;
2263 new_bb->frequency = EDGE_FREQUENCY (false_edge);
2264 add_bb_to_loop (new_bb, bb->loop_father);
2265 new_edge = make_edge (new_bb, dest, 0);
2266 new_edge->probability = REG_BR_PROB_BASE;
2267 new_edge->count = new_bb->count;
2268 if (BARRIER_P (BB_END (new_bb)))
2269 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2270 update_bb_for_insn (new_bb);
2272 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2274 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2276 set_curr_insn_location (true_edge->goto_locus);
2277 true_edge->goto_locus = curr_insn_location ();
2280 return new_bb;
2283 /* Mark all calls that can have a transaction restart. */
2285 static void
2286 mark_transaction_restart_calls (gimple stmt)
2288 struct tm_restart_node dummy;
2289 tm_restart_node **slot;
2291 if (!cfun->gimple_df->tm_restart)
2292 return;
2294 dummy.stmt = stmt;
2295 slot = cfun->gimple_df->tm_restart->find_slot (&dummy, NO_INSERT);
2296 if (slot)
2298 struct tm_restart_node *n = *slot;
2299 tree list = n->label_or_list;
2300 rtx_insn *insn;
2302 for (insn = next_real_insn (get_last_insn ());
2303 !CALL_P (insn);
2304 insn = next_real_insn (insn))
2305 continue;
2307 if (TREE_CODE (list) == LABEL_DECL)
2308 add_reg_note (insn, REG_TM, label_rtx (list));
2309 else
2310 for (; list ; list = TREE_CHAIN (list))
2311 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2315 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2316 statement STMT. */
2318 static void
2319 expand_call_stmt (gcall *stmt)
2321 tree exp, decl, lhs;
2322 bool builtin_p;
2323 size_t i;
2325 if (gimple_call_internal_p (stmt))
2327 expand_internal_call (stmt);
2328 return;
2331 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2333 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2334 decl = gimple_call_fndecl (stmt);
2335 builtin_p = decl && DECL_BUILT_IN (decl);
2337 /* If this is not a builtin function, the function type through which the
2338 call is made may be different from the type of the function. */
2339 if (!builtin_p)
2340 CALL_EXPR_FN (exp)
2341 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2342 CALL_EXPR_FN (exp));
2344 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2345 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2347 for (i = 0; i < gimple_call_num_args (stmt); i++)
2349 tree arg = gimple_call_arg (stmt, i);
2350 gimple def;
2351 /* TER addresses into arguments of builtin functions so we have a
2352 chance to infer more correct alignment information. See PR39954. */
2353 if (builtin_p
2354 && TREE_CODE (arg) == SSA_NAME
2355 && (def = get_gimple_for_ssa_name (arg))
2356 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2357 arg = gimple_assign_rhs1 (def);
2358 CALL_EXPR_ARG (exp, i) = arg;
2361 if (gimple_has_side_effects (stmt))
2362 TREE_SIDE_EFFECTS (exp) = 1;
2364 if (gimple_call_nothrow_p (stmt))
2365 TREE_NOTHROW (exp) = 1;
2367 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2368 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2369 if (decl
2370 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2371 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2372 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2373 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2374 else
2375 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2376 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2377 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2378 CALL_WITH_BOUNDS_P (exp) = gimple_call_with_bounds_p (stmt);
2380 /* Ensure RTL is created for debug args. */
2381 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2383 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2384 unsigned int ix;
2385 tree dtemp;
2387 if (debug_args)
2388 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2390 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2391 expand_debug_expr (dtemp);
2395 lhs = gimple_call_lhs (stmt);
2396 if (lhs)
2397 expand_assignment (lhs, exp, false);
2398 else
2399 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2401 mark_transaction_restart_calls (stmt);
2405 /* Generate RTL for an asm statement (explicit assembler code).
2406 STRING is a STRING_CST node containing the assembler code text,
2407 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
2408 insn is volatile; don't optimize it. */
2410 static void
2411 expand_asm_loc (tree string, int vol, location_t locus)
2413 rtx body;
2415 if (TREE_CODE (string) == ADDR_EXPR)
2416 string = TREE_OPERAND (string, 0);
2418 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2419 ggc_strdup (TREE_STRING_POINTER (string)),
2420 locus);
2422 MEM_VOLATILE_P (body) = vol;
2424 emit_insn (body);
2427 /* Return the number of times character C occurs in string S. */
2428 static int
2429 n_occurrences (int c, const char *s)
2431 int n = 0;
2432 while (*s)
2433 n += (*s++ == c);
2434 return n;
2437 /* A subroutine of expand_asm_operands. Check that all operands have
2438 the same number of alternatives. Return true if so. */
2440 static bool
2441 check_operand_nalternatives (const vec<const char *> &constraints)
2443 unsigned len = constraints.length();
2444 if (len > 0)
2446 int nalternatives = n_occurrences (',', constraints[0]);
2448 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2450 error ("too many alternatives in %<asm%>");
2451 return false;
2454 for (unsigned i = 1; i < len; ++i)
2455 if (n_occurrences (',', constraints[i]) != nalternatives)
2457 error ("operand constraints for %<asm%> differ "
2458 "in number of alternatives");
2459 return false;
2462 return true;
2465 /* Check for overlap between registers marked in CLOBBERED_REGS and
2466 anything inappropriate in T. Emit error and return the register
2467 variable definition for error, NULL_TREE for ok. */
2469 static bool
2470 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
2472 /* Conflicts between asm-declared register variables and the clobber
2473 list are not allowed. */
2474 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2476 if (overlap)
2478 error ("asm-specifier for variable %qE conflicts with asm clobber list",
2479 DECL_NAME (overlap));
2481 /* Reset registerness to stop multiple errors emitted for a single
2482 variable. */
2483 DECL_REGISTER (overlap) = 0;
2484 return true;
2487 return false;
2490 /* Generate RTL for an asm statement with arguments.
2491 STRING is the instruction template.
2492 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
2493 Each output or input has an expression in the TREE_VALUE and
2494 a tree list in TREE_PURPOSE which in turn contains a constraint
2495 name in TREE_VALUE (or NULL_TREE) and a constraint string
2496 in TREE_PURPOSE.
2497 CLOBBERS is a list of STRING_CST nodes each naming a hard register
2498 that is clobbered by this insn.
2500 LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
2501 should be the fallthru basic block of the asm goto.
2503 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
2504 Some elements of OUTPUTS may be replaced with trees representing temporary
2505 values. The caller should copy those temporary values to the originally
2506 specified lvalues.
2508 VOL nonzero means the insn is volatile; don't optimize it. */
2510 static void
2511 expand_asm_stmt (gasm *stmt)
2513 class save_input_location
2515 location_t old;
2517 public:
2518 explicit save_input_location(location_t where)
2520 old = input_location;
2521 input_location = where;
2524 ~save_input_location()
2526 input_location = old;
2530 location_t locus = gimple_location (stmt);
2532 if (gimple_asm_input_p (stmt))
2534 const char *s = gimple_asm_string (stmt);
2535 tree string = build_string (strlen (s), s);
2536 expand_asm_loc (string, gimple_asm_volatile_p (stmt), locus);
2537 return;
2540 /* There are some legacy diagnostics in here, and also avoids a
2541 sixth parameger to targetm.md_asm_adjust. */
2542 save_input_location s_i_l(locus);
2544 unsigned noutputs = gimple_asm_noutputs (stmt);
2545 unsigned ninputs = gimple_asm_ninputs (stmt);
2546 unsigned nlabels = gimple_asm_nlabels (stmt);
2547 unsigned i;
2549 /* ??? Diagnose during gimplification? */
2550 if (ninputs + noutputs + nlabels > MAX_RECOG_OPERANDS)
2552 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
2553 return;
2556 auto_vec<tree, MAX_RECOG_OPERANDS> output_tvec;
2557 auto_vec<tree, MAX_RECOG_OPERANDS> input_tvec;
2558 auto_vec<const char *, MAX_RECOG_OPERANDS> constraints;
2560 /* Copy the gimple vectors into new vectors that we can manipulate. */
2562 output_tvec.safe_grow (noutputs);
2563 input_tvec.safe_grow (ninputs);
2564 constraints.safe_grow (noutputs + ninputs);
2566 for (i = 0; i < noutputs; ++i)
2568 tree t = gimple_asm_output_op (stmt, i);
2569 output_tvec[i] = TREE_VALUE (t);
2570 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2572 for (i = 0; i < ninputs; i++)
2574 tree t = gimple_asm_input_op (stmt, i);
2575 input_tvec[i] = TREE_VALUE (t);
2576 constraints[i + noutputs]
2577 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2580 /* ??? Diagnose during gimplification? */
2581 if (! check_operand_nalternatives (constraints))
2582 return;
2584 /* Count the number of meaningful clobbered registers, ignoring what
2585 we would ignore later. */
2586 auto_vec<rtx> clobber_rvec;
2587 HARD_REG_SET clobbered_regs;
2588 CLEAR_HARD_REG_SET (clobbered_regs);
2590 if (unsigned n = gimple_asm_nclobbers (stmt))
2592 clobber_rvec.reserve (n);
2593 for (i = 0; i < n; i++)
2595 tree t = gimple_asm_clobber_op (stmt, i);
2596 const char *regname = TREE_STRING_POINTER (TREE_VALUE (t));
2597 int nregs, j;
2599 j = decode_reg_name_and_count (regname, &nregs);
2600 if (j < 0)
2602 if (j == -2)
2604 /* ??? Diagnose during gimplification? */
2605 error ("unknown register name %qs in %<asm%>", regname);
2607 else if (j == -4)
2609 rtx x = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2610 clobber_rvec.safe_push (x);
2612 else
2614 /* Otherwise we should have -1 == empty string
2615 or -3 == cc, which is not a register. */
2616 gcc_assert (j == -1 || j == -3);
2619 else
2620 for (int reg = j; reg < j + nregs; reg++)
2622 /* Clobbering the PIC register is an error. */
2623 if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
2625 /* ??? Diagnose during gimplification? */
2626 error ("PIC register clobbered by %qs in %<asm%>",
2627 regname);
2628 return;
2631 SET_HARD_REG_BIT (clobbered_regs, reg);
2632 rtx x = gen_rtx_REG (reg_raw_mode[reg], reg);
2633 clobber_rvec.safe_push (x);
2637 unsigned nclobbers = clobber_rvec.length();
2639 /* First pass over inputs and outputs checks validity and sets
2640 mark_addressable if needed. */
2641 /* ??? Diagnose during gimplification? */
2643 for (i = 0; i < noutputs; ++i)
2645 tree val = output_tvec[i];
2646 tree type = TREE_TYPE (val);
2647 const char *constraint;
2648 bool is_inout;
2649 bool allows_reg;
2650 bool allows_mem;
2652 /* Try to parse the output constraint. If that fails, there's
2653 no point in going further. */
2654 constraint = constraints[i];
2655 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
2656 &allows_mem, &allows_reg, &is_inout))
2657 return;
2659 if (! allows_reg
2660 && (allows_mem
2661 || is_inout
2662 || (DECL_P (val)
2663 && REG_P (DECL_RTL (val))
2664 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
2665 mark_addressable (val);
2668 for (i = 0; i < ninputs; ++i)
2670 bool allows_reg, allows_mem;
2671 const char *constraint;
2673 constraint = constraints[i + noutputs];
2674 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2675 constraints.address (),
2676 &allows_mem, &allows_reg))
2677 return;
2679 if (! allows_reg && allows_mem)
2680 mark_addressable (input_tvec[i]);
2683 /* Second pass evaluates arguments. */
2685 /* Make sure stack is consistent for asm goto. */
2686 if (nlabels > 0)
2687 do_pending_stack_adjust ();
2688 int old_generating_concat_p = generating_concat_p;
2690 /* Vector of RTX's of evaluated output operands. */
2691 auto_vec<rtx, MAX_RECOG_OPERANDS> output_rvec;
2692 auto_vec<int, MAX_RECOG_OPERANDS> inout_opnum;
2693 rtx_insn *after_rtl_seq = NULL, *after_rtl_end = NULL;
2695 output_rvec.safe_grow (noutputs);
2697 for (i = 0; i < noutputs; ++i)
2699 tree val = output_tvec[i];
2700 tree type = TREE_TYPE (val);
2701 bool is_inout, allows_reg, allows_mem, ok;
2702 rtx op;
2704 ok = parse_output_constraint (&constraints[i], i, ninputs,
2705 noutputs, &allows_mem, &allows_reg,
2706 &is_inout);
2707 gcc_assert (ok);
2709 /* If an output operand is not a decl or indirect ref and our constraint
2710 allows a register, make a temporary to act as an intermediate.
2711 Make the asm insn write into that, then we will copy it to
2712 the real output operand. Likewise for promoted variables. */
2714 generating_concat_p = 0;
2716 if ((TREE_CODE (val) == INDIRECT_REF
2717 && allows_mem)
2718 || (DECL_P (val)
2719 && (allows_mem || REG_P (DECL_RTL (val)))
2720 && ! (REG_P (DECL_RTL (val))
2721 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
2722 || ! allows_reg
2723 || is_inout)
2725 op = expand_expr (val, NULL_RTX, VOIDmode,
2726 !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
2727 if (MEM_P (op))
2728 op = validize_mem (op);
2730 if (! allows_reg && !MEM_P (op))
2731 error ("output number %d not directly addressable", i);
2732 if ((! allows_mem && MEM_P (op))
2733 || GET_CODE (op) == CONCAT)
2735 rtx old_op = op;
2736 op = gen_reg_rtx (GET_MODE (op));
2738 generating_concat_p = old_generating_concat_p;
2740 if (is_inout)
2741 emit_move_insn (op, old_op);
2743 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2744 emit_move_insn (old_op, op);
2745 after_rtl_seq = get_insns ();
2746 after_rtl_end = get_last_insn ();
2747 end_sequence ();
2750 else
2752 op = assign_temp (type, 0, 1);
2753 op = validize_mem (op);
2754 if (!MEM_P (op) && TREE_CODE (val) == SSA_NAME)
2755 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (val), op);
2757 generating_concat_p = old_generating_concat_p;
2759 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2760 expand_assignment (val, make_tree (type, op), false);
2761 after_rtl_seq = get_insns ();
2762 after_rtl_end = get_last_insn ();
2763 end_sequence ();
2765 output_rvec[i] = op;
2767 if (is_inout)
2768 inout_opnum.safe_push (i);
2771 auto_vec<rtx, MAX_RECOG_OPERANDS> input_rvec;
2772 auto_vec<machine_mode, MAX_RECOG_OPERANDS> input_mode;
2774 input_rvec.safe_grow (ninputs);
2775 input_mode.safe_grow (ninputs);
2777 generating_concat_p = 0;
2779 for (i = 0; i < ninputs; ++i)
2781 tree val = input_tvec[i];
2782 tree type = TREE_TYPE (val);
2783 bool allows_reg, allows_mem, ok;
2784 const char *constraint;
2785 rtx op;
2787 constraint = constraints[i + noutputs];
2788 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2789 constraints.address (),
2790 &allows_mem, &allows_reg);
2791 gcc_assert (ok);
2793 /* EXPAND_INITIALIZER will not generate code for valid initializer
2794 constants, but will still generate code for other types of operand.
2795 This is the behavior we want for constant constraints. */
2796 op = expand_expr (val, NULL_RTX, VOIDmode,
2797 allows_reg ? EXPAND_NORMAL
2798 : allows_mem ? EXPAND_MEMORY
2799 : EXPAND_INITIALIZER);
2801 /* Never pass a CONCAT to an ASM. */
2802 if (GET_CODE (op) == CONCAT)
2803 op = force_reg (GET_MODE (op), op);
2804 else if (MEM_P (op))
2805 op = validize_mem (op);
2807 if (asm_operand_ok (op, constraint, NULL) <= 0)
2809 if (allows_reg && TYPE_MODE (type) != BLKmode)
2810 op = force_reg (TYPE_MODE (type), op);
2811 else if (!allows_mem)
2812 warning (0, "asm operand %d probably doesn%'t match constraints",
2813 i + noutputs);
2814 else if (MEM_P (op))
2816 /* We won't recognize either volatile memory or memory
2817 with a queued address as available a memory_operand
2818 at this point. Ignore it: clearly this *is* a memory. */
2820 else
2821 gcc_unreachable ();
2823 input_rvec[i] = op;
2824 input_mode[i] = TYPE_MODE (type);
2827 /* For in-out operands, copy output rtx to input rtx. */
2828 unsigned ninout = inout_opnum.length();
2829 for (i = 0; i < ninout; i++)
2831 int j = inout_opnum[i];
2832 rtx o = output_rvec[j];
2834 input_rvec.safe_push (o);
2835 input_mode.safe_push (GET_MODE (o));
2837 char buffer[16];
2838 sprintf (buffer, "%d", j);
2839 constraints.safe_push (ggc_strdup (buffer));
2841 ninputs += ninout;
2843 /* Sometimes we wish to automatically clobber registers across an asm.
2844 Case in point is when the i386 backend moved from cc0 to a hard reg --
2845 maintaining source-level compatibility means automatically clobbering
2846 the flags register. */
2847 rtx_insn *after_md_seq = NULL;
2848 if (targetm.md_asm_adjust)
2849 after_md_seq = targetm.md_asm_adjust (output_rvec, input_rvec,
2850 constraints, clobber_rvec,
2851 clobbered_regs);
2853 /* Do not allow the hook to change the output and input count,
2854 lest it mess up the operand numbering. */
2855 gcc_assert (output_rvec.length() == noutputs);
2856 gcc_assert (input_rvec.length() == ninputs);
2857 gcc_assert (constraints.length() == noutputs + ninputs);
2859 /* But it certainly can adjust the clobbers. */
2860 nclobbers = clobber_rvec.length();
2862 /* Third pass checks for easy conflicts. */
2863 /* ??? Why are we doing this on trees instead of rtx. */
2865 bool clobber_conflict_found = 0;
2866 for (i = 0; i < noutputs; ++i)
2867 if (tree_conflicts_with_clobbers_p (output_tvec[i], &clobbered_regs))
2868 clobber_conflict_found = 1;
2869 for (i = 0; i < ninputs - ninout; ++i)
2870 if (tree_conflicts_with_clobbers_p (input_tvec[i], &clobbered_regs))
2871 clobber_conflict_found = 1;
2873 /* Make vectors for the expression-rtx, constraint strings,
2874 and named operands. */
2876 rtvec argvec = rtvec_alloc (ninputs);
2877 rtvec constraintvec = rtvec_alloc (ninputs);
2878 rtvec labelvec = rtvec_alloc (nlabels);
2880 rtx body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
2881 : GET_MODE (output_rvec[0])),
2882 ggc_strdup (gimple_asm_string (stmt)),
2883 empty_string, 0, argvec, constraintvec,
2884 labelvec, locus);
2885 MEM_VOLATILE_P (body) = gimple_asm_volatile_p (stmt);
2887 for (i = 0; i < ninputs; ++i)
2889 ASM_OPERANDS_INPUT (body, i) = input_rvec[i];
2890 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
2891 = gen_rtx_ASM_INPUT_loc (input_mode[i],
2892 constraints[i + noutputs],
2893 locus);
2896 /* Copy labels to the vector. */
2897 rtx_code_label *fallthru_label = NULL;
2898 if (nlabels > 0)
2900 basic_block fallthru_bb = NULL;
2901 edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
2902 if (fallthru)
2903 fallthru_bb = fallthru->dest;
2905 for (i = 0; i < nlabels; ++i)
2907 tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
2908 rtx r;
2909 /* If asm goto has any labels in the fallthru basic block, use
2910 a label that we emit immediately after the asm goto. Expansion
2911 may insert further instructions into the same basic block after
2912 asm goto and if we don't do this, insertion of instructions on
2913 the fallthru edge might misbehave. See PR58670. */
2914 if (fallthru_bb && label_to_block_fn (cfun, label) == fallthru_bb)
2916 if (fallthru_label == NULL_RTX)
2917 fallthru_label = gen_label_rtx ();
2918 r = fallthru_label;
2920 else
2921 r = label_rtx (label);
2922 ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
2926 /* Now, for each output, construct an rtx
2927 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
2928 ARGVEC CONSTRAINTS OPNAMES))
2929 If there is more than one, put them inside a PARALLEL. */
2931 if (nlabels > 0 && nclobbers == 0)
2933 gcc_assert (noutputs == 0);
2934 emit_jump_insn (body);
2936 else if (noutputs == 0 && nclobbers == 0)
2938 /* No output operands: put in a raw ASM_OPERANDS rtx. */
2939 emit_insn (body);
2941 else if (noutputs == 1 && nclobbers == 0)
2943 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
2944 emit_insn (gen_rtx_SET (output_rvec[0], body));
2946 else
2948 rtx obody = body;
2949 int num = noutputs;
2951 if (num == 0)
2952 num = 1;
2954 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
2956 /* For each output operand, store a SET. */
2957 for (i = 0; i < noutputs; ++i)
2959 rtx src, o = output_rvec[i];
2960 if (i == 0)
2962 ASM_OPERANDS_OUTPUT_CONSTRAINT (obody) = constraints[0];
2963 src = obody;
2965 else
2967 src = gen_rtx_ASM_OPERANDS (GET_MODE (o),
2968 ASM_OPERANDS_TEMPLATE (obody),
2969 constraints[i], i, argvec,
2970 constraintvec, labelvec, locus);
2971 MEM_VOLATILE_P (src) = gimple_asm_volatile_p (stmt);
2973 XVECEXP (body, 0, i) = gen_rtx_SET (o, src);
2976 /* If there are no outputs (but there are some clobbers)
2977 store the bare ASM_OPERANDS into the PARALLEL. */
2978 if (i == 0)
2979 XVECEXP (body, 0, i++) = obody;
2981 /* Store (clobber REG) for each clobbered register specified. */
2982 for (unsigned j = 0; j < nclobbers; ++j)
2984 rtx clobbered_reg = clobber_rvec[j];
2986 /* Do sanity check for overlap between clobbers and respectively
2987 input and outputs that hasn't been handled. Such overlap
2988 should have been detected and reported above. */
2989 if (!clobber_conflict_found && REG_P (clobbered_reg))
2991 /* We test the old body (obody) contents to avoid
2992 tripping over the under-construction body. */
2993 for (unsigned k = 0; k < noutputs; ++k)
2994 if (reg_overlap_mentioned_p (clobbered_reg, output_rvec[k]))
2995 internal_error ("asm clobber conflict with output operand");
2997 for (unsigned k = 0; k < ninputs - ninout; ++k)
2998 if (reg_overlap_mentioned_p (clobbered_reg, input_rvec[k]))
2999 internal_error ("asm clobber conflict with input operand");
3002 XVECEXP (body, 0, i++) = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
3005 if (nlabels > 0)
3006 emit_jump_insn (body);
3007 else
3008 emit_insn (body);
3011 generating_concat_p = old_generating_concat_p;
3013 if (fallthru_label)
3014 emit_label (fallthru_label);
3016 if (after_md_seq)
3017 emit_insn (after_md_seq);
3018 if (after_rtl_seq)
3019 emit_insn (after_rtl_seq);
3021 free_temp_slots ();
3022 crtl->has_asm_statement = 1;
3025 /* Emit code to jump to the address
3026 specified by the pointer expression EXP. */
3028 static void
3029 expand_computed_goto (tree exp)
3031 rtx x = expand_normal (exp);
3033 do_pending_stack_adjust ();
3034 emit_indirect_jump (x);
3037 /* Generate RTL code for a `goto' statement with target label LABEL.
3038 LABEL should be a LABEL_DECL tree node that was or will later be
3039 defined with `expand_label'. */
3041 static void
3042 expand_goto (tree label)
3044 #ifdef ENABLE_CHECKING
3045 /* Check for a nonlocal goto to a containing function. Should have
3046 gotten translated to __builtin_nonlocal_goto. */
3047 tree context = decl_function_context (label);
3048 gcc_assert (!context || context == current_function_decl);
3049 #endif
3051 emit_jump (jump_target_rtx (label));
3054 /* Output a return with no value. */
3056 static void
3057 expand_null_return_1 (void)
3059 clear_pending_stack_adjust ();
3060 do_pending_stack_adjust ();
3061 emit_jump (return_label);
3064 /* Generate RTL to return from the current function, with no value.
3065 (That is, we do not do anything about returning any value.) */
3067 void
3068 expand_null_return (void)
3070 /* If this function was declared to return a value, but we
3071 didn't, clobber the return registers so that they are not
3072 propagated live to the rest of the function. */
3073 clobber_return_register ();
3075 expand_null_return_1 ();
3078 /* Generate RTL to return from the current function, with value VAL. */
3080 static void
3081 expand_value_return (rtx val)
3083 /* Copy the value to the return location unless it's already there. */
3085 tree decl = DECL_RESULT (current_function_decl);
3086 rtx return_reg = DECL_RTL (decl);
3087 if (return_reg != val)
3089 tree funtype = TREE_TYPE (current_function_decl);
3090 tree type = TREE_TYPE (decl);
3091 int unsignedp = TYPE_UNSIGNED (type);
3092 machine_mode old_mode = DECL_MODE (decl);
3093 machine_mode mode;
3094 if (DECL_BY_REFERENCE (decl))
3095 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3096 else
3097 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3099 if (mode != old_mode)
3100 val = convert_modes (mode, old_mode, val, unsignedp);
3102 if (GET_CODE (return_reg) == PARALLEL)
3103 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3104 else
3105 emit_move_insn (return_reg, val);
3108 expand_null_return_1 ();
3111 /* Generate RTL to evaluate the expression RETVAL and return it
3112 from the current function. */
3114 static void
3115 expand_return (tree retval, tree bounds)
3117 rtx result_rtl;
3118 rtx val = 0;
3119 tree retval_rhs;
3120 rtx bounds_rtl;
3122 /* If function wants no value, give it none. */
3123 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3125 expand_normal (retval);
3126 expand_null_return ();
3127 return;
3130 if (retval == error_mark_node)
3132 /* Treat this like a return of no value from a function that
3133 returns a value. */
3134 expand_null_return ();
3135 return;
3137 else if ((TREE_CODE (retval) == MODIFY_EXPR
3138 || TREE_CODE (retval) == INIT_EXPR)
3139 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3140 retval_rhs = TREE_OPERAND (retval, 1);
3141 else
3142 retval_rhs = retval;
3144 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3146 /* Put returned bounds to the right place. */
3147 bounds_rtl = DECL_BOUNDS_RTL (DECL_RESULT (current_function_decl));
3148 if (bounds_rtl)
3150 rtx addr, bnd;
3152 if (bounds)
3154 bnd = expand_normal (bounds);
3155 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3157 else if (REG_P (bounds_rtl))
3159 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3160 addr = gen_rtx_MEM (Pmode, addr);
3161 bnd = targetm.calls.load_bounds_for_arg (addr, NULL, NULL);
3162 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3164 else
3166 int n;
3168 gcc_assert (GET_CODE (bounds_rtl) == PARALLEL);
3170 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3171 addr = gen_rtx_MEM (Pmode, addr);
3173 for (n = 0; n < XVECLEN (bounds_rtl, 0); n++)
3175 rtx offs = XEXP (XVECEXP (bounds_rtl, 0, n), 1);
3176 rtx slot = XEXP (XVECEXP (bounds_rtl, 0, n), 0);
3177 rtx from = adjust_address (addr, Pmode, INTVAL (offs));
3178 rtx bnd = targetm.calls.load_bounds_for_arg (from, NULL, NULL);
3179 targetm.calls.store_returned_bounds (slot, bnd);
3183 else if (chkp_function_instrumented_p (current_function_decl)
3184 && !BOUNDED_P (retval_rhs)
3185 && chkp_type_has_pointer (TREE_TYPE (retval_rhs))
3186 && TREE_CODE (retval_rhs) != RESULT_DECL)
3188 rtx addr = expand_normal (build_fold_addr_expr (retval_rhs));
3189 addr = gen_rtx_MEM (Pmode, addr);
3191 gcc_assert (MEM_P (result_rtl));
3193 chkp_copy_bounds_for_stack_parm (result_rtl, addr, TREE_TYPE (retval_rhs));
3196 /* If we are returning the RESULT_DECL, then the value has already
3197 been stored into it, so we don't have to do anything special. */
3198 if (TREE_CODE (retval_rhs) == RESULT_DECL)
3199 expand_value_return (result_rtl);
3201 /* If the result is an aggregate that is being returned in one (or more)
3202 registers, load the registers here. */
3204 else if (retval_rhs != 0
3205 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3206 && REG_P (result_rtl))
3208 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3209 if (val)
3211 /* Use the mode of the result value on the return register. */
3212 PUT_MODE (result_rtl, GET_MODE (val));
3213 expand_value_return (val);
3215 else
3216 expand_null_return ();
3218 else if (retval_rhs != 0
3219 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3220 && (REG_P (result_rtl)
3221 || (GET_CODE (result_rtl) == PARALLEL)))
3223 /* Compute the return value into a temporary (usually a pseudo reg). */
3225 = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3226 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3227 val = force_not_mem (val);
3228 expand_value_return (val);
3230 else
3232 /* No hard reg used; calculate value into hard return reg. */
3233 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3234 expand_value_return (result_rtl);
3238 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3239 STMT that doesn't require special handling for outgoing edges. That
3240 is no tailcalls and no GIMPLE_COND. */
3242 static void
3243 expand_gimple_stmt_1 (gimple stmt)
3245 tree op0;
3247 set_curr_insn_location (gimple_location (stmt));
3249 switch (gimple_code (stmt))
3251 case GIMPLE_GOTO:
3252 op0 = gimple_goto_dest (stmt);
3253 if (TREE_CODE (op0) == LABEL_DECL)
3254 expand_goto (op0);
3255 else
3256 expand_computed_goto (op0);
3257 break;
3258 case GIMPLE_LABEL:
3259 expand_label (gimple_label_label (as_a <glabel *> (stmt)));
3260 break;
3261 case GIMPLE_NOP:
3262 case GIMPLE_PREDICT:
3263 break;
3264 case GIMPLE_SWITCH:
3265 expand_case (as_a <gswitch *> (stmt));
3266 break;
3267 case GIMPLE_ASM:
3268 expand_asm_stmt (as_a <gasm *> (stmt));
3269 break;
3270 case GIMPLE_CALL:
3271 expand_call_stmt (as_a <gcall *> (stmt));
3272 break;
3274 case GIMPLE_RETURN:
3275 op0 = gimple_return_retval (as_a <greturn *> (stmt));
3277 if (op0 && op0 != error_mark_node)
3279 tree result = DECL_RESULT (current_function_decl);
3281 /* If we are not returning the current function's RESULT_DECL,
3282 build an assignment to it. */
3283 if (op0 != result)
3285 /* I believe that a function's RESULT_DECL is unique. */
3286 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3288 /* ??? We'd like to use simply expand_assignment here,
3289 but this fails if the value is of BLKmode but the return
3290 decl is a register. expand_return has special handling
3291 for this combination, which eventually should move
3292 to common code. See comments there. Until then, let's
3293 build a modify expression :-/ */
3294 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3295 result, op0);
3298 if (!op0)
3299 expand_null_return ();
3300 else
3301 expand_return (op0, gimple_return_retbnd (stmt));
3302 break;
3304 case GIMPLE_ASSIGN:
3306 gassign *assign_stmt = as_a <gassign *> (stmt);
3307 tree lhs = gimple_assign_lhs (assign_stmt);
3309 /* Tree expand used to fiddle with |= and &= of two bitfield
3310 COMPONENT_REFs here. This can't happen with gimple, the LHS
3311 of binary assigns must be a gimple reg. */
3313 if (TREE_CODE (lhs) != SSA_NAME
3314 || get_gimple_rhs_class (gimple_expr_code (stmt))
3315 == GIMPLE_SINGLE_RHS)
3317 tree rhs = gimple_assign_rhs1 (assign_stmt);
3318 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
3319 == GIMPLE_SINGLE_RHS);
3320 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
3321 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3322 if (TREE_CLOBBER_P (rhs))
3323 /* This is a clobber to mark the going out of scope for
3324 this LHS. */
3326 else
3327 expand_assignment (lhs, rhs,
3328 gimple_assign_nontemporal_move_p (
3329 assign_stmt));
3331 else
3333 rtx target, temp;
3334 bool nontemporal = gimple_assign_nontemporal_move_p (assign_stmt);
3335 struct separate_ops ops;
3336 bool promoted = false;
3338 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3339 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
3340 promoted = true;
3342 ops.code = gimple_assign_rhs_code (assign_stmt);
3343 ops.type = TREE_TYPE (lhs);
3344 switch (get_gimple_rhs_class (ops.code))
3346 case GIMPLE_TERNARY_RHS:
3347 ops.op2 = gimple_assign_rhs3 (assign_stmt);
3348 /* Fallthru */
3349 case GIMPLE_BINARY_RHS:
3350 ops.op1 = gimple_assign_rhs2 (assign_stmt);
3351 /* Fallthru */
3352 case GIMPLE_UNARY_RHS:
3353 ops.op0 = gimple_assign_rhs1 (assign_stmt);
3354 break;
3355 default:
3356 gcc_unreachable ();
3358 ops.location = gimple_location (stmt);
3360 /* If we want to use a nontemporal store, force the value to
3361 register first. If we store into a promoted register,
3362 don't directly expand to target. */
3363 temp = nontemporal || promoted ? NULL_RTX : target;
3364 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
3365 EXPAND_NORMAL);
3367 if (temp == target)
3369 else if (promoted)
3371 int unsignedp = SUBREG_PROMOTED_SIGN (target);
3372 /* If TEMP is a VOIDmode constant, use convert_modes to make
3373 sure that we properly convert it. */
3374 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
3376 temp = convert_modes (GET_MODE (target),
3377 TYPE_MODE (ops.type),
3378 temp, unsignedp);
3379 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
3380 GET_MODE (target), temp, unsignedp);
3383 convert_move (SUBREG_REG (target), temp, unsignedp);
3385 else if (nontemporal && emit_storent_insn (target, temp))
3387 else
3389 temp = force_operand (temp, target);
3390 if (temp != target)
3391 emit_move_insn (target, temp);
3395 break;
3397 default:
3398 gcc_unreachable ();
3402 /* Expand one gimple statement STMT and return the last RTL instruction
3403 before any of the newly generated ones.
3405 In addition to generating the necessary RTL instructions this also
3406 sets REG_EH_REGION notes if necessary and sets the current source
3407 location for diagnostics. */
3409 static rtx_insn *
3410 expand_gimple_stmt (gimple stmt)
3412 location_t saved_location = input_location;
3413 rtx_insn *last = get_last_insn ();
3414 int lp_nr;
3416 gcc_assert (cfun);
3418 /* We need to save and restore the current source location so that errors
3419 discovered during expansion are emitted with the right location. But
3420 it would be better if the diagnostic routines used the source location
3421 embedded in the tree nodes rather than globals. */
3422 if (gimple_has_location (stmt))
3423 input_location = gimple_location (stmt);
3425 expand_gimple_stmt_1 (stmt);
3427 /* Free any temporaries used to evaluate this statement. */
3428 free_temp_slots ();
3430 input_location = saved_location;
3432 /* Mark all insns that may trap. */
3433 lp_nr = lookup_stmt_eh_lp (stmt);
3434 if (lp_nr)
3436 rtx_insn *insn;
3437 for (insn = next_real_insn (last); insn;
3438 insn = next_real_insn (insn))
3440 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
3441 /* If we want exceptions for non-call insns, any
3442 may_trap_p instruction may throw. */
3443 && GET_CODE (PATTERN (insn)) != CLOBBER
3444 && GET_CODE (PATTERN (insn)) != USE
3445 && insn_could_throw_p (insn))
3446 make_reg_eh_region_note (insn, 0, lp_nr);
3450 return last;
3453 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
3454 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
3455 generated a tail call (something that might be denied by the ABI
3456 rules governing the call; see calls.c).
3458 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
3459 can still reach the rest of BB. The case here is __builtin_sqrt,
3460 where the NaN result goes through the external function (with a
3461 tailcall) and the normal result happens via a sqrt instruction. */
3463 static basic_block
3464 expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
3466 rtx_insn *last2, *last;
3467 edge e;
3468 edge_iterator ei;
3469 int probability;
3470 gcov_type count;
3472 last2 = last = expand_gimple_stmt (stmt);
3474 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
3475 if (CALL_P (last) && SIBLING_CALL_P (last))
3476 goto found;
3478 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3480 *can_fallthru = true;
3481 return NULL;
3483 found:
3484 /* ??? Wouldn't it be better to just reset any pending stack adjust?
3485 Any instructions emitted here are about to be deleted. */
3486 do_pending_stack_adjust ();
3488 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
3489 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
3490 EH or abnormal edges, we shouldn't have created a tail call in
3491 the first place. So it seems to me we should just be removing
3492 all edges here, or redirecting the existing fallthru edge to
3493 the exit block. */
3495 probability = 0;
3496 count = 0;
3498 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3500 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
3502 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3504 e->dest->count -= e->count;
3505 e->dest->frequency -= EDGE_FREQUENCY (e);
3506 if (e->dest->count < 0)
3507 e->dest->count = 0;
3508 if (e->dest->frequency < 0)
3509 e->dest->frequency = 0;
3511 count += e->count;
3512 probability += e->probability;
3513 remove_edge (e);
3515 else
3516 ei_next (&ei);
3519 /* This is somewhat ugly: the call_expr expander often emits instructions
3520 after the sibcall (to perform the function return). These confuse the
3521 find_many_sub_basic_blocks code, so we need to get rid of these. */
3522 last = NEXT_INSN (last);
3523 gcc_assert (BARRIER_P (last));
3525 *can_fallthru = false;
3526 while (NEXT_INSN (last))
3528 /* For instance an sqrt builtin expander expands if with
3529 sibcall in the then and label for `else`. */
3530 if (LABEL_P (NEXT_INSN (last)))
3532 *can_fallthru = true;
3533 break;
3535 delete_insn (NEXT_INSN (last));
3538 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
3539 | EDGE_SIBCALL);
3540 e->probability += probability;
3541 e->count += count;
3542 BB_END (bb) = last;
3543 update_bb_for_insn (bb);
3545 if (NEXT_INSN (last))
3547 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
3549 last = BB_END (bb);
3550 if (BARRIER_P (last))
3551 BB_END (bb) = PREV_INSN (last);
3554 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3556 return bb;
3559 /* Return the difference between the floor and the truncated result of
3560 a signed division by OP1 with remainder MOD. */
3561 static rtx
3562 floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3564 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
3565 return gen_rtx_IF_THEN_ELSE
3566 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3567 gen_rtx_IF_THEN_ELSE
3568 (mode, gen_rtx_LT (BImode,
3569 gen_rtx_DIV (mode, op1, mod),
3570 const0_rtx),
3571 constm1_rtx, const0_rtx),
3572 const0_rtx);
3575 /* Return the difference between the ceil and the truncated result of
3576 a signed division by OP1 with remainder MOD. */
3577 static rtx
3578 ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3580 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
3581 return gen_rtx_IF_THEN_ELSE
3582 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3583 gen_rtx_IF_THEN_ELSE
3584 (mode, gen_rtx_GT (BImode,
3585 gen_rtx_DIV (mode, op1, mod),
3586 const0_rtx),
3587 const1_rtx, const0_rtx),
3588 const0_rtx);
3591 /* Return the difference between the ceil and the truncated result of
3592 an unsigned division by OP1 with remainder MOD. */
3593 static rtx
3594 ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
3596 /* (mod != 0 ? 1 : 0) */
3597 return gen_rtx_IF_THEN_ELSE
3598 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3599 const1_rtx, const0_rtx);
3602 /* Return the difference between the rounded and the truncated result
3603 of a signed division by OP1 with remainder MOD. Halfway cases are
3604 rounded away from zero, rather than to the nearest even number. */
3605 static rtx
3606 round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3608 /* (abs (mod) >= abs (op1) - abs (mod)
3609 ? (op1 / mod > 0 ? 1 : -1)
3610 : 0) */
3611 return gen_rtx_IF_THEN_ELSE
3612 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
3613 gen_rtx_MINUS (mode,
3614 gen_rtx_ABS (mode, op1),
3615 gen_rtx_ABS (mode, mod))),
3616 gen_rtx_IF_THEN_ELSE
3617 (mode, gen_rtx_GT (BImode,
3618 gen_rtx_DIV (mode, op1, mod),
3619 const0_rtx),
3620 const1_rtx, constm1_rtx),
3621 const0_rtx);
3624 /* Return the difference between the rounded and the truncated result
3625 of a unsigned division by OP1 with remainder MOD. Halfway cases
3626 are rounded away from zero, rather than to the nearest even
3627 number. */
3628 static rtx
3629 round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
3631 /* (mod >= op1 - mod ? 1 : 0) */
3632 return gen_rtx_IF_THEN_ELSE
3633 (mode, gen_rtx_GE (BImode, mod,
3634 gen_rtx_MINUS (mode, op1, mod)),
3635 const1_rtx, const0_rtx);
3638 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
3639 any rtl. */
3641 static rtx
3642 convert_debug_memory_address (machine_mode mode, rtx x,
3643 addr_space_t as)
3645 machine_mode xmode = GET_MODE (x);
3647 #ifndef POINTERS_EXTEND_UNSIGNED
3648 gcc_assert (mode == Pmode
3649 || mode == targetm.addr_space.address_mode (as));
3650 gcc_assert (xmode == mode || xmode == VOIDmode);
3651 #else
3652 rtx temp;
3654 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
3656 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
3657 return x;
3659 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
3660 x = simplify_gen_subreg (mode, x, xmode,
3661 subreg_lowpart_offset
3662 (mode, xmode));
3663 else if (POINTERS_EXTEND_UNSIGNED > 0)
3664 x = gen_rtx_ZERO_EXTEND (mode, x);
3665 else if (!POINTERS_EXTEND_UNSIGNED)
3666 x = gen_rtx_SIGN_EXTEND (mode, x);
3667 else
3669 switch (GET_CODE (x))
3671 case SUBREG:
3672 if ((SUBREG_PROMOTED_VAR_P (x)
3673 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
3674 || (GET_CODE (SUBREG_REG (x)) == PLUS
3675 && REG_P (XEXP (SUBREG_REG (x), 0))
3676 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
3677 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
3678 && GET_MODE (SUBREG_REG (x)) == mode)
3679 return SUBREG_REG (x);
3680 break;
3681 case LABEL_REF:
3682 temp = gen_rtx_LABEL_REF (mode, LABEL_REF_LABEL (x));
3683 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
3684 return temp;
3685 case SYMBOL_REF:
3686 temp = shallow_copy_rtx (x);
3687 PUT_MODE (temp, mode);
3688 return temp;
3689 case CONST:
3690 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3691 if (temp)
3692 temp = gen_rtx_CONST (mode, temp);
3693 return temp;
3694 case PLUS:
3695 case MINUS:
3696 if (CONST_INT_P (XEXP (x, 1)))
3698 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3699 if (temp)
3700 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
3702 break;
3703 default:
3704 break;
3706 /* Don't know how to express ptr_extend as operation in debug info. */
3707 return NULL;
3709 #endif /* POINTERS_EXTEND_UNSIGNED */
3711 return x;
3714 /* Map from SSA_NAMEs to corresponding DEBUG_EXPR_DECLs created
3715 by avoid_deep_ter_for_debug. */
3717 static hash_map<tree, tree> *deep_ter_debug_map;
3719 /* Split too deep TER chains for debug stmts using debug temporaries. */
3721 static void
3722 avoid_deep_ter_for_debug (gimple stmt, int depth)
3724 use_operand_p use_p;
3725 ssa_op_iter iter;
3726 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
3728 tree use = USE_FROM_PTR (use_p);
3729 if (TREE_CODE (use) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (use))
3730 continue;
3731 gimple g = get_gimple_for_ssa_name (use);
3732 if (g == NULL)
3733 continue;
3734 if (depth > 6 && !stmt_ends_bb_p (g))
3736 if (deep_ter_debug_map == NULL)
3737 deep_ter_debug_map = new hash_map<tree, tree>;
3739 tree &vexpr = deep_ter_debug_map->get_or_insert (use);
3740 if (vexpr != NULL)
3741 continue;
3742 vexpr = make_node (DEBUG_EXPR_DECL);
3743 gimple def_temp = gimple_build_debug_bind (vexpr, use, g);
3744 DECL_ARTIFICIAL (vexpr) = 1;
3745 TREE_TYPE (vexpr) = TREE_TYPE (use);
3746 DECL_MODE (vexpr) = TYPE_MODE (TREE_TYPE (use));
3747 gimple_stmt_iterator gsi = gsi_for_stmt (g);
3748 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
3749 avoid_deep_ter_for_debug (def_temp, 0);
3751 else
3752 avoid_deep_ter_for_debug (g, depth + 1);
3756 /* Return an RTX equivalent to the value of the parameter DECL. */
3758 static rtx
3759 expand_debug_parm_decl (tree decl)
3761 rtx incoming = DECL_INCOMING_RTL (decl);
3763 if (incoming
3764 && GET_MODE (incoming) != BLKmode
3765 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
3766 || (MEM_P (incoming)
3767 && REG_P (XEXP (incoming, 0))
3768 && HARD_REGISTER_P (XEXP (incoming, 0)))))
3770 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
3772 #ifdef HAVE_window_save
3773 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
3774 If the target machine has an explicit window save instruction, the
3775 actual entry value is the corresponding OUTGOING_REGNO instead. */
3776 if (REG_P (incoming)
3777 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
3778 incoming
3779 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
3780 OUTGOING_REGNO (REGNO (incoming)), 0);
3781 else if (MEM_P (incoming))
3783 rtx reg = XEXP (incoming, 0);
3784 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
3786 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
3787 incoming = replace_equiv_address_nv (incoming, reg);
3789 else
3790 incoming = copy_rtx (incoming);
3792 #endif
3794 ENTRY_VALUE_EXP (rtl) = incoming;
3795 return rtl;
3798 if (incoming
3799 && GET_MODE (incoming) != BLKmode
3800 && !TREE_ADDRESSABLE (decl)
3801 && MEM_P (incoming)
3802 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
3803 || (GET_CODE (XEXP (incoming, 0)) == PLUS
3804 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
3805 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
3806 return copy_rtx (incoming);
3808 return NULL_RTX;
3811 /* Return an RTX equivalent to the value of the tree expression EXP. */
3813 static rtx
3814 expand_debug_expr (tree exp)
3816 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
3817 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3818 machine_mode inner_mode = VOIDmode;
3819 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
3820 addr_space_t as;
3822 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
3824 case tcc_expression:
3825 switch (TREE_CODE (exp))
3827 case COND_EXPR:
3828 case DOT_PROD_EXPR:
3829 case SAD_EXPR:
3830 case WIDEN_MULT_PLUS_EXPR:
3831 case WIDEN_MULT_MINUS_EXPR:
3832 case FMA_EXPR:
3833 goto ternary;
3835 case TRUTH_ANDIF_EXPR:
3836 case TRUTH_ORIF_EXPR:
3837 case TRUTH_AND_EXPR:
3838 case TRUTH_OR_EXPR:
3839 case TRUTH_XOR_EXPR:
3840 goto binary;
3842 case TRUTH_NOT_EXPR:
3843 goto unary;
3845 default:
3846 break;
3848 break;
3850 ternary:
3851 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
3852 if (!op2)
3853 return NULL_RTX;
3854 /* Fall through. */
3856 binary:
3857 case tcc_binary:
3858 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
3859 if (!op1)
3860 return NULL_RTX;
3861 switch (TREE_CODE (exp))
3863 case LSHIFT_EXPR:
3864 case RSHIFT_EXPR:
3865 case LROTATE_EXPR:
3866 case RROTATE_EXPR:
3867 case WIDEN_LSHIFT_EXPR:
3868 /* Ensure second operand isn't wider than the first one. */
3869 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 1)));
3870 if (SCALAR_INT_MODE_P (inner_mode))
3872 machine_mode opmode = mode;
3873 if (VECTOR_MODE_P (mode))
3874 opmode = GET_MODE_INNER (mode);
3875 if (SCALAR_INT_MODE_P (opmode)
3876 && (GET_MODE_PRECISION (opmode)
3877 < GET_MODE_PRECISION (inner_mode)))
3878 op1 = simplify_gen_subreg (opmode, op1, inner_mode,
3879 subreg_lowpart_offset (opmode,
3880 inner_mode));
3882 break;
3883 default:
3884 break;
3886 /* Fall through. */
3888 unary:
3889 case tcc_unary:
3890 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3891 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3892 if (!op0)
3893 return NULL_RTX;
3894 break;
3896 case tcc_comparison:
3897 unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
3898 goto binary;
3900 case tcc_type:
3901 case tcc_statement:
3902 gcc_unreachable ();
3904 case tcc_constant:
3905 case tcc_exceptional:
3906 case tcc_declaration:
3907 case tcc_reference:
3908 case tcc_vl_exp:
3909 break;
3912 switch (TREE_CODE (exp))
3914 case STRING_CST:
3915 if (!lookup_constant_def (exp))
3917 if (strlen (TREE_STRING_POINTER (exp)) + 1
3918 != (size_t) TREE_STRING_LENGTH (exp))
3919 return NULL_RTX;
3920 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
3921 op0 = gen_rtx_MEM (BLKmode, op0);
3922 set_mem_attributes (op0, exp, 0);
3923 return op0;
3925 /* Fall through... */
3927 case INTEGER_CST:
3928 case REAL_CST:
3929 case FIXED_CST:
3930 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
3931 return op0;
3933 case COMPLEX_CST:
3934 gcc_assert (COMPLEX_MODE_P (mode));
3935 op0 = expand_debug_expr (TREE_REALPART (exp));
3936 op1 = expand_debug_expr (TREE_IMAGPART (exp));
3937 return gen_rtx_CONCAT (mode, op0, op1);
3939 case DEBUG_EXPR_DECL:
3940 op0 = DECL_RTL_IF_SET (exp);
3942 if (op0)
3943 return op0;
3945 op0 = gen_rtx_DEBUG_EXPR (mode);
3946 DEBUG_EXPR_TREE_DECL (op0) = exp;
3947 SET_DECL_RTL (exp, op0);
3949 return op0;
3951 case VAR_DECL:
3952 case PARM_DECL:
3953 case FUNCTION_DECL:
3954 case LABEL_DECL:
3955 case CONST_DECL:
3956 case RESULT_DECL:
3957 op0 = DECL_RTL_IF_SET (exp);
3959 /* This decl was probably optimized away. */
3960 if (!op0)
3962 if (TREE_CODE (exp) != VAR_DECL
3963 || DECL_EXTERNAL (exp)
3964 || !TREE_STATIC (exp)
3965 || !DECL_NAME (exp)
3966 || DECL_HARD_REGISTER (exp)
3967 || DECL_IN_CONSTANT_POOL (exp)
3968 || mode == VOIDmode)
3969 return NULL;
3971 op0 = make_decl_rtl_for_debug (exp);
3972 if (!MEM_P (op0)
3973 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
3974 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
3975 return NULL;
3977 else
3978 op0 = copy_rtx (op0);
3980 if (GET_MODE (op0) == BLKmode
3981 /* If op0 is not BLKmode, but mode is, adjust_mode
3982 below would ICE. While it is likely a FE bug,
3983 try to be robust here. See PR43166. */
3984 || mode == BLKmode
3985 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
3987 gcc_assert (MEM_P (op0));
3988 op0 = adjust_address_nv (op0, mode, 0);
3989 return op0;
3992 /* Fall through. */
3994 adjust_mode:
3995 case PAREN_EXPR:
3996 CASE_CONVERT:
3998 inner_mode = GET_MODE (op0);
4000 if (mode == inner_mode)
4001 return op0;
4003 if (inner_mode == VOIDmode)
4005 if (TREE_CODE (exp) == SSA_NAME)
4006 inner_mode = TYPE_MODE (TREE_TYPE (exp));
4007 else
4008 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4009 if (mode == inner_mode)
4010 return op0;
4013 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4015 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4016 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4017 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4018 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4019 else
4020 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4022 else if (FLOAT_MODE_P (mode))
4024 gcc_assert (TREE_CODE (exp) != SSA_NAME);
4025 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4026 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
4027 else
4028 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
4030 else if (FLOAT_MODE_P (inner_mode))
4032 if (unsignedp)
4033 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4034 else
4035 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4037 else if (CONSTANT_P (op0)
4038 || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
4039 op0 = simplify_gen_subreg (mode, op0, inner_mode,
4040 subreg_lowpart_offset (mode,
4041 inner_mode));
4042 else if (UNARY_CLASS_P (exp)
4043 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
4044 : unsignedp)
4045 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4046 else
4047 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4049 return op0;
4052 case MEM_REF:
4053 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4055 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
4056 TREE_OPERAND (exp, 0),
4057 TREE_OPERAND (exp, 1));
4058 if (newexp)
4059 return expand_debug_expr (newexp);
4061 /* FALLTHROUGH */
4062 case INDIRECT_REF:
4063 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4064 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4065 if (!op0)
4066 return NULL;
4068 if (TREE_CODE (exp) == MEM_REF)
4070 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4071 || (GET_CODE (op0) == PLUS
4072 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
4073 /* (mem (debug_implicit_ptr)) might confuse aliasing.
4074 Instead just use get_inner_reference. */
4075 goto component_ref;
4077 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4078 if (!op1 || !CONST_INT_P (op1))
4079 return NULL;
4081 op0 = plus_constant (inner_mode, op0, INTVAL (op1));
4084 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4086 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4087 op0, as);
4088 if (op0 == NULL_RTX)
4089 return NULL;
4091 op0 = gen_rtx_MEM (mode, op0);
4092 set_mem_attributes (op0, exp, 0);
4093 if (TREE_CODE (exp) == MEM_REF
4094 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4095 set_mem_expr (op0, NULL_TREE);
4096 set_mem_addr_space (op0, as);
4098 return op0;
4100 case TARGET_MEM_REF:
4101 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
4102 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
4103 return NULL;
4105 op0 = expand_debug_expr
4106 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
4107 if (!op0)
4108 return NULL;
4110 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4111 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4112 op0, as);
4113 if (op0 == NULL_RTX)
4114 return NULL;
4116 op0 = gen_rtx_MEM (mode, op0);
4118 set_mem_attributes (op0, exp, 0);
4119 set_mem_addr_space (op0, as);
4121 return op0;
4123 component_ref:
4124 case ARRAY_REF:
4125 case ARRAY_RANGE_REF:
4126 case COMPONENT_REF:
4127 case BIT_FIELD_REF:
4128 case REALPART_EXPR:
4129 case IMAGPART_EXPR:
4130 case VIEW_CONVERT_EXPR:
4132 machine_mode mode1;
4133 HOST_WIDE_INT bitsize, bitpos;
4134 tree offset;
4135 int volatilep = 0;
4136 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
4137 &mode1, &unsignedp, &volatilep, false);
4138 rtx orig_op0;
4140 if (bitsize == 0)
4141 return NULL;
4143 orig_op0 = op0 = expand_debug_expr (tem);
4145 if (!op0)
4146 return NULL;
4148 if (offset)
4150 machine_mode addrmode, offmode;
4152 if (!MEM_P (op0))
4153 return NULL;
4155 op0 = XEXP (op0, 0);
4156 addrmode = GET_MODE (op0);
4157 if (addrmode == VOIDmode)
4158 addrmode = Pmode;
4160 op1 = expand_debug_expr (offset);
4161 if (!op1)
4162 return NULL;
4164 offmode = GET_MODE (op1);
4165 if (offmode == VOIDmode)
4166 offmode = TYPE_MODE (TREE_TYPE (offset));
4168 if (addrmode != offmode)
4169 op1 = simplify_gen_subreg (addrmode, op1, offmode,
4170 subreg_lowpart_offset (addrmode,
4171 offmode));
4173 /* Don't use offset_address here, we don't need a
4174 recognizable address, and we don't want to generate
4175 code. */
4176 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4177 op0, op1));
4180 if (MEM_P (op0))
4182 if (mode1 == VOIDmode)
4183 /* Bitfield. */
4184 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
4185 if (bitpos >= BITS_PER_UNIT)
4187 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
4188 bitpos %= BITS_PER_UNIT;
4190 else if (bitpos < 0)
4192 HOST_WIDE_INT units
4193 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4194 op0 = adjust_address_nv (op0, mode1, units);
4195 bitpos += units * BITS_PER_UNIT;
4197 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
4198 op0 = adjust_address_nv (op0, mode, 0);
4199 else if (GET_MODE (op0) != mode1)
4200 op0 = adjust_address_nv (op0, mode1, 0);
4201 else
4202 op0 = copy_rtx (op0);
4203 if (op0 == orig_op0)
4204 op0 = shallow_copy_rtx (op0);
4205 set_mem_attributes (op0, exp, 0);
4208 if (bitpos == 0 && mode == GET_MODE (op0))
4209 return op0;
4211 if (bitpos < 0)
4212 return NULL;
4214 if (GET_MODE (op0) == BLKmode)
4215 return NULL;
4217 if ((bitpos % BITS_PER_UNIT) == 0
4218 && bitsize == GET_MODE_BITSIZE (mode1))
4220 machine_mode opmode = GET_MODE (op0);
4222 if (opmode == VOIDmode)
4223 opmode = TYPE_MODE (TREE_TYPE (tem));
4225 /* This condition may hold if we're expanding the address
4226 right past the end of an array that turned out not to
4227 be addressable (i.e., the address was only computed in
4228 debug stmts). The gen_subreg below would rightfully
4229 crash, and the address doesn't really exist, so just
4230 drop it. */
4231 if (bitpos >= GET_MODE_BITSIZE (opmode))
4232 return NULL;
4234 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
4235 return simplify_gen_subreg (mode, op0, opmode,
4236 bitpos / BITS_PER_UNIT);
4239 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4240 && TYPE_UNSIGNED (TREE_TYPE (exp))
4241 ? SIGN_EXTRACT
4242 : ZERO_EXTRACT, mode,
4243 GET_MODE (op0) != VOIDmode
4244 ? GET_MODE (op0)
4245 : TYPE_MODE (TREE_TYPE (tem)),
4246 op0, GEN_INT (bitsize), GEN_INT (bitpos));
4249 case ABS_EXPR:
4250 return simplify_gen_unary (ABS, mode, op0, mode);
4252 case NEGATE_EXPR:
4253 return simplify_gen_unary (NEG, mode, op0, mode);
4255 case BIT_NOT_EXPR:
4256 return simplify_gen_unary (NOT, mode, op0, mode);
4258 case FLOAT_EXPR:
4259 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4260 0)))
4261 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4262 inner_mode);
4264 case FIX_TRUNC_EXPR:
4265 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4266 inner_mode);
4268 case POINTER_PLUS_EXPR:
4269 /* For the rare target where pointers are not the same size as
4270 size_t, we need to check for mis-matched modes and correct
4271 the addend. */
4272 if (op0 && op1
4273 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
4274 && GET_MODE (op0) != GET_MODE (op1))
4276 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1))
4277 /* If OP0 is a partial mode, then we must truncate, even if it has
4278 the same bitsize as OP1 as GCC's representation of partial modes
4279 is opaque. */
4280 || (GET_MODE_CLASS (GET_MODE (op0)) == MODE_PARTIAL_INT
4281 && GET_MODE_BITSIZE (GET_MODE (op0)) == GET_MODE_BITSIZE (GET_MODE (op1))))
4282 op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
4283 GET_MODE (op1));
4284 else
4285 /* We always sign-extend, regardless of the signedness of
4286 the operand, because the operand is always unsigned
4287 here even if the original C expression is signed. */
4288 op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
4289 GET_MODE (op1));
4291 /* Fall through. */
4292 case PLUS_EXPR:
4293 return simplify_gen_binary (PLUS, mode, op0, op1);
4295 case MINUS_EXPR:
4296 return simplify_gen_binary (MINUS, mode, op0, op1);
4298 case MULT_EXPR:
4299 return simplify_gen_binary (MULT, mode, op0, op1);
4301 case RDIV_EXPR:
4302 case TRUNC_DIV_EXPR:
4303 case EXACT_DIV_EXPR:
4304 if (unsignedp)
4305 return simplify_gen_binary (UDIV, mode, op0, op1);
4306 else
4307 return simplify_gen_binary (DIV, mode, op0, op1);
4309 case TRUNC_MOD_EXPR:
4310 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4312 case FLOOR_DIV_EXPR:
4313 if (unsignedp)
4314 return simplify_gen_binary (UDIV, mode, op0, op1);
4315 else
4317 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4318 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4319 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4320 return simplify_gen_binary (PLUS, mode, div, adj);
4323 case FLOOR_MOD_EXPR:
4324 if (unsignedp)
4325 return simplify_gen_binary (UMOD, mode, op0, op1);
4326 else
4328 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4329 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4330 adj = simplify_gen_unary (NEG, mode,
4331 simplify_gen_binary (MULT, mode, adj, op1),
4332 mode);
4333 return simplify_gen_binary (PLUS, mode, mod, adj);
4336 case CEIL_DIV_EXPR:
4337 if (unsignedp)
4339 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4340 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4341 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4342 return simplify_gen_binary (PLUS, mode, div, adj);
4344 else
4346 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4347 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4348 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4349 return simplify_gen_binary (PLUS, mode, div, adj);
4352 case CEIL_MOD_EXPR:
4353 if (unsignedp)
4355 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4356 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4357 adj = simplify_gen_unary (NEG, mode,
4358 simplify_gen_binary (MULT, mode, adj, op1),
4359 mode);
4360 return simplify_gen_binary (PLUS, mode, mod, adj);
4362 else
4364 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4365 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4366 adj = simplify_gen_unary (NEG, mode,
4367 simplify_gen_binary (MULT, mode, adj, op1),
4368 mode);
4369 return simplify_gen_binary (PLUS, mode, mod, adj);
4372 case ROUND_DIV_EXPR:
4373 if (unsignedp)
4375 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4376 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4377 rtx adj = round_udiv_adjust (mode, mod, op1);
4378 return simplify_gen_binary (PLUS, mode, div, adj);
4380 else
4382 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4383 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4384 rtx adj = round_sdiv_adjust (mode, mod, op1);
4385 return simplify_gen_binary (PLUS, mode, div, adj);
4388 case ROUND_MOD_EXPR:
4389 if (unsignedp)
4391 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4392 rtx adj = round_udiv_adjust (mode, mod, op1);
4393 adj = simplify_gen_unary (NEG, mode,
4394 simplify_gen_binary (MULT, mode, adj, op1),
4395 mode);
4396 return simplify_gen_binary (PLUS, mode, mod, adj);
4398 else
4400 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4401 rtx adj = round_sdiv_adjust (mode, mod, op1);
4402 adj = simplify_gen_unary (NEG, mode,
4403 simplify_gen_binary (MULT, mode, adj, op1),
4404 mode);
4405 return simplify_gen_binary (PLUS, mode, mod, adj);
4408 case LSHIFT_EXPR:
4409 return simplify_gen_binary (ASHIFT, mode, op0, op1);
4411 case RSHIFT_EXPR:
4412 if (unsignedp)
4413 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
4414 else
4415 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
4417 case LROTATE_EXPR:
4418 return simplify_gen_binary (ROTATE, mode, op0, op1);
4420 case RROTATE_EXPR:
4421 return simplify_gen_binary (ROTATERT, mode, op0, op1);
4423 case MIN_EXPR:
4424 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
4426 case MAX_EXPR:
4427 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
4429 case BIT_AND_EXPR:
4430 case TRUTH_AND_EXPR:
4431 return simplify_gen_binary (AND, mode, op0, op1);
4433 case BIT_IOR_EXPR:
4434 case TRUTH_OR_EXPR:
4435 return simplify_gen_binary (IOR, mode, op0, op1);
4437 case BIT_XOR_EXPR:
4438 case TRUTH_XOR_EXPR:
4439 return simplify_gen_binary (XOR, mode, op0, op1);
4441 case TRUTH_ANDIF_EXPR:
4442 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
4444 case TRUTH_ORIF_EXPR:
4445 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
4447 case TRUTH_NOT_EXPR:
4448 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
4450 case LT_EXPR:
4451 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
4452 op0, op1);
4454 case LE_EXPR:
4455 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
4456 op0, op1);
4458 case GT_EXPR:
4459 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
4460 op0, op1);
4462 case GE_EXPR:
4463 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
4464 op0, op1);
4466 case EQ_EXPR:
4467 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
4469 case NE_EXPR:
4470 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
4472 case UNORDERED_EXPR:
4473 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
4475 case ORDERED_EXPR:
4476 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
4478 case UNLT_EXPR:
4479 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
4481 case UNLE_EXPR:
4482 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
4484 case UNGT_EXPR:
4485 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
4487 case UNGE_EXPR:
4488 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
4490 case UNEQ_EXPR:
4491 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
4493 case LTGT_EXPR:
4494 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
4496 case COND_EXPR:
4497 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
4499 case COMPLEX_EXPR:
4500 gcc_assert (COMPLEX_MODE_P (mode));
4501 if (GET_MODE (op0) == VOIDmode)
4502 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
4503 if (GET_MODE (op1) == VOIDmode)
4504 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
4505 return gen_rtx_CONCAT (mode, op0, op1);
4507 case CONJ_EXPR:
4508 if (GET_CODE (op0) == CONCAT)
4509 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
4510 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
4511 XEXP (op0, 1),
4512 GET_MODE_INNER (mode)));
4513 else
4515 machine_mode imode = GET_MODE_INNER (mode);
4516 rtx re, im;
4518 if (MEM_P (op0))
4520 re = adjust_address_nv (op0, imode, 0);
4521 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
4523 else
4525 machine_mode ifmode = int_mode_for_mode (mode);
4526 machine_mode ihmode = int_mode_for_mode (imode);
4527 rtx halfsize;
4528 if (ifmode == BLKmode || ihmode == BLKmode)
4529 return NULL;
4530 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
4531 re = op0;
4532 if (mode != ifmode)
4533 re = gen_rtx_SUBREG (ifmode, re, 0);
4534 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
4535 if (imode != ihmode)
4536 re = gen_rtx_SUBREG (imode, re, 0);
4537 im = copy_rtx (op0);
4538 if (mode != ifmode)
4539 im = gen_rtx_SUBREG (ifmode, im, 0);
4540 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
4541 if (imode != ihmode)
4542 im = gen_rtx_SUBREG (imode, im, 0);
4544 im = gen_rtx_NEG (imode, im);
4545 return gen_rtx_CONCAT (mode, re, im);
4548 case ADDR_EXPR:
4549 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4550 if (!op0 || !MEM_P (op0))
4552 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
4553 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
4554 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
4555 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
4556 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
4557 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
4559 if (handled_component_p (TREE_OPERAND (exp, 0)))
4561 HOST_WIDE_INT bitoffset, bitsize, maxsize;
4562 tree decl
4563 = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
4564 &bitoffset, &bitsize, &maxsize);
4565 if ((TREE_CODE (decl) == VAR_DECL
4566 || TREE_CODE (decl) == PARM_DECL
4567 || TREE_CODE (decl) == RESULT_DECL)
4568 && (!TREE_ADDRESSABLE (decl)
4569 || target_for_debug_bind (decl))
4570 && (bitoffset % BITS_PER_UNIT) == 0
4571 && bitsize > 0
4572 && bitsize == maxsize)
4574 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
4575 return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
4579 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
4580 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4581 == ADDR_EXPR)
4583 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4584 0));
4585 if (op0 != NULL
4586 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4587 || (GET_CODE (op0) == PLUS
4588 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
4589 && CONST_INT_P (XEXP (op0, 1)))))
4591 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4592 1));
4593 if (!op1 || !CONST_INT_P (op1))
4594 return NULL;
4596 return plus_constant (mode, op0, INTVAL (op1));
4600 return NULL;
4603 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
4604 op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
4606 return op0;
4608 case VECTOR_CST:
4610 unsigned i;
4612 op0 = gen_rtx_CONCATN
4613 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4615 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
4617 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
4618 if (!op1)
4619 return NULL;
4620 XVECEXP (op0, 0, i) = op1;
4623 return op0;
4626 case CONSTRUCTOR:
4627 if (TREE_CLOBBER_P (exp))
4628 return NULL;
4629 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
4631 unsigned i;
4632 tree val;
4634 op0 = gen_rtx_CONCATN
4635 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4637 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
4639 op1 = expand_debug_expr (val);
4640 if (!op1)
4641 return NULL;
4642 XVECEXP (op0, 0, i) = op1;
4645 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
4647 op1 = expand_debug_expr
4648 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
4650 if (!op1)
4651 return NULL;
4653 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
4654 XVECEXP (op0, 0, i) = op1;
4657 return op0;
4659 else
4660 goto flag_unsupported;
4662 case CALL_EXPR:
4663 /* ??? Maybe handle some builtins? */
4664 return NULL;
4666 case SSA_NAME:
4668 gimple g = get_gimple_for_ssa_name (exp);
4669 if (g)
4671 tree t = NULL_TREE;
4672 if (deep_ter_debug_map)
4674 tree *slot = deep_ter_debug_map->get (exp);
4675 if (slot)
4676 t = *slot;
4678 if (t == NULL_TREE)
4679 t = gimple_assign_rhs_to_tree (g);
4680 op0 = expand_debug_expr (t);
4681 if (!op0)
4682 return NULL;
4684 else
4686 int part = var_to_partition (SA.map, exp);
4688 if (part == NO_PARTITION)
4690 /* If this is a reference to an incoming value of parameter
4691 that is never used in the code or where the incoming
4692 value is never used in the code, use PARM_DECL's
4693 DECL_RTL if set. */
4694 if (SSA_NAME_IS_DEFAULT_DEF (exp)
4695 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL)
4697 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
4698 if (op0)
4699 goto adjust_mode;
4700 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
4701 if (op0)
4702 goto adjust_mode;
4704 return NULL;
4707 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
4709 op0 = copy_rtx (SA.partition_to_pseudo[part]);
4711 goto adjust_mode;
4714 case ERROR_MARK:
4715 return NULL;
4717 /* Vector stuff. For most of the codes we don't have rtl codes. */
4718 case REALIGN_LOAD_EXPR:
4719 case REDUC_MAX_EXPR:
4720 case REDUC_MIN_EXPR:
4721 case REDUC_PLUS_EXPR:
4722 case VEC_COND_EXPR:
4723 case VEC_PACK_FIX_TRUNC_EXPR:
4724 case VEC_PACK_SAT_EXPR:
4725 case VEC_PACK_TRUNC_EXPR:
4726 case VEC_UNPACK_FLOAT_HI_EXPR:
4727 case VEC_UNPACK_FLOAT_LO_EXPR:
4728 case VEC_UNPACK_HI_EXPR:
4729 case VEC_UNPACK_LO_EXPR:
4730 case VEC_WIDEN_MULT_HI_EXPR:
4731 case VEC_WIDEN_MULT_LO_EXPR:
4732 case VEC_WIDEN_MULT_EVEN_EXPR:
4733 case VEC_WIDEN_MULT_ODD_EXPR:
4734 case VEC_WIDEN_LSHIFT_HI_EXPR:
4735 case VEC_WIDEN_LSHIFT_LO_EXPR:
4736 case VEC_PERM_EXPR:
4737 return NULL;
4739 /* Misc codes. */
4740 case ADDR_SPACE_CONVERT_EXPR:
4741 case FIXED_CONVERT_EXPR:
4742 case OBJ_TYPE_REF:
4743 case WITH_SIZE_EXPR:
4744 return NULL;
4746 case DOT_PROD_EXPR:
4747 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4748 && SCALAR_INT_MODE_P (mode))
4751 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4752 0)))
4753 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4754 inner_mode);
4756 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4757 1)))
4758 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
4759 inner_mode);
4760 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4761 return simplify_gen_binary (PLUS, mode, op0, op2);
4763 return NULL;
4765 case WIDEN_MULT_EXPR:
4766 case WIDEN_MULT_PLUS_EXPR:
4767 case WIDEN_MULT_MINUS_EXPR:
4768 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4769 && SCALAR_INT_MODE_P (mode))
4771 inner_mode = GET_MODE (op0);
4772 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4773 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4774 else
4775 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4776 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
4777 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
4778 else
4779 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
4780 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4781 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
4782 return op0;
4783 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
4784 return simplify_gen_binary (PLUS, mode, op0, op2);
4785 else
4786 return simplify_gen_binary (MINUS, mode, op2, op0);
4788 return NULL;
4790 case MULT_HIGHPART_EXPR:
4791 /* ??? Similar to the above. */
4792 return NULL;
4794 case WIDEN_SUM_EXPR:
4795 case WIDEN_LSHIFT_EXPR:
4796 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4797 && SCALAR_INT_MODE_P (mode))
4800 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4801 0)))
4802 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4803 inner_mode);
4804 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
4805 ? ASHIFT : PLUS, mode, op0, op1);
4807 return NULL;
4809 case FMA_EXPR:
4810 return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
4812 default:
4813 flag_unsupported:
4814 #ifdef ENABLE_CHECKING
4815 debug_tree (exp);
4816 gcc_unreachable ();
4817 #else
4818 return NULL;
4819 #endif
4823 /* Return an RTX equivalent to the source bind value of the tree expression
4824 EXP. */
4826 static rtx
4827 expand_debug_source_expr (tree exp)
4829 rtx op0 = NULL_RTX;
4830 machine_mode mode = VOIDmode, inner_mode;
4832 switch (TREE_CODE (exp))
4834 case PARM_DECL:
4836 mode = DECL_MODE (exp);
4837 op0 = expand_debug_parm_decl (exp);
4838 if (op0)
4839 break;
4840 /* See if this isn't an argument that has been completely
4841 optimized out. */
4842 if (!DECL_RTL_SET_P (exp)
4843 && !DECL_INCOMING_RTL (exp)
4844 && DECL_ABSTRACT_ORIGIN (current_function_decl))
4846 tree aexp = DECL_ORIGIN (exp);
4847 if (DECL_CONTEXT (aexp)
4848 == DECL_ABSTRACT_ORIGIN (current_function_decl))
4850 vec<tree, va_gc> **debug_args;
4851 unsigned int ix;
4852 tree ddecl;
4853 debug_args = decl_debug_args_lookup (current_function_decl);
4854 if (debug_args != NULL)
4856 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
4857 ix += 2)
4858 if (ddecl == aexp)
4859 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
4863 break;
4865 default:
4866 break;
4869 if (op0 == NULL_RTX)
4870 return NULL_RTX;
4872 inner_mode = GET_MODE (op0);
4873 if (mode == inner_mode)
4874 return op0;
4876 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4878 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4879 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4880 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4881 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4882 else
4883 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4885 else if (FLOAT_MODE_P (mode))
4886 gcc_unreachable ();
4887 else if (FLOAT_MODE_P (inner_mode))
4889 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4890 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4891 else
4892 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4894 else if (CONSTANT_P (op0)
4895 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
4896 op0 = simplify_gen_subreg (mode, op0, inner_mode,
4897 subreg_lowpart_offset (mode, inner_mode));
4898 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4899 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4900 else
4901 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4903 return op0;
4906 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
4907 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
4908 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
4910 static void
4911 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
4913 rtx exp = *exp_p;
4915 if (exp == NULL_RTX)
4916 return;
4918 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
4919 return;
4921 if (depth == 4)
4923 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
4924 rtx dval = make_debug_expr_from_rtl (exp);
4926 /* Emit a debug bind insn before INSN. */
4927 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
4928 DEBUG_EXPR_TREE_DECL (dval), exp,
4929 VAR_INIT_STATUS_INITIALIZED);
4931 emit_debug_insn_before (bind, insn);
4932 *exp_p = dval;
4933 return;
4936 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
4937 int i, j;
4938 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4939 switch (*format_ptr++)
4941 case 'e':
4942 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
4943 break;
4945 case 'E':
4946 case 'V':
4947 for (j = 0; j < XVECLEN (exp, i); j++)
4948 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
4949 break;
4951 default:
4952 break;
4956 /* Expand the _LOCs in debug insns. We run this after expanding all
4957 regular insns, so that any variables referenced in the function
4958 will have their DECL_RTLs set. */
4960 static void
4961 expand_debug_locations (void)
4963 rtx_insn *insn;
4964 rtx_insn *last = get_last_insn ();
4965 int save_strict_alias = flag_strict_aliasing;
4967 /* New alias sets while setting up memory attributes cause
4968 -fcompare-debug failures, even though it doesn't bring about any
4969 codegen changes. */
4970 flag_strict_aliasing = 0;
4972 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4973 if (DEBUG_INSN_P (insn))
4975 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
4976 rtx val;
4977 rtx_insn *prev_insn, *insn2;
4978 machine_mode mode;
4980 if (value == NULL_TREE)
4981 val = NULL_RTX;
4982 else
4984 if (INSN_VAR_LOCATION_STATUS (insn)
4985 == VAR_INIT_STATUS_UNINITIALIZED)
4986 val = expand_debug_source_expr (value);
4987 /* The avoid_deep_ter_for_debug function inserts
4988 debug bind stmts after SSA_NAME definition, with the
4989 SSA_NAME as the whole bind location. Disable temporarily
4990 expansion of that SSA_NAME into the DEBUG_EXPR_DECL
4991 being defined in this DEBUG_INSN. */
4992 else if (deep_ter_debug_map && TREE_CODE (value) == SSA_NAME)
4994 tree *slot = deep_ter_debug_map->get (value);
4995 if (slot)
4997 if (*slot == INSN_VAR_LOCATION_DECL (insn))
4998 *slot = NULL_TREE;
4999 else
5000 slot = NULL;
5002 val = expand_debug_expr (value);
5003 if (slot)
5004 *slot = INSN_VAR_LOCATION_DECL (insn);
5006 else
5007 val = expand_debug_expr (value);
5008 gcc_assert (last == get_last_insn ());
5011 if (!val)
5012 val = gen_rtx_UNKNOWN_VAR_LOC ();
5013 else
5015 mode = GET_MODE (INSN_VAR_LOCATION (insn));
5017 gcc_assert (mode == GET_MODE (val)
5018 || (GET_MODE (val) == VOIDmode
5019 && (CONST_SCALAR_INT_P (val)
5020 || GET_CODE (val) == CONST_FIXED
5021 || GET_CODE (val) == LABEL_REF)));
5024 INSN_VAR_LOCATION_LOC (insn) = val;
5025 prev_insn = PREV_INSN (insn);
5026 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
5027 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
5030 flag_strict_aliasing = save_strict_alias;
5033 /* Performs swapping operands of commutative operations to expand
5034 the expensive one first. */
5036 static void
5037 reorder_operands (basic_block bb)
5039 unsigned int *lattice; /* Hold cost of each statement. */
5040 unsigned int i = 0, n = 0;
5041 gimple_stmt_iterator gsi;
5042 gimple_seq stmts;
5043 gimple stmt;
5044 bool swap;
5045 tree op0, op1;
5046 ssa_op_iter iter;
5047 use_operand_p use_p;
5048 gimple def0, def1;
5050 /* Compute cost of each statement using estimate_num_insns. */
5051 stmts = bb_seq (bb);
5052 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5054 stmt = gsi_stmt (gsi);
5055 if (!is_gimple_debug (stmt))
5056 gimple_set_uid (stmt, n++);
5058 lattice = XNEWVEC (unsigned int, n);
5059 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5061 unsigned cost;
5062 stmt = gsi_stmt (gsi);
5063 if (is_gimple_debug (stmt))
5064 continue;
5065 cost = estimate_num_insns (stmt, &eni_size_weights);
5066 lattice[i] = cost;
5067 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
5069 tree use = USE_FROM_PTR (use_p);
5070 gimple def_stmt;
5071 if (TREE_CODE (use) != SSA_NAME)
5072 continue;
5073 def_stmt = get_gimple_for_ssa_name (use);
5074 if (!def_stmt)
5075 continue;
5076 lattice[i] += lattice[gimple_uid (def_stmt)];
5078 i++;
5079 if (!is_gimple_assign (stmt)
5080 || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
5081 continue;
5082 op0 = gimple_op (stmt, 1);
5083 op1 = gimple_op (stmt, 2);
5084 if (TREE_CODE (op0) != SSA_NAME
5085 || TREE_CODE (op1) != SSA_NAME)
5086 continue;
5087 /* Swap operands if the second one is more expensive. */
5088 def0 = get_gimple_for_ssa_name (op0);
5089 def1 = get_gimple_for_ssa_name (op1);
5090 if (!def1)
5091 continue;
5092 swap = false;
5093 if (!def0 || lattice[gimple_uid (def1)] > lattice[gimple_uid (def0)])
5094 swap = true;
5095 if (swap)
5097 if (dump_file && (dump_flags & TDF_DETAILS))
5099 fprintf (dump_file, "Swap operands in stmt:\n");
5100 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5101 fprintf (dump_file, "Cost left opnd=%d, right opnd=%d\n",
5102 def0 ? lattice[gimple_uid (def0)] : 0,
5103 lattice[gimple_uid (def1)]);
5105 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
5106 gimple_assign_rhs2_ptr (stmt));
5109 XDELETE (lattice);
5112 /* Expand basic block BB from GIMPLE trees to RTL. */
5114 static basic_block
5115 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
5117 gimple_stmt_iterator gsi;
5118 gimple_seq stmts;
5119 gimple stmt = NULL;
5120 rtx_note *note;
5121 rtx_insn *last;
5122 edge e;
5123 edge_iterator ei;
5125 if (dump_file)
5126 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
5127 bb->index);
5129 /* Note that since we are now transitioning from GIMPLE to RTL, we
5130 cannot use the gsi_*_bb() routines because they expect the basic
5131 block to be in GIMPLE, instead of RTL. Therefore, we need to
5132 access the BB sequence directly. */
5133 if (optimize)
5134 reorder_operands (bb);
5135 stmts = bb_seq (bb);
5136 bb->il.gimple.seq = NULL;
5137 bb->il.gimple.phi_nodes = NULL;
5138 rtl_profile_for_bb (bb);
5139 init_rtl_bb_info (bb);
5140 bb->flags |= BB_RTL;
5142 /* Remove the RETURN_EXPR if we may fall though to the exit
5143 instead. */
5144 gsi = gsi_last (stmts);
5145 if (!gsi_end_p (gsi)
5146 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
5148 greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
5150 gcc_assert (single_succ_p (bb));
5151 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
5153 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5154 && !gimple_return_retval (ret_stmt))
5156 gsi_remove (&gsi, false);
5157 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
5161 gsi = gsi_start (stmts);
5162 if (!gsi_end_p (gsi))
5164 stmt = gsi_stmt (gsi);
5165 if (gimple_code (stmt) != GIMPLE_LABEL)
5166 stmt = NULL;
5169 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
5171 if (stmt || elt)
5173 last = get_last_insn ();
5175 if (stmt)
5177 expand_gimple_stmt (stmt);
5178 gsi_next (&gsi);
5181 if (elt)
5182 emit_label (*elt);
5184 /* Java emits line number notes in the top of labels.
5185 ??? Make this go away once line number notes are obsoleted. */
5186 BB_HEAD (bb) = NEXT_INSN (last);
5187 if (NOTE_P (BB_HEAD (bb)))
5188 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
5189 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
5191 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5193 else
5194 BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
5196 NOTE_BASIC_BLOCK (note) = bb;
5198 for (; !gsi_end_p (gsi); gsi_next (&gsi))
5200 basic_block new_bb;
5202 stmt = gsi_stmt (gsi);
5204 /* If this statement is a non-debug one, and we generate debug
5205 insns, then this one might be the last real use of a TERed
5206 SSA_NAME, but where there are still some debug uses further
5207 down. Expanding the current SSA name in such further debug
5208 uses by their RHS might lead to wrong debug info, as coalescing
5209 might make the operands of such RHS be placed into the same
5210 pseudo as something else. Like so:
5211 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
5212 use(a_1);
5213 a_2 = ...
5214 #DEBUG ... => a_1
5215 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
5216 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
5217 the write to a_2 would actually have clobbered the place which
5218 formerly held a_0.
5220 So, instead of that, we recognize the situation, and generate
5221 debug temporaries at the last real use of TERed SSA names:
5222 a_1 = a_0 + 1;
5223 #DEBUG #D1 => a_1
5224 use(a_1);
5225 a_2 = ...
5226 #DEBUG ... => #D1
5228 if (MAY_HAVE_DEBUG_INSNS
5229 && SA.values
5230 && !is_gimple_debug (stmt))
5232 ssa_op_iter iter;
5233 tree op;
5234 gimple def;
5236 location_t sloc = curr_insn_location ();
5238 /* Look for SSA names that have their last use here (TERed
5239 names always have only one real use). */
5240 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5241 if ((def = get_gimple_for_ssa_name (op)))
5243 imm_use_iterator imm_iter;
5244 use_operand_p use_p;
5245 bool have_debug_uses = false;
5247 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5249 if (gimple_debug_bind_p (USE_STMT (use_p)))
5251 have_debug_uses = true;
5252 break;
5256 if (have_debug_uses)
5258 /* OP is a TERed SSA name, with DEF its defining
5259 statement, and where OP is used in further debug
5260 instructions. Generate a debug temporary, and
5261 replace all uses of OP in debug insns with that
5262 temporary. */
5263 gimple debugstmt;
5264 tree value = gimple_assign_rhs_to_tree (def);
5265 tree vexpr = make_node (DEBUG_EXPR_DECL);
5266 rtx val;
5267 machine_mode mode;
5269 set_curr_insn_location (gimple_location (def));
5271 DECL_ARTIFICIAL (vexpr) = 1;
5272 TREE_TYPE (vexpr) = TREE_TYPE (value);
5273 if (DECL_P (value))
5274 mode = DECL_MODE (value);
5275 else
5276 mode = TYPE_MODE (TREE_TYPE (value));
5277 DECL_MODE (vexpr) = mode;
5279 val = gen_rtx_VAR_LOCATION
5280 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5282 emit_debug_insn (val);
5284 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5286 if (!gimple_debug_bind_p (debugstmt))
5287 continue;
5289 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5290 SET_USE (use_p, vexpr);
5292 update_stmt (debugstmt);
5296 set_curr_insn_location (sloc);
5299 currently_expanding_gimple_stmt = stmt;
5301 /* Expand this statement, then evaluate the resulting RTL and
5302 fixup the CFG accordingly. */
5303 if (gimple_code (stmt) == GIMPLE_COND)
5305 new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
5306 if (new_bb)
5307 return new_bb;
5309 else if (gimple_debug_bind_p (stmt))
5311 location_t sloc = curr_insn_location ();
5312 gimple_stmt_iterator nsi = gsi;
5314 for (;;)
5316 tree var = gimple_debug_bind_get_var (stmt);
5317 tree value;
5318 rtx val;
5319 machine_mode mode;
5321 if (TREE_CODE (var) != DEBUG_EXPR_DECL
5322 && TREE_CODE (var) != LABEL_DECL
5323 && !target_for_debug_bind (var))
5324 goto delink_debug_stmt;
5326 if (gimple_debug_bind_has_value_p (stmt))
5327 value = gimple_debug_bind_get_value (stmt);
5328 else
5329 value = NULL_TREE;
5331 last = get_last_insn ();
5333 set_curr_insn_location (gimple_location (stmt));
5335 if (DECL_P (var))
5336 mode = DECL_MODE (var);
5337 else
5338 mode = TYPE_MODE (TREE_TYPE (var));
5340 val = gen_rtx_VAR_LOCATION
5341 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5343 emit_debug_insn (val);
5345 if (dump_file && (dump_flags & TDF_DETAILS))
5347 /* We can't dump the insn with a TREE where an RTX
5348 is expected. */
5349 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5350 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5351 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5354 delink_debug_stmt:
5355 /* In order not to generate too many debug temporaries,
5356 we delink all uses of debug statements we already expanded.
5357 Therefore debug statements between definition and real
5358 use of TERed SSA names will continue to use the SSA name,
5359 and not be replaced with debug temps. */
5360 delink_stmt_imm_use (stmt);
5362 gsi = nsi;
5363 gsi_next (&nsi);
5364 if (gsi_end_p (nsi))
5365 break;
5366 stmt = gsi_stmt (nsi);
5367 if (!gimple_debug_bind_p (stmt))
5368 break;
5371 set_curr_insn_location (sloc);
5373 else if (gimple_debug_source_bind_p (stmt))
5375 location_t sloc = curr_insn_location ();
5376 tree var = gimple_debug_source_bind_get_var (stmt);
5377 tree value = gimple_debug_source_bind_get_value (stmt);
5378 rtx val;
5379 machine_mode mode;
5381 last = get_last_insn ();
5383 set_curr_insn_location (gimple_location (stmt));
5385 mode = DECL_MODE (var);
5387 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
5388 VAR_INIT_STATUS_UNINITIALIZED);
5390 emit_debug_insn (val);
5392 if (dump_file && (dump_flags & TDF_DETAILS))
5394 /* We can't dump the insn with a TREE where an RTX
5395 is expected. */
5396 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5397 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5398 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5401 set_curr_insn_location (sloc);
5403 else
5405 gcall *call_stmt = dyn_cast <gcall *> (stmt);
5406 if (call_stmt
5407 && gimple_call_tail_p (call_stmt)
5408 && disable_tail_calls)
5409 gimple_call_set_tail (call_stmt, false);
5411 if (call_stmt && gimple_call_tail_p (call_stmt))
5413 bool can_fallthru;
5414 new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
5415 if (new_bb)
5417 if (can_fallthru)
5418 bb = new_bb;
5419 else
5420 return new_bb;
5423 else
5425 def_operand_p def_p;
5426 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
5428 if (def_p != NULL)
5430 /* Ignore this stmt if it is in the list of
5431 replaceable expressions. */
5432 if (SA.values
5433 && bitmap_bit_p (SA.values,
5434 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
5435 continue;
5437 last = expand_gimple_stmt (stmt);
5438 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5443 currently_expanding_gimple_stmt = NULL;
5445 /* Expand implicit goto and convert goto_locus. */
5446 FOR_EACH_EDGE (e, ei, bb->succs)
5448 if (e->goto_locus != UNKNOWN_LOCATION)
5449 set_curr_insn_location (e->goto_locus);
5450 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
5452 emit_jump (label_rtx_for_bb (e->dest));
5453 e->flags &= ~EDGE_FALLTHRU;
5457 /* Expanded RTL can create a jump in the last instruction of block.
5458 This later might be assumed to be a jump to successor and break edge insertion.
5459 We need to insert dummy move to prevent this. PR41440. */
5460 if (single_succ_p (bb)
5461 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
5462 && (last = get_last_insn ())
5463 && JUMP_P (last))
5465 rtx dummy = gen_reg_rtx (SImode);
5466 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
5469 do_pending_stack_adjust ();
5471 /* Find the block tail. The last insn in the block is the insn
5472 before a barrier and/or table jump insn. */
5473 last = get_last_insn ();
5474 if (BARRIER_P (last))
5475 last = PREV_INSN (last);
5476 if (JUMP_TABLE_DATA_P (last))
5477 last = PREV_INSN (PREV_INSN (last));
5478 BB_END (bb) = last;
5480 update_bb_for_insn (bb);
5482 return bb;
5486 /* Create a basic block for initialization code. */
5488 static basic_block
5489 construct_init_block (void)
5491 basic_block init_block, first_block;
5492 edge e = NULL;
5493 int flags;
5495 /* Multiple entry points not supported yet. */
5496 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
5497 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5498 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
5499 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5500 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5502 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
5504 /* When entry edge points to first basic block, we don't need jump,
5505 otherwise we have to jump into proper target. */
5506 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
5508 tree label = gimple_block_label (e->dest);
5510 emit_jump (jump_target_rtx (label));
5511 flags = 0;
5513 else
5514 flags = EDGE_FALLTHRU;
5516 init_block = create_basic_block (NEXT_INSN (get_insns ()),
5517 get_last_insn (),
5518 ENTRY_BLOCK_PTR_FOR_FN (cfun));
5519 init_block->frequency = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
5520 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5521 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5522 if (e)
5524 first_block = e->dest;
5525 redirect_edge_succ (e, init_block);
5526 e = make_edge (init_block, first_block, flags);
5528 else
5529 e = make_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5530 e->probability = REG_BR_PROB_BASE;
5531 e->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5533 update_bb_for_insn (init_block);
5534 return init_block;
5537 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
5538 found in the block tree. */
5540 static void
5541 set_block_levels (tree block, int level)
5543 while (block)
5545 BLOCK_NUMBER (block) = level;
5546 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
5547 block = BLOCK_CHAIN (block);
5551 /* Create a block containing landing pads and similar stuff. */
5553 static void
5554 construct_exit_block (void)
5556 rtx_insn *head = get_last_insn ();
5557 rtx_insn *end;
5558 basic_block exit_block;
5559 edge e, e2;
5560 unsigned ix;
5561 edge_iterator ei;
5562 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
5563 rtx_insn *orig_end = BB_END (prev_bb);
5565 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5567 /* Make sure the locus is set to the end of the function, so that
5568 epilogue line numbers and warnings are set properly. */
5569 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
5570 input_location = cfun->function_end_locus;
5572 /* Generate rtl for function exit. */
5573 expand_function_end ();
5575 end = get_last_insn ();
5576 if (head == end)
5577 return;
5578 /* While emitting the function end we could move end of the last basic
5579 block. */
5580 BB_END (prev_bb) = orig_end;
5581 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
5582 head = NEXT_INSN (head);
5583 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
5584 bb frequency counting will be confused. Any instructions before that
5585 label are emitted for the case where PREV_BB falls through into the
5586 exit block, so append those instructions to prev_bb in that case. */
5587 if (NEXT_INSN (head) != return_label)
5589 while (NEXT_INSN (head) != return_label)
5591 if (!NOTE_P (NEXT_INSN (head)))
5592 BB_END (prev_bb) = NEXT_INSN (head);
5593 head = NEXT_INSN (head);
5596 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
5597 exit_block->frequency = EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency;
5598 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5599 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5601 ix = 0;
5602 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
5604 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
5605 if (!(e->flags & EDGE_ABNORMAL))
5606 redirect_edge_succ (e, exit_block);
5607 else
5608 ix++;
5611 e = make_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5612 e->probability = REG_BR_PROB_BASE;
5613 e->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5614 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5615 if (e2 != e)
5617 e->count -= e2->count;
5618 exit_block->count -= e2->count;
5619 exit_block->frequency -= EDGE_FREQUENCY (e2);
5621 if (e->count < 0)
5622 e->count = 0;
5623 if (exit_block->count < 0)
5624 exit_block->count = 0;
5625 if (exit_block->frequency < 0)
5626 exit_block->frequency = 0;
5627 update_bb_for_insn (exit_block);
5630 /* Helper function for discover_nonconstant_array_refs.
5631 Look for ARRAY_REF nodes with non-constant indexes and mark them
5632 addressable. */
5634 static tree
5635 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
5636 void *data ATTRIBUTE_UNUSED)
5638 tree t = *tp;
5640 if (IS_TYPE_OR_DECL_P (t))
5641 *walk_subtrees = 0;
5642 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5644 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5645 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
5646 && (!TREE_OPERAND (t, 2)
5647 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5648 || (TREE_CODE (t) == COMPONENT_REF
5649 && (!TREE_OPERAND (t,2)
5650 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5651 || TREE_CODE (t) == BIT_FIELD_REF
5652 || TREE_CODE (t) == REALPART_EXPR
5653 || TREE_CODE (t) == IMAGPART_EXPR
5654 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5655 || CONVERT_EXPR_P (t))
5656 t = TREE_OPERAND (t, 0);
5658 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5660 t = get_base_address (t);
5661 if (t && DECL_P (t)
5662 && DECL_MODE (t) != BLKmode)
5663 TREE_ADDRESSABLE (t) = 1;
5666 *walk_subtrees = 0;
5669 return NULL_TREE;
5672 /* RTL expansion is not able to compile array references with variable
5673 offsets for arrays stored in single register. Discover such
5674 expressions and mark variables as addressable to avoid this
5675 scenario. */
5677 static void
5678 discover_nonconstant_array_refs (void)
5680 basic_block bb;
5681 gimple_stmt_iterator gsi;
5683 FOR_EACH_BB_FN (bb, cfun)
5684 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5686 gimple stmt = gsi_stmt (gsi);
5687 if (!is_gimple_debug (stmt))
5688 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
5692 /* This function sets crtl->args.internal_arg_pointer to a virtual
5693 register if DRAP is needed. Local register allocator will replace
5694 virtual_incoming_args_rtx with the virtual register. */
5696 static void
5697 expand_stack_alignment (void)
5699 rtx drap_rtx;
5700 unsigned int preferred_stack_boundary;
5702 if (! SUPPORTS_STACK_ALIGNMENT)
5703 return;
5705 if (cfun->calls_alloca
5706 || cfun->has_nonlocal_label
5707 || crtl->has_nonlocal_goto)
5708 crtl->need_drap = true;
5710 /* Call update_stack_boundary here again to update incoming stack
5711 boundary. It may set incoming stack alignment to a different
5712 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
5713 use the minimum incoming stack alignment to check if it is OK
5714 to perform sibcall optimization since sibcall optimization will
5715 only align the outgoing stack to incoming stack boundary. */
5716 if (targetm.calls.update_stack_boundary)
5717 targetm.calls.update_stack_boundary ();
5719 /* The incoming stack frame has to be aligned at least at
5720 parm_stack_boundary. */
5721 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
5723 /* Update crtl->stack_alignment_estimated and use it later to align
5724 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
5725 exceptions since callgraph doesn't collect incoming stack alignment
5726 in this case. */
5727 if (cfun->can_throw_non_call_exceptions
5728 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
5729 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
5730 else
5731 preferred_stack_boundary = crtl->preferred_stack_boundary;
5732 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
5733 crtl->stack_alignment_estimated = preferred_stack_boundary;
5734 if (preferred_stack_boundary > crtl->stack_alignment_needed)
5735 crtl->stack_alignment_needed = preferred_stack_boundary;
5737 gcc_assert (crtl->stack_alignment_needed
5738 <= crtl->stack_alignment_estimated);
5740 crtl->stack_realign_needed
5741 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
5742 crtl->stack_realign_tried = crtl->stack_realign_needed;
5744 crtl->stack_realign_processed = true;
5746 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
5747 alignment. */
5748 gcc_assert (targetm.calls.get_drap_rtx != NULL);
5749 drap_rtx = targetm.calls.get_drap_rtx ();
5751 /* stack_realign_drap and drap_rtx must match. */
5752 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
5754 /* Do nothing if NULL is returned, which means DRAP is not needed. */
5755 if (NULL != drap_rtx)
5757 crtl->args.internal_arg_pointer = drap_rtx;
5759 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
5760 needed. */
5761 fixup_tail_calls ();
5766 static void
5767 expand_main_function (void)
5769 #if (defined(INVOKE__main) \
5770 || (!defined(HAS_INIT_SECTION) \
5771 && !defined(INIT_SECTION_ASM_OP) \
5772 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
5773 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
5774 #endif
5778 /* Expand code to initialize the stack_protect_guard. This is invoked at
5779 the beginning of a function to be protected. */
5781 #ifndef HAVE_stack_protect_set
5782 # define HAVE_stack_protect_set 0
5783 # define gen_stack_protect_set(x,y) (gcc_unreachable (), NULL_RTX)
5784 #endif
5786 static void
5787 stack_protect_prologue (void)
5789 tree guard_decl = targetm.stack_protect_guard ();
5790 rtx x, y;
5792 x = expand_normal (crtl->stack_protect_guard);
5793 y = expand_normal (guard_decl);
5795 /* Allow the target to copy from Y to X without leaking Y into a
5796 register. */
5797 if (HAVE_stack_protect_set)
5799 rtx insn = gen_stack_protect_set (x, y);
5800 if (insn)
5802 emit_insn (insn);
5803 return;
5807 /* Otherwise do a straight move. */
5808 emit_move_insn (x, y);
5811 /* Translate the intermediate representation contained in the CFG
5812 from GIMPLE trees to RTL.
5814 We do conversion per basic block and preserve/update the tree CFG.
5815 This implies we have to do some magic as the CFG can simultaneously
5816 consist of basic blocks containing RTL and GIMPLE trees. This can
5817 confuse the CFG hooks, so be careful to not manipulate CFG during
5818 the expansion. */
5820 namespace {
5822 const pass_data pass_data_expand =
5824 RTL_PASS, /* type */
5825 "expand", /* name */
5826 OPTGROUP_NONE, /* optinfo_flags */
5827 TV_EXPAND, /* tv_id */
5828 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
5829 | PROP_gimple_lcx
5830 | PROP_gimple_lvec
5831 | PROP_gimple_lva), /* properties_required */
5832 PROP_rtl, /* properties_provided */
5833 ( PROP_ssa | PROP_trees ), /* properties_destroyed */
5834 0, /* todo_flags_start */
5835 0, /* todo_flags_finish */
5838 class pass_expand : public rtl_opt_pass
5840 public:
5841 pass_expand (gcc::context *ctxt)
5842 : rtl_opt_pass (pass_data_expand, ctxt)
5845 /* opt_pass methods: */
5846 virtual unsigned int execute (function *);
5848 }; // class pass_expand
5850 unsigned int
5851 pass_expand::execute (function *fun)
5853 basic_block bb, init_block;
5854 sbitmap blocks;
5855 edge_iterator ei;
5856 edge e;
5857 rtx_insn *var_seq, *var_ret_seq;
5858 unsigned i;
5860 timevar_push (TV_OUT_OF_SSA);
5861 rewrite_out_of_ssa (&SA);
5862 timevar_pop (TV_OUT_OF_SSA);
5863 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
5865 if (MAY_HAVE_DEBUG_STMTS && flag_tree_ter)
5867 gimple_stmt_iterator gsi;
5868 FOR_EACH_BB_FN (bb, cfun)
5869 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5870 if (gimple_debug_bind_p (gsi_stmt (gsi)))
5871 avoid_deep_ter_for_debug (gsi_stmt (gsi), 0);
5874 /* Make sure all values used by the optimization passes have sane
5875 defaults. */
5876 reg_renumber = 0;
5878 /* Some backends want to know that we are expanding to RTL. */
5879 currently_expanding_to_rtl = 1;
5880 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
5881 free_dominance_info (CDI_DOMINATORS);
5883 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
5885 if (chkp_function_instrumented_p (current_function_decl))
5886 chkp_reset_rtl_bounds ();
5888 insn_locations_init ();
5889 if (!DECL_IS_BUILTIN (current_function_decl))
5891 /* Eventually, all FEs should explicitly set function_start_locus. */
5892 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
5893 set_curr_insn_location
5894 (DECL_SOURCE_LOCATION (current_function_decl));
5895 else
5896 set_curr_insn_location (fun->function_start_locus);
5898 else
5899 set_curr_insn_location (UNKNOWN_LOCATION);
5900 prologue_location = curr_insn_location ();
5902 #ifdef INSN_SCHEDULING
5903 init_sched_attrs ();
5904 #endif
5906 /* Make sure first insn is a note even if we don't want linenums.
5907 This makes sure the first insn will never be deleted.
5908 Also, final expects a note to appear there. */
5909 emit_note (NOTE_INSN_DELETED);
5911 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
5912 discover_nonconstant_array_refs ();
5914 targetm.expand_to_rtl_hook ();
5915 crtl->stack_alignment_needed = STACK_BOUNDARY;
5916 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
5917 crtl->stack_alignment_estimated = 0;
5918 crtl->preferred_stack_boundary = STACK_BOUNDARY;
5919 fun->cfg->max_jumptable_ents = 0;
5921 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
5922 of the function section at exapnsion time to predict distance of calls. */
5923 resolve_unique_section (current_function_decl, 0, flag_function_sections);
5925 /* Expand the variables recorded during gimple lowering. */
5926 timevar_push (TV_VAR_EXPAND);
5927 start_sequence ();
5929 var_ret_seq = expand_used_vars ();
5931 var_seq = get_insns ();
5932 end_sequence ();
5933 timevar_pop (TV_VAR_EXPAND);
5935 /* Honor stack protection warnings. */
5936 if (warn_stack_protect)
5938 if (fun->calls_alloca)
5939 warning (OPT_Wstack_protector,
5940 "stack protector not protecting local variables: "
5941 "variable length buffer");
5942 if (has_short_buffer && !crtl->stack_protect_guard)
5943 warning (OPT_Wstack_protector,
5944 "stack protector not protecting function: "
5945 "all local arrays are less than %d bytes long",
5946 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
5949 /* Set up parameters and prepare for return, for the function. */
5950 expand_function_start (current_function_decl);
5952 /* If we emitted any instructions for setting up the variables,
5953 emit them before the FUNCTION_START note. */
5954 if (var_seq)
5956 emit_insn_before (var_seq, parm_birth_insn);
5958 /* In expand_function_end we'll insert the alloca save/restore
5959 before parm_birth_insn. We've just insertted an alloca call.
5960 Adjust the pointer to match. */
5961 parm_birth_insn = var_seq;
5964 /* Now that we also have the parameter RTXs, copy them over to our
5965 partitions. */
5966 for (i = 0; i < SA.map->num_partitions; i++)
5968 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
5970 if (TREE_CODE (var) != VAR_DECL
5971 && !SA.partition_to_pseudo[i])
5972 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
5973 gcc_assert (SA.partition_to_pseudo[i]);
5975 /* If this decl was marked as living in multiple places, reset
5976 this now to NULL. */
5977 if (DECL_RTL_IF_SET (var) == pc_rtx)
5978 SET_DECL_RTL (var, NULL);
5980 /* Some RTL parts really want to look at DECL_RTL(x) when x
5981 was a decl marked in REG_ATTR or MEM_ATTR. We could use
5982 SET_DECL_RTL here making this available, but that would mean
5983 to select one of the potentially many RTLs for one DECL. Instead
5984 of doing that we simply reset the MEM_EXPR of the RTL in question,
5985 then nobody can get at it and hence nobody can call DECL_RTL on it. */
5986 if (!DECL_RTL_SET_P (var))
5988 if (MEM_P (SA.partition_to_pseudo[i]))
5989 set_mem_expr (SA.partition_to_pseudo[i], NULL);
5993 /* If we have a class containing differently aligned pointers
5994 we need to merge those into the corresponding RTL pointer
5995 alignment. */
5996 for (i = 1; i < num_ssa_names; i++)
5998 tree name = ssa_name (i);
5999 int part;
6000 rtx r;
6002 if (!name
6003 /* We might have generated new SSA names in
6004 update_alias_info_with_stack_vars. They will have a NULL
6005 defining statements, and won't be part of the partitioning,
6006 so ignore those. */
6007 || !SSA_NAME_DEF_STMT (name))
6008 continue;
6009 part = var_to_partition (SA.map, name);
6010 if (part == NO_PARTITION)
6011 continue;
6013 /* Adjust all partition members to get the underlying decl of
6014 the representative which we might have created in expand_one_var. */
6015 if (SSA_NAME_VAR (name) == NULL_TREE)
6017 tree leader = partition_to_var (SA.map, part);
6018 gcc_assert (SSA_NAME_VAR (leader) != NULL_TREE);
6019 replace_ssa_name_symbol (name, SSA_NAME_VAR (leader));
6021 if (!POINTER_TYPE_P (TREE_TYPE (name)))
6022 continue;
6024 r = SA.partition_to_pseudo[part];
6025 if (REG_P (r))
6026 mark_reg_pointer (r, get_pointer_alignment (name));
6029 /* If this function is `main', emit a call to `__main'
6030 to run global initializers, etc. */
6031 if (DECL_NAME (current_function_decl)
6032 && MAIN_NAME_P (DECL_NAME (current_function_decl))
6033 && DECL_FILE_SCOPE_P (current_function_decl))
6034 expand_main_function ();
6036 /* Initialize the stack_protect_guard field. This must happen after the
6037 call to __main (if any) so that the external decl is initialized. */
6038 if (crtl->stack_protect_guard)
6039 stack_protect_prologue ();
6041 expand_phi_nodes (&SA);
6043 /* Register rtl specific functions for cfg. */
6044 rtl_register_cfg_hooks ();
6046 init_block = construct_init_block ();
6048 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
6049 remaining edges later. */
6050 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
6051 e->flags &= ~EDGE_EXECUTABLE;
6053 lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
6054 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
6055 next_bb)
6056 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
6058 if (MAY_HAVE_DEBUG_INSNS)
6059 expand_debug_locations ();
6061 if (deep_ter_debug_map)
6063 delete deep_ter_debug_map;
6064 deep_ter_debug_map = NULL;
6067 /* Free stuff we no longer need after GIMPLE optimizations. */
6068 free_dominance_info (CDI_DOMINATORS);
6069 free_dominance_info (CDI_POST_DOMINATORS);
6070 delete_tree_cfg_annotations ();
6072 timevar_push (TV_OUT_OF_SSA);
6073 finish_out_of_ssa (&SA);
6074 timevar_pop (TV_OUT_OF_SSA);
6076 timevar_push (TV_POST_EXPAND);
6077 /* We are no longer in SSA form. */
6078 fun->gimple_df->in_ssa_p = false;
6079 loops_state_clear (LOOP_CLOSED_SSA);
6081 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
6082 conservatively to true until they are all profile aware. */
6083 delete lab_rtx_for_bb;
6084 free_histograms ();
6086 construct_exit_block ();
6087 insn_locations_finalize ();
6089 if (var_ret_seq)
6091 rtx_insn *after = return_label;
6092 rtx_insn *next = NEXT_INSN (after);
6093 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
6094 after = next;
6095 emit_insn_after (var_ret_seq, after);
6098 /* Zap the tree EH table. */
6099 set_eh_throw_stmt_table (fun, NULL);
6101 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
6102 split edges which edge insertions might do. */
6103 rebuild_jump_labels (get_insns ());
6105 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun),
6106 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6108 edge e;
6109 edge_iterator ei;
6110 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6112 if (e->insns.r)
6114 rebuild_jump_labels_chain (e->insns.r);
6115 /* Put insns after parm birth, but before
6116 NOTE_INSNS_FUNCTION_BEG. */
6117 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (fun)
6118 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
6120 rtx_insn *insns = e->insns.r;
6121 e->insns.r = NULL;
6122 if (NOTE_P (parm_birth_insn)
6123 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
6124 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
6125 else
6126 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
6128 else
6129 commit_one_edge_insertion (e);
6131 else
6132 ei_next (&ei);
6136 /* We're done expanding trees to RTL. */
6137 currently_expanding_to_rtl = 0;
6139 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
6140 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6142 edge e;
6143 edge_iterator ei;
6144 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6146 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
6147 e->flags &= ~EDGE_EXECUTABLE;
6149 /* At the moment not all abnormal edges match the RTL
6150 representation. It is safe to remove them here as
6151 find_many_sub_basic_blocks will rediscover them.
6152 In the future we should get this fixed properly. */
6153 if ((e->flags & EDGE_ABNORMAL)
6154 && !(e->flags & EDGE_SIBCALL))
6155 remove_edge (e);
6156 else
6157 ei_next (&ei);
6161 blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
6162 bitmap_ones (blocks);
6163 find_many_sub_basic_blocks (blocks);
6164 sbitmap_free (blocks);
6165 purge_all_dead_edges ();
6167 expand_stack_alignment ();
6169 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
6170 function. */
6171 if (crtl->tail_call_emit)
6172 fixup_tail_calls ();
6174 /* After initial rtl generation, call back to finish generating
6175 exception support code. We need to do this before cleaning up
6176 the CFG as the code does not expect dead landing pads. */
6177 if (fun->eh->region_tree != NULL)
6178 finish_eh_generation ();
6180 /* Remove unreachable blocks, otherwise we cannot compute dominators
6181 which are needed for loop state verification. As a side-effect
6182 this also compacts blocks.
6183 ??? We cannot remove trivially dead insns here as for example
6184 the DRAP reg on i?86 is not magically live at this point.
6185 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
6186 cleanup_cfg (CLEANUP_NO_INSN_DEL);
6188 #ifdef ENABLE_CHECKING
6189 verify_flow_info ();
6190 #endif
6192 /* Initialize pseudos allocated for hard registers. */
6193 emit_initial_value_sets ();
6195 /* And finally unshare all RTL. */
6196 unshare_all_rtl ();
6198 /* There's no need to defer outputting this function any more; we
6199 know we want to output it. */
6200 DECL_DEFER_OUTPUT (current_function_decl) = 0;
6202 /* Now that we're done expanding trees to RTL, we shouldn't have any
6203 more CONCATs anywhere. */
6204 generating_concat_p = 0;
6206 if (dump_file)
6208 fprintf (dump_file,
6209 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
6210 /* And the pass manager will dump RTL for us. */
6213 /* If we're emitting a nested function, make sure its parent gets
6214 emitted as well. Doing otherwise confuses debug info. */
6216 tree parent;
6217 for (parent = DECL_CONTEXT (current_function_decl);
6218 parent != NULL_TREE;
6219 parent = get_containing_scope (parent))
6220 if (TREE_CODE (parent) == FUNCTION_DECL)
6221 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
6224 /* We are now committed to emitting code for this function. Do any
6225 preparation, such as emitting abstract debug info for the inline
6226 before it gets mangled by optimization. */
6227 if (cgraph_function_possibly_inlined_p (current_function_decl))
6228 (*debug_hooks->outlining_inline_function) (current_function_decl);
6230 TREE_ASM_WRITTEN (current_function_decl) = 1;
6232 /* After expanding, the return labels are no longer needed. */
6233 return_label = NULL;
6234 naked_return_label = NULL;
6236 /* After expanding, the tm_restart map is no longer needed. */
6237 if (fun->gimple_df->tm_restart)
6238 fun->gimple_df->tm_restart = NULL;
6240 /* Tag the blocks with a depth number so that change_scope can find
6241 the common parent easily. */
6242 set_block_levels (DECL_INITIAL (fun->decl), 0);
6243 default_rtl_profile ();
6245 timevar_pop (TV_POST_EXPAND);
6247 return 0;
6250 } // anon namespace
6252 rtl_opt_pass *
6253 make_pass_expand (gcc::context *ctxt)
6255 return new pass_expand (ctxt);