PR c/21536
[official-gcc.git] / gcc / cfgexpand.c
blob4e4a8e208c3e36a5725cc86b337abb8ace6045ee
1 /* A pass for lowering trees to RTL.
2 Copyright (C) 2004, 2005 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 2, 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 COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "expr.h"
31 #include "langhooks.h"
32 #include "tree-flow.h"
33 #include "timevar.h"
34 #include "tree-dump.h"
35 #include "tree-pass.h"
36 #include "except.h"
37 #include "flags.h"
38 #include "diagnostic.h"
39 #include "toplev.h"
41 /* Verify that there is exactly single jump instruction since last and attach
42 REG_BR_PROB note specifying probability.
43 ??? We really ought to pass the probability down to RTL expanders and let it
44 re-distribute it when the conditional expands into multiple conditionals.
45 This is however difficult to do. */
46 static void
47 add_reg_br_prob_note (FILE *dump_file, rtx last, int probability)
49 if (profile_status == PROFILE_ABSENT)
50 return;
51 for (last = NEXT_INSN (last); last && NEXT_INSN (last); last = NEXT_INSN (last))
52 if (JUMP_P (last))
54 /* It is common to emit condjump-around-jump sequence when we don't know
55 how to reverse the conditional. Special case this. */
56 if (!any_condjump_p (last)
57 || !JUMP_P (NEXT_INSN (last))
58 || !simplejump_p (NEXT_INSN (last))
59 || !BARRIER_P (NEXT_INSN (NEXT_INSN (last)))
60 || !LABEL_P (NEXT_INSN (NEXT_INSN (NEXT_INSN (last))))
61 || NEXT_INSN (NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))))
62 goto failed;
63 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
64 REG_NOTES (last)
65 = gen_rtx_EXPR_LIST (REG_BR_PROB,
66 GEN_INT (REG_BR_PROB_BASE - probability),
67 REG_NOTES (last));
68 return;
70 if (!last || !JUMP_P (last) || !any_condjump_p (last))
71 goto failed;
72 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
73 REG_NOTES (last)
74 = gen_rtx_EXPR_LIST (REG_BR_PROB,
75 GEN_INT (probability), REG_NOTES (last));
76 return;
77 failed:
78 if (dump_file)
79 fprintf (dump_file, "Failed to add probability note\n");
83 #ifndef LOCAL_ALIGNMENT
84 #define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
85 #endif
87 #ifndef STACK_ALIGNMENT_NEEDED
88 #define STACK_ALIGNMENT_NEEDED 1
89 #endif
91 #ifdef FRAME_GROWS_DOWNWARD
92 # undef FRAME_GROWS_DOWNWARD
93 # define FRAME_GROWS_DOWNWARD 1
94 #else
95 # define FRAME_GROWS_DOWNWARD 0
96 #endif
99 /* This structure holds data relevant to one variable that will be
100 placed in a stack slot. */
101 struct stack_var
103 /* The Variable. */
104 tree decl;
106 /* The offset of the variable. During partitioning, this is the
107 offset relative to the partition. After partitioning, this
108 is relative to the stack frame. */
109 HOST_WIDE_INT offset;
111 /* Initially, the size of the variable. Later, the size of the partition,
112 if this variable becomes it's partition's representative. */
113 HOST_WIDE_INT size;
115 /* The *byte* alignment required for this variable. Or as, with the
116 size, the alignment for this partition. */
117 unsigned int alignb;
119 /* The partition representative. */
120 size_t representative;
122 /* The next stack variable in the partition, or EOC. */
123 size_t next;
126 #define EOC ((size_t)-1)
128 /* We have an array of such objects while deciding allocation. */
129 static struct stack_var *stack_vars;
130 static size_t stack_vars_alloc;
131 static size_t stack_vars_num;
133 /* An array of indicies such that stack_vars[stack_vars_sorted[i]].size
134 is non-decreasing. */
135 static size_t *stack_vars_sorted;
137 /* We have an interference graph between such objects. This graph
138 is lower triangular. */
139 static bool *stack_vars_conflict;
140 static size_t stack_vars_conflict_alloc;
142 /* The phase of the stack frame. This is the known misalignment of
143 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
144 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
145 static int frame_phase;
148 /* Discover the byte alignment to use for DECL. Ignore alignment
149 we can't do with expected alignment of the stack boundary. */
151 static unsigned int
152 get_decl_align_unit (tree decl)
154 unsigned int align;
156 align = DECL_ALIGN (decl);
157 align = LOCAL_ALIGNMENT (TREE_TYPE (decl), align);
158 if (align > PREFERRED_STACK_BOUNDARY)
159 align = PREFERRED_STACK_BOUNDARY;
160 if (cfun->stack_alignment_needed < align)
161 cfun->stack_alignment_needed = align;
163 return align / BITS_PER_UNIT;
166 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
167 Return the frame offset. */
169 static HOST_WIDE_INT
170 alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align)
172 HOST_WIDE_INT offset, new_frame_offset;
174 new_frame_offset = frame_offset;
175 if (FRAME_GROWS_DOWNWARD)
177 new_frame_offset -= size + frame_phase;
178 new_frame_offset &= -align;
179 new_frame_offset += frame_phase;
180 offset = new_frame_offset;
182 else
184 new_frame_offset -= frame_phase;
185 new_frame_offset += align - 1;
186 new_frame_offset &= -align;
187 new_frame_offset += frame_phase;
188 offset = new_frame_offset;
189 new_frame_offset += size;
191 frame_offset = new_frame_offset;
193 return offset;
196 /* Accumulate DECL into STACK_VARS. */
198 static void
199 add_stack_var (tree decl)
201 if (stack_vars_num >= stack_vars_alloc)
203 if (stack_vars_alloc)
204 stack_vars_alloc = stack_vars_alloc * 3 / 2;
205 else
206 stack_vars_alloc = 32;
207 stack_vars
208 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
210 stack_vars[stack_vars_num].decl = decl;
211 stack_vars[stack_vars_num].offset = 0;
212 stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
213 stack_vars[stack_vars_num].alignb = get_decl_align_unit (decl);
215 /* All variables are initially in their own partition. */
216 stack_vars[stack_vars_num].representative = stack_vars_num;
217 stack_vars[stack_vars_num].next = EOC;
219 /* Ensure that this decl doesn't get put onto the list twice. */
220 SET_DECL_RTL (decl, pc_rtx);
222 stack_vars_num++;
225 /* Compute the linear index of a lower-triangular coordinate (I, J). */
227 static size_t
228 triangular_index (size_t i, size_t j)
230 if (i < j)
232 size_t t;
233 t = i, i = j, j = t;
235 return (i * (i + 1)) / 2 + j;
238 /* Ensure that STACK_VARS_CONFLICT is large enough for N objects. */
240 static void
241 resize_stack_vars_conflict (size_t n)
243 size_t size = triangular_index (n-1, n-1) + 1;
245 if (size <= stack_vars_conflict_alloc)
246 return;
248 stack_vars_conflict = XRESIZEVEC (bool, stack_vars_conflict, size);
249 memset (stack_vars_conflict + stack_vars_conflict_alloc, 0,
250 (size - stack_vars_conflict_alloc) * sizeof (bool));
251 stack_vars_conflict_alloc = size;
254 /* Make the decls associated with luid's X and Y conflict. */
256 static void
257 add_stack_var_conflict (size_t x, size_t y)
259 size_t index = triangular_index (x, y);
260 gcc_assert (index < stack_vars_conflict_alloc);
261 stack_vars_conflict[index] = true;
264 /* Check whether the decls associated with luid's X and Y conflict. */
266 static bool
267 stack_var_conflict_p (size_t x, size_t y)
269 size_t index = triangular_index (x, y);
270 gcc_assert (index < stack_vars_conflict_alloc);
271 return stack_vars_conflict[index];
274 /* A subroutine of expand_used_vars. If two variables X and Y have alias
275 sets that do not conflict, then do add a conflict for these variables
276 in the interference graph. We also have to mind MEM_IN_STRUCT_P and
277 MEM_SCALAR_P. */
279 static void
280 add_alias_set_conflicts (void)
282 size_t i, j, n = stack_vars_num;
284 for (i = 0; i < n; ++i)
286 bool aggr_i = AGGREGATE_TYPE_P (TREE_TYPE (stack_vars[i].decl));
287 HOST_WIDE_INT set_i = get_alias_set (stack_vars[i].decl);
289 for (j = 0; j < i; ++j)
291 bool aggr_j = AGGREGATE_TYPE_P (TREE_TYPE (stack_vars[j].decl));
292 HOST_WIDE_INT set_j = get_alias_set (stack_vars[j].decl);
293 if (aggr_i != aggr_j || !alias_sets_conflict_p (set_i, set_j))
294 add_stack_var_conflict (i, j);
299 /* A subroutine of partition_stack_vars. A comparison function for qsort,
300 sorting an array of indicies by the size of the object. */
302 static int
303 stack_var_size_cmp (const void *a, const void *b)
305 HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size;
306 HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size;
308 if (sa < sb)
309 return -1;
310 if (sa > sb)
311 return 1;
312 return 0;
315 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
316 partitioning algorithm. Partitions A and B are known to be non-conflicting.
317 Merge them into a single partition A.
319 At the same time, add OFFSET to all variables in partition B. At the end
320 of the partitioning process we've have a nice block easy to lay out within
321 the stack frame. */
323 static void
324 union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset)
326 size_t i, last;
328 /* Update each element of partition B with the given offset,
329 and merge them into partition A. */
330 for (last = i = b; i != EOC; last = i, i = stack_vars[i].next)
332 stack_vars[i].offset += offset;
333 stack_vars[i].representative = a;
335 stack_vars[last].next = stack_vars[a].next;
336 stack_vars[a].next = b;
338 /* Update the required alignment of partition A to account for B. */
339 if (stack_vars[a].alignb < stack_vars[b].alignb)
340 stack_vars[a].alignb = stack_vars[b].alignb;
342 /* Update the interference graph and merge the conflicts. */
343 for (last = stack_vars_num, i = 0; i < last; ++i)
344 if (stack_var_conflict_p (b, i))
345 add_stack_var_conflict (a, i);
348 /* A subroutine of expand_used_vars. Binpack the variables into
349 partitions constrained by the interference graph. The overall
350 algorithm used is as follows:
352 Sort the objects by size.
353 For each object A {
354 S = size(A)
355 O = 0
356 loop {
357 Look for the largest non-conflicting object B with size <= S.
358 UNION (A, B)
359 offset(B) = O
360 O += size(B)
361 S -= size(B)
366 static void
367 partition_stack_vars (void)
369 size_t si, sj, n = stack_vars_num;
371 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
372 for (si = 0; si < n; ++si)
373 stack_vars_sorted[si] = si;
375 if (n == 1)
376 return;
378 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp);
380 /* Special case: detect when all variables conflict, and thus we can't
381 do anything during the partitioning loop. It isn't uncommon (with
382 C code at least) to declare all variables at the top of the function,
383 and if we're not inlining, then all variables will be in the same scope.
384 Take advantage of very fast libc routines for this scan. */
385 gcc_assert (sizeof(bool) == sizeof(char));
386 if (memchr (stack_vars_conflict, false, stack_vars_conflict_alloc) == NULL)
387 return;
389 for (si = 0; si < n; ++si)
391 size_t i = stack_vars_sorted[si];
392 HOST_WIDE_INT isize = stack_vars[i].size;
393 HOST_WIDE_INT offset = 0;
395 for (sj = si; sj-- > 0; )
397 size_t j = stack_vars_sorted[sj];
398 HOST_WIDE_INT jsize = stack_vars[j].size;
399 unsigned int jalign = stack_vars[j].alignb;
401 /* Ignore objects that aren't partition representatives. */
402 if (stack_vars[j].representative != j)
403 continue;
405 /* Ignore objects too large for the remaining space. */
406 if (isize < jsize)
407 continue;
409 /* Ignore conflicting objects. */
410 if (stack_var_conflict_p (i, j))
411 continue;
413 /* Refine the remaining space check to include alignment. */
414 if (offset & (jalign - 1))
416 HOST_WIDE_INT toff = offset;
417 toff += jalign - 1;
418 toff &= -(HOST_WIDE_INT)jalign;
419 if (isize - (toff - offset) < jsize)
420 continue;
422 isize -= toff - offset;
423 offset = toff;
426 /* UNION the objects, placing J at OFFSET. */
427 union_stack_vars (i, j, offset);
429 isize -= jsize;
430 if (isize == 0)
431 break;
436 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
438 static void
439 dump_stack_var_partition (void)
441 size_t si, i, j, n = stack_vars_num;
443 for (si = 0; si < n; ++si)
445 i = stack_vars_sorted[si];
447 /* Skip variables that aren't partition representatives, for now. */
448 if (stack_vars[i].representative != i)
449 continue;
451 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
452 " align %u\n", (unsigned long) i, stack_vars[i].size,
453 stack_vars[i].alignb);
455 for (j = i; j != EOC; j = stack_vars[j].next)
457 fputc ('\t', dump_file);
458 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
459 fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n",
460 stack_vars[i].offset);
465 /* Assign rtl to DECL at frame offset OFFSET. */
467 static void
468 expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset)
470 HOST_WIDE_INT align;
471 rtx x;
473 /* If this fails, we've overflowed the stack frame. Error nicely? */
474 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
476 x = plus_constant (virtual_stack_vars_rtx, offset);
477 x = gen_rtx_MEM (DECL_MODE (decl), x);
479 /* Set alignment we actually gave this decl. */
480 offset -= frame_phase;
481 align = offset & -offset;
482 align *= BITS_PER_UNIT;
483 if (align > STACK_BOUNDARY || align == 0)
484 align = STACK_BOUNDARY;
485 DECL_ALIGN (decl) = align;
486 DECL_USER_ALIGN (decl) = 0;
488 set_mem_attributes (x, decl, true);
489 SET_DECL_RTL (decl, x);
492 /* A subroutine of expand_used_vars. Give each partition representative
493 a unique location within the stack frame. Update each partition member
494 with that location. */
496 static void
497 expand_stack_vars (void)
499 size_t si, i, j, n = stack_vars_num;
501 for (si = 0; si < n; ++si)
503 HOST_WIDE_INT offset;
505 i = stack_vars_sorted[si];
507 /* Skip variables that aren't partition representatives, for now. */
508 if (stack_vars[i].representative != i)
509 continue;
511 offset = alloc_stack_frame_space (stack_vars[i].size,
512 stack_vars[i].alignb);
514 /* Create rtl for each variable based on their location within the
515 partition. */
516 for (j = i; j != EOC; j = stack_vars[j].next)
517 expand_one_stack_var_at (stack_vars[j].decl,
518 stack_vars[j].offset + offset);
522 /* A subroutine of expand_one_var. Called to immediately assign rtl
523 to a variable to be allocated in the stack frame. */
525 static void
526 expand_one_stack_var (tree var)
528 HOST_WIDE_INT size, offset, align;
530 size = tree_low_cst (DECL_SIZE_UNIT (var), 1);
531 align = get_decl_align_unit (var);
532 offset = alloc_stack_frame_space (size, align);
534 expand_one_stack_var_at (var, offset);
537 /* A subroutine of expand_one_var. Called to assign rtl
538 to a TREE_STATIC VAR_DECL. */
540 static void
541 expand_one_static_var (tree var)
543 /* If this is an inlined copy of a static local variable,
544 look up the original. */
545 var = DECL_ORIGIN (var);
547 /* If we've already processed this variable because of that, do nothing. */
548 if (TREE_ASM_WRITTEN (var))
549 return;
551 /* Give the front end a chance to do whatever. In practice, this is
552 resolving duplicate names for IMA in C. */
553 if (lang_hooks.expand_decl (var))
554 return;
556 /* Otherwise, just emit the variable. */
557 rest_of_decl_compilation (var, 0, 0);
560 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
561 that will reside in a hard register. */
563 static void
564 expand_one_hard_reg_var (tree var)
566 rest_of_decl_compilation (var, 0, 0);
569 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
570 that will reside in a pseudo register. */
572 static void
573 expand_one_register_var (tree var)
575 tree type = TREE_TYPE (var);
576 int unsignedp = TYPE_UNSIGNED (type);
577 enum machine_mode reg_mode
578 = promote_mode (type, DECL_MODE (var), &unsignedp, 0);
579 rtx x = gen_reg_rtx (reg_mode);
581 SET_DECL_RTL (var, x);
583 /* Note if the object is a user variable. */
584 if (!DECL_ARTIFICIAL (var))
586 mark_user_reg (x);
588 /* Trust user variables which have a pointer type to really
589 be pointers. Do not trust compiler generated temporaries
590 as our type system is totally busted as it relates to
591 pointer arithmetic which translates into lots of compiler
592 generated objects with pointer types, but which are not really
593 pointers. */
594 if (POINTER_TYPE_P (type))
595 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
599 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
600 has some associated error, e.g. its type is error-mark. We just need
601 to pick something that won't crash the rest of the compiler. */
603 static void
604 expand_one_error_var (tree var)
606 enum machine_mode mode = DECL_MODE (var);
607 rtx x;
609 if (mode == BLKmode)
610 x = gen_rtx_MEM (BLKmode, const0_rtx);
611 else if (mode == VOIDmode)
612 x = const0_rtx;
613 else
614 x = gen_reg_rtx (mode);
616 SET_DECL_RTL (var, x);
619 /* A subroutine of expand_one_var. VAR is a variable that will be
620 allocated to the local stack frame. Return true if we wish to
621 add VAR to STACK_VARS so that it will be coalesced with other
622 variables. Return false to allocate VAR immediately.
624 This function is used to reduce the number of variables considered
625 for coalescing, which reduces the size of the quadratic problem. */
627 static bool
628 defer_stack_allocation (tree var, bool toplevel)
630 /* Variables in the outermost scope automatically conflict with
631 every other variable. The only reason to want to defer them
632 at all is that, after sorting, we can more efficiently pack
633 small variables in the stack frame. Continue to defer at -O2. */
634 if (toplevel && optimize < 2)
635 return false;
637 /* Without optimization, *most* variables are allocated from the
638 stack, which makes the quadratic problem large exactly when we
639 want compilation to proceed as quickly as possible. On the
640 other hand, we don't want the function's stack frame size to
641 get completely out of hand. So we avoid adding scalars and
642 "small" aggregates to the list at all. */
643 if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32)
644 return false;
646 return true;
649 /* A subroutine of expand_used_vars. Expand one variable according to
650 its flavor. Variables to be placed on the stack are not actually
651 expanded yet, merely recorded. */
653 static void
654 expand_one_var (tree var, bool toplevel)
656 if (TREE_CODE (var) != VAR_DECL)
657 lang_hooks.expand_decl (var);
658 else if (DECL_EXTERNAL (var))
660 else if (DECL_VALUE_EXPR (var))
662 else if (TREE_STATIC (var))
663 expand_one_static_var (var);
664 else if (DECL_RTL_SET_P (var))
666 else if (TREE_TYPE (var) == error_mark_node)
667 expand_one_error_var (var);
668 else if (DECL_HARD_REGISTER (var))
669 expand_one_hard_reg_var (var);
670 else if (use_register_for_decl (var))
671 expand_one_register_var (var);
672 else if (defer_stack_allocation (var, toplevel))
673 add_stack_var (var);
674 else
675 expand_one_stack_var (var);
678 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
679 expanding variables. Those variables that can be put into registers
680 are allocated pseudos; those that can't are put on the stack.
682 TOPLEVEL is true if this is the outermost BLOCK. */
684 static void
685 expand_used_vars_for_block (tree block, bool toplevel)
687 size_t i, j, old_sv_num, this_sv_num, new_sv_num;
688 tree t;
690 old_sv_num = toplevel ? 0 : stack_vars_num;
692 /* Expand all variables at this level. */
693 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
694 if (TREE_USED (t))
695 expand_one_var (t, toplevel);
697 this_sv_num = stack_vars_num;
699 /* Expand all variables at containing levels. */
700 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
701 expand_used_vars_for_block (t, false);
703 /* Since we do not track exact variable lifetimes (which is not even
704 possible for varibles whose address escapes), we mirror the block
705 tree in the interference graph. Here we cause all variables at this
706 level, and all sublevels, to conflict. Do make certain that a
707 variable conflicts with itself. */
708 if (old_sv_num < this_sv_num)
710 new_sv_num = stack_vars_num;
711 resize_stack_vars_conflict (new_sv_num);
713 for (i = old_sv_num; i < new_sv_num; ++i)
714 for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
715 add_stack_var_conflict (i, j);
719 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
720 and clear TREE_USED on all local variables. */
722 static void
723 clear_tree_used (tree block)
725 tree t;
727 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
728 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
729 TREE_USED (t) = 0;
731 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
732 clear_tree_used (t);
735 /* Expand all variables used in the function. */
737 static void
738 expand_used_vars (void)
740 tree t, outer_block = DECL_INITIAL (current_function_decl);
742 /* Compute the phase of the stack frame for this function. */
744 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
745 int off = STARTING_FRAME_OFFSET % align;
746 frame_phase = off ? align - off : 0;
749 /* Set TREE_USED on all variables in the unexpanded_var_list. */
750 for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
751 TREE_USED (TREE_VALUE (t)) = 1;
753 /* Clear TREE_USED on all variables associated with a block scope. */
754 clear_tree_used (outer_block);
756 /* At this point all variables on the unexpanded_var_list with TREE_USED
757 set are not associated with any block scope. Lay them out. */
758 for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
760 tree var = TREE_VALUE (t);
761 bool expand_now = false;
763 /* We didn't set a block for static or extern because it's hard
764 to tell the difference between a global variable (re)declared
765 in a local scope, and one that's really declared there to
766 begin with. And it doesn't really matter much, since we're
767 not giving them stack space. Expand them now. */
768 if (TREE_STATIC (var) || DECL_EXTERNAL (var))
769 expand_now = true;
771 /* Any variable that could have been hoisted into an SSA_NAME
772 will have been propagated anywhere the optimizers chose,
773 i.e. not confined to their original block. Allocate them
774 as if they were defined in the outermost scope. */
775 else if (is_gimple_reg (var))
776 expand_now = true;
778 /* If the variable is not associated with any block, then it
779 was created by the optimizers, and could be live anywhere
780 in the function. */
781 else if (TREE_USED (var))
782 expand_now = true;
784 /* Finally, mark all variables on the list as used. We'll use
785 this in a moment when we expand those associated with scopes. */
786 TREE_USED (var) = 1;
788 if (expand_now)
789 expand_one_var (var, true);
791 cfun->unexpanded_var_list = NULL_TREE;
793 /* At this point, all variables within the block tree with TREE_USED
794 set are actually used by the optimized function. Lay them out. */
795 expand_used_vars_for_block (outer_block, true);
797 if (stack_vars_num > 0)
799 /* Due to the way alias sets work, no variables with non-conflicting
800 alias sets may be assigned the same address. Add conflicts to
801 reflect this. */
802 add_alias_set_conflicts ();
804 /* Now that we have collected all stack variables, and have computed a
805 minimal interference graph, attempt to save some stack space. */
806 partition_stack_vars ();
807 if (dump_file)
808 dump_stack_var_partition ();
810 /* Assign rtl to each variable based on these partitions. */
811 expand_stack_vars ();
813 /* Free up stack variable graph data. */
814 XDELETEVEC (stack_vars);
815 XDELETEVEC (stack_vars_sorted);
816 XDELETEVEC (stack_vars_conflict);
817 stack_vars = NULL;
818 stack_vars_alloc = stack_vars_num = 0;
819 stack_vars_conflict = NULL;
820 stack_vars_conflict_alloc = 0;
823 /* If the target requires that FRAME_OFFSET be aligned, do it. */
824 if (STACK_ALIGNMENT_NEEDED)
826 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
827 if (!FRAME_GROWS_DOWNWARD)
828 frame_offset += align - 1;
829 frame_offset &= -align;
834 /* If we need to produce a detailed dump, print the tree representation
835 for STMT to the dump file. SINCE is the last RTX after which the RTL
836 generated for STMT should have been appended. */
838 static void
839 maybe_dump_rtl_for_tree_stmt (tree stmt, rtx since)
841 if (dump_file && (dump_flags & TDF_DETAILS))
843 fprintf (dump_file, "\n;; ");
844 print_generic_expr (dump_file, stmt, TDF_SLIM);
845 fprintf (dump_file, "\n");
847 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
851 /* A subroutine of expand_gimple_basic_block. Expand one COND_EXPR.
852 Returns a new basic block if we've terminated the current basic
853 block and created a new one. */
855 static basic_block
856 expand_gimple_cond_expr (basic_block bb, tree stmt)
858 basic_block new_bb, dest;
859 edge new_edge;
860 edge true_edge;
861 edge false_edge;
862 tree pred = COND_EXPR_COND (stmt);
863 tree then_exp = COND_EXPR_THEN (stmt);
864 tree else_exp = COND_EXPR_ELSE (stmt);
865 rtx last2, last;
867 last2 = last = get_last_insn ();
869 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
870 if (EXPR_LOCUS (stmt))
872 emit_line_note (*(EXPR_LOCUS (stmt)));
873 record_block_change (TREE_BLOCK (stmt));
876 /* These flags have no purpose in RTL land. */
877 true_edge->flags &= ~EDGE_TRUE_VALUE;
878 false_edge->flags &= ~EDGE_FALSE_VALUE;
880 /* We can either have a pure conditional jump with one fallthru edge or
881 two-way jump that needs to be decomposed into two basic blocks. */
882 if (TREE_CODE (then_exp) == GOTO_EXPR && IS_EMPTY_STMT (else_exp))
884 jumpif (pred, label_rtx (GOTO_DESTINATION (then_exp)));
885 add_reg_br_prob_note (dump_file, last, true_edge->probability);
886 maybe_dump_rtl_for_tree_stmt (stmt, last);
887 if (EXPR_LOCUS (then_exp))
888 emit_line_note (*(EXPR_LOCUS (then_exp)));
889 return NULL;
891 if (TREE_CODE (else_exp) == GOTO_EXPR && IS_EMPTY_STMT (then_exp))
893 jumpifnot (pred, label_rtx (GOTO_DESTINATION (else_exp)));
894 add_reg_br_prob_note (dump_file, last, false_edge->probability);
895 maybe_dump_rtl_for_tree_stmt (stmt, last);
896 if (EXPR_LOCUS (else_exp))
897 emit_line_note (*(EXPR_LOCUS (else_exp)));
898 return NULL;
900 gcc_assert (TREE_CODE (then_exp) == GOTO_EXPR
901 && TREE_CODE (else_exp) == GOTO_EXPR);
903 jumpif (pred, label_rtx (GOTO_DESTINATION (then_exp)));
904 add_reg_br_prob_note (dump_file, last, true_edge->probability);
905 last = get_last_insn ();
906 expand_expr (else_exp, const0_rtx, VOIDmode, 0);
908 BB_END (bb) = last;
909 if (BARRIER_P (BB_END (bb)))
910 BB_END (bb) = PREV_INSN (BB_END (bb));
911 update_bb_for_insn (bb);
913 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
914 dest = false_edge->dest;
915 redirect_edge_succ (false_edge, new_bb);
916 false_edge->flags |= EDGE_FALLTHRU;
917 new_bb->count = false_edge->count;
918 new_bb->frequency = EDGE_FREQUENCY (false_edge);
919 new_edge = make_edge (new_bb, dest, 0);
920 new_edge->probability = REG_BR_PROB_BASE;
921 new_edge->count = new_bb->count;
922 if (BARRIER_P (BB_END (new_bb)))
923 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
924 update_bb_for_insn (new_bb);
926 maybe_dump_rtl_for_tree_stmt (stmt, last2);
928 if (EXPR_LOCUS (else_exp))
929 emit_line_note (*(EXPR_LOCUS (else_exp)));
931 return new_bb;
934 /* A subroutine of expand_gimple_basic_block. Expand one CALL_EXPR
935 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
936 generated a tail call (something that might be denied by the ABI
937 rules governing the call; see calls.c).
939 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
940 can still reach the rest of BB. The case here is __builtin_sqrt,
941 where the NaN result goes through the external function (with a
942 tailcall) and the normal result happens via a sqrt instruction. */
944 static basic_block
945 expand_gimple_tailcall (basic_block bb, tree stmt, bool *can_fallthru)
947 rtx last2, last;
948 edge e;
949 edge_iterator ei;
950 int probability;
951 gcov_type count;
953 last2 = last = get_last_insn ();
955 expand_expr_stmt (stmt);
957 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
958 if (CALL_P (last) && SIBLING_CALL_P (last))
959 goto found;
961 maybe_dump_rtl_for_tree_stmt (stmt, last2);
963 *can_fallthru = true;
964 return NULL;
966 found:
967 /* ??? Wouldn't it be better to just reset any pending stack adjust?
968 Any instructions emitted here are about to be deleted. */
969 do_pending_stack_adjust ();
971 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
972 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
973 EH or abnormal edges, we shouldn't have created a tail call in
974 the first place. So it seems to me we should just be removing
975 all edges here, or redirecting the existing fallthru edge to
976 the exit block. */
978 probability = 0;
979 count = 0;
981 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
983 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
985 if (e->dest != EXIT_BLOCK_PTR)
987 e->dest->count -= e->count;
988 e->dest->frequency -= EDGE_FREQUENCY (e);
989 if (e->dest->count < 0)
990 e->dest->count = 0;
991 if (e->dest->frequency < 0)
992 e->dest->frequency = 0;
994 count += e->count;
995 probability += e->probability;
996 remove_edge (e);
998 else
999 ei_next (&ei);
1002 /* This is somewhat ugly: the call_expr expander often emits instructions
1003 after the sibcall (to perform the function return). These confuse the
1004 find_many_sub_basic_blocks code, so we need to get rid of these. */
1005 last = NEXT_INSN (last);
1006 gcc_assert (BARRIER_P (last));
1008 *can_fallthru = false;
1009 while (NEXT_INSN (last))
1011 /* For instance an sqrt builtin expander expands if with
1012 sibcall in the then and label for `else`. */
1013 if (LABEL_P (NEXT_INSN (last)))
1015 *can_fallthru = true;
1016 break;
1018 delete_insn (NEXT_INSN (last));
1021 e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
1022 e->probability += probability;
1023 e->count += count;
1024 BB_END (bb) = last;
1025 update_bb_for_insn (bb);
1027 if (NEXT_INSN (last))
1029 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1031 last = BB_END (bb);
1032 if (BARRIER_P (last))
1033 BB_END (bb) = PREV_INSN (last);
1036 maybe_dump_rtl_for_tree_stmt (stmt, last2);
1038 return bb;
1041 /* Expand basic block BB from GIMPLE trees to RTL. */
1043 static basic_block
1044 expand_gimple_basic_block (basic_block bb, FILE * dump_file)
1046 block_stmt_iterator bsi = bsi_start (bb);
1047 tree stmt = NULL;
1048 rtx note, last;
1049 edge e;
1050 edge_iterator ei;
1052 if (dump_file)
1054 fprintf (dump_file,
1055 "\n;; Generating RTL for tree basic block %d\n",
1056 bb->index);
1059 if (!bsi_end_p (bsi))
1060 stmt = bsi_stmt (bsi);
1062 if (stmt && TREE_CODE (stmt) == LABEL_EXPR)
1064 last = get_last_insn ();
1066 expand_expr_stmt (stmt);
1068 /* Java emits line number notes in the top of labels.
1069 ??? Make this go away once line number notes are obsoleted. */
1070 BB_HEAD (bb) = NEXT_INSN (last);
1071 if (NOTE_P (BB_HEAD (bb)))
1072 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
1073 bsi_next (&bsi);
1074 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
1076 maybe_dump_rtl_for_tree_stmt (stmt, last);
1078 else
1079 note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
1081 NOTE_BASIC_BLOCK (note) = bb;
1083 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1085 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
1086 e->flags &= ~EDGE_EXECUTABLE;
1088 /* At the moment not all abnormal edges match the RTL representation.
1089 It is safe to remove them here as find_many_sub_basic_blocks will
1090 rediscover them. In the future we should get this fixed properly. */
1091 if (e->flags & EDGE_ABNORMAL)
1092 remove_edge (e);
1093 else
1094 ei_next (&ei);
1097 for (; !bsi_end_p (bsi); bsi_next (&bsi))
1099 tree stmt = bsi_stmt (bsi);
1100 basic_block new_bb;
1102 if (!stmt)
1103 continue;
1105 /* Expand this statement, then evaluate the resulting RTL and
1106 fixup the CFG accordingly. */
1107 if (TREE_CODE (stmt) == COND_EXPR)
1109 new_bb = expand_gimple_cond_expr (bb, stmt);
1110 if (new_bb)
1111 return new_bb;
1113 else
1115 tree call = get_call_expr_in (stmt);
1116 if (call && CALL_EXPR_TAILCALL (call))
1118 bool can_fallthru;
1119 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
1120 if (new_bb)
1122 if (can_fallthru)
1123 bb = new_bb;
1124 else
1125 return new_bb;
1128 else
1130 last = get_last_insn ();
1131 expand_expr_stmt (stmt);
1132 maybe_dump_rtl_for_tree_stmt (stmt, last);
1137 do_pending_stack_adjust ();
1139 /* Find the block tail. The last insn in the block is the insn
1140 before a barrier and/or table jump insn. */
1141 last = get_last_insn ();
1142 if (BARRIER_P (last))
1143 last = PREV_INSN (last);
1144 if (JUMP_TABLE_DATA_P (last))
1145 last = PREV_INSN (PREV_INSN (last));
1146 BB_END (bb) = last;
1148 update_bb_for_insn (bb);
1150 return bb;
1154 /* Create a basic block for initialization code. */
1156 static basic_block
1157 construct_init_block (void)
1159 basic_block init_block, first_block;
1160 edge e = NULL;
1161 int flags;
1163 /* Multiple entry points not supported yet. */
1164 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
1166 e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
1168 /* When entry edge points to first basic block, we don't need jump,
1169 otherwise we have to jump into proper target. */
1170 if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
1172 tree label = tree_block_label (e->dest);
1174 emit_jump (label_rtx (label));
1175 flags = 0;
1177 else
1178 flags = EDGE_FALLTHRU;
1180 init_block = create_basic_block (NEXT_INSN (get_insns ()),
1181 get_last_insn (),
1182 ENTRY_BLOCK_PTR);
1183 init_block->frequency = ENTRY_BLOCK_PTR->frequency;
1184 init_block->count = ENTRY_BLOCK_PTR->count;
1185 if (e)
1187 first_block = e->dest;
1188 redirect_edge_succ (e, init_block);
1189 e = make_edge (init_block, first_block, flags);
1191 else
1192 e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1193 e->probability = REG_BR_PROB_BASE;
1194 e->count = ENTRY_BLOCK_PTR->count;
1196 update_bb_for_insn (init_block);
1197 return init_block;
1201 /* Create a block containing landing pads and similar stuff. */
1203 static void
1204 construct_exit_block (void)
1206 rtx head = get_last_insn ();
1207 rtx end;
1208 basic_block exit_block;
1209 edge e, e2;
1210 unsigned ix;
1211 edge_iterator ei;
1213 /* Make sure the locus is set to the end of the function, so that
1214 epilogue line numbers and warnings are set properly. */
1215 #ifdef USE_MAPPED_LOCATION
1216 if (cfun->function_end_locus != UNKNOWN_LOCATION)
1217 #else
1218 if (cfun->function_end_locus.file)
1219 #endif
1220 input_location = cfun->function_end_locus;
1222 /* The following insns belong to the top scope. */
1223 record_block_change (DECL_INITIAL (current_function_decl));
1225 /* Generate rtl for function exit. */
1226 expand_function_end ();
1228 end = get_last_insn ();
1229 if (head == end)
1230 return;
1231 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
1232 head = NEXT_INSN (head);
1233 exit_block = create_basic_block (NEXT_INSN (head), end,
1234 EXIT_BLOCK_PTR->prev_bb);
1235 exit_block->frequency = EXIT_BLOCK_PTR->frequency;
1236 exit_block->count = EXIT_BLOCK_PTR->count;
1238 ix = 0;
1239 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
1241 e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
1242 if (!(e->flags & EDGE_ABNORMAL))
1243 redirect_edge_succ (e, exit_block);
1244 else
1245 ix++;
1248 e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1249 e->probability = REG_BR_PROB_BASE;
1250 e->count = EXIT_BLOCK_PTR->count;
1251 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
1252 if (e2 != e)
1254 e->count -= e2->count;
1255 exit_block->count -= e2->count;
1256 exit_block->frequency -= EDGE_FREQUENCY (e2);
1258 if (e->count < 0)
1259 e->count = 0;
1260 if (exit_block->count < 0)
1261 exit_block->count = 0;
1262 if (exit_block->frequency < 0)
1263 exit_block->frequency = 0;
1264 update_bb_for_insn (exit_block);
1267 /* Translate the intermediate representation contained in the CFG
1268 from GIMPLE trees to RTL.
1270 We do conversion per basic block and preserve/update the tree CFG.
1271 This implies we have to do some magic as the CFG can simultaneously
1272 consist of basic blocks containing RTL and GIMPLE trees. This can
1273 confuse the CFG hooks, so be careful to not manipulate CFG during
1274 the expansion. */
1276 static void
1277 tree_expand_cfg (void)
1279 basic_block bb, init_block;
1280 sbitmap blocks;
1282 /* Some backends want to know that we are expanding to RTL. */
1283 currently_expanding_to_rtl = 1;
1285 /* Prepare the rtl middle end to start recording block changes. */
1286 reset_block_changes ();
1288 /* Expand the variables recorded during gimple lowering. */
1289 expand_used_vars ();
1291 /* Set up parameters and prepare for return, for the function. */
1292 expand_function_start (current_function_decl);
1294 /* If this function is `main', emit a call to `__main'
1295 to run global initializers, etc. */
1296 if (DECL_NAME (current_function_decl)
1297 && MAIN_NAME_P (DECL_NAME (current_function_decl))
1298 && DECL_FILE_SCOPE_P (current_function_decl))
1299 expand_main_function ();
1301 /* Register rtl specific functions for cfg. */
1302 rtl_register_cfg_hooks ();
1304 init_block = construct_init_block ();
1306 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
1307 bb = expand_gimple_basic_block (bb, dump_file);
1309 construct_exit_block ();
1311 /* We're done expanding trees to RTL. */
1312 currently_expanding_to_rtl = 0;
1314 /* Convert tree EH labels to RTL EH labels, and clean out any unreachable
1315 EH regions. */
1316 convert_from_eh_region_ranges ();
1318 rebuild_jump_labels (get_insns ());
1319 find_exception_handler_labels ();
1321 blocks = sbitmap_alloc (last_basic_block);
1322 sbitmap_ones (blocks);
1323 find_many_sub_basic_blocks (blocks);
1324 purge_all_dead_edges ();
1325 sbitmap_free (blocks);
1327 compact_blocks ();
1328 #ifdef ENABLE_CHECKING
1329 verify_flow_info();
1330 #endif
1332 /* There's no need to defer outputting this function any more; we
1333 know we want to output it. */
1334 DECL_DEFER_OUTPUT (current_function_decl) = 0;
1336 /* Now that we're done expanding trees to RTL, we shouldn't have any
1337 more CONCATs anywhere. */
1338 generating_concat_p = 0;
1340 finalize_block_changes ();
1342 if (dump_file)
1344 fprintf (dump_file,
1345 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
1346 /* And the pass manager will dump RTL for us. */
1350 struct tree_opt_pass pass_expand =
1352 "expand", /* name */
1353 NULL, /* gate */
1354 tree_expand_cfg, /* execute */
1355 NULL, /* sub */
1356 NULL, /* next */
1357 0, /* static_pass_number */
1358 TV_EXPAND, /* tv_id */
1359 /* ??? If TER is enabled, we actually receive GENERIC. */
1360 PROP_gimple_leh | PROP_cfg, /* properties_required */
1361 PROP_rtl, /* properties_provided */
1362 PROP_gimple_leh, /* properties_destroyed */
1363 0, /* todo_flags_start */
1364 0, /* todo_flags_finish */
1365 'r' /* letter */