mips.h (set_volatile): Delete.
[official-gcc.git] / gcc / cfgexpand.c
blob0a4e2ca91e68507dbfd7c4284d6b8f40980ac88f
1 /* A pass for lowering trees to RTL.
2 Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "basic-block.h"
28 #include "function.h"
29 #include "expr.h"
30 #include "langhooks.h"
31 #include "tree-flow.h"
32 #include "timevar.h"
33 #include "tree-dump.h"
34 #include "tree-pass.h"
35 #include "except.h"
36 #include "flags.h"
37 #include "diagnostic.h"
38 #include "toplev.h"
39 #include "debug.h"
40 #include "params.h"
41 #include "tree-inline.h"
42 #include "value-prof.h"
44 /* Verify that there is exactly single jump instruction since last and attach
45 REG_BR_PROB note specifying probability.
46 ??? We really ought to pass the probability down to RTL expanders and let it
47 re-distribute it when the conditional expands into multiple conditionals.
48 This is however difficult to do. */
49 void
50 add_reg_br_prob_note (rtx last, int probability)
52 if (profile_status == PROFILE_ABSENT)
53 return;
54 for (last = NEXT_INSN (last); last && NEXT_INSN (last); last = NEXT_INSN (last))
55 if (JUMP_P (last))
57 /* It is common to emit condjump-around-jump sequence when we don't know
58 how to reverse the conditional. Special case this. */
59 if (!any_condjump_p (last)
60 || !JUMP_P (NEXT_INSN (last))
61 || !simplejump_p (NEXT_INSN (last))
62 || !NEXT_INSN (NEXT_INSN (last))
63 || !BARRIER_P (NEXT_INSN (NEXT_INSN (last)))
64 || !NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))
65 || !LABEL_P (NEXT_INSN (NEXT_INSN (NEXT_INSN (last))))
66 || NEXT_INSN (NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))))
67 goto failed;
68 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
69 REG_NOTES (last)
70 = gen_rtx_EXPR_LIST (REG_BR_PROB,
71 GEN_INT (REG_BR_PROB_BASE - probability),
72 REG_NOTES (last));
73 return;
75 if (!last || !JUMP_P (last) || !any_condjump_p (last))
76 goto failed;
77 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
78 REG_NOTES (last)
79 = gen_rtx_EXPR_LIST (REG_BR_PROB,
80 GEN_INT (probability), REG_NOTES (last));
81 return;
82 failed:
83 if (dump_file)
84 fprintf (dump_file, "Failed to add probability note\n");
88 #ifndef LOCAL_ALIGNMENT
89 #define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
90 #endif
92 #ifndef STACK_ALIGNMENT_NEEDED
93 #define STACK_ALIGNMENT_NEEDED 1
94 #endif
97 /* This structure holds data relevant to one variable that will be
98 placed in a stack slot. */
99 struct stack_var
101 /* The Variable. */
102 tree decl;
104 /* The offset of the variable. During partitioning, this is the
105 offset relative to the partition. After partitioning, this
106 is relative to the stack frame. */
107 HOST_WIDE_INT offset;
109 /* Initially, the size of the variable. Later, the size of the partition,
110 if this variable becomes it's partition's representative. */
111 HOST_WIDE_INT size;
113 /* The *byte* alignment required for this variable. Or as, with the
114 size, the alignment for this partition. */
115 unsigned int alignb;
117 /* The partition representative. */
118 size_t representative;
120 /* The next stack variable in the partition, or EOC. */
121 size_t next;
124 #define EOC ((size_t)-1)
126 /* We have an array of such objects while deciding allocation. */
127 static struct stack_var *stack_vars;
128 static size_t stack_vars_alloc;
129 static size_t stack_vars_num;
131 /* An array of indicies such that stack_vars[stack_vars_sorted[i]].size
132 is non-decreasing. */
133 static size_t *stack_vars_sorted;
135 /* We have an interference graph between such objects. This graph
136 is lower triangular. */
137 static bool *stack_vars_conflict;
138 static size_t stack_vars_conflict_alloc;
140 /* The phase of the stack frame. This is the known misalignment of
141 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
142 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
143 static int frame_phase;
145 /* Used during expand_used_vars to remember if we saw any decls for
146 which we'd like to enable stack smashing protection. */
147 static bool has_protected_decls;
149 /* Used during expand_used_vars. Remember if we say a character buffer
150 smaller than our cutoff threshold. Used for -Wstack-protector. */
151 static bool has_short_buffer;
153 /* Discover the byte alignment to use for DECL. Ignore alignment
154 we can't do with expected alignment of the stack boundary. */
156 static unsigned int
157 get_decl_align_unit (tree decl)
159 unsigned int align;
161 align = DECL_ALIGN (decl);
162 align = LOCAL_ALIGNMENT (TREE_TYPE (decl), align);
163 if (align > PREFERRED_STACK_BOUNDARY)
164 align = PREFERRED_STACK_BOUNDARY;
165 if (cfun->stack_alignment_needed < align)
166 cfun->stack_alignment_needed = align;
168 return align / BITS_PER_UNIT;
171 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
172 Return the frame offset. */
174 static HOST_WIDE_INT
175 alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align)
177 HOST_WIDE_INT offset, new_frame_offset;
179 new_frame_offset = frame_offset;
180 if (FRAME_GROWS_DOWNWARD)
182 new_frame_offset -= size + frame_phase;
183 new_frame_offset &= -align;
184 new_frame_offset += frame_phase;
185 offset = new_frame_offset;
187 else
189 new_frame_offset -= frame_phase;
190 new_frame_offset += align - 1;
191 new_frame_offset &= -align;
192 new_frame_offset += frame_phase;
193 offset = new_frame_offset;
194 new_frame_offset += size;
196 frame_offset = new_frame_offset;
198 if (frame_offset_overflow (frame_offset, cfun->decl))
199 frame_offset = offset = 0;
201 return offset;
204 /* Accumulate DECL into STACK_VARS. */
206 static void
207 add_stack_var (tree decl)
209 if (stack_vars_num >= stack_vars_alloc)
211 if (stack_vars_alloc)
212 stack_vars_alloc = stack_vars_alloc * 3 / 2;
213 else
214 stack_vars_alloc = 32;
215 stack_vars
216 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
218 stack_vars[stack_vars_num].decl = decl;
219 stack_vars[stack_vars_num].offset = 0;
220 stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
221 stack_vars[stack_vars_num].alignb = get_decl_align_unit (decl);
223 /* All variables are initially in their own partition. */
224 stack_vars[stack_vars_num].representative = stack_vars_num;
225 stack_vars[stack_vars_num].next = EOC;
227 /* Ensure that this decl doesn't get put onto the list twice. */
228 SET_DECL_RTL (decl, pc_rtx);
230 stack_vars_num++;
233 /* Compute the linear index of a lower-triangular coordinate (I, J). */
235 static size_t
236 triangular_index (size_t i, size_t j)
238 if (i < j)
240 size_t t;
241 t = i, i = j, j = t;
243 return (i * (i + 1)) / 2 + j;
246 /* Ensure that STACK_VARS_CONFLICT is large enough for N objects. */
248 static void
249 resize_stack_vars_conflict (size_t n)
251 size_t size = triangular_index (n-1, n-1) + 1;
253 if (size <= stack_vars_conflict_alloc)
254 return;
256 stack_vars_conflict = XRESIZEVEC (bool, stack_vars_conflict, size);
257 memset (stack_vars_conflict + stack_vars_conflict_alloc, 0,
258 (size - stack_vars_conflict_alloc) * sizeof (bool));
259 stack_vars_conflict_alloc = size;
262 /* Make the decls associated with luid's X and Y conflict. */
264 static void
265 add_stack_var_conflict (size_t x, size_t y)
267 size_t index = triangular_index (x, y);
268 gcc_assert (index < stack_vars_conflict_alloc);
269 stack_vars_conflict[index] = true;
272 /* Check whether the decls associated with luid's X and Y conflict. */
274 static bool
275 stack_var_conflict_p (size_t x, size_t y)
277 size_t index = triangular_index (x, y);
278 gcc_assert (index < stack_vars_conflict_alloc);
279 return stack_vars_conflict[index];
282 /* Returns true if TYPE is or contains a union type. */
284 static bool
285 aggregate_contains_union_type (tree type)
287 tree field;
289 if (TREE_CODE (type) == UNION_TYPE
290 || TREE_CODE (type) == QUAL_UNION_TYPE)
291 return true;
292 if (TREE_CODE (type) == ARRAY_TYPE)
293 return aggregate_contains_union_type (TREE_TYPE (type));
294 if (TREE_CODE (type) != RECORD_TYPE)
295 return false;
297 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
298 if (TREE_CODE (field) == FIELD_DECL)
299 if (aggregate_contains_union_type (TREE_TYPE (field)))
300 return true;
302 return false;
305 /* A subroutine of expand_used_vars. If two variables X and Y have alias
306 sets that do not conflict, then do add a conflict for these variables
307 in the interference graph. We also need to make sure to add conflicts
308 for union containing structures. Else RTL alias analysis comes along
309 and due to type based aliasing rules decides that for two overlapping
310 union temporaries { short s; int i; } accesses to the same mem through
311 different types may not alias and happily reorders stores across
312 life-time boundaries of the temporaries (See PR25654).
313 We also have to mind MEM_IN_STRUCT_P and MEM_SCALAR_P. */
315 static void
316 add_alias_set_conflicts (void)
318 size_t i, j, n = stack_vars_num;
320 for (i = 0; i < n; ++i)
322 tree type_i = TREE_TYPE (stack_vars[i].decl);
323 bool aggr_i = AGGREGATE_TYPE_P (type_i);
324 bool contains_union;
326 contains_union = aggregate_contains_union_type (type_i);
327 for (j = 0; j < i; ++j)
329 tree type_j = TREE_TYPE (stack_vars[j].decl);
330 bool aggr_j = AGGREGATE_TYPE_P (type_j);
331 if (aggr_i != aggr_j
332 /* Either the objects conflict by means of type based
333 aliasing rules, or we need to add a conflict. */
334 || !objects_must_conflict_p (type_i, type_j)
335 /* In case the types do not conflict ensure that access
336 to elements will conflict. In case of unions we have
337 to be careful as type based aliasing rules may say
338 access to the same memory does not conflict. So play
339 safe and add a conflict in this case. */
340 || contains_union)
341 add_stack_var_conflict (i, j);
346 /* A subroutine of partition_stack_vars. A comparison function for qsort,
347 sorting an array of indicies by the size of the object. */
349 static int
350 stack_var_size_cmp (const void *a, const void *b)
352 HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size;
353 HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size;
354 unsigned int uida = DECL_UID (stack_vars[*(const size_t *)a].decl);
355 unsigned int uidb = DECL_UID (stack_vars[*(const size_t *)b].decl);
357 if (sa < sb)
358 return -1;
359 if (sa > sb)
360 return 1;
361 /* For stack variables of the same size use the uid of the decl
362 to make the sort stable. */
363 if (uida < uidb)
364 return -1;
365 if (uida > uidb)
366 return 1;
367 return 0;
370 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
371 partitioning algorithm. Partitions A and B are known to be non-conflicting.
372 Merge them into a single partition A.
374 At the same time, add OFFSET to all variables in partition B. At the end
375 of the partitioning process we've have a nice block easy to lay out within
376 the stack frame. */
378 static void
379 union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset)
381 size_t i, last;
383 /* Update each element of partition B with the given offset,
384 and merge them into partition A. */
385 for (last = i = b; i != EOC; last = i, i = stack_vars[i].next)
387 stack_vars[i].offset += offset;
388 stack_vars[i].representative = a;
390 stack_vars[last].next = stack_vars[a].next;
391 stack_vars[a].next = b;
393 /* Update the required alignment of partition A to account for B. */
394 if (stack_vars[a].alignb < stack_vars[b].alignb)
395 stack_vars[a].alignb = stack_vars[b].alignb;
397 /* Update the interference graph and merge the conflicts. */
398 for (last = stack_vars_num, i = 0; i < last; ++i)
399 if (stack_var_conflict_p (b, i))
400 add_stack_var_conflict (a, i);
403 /* A subroutine of expand_used_vars. Binpack the variables into
404 partitions constrained by the interference graph. The overall
405 algorithm used is as follows:
407 Sort the objects by size.
408 For each object A {
409 S = size(A)
410 O = 0
411 loop {
412 Look for the largest non-conflicting object B with size <= S.
413 UNION (A, B)
414 offset(B) = O
415 O += size(B)
416 S -= size(B)
421 static void
422 partition_stack_vars (void)
424 size_t si, sj, n = stack_vars_num;
426 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
427 for (si = 0; si < n; ++si)
428 stack_vars_sorted[si] = si;
430 if (n == 1)
431 return;
433 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp);
435 /* Special case: detect when all variables conflict, and thus we can't
436 do anything during the partitioning loop. It isn't uncommon (with
437 C code at least) to declare all variables at the top of the function,
438 and if we're not inlining, then all variables will be in the same scope.
439 Take advantage of very fast libc routines for this scan. */
440 gcc_assert (sizeof(bool) == sizeof(char));
441 if (memchr (stack_vars_conflict, false, stack_vars_conflict_alloc) == NULL)
442 return;
444 for (si = 0; si < n; ++si)
446 size_t i = stack_vars_sorted[si];
447 HOST_WIDE_INT isize = stack_vars[i].size;
448 HOST_WIDE_INT offset = 0;
450 for (sj = si; sj-- > 0; )
452 size_t j = stack_vars_sorted[sj];
453 HOST_WIDE_INT jsize = stack_vars[j].size;
454 unsigned int jalign = stack_vars[j].alignb;
456 /* Ignore objects that aren't partition representatives. */
457 if (stack_vars[j].representative != j)
458 continue;
460 /* Ignore objects too large for the remaining space. */
461 if (isize < jsize)
462 continue;
464 /* Ignore conflicting objects. */
465 if (stack_var_conflict_p (i, j))
466 continue;
468 /* Refine the remaining space check to include alignment. */
469 if (offset & (jalign - 1))
471 HOST_WIDE_INT toff = offset;
472 toff += jalign - 1;
473 toff &= -(HOST_WIDE_INT)jalign;
474 if (isize - (toff - offset) < jsize)
475 continue;
477 isize -= toff - offset;
478 offset = toff;
481 /* UNION the objects, placing J at OFFSET. */
482 union_stack_vars (i, j, offset);
484 isize -= jsize;
485 if (isize == 0)
486 break;
491 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
493 static void
494 dump_stack_var_partition (void)
496 size_t si, i, j, n = stack_vars_num;
498 for (si = 0; si < n; ++si)
500 i = stack_vars_sorted[si];
502 /* Skip variables that aren't partition representatives, for now. */
503 if (stack_vars[i].representative != i)
504 continue;
506 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
507 " align %u\n", (unsigned long) i, stack_vars[i].size,
508 stack_vars[i].alignb);
510 for (j = i; j != EOC; j = stack_vars[j].next)
512 fputc ('\t', dump_file);
513 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
514 fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n",
515 stack_vars[j].offset);
520 /* Assign rtl to DECL at frame offset OFFSET. */
522 static void
523 expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset)
525 HOST_WIDE_INT align;
526 rtx x;
528 /* If this fails, we've overflowed the stack frame. Error nicely? */
529 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
531 x = plus_constant (virtual_stack_vars_rtx, offset);
532 x = gen_rtx_MEM (DECL_MODE (decl), x);
534 /* Set alignment we actually gave this decl. */
535 offset -= frame_phase;
536 align = offset & -offset;
537 align *= BITS_PER_UNIT;
538 if (align > STACK_BOUNDARY || align == 0)
539 align = STACK_BOUNDARY;
540 DECL_ALIGN (decl) = align;
541 DECL_USER_ALIGN (decl) = 0;
543 set_mem_attributes (x, decl, true);
544 SET_DECL_RTL (decl, x);
547 /* A subroutine of expand_used_vars. Give each partition representative
548 a unique location within the stack frame. Update each partition member
549 with that location. */
551 static void
552 expand_stack_vars (bool (*pred) (tree))
554 size_t si, i, j, n = stack_vars_num;
556 for (si = 0; si < n; ++si)
558 HOST_WIDE_INT offset;
560 i = stack_vars_sorted[si];
562 /* Skip variables that aren't partition representatives, for now. */
563 if (stack_vars[i].representative != i)
564 continue;
566 /* Skip variables that have already had rtl assigned. See also
567 add_stack_var where we perpetrate this pc_rtx hack. */
568 if (DECL_RTL (stack_vars[i].decl) != pc_rtx)
569 continue;
571 /* Check the predicate to see whether this variable should be
572 allocated in this pass. */
573 if (pred && !pred (stack_vars[i].decl))
574 continue;
576 offset = alloc_stack_frame_space (stack_vars[i].size,
577 stack_vars[i].alignb);
579 /* Create rtl for each variable based on their location within the
580 partition. */
581 for (j = i; j != EOC; j = stack_vars[j].next)
583 gcc_assert (stack_vars[j].offset <= stack_vars[i].size);
584 expand_one_stack_var_at (stack_vars[j].decl,
585 stack_vars[j].offset + offset);
590 /* Take into account all sizes of partitions and reset DECL_RTLs. */
591 static HOST_WIDE_INT
592 account_stack_vars (void)
594 size_t si, j, i, n = stack_vars_num;
595 HOST_WIDE_INT size = 0;
597 for (si = 0; si < n; ++si)
599 i = stack_vars_sorted[si];
601 /* Skip variables that aren't partition representatives, for now. */
602 if (stack_vars[i].representative != i)
603 continue;
605 size += stack_vars[i].size;
606 for (j = i; j != EOC; j = stack_vars[j].next)
607 SET_DECL_RTL (stack_vars[j].decl, NULL);
609 return size;
612 /* A subroutine of expand_one_var. Called to immediately assign rtl
613 to a variable to be allocated in the stack frame. */
615 static void
616 expand_one_stack_var (tree var)
618 HOST_WIDE_INT size, offset, align;
620 size = tree_low_cst (DECL_SIZE_UNIT (var), 1);
621 align = get_decl_align_unit (var);
622 offset = alloc_stack_frame_space (size, align);
624 expand_one_stack_var_at (var, offset);
627 /* A subroutine of expand_one_var. Called to assign rtl
628 to a TREE_STATIC VAR_DECL. */
630 static void
631 expand_one_static_var (tree var)
633 /* In unit-at-a-time all the static variables are expanded at the end
634 of compilation process. */
635 if (flag_unit_at_a_time)
636 return;
637 /* If this is an inlined copy of a static local variable,
638 look up the original. */
639 var = DECL_ORIGIN (var);
641 /* If we've already processed this variable because of that, do nothing. */
642 if (TREE_ASM_WRITTEN (var))
643 return;
645 /* Give the front end a chance to do whatever. In practice, this is
646 resolving duplicate names for IMA in C. */
647 if (lang_hooks.expand_decl (var))
648 return;
650 /* Otherwise, just emit the variable. */
651 rest_of_decl_compilation (var, 0, 0);
654 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
655 that will reside in a hard register. */
657 static void
658 expand_one_hard_reg_var (tree var)
660 rest_of_decl_compilation (var, 0, 0);
663 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
664 that will reside in a pseudo register. */
666 static void
667 expand_one_register_var (tree var)
669 tree type = TREE_TYPE (var);
670 int unsignedp = TYPE_UNSIGNED (type);
671 enum machine_mode reg_mode
672 = promote_mode (type, DECL_MODE (var), &unsignedp, 0);
673 rtx x = gen_reg_rtx (reg_mode);
675 SET_DECL_RTL (var, x);
677 /* Note if the object is a user variable. */
678 if (!DECL_ARTIFICIAL (var))
679 mark_user_reg (x);
681 if (POINTER_TYPE_P (type))
682 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
685 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
686 has some associated error, e.g. its type is error-mark. We just need
687 to pick something that won't crash the rest of the compiler. */
689 static void
690 expand_one_error_var (tree var)
692 enum machine_mode mode = DECL_MODE (var);
693 rtx x;
695 if (mode == BLKmode)
696 x = gen_rtx_MEM (BLKmode, const0_rtx);
697 else if (mode == VOIDmode)
698 x = const0_rtx;
699 else
700 x = gen_reg_rtx (mode);
702 SET_DECL_RTL (var, x);
705 /* A subroutine of expand_one_var. VAR is a variable that will be
706 allocated to the local stack frame. Return true if we wish to
707 add VAR to STACK_VARS so that it will be coalesced with other
708 variables. Return false to allocate VAR immediately.
710 This function is used to reduce the number of variables considered
711 for coalescing, which reduces the size of the quadratic problem. */
713 static bool
714 defer_stack_allocation (tree var, bool toplevel)
716 /* If stack protection is enabled, *all* stack variables must be deferred,
717 so that we can re-order the strings to the top of the frame. */
718 if (flag_stack_protect)
719 return true;
721 /* Variables in the outermost scope automatically conflict with
722 every other variable. The only reason to want to defer them
723 at all is that, after sorting, we can more efficiently pack
724 small variables in the stack frame. Continue to defer at -O2. */
725 if (toplevel && optimize < 2)
726 return false;
728 /* Without optimization, *most* variables are allocated from the
729 stack, which makes the quadratic problem large exactly when we
730 want compilation to proceed as quickly as possible. On the
731 other hand, we don't want the function's stack frame size to
732 get completely out of hand. So we avoid adding scalars and
733 "small" aggregates to the list at all. */
734 if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32)
735 return false;
737 return true;
740 /* A subroutine of expand_used_vars. Expand one variable according to
741 its flavor. Variables to be placed on the stack are not actually
742 expanded yet, merely recorded.
743 When REALLY_EXPAND is false, only add stack values to be allocated.
744 Return stack usage this variable is supposed to take.
747 static HOST_WIDE_INT
748 expand_one_var (tree var, bool toplevel, bool really_expand)
750 if (TREE_CODE (var) != VAR_DECL)
752 if (really_expand)
753 lang_hooks.expand_decl (var);
755 else if (DECL_EXTERNAL (var))
757 else if (DECL_HAS_VALUE_EXPR_P (var))
759 else if (TREE_STATIC (var))
761 if (really_expand)
762 expand_one_static_var (var);
764 else if (DECL_RTL_SET_P (var))
766 else if (TREE_TYPE (var) == error_mark_node)
768 if (really_expand)
769 expand_one_error_var (var);
771 else if (DECL_HARD_REGISTER (var))
773 if (really_expand)
774 expand_one_hard_reg_var (var);
776 else if (use_register_for_decl (var))
778 if (really_expand)
779 expand_one_register_var (var);
781 else if (defer_stack_allocation (var, toplevel))
782 add_stack_var (var);
783 else
785 if (really_expand)
786 expand_one_stack_var (var);
787 return tree_low_cst (DECL_SIZE_UNIT (var), 1);
789 return 0;
792 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
793 expanding variables. Those variables that can be put into registers
794 are allocated pseudos; those that can't are put on the stack.
796 TOPLEVEL is true if this is the outermost BLOCK. */
798 static void
799 expand_used_vars_for_block (tree block, bool toplevel)
801 size_t i, j, old_sv_num, this_sv_num, new_sv_num;
802 tree t;
804 old_sv_num = toplevel ? 0 : stack_vars_num;
806 /* Expand all variables at this level. */
807 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
808 if (TREE_USED (t)
809 /* Force local static variables to be output when marked by
810 used attribute. For unit-at-a-time, cgraph code already takes
811 care of this. */
812 || (!flag_unit_at_a_time && TREE_STATIC (t)
813 && DECL_PRESERVE_P (t)))
814 expand_one_var (t, toplevel, true);
816 this_sv_num = stack_vars_num;
818 /* Expand all variables at containing levels. */
819 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
820 expand_used_vars_for_block (t, false);
822 /* Since we do not track exact variable lifetimes (which is not even
823 possible for variables whose address escapes), we mirror the block
824 tree in the interference graph. Here we cause all variables at this
825 level, and all sublevels, to conflict. Do make certain that a
826 variable conflicts with itself. */
827 if (old_sv_num < this_sv_num)
829 new_sv_num = stack_vars_num;
830 resize_stack_vars_conflict (new_sv_num);
832 for (i = old_sv_num; i < new_sv_num; ++i)
833 for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
834 add_stack_var_conflict (i, j);
838 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
839 and clear TREE_USED on all local variables. */
841 static void
842 clear_tree_used (tree block)
844 tree t;
846 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
847 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
848 TREE_USED (t) = 0;
850 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
851 clear_tree_used (t);
854 /* Examine TYPE and determine a bit mask of the following features. */
856 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
857 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
858 #define SPCT_HAS_ARRAY 4
859 #define SPCT_HAS_AGGREGATE 8
861 static unsigned int
862 stack_protect_classify_type (tree type)
864 unsigned int ret = 0;
865 tree t;
867 switch (TREE_CODE (type))
869 case ARRAY_TYPE:
870 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
871 if (t == char_type_node
872 || t == signed_char_type_node
873 || t == unsigned_char_type_node)
875 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
876 unsigned HOST_WIDE_INT len;
878 if (!TYPE_SIZE_UNIT (type)
879 || !host_integerp (TYPE_SIZE_UNIT (type), 1))
880 len = max;
881 else
882 len = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
884 if (len < max)
885 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
886 else
887 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
889 else
890 ret = SPCT_HAS_ARRAY;
891 break;
893 case UNION_TYPE:
894 case QUAL_UNION_TYPE:
895 case RECORD_TYPE:
896 ret = SPCT_HAS_AGGREGATE;
897 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
898 if (TREE_CODE (t) == FIELD_DECL)
899 ret |= stack_protect_classify_type (TREE_TYPE (t));
900 break;
902 default:
903 break;
906 return ret;
909 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
910 part of the local stack frame. Remember if we ever return nonzero for
911 any variable in this function. The return value is the phase number in
912 which the variable should be allocated. */
914 static int
915 stack_protect_decl_phase (tree decl)
917 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
918 int ret = 0;
920 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
921 has_short_buffer = true;
923 if (flag_stack_protect == 2)
925 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
926 && !(bits & SPCT_HAS_AGGREGATE))
927 ret = 1;
928 else if (bits & SPCT_HAS_ARRAY)
929 ret = 2;
931 else
932 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
934 if (ret)
935 has_protected_decls = true;
937 return ret;
940 /* Two helper routines that check for phase 1 and phase 2. These are used
941 as callbacks for expand_stack_vars. */
943 static bool
944 stack_protect_decl_phase_1 (tree decl)
946 return stack_protect_decl_phase (decl) == 1;
949 static bool
950 stack_protect_decl_phase_2 (tree decl)
952 return stack_protect_decl_phase (decl) == 2;
955 /* Ensure that variables in different stack protection phases conflict
956 so that they are not merged and share the same stack slot. */
958 static void
959 add_stack_protection_conflicts (void)
961 size_t i, j, n = stack_vars_num;
962 unsigned char *phase;
964 phase = XNEWVEC (unsigned char, n);
965 for (i = 0; i < n; ++i)
966 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
968 for (i = 0; i < n; ++i)
970 unsigned char ph_i = phase[i];
971 for (j = 0; j < i; ++j)
972 if (ph_i != phase[j])
973 add_stack_var_conflict (i, j);
976 XDELETEVEC (phase);
979 /* Create a decl for the guard at the top of the stack frame. */
981 static void
982 create_stack_guard (void)
984 tree guard = build_decl (VAR_DECL, NULL, ptr_type_node);
985 TREE_THIS_VOLATILE (guard) = 1;
986 TREE_USED (guard) = 1;
987 expand_one_stack_var (guard);
988 cfun->stack_protect_guard = guard;
991 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
992 expanding variables. Those variables that can be put into registers
993 are allocated pseudos; those that can't are put on the stack.
995 TOPLEVEL is true if this is the outermost BLOCK. */
997 static HOST_WIDE_INT
998 account_used_vars_for_block (tree block, bool toplevel)
1000 size_t i, j, old_sv_num, this_sv_num, new_sv_num;
1001 tree t;
1002 HOST_WIDE_INT size = 0;
1004 old_sv_num = toplevel ? 0 : stack_vars_num;
1006 /* Expand all variables at this level. */
1007 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1008 if (TREE_USED (t))
1009 size += expand_one_var (t, toplevel, false);
1011 this_sv_num = stack_vars_num;
1013 /* Expand all variables at containing levels. */
1014 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1015 size += account_used_vars_for_block (t, false);
1017 /* Since we do not track exact variable lifetimes (which is not even
1018 possible for variables whose address escapes), we mirror the block
1019 tree in the interference graph. Here we cause all variables at this
1020 level, and all sublevels, to conflict. Do make certain that a
1021 variable conflicts with itself. */
1022 if (old_sv_num < this_sv_num)
1024 new_sv_num = stack_vars_num;
1025 resize_stack_vars_conflict (new_sv_num);
1027 for (i = old_sv_num; i < new_sv_num; ++i)
1028 for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
1029 add_stack_var_conflict (i, j);
1031 return size;
1034 /* Prepare for expanding variables. */
1035 static void
1036 init_vars_expansion (void)
1038 tree t;
1039 /* Set TREE_USED on all variables in the unexpanded_var_list. */
1040 for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
1041 TREE_USED (TREE_VALUE (t)) = 1;
1043 /* Clear TREE_USED on all variables associated with a block scope. */
1044 clear_tree_used (DECL_INITIAL (current_function_decl));
1046 /* Initialize local stack smashing state. */
1047 has_protected_decls = false;
1048 has_short_buffer = false;
1051 /* Free up stack variable graph data. */
1052 static void
1053 fini_vars_expansion (void)
1055 XDELETEVEC (stack_vars);
1056 XDELETEVEC (stack_vars_sorted);
1057 XDELETEVEC (stack_vars_conflict);
1058 stack_vars = NULL;
1059 stack_vars_alloc = stack_vars_num = 0;
1060 stack_vars_conflict = NULL;
1061 stack_vars_conflict_alloc = 0;
1064 HOST_WIDE_INT
1065 estimated_stack_frame_size (void)
1067 HOST_WIDE_INT size = 0;
1068 tree t, outer_block = DECL_INITIAL (current_function_decl);
1070 init_vars_expansion ();
1072 /* At this point all variables on the unexpanded_var_list with TREE_USED
1073 set are not associated with any block scope. Lay them out. */
1074 for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
1076 tree var = TREE_VALUE (t);
1078 if (TREE_USED (var))
1079 size += expand_one_var (var, true, false);
1080 TREE_USED (var) = 1;
1082 size += account_used_vars_for_block (outer_block, true);
1083 if (stack_vars_num > 0)
1085 /* Due to the way alias sets work, no variables with non-conflicting
1086 alias sets may be assigned the same address. Add conflicts to
1087 reflect this. */
1088 add_alias_set_conflicts ();
1090 /* If stack protection is enabled, we don't share space between
1091 vulnerable data and non-vulnerable data. */
1092 if (flag_stack_protect)
1093 add_stack_protection_conflicts ();
1095 /* Now that we have collected all stack variables, and have computed a
1096 minimal interference graph, attempt to save some stack space. */
1097 partition_stack_vars ();
1098 if (dump_file)
1099 dump_stack_var_partition ();
1101 size += account_stack_vars ();
1102 fini_vars_expansion ();
1104 return size;
1107 /* Expand all variables used in the function. */
1109 static void
1110 expand_used_vars (void)
1112 tree t, outer_block = DECL_INITIAL (current_function_decl);
1114 /* Compute the phase of the stack frame for this function. */
1116 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1117 int off = STARTING_FRAME_OFFSET % align;
1118 frame_phase = off ? align - off : 0;
1121 init_vars_expansion ();
1123 /* At this point all variables on the unexpanded_var_list with TREE_USED
1124 set are not associated with any block scope. Lay them out. */
1125 for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
1127 tree var = TREE_VALUE (t);
1128 bool expand_now = false;
1130 /* We didn't set a block for static or extern because it's hard
1131 to tell the difference between a global variable (re)declared
1132 in a local scope, and one that's really declared there to
1133 begin with. And it doesn't really matter much, since we're
1134 not giving them stack space. Expand them now. */
1135 if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1136 expand_now = true;
1138 /* Any variable that could have been hoisted into an SSA_NAME
1139 will have been propagated anywhere the optimizers chose,
1140 i.e. not confined to their original block. Allocate them
1141 as if they were defined in the outermost scope. */
1142 else if (is_gimple_reg (var))
1143 expand_now = true;
1145 /* If the variable is not associated with any block, then it
1146 was created by the optimizers, and could be live anywhere
1147 in the function. */
1148 else if (TREE_USED (var))
1149 expand_now = true;
1151 /* Finally, mark all variables on the list as used. We'll use
1152 this in a moment when we expand those associated with scopes. */
1153 TREE_USED (var) = 1;
1155 if (expand_now)
1156 expand_one_var (var, true, true);
1158 cfun->unexpanded_var_list = NULL_TREE;
1160 /* At this point, all variables within the block tree with TREE_USED
1161 set are actually used by the optimized function. Lay them out. */
1162 expand_used_vars_for_block (outer_block, true);
1164 if (stack_vars_num > 0)
1166 /* Due to the way alias sets work, no variables with non-conflicting
1167 alias sets may be assigned the same address. Add conflicts to
1168 reflect this. */
1169 add_alias_set_conflicts ();
1171 /* If stack protection is enabled, we don't share space between
1172 vulnerable data and non-vulnerable data. */
1173 if (flag_stack_protect)
1174 add_stack_protection_conflicts ();
1176 /* Now that we have collected all stack variables, and have computed a
1177 minimal interference graph, attempt to save some stack space. */
1178 partition_stack_vars ();
1179 if (dump_file)
1180 dump_stack_var_partition ();
1183 /* There are several conditions under which we should create a
1184 stack guard: protect-all, alloca used, protected decls present. */
1185 if (flag_stack_protect == 2
1186 || (flag_stack_protect
1187 && (current_function_calls_alloca || has_protected_decls)))
1188 create_stack_guard ();
1190 /* Assign rtl to each variable based on these partitions. */
1191 if (stack_vars_num > 0)
1193 /* Reorder decls to be protected by iterating over the variables
1194 array multiple times, and allocating out of each phase in turn. */
1195 /* ??? We could probably integrate this into the qsort we did
1196 earlier, such that we naturally see these variables first,
1197 and thus naturally allocate things in the right order. */
1198 if (has_protected_decls)
1200 /* Phase 1 contains only character arrays. */
1201 expand_stack_vars (stack_protect_decl_phase_1);
1203 /* Phase 2 contains other kinds of arrays. */
1204 if (flag_stack_protect == 2)
1205 expand_stack_vars (stack_protect_decl_phase_2);
1208 expand_stack_vars (NULL);
1210 fini_vars_expansion ();
1213 /* If the target requires that FRAME_OFFSET be aligned, do it. */
1214 if (STACK_ALIGNMENT_NEEDED)
1216 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1217 if (!FRAME_GROWS_DOWNWARD)
1218 frame_offset += align - 1;
1219 frame_offset &= -align;
1224 /* If we need to produce a detailed dump, print the tree representation
1225 for STMT to the dump file. SINCE is the last RTX after which the RTL
1226 generated for STMT should have been appended. */
1228 static void
1229 maybe_dump_rtl_for_tree_stmt (tree stmt, rtx since)
1231 if (dump_file && (dump_flags & TDF_DETAILS))
1233 fprintf (dump_file, "\n;; ");
1234 print_generic_expr (dump_file, stmt, TDF_SLIM);
1235 fprintf (dump_file, "\n");
1237 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1241 /* Maps the blocks that do not contain tree labels to rtx labels. */
1243 static struct pointer_map_t *lab_rtx_for_bb;
1245 /* Returns the label_rtx expression for a label starting basic block BB. */
1247 static rtx
1248 label_rtx_for_bb (basic_block bb)
1250 tree_stmt_iterator tsi;
1251 tree lab, lab_stmt;
1252 void **elt;
1254 if (bb->flags & BB_RTL)
1255 return block_label (bb);
1257 elt = pointer_map_contains (lab_rtx_for_bb, bb);
1258 if (elt)
1259 return (rtx) *elt;
1261 /* Find the tree label if it is present. */
1263 for (tsi = tsi_start (bb_stmt_list (bb)); !tsi_end_p (tsi); tsi_next (&tsi))
1265 lab_stmt = tsi_stmt (tsi);
1266 if (TREE_CODE (lab_stmt) != LABEL_EXPR)
1267 break;
1269 lab = LABEL_EXPR_LABEL (lab_stmt);
1270 if (DECL_NONLOCAL (lab))
1271 break;
1273 return label_rtx (lab);
1276 elt = pointer_map_insert (lab_rtx_for_bb, bb);
1277 *elt = gen_label_rtx ();
1278 return (rtx) *elt;
1281 /* A subroutine of expand_gimple_basic_block. Expand one COND_EXPR.
1282 Returns a new basic block if we've terminated the current basic
1283 block and created a new one. */
1285 static basic_block
1286 expand_gimple_cond_expr (basic_block bb, tree stmt)
1288 basic_block new_bb, dest;
1289 edge new_edge;
1290 edge true_edge;
1291 edge false_edge;
1292 tree pred = COND_EXPR_COND (stmt);
1293 rtx last2, last;
1295 gcc_assert (COND_EXPR_THEN (stmt) == NULL_TREE);
1296 gcc_assert (COND_EXPR_ELSE (stmt) == NULL_TREE);
1297 last2 = last = get_last_insn ();
1299 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1300 if (EXPR_LOCUS (stmt))
1302 set_curr_insn_source_location (*(EXPR_LOCUS (stmt)));
1303 set_curr_insn_block (TREE_BLOCK (stmt));
1306 /* These flags have no purpose in RTL land. */
1307 true_edge->flags &= ~EDGE_TRUE_VALUE;
1308 false_edge->flags &= ~EDGE_FALSE_VALUE;
1310 /* We can either have a pure conditional jump with one fallthru edge or
1311 two-way jump that needs to be decomposed into two basic blocks. */
1312 if (false_edge->dest == bb->next_bb)
1314 jumpif (pred, label_rtx_for_bb (true_edge->dest));
1315 add_reg_br_prob_note (last, true_edge->probability);
1316 maybe_dump_rtl_for_tree_stmt (stmt, last);
1317 if (true_edge->goto_locus)
1318 set_curr_insn_source_location (location_from_locus (true_edge->goto_locus));
1319 false_edge->flags |= EDGE_FALLTHRU;
1320 return NULL;
1322 if (true_edge->dest == bb->next_bb)
1324 jumpifnot (pred, label_rtx_for_bb (false_edge->dest));
1325 add_reg_br_prob_note (last, false_edge->probability);
1326 maybe_dump_rtl_for_tree_stmt (stmt, last);
1327 if (false_edge->goto_locus)
1328 set_curr_insn_source_location (location_from_locus (false_edge->goto_locus));
1329 true_edge->flags |= EDGE_FALLTHRU;
1330 return NULL;
1333 jumpif (pred, label_rtx_for_bb (true_edge->dest));
1334 add_reg_br_prob_note (last, true_edge->probability);
1335 last = get_last_insn ();
1336 emit_jump (label_rtx_for_bb (false_edge->dest));
1338 BB_END (bb) = last;
1339 if (BARRIER_P (BB_END (bb)))
1340 BB_END (bb) = PREV_INSN (BB_END (bb));
1341 update_bb_for_insn (bb);
1343 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1344 dest = false_edge->dest;
1345 redirect_edge_succ (false_edge, new_bb);
1346 false_edge->flags |= EDGE_FALLTHRU;
1347 new_bb->count = false_edge->count;
1348 new_bb->frequency = EDGE_FREQUENCY (false_edge);
1349 new_edge = make_edge (new_bb, dest, 0);
1350 new_edge->probability = REG_BR_PROB_BASE;
1351 new_edge->count = new_bb->count;
1352 if (BARRIER_P (BB_END (new_bb)))
1353 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
1354 update_bb_for_insn (new_bb);
1356 maybe_dump_rtl_for_tree_stmt (stmt, last2);
1358 if (false_edge->goto_locus)
1359 set_curr_insn_source_location (location_from_locus (false_edge->goto_locus));
1361 return new_bb;
1364 /* A subroutine of expand_gimple_basic_block. Expand one CALL_EXPR
1365 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
1366 generated a tail call (something that might be denied by the ABI
1367 rules governing the call; see calls.c).
1369 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
1370 can still reach the rest of BB. The case here is __builtin_sqrt,
1371 where the NaN result goes through the external function (with a
1372 tailcall) and the normal result happens via a sqrt instruction. */
1374 static basic_block
1375 expand_gimple_tailcall (basic_block bb, tree stmt, bool *can_fallthru)
1377 rtx last2, last;
1378 edge e;
1379 edge_iterator ei;
1380 int probability;
1381 gcov_type count;
1383 last2 = last = get_last_insn ();
1385 expand_expr_stmt (stmt);
1387 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
1388 if (CALL_P (last) && SIBLING_CALL_P (last))
1389 goto found;
1391 maybe_dump_rtl_for_tree_stmt (stmt, last2);
1393 *can_fallthru = true;
1394 return NULL;
1396 found:
1397 /* ??? Wouldn't it be better to just reset any pending stack adjust?
1398 Any instructions emitted here are about to be deleted. */
1399 do_pending_stack_adjust ();
1401 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
1402 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
1403 EH or abnormal edges, we shouldn't have created a tail call in
1404 the first place. So it seems to me we should just be removing
1405 all edges here, or redirecting the existing fallthru edge to
1406 the exit block. */
1408 probability = 0;
1409 count = 0;
1411 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1413 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
1415 if (e->dest != EXIT_BLOCK_PTR)
1417 e->dest->count -= e->count;
1418 e->dest->frequency -= EDGE_FREQUENCY (e);
1419 if (e->dest->count < 0)
1420 e->dest->count = 0;
1421 if (e->dest->frequency < 0)
1422 e->dest->frequency = 0;
1424 count += e->count;
1425 probability += e->probability;
1426 remove_edge (e);
1428 else
1429 ei_next (&ei);
1432 /* This is somewhat ugly: the call_expr expander often emits instructions
1433 after the sibcall (to perform the function return). These confuse the
1434 find_many_sub_basic_blocks code, so we need to get rid of these. */
1435 last = NEXT_INSN (last);
1436 gcc_assert (BARRIER_P (last));
1438 *can_fallthru = false;
1439 while (NEXT_INSN (last))
1441 /* For instance an sqrt builtin expander expands if with
1442 sibcall in the then and label for `else`. */
1443 if (LABEL_P (NEXT_INSN (last)))
1445 *can_fallthru = true;
1446 break;
1448 delete_insn (NEXT_INSN (last));
1451 e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
1452 e->probability += probability;
1453 e->count += count;
1454 BB_END (bb) = last;
1455 update_bb_for_insn (bb);
1457 if (NEXT_INSN (last))
1459 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1461 last = BB_END (bb);
1462 if (BARRIER_P (last))
1463 BB_END (bb) = PREV_INSN (last);
1466 maybe_dump_rtl_for_tree_stmt (stmt, last2);
1468 return bb;
1471 /* Expand basic block BB from GIMPLE trees to RTL. */
1473 static basic_block
1474 expand_gimple_basic_block (basic_block bb)
1476 tree_stmt_iterator tsi;
1477 tree stmts = bb_stmt_list (bb);
1478 tree stmt = NULL;
1479 rtx note, last;
1480 edge e;
1481 edge_iterator ei;
1482 void **elt;
1484 if (dump_file)
1486 fprintf (dump_file,
1487 "\n;; Generating RTL for tree basic block %d\n",
1488 bb->index);
1491 bb->il.tree = NULL;
1492 init_rtl_bb_info (bb);
1493 bb->flags |= BB_RTL;
1495 /* Remove the RETURN_EXPR if we may fall though to the exit
1496 instead. */
1497 tsi = tsi_last (stmts);
1498 if (!tsi_end_p (tsi)
1499 && TREE_CODE (tsi_stmt (tsi)) == RETURN_EXPR)
1501 tree ret_stmt = tsi_stmt (tsi);
1503 gcc_assert (single_succ_p (bb));
1504 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
1506 if (bb->next_bb == EXIT_BLOCK_PTR
1507 && !TREE_OPERAND (ret_stmt, 0))
1509 tsi_delink (&tsi);
1510 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
1514 tsi = tsi_start (stmts);
1515 if (!tsi_end_p (tsi))
1517 stmt = tsi_stmt (tsi);
1518 if (TREE_CODE (stmt) != LABEL_EXPR)
1519 stmt = NULL_TREE;
1522 elt = pointer_map_contains (lab_rtx_for_bb, bb);
1524 if (stmt || elt)
1526 last = get_last_insn ();
1528 if (stmt)
1530 expand_expr_stmt (stmt);
1531 tsi_next (&tsi);
1534 if (elt)
1535 emit_label ((rtx) *elt);
1537 /* Java emits line number notes in the top of labels.
1538 ??? Make this go away once line number notes are obsoleted. */
1539 BB_HEAD (bb) = NEXT_INSN (last);
1540 if (NOTE_P (BB_HEAD (bb)))
1541 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
1542 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
1544 maybe_dump_rtl_for_tree_stmt (stmt, last);
1546 else
1547 note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
1549 NOTE_BASIC_BLOCK (note) = bb;
1551 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1553 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
1554 e->flags &= ~EDGE_EXECUTABLE;
1556 /* At the moment not all abnormal edges match the RTL representation.
1557 It is safe to remove them here as find_many_sub_basic_blocks will
1558 rediscover them. In the future we should get this fixed properly. */
1559 if (e->flags & EDGE_ABNORMAL)
1560 remove_edge (e);
1561 else
1562 ei_next (&ei);
1565 for (; !tsi_end_p (tsi); tsi_next (&tsi))
1567 tree stmt = tsi_stmt (tsi);
1568 basic_block new_bb;
1570 if (!stmt)
1571 continue;
1573 /* Expand this statement, then evaluate the resulting RTL and
1574 fixup the CFG accordingly. */
1575 if (TREE_CODE (stmt) == COND_EXPR)
1577 new_bb = expand_gimple_cond_expr (bb, stmt);
1578 if (new_bb)
1579 return new_bb;
1581 else
1583 tree call = get_call_expr_in (stmt);
1584 int region;
1585 /* For the benefit of calls.c, converting all this to rtl,
1586 we need to record the call expression, not just the outer
1587 modify statement. */
1588 if (call && call != stmt)
1590 if ((region = lookup_stmt_eh_region (stmt)) > 0)
1591 add_stmt_to_eh_region (call, region);
1592 gimple_duplicate_stmt_histograms (cfun, call, cfun, stmt);
1594 if (call && CALL_EXPR_TAILCALL (call))
1596 bool can_fallthru;
1597 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
1598 if (new_bb)
1600 if (can_fallthru)
1601 bb = new_bb;
1602 else
1603 return new_bb;
1606 else
1608 last = get_last_insn ();
1609 expand_expr_stmt (stmt);
1610 maybe_dump_rtl_for_tree_stmt (stmt, last);
1615 /* Expand implicit goto. */
1616 FOR_EACH_EDGE (e, ei, bb->succs)
1618 if (e->flags & EDGE_FALLTHRU)
1619 break;
1622 if (e && e->dest != bb->next_bb)
1624 emit_jump (label_rtx_for_bb (e->dest));
1625 if (e->goto_locus)
1626 set_curr_insn_source_location (location_from_locus (e->goto_locus));
1627 e->flags &= ~EDGE_FALLTHRU;
1630 do_pending_stack_adjust ();
1632 /* Find the block tail. The last insn in the block is the insn
1633 before a barrier and/or table jump insn. */
1634 last = get_last_insn ();
1635 if (BARRIER_P (last))
1636 last = PREV_INSN (last);
1637 if (JUMP_TABLE_DATA_P (last))
1638 last = PREV_INSN (PREV_INSN (last));
1639 BB_END (bb) = last;
1641 update_bb_for_insn (bb);
1643 return bb;
1647 /* Create a basic block for initialization code. */
1649 static basic_block
1650 construct_init_block (void)
1652 basic_block init_block, first_block;
1653 edge e = NULL;
1654 int flags;
1656 /* Multiple entry points not supported yet. */
1657 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
1658 init_rtl_bb_info (ENTRY_BLOCK_PTR);
1659 init_rtl_bb_info (EXIT_BLOCK_PTR);
1660 ENTRY_BLOCK_PTR->flags |= BB_RTL;
1661 EXIT_BLOCK_PTR->flags |= BB_RTL;
1663 e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
1665 /* When entry edge points to first basic block, we don't need jump,
1666 otherwise we have to jump into proper target. */
1667 if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
1669 tree label = tree_block_label (e->dest);
1671 emit_jump (label_rtx (label));
1672 flags = 0;
1674 else
1675 flags = EDGE_FALLTHRU;
1677 init_block = create_basic_block (NEXT_INSN (get_insns ()),
1678 get_last_insn (),
1679 ENTRY_BLOCK_PTR);
1680 init_block->frequency = ENTRY_BLOCK_PTR->frequency;
1681 init_block->count = ENTRY_BLOCK_PTR->count;
1682 if (e)
1684 first_block = e->dest;
1685 redirect_edge_succ (e, init_block);
1686 e = make_edge (init_block, first_block, flags);
1688 else
1689 e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1690 e->probability = REG_BR_PROB_BASE;
1691 e->count = ENTRY_BLOCK_PTR->count;
1693 update_bb_for_insn (init_block);
1694 return init_block;
1697 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
1698 found in the block tree. */
1700 static void
1701 set_block_levels (tree block, int level)
1703 while (block)
1705 BLOCK_NUMBER (block) = level;
1706 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
1707 block = BLOCK_CHAIN (block);
1711 /* Create a block containing landing pads and similar stuff. */
1713 static void
1714 construct_exit_block (void)
1716 rtx head = get_last_insn ();
1717 rtx end;
1718 basic_block exit_block;
1719 edge e, e2;
1720 unsigned ix;
1721 edge_iterator ei;
1722 rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb);
1724 /* Make sure the locus is set to the end of the function, so that
1725 epilogue line numbers and warnings are set properly. */
1726 #ifdef USE_MAPPED_LOCATION
1727 if (cfun->function_end_locus != UNKNOWN_LOCATION)
1728 #else
1729 if (cfun->function_end_locus.file)
1730 #endif
1731 input_location = cfun->function_end_locus;
1733 /* The following insns belong to the top scope. */
1734 set_curr_insn_block (DECL_INITIAL (current_function_decl));
1736 /* Generate rtl for function exit. */
1737 expand_function_end ();
1739 end = get_last_insn ();
1740 if (head == end)
1741 return;
1742 /* While emitting the function end we could move end of the last basic block.
1744 BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end;
1745 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
1746 head = NEXT_INSN (head);
1747 exit_block = create_basic_block (NEXT_INSN (head), end,
1748 EXIT_BLOCK_PTR->prev_bb);
1749 exit_block->frequency = EXIT_BLOCK_PTR->frequency;
1750 exit_block->count = EXIT_BLOCK_PTR->count;
1752 ix = 0;
1753 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
1755 e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
1756 if (!(e->flags & EDGE_ABNORMAL))
1757 redirect_edge_succ (e, exit_block);
1758 else
1759 ix++;
1762 e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1763 e->probability = REG_BR_PROB_BASE;
1764 e->count = EXIT_BLOCK_PTR->count;
1765 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
1766 if (e2 != e)
1768 e->count -= e2->count;
1769 exit_block->count -= e2->count;
1770 exit_block->frequency -= EDGE_FREQUENCY (e2);
1772 if (e->count < 0)
1773 e->count = 0;
1774 if (exit_block->count < 0)
1775 exit_block->count = 0;
1776 if (exit_block->frequency < 0)
1777 exit_block->frequency = 0;
1778 update_bb_for_insn (exit_block);
1781 /* Helper function for discover_nonconstant_array_refs.
1782 Look for ARRAY_REF nodes with non-constant indexes and mark them
1783 addressable. */
1785 static tree
1786 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
1787 void *data ATTRIBUTE_UNUSED)
1789 tree t = *tp;
1791 if (IS_TYPE_OR_DECL_P (t))
1792 *walk_subtrees = 0;
1793 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1795 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1796 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
1797 && (!TREE_OPERAND (t, 2)
1798 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1799 || (TREE_CODE (t) == COMPONENT_REF
1800 && (!TREE_OPERAND (t,2)
1801 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1802 || TREE_CODE (t) == BIT_FIELD_REF
1803 || TREE_CODE (t) == REALPART_EXPR
1804 || TREE_CODE (t) == IMAGPART_EXPR
1805 || TREE_CODE (t) == VIEW_CONVERT_EXPR
1806 || TREE_CODE (t) == NOP_EXPR
1807 || TREE_CODE (t) == CONVERT_EXPR)
1808 t = TREE_OPERAND (t, 0);
1810 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1812 t = get_base_address (t);
1813 if (t && DECL_P (t))
1814 TREE_ADDRESSABLE (t) = 1;
1817 *walk_subtrees = 0;
1820 return NULL_TREE;
1823 /* RTL expansion is not able to compile array references with variable
1824 offsets for arrays stored in single register. Discover such
1825 expressions and mark variables as addressable to avoid this
1826 scenario. */
1828 static void
1829 discover_nonconstant_array_refs (void)
1831 basic_block bb;
1832 block_stmt_iterator bsi;
1834 FOR_EACH_BB (bb)
1836 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1837 walk_tree (bsi_stmt_ptr (bsi), discover_nonconstant_array_refs_r,
1838 NULL , NULL);
1842 /* Translate the intermediate representation contained in the CFG
1843 from GIMPLE trees to RTL.
1845 We do conversion per basic block and preserve/update the tree CFG.
1846 This implies we have to do some magic as the CFG can simultaneously
1847 consist of basic blocks containing RTL and GIMPLE trees. This can
1848 confuse the CFG hooks, so be careful to not manipulate CFG during
1849 the expansion. */
1851 static unsigned int
1852 tree_expand_cfg (void)
1854 basic_block bb, init_block;
1855 sbitmap blocks;
1856 edge_iterator ei;
1857 edge e;
1859 /* Some backends want to know that we are expanding to RTL. */
1860 currently_expanding_to_rtl = 1;
1862 insn_locators_alloc ();
1863 if (!DECL_BUILT_IN (current_function_decl))
1864 set_curr_insn_source_location (DECL_SOURCE_LOCATION (current_function_decl));
1865 set_curr_insn_block (DECL_INITIAL (current_function_decl));
1866 prologue_locator = curr_insn_locator ();
1868 /* Make sure first insn is a note even if we don't want linenums.
1869 This makes sure the first insn will never be deleted.
1870 Also, final expects a note to appear there. */
1871 emit_note (NOTE_INSN_DELETED);
1873 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
1874 discover_nonconstant_array_refs ();
1876 /* Expand the variables recorded during gimple lowering. */
1877 expand_used_vars ();
1879 /* Honor stack protection warnings. */
1880 if (warn_stack_protect)
1882 if (current_function_calls_alloca)
1883 warning (OPT_Wstack_protector,
1884 "not protecting local variables: variable length buffer");
1885 if (has_short_buffer && !cfun->stack_protect_guard)
1886 warning (OPT_Wstack_protector,
1887 "not protecting function: no buffer at least %d bytes long",
1888 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
1891 /* Set up parameters and prepare for return, for the function. */
1892 expand_function_start (current_function_decl);
1894 /* If this function is `main', emit a call to `__main'
1895 to run global initializers, etc. */
1896 if (DECL_NAME (current_function_decl)
1897 && MAIN_NAME_P (DECL_NAME (current_function_decl))
1898 && DECL_FILE_SCOPE_P (current_function_decl))
1899 expand_main_function ();
1901 /* Initialize the stack_protect_guard field. This must happen after the
1902 call to __main (if any) so that the external decl is initialized. */
1903 if (cfun->stack_protect_guard)
1904 stack_protect_prologue ();
1906 /* Register rtl specific functions for cfg. */
1907 rtl_register_cfg_hooks ();
1909 init_block = construct_init_block ();
1911 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
1912 remaining edges in expand_gimple_basic_block. */
1913 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
1914 e->flags &= ~EDGE_EXECUTABLE;
1916 lab_rtx_for_bb = pointer_map_create ();
1917 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
1918 bb = expand_gimple_basic_block (bb);
1919 pointer_map_destroy (lab_rtx_for_bb);
1921 construct_exit_block ();
1922 set_curr_insn_block (DECL_INITIAL (current_function_decl));
1923 insn_locators_finalize ();
1925 /* We're done expanding trees to RTL. */
1926 currently_expanding_to_rtl = 0;
1928 /* Convert tree EH labels to RTL EH labels, and clean out any unreachable
1929 EH regions. */
1930 convert_from_eh_region_ranges ();
1932 rebuild_jump_labels (get_insns ());
1933 find_exception_handler_labels ();
1935 blocks = sbitmap_alloc (last_basic_block);
1936 sbitmap_ones (blocks);
1937 find_many_sub_basic_blocks (blocks);
1938 purge_all_dead_edges ();
1939 sbitmap_free (blocks);
1941 compact_blocks ();
1942 #ifdef ENABLE_CHECKING
1943 verify_flow_info ();
1944 #endif
1946 /* There's no need to defer outputting this function any more; we
1947 know we want to output it. */
1948 DECL_DEFER_OUTPUT (current_function_decl) = 0;
1950 /* Now that we're done expanding trees to RTL, we shouldn't have any
1951 more CONCATs anywhere. */
1952 generating_concat_p = 0;
1954 if (dump_file)
1956 fprintf (dump_file,
1957 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
1958 /* And the pass manager will dump RTL for us. */
1961 /* If we're emitting a nested function, make sure its parent gets
1962 emitted as well. Doing otherwise confuses debug info. */
1964 tree parent;
1965 for (parent = DECL_CONTEXT (current_function_decl);
1966 parent != NULL_TREE;
1967 parent = get_containing_scope (parent))
1968 if (TREE_CODE (parent) == FUNCTION_DECL)
1969 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
1972 /* We are now committed to emitting code for this function. Do any
1973 preparation, such as emitting abstract debug info for the inline
1974 before it gets mangled by optimization. */
1975 if (cgraph_function_possibly_inlined_p (current_function_decl))
1976 (*debug_hooks->outlining_inline_function) (current_function_decl);
1978 TREE_ASM_WRITTEN (current_function_decl) = 1;
1980 /* After expanding, the return labels are no longer needed. */
1981 return_label = NULL;
1982 naked_return_label = NULL;
1983 free_histograms ();
1984 /* Tag the blocks with a depth number so that change_scope can find
1985 the common parent easily. */
1986 set_block_levels (DECL_INITIAL (cfun->decl), 0);
1987 return 0;
1990 struct tree_opt_pass pass_expand =
1992 "expand", /* name */
1993 NULL, /* gate */
1994 tree_expand_cfg, /* execute */
1995 NULL, /* sub */
1996 NULL, /* next */
1997 0, /* static_pass_number */
1998 TV_EXPAND, /* tv_id */
1999 /* ??? If TER is enabled, we actually receive GENERIC. */
2000 PROP_gimple_leh | PROP_cfg, /* properties_required */
2001 PROP_rtl, /* properties_provided */
2002 PROP_trees, /* properties_destroyed */
2003 0, /* todo_flags_start */
2004 TODO_dump_func, /* todo_flags_finish */
2005 'r' /* letter */