1 /* A pass for lowering trees to RTL.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
27 #include "basic-block.h"
30 #include "langhooks.h"
31 #include "tree-flow.h"
33 #include "tree-dump.h"
34 #include "tree-pass.h"
37 #include "diagnostic.h"
41 #include "tree-inline.h"
42 #include "value-prof.h"
45 /* Verify that there is exactly single jump instruction since last and attach
46 REG_BR_PROB note specifying probability.
47 ??? We really ought to pass the probability down to RTL expanders and let it
48 re-distribute it when the conditional expands into multiple conditionals.
49 This is however difficult to do. */
51 add_reg_br_prob_note (rtx last, int probability)
53 if (profile_status == PROFILE_ABSENT)
55 for (last = NEXT_INSN (last); last && NEXT_INSN (last); last = NEXT_INSN (last))
58 /* It is common to emit condjump-around-jump sequence when we don't know
59 how to reverse the conditional. Special case this. */
60 if (!any_condjump_p (last)
61 || !JUMP_P (NEXT_INSN (last))
62 || !simplejump_p (NEXT_INSN (last))
63 || !NEXT_INSN (NEXT_INSN (last))
64 || !BARRIER_P (NEXT_INSN (NEXT_INSN (last)))
65 || !NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))
66 || !LABEL_P (NEXT_INSN (NEXT_INSN (NEXT_INSN (last))))
67 || NEXT_INSN (NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))))
69 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
71 = gen_rtx_EXPR_LIST (REG_BR_PROB,
72 GEN_INT (REG_BR_PROB_BASE - probability),
76 if (!last || !JUMP_P (last) || !any_condjump_p (last))
78 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
80 = gen_rtx_EXPR_LIST (REG_BR_PROB,
81 GEN_INT (probability), REG_NOTES (last));
85 fprintf (dump_file, "Failed to add probability note\n");
89 #ifndef LOCAL_ALIGNMENT
90 #define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
93 #ifndef STACK_ALIGNMENT_NEEDED
94 #define STACK_ALIGNMENT_NEEDED 1
98 /* This structure holds data relevant to one variable that will be
99 placed in a stack slot. */
105 /* The offset of the variable. During partitioning, this is the
106 offset relative to the partition. After partitioning, this
107 is relative to the stack frame. */
108 HOST_WIDE_INT offset;
110 /* Initially, the size of the variable. Later, the size of the partition,
111 if this variable becomes it's partition's representative. */
114 /* The *byte* alignment required for this variable. Or as, with the
115 size, the alignment for this partition. */
118 /* The partition representative. */
119 size_t representative;
121 /* The next stack variable in the partition, or EOC. */
125 #define EOC ((size_t)-1)
127 /* We have an array of such objects while deciding allocation. */
128 static struct stack_var *stack_vars;
129 static size_t stack_vars_alloc;
130 static size_t stack_vars_num;
132 /* An array of indicies such that stack_vars[stack_vars_sorted[i]].size
133 is non-decreasing. */
134 static size_t *stack_vars_sorted;
136 /* We have an interference graph between such objects. This graph
137 is lower triangular. */
138 static bool *stack_vars_conflict;
139 static size_t stack_vars_conflict_alloc;
141 /* The phase of the stack frame. This is the known misalignment of
142 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
143 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
144 static int frame_phase;
146 /* Used during expand_used_vars to remember if we saw any decls for
147 which we'd like to enable stack smashing protection. */
148 static bool has_protected_decls;
150 /* Used during expand_used_vars. Remember if we say a character buffer
151 smaller than our cutoff threshold. Used for -Wstack-protector. */
152 static bool has_short_buffer;
154 /* Discover the byte alignment to use for DECL. Ignore alignment
155 we can't do with expected alignment of the stack boundary. */
158 get_decl_align_unit (tree decl)
162 align = DECL_ALIGN (decl);
163 align = LOCAL_ALIGNMENT (TREE_TYPE (decl), align);
164 if (align > PREFERRED_STACK_BOUNDARY)
165 align = PREFERRED_STACK_BOUNDARY;
166 if (cfun->stack_alignment_needed < align)
167 cfun->stack_alignment_needed = align;
169 return align / BITS_PER_UNIT;
172 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
173 Return the frame offset. */
176 alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align)
178 HOST_WIDE_INT offset, new_frame_offset;
180 new_frame_offset = frame_offset;
181 if (FRAME_GROWS_DOWNWARD)
183 new_frame_offset -= size + frame_phase;
184 new_frame_offset &= -align;
185 new_frame_offset += frame_phase;
186 offset = new_frame_offset;
190 new_frame_offset -= frame_phase;
191 new_frame_offset += align - 1;
192 new_frame_offset &= -align;
193 new_frame_offset += frame_phase;
194 offset = new_frame_offset;
195 new_frame_offset += size;
197 frame_offset = new_frame_offset;
199 if (frame_offset_overflow (frame_offset, cfun->decl))
200 frame_offset = offset = 0;
205 /* Accumulate DECL into STACK_VARS. */
208 add_stack_var (tree decl)
210 if (stack_vars_num >= stack_vars_alloc)
212 if (stack_vars_alloc)
213 stack_vars_alloc = stack_vars_alloc * 3 / 2;
215 stack_vars_alloc = 32;
217 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
219 stack_vars[stack_vars_num].decl = decl;
220 stack_vars[stack_vars_num].offset = 0;
221 stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
222 stack_vars[stack_vars_num].alignb = get_decl_align_unit (decl);
224 /* All variables are initially in their own partition. */
225 stack_vars[stack_vars_num].representative = stack_vars_num;
226 stack_vars[stack_vars_num].next = EOC;
228 /* Ensure that this decl doesn't get put onto the list twice. */
229 SET_DECL_RTL (decl, pc_rtx);
234 /* Compute the linear index of a lower-triangular coordinate (I, J). */
237 triangular_index (size_t i, size_t j)
244 return (i * (i + 1)) / 2 + j;
247 /* Ensure that STACK_VARS_CONFLICT is large enough for N objects. */
250 resize_stack_vars_conflict (size_t n)
252 size_t size = triangular_index (n-1, n-1) + 1;
254 if (size <= stack_vars_conflict_alloc)
257 stack_vars_conflict = XRESIZEVEC (bool, stack_vars_conflict, size);
258 memset (stack_vars_conflict + stack_vars_conflict_alloc, 0,
259 (size - stack_vars_conflict_alloc) * sizeof (bool));
260 stack_vars_conflict_alloc = size;
263 /* Make the decls associated with luid's X and Y conflict. */
266 add_stack_var_conflict (size_t x, size_t y)
268 size_t index = triangular_index (x, y);
269 gcc_assert (index < stack_vars_conflict_alloc);
270 stack_vars_conflict[index] = true;
273 /* Check whether the decls associated with luid's X and Y conflict. */
276 stack_var_conflict_p (size_t x, size_t y)
278 size_t index = triangular_index (x, y);
279 gcc_assert (index < stack_vars_conflict_alloc);
280 return stack_vars_conflict[index];
283 /* Returns true if TYPE is or contains a union type. */
286 aggregate_contains_union_type (tree type)
290 if (TREE_CODE (type) == UNION_TYPE
291 || TREE_CODE (type) == QUAL_UNION_TYPE)
293 if (TREE_CODE (type) == ARRAY_TYPE)
294 return aggregate_contains_union_type (TREE_TYPE (type));
295 if (TREE_CODE (type) != RECORD_TYPE)
298 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
299 if (TREE_CODE (field) == FIELD_DECL)
300 if (aggregate_contains_union_type (TREE_TYPE (field)))
306 /* A subroutine of expand_used_vars. If two variables X and Y have alias
307 sets that do not conflict, then do add a conflict for these variables
308 in the interference graph. We also need to make sure to add conflicts
309 for union containing structures. Else RTL alias analysis comes along
310 and due to type based aliasing rules decides that for two overlapping
311 union temporaries { short s; int i; } accesses to the same mem through
312 different types may not alias and happily reorders stores across
313 life-time boundaries of the temporaries (See PR25654).
314 We also have to mind MEM_IN_STRUCT_P and MEM_SCALAR_P. */
317 add_alias_set_conflicts (void)
319 size_t i, j, n = stack_vars_num;
321 for (i = 0; i < n; ++i)
323 tree type_i = TREE_TYPE (stack_vars[i].decl);
324 bool aggr_i = AGGREGATE_TYPE_P (type_i);
327 contains_union = aggregate_contains_union_type (type_i);
328 for (j = 0; j < i; ++j)
330 tree type_j = TREE_TYPE (stack_vars[j].decl);
331 bool aggr_j = AGGREGATE_TYPE_P (type_j);
333 /* Either the objects conflict by means of type based
334 aliasing rules, or we need to add a conflict. */
335 || !objects_must_conflict_p (type_i, type_j)
336 /* In case the types do not conflict ensure that access
337 to elements will conflict. In case of unions we have
338 to be careful as type based aliasing rules may say
339 access to the same memory does not conflict. So play
340 safe and add a conflict in this case. */
342 add_stack_var_conflict (i, j);
347 /* A subroutine of partition_stack_vars. A comparison function for qsort,
348 sorting an array of indicies by the size of the object. */
351 stack_var_size_cmp (const void *a, const void *b)
353 HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size;
354 HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size;
355 unsigned int uida = DECL_UID (stack_vars[*(const size_t *)a].decl);
356 unsigned int uidb = DECL_UID (stack_vars[*(const size_t *)b].decl);
362 /* For stack variables of the same size use the uid of the decl
363 to make the sort stable. */
371 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
372 partitioning algorithm. Partitions A and B are known to be non-conflicting.
373 Merge them into a single partition A.
375 At the same time, add OFFSET to all variables in partition B. At the end
376 of the partitioning process we've have a nice block easy to lay out within
380 union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset)
384 /* Update each element of partition B with the given offset,
385 and merge them into partition A. */
386 for (last = i = b; i != EOC; last = i, i = stack_vars[i].next)
388 stack_vars[i].offset += offset;
389 stack_vars[i].representative = a;
391 stack_vars[last].next = stack_vars[a].next;
392 stack_vars[a].next = b;
394 /* Update the required alignment of partition A to account for B. */
395 if (stack_vars[a].alignb < stack_vars[b].alignb)
396 stack_vars[a].alignb = stack_vars[b].alignb;
398 /* Update the interference graph and merge the conflicts. */
399 for (last = stack_vars_num, i = 0; i < last; ++i)
400 if (stack_var_conflict_p (b, i))
401 add_stack_var_conflict (a, i);
404 /* A subroutine of expand_used_vars. Binpack the variables into
405 partitions constrained by the interference graph. The overall
406 algorithm used is as follows:
408 Sort the objects by size.
413 Look for the largest non-conflicting object B with size <= S.
423 partition_stack_vars (void)
425 size_t si, sj, n = stack_vars_num;
427 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
428 for (si = 0; si < n; ++si)
429 stack_vars_sorted[si] = si;
434 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp);
436 /* Special case: detect when all variables conflict, and thus we can't
437 do anything during the partitioning loop. It isn't uncommon (with
438 C code at least) to declare all variables at the top of the function,
439 and if we're not inlining, then all variables will be in the same scope.
440 Take advantage of very fast libc routines for this scan. */
441 gcc_assert (sizeof(bool) == sizeof(char));
442 if (memchr (stack_vars_conflict, false, stack_vars_conflict_alloc) == NULL)
445 for (si = 0; si < n; ++si)
447 size_t i = stack_vars_sorted[si];
448 HOST_WIDE_INT isize = stack_vars[i].size;
449 HOST_WIDE_INT offset = 0;
451 for (sj = si; sj-- > 0; )
453 size_t j = stack_vars_sorted[sj];
454 HOST_WIDE_INT jsize = stack_vars[j].size;
455 unsigned int jalign = stack_vars[j].alignb;
457 /* Ignore objects that aren't partition representatives. */
458 if (stack_vars[j].representative != j)
461 /* Ignore objects too large for the remaining space. */
465 /* Ignore conflicting objects. */
466 if (stack_var_conflict_p (i, j))
469 /* Refine the remaining space check to include alignment. */
470 if (offset & (jalign - 1))
472 HOST_WIDE_INT toff = offset;
474 toff &= -(HOST_WIDE_INT)jalign;
475 if (isize - (toff - offset) < jsize)
478 isize -= toff - offset;
482 /* UNION the objects, placing J at OFFSET. */
483 union_stack_vars (i, j, offset);
492 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
495 dump_stack_var_partition (void)
497 size_t si, i, j, n = stack_vars_num;
499 for (si = 0; si < n; ++si)
501 i = stack_vars_sorted[si];
503 /* Skip variables that aren't partition representatives, for now. */
504 if (stack_vars[i].representative != i)
507 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
508 " align %u\n", (unsigned long) i, stack_vars[i].size,
509 stack_vars[i].alignb);
511 for (j = i; j != EOC; j = stack_vars[j].next)
513 fputc ('\t', dump_file);
514 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
515 fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n",
516 stack_vars[j].offset);
521 /* Assign rtl to DECL at frame offset OFFSET. */
524 expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset)
529 /* If this fails, we've overflowed the stack frame. Error nicely? */
530 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
532 x = plus_constant (virtual_stack_vars_rtx, offset);
533 x = gen_rtx_MEM (DECL_MODE (decl), x);
535 /* Set alignment we actually gave this decl. */
536 offset -= frame_phase;
537 align = offset & -offset;
538 align *= BITS_PER_UNIT;
539 if (align > STACK_BOUNDARY || align == 0)
540 align = STACK_BOUNDARY;
541 DECL_ALIGN (decl) = align;
542 DECL_USER_ALIGN (decl) = 0;
544 set_mem_attributes (x, decl, true);
545 SET_DECL_RTL (decl, x);
548 /* A subroutine of expand_used_vars. Give each partition representative
549 a unique location within the stack frame. Update each partition member
550 with that location. */
553 expand_stack_vars (bool (*pred) (tree))
555 size_t si, i, j, n = stack_vars_num;
557 for (si = 0; si < n; ++si)
559 HOST_WIDE_INT offset;
561 i = stack_vars_sorted[si];
563 /* Skip variables that aren't partition representatives, for now. */
564 if (stack_vars[i].representative != i)
567 /* Skip variables that have already had rtl assigned. See also
568 add_stack_var where we perpetrate this pc_rtx hack. */
569 if (DECL_RTL (stack_vars[i].decl) != pc_rtx)
572 /* Check the predicate to see whether this variable should be
573 allocated in this pass. */
574 if (pred && !pred (stack_vars[i].decl))
577 offset = alloc_stack_frame_space (stack_vars[i].size,
578 stack_vars[i].alignb);
580 /* Create rtl for each variable based on their location within the
582 for (j = i; j != EOC; j = stack_vars[j].next)
584 gcc_assert (stack_vars[j].offset <= stack_vars[i].size);
585 expand_one_stack_var_at (stack_vars[j].decl,
586 stack_vars[j].offset + offset);
591 /* Take into account all sizes of partitions and reset DECL_RTLs. */
593 account_stack_vars (void)
595 size_t si, j, i, n = stack_vars_num;
596 HOST_WIDE_INT size = 0;
598 for (si = 0; si < n; ++si)
600 i = stack_vars_sorted[si];
602 /* Skip variables that aren't partition representatives, for now. */
603 if (stack_vars[i].representative != i)
606 size += stack_vars[i].size;
607 for (j = i; j != EOC; j = stack_vars[j].next)
608 SET_DECL_RTL (stack_vars[j].decl, NULL);
613 /* A subroutine of expand_one_var. Called to immediately assign rtl
614 to a variable to be allocated in the stack frame. */
617 expand_one_stack_var (tree var)
619 HOST_WIDE_INT size, offset, align;
621 size = tree_low_cst (DECL_SIZE_UNIT (var), 1);
622 align = get_decl_align_unit (var);
623 offset = alloc_stack_frame_space (size, align);
625 expand_one_stack_var_at (var, offset);
628 /* A subroutine of expand_one_var. Called to assign rtl
629 to a TREE_STATIC VAR_DECL. */
632 expand_one_static_var (tree var)
634 /* In unit-at-a-time all the static variables are expanded at the end
635 of compilation process. */
636 if (flag_unit_at_a_time)
638 /* If this is an inlined copy of a static local variable,
639 look up the original. */
640 var = DECL_ORIGIN (var);
642 /* If we've already processed this variable because of that, do nothing. */
643 if (TREE_ASM_WRITTEN (var))
646 /* Otherwise, just emit the variable. */
647 rest_of_decl_compilation (var, 0, 0);
650 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
651 that will reside in a hard register. */
654 expand_one_hard_reg_var (tree var)
656 rest_of_decl_compilation (var, 0, 0);
659 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
660 that will reside in a pseudo register. */
663 expand_one_register_var (tree var)
665 tree type = TREE_TYPE (var);
666 int unsignedp = TYPE_UNSIGNED (type);
667 enum machine_mode reg_mode
668 = promote_mode (type, DECL_MODE (var), &unsignedp, 0);
669 rtx x = gen_reg_rtx (reg_mode);
671 SET_DECL_RTL (var, x);
673 /* Note if the object is a user variable. */
674 if (!DECL_ARTIFICIAL (var))
677 if (POINTER_TYPE_P (type))
678 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
681 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
682 has some associated error, e.g. its type is error-mark. We just need
683 to pick something that won't crash the rest of the compiler. */
686 expand_one_error_var (tree var)
688 enum machine_mode mode = DECL_MODE (var);
692 x = gen_rtx_MEM (BLKmode, const0_rtx);
693 else if (mode == VOIDmode)
696 x = gen_reg_rtx (mode);
698 SET_DECL_RTL (var, x);
701 /* A subroutine of expand_one_var. VAR is a variable that will be
702 allocated to the local stack frame. Return true if we wish to
703 add VAR to STACK_VARS so that it will be coalesced with other
704 variables. Return false to allocate VAR immediately.
706 This function is used to reduce the number of variables considered
707 for coalescing, which reduces the size of the quadratic problem. */
710 defer_stack_allocation (tree var, bool toplevel)
712 /* If stack protection is enabled, *all* stack variables must be deferred,
713 so that we can re-order the strings to the top of the frame. */
714 if (flag_stack_protect)
717 /* Variables in the outermost scope automatically conflict with
718 every other variable. The only reason to want to defer them
719 at all is that, after sorting, we can more efficiently pack
720 small variables in the stack frame. Continue to defer at -O2. */
721 if (toplevel && optimize < 2)
724 /* Without optimization, *most* variables are allocated from the
725 stack, which makes the quadratic problem large exactly when we
726 want compilation to proceed as quickly as possible. On the
727 other hand, we don't want the function's stack frame size to
728 get completely out of hand. So we avoid adding scalars and
729 "small" aggregates to the list at all. */
730 if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32)
736 /* A subroutine of expand_used_vars. Expand one variable according to
737 its flavor. Variables to be placed on the stack are not actually
738 expanded yet, merely recorded.
739 When REALLY_EXPAND is false, only add stack values to be allocated.
740 Return stack usage this variable is supposed to take.
744 expand_one_var (tree var, bool toplevel, bool really_expand)
746 if (TREE_CODE (var) != VAR_DECL)
748 else if (DECL_EXTERNAL (var))
750 else if (DECL_HAS_VALUE_EXPR_P (var))
752 else if (TREE_STATIC (var))
755 expand_one_static_var (var);
757 else if (DECL_RTL_SET_P (var))
759 else if (TREE_TYPE (var) == error_mark_node)
762 expand_one_error_var (var);
764 else if (DECL_HARD_REGISTER (var))
767 expand_one_hard_reg_var (var);
769 else if (use_register_for_decl (var))
772 expand_one_register_var (var);
774 else if (defer_stack_allocation (var, toplevel))
779 expand_one_stack_var (var);
780 return tree_low_cst (DECL_SIZE_UNIT (var), 1);
785 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
786 expanding variables. Those variables that can be put into registers
787 are allocated pseudos; those that can't are put on the stack.
789 TOPLEVEL is true if this is the outermost BLOCK. */
792 expand_used_vars_for_block (tree block, bool toplevel)
794 size_t i, j, old_sv_num, this_sv_num, new_sv_num;
797 old_sv_num = toplevel ? 0 : stack_vars_num;
799 /* Expand all variables at this level. */
800 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
802 /* Force local static variables to be output when marked by
803 used attribute. For unit-at-a-time, cgraph code already takes
805 || (!flag_unit_at_a_time && TREE_STATIC (t)
806 && DECL_PRESERVE_P (t)))
807 expand_one_var (t, toplevel, true);
809 this_sv_num = stack_vars_num;
811 /* Expand all variables at containing levels. */
812 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
813 expand_used_vars_for_block (t, false);
815 /* Since we do not track exact variable lifetimes (which is not even
816 possible for variables whose address escapes), we mirror the block
817 tree in the interference graph. Here we cause all variables at this
818 level, and all sublevels, to conflict. Do make certain that a
819 variable conflicts with itself. */
820 if (old_sv_num < this_sv_num)
822 new_sv_num = stack_vars_num;
823 resize_stack_vars_conflict (new_sv_num);
825 for (i = old_sv_num; i < new_sv_num; ++i)
826 for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
827 add_stack_var_conflict (i, j);
831 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
832 and clear TREE_USED on all local variables. */
835 clear_tree_used (tree block)
839 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
840 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
843 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
847 /* Examine TYPE and determine a bit mask of the following features. */
849 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
850 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
851 #define SPCT_HAS_ARRAY 4
852 #define SPCT_HAS_AGGREGATE 8
855 stack_protect_classify_type (tree type)
857 unsigned int ret = 0;
860 switch (TREE_CODE (type))
863 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
864 if (t == char_type_node
865 || t == signed_char_type_node
866 || t == unsigned_char_type_node)
868 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
869 unsigned HOST_WIDE_INT len;
871 if (!TYPE_SIZE_UNIT (type)
872 || !host_integerp (TYPE_SIZE_UNIT (type), 1))
875 len = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
878 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
880 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
883 ret = SPCT_HAS_ARRAY;
887 case QUAL_UNION_TYPE:
889 ret = SPCT_HAS_AGGREGATE;
890 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
891 if (TREE_CODE (t) == FIELD_DECL)
892 ret |= stack_protect_classify_type (TREE_TYPE (t));
902 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
903 part of the local stack frame. Remember if we ever return nonzero for
904 any variable in this function. The return value is the phase number in
905 which the variable should be allocated. */
908 stack_protect_decl_phase (tree decl)
910 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
913 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
914 has_short_buffer = true;
916 if (flag_stack_protect == 2)
918 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
919 && !(bits & SPCT_HAS_AGGREGATE))
921 else if (bits & SPCT_HAS_ARRAY)
925 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
928 has_protected_decls = true;
933 /* Two helper routines that check for phase 1 and phase 2. These are used
934 as callbacks for expand_stack_vars. */
937 stack_protect_decl_phase_1 (tree decl)
939 return stack_protect_decl_phase (decl) == 1;
943 stack_protect_decl_phase_2 (tree decl)
945 return stack_protect_decl_phase (decl) == 2;
948 /* Ensure that variables in different stack protection phases conflict
949 so that they are not merged and share the same stack slot. */
952 add_stack_protection_conflicts (void)
954 size_t i, j, n = stack_vars_num;
955 unsigned char *phase;
957 phase = XNEWVEC (unsigned char, n);
958 for (i = 0; i < n; ++i)
959 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
961 for (i = 0; i < n; ++i)
963 unsigned char ph_i = phase[i];
964 for (j = 0; j < i; ++j)
965 if (ph_i != phase[j])
966 add_stack_var_conflict (i, j);
972 /* Create a decl for the guard at the top of the stack frame. */
975 create_stack_guard (void)
977 tree guard = build_decl (VAR_DECL, NULL, ptr_type_node);
978 TREE_THIS_VOLATILE (guard) = 1;
979 TREE_USED (guard) = 1;
980 expand_one_stack_var (guard);
981 cfun->stack_protect_guard = guard;
984 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
985 expanding variables. Those variables that can be put into registers
986 are allocated pseudos; those that can't are put on the stack.
988 TOPLEVEL is true if this is the outermost BLOCK. */
991 account_used_vars_for_block (tree block, bool toplevel)
993 size_t i, j, old_sv_num, this_sv_num, new_sv_num;
995 HOST_WIDE_INT size = 0;
997 old_sv_num = toplevel ? 0 : stack_vars_num;
999 /* Expand all variables at this level. */
1000 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1002 size += expand_one_var (t, toplevel, false);
1004 this_sv_num = stack_vars_num;
1006 /* Expand all variables at containing levels. */
1007 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1008 size += account_used_vars_for_block (t, false);
1010 /* Since we do not track exact variable lifetimes (which is not even
1011 possible for variables whose address escapes), we mirror the block
1012 tree in the interference graph. Here we cause all variables at this
1013 level, and all sublevels, to conflict. Do make certain that a
1014 variable conflicts with itself. */
1015 if (old_sv_num < this_sv_num)
1017 new_sv_num = stack_vars_num;
1018 resize_stack_vars_conflict (new_sv_num);
1020 for (i = old_sv_num; i < new_sv_num; ++i)
1021 for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
1022 add_stack_var_conflict (i, j);
1027 /* Prepare for expanding variables. */
1029 init_vars_expansion (void)
1032 /* Set TREE_USED on all variables in the unexpanded_var_list. */
1033 for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
1034 TREE_USED (TREE_VALUE (t)) = 1;
1036 /* Clear TREE_USED on all variables associated with a block scope. */
1037 clear_tree_used (DECL_INITIAL (current_function_decl));
1039 /* Initialize local stack smashing state. */
1040 has_protected_decls = false;
1041 has_short_buffer = false;
1044 /* Free up stack variable graph data. */
1046 fini_vars_expansion (void)
1048 XDELETEVEC (stack_vars);
1049 XDELETEVEC (stack_vars_sorted);
1050 XDELETEVEC (stack_vars_conflict);
1052 stack_vars_alloc = stack_vars_num = 0;
1053 stack_vars_conflict = NULL;
1054 stack_vars_conflict_alloc = 0;
1058 estimated_stack_frame_size (void)
1060 HOST_WIDE_INT size = 0;
1061 tree t, outer_block = DECL_INITIAL (current_function_decl);
1063 init_vars_expansion ();
1065 /* At this point all variables on the unexpanded_var_list with TREE_USED
1066 set are not associated with any block scope. Lay them out. */
1067 for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
1069 tree var = TREE_VALUE (t);
1071 if (TREE_USED (var))
1072 size += expand_one_var (var, true, false);
1073 TREE_USED (var) = 1;
1075 size += account_used_vars_for_block (outer_block, true);
1076 if (stack_vars_num > 0)
1078 /* Due to the way alias sets work, no variables with non-conflicting
1079 alias sets may be assigned the same address. Add conflicts to
1081 add_alias_set_conflicts ();
1083 /* If stack protection is enabled, we don't share space between
1084 vulnerable data and non-vulnerable data. */
1085 if (flag_stack_protect)
1086 add_stack_protection_conflicts ();
1088 /* Now that we have collected all stack variables, and have computed a
1089 minimal interference graph, attempt to save some stack space. */
1090 partition_stack_vars ();
1092 dump_stack_var_partition ();
1094 size += account_stack_vars ();
1095 fini_vars_expansion ();
1100 /* Expand all variables used in the function. */
1103 expand_used_vars (void)
1105 tree t, outer_block = DECL_INITIAL (current_function_decl);
1107 /* Compute the phase of the stack frame for this function. */
1109 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1110 int off = STARTING_FRAME_OFFSET % align;
1111 frame_phase = off ? align - off : 0;
1114 init_vars_expansion ();
1116 /* At this point all variables on the unexpanded_var_list with TREE_USED
1117 set are not associated with any block scope. Lay them out. */
1118 for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
1120 tree var = TREE_VALUE (t);
1121 bool expand_now = false;
1123 /* We didn't set a block for static or extern because it's hard
1124 to tell the difference between a global variable (re)declared
1125 in a local scope, and one that's really declared there to
1126 begin with. And it doesn't really matter much, since we're
1127 not giving them stack space. Expand them now. */
1128 if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1131 /* Any variable that could have been hoisted into an SSA_NAME
1132 will have been propagated anywhere the optimizers chose,
1133 i.e. not confined to their original block. Allocate them
1134 as if they were defined in the outermost scope. */
1135 else if (is_gimple_reg (var))
1138 /* If the variable is not associated with any block, then it
1139 was created by the optimizers, and could be live anywhere
1141 else if (TREE_USED (var))
1144 /* Finally, mark all variables on the list as used. We'll use
1145 this in a moment when we expand those associated with scopes. */
1146 TREE_USED (var) = 1;
1149 expand_one_var (var, true, true);
1151 cfun->unexpanded_var_list = NULL_TREE;
1153 /* At this point, all variables within the block tree with TREE_USED
1154 set are actually used by the optimized function. Lay them out. */
1155 expand_used_vars_for_block (outer_block, true);
1157 if (stack_vars_num > 0)
1159 /* Due to the way alias sets work, no variables with non-conflicting
1160 alias sets may be assigned the same address. Add conflicts to
1162 add_alias_set_conflicts ();
1164 /* If stack protection is enabled, we don't share space between
1165 vulnerable data and non-vulnerable data. */
1166 if (flag_stack_protect)
1167 add_stack_protection_conflicts ();
1169 /* Now that we have collected all stack variables, and have computed a
1170 minimal interference graph, attempt to save some stack space. */
1171 partition_stack_vars ();
1173 dump_stack_var_partition ();
1176 /* There are several conditions under which we should create a
1177 stack guard: protect-all, alloca used, protected decls present. */
1178 if (flag_stack_protect == 2
1179 || (flag_stack_protect
1180 && (current_function_calls_alloca || has_protected_decls)))
1181 create_stack_guard ();
1183 /* Assign rtl to each variable based on these partitions. */
1184 if (stack_vars_num > 0)
1186 /* Reorder decls to be protected by iterating over the variables
1187 array multiple times, and allocating out of each phase in turn. */
1188 /* ??? We could probably integrate this into the qsort we did
1189 earlier, such that we naturally see these variables first,
1190 and thus naturally allocate things in the right order. */
1191 if (has_protected_decls)
1193 /* Phase 1 contains only character arrays. */
1194 expand_stack_vars (stack_protect_decl_phase_1);
1196 /* Phase 2 contains other kinds of arrays. */
1197 if (flag_stack_protect == 2)
1198 expand_stack_vars (stack_protect_decl_phase_2);
1201 expand_stack_vars (NULL);
1203 fini_vars_expansion ();
1206 /* If the target requires that FRAME_OFFSET be aligned, do it. */
1207 if (STACK_ALIGNMENT_NEEDED)
1209 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1210 if (!FRAME_GROWS_DOWNWARD)
1211 frame_offset += align - 1;
1212 frame_offset &= -align;
1217 /* If we need to produce a detailed dump, print the tree representation
1218 for STMT to the dump file. SINCE is the last RTX after which the RTL
1219 generated for STMT should have been appended. */
1222 maybe_dump_rtl_for_tree_stmt (tree stmt, rtx since)
1224 if (dump_file && (dump_flags & TDF_DETAILS))
1226 fprintf (dump_file, "\n;; ");
1227 print_generic_expr (dump_file, stmt, TDF_SLIM);
1228 fprintf (dump_file, "\n");
1230 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1234 /* Maps the blocks that do not contain tree labels to rtx labels. */
1236 static struct pointer_map_t *lab_rtx_for_bb;
1238 /* Returns the label_rtx expression for a label starting basic block BB. */
1241 label_rtx_for_bb (basic_block bb)
1243 tree_stmt_iterator tsi;
1247 if (bb->flags & BB_RTL)
1248 return block_label (bb);
1250 elt = pointer_map_contains (lab_rtx_for_bb, bb);
1254 /* Find the tree label if it is present. */
1256 for (tsi = tsi_start (bb_stmt_list (bb)); !tsi_end_p (tsi); tsi_next (&tsi))
1258 lab_stmt = tsi_stmt (tsi);
1259 if (TREE_CODE (lab_stmt) != LABEL_EXPR)
1262 lab = LABEL_EXPR_LABEL (lab_stmt);
1263 if (DECL_NONLOCAL (lab))
1266 return label_rtx (lab);
1269 elt = pointer_map_insert (lab_rtx_for_bb, bb);
1270 *elt = gen_label_rtx ();
1274 /* A subroutine of expand_gimple_basic_block. Expand one COND_EXPR.
1275 Returns a new basic block if we've terminated the current basic
1276 block and created a new one. */
1279 expand_gimple_cond_expr (basic_block bb, tree stmt)
1281 basic_block new_bb, dest;
1285 tree pred = COND_EXPR_COND (stmt);
1288 gcc_assert (COND_EXPR_THEN (stmt) == NULL_TREE);
1289 gcc_assert (COND_EXPR_ELSE (stmt) == NULL_TREE);
1290 last2 = last = get_last_insn ();
1292 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1293 if (EXPR_LOCUS (stmt))
1295 set_curr_insn_source_location (*(EXPR_LOCUS (stmt)));
1296 set_curr_insn_block (TREE_BLOCK (stmt));
1299 /* These flags have no purpose in RTL land. */
1300 true_edge->flags &= ~EDGE_TRUE_VALUE;
1301 false_edge->flags &= ~EDGE_FALSE_VALUE;
1303 /* We can either have a pure conditional jump with one fallthru edge or
1304 two-way jump that needs to be decomposed into two basic blocks. */
1305 if (false_edge->dest == bb->next_bb)
1307 jumpif (pred, label_rtx_for_bb (true_edge->dest));
1308 add_reg_br_prob_note (last, true_edge->probability);
1309 maybe_dump_rtl_for_tree_stmt (stmt, last);
1310 if (true_edge->goto_locus)
1311 set_curr_insn_source_location (true_edge->goto_locus);
1312 false_edge->flags |= EDGE_FALLTHRU;
1315 if (true_edge->dest == bb->next_bb)
1317 jumpifnot (pred, label_rtx_for_bb (false_edge->dest));
1318 add_reg_br_prob_note (last, false_edge->probability);
1319 maybe_dump_rtl_for_tree_stmt (stmt, last);
1320 if (false_edge->goto_locus)
1321 set_curr_insn_source_location (false_edge->goto_locus);
1322 true_edge->flags |= EDGE_FALLTHRU;
1326 jumpif (pred, label_rtx_for_bb (true_edge->dest));
1327 add_reg_br_prob_note (last, true_edge->probability);
1328 last = get_last_insn ();
1329 emit_jump (label_rtx_for_bb (false_edge->dest));
1332 if (BARRIER_P (BB_END (bb)))
1333 BB_END (bb) = PREV_INSN (BB_END (bb));
1334 update_bb_for_insn (bb);
1336 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1337 dest = false_edge->dest;
1338 redirect_edge_succ (false_edge, new_bb);
1339 false_edge->flags |= EDGE_FALLTHRU;
1340 new_bb->count = false_edge->count;
1341 new_bb->frequency = EDGE_FREQUENCY (false_edge);
1342 new_edge = make_edge (new_bb, dest, 0);
1343 new_edge->probability = REG_BR_PROB_BASE;
1344 new_edge->count = new_bb->count;
1345 if (BARRIER_P (BB_END (new_bb)))
1346 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
1347 update_bb_for_insn (new_bb);
1349 maybe_dump_rtl_for_tree_stmt (stmt, last2);
1351 if (false_edge->goto_locus)
1352 set_curr_insn_source_location (false_edge->goto_locus);
1357 /* A subroutine of expand_gimple_basic_block. Expand one CALL_EXPR
1358 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
1359 generated a tail call (something that might be denied by the ABI
1360 rules governing the call; see calls.c).
1362 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
1363 can still reach the rest of BB. The case here is __builtin_sqrt,
1364 where the NaN result goes through the external function (with a
1365 tailcall) and the normal result happens via a sqrt instruction. */
1368 expand_gimple_tailcall (basic_block bb, tree stmt, bool *can_fallthru)
1376 last2 = last = get_last_insn ();
1378 expand_expr_stmt (stmt);
1380 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
1381 if (CALL_P (last) && SIBLING_CALL_P (last))
1384 maybe_dump_rtl_for_tree_stmt (stmt, last2);
1386 *can_fallthru = true;
1390 /* ??? Wouldn't it be better to just reset any pending stack adjust?
1391 Any instructions emitted here are about to be deleted. */
1392 do_pending_stack_adjust ();
1394 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
1395 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
1396 EH or abnormal edges, we shouldn't have created a tail call in
1397 the first place. So it seems to me we should just be removing
1398 all edges here, or redirecting the existing fallthru edge to
1404 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1406 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
1408 if (e->dest != EXIT_BLOCK_PTR)
1410 e->dest->count -= e->count;
1411 e->dest->frequency -= EDGE_FREQUENCY (e);
1412 if (e->dest->count < 0)
1414 if (e->dest->frequency < 0)
1415 e->dest->frequency = 0;
1418 probability += e->probability;
1425 /* This is somewhat ugly: the call_expr expander often emits instructions
1426 after the sibcall (to perform the function return). These confuse the
1427 find_many_sub_basic_blocks code, so we need to get rid of these. */
1428 last = NEXT_INSN (last);
1429 gcc_assert (BARRIER_P (last));
1431 *can_fallthru = false;
1432 while (NEXT_INSN (last))
1434 /* For instance an sqrt builtin expander expands if with
1435 sibcall in the then and label for `else`. */
1436 if (LABEL_P (NEXT_INSN (last)))
1438 *can_fallthru = true;
1441 delete_insn (NEXT_INSN (last));
1444 e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
1445 e->probability += probability;
1448 update_bb_for_insn (bb);
1450 if (NEXT_INSN (last))
1452 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1455 if (BARRIER_P (last))
1456 BB_END (bb) = PREV_INSN (last);
1459 maybe_dump_rtl_for_tree_stmt (stmt, last2);
1464 /* Expand basic block BB from GIMPLE trees to RTL. */
1467 expand_gimple_basic_block (basic_block bb)
1469 tree_stmt_iterator tsi;
1470 tree stmts = bb_stmt_list (bb);
1480 "\n;; Generating RTL for tree basic block %d\n",
1485 init_rtl_bb_info (bb);
1486 bb->flags |= BB_RTL;
1488 /* Remove the RETURN_EXPR if we may fall though to the exit
1490 tsi = tsi_last (stmts);
1491 if (!tsi_end_p (tsi)
1492 && TREE_CODE (tsi_stmt (tsi)) == RETURN_EXPR)
1494 tree ret_stmt = tsi_stmt (tsi);
1496 gcc_assert (single_succ_p (bb));
1497 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
1499 if (bb->next_bb == EXIT_BLOCK_PTR
1500 && !TREE_OPERAND (ret_stmt, 0))
1503 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
1507 tsi = tsi_start (stmts);
1508 if (!tsi_end_p (tsi))
1510 stmt = tsi_stmt (tsi);
1511 if (TREE_CODE (stmt) != LABEL_EXPR)
1515 elt = pointer_map_contains (lab_rtx_for_bb, bb);
1519 last = get_last_insn ();
1523 expand_expr_stmt (stmt);
1528 emit_label ((rtx) *elt);
1530 /* Java emits line number notes in the top of labels.
1531 ??? Make this go away once line number notes are obsoleted. */
1532 BB_HEAD (bb) = NEXT_INSN (last);
1533 if (NOTE_P (BB_HEAD (bb)))
1534 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
1535 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
1537 maybe_dump_rtl_for_tree_stmt (stmt, last);
1540 note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
1542 NOTE_BASIC_BLOCK (note) = bb;
1544 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1546 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
1547 e->flags &= ~EDGE_EXECUTABLE;
1549 /* At the moment not all abnormal edges match the RTL representation.
1550 It is safe to remove them here as find_many_sub_basic_blocks will
1551 rediscover them. In the future we should get this fixed properly. */
1552 if (e->flags & EDGE_ABNORMAL)
1558 for (; !tsi_end_p (tsi); tsi_next (&tsi))
1560 tree stmt = tsi_stmt (tsi);
1566 /* Expand this statement, then evaluate the resulting RTL and
1567 fixup the CFG accordingly. */
1568 if (TREE_CODE (stmt) == COND_EXPR)
1570 new_bb = expand_gimple_cond_expr (bb, stmt);
1576 tree call = get_call_expr_in (stmt);
1578 /* For the benefit of calls.c, converting all this to rtl,
1579 we need to record the call expression, not just the outer
1580 modify statement. */
1581 if (call && call != stmt)
1583 if ((region = lookup_stmt_eh_region (stmt)) > 0)
1584 add_stmt_to_eh_region (call, region);
1585 gimple_duplicate_stmt_histograms (cfun, call, cfun, stmt);
1587 if (call && CALL_EXPR_TAILCALL (call))
1590 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
1601 last = get_last_insn ();
1602 expand_expr_stmt (stmt);
1603 maybe_dump_rtl_for_tree_stmt (stmt, last);
1608 /* Expand implicit goto. */
1609 FOR_EACH_EDGE (e, ei, bb->succs)
1611 if (e->flags & EDGE_FALLTHRU)
1615 if (e && e->dest != bb->next_bb)
1617 emit_jump (label_rtx_for_bb (e->dest));
1619 set_curr_insn_source_location (e->goto_locus);
1620 e->flags &= ~EDGE_FALLTHRU;
1623 do_pending_stack_adjust ();
1625 /* Find the block tail. The last insn in the block is the insn
1626 before a barrier and/or table jump insn. */
1627 last = get_last_insn ();
1628 if (BARRIER_P (last))
1629 last = PREV_INSN (last);
1630 if (JUMP_TABLE_DATA_P (last))
1631 last = PREV_INSN (PREV_INSN (last));
1634 update_bb_for_insn (bb);
1640 /* Create a basic block for initialization code. */
1643 construct_init_block (void)
1645 basic_block init_block, first_block;
1649 /* Multiple entry points not supported yet. */
1650 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
1651 init_rtl_bb_info (ENTRY_BLOCK_PTR);
1652 init_rtl_bb_info (EXIT_BLOCK_PTR);
1653 ENTRY_BLOCK_PTR->flags |= BB_RTL;
1654 EXIT_BLOCK_PTR->flags |= BB_RTL;
1656 e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
1658 /* When entry edge points to first basic block, we don't need jump,
1659 otherwise we have to jump into proper target. */
1660 if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
1662 tree label = tree_block_label (e->dest);
1664 emit_jump (label_rtx (label));
1668 flags = EDGE_FALLTHRU;
1670 init_block = create_basic_block (NEXT_INSN (get_insns ()),
1673 init_block->frequency = ENTRY_BLOCK_PTR->frequency;
1674 init_block->count = ENTRY_BLOCK_PTR->count;
1677 first_block = e->dest;
1678 redirect_edge_succ (e, init_block);
1679 e = make_edge (init_block, first_block, flags);
1682 e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1683 e->probability = REG_BR_PROB_BASE;
1684 e->count = ENTRY_BLOCK_PTR->count;
1686 update_bb_for_insn (init_block);
1690 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
1691 found in the block tree. */
1694 set_block_levels (tree block, int level)
1698 BLOCK_NUMBER (block) = level;
1699 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
1700 block = BLOCK_CHAIN (block);
1704 /* Create a block containing landing pads and similar stuff. */
1707 construct_exit_block (void)
1709 rtx head = get_last_insn ();
1711 basic_block exit_block;
1715 rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb);
1717 /* Make sure the locus is set to the end of the function, so that
1718 epilogue line numbers and warnings are set properly. */
1719 if (cfun->function_end_locus != UNKNOWN_LOCATION)
1720 input_location = cfun->function_end_locus;
1722 /* The following insns belong to the top scope. */
1723 set_curr_insn_block (DECL_INITIAL (current_function_decl));
1725 /* Generate rtl for function exit. */
1726 expand_function_end ();
1728 end = get_last_insn ();
1731 /* While emitting the function end we could move end of the last basic block.
1733 BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end;
1734 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
1735 head = NEXT_INSN (head);
1736 exit_block = create_basic_block (NEXT_INSN (head), end,
1737 EXIT_BLOCK_PTR->prev_bb);
1738 exit_block->frequency = EXIT_BLOCK_PTR->frequency;
1739 exit_block->count = EXIT_BLOCK_PTR->count;
1742 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
1744 e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
1745 if (!(e->flags & EDGE_ABNORMAL))
1746 redirect_edge_succ (e, exit_block);
1751 e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1752 e->probability = REG_BR_PROB_BASE;
1753 e->count = EXIT_BLOCK_PTR->count;
1754 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
1757 e->count -= e2->count;
1758 exit_block->count -= e2->count;
1759 exit_block->frequency -= EDGE_FREQUENCY (e2);
1763 if (exit_block->count < 0)
1764 exit_block->count = 0;
1765 if (exit_block->frequency < 0)
1766 exit_block->frequency = 0;
1767 update_bb_for_insn (exit_block);
1770 /* Helper function for discover_nonconstant_array_refs.
1771 Look for ARRAY_REF nodes with non-constant indexes and mark them
1775 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
1776 void *data ATTRIBUTE_UNUSED)
1780 if (IS_TYPE_OR_DECL_P (t))
1782 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1784 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1785 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
1786 && (!TREE_OPERAND (t, 2)
1787 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1788 || (TREE_CODE (t) == COMPONENT_REF
1789 && (!TREE_OPERAND (t,2)
1790 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1791 || TREE_CODE (t) == BIT_FIELD_REF
1792 || TREE_CODE (t) == REALPART_EXPR
1793 || TREE_CODE (t) == IMAGPART_EXPR
1794 || TREE_CODE (t) == VIEW_CONVERT_EXPR
1795 || TREE_CODE (t) == NOP_EXPR
1796 || TREE_CODE (t) == CONVERT_EXPR)
1797 t = TREE_OPERAND (t, 0);
1799 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1801 t = get_base_address (t);
1802 if (t && DECL_P (t))
1803 TREE_ADDRESSABLE (t) = 1;
1812 /* RTL expansion is not able to compile array references with variable
1813 offsets for arrays stored in single register. Discover such
1814 expressions and mark variables as addressable to avoid this
1818 discover_nonconstant_array_refs (void)
1821 block_stmt_iterator bsi;
1825 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1826 walk_tree (bsi_stmt_ptr (bsi), discover_nonconstant_array_refs_r,
1831 /* Translate the intermediate representation contained in the CFG
1832 from GIMPLE trees to RTL.
1834 We do conversion per basic block and preserve/update the tree CFG.
1835 This implies we have to do some magic as the CFG can simultaneously
1836 consist of basic blocks containing RTL and GIMPLE trees. This can
1837 confuse the CFG hooks, so be careful to not manipulate CFG during
1841 tree_expand_cfg (void)
1843 basic_block bb, init_block;
1848 /* Some backends want to know that we are expanding to RTL. */
1849 currently_expanding_to_rtl = 1;
1851 insn_locators_alloc ();
1852 if (!DECL_BUILT_IN (current_function_decl))
1853 set_curr_insn_source_location (DECL_SOURCE_LOCATION (current_function_decl));
1854 set_curr_insn_block (DECL_INITIAL (current_function_decl));
1855 prologue_locator = curr_insn_locator ();
1857 /* Make sure first insn is a note even if we don't want linenums.
1858 This makes sure the first insn will never be deleted.
1859 Also, final expects a note to appear there. */
1860 emit_note (NOTE_INSN_DELETED);
1862 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
1863 discover_nonconstant_array_refs ();
1865 targetm.expand_to_rtl_hook ();
1867 /* Expand the variables recorded during gimple lowering. */
1868 expand_used_vars ();
1870 /* Honor stack protection warnings. */
1871 if (warn_stack_protect)
1873 if (current_function_calls_alloca)
1874 warning (OPT_Wstack_protector,
1875 "not protecting local variables: variable length buffer");
1876 if (has_short_buffer && !cfun->stack_protect_guard)
1877 warning (OPT_Wstack_protector,
1878 "not protecting function: no buffer at least %d bytes long",
1879 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
1882 /* Set up parameters and prepare for return, for the function. */
1883 expand_function_start (current_function_decl);
1885 /* If this function is `main', emit a call to `__main'
1886 to run global initializers, etc. */
1887 if (DECL_NAME (current_function_decl)
1888 && MAIN_NAME_P (DECL_NAME (current_function_decl))
1889 && DECL_FILE_SCOPE_P (current_function_decl))
1890 expand_main_function ();
1892 /* Initialize the stack_protect_guard field. This must happen after the
1893 call to __main (if any) so that the external decl is initialized. */
1894 if (cfun->stack_protect_guard)
1895 stack_protect_prologue ();
1897 /* Register rtl specific functions for cfg. */
1898 rtl_register_cfg_hooks ();
1900 init_block = construct_init_block ();
1902 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
1903 remaining edges in expand_gimple_basic_block. */
1904 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
1905 e->flags &= ~EDGE_EXECUTABLE;
1907 lab_rtx_for_bb = pointer_map_create ();
1908 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
1909 bb = expand_gimple_basic_block (bb);
1910 pointer_map_destroy (lab_rtx_for_bb);
1912 construct_exit_block ();
1913 set_curr_insn_block (DECL_INITIAL (current_function_decl));
1914 insn_locators_finalize ();
1916 /* We're done expanding trees to RTL. */
1917 currently_expanding_to_rtl = 0;
1919 /* Convert tree EH labels to RTL EH labels, and clean out any unreachable
1921 convert_from_eh_region_ranges ();
1923 rebuild_jump_labels (get_insns ());
1924 find_exception_handler_labels ();
1926 blocks = sbitmap_alloc (last_basic_block);
1927 sbitmap_ones (blocks);
1928 find_many_sub_basic_blocks (blocks);
1929 purge_all_dead_edges ();
1930 sbitmap_free (blocks);
1933 #ifdef ENABLE_CHECKING
1934 verify_flow_info ();
1937 /* There's no need to defer outputting this function any more; we
1938 know we want to output it. */
1939 DECL_DEFER_OUTPUT (current_function_decl) = 0;
1941 /* Now that we're done expanding trees to RTL, we shouldn't have any
1942 more CONCATs anywhere. */
1943 generating_concat_p = 0;
1948 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
1949 /* And the pass manager will dump RTL for us. */
1952 /* If we're emitting a nested function, make sure its parent gets
1953 emitted as well. Doing otherwise confuses debug info. */
1956 for (parent = DECL_CONTEXT (current_function_decl);
1957 parent != NULL_TREE;
1958 parent = get_containing_scope (parent))
1959 if (TREE_CODE (parent) == FUNCTION_DECL)
1960 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
1963 /* We are now committed to emitting code for this function. Do any
1964 preparation, such as emitting abstract debug info for the inline
1965 before it gets mangled by optimization. */
1966 if (cgraph_function_possibly_inlined_p (current_function_decl))
1967 (*debug_hooks->outlining_inline_function) (current_function_decl);
1969 TREE_ASM_WRITTEN (current_function_decl) = 1;
1971 /* After expanding, the return labels are no longer needed. */
1972 return_label = NULL;
1973 naked_return_label = NULL;
1975 /* Tag the blocks with a depth number so that change_scope can find
1976 the common parent easily. */
1977 set_block_levels (DECL_INITIAL (cfun->decl), 0);
1981 struct gimple_opt_pass pass_expand =
1985 "expand", /* name */
1987 tree_expand_cfg, /* execute */
1990 0, /* static_pass_number */
1991 TV_EXPAND, /* tv_id */
1992 /* ??? If TER is enabled, we actually receive GENERIC. */
1993 PROP_gimple_leh | PROP_cfg, /* properties_required */
1994 PROP_rtl, /* properties_provided */
1995 PROP_trees, /* properties_destroyed */
1996 0, /* todo_flags_start */
1997 TODO_dump_func, /* todo_flags_finish */