* doc/install.texi (Downloading the Source): Update references to
[official-gcc.git] / gcc / cfgexpand.c
blob6af1fa7b921768e26c430afe0d20d9e67c181219
1 /* A pass for lowering trees to RTL.
2 Copyright (C) 2004-2013 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 "tree.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "basic-block.h"
28 #include "function.h"
29 #include "expr.h"
30 #include "langhooks.h"
31 #include "tree-flow.h"
32 #include "tree-pass.h"
33 #include "except.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "gimple-pretty-print.h"
37 #include "toplev.h"
38 #include "debug.h"
39 #include "params.h"
40 #include "tree-inline.h"
41 #include "value-prof.h"
42 #include "target.h"
43 #include "ssaexpand.h"
44 #include "bitmap.h"
45 #include "sbitmap.h"
46 #include "cfgloop.h"
47 #include "regs.h" /* For reg_renumber. */
48 #include "insn-attr.h" /* For INSN_SCHEDULING. */
49 #include "asan.h"
51 /* This variable holds information helping the rewriting of SSA trees
52 into RTL. */
53 struct ssaexpand SA;
55 /* This variable holds the currently expanded gimple statement for purposes
56 of comminucating the profile info to the builtin expanders. */
57 gimple currently_expanding_gimple_stmt;
59 static rtx expand_debug_expr (tree);
61 /* Return an expression tree corresponding to the RHS of GIMPLE
62 statement STMT. */
64 tree
65 gimple_assign_rhs_to_tree (gimple stmt)
67 tree t;
68 enum gimple_rhs_class grhs_class;
70 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
72 if (grhs_class == GIMPLE_TERNARY_RHS)
73 t = build3 (gimple_assign_rhs_code (stmt),
74 TREE_TYPE (gimple_assign_lhs (stmt)),
75 gimple_assign_rhs1 (stmt),
76 gimple_assign_rhs2 (stmt),
77 gimple_assign_rhs3 (stmt));
78 else if (grhs_class == GIMPLE_BINARY_RHS)
79 t = build2 (gimple_assign_rhs_code (stmt),
80 TREE_TYPE (gimple_assign_lhs (stmt)),
81 gimple_assign_rhs1 (stmt),
82 gimple_assign_rhs2 (stmt));
83 else if (grhs_class == GIMPLE_UNARY_RHS)
84 t = build1 (gimple_assign_rhs_code (stmt),
85 TREE_TYPE (gimple_assign_lhs (stmt)),
86 gimple_assign_rhs1 (stmt));
87 else if (grhs_class == GIMPLE_SINGLE_RHS)
89 t = gimple_assign_rhs1 (stmt);
90 /* Avoid modifying this tree in place below. */
91 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
92 && gimple_location (stmt) != EXPR_LOCATION (t))
93 || (gimple_block (stmt)
94 && currently_expanding_to_rtl
95 && EXPR_P (t)))
96 t = copy_node (t);
98 else
99 gcc_unreachable ();
101 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
102 SET_EXPR_LOCATION (t, gimple_location (stmt));
104 return t;
108 #ifndef STACK_ALIGNMENT_NEEDED
109 #define STACK_ALIGNMENT_NEEDED 1
110 #endif
112 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
114 /* Associate declaration T with storage space X. If T is no
115 SSA name this is exactly SET_DECL_RTL, otherwise make the
116 partition of T associated with X. */
117 static inline void
118 set_rtl (tree t, rtx x)
120 if (TREE_CODE (t) == SSA_NAME)
122 SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
123 if (x && !MEM_P (x))
124 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
125 /* For the benefit of debug information at -O0 (where vartracking
126 doesn't run) record the place also in the base DECL if it's
127 a normal variable (not a parameter). */
128 if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
130 tree var = SSA_NAME_VAR (t);
131 /* If we don't yet have something recorded, just record it now. */
132 if (!DECL_RTL_SET_P (var))
133 SET_DECL_RTL (var, x);
134 /* If we have it set already to "multiple places" don't
135 change this. */
136 else if (DECL_RTL (var) == pc_rtx)
138 /* If we have something recorded and it's not the same place
139 as we want to record now, we have multiple partitions for the
140 same base variable, with different places. We can't just
141 randomly chose one, hence we have to say that we don't know.
142 This only happens with optimization, and there var-tracking
143 will figure out the right thing. */
144 else if (DECL_RTL (var) != x)
145 SET_DECL_RTL (var, pc_rtx);
148 else
149 SET_DECL_RTL (t, x);
152 /* This structure holds data relevant to one variable that will be
153 placed in a stack slot. */
154 struct stack_var
156 /* The Variable. */
157 tree decl;
159 /* Initially, the size of the variable. Later, the size of the partition,
160 if this variable becomes it's partition's representative. */
161 HOST_WIDE_INT size;
163 /* The *byte* alignment required for this variable. Or as, with the
164 size, the alignment for this partition. */
165 unsigned int alignb;
167 /* The partition representative. */
168 size_t representative;
170 /* The next stack variable in the partition, or EOC. */
171 size_t next;
173 /* The numbers of conflicting stack variables. */
174 bitmap conflicts;
177 #define EOC ((size_t)-1)
179 /* We have an array of such objects while deciding allocation. */
180 static struct stack_var *stack_vars;
181 static size_t stack_vars_alloc;
182 static size_t stack_vars_num;
183 static struct pointer_map_t *decl_to_stack_part;
185 /* Conflict bitmaps go on this obstack. This allows us to destroy
186 all of them in one big sweep. */
187 static bitmap_obstack stack_var_bitmap_obstack;
189 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
190 is non-decreasing. */
191 static size_t *stack_vars_sorted;
193 /* The phase of the stack frame. This is the known misalignment of
194 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
195 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
196 static int frame_phase;
198 /* Used during expand_used_vars to remember if we saw any decls for
199 which we'd like to enable stack smashing protection. */
200 static bool has_protected_decls;
202 /* Used during expand_used_vars. Remember if we say a character buffer
203 smaller than our cutoff threshold. Used for -Wstack-protector. */
204 static bool has_short_buffer;
206 /* Compute the byte alignment to use for DECL. Ignore alignment
207 we can't do with expected alignment of the stack boundary. */
209 static unsigned int
210 align_local_variable (tree decl)
212 unsigned int align = LOCAL_DECL_ALIGNMENT (decl);
213 DECL_ALIGN (decl) = align;
214 return align / BITS_PER_UNIT;
217 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
218 Return the frame offset. */
220 static HOST_WIDE_INT
221 alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
223 HOST_WIDE_INT offset, new_frame_offset;
225 new_frame_offset = frame_offset;
226 if (FRAME_GROWS_DOWNWARD)
228 new_frame_offset -= size + frame_phase;
229 new_frame_offset &= -align;
230 new_frame_offset += frame_phase;
231 offset = new_frame_offset;
233 else
235 new_frame_offset -= frame_phase;
236 new_frame_offset += align - 1;
237 new_frame_offset &= -align;
238 new_frame_offset += frame_phase;
239 offset = new_frame_offset;
240 new_frame_offset += size;
242 frame_offset = new_frame_offset;
244 if (frame_offset_overflow (frame_offset, cfun->decl))
245 frame_offset = offset = 0;
247 return offset;
250 /* Accumulate DECL into STACK_VARS. */
252 static void
253 add_stack_var (tree decl)
255 struct stack_var *v;
257 if (stack_vars_num >= stack_vars_alloc)
259 if (stack_vars_alloc)
260 stack_vars_alloc = stack_vars_alloc * 3 / 2;
261 else
262 stack_vars_alloc = 32;
263 stack_vars
264 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
266 if (!decl_to_stack_part)
267 decl_to_stack_part = pointer_map_create ();
269 v = &stack_vars[stack_vars_num];
270 * (size_t *)pointer_map_insert (decl_to_stack_part, decl) = stack_vars_num;
272 v->decl = decl;
273 v->size = tree_low_cst (DECL_SIZE_UNIT (SSAVAR (decl)), 1);
274 /* Ensure that all variables have size, so that &a != &b for any two
275 variables that are simultaneously live. */
276 if (v->size == 0)
277 v->size = 1;
278 v->alignb = align_local_variable (SSAVAR (decl));
279 /* An alignment of zero can mightily confuse us later. */
280 gcc_assert (v->alignb != 0);
282 /* All variables are initially in their own partition. */
283 v->representative = stack_vars_num;
284 v->next = EOC;
286 /* All variables initially conflict with no other. */
287 v->conflicts = NULL;
289 /* Ensure that this decl doesn't get put onto the list twice. */
290 set_rtl (decl, pc_rtx);
292 stack_vars_num++;
295 /* Make the decls associated with luid's X and Y conflict. */
297 static void
298 add_stack_var_conflict (size_t x, size_t y)
300 struct stack_var *a = &stack_vars[x];
301 struct stack_var *b = &stack_vars[y];
302 if (!a->conflicts)
303 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
304 if (!b->conflicts)
305 b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
306 bitmap_set_bit (a->conflicts, y);
307 bitmap_set_bit (b->conflicts, x);
310 /* Check whether the decls associated with luid's X and Y conflict. */
312 static bool
313 stack_var_conflict_p (size_t x, size_t y)
315 struct stack_var *a = &stack_vars[x];
316 struct stack_var *b = &stack_vars[y];
317 if (x == y)
318 return false;
319 /* Partitions containing an SSA name result from gimple registers
320 with things like unsupported modes. They are top-level and
321 hence conflict with everything else. */
322 if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
323 return true;
325 if (!a->conflicts || !b->conflicts)
326 return false;
327 return bitmap_bit_p (a->conflicts, y);
330 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
331 enter its partition number into bitmap DATA. */
333 static bool
334 visit_op (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
336 bitmap active = (bitmap)data;
337 op = get_base_address (op);
338 if (op
339 && DECL_P (op)
340 && DECL_RTL_IF_SET (op) == pc_rtx)
342 size_t *v = (size_t *) pointer_map_contains (decl_to_stack_part, op);
343 if (v)
344 bitmap_set_bit (active, *v);
346 return false;
349 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
350 record conflicts between it and all currently active other partitions
351 from bitmap DATA. */
353 static bool
354 visit_conflict (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
356 bitmap active = (bitmap)data;
357 op = get_base_address (op);
358 if (op
359 && DECL_P (op)
360 && DECL_RTL_IF_SET (op) == pc_rtx)
362 size_t *v =
363 (size_t *) pointer_map_contains (decl_to_stack_part, op);
364 if (v && bitmap_set_bit (active, *v))
366 size_t num = *v;
367 bitmap_iterator bi;
368 unsigned i;
369 gcc_assert (num < stack_vars_num);
370 EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
371 add_stack_var_conflict (num, i);
374 return false;
377 /* Helper routine for add_scope_conflicts, calculating the active partitions
378 at the end of BB, leaving the result in WORK. We're called to generate
379 conflicts when FOR_CONFLICT is true, otherwise we're just tracking
380 liveness. */
382 static void
383 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
385 edge e;
386 edge_iterator ei;
387 gimple_stmt_iterator gsi;
388 bool (*visit)(gimple, tree, void *);
390 bitmap_clear (work);
391 FOR_EACH_EDGE (e, ei, bb->preds)
392 bitmap_ior_into (work, (bitmap)e->src->aux);
394 visit = visit_op;
396 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
398 gimple stmt = gsi_stmt (gsi);
399 walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
401 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
403 gimple stmt = gsi_stmt (gsi);
405 if (gimple_clobber_p (stmt))
407 tree lhs = gimple_assign_lhs (stmt);
408 size_t *v;
409 /* Nested function lowering might introduce LHSs
410 that are COMPONENT_REFs. */
411 if (TREE_CODE (lhs) != VAR_DECL)
412 continue;
413 if (DECL_RTL_IF_SET (lhs) == pc_rtx
414 && (v = (size_t *)
415 pointer_map_contains (decl_to_stack_part, lhs)))
416 bitmap_clear_bit (work, *v);
418 else if (!is_gimple_debug (stmt))
420 if (for_conflict
421 && visit == visit_op)
423 /* If this is the first real instruction in this BB we need
424 to add conflicts for everything live at this point now.
425 Unlike classical liveness for named objects we can't
426 rely on seeing a def/use of the names we're interested in.
427 There might merely be indirect loads/stores. We'd not add any
428 conflicts for such partitions. */
429 bitmap_iterator bi;
430 unsigned i;
431 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
433 struct stack_var *a = &stack_vars[i];
434 if (!a->conflicts)
435 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
436 bitmap_ior_into (a->conflicts, work);
438 visit = visit_conflict;
440 walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
445 /* Generate stack partition conflicts between all partitions that are
446 simultaneously live. */
448 static void
449 add_scope_conflicts (void)
451 basic_block bb;
452 bool changed;
453 bitmap work = BITMAP_ALLOC (NULL);
454 int *rpo;
455 int n_bbs;
457 /* We approximate the live range of a stack variable by taking the first
458 mention of its name as starting point(s), and by the end-of-scope
459 death clobber added by gimplify as ending point(s) of the range.
460 This overapproximates in the case we for instance moved an address-taken
461 operation upward, without also moving a dereference to it upwards.
462 But it's conservatively correct as a variable never can hold values
463 before its name is mentioned at least once.
465 We then do a mostly classical bitmap liveness algorithm. */
467 FOR_ALL_BB (bb)
468 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
470 rpo = XNEWVEC (int, last_basic_block);
471 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
473 changed = true;
474 while (changed)
476 int i;
477 changed = false;
478 for (i = 0; i < n_bbs; i++)
480 bitmap active;
481 bb = BASIC_BLOCK (rpo[i]);
482 active = (bitmap)bb->aux;
483 add_scope_conflicts_1 (bb, work, false);
484 if (bitmap_ior_into (active, work))
485 changed = true;
489 FOR_EACH_BB (bb)
490 add_scope_conflicts_1 (bb, work, true);
492 free (rpo);
493 BITMAP_FREE (work);
494 FOR_ALL_BB (bb)
495 BITMAP_FREE (bb->aux);
498 /* A subroutine of partition_stack_vars. A comparison function for qsort,
499 sorting an array of indices by the properties of the object. */
501 static int
502 stack_var_cmp (const void *a, const void *b)
504 size_t ia = *(const size_t *)a;
505 size_t ib = *(const size_t *)b;
506 unsigned int aligna = stack_vars[ia].alignb;
507 unsigned int alignb = stack_vars[ib].alignb;
508 HOST_WIDE_INT sizea = stack_vars[ia].size;
509 HOST_WIDE_INT sizeb = stack_vars[ib].size;
510 tree decla = stack_vars[ia].decl;
511 tree declb = stack_vars[ib].decl;
512 bool largea, largeb;
513 unsigned int uida, uidb;
515 /* Primary compare on "large" alignment. Large comes first. */
516 largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
517 largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
518 if (largea != largeb)
519 return (int)largeb - (int)largea;
521 /* Secondary compare on size, decreasing */
522 if (sizea > sizeb)
523 return -1;
524 if (sizea < sizeb)
525 return 1;
527 /* Tertiary compare on true alignment, decreasing. */
528 if (aligna < alignb)
529 return -1;
530 if (aligna > alignb)
531 return 1;
533 /* Final compare on ID for sort stability, increasing.
534 Two SSA names are compared by their version, SSA names come before
535 non-SSA names, and two normal decls are compared by their DECL_UID. */
536 if (TREE_CODE (decla) == SSA_NAME)
538 if (TREE_CODE (declb) == SSA_NAME)
539 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
540 else
541 return -1;
543 else if (TREE_CODE (declb) == SSA_NAME)
544 return 1;
545 else
546 uida = DECL_UID (decla), uidb = DECL_UID (declb);
547 if (uida < uidb)
548 return 1;
549 if (uida > uidb)
550 return -1;
551 return 0;
555 /* If the points-to solution *PI points to variables that are in a partition
556 together with other variables add all partition members to the pointed-to
557 variables bitmap. */
559 static void
560 add_partitioned_vars_to_ptset (struct pt_solution *pt,
561 struct pointer_map_t *decls_to_partitions,
562 struct pointer_set_t *visited, bitmap temp)
564 bitmap_iterator bi;
565 unsigned i;
566 bitmap *part;
568 if (pt->anything
569 || pt->vars == NULL
570 /* The pointed-to vars bitmap is shared, it is enough to
571 visit it once. */
572 || pointer_set_insert(visited, pt->vars))
573 return;
575 bitmap_clear (temp);
577 /* By using a temporary bitmap to store all members of the partitions
578 we have to add we make sure to visit each of the partitions only
579 once. */
580 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
581 if ((!temp
582 || !bitmap_bit_p (temp, i))
583 && (part = (bitmap *) pointer_map_contains (decls_to_partitions,
584 (void *)(size_t) i)))
585 bitmap_ior_into (temp, *part);
586 if (!bitmap_empty_p (temp))
587 bitmap_ior_into (pt->vars, temp);
590 /* Update points-to sets based on partition info, so we can use them on RTL.
591 The bitmaps representing stack partitions will be saved until expand,
592 where partitioned decls used as bases in memory expressions will be
593 rewritten. */
595 static void
596 update_alias_info_with_stack_vars (void)
598 struct pointer_map_t *decls_to_partitions = NULL;
599 size_t i, j;
600 tree var = NULL_TREE;
602 for (i = 0; i < stack_vars_num; i++)
604 bitmap part = NULL;
605 tree name;
606 struct ptr_info_def *pi;
608 /* Not interested in partitions with single variable. */
609 if (stack_vars[i].representative != i
610 || stack_vars[i].next == EOC)
611 continue;
613 if (!decls_to_partitions)
615 decls_to_partitions = pointer_map_create ();
616 cfun->gimple_df->decls_to_pointers = pointer_map_create ();
619 /* Create an SSA_NAME that points to the partition for use
620 as base during alias-oracle queries on RTL for bases that
621 have been partitioned. */
622 if (var == NULL_TREE)
623 var = create_tmp_var (ptr_type_node, NULL);
624 name = make_ssa_name (var, NULL);
626 /* Create bitmaps representing partitions. They will be used for
627 points-to sets later, so use GGC alloc. */
628 part = BITMAP_GGC_ALLOC ();
629 for (j = i; j != EOC; j = stack_vars[j].next)
631 tree decl = stack_vars[j].decl;
632 unsigned int uid = DECL_PT_UID (decl);
633 bitmap_set_bit (part, uid);
634 *((bitmap *) pointer_map_insert (decls_to_partitions,
635 (void *)(size_t) uid)) = part;
636 *((tree *) pointer_map_insert (cfun->gimple_df->decls_to_pointers,
637 decl)) = name;
638 if (TREE_ADDRESSABLE (decl))
639 TREE_ADDRESSABLE (name) = 1;
642 /* Make the SSA name point to all partition members. */
643 pi = get_ptr_info (name);
644 pt_solution_set (&pi->pt, part, false);
647 /* Make all points-to sets that contain one member of a partition
648 contain all members of the partition. */
649 if (decls_to_partitions)
651 unsigned i;
652 struct pointer_set_t *visited = pointer_set_create ();
653 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
655 for (i = 1; i < num_ssa_names; i++)
657 tree name = ssa_name (i);
658 struct ptr_info_def *pi;
660 if (name
661 && POINTER_TYPE_P (TREE_TYPE (name))
662 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
663 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
664 visited, temp);
667 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
668 decls_to_partitions, visited, temp);
670 pointer_set_destroy (visited);
671 pointer_map_destroy (decls_to_partitions);
672 BITMAP_FREE (temp);
676 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
677 partitioning algorithm. Partitions A and B are known to be non-conflicting.
678 Merge them into a single partition A. */
680 static void
681 union_stack_vars (size_t a, size_t b)
683 struct stack_var *vb = &stack_vars[b];
684 bitmap_iterator bi;
685 unsigned u;
687 gcc_assert (stack_vars[b].next == EOC);
688 /* Add B to A's partition. */
689 stack_vars[b].next = stack_vars[a].next;
690 stack_vars[b].representative = a;
691 stack_vars[a].next = b;
693 /* Update the required alignment of partition A to account for B. */
694 if (stack_vars[a].alignb < stack_vars[b].alignb)
695 stack_vars[a].alignb = stack_vars[b].alignb;
697 /* Update the interference graph and merge the conflicts. */
698 if (vb->conflicts)
700 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
701 add_stack_var_conflict (a, stack_vars[u].representative);
702 BITMAP_FREE (vb->conflicts);
706 /* A subroutine of expand_used_vars. Binpack the variables into
707 partitions constrained by the interference graph. The overall
708 algorithm used is as follows:
710 Sort the objects by size in descending order.
711 For each object A {
712 S = size(A)
713 O = 0
714 loop {
715 Look for the largest non-conflicting object B with size <= S.
716 UNION (A, B)
721 static void
722 partition_stack_vars (void)
724 size_t si, sj, n = stack_vars_num;
726 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
727 for (si = 0; si < n; ++si)
728 stack_vars_sorted[si] = si;
730 if (n == 1)
731 return;
733 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
735 for (si = 0; si < n; ++si)
737 size_t i = stack_vars_sorted[si];
738 unsigned int ialign = stack_vars[i].alignb;
739 HOST_WIDE_INT isize = stack_vars[i].size;
741 /* Ignore objects that aren't partition representatives. If we
742 see a var that is not a partition representative, it must
743 have been merged earlier. */
744 if (stack_vars[i].representative != i)
745 continue;
747 for (sj = si + 1; sj < n; ++sj)
749 size_t j = stack_vars_sorted[sj];
750 unsigned int jalign = stack_vars[j].alignb;
751 HOST_WIDE_INT jsize = stack_vars[j].size;
753 /* Ignore objects that aren't partition representatives. */
754 if (stack_vars[j].representative != j)
755 continue;
757 /* Do not mix objects of "small" (supported) alignment
758 and "large" (unsupported) alignment. */
759 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
760 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
761 break;
763 /* For Address Sanitizer do not mix objects with different
764 sizes, as the shorter vars wouldn't be adequately protected.
765 Don't do that for "large" (unsupported) alignment objects,
766 those aren't protected anyway. */
767 if (flag_asan && isize != jsize
768 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
769 break;
771 /* Ignore conflicting objects. */
772 if (stack_var_conflict_p (i, j))
773 continue;
775 /* UNION the objects, placing J at OFFSET. */
776 union_stack_vars (i, j);
780 update_alias_info_with_stack_vars ();
783 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
785 static void
786 dump_stack_var_partition (void)
788 size_t si, i, j, n = stack_vars_num;
790 for (si = 0; si < n; ++si)
792 i = stack_vars_sorted[si];
794 /* Skip variables that aren't partition representatives, for now. */
795 if (stack_vars[i].representative != i)
796 continue;
798 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
799 " align %u\n", (unsigned long) i, stack_vars[i].size,
800 stack_vars[i].alignb);
802 for (j = i; j != EOC; j = stack_vars[j].next)
804 fputc ('\t', dump_file);
805 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
807 fputc ('\n', dump_file);
811 /* Assign rtl to DECL at BASE + OFFSET. */
813 static void
814 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
815 HOST_WIDE_INT offset)
817 unsigned align;
818 rtx x;
820 /* If this fails, we've overflowed the stack frame. Error nicely? */
821 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
823 x = plus_constant (Pmode, base, offset);
824 x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
826 if (TREE_CODE (decl) != SSA_NAME)
828 /* Set alignment we actually gave this decl if it isn't an SSA name.
829 If it is we generate stack slots only accidentally so it isn't as
830 important, we'll simply use the alignment that is already set. */
831 if (base == virtual_stack_vars_rtx)
832 offset -= frame_phase;
833 align = offset & -offset;
834 align *= BITS_PER_UNIT;
835 if (align == 0 || align > base_align)
836 align = base_align;
838 /* One would think that we could assert that we're not decreasing
839 alignment here, but (at least) the i386 port does exactly this
840 via the MINIMUM_ALIGNMENT hook. */
842 DECL_ALIGN (decl) = align;
843 DECL_USER_ALIGN (decl) = 0;
846 set_mem_attributes (x, SSAVAR (decl), true);
847 set_rtl (decl, x);
850 struct stack_vars_data
852 /* Vector of offset pairs, always end of some padding followed
853 by start of the padding that needs Address Sanitizer protection.
854 The vector is in reversed, highest offset pairs come first. */
855 vec<HOST_WIDE_INT> asan_vec;
857 /* Vector of partition representative decls in between the paddings. */
858 vec<tree> asan_decl_vec;
861 /* A subroutine of expand_used_vars. Give each partition representative
862 a unique location within the stack frame. Update each partition member
863 with that location. */
865 static void
866 expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
868 size_t si, i, j, n = stack_vars_num;
869 HOST_WIDE_INT large_size = 0, large_alloc = 0;
870 rtx large_base = NULL;
871 unsigned large_align = 0;
872 tree decl;
874 /* Determine if there are any variables requiring "large" alignment.
875 Since these are dynamically allocated, we only process these if
876 no predicate involved. */
877 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
878 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
880 /* Find the total size of these variables. */
881 for (si = 0; si < n; ++si)
883 unsigned alignb;
885 i = stack_vars_sorted[si];
886 alignb = stack_vars[i].alignb;
888 /* Stop when we get to the first decl with "small" alignment. */
889 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
890 break;
892 /* Skip variables that aren't partition representatives. */
893 if (stack_vars[i].representative != i)
894 continue;
896 /* Skip variables that have already had rtl assigned. See also
897 add_stack_var where we perpetrate this pc_rtx hack. */
898 decl = stack_vars[i].decl;
899 if ((TREE_CODE (decl) == SSA_NAME
900 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
901 : DECL_RTL (decl)) != pc_rtx)
902 continue;
904 large_size += alignb - 1;
905 large_size &= -(HOST_WIDE_INT)alignb;
906 large_size += stack_vars[i].size;
909 /* If there were any, allocate space. */
910 if (large_size > 0)
911 large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
912 large_align, true);
915 for (si = 0; si < n; ++si)
917 rtx base;
918 unsigned base_align, alignb;
919 HOST_WIDE_INT offset;
921 i = stack_vars_sorted[si];
923 /* Skip variables that aren't partition representatives, for now. */
924 if (stack_vars[i].representative != i)
925 continue;
927 /* Skip variables that have already had rtl assigned. See also
928 add_stack_var where we perpetrate this pc_rtx hack. */
929 decl = stack_vars[i].decl;
930 if ((TREE_CODE (decl) == SSA_NAME
931 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
932 : DECL_RTL (decl)) != pc_rtx)
933 continue;
935 /* Check the predicate to see whether this variable should be
936 allocated in this pass. */
937 if (pred && !pred (i))
938 continue;
940 alignb = stack_vars[i].alignb;
941 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
943 if (flag_asan && pred)
945 HOST_WIDE_INT prev_offset = frame_offset;
946 tree repr_decl = NULL_TREE;
948 offset
949 = alloc_stack_frame_space (stack_vars[i].size
950 + ASAN_RED_ZONE_SIZE,
951 MAX (alignb, ASAN_RED_ZONE_SIZE));
952 data->asan_vec.safe_push (prev_offset);
953 data->asan_vec.safe_push (offset + stack_vars[i].size);
954 /* Find best representative of the partition.
955 Prefer those with DECL_NAME, even better
956 satisfying asan_protect_stack_decl predicate. */
957 for (j = i; j != EOC; j = stack_vars[j].next)
958 if (asan_protect_stack_decl (stack_vars[j].decl)
959 && DECL_NAME (stack_vars[j].decl))
961 repr_decl = stack_vars[j].decl;
962 break;
964 else if (repr_decl == NULL_TREE
965 && DECL_P (stack_vars[j].decl)
966 && DECL_NAME (stack_vars[j].decl))
967 repr_decl = stack_vars[j].decl;
968 if (repr_decl == NULL_TREE)
969 repr_decl = stack_vars[i].decl;
970 data->asan_decl_vec.safe_push (repr_decl);
972 else
973 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
974 base = virtual_stack_vars_rtx;
975 base_align = crtl->max_used_stack_slot_alignment;
977 else
979 /* Large alignment is only processed in the last pass. */
980 if (pred)
981 continue;
982 gcc_assert (large_base != NULL);
984 large_alloc += alignb - 1;
985 large_alloc &= -(HOST_WIDE_INT)alignb;
986 offset = large_alloc;
987 large_alloc += stack_vars[i].size;
989 base = large_base;
990 base_align = large_align;
993 /* Create rtl for each variable based on their location within the
994 partition. */
995 for (j = i; j != EOC; j = stack_vars[j].next)
997 expand_one_stack_var_at (stack_vars[j].decl,
998 base, base_align,
999 offset);
1003 gcc_assert (large_alloc == large_size);
1006 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1007 static HOST_WIDE_INT
1008 account_stack_vars (void)
1010 size_t si, j, i, n = stack_vars_num;
1011 HOST_WIDE_INT size = 0;
1013 for (si = 0; si < n; ++si)
1015 i = stack_vars_sorted[si];
1017 /* Skip variables that aren't partition representatives, for now. */
1018 if (stack_vars[i].representative != i)
1019 continue;
1021 size += stack_vars[i].size;
1022 for (j = i; j != EOC; j = stack_vars[j].next)
1023 set_rtl (stack_vars[j].decl, NULL);
1025 return size;
1028 /* A subroutine of expand_one_var. Called to immediately assign rtl
1029 to a variable to be allocated in the stack frame. */
1031 static void
1032 expand_one_stack_var (tree var)
1034 HOST_WIDE_INT size, offset;
1035 unsigned byte_align;
1037 size = tree_low_cst (DECL_SIZE_UNIT (SSAVAR (var)), 1);
1038 byte_align = align_local_variable (SSAVAR (var));
1040 /* We handle highly aligned variables in expand_stack_vars. */
1041 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1043 offset = alloc_stack_frame_space (size, byte_align);
1045 expand_one_stack_var_at (var, virtual_stack_vars_rtx,
1046 crtl->max_used_stack_slot_alignment, offset);
1049 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1050 that will reside in a hard register. */
1052 static void
1053 expand_one_hard_reg_var (tree var)
1055 rest_of_decl_compilation (var, 0, 0);
1058 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1059 that will reside in a pseudo register. */
1061 static void
1062 expand_one_register_var (tree var)
1064 tree decl = SSAVAR (var);
1065 tree type = TREE_TYPE (decl);
1066 enum machine_mode reg_mode = promote_decl_mode (decl, NULL);
1067 rtx x = gen_reg_rtx (reg_mode);
1069 set_rtl (var, x);
1071 /* Note if the object is a user variable. */
1072 if (!DECL_ARTIFICIAL (decl))
1073 mark_user_reg (x);
1075 if (POINTER_TYPE_P (type))
1076 mark_reg_pointer (x, get_pointer_alignment (var));
1079 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1080 has some associated error, e.g. its type is error-mark. We just need
1081 to pick something that won't crash the rest of the compiler. */
1083 static void
1084 expand_one_error_var (tree var)
1086 enum machine_mode mode = DECL_MODE (var);
1087 rtx x;
1089 if (mode == BLKmode)
1090 x = gen_rtx_MEM (BLKmode, const0_rtx);
1091 else if (mode == VOIDmode)
1092 x = const0_rtx;
1093 else
1094 x = gen_reg_rtx (mode);
1096 SET_DECL_RTL (var, x);
1099 /* A subroutine of expand_one_var. VAR is a variable that will be
1100 allocated to the local stack frame. Return true if we wish to
1101 add VAR to STACK_VARS so that it will be coalesced with other
1102 variables. Return false to allocate VAR immediately.
1104 This function is used to reduce the number of variables considered
1105 for coalescing, which reduces the size of the quadratic problem. */
1107 static bool
1108 defer_stack_allocation (tree var, bool toplevel)
1110 /* If stack protection is enabled, *all* stack variables must be deferred,
1111 so that we can re-order the strings to the top of the frame.
1112 Similarly for Address Sanitizer. */
1113 if (flag_stack_protect || flag_asan)
1114 return true;
1116 /* We handle "large" alignment via dynamic allocation. We want to handle
1117 this extra complication in only one place, so defer them. */
1118 if (DECL_ALIGN (var) > MAX_SUPPORTED_STACK_ALIGNMENT)
1119 return true;
1121 /* Variables in the outermost scope automatically conflict with
1122 every other variable. The only reason to want to defer them
1123 at all is that, after sorting, we can more efficiently pack
1124 small variables in the stack frame. Continue to defer at -O2. */
1125 if (toplevel && optimize < 2)
1126 return false;
1128 /* Without optimization, *most* variables are allocated from the
1129 stack, which makes the quadratic problem large exactly when we
1130 want compilation to proceed as quickly as possible. On the
1131 other hand, we don't want the function's stack frame size to
1132 get completely out of hand. So we avoid adding scalars and
1133 "small" aggregates to the list at all. */
1134 if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32)
1135 return false;
1137 return true;
1140 /* A subroutine of expand_used_vars. Expand one variable according to
1141 its flavor. Variables to be placed on the stack are not actually
1142 expanded yet, merely recorded.
1143 When REALLY_EXPAND is false, only add stack values to be allocated.
1144 Return stack usage this variable is supposed to take.
1147 static HOST_WIDE_INT
1148 expand_one_var (tree var, bool toplevel, bool really_expand)
1150 unsigned int align = BITS_PER_UNIT;
1151 tree origvar = var;
1153 var = SSAVAR (var);
1155 if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
1157 /* Because we don't know if VAR will be in register or on stack,
1158 we conservatively assume it will be on stack even if VAR is
1159 eventually put into register after RA pass. For non-automatic
1160 variables, which won't be on stack, we collect alignment of
1161 type and ignore user specified alignment. */
1162 if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1163 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1164 TYPE_MODE (TREE_TYPE (var)),
1165 TYPE_ALIGN (TREE_TYPE (var)));
1166 else if (DECL_HAS_VALUE_EXPR_P (var)
1167 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1168 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1169 or variables which were assigned a stack slot already by
1170 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1171 changed from the offset chosen to it. */
1172 align = crtl->stack_alignment_estimated;
1173 else
1174 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1176 /* If the variable alignment is very large we'll dynamicaly allocate
1177 it, which means that in-frame portion is just a pointer. */
1178 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1179 align = POINTER_SIZE;
1182 if (SUPPORTS_STACK_ALIGNMENT
1183 && crtl->stack_alignment_estimated < align)
1185 /* stack_alignment_estimated shouldn't change after stack
1186 realign decision made */
1187 gcc_assert(!crtl->stack_realign_processed);
1188 crtl->stack_alignment_estimated = align;
1191 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1192 So here we only make sure stack_alignment_needed >= align. */
1193 if (crtl->stack_alignment_needed < align)
1194 crtl->stack_alignment_needed = align;
1195 if (crtl->max_used_stack_slot_alignment < align)
1196 crtl->max_used_stack_slot_alignment = align;
1198 if (TREE_CODE (origvar) == SSA_NAME)
1200 gcc_assert (TREE_CODE (var) != VAR_DECL
1201 || (!DECL_EXTERNAL (var)
1202 && !DECL_HAS_VALUE_EXPR_P (var)
1203 && !TREE_STATIC (var)
1204 && TREE_TYPE (var) != error_mark_node
1205 && !DECL_HARD_REGISTER (var)
1206 && really_expand));
1208 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1210 else if (DECL_EXTERNAL (var))
1212 else if (DECL_HAS_VALUE_EXPR_P (var))
1214 else if (TREE_STATIC (var))
1216 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1218 else if (TREE_TYPE (var) == error_mark_node)
1220 if (really_expand)
1221 expand_one_error_var (var);
1223 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1225 if (really_expand)
1226 expand_one_hard_reg_var (var);
1228 else if (use_register_for_decl (var))
1230 if (really_expand)
1231 expand_one_register_var (origvar);
1233 else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
1235 /* Reject variables which cover more than half of the address-space. */
1236 if (really_expand)
1238 error ("size of variable %q+D is too large", var);
1239 expand_one_error_var (var);
1242 else if (defer_stack_allocation (var, toplevel))
1243 add_stack_var (origvar);
1244 else
1246 if (really_expand)
1247 expand_one_stack_var (origvar);
1248 return tree_low_cst (DECL_SIZE_UNIT (var), 1);
1250 return 0;
1253 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1254 expanding variables. Those variables that can be put into registers
1255 are allocated pseudos; those that can't are put on the stack.
1257 TOPLEVEL is true if this is the outermost BLOCK. */
1259 static void
1260 expand_used_vars_for_block (tree block, bool toplevel)
1262 tree t;
1264 /* Expand all variables at this level. */
1265 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1266 if (TREE_USED (t)
1267 && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1268 || !DECL_NONSHAREABLE (t)))
1269 expand_one_var (t, toplevel, true);
1271 /* Expand all variables at containing levels. */
1272 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1273 expand_used_vars_for_block (t, false);
1276 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1277 and clear TREE_USED on all local variables. */
1279 static void
1280 clear_tree_used (tree block)
1282 tree t;
1284 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1285 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1286 if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1287 || !DECL_NONSHAREABLE (t))
1288 TREE_USED (t) = 0;
1290 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1291 clear_tree_used (t);
1294 /* Examine TYPE and determine a bit mask of the following features. */
1296 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1297 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1298 #define SPCT_HAS_ARRAY 4
1299 #define SPCT_HAS_AGGREGATE 8
1301 static unsigned int
1302 stack_protect_classify_type (tree type)
1304 unsigned int ret = 0;
1305 tree t;
1307 switch (TREE_CODE (type))
1309 case ARRAY_TYPE:
1310 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1311 if (t == char_type_node
1312 || t == signed_char_type_node
1313 || t == unsigned_char_type_node)
1315 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1316 unsigned HOST_WIDE_INT len;
1318 if (!TYPE_SIZE_UNIT (type)
1319 || !host_integerp (TYPE_SIZE_UNIT (type), 1))
1320 len = max;
1321 else
1322 len = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
1324 if (len < max)
1325 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1326 else
1327 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1329 else
1330 ret = SPCT_HAS_ARRAY;
1331 break;
1333 case UNION_TYPE:
1334 case QUAL_UNION_TYPE:
1335 case RECORD_TYPE:
1336 ret = SPCT_HAS_AGGREGATE;
1337 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1338 if (TREE_CODE (t) == FIELD_DECL)
1339 ret |= stack_protect_classify_type (TREE_TYPE (t));
1340 break;
1342 default:
1343 break;
1346 return ret;
1349 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1350 part of the local stack frame. Remember if we ever return nonzero for
1351 any variable in this function. The return value is the phase number in
1352 which the variable should be allocated. */
1354 static int
1355 stack_protect_decl_phase (tree decl)
1357 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1358 int ret = 0;
1360 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1361 has_short_buffer = true;
1363 if (flag_stack_protect == 2)
1365 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1366 && !(bits & SPCT_HAS_AGGREGATE))
1367 ret = 1;
1368 else if (bits & SPCT_HAS_ARRAY)
1369 ret = 2;
1371 else
1372 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1374 if (ret)
1375 has_protected_decls = true;
1377 return ret;
1380 /* Two helper routines that check for phase 1 and phase 2. These are used
1381 as callbacks for expand_stack_vars. */
1383 static bool
1384 stack_protect_decl_phase_1 (size_t i)
1386 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
1389 static bool
1390 stack_protect_decl_phase_2 (size_t i)
1392 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
1395 /* And helper function that checks for asan phase (with stack protector
1396 it is phase 3). This is used as callback for expand_stack_vars.
1397 Returns true if any of the vars in the partition need to be protected. */
1399 static bool
1400 asan_decl_phase_3 (size_t i)
1402 while (i != EOC)
1404 if (asan_protect_stack_decl (stack_vars[i].decl))
1405 return true;
1406 i = stack_vars[i].next;
1408 return false;
1411 /* Ensure that variables in different stack protection phases conflict
1412 so that they are not merged and share the same stack slot. */
1414 static void
1415 add_stack_protection_conflicts (void)
1417 size_t i, j, n = stack_vars_num;
1418 unsigned char *phase;
1420 phase = XNEWVEC (unsigned char, n);
1421 for (i = 0; i < n; ++i)
1422 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1424 for (i = 0; i < n; ++i)
1426 unsigned char ph_i = phase[i];
1427 for (j = i + 1; j < n; ++j)
1428 if (ph_i != phase[j])
1429 add_stack_var_conflict (i, j);
1432 XDELETEVEC (phase);
1435 /* Create a decl for the guard at the top of the stack frame. */
1437 static void
1438 create_stack_guard (void)
1440 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1441 VAR_DECL, NULL, ptr_type_node);
1442 TREE_THIS_VOLATILE (guard) = 1;
1443 TREE_USED (guard) = 1;
1444 expand_one_stack_var (guard);
1445 crtl->stack_protect_guard = guard;
1448 /* Prepare for expanding variables. */
1449 static void
1450 init_vars_expansion (void)
1452 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
1453 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
1455 /* A map from decl to stack partition. */
1456 decl_to_stack_part = pointer_map_create ();
1458 /* Initialize local stack smashing state. */
1459 has_protected_decls = false;
1460 has_short_buffer = false;
1463 /* Free up stack variable graph data. */
1464 static void
1465 fini_vars_expansion (void)
1467 bitmap_obstack_release (&stack_var_bitmap_obstack);
1468 if (stack_vars)
1469 XDELETEVEC (stack_vars);
1470 if (stack_vars_sorted)
1471 XDELETEVEC (stack_vars_sorted);
1472 stack_vars = NULL;
1473 stack_vars_sorted = NULL;
1474 stack_vars_alloc = stack_vars_num = 0;
1475 pointer_map_destroy (decl_to_stack_part);
1476 decl_to_stack_part = NULL;
1479 /* Make a fair guess for the size of the stack frame of the function
1480 in NODE. This doesn't have to be exact, the result is only used in
1481 the inline heuristics. So we don't want to run the full stack var
1482 packing algorithm (which is quadratic in the number of stack vars).
1483 Instead, we calculate the total size of all stack vars. This turns
1484 out to be a pretty fair estimate -- packing of stack vars doesn't
1485 happen very often. */
1487 HOST_WIDE_INT
1488 estimated_stack_frame_size (struct cgraph_node *node)
1490 HOST_WIDE_INT size = 0;
1491 size_t i;
1492 tree var;
1493 struct function *fn = DECL_STRUCT_FUNCTION (node->symbol.decl);
1495 push_cfun (fn);
1497 init_vars_expansion ();
1499 FOR_EACH_LOCAL_DECL (fn, i, var)
1500 if (auto_var_in_fn_p (var, fn->decl))
1501 size += expand_one_var (var, true, false);
1503 if (stack_vars_num > 0)
1505 /* Fake sorting the stack vars for account_stack_vars (). */
1506 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1507 for (i = 0; i < stack_vars_num; ++i)
1508 stack_vars_sorted[i] = i;
1509 size += account_stack_vars ();
1512 fini_vars_expansion ();
1513 pop_cfun ();
1514 return size;
1517 /* Expand all variables used in the function. */
1519 static rtx
1520 expand_used_vars (void)
1522 tree var, outer_block = DECL_INITIAL (current_function_decl);
1523 vec<tree> maybe_local_decls = vNULL;
1524 rtx var_end_seq = NULL_RTX;
1525 struct pointer_map_t *ssa_name_decls;
1526 unsigned i;
1527 unsigned len;
1529 /* Compute the phase of the stack frame for this function. */
1531 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1532 int off = STARTING_FRAME_OFFSET % align;
1533 frame_phase = off ? align - off : 0;
1536 /* Set TREE_USED on all variables in the local_decls. */
1537 FOR_EACH_LOCAL_DECL (cfun, i, var)
1538 TREE_USED (var) = 1;
1539 /* Clear TREE_USED on all variables associated with a block scope. */
1540 clear_tree_used (DECL_INITIAL (current_function_decl));
1542 init_vars_expansion ();
1544 ssa_name_decls = pointer_map_create ();
1545 for (i = 0; i < SA.map->num_partitions; i++)
1547 tree var = partition_to_var (SA.map, i);
1549 gcc_assert (!virtual_operand_p (var));
1551 /* Assign decls to each SSA name partition, share decls for partitions
1552 we could have coalesced (those with the same type). */
1553 if (SSA_NAME_VAR (var) == NULL_TREE)
1555 void **slot = pointer_map_insert (ssa_name_decls, TREE_TYPE (var));
1556 if (!*slot)
1557 *slot = (void *) create_tmp_reg (TREE_TYPE (var), NULL);
1558 replace_ssa_name_symbol (var, (tree) *slot);
1561 if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1562 expand_one_var (var, true, true);
1563 else
1565 /* This is a PARM_DECL or RESULT_DECL. For those partitions that
1566 contain the default def (representing the parm or result itself)
1567 we don't do anything here. But those which don't contain the
1568 default def (representing a temporary based on the parm/result)
1569 we need to allocate space just like for normal VAR_DECLs. */
1570 if (!bitmap_bit_p (SA.partition_has_default_def, i))
1572 expand_one_var (var, true, true);
1573 gcc_assert (SA.partition_to_pseudo[i]);
1577 pointer_map_destroy (ssa_name_decls);
1579 /* At this point all variables on the local_decls with TREE_USED
1580 set are not associated with any block scope. Lay them out. */
1582 len = vec_safe_length (cfun->local_decls);
1583 FOR_EACH_LOCAL_DECL (cfun, i, var)
1585 bool expand_now = false;
1587 /* Expanded above already. */
1588 if (is_gimple_reg (var))
1590 TREE_USED (var) = 0;
1591 goto next;
1593 /* We didn't set a block for static or extern because it's hard
1594 to tell the difference between a global variable (re)declared
1595 in a local scope, and one that's really declared there to
1596 begin with. And it doesn't really matter much, since we're
1597 not giving them stack space. Expand them now. */
1598 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1599 expand_now = true;
1601 /* If the variable is not associated with any block, then it
1602 was created by the optimizers, and could be live anywhere
1603 in the function. */
1604 else if (TREE_USED (var))
1605 expand_now = true;
1607 /* Finally, mark all variables on the list as used. We'll use
1608 this in a moment when we expand those associated with scopes. */
1609 TREE_USED (var) = 1;
1611 if (expand_now)
1612 expand_one_var (var, true, true);
1614 next:
1615 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1617 rtx rtl = DECL_RTL_IF_SET (var);
1619 /* Keep artificial non-ignored vars in cfun->local_decls
1620 chain until instantiate_decls. */
1621 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1622 add_local_decl (cfun, var);
1623 else if (rtl == NULL_RTX)
1624 /* If rtl isn't set yet, which can happen e.g. with
1625 -fstack-protector, retry before returning from this
1626 function. */
1627 maybe_local_decls.safe_push (var);
1631 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
1633 +-----------------+-----------------+
1634 | ...processed... | ...duplicates...|
1635 +-----------------+-----------------+
1637 +-- LEN points here.
1639 We just want the duplicates, as those are the artificial
1640 non-ignored vars that we want to keep until instantiate_decls.
1641 Move them down and truncate the array. */
1642 if (!vec_safe_is_empty (cfun->local_decls))
1643 cfun->local_decls->block_remove (0, len);
1645 /* At this point, all variables within the block tree with TREE_USED
1646 set are actually used by the optimized function. Lay them out. */
1647 expand_used_vars_for_block (outer_block, true);
1649 if (stack_vars_num > 0)
1651 add_scope_conflicts ();
1653 /* If stack protection is enabled, we don't share space between
1654 vulnerable data and non-vulnerable data. */
1655 if (flag_stack_protect)
1656 add_stack_protection_conflicts ();
1658 /* Now that we have collected all stack variables, and have computed a
1659 minimal interference graph, attempt to save some stack space. */
1660 partition_stack_vars ();
1661 if (dump_file)
1662 dump_stack_var_partition ();
1665 /* There are several conditions under which we should create a
1666 stack guard: protect-all, alloca used, protected decls present. */
1667 if (flag_stack_protect == 2
1668 || (flag_stack_protect
1669 && (cfun->calls_alloca || has_protected_decls)))
1670 create_stack_guard ();
1672 /* Assign rtl to each variable based on these partitions. */
1673 if (stack_vars_num > 0)
1675 struct stack_vars_data data;
1677 data.asan_vec = vNULL;
1678 data.asan_decl_vec = vNULL;
1680 /* Reorder decls to be protected by iterating over the variables
1681 array multiple times, and allocating out of each phase in turn. */
1682 /* ??? We could probably integrate this into the qsort we did
1683 earlier, such that we naturally see these variables first,
1684 and thus naturally allocate things in the right order. */
1685 if (has_protected_decls)
1687 /* Phase 1 contains only character arrays. */
1688 expand_stack_vars (stack_protect_decl_phase_1, &data);
1690 /* Phase 2 contains other kinds of arrays. */
1691 if (flag_stack_protect == 2)
1692 expand_stack_vars (stack_protect_decl_phase_2, &data);
1695 if (flag_asan)
1696 /* Phase 3, any partitions that need asan protection
1697 in addition to phase 1 and 2. */
1698 expand_stack_vars (asan_decl_phase_3, &data);
1700 if (!data.asan_vec.is_empty ())
1702 HOST_WIDE_INT prev_offset = frame_offset;
1703 HOST_WIDE_INT offset
1704 = alloc_stack_frame_space (ASAN_RED_ZONE_SIZE,
1705 ASAN_RED_ZONE_SIZE);
1706 data.asan_vec.safe_push (prev_offset);
1707 data.asan_vec.safe_push (offset);
1709 var_end_seq
1710 = asan_emit_stack_protection (virtual_stack_vars_rtx,
1711 data.asan_vec.address (),
1712 data.asan_decl_vec. address(),
1713 data.asan_vec.length ());
1716 expand_stack_vars (NULL, &data);
1718 data.asan_vec.release ();
1719 data.asan_decl_vec.release ();
1722 fini_vars_expansion ();
1724 /* If there were any artificial non-ignored vars without rtl
1725 found earlier, see if deferred stack allocation hasn't assigned
1726 rtl to them. */
1727 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
1729 rtx rtl = DECL_RTL_IF_SET (var);
1731 /* Keep artificial non-ignored vars in cfun->local_decls
1732 chain until instantiate_decls. */
1733 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1734 add_local_decl (cfun, var);
1736 maybe_local_decls.release ();
1738 /* If the target requires that FRAME_OFFSET be aligned, do it. */
1739 if (STACK_ALIGNMENT_NEEDED)
1741 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1742 if (!FRAME_GROWS_DOWNWARD)
1743 frame_offset += align - 1;
1744 frame_offset &= -align;
1747 return var_end_seq;
1751 /* If we need to produce a detailed dump, print the tree representation
1752 for STMT to the dump file. SINCE is the last RTX after which the RTL
1753 generated for STMT should have been appended. */
1755 static void
1756 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx since)
1758 if (dump_file && (dump_flags & TDF_DETAILS))
1760 fprintf (dump_file, "\n;; ");
1761 print_gimple_stmt (dump_file, stmt, 0,
1762 TDF_SLIM | (dump_flags & TDF_LINENO));
1763 fprintf (dump_file, "\n");
1765 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1769 /* Maps the blocks that do not contain tree labels to rtx labels. */
1771 static struct pointer_map_t *lab_rtx_for_bb;
1773 /* Returns the label_rtx expression for a label starting basic block BB. */
1775 static rtx
1776 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
1778 gimple_stmt_iterator gsi;
1779 tree lab;
1780 gimple lab_stmt;
1781 void **elt;
1783 if (bb->flags & BB_RTL)
1784 return block_label (bb);
1786 elt = pointer_map_contains (lab_rtx_for_bb, bb);
1787 if (elt)
1788 return (rtx) *elt;
1790 /* Find the tree label if it is present. */
1792 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1794 lab_stmt = gsi_stmt (gsi);
1795 if (gimple_code (lab_stmt) != GIMPLE_LABEL)
1796 break;
1798 lab = gimple_label_label (lab_stmt);
1799 if (DECL_NONLOCAL (lab))
1800 break;
1802 return label_rtx (lab);
1805 elt = pointer_map_insert (lab_rtx_for_bb, bb);
1806 *elt = gen_label_rtx ();
1807 return (rtx) *elt;
1811 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
1812 of a basic block where we just expanded the conditional at the end,
1813 possibly clean up the CFG and instruction sequence. LAST is the
1814 last instruction before the just emitted jump sequence. */
1816 static void
1817 maybe_cleanup_end_of_block (edge e, rtx last)
1819 /* Special case: when jumpif decides that the condition is
1820 trivial it emits an unconditional jump (and the necessary
1821 barrier). But we still have two edges, the fallthru one is
1822 wrong. purge_dead_edges would clean this up later. Unfortunately
1823 we have to insert insns (and split edges) before
1824 find_many_sub_basic_blocks and hence before purge_dead_edges.
1825 But splitting edges might create new blocks which depend on the
1826 fact that if there are two edges there's no barrier. So the
1827 barrier would get lost and verify_flow_info would ICE. Instead
1828 of auditing all edge splitters to care for the barrier (which
1829 normally isn't there in a cleaned CFG), fix it here. */
1830 if (BARRIER_P (get_last_insn ()))
1832 rtx insn;
1833 remove_edge (e);
1834 /* Now, we have a single successor block, if we have insns to
1835 insert on the remaining edge we potentially will insert
1836 it at the end of this block (if the dest block isn't feasible)
1837 in order to avoid splitting the edge. This insertion will take
1838 place in front of the last jump. But we might have emitted
1839 multiple jumps (conditional and one unconditional) to the
1840 same destination. Inserting in front of the last one then
1841 is a problem. See PR 40021. We fix this by deleting all
1842 jumps except the last unconditional one. */
1843 insn = PREV_INSN (get_last_insn ());
1844 /* Make sure we have an unconditional jump. Otherwise we're
1845 confused. */
1846 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
1847 for (insn = PREV_INSN (insn); insn != last;)
1849 insn = PREV_INSN (insn);
1850 if (JUMP_P (NEXT_INSN (insn)))
1852 if (!any_condjump_p (NEXT_INSN (insn)))
1854 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
1855 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
1857 delete_insn (NEXT_INSN (insn));
1863 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
1864 Returns a new basic block if we've terminated the current basic
1865 block and created a new one. */
1867 static basic_block
1868 expand_gimple_cond (basic_block bb, gimple stmt)
1870 basic_block new_bb, dest;
1871 edge new_edge;
1872 edge true_edge;
1873 edge false_edge;
1874 rtx last2, last;
1875 enum tree_code code;
1876 tree op0, op1;
1878 code = gimple_cond_code (stmt);
1879 op0 = gimple_cond_lhs (stmt);
1880 op1 = gimple_cond_rhs (stmt);
1881 /* We're sometimes presented with such code:
1882 D.123_1 = x < y;
1883 if (D.123_1 != 0)
1885 This would expand to two comparisons which then later might
1886 be cleaned up by combine. But some pattern matchers like if-conversion
1887 work better when there's only one compare, so make up for this
1888 here as special exception if TER would have made the same change. */
1889 if (gimple_cond_single_var_p (stmt)
1890 && SA.values
1891 && TREE_CODE (op0) == SSA_NAME
1892 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
1894 gimple second = SSA_NAME_DEF_STMT (op0);
1895 if (gimple_code (second) == GIMPLE_ASSIGN)
1897 enum tree_code code2 = gimple_assign_rhs_code (second);
1898 if (TREE_CODE_CLASS (code2) == tcc_comparison)
1900 code = code2;
1901 op0 = gimple_assign_rhs1 (second);
1902 op1 = gimple_assign_rhs2 (second);
1904 /* If jumps are cheap turn some more codes into
1905 jumpy sequences. */
1906 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4)
1908 if ((code2 == BIT_AND_EXPR
1909 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
1910 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
1911 || code2 == TRUTH_AND_EXPR)
1913 code = TRUTH_ANDIF_EXPR;
1914 op0 = gimple_assign_rhs1 (second);
1915 op1 = gimple_assign_rhs2 (second);
1917 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
1919 code = TRUTH_ORIF_EXPR;
1920 op0 = gimple_assign_rhs1 (second);
1921 op1 = gimple_assign_rhs2 (second);
1927 last2 = last = get_last_insn ();
1929 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1930 set_curr_insn_location (gimple_location (stmt));
1932 /* These flags have no purpose in RTL land. */
1933 true_edge->flags &= ~EDGE_TRUE_VALUE;
1934 false_edge->flags &= ~EDGE_FALSE_VALUE;
1936 /* We can either have a pure conditional jump with one fallthru edge or
1937 two-way jump that needs to be decomposed into two basic blocks. */
1938 if (false_edge->dest == bb->next_bb)
1940 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
1941 true_edge->probability);
1942 maybe_dump_rtl_for_gimple_stmt (stmt, last);
1943 if (true_edge->goto_locus != UNKNOWN_LOCATION)
1944 set_curr_insn_location (true_edge->goto_locus);
1945 false_edge->flags |= EDGE_FALLTHRU;
1946 maybe_cleanup_end_of_block (false_edge, last);
1947 return NULL;
1949 if (true_edge->dest == bb->next_bb)
1951 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
1952 false_edge->probability);
1953 maybe_dump_rtl_for_gimple_stmt (stmt, last);
1954 if (false_edge->goto_locus != UNKNOWN_LOCATION)
1955 set_curr_insn_location (false_edge->goto_locus);
1956 true_edge->flags |= EDGE_FALLTHRU;
1957 maybe_cleanup_end_of_block (true_edge, last);
1958 return NULL;
1961 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
1962 true_edge->probability);
1963 last = get_last_insn ();
1964 if (false_edge->goto_locus != UNKNOWN_LOCATION)
1965 set_curr_insn_location (false_edge->goto_locus);
1966 emit_jump (label_rtx_for_bb (false_edge->dest));
1968 BB_END (bb) = last;
1969 if (BARRIER_P (BB_END (bb)))
1970 BB_END (bb) = PREV_INSN (BB_END (bb));
1971 update_bb_for_insn (bb);
1973 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1974 dest = false_edge->dest;
1975 redirect_edge_succ (false_edge, new_bb);
1976 false_edge->flags |= EDGE_FALLTHRU;
1977 new_bb->count = false_edge->count;
1978 new_bb->frequency = EDGE_FREQUENCY (false_edge);
1979 if (current_loops && bb->loop_father)
1980 add_bb_to_loop (new_bb, bb->loop_father);
1981 new_edge = make_edge (new_bb, dest, 0);
1982 new_edge->probability = REG_BR_PROB_BASE;
1983 new_edge->count = new_bb->count;
1984 if (BARRIER_P (BB_END (new_bb)))
1985 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
1986 update_bb_for_insn (new_bb);
1988 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
1990 if (true_edge->goto_locus != UNKNOWN_LOCATION)
1992 set_curr_insn_location (true_edge->goto_locus);
1993 true_edge->goto_locus = curr_insn_location ();
1996 return new_bb;
1999 /* Mark all calls that can have a transaction restart. */
2001 static void
2002 mark_transaction_restart_calls (gimple stmt)
2004 struct tm_restart_node dummy;
2005 void **slot;
2007 if (!cfun->gimple_df->tm_restart)
2008 return;
2010 dummy.stmt = stmt;
2011 slot = htab_find_slot (cfun->gimple_df->tm_restart, &dummy, NO_INSERT);
2012 if (slot)
2014 struct tm_restart_node *n = (struct tm_restart_node *) *slot;
2015 tree list = n->label_or_list;
2016 rtx insn;
2018 for (insn = next_real_insn (get_last_insn ());
2019 !CALL_P (insn);
2020 insn = next_real_insn (insn))
2021 continue;
2023 if (TREE_CODE (list) == LABEL_DECL)
2024 add_reg_note (insn, REG_TM, label_rtx (list));
2025 else
2026 for (; list ; list = TREE_CHAIN (list))
2027 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2031 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2032 statement STMT. */
2034 static void
2035 expand_call_stmt (gimple stmt)
2037 tree exp, decl, lhs;
2038 bool builtin_p;
2039 size_t i;
2041 if (gimple_call_internal_p (stmt))
2043 expand_internal_call (stmt);
2044 return;
2047 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2049 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2050 decl = gimple_call_fndecl (stmt);
2051 builtin_p = decl && DECL_BUILT_IN (decl);
2053 /* If this is not a builtin function, the function type through which the
2054 call is made may be different from the type of the function. */
2055 if (!builtin_p)
2056 CALL_EXPR_FN (exp)
2057 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2058 CALL_EXPR_FN (exp));
2060 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2061 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2063 for (i = 0; i < gimple_call_num_args (stmt); i++)
2065 tree arg = gimple_call_arg (stmt, i);
2066 gimple def;
2067 /* TER addresses into arguments of builtin functions so we have a
2068 chance to infer more correct alignment information. See PR39954. */
2069 if (builtin_p
2070 && TREE_CODE (arg) == SSA_NAME
2071 && (def = get_gimple_for_ssa_name (arg))
2072 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2073 arg = gimple_assign_rhs1 (def);
2074 CALL_EXPR_ARG (exp, i) = arg;
2077 if (gimple_has_side_effects (stmt))
2078 TREE_SIDE_EFFECTS (exp) = 1;
2080 if (gimple_call_nothrow_p (stmt))
2081 TREE_NOTHROW (exp) = 1;
2083 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2084 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2085 if (decl
2086 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2087 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2088 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2089 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2090 else
2091 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2092 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2093 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2095 /* Ensure RTL is created for debug args. */
2096 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2098 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2099 unsigned int ix;
2100 tree dtemp;
2102 if (debug_args)
2103 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2105 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2106 expand_debug_expr (dtemp);
2110 lhs = gimple_call_lhs (stmt);
2111 if (lhs)
2112 expand_assignment (lhs, exp, false);
2113 else
2114 expand_expr_real_1 (exp, const0_rtx, VOIDmode, EXPAND_NORMAL, NULL);
2116 mark_transaction_restart_calls (stmt);
2119 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
2120 STMT that doesn't require special handling for outgoing edges. That
2121 is no tailcalls and no GIMPLE_COND. */
2123 static void
2124 expand_gimple_stmt_1 (gimple stmt)
2126 tree op0;
2128 set_curr_insn_location (gimple_location (stmt));
2130 switch (gimple_code (stmt))
2132 case GIMPLE_GOTO:
2133 op0 = gimple_goto_dest (stmt);
2134 if (TREE_CODE (op0) == LABEL_DECL)
2135 expand_goto (op0);
2136 else
2137 expand_computed_goto (op0);
2138 break;
2139 case GIMPLE_LABEL:
2140 expand_label (gimple_label_label (stmt));
2141 break;
2142 case GIMPLE_NOP:
2143 case GIMPLE_PREDICT:
2144 break;
2145 case GIMPLE_SWITCH:
2146 expand_case (stmt);
2147 break;
2148 case GIMPLE_ASM:
2149 expand_asm_stmt (stmt);
2150 break;
2151 case GIMPLE_CALL:
2152 expand_call_stmt (stmt);
2153 break;
2155 case GIMPLE_RETURN:
2156 op0 = gimple_return_retval (stmt);
2158 if (op0 && op0 != error_mark_node)
2160 tree result = DECL_RESULT (current_function_decl);
2162 /* If we are not returning the current function's RESULT_DECL,
2163 build an assignment to it. */
2164 if (op0 != result)
2166 /* I believe that a function's RESULT_DECL is unique. */
2167 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
2169 /* ??? We'd like to use simply expand_assignment here,
2170 but this fails if the value is of BLKmode but the return
2171 decl is a register. expand_return has special handling
2172 for this combination, which eventually should move
2173 to common code. See comments there. Until then, let's
2174 build a modify expression :-/ */
2175 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
2176 result, op0);
2179 if (!op0)
2180 expand_null_return ();
2181 else
2182 expand_return (op0);
2183 break;
2185 case GIMPLE_ASSIGN:
2187 tree lhs = gimple_assign_lhs (stmt);
2189 /* Tree expand used to fiddle with |= and &= of two bitfield
2190 COMPONENT_REFs here. This can't happen with gimple, the LHS
2191 of binary assigns must be a gimple reg. */
2193 if (TREE_CODE (lhs) != SSA_NAME
2194 || get_gimple_rhs_class (gimple_expr_code (stmt))
2195 == GIMPLE_SINGLE_RHS)
2197 tree rhs = gimple_assign_rhs1 (stmt);
2198 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
2199 == GIMPLE_SINGLE_RHS);
2200 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
2201 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
2202 if (TREE_CLOBBER_P (rhs))
2203 /* This is a clobber to mark the going out of scope for
2204 this LHS. */
2206 else
2207 expand_assignment (lhs, rhs,
2208 gimple_assign_nontemporal_move_p (stmt));
2210 else
2212 rtx target, temp;
2213 bool nontemporal = gimple_assign_nontemporal_move_p (stmt);
2214 struct separate_ops ops;
2215 bool promoted = false;
2217 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
2218 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
2219 promoted = true;
2221 ops.code = gimple_assign_rhs_code (stmt);
2222 ops.type = TREE_TYPE (lhs);
2223 switch (get_gimple_rhs_class (gimple_expr_code (stmt)))
2225 case GIMPLE_TERNARY_RHS:
2226 ops.op2 = gimple_assign_rhs3 (stmt);
2227 /* Fallthru */
2228 case GIMPLE_BINARY_RHS:
2229 ops.op1 = gimple_assign_rhs2 (stmt);
2230 /* Fallthru */
2231 case GIMPLE_UNARY_RHS:
2232 ops.op0 = gimple_assign_rhs1 (stmt);
2233 break;
2234 default:
2235 gcc_unreachable ();
2237 ops.location = gimple_location (stmt);
2239 /* If we want to use a nontemporal store, force the value to
2240 register first. If we store into a promoted register,
2241 don't directly expand to target. */
2242 temp = nontemporal || promoted ? NULL_RTX : target;
2243 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
2244 EXPAND_NORMAL);
2246 if (temp == target)
2248 else if (promoted)
2250 int unsignedp = SUBREG_PROMOTED_UNSIGNED_P (target);
2251 /* If TEMP is a VOIDmode constant, use convert_modes to make
2252 sure that we properly convert it. */
2253 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
2255 temp = convert_modes (GET_MODE (target),
2256 TYPE_MODE (ops.type),
2257 temp, unsignedp);
2258 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
2259 GET_MODE (target), temp, unsignedp);
2262 convert_move (SUBREG_REG (target), temp, unsignedp);
2264 else if (nontemporal && emit_storent_insn (target, temp))
2266 else
2268 temp = force_operand (temp, target);
2269 if (temp != target)
2270 emit_move_insn (target, temp);
2274 break;
2276 default:
2277 gcc_unreachable ();
2281 /* Expand one gimple statement STMT and return the last RTL instruction
2282 before any of the newly generated ones.
2284 In addition to generating the necessary RTL instructions this also
2285 sets REG_EH_REGION notes if necessary and sets the current source
2286 location for diagnostics. */
2288 static rtx
2289 expand_gimple_stmt (gimple stmt)
2291 location_t saved_location = input_location;
2292 rtx last = get_last_insn ();
2293 int lp_nr;
2295 gcc_assert (cfun);
2297 /* We need to save and restore the current source location so that errors
2298 discovered during expansion are emitted with the right location. But
2299 it would be better if the diagnostic routines used the source location
2300 embedded in the tree nodes rather than globals. */
2301 if (gimple_has_location (stmt))
2302 input_location = gimple_location (stmt);
2304 expand_gimple_stmt_1 (stmt);
2306 /* Free any temporaries used to evaluate this statement. */
2307 free_temp_slots ();
2309 input_location = saved_location;
2311 /* Mark all insns that may trap. */
2312 lp_nr = lookup_stmt_eh_lp (stmt);
2313 if (lp_nr)
2315 rtx insn;
2316 for (insn = next_real_insn (last); insn;
2317 insn = next_real_insn (insn))
2319 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
2320 /* If we want exceptions for non-call insns, any
2321 may_trap_p instruction may throw. */
2322 && GET_CODE (PATTERN (insn)) != CLOBBER
2323 && GET_CODE (PATTERN (insn)) != USE
2324 && insn_could_throw_p (insn))
2325 make_reg_eh_region_note (insn, 0, lp_nr);
2329 return last;
2332 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
2333 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
2334 generated a tail call (something that might be denied by the ABI
2335 rules governing the call; see calls.c).
2337 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
2338 can still reach the rest of BB. The case here is __builtin_sqrt,
2339 where the NaN result goes through the external function (with a
2340 tailcall) and the normal result happens via a sqrt instruction. */
2342 static basic_block
2343 expand_gimple_tailcall (basic_block bb, gimple stmt, bool *can_fallthru)
2345 rtx last2, last;
2346 edge e;
2347 edge_iterator ei;
2348 int probability;
2349 gcov_type count;
2351 last2 = last = expand_gimple_stmt (stmt);
2353 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
2354 if (CALL_P (last) && SIBLING_CALL_P (last))
2355 goto found;
2357 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2359 *can_fallthru = true;
2360 return NULL;
2362 found:
2363 /* ??? Wouldn't it be better to just reset any pending stack adjust?
2364 Any instructions emitted here are about to be deleted. */
2365 do_pending_stack_adjust ();
2367 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
2368 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
2369 EH or abnormal edges, we shouldn't have created a tail call in
2370 the first place. So it seems to me we should just be removing
2371 all edges here, or redirecting the existing fallthru edge to
2372 the exit block. */
2374 probability = 0;
2375 count = 0;
2377 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2379 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
2381 if (e->dest != EXIT_BLOCK_PTR)
2383 e->dest->count -= e->count;
2384 e->dest->frequency -= EDGE_FREQUENCY (e);
2385 if (e->dest->count < 0)
2386 e->dest->count = 0;
2387 if (e->dest->frequency < 0)
2388 e->dest->frequency = 0;
2390 count += e->count;
2391 probability += e->probability;
2392 remove_edge (e);
2394 else
2395 ei_next (&ei);
2398 /* This is somewhat ugly: the call_expr expander often emits instructions
2399 after the sibcall (to perform the function return). These confuse the
2400 find_many_sub_basic_blocks code, so we need to get rid of these. */
2401 last = NEXT_INSN (last);
2402 gcc_assert (BARRIER_P (last));
2404 *can_fallthru = false;
2405 while (NEXT_INSN (last))
2407 /* For instance an sqrt builtin expander expands if with
2408 sibcall in the then and label for `else`. */
2409 if (LABEL_P (NEXT_INSN (last)))
2411 *can_fallthru = true;
2412 break;
2414 delete_insn (NEXT_INSN (last));
2417 e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
2418 e->probability += probability;
2419 e->count += count;
2420 BB_END (bb) = last;
2421 update_bb_for_insn (bb);
2423 if (NEXT_INSN (last))
2425 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2427 last = BB_END (bb);
2428 if (BARRIER_P (last))
2429 BB_END (bb) = PREV_INSN (last);
2432 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2434 return bb;
2437 /* Return the difference between the floor and the truncated result of
2438 a signed division by OP1 with remainder MOD. */
2439 static rtx
2440 floor_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2442 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
2443 return gen_rtx_IF_THEN_ELSE
2444 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2445 gen_rtx_IF_THEN_ELSE
2446 (mode, gen_rtx_LT (BImode,
2447 gen_rtx_DIV (mode, op1, mod),
2448 const0_rtx),
2449 constm1_rtx, const0_rtx),
2450 const0_rtx);
2453 /* Return the difference between the ceil and the truncated result of
2454 a signed division by OP1 with remainder MOD. */
2455 static rtx
2456 ceil_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2458 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
2459 return gen_rtx_IF_THEN_ELSE
2460 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2461 gen_rtx_IF_THEN_ELSE
2462 (mode, gen_rtx_GT (BImode,
2463 gen_rtx_DIV (mode, op1, mod),
2464 const0_rtx),
2465 const1_rtx, const0_rtx),
2466 const0_rtx);
2469 /* Return the difference between the ceil and the truncated result of
2470 an unsigned division by OP1 with remainder MOD. */
2471 static rtx
2472 ceil_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
2474 /* (mod != 0 ? 1 : 0) */
2475 return gen_rtx_IF_THEN_ELSE
2476 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2477 const1_rtx, const0_rtx);
2480 /* Return the difference between the rounded and the truncated result
2481 of a signed division by OP1 with remainder MOD. Halfway cases are
2482 rounded away from zero, rather than to the nearest even number. */
2483 static rtx
2484 round_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2486 /* (abs (mod) >= abs (op1) - abs (mod)
2487 ? (op1 / mod > 0 ? 1 : -1)
2488 : 0) */
2489 return gen_rtx_IF_THEN_ELSE
2490 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
2491 gen_rtx_MINUS (mode,
2492 gen_rtx_ABS (mode, op1),
2493 gen_rtx_ABS (mode, mod))),
2494 gen_rtx_IF_THEN_ELSE
2495 (mode, gen_rtx_GT (BImode,
2496 gen_rtx_DIV (mode, op1, mod),
2497 const0_rtx),
2498 const1_rtx, constm1_rtx),
2499 const0_rtx);
2502 /* Return the difference between the rounded and the truncated result
2503 of a unsigned division by OP1 with remainder MOD. Halfway cases
2504 are rounded away from zero, rather than to the nearest even
2505 number. */
2506 static rtx
2507 round_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2509 /* (mod >= op1 - mod ? 1 : 0) */
2510 return gen_rtx_IF_THEN_ELSE
2511 (mode, gen_rtx_GE (BImode, mod,
2512 gen_rtx_MINUS (mode, op1, mod)),
2513 const1_rtx, const0_rtx);
2516 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
2517 any rtl. */
2519 static rtx
2520 convert_debug_memory_address (enum machine_mode mode, rtx x,
2521 addr_space_t as)
2523 enum machine_mode xmode = GET_MODE (x);
2525 #ifndef POINTERS_EXTEND_UNSIGNED
2526 gcc_assert (mode == Pmode
2527 || mode == targetm.addr_space.address_mode (as));
2528 gcc_assert (xmode == mode || xmode == VOIDmode);
2529 #else
2530 rtx temp;
2532 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
2534 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
2535 return x;
2537 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
2538 x = simplify_gen_subreg (mode, x, xmode,
2539 subreg_lowpart_offset
2540 (mode, xmode));
2541 else if (POINTERS_EXTEND_UNSIGNED > 0)
2542 x = gen_rtx_ZERO_EXTEND (mode, x);
2543 else if (!POINTERS_EXTEND_UNSIGNED)
2544 x = gen_rtx_SIGN_EXTEND (mode, x);
2545 else
2547 switch (GET_CODE (x))
2549 case SUBREG:
2550 if ((SUBREG_PROMOTED_VAR_P (x)
2551 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
2552 || (GET_CODE (SUBREG_REG (x)) == PLUS
2553 && REG_P (XEXP (SUBREG_REG (x), 0))
2554 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
2555 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
2556 && GET_MODE (SUBREG_REG (x)) == mode)
2557 return SUBREG_REG (x);
2558 break;
2559 case LABEL_REF:
2560 temp = gen_rtx_LABEL_REF (mode, XEXP (x, 0));
2561 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
2562 return temp;
2563 case SYMBOL_REF:
2564 temp = shallow_copy_rtx (x);
2565 PUT_MODE (temp, mode);
2566 return temp;
2567 case CONST:
2568 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
2569 if (temp)
2570 temp = gen_rtx_CONST (mode, temp);
2571 return temp;
2572 case PLUS:
2573 case MINUS:
2574 if (CONST_INT_P (XEXP (x, 1)))
2576 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
2577 if (temp)
2578 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
2580 break;
2581 default:
2582 break;
2584 /* Don't know how to express ptr_extend as operation in debug info. */
2585 return NULL;
2587 #endif /* POINTERS_EXTEND_UNSIGNED */
2589 return x;
2592 /* Return an RTX equivalent to the value of the parameter DECL. */
2594 static rtx
2595 expand_debug_parm_decl (tree decl)
2597 rtx incoming = DECL_INCOMING_RTL (decl);
2599 if (incoming
2600 && GET_MODE (incoming) != BLKmode
2601 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
2602 || (MEM_P (incoming)
2603 && REG_P (XEXP (incoming, 0))
2604 && HARD_REGISTER_P (XEXP (incoming, 0)))))
2606 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
2608 #ifdef HAVE_window_save
2609 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
2610 If the target machine has an explicit window save instruction, the
2611 actual entry value is the corresponding OUTGOING_REGNO instead. */
2612 if (REG_P (incoming)
2613 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
2614 incoming
2615 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
2616 OUTGOING_REGNO (REGNO (incoming)), 0);
2617 else if (MEM_P (incoming))
2619 rtx reg = XEXP (incoming, 0);
2620 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
2622 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
2623 incoming = replace_equiv_address_nv (incoming, reg);
2626 #endif
2628 ENTRY_VALUE_EXP (rtl) = incoming;
2629 return rtl;
2632 if (incoming
2633 && GET_MODE (incoming) != BLKmode
2634 && !TREE_ADDRESSABLE (decl)
2635 && MEM_P (incoming)
2636 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
2637 || (GET_CODE (XEXP (incoming, 0)) == PLUS
2638 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
2639 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
2640 return incoming;
2642 return NULL_RTX;
2645 /* Return an RTX equivalent to the value of the tree expression EXP. */
2647 static rtx
2648 expand_debug_expr (tree exp)
2650 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
2651 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2652 enum machine_mode inner_mode = VOIDmode;
2653 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
2654 addr_space_t as;
2656 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
2658 case tcc_expression:
2659 switch (TREE_CODE (exp))
2661 case COND_EXPR:
2662 case DOT_PROD_EXPR:
2663 case WIDEN_MULT_PLUS_EXPR:
2664 case WIDEN_MULT_MINUS_EXPR:
2665 case FMA_EXPR:
2666 goto ternary;
2668 case TRUTH_ANDIF_EXPR:
2669 case TRUTH_ORIF_EXPR:
2670 case TRUTH_AND_EXPR:
2671 case TRUTH_OR_EXPR:
2672 case TRUTH_XOR_EXPR:
2673 goto binary;
2675 case TRUTH_NOT_EXPR:
2676 goto unary;
2678 default:
2679 break;
2681 break;
2683 ternary:
2684 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
2685 if (!op2)
2686 return NULL_RTX;
2687 /* Fall through. */
2689 binary:
2690 case tcc_binary:
2691 case tcc_comparison:
2692 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
2693 if (!op1)
2694 return NULL_RTX;
2695 /* Fall through. */
2697 unary:
2698 case tcc_unary:
2699 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
2700 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2701 if (!op0)
2702 return NULL_RTX;
2703 break;
2705 case tcc_type:
2706 case tcc_statement:
2707 gcc_unreachable ();
2709 case tcc_constant:
2710 case tcc_exceptional:
2711 case tcc_declaration:
2712 case tcc_reference:
2713 case tcc_vl_exp:
2714 break;
2717 switch (TREE_CODE (exp))
2719 case STRING_CST:
2720 if (!lookup_constant_def (exp))
2722 if (strlen (TREE_STRING_POINTER (exp)) + 1
2723 != (size_t) TREE_STRING_LENGTH (exp))
2724 return NULL_RTX;
2725 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
2726 op0 = gen_rtx_MEM (BLKmode, op0);
2727 set_mem_attributes (op0, exp, 0);
2728 return op0;
2730 /* Fall through... */
2732 case INTEGER_CST:
2733 case REAL_CST:
2734 case FIXED_CST:
2735 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
2736 return op0;
2738 case COMPLEX_CST:
2739 gcc_assert (COMPLEX_MODE_P (mode));
2740 op0 = expand_debug_expr (TREE_REALPART (exp));
2741 op1 = expand_debug_expr (TREE_IMAGPART (exp));
2742 return gen_rtx_CONCAT (mode, op0, op1);
2744 case DEBUG_EXPR_DECL:
2745 op0 = DECL_RTL_IF_SET (exp);
2747 if (op0)
2748 return op0;
2750 op0 = gen_rtx_DEBUG_EXPR (mode);
2751 DEBUG_EXPR_TREE_DECL (op0) = exp;
2752 SET_DECL_RTL (exp, op0);
2754 return op0;
2756 case VAR_DECL:
2757 case PARM_DECL:
2758 case FUNCTION_DECL:
2759 case LABEL_DECL:
2760 case CONST_DECL:
2761 case RESULT_DECL:
2762 op0 = DECL_RTL_IF_SET (exp);
2764 /* This decl was probably optimized away. */
2765 if (!op0)
2767 if (TREE_CODE (exp) != VAR_DECL
2768 || DECL_EXTERNAL (exp)
2769 || !TREE_STATIC (exp)
2770 || !DECL_NAME (exp)
2771 || DECL_HARD_REGISTER (exp)
2772 || DECL_IN_CONSTANT_POOL (exp)
2773 || mode == VOIDmode)
2774 return NULL;
2776 op0 = make_decl_rtl_for_debug (exp);
2777 if (!MEM_P (op0)
2778 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
2779 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
2780 return NULL;
2782 else
2783 op0 = copy_rtx (op0);
2785 if (GET_MODE (op0) == BLKmode
2786 /* If op0 is not BLKmode, but BLKmode is, adjust_mode
2787 below would ICE. While it is likely a FE bug,
2788 try to be robust here. See PR43166. */
2789 || mode == BLKmode
2790 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
2792 gcc_assert (MEM_P (op0));
2793 op0 = adjust_address_nv (op0, mode, 0);
2794 return op0;
2797 /* Fall through. */
2799 adjust_mode:
2800 case PAREN_EXPR:
2801 case NOP_EXPR:
2802 case CONVERT_EXPR:
2804 inner_mode = GET_MODE (op0);
2806 if (mode == inner_mode)
2807 return op0;
2809 if (inner_mode == VOIDmode)
2811 if (TREE_CODE (exp) == SSA_NAME)
2812 inner_mode = TYPE_MODE (TREE_TYPE (exp));
2813 else
2814 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
2815 if (mode == inner_mode)
2816 return op0;
2819 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
2821 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
2822 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
2823 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
2824 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
2825 else
2826 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
2828 else if (FLOAT_MODE_P (mode))
2830 gcc_assert (TREE_CODE (exp) != SSA_NAME);
2831 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
2832 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
2833 else
2834 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
2836 else if (FLOAT_MODE_P (inner_mode))
2838 if (unsignedp)
2839 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
2840 else
2841 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
2843 else if (CONSTANT_P (op0)
2844 || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
2845 op0 = simplify_gen_subreg (mode, op0, inner_mode,
2846 subreg_lowpart_offset (mode,
2847 inner_mode));
2848 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == tcc_unary
2849 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
2850 : unsignedp)
2851 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
2852 else
2853 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
2855 return op0;
2858 case MEM_REF:
2859 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
2861 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
2862 TREE_OPERAND (exp, 0),
2863 TREE_OPERAND (exp, 1));
2864 if (newexp)
2865 return expand_debug_expr (newexp);
2867 /* FALLTHROUGH */
2868 case INDIRECT_REF:
2869 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
2870 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2871 if (!op0)
2872 return NULL;
2874 if (TREE_CODE (exp) == MEM_REF)
2876 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
2877 || (GET_CODE (op0) == PLUS
2878 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
2879 /* (mem (debug_implicit_ptr)) might confuse aliasing.
2880 Instead just use get_inner_reference. */
2881 goto component_ref;
2883 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
2884 if (!op1 || !CONST_INT_P (op1))
2885 return NULL;
2887 op0 = plus_constant (inner_mode, op0, INTVAL (op1));
2890 if (POINTER_TYPE_P (TREE_TYPE (exp)))
2891 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
2892 else
2893 as = ADDR_SPACE_GENERIC;
2895 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
2896 op0, as);
2897 if (op0 == NULL_RTX)
2898 return NULL;
2900 op0 = gen_rtx_MEM (mode, op0);
2901 set_mem_attributes (op0, exp, 0);
2902 if (TREE_CODE (exp) == MEM_REF
2903 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
2904 set_mem_expr (op0, NULL_TREE);
2905 set_mem_addr_space (op0, as);
2907 return op0;
2909 case TARGET_MEM_REF:
2910 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
2911 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
2912 return NULL;
2914 op0 = expand_debug_expr
2915 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
2916 if (!op0)
2917 return NULL;
2919 if (POINTER_TYPE_P (TREE_TYPE (exp)))
2920 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
2921 else
2922 as = ADDR_SPACE_GENERIC;
2924 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
2925 op0, as);
2926 if (op0 == NULL_RTX)
2927 return NULL;
2929 op0 = gen_rtx_MEM (mode, op0);
2931 set_mem_attributes (op0, exp, 0);
2932 set_mem_addr_space (op0, as);
2934 return op0;
2936 component_ref:
2937 case ARRAY_REF:
2938 case ARRAY_RANGE_REF:
2939 case COMPONENT_REF:
2940 case BIT_FIELD_REF:
2941 case REALPART_EXPR:
2942 case IMAGPART_EXPR:
2943 case VIEW_CONVERT_EXPR:
2945 enum machine_mode mode1;
2946 HOST_WIDE_INT bitsize, bitpos;
2947 tree offset;
2948 int volatilep = 0;
2949 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
2950 &mode1, &unsignedp, &volatilep, false);
2951 rtx orig_op0;
2953 if (bitsize == 0)
2954 return NULL;
2956 orig_op0 = op0 = expand_debug_expr (tem);
2958 if (!op0)
2959 return NULL;
2961 if (offset)
2963 enum machine_mode addrmode, offmode;
2965 if (!MEM_P (op0))
2966 return NULL;
2968 op0 = XEXP (op0, 0);
2969 addrmode = GET_MODE (op0);
2970 if (addrmode == VOIDmode)
2971 addrmode = Pmode;
2973 op1 = expand_debug_expr (offset);
2974 if (!op1)
2975 return NULL;
2977 offmode = GET_MODE (op1);
2978 if (offmode == VOIDmode)
2979 offmode = TYPE_MODE (TREE_TYPE (offset));
2981 if (addrmode != offmode)
2982 op1 = simplify_gen_subreg (addrmode, op1, offmode,
2983 subreg_lowpart_offset (addrmode,
2984 offmode));
2986 /* Don't use offset_address here, we don't need a
2987 recognizable address, and we don't want to generate
2988 code. */
2989 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
2990 op0, op1));
2993 if (MEM_P (op0))
2995 if (mode1 == VOIDmode)
2996 /* Bitfield. */
2997 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
2998 if (bitpos >= BITS_PER_UNIT)
3000 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
3001 bitpos %= BITS_PER_UNIT;
3003 else if (bitpos < 0)
3005 HOST_WIDE_INT units
3006 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
3007 op0 = adjust_address_nv (op0, mode1, units);
3008 bitpos += units * BITS_PER_UNIT;
3010 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
3011 op0 = adjust_address_nv (op0, mode, 0);
3012 else if (GET_MODE (op0) != mode1)
3013 op0 = adjust_address_nv (op0, mode1, 0);
3014 else
3015 op0 = copy_rtx (op0);
3016 if (op0 == orig_op0)
3017 op0 = shallow_copy_rtx (op0);
3018 set_mem_attributes (op0, exp, 0);
3021 if (bitpos == 0 && mode == GET_MODE (op0))
3022 return op0;
3024 if (bitpos < 0)
3025 return NULL;
3027 if (GET_MODE (op0) == BLKmode)
3028 return NULL;
3030 if ((bitpos % BITS_PER_UNIT) == 0
3031 && bitsize == GET_MODE_BITSIZE (mode1))
3033 enum machine_mode opmode = GET_MODE (op0);
3035 if (opmode == VOIDmode)
3036 opmode = TYPE_MODE (TREE_TYPE (tem));
3038 /* This condition may hold if we're expanding the address
3039 right past the end of an array that turned out not to
3040 be addressable (i.e., the address was only computed in
3041 debug stmts). The gen_subreg below would rightfully
3042 crash, and the address doesn't really exist, so just
3043 drop it. */
3044 if (bitpos >= GET_MODE_BITSIZE (opmode))
3045 return NULL;
3047 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
3048 return simplify_gen_subreg (mode, op0, opmode,
3049 bitpos / BITS_PER_UNIT);
3052 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
3053 && TYPE_UNSIGNED (TREE_TYPE (exp))
3054 ? SIGN_EXTRACT
3055 : ZERO_EXTRACT, mode,
3056 GET_MODE (op0) != VOIDmode
3057 ? GET_MODE (op0)
3058 : TYPE_MODE (TREE_TYPE (tem)),
3059 op0, GEN_INT (bitsize), GEN_INT (bitpos));
3062 case ABS_EXPR:
3063 return simplify_gen_unary (ABS, mode, op0, mode);
3065 case NEGATE_EXPR:
3066 return simplify_gen_unary (NEG, mode, op0, mode);
3068 case BIT_NOT_EXPR:
3069 return simplify_gen_unary (NOT, mode, op0, mode);
3071 case FLOAT_EXPR:
3072 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
3073 0)))
3074 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
3075 inner_mode);
3077 case FIX_TRUNC_EXPR:
3078 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
3079 inner_mode);
3081 case POINTER_PLUS_EXPR:
3082 /* For the rare target where pointers are not the same size as
3083 size_t, we need to check for mis-matched modes and correct
3084 the addend. */
3085 if (op0 && op1
3086 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
3087 && GET_MODE (op0) != GET_MODE (op1))
3089 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1)))
3090 op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
3091 GET_MODE (op1));
3092 else
3093 /* We always sign-extend, regardless of the signedness of
3094 the operand, because the operand is always unsigned
3095 here even if the original C expression is signed. */
3096 op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
3097 GET_MODE (op1));
3099 /* Fall through. */
3100 case PLUS_EXPR:
3101 return simplify_gen_binary (PLUS, mode, op0, op1);
3103 case MINUS_EXPR:
3104 return simplify_gen_binary (MINUS, mode, op0, op1);
3106 case MULT_EXPR:
3107 return simplify_gen_binary (MULT, mode, op0, op1);
3109 case RDIV_EXPR:
3110 case TRUNC_DIV_EXPR:
3111 case EXACT_DIV_EXPR:
3112 if (unsignedp)
3113 return simplify_gen_binary (UDIV, mode, op0, op1);
3114 else
3115 return simplify_gen_binary (DIV, mode, op0, op1);
3117 case TRUNC_MOD_EXPR:
3118 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
3120 case FLOOR_DIV_EXPR:
3121 if (unsignedp)
3122 return simplify_gen_binary (UDIV, mode, op0, op1);
3123 else
3125 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
3126 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
3127 rtx adj = floor_sdiv_adjust (mode, mod, op1);
3128 return simplify_gen_binary (PLUS, mode, div, adj);
3131 case FLOOR_MOD_EXPR:
3132 if (unsignedp)
3133 return simplify_gen_binary (UMOD, mode, op0, op1);
3134 else
3136 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
3137 rtx adj = floor_sdiv_adjust (mode, mod, op1);
3138 adj = simplify_gen_unary (NEG, mode,
3139 simplify_gen_binary (MULT, mode, adj, op1),
3140 mode);
3141 return simplify_gen_binary (PLUS, mode, mod, adj);
3144 case CEIL_DIV_EXPR:
3145 if (unsignedp)
3147 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
3148 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
3149 rtx adj = ceil_udiv_adjust (mode, mod, op1);
3150 return simplify_gen_binary (PLUS, mode, div, adj);
3152 else
3154 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
3155 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
3156 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
3157 return simplify_gen_binary (PLUS, mode, div, adj);
3160 case CEIL_MOD_EXPR:
3161 if (unsignedp)
3163 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
3164 rtx adj = ceil_udiv_adjust (mode, mod, op1);
3165 adj = simplify_gen_unary (NEG, mode,
3166 simplify_gen_binary (MULT, mode, adj, op1),
3167 mode);
3168 return simplify_gen_binary (PLUS, mode, mod, adj);
3170 else
3172 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
3173 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
3174 adj = simplify_gen_unary (NEG, mode,
3175 simplify_gen_binary (MULT, mode, adj, op1),
3176 mode);
3177 return simplify_gen_binary (PLUS, mode, mod, adj);
3180 case ROUND_DIV_EXPR:
3181 if (unsignedp)
3183 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
3184 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
3185 rtx adj = round_udiv_adjust (mode, mod, op1);
3186 return simplify_gen_binary (PLUS, mode, div, adj);
3188 else
3190 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
3191 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
3192 rtx adj = round_sdiv_adjust (mode, mod, op1);
3193 return simplify_gen_binary (PLUS, mode, div, adj);
3196 case ROUND_MOD_EXPR:
3197 if (unsignedp)
3199 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
3200 rtx adj = round_udiv_adjust (mode, mod, op1);
3201 adj = simplify_gen_unary (NEG, mode,
3202 simplify_gen_binary (MULT, mode, adj, op1),
3203 mode);
3204 return simplify_gen_binary (PLUS, mode, mod, adj);
3206 else
3208 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
3209 rtx adj = round_sdiv_adjust (mode, mod, op1);
3210 adj = simplify_gen_unary (NEG, mode,
3211 simplify_gen_binary (MULT, mode, adj, op1),
3212 mode);
3213 return simplify_gen_binary (PLUS, mode, mod, adj);
3216 case LSHIFT_EXPR:
3217 return simplify_gen_binary (ASHIFT, mode, op0, op1);
3219 case RSHIFT_EXPR:
3220 if (unsignedp)
3221 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
3222 else
3223 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
3225 case LROTATE_EXPR:
3226 return simplify_gen_binary (ROTATE, mode, op0, op1);
3228 case RROTATE_EXPR:
3229 return simplify_gen_binary (ROTATERT, mode, op0, op1);
3231 case MIN_EXPR:
3232 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
3234 case MAX_EXPR:
3235 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
3237 case BIT_AND_EXPR:
3238 case TRUTH_AND_EXPR:
3239 return simplify_gen_binary (AND, mode, op0, op1);
3241 case BIT_IOR_EXPR:
3242 case TRUTH_OR_EXPR:
3243 return simplify_gen_binary (IOR, mode, op0, op1);
3245 case BIT_XOR_EXPR:
3246 case TRUTH_XOR_EXPR:
3247 return simplify_gen_binary (XOR, mode, op0, op1);
3249 case TRUTH_ANDIF_EXPR:
3250 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
3252 case TRUTH_ORIF_EXPR:
3253 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
3255 case TRUTH_NOT_EXPR:
3256 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
3258 case LT_EXPR:
3259 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
3260 op0, op1);
3262 case LE_EXPR:
3263 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
3264 op0, op1);
3266 case GT_EXPR:
3267 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
3268 op0, op1);
3270 case GE_EXPR:
3271 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
3272 op0, op1);
3274 case EQ_EXPR:
3275 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
3277 case NE_EXPR:
3278 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
3280 case UNORDERED_EXPR:
3281 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
3283 case ORDERED_EXPR:
3284 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
3286 case UNLT_EXPR:
3287 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
3289 case UNLE_EXPR:
3290 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
3292 case UNGT_EXPR:
3293 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
3295 case UNGE_EXPR:
3296 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
3298 case UNEQ_EXPR:
3299 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
3301 case LTGT_EXPR:
3302 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
3304 case COND_EXPR:
3305 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
3307 case COMPLEX_EXPR:
3308 gcc_assert (COMPLEX_MODE_P (mode));
3309 if (GET_MODE (op0) == VOIDmode)
3310 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
3311 if (GET_MODE (op1) == VOIDmode)
3312 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
3313 return gen_rtx_CONCAT (mode, op0, op1);
3315 case CONJ_EXPR:
3316 if (GET_CODE (op0) == CONCAT)
3317 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
3318 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
3319 XEXP (op0, 1),
3320 GET_MODE_INNER (mode)));
3321 else
3323 enum machine_mode imode = GET_MODE_INNER (mode);
3324 rtx re, im;
3326 if (MEM_P (op0))
3328 re = adjust_address_nv (op0, imode, 0);
3329 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
3331 else
3333 enum machine_mode ifmode = int_mode_for_mode (mode);
3334 enum machine_mode ihmode = int_mode_for_mode (imode);
3335 rtx halfsize;
3336 if (ifmode == BLKmode || ihmode == BLKmode)
3337 return NULL;
3338 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
3339 re = op0;
3340 if (mode != ifmode)
3341 re = gen_rtx_SUBREG (ifmode, re, 0);
3342 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
3343 if (imode != ihmode)
3344 re = gen_rtx_SUBREG (imode, re, 0);
3345 im = copy_rtx (op0);
3346 if (mode != ifmode)
3347 im = gen_rtx_SUBREG (ifmode, im, 0);
3348 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
3349 if (imode != ihmode)
3350 im = gen_rtx_SUBREG (imode, im, 0);
3352 im = gen_rtx_NEG (imode, im);
3353 return gen_rtx_CONCAT (mode, re, im);
3356 case ADDR_EXPR:
3357 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3358 if (!op0 || !MEM_P (op0))
3360 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
3361 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
3362 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
3363 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
3364 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
3365 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
3367 if (handled_component_p (TREE_OPERAND (exp, 0)))
3369 HOST_WIDE_INT bitoffset, bitsize, maxsize;
3370 tree decl
3371 = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
3372 &bitoffset, &bitsize, &maxsize);
3373 if ((TREE_CODE (decl) == VAR_DECL
3374 || TREE_CODE (decl) == PARM_DECL
3375 || TREE_CODE (decl) == RESULT_DECL)
3376 && (!TREE_ADDRESSABLE (decl)
3377 || target_for_debug_bind (decl))
3378 && (bitoffset % BITS_PER_UNIT) == 0
3379 && bitsize > 0
3380 && bitsize == maxsize)
3382 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
3383 return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
3387 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
3388 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
3389 == ADDR_EXPR)
3391 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
3392 0));
3393 if (op0 != NULL
3394 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
3395 || (GET_CODE (op0) == PLUS
3396 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
3397 && CONST_INT_P (XEXP (op0, 1)))))
3399 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
3400 1));
3401 if (!op1 || !CONST_INT_P (op1))
3402 return NULL;
3404 return plus_constant (mode, op0, INTVAL (op1));
3408 return NULL;
3411 as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
3412 op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
3414 return op0;
3416 case VECTOR_CST:
3418 unsigned i;
3420 op0 = gen_rtx_CONCATN
3421 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
3423 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
3425 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
3426 if (!op1)
3427 return NULL;
3428 XVECEXP (op0, 0, i) = op1;
3431 return op0;
3434 case CONSTRUCTOR:
3435 if (TREE_CLOBBER_P (exp))
3436 return NULL;
3437 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
3439 unsigned i;
3440 tree val;
3442 op0 = gen_rtx_CONCATN
3443 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
3445 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
3447 op1 = expand_debug_expr (val);
3448 if (!op1)
3449 return NULL;
3450 XVECEXP (op0, 0, i) = op1;
3453 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
3455 op1 = expand_debug_expr
3456 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
3458 if (!op1)
3459 return NULL;
3461 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
3462 XVECEXP (op0, 0, i) = op1;
3465 return op0;
3467 else
3468 goto flag_unsupported;
3470 case CALL_EXPR:
3471 /* ??? Maybe handle some builtins? */
3472 return NULL;
3474 case SSA_NAME:
3476 gimple g = get_gimple_for_ssa_name (exp);
3477 if (g)
3479 op0 = expand_debug_expr (gimple_assign_rhs_to_tree (g));
3480 if (!op0)
3481 return NULL;
3483 else
3485 int part = var_to_partition (SA.map, exp);
3487 if (part == NO_PARTITION)
3489 /* If this is a reference to an incoming value of parameter
3490 that is never used in the code or where the incoming
3491 value is never used in the code, use PARM_DECL's
3492 DECL_RTL if set. */
3493 if (SSA_NAME_IS_DEFAULT_DEF (exp)
3494 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL)
3496 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
3497 if (op0)
3498 goto adjust_mode;
3499 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
3500 if (op0)
3501 goto adjust_mode;
3503 return NULL;
3506 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
3508 op0 = copy_rtx (SA.partition_to_pseudo[part]);
3510 goto adjust_mode;
3513 case ERROR_MARK:
3514 return NULL;
3516 /* Vector stuff. For most of the codes we don't have rtl codes. */
3517 case REALIGN_LOAD_EXPR:
3518 case REDUC_MAX_EXPR:
3519 case REDUC_MIN_EXPR:
3520 case REDUC_PLUS_EXPR:
3521 case VEC_COND_EXPR:
3522 case VEC_LSHIFT_EXPR:
3523 case VEC_PACK_FIX_TRUNC_EXPR:
3524 case VEC_PACK_SAT_EXPR:
3525 case VEC_PACK_TRUNC_EXPR:
3526 case VEC_RSHIFT_EXPR:
3527 case VEC_UNPACK_FLOAT_HI_EXPR:
3528 case VEC_UNPACK_FLOAT_LO_EXPR:
3529 case VEC_UNPACK_HI_EXPR:
3530 case VEC_UNPACK_LO_EXPR:
3531 case VEC_WIDEN_MULT_HI_EXPR:
3532 case VEC_WIDEN_MULT_LO_EXPR:
3533 case VEC_WIDEN_MULT_EVEN_EXPR:
3534 case VEC_WIDEN_MULT_ODD_EXPR:
3535 case VEC_WIDEN_LSHIFT_HI_EXPR:
3536 case VEC_WIDEN_LSHIFT_LO_EXPR:
3537 case VEC_PERM_EXPR:
3538 return NULL;
3540 /* Misc codes. */
3541 case ADDR_SPACE_CONVERT_EXPR:
3542 case FIXED_CONVERT_EXPR:
3543 case OBJ_TYPE_REF:
3544 case WITH_SIZE_EXPR:
3545 return NULL;
3547 case DOT_PROD_EXPR:
3548 if (SCALAR_INT_MODE_P (GET_MODE (op0))
3549 && SCALAR_INT_MODE_P (mode))
3552 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
3553 0)))
3554 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
3555 inner_mode);
3557 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
3558 1)))
3559 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
3560 inner_mode);
3561 op0 = simplify_gen_binary (MULT, mode, op0, op1);
3562 return simplify_gen_binary (PLUS, mode, op0, op2);
3564 return NULL;
3566 case WIDEN_MULT_EXPR:
3567 case WIDEN_MULT_PLUS_EXPR:
3568 case WIDEN_MULT_MINUS_EXPR:
3569 if (SCALAR_INT_MODE_P (GET_MODE (op0))
3570 && SCALAR_INT_MODE_P (mode))
3572 inner_mode = GET_MODE (op0);
3573 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3574 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
3575 else
3576 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
3577 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
3578 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
3579 else
3580 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
3581 op0 = simplify_gen_binary (MULT, mode, op0, op1);
3582 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
3583 return op0;
3584 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
3585 return simplify_gen_binary (PLUS, mode, op0, op2);
3586 else
3587 return simplify_gen_binary (MINUS, mode, op2, op0);
3589 return NULL;
3591 case MULT_HIGHPART_EXPR:
3592 /* ??? Similar to the above. */
3593 return NULL;
3595 case WIDEN_SUM_EXPR:
3596 case WIDEN_LSHIFT_EXPR:
3597 if (SCALAR_INT_MODE_P (GET_MODE (op0))
3598 && SCALAR_INT_MODE_P (mode))
3601 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
3602 0)))
3603 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
3604 inner_mode);
3605 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
3606 ? ASHIFT : PLUS, mode, op0, op1);
3608 return NULL;
3610 case FMA_EXPR:
3611 return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
3613 default:
3614 flag_unsupported:
3615 #ifdef ENABLE_CHECKING
3616 debug_tree (exp);
3617 gcc_unreachable ();
3618 #else
3619 return NULL;
3620 #endif
3624 /* Return an RTX equivalent to the source bind value of the tree expression
3625 EXP. */
3627 static rtx
3628 expand_debug_source_expr (tree exp)
3630 rtx op0 = NULL_RTX;
3631 enum machine_mode mode = VOIDmode, inner_mode;
3633 switch (TREE_CODE (exp))
3635 case PARM_DECL:
3637 mode = DECL_MODE (exp);
3638 op0 = expand_debug_parm_decl (exp);
3639 if (op0)
3640 break;
3641 /* See if this isn't an argument that has been completely
3642 optimized out. */
3643 if (!DECL_RTL_SET_P (exp)
3644 && !DECL_INCOMING_RTL (exp)
3645 && DECL_ABSTRACT_ORIGIN (current_function_decl))
3647 tree aexp = DECL_ORIGIN (exp);
3648 if (DECL_CONTEXT (aexp)
3649 == DECL_ABSTRACT_ORIGIN (current_function_decl))
3651 vec<tree, va_gc> **debug_args;
3652 unsigned int ix;
3653 tree ddecl;
3654 debug_args = decl_debug_args_lookup (current_function_decl);
3655 if (debug_args != NULL)
3657 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
3658 ix += 2)
3659 if (ddecl == aexp)
3660 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
3664 break;
3666 default:
3667 break;
3670 if (op0 == NULL_RTX)
3671 return NULL_RTX;
3673 inner_mode = GET_MODE (op0);
3674 if (mode == inner_mode)
3675 return op0;
3677 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
3679 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
3680 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
3681 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
3682 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
3683 else
3684 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
3686 else if (FLOAT_MODE_P (mode))
3687 gcc_unreachable ();
3688 else if (FLOAT_MODE_P (inner_mode))
3690 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
3691 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
3692 else
3693 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
3695 else if (CONSTANT_P (op0)
3696 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
3697 op0 = simplify_gen_subreg (mode, op0, inner_mode,
3698 subreg_lowpart_offset (mode, inner_mode));
3699 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
3700 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
3701 else
3702 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
3704 return op0;
3707 /* Expand the _LOCs in debug insns. We run this after expanding all
3708 regular insns, so that any variables referenced in the function
3709 will have their DECL_RTLs set. */
3711 static void
3712 expand_debug_locations (void)
3714 rtx insn;
3715 rtx last = get_last_insn ();
3716 int save_strict_alias = flag_strict_aliasing;
3718 /* New alias sets while setting up memory attributes cause
3719 -fcompare-debug failures, even though it doesn't bring about any
3720 codegen changes. */
3721 flag_strict_aliasing = 0;
3723 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3724 if (DEBUG_INSN_P (insn))
3726 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
3727 rtx val;
3728 enum machine_mode mode;
3730 if (value == NULL_TREE)
3731 val = NULL_RTX;
3732 else
3734 if (INSN_VAR_LOCATION_STATUS (insn)
3735 == VAR_INIT_STATUS_UNINITIALIZED)
3736 val = expand_debug_source_expr (value);
3737 else
3738 val = expand_debug_expr (value);
3739 gcc_assert (last == get_last_insn ());
3742 if (!val)
3743 val = gen_rtx_UNKNOWN_VAR_LOC ();
3744 else
3746 mode = GET_MODE (INSN_VAR_LOCATION (insn));
3748 gcc_assert (mode == GET_MODE (val)
3749 || (GET_MODE (val) == VOIDmode
3750 && (CONST_SCALAR_INT_P (val)
3751 || GET_CODE (val) == CONST_FIXED
3752 || GET_CODE (val) == LABEL_REF)));
3755 INSN_VAR_LOCATION_LOC (insn) = val;
3758 flag_strict_aliasing = save_strict_alias;
3761 /* Expand basic block BB from GIMPLE trees to RTL. */
3763 static basic_block
3764 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
3766 gimple_stmt_iterator gsi;
3767 gimple_seq stmts;
3768 gimple stmt = NULL;
3769 rtx note, last;
3770 edge e;
3771 edge_iterator ei;
3772 void **elt;
3774 if (dump_file)
3775 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
3776 bb->index);
3778 /* Note that since we are now transitioning from GIMPLE to RTL, we
3779 cannot use the gsi_*_bb() routines because they expect the basic
3780 block to be in GIMPLE, instead of RTL. Therefore, we need to
3781 access the BB sequence directly. */
3782 stmts = bb_seq (bb);
3783 bb->il.gimple.seq = NULL;
3784 bb->il.gimple.phi_nodes = NULL;
3785 rtl_profile_for_bb (bb);
3786 init_rtl_bb_info (bb);
3787 bb->flags |= BB_RTL;
3789 /* Remove the RETURN_EXPR if we may fall though to the exit
3790 instead. */
3791 gsi = gsi_last (stmts);
3792 if (!gsi_end_p (gsi)
3793 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
3795 gimple ret_stmt = gsi_stmt (gsi);
3797 gcc_assert (single_succ_p (bb));
3798 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
3800 if (bb->next_bb == EXIT_BLOCK_PTR
3801 && !gimple_return_retval (ret_stmt))
3803 gsi_remove (&gsi, false);
3804 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
3808 gsi = gsi_start (stmts);
3809 if (!gsi_end_p (gsi))
3811 stmt = gsi_stmt (gsi);
3812 if (gimple_code (stmt) != GIMPLE_LABEL)
3813 stmt = NULL;
3816 elt = pointer_map_contains (lab_rtx_for_bb, bb);
3818 if (stmt || elt)
3820 last = get_last_insn ();
3822 if (stmt)
3824 expand_gimple_stmt (stmt);
3825 gsi_next (&gsi);
3828 if (elt)
3829 emit_label ((rtx) *elt);
3831 /* Java emits line number notes in the top of labels.
3832 ??? Make this go away once line number notes are obsoleted. */
3833 BB_HEAD (bb) = NEXT_INSN (last);
3834 if (NOTE_P (BB_HEAD (bb)))
3835 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
3836 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
3838 maybe_dump_rtl_for_gimple_stmt (stmt, last);
3840 else
3841 note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
3843 NOTE_BASIC_BLOCK (note) = bb;
3845 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3847 basic_block new_bb;
3849 stmt = gsi_stmt (gsi);
3851 /* If this statement is a non-debug one, and we generate debug
3852 insns, then this one might be the last real use of a TERed
3853 SSA_NAME, but where there are still some debug uses further
3854 down. Expanding the current SSA name in such further debug
3855 uses by their RHS might lead to wrong debug info, as coalescing
3856 might make the operands of such RHS be placed into the same
3857 pseudo as something else. Like so:
3858 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
3859 use(a_1);
3860 a_2 = ...
3861 #DEBUG ... => a_1
3862 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
3863 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
3864 the write to a_2 would actually have clobbered the place which
3865 formerly held a_0.
3867 So, instead of that, we recognize the situation, and generate
3868 debug temporaries at the last real use of TERed SSA names:
3869 a_1 = a_0 + 1;
3870 #DEBUG #D1 => a_1
3871 use(a_1);
3872 a_2 = ...
3873 #DEBUG ... => #D1
3875 if (MAY_HAVE_DEBUG_INSNS
3876 && SA.values
3877 && !is_gimple_debug (stmt))
3879 ssa_op_iter iter;
3880 tree op;
3881 gimple def;
3883 location_t sloc = curr_insn_location ();
3885 /* Look for SSA names that have their last use here (TERed
3886 names always have only one real use). */
3887 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3888 if ((def = get_gimple_for_ssa_name (op)))
3890 imm_use_iterator imm_iter;
3891 use_operand_p use_p;
3892 bool have_debug_uses = false;
3894 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
3896 if (gimple_debug_bind_p (USE_STMT (use_p)))
3898 have_debug_uses = true;
3899 break;
3903 if (have_debug_uses)
3905 /* OP is a TERed SSA name, with DEF it's defining
3906 statement, and where OP is used in further debug
3907 instructions. Generate a debug temporary, and
3908 replace all uses of OP in debug insns with that
3909 temporary. */
3910 gimple debugstmt;
3911 tree value = gimple_assign_rhs_to_tree (def);
3912 tree vexpr = make_node (DEBUG_EXPR_DECL);
3913 rtx val;
3914 enum machine_mode mode;
3916 set_curr_insn_location (gimple_location (def));
3918 DECL_ARTIFICIAL (vexpr) = 1;
3919 TREE_TYPE (vexpr) = TREE_TYPE (value);
3920 if (DECL_P (value))
3921 mode = DECL_MODE (value);
3922 else
3923 mode = TYPE_MODE (TREE_TYPE (value));
3924 DECL_MODE (vexpr) = mode;
3926 val = gen_rtx_VAR_LOCATION
3927 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3929 emit_debug_insn (val);
3931 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
3933 if (!gimple_debug_bind_p (debugstmt))
3934 continue;
3936 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
3937 SET_USE (use_p, vexpr);
3939 update_stmt (debugstmt);
3943 set_curr_insn_location (sloc);
3946 currently_expanding_gimple_stmt = stmt;
3948 /* Expand this statement, then evaluate the resulting RTL and
3949 fixup the CFG accordingly. */
3950 if (gimple_code (stmt) == GIMPLE_COND)
3952 new_bb = expand_gimple_cond (bb, stmt);
3953 if (new_bb)
3954 return new_bb;
3956 else if (gimple_debug_bind_p (stmt))
3958 location_t sloc = curr_insn_location ();
3959 gimple_stmt_iterator nsi = gsi;
3961 for (;;)
3963 tree var = gimple_debug_bind_get_var (stmt);
3964 tree value;
3965 rtx val;
3966 enum machine_mode mode;
3968 if (TREE_CODE (var) != DEBUG_EXPR_DECL
3969 && TREE_CODE (var) != LABEL_DECL
3970 && !target_for_debug_bind (var))
3971 goto delink_debug_stmt;
3973 if (gimple_debug_bind_has_value_p (stmt))
3974 value = gimple_debug_bind_get_value (stmt);
3975 else
3976 value = NULL_TREE;
3978 last = get_last_insn ();
3980 set_curr_insn_location (gimple_location (stmt));
3982 if (DECL_P (var))
3983 mode = DECL_MODE (var);
3984 else
3985 mode = TYPE_MODE (TREE_TYPE (var));
3987 val = gen_rtx_VAR_LOCATION
3988 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3990 emit_debug_insn (val);
3992 if (dump_file && (dump_flags & TDF_DETAILS))
3994 /* We can't dump the insn with a TREE where an RTX
3995 is expected. */
3996 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
3997 maybe_dump_rtl_for_gimple_stmt (stmt, last);
3998 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
4001 delink_debug_stmt:
4002 /* In order not to generate too many debug temporaries,
4003 we delink all uses of debug statements we already expanded.
4004 Therefore debug statements between definition and real
4005 use of TERed SSA names will continue to use the SSA name,
4006 and not be replaced with debug temps. */
4007 delink_stmt_imm_use (stmt);
4009 gsi = nsi;
4010 gsi_next (&nsi);
4011 if (gsi_end_p (nsi))
4012 break;
4013 stmt = gsi_stmt (nsi);
4014 if (!gimple_debug_bind_p (stmt))
4015 break;
4018 set_curr_insn_location (sloc);
4020 else if (gimple_debug_source_bind_p (stmt))
4022 location_t sloc = curr_insn_location ();
4023 tree var = gimple_debug_source_bind_get_var (stmt);
4024 tree value = gimple_debug_source_bind_get_value (stmt);
4025 rtx val;
4026 enum machine_mode mode;
4028 last = get_last_insn ();
4030 set_curr_insn_location (gimple_location (stmt));
4032 mode = DECL_MODE (var);
4034 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
4035 VAR_INIT_STATUS_UNINITIALIZED);
4037 emit_debug_insn (val);
4039 if (dump_file && (dump_flags & TDF_DETAILS))
4041 /* We can't dump the insn with a TREE where an RTX
4042 is expected. */
4043 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
4044 maybe_dump_rtl_for_gimple_stmt (stmt, last);
4045 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
4048 set_curr_insn_location (sloc);
4050 else
4052 if (is_gimple_call (stmt)
4053 && gimple_call_tail_p (stmt)
4054 && disable_tail_calls)
4055 gimple_call_set_tail (stmt, false);
4057 if (is_gimple_call (stmt) && gimple_call_tail_p (stmt))
4059 bool can_fallthru;
4060 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
4061 if (new_bb)
4063 if (can_fallthru)
4064 bb = new_bb;
4065 else
4066 return new_bb;
4069 else
4071 def_operand_p def_p;
4072 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
4074 if (def_p != NULL)
4076 /* Ignore this stmt if it is in the list of
4077 replaceable expressions. */
4078 if (SA.values
4079 && bitmap_bit_p (SA.values,
4080 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
4081 continue;
4083 last = expand_gimple_stmt (stmt);
4084 maybe_dump_rtl_for_gimple_stmt (stmt, last);
4089 currently_expanding_gimple_stmt = NULL;
4091 /* Expand implicit goto and convert goto_locus. */
4092 FOR_EACH_EDGE (e, ei, bb->succs)
4094 if (e->goto_locus != UNKNOWN_LOCATION)
4095 set_curr_insn_location (e->goto_locus);
4096 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
4098 emit_jump (label_rtx_for_bb (e->dest));
4099 e->flags &= ~EDGE_FALLTHRU;
4103 /* Expanded RTL can create a jump in the last instruction of block.
4104 This later might be assumed to be a jump to successor and break edge insertion.
4105 We need to insert dummy move to prevent this. PR41440. */
4106 if (single_succ_p (bb)
4107 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
4108 && (last = get_last_insn ())
4109 && JUMP_P (last))
4111 rtx dummy = gen_reg_rtx (SImode);
4112 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
4115 do_pending_stack_adjust ();
4117 /* Find the block tail. The last insn in the block is the insn
4118 before a barrier and/or table jump insn. */
4119 last = get_last_insn ();
4120 if (BARRIER_P (last))
4121 last = PREV_INSN (last);
4122 if (JUMP_TABLE_DATA_P (last))
4123 last = PREV_INSN (PREV_INSN (last));
4124 BB_END (bb) = last;
4126 update_bb_for_insn (bb);
4128 return bb;
4132 /* Create a basic block for initialization code. */
4134 static basic_block
4135 construct_init_block (void)
4137 basic_block init_block, first_block;
4138 edge e = NULL;
4139 int flags;
4141 /* Multiple entry points not supported yet. */
4142 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
4143 init_rtl_bb_info (ENTRY_BLOCK_PTR);
4144 init_rtl_bb_info (EXIT_BLOCK_PTR);
4145 ENTRY_BLOCK_PTR->flags |= BB_RTL;
4146 EXIT_BLOCK_PTR->flags |= BB_RTL;
4148 e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
4150 /* When entry edge points to first basic block, we don't need jump,
4151 otherwise we have to jump into proper target. */
4152 if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
4154 tree label = gimple_block_label (e->dest);
4156 emit_jump (label_rtx (label));
4157 flags = 0;
4159 else
4160 flags = EDGE_FALLTHRU;
4162 init_block = create_basic_block (NEXT_INSN (get_insns ()),
4163 get_last_insn (),
4164 ENTRY_BLOCK_PTR);
4165 init_block->frequency = ENTRY_BLOCK_PTR->frequency;
4166 init_block->count = ENTRY_BLOCK_PTR->count;
4167 if (current_loops && ENTRY_BLOCK_PTR->loop_father)
4168 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR->loop_father);
4169 if (e)
4171 first_block = e->dest;
4172 redirect_edge_succ (e, init_block);
4173 e = make_edge (init_block, first_block, flags);
4175 else
4176 e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
4177 e->probability = REG_BR_PROB_BASE;
4178 e->count = ENTRY_BLOCK_PTR->count;
4180 update_bb_for_insn (init_block);
4181 return init_block;
4184 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
4185 found in the block tree. */
4187 static void
4188 set_block_levels (tree block, int level)
4190 while (block)
4192 BLOCK_NUMBER (block) = level;
4193 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
4194 block = BLOCK_CHAIN (block);
4198 /* Create a block containing landing pads and similar stuff. */
4200 static void
4201 construct_exit_block (void)
4203 rtx head = get_last_insn ();
4204 rtx end;
4205 basic_block exit_block;
4206 edge e, e2;
4207 unsigned ix;
4208 edge_iterator ei;
4209 rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb);
4211 rtl_profile_for_bb (EXIT_BLOCK_PTR);
4213 /* Make sure the locus is set to the end of the function, so that
4214 epilogue line numbers and warnings are set properly. */
4215 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
4216 input_location = cfun->function_end_locus;
4218 /* Generate rtl for function exit. */
4219 expand_function_end ();
4221 end = get_last_insn ();
4222 if (head == end)
4223 return;
4224 /* While emitting the function end we could move end of the last basic block.
4226 BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end;
4227 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
4228 head = NEXT_INSN (head);
4229 exit_block = create_basic_block (NEXT_INSN (head), end,
4230 EXIT_BLOCK_PTR->prev_bb);
4231 exit_block->frequency = EXIT_BLOCK_PTR->frequency;
4232 exit_block->count = EXIT_BLOCK_PTR->count;
4233 if (current_loops && EXIT_BLOCK_PTR->loop_father)
4234 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR->loop_father);
4236 ix = 0;
4237 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
4239 e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
4240 if (!(e->flags & EDGE_ABNORMAL))
4241 redirect_edge_succ (e, exit_block);
4242 else
4243 ix++;
4246 e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
4247 e->probability = REG_BR_PROB_BASE;
4248 e->count = EXIT_BLOCK_PTR->count;
4249 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
4250 if (e2 != e)
4252 e->count -= e2->count;
4253 exit_block->count -= e2->count;
4254 exit_block->frequency -= EDGE_FREQUENCY (e2);
4256 if (e->count < 0)
4257 e->count = 0;
4258 if (exit_block->count < 0)
4259 exit_block->count = 0;
4260 if (exit_block->frequency < 0)
4261 exit_block->frequency = 0;
4262 update_bb_for_insn (exit_block);
4265 /* Helper function for discover_nonconstant_array_refs.
4266 Look for ARRAY_REF nodes with non-constant indexes and mark them
4267 addressable. */
4269 static tree
4270 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
4271 void *data ATTRIBUTE_UNUSED)
4273 tree t = *tp;
4275 if (IS_TYPE_OR_DECL_P (t))
4276 *walk_subtrees = 0;
4277 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
4279 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
4280 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
4281 && (!TREE_OPERAND (t, 2)
4282 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
4283 || (TREE_CODE (t) == COMPONENT_REF
4284 && (!TREE_OPERAND (t,2)
4285 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
4286 || TREE_CODE (t) == BIT_FIELD_REF
4287 || TREE_CODE (t) == REALPART_EXPR
4288 || TREE_CODE (t) == IMAGPART_EXPR
4289 || TREE_CODE (t) == VIEW_CONVERT_EXPR
4290 || CONVERT_EXPR_P (t))
4291 t = TREE_OPERAND (t, 0);
4293 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
4295 t = get_base_address (t);
4296 if (t && DECL_P (t)
4297 && DECL_MODE (t) != BLKmode)
4298 TREE_ADDRESSABLE (t) = 1;
4301 *walk_subtrees = 0;
4304 return NULL_TREE;
4307 /* RTL expansion is not able to compile array references with variable
4308 offsets for arrays stored in single register. Discover such
4309 expressions and mark variables as addressable to avoid this
4310 scenario. */
4312 static void
4313 discover_nonconstant_array_refs (void)
4315 basic_block bb;
4316 gimple_stmt_iterator gsi;
4318 FOR_EACH_BB (bb)
4319 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4321 gimple stmt = gsi_stmt (gsi);
4322 if (!is_gimple_debug (stmt))
4323 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
4327 /* This function sets crtl->args.internal_arg_pointer to a virtual
4328 register if DRAP is needed. Local register allocator will replace
4329 virtual_incoming_args_rtx with the virtual register. */
4331 static void
4332 expand_stack_alignment (void)
4334 rtx drap_rtx;
4335 unsigned int preferred_stack_boundary;
4337 if (! SUPPORTS_STACK_ALIGNMENT)
4338 return;
4340 if (cfun->calls_alloca
4341 || cfun->has_nonlocal_label
4342 || crtl->has_nonlocal_goto)
4343 crtl->need_drap = true;
4345 /* Call update_stack_boundary here again to update incoming stack
4346 boundary. It may set incoming stack alignment to a different
4347 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
4348 use the minimum incoming stack alignment to check if it is OK
4349 to perform sibcall optimization since sibcall optimization will
4350 only align the outgoing stack to incoming stack boundary. */
4351 if (targetm.calls.update_stack_boundary)
4352 targetm.calls.update_stack_boundary ();
4354 /* The incoming stack frame has to be aligned at least at
4355 parm_stack_boundary. */
4356 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
4358 /* Update crtl->stack_alignment_estimated and use it later to align
4359 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
4360 exceptions since callgraph doesn't collect incoming stack alignment
4361 in this case. */
4362 if (cfun->can_throw_non_call_exceptions
4363 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
4364 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
4365 else
4366 preferred_stack_boundary = crtl->preferred_stack_boundary;
4367 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
4368 crtl->stack_alignment_estimated = preferred_stack_boundary;
4369 if (preferred_stack_boundary > crtl->stack_alignment_needed)
4370 crtl->stack_alignment_needed = preferred_stack_boundary;
4372 gcc_assert (crtl->stack_alignment_needed
4373 <= crtl->stack_alignment_estimated);
4375 crtl->stack_realign_needed
4376 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
4377 crtl->stack_realign_tried = crtl->stack_realign_needed;
4379 crtl->stack_realign_processed = true;
4381 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
4382 alignment. */
4383 gcc_assert (targetm.calls.get_drap_rtx != NULL);
4384 drap_rtx = targetm.calls.get_drap_rtx ();
4386 /* stack_realign_drap and drap_rtx must match. */
4387 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
4389 /* Do nothing if NULL is returned, which means DRAP is not needed. */
4390 if (NULL != drap_rtx)
4392 crtl->args.internal_arg_pointer = drap_rtx;
4394 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
4395 needed. */
4396 fixup_tail_calls ();
4400 /* Translate the intermediate representation contained in the CFG
4401 from GIMPLE trees to RTL.
4403 We do conversion per basic block and preserve/update the tree CFG.
4404 This implies we have to do some magic as the CFG can simultaneously
4405 consist of basic blocks containing RTL and GIMPLE trees. This can
4406 confuse the CFG hooks, so be careful to not manipulate CFG during
4407 the expansion. */
4409 static unsigned int
4410 gimple_expand_cfg (void)
4412 basic_block bb, init_block;
4413 sbitmap blocks;
4414 edge_iterator ei;
4415 edge e;
4416 rtx var_seq, var_ret_seq;
4417 unsigned i;
4419 timevar_push (TV_OUT_OF_SSA);
4420 rewrite_out_of_ssa (&SA);
4421 timevar_pop (TV_OUT_OF_SSA);
4422 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
4424 /* Make sure all values used by the optimization passes have sane
4425 defaults. */
4426 reg_renumber = 0;
4428 /* Some backends want to know that we are expanding to RTL. */
4429 currently_expanding_to_rtl = 1;
4430 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
4431 free_dominance_info (CDI_DOMINATORS);
4433 rtl_profile_for_bb (ENTRY_BLOCK_PTR);
4435 insn_locations_init ();
4436 if (!DECL_IS_BUILTIN (current_function_decl))
4438 /* Eventually, all FEs should explicitly set function_start_locus. */
4439 if (LOCATION_LOCUS (cfun->function_start_locus) == UNKNOWN_LOCATION)
4440 set_curr_insn_location
4441 (DECL_SOURCE_LOCATION (current_function_decl));
4442 else
4443 set_curr_insn_location (cfun->function_start_locus);
4445 else
4446 set_curr_insn_location (UNKNOWN_LOCATION);
4447 prologue_location = curr_insn_location ();
4449 #ifdef INSN_SCHEDULING
4450 init_sched_attrs ();
4451 #endif
4453 /* Make sure first insn is a note even if we don't want linenums.
4454 This makes sure the first insn will never be deleted.
4455 Also, final expects a note to appear there. */
4456 emit_note (NOTE_INSN_DELETED);
4458 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
4459 discover_nonconstant_array_refs ();
4461 targetm.expand_to_rtl_hook ();
4462 crtl->stack_alignment_needed = STACK_BOUNDARY;
4463 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
4464 crtl->stack_alignment_estimated = 0;
4465 crtl->preferred_stack_boundary = STACK_BOUNDARY;
4466 cfun->cfg->max_jumptable_ents = 0;
4468 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
4469 of the function section at exapnsion time to predict distance of calls. */
4470 resolve_unique_section (current_function_decl, 0, flag_function_sections);
4472 /* Expand the variables recorded during gimple lowering. */
4473 timevar_push (TV_VAR_EXPAND);
4474 start_sequence ();
4476 var_ret_seq = expand_used_vars ();
4478 var_seq = get_insns ();
4479 end_sequence ();
4480 timevar_pop (TV_VAR_EXPAND);
4482 /* Honor stack protection warnings. */
4483 if (warn_stack_protect)
4485 if (cfun->calls_alloca)
4486 warning (OPT_Wstack_protector,
4487 "stack protector not protecting local variables: "
4488 "variable length buffer");
4489 if (has_short_buffer && !crtl->stack_protect_guard)
4490 warning (OPT_Wstack_protector,
4491 "stack protector not protecting function: "
4492 "all local arrays are less than %d bytes long",
4493 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
4496 /* Set up parameters and prepare for return, for the function. */
4497 expand_function_start (current_function_decl);
4499 /* If we emitted any instructions for setting up the variables,
4500 emit them before the FUNCTION_START note. */
4501 if (var_seq)
4503 emit_insn_before (var_seq, parm_birth_insn);
4505 /* In expand_function_end we'll insert the alloca save/restore
4506 before parm_birth_insn. We've just insertted an alloca call.
4507 Adjust the pointer to match. */
4508 parm_birth_insn = var_seq;
4511 /* Now that we also have the parameter RTXs, copy them over to our
4512 partitions. */
4513 for (i = 0; i < SA.map->num_partitions; i++)
4515 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
4517 if (TREE_CODE (var) != VAR_DECL
4518 && !SA.partition_to_pseudo[i])
4519 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
4520 gcc_assert (SA.partition_to_pseudo[i]);
4522 /* If this decl was marked as living in multiple places, reset
4523 this now to NULL. */
4524 if (DECL_RTL_IF_SET (var) == pc_rtx)
4525 SET_DECL_RTL (var, NULL);
4527 /* Some RTL parts really want to look at DECL_RTL(x) when x
4528 was a decl marked in REG_ATTR or MEM_ATTR. We could use
4529 SET_DECL_RTL here making this available, but that would mean
4530 to select one of the potentially many RTLs for one DECL. Instead
4531 of doing that we simply reset the MEM_EXPR of the RTL in question,
4532 then nobody can get at it and hence nobody can call DECL_RTL on it. */
4533 if (!DECL_RTL_SET_P (var))
4535 if (MEM_P (SA.partition_to_pseudo[i]))
4536 set_mem_expr (SA.partition_to_pseudo[i], NULL);
4540 /* If we have a class containing differently aligned pointers
4541 we need to merge those into the corresponding RTL pointer
4542 alignment. */
4543 for (i = 1; i < num_ssa_names; i++)
4545 tree name = ssa_name (i);
4546 int part;
4547 rtx r;
4549 if (!name
4550 /* We might have generated new SSA names in
4551 update_alias_info_with_stack_vars. They will have a NULL
4552 defining statements, and won't be part of the partitioning,
4553 so ignore those. */
4554 || !SSA_NAME_DEF_STMT (name))
4555 continue;
4556 part = var_to_partition (SA.map, name);
4557 if (part == NO_PARTITION)
4558 continue;
4560 /* Adjust all partition members to get the underlying decl of
4561 the representative which we might have created in expand_one_var. */
4562 if (SSA_NAME_VAR (name) == NULL_TREE)
4564 tree leader = partition_to_var (SA.map, part);
4565 gcc_assert (SSA_NAME_VAR (leader) != NULL_TREE);
4566 replace_ssa_name_symbol (name, SSA_NAME_VAR (leader));
4568 if (!POINTER_TYPE_P (TREE_TYPE (name)))
4569 continue;
4571 r = SA.partition_to_pseudo[part];
4572 if (REG_P (r))
4573 mark_reg_pointer (r, get_pointer_alignment (name));
4576 /* If this function is `main', emit a call to `__main'
4577 to run global initializers, etc. */
4578 if (DECL_NAME (current_function_decl)
4579 && MAIN_NAME_P (DECL_NAME (current_function_decl))
4580 && DECL_FILE_SCOPE_P (current_function_decl))
4581 expand_main_function ();
4583 /* Initialize the stack_protect_guard field. This must happen after the
4584 call to __main (if any) so that the external decl is initialized. */
4585 if (crtl->stack_protect_guard)
4586 stack_protect_prologue ();
4588 expand_phi_nodes (&SA);
4590 /* Register rtl specific functions for cfg. */
4591 rtl_register_cfg_hooks ();
4593 init_block = construct_init_block ();
4595 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
4596 remaining edges later. */
4597 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
4598 e->flags &= ~EDGE_EXECUTABLE;
4600 lab_rtx_for_bb = pointer_map_create ();
4601 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
4602 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
4604 if (MAY_HAVE_DEBUG_INSNS)
4605 expand_debug_locations ();
4607 /* Free stuff we no longer need after GIMPLE optimizations. */
4608 free_dominance_info (CDI_DOMINATORS);
4609 free_dominance_info (CDI_POST_DOMINATORS);
4610 delete_tree_cfg_annotations ();
4612 timevar_push (TV_OUT_OF_SSA);
4613 finish_out_of_ssa (&SA);
4614 timevar_pop (TV_OUT_OF_SSA);
4616 timevar_push (TV_POST_EXPAND);
4617 /* We are no longer in SSA form. */
4618 cfun->gimple_df->in_ssa_p = false;
4619 if (current_loops)
4620 loops_state_clear (LOOP_CLOSED_SSA);
4622 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
4623 conservatively to true until they are all profile aware. */
4624 pointer_map_destroy (lab_rtx_for_bb);
4625 free_histograms ();
4627 construct_exit_block ();
4628 insn_locations_finalize ();
4630 if (var_ret_seq)
4632 rtx after = return_label;
4633 rtx next = NEXT_INSN (after);
4634 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
4635 after = next;
4636 emit_insn_after (var_ret_seq, after);
4639 /* Zap the tree EH table. */
4640 set_eh_throw_stmt_table (cfun, NULL);
4642 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
4643 split edges which edge insertions might do. */
4644 rebuild_jump_labels (get_insns ());
4646 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
4648 edge e;
4649 edge_iterator ei;
4650 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
4652 if (e->insns.r)
4654 rebuild_jump_labels_chain (e->insns.r);
4655 /* Avoid putting insns before parm_birth_insn. */
4656 if (e->src == ENTRY_BLOCK_PTR
4657 && single_succ_p (ENTRY_BLOCK_PTR)
4658 && parm_birth_insn)
4660 rtx insns = e->insns.r;
4661 e->insns.r = NULL_RTX;
4662 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
4664 else
4665 commit_one_edge_insertion (e);
4667 else
4668 ei_next (&ei);
4672 /* We're done expanding trees to RTL. */
4673 currently_expanding_to_rtl = 0;
4675 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR, next_bb)
4677 edge e;
4678 edge_iterator ei;
4679 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
4681 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
4682 e->flags &= ~EDGE_EXECUTABLE;
4684 /* At the moment not all abnormal edges match the RTL
4685 representation. It is safe to remove them here as
4686 find_many_sub_basic_blocks will rediscover them.
4687 In the future we should get this fixed properly. */
4688 if ((e->flags & EDGE_ABNORMAL)
4689 && !(e->flags & EDGE_SIBCALL))
4690 remove_edge (e);
4691 else
4692 ei_next (&ei);
4696 blocks = sbitmap_alloc (last_basic_block);
4697 bitmap_ones (blocks);
4698 find_many_sub_basic_blocks (blocks);
4699 sbitmap_free (blocks);
4700 purge_all_dead_edges ();
4702 expand_stack_alignment ();
4704 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
4705 function. */
4706 if (crtl->tail_call_emit)
4707 fixup_tail_calls ();
4709 /* After initial rtl generation, call back to finish generating
4710 exception support code. We need to do this before cleaning up
4711 the CFG as the code does not expect dead landing pads. */
4712 if (cfun->eh->region_tree != NULL)
4713 finish_eh_generation ();
4715 /* Remove unreachable blocks, otherwise we cannot compute dominators
4716 which are needed for loop state verification. As a side-effect
4717 this also compacts blocks.
4718 ??? We cannot remove trivially dead insns here as for example
4719 the DRAP reg on i?86 is not magically live at this point.
4720 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
4721 cleanup_cfg (CLEANUP_NO_INSN_DEL);
4723 #ifdef ENABLE_CHECKING
4724 verify_flow_info ();
4725 #endif
4727 /* Initialize pseudos allocated for hard registers. */
4728 emit_initial_value_sets ();
4730 /* And finally unshare all RTL. */
4731 unshare_all_rtl ();
4733 /* There's no need to defer outputting this function any more; we
4734 know we want to output it. */
4735 DECL_DEFER_OUTPUT (current_function_decl) = 0;
4737 /* Now that we're done expanding trees to RTL, we shouldn't have any
4738 more CONCATs anywhere. */
4739 generating_concat_p = 0;
4741 if (dump_file)
4743 fprintf (dump_file,
4744 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
4745 /* And the pass manager will dump RTL for us. */
4748 /* If we're emitting a nested function, make sure its parent gets
4749 emitted as well. Doing otherwise confuses debug info. */
4751 tree parent;
4752 for (parent = DECL_CONTEXT (current_function_decl);
4753 parent != NULL_TREE;
4754 parent = get_containing_scope (parent))
4755 if (TREE_CODE (parent) == FUNCTION_DECL)
4756 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
4759 /* We are now committed to emitting code for this function. Do any
4760 preparation, such as emitting abstract debug info for the inline
4761 before it gets mangled by optimization. */
4762 if (cgraph_function_possibly_inlined_p (current_function_decl))
4763 (*debug_hooks->outlining_inline_function) (current_function_decl);
4765 TREE_ASM_WRITTEN (current_function_decl) = 1;
4767 /* After expanding, the return labels are no longer needed. */
4768 return_label = NULL;
4769 naked_return_label = NULL;
4771 /* After expanding, the tm_restart map is no longer needed. */
4772 if (cfun->gimple_df->tm_restart)
4774 htab_delete (cfun->gimple_df->tm_restart);
4775 cfun->gimple_df->tm_restart = NULL;
4778 /* Tag the blocks with a depth number so that change_scope can find
4779 the common parent easily. */
4780 set_block_levels (DECL_INITIAL (cfun->decl), 0);
4781 default_rtl_profile ();
4783 timevar_pop (TV_POST_EXPAND);
4785 return 0;
4788 struct rtl_opt_pass pass_expand =
4791 RTL_PASS,
4792 "expand", /* name */
4793 OPTGROUP_NONE, /* optinfo_flags */
4794 NULL, /* gate */
4795 gimple_expand_cfg, /* execute */
4796 NULL, /* sub */
4797 NULL, /* next */
4798 0, /* static_pass_number */
4799 TV_EXPAND, /* tv_id */
4800 PROP_ssa | PROP_gimple_leh | PROP_cfg
4801 | PROP_gimple_lcx, /* properties_required */
4802 PROP_rtl, /* properties_provided */
4803 PROP_ssa | PROP_trees, /* properties_destroyed */
4804 TODO_verify_ssa | TODO_verify_flow
4805 | TODO_verify_stmts, /* todo_flags_start */
4806 TODO_ggc_collect /* todo_flags_finish */