2015-06-23 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / cfgexpand.c
blob6b79b1dae77831930d12318f1b23e9c3b53d4a85
1 /* A pass for lowering trees to RTL.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "hard-reg-set.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "stringpool.h"
31 #include "varasm.h"
32 #include "stor-layout.h"
33 #include "stmt.h"
34 #include "print-tree.h"
35 #include "tm_p.h"
36 #include "predict.h"
37 #include "function.h"
38 #include "dominance.h"
39 #include "cfg.h"
40 #include "cfgrtl.h"
41 #include "cfganal.h"
42 #include "cfgbuild.h"
43 #include "cfgcleanup.h"
44 #include "basic-block.h"
45 #include "insn-codes.h"
46 #include "optabs.h"
47 #include "flags.h"
48 #include "insn-config.h"
49 #include "expmed.h"
50 #include "dojump.h"
51 #include "explow.h"
52 #include "calls.h"
53 #include "emit-rtl.h"
54 #include "expr.h"
55 #include "langhooks.h"
56 #include "bitmap.h"
57 #include "tree-ssa-alias.h"
58 #include "internal-fn.h"
59 #include "tree-eh.h"
60 #include "gimple-expr.h"
61 #include "gimple.h"
62 #include "gimple-iterator.h"
63 #include "gimple-walk.h"
64 #include "gimple-ssa.h"
65 #include "plugin-api.h"
66 #include "ipa-ref.h"
67 #include "cgraph.h"
68 #include "tree-cfg.h"
69 #include "tree-phinodes.h"
70 #include "ssa-iterators.h"
71 #include "tree-ssanames.h"
72 #include "tree-dfa.h"
73 #include "tree-ssa.h"
74 #include "tree-pass.h"
75 #include "except.h"
76 #include "diagnostic.h"
77 #include "gimple-pretty-print.h"
78 #include "toplev.h"
79 #include "debug.h"
80 #include "params.h"
81 #include "tree-inline.h"
82 #include "value-prof.h"
83 #include "target.h"
84 #include "tree-ssa-live.h"
85 #include "tree-outof-ssa.h"
86 #include "sbitmap.h"
87 #include "cfgloop.h"
88 #include "regs.h" /* For reg_renumber. */
89 #include "insn-attr.h" /* For INSN_SCHEDULING. */
90 #include "asan.h"
91 #include "tree-ssa-address.h"
92 #include "recog.h"
93 #include "output.h"
94 #include "builtins.h"
95 #include "tree-chkp.h"
96 #include "rtl-chkp.h"
98 /* Some systems use __main in a way incompatible with its use in gcc, in these
99 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
100 give the same symbol without quotes for an alternative entry point. You
101 must define both, or neither. */
102 #ifndef NAME__MAIN
103 #define NAME__MAIN "__main"
104 #endif
106 /* This variable holds information helping the rewriting of SSA trees
107 into RTL. */
108 struct ssaexpand SA;
110 /* This variable holds the currently expanded gimple statement for purposes
111 of comminucating the profile info to the builtin expanders. */
112 gimple currently_expanding_gimple_stmt;
114 static rtx expand_debug_expr (tree);
116 /* Return an expression tree corresponding to the RHS of GIMPLE
117 statement STMT. */
119 tree
120 gimple_assign_rhs_to_tree (gimple stmt)
122 tree t;
123 enum gimple_rhs_class grhs_class;
125 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
127 if (grhs_class == GIMPLE_TERNARY_RHS)
128 t = build3 (gimple_assign_rhs_code (stmt),
129 TREE_TYPE (gimple_assign_lhs (stmt)),
130 gimple_assign_rhs1 (stmt),
131 gimple_assign_rhs2 (stmt),
132 gimple_assign_rhs3 (stmt));
133 else if (grhs_class == GIMPLE_BINARY_RHS)
134 t = build2 (gimple_assign_rhs_code (stmt),
135 TREE_TYPE (gimple_assign_lhs (stmt)),
136 gimple_assign_rhs1 (stmt),
137 gimple_assign_rhs2 (stmt));
138 else if (grhs_class == GIMPLE_UNARY_RHS)
139 t = build1 (gimple_assign_rhs_code (stmt),
140 TREE_TYPE (gimple_assign_lhs (stmt)),
141 gimple_assign_rhs1 (stmt));
142 else if (grhs_class == GIMPLE_SINGLE_RHS)
144 t = gimple_assign_rhs1 (stmt);
145 /* Avoid modifying this tree in place below. */
146 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
147 && gimple_location (stmt) != EXPR_LOCATION (t))
148 || (gimple_block (stmt)
149 && currently_expanding_to_rtl
150 && EXPR_P (t)))
151 t = copy_node (t);
153 else
154 gcc_unreachable ();
156 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
157 SET_EXPR_LOCATION (t, gimple_location (stmt));
159 return t;
163 #ifndef STACK_ALIGNMENT_NEEDED
164 #define STACK_ALIGNMENT_NEEDED 1
165 #endif
167 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
169 /* Associate declaration T with storage space X. If T is no
170 SSA name this is exactly SET_DECL_RTL, otherwise make the
171 partition of T associated with X. */
172 static inline void
173 set_rtl (tree t, rtx x)
175 if (TREE_CODE (t) == SSA_NAME)
177 SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
178 if (x && !MEM_P (x))
179 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
180 /* For the benefit of debug information at -O0 (where vartracking
181 doesn't run) record the place also in the base DECL if it's
182 a normal variable (not a parameter). */
183 if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
185 tree var = SSA_NAME_VAR (t);
186 /* If we don't yet have something recorded, just record it now. */
187 if (!DECL_RTL_SET_P (var))
188 SET_DECL_RTL (var, x);
189 /* If we have it set already to "multiple places" don't
190 change this. */
191 else if (DECL_RTL (var) == pc_rtx)
193 /* If we have something recorded and it's not the same place
194 as we want to record now, we have multiple partitions for the
195 same base variable, with different places. We can't just
196 randomly chose one, hence we have to say that we don't know.
197 This only happens with optimization, and there var-tracking
198 will figure out the right thing. */
199 else if (DECL_RTL (var) != x)
200 SET_DECL_RTL (var, pc_rtx);
203 else
204 SET_DECL_RTL (t, x);
207 /* This structure holds data relevant to one variable that will be
208 placed in a stack slot. */
209 struct stack_var
211 /* The Variable. */
212 tree decl;
214 /* Initially, the size of the variable. Later, the size of the partition,
215 if this variable becomes it's partition's representative. */
216 HOST_WIDE_INT size;
218 /* The *byte* alignment required for this variable. Or as, with the
219 size, the alignment for this partition. */
220 unsigned int alignb;
222 /* The partition representative. */
223 size_t representative;
225 /* The next stack variable in the partition, or EOC. */
226 size_t next;
228 /* The numbers of conflicting stack variables. */
229 bitmap conflicts;
232 #define EOC ((size_t)-1)
234 /* We have an array of such objects while deciding allocation. */
235 static struct stack_var *stack_vars;
236 static size_t stack_vars_alloc;
237 static size_t stack_vars_num;
238 static hash_map<tree, size_t> *decl_to_stack_part;
240 /* Conflict bitmaps go on this obstack. This allows us to destroy
241 all of them in one big sweep. */
242 static bitmap_obstack stack_var_bitmap_obstack;
244 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
245 is non-decreasing. */
246 static size_t *stack_vars_sorted;
248 /* The phase of the stack frame. This is the known misalignment of
249 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
250 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
251 static int frame_phase;
253 /* Used during expand_used_vars to remember if we saw any decls for
254 which we'd like to enable stack smashing protection. */
255 static bool has_protected_decls;
257 /* Used during expand_used_vars. Remember if we say a character buffer
258 smaller than our cutoff threshold. Used for -Wstack-protector. */
259 static bool has_short_buffer;
261 /* Compute the byte alignment to use for DECL. Ignore alignment
262 we can't do with expected alignment of the stack boundary. */
264 static unsigned int
265 align_local_variable (tree decl)
267 unsigned int align = LOCAL_DECL_ALIGNMENT (decl);
268 DECL_ALIGN (decl) = align;
269 return align / BITS_PER_UNIT;
272 /* Align given offset BASE with ALIGN. Truncate up if ALIGN_UP is true,
273 down otherwise. Return truncated BASE value. */
275 static inline unsigned HOST_WIDE_INT
276 align_base (HOST_WIDE_INT base, unsigned HOST_WIDE_INT align, bool align_up)
278 return align_up ? (base + align - 1) & -align : base & -align;
281 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
282 Return the frame offset. */
284 static HOST_WIDE_INT
285 alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
287 HOST_WIDE_INT offset, new_frame_offset;
289 if (FRAME_GROWS_DOWNWARD)
291 new_frame_offset
292 = align_base (frame_offset - frame_phase - size,
293 align, false) + frame_phase;
294 offset = new_frame_offset;
296 else
298 new_frame_offset
299 = align_base (frame_offset - frame_phase, align, true) + frame_phase;
300 offset = new_frame_offset;
301 new_frame_offset += size;
303 frame_offset = new_frame_offset;
305 if (frame_offset_overflow (frame_offset, cfun->decl))
306 frame_offset = offset = 0;
308 return offset;
311 /* Accumulate DECL into STACK_VARS. */
313 static void
314 add_stack_var (tree decl)
316 struct stack_var *v;
318 if (stack_vars_num >= stack_vars_alloc)
320 if (stack_vars_alloc)
321 stack_vars_alloc = stack_vars_alloc * 3 / 2;
322 else
323 stack_vars_alloc = 32;
324 stack_vars
325 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
327 if (!decl_to_stack_part)
328 decl_to_stack_part = new hash_map<tree, size_t>;
330 v = &stack_vars[stack_vars_num];
331 decl_to_stack_part->put (decl, stack_vars_num);
333 v->decl = decl;
334 v->size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (decl)));
335 /* Ensure that all variables have size, so that &a != &b for any two
336 variables that are simultaneously live. */
337 if (v->size == 0)
338 v->size = 1;
339 v->alignb = align_local_variable (SSAVAR (decl));
340 /* An alignment of zero can mightily confuse us later. */
341 gcc_assert (v->alignb != 0);
343 /* All variables are initially in their own partition. */
344 v->representative = stack_vars_num;
345 v->next = EOC;
347 /* All variables initially conflict with no other. */
348 v->conflicts = NULL;
350 /* Ensure that this decl doesn't get put onto the list twice. */
351 set_rtl (decl, pc_rtx);
353 stack_vars_num++;
356 /* Make the decls associated with luid's X and Y conflict. */
358 static void
359 add_stack_var_conflict (size_t x, size_t y)
361 struct stack_var *a = &stack_vars[x];
362 struct stack_var *b = &stack_vars[y];
363 if (!a->conflicts)
364 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
365 if (!b->conflicts)
366 b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
367 bitmap_set_bit (a->conflicts, y);
368 bitmap_set_bit (b->conflicts, x);
371 /* Check whether the decls associated with luid's X and Y conflict. */
373 static bool
374 stack_var_conflict_p (size_t x, size_t y)
376 struct stack_var *a = &stack_vars[x];
377 struct stack_var *b = &stack_vars[y];
378 if (x == y)
379 return false;
380 /* Partitions containing an SSA name result from gimple registers
381 with things like unsupported modes. They are top-level and
382 hence conflict with everything else. */
383 if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
384 return true;
386 if (!a->conflicts || !b->conflicts)
387 return false;
388 return bitmap_bit_p (a->conflicts, y);
391 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
392 enter its partition number into bitmap DATA. */
394 static bool
395 visit_op (gimple, tree op, tree, void *data)
397 bitmap active = (bitmap)data;
398 op = get_base_address (op);
399 if (op
400 && DECL_P (op)
401 && DECL_RTL_IF_SET (op) == pc_rtx)
403 size_t *v = decl_to_stack_part->get (op);
404 if (v)
405 bitmap_set_bit (active, *v);
407 return false;
410 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
411 record conflicts between it and all currently active other partitions
412 from bitmap DATA. */
414 static bool
415 visit_conflict (gimple, tree op, tree, void *data)
417 bitmap active = (bitmap)data;
418 op = get_base_address (op);
419 if (op
420 && DECL_P (op)
421 && DECL_RTL_IF_SET (op) == pc_rtx)
423 size_t *v = decl_to_stack_part->get (op);
424 if (v && bitmap_set_bit (active, *v))
426 size_t num = *v;
427 bitmap_iterator bi;
428 unsigned i;
429 gcc_assert (num < stack_vars_num);
430 EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
431 add_stack_var_conflict (num, i);
434 return false;
437 /* Helper routine for add_scope_conflicts, calculating the active partitions
438 at the end of BB, leaving the result in WORK. We're called to generate
439 conflicts when FOR_CONFLICT is true, otherwise we're just tracking
440 liveness. */
442 static void
443 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
445 edge e;
446 edge_iterator ei;
447 gimple_stmt_iterator gsi;
448 walk_stmt_load_store_addr_fn visit;
450 bitmap_clear (work);
451 FOR_EACH_EDGE (e, ei, bb->preds)
452 bitmap_ior_into (work, (bitmap)e->src->aux);
454 visit = visit_op;
456 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
458 gimple stmt = gsi_stmt (gsi);
459 walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
461 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
463 gimple stmt = gsi_stmt (gsi);
465 if (gimple_clobber_p (stmt))
467 tree lhs = gimple_assign_lhs (stmt);
468 size_t *v;
469 /* Nested function lowering might introduce LHSs
470 that are COMPONENT_REFs. */
471 if (TREE_CODE (lhs) != VAR_DECL)
472 continue;
473 if (DECL_RTL_IF_SET (lhs) == pc_rtx
474 && (v = decl_to_stack_part->get (lhs)))
475 bitmap_clear_bit (work, *v);
477 else if (!is_gimple_debug (stmt))
479 if (for_conflict
480 && visit == visit_op)
482 /* If this is the first real instruction in this BB we need
483 to add conflicts for everything live at this point now.
484 Unlike classical liveness for named objects we can't
485 rely on seeing a def/use of the names we're interested in.
486 There might merely be indirect loads/stores. We'd not add any
487 conflicts for such partitions. */
488 bitmap_iterator bi;
489 unsigned i;
490 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
492 struct stack_var *a = &stack_vars[i];
493 if (!a->conflicts)
494 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
495 bitmap_ior_into (a->conflicts, work);
497 visit = visit_conflict;
499 walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
504 /* Generate stack partition conflicts between all partitions that are
505 simultaneously live. */
507 static void
508 add_scope_conflicts (void)
510 basic_block bb;
511 bool changed;
512 bitmap work = BITMAP_ALLOC (NULL);
513 int *rpo;
514 int n_bbs;
516 /* We approximate the live range of a stack variable by taking the first
517 mention of its name as starting point(s), and by the end-of-scope
518 death clobber added by gimplify as ending point(s) of the range.
519 This overapproximates in the case we for instance moved an address-taken
520 operation upward, without also moving a dereference to it upwards.
521 But it's conservatively correct as a variable never can hold values
522 before its name is mentioned at least once.
524 We then do a mostly classical bitmap liveness algorithm. */
526 FOR_ALL_BB_FN (bb, cfun)
527 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
529 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
530 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
532 changed = true;
533 while (changed)
535 int i;
536 changed = false;
537 for (i = 0; i < n_bbs; i++)
539 bitmap active;
540 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
541 active = (bitmap)bb->aux;
542 add_scope_conflicts_1 (bb, work, false);
543 if (bitmap_ior_into (active, work))
544 changed = true;
548 FOR_EACH_BB_FN (bb, cfun)
549 add_scope_conflicts_1 (bb, work, true);
551 free (rpo);
552 BITMAP_FREE (work);
553 FOR_ALL_BB_FN (bb, cfun)
554 BITMAP_FREE (bb->aux);
557 /* A subroutine of partition_stack_vars. A comparison function for qsort,
558 sorting an array of indices by the properties of the object. */
560 static int
561 stack_var_cmp (const void *a, const void *b)
563 size_t ia = *(const size_t *)a;
564 size_t ib = *(const size_t *)b;
565 unsigned int aligna = stack_vars[ia].alignb;
566 unsigned int alignb = stack_vars[ib].alignb;
567 HOST_WIDE_INT sizea = stack_vars[ia].size;
568 HOST_WIDE_INT sizeb = stack_vars[ib].size;
569 tree decla = stack_vars[ia].decl;
570 tree declb = stack_vars[ib].decl;
571 bool largea, largeb;
572 unsigned int uida, uidb;
574 /* Primary compare on "large" alignment. Large comes first. */
575 largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
576 largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
577 if (largea != largeb)
578 return (int)largeb - (int)largea;
580 /* Secondary compare on size, decreasing */
581 if (sizea > sizeb)
582 return -1;
583 if (sizea < sizeb)
584 return 1;
586 /* Tertiary compare on true alignment, decreasing. */
587 if (aligna < alignb)
588 return -1;
589 if (aligna > alignb)
590 return 1;
592 /* Final compare on ID for sort stability, increasing.
593 Two SSA names are compared by their version, SSA names come before
594 non-SSA names, and two normal decls are compared by their DECL_UID. */
595 if (TREE_CODE (decla) == SSA_NAME)
597 if (TREE_CODE (declb) == SSA_NAME)
598 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
599 else
600 return -1;
602 else if (TREE_CODE (declb) == SSA_NAME)
603 return 1;
604 else
605 uida = DECL_UID (decla), uidb = DECL_UID (declb);
606 if (uida < uidb)
607 return 1;
608 if (uida > uidb)
609 return -1;
610 return 0;
613 struct part_traits : default_hashmap_traits
615 template<typename T>
616 static bool
617 is_deleted (T &e)
618 { return e.m_value == reinterpret_cast<void *> (1); }
620 template<typename T> static bool is_empty (T &e) { return e.m_value == NULL; }
621 template<typename T>
622 static void
623 mark_deleted (T &e)
624 { e.m_value = reinterpret_cast<T> (1); }
626 template<typename T>
627 static void
628 mark_empty (T &e)
629 { e.m_value = NULL; }
632 typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
634 /* If the points-to solution *PI points to variables that are in a partition
635 together with other variables add all partition members to the pointed-to
636 variables bitmap. */
638 static void
639 add_partitioned_vars_to_ptset (struct pt_solution *pt,
640 part_hashmap *decls_to_partitions,
641 hash_set<bitmap> *visited, bitmap temp)
643 bitmap_iterator bi;
644 unsigned i;
645 bitmap *part;
647 if (pt->anything
648 || pt->vars == NULL
649 /* The pointed-to vars bitmap is shared, it is enough to
650 visit it once. */
651 || visited->add (pt->vars))
652 return;
654 bitmap_clear (temp);
656 /* By using a temporary bitmap to store all members of the partitions
657 we have to add we make sure to visit each of the partitions only
658 once. */
659 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
660 if ((!temp
661 || !bitmap_bit_p (temp, i))
662 && (part = decls_to_partitions->get (i)))
663 bitmap_ior_into (temp, *part);
664 if (!bitmap_empty_p (temp))
665 bitmap_ior_into (pt->vars, temp);
668 /* Update points-to sets based on partition info, so we can use them on RTL.
669 The bitmaps representing stack partitions will be saved until expand,
670 where partitioned decls used as bases in memory expressions will be
671 rewritten. */
673 static void
674 update_alias_info_with_stack_vars (void)
676 part_hashmap *decls_to_partitions = NULL;
677 size_t i, j;
678 tree var = NULL_TREE;
680 for (i = 0; i < stack_vars_num; i++)
682 bitmap part = NULL;
683 tree name;
684 struct ptr_info_def *pi;
686 /* Not interested in partitions with single variable. */
687 if (stack_vars[i].representative != i
688 || stack_vars[i].next == EOC)
689 continue;
691 if (!decls_to_partitions)
693 decls_to_partitions = new part_hashmap;
694 cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
697 /* Create an SSA_NAME that points to the partition for use
698 as base during alias-oracle queries on RTL for bases that
699 have been partitioned. */
700 if (var == NULL_TREE)
701 var = create_tmp_var (ptr_type_node);
702 name = make_ssa_name (var);
704 /* Create bitmaps representing partitions. They will be used for
705 points-to sets later, so use GGC alloc. */
706 part = BITMAP_GGC_ALLOC ();
707 for (j = i; j != EOC; j = stack_vars[j].next)
709 tree decl = stack_vars[j].decl;
710 unsigned int uid = DECL_PT_UID (decl);
711 bitmap_set_bit (part, uid);
712 decls_to_partitions->put (uid, part);
713 cfun->gimple_df->decls_to_pointers->put (decl, name);
714 if (TREE_ADDRESSABLE (decl))
715 TREE_ADDRESSABLE (name) = 1;
718 /* Make the SSA name point to all partition members. */
719 pi = get_ptr_info (name);
720 pt_solution_set (&pi->pt, part, false);
723 /* Make all points-to sets that contain one member of a partition
724 contain all members of the partition. */
725 if (decls_to_partitions)
727 unsigned i;
728 hash_set<bitmap> visited;
729 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
731 for (i = 1; i < num_ssa_names; i++)
733 tree name = ssa_name (i);
734 struct ptr_info_def *pi;
736 if (name
737 && POINTER_TYPE_P (TREE_TYPE (name))
738 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
739 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
740 &visited, temp);
743 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
744 decls_to_partitions, &visited, temp);
746 delete decls_to_partitions;
747 BITMAP_FREE (temp);
751 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
752 partitioning algorithm. Partitions A and B are known to be non-conflicting.
753 Merge them into a single partition A. */
755 static void
756 union_stack_vars (size_t a, size_t b)
758 struct stack_var *vb = &stack_vars[b];
759 bitmap_iterator bi;
760 unsigned u;
762 gcc_assert (stack_vars[b].next == EOC);
763 /* Add B to A's partition. */
764 stack_vars[b].next = stack_vars[a].next;
765 stack_vars[b].representative = a;
766 stack_vars[a].next = b;
768 /* Update the required alignment of partition A to account for B. */
769 if (stack_vars[a].alignb < stack_vars[b].alignb)
770 stack_vars[a].alignb = stack_vars[b].alignb;
772 /* Update the interference graph and merge the conflicts. */
773 if (vb->conflicts)
775 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
776 add_stack_var_conflict (a, stack_vars[u].representative);
777 BITMAP_FREE (vb->conflicts);
781 /* A subroutine of expand_used_vars. Binpack the variables into
782 partitions constrained by the interference graph. The overall
783 algorithm used is as follows:
785 Sort the objects by size in descending order.
786 For each object A {
787 S = size(A)
788 O = 0
789 loop {
790 Look for the largest non-conflicting object B with size <= S.
791 UNION (A, B)
796 static void
797 partition_stack_vars (void)
799 size_t si, sj, n = stack_vars_num;
801 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
802 for (si = 0; si < n; ++si)
803 stack_vars_sorted[si] = si;
805 if (n == 1)
806 return;
808 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
810 for (si = 0; si < n; ++si)
812 size_t i = stack_vars_sorted[si];
813 unsigned int ialign = stack_vars[i].alignb;
814 HOST_WIDE_INT isize = stack_vars[i].size;
816 /* Ignore objects that aren't partition representatives. If we
817 see a var that is not a partition representative, it must
818 have been merged earlier. */
819 if (stack_vars[i].representative != i)
820 continue;
822 for (sj = si + 1; sj < n; ++sj)
824 size_t j = stack_vars_sorted[sj];
825 unsigned int jalign = stack_vars[j].alignb;
826 HOST_WIDE_INT jsize = stack_vars[j].size;
828 /* Ignore objects that aren't partition representatives. */
829 if (stack_vars[j].representative != j)
830 continue;
832 /* Do not mix objects of "small" (supported) alignment
833 and "large" (unsupported) alignment. */
834 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
835 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
836 break;
838 /* For Address Sanitizer do not mix objects with different
839 sizes, as the shorter vars wouldn't be adequately protected.
840 Don't do that for "large" (unsupported) alignment objects,
841 those aren't protected anyway. */
842 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && isize != jsize
843 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
844 break;
846 /* Ignore conflicting objects. */
847 if (stack_var_conflict_p (i, j))
848 continue;
850 /* UNION the objects, placing J at OFFSET. */
851 union_stack_vars (i, j);
855 update_alias_info_with_stack_vars ();
858 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
860 static void
861 dump_stack_var_partition (void)
863 size_t si, i, j, n = stack_vars_num;
865 for (si = 0; si < n; ++si)
867 i = stack_vars_sorted[si];
869 /* Skip variables that aren't partition representatives, for now. */
870 if (stack_vars[i].representative != i)
871 continue;
873 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
874 " align %u\n", (unsigned long) i, stack_vars[i].size,
875 stack_vars[i].alignb);
877 for (j = i; j != EOC; j = stack_vars[j].next)
879 fputc ('\t', dump_file);
880 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
882 fputc ('\n', dump_file);
886 /* Assign rtl to DECL at BASE + OFFSET. */
888 static void
889 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
890 HOST_WIDE_INT offset)
892 unsigned align;
893 rtx x;
895 /* If this fails, we've overflowed the stack frame. Error nicely? */
896 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
898 x = plus_constant (Pmode, base, offset);
899 x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
901 if (TREE_CODE (decl) != SSA_NAME)
903 /* Set alignment we actually gave this decl if it isn't an SSA name.
904 If it is we generate stack slots only accidentally so it isn't as
905 important, we'll simply use the alignment that is already set. */
906 if (base == virtual_stack_vars_rtx)
907 offset -= frame_phase;
908 align = offset & -offset;
909 align *= BITS_PER_UNIT;
910 if (align == 0 || align > base_align)
911 align = base_align;
913 /* One would think that we could assert that we're not decreasing
914 alignment here, but (at least) the i386 port does exactly this
915 via the MINIMUM_ALIGNMENT hook. */
917 DECL_ALIGN (decl) = align;
918 DECL_USER_ALIGN (decl) = 0;
921 set_mem_attributes (x, SSAVAR (decl), true);
922 set_rtl (decl, x);
925 struct stack_vars_data
927 /* Vector of offset pairs, always end of some padding followed
928 by start of the padding that needs Address Sanitizer protection.
929 The vector is in reversed, highest offset pairs come first. */
930 vec<HOST_WIDE_INT> asan_vec;
932 /* Vector of partition representative decls in between the paddings. */
933 vec<tree> asan_decl_vec;
935 /* Base pseudo register for Address Sanitizer protected automatic vars. */
936 rtx asan_base;
938 /* Alignment needed for the Address Sanitizer protected automatic vars. */
939 unsigned int asan_alignb;
942 /* A subroutine of expand_used_vars. Give each partition representative
943 a unique location within the stack frame. Update each partition member
944 with that location. */
946 static void
947 expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
949 size_t si, i, j, n = stack_vars_num;
950 HOST_WIDE_INT large_size = 0, large_alloc = 0;
951 rtx large_base = NULL;
952 unsigned large_align = 0;
953 tree decl;
955 /* Determine if there are any variables requiring "large" alignment.
956 Since these are dynamically allocated, we only process these if
957 no predicate involved. */
958 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
959 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
961 /* Find the total size of these variables. */
962 for (si = 0; si < n; ++si)
964 unsigned alignb;
966 i = stack_vars_sorted[si];
967 alignb = stack_vars[i].alignb;
969 /* All "large" alignment decls come before all "small" alignment
970 decls, but "large" alignment decls are not sorted based on
971 their alignment. Increase large_align to track the largest
972 required alignment. */
973 if ((alignb * BITS_PER_UNIT) > large_align)
974 large_align = alignb * BITS_PER_UNIT;
976 /* Stop when we get to the first decl with "small" alignment. */
977 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
978 break;
980 /* Skip variables that aren't partition representatives. */
981 if (stack_vars[i].representative != i)
982 continue;
984 /* Skip variables that have already had rtl assigned. See also
985 add_stack_var where we perpetrate this pc_rtx hack. */
986 decl = stack_vars[i].decl;
987 if ((TREE_CODE (decl) == SSA_NAME
988 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
989 : DECL_RTL (decl)) != pc_rtx)
990 continue;
992 large_size += alignb - 1;
993 large_size &= -(HOST_WIDE_INT)alignb;
994 large_size += stack_vars[i].size;
997 /* If there were any, allocate space. */
998 if (large_size > 0)
999 large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
1000 large_align, true);
1003 for (si = 0; si < n; ++si)
1005 rtx base;
1006 unsigned base_align, alignb;
1007 HOST_WIDE_INT offset;
1009 i = stack_vars_sorted[si];
1011 /* Skip variables that aren't partition representatives, for now. */
1012 if (stack_vars[i].representative != i)
1013 continue;
1015 /* Skip variables that have already had rtl assigned. See also
1016 add_stack_var where we perpetrate this pc_rtx hack. */
1017 decl = stack_vars[i].decl;
1018 if ((TREE_CODE (decl) == SSA_NAME
1019 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
1020 : DECL_RTL (decl)) != pc_rtx)
1021 continue;
1023 /* Check the predicate to see whether this variable should be
1024 allocated in this pass. */
1025 if (pred && !pred (i))
1026 continue;
1028 alignb = stack_vars[i].alignb;
1029 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1031 base = virtual_stack_vars_rtx;
1032 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && pred)
1034 HOST_WIDE_INT prev_offset
1035 = align_base (frame_offset,
1036 MAX (alignb, ASAN_RED_ZONE_SIZE),
1037 FRAME_GROWS_DOWNWARD);
1038 tree repr_decl = NULL_TREE;
1039 offset
1040 = alloc_stack_frame_space (stack_vars[i].size
1041 + ASAN_RED_ZONE_SIZE,
1042 MAX (alignb, ASAN_RED_ZONE_SIZE));
1044 data->asan_vec.safe_push (prev_offset);
1045 data->asan_vec.safe_push (offset + stack_vars[i].size);
1046 /* Find best representative of the partition.
1047 Prefer those with DECL_NAME, even better
1048 satisfying asan_protect_stack_decl predicate. */
1049 for (j = i; j != EOC; j = stack_vars[j].next)
1050 if (asan_protect_stack_decl (stack_vars[j].decl)
1051 && DECL_NAME (stack_vars[j].decl))
1053 repr_decl = stack_vars[j].decl;
1054 break;
1056 else if (repr_decl == NULL_TREE
1057 && DECL_P (stack_vars[j].decl)
1058 && DECL_NAME (stack_vars[j].decl))
1059 repr_decl = stack_vars[j].decl;
1060 if (repr_decl == NULL_TREE)
1061 repr_decl = stack_vars[i].decl;
1062 data->asan_decl_vec.safe_push (repr_decl);
1063 data->asan_alignb = MAX (data->asan_alignb, alignb);
1064 if (data->asan_base == NULL)
1065 data->asan_base = gen_reg_rtx (Pmode);
1066 base = data->asan_base;
1068 if (!STRICT_ALIGNMENT)
1069 base_align = crtl->max_used_stack_slot_alignment;
1070 else
1071 base_align = MAX (crtl->max_used_stack_slot_alignment,
1072 GET_MODE_ALIGNMENT (SImode)
1073 << ASAN_SHADOW_SHIFT);
1075 else
1077 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1078 base_align = crtl->max_used_stack_slot_alignment;
1081 else
1083 /* Large alignment is only processed in the last pass. */
1084 if (pred)
1085 continue;
1086 gcc_assert (large_base != NULL);
1088 large_alloc += alignb - 1;
1089 large_alloc &= -(HOST_WIDE_INT)alignb;
1090 offset = large_alloc;
1091 large_alloc += stack_vars[i].size;
1093 base = large_base;
1094 base_align = large_align;
1097 /* Create rtl for each variable based on their location within the
1098 partition. */
1099 for (j = i; j != EOC; j = stack_vars[j].next)
1101 expand_one_stack_var_at (stack_vars[j].decl,
1102 base, base_align,
1103 offset);
1107 gcc_assert (large_alloc == large_size);
1110 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1111 static HOST_WIDE_INT
1112 account_stack_vars (void)
1114 size_t si, j, i, n = stack_vars_num;
1115 HOST_WIDE_INT size = 0;
1117 for (si = 0; si < n; ++si)
1119 i = stack_vars_sorted[si];
1121 /* Skip variables that aren't partition representatives, for now. */
1122 if (stack_vars[i].representative != i)
1123 continue;
1125 size += stack_vars[i].size;
1126 for (j = i; j != EOC; j = stack_vars[j].next)
1127 set_rtl (stack_vars[j].decl, NULL);
1129 return size;
1132 /* A subroutine of expand_one_var. Called to immediately assign rtl
1133 to a variable to be allocated in the stack frame. */
1135 static void
1136 expand_one_stack_var (tree var)
1138 HOST_WIDE_INT size, offset;
1139 unsigned byte_align;
1141 size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (var)));
1142 byte_align = align_local_variable (SSAVAR (var));
1144 /* We handle highly aligned variables in expand_stack_vars. */
1145 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1147 offset = alloc_stack_frame_space (size, byte_align);
1149 expand_one_stack_var_at (var, virtual_stack_vars_rtx,
1150 crtl->max_used_stack_slot_alignment, offset);
1153 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1154 that will reside in a hard register. */
1156 static void
1157 expand_one_hard_reg_var (tree var)
1159 rest_of_decl_compilation (var, 0, 0);
1162 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1163 that will reside in a pseudo register. */
1165 static void
1166 expand_one_register_var (tree var)
1168 tree decl = SSAVAR (var);
1169 tree type = TREE_TYPE (decl);
1170 machine_mode reg_mode = promote_decl_mode (decl, NULL);
1171 rtx x = gen_reg_rtx (reg_mode);
1173 set_rtl (var, x);
1175 /* Note if the object is a user variable. */
1176 if (!DECL_ARTIFICIAL (decl))
1177 mark_user_reg (x);
1179 if (POINTER_TYPE_P (type))
1180 mark_reg_pointer (x, get_pointer_alignment (var));
1183 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1184 has some associated error, e.g. its type is error-mark. We just need
1185 to pick something that won't crash the rest of the compiler. */
1187 static void
1188 expand_one_error_var (tree var)
1190 machine_mode mode = DECL_MODE (var);
1191 rtx x;
1193 if (mode == BLKmode)
1194 x = gen_rtx_MEM (BLKmode, const0_rtx);
1195 else if (mode == VOIDmode)
1196 x = const0_rtx;
1197 else
1198 x = gen_reg_rtx (mode);
1200 SET_DECL_RTL (var, x);
1203 /* A subroutine of expand_one_var. VAR is a variable that will be
1204 allocated to the local stack frame. Return true if we wish to
1205 add VAR to STACK_VARS so that it will be coalesced with other
1206 variables. Return false to allocate VAR immediately.
1208 This function is used to reduce the number of variables considered
1209 for coalescing, which reduces the size of the quadratic problem. */
1211 static bool
1212 defer_stack_allocation (tree var, bool toplevel)
1214 /* Whether the variable is small enough for immediate allocation not to be
1215 a problem with regard to the frame size. */
1216 bool smallish
1217 = ((HOST_WIDE_INT) tree_to_uhwi (DECL_SIZE_UNIT (var))
1218 < PARAM_VALUE (PARAM_MIN_SIZE_FOR_STACK_SHARING));
1220 /* If stack protection is enabled, *all* stack variables must be deferred,
1221 so that we can re-order the strings to the top of the frame.
1222 Similarly for Address Sanitizer. */
1223 if (flag_stack_protect || ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK))
1224 return true;
1226 /* We handle "large" alignment via dynamic allocation. We want to handle
1227 this extra complication in only one place, so defer them. */
1228 if (DECL_ALIGN (var) > MAX_SUPPORTED_STACK_ALIGNMENT)
1229 return true;
1231 /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1232 might be detached from their block and appear at toplevel when we reach
1233 here. We want to coalesce them with variables from other blocks when
1234 the immediate contribution to the frame size would be noticeable. */
1235 if (toplevel && optimize > 0 && DECL_IGNORED_P (var) && !smallish)
1236 return true;
1238 /* Variables declared in the outermost scope automatically conflict
1239 with every other variable. The only reason to want to defer them
1240 at all is that, after sorting, we can more efficiently pack
1241 small variables in the stack frame. Continue to defer at -O2. */
1242 if (toplevel && optimize < 2)
1243 return false;
1245 /* Without optimization, *most* variables are allocated from the
1246 stack, which makes the quadratic problem large exactly when we
1247 want compilation to proceed as quickly as possible. On the
1248 other hand, we don't want the function's stack frame size to
1249 get completely out of hand. So we avoid adding scalars and
1250 "small" aggregates to the list at all. */
1251 if (optimize == 0 && smallish)
1252 return false;
1254 return true;
1257 /* A subroutine of expand_used_vars. Expand one variable according to
1258 its flavor. Variables to be placed on the stack are not actually
1259 expanded yet, merely recorded.
1260 When REALLY_EXPAND is false, only add stack values to be allocated.
1261 Return stack usage this variable is supposed to take.
1264 static HOST_WIDE_INT
1265 expand_one_var (tree var, bool toplevel, bool really_expand)
1267 unsigned int align = BITS_PER_UNIT;
1268 tree origvar = var;
1270 var = SSAVAR (var);
1272 if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
1274 /* Because we don't know if VAR will be in register or on stack,
1275 we conservatively assume it will be on stack even if VAR is
1276 eventually put into register after RA pass. For non-automatic
1277 variables, which won't be on stack, we collect alignment of
1278 type and ignore user specified alignment. Similarly for
1279 SSA_NAMEs for which use_register_for_decl returns true. */
1280 if (TREE_STATIC (var)
1281 || DECL_EXTERNAL (var)
1282 || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1283 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1284 TYPE_MODE (TREE_TYPE (var)),
1285 TYPE_ALIGN (TREE_TYPE (var)));
1286 else if (DECL_HAS_VALUE_EXPR_P (var)
1287 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1288 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1289 or variables which were assigned a stack slot already by
1290 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1291 changed from the offset chosen to it. */
1292 align = crtl->stack_alignment_estimated;
1293 else
1294 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1296 /* If the variable alignment is very large we'll dynamicaly allocate
1297 it, which means that in-frame portion is just a pointer. */
1298 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1299 align = POINTER_SIZE;
1302 if (SUPPORTS_STACK_ALIGNMENT
1303 && crtl->stack_alignment_estimated < align)
1305 /* stack_alignment_estimated shouldn't change after stack
1306 realign decision made */
1307 gcc_assert (!crtl->stack_realign_processed);
1308 crtl->stack_alignment_estimated = align;
1311 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1312 So here we only make sure stack_alignment_needed >= align. */
1313 if (crtl->stack_alignment_needed < align)
1314 crtl->stack_alignment_needed = align;
1315 if (crtl->max_used_stack_slot_alignment < align)
1316 crtl->max_used_stack_slot_alignment = align;
1318 if (TREE_CODE (origvar) == SSA_NAME)
1320 gcc_assert (TREE_CODE (var) != VAR_DECL
1321 || (!DECL_EXTERNAL (var)
1322 && !DECL_HAS_VALUE_EXPR_P (var)
1323 && !TREE_STATIC (var)
1324 && TREE_TYPE (var) != error_mark_node
1325 && !DECL_HARD_REGISTER (var)
1326 && really_expand));
1328 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1330 else if (DECL_EXTERNAL (var))
1332 else if (DECL_HAS_VALUE_EXPR_P (var))
1334 else if (TREE_STATIC (var))
1336 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1338 else if (TREE_TYPE (var) == error_mark_node)
1340 if (really_expand)
1341 expand_one_error_var (var);
1343 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1345 if (really_expand)
1347 expand_one_hard_reg_var (var);
1348 if (!DECL_HARD_REGISTER (var))
1349 /* Invalid register specification. */
1350 expand_one_error_var (var);
1353 else if (use_register_for_decl (var))
1355 if (really_expand)
1356 expand_one_register_var (origvar);
1358 else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
1360 /* Reject variables which cover more than half of the address-space. */
1361 if (really_expand)
1363 error ("size of variable %q+D is too large", var);
1364 expand_one_error_var (var);
1367 else if (defer_stack_allocation (var, toplevel))
1368 add_stack_var (origvar);
1369 else
1371 if (really_expand)
1372 expand_one_stack_var (origvar);
1373 return tree_to_uhwi (DECL_SIZE_UNIT (var));
1375 return 0;
1378 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1379 expanding variables. Those variables that can be put into registers
1380 are allocated pseudos; those that can't are put on the stack.
1382 TOPLEVEL is true if this is the outermost BLOCK. */
1384 static void
1385 expand_used_vars_for_block (tree block, bool toplevel)
1387 tree t;
1389 /* Expand all variables at this level. */
1390 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1391 if (TREE_USED (t)
1392 && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1393 || !DECL_NONSHAREABLE (t)))
1394 expand_one_var (t, toplevel, true);
1396 /* Expand all variables at containing levels. */
1397 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1398 expand_used_vars_for_block (t, false);
1401 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1402 and clear TREE_USED on all local variables. */
1404 static void
1405 clear_tree_used (tree block)
1407 tree t;
1409 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1410 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1411 if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1412 || !DECL_NONSHAREABLE (t))
1413 TREE_USED (t) = 0;
1415 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1416 clear_tree_used (t);
1419 enum {
1420 SPCT_FLAG_DEFAULT = 1,
1421 SPCT_FLAG_ALL = 2,
1422 SPCT_FLAG_STRONG = 3,
1423 SPCT_FLAG_EXPLICIT = 4
1426 /* Examine TYPE and determine a bit mask of the following features. */
1428 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1429 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1430 #define SPCT_HAS_ARRAY 4
1431 #define SPCT_HAS_AGGREGATE 8
1433 static unsigned int
1434 stack_protect_classify_type (tree type)
1436 unsigned int ret = 0;
1437 tree t;
1439 switch (TREE_CODE (type))
1441 case ARRAY_TYPE:
1442 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1443 if (t == char_type_node
1444 || t == signed_char_type_node
1445 || t == unsigned_char_type_node)
1447 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1448 unsigned HOST_WIDE_INT len;
1450 if (!TYPE_SIZE_UNIT (type)
1451 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1452 len = max;
1453 else
1454 len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1456 if (len < max)
1457 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1458 else
1459 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1461 else
1462 ret = SPCT_HAS_ARRAY;
1463 break;
1465 case UNION_TYPE:
1466 case QUAL_UNION_TYPE:
1467 case RECORD_TYPE:
1468 ret = SPCT_HAS_AGGREGATE;
1469 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1470 if (TREE_CODE (t) == FIELD_DECL)
1471 ret |= stack_protect_classify_type (TREE_TYPE (t));
1472 break;
1474 default:
1475 break;
1478 return ret;
1481 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1482 part of the local stack frame. Remember if we ever return nonzero for
1483 any variable in this function. The return value is the phase number in
1484 which the variable should be allocated. */
1486 static int
1487 stack_protect_decl_phase (tree decl)
1489 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1490 int ret = 0;
1492 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1493 has_short_buffer = true;
1495 if (flag_stack_protect == SPCT_FLAG_ALL
1496 || flag_stack_protect == SPCT_FLAG_STRONG
1497 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1498 && lookup_attribute ("stack_protect",
1499 DECL_ATTRIBUTES (current_function_decl))))
1501 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1502 && !(bits & SPCT_HAS_AGGREGATE))
1503 ret = 1;
1504 else if (bits & SPCT_HAS_ARRAY)
1505 ret = 2;
1507 else
1508 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1510 if (ret)
1511 has_protected_decls = true;
1513 return ret;
1516 /* Two helper routines that check for phase 1 and phase 2. These are used
1517 as callbacks for expand_stack_vars. */
1519 static bool
1520 stack_protect_decl_phase_1 (size_t i)
1522 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
1525 static bool
1526 stack_protect_decl_phase_2 (size_t i)
1528 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
1531 /* And helper function that checks for asan phase (with stack protector
1532 it is phase 3). This is used as callback for expand_stack_vars.
1533 Returns true if any of the vars in the partition need to be protected. */
1535 static bool
1536 asan_decl_phase_3 (size_t i)
1538 while (i != EOC)
1540 if (asan_protect_stack_decl (stack_vars[i].decl))
1541 return true;
1542 i = stack_vars[i].next;
1544 return false;
1547 /* Ensure that variables in different stack protection phases conflict
1548 so that they are not merged and share the same stack slot. */
1550 static void
1551 add_stack_protection_conflicts (void)
1553 size_t i, j, n = stack_vars_num;
1554 unsigned char *phase;
1556 phase = XNEWVEC (unsigned char, n);
1557 for (i = 0; i < n; ++i)
1558 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1560 for (i = 0; i < n; ++i)
1562 unsigned char ph_i = phase[i];
1563 for (j = i + 1; j < n; ++j)
1564 if (ph_i != phase[j])
1565 add_stack_var_conflict (i, j);
1568 XDELETEVEC (phase);
1571 /* Create a decl for the guard at the top of the stack frame. */
1573 static void
1574 create_stack_guard (void)
1576 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1577 VAR_DECL, NULL, ptr_type_node);
1578 TREE_THIS_VOLATILE (guard) = 1;
1579 TREE_USED (guard) = 1;
1580 expand_one_stack_var (guard);
1581 crtl->stack_protect_guard = guard;
1584 /* Prepare for expanding variables. */
1585 static void
1586 init_vars_expansion (void)
1588 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
1589 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
1591 /* A map from decl to stack partition. */
1592 decl_to_stack_part = new hash_map<tree, size_t>;
1594 /* Initialize local stack smashing state. */
1595 has_protected_decls = false;
1596 has_short_buffer = false;
1599 /* Free up stack variable graph data. */
1600 static void
1601 fini_vars_expansion (void)
1603 bitmap_obstack_release (&stack_var_bitmap_obstack);
1604 if (stack_vars)
1605 XDELETEVEC (stack_vars);
1606 if (stack_vars_sorted)
1607 XDELETEVEC (stack_vars_sorted);
1608 stack_vars = NULL;
1609 stack_vars_sorted = NULL;
1610 stack_vars_alloc = stack_vars_num = 0;
1611 delete decl_to_stack_part;
1612 decl_to_stack_part = NULL;
1615 /* Make a fair guess for the size of the stack frame of the function
1616 in NODE. This doesn't have to be exact, the result is only used in
1617 the inline heuristics. So we don't want to run the full stack var
1618 packing algorithm (which is quadratic in the number of stack vars).
1619 Instead, we calculate the total size of all stack vars. This turns
1620 out to be a pretty fair estimate -- packing of stack vars doesn't
1621 happen very often. */
1623 HOST_WIDE_INT
1624 estimated_stack_frame_size (struct cgraph_node *node)
1626 HOST_WIDE_INT size = 0;
1627 size_t i;
1628 tree var;
1629 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1631 push_cfun (fn);
1633 init_vars_expansion ();
1635 FOR_EACH_LOCAL_DECL (fn, i, var)
1636 if (auto_var_in_fn_p (var, fn->decl))
1637 size += expand_one_var (var, true, false);
1639 if (stack_vars_num > 0)
1641 /* Fake sorting the stack vars for account_stack_vars (). */
1642 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1643 for (i = 0; i < stack_vars_num; ++i)
1644 stack_vars_sorted[i] = i;
1645 size += account_stack_vars ();
1648 fini_vars_expansion ();
1649 pop_cfun ();
1650 return size;
1653 /* Helper routine to check if a record or union contains an array field. */
1655 static int
1656 record_or_union_type_has_array_p (const_tree tree_type)
1658 tree fields = TYPE_FIELDS (tree_type);
1659 tree f;
1661 for (f = fields; f; f = DECL_CHAIN (f))
1662 if (TREE_CODE (f) == FIELD_DECL)
1664 tree field_type = TREE_TYPE (f);
1665 if (RECORD_OR_UNION_TYPE_P (field_type)
1666 && record_or_union_type_has_array_p (field_type))
1667 return 1;
1668 if (TREE_CODE (field_type) == ARRAY_TYPE)
1669 return 1;
1671 return 0;
1674 /* Check if the current function has local referenced variables that
1675 have their addresses taken, contain an array, or are arrays. */
1677 static bool
1678 stack_protect_decl_p ()
1680 unsigned i;
1681 tree var;
1683 FOR_EACH_LOCAL_DECL (cfun, i, var)
1684 if (!is_global_var (var))
1686 tree var_type = TREE_TYPE (var);
1687 if (TREE_CODE (var) == VAR_DECL
1688 && (TREE_CODE (var_type) == ARRAY_TYPE
1689 || TREE_ADDRESSABLE (var)
1690 || (RECORD_OR_UNION_TYPE_P (var_type)
1691 && record_or_union_type_has_array_p (var_type))))
1692 return true;
1694 return false;
1697 /* Check if the current function has calls that use a return slot. */
1699 static bool
1700 stack_protect_return_slot_p ()
1702 basic_block bb;
1704 FOR_ALL_BB_FN (bb, cfun)
1705 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1706 !gsi_end_p (gsi); gsi_next (&gsi))
1708 gimple stmt = gsi_stmt (gsi);
1709 /* This assumes that calls to internal-only functions never
1710 use a return slot. */
1711 if (is_gimple_call (stmt)
1712 && !gimple_call_internal_p (stmt)
1713 && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
1714 gimple_call_fndecl (stmt)))
1715 return true;
1717 return false;
1720 /* Expand all variables used in the function. */
1722 static rtx_insn *
1723 expand_used_vars (void)
1725 tree var, outer_block = DECL_INITIAL (current_function_decl);
1726 vec<tree> maybe_local_decls = vNULL;
1727 rtx_insn *var_end_seq = NULL;
1728 unsigned i;
1729 unsigned len;
1730 bool gen_stack_protect_signal = false;
1732 /* Compute the phase of the stack frame for this function. */
1734 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1735 int off = STARTING_FRAME_OFFSET % align;
1736 frame_phase = off ? align - off : 0;
1739 /* Set TREE_USED on all variables in the local_decls. */
1740 FOR_EACH_LOCAL_DECL (cfun, i, var)
1741 TREE_USED (var) = 1;
1742 /* Clear TREE_USED on all variables associated with a block scope. */
1743 clear_tree_used (DECL_INITIAL (current_function_decl));
1745 init_vars_expansion ();
1747 if (targetm.use_pseudo_pic_reg ())
1748 pic_offset_table_rtx = gen_reg_rtx (Pmode);
1750 hash_map<tree, tree> ssa_name_decls;
1751 for (i = 0; i < SA.map->num_partitions; i++)
1753 tree var = partition_to_var (SA.map, i);
1755 gcc_assert (!virtual_operand_p (var));
1757 /* Assign decls to each SSA name partition, share decls for partitions
1758 we could have coalesced (those with the same type). */
1759 if (SSA_NAME_VAR (var) == NULL_TREE)
1761 tree *slot = &ssa_name_decls.get_or_insert (TREE_TYPE (var));
1762 if (!*slot)
1763 *slot = create_tmp_reg (TREE_TYPE (var));
1764 replace_ssa_name_symbol (var, *slot);
1767 /* Always allocate space for partitions based on VAR_DECLs. But for
1768 those based on PARM_DECLs or RESULT_DECLs and which matter for the
1769 debug info, there is no need to do so if optimization is disabled
1770 because all the SSA_NAMEs based on these DECLs have been coalesced
1771 into a single partition, which is thus assigned the canonical RTL
1772 location of the DECLs. If in_lto_p, we can't rely on optimize,
1773 a function could be compiled with -O1 -flto first and only the
1774 link performed at -O0. */
1775 if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1776 expand_one_var (var, true, true);
1777 else if (DECL_IGNORED_P (SSA_NAME_VAR (var)) || optimize || in_lto_p)
1779 /* This is a PARM_DECL or RESULT_DECL. For those partitions that
1780 contain the default def (representing the parm or result itself)
1781 we don't do anything here. But those which don't contain the
1782 default def (representing a temporary based on the parm/result)
1783 we need to allocate space just like for normal VAR_DECLs. */
1784 if (!bitmap_bit_p (SA.partition_has_default_def, i))
1786 expand_one_var (var, true, true);
1787 gcc_assert (SA.partition_to_pseudo[i]);
1792 if (flag_stack_protect == SPCT_FLAG_STRONG)
1793 gen_stack_protect_signal
1794 = stack_protect_decl_p () || stack_protect_return_slot_p ();
1796 /* At this point all variables on the local_decls with TREE_USED
1797 set are not associated with any block scope. Lay them out. */
1799 len = vec_safe_length (cfun->local_decls);
1800 FOR_EACH_LOCAL_DECL (cfun, i, var)
1802 bool expand_now = false;
1804 /* Expanded above already. */
1805 if (is_gimple_reg (var))
1807 TREE_USED (var) = 0;
1808 goto next;
1810 /* We didn't set a block for static or extern because it's hard
1811 to tell the difference between a global variable (re)declared
1812 in a local scope, and one that's really declared there to
1813 begin with. And it doesn't really matter much, since we're
1814 not giving them stack space. Expand them now. */
1815 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1816 expand_now = true;
1818 /* Expand variables not associated with any block now. Those created by
1819 the optimizers could be live anywhere in the function. Those that
1820 could possibly have been scoped originally and detached from their
1821 block will have their allocation deferred so we coalesce them with
1822 others when optimization is enabled. */
1823 else if (TREE_USED (var))
1824 expand_now = true;
1826 /* Finally, mark all variables on the list as used. We'll use
1827 this in a moment when we expand those associated with scopes. */
1828 TREE_USED (var) = 1;
1830 if (expand_now)
1831 expand_one_var (var, true, true);
1833 next:
1834 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1836 rtx rtl = DECL_RTL_IF_SET (var);
1838 /* Keep artificial non-ignored vars in cfun->local_decls
1839 chain until instantiate_decls. */
1840 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1841 add_local_decl (cfun, var);
1842 else if (rtl == NULL_RTX)
1843 /* If rtl isn't set yet, which can happen e.g. with
1844 -fstack-protector, retry before returning from this
1845 function. */
1846 maybe_local_decls.safe_push (var);
1850 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
1852 +-----------------+-----------------+
1853 | ...processed... | ...duplicates...|
1854 +-----------------+-----------------+
1856 +-- LEN points here.
1858 We just want the duplicates, as those are the artificial
1859 non-ignored vars that we want to keep until instantiate_decls.
1860 Move them down and truncate the array. */
1861 if (!vec_safe_is_empty (cfun->local_decls))
1862 cfun->local_decls->block_remove (0, len);
1864 /* At this point, all variables within the block tree with TREE_USED
1865 set are actually used by the optimized function. Lay them out. */
1866 expand_used_vars_for_block (outer_block, true);
1868 if (stack_vars_num > 0)
1870 add_scope_conflicts ();
1872 /* If stack protection is enabled, we don't share space between
1873 vulnerable data and non-vulnerable data. */
1874 if (flag_stack_protect != 0
1875 && (flag_stack_protect != SPCT_FLAG_EXPLICIT
1876 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1877 && lookup_attribute ("stack_protect",
1878 DECL_ATTRIBUTES (current_function_decl)))))
1879 add_stack_protection_conflicts ();
1881 /* Now that we have collected all stack variables, and have computed a
1882 minimal interference graph, attempt to save some stack space. */
1883 partition_stack_vars ();
1884 if (dump_file)
1885 dump_stack_var_partition ();
1888 switch (flag_stack_protect)
1890 case SPCT_FLAG_ALL:
1891 create_stack_guard ();
1892 break;
1894 case SPCT_FLAG_STRONG:
1895 if (gen_stack_protect_signal
1896 || cfun->calls_alloca || has_protected_decls
1897 || lookup_attribute ("stack_protect",
1898 DECL_ATTRIBUTES (current_function_decl)))
1899 create_stack_guard ();
1900 break;
1902 case SPCT_FLAG_DEFAULT:
1903 if (cfun->calls_alloca || has_protected_decls
1904 || lookup_attribute ("stack_protect",
1905 DECL_ATTRIBUTES (current_function_decl)))
1906 create_stack_guard ();
1907 break;
1909 case SPCT_FLAG_EXPLICIT:
1910 if (lookup_attribute ("stack_protect",
1911 DECL_ATTRIBUTES (current_function_decl)))
1912 create_stack_guard ();
1913 break;
1914 default:
1918 /* Assign rtl to each variable based on these partitions. */
1919 if (stack_vars_num > 0)
1921 struct stack_vars_data data;
1923 data.asan_vec = vNULL;
1924 data.asan_decl_vec = vNULL;
1925 data.asan_base = NULL_RTX;
1926 data.asan_alignb = 0;
1928 /* Reorder decls to be protected by iterating over the variables
1929 array multiple times, and allocating out of each phase in turn. */
1930 /* ??? We could probably integrate this into the qsort we did
1931 earlier, such that we naturally see these variables first,
1932 and thus naturally allocate things in the right order. */
1933 if (has_protected_decls)
1935 /* Phase 1 contains only character arrays. */
1936 expand_stack_vars (stack_protect_decl_phase_1, &data);
1938 /* Phase 2 contains other kinds of arrays. */
1939 if (flag_stack_protect == SPCT_FLAG_ALL
1940 || flag_stack_protect == SPCT_FLAG_STRONG
1941 || (flag_stack_protect == SPCT_FLAG_EXPLICIT
1942 && lookup_attribute ("stack_protect",
1943 DECL_ATTRIBUTES (current_function_decl))))
1944 expand_stack_vars (stack_protect_decl_phase_2, &data);
1947 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK)
1948 /* Phase 3, any partitions that need asan protection
1949 in addition to phase 1 and 2. */
1950 expand_stack_vars (asan_decl_phase_3, &data);
1952 if (!data.asan_vec.is_empty ())
1954 HOST_WIDE_INT prev_offset = frame_offset;
1955 HOST_WIDE_INT offset, sz, redzonesz;
1956 redzonesz = ASAN_RED_ZONE_SIZE;
1957 sz = data.asan_vec[0] - prev_offset;
1958 if (data.asan_alignb > ASAN_RED_ZONE_SIZE
1959 && data.asan_alignb <= 4096
1960 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
1961 redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
1962 & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
1963 offset
1964 = alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE);
1965 data.asan_vec.safe_push (prev_offset);
1966 data.asan_vec.safe_push (offset);
1967 /* Leave space for alignment if STRICT_ALIGNMENT. */
1968 if (STRICT_ALIGNMENT)
1969 alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
1970 << ASAN_SHADOW_SHIFT)
1971 / BITS_PER_UNIT, 1);
1973 var_end_seq
1974 = asan_emit_stack_protection (virtual_stack_vars_rtx,
1975 data.asan_base,
1976 data.asan_alignb,
1977 data.asan_vec.address (),
1978 data.asan_decl_vec.address (),
1979 data.asan_vec.length ());
1982 expand_stack_vars (NULL, &data);
1984 data.asan_vec.release ();
1985 data.asan_decl_vec.release ();
1988 fini_vars_expansion ();
1990 /* If there were any artificial non-ignored vars without rtl
1991 found earlier, see if deferred stack allocation hasn't assigned
1992 rtl to them. */
1993 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
1995 rtx rtl = DECL_RTL_IF_SET (var);
1997 /* Keep artificial non-ignored vars in cfun->local_decls
1998 chain until instantiate_decls. */
1999 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
2000 add_local_decl (cfun, var);
2002 maybe_local_decls.release ();
2004 /* If the target requires that FRAME_OFFSET be aligned, do it. */
2005 if (STACK_ALIGNMENT_NEEDED)
2007 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
2008 if (!FRAME_GROWS_DOWNWARD)
2009 frame_offset += align - 1;
2010 frame_offset &= -align;
2013 return var_end_seq;
2017 /* If we need to produce a detailed dump, print the tree representation
2018 for STMT to the dump file. SINCE is the last RTX after which the RTL
2019 generated for STMT should have been appended. */
2021 static void
2022 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx_insn *since)
2024 if (dump_file && (dump_flags & TDF_DETAILS))
2026 fprintf (dump_file, "\n;; ");
2027 print_gimple_stmt (dump_file, stmt, 0,
2028 TDF_SLIM | (dump_flags & TDF_LINENO));
2029 fprintf (dump_file, "\n");
2031 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
2035 /* Maps the blocks that do not contain tree labels to rtx labels. */
2037 static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
2039 /* Returns the label_rtx expression for a label starting basic block BB. */
2041 static rtx_code_label *
2042 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
2044 gimple_stmt_iterator gsi;
2045 tree lab;
2047 if (bb->flags & BB_RTL)
2048 return block_label (bb);
2050 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
2051 if (elt)
2052 return *elt;
2054 /* Find the tree label if it is present. */
2056 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2058 glabel *lab_stmt;
2060 lab_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
2061 if (!lab_stmt)
2062 break;
2064 lab = gimple_label_label (lab_stmt);
2065 if (DECL_NONLOCAL (lab))
2066 break;
2068 return jump_target_rtx (lab);
2071 rtx_code_label *l = gen_label_rtx ();
2072 lab_rtx_for_bb->put (bb, l);
2073 return l;
2077 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2078 of a basic block where we just expanded the conditional at the end,
2079 possibly clean up the CFG and instruction sequence. LAST is the
2080 last instruction before the just emitted jump sequence. */
2082 static void
2083 maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2085 /* Special case: when jumpif decides that the condition is
2086 trivial it emits an unconditional jump (and the necessary
2087 barrier). But we still have two edges, the fallthru one is
2088 wrong. purge_dead_edges would clean this up later. Unfortunately
2089 we have to insert insns (and split edges) before
2090 find_many_sub_basic_blocks and hence before purge_dead_edges.
2091 But splitting edges might create new blocks which depend on the
2092 fact that if there are two edges there's no barrier. So the
2093 barrier would get lost and verify_flow_info would ICE. Instead
2094 of auditing all edge splitters to care for the barrier (which
2095 normally isn't there in a cleaned CFG), fix it here. */
2096 if (BARRIER_P (get_last_insn ()))
2098 rtx_insn *insn;
2099 remove_edge (e);
2100 /* Now, we have a single successor block, if we have insns to
2101 insert on the remaining edge we potentially will insert
2102 it at the end of this block (if the dest block isn't feasible)
2103 in order to avoid splitting the edge. This insertion will take
2104 place in front of the last jump. But we might have emitted
2105 multiple jumps (conditional and one unconditional) to the
2106 same destination. Inserting in front of the last one then
2107 is a problem. See PR 40021. We fix this by deleting all
2108 jumps except the last unconditional one. */
2109 insn = PREV_INSN (get_last_insn ());
2110 /* Make sure we have an unconditional jump. Otherwise we're
2111 confused. */
2112 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2113 for (insn = PREV_INSN (insn); insn != last;)
2115 insn = PREV_INSN (insn);
2116 if (JUMP_P (NEXT_INSN (insn)))
2118 if (!any_condjump_p (NEXT_INSN (insn)))
2120 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2121 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2123 delete_insn (NEXT_INSN (insn));
2129 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2130 Returns a new basic block if we've terminated the current basic
2131 block and created a new one. */
2133 static basic_block
2134 expand_gimple_cond (basic_block bb, gcond *stmt)
2136 basic_block new_bb, dest;
2137 edge new_edge;
2138 edge true_edge;
2139 edge false_edge;
2140 rtx_insn *last2, *last;
2141 enum tree_code code;
2142 tree op0, op1;
2144 code = gimple_cond_code (stmt);
2145 op0 = gimple_cond_lhs (stmt);
2146 op1 = gimple_cond_rhs (stmt);
2147 /* We're sometimes presented with such code:
2148 D.123_1 = x < y;
2149 if (D.123_1 != 0)
2151 This would expand to two comparisons which then later might
2152 be cleaned up by combine. But some pattern matchers like if-conversion
2153 work better when there's only one compare, so make up for this
2154 here as special exception if TER would have made the same change. */
2155 if (SA.values
2156 && TREE_CODE (op0) == SSA_NAME
2157 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2158 && TREE_CODE (op1) == INTEGER_CST
2159 && ((gimple_cond_code (stmt) == NE_EXPR
2160 && integer_zerop (op1))
2161 || (gimple_cond_code (stmt) == EQ_EXPR
2162 && integer_onep (op1)))
2163 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2165 gimple second = SSA_NAME_DEF_STMT (op0);
2166 if (gimple_code (second) == GIMPLE_ASSIGN)
2168 enum tree_code code2 = gimple_assign_rhs_code (second);
2169 if (TREE_CODE_CLASS (code2) == tcc_comparison)
2171 code = code2;
2172 op0 = gimple_assign_rhs1 (second);
2173 op1 = gimple_assign_rhs2 (second);
2175 /* If jumps are cheap and the target does not support conditional
2176 compare, turn some more codes into jumpy sequences. */
2177 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4
2178 && targetm.gen_ccmp_first == NULL)
2180 if ((code2 == BIT_AND_EXPR
2181 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2182 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2183 || code2 == TRUTH_AND_EXPR)
2185 code = TRUTH_ANDIF_EXPR;
2186 op0 = gimple_assign_rhs1 (second);
2187 op1 = gimple_assign_rhs2 (second);
2189 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2191 code = TRUTH_ORIF_EXPR;
2192 op0 = gimple_assign_rhs1 (second);
2193 op1 = gimple_assign_rhs2 (second);
2199 last2 = last = get_last_insn ();
2201 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2202 set_curr_insn_location (gimple_location (stmt));
2204 /* These flags have no purpose in RTL land. */
2205 true_edge->flags &= ~EDGE_TRUE_VALUE;
2206 false_edge->flags &= ~EDGE_FALSE_VALUE;
2208 /* We can either have a pure conditional jump with one fallthru edge or
2209 two-way jump that needs to be decomposed into two basic blocks. */
2210 if (false_edge->dest == bb->next_bb)
2212 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2213 true_edge->probability);
2214 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2215 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2216 set_curr_insn_location (true_edge->goto_locus);
2217 false_edge->flags |= EDGE_FALLTHRU;
2218 maybe_cleanup_end_of_block (false_edge, last);
2219 return NULL;
2221 if (true_edge->dest == bb->next_bb)
2223 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2224 false_edge->probability);
2225 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2226 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2227 set_curr_insn_location (false_edge->goto_locus);
2228 true_edge->flags |= EDGE_FALLTHRU;
2229 maybe_cleanup_end_of_block (true_edge, last);
2230 return NULL;
2233 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2234 true_edge->probability);
2235 last = get_last_insn ();
2236 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2237 set_curr_insn_location (false_edge->goto_locus);
2238 emit_jump (label_rtx_for_bb (false_edge->dest));
2240 BB_END (bb) = last;
2241 if (BARRIER_P (BB_END (bb)))
2242 BB_END (bb) = PREV_INSN (BB_END (bb));
2243 update_bb_for_insn (bb);
2245 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2246 dest = false_edge->dest;
2247 redirect_edge_succ (false_edge, new_bb);
2248 false_edge->flags |= EDGE_FALLTHRU;
2249 new_bb->count = false_edge->count;
2250 new_bb->frequency = EDGE_FREQUENCY (false_edge);
2251 add_bb_to_loop (new_bb, bb->loop_father);
2252 new_edge = make_edge (new_bb, dest, 0);
2253 new_edge->probability = REG_BR_PROB_BASE;
2254 new_edge->count = new_bb->count;
2255 if (BARRIER_P (BB_END (new_bb)))
2256 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2257 update_bb_for_insn (new_bb);
2259 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2261 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2263 set_curr_insn_location (true_edge->goto_locus);
2264 true_edge->goto_locus = curr_insn_location ();
2267 return new_bb;
2270 /* Mark all calls that can have a transaction restart. */
2272 static void
2273 mark_transaction_restart_calls (gimple stmt)
2275 struct tm_restart_node dummy;
2276 tm_restart_node **slot;
2278 if (!cfun->gimple_df->tm_restart)
2279 return;
2281 dummy.stmt = stmt;
2282 slot = cfun->gimple_df->tm_restart->find_slot (&dummy, NO_INSERT);
2283 if (slot)
2285 struct tm_restart_node *n = *slot;
2286 tree list = n->label_or_list;
2287 rtx_insn *insn;
2289 for (insn = next_real_insn (get_last_insn ());
2290 !CALL_P (insn);
2291 insn = next_real_insn (insn))
2292 continue;
2294 if (TREE_CODE (list) == LABEL_DECL)
2295 add_reg_note (insn, REG_TM, label_rtx (list));
2296 else
2297 for (; list ; list = TREE_CHAIN (list))
2298 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2302 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2303 statement STMT. */
2305 static void
2306 expand_call_stmt (gcall *stmt)
2308 tree exp, decl, lhs;
2309 bool builtin_p;
2310 size_t i;
2312 if (gimple_call_internal_p (stmt))
2314 expand_internal_call (stmt);
2315 return;
2318 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2320 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2321 decl = gimple_call_fndecl (stmt);
2322 builtin_p = decl && DECL_BUILT_IN (decl);
2324 /* If this is not a builtin function, the function type through which the
2325 call is made may be different from the type of the function. */
2326 if (!builtin_p)
2327 CALL_EXPR_FN (exp)
2328 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2329 CALL_EXPR_FN (exp));
2331 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2332 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2334 for (i = 0; i < gimple_call_num_args (stmt); i++)
2336 tree arg = gimple_call_arg (stmt, i);
2337 gimple def;
2338 /* TER addresses into arguments of builtin functions so we have a
2339 chance to infer more correct alignment information. See PR39954. */
2340 if (builtin_p
2341 && TREE_CODE (arg) == SSA_NAME
2342 && (def = get_gimple_for_ssa_name (arg))
2343 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2344 arg = gimple_assign_rhs1 (def);
2345 CALL_EXPR_ARG (exp, i) = arg;
2348 if (gimple_has_side_effects (stmt))
2349 TREE_SIDE_EFFECTS (exp) = 1;
2351 if (gimple_call_nothrow_p (stmt))
2352 TREE_NOTHROW (exp) = 1;
2354 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2355 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2356 if (decl
2357 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2358 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2359 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2360 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2361 else
2362 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2363 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2364 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2365 CALL_WITH_BOUNDS_P (exp) = gimple_call_with_bounds_p (stmt);
2367 /* Ensure RTL is created for debug args. */
2368 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2370 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2371 unsigned int ix;
2372 tree dtemp;
2374 if (debug_args)
2375 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2377 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2378 expand_debug_expr (dtemp);
2382 lhs = gimple_call_lhs (stmt);
2383 if (lhs)
2384 expand_assignment (lhs, exp, false);
2385 else
2386 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2388 mark_transaction_restart_calls (stmt);
2392 /* Generate RTL for an asm statement (explicit assembler code).
2393 STRING is a STRING_CST node containing the assembler code text,
2394 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
2395 insn is volatile; don't optimize it. */
2397 static void
2398 expand_asm_loc (tree string, int vol, location_t locus)
2400 rtx body;
2402 if (TREE_CODE (string) == ADDR_EXPR)
2403 string = TREE_OPERAND (string, 0);
2405 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2406 ggc_strdup (TREE_STRING_POINTER (string)),
2407 locus);
2409 MEM_VOLATILE_P (body) = vol;
2411 emit_insn (body);
2414 /* Return the number of times character C occurs in string S. */
2415 static int
2416 n_occurrences (int c, const char *s)
2418 int n = 0;
2419 while (*s)
2420 n += (*s++ == c);
2421 return n;
2424 /* A subroutine of expand_asm_operands. Check that all operands have
2425 the same number of alternatives. Return true if so. */
2427 static bool
2428 check_operand_nalternatives (const vec<const char *> &constraints)
2430 unsigned len = constraints.length();
2431 if (len > 0)
2433 int nalternatives = n_occurrences (',', constraints[0]);
2435 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2437 error ("too many alternatives in %<asm%>");
2438 return false;
2441 for (unsigned i = 1; i < len; ++i)
2442 if (n_occurrences (',', constraints[i]) != nalternatives)
2444 error ("operand constraints for %<asm%> differ "
2445 "in number of alternatives");
2446 return false;
2449 return true;
2452 /* Check for overlap between registers marked in CLOBBERED_REGS and
2453 anything inappropriate in T. Emit error and return the register
2454 variable definition for error, NULL_TREE for ok. */
2456 static bool
2457 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
2459 /* Conflicts between asm-declared register variables and the clobber
2460 list are not allowed. */
2461 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2463 if (overlap)
2465 error ("asm-specifier for variable %qE conflicts with asm clobber list",
2466 DECL_NAME (overlap));
2468 /* Reset registerness to stop multiple errors emitted for a single
2469 variable. */
2470 DECL_REGISTER (overlap) = 0;
2471 return true;
2474 return false;
2477 /* Generate RTL for an asm statement with arguments.
2478 STRING is the instruction template.
2479 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
2480 Each output or input has an expression in the TREE_VALUE and
2481 a tree list in TREE_PURPOSE which in turn contains a constraint
2482 name in TREE_VALUE (or NULL_TREE) and a constraint string
2483 in TREE_PURPOSE.
2484 CLOBBERS is a list of STRING_CST nodes each naming a hard register
2485 that is clobbered by this insn.
2487 LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
2488 should be the fallthru basic block of the asm goto.
2490 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
2491 Some elements of OUTPUTS may be replaced with trees representing temporary
2492 values. The caller should copy those temporary values to the originally
2493 specified lvalues.
2495 VOL nonzero means the insn is volatile; don't optimize it. */
2497 static void
2498 expand_asm_stmt (gasm *stmt)
2500 class save_input_location
2502 location_t old;
2504 public:
2505 explicit save_input_location(location_t where)
2507 old = input_location;
2508 input_location = where;
2511 ~save_input_location()
2513 input_location = old;
2517 location_t locus = gimple_location (stmt);
2519 if (gimple_asm_input_p (stmt))
2521 const char *s = gimple_asm_string (stmt);
2522 tree string = build_string (strlen (s), s);
2523 expand_asm_loc (string, gimple_asm_volatile_p (stmt), locus);
2524 return;
2527 /* There are some legacy diagnostics in here, and also avoids a
2528 sixth parameger to targetm.md_asm_adjust. */
2529 save_input_location s_i_l(locus);
2531 unsigned noutputs = gimple_asm_noutputs (stmt);
2532 unsigned ninputs = gimple_asm_ninputs (stmt);
2533 unsigned nlabels = gimple_asm_nlabels (stmt);
2534 unsigned i;
2536 /* ??? Diagnose during gimplification? */
2537 if (ninputs + noutputs + nlabels > MAX_RECOG_OPERANDS)
2539 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
2540 return;
2543 auto_vec<tree, MAX_RECOG_OPERANDS> output_tvec;
2544 auto_vec<tree, MAX_RECOG_OPERANDS> input_tvec;
2545 auto_vec<const char *, MAX_RECOG_OPERANDS> constraints;
2547 /* Copy the gimple vectors into new vectors that we can manipulate. */
2549 output_tvec.safe_grow (noutputs);
2550 input_tvec.safe_grow (ninputs);
2551 constraints.safe_grow (noutputs + ninputs);
2553 for (i = 0; i < noutputs; ++i)
2555 tree t = gimple_asm_output_op (stmt, i);
2556 output_tvec[i] = TREE_VALUE (t);
2557 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2559 for (i = 0; i < ninputs; i++)
2561 tree t = gimple_asm_input_op (stmt, i);
2562 input_tvec[i] = TREE_VALUE (t);
2563 constraints[i + noutputs]
2564 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2567 /* ??? Diagnose during gimplification? */
2568 if (! check_operand_nalternatives (constraints))
2569 return;
2571 /* Count the number of meaningful clobbered registers, ignoring what
2572 we would ignore later. */
2573 auto_vec<rtx> clobber_rvec;
2574 HARD_REG_SET clobbered_regs;
2575 CLEAR_HARD_REG_SET (clobbered_regs);
2577 if (unsigned n = gimple_asm_nclobbers (stmt))
2579 clobber_rvec.reserve (n);
2580 for (i = 0; i < n; i++)
2582 tree t = gimple_asm_clobber_op (stmt, i);
2583 const char *regname = TREE_STRING_POINTER (TREE_VALUE (t));
2584 int nregs, j;
2586 j = decode_reg_name_and_count (regname, &nregs);
2587 if (j < 0)
2589 if (j == -2)
2591 /* ??? Diagnose during gimplification? */
2592 error ("unknown register name %qs in %<asm%>", regname);
2594 else if (j == -4)
2596 rtx x = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2597 clobber_rvec.safe_push (x);
2599 else
2601 /* Otherwise we should have -1 == empty string
2602 or -3 == cc, which is not a register. */
2603 gcc_assert (j == -1 || j == -3);
2606 else
2607 for (int reg = j; reg < j + nregs; reg++)
2609 /* Clobbering the PIC register is an error. */
2610 if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
2612 /* ??? Diagnose during gimplification? */
2613 error ("PIC register clobbered by %qs in %<asm%>",
2614 regname);
2615 return;
2618 SET_HARD_REG_BIT (clobbered_regs, reg);
2619 rtx x = gen_rtx_REG (reg_raw_mode[reg], reg);
2620 clobber_rvec.safe_push (x);
2624 unsigned nclobbers = clobber_rvec.length();
2626 /* First pass over inputs and outputs checks validity and sets
2627 mark_addressable if needed. */
2628 /* ??? Diagnose during gimplification? */
2630 for (i = 0; i < noutputs; ++i)
2632 tree val = output_tvec[i];
2633 tree type = TREE_TYPE (val);
2634 const char *constraint;
2635 bool is_inout;
2636 bool allows_reg;
2637 bool allows_mem;
2639 /* Try to parse the output constraint. If that fails, there's
2640 no point in going further. */
2641 constraint = constraints[i];
2642 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
2643 &allows_mem, &allows_reg, &is_inout))
2644 return;
2646 if (! allows_reg
2647 && (allows_mem
2648 || is_inout
2649 || (DECL_P (val)
2650 && REG_P (DECL_RTL (val))
2651 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
2652 mark_addressable (val);
2655 for (i = 0; i < ninputs; ++i)
2657 bool allows_reg, allows_mem;
2658 const char *constraint;
2660 constraint = constraints[i + noutputs];
2661 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2662 constraints.address (),
2663 &allows_mem, &allows_reg))
2664 return;
2666 if (! allows_reg && allows_mem)
2667 mark_addressable (input_tvec[i]);
2670 /* Second pass evaluates arguments. */
2672 /* Make sure stack is consistent for asm goto. */
2673 if (nlabels > 0)
2674 do_pending_stack_adjust ();
2675 int old_generating_concat_p = generating_concat_p;
2677 /* Vector of RTX's of evaluated output operands. */
2678 auto_vec<rtx, MAX_RECOG_OPERANDS> output_rvec;
2679 auto_vec<int, MAX_RECOG_OPERANDS> inout_opnum;
2680 rtx_insn *after_rtl_seq = NULL, *after_rtl_end = NULL;
2682 output_rvec.safe_grow (noutputs);
2684 for (i = 0; i < noutputs; ++i)
2686 tree val = output_tvec[i];
2687 tree type = TREE_TYPE (val);
2688 bool is_inout, allows_reg, allows_mem, ok;
2689 rtx op;
2691 ok = parse_output_constraint (&constraints[i], i, ninputs,
2692 noutputs, &allows_mem, &allows_reg,
2693 &is_inout);
2694 gcc_assert (ok);
2696 /* If an output operand is not a decl or indirect ref and our constraint
2697 allows a register, make a temporary to act as an intermediate.
2698 Make the asm insn write into that, then we will copy it to
2699 the real output operand. Likewise for promoted variables. */
2701 generating_concat_p = 0;
2703 if ((TREE_CODE (val) == INDIRECT_REF
2704 && allows_mem)
2705 || (DECL_P (val)
2706 && (allows_mem || REG_P (DECL_RTL (val)))
2707 && ! (REG_P (DECL_RTL (val))
2708 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
2709 || ! allows_reg
2710 || is_inout)
2712 op = expand_expr (val, NULL_RTX, VOIDmode,
2713 !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
2714 if (MEM_P (op))
2715 op = validize_mem (op);
2717 if (! allows_reg && !MEM_P (op))
2718 error ("output number %d not directly addressable", i);
2719 if ((! allows_mem && MEM_P (op))
2720 || GET_CODE (op) == CONCAT)
2722 rtx old_op = op;
2723 op = gen_reg_rtx (GET_MODE (op));
2725 generating_concat_p = old_generating_concat_p;
2727 if (is_inout)
2728 emit_move_insn (op, old_op);
2730 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2731 emit_move_insn (old_op, op);
2732 after_rtl_seq = get_insns ();
2733 after_rtl_end = get_last_insn ();
2734 end_sequence ();
2737 else
2739 op = assign_temp (type, 0, 1);
2740 op = validize_mem (op);
2741 if (!MEM_P (op) && TREE_CODE (val) == SSA_NAME)
2742 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (val), op);
2744 generating_concat_p = old_generating_concat_p;
2746 push_to_sequence2 (after_rtl_seq, after_rtl_end);
2747 expand_assignment (val, make_tree (type, op), false);
2748 after_rtl_seq = get_insns ();
2749 after_rtl_end = get_last_insn ();
2750 end_sequence ();
2752 output_rvec[i] = op;
2754 if (is_inout)
2755 inout_opnum.safe_push (i);
2758 auto_vec<rtx, MAX_RECOG_OPERANDS> input_rvec;
2759 auto_vec<machine_mode, MAX_RECOG_OPERANDS> input_mode;
2761 input_rvec.safe_grow (ninputs);
2762 input_mode.safe_grow (ninputs);
2764 generating_concat_p = 0;
2766 for (i = 0; i < ninputs; ++i)
2768 tree val = input_tvec[i];
2769 tree type = TREE_TYPE (val);
2770 bool allows_reg, allows_mem, ok;
2771 const char *constraint;
2772 rtx op;
2774 constraint = constraints[i + noutputs];
2775 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2776 constraints.address (),
2777 &allows_mem, &allows_reg);
2778 gcc_assert (ok);
2780 /* EXPAND_INITIALIZER will not generate code for valid initializer
2781 constants, but will still generate code for other types of operand.
2782 This is the behavior we want for constant constraints. */
2783 op = expand_expr (val, NULL_RTX, VOIDmode,
2784 allows_reg ? EXPAND_NORMAL
2785 : allows_mem ? EXPAND_MEMORY
2786 : EXPAND_INITIALIZER);
2788 /* Never pass a CONCAT to an ASM. */
2789 if (GET_CODE (op) == CONCAT)
2790 op = force_reg (GET_MODE (op), op);
2791 else if (MEM_P (op))
2792 op = validize_mem (op);
2794 if (asm_operand_ok (op, constraint, NULL) <= 0)
2796 if (allows_reg && TYPE_MODE (type) != BLKmode)
2797 op = force_reg (TYPE_MODE (type), op);
2798 else if (!allows_mem)
2799 warning (0, "asm operand %d probably doesn%'t match constraints",
2800 i + noutputs);
2801 else if (MEM_P (op))
2803 /* We won't recognize either volatile memory or memory
2804 with a queued address as available a memory_operand
2805 at this point. Ignore it: clearly this *is* a memory. */
2807 else
2808 gcc_unreachable ();
2810 input_rvec[i] = op;
2811 input_mode[i] = TYPE_MODE (type);
2814 /* For in-out operands, copy output rtx to input rtx. */
2815 unsigned ninout = inout_opnum.length();
2816 for (i = 0; i < ninout; i++)
2818 int j = inout_opnum[i];
2819 rtx o = output_rvec[j];
2821 input_rvec.safe_push (o);
2822 input_mode.safe_push (GET_MODE (o));
2824 char buffer[16];
2825 sprintf (buffer, "%d", j);
2826 constraints.safe_push (ggc_strdup (buffer));
2828 ninputs += ninout;
2830 /* Sometimes we wish to automatically clobber registers across an asm.
2831 Case in point is when the i386 backend moved from cc0 to a hard reg --
2832 maintaining source-level compatibility means automatically clobbering
2833 the flags register. */
2834 rtx_insn *after_md_seq = NULL;
2835 if (targetm.md_asm_adjust)
2836 after_md_seq = targetm.md_asm_adjust (output_rvec, input_rvec,
2837 constraints, clobber_rvec,
2838 clobbered_regs);
2840 /* Do not allow the hook to change the output and input count,
2841 lest it mess up the operand numbering. */
2842 gcc_assert (output_rvec.length() == noutputs);
2843 gcc_assert (input_rvec.length() == ninputs);
2844 gcc_assert (constraints.length() == noutputs + ninputs);
2846 /* But it certainly can adjust the clobbers. */
2847 nclobbers = clobber_rvec.length();
2849 /* Third pass checks for easy conflicts. */
2850 /* ??? Why are we doing this on trees instead of rtx. */
2852 bool clobber_conflict_found = 0;
2853 for (i = 0; i < noutputs; ++i)
2854 if (tree_conflicts_with_clobbers_p (output_tvec[i], &clobbered_regs))
2855 clobber_conflict_found = 1;
2856 for (i = 0; i < ninputs - ninout; ++i)
2857 if (tree_conflicts_with_clobbers_p (input_tvec[i], &clobbered_regs))
2858 clobber_conflict_found = 1;
2860 /* Make vectors for the expression-rtx, constraint strings,
2861 and named operands. */
2863 rtvec argvec = rtvec_alloc (ninputs);
2864 rtvec constraintvec = rtvec_alloc (ninputs);
2865 rtvec labelvec = rtvec_alloc (nlabels);
2867 rtx body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
2868 : GET_MODE (output_rvec[0])),
2869 ggc_strdup (gimple_asm_string (stmt)),
2870 empty_string, 0, argvec, constraintvec,
2871 labelvec, locus);
2872 MEM_VOLATILE_P (body) = gimple_asm_volatile_p (stmt);
2874 for (i = 0; i < ninputs; ++i)
2876 ASM_OPERANDS_INPUT (body, i) = input_rvec[i];
2877 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
2878 = gen_rtx_ASM_INPUT_loc (input_mode[i],
2879 constraints[i + noutputs],
2880 locus);
2883 /* Copy labels to the vector. */
2884 rtx_code_label *fallthru_label = NULL;
2885 if (nlabels > 0)
2887 basic_block fallthru_bb = NULL;
2888 edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
2889 if (fallthru)
2890 fallthru_bb = fallthru->dest;
2892 for (i = 0; i < nlabels; ++i)
2894 tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
2895 rtx_insn *r;
2896 /* If asm goto has any labels in the fallthru basic block, use
2897 a label that we emit immediately after the asm goto. Expansion
2898 may insert further instructions into the same basic block after
2899 asm goto and if we don't do this, insertion of instructions on
2900 the fallthru edge might misbehave. See PR58670. */
2901 if (fallthru_bb && label_to_block_fn (cfun, label) == fallthru_bb)
2903 if (fallthru_label == NULL_RTX)
2904 fallthru_label = gen_label_rtx ();
2905 r = fallthru_label;
2907 else
2908 r = label_rtx (label);
2909 ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
2913 /* Now, for each output, construct an rtx
2914 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
2915 ARGVEC CONSTRAINTS OPNAMES))
2916 If there is more than one, put them inside a PARALLEL. */
2918 if (nlabels > 0 && nclobbers == 0)
2920 gcc_assert (noutputs == 0);
2921 emit_jump_insn (body);
2923 else if (noutputs == 0 && nclobbers == 0)
2925 /* No output operands: put in a raw ASM_OPERANDS rtx. */
2926 emit_insn (body);
2928 else if (noutputs == 1 && nclobbers == 0)
2930 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
2931 emit_insn (gen_rtx_SET (output_rvec[0], body));
2933 else
2935 rtx obody = body;
2936 int num = noutputs;
2938 if (num == 0)
2939 num = 1;
2941 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
2943 /* For each output operand, store a SET. */
2944 for (i = 0; i < noutputs; ++i)
2946 rtx src, o = output_rvec[i];
2947 if (i == 0)
2949 ASM_OPERANDS_OUTPUT_CONSTRAINT (obody) = constraints[0];
2950 src = obody;
2952 else
2954 src = gen_rtx_ASM_OPERANDS (GET_MODE (o),
2955 ASM_OPERANDS_TEMPLATE (obody),
2956 constraints[i], i, argvec,
2957 constraintvec, labelvec, locus);
2958 MEM_VOLATILE_P (src) = gimple_asm_volatile_p (stmt);
2960 XVECEXP (body, 0, i) = gen_rtx_SET (o, src);
2963 /* If there are no outputs (but there are some clobbers)
2964 store the bare ASM_OPERANDS into the PARALLEL. */
2965 if (i == 0)
2966 XVECEXP (body, 0, i++) = obody;
2968 /* Store (clobber REG) for each clobbered register specified. */
2969 for (unsigned j = 0; j < nclobbers; ++j)
2971 rtx clobbered_reg = clobber_rvec[j];
2973 /* Do sanity check for overlap between clobbers and respectively
2974 input and outputs that hasn't been handled. Such overlap
2975 should have been detected and reported above. */
2976 if (!clobber_conflict_found && REG_P (clobbered_reg))
2978 /* We test the old body (obody) contents to avoid
2979 tripping over the under-construction body. */
2980 for (unsigned k = 0; k < noutputs; ++k)
2981 if (reg_overlap_mentioned_p (clobbered_reg, output_rvec[k]))
2982 internal_error ("asm clobber conflict with output operand");
2984 for (unsigned k = 0; k < ninputs - ninout; ++k)
2985 if (reg_overlap_mentioned_p (clobbered_reg, input_rvec[k]))
2986 internal_error ("asm clobber conflict with input operand");
2989 XVECEXP (body, 0, i++) = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
2992 if (nlabels > 0)
2993 emit_jump_insn (body);
2994 else
2995 emit_insn (body);
2998 generating_concat_p = old_generating_concat_p;
3000 if (fallthru_label)
3001 emit_label (fallthru_label);
3003 if (after_md_seq)
3004 emit_insn (after_md_seq);
3005 if (after_rtl_seq)
3006 emit_insn (after_rtl_seq);
3008 free_temp_slots ();
3009 crtl->has_asm_statement = 1;
3012 /* Emit code to jump to the address
3013 specified by the pointer expression EXP. */
3015 static void
3016 expand_computed_goto (tree exp)
3018 rtx x = expand_normal (exp);
3020 do_pending_stack_adjust ();
3021 emit_indirect_jump (x);
3024 /* Generate RTL code for a `goto' statement with target label LABEL.
3025 LABEL should be a LABEL_DECL tree node that was or will later be
3026 defined with `expand_label'. */
3028 static void
3029 expand_goto (tree label)
3031 #ifdef ENABLE_CHECKING
3032 /* Check for a nonlocal goto to a containing function. Should have
3033 gotten translated to __builtin_nonlocal_goto. */
3034 tree context = decl_function_context (label);
3035 gcc_assert (!context || context == current_function_decl);
3036 #endif
3038 emit_jump (jump_target_rtx (label));
3041 /* Output a return with no value. */
3043 static void
3044 expand_null_return_1 (void)
3046 clear_pending_stack_adjust ();
3047 do_pending_stack_adjust ();
3048 emit_jump (return_label);
3051 /* Generate RTL to return from the current function, with no value.
3052 (That is, we do not do anything about returning any value.) */
3054 void
3055 expand_null_return (void)
3057 /* If this function was declared to return a value, but we
3058 didn't, clobber the return registers so that they are not
3059 propagated live to the rest of the function. */
3060 clobber_return_register ();
3062 expand_null_return_1 ();
3065 /* Generate RTL to return from the current function, with value VAL. */
3067 static void
3068 expand_value_return (rtx val)
3070 /* Copy the value to the return location unless it's already there. */
3072 tree decl = DECL_RESULT (current_function_decl);
3073 rtx return_reg = DECL_RTL (decl);
3074 if (return_reg != val)
3076 tree funtype = TREE_TYPE (current_function_decl);
3077 tree type = TREE_TYPE (decl);
3078 int unsignedp = TYPE_UNSIGNED (type);
3079 machine_mode old_mode = DECL_MODE (decl);
3080 machine_mode mode;
3081 if (DECL_BY_REFERENCE (decl))
3082 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3083 else
3084 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3086 if (mode != old_mode)
3087 val = convert_modes (mode, old_mode, val, unsignedp);
3089 if (GET_CODE (return_reg) == PARALLEL)
3090 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3091 else
3092 emit_move_insn (return_reg, val);
3095 expand_null_return_1 ();
3098 /* Generate RTL to evaluate the expression RETVAL and return it
3099 from the current function. */
3101 static void
3102 expand_return (tree retval, tree bounds)
3104 rtx result_rtl;
3105 rtx val = 0;
3106 tree retval_rhs;
3107 rtx bounds_rtl;
3109 /* If function wants no value, give it none. */
3110 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3112 expand_normal (retval);
3113 expand_null_return ();
3114 return;
3117 if (retval == error_mark_node)
3119 /* Treat this like a return of no value from a function that
3120 returns a value. */
3121 expand_null_return ();
3122 return;
3124 else if ((TREE_CODE (retval) == MODIFY_EXPR
3125 || TREE_CODE (retval) == INIT_EXPR)
3126 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3127 retval_rhs = TREE_OPERAND (retval, 1);
3128 else
3129 retval_rhs = retval;
3131 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3133 /* Put returned bounds to the right place. */
3134 bounds_rtl = DECL_BOUNDS_RTL (DECL_RESULT (current_function_decl));
3135 if (bounds_rtl)
3137 rtx addr = NULL;
3138 rtx bnd = NULL;
3140 if (bounds && bounds != error_mark_node)
3142 bnd = expand_normal (bounds);
3143 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3145 else if (REG_P (bounds_rtl))
3147 if (bounds)
3148 bnd = chkp_expand_zero_bounds ();
3149 else
3151 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3152 addr = gen_rtx_MEM (Pmode, addr);
3153 bnd = targetm.calls.load_bounds_for_arg (addr, NULL, NULL);
3156 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3158 else
3160 int n;
3162 gcc_assert (GET_CODE (bounds_rtl) == PARALLEL);
3164 if (bounds)
3165 bnd = chkp_expand_zero_bounds ();
3166 else
3168 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3169 addr = gen_rtx_MEM (Pmode, addr);
3172 for (n = 0; n < XVECLEN (bounds_rtl, 0); n++)
3174 rtx slot = XEXP (XVECEXP (bounds_rtl, 0, n), 0);
3175 if (!bounds)
3177 rtx offs = XEXP (XVECEXP (bounds_rtl, 0, n), 1);
3178 rtx from = adjust_address (addr, Pmode, INTVAL (offs));
3179 bnd = targetm.calls.load_bounds_for_arg (from, NULL, NULL);
3181 targetm.calls.store_returned_bounds (slot, bnd);
3185 else if (chkp_function_instrumented_p (current_function_decl)
3186 && !BOUNDED_P (retval_rhs)
3187 && chkp_type_has_pointer (TREE_TYPE (retval_rhs))
3188 && TREE_CODE (retval_rhs) != RESULT_DECL)
3190 rtx addr = expand_normal (build_fold_addr_expr (retval_rhs));
3191 addr = gen_rtx_MEM (Pmode, addr);
3193 gcc_assert (MEM_P (result_rtl));
3195 chkp_copy_bounds_for_stack_parm (result_rtl, addr, TREE_TYPE (retval_rhs));
3198 /* If we are returning the RESULT_DECL, then the value has already
3199 been stored into it, so we don't have to do anything special. */
3200 if (TREE_CODE (retval_rhs) == RESULT_DECL)
3201 expand_value_return (result_rtl);
3203 /* If the result is an aggregate that is being returned in one (or more)
3204 registers, load the registers here. */
3206 else if (retval_rhs != 0
3207 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3208 && REG_P (result_rtl))
3210 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3211 if (val)
3213 /* Use the mode of the result value on the return register. */
3214 PUT_MODE (result_rtl, GET_MODE (val));
3215 expand_value_return (val);
3217 else
3218 expand_null_return ();
3220 else if (retval_rhs != 0
3221 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3222 && (REG_P (result_rtl)
3223 || (GET_CODE (result_rtl) == PARALLEL)))
3225 /* Compute the return value into a temporary (usually a pseudo reg). */
3227 = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3228 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3229 val = force_not_mem (val);
3230 expand_value_return (val);
3232 else
3234 /* No hard reg used; calculate value into hard return reg. */
3235 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3236 expand_value_return (result_rtl);
3240 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3241 STMT that doesn't require special handling for outgoing edges. That
3242 is no tailcalls and no GIMPLE_COND. */
3244 static void
3245 expand_gimple_stmt_1 (gimple stmt)
3247 tree op0;
3249 set_curr_insn_location (gimple_location (stmt));
3251 switch (gimple_code (stmt))
3253 case GIMPLE_GOTO:
3254 op0 = gimple_goto_dest (stmt);
3255 if (TREE_CODE (op0) == LABEL_DECL)
3256 expand_goto (op0);
3257 else
3258 expand_computed_goto (op0);
3259 break;
3260 case GIMPLE_LABEL:
3261 expand_label (gimple_label_label (as_a <glabel *> (stmt)));
3262 break;
3263 case GIMPLE_NOP:
3264 case GIMPLE_PREDICT:
3265 break;
3266 case GIMPLE_SWITCH:
3267 expand_case (as_a <gswitch *> (stmt));
3268 break;
3269 case GIMPLE_ASM:
3270 expand_asm_stmt (as_a <gasm *> (stmt));
3271 break;
3272 case GIMPLE_CALL:
3273 expand_call_stmt (as_a <gcall *> (stmt));
3274 break;
3276 case GIMPLE_RETURN:
3278 tree bnd = gimple_return_retbnd (as_a <greturn *> (stmt));
3279 op0 = gimple_return_retval (as_a <greturn *> (stmt));
3281 if (op0 && op0 != error_mark_node)
3283 tree result = DECL_RESULT (current_function_decl);
3285 /* If we are not returning the current function's RESULT_DECL,
3286 build an assignment to it. */
3287 if (op0 != result)
3289 /* I believe that a function's RESULT_DECL is unique. */
3290 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3292 /* ??? We'd like to use simply expand_assignment here,
3293 but this fails if the value is of BLKmode but the return
3294 decl is a register. expand_return has special handling
3295 for this combination, which eventually should move
3296 to common code. See comments there. Until then, let's
3297 build a modify expression :-/ */
3298 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3299 result, op0);
3301 /* Mark we have return statement with missing bounds. */
3302 if (!bnd && chkp_function_instrumented_p (cfun->decl))
3303 bnd = error_mark_node;
3306 if (!op0)
3307 expand_null_return ();
3308 else
3309 expand_return (op0, bnd);
3311 break;
3313 case GIMPLE_ASSIGN:
3315 gassign *assign_stmt = as_a <gassign *> (stmt);
3316 tree lhs = gimple_assign_lhs (assign_stmt);
3318 /* Tree expand used to fiddle with |= and &= of two bitfield
3319 COMPONENT_REFs here. This can't happen with gimple, the LHS
3320 of binary assigns must be a gimple reg. */
3322 if (TREE_CODE (lhs) != SSA_NAME
3323 || get_gimple_rhs_class (gimple_expr_code (stmt))
3324 == GIMPLE_SINGLE_RHS)
3326 tree rhs = gimple_assign_rhs1 (assign_stmt);
3327 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
3328 == GIMPLE_SINGLE_RHS);
3329 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
3330 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3331 if (TREE_CLOBBER_P (rhs))
3332 /* This is a clobber to mark the going out of scope for
3333 this LHS. */
3335 else
3336 expand_assignment (lhs, rhs,
3337 gimple_assign_nontemporal_move_p (
3338 assign_stmt));
3340 else
3342 rtx target, temp;
3343 bool nontemporal = gimple_assign_nontemporal_move_p (assign_stmt);
3344 struct separate_ops ops;
3345 bool promoted = false;
3347 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3348 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
3349 promoted = true;
3351 ops.code = gimple_assign_rhs_code (assign_stmt);
3352 ops.type = TREE_TYPE (lhs);
3353 switch (get_gimple_rhs_class (ops.code))
3355 case GIMPLE_TERNARY_RHS:
3356 ops.op2 = gimple_assign_rhs3 (assign_stmt);
3357 /* Fallthru */
3358 case GIMPLE_BINARY_RHS:
3359 ops.op1 = gimple_assign_rhs2 (assign_stmt);
3360 /* Fallthru */
3361 case GIMPLE_UNARY_RHS:
3362 ops.op0 = gimple_assign_rhs1 (assign_stmt);
3363 break;
3364 default:
3365 gcc_unreachable ();
3367 ops.location = gimple_location (stmt);
3369 /* If we want to use a nontemporal store, force the value to
3370 register first. If we store into a promoted register,
3371 don't directly expand to target. */
3372 temp = nontemporal || promoted ? NULL_RTX : target;
3373 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
3374 EXPAND_NORMAL);
3376 if (temp == target)
3378 else if (promoted)
3380 int unsignedp = SUBREG_PROMOTED_SIGN (target);
3381 /* If TEMP is a VOIDmode constant, use convert_modes to make
3382 sure that we properly convert it. */
3383 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
3385 temp = convert_modes (GET_MODE (target),
3386 TYPE_MODE (ops.type),
3387 temp, unsignedp);
3388 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
3389 GET_MODE (target), temp, unsignedp);
3392 convert_move (SUBREG_REG (target), temp, unsignedp);
3394 else if (nontemporal && emit_storent_insn (target, temp))
3396 else
3398 temp = force_operand (temp, target);
3399 if (temp != target)
3400 emit_move_insn (target, temp);
3404 break;
3406 default:
3407 gcc_unreachable ();
3411 /* Expand one gimple statement STMT and return the last RTL instruction
3412 before any of the newly generated ones.
3414 In addition to generating the necessary RTL instructions this also
3415 sets REG_EH_REGION notes if necessary and sets the current source
3416 location for diagnostics. */
3418 static rtx_insn *
3419 expand_gimple_stmt (gimple stmt)
3421 location_t saved_location = input_location;
3422 rtx_insn *last = get_last_insn ();
3423 int lp_nr;
3425 gcc_assert (cfun);
3427 /* We need to save and restore the current source location so that errors
3428 discovered during expansion are emitted with the right location. But
3429 it would be better if the diagnostic routines used the source location
3430 embedded in the tree nodes rather than globals. */
3431 if (gimple_has_location (stmt))
3432 input_location = gimple_location (stmt);
3434 expand_gimple_stmt_1 (stmt);
3436 /* Free any temporaries used to evaluate this statement. */
3437 free_temp_slots ();
3439 input_location = saved_location;
3441 /* Mark all insns that may trap. */
3442 lp_nr = lookup_stmt_eh_lp (stmt);
3443 if (lp_nr)
3445 rtx_insn *insn;
3446 for (insn = next_real_insn (last); insn;
3447 insn = next_real_insn (insn))
3449 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
3450 /* If we want exceptions for non-call insns, any
3451 may_trap_p instruction may throw. */
3452 && GET_CODE (PATTERN (insn)) != CLOBBER
3453 && GET_CODE (PATTERN (insn)) != USE
3454 && insn_could_throw_p (insn))
3455 make_reg_eh_region_note (insn, 0, lp_nr);
3459 return last;
3462 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
3463 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
3464 generated a tail call (something that might be denied by the ABI
3465 rules governing the call; see calls.c).
3467 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
3468 can still reach the rest of BB. The case here is __builtin_sqrt,
3469 where the NaN result goes through the external function (with a
3470 tailcall) and the normal result happens via a sqrt instruction. */
3472 static basic_block
3473 expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
3475 rtx_insn *last2, *last;
3476 edge e;
3477 edge_iterator ei;
3478 int probability;
3479 gcov_type count;
3481 last2 = last = expand_gimple_stmt (stmt);
3483 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
3484 if (CALL_P (last) && SIBLING_CALL_P (last))
3485 goto found;
3487 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3489 *can_fallthru = true;
3490 return NULL;
3492 found:
3493 /* ??? Wouldn't it be better to just reset any pending stack adjust?
3494 Any instructions emitted here are about to be deleted. */
3495 do_pending_stack_adjust ();
3497 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
3498 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
3499 EH or abnormal edges, we shouldn't have created a tail call in
3500 the first place. So it seems to me we should just be removing
3501 all edges here, or redirecting the existing fallthru edge to
3502 the exit block. */
3504 probability = 0;
3505 count = 0;
3507 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3509 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
3511 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3513 e->dest->count -= e->count;
3514 e->dest->frequency -= EDGE_FREQUENCY (e);
3515 if (e->dest->count < 0)
3516 e->dest->count = 0;
3517 if (e->dest->frequency < 0)
3518 e->dest->frequency = 0;
3520 count += e->count;
3521 probability += e->probability;
3522 remove_edge (e);
3524 else
3525 ei_next (&ei);
3528 /* This is somewhat ugly: the call_expr expander often emits instructions
3529 after the sibcall (to perform the function return). These confuse the
3530 find_many_sub_basic_blocks code, so we need to get rid of these. */
3531 last = NEXT_INSN (last);
3532 gcc_assert (BARRIER_P (last));
3534 *can_fallthru = false;
3535 while (NEXT_INSN (last))
3537 /* For instance an sqrt builtin expander expands if with
3538 sibcall in the then and label for `else`. */
3539 if (LABEL_P (NEXT_INSN (last)))
3541 *can_fallthru = true;
3542 break;
3544 delete_insn (NEXT_INSN (last));
3547 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
3548 | EDGE_SIBCALL);
3549 e->probability += probability;
3550 e->count += count;
3551 BB_END (bb) = last;
3552 update_bb_for_insn (bb);
3554 if (NEXT_INSN (last))
3556 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
3558 last = BB_END (bb);
3559 if (BARRIER_P (last))
3560 BB_END (bb) = PREV_INSN (last);
3563 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3565 return bb;
3568 /* Return the difference between the floor and the truncated result of
3569 a signed division by OP1 with remainder MOD. */
3570 static rtx
3571 floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3573 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
3574 return gen_rtx_IF_THEN_ELSE
3575 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3576 gen_rtx_IF_THEN_ELSE
3577 (mode, gen_rtx_LT (BImode,
3578 gen_rtx_DIV (mode, op1, mod),
3579 const0_rtx),
3580 constm1_rtx, const0_rtx),
3581 const0_rtx);
3584 /* Return the difference between the ceil and the truncated result of
3585 a signed division by OP1 with remainder MOD. */
3586 static rtx
3587 ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3589 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
3590 return gen_rtx_IF_THEN_ELSE
3591 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3592 gen_rtx_IF_THEN_ELSE
3593 (mode, gen_rtx_GT (BImode,
3594 gen_rtx_DIV (mode, op1, mod),
3595 const0_rtx),
3596 const1_rtx, const0_rtx),
3597 const0_rtx);
3600 /* Return the difference between the ceil and the truncated result of
3601 an unsigned division by OP1 with remainder MOD. */
3602 static rtx
3603 ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
3605 /* (mod != 0 ? 1 : 0) */
3606 return gen_rtx_IF_THEN_ELSE
3607 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3608 const1_rtx, const0_rtx);
3611 /* Return the difference between the rounded and the truncated result
3612 of a signed division by OP1 with remainder MOD. Halfway cases are
3613 rounded away from zero, rather than to the nearest even number. */
3614 static rtx
3615 round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3617 /* (abs (mod) >= abs (op1) - abs (mod)
3618 ? (op1 / mod > 0 ? 1 : -1)
3619 : 0) */
3620 return gen_rtx_IF_THEN_ELSE
3621 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
3622 gen_rtx_MINUS (mode,
3623 gen_rtx_ABS (mode, op1),
3624 gen_rtx_ABS (mode, mod))),
3625 gen_rtx_IF_THEN_ELSE
3626 (mode, gen_rtx_GT (BImode,
3627 gen_rtx_DIV (mode, op1, mod),
3628 const0_rtx),
3629 const1_rtx, constm1_rtx),
3630 const0_rtx);
3633 /* Return the difference between the rounded and the truncated result
3634 of a unsigned division by OP1 with remainder MOD. Halfway cases
3635 are rounded away from zero, rather than to the nearest even
3636 number. */
3637 static rtx
3638 round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
3640 /* (mod >= op1 - mod ? 1 : 0) */
3641 return gen_rtx_IF_THEN_ELSE
3642 (mode, gen_rtx_GE (BImode, mod,
3643 gen_rtx_MINUS (mode, op1, mod)),
3644 const1_rtx, const0_rtx);
3647 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
3648 any rtl. */
3650 static rtx
3651 convert_debug_memory_address (machine_mode mode, rtx x,
3652 addr_space_t as)
3654 machine_mode xmode = GET_MODE (x);
3656 #ifndef POINTERS_EXTEND_UNSIGNED
3657 gcc_assert (mode == Pmode
3658 || mode == targetm.addr_space.address_mode (as));
3659 gcc_assert (xmode == mode || xmode == VOIDmode);
3660 #else
3661 rtx temp;
3663 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
3665 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
3666 return x;
3668 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
3669 x = simplify_gen_subreg (mode, x, xmode,
3670 subreg_lowpart_offset
3671 (mode, xmode));
3672 else if (POINTERS_EXTEND_UNSIGNED > 0)
3673 x = gen_rtx_ZERO_EXTEND (mode, x);
3674 else if (!POINTERS_EXTEND_UNSIGNED)
3675 x = gen_rtx_SIGN_EXTEND (mode, x);
3676 else
3678 switch (GET_CODE (x))
3680 case SUBREG:
3681 if ((SUBREG_PROMOTED_VAR_P (x)
3682 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
3683 || (GET_CODE (SUBREG_REG (x)) == PLUS
3684 && REG_P (XEXP (SUBREG_REG (x), 0))
3685 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
3686 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
3687 && GET_MODE (SUBREG_REG (x)) == mode)
3688 return SUBREG_REG (x);
3689 break;
3690 case LABEL_REF:
3691 temp = gen_rtx_LABEL_REF (mode, LABEL_REF_LABEL (x));
3692 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
3693 return temp;
3694 case SYMBOL_REF:
3695 temp = shallow_copy_rtx (x);
3696 PUT_MODE (temp, mode);
3697 return temp;
3698 case CONST:
3699 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3700 if (temp)
3701 temp = gen_rtx_CONST (mode, temp);
3702 return temp;
3703 case PLUS:
3704 case MINUS:
3705 if (CONST_INT_P (XEXP (x, 1)))
3707 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3708 if (temp)
3709 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
3711 break;
3712 default:
3713 break;
3715 /* Don't know how to express ptr_extend as operation in debug info. */
3716 return NULL;
3718 #endif /* POINTERS_EXTEND_UNSIGNED */
3720 return x;
3723 /* Map from SSA_NAMEs to corresponding DEBUG_EXPR_DECLs created
3724 by avoid_deep_ter_for_debug. */
3726 static hash_map<tree, tree> *deep_ter_debug_map;
3728 /* Split too deep TER chains for debug stmts using debug temporaries. */
3730 static void
3731 avoid_deep_ter_for_debug (gimple stmt, int depth)
3733 use_operand_p use_p;
3734 ssa_op_iter iter;
3735 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
3737 tree use = USE_FROM_PTR (use_p);
3738 if (TREE_CODE (use) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (use))
3739 continue;
3740 gimple g = get_gimple_for_ssa_name (use);
3741 if (g == NULL)
3742 continue;
3743 if (depth > 6 && !stmt_ends_bb_p (g))
3745 if (deep_ter_debug_map == NULL)
3746 deep_ter_debug_map = new hash_map<tree, tree>;
3748 tree &vexpr = deep_ter_debug_map->get_or_insert (use);
3749 if (vexpr != NULL)
3750 continue;
3751 vexpr = make_node (DEBUG_EXPR_DECL);
3752 gimple def_temp = gimple_build_debug_bind (vexpr, use, g);
3753 DECL_ARTIFICIAL (vexpr) = 1;
3754 TREE_TYPE (vexpr) = TREE_TYPE (use);
3755 DECL_MODE (vexpr) = TYPE_MODE (TREE_TYPE (use));
3756 gimple_stmt_iterator gsi = gsi_for_stmt (g);
3757 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
3758 avoid_deep_ter_for_debug (def_temp, 0);
3760 else
3761 avoid_deep_ter_for_debug (g, depth + 1);
3765 /* Return an RTX equivalent to the value of the parameter DECL. */
3767 static rtx
3768 expand_debug_parm_decl (tree decl)
3770 rtx incoming = DECL_INCOMING_RTL (decl);
3772 if (incoming
3773 && GET_MODE (incoming) != BLKmode
3774 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
3775 || (MEM_P (incoming)
3776 && REG_P (XEXP (incoming, 0))
3777 && HARD_REGISTER_P (XEXP (incoming, 0)))))
3779 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
3781 #ifdef HAVE_window_save
3782 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
3783 If the target machine has an explicit window save instruction, the
3784 actual entry value is the corresponding OUTGOING_REGNO instead. */
3785 if (REG_P (incoming)
3786 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
3787 incoming
3788 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
3789 OUTGOING_REGNO (REGNO (incoming)), 0);
3790 else if (MEM_P (incoming))
3792 rtx reg = XEXP (incoming, 0);
3793 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
3795 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
3796 incoming = replace_equiv_address_nv (incoming, reg);
3798 else
3799 incoming = copy_rtx (incoming);
3801 #endif
3803 ENTRY_VALUE_EXP (rtl) = incoming;
3804 return rtl;
3807 if (incoming
3808 && GET_MODE (incoming) != BLKmode
3809 && !TREE_ADDRESSABLE (decl)
3810 && MEM_P (incoming)
3811 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
3812 || (GET_CODE (XEXP (incoming, 0)) == PLUS
3813 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
3814 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
3815 return copy_rtx (incoming);
3817 return NULL_RTX;
3820 /* Return an RTX equivalent to the value of the tree expression EXP. */
3822 static rtx
3823 expand_debug_expr (tree exp)
3825 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
3826 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3827 machine_mode inner_mode = VOIDmode;
3828 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
3829 addr_space_t as;
3831 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
3833 case tcc_expression:
3834 switch (TREE_CODE (exp))
3836 case COND_EXPR:
3837 case DOT_PROD_EXPR:
3838 case SAD_EXPR:
3839 case WIDEN_MULT_PLUS_EXPR:
3840 case WIDEN_MULT_MINUS_EXPR:
3841 case FMA_EXPR:
3842 goto ternary;
3844 case TRUTH_ANDIF_EXPR:
3845 case TRUTH_ORIF_EXPR:
3846 case TRUTH_AND_EXPR:
3847 case TRUTH_OR_EXPR:
3848 case TRUTH_XOR_EXPR:
3849 goto binary;
3851 case TRUTH_NOT_EXPR:
3852 goto unary;
3854 default:
3855 break;
3857 break;
3859 ternary:
3860 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
3861 if (!op2)
3862 return NULL_RTX;
3863 /* Fall through. */
3865 binary:
3866 case tcc_binary:
3867 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
3868 if (!op1)
3869 return NULL_RTX;
3870 switch (TREE_CODE (exp))
3872 case LSHIFT_EXPR:
3873 case RSHIFT_EXPR:
3874 case LROTATE_EXPR:
3875 case RROTATE_EXPR:
3876 case WIDEN_LSHIFT_EXPR:
3877 /* Ensure second operand isn't wider than the first one. */
3878 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 1)));
3879 if (SCALAR_INT_MODE_P (inner_mode))
3881 machine_mode opmode = mode;
3882 if (VECTOR_MODE_P (mode))
3883 opmode = GET_MODE_INNER (mode);
3884 if (SCALAR_INT_MODE_P (opmode)
3885 && (GET_MODE_PRECISION (opmode)
3886 < GET_MODE_PRECISION (inner_mode)))
3887 op1 = simplify_gen_subreg (opmode, op1, inner_mode,
3888 subreg_lowpart_offset (opmode,
3889 inner_mode));
3891 break;
3892 default:
3893 break;
3895 /* Fall through. */
3897 unary:
3898 case tcc_unary:
3899 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3900 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3901 if (!op0)
3902 return NULL_RTX;
3903 break;
3905 case tcc_comparison:
3906 unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
3907 goto binary;
3909 case tcc_type:
3910 case tcc_statement:
3911 gcc_unreachable ();
3913 case tcc_constant:
3914 case tcc_exceptional:
3915 case tcc_declaration:
3916 case tcc_reference:
3917 case tcc_vl_exp:
3918 break;
3921 switch (TREE_CODE (exp))
3923 case STRING_CST:
3924 if (!lookup_constant_def (exp))
3926 if (strlen (TREE_STRING_POINTER (exp)) + 1
3927 != (size_t) TREE_STRING_LENGTH (exp))
3928 return NULL_RTX;
3929 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
3930 op0 = gen_rtx_MEM (BLKmode, op0);
3931 set_mem_attributes (op0, exp, 0);
3932 return op0;
3934 /* Fall through... */
3936 case INTEGER_CST:
3937 case REAL_CST:
3938 case FIXED_CST:
3939 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
3940 return op0;
3942 case COMPLEX_CST:
3943 gcc_assert (COMPLEX_MODE_P (mode));
3944 op0 = expand_debug_expr (TREE_REALPART (exp));
3945 op1 = expand_debug_expr (TREE_IMAGPART (exp));
3946 return gen_rtx_CONCAT (mode, op0, op1);
3948 case DEBUG_EXPR_DECL:
3949 op0 = DECL_RTL_IF_SET (exp);
3951 if (op0)
3952 return op0;
3954 op0 = gen_rtx_DEBUG_EXPR (mode);
3955 DEBUG_EXPR_TREE_DECL (op0) = exp;
3956 SET_DECL_RTL (exp, op0);
3958 return op0;
3960 case VAR_DECL:
3961 case PARM_DECL:
3962 case FUNCTION_DECL:
3963 case LABEL_DECL:
3964 case CONST_DECL:
3965 case RESULT_DECL:
3966 op0 = DECL_RTL_IF_SET (exp);
3968 /* This decl was probably optimized away. */
3969 if (!op0)
3971 if (TREE_CODE (exp) != VAR_DECL
3972 || DECL_EXTERNAL (exp)
3973 || !TREE_STATIC (exp)
3974 || !DECL_NAME (exp)
3975 || DECL_HARD_REGISTER (exp)
3976 || DECL_IN_CONSTANT_POOL (exp)
3977 || mode == VOIDmode)
3978 return NULL;
3980 op0 = make_decl_rtl_for_debug (exp);
3981 if (!MEM_P (op0)
3982 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
3983 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
3984 return NULL;
3986 else
3987 op0 = copy_rtx (op0);
3989 if (GET_MODE (op0) == BLKmode
3990 /* If op0 is not BLKmode, but mode is, adjust_mode
3991 below would ICE. While it is likely a FE bug,
3992 try to be robust here. See PR43166. */
3993 || mode == BLKmode
3994 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
3996 gcc_assert (MEM_P (op0));
3997 op0 = adjust_address_nv (op0, mode, 0);
3998 return op0;
4001 /* Fall through. */
4003 adjust_mode:
4004 case PAREN_EXPR:
4005 CASE_CONVERT:
4007 inner_mode = GET_MODE (op0);
4009 if (mode == inner_mode)
4010 return op0;
4012 if (inner_mode == VOIDmode)
4014 if (TREE_CODE (exp) == SSA_NAME)
4015 inner_mode = TYPE_MODE (TREE_TYPE (exp));
4016 else
4017 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4018 if (mode == inner_mode)
4019 return op0;
4022 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4024 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4025 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4026 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4027 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4028 else
4029 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4031 else if (FLOAT_MODE_P (mode))
4033 gcc_assert (TREE_CODE (exp) != SSA_NAME);
4034 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4035 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
4036 else
4037 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
4039 else if (FLOAT_MODE_P (inner_mode))
4041 if (unsignedp)
4042 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4043 else
4044 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4046 else if (CONSTANT_P (op0)
4047 || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
4048 op0 = simplify_gen_subreg (mode, op0, inner_mode,
4049 subreg_lowpart_offset (mode,
4050 inner_mode));
4051 else if (UNARY_CLASS_P (exp)
4052 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
4053 : unsignedp)
4054 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4055 else
4056 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4058 return op0;
4061 case MEM_REF:
4062 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4064 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
4065 TREE_OPERAND (exp, 0),
4066 TREE_OPERAND (exp, 1));
4067 if (newexp)
4068 return expand_debug_expr (newexp);
4070 /* FALLTHROUGH */
4071 case INDIRECT_REF:
4072 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4073 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4074 if (!op0)
4075 return NULL;
4077 if (TREE_CODE (exp) == MEM_REF)
4079 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4080 || (GET_CODE (op0) == PLUS
4081 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
4082 /* (mem (debug_implicit_ptr)) might confuse aliasing.
4083 Instead just use get_inner_reference. */
4084 goto component_ref;
4086 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4087 if (!op1 || !CONST_INT_P (op1))
4088 return NULL;
4090 op0 = plus_constant (inner_mode, op0, INTVAL (op1));
4093 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4095 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4096 op0, as);
4097 if (op0 == NULL_RTX)
4098 return NULL;
4100 op0 = gen_rtx_MEM (mode, op0);
4101 set_mem_attributes (op0, exp, 0);
4102 if (TREE_CODE (exp) == MEM_REF
4103 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4104 set_mem_expr (op0, NULL_TREE);
4105 set_mem_addr_space (op0, as);
4107 return op0;
4109 case TARGET_MEM_REF:
4110 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
4111 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
4112 return NULL;
4114 op0 = expand_debug_expr
4115 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
4116 if (!op0)
4117 return NULL;
4119 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4120 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4121 op0, as);
4122 if (op0 == NULL_RTX)
4123 return NULL;
4125 op0 = gen_rtx_MEM (mode, op0);
4127 set_mem_attributes (op0, exp, 0);
4128 set_mem_addr_space (op0, as);
4130 return op0;
4132 component_ref:
4133 case ARRAY_REF:
4134 case ARRAY_RANGE_REF:
4135 case COMPONENT_REF:
4136 case BIT_FIELD_REF:
4137 case REALPART_EXPR:
4138 case IMAGPART_EXPR:
4139 case VIEW_CONVERT_EXPR:
4141 machine_mode mode1;
4142 HOST_WIDE_INT bitsize, bitpos;
4143 tree offset;
4144 int volatilep = 0;
4145 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
4146 &mode1, &unsignedp, &volatilep, false);
4147 rtx orig_op0;
4149 if (bitsize == 0)
4150 return NULL;
4152 orig_op0 = op0 = expand_debug_expr (tem);
4154 if (!op0)
4155 return NULL;
4157 if (offset)
4159 machine_mode addrmode, offmode;
4161 if (!MEM_P (op0))
4162 return NULL;
4164 op0 = XEXP (op0, 0);
4165 addrmode = GET_MODE (op0);
4166 if (addrmode == VOIDmode)
4167 addrmode = Pmode;
4169 op1 = expand_debug_expr (offset);
4170 if (!op1)
4171 return NULL;
4173 offmode = GET_MODE (op1);
4174 if (offmode == VOIDmode)
4175 offmode = TYPE_MODE (TREE_TYPE (offset));
4177 if (addrmode != offmode)
4178 op1 = simplify_gen_subreg (addrmode, op1, offmode,
4179 subreg_lowpart_offset (addrmode,
4180 offmode));
4182 /* Don't use offset_address here, we don't need a
4183 recognizable address, and we don't want to generate
4184 code. */
4185 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4186 op0, op1));
4189 if (MEM_P (op0))
4191 if (mode1 == VOIDmode)
4192 /* Bitfield. */
4193 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
4194 if (bitpos >= BITS_PER_UNIT)
4196 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
4197 bitpos %= BITS_PER_UNIT;
4199 else if (bitpos < 0)
4201 HOST_WIDE_INT units
4202 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4203 op0 = adjust_address_nv (op0, mode1, units);
4204 bitpos += units * BITS_PER_UNIT;
4206 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
4207 op0 = adjust_address_nv (op0, mode, 0);
4208 else if (GET_MODE (op0) != mode1)
4209 op0 = adjust_address_nv (op0, mode1, 0);
4210 else
4211 op0 = copy_rtx (op0);
4212 if (op0 == orig_op0)
4213 op0 = shallow_copy_rtx (op0);
4214 set_mem_attributes (op0, exp, 0);
4217 if (bitpos == 0 && mode == GET_MODE (op0))
4218 return op0;
4220 if (bitpos < 0)
4221 return NULL;
4223 if (GET_MODE (op0) == BLKmode)
4224 return NULL;
4226 if ((bitpos % BITS_PER_UNIT) == 0
4227 && bitsize == GET_MODE_BITSIZE (mode1))
4229 machine_mode opmode = GET_MODE (op0);
4231 if (opmode == VOIDmode)
4232 opmode = TYPE_MODE (TREE_TYPE (tem));
4234 /* This condition may hold if we're expanding the address
4235 right past the end of an array that turned out not to
4236 be addressable (i.e., the address was only computed in
4237 debug stmts). The gen_subreg below would rightfully
4238 crash, and the address doesn't really exist, so just
4239 drop it. */
4240 if (bitpos >= GET_MODE_BITSIZE (opmode))
4241 return NULL;
4243 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
4244 return simplify_gen_subreg (mode, op0, opmode,
4245 bitpos / BITS_PER_UNIT);
4248 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4249 && TYPE_UNSIGNED (TREE_TYPE (exp))
4250 ? SIGN_EXTRACT
4251 : ZERO_EXTRACT, mode,
4252 GET_MODE (op0) != VOIDmode
4253 ? GET_MODE (op0)
4254 : TYPE_MODE (TREE_TYPE (tem)),
4255 op0, GEN_INT (bitsize), GEN_INT (bitpos));
4258 case ABS_EXPR:
4259 return simplify_gen_unary (ABS, mode, op0, mode);
4261 case NEGATE_EXPR:
4262 return simplify_gen_unary (NEG, mode, op0, mode);
4264 case BIT_NOT_EXPR:
4265 return simplify_gen_unary (NOT, mode, op0, mode);
4267 case FLOAT_EXPR:
4268 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4269 0)))
4270 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4271 inner_mode);
4273 case FIX_TRUNC_EXPR:
4274 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4275 inner_mode);
4277 case POINTER_PLUS_EXPR:
4278 /* For the rare target where pointers are not the same size as
4279 size_t, we need to check for mis-matched modes and correct
4280 the addend. */
4281 if (op0 && op1
4282 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
4283 && GET_MODE (op0) != GET_MODE (op1))
4285 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1))
4286 /* If OP0 is a partial mode, then we must truncate, even if it has
4287 the same bitsize as OP1 as GCC's representation of partial modes
4288 is opaque. */
4289 || (GET_MODE_CLASS (GET_MODE (op0)) == MODE_PARTIAL_INT
4290 && GET_MODE_BITSIZE (GET_MODE (op0)) == GET_MODE_BITSIZE (GET_MODE (op1))))
4291 op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
4292 GET_MODE (op1));
4293 else
4294 /* We always sign-extend, regardless of the signedness of
4295 the operand, because the operand is always unsigned
4296 here even if the original C expression is signed. */
4297 op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
4298 GET_MODE (op1));
4300 /* Fall through. */
4301 case PLUS_EXPR:
4302 return simplify_gen_binary (PLUS, mode, op0, op1);
4304 case MINUS_EXPR:
4305 return simplify_gen_binary (MINUS, mode, op0, op1);
4307 case MULT_EXPR:
4308 return simplify_gen_binary (MULT, mode, op0, op1);
4310 case RDIV_EXPR:
4311 case TRUNC_DIV_EXPR:
4312 case EXACT_DIV_EXPR:
4313 if (unsignedp)
4314 return simplify_gen_binary (UDIV, mode, op0, op1);
4315 else
4316 return simplify_gen_binary (DIV, mode, op0, op1);
4318 case TRUNC_MOD_EXPR:
4319 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4321 case FLOOR_DIV_EXPR:
4322 if (unsignedp)
4323 return simplify_gen_binary (UDIV, mode, op0, op1);
4324 else
4326 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4327 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4328 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4329 return simplify_gen_binary (PLUS, mode, div, adj);
4332 case FLOOR_MOD_EXPR:
4333 if (unsignedp)
4334 return simplify_gen_binary (UMOD, mode, op0, op1);
4335 else
4337 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4338 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4339 adj = simplify_gen_unary (NEG, mode,
4340 simplify_gen_binary (MULT, mode, adj, op1),
4341 mode);
4342 return simplify_gen_binary (PLUS, mode, mod, adj);
4345 case CEIL_DIV_EXPR:
4346 if (unsignedp)
4348 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4349 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4350 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4351 return simplify_gen_binary (PLUS, mode, div, adj);
4353 else
4355 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4356 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4357 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4358 return simplify_gen_binary (PLUS, mode, div, adj);
4361 case CEIL_MOD_EXPR:
4362 if (unsignedp)
4364 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4365 rtx adj = ceil_udiv_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);
4371 else
4373 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4374 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4375 adj = simplify_gen_unary (NEG, mode,
4376 simplify_gen_binary (MULT, mode, adj, op1),
4377 mode);
4378 return simplify_gen_binary (PLUS, mode, mod, adj);
4381 case ROUND_DIV_EXPR:
4382 if (unsignedp)
4384 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4385 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4386 rtx adj = round_udiv_adjust (mode, mod, op1);
4387 return simplify_gen_binary (PLUS, mode, div, adj);
4389 else
4391 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4392 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4393 rtx adj = round_sdiv_adjust (mode, mod, op1);
4394 return simplify_gen_binary (PLUS, mode, div, adj);
4397 case ROUND_MOD_EXPR:
4398 if (unsignedp)
4400 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4401 rtx adj = round_udiv_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);
4407 else
4409 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4410 rtx adj = round_sdiv_adjust (mode, mod, op1);
4411 adj = simplify_gen_unary (NEG, mode,
4412 simplify_gen_binary (MULT, mode, adj, op1),
4413 mode);
4414 return simplify_gen_binary (PLUS, mode, mod, adj);
4417 case LSHIFT_EXPR:
4418 return simplify_gen_binary (ASHIFT, mode, op0, op1);
4420 case RSHIFT_EXPR:
4421 if (unsignedp)
4422 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
4423 else
4424 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
4426 case LROTATE_EXPR:
4427 return simplify_gen_binary (ROTATE, mode, op0, op1);
4429 case RROTATE_EXPR:
4430 return simplify_gen_binary (ROTATERT, mode, op0, op1);
4432 case MIN_EXPR:
4433 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
4435 case MAX_EXPR:
4436 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
4438 case BIT_AND_EXPR:
4439 case TRUTH_AND_EXPR:
4440 return simplify_gen_binary (AND, mode, op0, op1);
4442 case BIT_IOR_EXPR:
4443 case TRUTH_OR_EXPR:
4444 return simplify_gen_binary (IOR, mode, op0, op1);
4446 case BIT_XOR_EXPR:
4447 case TRUTH_XOR_EXPR:
4448 return simplify_gen_binary (XOR, mode, op0, op1);
4450 case TRUTH_ANDIF_EXPR:
4451 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
4453 case TRUTH_ORIF_EXPR:
4454 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
4456 case TRUTH_NOT_EXPR:
4457 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
4459 case LT_EXPR:
4460 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
4461 op0, op1);
4463 case LE_EXPR:
4464 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
4465 op0, op1);
4467 case GT_EXPR:
4468 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
4469 op0, op1);
4471 case GE_EXPR:
4472 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
4473 op0, op1);
4475 case EQ_EXPR:
4476 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
4478 case NE_EXPR:
4479 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
4481 case UNORDERED_EXPR:
4482 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
4484 case ORDERED_EXPR:
4485 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
4487 case UNLT_EXPR:
4488 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
4490 case UNLE_EXPR:
4491 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
4493 case UNGT_EXPR:
4494 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
4496 case UNGE_EXPR:
4497 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
4499 case UNEQ_EXPR:
4500 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
4502 case LTGT_EXPR:
4503 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
4505 case COND_EXPR:
4506 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
4508 case COMPLEX_EXPR:
4509 gcc_assert (COMPLEX_MODE_P (mode));
4510 if (GET_MODE (op0) == VOIDmode)
4511 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
4512 if (GET_MODE (op1) == VOIDmode)
4513 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
4514 return gen_rtx_CONCAT (mode, op0, op1);
4516 case CONJ_EXPR:
4517 if (GET_CODE (op0) == CONCAT)
4518 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
4519 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
4520 XEXP (op0, 1),
4521 GET_MODE_INNER (mode)));
4522 else
4524 machine_mode imode = GET_MODE_INNER (mode);
4525 rtx re, im;
4527 if (MEM_P (op0))
4529 re = adjust_address_nv (op0, imode, 0);
4530 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
4532 else
4534 machine_mode ifmode = int_mode_for_mode (mode);
4535 machine_mode ihmode = int_mode_for_mode (imode);
4536 rtx halfsize;
4537 if (ifmode == BLKmode || ihmode == BLKmode)
4538 return NULL;
4539 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
4540 re = op0;
4541 if (mode != ifmode)
4542 re = gen_rtx_SUBREG (ifmode, re, 0);
4543 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
4544 if (imode != ihmode)
4545 re = gen_rtx_SUBREG (imode, re, 0);
4546 im = copy_rtx (op0);
4547 if (mode != ifmode)
4548 im = gen_rtx_SUBREG (ifmode, im, 0);
4549 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
4550 if (imode != ihmode)
4551 im = gen_rtx_SUBREG (imode, im, 0);
4553 im = gen_rtx_NEG (imode, im);
4554 return gen_rtx_CONCAT (mode, re, im);
4557 case ADDR_EXPR:
4558 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4559 if (!op0 || !MEM_P (op0))
4561 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
4562 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
4563 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
4564 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
4565 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
4566 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
4568 if (handled_component_p (TREE_OPERAND (exp, 0)))
4570 HOST_WIDE_INT bitoffset, bitsize, maxsize;
4571 tree decl
4572 = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
4573 &bitoffset, &bitsize, &maxsize);
4574 if ((TREE_CODE (decl) == VAR_DECL
4575 || TREE_CODE (decl) == PARM_DECL
4576 || TREE_CODE (decl) == RESULT_DECL)
4577 && (!TREE_ADDRESSABLE (decl)
4578 || target_for_debug_bind (decl))
4579 && (bitoffset % BITS_PER_UNIT) == 0
4580 && bitsize > 0
4581 && bitsize == maxsize)
4583 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
4584 return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
4588 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
4589 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4590 == ADDR_EXPR)
4592 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4593 0));
4594 if (op0 != NULL
4595 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4596 || (GET_CODE (op0) == PLUS
4597 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
4598 && CONST_INT_P (XEXP (op0, 1)))))
4600 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4601 1));
4602 if (!op1 || !CONST_INT_P (op1))
4603 return NULL;
4605 return plus_constant (mode, op0, INTVAL (op1));
4609 return NULL;
4612 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
4613 op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
4615 return op0;
4617 case VECTOR_CST:
4619 unsigned i;
4621 op0 = gen_rtx_CONCATN
4622 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4624 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
4626 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
4627 if (!op1)
4628 return NULL;
4629 XVECEXP (op0, 0, i) = op1;
4632 return op0;
4635 case CONSTRUCTOR:
4636 if (TREE_CLOBBER_P (exp))
4637 return NULL;
4638 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
4640 unsigned i;
4641 tree val;
4643 op0 = gen_rtx_CONCATN
4644 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4646 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
4648 op1 = expand_debug_expr (val);
4649 if (!op1)
4650 return NULL;
4651 XVECEXP (op0, 0, i) = op1;
4654 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
4656 op1 = expand_debug_expr
4657 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
4659 if (!op1)
4660 return NULL;
4662 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
4663 XVECEXP (op0, 0, i) = op1;
4666 return op0;
4668 else
4669 goto flag_unsupported;
4671 case CALL_EXPR:
4672 /* ??? Maybe handle some builtins? */
4673 return NULL;
4675 case SSA_NAME:
4677 gimple g = get_gimple_for_ssa_name (exp);
4678 if (g)
4680 tree t = NULL_TREE;
4681 if (deep_ter_debug_map)
4683 tree *slot = deep_ter_debug_map->get (exp);
4684 if (slot)
4685 t = *slot;
4687 if (t == NULL_TREE)
4688 t = gimple_assign_rhs_to_tree (g);
4689 op0 = expand_debug_expr (t);
4690 if (!op0)
4691 return NULL;
4693 else
4695 int part = var_to_partition (SA.map, exp);
4697 if (part == NO_PARTITION)
4699 /* If this is a reference to an incoming value of parameter
4700 that is never used in the code or where the incoming
4701 value is never used in the code, use PARM_DECL's
4702 DECL_RTL if set. */
4703 if (SSA_NAME_IS_DEFAULT_DEF (exp)
4704 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL)
4706 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
4707 if (op0)
4708 goto adjust_mode;
4709 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
4710 if (op0)
4711 goto adjust_mode;
4713 return NULL;
4716 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
4718 op0 = copy_rtx (SA.partition_to_pseudo[part]);
4720 goto adjust_mode;
4723 case ERROR_MARK:
4724 return NULL;
4726 /* Vector stuff. For most of the codes we don't have rtl codes. */
4727 case REALIGN_LOAD_EXPR:
4728 case REDUC_MAX_EXPR:
4729 case REDUC_MIN_EXPR:
4730 case REDUC_PLUS_EXPR:
4731 case VEC_COND_EXPR:
4732 case VEC_PACK_FIX_TRUNC_EXPR:
4733 case VEC_PACK_SAT_EXPR:
4734 case VEC_PACK_TRUNC_EXPR:
4735 case VEC_UNPACK_FLOAT_HI_EXPR:
4736 case VEC_UNPACK_FLOAT_LO_EXPR:
4737 case VEC_UNPACK_HI_EXPR:
4738 case VEC_UNPACK_LO_EXPR:
4739 case VEC_WIDEN_MULT_HI_EXPR:
4740 case VEC_WIDEN_MULT_LO_EXPR:
4741 case VEC_WIDEN_MULT_EVEN_EXPR:
4742 case VEC_WIDEN_MULT_ODD_EXPR:
4743 case VEC_WIDEN_LSHIFT_HI_EXPR:
4744 case VEC_WIDEN_LSHIFT_LO_EXPR:
4745 case VEC_PERM_EXPR:
4746 return NULL;
4748 /* Misc codes. */
4749 case ADDR_SPACE_CONVERT_EXPR:
4750 case FIXED_CONVERT_EXPR:
4751 case OBJ_TYPE_REF:
4752 case WITH_SIZE_EXPR:
4753 return NULL;
4755 case DOT_PROD_EXPR:
4756 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4757 && SCALAR_INT_MODE_P (mode))
4760 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4761 0)))
4762 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4763 inner_mode);
4765 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4766 1)))
4767 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
4768 inner_mode);
4769 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4770 return simplify_gen_binary (PLUS, mode, op0, op2);
4772 return NULL;
4774 case WIDEN_MULT_EXPR:
4775 case WIDEN_MULT_PLUS_EXPR:
4776 case WIDEN_MULT_MINUS_EXPR:
4777 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4778 && SCALAR_INT_MODE_P (mode))
4780 inner_mode = GET_MODE (op0);
4781 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4782 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4783 else
4784 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4785 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
4786 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
4787 else
4788 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
4789 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4790 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
4791 return op0;
4792 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
4793 return simplify_gen_binary (PLUS, mode, op0, op2);
4794 else
4795 return simplify_gen_binary (MINUS, mode, op2, op0);
4797 return NULL;
4799 case MULT_HIGHPART_EXPR:
4800 /* ??? Similar to the above. */
4801 return NULL;
4803 case WIDEN_SUM_EXPR:
4804 case WIDEN_LSHIFT_EXPR:
4805 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4806 && SCALAR_INT_MODE_P (mode))
4809 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4810 0)))
4811 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4812 inner_mode);
4813 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
4814 ? ASHIFT : PLUS, mode, op0, op1);
4816 return NULL;
4818 case FMA_EXPR:
4819 return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
4821 default:
4822 flag_unsupported:
4823 #ifdef ENABLE_CHECKING
4824 debug_tree (exp);
4825 gcc_unreachable ();
4826 #else
4827 return NULL;
4828 #endif
4832 /* Return an RTX equivalent to the source bind value of the tree expression
4833 EXP. */
4835 static rtx
4836 expand_debug_source_expr (tree exp)
4838 rtx op0 = NULL_RTX;
4839 machine_mode mode = VOIDmode, inner_mode;
4841 switch (TREE_CODE (exp))
4843 case PARM_DECL:
4845 mode = DECL_MODE (exp);
4846 op0 = expand_debug_parm_decl (exp);
4847 if (op0)
4848 break;
4849 /* See if this isn't an argument that has been completely
4850 optimized out. */
4851 if (!DECL_RTL_SET_P (exp)
4852 && !DECL_INCOMING_RTL (exp)
4853 && DECL_ABSTRACT_ORIGIN (current_function_decl))
4855 tree aexp = DECL_ORIGIN (exp);
4856 if (DECL_CONTEXT (aexp)
4857 == DECL_ABSTRACT_ORIGIN (current_function_decl))
4859 vec<tree, va_gc> **debug_args;
4860 unsigned int ix;
4861 tree ddecl;
4862 debug_args = decl_debug_args_lookup (current_function_decl);
4863 if (debug_args != NULL)
4865 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
4866 ix += 2)
4867 if (ddecl == aexp)
4868 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
4872 break;
4874 default:
4875 break;
4878 if (op0 == NULL_RTX)
4879 return NULL_RTX;
4881 inner_mode = GET_MODE (op0);
4882 if (mode == inner_mode)
4883 return op0;
4885 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4887 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4888 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4889 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4890 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4891 else
4892 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4894 else if (FLOAT_MODE_P (mode))
4895 gcc_unreachable ();
4896 else if (FLOAT_MODE_P (inner_mode))
4898 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4899 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4900 else
4901 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4903 else if (CONSTANT_P (op0)
4904 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
4905 op0 = simplify_gen_subreg (mode, op0, inner_mode,
4906 subreg_lowpart_offset (mode, inner_mode));
4907 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4908 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4909 else
4910 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4912 return op0;
4915 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
4916 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
4917 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
4919 static void
4920 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
4922 rtx exp = *exp_p;
4924 if (exp == NULL_RTX)
4925 return;
4927 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
4928 return;
4930 if (depth == 4)
4932 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
4933 rtx dval = make_debug_expr_from_rtl (exp);
4935 /* Emit a debug bind insn before INSN. */
4936 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
4937 DEBUG_EXPR_TREE_DECL (dval), exp,
4938 VAR_INIT_STATUS_INITIALIZED);
4940 emit_debug_insn_before (bind, insn);
4941 *exp_p = dval;
4942 return;
4945 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
4946 int i, j;
4947 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4948 switch (*format_ptr++)
4950 case 'e':
4951 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
4952 break;
4954 case 'E':
4955 case 'V':
4956 for (j = 0; j < XVECLEN (exp, i); j++)
4957 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
4958 break;
4960 default:
4961 break;
4965 /* Expand the _LOCs in debug insns. We run this after expanding all
4966 regular insns, so that any variables referenced in the function
4967 will have their DECL_RTLs set. */
4969 static void
4970 expand_debug_locations (void)
4972 rtx_insn *insn;
4973 rtx_insn *last = get_last_insn ();
4974 int save_strict_alias = flag_strict_aliasing;
4976 /* New alias sets while setting up memory attributes cause
4977 -fcompare-debug failures, even though it doesn't bring about any
4978 codegen changes. */
4979 flag_strict_aliasing = 0;
4981 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4982 if (DEBUG_INSN_P (insn))
4984 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
4985 rtx val;
4986 rtx_insn *prev_insn, *insn2;
4987 machine_mode mode;
4989 if (value == NULL_TREE)
4990 val = NULL_RTX;
4991 else
4993 if (INSN_VAR_LOCATION_STATUS (insn)
4994 == VAR_INIT_STATUS_UNINITIALIZED)
4995 val = expand_debug_source_expr (value);
4996 /* The avoid_deep_ter_for_debug function inserts
4997 debug bind stmts after SSA_NAME definition, with the
4998 SSA_NAME as the whole bind location. Disable temporarily
4999 expansion of that SSA_NAME into the DEBUG_EXPR_DECL
5000 being defined in this DEBUG_INSN. */
5001 else if (deep_ter_debug_map && TREE_CODE (value) == SSA_NAME)
5003 tree *slot = deep_ter_debug_map->get (value);
5004 if (slot)
5006 if (*slot == INSN_VAR_LOCATION_DECL (insn))
5007 *slot = NULL_TREE;
5008 else
5009 slot = NULL;
5011 val = expand_debug_expr (value);
5012 if (slot)
5013 *slot = INSN_VAR_LOCATION_DECL (insn);
5015 else
5016 val = expand_debug_expr (value);
5017 gcc_assert (last == get_last_insn ());
5020 if (!val)
5021 val = gen_rtx_UNKNOWN_VAR_LOC ();
5022 else
5024 mode = GET_MODE (INSN_VAR_LOCATION (insn));
5026 gcc_assert (mode == GET_MODE (val)
5027 || (GET_MODE (val) == VOIDmode
5028 && (CONST_SCALAR_INT_P (val)
5029 || GET_CODE (val) == CONST_FIXED
5030 || GET_CODE (val) == LABEL_REF)));
5033 INSN_VAR_LOCATION_LOC (insn) = val;
5034 prev_insn = PREV_INSN (insn);
5035 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
5036 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
5039 flag_strict_aliasing = save_strict_alias;
5042 /* Performs swapping operands of commutative operations to expand
5043 the expensive one first. */
5045 static void
5046 reorder_operands (basic_block bb)
5048 unsigned int *lattice; /* Hold cost of each statement. */
5049 unsigned int i = 0, n = 0;
5050 gimple_stmt_iterator gsi;
5051 gimple_seq stmts;
5052 gimple stmt;
5053 bool swap;
5054 tree op0, op1;
5055 ssa_op_iter iter;
5056 use_operand_p use_p;
5057 gimple def0, def1;
5059 /* Compute cost of each statement using estimate_num_insns. */
5060 stmts = bb_seq (bb);
5061 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5063 stmt = gsi_stmt (gsi);
5064 if (!is_gimple_debug (stmt))
5065 gimple_set_uid (stmt, n++);
5067 lattice = XNEWVEC (unsigned int, n);
5068 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
5070 unsigned cost;
5071 stmt = gsi_stmt (gsi);
5072 if (is_gimple_debug (stmt))
5073 continue;
5074 cost = estimate_num_insns (stmt, &eni_size_weights);
5075 lattice[i] = cost;
5076 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
5078 tree use = USE_FROM_PTR (use_p);
5079 gimple def_stmt;
5080 if (TREE_CODE (use) != SSA_NAME)
5081 continue;
5082 def_stmt = get_gimple_for_ssa_name (use);
5083 if (!def_stmt)
5084 continue;
5085 lattice[i] += lattice[gimple_uid (def_stmt)];
5087 i++;
5088 if (!is_gimple_assign (stmt)
5089 || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
5090 continue;
5091 op0 = gimple_op (stmt, 1);
5092 op1 = gimple_op (stmt, 2);
5093 if (TREE_CODE (op0) != SSA_NAME
5094 || TREE_CODE (op1) != SSA_NAME)
5095 continue;
5096 /* Swap operands if the second one is more expensive. */
5097 def0 = get_gimple_for_ssa_name (op0);
5098 def1 = get_gimple_for_ssa_name (op1);
5099 if (!def1)
5100 continue;
5101 swap = false;
5102 if (!def0 || lattice[gimple_uid (def1)] > lattice[gimple_uid (def0)])
5103 swap = true;
5104 if (swap)
5106 if (dump_file && (dump_flags & TDF_DETAILS))
5108 fprintf (dump_file, "Swap operands in stmt:\n");
5109 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5110 fprintf (dump_file, "Cost left opnd=%d, right opnd=%d\n",
5111 def0 ? lattice[gimple_uid (def0)] : 0,
5112 lattice[gimple_uid (def1)]);
5114 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
5115 gimple_assign_rhs2_ptr (stmt));
5118 XDELETE (lattice);
5121 /* Expand basic block BB from GIMPLE trees to RTL. */
5123 static basic_block
5124 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
5126 gimple_stmt_iterator gsi;
5127 gimple_seq stmts;
5128 gimple stmt = NULL;
5129 rtx_note *note;
5130 rtx_insn *last;
5131 edge e;
5132 edge_iterator ei;
5134 if (dump_file)
5135 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
5136 bb->index);
5138 /* Note that since we are now transitioning from GIMPLE to RTL, we
5139 cannot use the gsi_*_bb() routines because they expect the basic
5140 block to be in GIMPLE, instead of RTL. Therefore, we need to
5141 access the BB sequence directly. */
5142 if (optimize)
5143 reorder_operands (bb);
5144 stmts = bb_seq (bb);
5145 bb->il.gimple.seq = NULL;
5146 bb->il.gimple.phi_nodes = NULL;
5147 rtl_profile_for_bb (bb);
5148 init_rtl_bb_info (bb);
5149 bb->flags |= BB_RTL;
5151 /* Remove the RETURN_EXPR if we may fall though to the exit
5152 instead. */
5153 gsi = gsi_last (stmts);
5154 if (!gsi_end_p (gsi)
5155 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
5157 greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
5159 gcc_assert (single_succ_p (bb));
5160 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
5162 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5163 && !gimple_return_retval (ret_stmt))
5165 gsi_remove (&gsi, false);
5166 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
5170 gsi = gsi_start (stmts);
5171 if (!gsi_end_p (gsi))
5173 stmt = gsi_stmt (gsi);
5174 if (gimple_code (stmt) != GIMPLE_LABEL)
5175 stmt = NULL;
5178 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
5180 if (stmt || elt)
5182 last = get_last_insn ();
5184 if (stmt)
5186 expand_gimple_stmt (stmt);
5187 gsi_next (&gsi);
5190 if (elt)
5191 emit_label (*elt);
5193 /* Java emits line number notes in the top of labels.
5194 ??? Make this go away once line number notes are obsoleted. */
5195 BB_HEAD (bb) = NEXT_INSN (last);
5196 if (NOTE_P (BB_HEAD (bb)))
5197 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
5198 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
5200 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5202 else
5203 BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
5205 NOTE_BASIC_BLOCK (note) = bb;
5207 for (; !gsi_end_p (gsi); gsi_next (&gsi))
5209 basic_block new_bb;
5211 stmt = gsi_stmt (gsi);
5213 /* If this statement is a non-debug one, and we generate debug
5214 insns, then this one might be the last real use of a TERed
5215 SSA_NAME, but where there are still some debug uses further
5216 down. Expanding the current SSA name in such further debug
5217 uses by their RHS might lead to wrong debug info, as coalescing
5218 might make the operands of such RHS be placed into the same
5219 pseudo as something else. Like so:
5220 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
5221 use(a_1);
5222 a_2 = ...
5223 #DEBUG ... => a_1
5224 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
5225 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
5226 the write to a_2 would actually have clobbered the place which
5227 formerly held a_0.
5229 So, instead of that, we recognize the situation, and generate
5230 debug temporaries at the last real use of TERed SSA names:
5231 a_1 = a_0 + 1;
5232 #DEBUG #D1 => a_1
5233 use(a_1);
5234 a_2 = ...
5235 #DEBUG ... => #D1
5237 if (MAY_HAVE_DEBUG_INSNS
5238 && SA.values
5239 && !is_gimple_debug (stmt))
5241 ssa_op_iter iter;
5242 tree op;
5243 gimple def;
5245 location_t sloc = curr_insn_location ();
5247 /* Look for SSA names that have their last use here (TERed
5248 names always have only one real use). */
5249 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5250 if ((def = get_gimple_for_ssa_name (op)))
5252 imm_use_iterator imm_iter;
5253 use_operand_p use_p;
5254 bool have_debug_uses = false;
5256 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5258 if (gimple_debug_bind_p (USE_STMT (use_p)))
5260 have_debug_uses = true;
5261 break;
5265 if (have_debug_uses)
5267 /* OP is a TERed SSA name, with DEF its defining
5268 statement, and where OP is used in further debug
5269 instructions. Generate a debug temporary, and
5270 replace all uses of OP in debug insns with that
5271 temporary. */
5272 gimple debugstmt;
5273 tree value = gimple_assign_rhs_to_tree (def);
5274 tree vexpr = make_node (DEBUG_EXPR_DECL);
5275 rtx val;
5276 machine_mode mode;
5278 set_curr_insn_location (gimple_location (def));
5280 DECL_ARTIFICIAL (vexpr) = 1;
5281 TREE_TYPE (vexpr) = TREE_TYPE (value);
5282 if (DECL_P (value))
5283 mode = DECL_MODE (value);
5284 else
5285 mode = TYPE_MODE (TREE_TYPE (value));
5286 DECL_MODE (vexpr) = mode;
5288 val = gen_rtx_VAR_LOCATION
5289 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5291 emit_debug_insn (val);
5293 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5295 if (!gimple_debug_bind_p (debugstmt))
5296 continue;
5298 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5299 SET_USE (use_p, vexpr);
5301 update_stmt (debugstmt);
5305 set_curr_insn_location (sloc);
5308 currently_expanding_gimple_stmt = stmt;
5310 /* Expand this statement, then evaluate the resulting RTL and
5311 fixup the CFG accordingly. */
5312 if (gimple_code (stmt) == GIMPLE_COND)
5314 new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
5315 if (new_bb)
5316 return new_bb;
5318 else if (gimple_debug_bind_p (stmt))
5320 location_t sloc = curr_insn_location ();
5321 gimple_stmt_iterator nsi = gsi;
5323 for (;;)
5325 tree var = gimple_debug_bind_get_var (stmt);
5326 tree value;
5327 rtx val;
5328 machine_mode mode;
5330 if (TREE_CODE (var) != DEBUG_EXPR_DECL
5331 && TREE_CODE (var) != LABEL_DECL
5332 && !target_for_debug_bind (var))
5333 goto delink_debug_stmt;
5335 if (gimple_debug_bind_has_value_p (stmt))
5336 value = gimple_debug_bind_get_value (stmt);
5337 else
5338 value = NULL_TREE;
5340 last = get_last_insn ();
5342 set_curr_insn_location (gimple_location (stmt));
5344 if (DECL_P (var))
5345 mode = DECL_MODE (var);
5346 else
5347 mode = TYPE_MODE (TREE_TYPE (var));
5349 val = gen_rtx_VAR_LOCATION
5350 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5352 emit_debug_insn (val);
5354 if (dump_file && (dump_flags & TDF_DETAILS))
5356 /* We can't dump the insn with a TREE where an RTX
5357 is expected. */
5358 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5359 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5360 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5363 delink_debug_stmt:
5364 /* In order not to generate too many debug temporaries,
5365 we delink all uses of debug statements we already expanded.
5366 Therefore debug statements between definition and real
5367 use of TERed SSA names will continue to use the SSA name,
5368 and not be replaced with debug temps. */
5369 delink_stmt_imm_use (stmt);
5371 gsi = nsi;
5372 gsi_next (&nsi);
5373 if (gsi_end_p (nsi))
5374 break;
5375 stmt = gsi_stmt (nsi);
5376 if (!gimple_debug_bind_p (stmt))
5377 break;
5380 set_curr_insn_location (sloc);
5382 else if (gimple_debug_source_bind_p (stmt))
5384 location_t sloc = curr_insn_location ();
5385 tree var = gimple_debug_source_bind_get_var (stmt);
5386 tree value = gimple_debug_source_bind_get_value (stmt);
5387 rtx val;
5388 machine_mode mode;
5390 last = get_last_insn ();
5392 set_curr_insn_location (gimple_location (stmt));
5394 mode = DECL_MODE (var);
5396 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
5397 VAR_INIT_STATUS_UNINITIALIZED);
5399 emit_debug_insn (val);
5401 if (dump_file && (dump_flags & TDF_DETAILS))
5403 /* We can't dump the insn with a TREE where an RTX
5404 is expected. */
5405 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5406 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5407 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5410 set_curr_insn_location (sloc);
5412 else
5414 gcall *call_stmt = dyn_cast <gcall *> (stmt);
5415 if (call_stmt
5416 && gimple_call_tail_p (call_stmt)
5417 && disable_tail_calls)
5418 gimple_call_set_tail (call_stmt, false);
5420 if (call_stmt && gimple_call_tail_p (call_stmt))
5422 bool can_fallthru;
5423 new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
5424 if (new_bb)
5426 if (can_fallthru)
5427 bb = new_bb;
5428 else
5429 return new_bb;
5432 else
5434 def_operand_p def_p;
5435 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
5437 if (def_p != NULL)
5439 /* Ignore this stmt if it is in the list of
5440 replaceable expressions. */
5441 if (SA.values
5442 && bitmap_bit_p (SA.values,
5443 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
5444 continue;
5446 last = expand_gimple_stmt (stmt);
5447 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5452 currently_expanding_gimple_stmt = NULL;
5454 /* Expand implicit goto and convert goto_locus. */
5455 FOR_EACH_EDGE (e, ei, bb->succs)
5457 if (e->goto_locus != UNKNOWN_LOCATION)
5458 set_curr_insn_location (e->goto_locus);
5459 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
5461 emit_jump (label_rtx_for_bb (e->dest));
5462 e->flags &= ~EDGE_FALLTHRU;
5466 /* Expanded RTL can create a jump in the last instruction of block.
5467 This later might be assumed to be a jump to successor and break edge insertion.
5468 We need to insert dummy move to prevent this. PR41440. */
5469 if (single_succ_p (bb)
5470 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
5471 && (last = get_last_insn ())
5472 && JUMP_P (last))
5474 rtx dummy = gen_reg_rtx (SImode);
5475 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
5478 do_pending_stack_adjust ();
5480 /* Find the block tail. The last insn in the block is the insn
5481 before a barrier and/or table jump insn. */
5482 last = get_last_insn ();
5483 if (BARRIER_P (last))
5484 last = PREV_INSN (last);
5485 if (JUMP_TABLE_DATA_P (last))
5486 last = PREV_INSN (PREV_INSN (last));
5487 BB_END (bb) = last;
5489 update_bb_for_insn (bb);
5491 return bb;
5495 /* Create a basic block for initialization code. */
5497 static basic_block
5498 construct_init_block (void)
5500 basic_block init_block, first_block;
5501 edge e = NULL;
5502 int flags;
5504 /* Multiple entry points not supported yet. */
5505 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
5506 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5507 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
5508 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5509 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5511 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
5513 /* When entry edge points to first basic block, we don't need jump,
5514 otherwise we have to jump into proper target. */
5515 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
5517 tree label = gimple_block_label (e->dest);
5519 emit_jump (jump_target_rtx (label));
5520 flags = 0;
5522 else
5523 flags = EDGE_FALLTHRU;
5525 init_block = create_basic_block (NEXT_INSN (get_insns ()),
5526 get_last_insn (),
5527 ENTRY_BLOCK_PTR_FOR_FN (cfun));
5528 init_block->frequency = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
5529 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5530 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5531 if (e)
5533 first_block = e->dest;
5534 redirect_edge_succ (e, init_block);
5535 e = make_edge (init_block, first_block, flags);
5537 else
5538 e = make_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5539 e->probability = REG_BR_PROB_BASE;
5540 e->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5542 update_bb_for_insn (init_block);
5543 return init_block;
5546 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
5547 found in the block tree. */
5549 static void
5550 set_block_levels (tree block, int level)
5552 while (block)
5554 BLOCK_NUMBER (block) = level;
5555 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
5556 block = BLOCK_CHAIN (block);
5560 /* Create a block containing landing pads and similar stuff. */
5562 static void
5563 construct_exit_block (void)
5565 rtx_insn *head = get_last_insn ();
5566 rtx_insn *end;
5567 basic_block exit_block;
5568 edge e, e2;
5569 unsigned ix;
5570 edge_iterator ei;
5571 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
5572 rtx_insn *orig_end = BB_END (prev_bb);
5574 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5576 /* Make sure the locus is set to the end of the function, so that
5577 epilogue line numbers and warnings are set properly. */
5578 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
5579 input_location = cfun->function_end_locus;
5581 /* Generate rtl for function exit. */
5582 expand_function_end ();
5584 end = get_last_insn ();
5585 if (head == end)
5586 return;
5587 /* While emitting the function end we could move end of the last basic
5588 block. */
5589 BB_END (prev_bb) = orig_end;
5590 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
5591 head = NEXT_INSN (head);
5592 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
5593 bb frequency counting will be confused. Any instructions before that
5594 label are emitted for the case where PREV_BB falls through into the
5595 exit block, so append those instructions to prev_bb in that case. */
5596 if (NEXT_INSN (head) != return_label)
5598 while (NEXT_INSN (head) != return_label)
5600 if (!NOTE_P (NEXT_INSN (head)))
5601 BB_END (prev_bb) = NEXT_INSN (head);
5602 head = NEXT_INSN (head);
5605 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
5606 exit_block->frequency = EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency;
5607 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5608 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5610 ix = 0;
5611 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
5613 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
5614 if (!(e->flags & EDGE_ABNORMAL))
5615 redirect_edge_succ (e, exit_block);
5616 else
5617 ix++;
5620 e = make_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5621 e->probability = REG_BR_PROB_BASE;
5622 e->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5623 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5624 if (e2 != e)
5626 e->count -= e2->count;
5627 exit_block->count -= e2->count;
5628 exit_block->frequency -= EDGE_FREQUENCY (e2);
5630 if (e->count < 0)
5631 e->count = 0;
5632 if (exit_block->count < 0)
5633 exit_block->count = 0;
5634 if (exit_block->frequency < 0)
5635 exit_block->frequency = 0;
5636 update_bb_for_insn (exit_block);
5639 /* Helper function for discover_nonconstant_array_refs.
5640 Look for ARRAY_REF nodes with non-constant indexes and mark them
5641 addressable. */
5643 static tree
5644 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
5645 void *data ATTRIBUTE_UNUSED)
5647 tree t = *tp;
5649 if (IS_TYPE_OR_DECL_P (t))
5650 *walk_subtrees = 0;
5651 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5653 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5654 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
5655 && (!TREE_OPERAND (t, 2)
5656 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5657 || (TREE_CODE (t) == COMPONENT_REF
5658 && (!TREE_OPERAND (t,2)
5659 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5660 || TREE_CODE (t) == BIT_FIELD_REF
5661 || TREE_CODE (t) == REALPART_EXPR
5662 || TREE_CODE (t) == IMAGPART_EXPR
5663 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5664 || CONVERT_EXPR_P (t))
5665 t = TREE_OPERAND (t, 0);
5667 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5669 t = get_base_address (t);
5670 if (t && DECL_P (t)
5671 && DECL_MODE (t) != BLKmode)
5672 TREE_ADDRESSABLE (t) = 1;
5675 *walk_subtrees = 0;
5678 return NULL_TREE;
5681 /* RTL expansion is not able to compile array references with variable
5682 offsets for arrays stored in single register. Discover such
5683 expressions and mark variables as addressable to avoid this
5684 scenario. */
5686 static void
5687 discover_nonconstant_array_refs (void)
5689 basic_block bb;
5690 gimple_stmt_iterator gsi;
5692 FOR_EACH_BB_FN (bb, cfun)
5693 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5695 gimple stmt = gsi_stmt (gsi);
5696 if (!is_gimple_debug (stmt))
5697 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
5701 /* This function sets crtl->args.internal_arg_pointer to a virtual
5702 register if DRAP is needed. Local register allocator will replace
5703 virtual_incoming_args_rtx with the virtual register. */
5705 static void
5706 expand_stack_alignment (void)
5708 rtx drap_rtx;
5709 unsigned int preferred_stack_boundary;
5711 if (! SUPPORTS_STACK_ALIGNMENT)
5712 return;
5714 if (cfun->calls_alloca
5715 || cfun->has_nonlocal_label
5716 || crtl->has_nonlocal_goto)
5717 crtl->need_drap = true;
5719 /* Call update_stack_boundary here again to update incoming stack
5720 boundary. It may set incoming stack alignment to a different
5721 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
5722 use the minimum incoming stack alignment to check if it is OK
5723 to perform sibcall optimization since sibcall optimization will
5724 only align the outgoing stack to incoming stack boundary. */
5725 if (targetm.calls.update_stack_boundary)
5726 targetm.calls.update_stack_boundary ();
5728 /* The incoming stack frame has to be aligned at least at
5729 parm_stack_boundary. */
5730 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
5732 /* Update crtl->stack_alignment_estimated and use it later to align
5733 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
5734 exceptions since callgraph doesn't collect incoming stack alignment
5735 in this case. */
5736 if (cfun->can_throw_non_call_exceptions
5737 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
5738 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
5739 else
5740 preferred_stack_boundary = crtl->preferred_stack_boundary;
5741 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
5742 crtl->stack_alignment_estimated = preferred_stack_boundary;
5743 if (preferred_stack_boundary > crtl->stack_alignment_needed)
5744 crtl->stack_alignment_needed = preferred_stack_boundary;
5746 gcc_assert (crtl->stack_alignment_needed
5747 <= crtl->stack_alignment_estimated);
5749 crtl->stack_realign_needed
5750 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
5751 crtl->stack_realign_tried = crtl->stack_realign_needed;
5753 crtl->stack_realign_processed = true;
5755 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
5756 alignment. */
5757 gcc_assert (targetm.calls.get_drap_rtx != NULL);
5758 drap_rtx = targetm.calls.get_drap_rtx ();
5760 /* stack_realign_drap and drap_rtx must match. */
5761 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
5763 /* Do nothing if NULL is returned, which means DRAP is not needed. */
5764 if (NULL != drap_rtx)
5766 crtl->args.internal_arg_pointer = drap_rtx;
5768 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
5769 needed. */
5770 fixup_tail_calls ();
5775 static void
5776 expand_main_function (void)
5778 #if (defined(INVOKE__main) \
5779 || (!defined(HAS_INIT_SECTION) \
5780 && !defined(INIT_SECTION_ASM_OP) \
5781 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
5782 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
5783 #endif
5787 /* Expand code to initialize the stack_protect_guard. This is invoked at
5788 the beginning of a function to be protected. */
5790 #ifndef HAVE_stack_protect_set
5791 # define HAVE_stack_protect_set 0
5792 # define gen_stack_protect_set(x,y) (gcc_unreachable (), NULL_RTX)
5793 #endif
5795 static void
5796 stack_protect_prologue (void)
5798 tree guard_decl = targetm.stack_protect_guard ();
5799 rtx x, y;
5801 x = expand_normal (crtl->stack_protect_guard);
5802 y = expand_normal (guard_decl);
5804 /* Allow the target to copy from Y to X without leaking Y into a
5805 register. */
5806 if (HAVE_stack_protect_set)
5808 rtx insn = gen_stack_protect_set (x, y);
5809 if (insn)
5811 emit_insn (insn);
5812 return;
5816 /* Otherwise do a straight move. */
5817 emit_move_insn (x, y);
5820 /* Translate the intermediate representation contained in the CFG
5821 from GIMPLE trees to RTL.
5823 We do conversion per basic block and preserve/update the tree CFG.
5824 This implies we have to do some magic as the CFG can simultaneously
5825 consist of basic blocks containing RTL and GIMPLE trees. This can
5826 confuse the CFG hooks, so be careful to not manipulate CFG during
5827 the expansion. */
5829 namespace {
5831 const pass_data pass_data_expand =
5833 RTL_PASS, /* type */
5834 "expand", /* name */
5835 OPTGROUP_NONE, /* optinfo_flags */
5836 TV_EXPAND, /* tv_id */
5837 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
5838 | PROP_gimple_lcx
5839 | PROP_gimple_lvec
5840 | PROP_gimple_lva), /* properties_required */
5841 PROP_rtl, /* properties_provided */
5842 ( PROP_ssa | PROP_trees ), /* properties_destroyed */
5843 0, /* todo_flags_start */
5844 0, /* todo_flags_finish */
5847 class pass_expand : public rtl_opt_pass
5849 public:
5850 pass_expand (gcc::context *ctxt)
5851 : rtl_opt_pass (pass_data_expand, ctxt)
5854 /* opt_pass methods: */
5855 virtual unsigned int execute (function *);
5857 }; // class pass_expand
5859 unsigned int
5860 pass_expand::execute (function *fun)
5862 basic_block bb, init_block;
5863 sbitmap blocks;
5864 edge_iterator ei;
5865 edge e;
5866 rtx_insn *var_seq, *var_ret_seq;
5867 unsigned i;
5869 timevar_push (TV_OUT_OF_SSA);
5870 rewrite_out_of_ssa (&SA);
5871 timevar_pop (TV_OUT_OF_SSA);
5872 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
5874 if (MAY_HAVE_DEBUG_STMTS && flag_tree_ter)
5876 gimple_stmt_iterator gsi;
5877 FOR_EACH_BB_FN (bb, cfun)
5878 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5879 if (gimple_debug_bind_p (gsi_stmt (gsi)))
5880 avoid_deep_ter_for_debug (gsi_stmt (gsi), 0);
5883 /* Make sure all values used by the optimization passes have sane
5884 defaults. */
5885 reg_renumber = 0;
5887 /* Some backends want to know that we are expanding to RTL. */
5888 currently_expanding_to_rtl = 1;
5889 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
5890 free_dominance_info (CDI_DOMINATORS);
5892 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
5894 if (chkp_function_instrumented_p (current_function_decl))
5895 chkp_reset_rtl_bounds ();
5897 insn_locations_init ();
5898 if (!DECL_IS_BUILTIN (current_function_decl))
5900 /* Eventually, all FEs should explicitly set function_start_locus. */
5901 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
5902 set_curr_insn_location
5903 (DECL_SOURCE_LOCATION (current_function_decl));
5904 else
5905 set_curr_insn_location (fun->function_start_locus);
5907 else
5908 set_curr_insn_location (UNKNOWN_LOCATION);
5909 prologue_location = curr_insn_location ();
5911 #ifdef INSN_SCHEDULING
5912 init_sched_attrs ();
5913 #endif
5915 /* Make sure first insn is a note even if we don't want linenums.
5916 This makes sure the first insn will never be deleted.
5917 Also, final expects a note to appear there. */
5918 emit_note (NOTE_INSN_DELETED);
5920 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
5921 discover_nonconstant_array_refs ();
5923 targetm.expand_to_rtl_hook ();
5924 crtl->stack_alignment_needed = STACK_BOUNDARY;
5925 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
5926 crtl->stack_alignment_estimated = 0;
5927 crtl->preferred_stack_boundary = STACK_BOUNDARY;
5928 fun->cfg->max_jumptable_ents = 0;
5930 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
5931 of the function section at exapnsion time to predict distance of calls. */
5932 resolve_unique_section (current_function_decl, 0, flag_function_sections);
5934 /* Expand the variables recorded during gimple lowering. */
5935 timevar_push (TV_VAR_EXPAND);
5936 start_sequence ();
5938 var_ret_seq = expand_used_vars ();
5940 var_seq = get_insns ();
5941 end_sequence ();
5942 timevar_pop (TV_VAR_EXPAND);
5944 /* Honor stack protection warnings. */
5945 if (warn_stack_protect)
5947 if (fun->calls_alloca)
5948 warning (OPT_Wstack_protector,
5949 "stack protector not protecting local variables: "
5950 "variable length buffer");
5951 if (has_short_buffer && !crtl->stack_protect_guard)
5952 warning (OPT_Wstack_protector,
5953 "stack protector not protecting function: "
5954 "all local arrays are less than %d bytes long",
5955 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
5958 /* Set up parameters and prepare for return, for the function. */
5959 expand_function_start (current_function_decl);
5961 /* If we emitted any instructions for setting up the variables,
5962 emit them before the FUNCTION_START note. */
5963 if (var_seq)
5965 emit_insn_before (var_seq, parm_birth_insn);
5967 /* In expand_function_end we'll insert the alloca save/restore
5968 before parm_birth_insn. We've just insertted an alloca call.
5969 Adjust the pointer to match. */
5970 parm_birth_insn = var_seq;
5973 /* Now that we also have the parameter RTXs, copy them over to our
5974 partitions. */
5975 for (i = 0; i < SA.map->num_partitions; i++)
5977 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
5979 if (TREE_CODE (var) != VAR_DECL
5980 && !SA.partition_to_pseudo[i])
5981 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
5982 gcc_assert (SA.partition_to_pseudo[i]);
5984 /* If this decl was marked as living in multiple places, reset
5985 this now to NULL. */
5986 if (DECL_RTL_IF_SET (var) == pc_rtx)
5987 SET_DECL_RTL (var, NULL);
5989 /* Some RTL parts really want to look at DECL_RTL(x) when x
5990 was a decl marked in REG_ATTR or MEM_ATTR. We could use
5991 SET_DECL_RTL here making this available, but that would mean
5992 to select one of the potentially many RTLs for one DECL. Instead
5993 of doing that we simply reset the MEM_EXPR of the RTL in question,
5994 then nobody can get at it and hence nobody can call DECL_RTL on it. */
5995 if (!DECL_RTL_SET_P (var))
5997 if (MEM_P (SA.partition_to_pseudo[i]))
5998 set_mem_expr (SA.partition_to_pseudo[i], NULL);
6002 /* If we have a class containing differently aligned pointers
6003 we need to merge those into the corresponding RTL pointer
6004 alignment. */
6005 for (i = 1; i < num_ssa_names; i++)
6007 tree name = ssa_name (i);
6008 int part;
6009 rtx r;
6011 if (!name
6012 /* We might have generated new SSA names in
6013 update_alias_info_with_stack_vars. They will have a NULL
6014 defining statements, and won't be part of the partitioning,
6015 so ignore those. */
6016 || !SSA_NAME_DEF_STMT (name))
6017 continue;
6018 part = var_to_partition (SA.map, name);
6019 if (part == NO_PARTITION)
6020 continue;
6022 /* Adjust all partition members to get the underlying decl of
6023 the representative which we might have created in expand_one_var. */
6024 if (SSA_NAME_VAR (name) == NULL_TREE)
6026 tree leader = partition_to_var (SA.map, part);
6027 gcc_assert (SSA_NAME_VAR (leader) != NULL_TREE);
6028 replace_ssa_name_symbol (name, SSA_NAME_VAR (leader));
6030 if (!POINTER_TYPE_P (TREE_TYPE (name)))
6031 continue;
6033 r = SA.partition_to_pseudo[part];
6034 if (REG_P (r))
6035 mark_reg_pointer (r, get_pointer_alignment (name));
6038 /* If this function is `main', emit a call to `__main'
6039 to run global initializers, etc. */
6040 if (DECL_NAME (current_function_decl)
6041 && MAIN_NAME_P (DECL_NAME (current_function_decl))
6042 && DECL_FILE_SCOPE_P (current_function_decl))
6043 expand_main_function ();
6045 /* Initialize the stack_protect_guard field. This must happen after the
6046 call to __main (if any) so that the external decl is initialized. */
6047 if (crtl->stack_protect_guard)
6048 stack_protect_prologue ();
6050 expand_phi_nodes (&SA);
6052 /* Register rtl specific functions for cfg. */
6053 rtl_register_cfg_hooks ();
6055 init_block = construct_init_block ();
6057 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
6058 remaining edges later. */
6059 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
6060 e->flags &= ~EDGE_EXECUTABLE;
6062 lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
6063 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
6064 next_bb)
6065 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
6067 if (MAY_HAVE_DEBUG_INSNS)
6068 expand_debug_locations ();
6070 if (deep_ter_debug_map)
6072 delete deep_ter_debug_map;
6073 deep_ter_debug_map = NULL;
6076 /* Free stuff we no longer need after GIMPLE optimizations. */
6077 free_dominance_info (CDI_DOMINATORS);
6078 free_dominance_info (CDI_POST_DOMINATORS);
6079 delete_tree_cfg_annotations ();
6081 timevar_push (TV_OUT_OF_SSA);
6082 finish_out_of_ssa (&SA);
6083 timevar_pop (TV_OUT_OF_SSA);
6085 timevar_push (TV_POST_EXPAND);
6086 /* We are no longer in SSA form. */
6087 fun->gimple_df->in_ssa_p = false;
6088 loops_state_clear (LOOP_CLOSED_SSA);
6090 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
6091 conservatively to true until they are all profile aware. */
6092 delete lab_rtx_for_bb;
6093 free_histograms ();
6095 construct_exit_block ();
6096 insn_locations_finalize ();
6098 if (var_ret_seq)
6100 rtx_insn *after = return_label;
6101 rtx_insn *next = NEXT_INSN (after);
6102 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
6103 after = next;
6104 emit_insn_after (var_ret_seq, after);
6107 /* Zap the tree EH table. */
6108 set_eh_throw_stmt_table (fun, NULL);
6110 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
6111 split edges which edge insertions might do. */
6112 rebuild_jump_labels (get_insns ());
6114 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun),
6115 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6117 edge e;
6118 edge_iterator ei;
6119 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6121 if (e->insns.r)
6123 rebuild_jump_labels_chain (e->insns.r);
6124 /* Put insns after parm birth, but before
6125 NOTE_INSNS_FUNCTION_BEG. */
6126 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (fun)
6127 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
6129 rtx_insn *insns = e->insns.r;
6130 e->insns.r = NULL;
6131 if (NOTE_P (parm_birth_insn)
6132 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
6133 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
6134 else
6135 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
6137 else
6138 commit_one_edge_insertion (e);
6140 else
6141 ei_next (&ei);
6145 /* We're done expanding trees to RTL. */
6146 currently_expanding_to_rtl = 0;
6148 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
6149 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
6151 edge e;
6152 edge_iterator ei;
6153 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
6155 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
6156 e->flags &= ~EDGE_EXECUTABLE;
6158 /* At the moment not all abnormal edges match the RTL
6159 representation. It is safe to remove them here as
6160 find_many_sub_basic_blocks will rediscover them.
6161 In the future we should get this fixed properly. */
6162 if ((e->flags & EDGE_ABNORMAL)
6163 && !(e->flags & EDGE_SIBCALL))
6164 remove_edge (e);
6165 else
6166 ei_next (&ei);
6170 blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
6171 bitmap_ones (blocks);
6172 find_many_sub_basic_blocks (blocks);
6173 sbitmap_free (blocks);
6174 purge_all_dead_edges ();
6176 expand_stack_alignment ();
6178 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
6179 function. */
6180 if (crtl->tail_call_emit)
6181 fixup_tail_calls ();
6183 /* After initial rtl generation, call back to finish generating
6184 exception support code. We need to do this before cleaning up
6185 the CFG as the code does not expect dead landing pads. */
6186 if (fun->eh->region_tree != NULL)
6187 finish_eh_generation ();
6189 /* Remove unreachable blocks, otherwise we cannot compute dominators
6190 which are needed for loop state verification. As a side-effect
6191 this also compacts blocks.
6192 ??? We cannot remove trivially dead insns here as for example
6193 the DRAP reg on i?86 is not magically live at this point.
6194 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
6195 cleanup_cfg (CLEANUP_NO_INSN_DEL);
6197 #ifdef ENABLE_CHECKING
6198 verify_flow_info ();
6199 #endif
6201 /* Initialize pseudos allocated for hard registers. */
6202 emit_initial_value_sets ();
6204 /* And finally unshare all RTL. */
6205 unshare_all_rtl ();
6207 /* There's no need to defer outputting this function any more; we
6208 know we want to output it. */
6209 DECL_DEFER_OUTPUT (current_function_decl) = 0;
6211 /* Now that we're done expanding trees to RTL, we shouldn't have any
6212 more CONCATs anywhere. */
6213 generating_concat_p = 0;
6215 if (dump_file)
6217 fprintf (dump_file,
6218 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
6219 /* And the pass manager will dump RTL for us. */
6222 /* If we're emitting a nested function, make sure its parent gets
6223 emitted as well. Doing otherwise confuses debug info. */
6225 tree parent;
6226 for (parent = DECL_CONTEXT (current_function_decl);
6227 parent != NULL_TREE;
6228 parent = get_containing_scope (parent))
6229 if (TREE_CODE (parent) == FUNCTION_DECL)
6230 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
6233 /* We are now committed to emitting code for this function. Do any
6234 preparation, such as emitting abstract debug info for the inline
6235 before it gets mangled by optimization. */
6236 if (cgraph_function_possibly_inlined_p (current_function_decl))
6237 (*debug_hooks->outlining_inline_function) (current_function_decl);
6239 TREE_ASM_WRITTEN (current_function_decl) = 1;
6241 /* After expanding, the return labels are no longer needed. */
6242 return_label = NULL;
6243 naked_return_label = NULL;
6245 /* After expanding, the tm_restart map is no longer needed. */
6246 if (fun->gimple_df->tm_restart)
6247 fun->gimple_df->tm_restart = NULL;
6249 /* Tag the blocks with a depth number so that change_scope can find
6250 the common parent easily. */
6251 set_block_levels (DECL_INITIAL (fun->decl), 0);
6252 default_rtl_profile ();
6254 timevar_pop (TV_POST_EXPAND);
6256 return 0;
6259 } // anon namespace
6261 rtl_opt_pass *
6262 make_pass_expand (gcc::context *ctxt)
6264 return new pass_expand (ctxt);