PR tree-optimization/43833
[official-gcc/alias-decl.git] / gcc / cfgexpand.c
blob319b1832295d6620f28edb716b5ff40e9a56da42
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 "toplev.h"
40 #include "debug.h"
41 #include "params.h"
42 #include "tree-inline.h"
43 #include "value-prof.h"
44 #include "target.h"
45 #include "ssaexpand.h"
48 /* This variable holds information helping the rewriting of SSA trees
49 into RTL. */
50 struct ssaexpand SA;
52 /* This variable holds the currently expanded gimple statement for purposes
53 of comminucating the profile info to the builtin expanders. */
54 gimple currently_expanding_gimple_stmt;
56 /* Return an expression tree corresponding to the RHS of GIMPLE
57 statement STMT. */
59 tree
60 gimple_assign_rhs_to_tree (gimple stmt)
62 tree t;
63 enum gimple_rhs_class grhs_class;
65 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
67 if (grhs_class == GIMPLE_BINARY_RHS)
68 t = build2 (gimple_assign_rhs_code (stmt),
69 TREE_TYPE (gimple_assign_lhs (stmt)),
70 gimple_assign_rhs1 (stmt),
71 gimple_assign_rhs2 (stmt));
72 else if (grhs_class == GIMPLE_UNARY_RHS)
73 t = build1 (gimple_assign_rhs_code (stmt),
74 TREE_TYPE (gimple_assign_lhs (stmt)),
75 gimple_assign_rhs1 (stmt));
76 else if (grhs_class == GIMPLE_SINGLE_RHS)
78 t = gimple_assign_rhs1 (stmt);
79 /* Avoid modifying this tree in place below. */
80 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
81 && gimple_location (stmt) != EXPR_LOCATION (t))
82 || (gimple_block (stmt)
83 && currently_expanding_to_rtl
84 && EXPR_P (t)
85 && gimple_block (stmt) != TREE_BLOCK (t)))
86 t = copy_node (t);
88 else
89 gcc_unreachable ();
91 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
92 SET_EXPR_LOCATION (t, gimple_location (stmt));
93 if (gimple_block (stmt) && currently_expanding_to_rtl && EXPR_P (t))
94 TREE_BLOCK (t) = gimple_block (stmt);
96 return t;
100 #ifndef STACK_ALIGNMENT_NEEDED
101 #define STACK_ALIGNMENT_NEEDED 1
102 #endif
104 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
106 /* Associate declaration T with storage space X. If T is no
107 SSA name this is exactly SET_DECL_RTL, otherwise make the
108 partition of T associated with X. */
109 static inline void
110 set_rtl (tree t, rtx x)
112 if (TREE_CODE (t) == SSA_NAME)
114 SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
115 if (x && !MEM_P (x))
116 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
117 /* For the benefit of debug information at -O0 (where vartracking
118 doesn't run) record the place also in the base DECL if it's
119 a normal variable (not a parameter). */
120 if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
122 tree var = SSA_NAME_VAR (t);
123 /* If we don't yet have something recorded, just record it now. */
124 if (!DECL_RTL_SET_P (var))
125 SET_DECL_RTL (var, x);
126 /* If we have it set alrady to "multiple places" don't
127 change this. */
128 else if (DECL_RTL (var) == pc_rtx)
130 /* If we have something recorded and it's not the same place
131 as we want to record now, we have multiple partitions for the
132 same base variable, with different places. We can't just
133 randomly chose one, hence we have to say that we don't know.
134 This only happens with optimization, and there var-tracking
135 will figure out the right thing. */
136 else if (DECL_RTL (var) != x)
137 SET_DECL_RTL (var, pc_rtx);
140 else
141 SET_DECL_RTL (t, x);
144 /* This structure holds data relevant to one variable that will be
145 placed in a stack slot. */
146 struct stack_var
148 /* The Variable. */
149 tree decl;
151 /* The offset of the variable. During partitioning, this is the
152 offset relative to the partition. After partitioning, this
153 is relative to the stack frame. */
154 HOST_WIDE_INT offset;
156 /* Initially, the size of the variable. Later, the size of the partition,
157 if this variable becomes it's partition's representative. */
158 HOST_WIDE_INT size;
160 /* The *byte* alignment required for this variable. Or as, with the
161 size, the alignment for this partition. */
162 unsigned int alignb;
164 /* The partition representative. */
165 size_t representative;
167 /* The next stack variable in the partition, or EOC. */
168 size_t next;
170 /* The numbers of conflicting stack variables. */
171 bitmap conflicts;
174 #define EOC ((size_t)-1)
176 /* We have an array of such objects while deciding allocation. */
177 static struct stack_var *stack_vars;
178 static size_t stack_vars_alloc;
179 static size_t stack_vars_num;
181 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
182 is non-decreasing. */
183 static size_t *stack_vars_sorted;
185 /* The phase of the stack frame. This is the known misalignment of
186 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
187 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
188 static int frame_phase;
190 /* Used during expand_used_vars to remember if we saw any decls for
191 which we'd like to enable stack smashing protection. */
192 static bool has_protected_decls;
194 /* Used during expand_used_vars. Remember if we say a character buffer
195 smaller than our cutoff threshold. Used for -Wstack-protector. */
196 static bool has_short_buffer;
198 /* Discover the byte alignment to use for DECL. Ignore alignment
199 we can't do with expected alignment of the stack boundary. */
201 static unsigned int
202 get_decl_align_unit (tree decl)
204 unsigned int align;
206 align = LOCAL_DECL_ALIGNMENT (decl);
208 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
209 align = MAX_SUPPORTED_STACK_ALIGNMENT;
211 if (SUPPORTS_STACK_ALIGNMENT)
213 if (crtl->stack_alignment_estimated < align)
215 gcc_assert(!crtl->stack_realign_processed);
216 crtl->stack_alignment_estimated = align;
220 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
221 So here we only make sure stack_alignment_needed >= align. */
222 if (crtl->stack_alignment_needed < align)
223 crtl->stack_alignment_needed = align;
224 if (crtl->max_used_stack_slot_alignment < align)
225 crtl->max_used_stack_slot_alignment = align;
227 return align / BITS_PER_UNIT;
230 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
231 Return the frame offset. */
233 static HOST_WIDE_INT
234 alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align)
236 HOST_WIDE_INT offset, new_frame_offset;
238 new_frame_offset = frame_offset;
239 if (FRAME_GROWS_DOWNWARD)
241 new_frame_offset -= size + frame_phase;
242 new_frame_offset &= -align;
243 new_frame_offset += frame_phase;
244 offset = new_frame_offset;
246 else
248 new_frame_offset -= frame_phase;
249 new_frame_offset += align - 1;
250 new_frame_offset &= -align;
251 new_frame_offset += frame_phase;
252 offset = new_frame_offset;
253 new_frame_offset += size;
255 frame_offset = new_frame_offset;
257 if (frame_offset_overflow (frame_offset, cfun->decl))
258 frame_offset = offset = 0;
260 return offset;
263 /* Accumulate DECL into STACK_VARS. */
265 static void
266 add_stack_var (tree decl)
268 if (stack_vars_num >= stack_vars_alloc)
270 if (stack_vars_alloc)
271 stack_vars_alloc = stack_vars_alloc * 3 / 2;
272 else
273 stack_vars_alloc = 32;
274 stack_vars
275 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
277 stack_vars[stack_vars_num].decl = decl;
278 stack_vars[stack_vars_num].offset = 0;
279 stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (SSAVAR (decl)), 1);
280 stack_vars[stack_vars_num].alignb = get_decl_align_unit (SSAVAR (decl));
282 /* All variables are initially in their own partition. */
283 stack_vars[stack_vars_num].representative = stack_vars_num;
284 stack_vars[stack_vars_num].next = EOC;
286 /* All variables initially conflict with no other. */
287 stack_vars[stack_vars_num].conflicts = NULL;
289 /* Ensure that this decl doesn't get put onto the list twice. */
290 set_rtl (decl, pc_rtx);
292 stack_vars_num++;
295 /* Make the decls associated with luid's X and Y conflict. */
297 static void
298 add_stack_var_conflict (size_t x, size_t y)
300 struct stack_var *a = &stack_vars[x];
301 struct stack_var *b = &stack_vars[y];
302 if (!a->conflicts)
303 a->conflicts = BITMAP_ALLOC (NULL);
304 if (!b->conflicts)
305 b->conflicts = BITMAP_ALLOC (NULL);
306 bitmap_set_bit (a->conflicts, y);
307 bitmap_set_bit (b->conflicts, x);
310 /* Check whether the decls associated with luid's X and Y conflict. */
312 static bool
313 stack_var_conflict_p (size_t x, size_t y)
315 struct stack_var *a = &stack_vars[x];
316 struct stack_var *b = &stack_vars[y];
317 if (!a->conflicts || !b->conflicts)
318 return false;
319 return bitmap_bit_p (a->conflicts, y);
322 /* Returns true if TYPE is or contains a union type. */
324 static bool
325 aggregate_contains_union_type (tree type)
327 tree field;
329 if (TREE_CODE (type) == UNION_TYPE
330 || TREE_CODE (type) == QUAL_UNION_TYPE)
331 return true;
332 if (TREE_CODE (type) == ARRAY_TYPE)
333 return aggregate_contains_union_type (TREE_TYPE (type));
334 if (TREE_CODE (type) != RECORD_TYPE)
335 return false;
337 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
338 if (TREE_CODE (field) == FIELD_DECL)
339 if (aggregate_contains_union_type (TREE_TYPE (field)))
340 return true;
342 return false;
345 /* A subroutine of expand_used_vars. If two variables X and Y have alias
346 sets that do not conflict, then do add a conflict for these variables
347 in the interference graph. We also need to make sure to add conflicts
348 for union containing structures. Else RTL alias analysis comes along
349 and due to type based aliasing rules decides that for two overlapping
350 union temporaries { short s; int i; } accesses to the same mem through
351 different types may not alias and happily reorders stores across
352 life-time boundaries of the temporaries (See PR25654).
353 We also have to mind MEM_IN_STRUCT_P and MEM_SCALAR_P. */
355 static void
356 add_alias_set_conflicts (void)
358 size_t i, j, n = stack_vars_num;
360 for (i = 0; i < n; ++i)
362 tree type_i = TREE_TYPE (stack_vars[i].decl);
363 bool aggr_i = AGGREGATE_TYPE_P (type_i);
364 bool contains_union;
366 contains_union = aggregate_contains_union_type (type_i);
367 for (j = 0; j < i; ++j)
369 tree type_j = TREE_TYPE (stack_vars[j].decl);
370 bool aggr_j = AGGREGATE_TYPE_P (type_j);
371 if (aggr_i != aggr_j
372 /* Either the objects conflict by means of type based
373 aliasing rules, or we need to add a conflict. */
374 || !objects_must_conflict_p (type_i, type_j)
375 /* In case the types do not conflict ensure that access
376 to elements will conflict. In case of unions we have
377 to be careful as type based aliasing rules may say
378 access to the same memory does not conflict. So play
379 safe and add a conflict in this case. */
380 || contains_union)
381 add_stack_var_conflict (i, j);
386 /* A subroutine of partition_stack_vars. A comparison function for qsort,
387 sorting an array of indices by the size and type of the object. */
389 static int
390 stack_var_size_cmp (const void *a, const void *b)
392 HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size;
393 HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size;
394 tree decla, declb;
395 unsigned int uida, uidb;
397 if (sa < sb)
398 return -1;
399 if (sa > sb)
400 return 1;
401 decla = stack_vars[*(const size_t *)a].decl;
402 declb = stack_vars[*(const size_t *)b].decl;
403 /* For stack variables of the same size use and id of the decls
404 to make the sort stable. Two SSA names are compared by their
405 version, SSA names come before non-SSA names, and two normal
406 decls are compared by their DECL_UID. */
407 if (TREE_CODE (decla) == SSA_NAME)
409 if (TREE_CODE (declb) == SSA_NAME)
410 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
411 else
412 return -1;
414 else if (TREE_CODE (declb) == SSA_NAME)
415 return 1;
416 else
417 uida = DECL_UID (decla), uidb = DECL_UID (declb);
418 if (uida < uidb)
419 return -1;
420 if (uida > uidb)
421 return 1;
422 return 0;
426 /* If the points-to solution *PI points to variables that are in a partition
427 together with other variables add all partition members to the pointed-to
428 variables bitmap. */
430 static void
431 add_partitioned_vars_to_ptset (struct pt_solution *pt,
432 struct pointer_map_t *decls_to_partitions,
433 struct pointer_set_t *visited, bitmap temp)
435 bitmap_iterator bi;
436 unsigned i;
437 bitmap *part;
439 if (pt->anything
440 || pt->vars == NULL
441 /* The pointed-to vars bitmap is shared, it is enough to
442 visit it once. */
443 || pointer_set_insert(visited, pt->vars))
444 return;
446 bitmap_clear (temp);
448 /* By using a temporary bitmap to store all members of the partitions
449 we have to add we make sure to visit each of the partitions only
450 once. */
451 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
452 if ((!temp
453 || !bitmap_bit_p (temp, i))
454 && (part = (bitmap *) pointer_map_contains (decls_to_partitions,
455 (void *)(size_t) i)))
456 bitmap_ior_into (temp, *part);
457 if (!bitmap_empty_p (temp))
458 bitmap_ior_into (pt->vars, temp);
461 /* Update points-to sets based on partition info, so we can use them on RTL.
462 The bitmaps representing stack partitions will be saved until expand,
463 where partitioned decls used as bases in memory expressions will be
464 rewritten. */
466 static void
467 update_alias_info_with_stack_vars (void)
469 struct pointer_map_t *decls_to_partitions = NULL;
470 size_t i, j;
471 tree var = NULL_TREE;
473 for (i = 0; i < stack_vars_num; i++)
475 bitmap part = NULL;
476 tree name;
477 struct ptr_info_def *pi;
479 /* Not interested in partitions with single variable. */
480 if (stack_vars[i].representative != i
481 || stack_vars[i].next == EOC)
482 continue;
484 if (!decls_to_partitions)
486 decls_to_partitions = pointer_map_create ();
487 cfun->gimple_df->decls_to_pointers = pointer_map_create ();
490 /* Create an SSA_NAME that points to the partition for use
491 as base during alias-oracle queries on RTL for bases that
492 have been partitioned. */
493 if (var == NULL_TREE)
494 var = create_tmp_var (ptr_type_node, NULL);
495 name = make_ssa_name (var, NULL);
497 /* Create bitmaps representing partitions. They will be used for
498 points-to sets later, so use GGC alloc. */
499 part = BITMAP_GGC_ALLOC ();
500 for (j = i; j != EOC; j = stack_vars[j].next)
502 tree decl = stack_vars[j].decl;
503 unsigned int uid = DECL_PT_UID (decl);
504 /* We should never end up partitioning SSA names (though they
505 may end up on the stack). Neither should we allocate stack
506 space to something that is unused and thus unreferenced. */
507 gcc_assert (DECL_P (decl)
508 && referenced_var_lookup (DECL_UID (decl)));
509 bitmap_set_bit (part, uid);
510 *((bitmap *) pointer_map_insert (decls_to_partitions,
511 (void *)(size_t) uid)) = part;
512 *((tree *) pointer_map_insert (cfun->gimple_df->decls_to_pointers,
513 decl)) = name;
516 /* Make the SSA name point to all partition members. */
517 pi = get_ptr_info (name);
518 pt_solution_set (&pi->pt, part, false, false);
521 /* Make all points-to sets that contain one member of a partition
522 contain all members of the partition. */
523 if (decls_to_partitions)
525 unsigned i;
526 struct pointer_set_t *visited = pointer_set_create ();
527 bitmap temp = BITMAP_ALLOC (NULL);
529 for (i = 1; i < num_ssa_names; i++)
531 tree name = ssa_name (i);
532 struct ptr_info_def *pi;
534 if (name
535 && POINTER_TYPE_P (TREE_TYPE (name))
536 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
537 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
538 visited, temp);
541 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
542 decls_to_partitions, visited, temp);
544 pointer_set_destroy (visited);
545 pointer_map_destroy (decls_to_partitions);
546 BITMAP_FREE (temp);
550 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
551 partitioning algorithm. Partitions A and B are known to be non-conflicting.
552 Merge them into a single partition A.
554 At the same time, add OFFSET to all variables in partition B. At the end
555 of the partitioning process we've have a nice block easy to lay out within
556 the stack frame. */
558 static void
559 union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset)
561 size_t i, last;
562 struct stack_var *vb = &stack_vars[b];
563 bitmap_iterator bi;
564 unsigned u;
566 /* Update each element of partition B with the given offset,
567 and merge them into partition A. */
568 for (last = i = b; i != EOC; last = i, i = stack_vars[i].next)
570 stack_vars[i].offset += offset;
571 stack_vars[i].representative = a;
573 stack_vars[last].next = stack_vars[a].next;
574 stack_vars[a].next = b;
576 /* Update the required alignment of partition A to account for B. */
577 if (stack_vars[a].alignb < stack_vars[b].alignb)
578 stack_vars[a].alignb = stack_vars[b].alignb;
580 /* Update the interference graph and merge the conflicts. */
581 if (vb->conflicts)
583 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
584 add_stack_var_conflict (a, stack_vars[u].representative);
585 BITMAP_FREE (vb->conflicts);
589 /* A subroutine of expand_used_vars. Binpack the variables into
590 partitions constrained by the interference graph. The overall
591 algorithm used is as follows:
593 Sort the objects by size.
594 For each object A {
595 S = size(A)
596 O = 0
597 loop {
598 Look for the largest non-conflicting object B with size <= S.
599 UNION (A, B)
600 offset(B) = O
601 O += size(B)
602 S -= size(B)
607 static void
608 partition_stack_vars (void)
610 size_t si, sj, n = stack_vars_num;
612 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
613 for (si = 0; si < n; ++si)
614 stack_vars_sorted[si] = si;
616 if (n == 1)
617 return;
619 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp);
621 for (si = 0; si < n; ++si)
623 size_t i = stack_vars_sorted[si];
624 HOST_WIDE_INT isize = stack_vars[i].size;
625 HOST_WIDE_INT offset = 0;
627 for (sj = si; sj-- > 0; )
629 size_t j = stack_vars_sorted[sj];
630 HOST_WIDE_INT jsize = stack_vars[j].size;
631 unsigned int jalign = stack_vars[j].alignb;
633 /* Ignore objects that aren't partition representatives. */
634 if (stack_vars[j].representative != j)
635 continue;
637 /* Ignore objects too large for the remaining space. */
638 if (isize < jsize)
639 continue;
641 /* Ignore conflicting objects. */
642 if (stack_var_conflict_p (i, j))
643 continue;
645 /* Refine the remaining space check to include alignment. */
646 if (offset & (jalign - 1))
648 HOST_WIDE_INT toff = offset;
649 toff += jalign - 1;
650 toff &= -(HOST_WIDE_INT)jalign;
651 if (isize - (toff - offset) < jsize)
652 continue;
654 isize -= toff - offset;
655 offset = toff;
658 /* UNION the objects, placing J at OFFSET. */
659 union_stack_vars (i, j, offset);
661 isize -= jsize;
662 if (isize == 0)
663 break;
667 if (optimize)
668 update_alias_info_with_stack_vars ();
671 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
673 static void
674 dump_stack_var_partition (void)
676 size_t si, i, j, n = stack_vars_num;
678 for (si = 0; si < n; ++si)
680 i = stack_vars_sorted[si];
682 /* Skip variables that aren't partition representatives, for now. */
683 if (stack_vars[i].representative != i)
684 continue;
686 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
687 " align %u\n", (unsigned long) i, stack_vars[i].size,
688 stack_vars[i].alignb);
690 for (j = i; j != EOC; j = stack_vars[j].next)
692 fputc ('\t', dump_file);
693 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
694 fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n",
695 stack_vars[j].offset);
700 /* Assign rtl to DECL at frame offset OFFSET. */
702 static void
703 expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset)
705 /* Alignment is unsigned. */
706 unsigned HOST_WIDE_INT align;
707 rtx x;
709 /* If this fails, we've overflowed the stack frame. Error nicely? */
710 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
712 x = plus_constant (virtual_stack_vars_rtx, offset);
713 x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
715 if (TREE_CODE (decl) != SSA_NAME)
717 /* Set alignment we actually gave this decl if it isn't an SSA name.
718 If it is we generate stack slots only accidentally so it isn't as
719 important, we'll simply use the alignment that is already set. */
720 offset -= frame_phase;
721 align = offset & -offset;
722 align *= BITS_PER_UNIT;
723 if (align == 0)
724 align = STACK_BOUNDARY;
725 else if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
726 align = MAX_SUPPORTED_STACK_ALIGNMENT;
728 DECL_ALIGN (decl) = align;
729 DECL_USER_ALIGN (decl) = 0;
732 set_mem_attributes (x, SSAVAR (decl), true);
733 set_rtl (decl, x);
736 /* A subroutine of expand_used_vars. Give each partition representative
737 a unique location within the stack frame. Update each partition member
738 with that location. */
740 static void
741 expand_stack_vars (bool (*pred) (tree))
743 size_t si, i, j, n = stack_vars_num;
745 for (si = 0; si < n; ++si)
747 HOST_WIDE_INT offset;
749 i = stack_vars_sorted[si];
751 /* Skip variables that aren't partition representatives, for now. */
752 if (stack_vars[i].representative != i)
753 continue;
755 /* Skip variables that have already had rtl assigned. See also
756 add_stack_var where we perpetrate this pc_rtx hack. */
757 if ((TREE_CODE (stack_vars[i].decl) == SSA_NAME
758 ? SA.partition_to_pseudo[var_to_partition (SA.map, stack_vars[i].decl)]
759 : DECL_RTL (stack_vars[i].decl)) != pc_rtx)
760 continue;
762 /* Check the predicate to see whether this variable should be
763 allocated in this pass. */
764 if (pred && !pred (stack_vars[i].decl))
765 continue;
767 offset = alloc_stack_frame_space (stack_vars[i].size,
768 stack_vars[i].alignb);
770 /* Create rtl for each variable based on their location within the
771 partition. */
772 for (j = i; j != EOC; j = stack_vars[j].next)
774 gcc_assert (stack_vars[j].offset <= stack_vars[i].size);
775 expand_one_stack_var_at (stack_vars[j].decl,
776 stack_vars[j].offset + offset);
781 /* Take into account all sizes of partitions and reset DECL_RTLs. */
782 static HOST_WIDE_INT
783 account_stack_vars (void)
785 size_t si, j, i, n = stack_vars_num;
786 HOST_WIDE_INT size = 0;
788 for (si = 0; si < n; ++si)
790 i = stack_vars_sorted[si];
792 /* Skip variables that aren't partition representatives, for now. */
793 if (stack_vars[i].representative != i)
794 continue;
796 size += stack_vars[i].size;
797 for (j = i; j != EOC; j = stack_vars[j].next)
798 set_rtl (stack_vars[j].decl, NULL);
800 return size;
803 /* A subroutine of expand_one_var. Called to immediately assign rtl
804 to a variable to be allocated in the stack frame. */
806 static void
807 expand_one_stack_var (tree var)
809 HOST_WIDE_INT size, offset, align;
811 size = tree_low_cst (DECL_SIZE_UNIT (SSAVAR (var)), 1);
812 align = get_decl_align_unit (SSAVAR (var));
813 offset = alloc_stack_frame_space (size, align);
815 expand_one_stack_var_at (var, offset);
818 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
819 that will reside in a hard register. */
821 static void
822 expand_one_hard_reg_var (tree var)
824 rest_of_decl_compilation (var, 0, 0);
827 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
828 that will reside in a pseudo register. */
830 static void
831 expand_one_register_var (tree var)
833 tree decl = SSAVAR (var);
834 tree type = TREE_TYPE (decl);
835 enum machine_mode reg_mode = promote_decl_mode (decl, NULL);
836 rtx x = gen_reg_rtx (reg_mode);
838 set_rtl (var, x);
840 /* Note if the object is a user variable. */
841 if (!DECL_ARTIFICIAL (decl))
842 mark_user_reg (x);
844 if (POINTER_TYPE_P (type))
845 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (type)));
848 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
849 has some associated error, e.g. its type is error-mark. We just need
850 to pick something that won't crash the rest of the compiler. */
852 static void
853 expand_one_error_var (tree var)
855 enum machine_mode mode = DECL_MODE (var);
856 rtx x;
858 if (mode == BLKmode)
859 x = gen_rtx_MEM (BLKmode, const0_rtx);
860 else if (mode == VOIDmode)
861 x = const0_rtx;
862 else
863 x = gen_reg_rtx (mode);
865 SET_DECL_RTL (var, x);
868 /* A subroutine of expand_one_var. VAR is a variable that will be
869 allocated to the local stack frame. Return true if we wish to
870 add VAR to STACK_VARS so that it will be coalesced with other
871 variables. Return false to allocate VAR immediately.
873 This function is used to reduce the number of variables considered
874 for coalescing, which reduces the size of the quadratic problem. */
876 static bool
877 defer_stack_allocation (tree var, bool toplevel)
879 /* If stack protection is enabled, *all* stack variables must be deferred,
880 so that we can re-order the strings to the top of the frame. */
881 if (flag_stack_protect)
882 return true;
884 /* Variables in the outermost scope automatically conflict with
885 every other variable. The only reason to want to defer them
886 at all is that, after sorting, we can more efficiently pack
887 small variables in the stack frame. Continue to defer at -O2. */
888 if (toplevel && optimize < 2)
889 return false;
891 /* Without optimization, *most* variables are allocated from the
892 stack, which makes the quadratic problem large exactly when we
893 want compilation to proceed as quickly as possible. On the
894 other hand, we don't want the function's stack frame size to
895 get completely out of hand. So we avoid adding scalars and
896 "small" aggregates to the list at all. */
897 if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32)
898 return false;
900 return true;
903 /* A subroutine of expand_used_vars. Expand one variable according to
904 its flavor. Variables to be placed on the stack are not actually
905 expanded yet, merely recorded.
906 When REALLY_EXPAND is false, only add stack values to be allocated.
907 Return stack usage this variable is supposed to take.
910 static HOST_WIDE_INT
911 expand_one_var (tree var, bool toplevel, bool really_expand)
913 tree origvar = var;
914 var = SSAVAR (var);
916 if (SUPPORTS_STACK_ALIGNMENT
917 && TREE_TYPE (var) != error_mark_node
918 && TREE_CODE (var) == VAR_DECL)
920 unsigned int align;
922 /* Because we don't know if VAR will be in register or on stack,
923 we conservatively assume it will be on stack even if VAR is
924 eventually put into register after RA pass. For non-automatic
925 variables, which won't be on stack, we collect alignment of
926 type and ignore user specified alignment. */
927 if (TREE_STATIC (var) || DECL_EXTERNAL (var))
928 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
929 TYPE_MODE (TREE_TYPE (var)),
930 TYPE_ALIGN (TREE_TYPE (var)));
931 else
932 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
934 if (crtl->stack_alignment_estimated < align)
936 /* stack_alignment_estimated shouldn't change after stack
937 realign decision made */
938 gcc_assert(!crtl->stack_realign_processed);
939 crtl->stack_alignment_estimated = align;
943 if (TREE_CODE (origvar) == SSA_NAME)
945 gcc_assert (TREE_CODE (var) != VAR_DECL
946 || (!DECL_EXTERNAL (var)
947 && !DECL_HAS_VALUE_EXPR_P (var)
948 && !TREE_STATIC (var)
949 && TREE_TYPE (var) != error_mark_node
950 && !DECL_HARD_REGISTER (var)
951 && really_expand));
953 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
955 else if (DECL_EXTERNAL (var))
957 else if (DECL_HAS_VALUE_EXPR_P (var))
959 else if (TREE_STATIC (var))
961 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
963 else if (TREE_TYPE (var) == error_mark_node)
965 if (really_expand)
966 expand_one_error_var (var);
968 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
970 if (really_expand)
971 expand_one_hard_reg_var (var);
973 else if (use_register_for_decl (var))
975 if (really_expand)
976 expand_one_register_var (origvar);
978 else if (!host_integerp (DECL_SIZE_UNIT (var), 1))
980 if (really_expand)
982 error ("size of variable %q+D is too large", var);
983 expand_one_error_var (var);
986 else if (defer_stack_allocation (var, toplevel))
987 add_stack_var (origvar);
988 else
990 if (really_expand)
991 expand_one_stack_var (origvar);
992 return tree_low_cst (DECL_SIZE_UNIT (var), 1);
994 return 0;
997 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
998 expanding variables. Those variables that can be put into registers
999 are allocated pseudos; those that can't are put on the stack.
1001 TOPLEVEL is true if this is the outermost BLOCK. */
1003 static void
1004 expand_used_vars_for_block (tree block, bool toplevel)
1006 size_t i, j, old_sv_num, this_sv_num, new_sv_num;
1007 tree t;
1009 old_sv_num = toplevel ? 0 : stack_vars_num;
1011 /* Expand all variables at this level. */
1012 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1013 if (TREE_USED (t))
1014 expand_one_var (t, toplevel, true);
1016 this_sv_num = stack_vars_num;
1018 /* Expand all variables at containing levels. */
1019 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1020 expand_used_vars_for_block (t, false);
1022 /* Since we do not track exact variable lifetimes (which is not even
1023 possible for variables whose address escapes), we mirror the block
1024 tree in the interference graph. Here we cause all variables at this
1025 level, and all sublevels, to conflict. */
1026 if (old_sv_num < this_sv_num)
1028 new_sv_num = stack_vars_num;
1030 for (i = old_sv_num; i < new_sv_num; ++i)
1031 for (j = i < this_sv_num ? i : this_sv_num; j-- > old_sv_num ;)
1032 add_stack_var_conflict (i, j);
1036 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1037 and clear TREE_USED on all local variables. */
1039 static void
1040 clear_tree_used (tree block)
1042 tree t;
1044 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1045 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1046 TREE_USED (t) = 0;
1048 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1049 clear_tree_used (t);
1052 /* Examine TYPE and determine a bit mask of the following features. */
1054 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1055 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1056 #define SPCT_HAS_ARRAY 4
1057 #define SPCT_HAS_AGGREGATE 8
1059 static unsigned int
1060 stack_protect_classify_type (tree type)
1062 unsigned int ret = 0;
1063 tree t;
1065 switch (TREE_CODE (type))
1067 case ARRAY_TYPE:
1068 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1069 if (t == char_type_node
1070 || t == signed_char_type_node
1071 || t == unsigned_char_type_node)
1073 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1074 unsigned HOST_WIDE_INT len;
1076 if (!TYPE_SIZE_UNIT (type)
1077 || !host_integerp (TYPE_SIZE_UNIT (type), 1))
1078 len = max;
1079 else
1080 len = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
1082 if (len < max)
1083 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1084 else
1085 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1087 else
1088 ret = SPCT_HAS_ARRAY;
1089 break;
1091 case UNION_TYPE:
1092 case QUAL_UNION_TYPE:
1093 case RECORD_TYPE:
1094 ret = SPCT_HAS_AGGREGATE;
1095 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1096 if (TREE_CODE (t) == FIELD_DECL)
1097 ret |= stack_protect_classify_type (TREE_TYPE (t));
1098 break;
1100 default:
1101 break;
1104 return ret;
1107 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1108 part of the local stack frame. Remember if we ever return nonzero for
1109 any variable in this function. The return value is the phase number in
1110 which the variable should be allocated. */
1112 static int
1113 stack_protect_decl_phase (tree decl)
1115 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1116 int ret = 0;
1118 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1119 has_short_buffer = true;
1121 if (flag_stack_protect == 2)
1123 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1124 && !(bits & SPCT_HAS_AGGREGATE))
1125 ret = 1;
1126 else if (bits & SPCT_HAS_ARRAY)
1127 ret = 2;
1129 else
1130 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1132 if (ret)
1133 has_protected_decls = true;
1135 return ret;
1138 /* Two helper routines that check for phase 1 and phase 2. These are used
1139 as callbacks for expand_stack_vars. */
1141 static bool
1142 stack_protect_decl_phase_1 (tree decl)
1144 return stack_protect_decl_phase (decl) == 1;
1147 static bool
1148 stack_protect_decl_phase_2 (tree decl)
1150 return stack_protect_decl_phase (decl) == 2;
1153 /* Ensure that variables in different stack protection phases conflict
1154 so that they are not merged and share the same stack slot. */
1156 static void
1157 add_stack_protection_conflicts (void)
1159 size_t i, j, n = stack_vars_num;
1160 unsigned char *phase;
1162 phase = XNEWVEC (unsigned char, n);
1163 for (i = 0; i < n; ++i)
1164 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1166 for (i = 0; i < n; ++i)
1168 unsigned char ph_i = phase[i];
1169 for (j = 0; j < i; ++j)
1170 if (ph_i != phase[j])
1171 add_stack_var_conflict (i, j);
1174 XDELETEVEC (phase);
1177 /* Create a decl for the guard at the top of the stack frame. */
1179 static void
1180 create_stack_guard (void)
1182 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1183 VAR_DECL, NULL, ptr_type_node);
1184 TREE_THIS_VOLATILE (guard) = 1;
1185 TREE_USED (guard) = 1;
1186 expand_one_stack_var (guard);
1187 crtl->stack_protect_guard = guard;
1190 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1191 expanding variables. Those variables that can be put into registers
1192 are allocated pseudos; those that can't are put on the stack.
1194 TOPLEVEL is true if this is the outermost BLOCK. */
1196 static HOST_WIDE_INT
1197 account_used_vars_for_block (tree block, bool toplevel)
1199 tree t;
1200 HOST_WIDE_INT size = 0;
1202 /* Expand all variables at this level. */
1203 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1204 if (TREE_USED (t))
1205 size += expand_one_var (t, toplevel, false);
1207 /* Expand all variables at containing levels. */
1208 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1209 size += account_used_vars_for_block (t, false);
1211 return size;
1214 /* Prepare for expanding variables. */
1215 static void
1216 init_vars_expansion (void)
1218 tree t;
1219 /* Set TREE_USED on all variables in the local_decls. */
1220 for (t = cfun->local_decls; t; t = TREE_CHAIN (t))
1221 TREE_USED (TREE_VALUE (t)) = 1;
1223 /* Clear TREE_USED on all variables associated with a block scope. */
1224 clear_tree_used (DECL_INITIAL (current_function_decl));
1226 /* Initialize local stack smashing state. */
1227 has_protected_decls = false;
1228 has_short_buffer = false;
1231 /* Free up stack variable graph data. */
1232 static void
1233 fini_vars_expansion (void)
1235 size_t i, n = stack_vars_num;
1236 for (i = 0; i < n; i++)
1237 BITMAP_FREE (stack_vars[i].conflicts);
1238 XDELETEVEC (stack_vars);
1239 XDELETEVEC (stack_vars_sorted);
1240 stack_vars = NULL;
1241 stack_vars_alloc = stack_vars_num = 0;
1244 /* Make a fair guess for the size of the stack frame of the current
1245 function. This doesn't have to be exact, the result is only used
1246 in the inline heuristics. So we don't want to run the full stack
1247 var packing algorithm (which is quadratic in the number of stack
1248 vars). Instead, we calculate the total size of all stack vars.
1249 This turns out to be a pretty fair estimate -- packing of stack
1250 vars doesn't happen very often. */
1252 HOST_WIDE_INT
1253 estimated_stack_frame_size (void)
1255 HOST_WIDE_INT size = 0;
1256 size_t i;
1257 tree t, outer_block = DECL_INITIAL (current_function_decl);
1259 init_vars_expansion ();
1261 for (t = cfun->local_decls; t; t = TREE_CHAIN (t))
1263 tree var = TREE_VALUE (t);
1265 if (TREE_USED (var))
1266 size += expand_one_var (var, true, false);
1267 TREE_USED (var) = 1;
1269 size += account_used_vars_for_block (outer_block, true);
1271 if (stack_vars_num > 0)
1273 /* Fake sorting the stack vars for account_stack_vars (). */
1274 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1275 for (i = 0; i < stack_vars_num; ++i)
1276 stack_vars_sorted[i] = i;
1277 size += account_stack_vars ();
1278 fini_vars_expansion ();
1281 return size;
1284 /* Expand all variables used in the function. */
1286 static void
1287 expand_used_vars (void)
1289 tree t, next, outer_block = DECL_INITIAL (current_function_decl);
1290 tree maybe_local_decls = NULL_TREE;
1291 unsigned i;
1293 /* Compute the phase of the stack frame for this function. */
1295 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1296 int off = STARTING_FRAME_OFFSET % align;
1297 frame_phase = off ? align - off : 0;
1300 init_vars_expansion ();
1302 for (i = 0; i < SA.map->num_partitions; i++)
1304 tree var = partition_to_var (SA.map, i);
1306 gcc_assert (is_gimple_reg (var));
1307 if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1308 expand_one_var (var, true, true);
1309 else
1311 /* This is a PARM_DECL or RESULT_DECL. For those partitions that
1312 contain the default def (representing the parm or result itself)
1313 we don't do anything here. But those which don't contain the
1314 default def (representing a temporary based on the parm/result)
1315 we need to allocate space just like for normal VAR_DECLs. */
1316 if (!bitmap_bit_p (SA.partition_has_default_def, i))
1318 expand_one_var (var, true, true);
1319 gcc_assert (SA.partition_to_pseudo[i]);
1324 /* At this point all variables on the local_decls with TREE_USED
1325 set are not associated with any block scope. Lay them out. */
1326 t = cfun->local_decls;
1327 cfun->local_decls = NULL_TREE;
1328 for (; t; t = next)
1330 tree var = TREE_VALUE (t);
1331 bool expand_now = false;
1333 next = TREE_CHAIN (t);
1335 /* Expanded above already. */
1336 if (is_gimple_reg (var))
1338 TREE_USED (var) = 0;
1339 goto next;
1341 /* We didn't set a block for static or extern because it's hard
1342 to tell the difference between a global variable (re)declared
1343 in a local scope, and one that's really declared there to
1344 begin with. And it doesn't really matter much, since we're
1345 not giving them stack space. Expand them now. */
1346 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1347 expand_now = true;
1349 /* If the variable is not associated with any block, then it
1350 was created by the optimizers, and could be live anywhere
1351 in the function. */
1352 else if (TREE_USED (var))
1353 expand_now = true;
1355 /* Finally, mark all variables on the list as used. We'll use
1356 this in a moment when we expand those associated with scopes. */
1357 TREE_USED (var) = 1;
1359 if (expand_now)
1360 expand_one_var (var, true, true);
1362 next:
1363 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1365 rtx rtl = DECL_RTL_IF_SET (var);
1367 /* Keep artificial non-ignored vars in cfun->local_decls
1368 chain until instantiate_decls. */
1369 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1371 TREE_CHAIN (t) = cfun->local_decls;
1372 cfun->local_decls = t;
1373 continue;
1375 else if (rtl == NULL_RTX)
1377 /* If rtl isn't set yet, which can happen e.g. with
1378 -fstack-protector, retry before returning from this
1379 function. */
1380 TREE_CHAIN (t) = maybe_local_decls;
1381 maybe_local_decls = t;
1382 continue;
1386 ggc_free (t);
1389 /* At this point, all variables within the block tree with TREE_USED
1390 set are actually used by the optimized function. Lay them out. */
1391 expand_used_vars_for_block (outer_block, true);
1393 if (stack_vars_num > 0)
1395 /* Due to the way alias sets work, no variables with non-conflicting
1396 alias sets may be assigned the same address. Add conflicts to
1397 reflect this. */
1398 add_alias_set_conflicts ();
1400 /* If stack protection is enabled, we don't share space between
1401 vulnerable data and non-vulnerable data. */
1402 if (flag_stack_protect)
1403 add_stack_protection_conflicts ();
1405 /* Now that we have collected all stack variables, and have computed a
1406 minimal interference graph, attempt to save some stack space. */
1407 partition_stack_vars ();
1408 if (dump_file)
1409 dump_stack_var_partition ();
1412 /* There are several conditions under which we should create a
1413 stack guard: protect-all, alloca used, protected decls present. */
1414 if (flag_stack_protect == 2
1415 || (flag_stack_protect
1416 && (cfun->calls_alloca || has_protected_decls)))
1417 create_stack_guard ();
1419 /* Assign rtl to each variable based on these partitions. */
1420 if (stack_vars_num > 0)
1422 /* Reorder decls to be protected by iterating over the variables
1423 array multiple times, and allocating out of each phase in turn. */
1424 /* ??? We could probably integrate this into the qsort we did
1425 earlier, such that we naturally see these variables first,
1426 and thus naturally allocate things in the right order. */
1427 if (has_protected_decls)
1429 /* Phase 1 contains only character arrays. */
1430 expand_stack_vars (stack_protect_decl_phase_1);
1432 /* Phase 2 contains other kinds of arrays. */
1433 if (flag_stack_protect == 2)
1434 expand_stack_vars (stack_protect_decl_phase_2);
1437 expand_stack_vars (NULL);
1439 fini_vars_expansion ();
1442 /* If there were any artificial non-ignored vars without rtl
1443 found earlier, see if deferred stack allocation hasn't assigned
1444 rtl to them. */
1445 for (t = maybe_local_decls; t; t = next)
1447 tree var = TREE_VALUE (t);
1448 rtx rtl = DECL_RTL_IF_SET (var);
1450 next = TREE_CHAIN (t);
1452 /* Keep artificial non-ignored vars in cfun->local_decls
1453 chain until instantiate_decls. */
1454 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1456 TREE_CHAIN (t) = cfun->local_decls;
1457 cfun->local_decls = t;
1458 continue;
1461 ggc_free (t);
1464 /* If the target requires that FRAME_OFFSET be aligned, do it. */
1465 if (STACK_ALIGNMENT_NEEDED)
1467 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1468 if (!FRAME_GROWS_DOWNWARD)
1469 frame_offset += align - 1;
1470 frame_offset &= -align;
1475 /* If we need to produce a detailed dump, print the tree representation
1476 for STMT to the dump file. SINCE is the last RTX after which the RTL
1477 generated for STMT should have been appended. */
1479 static void
1480 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx since)
1482 if (dump_file && (dump_flags & TDF_DETAILS))
1484 fprintf (dump_file, "\n;; ");
1485 print_gimple_stmt (dump_file, stmt, 0,
1486 TDF_SLIM | (dump_flags & TDF_LINENO));
1487 fprintf (dump_file, "\n");
1489 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1493 /* Maps the blocks that do not contain tree labels to rtx labels. */
1495 static struct pointer_map_t *lab_rtx_for_bb;
1497 /* Returns the label_rtx expression for a label starting basic block BB. */
1499 static rtx
1500 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
1502 gimple_stmt_iterator gsi;
1503 tree lab;
1504 gimple lab_stmt;
1505 void **elt;
1507 if (bb->flags & BB_RTL)
1508 return block_label (bb);
1510 elt = pointer_map_contains (lab_rtx_for_bb, bb);
1511 if (elt)
1512 return (rtx) *elt;
1514 /* Find the tree label if it is present. */
1516 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1518 lab_stmt = gsi_stmt (gsi);
1519 if (gimple_code (lab_stmt) != GIMPLE_LABEL)
1520 break;
1522 lab = gimple_label_label (lab_stmt);
1523 if (DECL_NONLOCAL (lab))
1524 break;
1526 return label_rtx (lab);
1529 elt = pointer_map_insert (lab_rtx_for_bb, bb);
1530 *elt = gen_label_rtx ();
1531 return (rtx) *elt;
1535 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
1536 of a basic block where we just expanded the conditional at the end,
1537 possibly clean up the CFG and instruction sequence. LAST is the
1538 last instruction before the just emitted jump sequence. */
1540 static void
1541 maybe_cleanup_end_of_block (edge e, rtx last)
1543 /* Special case: when jumpif decides that the condition is
1544 trivial it emits an unconditional jump (and the necessary
1545 barrier). But we still have two edges, the fallthru one is
1546 wrong. purge_dead_edges would clean this up later. Unfortunately
1547 we have to insert insns (and split edges) before
1548 find_many_sub_basic_blocks and hence before purge_dead_edges.
1549 But splitting edges might create new blocks which depend on the
1550 fact that if there are two edges there's no barrier. So the
1551 barrier would get lost and verify_flow_info would ICE. Instead
1552 of auditing all edge splitters to care for the barrier (which
1553 normally isn't there in a cleaned CFG), fix it here. */
1554 if (BARRIER_P (get_last_insn ()))
1556 rtx insn;
1557 remove_edge (e);
1558 /* Now, we have a single successor block, if we have insns to
1559 insert on the remaining edge we potentially will insert
1560 it at the end of this block (if the dest block isn't feasible)
1561 in order to avoid splitting the edge. This insertion will take
1562 place in front of the last jump. But we might have emitted
1563 multiple jumps (conditional and one unconditional) to the
1564 same destination. Inserting in front of the last one then
1565 is a problem. See PR 40021. We fix this by deleting all
1566 jumps except the last unconditional one. */
1567 insn = PREV_INSN (get_last_insn ());
1568 /* Make sure we have an unconditional jump. Otherwise we're
1569 confused. */
1570 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
1571 for (insn = PREV_INSN (insn); insn != last;)
1573 insn = PREV_INSN (insn);
1574 if (JUMP_P (NEXT_INSN (insn)))
1575 delete_insn (NEXT_INSN (insn));
1580 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
1581 Returns a new basic block if we've terminated the current basic
1582 block and created a new one. */
1584 static basic_block
1585 expand_gimple_cond (basic_block bb, gimple stmt)
1587 basic_block new_bb, dest;
1588 edge new_edge;
1589 edge true_edge;
1590 edge false_edge;
1591 rtx last2, last;
1592 enum tree_code code;
1593 tree op0, op1;
1595 code = gimple_cond_code (stmt);
1596 op0 = gimple_cond_lhs (stmt);
1597 op1 = gimple_cond_rhs (stmt);
1598 /* We're sometimes presented with such code:
1599 D.123_1 = x < y;
1600 if (D.123_1 != 0)
1602 This would expand to two comparisons which then later might
1603 be cleaned up by combine. But some pattern matchers like if-conversion
1604 work better when there's only one compare, so make up for this
1605 here as special exception if TER would have made the same change. */
1606 if (gimple_cond_single_var_p (stmt)
1607 && SA.values
1608 && TREE_CODE (op0) == SSA_NAME
1609 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
1611 gimple second = SSA_NAME_DEF_STMT (op0);
1612 if (gimple_code (second) == GIMPLE_ASSIGN)
1614 enum tree_code code2 = gimple_assign_rhs_code (second);
1615 if (TREE_CODE_CLASS (code2) == tcc_comparison)
1617 code = code2;
1618 op0 = gimple_assign_rhs1 (second);
1619 op1 = gimple_assign_rhs2 (second);
1621 /* If jumps are cheap turn some more codes into
1622 jumpy sequences. */
1623 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4)
1625 if ((code2 == BIT_AND_EXPR
1626 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
1627 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
1628 || code2 == TRUTH_AND_EXPR)
1630 code = TRUTH_ANDIF_EXPR;
1631 op0 = gimple_assign_rhs1 (second);
1632 op1 = gimple_assign_rhs2 (second);
1634 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
1636 code = TRUTH_ORIF_EXPR;
1637 op0 = gimple_assign_rhs1 (second);
1638 op1 = gimple_assign_rhs2 (second);
1644 last2 = last = get_last_insn ();
1646 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1647 if (gimple_has_location (stmt))
1649 set_curr_insn_source_location (gimple_location (stmt));
1650 set_curr_insn_block (gimple_block (stmt));
1653 /* These flags have no purpose in RTL land. */
1654 true_edge->flags &= ~EDGE_TRUE_VALUE;
1655 false_edge->flags &= ~EDGE_FALSE_VALUE;
1657 /* We can either have a pure conditional jump with one fallthru edge or
1658 two-way jump that needs to be decomposed into two basic blocks. */
1659 if (false_edge->dest == bb->next_bb)
1661 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
1662 true_edge->probability);
1663 maybe_dump_rtl_for_gimple_stmt (stmt, last);
1664 if (true_edge->goto_locus)
1666 set_curr_insn_source_location (true_edge->goto_locus);
1667 set_curr_insn_block (true_edge->goto_block);
1668 true_edge->goto_locus = curr_insn_locator ();
1670 true_edge->goto_block = NULL;
1671 false_edge->flags |= EDGE_FALLTHRU;
1672 maybe_cleanup_end_of_block (false_edge, last);
1673 return NULL;
1675 if (true_edge->dest == bb->next_bb)
1677 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
1678 false_edge->probability);
1679 maybe_dump_rtl_for_gimple_stmt (stmt, last);
1680 if (false_edge->goto_locus)
1682 set_curr_insn_source_location (false_edge->goto_locus);
1683 set_curr_insn_block (false_edge->goto_block);
1684 false_edge->goto_locus = curr_insn_locator ();
1686 false_edge->goto_block = NULL;
1687 true_edge->flags |= EDGE_FALLTHRU;
1688 maybe_cleanup_end_of_block (true_edge, last);
1689 return NULL;
1692 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
1693 true_edge->probability);
1694 last = get_last_insn ();
1695 if (false_edge->goto_locus)
1697 set_curr_insn_source_location (false_edge->goto_locus);
1698 set_curr_insn_block (false_edge->goto_block);
1699 false_edge->goto_locus = curr_insn_locator ();
1701 false_edge->goto_block = NULL;
1702 emit_jump (label_rtx_for_bb (false_edge->dest));
1704 BB_END (bb) = last;
1705 if (BARRIER_P (BB_END (bb)))
1706 BB_END (bb) = PREV_INSN (BB_END (bb));
1707 update_bb_for_insn (bb);
1709 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1710 dest = false_edge->dest;
1711 redirect_edge_succ (false_edge, new_bb);
1712 false_edge->flags |= EDGE_FALLTHRU;
1713 new_bb->count = false_edge->count;
1714 new_bb->frequency = EDGE_FREQUENCY (false_edge);
1715 new_edge = make_edge (new_bb, dest, 0);
1716 new_edge->probability = REG_BR_PROB_BASE;
1717 new_edge->count = new_bb->count;
1718 if (BARRIER_P (BB_END (new_bb)))
1719 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
1720 update_bb_for_insn (new_bb);
1722 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
1724 if (true_edge->goto_locus)
1726 set_curr_insn_source_location (true_edge->goto_locus);
1727 set_curr_insn_block (true_edge->goto_block);
1728 true_edge->goto_locus = curr_insn_locator ();
1730 true_edge->goto_block = NULL;
1732 return new_bb;
1735 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
1736 statement STMT. */
1738 static void
1739 expand_call_stmt (gimple stmt)
1741 tree exp;
1742 tree lhs = gimple_call_lhs (stmt);
1743 size_t i;
1744 bool builtin_p;
1745 tree decl;
1747 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
1749 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
1750 decl = gimple_call_fndecl (stmt);
1751 builtin_p = decl && DECL_BUILT_IN (decl);
1753 TREE_TYPE (exp) = gimple_call_return_type (stmt);
1754 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
1756 for (i = 0; i < gimple_call_num_args (stmt); i++)
1758 tree arg = gimple_call_arg (stmt, i);
1759 gimple def;
1760 /* TER addresses into arguments of builtin functions so we have a
1761 chance to infer more correct alignment information. See PR39954. */
1762 if (builtin_p
1763 && TREE_CODE (arg) == SSA_NAME
1764 && (def = get_gimple_for_ssa_name (arg))
1765 && gimple_assign_rhs_code (def) == ADDR_EXPR)
1766 arg = gimple_assign_rhs1 (def);
1767 CALL_EXPR_ARG (exp, i) = arg;
1770 if (gimple_has_side_effects (stmt))
1771 TREE_SIDE_EFFECTS (exp) = 1;
1773 if (gimple_call_nothrow_p (stmt))
1774 TREE_NOTHROW (exp) = 1;
1776 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
1777 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
1778 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
1779 CALL_CANNOT_INLINE_P (exp) = gimple_call_cannot_inline_p (stmt);
1780 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
1781 SET_EXPR_LOCATION (exp, gimple_location (stmt));
1782 TREE_BLOCK (exp) = gimple_block (stmt);
1784 if (lhs)
1785 expand_assignment (lhs, exp, false);
1786 else
1787 expand_expr_real_1 (exp, const0_rtx, VOIDmode, EXPAND_NORMAL, NULL);
1790 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
1791 STMT that doesn't require special handling for outgoing edges. That
1792 is no tailcalls and no GIMPLE_COND. */
1794 static void
1795 expand_gimple_stmt_1 (gimple stmt)
1797 tree op0;
1798 switch (gimple_code (stmt))
1800 case GIMPLE_GOTO:
1801 op0 = gimple_goto_dest (stmt);
1802 if (TREE_CODE (op0) == LABEL_DECL)
1803 expand_goto (op0);
1804 else
1805 expand_computed_goto (op0);
1806 break;
1807 case GIMPLE_LABEL:
1808 expand_label (gimple_label_label (stmt));
1809 break;
1810 case GIMPLE_NOP:
1811 case GIMPLE_PREDICT:
1812 break;
1813 case GIMPLE_SWITCH:
1814 expand_case (stmt);
1815 break;
1816 case GIMPLE_ASM:
1817 expand_asm_stmt (stmt);
1818 break;
1819 case GIMPLE_CALL:
1820 expand_call_stmt (stmt);
1821 break;
1823 case GIMPLE_RETURN:
1824 op0 = gimple_return_retval (stmt);
1826 if (op0 && op0 != error_mark_node)
1828 tree result = DECL_RESULT (current_function_decl);
1830 /* If we are not returning the current function's RESULT_DECL,
1831 build an assignment to it. */
1832 if (op0 != result)
1834 /* I believe that a function's RESULT_DECL is unique. */
1835 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
1837 /* ??? We'd like to use simply expand_assignment here,
1838 but this fails if the value is of BLKmode but the return
1839 decl is a register. expand_return has special handling
1840 for this combination, which eventually should move
1841 to common code. See comments there. Until then, let's
1842 build a modify expression :-/ */
1843 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
1844 result, op0);
1847 if (!op0)
1848 expand_null_return ();
1849 else
1850 expand_return (op0);
1851 break;
1853 case GIMPLE_ASSIGN:
1855 tree lhs = gimple_assign_lhs (stmt);
1857 /* Tree expand used to fiddle with |= and &= of two bitfield
1858 COMPONENT_REFs here. This can't happen with gimple, the LHS
1859 of binary assigns must be a gimple reg. */
1861 if (TREE_CODE (lhs) != SSA_NAME
1862 || get_gimple_rhs_class (gimple_expr_code (stmt))
1863 == GIMPLE_SINGLE_RHS)
1865 tree rhs = gimple_assign_rhs1 (stmt);
1866 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
1867 == GIMPLE_SINGLE_RHS);
1868 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
1869 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
1870 expand_assignment (lhs, rhs,
1871 gimple_assign_nontemporal_move_p (stmt));
1873 else
1875 rtx target, temp;
1876 bool nontemporal = gimple_assign_nontemporal_move_p (stmt);
1877 struct separate_ops ops;
1878 bool promoted = false;
1880 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
1881 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
1882 promoted = true;
1884 ops.code = gimple_assign_rhs_code (stmt);
1885 ops.type = TREE_TYPE (lhs);
1886 switch (get_gimple_rhs_class (gimple_expr_code (stmt)))
1888 case GIMPLE_BINARY_RHS:
1889 ops.op1 = gimple_assign_rhs2 (stmt);
1890 /* Fallthru */
1891 case GIMPLE_UNARY_RHS:
1892 ops.op0 = gimple_assign_rhs1 (stmt);
1893 break;
1894 default:
1895 gcc_unreachable ();
1897 ops.location = gimple_location (stmt);
1899 /* If we want to use a nontemporal store, force the value to
1900 register first. If we store into a promoted register,
1901 don't directly expand to target. */
1902 temp = nontemporal || promoted ? NULL_RTX : target;
1903 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
1904 EXPAND_NORMAL);
1906 if (temp == target)
1908 else if (promoted)
1910 int unsignedp = SUBREG_PROMOTED_UNSIGNED_P (target);
1911 /* If TEMP is a VOIDmode constant, use convert_modes to make
1912 sure that we properly convert it. */
1913 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
1915 temp = convert_modes (GET_MODE (target),
1916 TYPE_MODE (ops.type),
1917 temp, unsignedp);
1918 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
1919 GET_MODE (target), temp, unsignedp);
1922 convert_move (SUBREG_REG (target), temp, unsignedp);
1924 else if (nontemporal && emit_storent_insn (target, temp))
1926 else
1928 temp = force_operand (temp, target);
1929 if (temp != target)
1930 emit_move_insn (target, temp);
1934 break;
1936 default:
1937 gcc_unreachable ();
1941 /* Expand one gimple statement STMT and return the last RTL instruction
1942 before any of the newly generated ones.
1944 In addition to generating the necessary RTL instructions this also
1945 sets REG_EH_REGION notes if necessary and sets the current source
1946 location for diagnostics. */
1948 static rtx
1949 expand_gimple_stmt (gimple stmt)
1951 int lp_nr = 0;
1952 rtx last = NULL;
1953 location_t saved_location = input_location;
1955 last = get_last_insn ();
1957 /* If this is an expression of some kind and it has an associated line
1958 number, then emit the line number before expanding the expression.
1960 We need to save and restore the file and line information so that
1961 errors discovered during expansion are emitted with the right
1962 information. It would be better of the diagnostic routines
1963 used the file/line information embedded in the tree nodes rather
1964 than globals. */
1965 gcc_assert (cfun);
1967 if (gimple_has_location (stmt))
1969 input_location = gimple_location (stmt);
1970 set_curr_insn_source_location (input_location);
1972 /* Record where the insns produced belong. */
1973 set_curr_insn_block (gimple_block (stmt));
1976 expand_gimple_stmt_1 (stmt);
1977 /* Free any temporaries used to evaluate this statement. */
1978 free_temp_slots ();
1980 input_location = saved_location;
1982 /* Mark all insns that may trap. */
1983 lp_nr = lookup_stmt_eh_lp (stmt);
1984 if (lp_nr)
1986 rtx insn;
1987 for (insn = next_real_insn (last); insn;
1988 insn = next_real_insn (insn))
1990 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1991 /* If we want exceptions for non-call insns, any
1992 may_trap_p instruction may throw. */
1993 && GET_CODE (PATTERN (insn)) != CLOBBER
1994 && GET_CODE (PATTERN (insn)) != USE
1995 && insn_could_throw_p (insn))
1996 make_reg_eh_region_note (insn, 0, lp_nr);
2000 return last;
2003 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
2004 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
2005 generated a tail call (something that might be denied by the ABI
2006 rules governing the call; see calls.c).
2008 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
2009 can still reach the rest of BB. The case here is __builtin_sqrt,
2010 where the NaN result goes through the external function (with a
2011 tailcall) and the normal result happens via a sqrt instruction. */
2013 static basic_block
2014 expand_gimple_tailcall (basic_block bb, gimple stmt, bool *can_fallthru)
2016 rtx last2, last;
2017 edge e;
2018 edge_iterator ei;
2019 int probability;
2020 gcov_type count;
2022 last2 = last = expand_gimple_stmt (stmt);
2024 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
2025 if (CALL_P (last) && SIBLING_CALL_P (last))
2026 goto found;
2028 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2030 *can_fallthru = true;
2031 return NULL;
2033 found:
2034 /* ??? Wouldn't it be better to just reset any pending stack adjust?
2035 Any instructions emitted here are about to be deleted. */
2036 do_pending_stack_adjust ();
2038 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
2039 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
2040 EH or abnormal edges, we shouldn't have created a tail call in
2041 the first place. So it seems to me we should just be removing
2042 all edges here, or redirecting the existing fallthru edge to
2043 the exit block. */
2045 probability = 0;
2046 count = 0;
2048 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2050 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
2052 if (e->dest != EXIT_BLOCK_PTR)
2054 e->dest->count -= e->count;
2055 e->dest->frequency -= EDGE_FREQUENCY (e);
2056 if (e->dest->count < 0)
2057 e->dest->count = 0;
2058 if (e->dest->frequency < 0)
2059 e->dest->frequency = 0;
2061 count += e->count;
2062 probability += e->probability;
2063 remove_edge (e);
2065 else
2066 ei_next (&ei);
2069 /* This is somewhat ugly: the call_expr expander often emits instructions
2070 after the sibcall (to perform the function return). These confuse the
2071 find_many_sub_basic_blocks code, so we need to get rid of these. */
2072 last = NEXT_INSN (last);
2073 gcc_assert (BARRIER_P (last));
2075 *can_fallthru = false;
2076 while (NEXT_INSN (last))
2078 /* For instance an sqrt builtin expander expands if with
2079 sibcall in the then and label for `else`. */
2080 if (LABEL_P (NEXT_INSN (last)))
2082 *can_fallthru = true;
2083 break;
2085 delete_insn (NEXT_INSN (last));
2088 e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
2089 e->probability += probability;
2090 e->count += count;
2091 BB_END (bb) = last;
2092 update_bb_for_insn (bb);
2094 if (NEXT_INSN (last))
2096 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2098 last = BB_END (bb);
2099 if (BARRIER_P (last))
2100 BB_END (bb) = PREV_INSN (last);
2103 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2105 return bb;
2108 /* Return the difference between the floor and the truncated result of
2109 a signed division by OP1 with remainder MOD. */
2110 static rtx
2111 floor_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2113 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
2114 return gen_rtx_IF_THEN_ELSE
2115 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2116 gen_rtx_IF_THEN_ELSE
2117 (mode, gen_rtx_LT (BImode,
2118 gen_rtx_DIV (mode, op1, mod),
2119 const0_rtx),
2120 constm1_rtx, const0_rtx),
2121 const0_rtx);
2124 /* Return the difference between the ceil and the truncated result of
2125 a signed division by OP1 with remainder MOD. */
2126 static rtx
2127 ceil_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2129 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
2130 return gen_rtx_IF_THEN_ELSE
2131 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2132 gen_rtx_IF_THEN_ELSE
2133 (mode, gen_rtx_GT (BImode,
2134 gen_rtx_DIV (mode, op1, mod),
2135 const0_rtx),
2136 const1_rtx, const0_rtx),
2137 const0_rtx);
2140 /* Return the difference between the ceil and the truncated result of
2141 an unsigned division by OP1 with remainder MOD. */
2142 static rtx
2143 ceil_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
2145 /* (mod != 0 ? 1 : 0) */
2146 return gen_rtx_IF_THEN_ELSE
2147 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2148 const1_rtx, const0_rtx);
2151 /* Return the difference between the rounded and the truncated result
2152 of a signed division by OP1 with remainder MOD. Halfway cases are
2153 rounded away from zero, rather than to the nearest even number. */
2154 static rtx
2155 round_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2157 /* (abs (mod) >= abs (op1) - abs (mod)
2158 ? (op1 / mod > 0 ? 1 : -1)
2159 : 0) */
2160 return gen_rtx_IF_THEN_ELSE
2161 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
2162 gen_rtx_MINUS (mode,
2163 gen_rtx_ABS (mode, op1),
2164 gen_rtx_ABS (mode, mod))),
2165 gen_rtx_IF_THEN_ELSE
2166 (mode, gen_rtx_GT (BImode,
2167 gen_rtx_DIV (mode, op1, mod),
2168 const0_rtx),
2169 const1_rtx, constm1_rtx),
2170 const0_rtx);
2173 /* Return the difference between the rounded and the truncated result
2174 of a unsigned division by OP1 with remainder MOD. Halfway cases
2175 are rounded away from zero, rather than to the nearest even
2176 number. */
2177 static rtx
2178 round_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2180 /* (mod >= op1 - mod ? 1 : 0) */
2181 return gen_rtx_IF_THEN_ELSE
2182 (mode, gen_rtx_GE (BImode, mod,
2183 gen_rtx_MINUS (mode, op1, mod)),
2184 const1_rtx, const0_rtx);
2187 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
2188 any rtl. */
2190 static rtx
2191 convert_debug_memory_address (enum machine_mode mode, rtx x)
2193 enum machine_mode xmode = GET_MODE (x);
2195 #ifndef POINTERS_EXTEND_UNSIGNED
2196 gcc_assert (mode == Pmode);
2197 gcc_assert (xmode == mode || xmode == VOIDmode);
2198 #else
2199 gcc_assert (mode == Pmode || mode == ptr_mode);
2201 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
2202 return x;
2204 if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (xmode))
2205 x = simplify_gen_subreg (mode, x, xmode,
2206 subreg_lowpart_offset
2207 (mode, xmode));
2208 else if (POINTERS_EXTEND_UNSIGNED > 0)
2209 x = gen_rtx_ZERO_EXTEND (mode, x);
2210 else if (!POINTERS_EXTEND_UNSIGNED)
2211 x = gen_rtx_SIGN_EXTEND (mode, x);
2212 else
2213 gcc_unreachable ();
2214 #endif /* POINTERS_EXTEND_UNSIGNED */
2216 return x;
2219 /* Return an RTX equivalent to the value of the tree expression
2220 EXP. */
2222 static rtx
2223 expand_debug_expr (tree exp)
2225 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
2226 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2227 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
2228 addr_space_t as;
2229 enum machine_mode address_mode;
2231 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
2233 case tcc_expression:
2234 switch (TREE_CODE (exp))
2236 case COND_EXPR:
2237 case DOT_PROD_EXPR:
2238 goto ternary;
2240 case TRUTH_ANDIF_EXPR:
2241 case TRUTH_ORIF_EXPR:
2242 case TRUTH_AND_EXPR:
2243 case TRUTH_OR_EXPR:
2244 case TRUTH_XOR_EXPR:
2245 goto binary;
2247 case TRUTH_NOT_EXPR:
2248 goto unary;
2250 default:
2251 break;
2253 break;
2255 ternary:
2256 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
2257 if (!op2)
2258 return NULL_RTX;
2259 /* Fall through. */
2261 binary:
2262 case tcc_binary:
2263 case tcc_comparison:
2264 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
2265 if (!op1)
2266 return NULL_RTX;
2267 /* Fall through. */
2269 unary:
2270 case tcc_unary:
2271 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2272 if (!op0)
2273 return NULL_RTX;
2274 break;
2276 case tcc_type:
2277 case tcc_statement:
2278 gcc_unreachable ();
2280 case tcc_constant:
2281 case tcc_exceptional:
2282 case tcc_declaration:
2283 case tcc_reference:
2284 case tcc_vl_exp:
2285 break;
2288 switch (TREE_CODE (exp))
2290 case STRING_CST:
2291 if (!lookup_constant_def (exp))
2293 if (strlen (TREE_STRING_POINTER (exp)) + 1
2294 != (size_t) TREE_STRING_LENGTH (exp))
2295 return NULL_RTX;
2296 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
2297 op0 = gen_rtx_MEM (BLKmode, op0);
2298 set_mem_attributes (op0, exp, 0);
2299 return op0;
2301 /* Fall through... */
2303 case INTEGER_CST:
2304 case REAL_CST:
2305 case FIXED_CST:
2306 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
2307 return op0;
2309 case COMPLEX_CST:
2310 gcc_assert (COMPLEX_MODE_P (mode));
2311 op0 = expand_debug_expr (TREE_REALPART (exp));
2312 op1 = expand_debug_expr (TREE_IMAGPART (exp));
2313 return gen_rtx_CONCAT (mode, op0, op1);
2315 case DEBUG_EXPR_DECL:
2316 op0 = DECL_RTL_IF_SET (exp);
2318 if (op0)
2319 return op0;
2321 op0 = gen_rtx_DEBUG_EXPR (mode);
2322 DEBUG_EXPR_TREE_DECL (op0) = exp;
2323 SET_DECL_RTL (exp, op0);
2325 return op0;
2327 case VAR_DECL:
2328 case PARM_DECL:
2329 case FUNCTION_DECL:
2330 case LABEL_DECL:
2331 case CONST_DECL:
2332 case RESULT_DECL:
2333 op0 = DECL_RTL_IF_SET (exp);
2335 /* This decl was probably optimized away. */
2336 if (!op0)
2338 if (TREE_CODE (exp) != VAR_DECL
2339 || DECL_EXTERNAL (exp)
2340 || !TREE_STATIC (exp)
2341 || !DECL_NAME (exp)
2342 || DECL_HARD_REGISTER (exp)
2343 || mode == VOIDmode)
2344 return NULL;
2346 op0 = make_decl_rtl_for_debug (exp);
2347 if (!MEM_P (op0)
2348 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
2349 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
2350 return NULL;
2352 else
2353 op0 = copy_rtx (op0);
2355 if (GET_MODE (op0) == BLKmode
2356 /* If op0 is not BLKmode, but BLKmode is, adjust_mode
2357 below would ICE. While it is likely a FE bug,
2358 try to be robust here. See PR43166. */
2359 || mode == BLKmode
2360 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
2362 gcc_assert (MEM_P (op0));
2363 op0 = adjust_address_nv (op0, mode, 0);
2364 return op0;
2367 /* Fall through. */
2369 adjust_mode:
2370 case PAREN_EXPR:
2371 case NOP_EXPR:
2372 case CONVERT_EXPR:
2374 enum machine_mode inner_mode = GET_MODE (op0);
2376 if (mode == inner_mode)
2377 return op0;
2379 if (inner_mode == VOIDmode)
2381 if (TREE_CODE (exp) == SSA_NAME)
2382 inner_mode = TYPE_MODE (TREE_TYPE (exp));
2383 else
2384 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
2385 if (mode == inner_mode)
2386 return op0;
2389 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
2391 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
2392 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
2393 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
2394 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
2395 else
2396 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
2398 else if (FLOAT_MODE_P (mode))
2400 gcc_assert (TREE_CODE (exp) != SSA_NAME);
2401 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
2402 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
2403 else
2404 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
2406 else if (FLOAT_MODE_P (inner_mode))
2408 if (unsignedp)
2409 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
2410 else
2411 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
2413 else if (CONSTANT_P (op0)
2414 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
2415 op0 = simplify_gen_subreg (mode, op0, inner_mode,
2416 subreg_lowpart_offset (mode,
2417 inner_mode));
2418 else if (unsignedp)
2419 op0 = gen_rtx_ZERO_EXTEND (mode, op0);
2420 else
2421 op0 = gen_rtx_SIGN_EXTEND (mode, op0);
2423 return op0;
2426 case INDIRECT_REF:
2427 case ALIGN_INDIRECT_REF:
2428 case MISALIGNED_INDIRECT_REF:
2429 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2430 if (!op0)
2431 return NULL;
2433 if (POINTER_TYPE_P (TREE_TYPE (exp)))
2435 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
2436 address_mode = targetm.addr_space.address_mode (as);
2438 else
2440 as = ADDR_SPACE_GENERIC;
2441 address_mode = Pmode;
2444 if (TREE_CODE (exp) == ALIGN_INDIRECT_REF)
2446 int align = TYPE_ALIGN_UNIT (TREE_TYPE (exp));
2447 op0 = gen_rtx_AND (address_mode, op0, GEN_INT (-align));
2450 op0 = gen_rtx_MEM (mode, op0);
2452 set_mem_attributes (op0, exp, 0);
2453 set_mem_addr_space (op0, as);
2455 return op0;
2457 case TARGET_MEM_REF:
2458 if (TMR_SYMBOL (exp) && !DECL_RTL_SET_P (TMR_SYMBOL (exp)))
2459 return NULL;
2461 op0 = expand_debug_expr
2462 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
2463 if (!op0)
2464 return NULL;
2466 as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
2468 op0 = gen_rtx_MEM (mode, op0);
2470 set_mem_attributes (op0, exp, 0);
2471 set_mem_addr_space (op0, as);
2473 return op0;
2475 case ARRAY_REF:
2476 case ARRAY_RANGE_REF:
2477 case COMPONENT_REF:
2478 case BIT_FIELD_REF:
2479 case REALPART_EXPR:
2480 case IMAGPART_EXPR:
2481 case VIEW_CONVERT_EXPR:
2483 enum machine_mode mode1;
2484 HOST_WIDE_INT bitsize, bitpos;
2485 tree offset;
2486 int volatilep = 0;
2487 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
2488 &mode1, &unsignedp, &volatilep, false);
2489 rtx orig_op0;
2491 if (bitsize == 0)
2492 return NULL;
2494 orig_op0 = op0 = expand_debug_expr (tem);
2496 if (!op0)
2497 return NULL;
2499 if (offset)
2501 enum machine_mode addrmode, offmode;
2503 if (!MEM_P (op0))
2504 return NULL;
2506 op0 = XEXP (op0, 0);
2507 addrmode = GET_MODE (op0);
2508 if (addrmode == VOIDmode)
2509 addrmode = Pmode;
2511 op1 = expand_debug_expr (offset);
2512 if (!op1)
2513 return NULL;
2515 offmode = GET_MODE (op1);
2516 if (offmode == VOIDmode)
2517 offmode = TYPE_MODE (TREE_TYPE (offset));
2519 if (addrmode != offmode)
2520 op1 = simplify_gen_subreg (addrmode, op1, offmode,
2521 subreg_lowpart_offset (addrmode,
2522 offmode));
2524 /* Don't use offset_address here, we don't need a
2525 recognizable address, and we don't want to generate
2526 code. */
2527 op0 = gen_rtx_MEM (mode, gen_rtx_PLUS (addrmode, op0, op1));
2530 if (MEM_P (op0))
2532 if (mode1 == VOIDmode)
2533 /* Bitfield. */
2534 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
2535 if (bitpos >= BITS_PER_UNIT)
2537 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
2538 bitpos %= BITS_PER_UNIT;
2540 else if (bitpos < 0)
2542 HOST_WIDE_INT units
2543 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
2544 op0 = adjust_address_nv (op0, mode1, units);
2545 bitpos += units * BITS_PER_UNIT;
2547 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
2548 op0 = adjust_address_nv (op0, mode, 0);
2549 else if (GET_MODE (op0) != mode1)
2550 op0 = adjust_address_nv (op0, mode1, 0);
2551 else
2552 op0 = copy_rtx (op0);
2553 if (op0 == orig_op0)
2554 op0 = shallow_copy_rtx (op0);
2555 set_mem_attributes (op0, exp, 0);
2558 if (bitpos == 0 && mode == GET_MODE (op0))
2559 return op0;
2561 if (bitpos < 0)
2562 return NULL;
2564 if ((bitpos % BITS_PER_UNIT) == 0
2565 && bitsize == GET_MODE_BITSIZE (mode1))
2567 enum machine_mode opmode = GET_MODE (op0);
2569 gcc_assert (opmode != BLKmode);
2571 if (opmode == VOIDmode)
2572 opmode = mode1;
2574 /* This condition may hold if we're expanding the address
2575 right past the end of an array that turned out not to
2576 be addressable (i.e., the address was only computed in
2577 debug stmts). The gen_subreg below would rightfully
2578 crash, and the address doesn't really exist, so just
2579 drop it. */
2580 if (bitpos >= GET_MODE_BITSIZE (opmode))
2581 return NULL;
2583 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
2584 return simplify_gen_subreg (mode, op0, opmode,
2585 bitpos / BITS_PER_UNIT);
2588 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
2589 && TYPE_UNSIGNED (TREE_TYPE (exp))
2590 ? SIGN_EXTRACT
2591 : ZERO_EXTRACT, mode,
2592 GET_MODE (op0) != VOIDmode
2593 ? GET_MODE (op0) : mode1,
2594 op0, GEN_INT (bitsize), GEN_INT (bitpos));
2597 case ABS_EXPR:
2598 return gen_rtx_ABS (mode, op0);
2600 case NEGATE_EXPR:
2601 return gen_rtx_NEG (mode, op0);
2603 case BIT_NOT_EXPR:
2604 return gen_rtx_NOT (mode, op0);
2606 case FLOAT_EXPR:
2607 if (unsignedp)
2608 return gen_rtx_UNSIGNED_FLOAT (mode, op0);
2609 else
2610 return gen_rtx_FLOAT (mode, op0);
2612 case FIX_TRUNC_EXPR:
2613 if (unsignedp)
2614 return gen_rtx_UNSIGNED_FIX (mode, op0);
2615 else
2616 return gen_rtx_FIX (mode, op0);
2618 case POINTER_PLUS_EXPR:
2619 /* For the rare target where pointers are not the same size as
2620 size_t, we need to check for mis-matched modes and correct
2621 the addend. */
2622 if (op0 && op1
2623 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
2624 && GET_MODE (op0) != GET_MODE (op1))
2626 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1)))
2627 op1 = gen_rtx_TRUNCATE (GET_MODE (op0), op1);
2628 else
2629 /* We always sign-extend, regardless of the signedness of
2630 the operand, because the operand is always unsigned
2631 here even if the original C expression is signed. */
2632 op1 = gen_rtx_SIGN_EXTEND (GET_MODE (op0), op1);
2634 /* Fall through. */
2635 case PLUS_EXPR:
2636 return gen_rtx_PLUS (mode, op0, op1);
2638 case MINUS_EXPR:
2639 return gen_rtx_MINUS (mode, op0, op1);
2641 case MULT_EXPR:
2642 return gen_rtx_MULT (mode, op0, op1);
2644 case RDIV_EXPR:
2645 case TRUNC_DIV_EXPR:
2646 case EXACT_DIV_EXPR:
2647 if (unsignedp)
2648 return gen_rtx_UDIV (mode, op0, op1);
2649 else
2650 return gen_rtx_DIV (mode, op0, op1);
2652 case TRUNC_MOD_EXPR:
2653 if (unsignedp)
2654 return gen_rtx_UMOD (mode, op0, op1);
2655 else
2656 return gen_rtx_MOD (mode, op0, op1);
2658 case FLOOR_DIV_EXPR:
2659 if (unsignedp)
2660 return gen_rtx_UDIV (mode, op0, op1);
2661 else
2663 rtx div = gen_rtx_DIV (mode, op0, op1);
2664 rtx mod = gen_rtx_MOD (mode, op0, op1);
2665 rtx adj = floor_sdiv_adjust (mode, mod, op1);
2666 return gen_rtx_PLUS (mode, div, adj);
2669 case FLOOR_MOD_EXPR:
2670 if (unsignedp)
2671 return gen_rtx_UMOD (mode, op0, op1);
2672 else
2674 rtx mod = gen_rtx_MOD (mode, op0, op1);
2675 rtx adj = floor_sdiv_adjust (mode, mod, op1);
2676 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2677 return gen_rtx_PLUS (mode, mod, adj);
2680 case CEIL_DIV_EXPR:
2681 if (unsignedp)
2683 rtx div = gen_rtx_UDIV (mode, op0, op1);
2684 rtx mod = gen_rtx_UMOD (mode, op0, op1);
2685 rtx adj = ceil_udiv_adjust (mode, mod, op1);
2686 return gen_rtx_PLUS (mode, div, adj);
2688 else
2690 rtx div = gen_rtx_DIV (mode, op0, op1);
2691 rtx mod = gen_rtx_MOD (mode, op0, op1);
2692 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
2693 return gen_rtx_PLUS (mode, div, adj);
2696 case CEIL_MOD_EXPR:
2697 if (unsignedp)
2699 rtx mod = gen_rtx_UMOD (mode, op0, op1);
2700 rtx adj = ceil_udiv_adjust (mode, mod, op1);
2701 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2702 return gen_rtx_PLUS (mode, mod, adj);
2704 else
2706 rtx mod = gen_rtx_MOD (mode, op0, op1);
2707 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
2708 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2709 return gen_rtx_PLUS (mode, mod, adj);
2712 case ROUND_DIV_EXPR:
2713 if (unsignedp)
2715 rtx div = gen_rtx_UDIV (mode, op0, op1);
2716 rtx mod = gen_rtx_UMOD (mode, op0, op1);
2717 rtx adj = round_udiv_adjust (mode, mod, op1);
2718 return gen_rtx_PLUS (mode, div, adj);
2720 else
2722 rtx div = gen_rtx_DIV (mode, op0, op1);
2723 rtx mod = gen_rtx_MOD (mode, op0, op1);
2724 rtx adj = round_sdiv_adjust (mode, mod, op1);
2725 return gen_rtx_PLUS (mode, div, adj);
2728 case ROUND_MOD_EXPR:
2729 if (unsignedp)
2731 rtx mod = gen_rtx_UMOD (mode, op0, op1);
2732 rtx adj = round_udiv_adjust (mode, mod, op1);
2733 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2734 return gen_rtx_PLUS (mode, mod, adj);
2736 else
2738 rtx mod = gen_rtx_MOD (mode, op0, op1);
2739 rtx adj = round_sdiv_adjust (mode, mod, op1);
2740 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2741 return gen_rtx_PLUS (mode, mod, adj);
2744 case LSHIFT_EXPR:
2745 return gen_rtx_ASHIFT (mode, op0, op1);
2747 case RSHIFT_EXPR:
2748 if (unsignedp)
2749 return gen_rtx_LSHIFTRT (mode, op0, op1);
2750 else
2751 return gen_rtx_ASHIFTRT (mode, op0, op1);
2753 case LROTATE_EXPR:
2754 return gen_rtx_ROTATE (mode, op0, op1);
2756 case RROTATE_EXPR:
2757 return gen_rtx_ROTATERT (mode, op0, op1);
2759 case MIN_EXPR:
2760 if (unsignedp)
2761 return gen_rtx_UMIN (mode, op0, op1);
2762 else
2763 return gen_rtx_SMIN (mode, op0, op1);
2765 case MAX_EXPR:
2766 if (unsignedp)
2767 return gen_rtx_UMAX (mode, op0, op1);
2768 else
2769 return gen_rtx_SMAX (mode, op0, op1);
2771 case BIT_AND_EXPR:
2772 case TRUTH_AND_EXPR:
2773 return gen_rtx_AND (mode, op0, op1);
2775 case BIT_IOR_EXPR:
2776 case TRUTH_OR_EXPR:
2777 return gen_rtx_IOR (mode, op0, op1);
2779 case BIT_XOR_EXPR:
2780 case TRUTH_XOR_EXPR:
2781 return gen_rtx_XOR (mode, op0, op1);
2783 case TRUTH_ANDIF_EXPR:
2784 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
2786 case TRUTH_ORIF_EXPR:
2787 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
2789 case TRUTH_NOT_EXPR:
2790 return gen_rtx_EQ (mode, op0, const0_rtx);
2792 case LT_EXPR:
2793 if (unsignedp)
2794 return gen_rtx_LTU (mode, op0, op1);
2795 else
2796 return gen_rtx_LT (mode, op0, op1);
2798 case LE_EXPR:
2799 if (unsignedp)
2800 return gen_rtx_LEU (mode, op0, op1);
2801 else
2802 return gen_rtx_LE (mode, op0, op1);
2804 case GT_EXPR:
2805 if (unsignedp)
2806 return gen_rtx_GTU (mode, op0, op1);
2807 else
2808 return gen_rtx_GT (mode, op0, op1);
2810 case GE_EXPR:
2811 if (unsignedp)
2812 return gen_rtx_GEU (mode, op0, op1);
2813 else
2814 return gen_rtx_GE (mode, op0, op1);
2816 case EQ_EXPR:
2817 return gen_rtx_EQ (mode, op0, op1);
2819 case NE_EXPR:
2820 return gen_rtx_NE (mode, op0, op1);
2822 case UNORDERED_EXPR:
2823 return gen_rtx_UNORDERED (mode, op0, op1);
2825 case ORDERED_EXPR:
2826 return gen_rtx_ORDERED (mode, op0, op1);
2828 case UNLT_EXPR:
2829 return gen_rtx_UNLT (mode, op0, op1);
2831 case UNLE_EXPR:
2832 return gen_rtx_UNLE (mode, op0, op1);
2834 case UNGT_EXPR:
2835 return gen_rtx_UNGT (mode, op0, op1);
2837 case UNGE_EXPR:
2838 return gen_rtx_UNGE (mode, op0, op1);
2840 case UNEQ_EXPR:
2841 return gen_rtx_UNEQ (mode, op0, op1);
2843 case LTGT_EXPR:
2844 return gen_rtx_LTGT (mode, op0, op1);
2846 case COND_EXPR:
2847 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
2849 case COMPLEX_EXPR:
2850 gcc_assert (COMPLEX_MODE_P (mode));
2851 if (GET_MODE (op0) == VOIDmode)
2852 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
2853 if (GET_MODE (op1) == VOIDmode)
2854 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
2855 return gen_rtx_CONCAT (mode, op0, op1);
2857 case CONJ_EXPR:
2858 if (GET_CODE (op0) == CONCAT)
2859 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
2860 gen_rtx_NEG (GET_MODE_INNER (mode),
2861 XEXP (op0, 1)));
2862 else
2864 enum machine_mode imode = GET_MODE_INNER (mode);
2865 rtx re, im;
2867 if (MEM_P (op0))
2869 re = adjust_address_nv (op0, imode, 0);
2870 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
2872 else
2874 enum machine_mode ifmode = int_mode_for_mode (mode);
2875 enum machine_mode ihmode = int_mode_for_mode (imode);
2876 rtx halfsize;
2877 if (ifmode == BLKmode || ihmode == BLKmode)
2878 return NULL;
2879 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
2880 re = op0;
2881 if (mode != ifmode)
2882 re = gen_rtx_SUBREG (ifmode, re, 0);
2883 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
2884 if (imode != ihmode)
2885 re = gen_rtx_SUBREG (imode, re, 0);
2886 im = copy_rtx (op0);
2887 if (mode != ifmode)
2888 im = gen_rtx_SUBREG (ifmode, im, 0);
2889 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
2890 if (imode != ihmode)
2891 im = gen_rtx_SUBREG (imode, im, 0);
2893 im = gen_rtx_NEG (imode, im);
2894 return gen_rtx_CONCAT (mode, re, im);
2897 case ADDR_EXPR:
2898 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2899 if (!op0 || !MEM_P (op0))
2900 return NULL;
2902 op0 = convert_debug_memory_address (mode, XEXP (op0, 0));
2904 return op0;
2906 case VECTOR_CST:
2907 exp = build_constructor_from_list (TREE_TYPE (exp),
2908 TREE_VECTOR_CST_ELTS (exp));
2909 /* Fall through. */
2911 case CONSTRUCTOR:
2912 if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
2914 unsigned i;
2915 tree val;
2917 op0 = gen_rtx_CONCATN
2918 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
2920 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
2922 op1 = expand_debug_expr (val);
2923 if (!op1)
2924 return NULL;
2925 XVECEXP (op0, 0, i) = op1;
2928 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
2930 op1 = expand_debug_expr
2931 (fold_convert (TREE_TYPE (TREE_TYPE (exp)), integer_zero_node));
2933 if (!op1)
2934 return NULL;
2936 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
2937 XVECEXP (op0, 0, i) = op1;
2940 return op0;
2942 else
2943 goto flag_unsupported;
2945 case CALL_EXPR:
2946 /* ??? Maybe handle some builtins? */
2947 return NULL;
2949 case SSA_NAME:
2951 gimple g = get_gimple_for_ssa_name (exp);
2952 if (g)
2954 op0 = expand_debug_expr (gimple_assign_rhs_to_tree (g));
2955 if (!op0)
2956 return NULL;
2958 else
2960 int part = var_to_partition (SA.map, exp);
2962 if (part == NO_PARTITION)
2963 return NULL;
2965 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
2967 op0 = SA.partition_to_pseudo[part];
2969 goto adjust_mode;
2972 case ERROR_MARK:
2973 return NULL;
2975 /* Vector stuff. For most of the codes we don't have rtl codes. */
2976 case REALIGN_LOAD_EXPR:
2977 case REDUC_MAX_EXPR:
2978 case REDUC_MIN_EXPR:
2979 case REDUC_PLUS_EXPR:
2980 case VEC_COND_EXPR:
2981 case VEC_EXTRACT_EVEN_EXPR:
2982 case VEC_EXTRACT_ODD_EXPR:
2983 case VEC_INTERLEAVE_HIGH_EXPR:
2984 case VEC_INTERLEAVE_LOW_EXPR:
2985 case VEC_LSHIFT_EXPR:
2986 case VEC_PACK_FIX_TRUNC_EXPR:
2987 case VEC_PACK_SAT_EXPR:
2988 case VEC_PACK_TRUNC_EXPR:
2989 case VEC_RSHIFT_EXPR:
2990 case VEC_UNPACK_FLOAT_HI_EXPR:
2991 case VEC_UNPACK_FLOAT_LO_EXPR:
2992 case VEC_UNPACK_HI_EXPR:
2993 case VEC_UNPACK_LO_EXPR:
2994 case VEC_WIDEN_MULT_HI_EXPR:
2995 case VEC_WIDEN_MULT_LO_EXPR:
2996 return NULL;
2998 /* Misc codes. */
2999 case ADDR_SPACE_CONVERT_EXPR:
3000 case FIXED_CONVERT_EXPR:
3001 case OBJ_TYPE_REF:
3002 case WITH_SIZE_EXPR:
3003 return NULL;
3005 case DOT_PROD_EXPR:
3006 if (SCALAR_INT_MODE_P (GET_MODE (op0))
3007 && SCALAR_INT_MODE_P (mode))
3009 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3010 op0 = gen_rtx_ZERO_EXTEND (mode, op0);
3011 else
3012 op0 = gen_rtx_SIGN_EXTEND (mode, op0);
3013 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
3014 op1 = gen_rtx_ZERO_EXTEND (mode, op1);
3015 else
3016 op1 = gen_rtx_SIGN_EXTEND (mode, op1);
3017 op0 = gen_rtx_MULT (mode, op0, op1);
3018 return gen_rtx_PLUS (mode, op0, op2);
3020 return NULL;
3022 case WIDEN_MULT_EXPR:
3023 if (SCALAR_INT_MODE_P (GET_MODE (op0))
3024 && SCALAR_INT_MODE_P (mode))
3026 enum machine_mode inner_mode = GET_MODE (op0);
3027 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3028 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
3029 else
3030 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
3031 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
3032 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
3033 else
3034 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
3035 return gen_rtx_MULT (mode, op0, op1);
3037 return NULL;
3039 case WIDEN_SUM_EXPR:
3040 if (SCALAR_INT_MODE_P (GET_MODE (op0))
3041 && SCALAR_INT_MODE_P (mode))
3043 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3044 op0 = gen_rtx_ZERO_EXTEND (mode, op0);
3045 else
3046 op0 = gen_rtx_SIGN_EXTEND (mode, op0);
3047 return gen_rtx_PLUS (mode, op0, op1);
3049 return NULL;
3051 default:
3052 flag_unsupported:
3053 #ifdef ENABLE_CHECKING
3054 debug_tree (exp);
3055 gcc_unreachable ();
3056 #else
3057 return NULL;
3058 #endif
3062 /* Expand the _LOCs in debug insns. We run this after expanding all
3063 regular insns, so that any variables referenced in the function
3064 will have their DECL_RTLs set. */
3066 static void
3067 expand_debug_locations (void)
3069 rtx insn;
3070 rtx last = get_last_insn ();
3071 int save_strict_alias = flag_strict_aliasing;
3073 /* New alias sets while setting up memory attributes cause
3074 -fcompare-debug failures, even though it doesn't bring about any
3075 codegen changes. */
3076 flag_strict_aliasing = 0;
3078 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3079 if (DEBUG_INSN_P (insn))
3081 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
3082 rtx val;
3083 enum machine_mode mode;
3085 if (value == NULL_TREE)
3086 val = NULL_RTX;
3087 else
3089 val = expand_debug_expr (value);
3090 gcc_assert (last == get_last_insn ());
3093 if (!val)
3094 val = gen_rtx_UNKNOWN_VAR_LOC ();
3095 else
3097 mode = GET_MODE (INSN_VAR_LOCATION (insn));
3099 gcc_assert (mode == GET_MODE (val)
3100 || (GET_MODE (val) == VOIDmode
3101 && (CONST_INT_P (val)
3102 || GET_CODE (val) == CONST_FIXED
3103 || GET_CODE (val) == CONST_DOUBLE
3104 || GET_CODE (val) == LABEL_REF)));
3107 INSN_VAR_LOCATION_LOC (insn) = val;
3110 flag_strict_aliasing = save_strict_alias;
3113 /* Expand basic block BB from GIMPLE trees to RTL. */
3115 static basic_block
3116 expand_gimple_basic_block (basic_block bb)
3118 gimple_stmt_iterator gsi;
3119 gimple_seq stmts;
3120 gimple stmt = NULL;
3121 rtx note, last;
3122 edge e;
3123 edge_iterator ei;
3124 void **elt;
3126 if (dump_file)
3127 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
3128 bb->index);
3130 /* Note that since we are now transitioning from GIMPLE to RTL, we
3131 cannot use the gsi_*_bb() routines because they expect the basic
3132 block to be in GIMPLE, instead of RTL. Therefore, we need to
3133 access the BB sequence directly. */
3134 stmts = bb_seq (bb);
3135 bb->il.gimple = NULL;
3136 rtl_profile_for_bb (bb);
3137 init_rtl_bb_info (bb);
3138 bb->flags |= BB_RTL;
3140 /* Remove the RETURN_EXPR if we may fall though to the exit
3141 instead. */
3142 gsi = gsi_last (stmts);
3143 if (!gsi_end_p (gsi)
3144 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
3146 gimple ret_stmt = gsi_stmt (gsi);
3148 gcc_assert (single_succ_p (bb));
3149 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
3151 if (bb->next_bb == EXIT_BLOCK_PTR
3152 && !gimple_return_retval (ret_stmt))
3154 gsi_remove (&gsi, false);
3155 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
3159 gsi = gsi_start (stmts);
3160 if (!gsi_end_p (gsi))
3162 stmt = gsi_stmt (gsi);
3163 if (gimple_code (stmt) != GIMPLE_LABEL)
3164 stmt = NULL;
3167 elt = pointer_map_contains (lab_rtx_for_bb, bb);
3169 if (stmt || elt)
3171 last = get_last_insn ();
3173 if (stmt)
3175 expand_gimple_stmt (stmt);
3176 gsi_next (&gsi);
3179 if (elt)
3180 emit_label ((rtx) *elt);
3182 /* Java emits line number notes in the top of labels.
3183 ??? Make this go away once line number notes are obsoleted. */
3184 BB_HEAD (bb) = NEXT_INSN (last);
3185 if (NOTE_P (BB_HEAD (bb)))
3186 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
3187 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
3189 maybe_dump_rtl_for_gimple_stmt (stmt, last);
3191 else
3192 note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
3194 NOTE_BASIC_BLOCK (note) = bb;
3196 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3198 basic_block new_bb;
3200 stmt = gsi_stmt (gsi);
3202 /* If this statement is a non-debug one, and we generate debug
3203 insns, then this one might be the last real use of a TERed
3204 SSA_NAME, but where there are still some debug uses further
3205 down. Expanding the current SSA name in such further debug
3206 uses by their RHS might lead to wrong debug info, as coalescing
3207 might make the operands of such RHS be placed into the same
3208 pseudo as something else. Like so:
3209 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
3210 use(a_1);
3211 a_2 = ...
3212 #DEBUG ... => a_1
3213 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
3214 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
3215 the write to a_2 would actually have clobbered the place which
3216 formerly held a_0.
3218 So, instead of that, we recognize the situation, and generate
3219 debug temporaries at the last real use of TERed SSA names:
3220 a_1 = a_0 + 1;
3221 #DEBUG #D1 => a_1
3222 use(a_1);
3223 a_2 = ...
3224 #DEBUG ... => #D1
3226 if (MAY_HAVE_DEBUG_INSNS
3227 && SA.values
3228 && !is_gimple_debug (stmt))
3230 ssa_op_iter iter;
3231 tree op;
3232 gimple def;
3234 location_t sloc = get_curr_insn_source_location ();
3235 tree sblock = get_curr_insn_block ();
3237 /* Look for SSA names that have their last use here (TERed
3238 names always have only one real use). */
3239 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3240 if ((def = get_gimple_for_ssa_name (op)))
3242 imm_use_iterator imm_iter;
3243 use_operand_p use_p;
3244 bool have_debug_uses = false;
3246 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
3248 if (gimple_debug_bind_p (USE_STMT (use_p)))
3250 have_debug_uses = true;
3251 break;
3255 if (have_debug_uses)
3257 /* OP is a TERed SSA name, with DEF it's defining
3258 statement, and where OP is used in further debug
3259 instructions. Generate a debug temporary, and
3260 replace all uses of OP in debug insns with that
3261 temporary. */
3262 gimple debugstmt;
3263 tree value = gimple_assign_rhs_to_tree (def);
3264 tree vexpr = make_node (DEBUG_EXPR_DECL);
3265 rtx val;
3266 enum machine_mode mode;
3268 set_curr_insn_source_location (gimple_location (def));
3269 set_curr_insn_block (gimple_block (def));
3271 DECL_ARTIFICIAL (vexpr) = 1;
3272 TREE_TYPE (vexpr) = TREE_TYPE (value);
3273 if (DECL_P (value))
3274 mode = DECL_MODE (value);
3275 else
3276 mode = TYPE_MODE (TREE_TYPE (value));
3277 DECL_MODE (vexpr) = mode;
3279 val = gen_rtx_VAR_LOCATION
3280 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3282 val = emit_debug_insn (val);
3284 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
3286 if (!gimple_debug_bind_p (debugstmt))
3287 continue;
3289 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
3290 SET_USE (use_p, vexpr);
3292 update_stmt (debugstmt);
3296 set_curr_insn_source_location (sloc);
3297 set_curr_insn_block (sblock);
3300 currently_expanding_gimple_stmt = stmt;
3302 /* Expand this statement, then evaluate the resulting RTL and
3303 fixup the CFG accordingly. */
3304 if (gimple_code (stmt) == GIMPLE_COND)
3306 new_bb = expand_gimple_cond (bb, stmt);
3307 if (new_bb)
3308 return new_bb;
3310 else if (gimple_debug_bind_p (stmt))
3312 location_t sloc = get_curr_insn_source_location ();
3313 tree sblock = get_curr_insn_block ();
3314 gimple_stmt_iterator nsi = gsi;
3316 for (;;)
3318 tree var = gimple_debug_bind_get_var (stmt);
3319 tree value;
3320 rtx val;
3321 enum machine_mode mode;
3323 if (gimple_debug_bind_has_value_p (stmt))
3324 value = gimple_debug_bind_get_value (stmt);
3325 else
3326 value = NULL_TREE;
3328 last = get_last_insn ();
3330 set_curr_insn_source_location (gimple_location (stmt));
3331 set_curr_insn_block (gimple_block (stmt));
3333 if (DECL_P (var))
3334 mode = DECL_MODE (var);
3335 else
3336 mode = TYPE_MODE (TREE_TYPE (var));
3338 val = gen_rtx_VAR_LOCATION
3339 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3341 val = emit_debug_insn (val);
3343 if (dump_file && (dump_flags & TDF_DETAILS))
3345 /* We can't dump the insn with a TREE where an RTX
3346 is expected. */
3347 INSN_VAR_LOCATION_LOC (val) = const0_rtx;
3348 maybe_dump_rtl_for_gimple_stmt (stmt, last);
3349 INSN_VAR_LOCATION_LOC (val) = (rtx)value;
3352 /* In order not to generate too many debug temporaries,
3353 we delink all uses of debug statements we already expanded.
3354 Therefore debug statements between definition and real
3355 use of TERed SSA names will continue to use the SSA name,
3356 and not be replaced with debug temps. */
3357 delink_stmt_imm_use (stmt);
3359 gsi = nsi;
3360 gsi_next (&nsi);
3361 if (gsi_end_p (nsi))
3362 break;
3363 stmt = gsi_stmt (nsi);
3364 if (!gimple_debug_bind_p (stmt))
3365 break;
3368 set_curr_insn_source_location (sloc);
3369 set_curr_insn_block (sblock);
3371 else
3373 if (is_gimple_call (stmt) && gimple_call_tail_p (stmt))
3375 bool can_fallthru;
3376 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
3377 if (new_bb)
3379 if (can_fallthru)
3380 bb = new_bb;
3381 else
3382 return new_bb;
3385 else
3387 def_operand_p def_p;
3388 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
3390 if (def_p != NULL)
3392 /* Ignore this stmt if it is in the list of
3393 replaceable expressions. */
3394 if (SA.values
3395 && bitmap_bit_p (SA.values,
3396 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
3397 continue;
3399 last = expand_gimple_stmt (stmt);
3400 maybe_dump_rtl_for_gimple_stmt (stmt, last);
3405 currently_expanding_gimple_stmt = NULL;
3407 /* Expand implicit goto and convert goto_locus. */
3408 FOR_EACH_EDGE (e, ei, bb->succs)
3410 if (e->goto_locus && e->goto_block)
3412 set_curr_insn_source_location (e->goto_locus);
3413 set_curr_insn_block (e->goto_block);
3414 e->goto_locus = curr_insn_locator ();
3416 e->goto_block = NULL;
3417 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
3419 emit_jump (label_rtx_for_bb (e->dest));
3420 e->flags &= ~EDGE_FALLTHRU;
3424 /* Expanded RTL can create a jump in the last instruction of block.
3425 This later might be assumed to be a jump to successor and break edge insertion.
3426 We need to insert dummy move to prevent this. PR41440. */
3427 if (single_succ_p (bb)
3428 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
3429 && (last = get_last_insn ())
3430 && JUMP_P (last))
3432 rtx dummy = gen_reg_rtx (SImode);
3433 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
3436 do_pending_stack_adjust ();
3438 /* Find the block tail. The last insn in the block is the insn
3439 before a barrier and/or table jump insn. */
3440 last = get_last_insn ();
3441 if (BARRIER_P (last))
3442 last = PREV_INSN (last);
3443 if (JUMP_TABLE_DATA_P (last))
3444 last = PREV_INSN (PREV_INSN (last));
3445 BB_END (bb) = last;
3447 update_bb_for_insn (bb);
3449 return bb;
3453 /* Create a basic block for initialization code. */
3455 static basic_block
3456 construct_init_block (void)
3458 basic_block init_block, first_block;
3459 edge e = NULL;
3460 int flags;
3462 /* Multiple entry points not supported yet. */
3463 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
3464 init_rtl_bb_info (ENTRY_BLOCK_PTR);
3465 init_rtl_bb_info (EXIT_BLOCK_PTR);
3466 ENTRY_BLOCK_PTR->flags |= BB_RTL;
3467 EXIT_BLOCK_PTR->flags |= BB_RTL;
3469 e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
3471 /* When entry edge points to first basic block, we don't need jump,
3472 otherwise we have to jump into proper target. */
3473 if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
3475 tree label = gimple_block_label (e->dest);
3477 emit_jump (label_rtx (label));
3478 flags = 0;
3480 else
3481 flags = EDGE_FALLTHRU;
3483 init_block = create_basic_block (NEXT_INSN (get_insns ()),
3484 get_last_insn (),
3485 ENTRY_BLOCK_PTR);
3486 init_block->frequency = ENTRY_BLOCK_PTR->frequency;
3487 init_block->count = ENTRY_BLOCK_PTR->count;
3488 if (e)
3490 first_block = e->dest;
3491 redirect_edge_succ (e, init_block);
3492 e = make_edge (init_block, first_block, flags);
3494 else
3495 e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
3496 e->probability = REG_BR_PROB_BASE;
3497 e->count = ENTRY_BLOCK_PTR->count;
3499 update_bb_for_insn (init_block);
3500 return init_block;
3503 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
3504 found in the block tree. */
3506 static void
3507 set_block_levels (tree block, int level)
3509 while (block)
3511 BLOCK_NUMBER (block) = level;
3512 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
3513 block = BLOCK_CHAIN (block);
3517 /* Create a block containing landing pads and similar stuff. */
3519 static void
3520 construct_exit_block (void)
3522 rtx head = get_last_insn ();
3523 rtx end;
3524 basic_block exit_block;
3525 edge e, e2;
3526 unsigned ix;
3527 edge_iterator ei;
3528 rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb);
3530 rtl_profile_for_bb (EXIT_BLOCK_PTR);
3532 /* Make sure the locus is set to the end of the function, so that
3533 epilogue line numbers and warnings are set properly. */
3534 if (cfun->function_end_locus != UNKNOWN_LOCATION)
3535 input_location = cfun->function_end_locus;
3537 /* The following insns belong to the top scope. */
3538 set_curr_insn_block (DECL_INITIAL (current_function_decl));
3540 /* Generate rtl for function exit. */
3541 expand_function_end ();
3543 end = get_last_insn ();
3544 if (head == end)
3545 return;
3546 /* While emitting the function end we could move end of the last basic block.
3548 BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end;
3549 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
3550 head = NEXT_INSN (head);
3551 exit_block = create_basic_block (NEXT_INSN (head), end,
3552 EXIT_BLOCK_PTR->prev_bb);
3553 exit_block->frequency = EXIT_BLOCK_PTR->frequency;
3554 exit_block->count = EXIT_BLOCK_PTR->count;
3556 ix = 0;
3557 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
3559 e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
3560 if (!(e->flags & EDGE_ABNORMAL))
3561 redirect_edge_succ (e, exit_block);
3562 else
3563 ix++;
3566 e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
3567 e->probability = REG_BR_PROB_BASE;
3568 e->count = EXIT_BLOCK_PTR->count;
3569 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
3570 if (e2 != e)
3572 e->count -= e2->count;
3573 exit_block->count -= e2->count;
3574 exit_block->frequency -= EDGE_FREQUENCY (e2);
3576 if (e->count < 0)
3577 e->count = 0;
3578 if (exit_block->count < 0)
3579 exit_block->count = 0;
3580 if (exit_block->frequency < 0)
3581 exit_block->frequency = 0;
3582 update_bb_for_insn (exit_block);
3585 /* Helper function for discover_nonconstant_array_refs.
3586 Look for ARRAY_REF nodes with non-constant indexes and mark them
3587 addressable. */
3589 static tree
3590 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
3591 void *data ATTRIBUTE_UNUSED)
3593 tree t = *tp;
3595 if (IS_TYPE_OR_DECL_P (t))
3596 *walk_subtrees = 0;
3597 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3599 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3600 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
3601 && (!TREE_OPERAND (t, 2)
3602 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
3603 || (TREE_CODE (t) == COMPONENT_REF
3604 && (!TREE_OPERAND (t,2)
3605 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
3606 || TREE_CODE (t) == BIT_FIELD_REF
3607 || TREE_CODE (t) == REALPART_EXPR
3608 || TREE_CODE (t) == IMAGPART_EXPR
3609 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3610 || CONVERT_EXPR_P (t))
3611 t = TREE_OPERAND (t, 0);
3613 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3615 t = get_base_address (t);
3616 if (t && DECL_P (t)
3617 && DECL_MODE (t) != BLKmode)
3618 TREE_ADDRESSABLE (t) = 1;
3621 *walk_subtrees = 0;
3624 return NULL_TREE;
3627 /* RTL expansion is not able to compile array references with variable
3628 offsets for arrays stored in single register. Discover such
3629 expressions and mark variables as addressable to avoid this
3630 scenario. */
3632 static void
3633 discover_nonconstant_array_refs (void)
3635 basic_block bb;
3636 gimple_stmt_iterator gsi;
3638 FOR_EACH_BB (bb)
3639 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3641 gimple stmt = gsi_stmt (gsi);
3642 if (!is_gimple_debug (stmt))
3643 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
3647 /* This function sets crtl->args.internal_arg_pointer to a virtual
3648 register if DRAP is needed. Local register allocator will replace
3649 virtual_incoming_args_rtx with the virtual register. */
3651 static void
3652 expand_stack_alignment (void)
3654 rtx drap_rtx;
3655 unsigned int preferred_stack_boundary;
3657 if (! SUPPORTS_STACK_ALIGNMENT)
3658 return;
3660 if (cfun->calls_alloca
3661 || cfun->has_nonlocal_label
3662 || crtl->has_nonlocal_goto)
3663 crtl->need_drap = true;
3665 /* Call update_stack_boundary here again to update incoming stack
3666 boundary. It may set incoming stack alignment to a different
3667 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
3668 use the minimum incoming stack alignment to check if it is OK
3669 to perform sibcall optimization since sibcall optimization will
3670 only align the outgoing stack to incoming stack boundary. */
3671 if (targetm.calls.update_stack_boundary)
3672 targetm.calls.update_stack_boundary ();
3674 /* The incoming stack frame has to be aligned at least at
3675 parm_stack_boundary. */
3676 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
3678 /* Update crtl->stack_alignment_estimated and use it later to align
3679 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
3680 exceptions since callgraph doesn't collect incoming stack alignment
3681 in this case. */
3682 if (flag_non_call_exceptions
3683 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
3684 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3685 else
3686 preferred_stack_boundary = crtl->preferred_stack_boundary;
3687 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
3688 crtl->stack_alignment_estimated = preferred_stack_boundary;
3689 if (preferred_stack_boundary > crtl->stack_alignment_needed)
3690 crtl->stack_alignment_needed = preferred_stack_boundary;
3692 gcc_assert (crtl->stack_alignment_needed
3693 <= crtl->stack_alignment_estimated);
3695 crtl->stack_realign_needed
3696 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
3697 crtl->stack_realign_tried = crtl->stack_realign_needed;
3699 crtl->stack_realign_processed = true;
3701 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
3702 alignment. */
3703 gcc_assert (targetm.calls.get_drap_rtx != NULL);
3704 drap_rtx = targetm.calls.get_drap_rtx ();
3706 /* stack_realign_drap and drap_rtx must match. */
3707 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
3709 /* Do nothing if NULL is returned, which means DRAP is not needed. */
3710 if (NULL != drap_rtx)
3712 crtl->args.internal_arg_pointer = drap_rtx;
3714 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
3715 needed. */
3716 fixup_tail_calls ();
3720 /* Translate the intermediate representation contained in the CFG
3721 from GIMPLE trees to RTL.
3723 We do conversion per basic block and preserve/update the tree CFG.
3724 This implies we have to do some magic as the CFG can simultaneously
3725 consist of basic blocks containing RTL and GIMPLE trees. This can
3726 confuse the CFG hooks, so be careful to not manipulate CFG during
3727 the expansion. */
3729 static unsigned int
3730 gimple_expand_cfg (void)
3732 basic_block bb, init_block;
3733 sbitmap blocks;
3734 edge_iterator ei;
3735 edge e;
3736 unsigned i;
3738 rewrite_out_of_ssa (&SA);
3739 SA.partition_to_pseudo = (rtx *)xcalloc (SA.map->num_partitions,
3740 sizeof (rtx));
3742 /* Some backends want to know that we are expanding to RTL. */
3743 currently_expanding_to_rtl = 1;
3745 rtl_profile_for_bb (ENTRY_BLOCK_PTR);
3747 insn_locators_alloc ();
3748 if (!DECL_IS_BUILTIN (current_function_decl))
3750 /* Eventually, all FEs should explicitly set function_start_locus. */
3751 if (cfun->function_start_locus == UNKNOWN_LOCATION)
3752 set_curr_insn_source_location
3753 (DECL_SOURCE_LOCATION (current_function_decl));
3754 else
3755 set_curr_insn_source_location (cfun->function_start_locus);
3757 set_curr_insn_block (DECL_INITIAL (current_function_decl));
3758 prologue_locator = curr_insn_locator ();
3760 /* Make sure first insn is a note even if we don't want linenums.
3761 This makes sure the first insn will never be deleted.
3762 Also, final expects a note to appear there. */
3763 emit_note (NOTE_INSN_DELETED);
3765 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
3766 discover_nonconstant_array_refs ();
3768 targetm.expand_to_rtl_hook ();
3769 crtl->stack_alignment_needed = STACK_BOUNDARY;
3770 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
3771 crtl->stack_alignment_estimated = 0;
3772 crtl->preferred_stack_boundary = STACK_BOUNDARY;
3773 cfun->cfg->max_jumptable_ents = 0;
3776 /* Expand the variables recorded during gimple lowering. */
3777 expand_used_vars ();
3779 /* Honor stack protection warnings. */
3780 if (warn_stack_protect)
3782 if (cfun->calls_alloca)
3783 warning (OPT_Wstack_protector,
3784 "stack protector not protecting local variables: "
3785 "variable length buffer");
3786 if (has_short_buffer && !crtl->stack_protect_guard)
3787 warning (OPT_Wstack_protector,
3788 "stack protector not protecting function: "
3789 "all local arrays are less than %d bytes long",
3790 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
3793 /* Set up parameters and prepare for return, for the function. */
3794 expand_function_start (current_function_decl);
3796 /* Now that we also have the parameter RTXs, copy them over to our
3797 partitions. */
3798 for (i = 0; i < SA.map->num_partitions; i++)
3800 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
3802 if (TREE_CODE (var) != VAR_DECL
3803 && !SA.partition_to_pseudo[i])
3804 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
3805 gcc_assert (SA.partition_to_pseudo[i]);
3807 /* If this decl was marked as living in multiple places, reset
3808 this now to NULL. */
3809 if (DECL_RTL_IF_SET (var) == pc_rtx)
3810 SET_DECL_RTL (var, NULL);
3812 /* Some RTL parts really want to look at DECL_RTL(x) when x
3813 was a decl marked in REG_ATTR or MEM_ATTR. We could use
3814 SET_DECL_RTL here making this available, but that would mean
3815 to select one of the potentially many RTLs for one DECL. Instead
3816 of doing that we simply reset the MEM_EXPR of the RTL in question,
3817 then nobody can get at it and hence nobody can call DECL_RTL on it. */
3818 if (!DECL_RTL_SET_P (var))
3820 if (MEM_P (SA.partition_to_pseudo[i]))
3821 set_mem_expr (SA.partition_to_pseudo[i], NULL);
3825 /* If this function is `main', emit a call to `__main'
3826 to run global initializers, etc. */
3827 if (DECL_NAME (current_function_decl)
3828 && MAIN_NAME_P (DECL_NAME (current_function_decl))
3829 && DECL_FILE_SCOPE_P (current_function_decl))
3830 expand_main_function ();
3832 /* Initialize the stack_protect_guard field. This must happen after the
3833 call to __main (if any) so that the external decl is initialized. */
3834 if (crtl->stack_protect_guard)
3835 stack_protect_prologue ();
3837 expand_phi_nodes (&SA);
3839 /* Register rtl specific functions for cfg. */
3840 rtl_register_cfg_hooks ();
3842 init_block = construct_init_block ();
3844 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
3845 remaining edges later. */
3846 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3847 e->flags &= ~EDGE_EXECUTABLE;
3849 lab_rtx_for_bb = pointer_map_create ();
3850 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
3851 bb = expand_gimple_basic_block (bb);
3853 if (MAY_HAVE_DEBUG_INSNS)
3854 expand_debug_locations ();
3856 execute_free_datastructures ();
3857 finish_out_of_ssa (&SA);
3859 /* We are no longer in SSA form. */
3860 cfun->gimple_df->in_ssa_p = false;
3862 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
3863 conservatively to true until they are all profile aware. */
3864 pointer_map_destroy (lab_rtx_for_bb);
3865 free_histograms ();
3867 construct_exit_block ();
3868 set_curr_insn_block (DECL_INITIAL (current_function_decl));
3869 insn_locators_finalize ();
3871 /* Zap the tree EH table. */
3872 set_eh_throw_stmt_table (cfun, NULL);
3874 rebuild_jump_labels (get_insns ());
3876 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
3878 edge e;
3879 edge_iterator ei;
3880 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3882 if (e->insns.r)
3883 commit_one_edge_insertion (e);
3884 else
3885 ei_next (&ei);
3889 /* We're done expanding trees to RTL. */
3890 currently_expanding_to_rtl = 0;
3892 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR, next_bb)
3894 edge e;
3895 edge_iterator ei;
3896 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3898 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
3899 e->flags &= ~EDGE_EXECUTABLE;
3901 /* At the moment not all abnormal edges match the RTL
3902 representation. It is safe to remove them here as
3903 find_many_sub_basic_blocks will rediscover them.
3904 In the future we should get this fixed properly. */
3905 if ((e->flags & EDGE_ABNORMAL)
3906 && !(e->flags & EDGE_SIBCALL))
3907 remove_edge (e);
3908 else
3909 ei_next (&ei);
3913 blocks = sbitmap_alloc (last_basic_block);
3914 sbitmap_ones (blocks);
3915 find_many_sub_basic_blocks (blocks);
3916 sbitmap_free (blocks);
3917 purge_all_dead_edges ();
3919 compact_blocks ();
3921 expand_stack_alignment ();
3923 #ifdef ENABLE_CHECKING
3924 verify_flow_info ();
3925 #endif
3927 /* There's no need to defer outputting this function any more; we
3928 know we want to output it. */
3929 DECL_DEFER_OUTPUT (current_function_decl) = 0;
3931 /* Now that we're done expanding trees to RTL, we shouldn't have any
3932 more CONCATs anywhere. */
3933 generating_concat_p = 0;
3935 if (dump_file)
3937 fprintf (dump_file,
3938 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
3939 /* And the pass manager will dump RTL for us. */
3942 /* If we're emitting a nested function, make sure its parent gets
3943 emitted as well. Doing otherwise confuses debug info. */
3945 tree parent;
3946 for (parent = DECL_CONTEXT (current_function_decl);
3947 parent != NULL_TREE;
3948 parent = get_containing_scope (parent))
3949 if (TREE_CODE (parent) == FUNCTION_DECL)
3950 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
3953 /* We are now committed to emitting code for this function. Do any
3954 preparation, such as emitting abstract debug info for the inline
3955 before it gets mangled by optimization. */
3956 if (cgraph_function_possibly_inlined_p (current_function_decl))
3957 (*debug_hooks->outlining_inline_function) (current_function_decl);
3959 TREE_ASM_WRITTEN (current_function_decl) = 1;
3961 /* After expanding, the return labels are no longer needed. */
3962 return_label = NULL;
3963 naked_return_label = NULL;
3964 /* Tag the blocks with a depth number so that change_scope can find
3965 the common parent easily. */
3966 set_block_levels (DECL_INITIAL (cfun->decl), 0);
3967 default_rtl_profile ();
3968 return 0;
3971 struct rtl_opt_pass pass_expand =
3974 RTL_PASS,
3975 "expand", /* name */
3976 NULL, /* gate */
3977 gimple_expand_cfg, /* execute */
3978 NULL, /* sub */
3979 NULL, /* next */
3980 0, /* static_pass_number */
3981 TV_EXPAND, /* tv_id */
3982 PROP_ssa | PROP_gimple_leh | PROP_cfg
3983 | PROP_gimple_lcx, /* properties_required */
3984 PROP_rtl, /* properties_provided */
3985 PROP_ssa | PROP_trees, /* properties_destroyed */
3986 TODO_verify_ssa | TODO_verify_flow
3987 | TODO_verify_stmts, /* todo_flags_start */
3988 TODO_dump_func
3989 | TODO_ggc_collect /* todo_flags_finish */