Bring in all split-stack work done over on gccgo branch.
[official-gcc.git] / gcc / cfgexpand.c
blob1e67e7789789dfc9fd83445d4d1c4cf02861f56f
1 /* A pass for lowering trees to RTL.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "expr.h"
31 #include "langhooks.h"
32 #include "tree-flow.h"
33 #include "timevar.h"
34 #include "tree-dump.h"
35 #include "tree-pass.h"
36 #include "except.h"
37 #include "flags.h"
38 #include "diagnostic.h"
39 #include "tree-pretty-print.h"
40 #include "gimple-pretty-print.h"
41 #include "toplev.h"
42 #include "debug.h"
43 #include "params.h"
44 #include "tree-inline.h"
45 #include "value-prof.h"
46 #include "target.h"
47 #include "ssaexpand.h"
48 #include "bitmap.h"
49 #include "sbitmap.h"
50 #include "insn-attr.h" /* For INSN_SCHEDULING. */
52 /* This variable holds information helping the rewriting of SSA trees
53 into RTL. */
54 struct ssaexpand SA;
56 /* This variable holds the currently expanded gimple statement for purposes
57 of comminucating the profile info to the builtin expanders. */
58 gimple currently_expanding_gimple_stmt;
60 /* Return an expression tree corresponding to the RHS of GIMPLE
61 statement STMT. */
63 tree
64 gimple_assign_rhs_to_tree (gimple stmt)
66 tree t;
67 enum gimple_rhs_class grhs_class;
69 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
71 if (grhs_class == GIMPLE_TERNARY_RHS)
72 t = build3 (gimple_assign_rhs_code (stmt),
73 TREE_TYPE (gimple_assign_lhs (stmt)),
74 gimple_assign_rhs1 (stmt),
75 gimple_assign_rhs2 (stmt),
76 gimple_assign_rhs3 (stmt));
77 else if (grhs_class == GIMPLE_BINARY_RHS)
78 t = build2 (gimple_assign_rhs_code (stmt),
79 TREE_TYPE (gimple_assign_lhs (stmt)),
80 gimple_assign_rhs1 (stmt),
81 gimple_assign_rhs2 (stmt));
82 else if (grhs_class == GIMPLE_UNARY_RHS)
83 t = build1 (gimple_assign_rhs_code (stmt),
84 TREE_TYPE (gimple_assign_lhs (stmt)),
85 gimple_assign_rhs1 (stmt));
86 else if (grhs_class == GIMPLE_SINGLE_RHS)
88 t = gimple_assign_rhs1 (stmt);
89 /* Avoid modifying this tree in place below. */
90 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
91 && gimple_location (stmt) != EXPR_LOCATION (t))
92 || (gimple_block (stmt)
93 && currently_expanding_to_rtl
94 && EXPR_P (t)
95 && gimple_block (stmt) != TREE_BLOCK (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));
103 if (gimple_block (stmt) && currently_expanding_to_rtl && EXPR_P (t))
104 TREE_BLOCK (t) = gimple_block (stmt);
106 return t;
110 #ifndef STACK_ALIGNMENT_NEEDED
111 #define STACK_ALIGNMENT_NEEDED 1
112 #endif
114 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
116 /* Associate declaration T with storage space X. If T is no
117 SSA name this is exactly SET_DECL_RTL, otherwise make the
118 partition of T associated with X. */
119 static inline void
120 set_rtl (tree t, rtx x)
122 if (TREE_CODE (t) == SSA_NAME)
124 SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
125 if (x && !MEM_P (x))
126 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
127 /* For the benefit of debug information at -O0 (where vartracking
128 doesn't run) record the place also in the base DECL if it's
129 a normal variable (not a parameter). */
130 if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
132 tree var = SSA_NAME_VAR (t);
133 /* If we don't yet have something recorded, just record it now. */
134 if (!DECL_RTL_SET_P (var))
135 SET_DECL_RTL (var, x);
136 /* If we have it set alrady to "multiple places" don't
137 change this. */
138 else if (DECL_RTL (var) == pc_rtx)
140 /* If we have something recorded and it's not the same place
141 as we want to record now, we have multiple partitions for the
142 same base variable, with different places. We can't just
143 randomly chose one, hence we have to say that we don't know.
144 This only happens with optimization, and there var-tracking
145 will figure out the right thing. */
146 else if (DECL_RTL (var) != x)
147 SET_DECL_RTL (var, pc_rtx);
150 else
151 SET_DECL_RTL (t, x);
154 /* This structure holds data relevant to one variable that will be
155 placed in a stack slot. */
156 struct stack_var
158 /* The Variable. */
159 tree decl;
161 /* The offset of the variable. During partitioning, this is the
162 offset relative to the partition. After partitioning, this
163 is relative to the stack frame. */
164 HOST_WIDE_INT offset;
166 /* Initially, the size of the variable. Later, the size of the partition,
167 if this variable becomes it's partition's representative. */
168 HOST_WIDE_INT size;
170 /* The *byte* alignment required for this variable. Or as, with the
171 size, the alignment for this partition. */
172 unsigned int alignb;
174 /* The partition representative. */
175 size_t representative;
177 /* The next stack variable in the partition, or EOC. */
178 size_t next;
180 /* The numbers of conflicting stack variables. */
181 bitmap conflicts;
184 #define EOC ((size_t)-1)
186 /* We have an array of such objects while deciding allocation. */
187 static struct stack_var *stack_vars;
188 static size_t stack_vars_alloc;
189 static size_t stack_vars_num;
191 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
192 is non-decreasing. */
193 static size_t *stack_vars_sorted;
195 /* The phase of the stack frame. This is the known misalignment of
196 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
197 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
198 static int frame_phase;
200 /* Used during expand_used_vars to remember if we saw any decls for
201 which we'd like to enable stack smashing protection. */
202 static bool has_protected_decls;
204 /* Used during expand_used_vars. Remember if we say a character buffer
205 smaller than our cutoff threshold. Used for -Wstack-protector. */
206 static bool has_short_buffer;
208 /* Update stack alignment requirement. */
210 static void
211 update_stack_alignment (unsigned int align)
213 if (SUPPORTS_STACK_ALIGNMENT)
215 if (crtl->stack_alignment_estimated < align)
217 gcc_assert(!crtl->stack_realign_processed);
218 crtl->stack_alignment_estimated = align;
222 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
223 So here we only make sure stack_alignment_needed >= align. */
224 if (crtl->stack_alignment_needed < align)
225 crtl->stack_alignment_needed = align;
226 if (crtl->max_used_stack_slot_alignment < align)
227 crtl->max_used_stack_slot_alignment = align;
230 /* Discover the byte alignment to use for DECL. Ignore alignment
231 we can't do with expected alignment of the stack boundary. */
233 static unsigned int
234 get_decl_align_unit (tree decl)
236 unsigned int align;
238 align = LOCAL_DECL_ALIGNMENT (decl);
240 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
241 align = MAX_SUPPORTED_STACK_ALIGNMENT;
243 update_stack_alignment (align);
245 return align / BITS_PER_UNIT;
248 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
249 Return the frame offset. */
251 static HOST_WIDE_INT
252 alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align)
254 HOST_WIDE_INT offset, new_frame_offset;
256 new_frame_offset = frame_offset;
257 if (FRAME_GROWS_DOWNWARD)
259 new_frame_offset -= size + frame_phase;
260 new_frame_offset &= -align;
261 new_frame_offset += frame_phase;
262 offset = new_frame_offset;
264 else
266 new_frame_offset -= frame_phase;
267 new_frame_offset += align - 1;
268 new_frame_offset &= -align;
269 new_frame_offset += frame_phase;
270 offset = new_frame_offset;
271 new_frame_offset += size;
273 frame_offset = new_frame_offset;
275 if (frame_offset_overflow (frame_offset, cfun->decl))
276 frame_offset = offset = 0;
278 return offset;
281 /* Accumulate DECL into STACK_VARS. */
283 static void
284 add_stack_var (tree decl)
286 if (stack_vars_num >= stack_vars_alloc)
288 if (stack_vars_alloc)
289 stack_vars_alloc = stack_vars_alloc * 3 / 2;
290 else
291 stack_vars_alloc = 32;
292 stack_vars
293 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
295 stack_vars[stack_vars_num].decl = decl;
296 stack_vars[stack_vars_num].offset = 0;
297 stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (SSAVAR (decl)), 1);
298 stack_vars[stack_vars_num].alignb = get_decl_align_unit (SSAVAR (decl));
300 /* All variables are initially in their own partition. */
301 stack_vars[stack_vars_num].representative = stack_vars_num;
302 stack_vars[stack_vars_num].next = EOC;
304 /* All variables initially conflict with no other. */
305 stack_vars[stack_vars_num].conflicts = NULL;
307 /* Ensure that this decl doesn't get put onto the list twice. */
308 set_rtl (decl, pc_rtx);
310 stack_vars_num++;
313 /* Make the decls associated with luid's X and Y conflict. */
315 static void
316 add_stack_var_conflict (size_t x, size_t y)
318 struct stack_var *a = &stack_vars[x];
319 struct stack_var *b = &stack_vars[y];
320 if (!a->conflicts)
321 a->conflicts = BITMAP_ALLOC (NULL);
322 if (!b->conflicts)
323 b->conflicts = BITMAP_ALLOC (NULL);
324 bitmap_set_bit (a->conflicts, y);
325 bitmap_set_bit (b->conflicts, x);
328 /* Check whether the decls associated with luid's X and Y conflict. */
330 static bool
331 stack_var_conflict_p (size_t x, size_t y)
333 struct stack_var *a = &stack_vars[x];
334 struct stack_var *b = &stack_vars[y];
335 if (!a->conflicts || !b->conflicts)
336 return false;
337 return bitmap_bit_p (a->conflicts, y);
340 /* Returns true if TYPE is or contains a union type. */
342 static bool
343 aggregate_contains_union_type (tree type)
345 tree field;
347 if (TREE_CODE (type) == UNION_TYPE
348 || TREE_CODE (type) == QUAL_UNION_TYPE)
349 return true;
350 if (TREE_CODE (type) == ARRAY_TYPE)
351 return aggregate_contains_union_type (TREE_TYPE (type));
352 if (TREE_CODE (type) != RECORD_TYPE)
353 return false;
355 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
356 if (TREE_CODE (field) == FIELD_DECL)
357 if (aggregate_contains_union_type (TREE_TYPE (field)))
358 return true;
360 return false;
363 /* A subroutine of expand_used_vars. If two variables X and Y have alias
364 sets that do not conflict, then do add a conflict for these variables
365 in the interference graph. We also need to make sure to add conflicts
366 for union containing structures. Else RTL alias analysis comes along
367 and due to type based aliasing rules decides that for two overlapping
368 union temporaries { short s; int i; } accesses to the same mem through
369 different types may not alias and happily reorders stores across
370 life-time boundaries of the temporaries (See PR25654).
371 We also have to mind MEM_IN_STRUCT_P and MEM_SCALAR_P. */
373 static void
374 add_alias_set_conflicts (void)
376 size_t i, j, n = stack_vars_num;
378 for (i = 0; i < n; ++i)
380 tree type_i = TREE_TYPE (stack_vars[i].decl);
381 bool aggr_i = AGGREGATE_TYPE_P (type_i);
382 bool contains_union;
384 contains_union = aggregate_contains_union_type (type_i);
385 for (j = 0; j < i; ++j)
387 tree type_j = TREE_TYPE (stack_vars[j].decl);
388 bool aggr_j = AGGREGATE_TYPE_P (type_j);
389 if (aggr_i != aggr_j
390 /* Either the objects conflict by means of type based
391 aliasing rules, or we need to add a conflict. */
392 || !objects_must_conflict_p (type_i, type_j)
393 /* In case the types do not conflict ensure that access
394 to elements will conflict. In case of unions we have
395 to be careful as type based aliasing rules may say
396 access to the same memory does not conflict. So play
397 safe and add a conflict in this case. */
398 || contains_union)
399 add_stack_var_conflict (i, j);
404 /* A subroutine of partition_stack_vars. A comparison function for qsort,
405 sorting an array of indices by the size and type of the object. */
407 static int
408 stack_var_size_cmp (const void *a, const void *b)
410 HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size;
411 HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size;
412 tree decla, declb;
413 unsigned int uida, uidb;
415 if (sa < sb)
416 return -1;
417 if (sa > sb)
418 return 1;
419 decla = stack_vars[*(const size_t *)a].decl;
420 declb = stack_vars[*(const size_t *)b].decl;
421 /* For stack variables of the same size use and id of the decls
422 to make the sort stable. Two SSA names are compared by their
423 version, SSA names come before non-SSA names, and two normal
424 decls are compared by their DECL_UID. */
425 if (TREE_CODE (decla) == SSA_NAME)
427 if (TREE_CODE (declb) == SSA_NAME)
428 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
429 else
430 return -1;
432 else if (TREE_CODE (declb) == SSA_NAME)
433 return 1;
434 else
435 uida = DECL_UID (decla), uidb = DECL_UID (declb);
436 if (uida < uidb)
437 return -1;
438 if (uida > uidb)
439 return 1;
440 return 0;
444 /* If the points-to solution *PI points to variables that are in a partition
445 together with other variables add all partition members to the pointed-to
446 variables bitmap. */
448 static void
449 add_partitioned_vars_to_ptset (struct pt_solution *pt,
450 struct pointer_map_t *decls_to_partitions,
451 struct pointer_set_t *visited, bitmap temp)
453 bitmap_iterator bi;
454 unsigned i;
455 bitmap *part;
457 if (pt->anything
458 || pt->vars == NULL
459 /* The pointed-to vars bitmap is shared, it is enough to
460 visit it once. */
461 || pointer_set_insert(visited, pt->vars))
462 return;
464 bitmap_clear (temp);
466 /* By using a temporary bitmap to store all members of the partitions
467 we have to add we make sure to visit each of the partitions only
468 once. */
469 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
470 if ((!temp
471 || !bitmap_bit_p (temp, i))
472 && (part = (bitmap *) pointer_map_contains (decls_to_partitions,
473 (void *)(size_t) i)))
474 bitmap_ior_into (temp, *part);
475 if (!bitmap_empty_p (temp))
476 bitmap_ior_into (pt->vars, temp);
479 /* Update points-to sets based on partition info, so we can use them on RTL.
480 The bitmaps representing stack partitions will be saved until expand,
481 where partitioned decls used as bases in memory expressions will be
482 rewritten. */
484 static void
485 update_alias_info_with_stack_vars (void)
487 struct pointer_map_t *decls_to_partitions = NULL;
488 size_t i, j;
489 tree var = NULL_TREE;
491 for (i = 0; i < stack_vars_num; i++)
493 bitmap part = NULL;
494 tree name;
495 struct ptr_info_def *pi;
497 /* Not interested in partitions with single variable. */
498 if (stack_vars[i].representative != i
499 || stack_vars[i].next == EOC)
500 continue;
502 if (!decls_to_partitions)
504 decls_to_partitions = pointer_map_create ();
505 cfun->gimple_df->decls_to_pointers = pointer_map_create ();
508 /* Create an SSA_NAME that points to the partition for use
509 as base during alias-oracle queries on RTL for bases that
510 have been partitioned. */
511 if (var == NULL_TREE)
512 var = create_tmp_var (ptr_type_node, NULL);
513 name = make_ssa_name (var, NULL);
515 /* Create bitmaps representing partitions. They will be used for
516 points-to sets later, so use GGC alloc. */
517 part = BITMAP_GGC_ALLOC ();
518 for (j = i; j != EOC; j = stack_vars[j].next)
520 tree decl = stack_vars[j].decl;
521 unsigned int uid = DECL_PT_UID (decl);
522 /* We should never end up partitioning SSA names (though they
523 may end up on the stack). Neither should we allocate stack
524 space to something that is unused and thus unreferenced. */
525 gcc_assert (DECL_P (decl)
526 && referenced_var_lookup (DECL_UID (decl)));
527 bitmap_set_bit (part, uid);
528 *((bitmap *) pointer_map_insert (decls_to_partitions,
529 (void *)(size_t) uid)) = part;
530 *((tree *) pointer_map_insert (cfun->gimple_df->decls_to_pointers,
531 decl)) = name;
534 /* Make the SSA name point to all partition members. */
535 pi = get_ptr_info (name);
536 pt_solution_set (&pi->pt, part, false, false);
539 /* Make all points-to sets that contain one member of a partition
540 contain all members of the partition. */
541 if (decls_to_partitions)
543 unsigned i;
544 struct pointer_set_t *visited = pointer_set_create ();
545 bitmap temp = BITMAP_ALLOC (NULL);
547 for (i = 1; i < num_ssa_names; i++)
549 tree name = ssa_name (i);
550 struct ptr_info_def *pi;
552 if (name
553 && POINTER_TYPE_P (TREE_TYPE (name))
554 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
555 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
556 visited, temp);
559 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
560 decls_to_partitions, visited, temp);
562 pointer_set_destroy (visited);
563 pointer_map_destroy (decls_to_partitions);
564 BITMAP_FREE (temp);
568 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
569 partitioning algorithm. Partitions A and B are known to be non-conflicting.
570 Merge them into a single partition A.
572 At the same time, add OFFSET to all variables in partition B. At the end
573 of the partitioning process we've have a nice block easy to lay out within
574 the stack frame. */
576 static void
577 union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset)
579 size_t i, last;
580 struct stack_var *vb = &stack_vars[b];
581 bitmap_iterator bi;
582 unsigned u;
584 /* Update each element of partition B with the given offset,
585 and merge them into partition A. */
586 for (last = i = b; i != EOC; last = i, i = stack_vars[i].next)
588 stack_vars[i].offset += offset;
589 stack_vars[i].representative = a;
591 stack_vars[last].next = stack_vars[a].next;
592 stack_vars[a].next = b;
594 /* Update the required alignment of partition A to account for B. */
595 if (stack_vars[a].alignb < stack_vars[b].alignb)
596 stack_vars[a].alignb = stack_vars[b].alignb;
598 /* Update the interference graph and merge the conflicts. */
599 if (vb->conflicts)
601 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
602 add_stack_var_conflict (a, stack_vars[u].representative);
603 BITMAP_FREE (vb->conflicts);
607 /* A subroutine of expand_used_vars. Binpack the variables into
608 partitions constrained by the interference graph. The overall
609 algorithm used is as follows:
611 Sort the objects by size.
612 For each object A {
613 S = size(A)
614 O = 0
615 loop {
616 Look for the largest non-conflicting object B with size <= S.
617 UNION (A, B)
618 offset(B) = O
619 O += size(B)
620 S -= size(B)
625 static void
626 partition_stack_vars (void)
628 size_t si, sj, n = stack_vars_num;
630 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
631 for (si = 0; si < n; ++si)
632 stack_vars_sorted[si] = si;
634 if (n == 1)
635 return;
637 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp);
639 for (si = 0; si < n; ++si)
641 size_t i = stack_vars_sorted[si];
642 HOST_WIDE_INT isize = stack_vars[i].size;
643 HOST_WIDE_INT offset = 0;
645 for (sj = si; sj-- > 0; )
647 size_t j = stack_vars_sorted[sj];
648 HOST_WIDE_INT jsize = stack_vars[j].size;
649 unsigned int jalign = stack_vars[j].alignb;
651 /* Ignore objects that aren't partition representatives. */
652 if (stack_vars[j].representative != j)
653 continue;
655 /* Ignore objects too large for the remaining space. */
656 if (isize < jsize)
657 continue;
659 /* Ignore conflicting objects. */
660 if (stack_var_conflict_p (i, j))
661 continue;
663 /* Refine the remaining space check to include alignment. */
664 if (offset & (jalign - 1))
666 HOST_WIDE_INT toff = offset;
667 toff += jalign - 1;
668 toff &= -(HOST_WIDE_INT)jalign;
669 if (isize - (toff - offset) < jsize)
670 continue;
672 isize -= toff - offset;
673 offset = toff;
676 /* UNION the objects, placing J at OFFSET. */
677 union_stack_vars (i, j, offset);
679 isize -= jsize;
680 if (isize == 0)
681 break;
685 if (optimize)
686 update_alias_info_with_stack_vars ();
689 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
691 static void
692 dump_stack_var_partition (void)
694 size_t si, i, j, n = stack_vars_num;
696 for (si = 0; si < n; ++si)
698 i = stack_vars_sorted[si];
700 /* Skip variables that aren't partition representatives, for now. */
701 if (stack_vars[i].representative != i)
702 continue;
704 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
705 " align %u\n", (unsigned long) i, stack_vars[i].size,
706 stack_vars[i].alignb);
708 for (j = i; j != EOC; j = stack_vars[j].next)
710 fputc ('\t', dump_file);
711 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
712 fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n",
713 stack_vars[j].offset);
718 /* Assign rtl to DECL at frame offset OFFSET. */
720 static void
721 expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset)
723 /* Alignment is unsigned. */
724 unsigned HOST_WIDE_INT align, max_align;
725 rtx x;
727 /* If this fails, we've overflowed the stack frame. Error nicely? */
728 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
730 x = plus_constant (virtual_stack_vars_rtx, offset);
731 x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
733 if (TREE_CODE (decl) != SSA_NAME)
735 /* Set alignment we actually gave this decl if it isn't an SSA name.
736 If it is we generate stack slots only accidentally so it isn't as
737 important, we'll simply use the alignment that is already set. */
738 offset -= frame_phase;
739 align = offset & -offset;
740 align *= BITS_PER_UNIT;
741 max_align = MAX (crtl->max_used_stack_slot_alignment,
742 PREFERRED_STACK_BOUNDARY);
743 if (align == 0 || align > max_align)
744 align = max_align;
746 update_stack_alignment (align);
747 DECL_ALIGN (decl) = align;
748 DECL_USER_ALIGN (decl) = 0;
751 set_mem_attributes (x, SSAVAR (decl), true);
752 set_rtl (decl, x);
755 /* A subroutine of expand_used_vars. Give each partition representative
756 a unique location within the stack frame. Update each partition member
757 with that location. */
759 static void
760 expand_stack_vars (bool (*pred) (tree))
762 size_t si, i, j, n = stack_vars_num;
764 for (si = 0; si < n; ++si)
766 HOST_WIDE_INT offset;
768 i = stack_vars_sorted[si];
770 /* Skip variables that aren't partition representatives, for now. */
771 if (stack_vars[i].representative != i)
772 continue;
774 /* Skip variables that have already had rtl assigned. See also
775 add_stack_var where we perpetrate this pc_rtx hack. */
776 if ((TREE_CODE (stack_vars[i].decl) == SSA_NAME
777 ? SA.partition_to_pseudo[var_to_partition (SA.map, stack_vars[i].decl)]
778 : DECL_RTL (stack_vars[i].decl)) != pc_rtx)
779 continue;
781 /* Check the predicate to see whether this variable should be
782 allocated in this pass. */
783 if (pred && !pred (stack_vars[i].decl))
784 continue;
786 offset = alloc_stack_frame_space (stack_vars[i].size,
787 stack_vars[i].alignb);
789 /* Create rtl for each variable based on their location within the
790 partition. */
791 for (j = i; j != EOC; j = stack_vars[j].next)
793 gcc_assert (stack_vars[j].offset <= stack_vars[i].size);
794 expand_one_stack_var_at (stack_vars[j].decl,
795 stack_vars[j].offset + offset);
800 /* Take into account all sizes of partitions and reset DECL_RTLs. */
801 static HOST_WIDE_INT
802 account_stack_vars (void)
804 size_t si, j, i, n = stack_vars_num;
805 HOST_WIDE_INT size = 0;
807 for (si = 0; si < n; ++si)
809 i = stack_vars_sorted[si];
811 /* Skip variables that aren't partition representatives, for now. */
812 if (stack_vars[i].representative != i)
813 continue;
815 size += stack_vars[i].size;
816 for (j = i; j != EOC; j = stack_vars[j].next)
817 set_rtl (stack_vars[j].decl, NULL);
819 return size;
822 /* A subroutine of expand_one_var. Called to immediately assign rtl
823 to a variable to be allocated in the stack frame. */
825 static void
826 expand_one_stack_var (tree var)
828 HOST_WIDE_INT size, offset, align;
830 size = tree_low_cst (DECL_SIZE_UNIT (SSAVAR (var)), 1);
831 align = get_decl_align_unit (SSAVAR (var));
832 offset = alloc_stack_frame_space (size, align);
834 expand_one_stack_var_at (var, offset);
837 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
838 that will reside in a hard register. */
840 static void
841 expand_one_hard_reg_var (tree var)
843 rest_of_decl_compilation (var, 0, 0);
846 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
847 that will reside in a pseudo register. */
849 static void
850 expand_one_register_var (tree var)
852 tree decl = SSAVAR (var);
853 tree type = TREE_TYPE (decl);
854 enum machine_mode reg_mode = promote_decl_mode (decl, NULL);
855 rtx x = gen_reg_rtx (reg_mode);
857 set_rtl (var, x);
859 /* Note if the object is a user variable. */
860 if (!DECL_ARTIFICIAL (decl))
861 mark_user_reg (x);
863 if (POINTER_TYPE_P (type))
864 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (type)));
867 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
868 has some associated error, e.g. its type is error-mark. We just need
869 to pick something that won't crash the rest of the compiler. */
871 static void
872 expand_one_error_var (tree var)
874 enum machine_mode mode = DECL_MODE (var);
875 rtx x;
877 if (mode == BLKmode)
878 x = gen_rtx_MEM (BLKmode, const0_rtx);
879 else if (mode == VOIDmode)
880 x = const0_rtx;
881 else
882 x = gen_reg_rtx (mode);
884 SET_DECL_RTL (var, x);
887 /* A subroutine of expand_one_var. VAR is a variable that will be
888 allocated to the local stack frame. Return true if we wish to
889 add VAR to STACK_VARS so that it will be coalesced with other
890 variables. Return false to allocate VAR immediately.
892 This function is used to reduce the number of variables considered
893 for coalescing, which reduces the size of the quadratic problem. */
895 static bool
896 defer_stack_allocation (tree var, bool toplevel)
898 /* If stack protection is enabled, *all* stack variables must be deferred,
899 so that we can re-order the strings to the top of the frame. */
900 if (flag_stack_protect)
901 return true;
903 /* Variables in the outermost scope automatically conflict with
904 every other variable. The only reason to want to defer them
905 at all is that, after sorting, we can more efficiently pack
906 small variables in the stack frame. Continue to defer at -O2. */
907 if (toplevel && optimize < 2)
908 return false;
910 /* Without optimization, *most* variables are allocated from the
911 stack, which makes the quadratic problem large exactly when we
912 want compilation to proceed as quickly as possible. On the
913 other hand, we don't want the function's stack frame size to
914 get completely out of hand. So we avoid adding scalars and
915 "small" aggregates to the list at all. */
916 if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32)
917 return false;
919 return true;
922 /* A subroutine of expand_used_vars. Expand one variable according to
923 its flavor. Variables to be placed on the stack are not actually
924 expanded yet, merely recorded.
925 When REALLY_EXPAND is false, only add stack values to be allocated.
926 Return stack usage this variable is supposed to take.
929 static HOST_WIDE_INT
930 expand_one_var (tree var, bool toplevel, bool really_expand)
932 tree origvar = var;
933 var = SSAVAR (var);
935 if (SUPPORTS_STACK_ALIGNMENT
936 && TREE_TYPE (var) != error_mark_node
937 && TREE_CODE (var) == VAR_DECL)
939 unsigned int align;
941 /* Because we don't know if VAR will be in register or on stack,
942 we conservatively assume it will be on stack even if VAR is
943 eventually put into register after RA pass. For non-automatic
944 variables, which won't be on stack, we collect alignment of
945 type and ignore user specified alignment. */
946 if (TREE_STATIC (var) || DECL_EXTERNAL (var))
947 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
948 TYPE_MODE (TREE_TYPE (var)),
949 TYPE_ALIGN (TREE_TYPE (var)));
950 else if (DECL_HAS_VALUE_EXPR_P (var)
951 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
952 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
953 or variables which were assigned a stack slot already by
954 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
955 changed from the offset chosen to it. */
956 align = crtl->stack_alignment_estimated;
957 else
958 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
960 if (crtl->stack_alignment_estimated < align)
962 /* stack_alignment_estimated shouldn't change after stack
963 realign decision made */
964 gcc_assert(!crtl->stack_realign_processed);
965 crtl->stack_alignment_estimated = align;
969 if (TREE_CODE (origvar) == SSA_NAME)
971 gcc_assert (TREE_CODE (var) != VAR_DECL
972 || (!DECL_EXTERNAL (var)
973 && !DECL_HAS_VALUE_EXPR_P (var)
974 && !TREE_STATIC (var)
975 && TREE_TYPE (var) != error_mark_node
976 && !DECL_HARD_REGISTER (var)
977 && really_expand));
979 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
981 else if (DECL_EXTERNAL (var))
983 else if (DECL_HAS_VALUE_EXPR_P (var))
985 else if (TREE_STATIC (var))
987 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
989 else if (TREE_TYPE (var) == error_mark_node)
991 if (really_expand)
992 expand_one_error_var (var);
994 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
996 if (really_expand)
997 expand_one_hard_reg_var (var);
999 else if (use_register_for_decl (var))
1001 if (really_expand)
1002 expand_one_register_var (origvar);
1004 else if (!host_integerp (DECL_SIZE_UNIT (var), 1))
1006 if (really_expand)
1008 error ("size of variable %q+D is too large", var);
1009 expand_one_error_var (var);
1012 else if (defer_stack_allocation (var, toplevel))
1013 add_stack_var (origvar);
1014 else
1016 if (really_expand)
1017 expand_one_stack_var (origvar);
1018 return tree_low_cst (DECL_SIZE_UNIT (var), 1);
1020 return 0;
1023 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1024 expanding variables. Those variables that can be put into registers
1025 are allocated pseudos; those that can't are put on the stack.
1027 TOPLEVEL is true if this is the outermost BLOCK. */
1029 static void
1030 expand_used_vars_for_block (tree block, bool toplevel)
1032 size_t i, j, old_sv_num, this_sv_num, new_sv_num;
1033 tree t;
1035 old_sv_num = toplevel ? 0 : stack_vars_num;
1037 /* Expand all variables at this level. */
1038 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1039 if (TREE_USED (t))
1040 expand_one_var (t, toplevel, true);
1042 this_sv_num = stack_vars_num;
1044 /* Expand all variables at containing levels. */
1045 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1046 expand_used_vars_for_block (t, false);
1048 /* Since we do not track exact variable lifetimes (which is not even
1049 possible for variables whose address escapes), we mirror the block
1050 tree in the interference graph. Here we cause all variables at this
1051 level, and all sublevels, to conflict. */
1052 if (old_sv_num < this_sv_num)
1054 new_sv_num = stack_vars_num;
1056 for (i = old_sv_num; i < new_sv_num; ++i)
1057 for (j = i < this_sv_num ? i : this_sv_num; j-- > old_sv_num ;)
1058 add_stack_var_conflict (i, j);
1062 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1063 and clear TREE_USED on all local variables. */
1065 static void
1066 clear_tree_used (tree block)
1068 tree t;
1070 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1071 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1072 TREE_USED (t) = 0;
1074 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1075 clear_tree_used (t);
1078 /* Examine TYPE and determine a bit mask of the following features. */
1080 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1081 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1082 #define SPCT_HAS_ARRAY 4
1083 #define SPCT_HAS_AGGREGATE 8
1085 static unsigned int
1086 stack_protect_classify_type (tree type)
1088 unsigned int ret = 0;
1089 tree t;
1091 switch (TREE_CODE (type))
1093 case ARRAY_TYPE:
1094 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1095 if (t == char_type_node
1096 || t == signed_char_type_node
1097 || t == unsigned_char_type_node)
1099 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1100 unsigned HOST_WIDE_INT len;
1102 if (!TYPE_SIZE_UNIT (type)
1103 || !host_integerp (TYPE_SIZE_UNIT (type), 1))
1104 len = max;
1105 else
1106 len = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
1108 if (len < max)
1109 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1110 else
1111 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1113 else
1114 ret = SPCT_HAS_ARRAY;
1115 break;
1117 case UNION_TYPE:
1118 case QUAL_UNION_TYPE:
1119 case RECORD_TYPE:
1120 ret = SPCT_HAS_AGGREGATE;
1121 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1122 if (TREE_CODE (t) == FIELD_DECL)
1123 ret |= stack_protect_classify_type (TREE_TYPE (t));
1124 break;
1126 default:
1127 break;
1130 return ret;
1133 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1134 part of the local stack frame. Remember if we ever return nonzero for
1135 any variable in this function. The return value is the phase number in
1136 which the variable should be allocated. */
1138 static int
1139 stack_protect_decl_phase (tree decl)
1141 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1142 int ret = 0;
1144 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1145 has_short_buffer = true;
1147 if (flag_stack_protect == 2)
1149 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1150 && !(bits & SPCT_HAS_AGGREGATE))
1151 ret = 1;
1152 else if (bits & SPCT_HAS_ARRAY)
1153 ret = 2;
1155 else
1156 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1158 if (ret)
1159 has_protected_decls = true;
1161 return ret;
1164 /* Two helper routines that check for phase 1 and phase 2. These are used
1165 as callbacks for expand_stack_vars. */
1167 static bool
1168 stack_protect_decl_phase_1 (tree decl)
1170 return stack_protect_decl_phase (decl) == 1;
1173 static bool
1174 stack_protect_decl_phase_2 (tree decl)
1176 return stack_protect_decl_phase (decl) == 2;
1179 /* Ensure that variables in different stack protection phases conflict
1180 so that they are not merged and share the same stack slot. */
1182 static void
1183 add_stack_protection_conflicts (void)
1185 size_t i, j, n = stack_vars_num;
1186 unsigned char *phase;
1188 phase = XNEWVEC (unsigned char, n);
1189 for (i = 0; i < n; ++i)
1190 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1192 for (i = 0; i < n; ++i)
1194 unsigned char ph_i = phase[i];
1195 for (j = 0; j < i; ++j)
1196 if (ph_i != phase[j])
1197 add_stack_var_conflict (i, j);
1200 XDELETEVEC (phase);
1203 /* Create a decl for the guard at the top of the stack frame. */
1205 static void
1206 create_stack_guard (void)
1208 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1209 VAR_DECL, NULL, ptr_type_node);
1210 TREE_THIS_VOLATILE (guard) = 1;
1211 TREE_USED (guard) = 1;
1212 expand_one_stack_var (guard);
1213 crtl->stack_protect_guard = guard;
1216 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1217 expanding variables. Those variables that can be put into registers
1218 are allocated pseudos; those that can't are put on the stack.
1220 TOPLEVEL is true if this is the outermost BLOCK. */
1222 static HOST_WIDE_INT
1223 account_used_vars_for_block (tree block, bool toplevel)
1225 tree t;
1226 HOST_WIDE_INT size = 0;
1228 /* Expand all variables at this level. */
1229 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1230 if (TREE_USED (t))
1231 size += expand_one_var (t, toplevel, false);
1233 /* Expand all variables at containing levels. */
1234 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1235 size += account_used_vars_for_block (t, false);
1237 return size;
1240 /* Prepare for expanding variables. */
1241 static void
1242 init_vars_expansion (void)
1244 tree t;
1245 unsigned ix;
1246 /* Set TREE_USED on all variables in the local_decls. */
1247 FOR_EACH_LOCAL_DECL (cfun, ix, t)
1248 TREE_USED (t) = 1;
1250 /* Clear TREE_USED on all variables associated with a block scope. */
1251 clear_tree_used (DECL_INITIAL (current_function_decl));
1253 /* Initialize local stack smashing state. */
1254 has_protected_decls = false;
1255 has_short_buffer = false;
1258 /* Free up stack variable graph data. */
1259 static void
1260 fini_vars_expansion (void)
1262 size_t i, n = stack_vars_num;
1263 for (i = 0; i < n; i++)
1264 BITMAP_FREE (stack_vars[i].conflicts);
1265 XDELETEVEC (stack_vars);
1266 XDELETEVEC (stack_vars_sorted);
1267 stack_vars = NULL;
1268 stack_vars_alloc = stack_vars_num = 0;
1271 /* Make a fair guess for the size of the stack frame of the decl
1272 passed. This doesn't have to be exact, the result is only used
1273 in the inline heuristics. So we don't want to run the full stack
1274 var packing algorithm (which is quadratic in the number of stack
1275 vars). Instead, we calculate the total size of all stack vars.
1276 This turns out to be a pretty fair estimate -- packing of stack
1277 vars doesn't happen very often. */
1279 HOST_WIDE_INT
1280 estimated_stack_frame_size (tree decl)
1282 HOST_WIDE_INT size = 0;
1283 size_t i;
1284 tree var, outer_block = DECL_INITIAL (current_function_decl);
1285 unsigned ix;
1286 tree old_cur_fun_decl = current_function_decl;
1287 current_function_decl = decl;
1288 push_cfun (DECL_STRUCT_FUNCTION (decl));
1290 init_vars_expansion ();
1292 FOR_EACH_LOCAL_DECL (cfun, ix, var)
1294 if (TREE_USED (var))
1295 size += expand_one_var (var, true, false);
1296 TREE_USED (var) = 1;
1298 size += account_used_vars_for_block (outer_block, true);
1300 if (stack_vars_num > 0)
1302 /* Fake sorting the stack vars for account_stack_vars (). */
1303 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1304 for (i = 0; i < stack_vars_num; ++i)
1305 stack_vars_sorted[i] = i;
1306 size += account_stack_vars ();
1307 fini_vars_expansion ();
1309 pop_cfun ();
1310 current_function_decl = old_cur_fun_decl;
1311 return size;
1314 /* Expand all variables used in the function. */
1316 static void
1317 expand_used_vars (void)
1319 tree var, outer_block = DECL_INITIAL (current_function_decl);
1320 VEC(tree,heap) *maybe_local_decls = NULL;
1321 unsigned i;
1322 unsigned len;
1324 /* Compute the phase of the stack frame for this function. */
1326 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1327 int off = STARTING_FRAME_OFFSET % align;
1328 frame_phase = off ? align - off : 0;
1331 init_vars_expansion ();
1333 for (i = 0; i < SA.map->num_partitions; i++)
1335 tree var = partition_to_var (SA.map, i);
1337 gcc_assert (is_gimple_reg (var));
1338 if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1339 expand_one_var (var, true, true);
1340 else
1342 /* This is a PARM_DECL or RESULT_DECL. For those partitions that
1343 contain the default def (representing the parm or result itself)
1344 we don't do anything here. But those which don't contain the
1345 default def (representing a temporary based on the parm/result)
1346 we need to allocate space just like for normal VAR_DECLs. */
1347 if (!bitmap_bit_p (SA.partition_has_default_def, i))
1349 expand_one_var (var, true, true);
1350 gcc_assert (SA.partition_to_pseudo[i]);
1355 /* At this point all variables on the local_decls with TREE_USED
1356 set are not associated with any block scope. Lay them out. */
1358 len = VEC_length (tree, cfun->local_decls);
1359 FOR_EACH_LOCAL_DECL (cfun, i, var)
1361 bool expand_now = false;
1363 /* Expanded above already. */
1364 if (is_gimple_reg (var))
1366 TREE_USED (var) = 0;
1367 goto next;
1369 /* We didn't set a block for static or extern because it's hard
1370 to tell the difference between a global variable (re)declared
1371 in a local scope, and one that's really declared there to
1372 begin with. And it doesn't really matter much, since we're
1373 not giving them stack space. Expand them now. */
1374 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1375 expand_now = true;
1377 /* If the variable is not associated with any block, then it
1378 was created by the optimizers, and could be live anywhere
1379 in the function. */
1380 else if (TREE_USED (var))
1381 expand_now = true;
1383 /* Finally, mark all variables on the list as used. We'll use
1384 this in a moment when we expand those associated with scopes. */
1385 TREE_USED (var) = 1;
1387 if (expand_now)
1388 expand_one_var (var, true, true);
1390 next:
1391 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1393 rtx rtl = DECL_RTL_IF_SET (var);
1395 /* Keep artificial non-ignored vars in cfun->local_decls
1396 chain until instantiate_decls. */
1397 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1398 add_local_decl (cfun, var);
1399 else if (rtl == NULL_RTX)
1400 /* If rtl isn't set yet, which can happen e.g. with
1401 -fstack-protector, retry before returning from this
1402 function. */
1403 VEC_safe_push (tree, heap, maybe_local_decls, var);
1407 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
1409 +-----------------+-----------------+
1410 | ...processed... | ...duplicates...|
1411 +-----------------+-----------------+
1413 +-- LEN points here.
1415 We just want the duplicates, as those are the artificial
1416 non-ignored vars that we want to keep until instantiate_decls.
1417 Move them down and truncate the array. */
1418 if (!VEC_empty (tree, cfun->local_decls))
1419 VEC_block_remove (tree, cfun->local_decls, 0, len);
1421 /* At this point, all variables within the block tree with TREE_USED
1422 set are actually used by the optimized function. Lay them out. */
1423 expand_used_vars_for_block (outer_block, true);
1425 if (stack_vars_num > 0)
1427 /* Due to the way alias sets work, no variables with non-conflicting
1428 alias sets may be assigned the same address. Add conflicts to
1429 reflect this. */
1430 add_alias_set_conflicts ();
1432 /* If stack protection is enabled, we don't share space between
1433 vulnerable data and non-vulnerable data. */
1434 if (flag_stack_protect)
1435 add_stack_protection_conflicts ();
1437 /* Now that we have collected all stack variables, and have computed a
1438 minimal interference graph, attempt to save some stack space. */
1439 partition_stack_vars ();
1440 if (dump_file)
1441 dump_stack_var_partition ();
1444 /* There are several conditions under which we should create a
1445 stack guard: protect-all, alloca used, protected decls present. */
1446 if (flag_stack_protect == 2
1447 || (flag_stack_protect
1448 && (cfun->calls_alloca || has_protected_decls)))
1449 create_stack_guard ();
1451 /* Assign rtl to each variable based on these partitions. */
1452 if (stack_vars_num > 0)
1454 /* Reorder decls to be protected by iterating over the variables
1455 array multiple times, and allocating out of each phase in turn. */
1456 /* ??? We could probably integrate this into the qsort we did
1457 earlier, such that we naturally see these variables first,
1458 and thus naturally allocate things in the right order. */
1459 if (has_protected_decls)
1461 /* Phase 1 contains only character arrays. */
1462 expand_stack_vars (stack_protect_decl_phase_1);
1464 /* Phase 2 contains other kinds of arrays. */
1465 if (flag_stack_protect == 2)
1466 expand_stack_vars (stack_protect_decl_phase_2);
1469 expand_stack_vars (NULL);
1471 fini_vars_expansion ();
1474 /* If there were any artificial non-ignored vars without rtl
1475 found earlier, see if deferred stack allocation hasn't assigned
1476 rtl to them. */
1477 FOR_EACH_VEC_ELT_REVERSE (tree, maybe_local_decls, i, var)
1479 rtx rtl = DECL_RTL_IF_SET (var);
1481 /* Keep artificial non-ignored vars in cfun->local_decls
1482 chain until instantiate_decls. */
1483 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1484 add_local_decl (cfun, var);
1486 VEC_free (tree, heap, maybe_local_decls);
1488 /* If the target requires that FRAME_OFFSET be aligned, do it. */
1489 if (STACK_ALIGNMENT_NEEDED)
1491 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1492 if (!FRAME_GROWS_DOWNWARD)
1493 frame_offset += align - 1;
1494 frame_offset &= -align;
1499 /* If we need to produce a detailed dump, print the tree representation
1500 for STMT to the dump file. SINCE is the last RTX after which the RTL
1501 generated for STMT should have been appended. */
1503 static void
1504 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx since)
1506 if (dump_file && (dump_flags & TDF_DETAILS))
1508 fprintf (dump_file, "\n;; ");
1509 print_gimple_stmt (dump_file, stmt, 0,
1510 TDF_SLIM | (dump_flags & TDF_LINENO));
1511 fprintf (dump_file, "\n");
1513 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1517 /* Maps the blocks that do not contain tree labels to rtx labels. */
1519 static struct pointer_map_t *lab_rtx_for_bb;
1521 /* Returns the label_rtx expression for a label starting basic block BB. */
1523 static rtx
1524 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
1526 gimple_stmt_iterator gsi;
1527 tree lab;
1528 gimple lab_stmt;
1529 void **elt;
1531 if (bb->flags & BB_RTL)
1532 return block_label (bb);
1534 elt = pointer_map_contains (lab_rtx_for_bb, bb);
1535 if (elt)
1536 return (rtx) *elt;
1538 /* Find the tree label if it is present. */
1540 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1542 lab_stmt = gsi_stmt (gsi);
1543 if (gimple_code (lab_stmt) != GIMPLE_LABEL)
1544 break;
1546 lab = gimple_label_label (lab_stmt);
1547 if (DECL_NONLOCAL (lab))
1548 break;
1550 return label_rtx (lab);
1553 elt = pointer_map_insert (lab_rtx_for_bb, bb);
1554 *elt = gen_label_rtx ();
1555 return (rtx) *elt;
1559 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
1560 of a basic block where we just expanded the conditional at the end,
1561 possibly clean up the CFG and instruction sequence. LAST is the
1562 last instruction before the just emitted jump sequence. */
1564 static void
1565 maybe_cleanup_end_of_block (edge e, rtx last)
1567 /* Special case: when jumpif decides that the condition is
1568 trivial it emits an unconditional jump (and the necessary
1569 barrier). But we still have two edges, the fallthru one is
1570 wrong. purge_dead_edges would clean this up later. Unfortunately
1571 we have to insert insns (and split edges) before
1572 find_many_sub_basic_blocks and hence before purge_dead_edges.
1573 But splitting edges might create new blocks which depend on the
1574 fact that if there are two edges there's no barrier. So the
1575 barrier would get lost and verify_flow_info would ICE. Instead
1576 of auditing all edge splitters to care for the barrier (which
1577 normally isn't there in a cleaned CFG), fix it here. */
1578 if (BARRIER_P (get_last_insn ()))
1580 rtx insn;
1581 remove_edge (e);
1582 /* Now, we have a single successor block, if we have insns to
1583 insert on the remaining edge we potentially will insert
1584 it at the end of this block (if the dest block isn't feasible)
1585 in order to avoid splitting the edge. This insertion will take
1586 place in front of the last jump. But we might have emitted
1587 multiple jumps (conditional and one unconditional) to the
1588 same destination. Inserting in front of the last one then
1589 is a problem. See PR 40021. We fix this by deleting all
1590 jumps except the last unconditional one. */
1591 insn = PREV_INSN (get_last_insn ());
1592 /* Make sure we have an unconditional jump. Otherwise we're
1593 confused. */
1594 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
1595 for (insn = PREV_INSN (insn); insn != last;)
1597 insn = PREV_INSN (insn);
1598 if (JUMP_P (NEXT_INSN (insn)))
1599 delete_insn (NEXT_INSN (insn));
1604 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
1605 Returns a new basic block if we've terminated the current basic
1606 block and created a new one. */
1608 static basic_block
1609 expand_gimple_cond (basic_block bb, gimple stmt)
1611 basic_block new_bb, dest;
1612 edge new_edge;
1613 edge true_edge;
1614 edge false_edge;
1615 rtx last2, last;
1616 enum tree_code code;
1617 tree op0, op1;
1619 code = gimple_cond_code (stmt);
1620 op0 = gimple_cond_lhs (stmt);
1621 op1 = gimple_cond_rhs (stmt);
1622 /* We're sometimes presented with such code:
1623 D.123_1 = x < y;
1624 if (D.123_1 != 0)
1626 This would expand to two comparisons which then later might
1627 be cleaned up by combine. But some pattern matchers like if-conversion
1628 work better when there's only one compare, so make up for this
1629 here as special exception if TER would have made the same change. */
1630 if (gimple_cond_single_var_p (stmt)
1631 && SA.values
1632 && TREE_CODE (op0) == SSA_NAME
1633 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
1635 gimple second = SSA_NAME_DEF_STMT (op0);
1636 if (gimple_code (second) == GIMPLE_ASSIGN)
1638 enum tree_code code2 = gimple_assign_rhs_code (second);
1639 if (TREE_CODE_CLASS (code2) == tcc_comparison)
1641 code = code2;
1642 op0 = gimple_assign_rhs1 (second);
1643 op1 = gimple_assign_rhs2 (second);
1645 /* If jumps are cheap turn some more codes into
1646 jumpy sequences. */
1647 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4)
1649 if ((code2 == BIT_AND_EXPR
1650 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
1651 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
1652 || code2 == TRUTH_AND_EXPR)
1654 code = TRUTH_ANDIF_EXPR;
1655 op0 = gimple_assign_rhs1 (second);
1656 op1 = gimple_assign_rhs2 (second);
1658 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
1660 code = TRUTH_ORIF_EXPR;
1661 op0 = gimple_assign_rhs1 (second);
1662 op1 = gimple_assign_rhs2 (second);
1668 last2 = last = get_last_insn ();
1670 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1671 if (gimple_has_location (stmt))
1673 set_curr_insn_source_location (gimple_location (stmt));
1674 set_curr_insn_block (gimple_block (stmt));
1677 /* These flags have no purpose in RTL land. */
1678 true_edge->flags &= ~EDGE_TRUE_VALUE;
1679 false_edge->flags &= ~EDGE_FALSE_VALUE;
1681 /* We can either have a pure conditional jump with one fallthru edge or
1682 two-way jump that needs to be decomposed into two basic blocks. */
1683 if (false_edge->dest == bb->next_bb)
1685 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
1686 true_edge->probability);
1687 maybe_dump_rtl_for_gimple_stmt (stmt, last);
1688 if (true_edge->goto_locus)
1690 set_curr_insn_source_location (true_edge->goto_locus);
1691 set_curr_insn_block (true_edge->goto_block);
1692 true_edge->goto_locus = curr_insn_locator ();
1694 true_edge->goto_block = NULL;
1695 false_edge->flags |= EDGE_FALLTHRU;
1696 maybe_cleanup_end_of_block (false_edge, last);
1697 return NULL;
1699 if (true_edge->dest == bb->next_bb)
1701 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
1702 false_edge->probability);
1703 maybe_dump_rtl_for_gimple_stmt (stmt, last);
1704 if (false_edge->goto_locus)
1706 set_curr_insn_source_location (false_edge->goto_locus);
1707 set_curr_insn_block (false_edge->goto_block);
1708 false_edge->goto_locus = curr_insn_locator ();
1710 false_edge->goto_block = NULL;
1711 true_edge->flags |= EDGE_FALLTHRU;
1712 maybe_cleanup_end_of_block (true_edge, last);
1713 return NULL;
1716 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
1717 true_edge->probability);
1718 last = get_last_insn ();
1719 if (false_edge->goto_locus)
1721 set_curr_insn_source_location (false_edge->goto_locus);
1722 set_curr_insn_block (false_edge->goto_block);
1723 false_edge->goto_locus = curr_insn_locator ();
1725 false_edge->goto_block = NULL;
1726 emit_jump (label_rtx_for_bb (false_edge->dest));
1728 BB_END (bb) = last;
1729 if (BARRIER_P (BB_END (bb)))
1730 BB_END (bb) = PREV_INSN (BB_END (bb));
1731 update_bb_for_insn (bb);
1733 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1734 dest = false_edge->dest;
1735 redirect_edge_succ (false_edge, new_bb);
1736 false_edge->flags |= EDGE_FALLTHRU;
1737 new_bb->count = false_edge->count;
1738 new_bb->frequency = EDGE_FREQUENCY (false_edge);
1739 new_edge = make_edge (new_bb, dest, 0);
1740 new_edge->probability = REG_BR_PROB_BASE;
1741 new_edge->count = new_bb->count;
1742 if (BARRIER_P (BB_END (new_bb)))
1743 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
1744 update_bb_for_insn (new_bb);
1746 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
1748 if (true_edge->goto_locus)
1750 set_curr_insn_source_location (true_edge->goto_locus);
1751 set_curr_insn_block (true_edge->goto_block);
1752 true_edge->goto_locus = curr_insn_locator ();
1754 true_edge->goto_block = NULL;
1756 return new_bb;
1759 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
1760 statement STMT. */
1762 static void
1763 expand_call_stmt (gimple stmt)
1765 tree exp;
1766 tree lhs = gimple_call_lhs (stmt);
1767 size_t i;
1768 bool builtin_p;
1769 tree decl;
1771 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
1773 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
1774 decl = gimple_call_fndecl (stmt);
1775 builtin_p = decl && DECL_BUILT_IN (decl);
1777 TREE_TYPE (exp) = gimple_call_return_type (stmt);
1778 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
1780 for (i = 0; i < gimple_call_num_args (stmt); i++)
1782 tree arg = gimple_call_arg (stmt, i);
1783 gimple def;
1784 /* TER addresses into arguments of builtin functions so we have a
1785 chance to infer more correct alignment information. See PR39954. */
1786 if (builtin_p
1787 && TREE_CODE (arg) == SSA_NAME
1788 && (def = get_gimple_for_ssa_name (arg))
1789 && gimple_assign_rhs_code (def) == ADDR_EXPR)
1790 arg = gimple_assign_rhs1 (def);
1791 CALL_EXPR_ARG (exp, i) = arg;
1794 if (gimple_has_side_effects (stmt))
1795 TREE_SIDE_EFFECTS (exp) = 1;
1797 if (gimple_call_nothrow_p (stmt))
1798 TREE_NOTHROW (exp) = 1;
1800 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
1801 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
1802 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
1803 CALL_CANNOT_INLINE_P (exp) = gimple_call_cannot_inline_p (stmt);
1804 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
1805 SET_EXPR_LOCATION (exp, gimple_location (stmt));
1806 TREE_BLOCK (exp) = gimple_block (stmt);
1808 if (lhs)
1809 expand_assignment (lhs, exp, false);
1810 else
1811 expand_expr_real_1 (exp, const0_rtx, VOIDmode, EXPAND_NORMAL, NULL);
1814 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
1815 STMT that doesn't require special handling for outgoing edges. That
1816 is no tailcalls and no GIMPLE_COND. */
1818 static void
1819 expand_gimple_stmt_1 (gimple stmt)
1821 tree op0;
1822 switch (gimple_code (stmt))
1824 case GIMPLE_GOTO:
1825 op0 = gimple_goto_dest (stmt);
1826 if (TREE_CODE (op0) == LABEL_DECL)
1827 expand_goto (op0);
1828 else
1829 expand_computed_goto (op0);
1830 break;
1831 case GIMPLE_LABEL:
1832 expand_label (gimple_label_label (stmt));
1833 break;
1834 case GIMPLE_NOP:
1835 case GIMPLE_PREDICT:
1836 break;
1837 case GIMPLE_SWITCH:
1838 expand_case (stmt);
1839 break;
1840 case GIMPLE_ASM:
1841 expand_asm_stmt (stmt);
1842 break;
1843 case GIMPLE_CALL:
1844 expand_call_stmt (stmt);
1845 break;
1847 case GIMPLE_RETURN:
1848 op0 = gimple_return_retval (stmt);
1850 if (op0 && op0 != error_mark_node)
1852 tree result = DECL_RESULT (current_function_decl);
1854 /* If we are not returning the current function's RESULT_DECL,
1855 build an assignment to it. */
1856 if (op0 != result)
1858 /* I believe that a function's RESULT_DECL is unique. */
1859 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
1861 /* ??? We'd like to use simply expand_assignment here,
1862 but this fails if the value is of BLKmode but the return
1863 decl is a register. expand_return has special handling
1864 for this combination, which eventually should move
1865 to common code. See comments there. Until then, let's
1866 build a modify expression :-/ */
1867 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
1868 result, op0);
1871 if (!op0)
1872 expand_null_return ();
1873 else
1874 expand_return (op0);
1875 break;
1877 case GIMPLE_ASSIGN:
1879 tree lhs = gimple_assign_lhs (stmt);
1881 /* Tree expand used to fiddle with |= and &= of two bitfield
1882 COMPONENT_REFs here. This can't happen with gimple, the LHS
1883 of binary assigns must be a gimple reg. */
1885 if (TREE_CODE (lhs) != SSA_NAME
1886 || get_gimple_rhs_class (gimple_expr_code (stmt))
1887 == GIMPLE_SINGLE_RHS)
1889 tree rhs = gimple_assign_rhs1 (stmt);
1890 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
1891 == GIMPLE_SINGLE_RHS);
1892 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
1893 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
1894 expand_assignment (lhs, rhs,
1895 gimple_assign_nontemporal_move_p (stmt));
1897 else
1899 rtx target, temp;
1900 bool nontemporal = gimple_assign_nontemporal_move_p (stmt);
1901 struct separate_ops ops;
1902 bool promoted = false;
1904 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
1905 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
1906 promoted = true;
1908 ops.code = gimple_assign_rhs_code (stmt);
1909 ops.type = TREE_TYPE (lhs);
1910 switch (get_gimple_rhs_class (gimple_expr_code (stmt)))
1912 case GIMPLE_TERNARY_RHS:
1913 ops.op2 = gimple_assign_rhs3 (stmt);
1914 /* Fallthru */
1915 case GIMPLE_BINARY_RHS:
1916 ops.op1 = gimple_assign_rhs2 (stmt);
1917 /* Fallthru */
1918 case GIMPLE_UNARY_RHS:
1919 ops.op0 = gimple_assign_rhs1 (stmt);
1920 break;
1921 default:
1922 gcc_unreachable ();
1924 ops.location = gimple_location (stmt);
1926 /* If we want to use a nontemporal store, force the value to
1927 register first. If we store into a promoted register,
1928 don't directly expand to target. */
1929 temp = nontemporal || promoted ? NULL_RTX : target;
1930 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
1931 EXPAND_NORMAL);
1933 if (temp == target)
1935 else if (promoted)
1937 int unsignedp = SUBREG_PROMOTED_UNSIGNED_P (target);
1938 /* If TEMP is a VOIDmode constant, use convert_modes to make
1939 sure that we properly convert it. */
1940 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
1942 temp = convert_modes (GET_MODE (target),
1943 TYPE_MODE (ops.type),
1944 temp, unsignedp);
1945 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
1946 GET_MODE (target), temp, unsignedp);
1949 convert_move (SUBREG_REG (target), temp, unsignedp);
1951 else if (nontemporal && emit_storent_insn (target, temp))
1953 else
1955 temp = force_operand (temp, target);
1956 if (temp != target)
1957 emit_move_insn (target, temp);
1961 break;
1963 default:
1964 gcc_unreachable ();
1968 /* Expand one gimple statement STMT and return the last RTL instruction
1969 before any of the newly generated ones.
1971 In addition to generating the necessary RTL instructions this also
1972 sets REG_EH_REGION notes if necessary and sets the current source
1973 location for diagnostics. */
1975 static rtx
1976 expand_gimple_stmt (gimple stmt)
1978 int lp_nr = 0;
1979 rtx last = NULL;
1980 location_t saved_location = input_location;
1982 last = get_last_insn ();
1984 /* If this is an expression of some kind and it has an associated line
1985 number, then emit the line number before expanding the expression.
1987 We need to save and restore the file and line information so that
1988 errors discovered during expansion are emitted with the right
1989 information. It would be better of the diagnostic routines
1990 used the file/line information embedded in the tree nodes rather
1991 than globals. */
1992 gcc_assert (cfun);
1994 if (gimple_has_location (stmt))
1996 input_location = gimple_location (stmt);
1997 set_curr_insn_source_location (input_location);
1999 /* Record where the insns produced belong. */
2000 set_curr_insn_block (gimple_block (stmt));
2003 expand_gimple_stmt_1 (stmt);
2004 /* Free any temporaries used to evaluate this statement. */
2005 free_temp_slots ();
2007 input_location = saved_location;
2009 /* Mark all insns that may trap. */
2010 lp_nr = lookup_stmt_eh_lp (stmt);
2011 if (lp_nr)
2013 rtx insn;
2014 for (insn = next_real_insn (last); insn;
2015 insn = next_real_insn (insn))
2017 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
2018 /* If we want exceptions for non-call insns, any
2019 may_trap_p instruction may throw. */
2020 && GET_CODE (PATTERN (insn)) != CLOBBER
2021 && GET_CODE (PATTERN (insn)) != USE
2022 && insn_could_throw_p (insn))
2023 make_reg_eh_region_note (insn, 0, lp_nr);
2027 return last;
2030 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
2031 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
2032 generated a tail call (something that might be denied by the ABI
2033 rules governing the call; see calls.c).
2035 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
2036 can still reach the rest of BB. The case here is __builtin_sqrt,
2037 where the NaN result goes through the external function (with a
2038 tailcall) and the normal result happens via a sqrt instruction. */
2040 static basic_block
2041 expand_gimple_tailcall (basic_block bb, gimple stmt, bool *can_fallthru)
2043 rtx last2, last;
2044 edge e;
2045 edge_iterator ei;
2046 int probability;
2047 gcov_type count;
2049 last2 = last = expand_gimple_stmt (stmt);
2051 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
2052 if (CALL_P (last) && SIBLING_CALL_P (last))
2053 goto found;
2055 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2057 *can_fallthru = true;
2058 return NULL;
2060 found:
2061 /* ??? Wouldn't it be better to just reset any pending stack adjust?
2062 Any instructions emitted here are about to be deleted. */
2063 do_pending_stack_adjust ();
2065 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
2066 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
2067 EH or abnormal edges, we shouldn't have created a tail call in
2068 the first place. So it seems to me we should just be removing
2069 all edges here, or redirecting the existing fallthru edge to
2070 the exit block. */
2072 probability = 0;
2073 count = 0;
2075 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2077 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
2079 if (e->dest != EXIT_BLOCK_PTR)
2081 e->dest->count -= e->count;
2082 e->dest->frequency -= EDGE_FREQUENCY (e);
2083 if (e->dest->count < 0)
2084 e->dest->count = 0;
2085 if (e->dest->frequency < 0)
2086 e->dest->frequency = 0;
2088 count += e->count;
2089 probability += e->probability;
2090 remove_edge (e);
2092 else
2093 ei_next (&ei);
2096 /* This is somewhat ugly: the call_expr expander often emits instructions
2097 after the sibcall (to perform the function return). These confuse the
2098 find_many_sub_basic_blocks code, so we need to get rid of these. */
2099 last = NEXT_INSN (last);
2100 gcc_assert (BARRIER_P (last));
2102 *can_fallthru = false;
2103 while (NEXT_INSN (last))
2105 /* For instance an sqrt builtin expander expands if with
2106 sibcall in the then and label for `else`. */
2107 if (LABEL_P (NEXT_INSN (last)))
2109 *can_fallthru = true;
2110 break;
2112 delete_insn (NEXT_INSN (last));
2115 e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
2116 e->probability += probability;
2117 e->count += count;
2118 BB_END (bb) = last;
2119 update_bb_for_insn (bb);
2121 if (NEXT_INSN (last))
2123 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2125 last = BB_END (bb);
2126 if (BARRIER_P (last))
2127 BB_END (bb) = PREV_INSN (last);
2130 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2132 return bb;
2135 /* Return the difference between the floor and the truncated result of
2136 a signed division by OP1 with remainder MOD. */
2137 static rtx
2138 floor_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2140 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
2141 return gen_rtx_IF_THEN_ELSE
2142 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2143 gen_rtx_IF_THEN_ELSE
2144 (mode, gen_rtx_LT (BImode,
2145 gen_rtx_DIV (mode, op1, mod),
2146 const0_rtx),
2147 constm1_rtx, const0_rtx),
2148 const0_rtx);
2151 /* Return the difference between the ceil and the truncated result of
2152 a signed division by OP1 with remainder MOD. */
2153 static rtx
2154 ceil_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2156 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
2157 return gen_rtx_IF_THEN_ELSE
2158 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2159 gen_rtx_IF_THEN_ELSE
2160 (mode, gen_rtx_GT (BImode,
2161 gen_rtx_DIV (mode, op1, mod),
2162 const0_rtx),
2163 const1_rtx, const0_rtx),
2164 const0_rtx);
2167 /* Return the difference between the ceil and the truncated result of
2168 an unsigned division by OP1 with remainder MOD. */
2169 static rtx
2170 ceil_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
2172 /* (mod != 0 ? 1 : 0) */
2173 return gen_rtx_IF_THEN_ELSE
2174 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2175 const1_rtx, const0_rtx);
2178 /* Return the difference between the rounded and the truncated result
2179 of a signed division by OP1 with remainder MOD. Halfway cases are
2180 rounded away from zero, rather than to the nearest even number. */
2181 static rtx
2182 round_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2184 /* (abs (mod) >= abs (op1) - abs (mod)
2185 ? (op1 / mod > 0 ? 1 : -1)
2186 : 0) */
2187 return gen_rtx_IF_THEN_ELSE
2188 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
2189 gen_rtx_MINUS (mode,
2190 gen_rtx_ABS (mode, op1),
2191 gen_rtx_ABS (mode, mod))),
2192 gen_rtx_IF_THEN_ELSE
2193 (mode, gen_rtx_GT (BImode,
2194 gen_rtx_DIV (mode, op1, mod),
2195 const0_rtx),
2196 const1_rtx, constm1_rtx),
2197 const0_rtx);
2200 /* Return the difference between the rounded and the truncated result
2201 of a unsigned division by OP1 with remainder MOD. Halfway cases
2202 are rounded away from zero, rather than to the nearest even
2203 number. */
2204 static rtx
2205 round_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2207 /* (mod >= op1 - mod ? 1 : 0) */
2208 return gen_rtx_IF_THEN_ELSE
2209 (mode, gen_rtx_GE (BImode, mod,
2210 gen_rtx_MINUS (mode, op1, mod)),
2211 const1_rtx, const0_rtx);
2214 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
2215 any rtl. */
2217 static rtx
2218 convert_debug_memory_address (enum machine_mode mode, rtx x)
2220 enum machine_mode xmode = GET_MODE (x);
2222 #ifndef POINTERS_EXTEND_UNSIGNED
2223 gcc_assert (mode == Pmode);
2224 gcc_assert (xmode == mode || xmode == VOIDmode);
2225 #else
2226 gcc_assert (mode == Pmode || mode == ptr_mode);
2228 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
2229 return x;
2231 if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (xmode))
2232 x = simplify_gen_subreg (mode, x, xmode,
2233 subreg_lowpart_offset
2234 (mode, xmode));
2235 else if (POINTERS_EXTEND_UNSIGNED > 0)
2236 x = gen_rtx_ZERO_EXTEND (mode, x);
2237 else if (!POINTERS_EXTEND_UNSIGNED)
2238 x = gen_rtx_SIGN_EXTEND (mode, x);
2239 else
2240 gcc_unreachable ();
2241 #endif /* POINTERS_EXTEND_UNSIGNED */
2243 return x;
2246 /* Return an RTX equivalent to the value of the tree expression
2247 EXP. */
2249 static rtx
2250 expand_debug_expr (tree exp)
2252 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
2253 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2254 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
2255 addr_space_t as;
2257 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
2259 case tcc_expression:
2260 switch (TREE_CODE (exp))
2262 case COND_EXPR:
2263 case DOT_PROD_EXPR:
2264 case WIDEN_MULT_PLUS_EXPR:
2265 case WIDEN_MULT_MINUS_EXPR:
2266 goto ternary;
2268 case TRUTH_ANDIF_EXPR:
2269 case TRUTH_ORIF_EXPR:
2270 case TRUTH_AND_EXPR:
2271 case TRUTH_OR_EXPR:
2272 case TRUTH_XOR_EXPR:
2273 goto binary;
2275 case TRUTH_NOT_EXPR:
2276 goto unary;
2278 default:
2279 break;
2281 break;
2283 ternary:
2284 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
2285 if (!op2)
2286 return NULL_RTX;
2287 /* Fall through. */
2289 binary:
2290 case tcc_binary:
2291 case tcc_comparison:
2292 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
2293 if (!op1)
2294 return NULL_RTX;
2295 /* Fall through. */
2297 unary:
2298 case tcc_unary:
2299 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2300 if (!op0)
2301 return NULL_RTX;
2302 break;
2304 case tcc_type:
2305 case tcc_statement:
2306 gcc_unreachable ();
2308 case tcc_constant:
2309 case tcc_exceptional:
2310 case tcc_declaration:
2311 case tcc_reference:
2312 case tcc_vl_exp:
2313 break;
2316 switch (TREE_CODE (exp))
2318 case STRING_CST:
2319 if (!lookup_constant_def (exp))
2321 if (strlen (TREE_STRING_POINTER (exp)) + 1
2322 != (size_t) TREE_STRING_LENGTH (exp))
2323 return NULL_RTX;
2324 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
2325 op0 = gen_rtx_MEM (BLKmode, op0);
2326 set_mem_attributes (op0, exp, 0);
2327 return op0;
2329 /* Fall through... */
2331 case INTEGER_CST:
2332 case REAL_CST:
2333 case FIXED_CST:
2334 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
2335 return op0;
2337 case COMPLEX_CST:
2338 gcc_assert (COMPLEX_MODE_P (mode));
2339 op0 = expand_debug_expr (TREE_REALPART (exp));
2340 op1 = expand_debug_expr (TREE_IMAGPART (exp));
2341 return gen_rtx_CONCAT (mode, op0, op1);
2343 case DEBUG_EXPR_DECL:
2344 op0 = DECL_RTL_IF_SET (exp);
2346 if (op0)
2347 return op0;
2349 op0 = gen_rtx_DEBUG_EXPR (mode);
2350 DEBUG_EXPR_TREE_DECL (op0) = exp;
2351 SET_DECL_RTL (exp, op0);
2353 return op0;
2355 case VAR_DECL:
2356 case PARM_DECL:
2357 case FUNCTION_DECL:
2358 case LABEL_DECL:
2359 case CONST_DECL:
2360 case RESULT_DECL:
2361 op0 = DECL_RTL_IF_SET (exp);
2363 /* This decl was probably optimized away. */
2364 if (!op0)
2366 if (TREE_CODE (exp) != VAR_DECL
2367 || DECL_EXTERNAL (exp)
2368 || !TREE_STATIC (exp)
2369 || !DECL_NAME (exp)
2370 || DECL_HARD_REGISTER (exp)
2371 || mode == VOIDmode)
2372 return NULL;
2374 op0 = make_decl_rtl_for_debug (exp);
2375 if (!MEM_P (op0)
2376 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
2377 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
2378 return NULL;
2380 else
2381 op0 = copy_rtx (op0);
2383 if (GET_MODE (op0) == BLKmode
2384 /* If op0 is not BLKmode, but BLKmode is, adjust_mode
2385 below would ICE. While it is likely a FE bug,
2386 try to be robust here. See PR43166. */
2387 || mode == BLKmode
2388 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
2390 gcc_assert (MEM_P (op0));
2391 op0 = adjust_address_nv (op0, mode, 0);
2392 return op0;
2395 /* Fall through. */
2397 adjust_mode:
2398 case PAREN_EXPR:
2399 case NOP_EXPR:
2400 case CONVERT_EXPR:
2402 enum machine_mode inner_mode = GET_MODE (op0);
2404 if (mode == inner_mode)
2405 return op0;
2407 if (inner_mode == VOIDmode)
2409 if (TREE_CODE (exp) == SSA_NAME)
2410 inner_mode = TYPE_MODE (TREE_TYPE (exp));
2411 else
2412 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
2413 if (mode == inner_mode)
2414 return op0;
2417 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
2419 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
2420 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
2421 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
2422 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
2423 else
2424 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
2426 else if (FLOAT_MODE_P (mode))
2428 gcc_assert (TREE_CODE (exp) != SSA_NAME);
2429 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
2430 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
2431 else
2432 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
2434 else if (FLOAT_MODE_P (inner_mode))
2436 if (unsignedp)
2437 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
2438 else
2439 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
2441 else if (CONSTANT_P (op0)
2442 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
2443 op0 = simplify_gen_subreg (mode, op0, inner_mode,
2444 subreg_lowpart_offset (mode,
2445 inner_mode));
2446 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == tcc_unary
2447 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
2448 : unsignedp)
2449 op0 = gen_rtx_ZERO_EXTEND (mode, op0);
2450 else
2451 op0 = gen_rtx_SIGN_EXTEND (mode, op0);
2453 return op0;
2456 case MEM_REF:
2457 /* ??? FIXME. */
2458 if (!integer_zerop (TREE_OPERAND (exp, 1)))
2459 return NULL;
2460 /* Fallthru. */
2461 case INDIRECT_REF:
2462 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2463 if (!op0)
2464 return NULL;
2466 if (POINTER_TYPE_P (TREE_TYPE (exp)))
2467 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
2468 else
2469 as = ADDR_SPACE_GENERIC;
2471 op0 = gen_rtx_MEM (mode, op0);
2473 set_mem_attributes (op0, exp, 0);
2474 set_mem_addr_space (op0, as);
2476 return op0;
2478 case TARGET_MEM_REF:
2479 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
2480 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
2481 return NULL;
2483 op0 = expand_debug_expr
2484 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
2485 if (!op0)
2486 return NULL;
2488 as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
2490 op0 = gen_rtx_MEM (mode, op0);
2492 set_mem_attributes (op0, exp, 0);
2493 set_mem_addr_space (op0, as);
2495 return op0;
2497 case ARRAY_REF:
2498 case ARRAY_RANGE_REF:
2499 case COMPONENT_REF:
2500 case BIT_FIELD_REF:
2501 case REALPART_EXPR:
2502 case IMAGPART_EXPR:
2503 case VIEW_CONVERT_EXPR:
2505 enum machine_mode mode1;
2506 HOST_WIDE_INT bitsize, bitpos;
2507 tree offset;
2508 int volatilep = 0;
2509 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
2510 &mode1, &unsignedp, &volatilep, false);
2511 rtx orig_op0;
2513 if (bitsize == 0)
2514 return NULL;
2516 orig_op0 = op0 = expand_debug_expr (tem);
2518 if (!op0)
2519 return NULL;
2521 if (offset)
2523 enum machine_mode addrmode, offmode;
2525 if (!MEM_P (op0))
2526 return NULL;
2528 op0 = XEXP (op0, 0);
2529 addrmode = GET_MODE (op0);
2530 if (addrmode == VOIDmode)
2531 addrmode = Pmode;
2533 op1 = expand_debug_expr (offset);
2534 if (!op1)
2535 return NULL;
2537 offmode = GET_MODE (op1);
2538 if (offmode == VOIDmode)
2539 offmode = TYPE_MODE (TREE_TYPE (offset));
2541 if (addrmode != offmode)
2542 op1 = simplify_gen_subreg (addrmode, op1, offmode,
2543 subreg_lowpart_offset (addrmode,
2544 offmode));
2546 /* Don't use offset_address here, we don't need a
2547 recognizable address, and we don't want to generate
2548 code. */
2549 op0 = gen_rtx_MEM (mode, gen_rtx_PLUS (addrmode, op0, op1));
2552 if (MEM_P (op0))
2554 if (mode1 == VOIDmode)
2555 /* Bitfield. */
2556 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
2557 if (bitpos >= BITS_PER_UNIT)
2559 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
2560 bitpos %= BITS_PER_UNIT;
2562 else if (bitpos < 0)
2564 HOST_WIDE_INT units
2565 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
2566 op0 = adjust_address_nv (op0, mode1, units);
2567 bitpos += units * BITS_PER_UNIT;
2569 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
2570 op0 = adjust_address_nv (op0, mode, 0);
2571 else if (GET_MODE (op0) != mode1)
2572 op0 = adjust_address_nv (op0, mode1, 0);
2573 else
2574 op0 = copy_rtx (op0);
2575 if (op0 == orig_op0)
2576 op0 = shallow_copy_rtx (op0);
2577 set_mem_attributes (op0, exp, 0);
2580 if (bitpos == 0 && mode == GET_MODE (op0))
2581 return op0;
2583 if (bitpos < 0)
2584 return NULL;
2586 if (GET_MODE (op0) == BLKmode)
2587 return NULL;
2589 if ((bitpos % BITS_PER_UNIT) == 0
2590 && bitsize == GET_MODE_BITSIZE (mode1))
2592 enum machine_mode opmode = GET_MODE (op0);
2594 if (opmode == VOIDmode)
2595 opmode = mode1;
2597 /* This condition may hold if we're expanding the address
2598 right past the end of an array that turned out not to
2599 be addressable (i.e., the address was only computed in
2600 debug stmts). The gen_subreg below would rightfully
2601 crash, and the address doesn't really exist, so just
2602 drop it. */
2603 if (bitpos >= GET_MODE_BITSIZE (opmode))
2604 return NULL;
2606 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
2607 return simplify_gen_subreg (mode, op0, opmode,
2608 bitpos / BITS_PER_UNIT);
2611 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
2612 && TYPE_UNSIGNED (TREE_TYPE (exp))
2613 ? SIGN_EXTRACT
2614 : ZERO_EXTRACT, mode,
2615 GET_MODE (op0) != VOIDmode
2616 ? GET_MODE (op0) : mode1,
2617 op0, GEN_INT (bitsize), GEN_INT (bitpos));
2620 case ABS_EXPR:
2621 return gen_rtx_ABS (mode, op0);
2623 case NEGATE_EXPR:
2624 return gen_rtx_NEG (mode, op0);
2626 case BIT_NOT_EXPR:
2627 return gen_rtx_NOT (mode, op0);
2629 case FLOAT_EXPR:
2630 if (unsignedp)
2631 return gen_rtx_UNSIGNED_FLOAT (mode, op0);
2632 else
2633 return gen_rtx_FLOAT (mode, op0);
2635 case FIX_TRUNC_EXPR:
2636 if (unsignedp)
2637 return gen_rtx_UNSIGNED_FIX (mode, op0);
2638 else
2639 return gen_rtx_FIX (mode, op0);
2641 case POINTER_PLUS_EXPR:
2642 /* For the rare target where pointers are not the same size as
2643 size_t, we need to check for mis-matched modes and correct
2644 the addend. */
2645 if (op0 && op1
2646 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
2647 && GET_MODE (op0) != GET_MODE (op1))
2649 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1)))
2650 op1 = gen_rtx_TRUNCATE (GET_MODE (op0), op1);
2651 else
2652 /* We always sign-extend, regardless of the signedness of
2653 the operand, because the operand is always unsigned
2654 here even if the original C expression is signed. */
2655 op1 = gen_rtx_SIGN_EXTEND (GET_MODE (op0), op1);
2657 /* Fall through. */
2658 case PLUS_EXPR:
2659 return gen_rtx_PLUS (mode, op0, op1);
2661 case MINUS_EXPR:
2662 return gen_rtx_MINUS (mode, op0, op1);
2664 case MULT_EXPR:
2665 return gen_rtx_MULT (mode, op0, op1);
2667 case RDIV_EXPR:
2668 case TRUNC_DIV_EXPR:
2669 case EXACT_DIV_EXPR:
2670 if (unsignedp)
2671 return gen_rtx_UDIV (mode, op0, op1);
2672 else
2673 return gen_rtx_DIV (mode, op0, op1);
2675 case TRUNC_MOD_EXPR:
2676 if (unsignedp)
2677 return gen_rtx_UMOD (mode, op0, op1);
2678 else
2679 return gen_rtx_MOD (mode, op0, op1);
2681 case FLOOR_DIV_EXPR:
2682 if (unsignedp)
2683 return gen_rtx_UDIV (mode, op0, op1);
2684 else
2686 rtx div = gen_rtx_DIV (mode, op0, op1);
2687 rtx mod = gen_rtx_MOD (mode, op0, op1);
2688 rtx adj = floor_sdiv_adjust (mode, mod, op1);
2689 return gen_rtx_PLUS (mode, div, adj);
2692 case FLOOR_MOD_EXPR:
2693 if (unsignedp)
2694 return gen_rtx_UMOD (mode, op0, op1);
2695 else
2697 rtx mod = gen_rtx_MOD (mode, op0, op1);
2698 rtx adj = floor_sdiv_adjust (mode, mod, op1);
2699 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2700 return gen_rtx_PLUS (mode, mod, adj);
2703 case CEIL_DIV_EXPR:
2704 if (unsignedp)
2706 rtx div = gen_rtx_UDIV (mode, op0, op1);
2707 rtx mod = gen_rtx_UMOD (mode, op0, op1);
2708 rtx adj = ceil_udiv_adjust (mode, mod, op1);
2709 return gen_rtx_PLUS (mode, div, adj);
2711 else
2713 rtx div = gen_rtx_DIV (mode, op0, op1);
2714 rtx mod = gen_rtx_MOD (mode, op0, op1);
2715 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
2716 return gen_rtx_PLUS (mode, div, adj);
2719 case CEIL_MOD_EXPR:
2720 if (unsignedp)
2722 rtx mod = gen_rtx_UMOD (mode, op0, op1);
2723 rtx adj = ceil_udiv_adjust (mode, mod, op1);
2724 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2725 return gen_rtx_PLUS (mode, mod, adj);
2727 else
2729 rtx mod = gen_rtx_MOD (mode, op0, op1);
2730 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
2731 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2732 return gen_rtx_PLUS (mode, mod, adj);
2735 case ROUND_DIV_EXPR:
2736 if (unsignedp)
2738 rtx div = gen_rtx_UDIV (mode, op0, op1);
2739 rtx mod = gen_rtx_UMOD (mode, op0, op1);
2740 rtx adj = round_udiv_adjust (mode, mod, op1);
2741 return gen_rtx_PLUS (mode, div, adj);
2743 else
2745 rtx div = gen_rtx_DIV (mode, op0, op1);
2746 rtx mod = gen_rtx_MOD (mode, op0, op1);
2747 rtx adj = round_sdiv_adjust (mode, mod, op1);
2748 return gen_rtx_PLUS (mode, div, adj);
2751 case ROUND_MOD_EXPR:
2752 if (unsignedp)
2754 rtx mod = gen_rtx_UMOD (mode, op0, op1);
2755 rtx adj = round_udiv_adjust (mode, mod, op1);
2756 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2757 return gen_rtx_PLUS (mode, mod, adj);
2759 else
2761 rtx mod = gen_rtx_MOD (mode, op0, op1);
2762 rtx adj = round_sdiv_adjust (mode, mod, op1);
2763 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2764 return gen_rtx_PLUS (mode, mod, adj);
2767 case LSHIFT_EXPR:
2768 return gen_rtx_ASHIFT (mode, op0, op1);
2770 case RSHIFT_EXPR:
2771 if (unsignedp)
2772 return gen_rtx_LSHIFTRT (mode, op0, op1);
2773 else
2774 return gen_rtx_ASHIFTRT (mode, op0, op1);
2776 case LROTATE_EXPR:
2777 return gen_rtx_ROTATE (mode, op0, op1);
2779 case RROTATE_EXPR:
2780 return gen_rtx_ROTATERT (mode, op0, op1);
2782 case MIN_EXPR:
2783 if (unsignedp)
2784 return gen_rtx_UMIN (mode, op0, op1);
2785 else
2786 return gen_rtx_SMIN (mode, op0, op1);
2788 case MAX_EXPR:
2789 if (unsignedp)
2790 return gen_rtx_UMAX (mode, op0, op1);
2791 else
2792 return gen_rtx_SMAX (mode, op0, op1);
2794 case BIT_AND_EXPR:
2795 case TRUTH_AND_EXPR:
2796 return gen_rtx_AND (mode, op0, op1);
2798 case BIT_IOR_EXPR:
2799 case TRUTH_OR_EXPR:
2800 return gen_rtx_IOR (mode, op0, op1);
2802 case BIT_XOR_EXPR:
2803 case TRUTH_XOR_EXPR:
2804 return gen_rtx_XOR (mode, op0, op1);
2806 case TRUTH_ANDIF_EXPR:
2807 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
2809 case TRUTH_ORIF_EXPR:
2810 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
2812 case TRUTH_NOT_EXPR:
2813 return gen_rtx_EQ (mode, op0, const0_rtx);
2815 case LT_EXPR:
2816 if (unsignedp)
2817 return gen_rtx_LTU (mode, op0, op1);
2818 else
2819 return gen_rtx_LT (mode, op0, op1);
2821 case LE_EXPR:
2822 if (unsignedp)
2823 return gen_rtx_LEU (mode, op0, op1);
2824 else
2825 return gen_rtx_LE (mode, op0, op1);
2827 case GT_EXPR:
2828 if (unsignedp)
2829 return gen_rtx_GTU (mode, op0, op1);
2830 else
2831 return gen_rtx_GT (mode, op0, op1);
2833 case GE_EXPR:
2834 if (unsignedp)
2835 return gen_rtx_GEU (mode, op0, op1);
2836 else
2837 return gen_rtx_GE (mode, op0, op1);
2839 case EQ_EXPR:
2840 return gen_rtx_EQ (mode, op0, op1);
2842 case NE_EXPR:
2843 return gen_rtx_NE (mode, op0, op1);
2845 case UNORDERED_EXPR:
2846 return gen_rtx_UNORDERED (mode, op0, op1);
2848 case ORDERED_EXPR:
2849 return gen_rtx_ORDERED (mode, op0, op1);
2851 case UNLT_EXPR:
2852 return gen_rtx_UNLT (mode, op0, op1);
2854 case UNLE_EXPR:
2855 return gen_rtx_UNLE (mode, op0, op1);
2857 case UNGT_EXPR:
2858 return gen_rtx_UNGT (mode, op0, op1);
2860 case UNGE_EXPR:
2861 return gen_rtx_UNGE (mode, op0, op1);
2863 case UNEQ_EXPR:
2864 return gen_rtx_UNEQ (mode, op0, op1);
2866 case LTGT_EXPR:
2867 return gen_rtx_LTGT (mode, op0, op1);
2869 case COND_EXPR:
2870 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
2872 case COMPLEX_EXPR:
2873 gcc_assert (COMPLEX_MODE_P (mode));
2874 if (GET_MODE (op0) == VOIDmode)
2875 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
2876 if (GET_MODE (op1) == VOIDmode)
2877 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
2878 return gen_rtx_CONCAT (mode, op0, op1);
2880 case CONJ_EXPR:
2881 if (GET_CODE (op0) == CONCAT)
2882 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
2883 gen_rtx_NEG (GET_MODE_INNER (mode),
2884 XEXP (op0, 1)));
2885 else
2887 enum machine_mode imode = GET_MODE_INNER (mode);
2888 rtx re, im;
2890 if (MEM_P (op0))
2892 re = adjust_address_nv (op0, imode, 0);
2893 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
2895 else
2897 enum machine_mode ifmode = int_mode_for_mode (mode);
2898 enum machine_mode ihmode = int_mode_for_mode (imode);
2899 rtx halfsize;
2900 if (ifmode == BLKmode || ihmode == BLKmode)
2901 return NULL;
2902 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
2903 re = op0;
2904 if (mode != ifmode)
2905 re = gen_rtx_SUBREG (ifmode, re, 0);
2906 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
2907 if (imode != ihmode)
2908 re = gen_rtx_SUBREG (imode, re, 0);
2909 im = copy_rtx (op0);
2910 if (mode != ifmode)
2911 im = gen_rtx_SUBREG (ifmode, im, 0);
2912 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
2913 if (imode != ihmode)
2914 im = gen_rtx_SUBREG (imode, im, 0);
2916 im = gen_rtx_NEG (imode, im);
2917 return gen_rtx_CONCAT (mode, re, im);
2920 case ADDR_EXPR:
2921 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2922 if (!op0 || !MEM_P (op0))
2924 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
2925 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
2926 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
2927 && !TREE_ADDRESSABLE (TREE_OPERAND (exp, 0)))
2928 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
2930 if (handled_component_p (TREE_OPERAND (exp, 0)))
2932 HOST_WIDE_INT bitoffset, bitsize, maxsize;
2933 tree decl
2934 = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
2935 &bitoffset, &bitsize, &maxsize);
2936 if ((TREE_CODE (decl) == VAR_DECL
2937 || TREE_CODE (decl) == PARM_DECL
2938 || TREE_CODE (decl) == RESULT_DECL)
2939 && !TREE_ADDRESSABLE (decl)
2940 && (bitoffset % BITS_PER_UNIT) == 0
2941 && bitsize > 0
2942 && bitsize == maxsize)
2943 return plus_constant (gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl),
2944 bitoffset / BITS_PER_UNIT);
2947 return NULL;
2950 op0 = convert_debug_memory_address (mode, XEXP (op0, 0));
2952 return op0;
2954 case VECTOR_CST:
2955 exp = build_constructor_from_list (TREE_TYPE (exp),
2956 TREE_VECTOR_CST_ELTS (exp));
2957 /* Fall through. */
2959 case CONSTRUCTOR:
2960 if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
2962 unsigned i;
2963 tree val;
2965 op0 = gen_rtx_CONCATN
2966 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
2968 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
2970 op1 = expand_debug_expr (val);
2971 if (!op1)
2972 return NULL;
2973 XVECEXP (op0, 0, i) = op1;
2976 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
2978 op1 = expand_debug_expr
2979 (fold_convert (TREE_TYPE (TREE_TYPE (exp)), integer_zero_node));
2981 if (!op1)
2982 return NULL;
2984 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
2985 XVECEXP (op0, 0, i) = op1;
2988 return op0;
2990 else
2991 goto flag_unsupported;
2993 case CALL_EXPR:
2994 /* ??? Maybe handle some builtins? */
2995 return NULL;
2997 case SSA_NAME:
2999 gimple g = get_gimple_for_ssa_name (exp);
3000 if (g)
3002 op0 = expand_debug_expr (gimple_assign_rhs_to_tree (g));
3003 if (!op0)
3004 return NULL;
3006 else
3008 int part = var_to_partition (SA.map, exp);
3010 if (part == NO_PARTITION)
3011 return NULL;
3013 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
3015 op0 = SA.partition_to_pseudo[part];
3017 goto adjust_mode;
3020 case ERROR_MARK:
3021 return NULL;
3023 /* Vector stuff. For most of the codes we don't have rtl codes. */
3024 case REALIGN_LOAD_EXPR:
3025 case REDUC_MAX_EXPR:
3026 case REDUC_MIN_EXPR:
3027 case REDUC_PLUS_EXPR:
3028 case VEC_COND_EXPR:
3029 case VEC_EXTRACT_EVEN_EXPR:
3030 case VEC_EXTRACT_ODD_EXPR:
3031 case VEC_INTERLEAVE_HIGH_EXPR:
3032 case VEC_INTERLEAVE_LOW_EXPR:
3033 case VEC_LSHIFT_EXPR:
3034 case VEC_PACK_FIX_TRUNC_EXPR:
3035 case VEC_PACK_SAT_EXPR:
3036 case VEC_PACK_TRUNC_EXPR:
3037 case VEC_RSHIFT_EXPR:
3038 case VEC_UNPACK_FLOAT_HI_EXPR:
3039 case VEC_UNPACK_FLOAT_LO_EXPR:
3040 case VEC_UNPACK_HI_EXPR:
3041 case VEC_UNPACK_LO_EXPR:
3042 case VEC_WIDEN_MULT_HI_EXPR:
3043 case VEC_WIDEN_MULT_LO_EXPR:
3044 return NULL;
3046 /* Misc codes. */
3047 case ADDR_SPACE_CONVERT_EXPR:
3048 case FIXED_CONVERT_EXPR:
3049 case OBJ_TYPE_REF:
3050 case WITH_SIZE_EXPR:
3051 return NULL;
3053 case DOT_PROD_EXPR:
3054 if (SCALAR_INT_MODE_P (GET_MODE (op0))
3055 && SCALAR_INT_MODE_P (mode))
3057 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3058 op0 = gen_rtx_ZERO_EXTEND (mode, op0);
3059 else
3060 op0 = gen_rtx_SIGN_EXTEND (mode, op0);
3061 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
3062 op1 = gen_rtx_ZERO_EXTEND (mode, op1);
3063 else
3064 op1 = gen_rtx_SIGN_EXTEND (mode, op1);
3065 op0 = gen_rtx_MULT (mode, op0, op1);
3066 return gen_rtx_PLUS (mode, op0, op2);
3068 return NULL;
3070 case WIDEN_MULT_EXPR:
3071 case WIDEN_MULT_PLUS_EXPR:
3072 case WIDEN_MULT_MINUS_EXPR:
3073 if (SCALAR_INT_MODE_P (GET_MODE (op0))
3074 && SCALAR_INT_MODE_P (mode))
3076 enum machine_mode inner_mode = GET_MODE (op0);
3077 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3078 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
3079 else
3080 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
3081 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
3082 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
3083 else
3084 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
3085 op0 = gen_rtx_MULT (mode, op0, op1);
3086 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
3087 return op0;
3088 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
3089 return gen_rtx_PLUS (mode, op0, op2);
3090 else
3091 return gen_rtx_MINUS (mode, op2, op0);
3093 return NULL;
3095 case WIDEN_SUM_EXPR:
3096 if (SCALAR_INT_MODE_P (GET_MODE (op0))
3097 && SCALAR_INT_MODE_P (mode))
3099 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3100 op0 = gen_rtx_ZERO_EXTEND (mode, op0);
3101 else
3102 op0 = gen_rtx_SIGN_EXTEND (mode, op0);
3103 return gen_rtx_PLUS (mode, op0, op1);
3105 return NULL;
3107 default:
3108 flag_unsupported:
3109 #ifdef ENABLE_CHECKING
3110 debug_tree (exp);
3111 gcc_unreachable ();
3112 #else
3113 return NULL;
3114 #endif
3118 /* Expand the _LOCs in debug insns. We run this after expanding all
3119 regular insns, so that any variables referenced in the function
3120 will have their DECL_RTLs set. */
3122 static void
3123 expand_debug_locations (void)
3125 rtx insn;
3126 rtx last = get_last_insn ();
3127 int save_strict_alias = flag_strict_aliasing;
3129 /* New alias sets while setting up memory attributes cause
3130 -fcompare-debug failures, even though it doesn't bring about any
3131 codegen changes. */
3132 flag_strict_aliasing = 0;
3134 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3135 if (DEBUG_INSN_P (insn))
3137 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
3138 rtx val;
3139 enum machine_mode mode;
3141 if (value == NULL_TREE)
3142 val = NULL_RTX;
3143 else
3145 val = expand_debug_expr (value);
3146 gcc_assert (last == get_last_insn ());
3149 if (!val)
3150 val = gen_rtx_UNKNOWN_VAR_LOC ();
3151 else
3153 mode = GET_MODE (INSN_VAR_LOCATION (insn));
3155 gcc_assert (mode == GET_MODE (val)
3156 || (GET_MODE (val) == VOIDmode
3157 && (CONST_INT_P (val)
3158 || GET_CODE (val) == CONST_FIXED
3159 || GET_CODE (val) == CONST_DOUBLE
3160 || GET_CODE (val) == LABEL_REF)));
3163 INSN_VAR_LOCATION_LOC (insn) = val;
3166 flag_strict_aliasing = save_strict_alias;
3169 /* Expand basic block BB from GIMPLE trees to RTL. */
3171 static basic_block
3172 expand_gimple_basic_block (basic_block bb)
3174 gimple_stmt_iterator gsi;
3175 gimple_seq stmts;
3176 gimple stmt = NULL;
3177 rtx note, last;
3178 edge e;
3179 edge_iterator ei;
3180 void **elt;
3182 if (dump_file)
3183 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
3184 bb->index);
3186 /* Note that since we are now transitioning from GIMPLE to RTL, we
3187 cannot use the gsi_*_bb() routines because they expect the basic
3188 block to be in GIMPLE, instead of RTL. Therefore, we need to
3189 access the BB sequence directly. */
3190 stmts = bb_seq (bb);
3191 bb->il.gimple = NULL;
3192 rtl_profile_for_bb (bb);
3193 init_rtl_bb_info (bb);
3194 bb->flags |= BB_RTL;
3196 /* Remove the RETURN_EXPR if we may fall though to the exit
3197 instead. */
3198 gsi = gsi_last (stmts);
3199 if (!gsi_end_p (gsi)
3200 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
3202 gimple ret_stmt = gsi_stmt (gsi);
3204 gcc_assert (single_succ_p (bb));
3205 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
3207 if (bb->next_bb == EXIT_BLOCK_PTR
3208 && !gimple_return_retval (ret_stmt))
3210 gsi_remove (&gsi, false);
3211 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
3215 gsi = gsi_start (stmts);
3216 if (!gsi_end_p (gsi))
3218 stmt = gsi_stmt (gsi);
3219 if (gimple_code (stmt) != GIMPLE_LABEL)
3220 stmt = NULL;
3223 elt = pointer_map_contains (lab_rtx_for_bb, bb);
3225 if (stmt || elt)
3227 last = get_last_insn ();
3229 if (stmt)
3231 expand_gimple_stmt (stmt);
3232 gsi_next (&gsi);
3235 if (elt)
3236 emit_label ((rtx) *elt);
3238 /* Java emits line number notes in the top of labels.
3239 ??? Make this go away once line number notes are obsoleted. */
3240 BB_HEAD (bb) = NEXT_INSN (last);
3241 if (NOTE_P (BB_HEAD (bb)))
3242 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
3243 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
3245 maybe_dump_rtl_for_gimple_stmt (stmt, last);
3247 else
3248 note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
3250 NOTE_BASIC_BLOCK (note) = bb;
3252 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3254 basic_block new_bb;
3256 stmt = gsi_stmt (gsi);
3258 /* If this statement is a non-debug one, and we generate debug
3259 insns, then this one might be the last real use of a TERed
3260 SSA_NAME, but where there are still some debug uses further
3261 down. Expanding the current SSA name in such further debug
3262 uses by their RHS might lead to wrong debug info, as coalescing
3263 might make the operands of such RHS be placed into the same
3264 pseudo as something else. Like so:
3265 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
3266 use(a_1);
3267 a_2 = ...
3268 #DEBUG ... => a_1
3269 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
3270 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
3271 the write to a_2 would actually have clobbered the place which
3272 formerly held a_0.
3274 So, instead of that, we recognize the situation, and generate
3275 debug temporaries at the last real use of TERed SSA names:
3276 a_1 = a_0 + 1;
3277 #DEBUG #D1 => a_1
3278 use(a_1);
3279 a_2 = ...
3280 #DEBUG ... => #D1
3282 if (MAY_HAVE_DEBUG_INSNS
3283 && SA.values
3284 && !is_gimple_debug (stmt))
3286 ssa_op_iter iter;
3287 tree op;
3288 gimple def;
3290 location_t sloc = get_curr_insn_source_location ();
3291 tree sblock = get_curr_insn_block ();
3293 /* Look for SSA names that have their last use here (TERed
3294 names always have only one real use). */
3295 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3296 if ((def = get_gimple_for_ssa_name (op)))
3298 imm_use_iterator imm_iter;
3299 use_operand_p use_p;
3300 bool have_debug_uses = false;
3302 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
3304 if (gimple_debug_bind_p (USE_STMT (use_p)))
3306 have_debug_uses = true;
3307 break;
3311 if (have_debug_uses)
3313 /* OP is a TERed SSA name, with DEF it's defining
3314 statement, and where OP is used in further debug
3315 instructions. Generate a debug temporary, and
3316 replace all uses of OP in debug insns with that
3317 temporary. */
3318 gimple debugstmt;
3319 tree value = gimple_assign_rhs_to_tree (def);
3320 tree vexpr = make_node (DEBUG_EXPR_DECL);
3321 rtx val;
3322 enum machine_mode mode;
3324 set_curr_insn_source_location (gimple_location (def));
3325 set_curr_insn_block (gimple_block (def));
3327 DECL_ARTIFICIAL (vexpr) = 1;
3328 TREE_TYPE (vexpr) = TREE_TYPE (value);
3329 if (DECL_P (value))
3330 mode = DECL_MODE (value);
3331 else
3332 mode = TYPE_MODE (TREE_TYPE (value));
3333 DECL_MODE (vexpr) = mode;
3335 val = gen_rtx_VAR_LOCATION
3336 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3338 val = emit_debug_insn (val);
3340 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
3342 if (!gimple_debug_bind_p (debugstmt))
3343 continue;
3345 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
3346 SET_USE (use_p, vexpr);
3348 update_stmt (debugstmt);
3352 set_curr_insn_source_location (sloc);
3353 set_curr_insn_block (sblock);
3356 currently_expanding_gimple_stmt = stmt;
3358 /* Expand this statement, then evaluate the resulting RTL and
3359 fixup the CFG accordingly. */
3360 if (gimple_code (stmt) == GIMPLE_COND)
3362 new_bb = expand_gimple_cond (bb, stmt);
3363 if (new_bb)
3364 return new_bb;
3366 else if (gimple_debug_bind_p (stmt))
3368 location_t sloc = get_curr_insn_source_location ();
3369 tree sblock = get_curr_insn_block ();
3370 gimple_stmt_iterator nsi = gsi;
3372 for (;;)
3374 tree var = gimple_debug_bind_get_var (stmt);
3375 tree value;
3376 rtx val;
3377 enum machine_mode mode;
3379 if (gimple_debug_bind_has_value_p (stmt))
3380 value = gimple_debug_bind_get_value (stmt);
3381 else
3382 value = NULL_TREE;
3384 last = get_last_insn ();
3386 set_curr_insn_source_location (gimple_location (stmt));
3387 set_curr_insn_block (gimple_block (stmt));
3389 if (DECL_P (var))
3390 mode = DECL_MODE (var);
3391 else
3392 mode = TYPE_MODE (TREE_TYPE (var));
3394 val = gen_rtx_VAR_LOCATION
3395 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3397 val = emit_debug_insn (val);
3399 if (dump_file && (dump_flags & TDF_DETAILS))
3401 /* We can't dump the insn with a TREE where an RTX
3402 is expected. */
3403 INSN_VAR_LOCATION_LOC (val) = const0_rtx;
3404 maybe_dump_rtl_for_gimple_stmt (stmt, last);
3405 INSN_VAR_LOCATION_LOC (val) = (rtx)value;
3408 /* In order not to generate too many debug temporaries,
3409 we delink all uses of debug statements we already expanded.
3410 Therefore debug statements between definition and real
3411 use of TERed SSA names will continue to use the SSA name,
3412 and not be replaced with debug temps. */
3413 delink_stmt_imm_use (stmt);
3415 gsi = nsi;
3416 gsi_next (&nsi);
3417 if (gsi_end_p (nsi))
3418 break;
3419 stmt = gsi_stmt (nsi);
3420 if (!gimple_debug_bind_p (stmt))
3421 break;
3424 set_curr_insn_source_location (sloc);
3425 set_curr_insn_block (sblock);
3427 else
3429 if (is_gimple_call (stmt) && gimple_call_tail_p (stmt))
3431 bool can_fallthru;
3432 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
3433 if (new_bb)
3435 if (can_fallthru)
3436 bb = new_bb;
3437 else
3438 return new_bb;
3441 else
3443 def_operand_p def_p;
3444 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
3446 if (def_p != NULL)
3448 /* Ignore this stmt if it is in the list of
3449 replaceable expressions. */
3450 if (SA.values
3451 && bitmap_bit_p (SA.values,
3452 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
3453 continue;
3455 last = expand_gimple_stmt (stmt);
3456 maybe_dump_rtl_for_gimple_stmt (stmt, last);
3461 currently_expanding_gimple_stmt = NULL;
3463 /* Expand implicit goto and convert goto_locus. */
3464 FOR_EACH_EDGE (e, ei, bb->succs)
3466 if (e->goto_locus && e->goto_block)
3468 set_curr_insn_source_location (e->goto_locus);
3469 set_curr_insn_block (e->goto_block);
3470 e->goto_locus = curr_insn_locator ();
3472 e->goto_block = NULL;
3473 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
3475 emit_jump (label_rtx_for_bb (e->dest));
3476 e->flags &= ~EDGE_FALLTHRU;
3480 /* Expanded RTL can create a jump in the last instruction of block.
3481 This later might be assumed to be a jump to successor and break edge insertion.
3482 We need to insert dummy move to prevent this. PR41440. */
3483 if (single_succ_p (bb)
3484 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
3485 && (last = get_last_insn ())
3486 && JUMP_P (last))
3488 rtx dummy = gen_reg_rtx (SImode);
3489 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
3492 do_pending_stack_adjust ();
3494 /* Find the block tail. The last insn in the block is the insn
3495 before a barrier and/or table jump insn. */
3496 last = get_last_insn ();
3497 if (BARRIER_P (last))
3498 last = PREV_INSN (last);
3499 if (JUMP_TABLE_DATA_P (last))
3500 last = PREV_INSN (PREV_INSN (last));
3501 BB_END (bb) = last;
3503 update_bb_for_insn (bb);
3505 return bb;
3509 /* Create a basic block for initialization code. */
3511 static basic_block
3512 construct_init_block (void)
3514 basic_block init_block, first_block;
3515 edge e = NULL;
3516 int flags;
3518 /* Multiple entry points not supported yet. */
3519 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
3520 init_rtl_bb_info (ENTRY_BLOCK_PTR);
3521 init_rtl_bb_info (EXIT_BLOCK_PTR);
3522 ENTRY_BLOCK_PTR->flags |= BB_RTL;
3523 EXIT_BLOCK_PTR->flags |= BB_RTL;
3525 e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
3527 /* When entry edge points to first basic block, we don't need jump,
3528 otherwise we have to jump into proper target. */
3529 if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
3531 tree label = gimple_block_label (e->dest);
3533 emit_jump (label_rtx (label));
3534 flags = 0;
3536 else
3537 flags = EDGE_FALLTHRU;
3539 init_block = create_basic_block (NEXT_INSN (get_insns ()),
3540 get_last_insn (),
3541 ENTRY_BLOCK_PTR);
3542 init_block->frequency = ENTRY_BLOCK_PTR->frequency;
3543 init_block->count = ENTRY_BLOCK_PTR->count;
3544 if (e)
3546 first_block = e->dest;
3547 redirect_edge_succ (e, init_block);
3548 e = make_edge (init_block, first_block, flags);
3550 else
3551 e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
3552 e->probability = REG_BR_PROB_BASE;
3553 e->count = ENTRY_BLOCK_PTR->count;
3555 update_bb_for_insn (init_block);
3556 return init_block;
3559 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
3560 found in the block tree. */
3562 static void
3563 set_block_levels (tree block, int level)
3565 while (block)
3567 BLOCK_NUMBER (block) = level;
3568 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
3569 block = BLOCK_CHAIN (block);
3573 /* Create a block containing landing pads and similar stuff. */
3575 static void
3576 construct_exit_block (void)
3578 rtx head = get_last_insn ();
3579 rtx end;
3580 basic_block exit_block;
3581 edge e, e2;
3582 unsigned ix;
3583 edge_iterator ei;
3584 rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb);
3586 rtl_profile_for_bb (EXIT_BLOCK_PTR);
3588 /* Make sure the locus is set to the end of the function, so that
3589 epilogue line numbers and warnings are set properly. */
3590 if (cfun->function_end_locus != UNKNOWN_LOCATION)
3591 input_location = cfun->function_end_locus;
3593 /* The following insns belong to the top scope. */
3594 set_curr_insn_block (DECL_INITIAL (current_function_decl));
3596 /* Generate rtl for function exit. */
3597 expand_function_end ();
3599 end = get_last_insn ();
3600 if (head == end)
3601 return;
3602 /* While emitting the function end we could move end of the last basic block.
3604 BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end;
3605 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
3606 head = NEXT_INSN (head);
3607 exit_block = create_basic_block (NEXT_INSN (head), end,
3608 EXIT_BLOCK_PTR->prev_bb);
3609 exit_block->frequency = EXIT_BLOCK_PTR->frequency;
3610 exit_block->count = EXIT_BLOCK_PTR->count;
3612 ix = 0;
3613 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
3615 e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
3616 if (!(e->flags & EDGE_ABNORMAL))
3617 redirect_edge_succ (e, exit_block);
3618 else
3619 ix++;
3622 e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
3623 e->probability = REG_BR_PROB_BASE;
3624 e->count = EXIT_BLOCK_PTR->count;
3625 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
3626 if (e2 != e)
3628 e->count -= e2->count;
3629 exit_block->count -= e2->count;
3630 exit_block->frequency -= EDGE_FREQUENCY (e2);
3632 if (e->count < 0)
3633 e->count = 0;
3634 if (exit_block->count < 0)
3635 exit_block->count = 0;
3636 if (exit_block->frequency < 0)
3637 exit_block->frequency = 0;
3638 update_bb_for_insn (exit_block);
3641 /* Helper function for discover_nonconstant_array_refs.
3642 Look for ARRAY_REF nodes with non-constant indexes and mark them
3643 addressable. */
3645 static tree
3646 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
3647 void *data ATTRIBUTE_UNUSED)
3649 tree t = *tp;
3651 if (IS_TYPE_OR_DECL_P (t))
3652 *walk_subtrees = 0;
3653 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3655 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3656 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
3657 && (!TREE_OPERAND (t, 2)
3658 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
3659 || (TREE_CODE (t) == COMPONENT_REF
3660 && (!TREE_OPERAND (t,2)
3661 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
3662 || TREE_CODE (t) == BIT_FIELD_REF
3663 || TREE_CODE (t) == REALPART_EXPR
3664 || TREE_CODE (t) == IMAGPART_EXPR
3665 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3666 || CONVERT_EXPR_P (t))
3667 t = TREE_OPERAND (t, 0);
3669 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3671 t = get_base_address (t);
3672 if (t && DECL_P (t)
3673 && DECL_MODE (t) != BLKmode)
3674 TREE_ADDRESSABLE (t) = 1;
3677 *walk_subtrees = 0;
3680 return NULL_TREE;
3683 /* RTL expansion is not able to compile array references with variable
3684 offsets for arrays stored in single register. Discover such
3685 expressions and mark variables as addressable to avoid this
3686 scenario. */
3688 static void
3689 discover_nonconstant_array_refs (void)
3691 basic_block bb;
3692 gimple_stmt_iterator gsi;
3694 FOR_EACH_BB (bb)
3695 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3697 gimple stmt = gsi_stmt (gsi);
3698 if (!is_gimple_debug (stmt))
3699 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
3703 /* This function sets crtl->args.internal_arg_pointer to a virtual
3704 register if DRAP is needed. Local register allocator will replace
3705 virtual_incoming_args_rtx with the virtual register. */
3707 static void
3708 expand_stack_alignment (void)
3710 rtx drap_rtx;
3711 unsigned int preferred_stack_boundary;
3713 if (! SUPPORTS_STACK_ALIGNMENT)
3714 return;
3716 if (cfun->calls_alloca
3717 || cfun->has_nonlocal_label
3718 || crtl->has_nonlocal_goto)
3719 crtl->need_drap = true;
3721 /* Call update_stack_boundary here again to update incoming stack
3722 boundary. It may set incoming stack alignment to a different
3723 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
3724 use the minimum incoming stack alignment to check if it is OK
3725 to perform sibcall optimization since sibcall optimization will
3726 only align the outgoing stack to incoming stack boundary. */
3727 if (targetm.calls.update_stack_boundary)
3728 targetm.calls.update_stack_boundary ();
3730 /* The incoming stack frame has to be aligned at least at
3731 parm_stack_boundary. */
3732 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
3734 /* Update crtl->stack_alignment_estimated and use it later to align
3735 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
3736 exceptions since callgraph doesn't collect incoming stack alignment
3737 in this case. */
3738 if (cfun->can_throw_non_call_exceptions
3739 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
3740 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3741 else
3742 preferred_stack_boundary = crtl->preferred_stack_boundary;
3743 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
3744 crtl->stack_alignment_estimated = preferred_stack_boundary;
3745 if (preferred_stack_boundary > crtl->stack_alignment_needed)
3746 crtl->stack_alignment_needed = preferred_stack_boundary;
3748 gcc_assert (crtl->stack_alignment_needed
3749 <= crtl->stack_alignment_estimated);
3751 crtl->stack_realign_needed
3752 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
3753 crtl->stack_realign_tried = crtl->stack_realign_needed;
3755 crtl->stack_realign_processed = true;
3757 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
3758 alignment. */
3759 gcc_assert (targetm.calls.get_drap_rtx != NULL);
3760 drap_rtx = targetm.calls.get_drap_rtx ();
3762 /* stack_realign_drap and drap_rtx must match. */
3763 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
3765 /* Do nothing if NULL is returned, which means DRAP is not needed. */
3766 if (NULL != drap_rtx)
3768 crtl->args.internal_arg_pointer = drap_rtx;
3770 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
3771 needed. */
3772 fixup_tail_calls ();
3776 /* Translate the intermediate representation contained in the CFG
3777 from GIMPLE trees to RTL.
3779 We do conversion per basic block and preserve/update the tree CFG.
3780 This implies we have to do some magic as the CFG can simultaneously
3781 consist of basic blocks containing RTL and GIMPLE trees. This can
3782 confuse the CFG hooks, so be careful to not manipulate CFG during
3783 the expansion. */
3785 static unsigned int
3786 gimple_expand_cfg (void)
3788 basic_block bb, init_block;
3789 sbitmap blocks;
3790 edge_iterator ei;
3791 edge e;
3792 unsigned i;
3794 timevar_push (TV_OUT_OF_SSA);
3795 rewrite_out_of_ssa (&SA);
3796 timevar_pop (TV_OUT_OF_SSA);
3797 SA.partition_to_pseudo = (rtx *)xcalloc (SA.map->num_partitions,
3798 sizeof (rtx));
3800 /* Some backends want to know that we are expanding to RTL. */
3801 currently_expanding_to_rtl = 1;
3803 rtl_profile_for_bb (ENTRY_BLOCK_PTR);
3805 insn_locators_alloc ();
3806 if (!DECL_IS_BUILTIN (current_function_decl))
3808 /* Eventually, all FEs should explicitly set function_start_locus. */
3809 if (cfun->function_start_locus == UNKNOWN_LOCATION)
3810 set_curr_insn_source_location
3811 (DECL_SOURCE_LOCATION (current_function_decl));
3812 else
3813 set_curr_insn_source_location (cfun->function_start_locus);
3815 set_curr_insn_block (DECL_INITIAL (current_function_decl));
3816 prologue_locator = curr_insn_locator ();
3818 #ifdef INSN_SCHEDULING
3819 init_sched_attrs ();
3820 #endif
3822 /* Make sure first insn is a note even if we don't want linenums.
3823 This makes sure the first insn will never be deleted.
3824 Also, final expects a note to appear there. */
3825 emit_note (NOTE_INSN_DELETED);
3827 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
3828 discover_nonconstant_array_refs ();
3830 targetm.expand_to_rtl_hook ();
3831 crtl->stack_alignment_needed = STACK_BOUNDARY;
3832 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
3833 crtl->stack_alignment_estimated = 0;
3834 crtl->preferred_stack_boundary = STACK_BOUNDARY;
3835 cfun->cfg->max_jumptable_ents = 0;
3838 /* Expand the variables recorded during gimple lowering. */
3839 timevar_push (TV_VAR_EXPAND);
3840 expand_used_vars ();
3841 timevar_pop (TV_VAR_EXPAND);
3843 /* Honor stack protection warnings. */
3844 if (warn_stack_protect)
3846 if (cfun->calls_alloca)
3847 warning (OPT_Wstack_protector,
3848 "stack protector not protecting local variables: "
3849 "variable length buffer");
3850 if (has_short_buffer && !crtl->stack_protect_guard)
3851 warning (OPT_Wstack_protector,
3852 "stack protector not protecting function: "
3853 "all local arrays are less than %d bytes long",
3854 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
3857 /* Set up parameters and prepare for return, for the function. */
3858 expand_function_start (current_function_decl);
3860 /* Now that we also have the parameter RTXs, copy them over to our
3861 partitions. */
3862 for (i = 0; i < SA.map->num_partitions; i++)
3864 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
3866 if (TREE_CODE (var) != VAR_DECL
3867 && !SA.partition_to_pseudo[i])
3868 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
3869 gcc_assert (SA.partition_to_pseudo[i]);
3871 /* If this decl was marked as living in multiple places, reset
3872 this now to NULL. */
3873 if (DECL_RTL_IF_SET (var) == pc_rtx)
3874 SET_DECL_RTL (var, NULL);
3876 /* Some RTL parts really want to look at DECL_RTL(x) when x
3877 was a decl marked in REG_ATTR or MEM_ATTR. We could use
3878 SET_DECL_RTL here making this available, but that would mean
3879 to select one of the potentially many RTLs for one DECL. Instead
3880 of doing that we simply reset the MEM_EXPR of the RTL in question,
3881 then nobody can get at it and hence nobody can call DECL_RTL on it. */
3882 if (!DECL_RTL_SET_P (var))
3884 if (MEM_P (SA.partition_to_pseudo[i]))
3885 set_mem_expr (SA.partition_to_pseudo[i], NULL);
3889 /* If this function is `main', emit a call to `__main'
3890 to run global initializers, etc. */
3891 if (DECL_NAME (current_function_decl)
3892 && MAIN_NAME_P (DECL_NAME (current_function_decl))
3893 && DECL_FILE_SCOPE_P (current_function_decl))
3894 expand_main_function ();
3896 /* Initialize the stack_protect_guard field. This must happen after the
3897 call to __main (if any) so that the external decl is initialized. */
3898 if (crtl->stack_protect_guard)
3899 stack_protect_prologue ();
3901 expand_phi_nodes (&SA);
3903 /* Register rtl specific functions for cfg. */
3904 rtl_register_cfg_hooks ();
3906 init_block = construct_init_block ();
3908 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
3909 remaining edges later. */
3910 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3911 e->flags &= ~EDGE_EXECUTABLE;
3913 lab_rtx_for_bb = pointer_map_create ();
3914 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
3915 bb = expand_gimple_basic_block (bb);
3917 if (MAY_HAVE_DEBUG_INSNS)
3918 expand_debug_locations ();
3920 execute_free_datastructures ();
3921 timevar_push (TV_OUT_OF_SSA);
3922 finish_out_of_ssa (&SA);
3923 timevar_pop (TV_OUT_OF_SSA);
3925 timevar_push (TV_POST_EXPAND);
3926 /* We are no longer in SSA form. */
3927 cfun->gimple_df->in_ssa_p = false;
3929 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
3930 conservatively to true until they are all profile aware. */
3931 pointer_map_destroy (lab_rtx_for_bb);
3932 free_histograms ();
3934 construct_exit_block ();
3935 set_curr_insn_block (DECL_INITIAL (current_function_decl));
3936 insn_locators_finalize ();
3938 /* Zap the tree EH table. */
3939 set_eh_throw_stmt_table (cfun, NULL);
3941 rebuild_jump_labels (get_insns ());
3943 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
3945 edge e;
3946 edge_iterator ei;
3947 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3949 if (e->insns.r)
3950 commit_one_edge_insertion (e);
3951 else
3952 ei_next (&ei);
3956 /* We're done expanding trees to RTL. */
3957 currently_expanding_to_rtl = 0;
3959 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR, next_bb)
3961 edge e;
3962 edge_iterator ei;
3963 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3965 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
3966 e->flags &= ~EDGE_EXECUTABLE;
3968 /* At the moment not all abnormal edges match the RTL
3969 representation. It is safe to remove them here as
3970 find_many_sub_basic_blocks will rediscover them.
3971 In the future we should get this fixed properly. */
3972 if ((e->flags & EDGE_ABNORMAL)
3973 && !(e->flags & EDGE_SIBCALL))
3974 remove_edge (e);
3975 else
3976 ei_next (&ei);
3980 blocks = sbitmap_alloc (last_basic_block);
3981 sbitmap_ones (blocks);
3982 find_many_sub_basic_blocks (blocks);
3983 sbitmap_free (blocks);
3984 purge_all_dead_edges ();
3986 compact_blocks ();
3988 expand_stack_alignment ();
3990 #ifdef ENABLE_CHECKING
3991 verify_flow_info ();
3992 #endif
3994 /* There's no need to defer outputting this function any more; we
3995 know we want to output it. */
3996 DECL_DEFER_OUTPUT (current_function_decl) = 0;
3998 /* Now that we're done expanding trees to RTL, we shouldn't have any
3999 more CONCATs anywhere. */
4000 generating_concat_p = 0;
4002 if (dump_file)
4004 fprintf (dump_file,
4005 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
4006 /* And the pass manager will dump RTL for us. */
4009 /* If we're emitting a nested function, make sure its parent gets
4010 emitted as well. Doing otherwise confuses debug info. */
4012 tree parent;
4013 for (parent = DECL_CONTEXT (current_function_decl);
4014 parent != NULL_TREE;
4015 parent = get_containing_scope (parent))
4016 if (TREE_CODE (parent) == FUNCTION_DECL)
4017 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
4020 /* We are now committed to emitting code for this function. Do any
4021 preparation, such as emitting abstract debug info for the inline
4022 before it gets mangled by optimization. */
4023 if (cgraph_function_possibly_inlined_p (current_function_decl))
4024 (*debug_hooks->outlining_inline_function) (current_function_decl);
4026 TREE_ASM_WRITTEN (current_function_decl) = 1;
4028 /* After expanding, the return labels are no longer needed. */
4029 return_label = NULL;
4030 naked_return_label = NULL;
4031 /* Tag the blocks with a depth number so that change_scope can find
4032 the common parent easily. */
4033 set_block_levels (DECL_INITIAL (cfun->decl), 0);
4034 default_rtl_profile ();
4035 timevar_pop (TV_POST_EXPAND);
4036 return 0;
4039 struct rtl_opt_pass pass_expand =
4042 RTL_PASS,
4043 "expand", /* name */
4044 NULL, /* gate */
4045 gimple_expand_cfg, /* execute */
4046 NULL, /* sub */
4047 NULL, /* next */
4048 0, /* static_pass_number */
4049 TV_EXPAND, /* tv_id */
4050 PROP_ssa | PROP_gimple_leh | PROP_cfg
4051 | PROP_gimple_lcx, /* properties_required */
4052 PROP_rtl, /* properties_provided */
4053 PROP_ssa | PROP_trees, /* properties_destroyed */
4054 TODO_verify_ssa | TODO_verify_flow
4055 | TODO_verify_stmts, /* todo_flags_start */
4056 TODO_dump_func
4057 | TODO_ggc_collect /* todo_flags_finish */