Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / gcc / cfgexpand.c
blobc73bba7d9a9c0cf25366c5e8b15c07d8e600f5c8
1 /* A pass for lowering trees to RTL.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "expr.h"
31 #include "langhooks.h"
32 #include "tree-flow.h"
33 #include "timevar.h"
34 #include "tree-dump.h"
35 #include "tree-pass.h"
36 #include "except.h"
37 #include "flags.h"
38 #include "diagnostic.h"
39 #include "toplev.h"
40 #include "debug.h"
41 #include "params.h"
42 #include "tree-inline.h"
43 #include "value-prof.h"
44 #include "target.h"
47 /* Return an expression tree corresponding to the RHS of GIMPLE
48 statement STMT. */
50 tree
51 gimple_assign_rhs_to_tree (gimple stmt)
53 tree t;
54 enum gimple_rhs_class grhs_class;
56 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
58 if (grhs_class == GIMPLE_BINARY_RHS)
59 t = build2 (gimple_assign_rhs_code (stmt),
60 TREE_TYPE (gimple_assign_lhs (stmt)),
61 gimple_assign_rhs1 (stmt),
62 gimple_assign_rhs2 (stmt));
63 else if (grhs_class == GIMPLE_UNARY_RHS)
64 t = build1 (gimple_assign_rhs_code (stmt),
65 TREE_TYPE (gimple_assign_lhs (stmt)),
66 gimple_assign_rhs1 (stmt));
67 else if (grhs_class == GIMPLE_SINGLE_RHS)
68 t = gimple_assign_rhs1 (stmt);
69 else
70 gcc_unreachable ();
72 return t;
75 /* Return an expression tree corresponding to the PREDICATE of GIMPLE_COND
76 statement STMT. */
78 static tree
79 gimple_cond_pred_to_tree (gimple stmt)
81 return build2 (gimple_cond_code (stmt), boolean_type_node,
82 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
85 /* Helper for gimple_to_tree. Set EXPR_LOCATION for every expression
86 inside *TP. DATA is the location to set. */
88 static tree
89 set_expr_location_r (tree *tp, int *ws ATTRIBUTE_UNUSED, void *data)
91 location_t *loc = (location_t *) data;
92 if (EXPR_P (*tp))
93 SET_EXPR_LOCATION (*tp, *loc);
95 return NULL_TREE;
99 /* RTL expansion has traditionally been done on trees, so the
100 transition to doing it on GIMPLE tuples is very invasive to the RTL
101 expander. To facilitate the transition, this function takes a
102 GIMPLE tuple STMT and returns the same statement in the form of a
103 tree. */
105 static tree
106 gimple_to_tree (gimple stmt)
108 tree t;
109 int rn;
110 tree_ann_common_t ann;
111 location_t loc;
113 switch (gimple_code (stmt))
115 case GIMPLE_ASSIGN:
117 tree lhs = gimple_assign_lhs (stmt);
119 t = gimple_assign_rhs_to_tree (stmt);
120 t = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, t);
121 if (gimple_assign_nontemporal_move_p (stmt))
122 MOVE_NONTEMPORAL (t) = true;
124 break;
126 case GIMPLE_COND:
127 t = gimple_cond_pred_to_tree (stmt);
128 t = build3 (COND_EXPR, void_type_node, t, NULL_TREE, NULL_TREE);
129 break;
131 case GIMPLE_GOTO:
132 t = build1 (GOTO_EXPR, void_type_node, gimple_goto_dest (stmt));
133 break;
135 case GIMPLE_LABEL:
136 t = build1 (LABEL_EXPR, void_type_node, gimple_label_label (stmt));
137 break;
139 case GIMPLE_RETURN:
141 tree retval = gimple_return_retval (stmt);
143 if (retval && retval != error_mark_node)
145 tree result = DECL_RESULT (current_function_decl);
147 /* If we are not returning the current function's RESULT_DECL,
148 build an assignment to it. */
149 if (retval != result)
151 /* I believe that a function's RESULT_DECL is unique. */
152 gcc_assert (TREE_CODE (retval) != RESULT_DECL);
154 retval = build2 (MODIFY_EXPR, TREE_TYPE (result),
155 result, retval);
158 t = build1 (RETURN_EXPR, void_type_node, retval);
160 break;
162 case GIMPLE_ASM:
164 size_t i, n;
165 tree out, in, cl;
166 const char *s;
168 out = NULL_TREE;
169 n = gimple_asm_noutputs (stmt);
170 if (n > 0)
172 t = out = gimple_asm_output_op (stmt, 0);
173 for (i = 1; i < n; i++)
175 TREE_CHAIN (t) = gimple_asm_output_op (stmt, i);
176 t = gimple_asm_output_op (stmt, i);
180 in = NULL_TREE;
181 n = gimple_asm_ninputs (stmt);
182 if (n > 0)
184 t = in = gimple_asm_input_op (stmt, 0);
185 for (i = 1; i < n; i++)
187 TREE_CHAIN (t) = gimple_asm_input_op (stmt, i);
188 t = gimple_asm_input_op (stmt, i);
192 cl = NULL_TREE;
193 n = gimple_asm_nclobbers (stmt);
194 if (n > 0)
196 t = cl = gimple_asm_clobber_op (stmt, 0);
197 for (i = 1; i < n; i++)
199 TREE_CHAIN (t) = gimple_asm_clobber_op (stmt, i);
200 t = gimple_asm_clobber_op (stmt, i);
204 s = gimple_asm_string (stmt);
205 t = build4 (ASM_EXPR, void_type_node, build_string (strlen (s), s),
206 out, in, cl);
207 ASM_VOLATILE_P (t) = gimple_asm_volatile_p (stmt);
208 ASM_INPUT_P (t) = gimple_asm_input_p (stmt);
210 break;
212 case GIMPLE_CALL:
214 size_t i;
215 tree fn;
216 tree_ann_common_t ann;
218 t = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
220 CALL_EXPR_FN (t) = gimple_call_fn (stmt);
221 TREE_TYPE (t) = gimple_call_return_type (stmt);
222 CALL_EXPR_STATIC_CHAIN (t) = gimple_call_chain (stmt);
224 for (i = 0; i < gimple_call_num_args (stmt); i++)
225 CALL_EXPR_ARG (t, i) = gimple_call_arg (stmt, i);
227 if (!(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
228 TREE_SIDE_EFFECTS (t) = 1;
230 if (gimple_call_flags (stmt) & ECF_NOTHROW)
231 TREE_NOTHROW (t) = 1;
233 CALL_EXPR_TAILCALL (t) = gimple_call_tail_p (stmt);
234 CALL_EXPR_RETURN_SLOT_OPT (t) = gimple_call_return_slot_opt_p (stmt);
235 CALL_FROM_THUNK_P (t) = gimple_call_from_thunk_p (stmt);
236 CALL_CANNOT_INLINE_P (t) = gimple_call_cannot_inline_p (stmt);
237 CALL_EXPR_VA_ARG_PACK (t) = gimple_call_va_arg_pack_p (stmt);
239 /* If the call has a LHS then create a MODIFY_EXPR to hold it. */
241 tree lhs = gimple_call_lhs (stmt);
243 if (lhs)
244 t = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, t);
247 /* Record the original call statement, as it may be used
248 to retrieve profile information during expansion. */
250 if ((fn = gimple_call_fndecl (stmt)) != NULL_TREE
251 && DECL_BUILT_IN (fn))
253 ann = get_tree_common_ann (t);
254 ann->stmt = stmt;
257 break;
259 case GIMPLE_SWITCH:
261 tree label_vec;
262 size_t i;
263 tree elt = gimple_switch_label (stmt, 0);
265 label_vec = make_tree_vec (gimple_switch_num_labels (stmt));
267 if (!CASE_LOW (elt) && !CASE_HIGH (elt))
269 for (i = 1; i < gimple_switch_num_labels (stmt); i++)
270 TREE_VEC_ELT (label_vec, i - 1) = gimple_switch_label (stmt, i);
272 /* The default case in a SWITCH_EXPR must be at the end of
273 the label vector. */
274 TREE_VEC_ELT (label_vec, i - 1) = gimple_switch_label (stmt, 0);
276 else
278 for (i = 0; i < gimple_switch_num_labels (stmt); i++)
279 TREE_VEC_ELT (label_vec, i) = gimple_switch_label (stmt, i);
282 t = build3 (SWITCH_EXPR, void_type_node, gimple_switch_index (stmt),
283 NULL, label_vec);
285 break;
287 case GIMPLE_NOP:
288 case GIMPLE_PREDICT:
289 t = build1 (NOP_EXPR, void_type_node, size_zero_node);
290 break;
292 case GIMPLE_RESX:
293 t = build_resx (gimple_resx_region (stmt));
294 break;
296 default:
297 if (errorcount == 0)
299 error ("Unrecognized GIMPLE statement during RTL expansion");
300 print_gimple_stmt (stderr, stmt, 4, 0);
301 gcc_unreachable ();
303 else
305 /* Ignore any bad gimple codes if we're going to die anyhow,
306 so we can at least set TREE_ASM_WRITTEN and have the rest
307 of compilation advance without sudden ICE death. */
308 t = build1 (NOP_EXPR, void_type_node, size_zero_node);
309 break;
313 /* If STMT is inside an exception region, record it in the generated
314 expression. */
315 rn = lookup_stmt_eh_region (stmt);
316 if (rn >= 0)
318 tree call = get_call_expr_in (t);
320 ann = get_tree_common_ann (t);
321 ann->rn = rn;
323 /* For a CALL_EXPR on the RHS of an assignment, calls.c looks up
324 the CALL_EXPR not the assignment statment for EH region number. */
325 if (call && call != t)
327 ann = get_tree_common_ann (call);
328 ann->rn = rn;
332 /* Set EXPR_LOCATION in all the embedded expressions. */
333 loc = gimple_location (stmt);
334 walk_tree (&t, set_expr_location_r, (void *) &loc, NULL);
336 TREE_BLOCK (t) = gimple_block (stmt);
338 return t;
342 /* Release back to GC memory allocated by gimple_to_tree. */
344 static void
345 release_stmt_tree (gimple stmt, tree stmt_tree)
347 tree_ann_common_t ann;
349 switch (gimple_code (stmt))
351 case GIMPLE_ASSIGN:
352 if (get_gimple_rhs_class (gimple_expr_code (stmt)) != GIMPLE_SINGLE_RHS)
353 ggc_free (TREE_OPERAND (stmt_tree, 1));
354 break;
355 case GIMPLE_COND:
356 ggc_free (COND_EXPR_COND (stmt_tree));
357 break;
358 case GIMPLE_RETURN:
359 if (TREE_OPERAND (stmt_tree, 0)
360 && TREE_CODE (TREE_OPERAND (stmt_tree, 0)) == MODIFY_EXPR)
361 ggc_free (TREE_OPERAND (stmt_tree, 0));
362 break;
363 case GIMPLE_CALL:
364 if (gimple_call_lhs (stmt))
366 ann = tree_common_ann (TREE_OPERAND (stmt_tree, 1));
367 if (ann)
368 ggc_free (ann);
369 ggc_free (TREE_OPERAND (stmt_tree, 1));
371 break;
372 default:
373 break;
375 ann = tree_common_ann (stmt_tree);
376 if (ann)
377 ggc_free (ann);
378 ggc_free (stmt_tree);
382 /* Verify that there is exactly single jump instruction since last and attach
383 REG_BR_PROB note specifying probability.
384 ??? We really ought to pass the probability down to RTL expanders and let it
385 re-distribute it when the conditional expands into multiple conditionals.
386 This is however difficult to do. */
387 void
388 add_reg_br_prob_note (rtx last, int probability)
390 if (profile_status == PROFILE_ABSENT)
391 return;
392 for (last = NEXT_INSN (last); last && NEXT_INSN (last); last = NEXT_INSN (last))
393 if (JUMP_P (last))
395 /* It is common to emit condjump-around-jump sequence when we don't know
396 how to reverse the conditional. Special case this. */
397 if (!any_condjump_p (last)
398 || !JUMP_P (NEXT_INSN (last))
399 || !simplejump_p (NEXT_INSN (last))
400 || !NEXT_INSN (NEXT_INSN (last))
401 || !BARRIER_P (NEXT_INSN (NEXT_INSN (last)))
402 || !NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))
403 || !LABEL_P (NEXT_INSN (NEXT_INSN (NEXT_INSN (last))))
404 || NEXT_INSN (NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))))
405 goto failed;
406 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
407 add_reg_note (last, REG_BR_PROB,
408 GEN_INT (REG_BR_PROB_BASE - probability));
409 return;
411 if (!last || !JUMP_P (last) || !any_condjump_p (last))
412 goto failed;
413 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
414 add_reg_note (last, REG_BR_PROB, GEN_INT (probability));
415 return;
416 failed:
417 if (dump_file)
418 fprintf (dump_file, "Failed to add probability note\n");
422 #ifndef STACK_ALIGNMENT_NEEDED
423 #define STACK_ALIGNMENT_NEEDED 1
424 #endif
427 /* This structure holds data relevant to one variable that will be
428 placed in a stack slot. */
429 struct stack_var
431 /* The Variable. */
432 tree decl;
434 /* The offset of the variable. During partitioning, this is the
435 offset relative to the partition. After partitioning, this
436 is relative to the stack frame. */
437 HOST_WIDE_INT offset;
439 /* Initially, the size of the variable. Later, the size of the partition,
440 if this variable becomes it's partition's representative. */
441 HOST_WIDE_INT size;
443 /* The *byte* alignment required for this variable. Or as, with the
444 size, the alignment for this partition. */
445 unsigned int alignb;
447 /* The partition representative. */
448 size_t representative;
450 /* The next stack variable in the partition, or EOC. */
451 size_t next;
454 #define EOC ((size_t)-1)
456 /* We have an array of such objects while deciding allocation. */
457 static struct stack_var *stack_vars;
458 static size_t stack_vars_alloc;
459 static size_t stack_vars_num;
461 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
462 is non-decreasing. */
463 static size_t *stack_vars_sorted;
465 /* We have an interference graph between such objects. This graph
466 is lower triangular. */
467 static bool *stack_vars_conflict;
468 static size_t stack_vars_conflict_alloc;
470 /* The phase of the stack frame. This is the known misalignment of
471 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
472 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
473 static int frame_phase;
475 /* Used during expand_used_vars to remember if we saw any decls for
476 which we'd like to enable stack smashing protection. */
477 static bool has_protected_decls;
479 /* Used during expand_used_vars. Remember if we say a character buffer
480 smaller than our cutoff threshold. Used for -Wstack-protector. */
481 static bool has_short_buffer;
483 /* Discover the byte alignment to use for DECL. Ignore alignment
484 we can't do with expected alignment of the stack boundary. */
486 static unsigned int
487 get_decl_align_unit (tree decl)
489 unsigned int align;
491 align = DECL_ALIGN (decl);
492 align = LOCAL_ALIGNMENT (TREE_TYPE (decl), align);
494 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
495 align = MAX_SUPPORTED_STACK_ALIGNMENT;
497 if (SUPPORTS_STACK_ALIGNMENT)
499 if (crtl->stack_alignment_estimated < align)
501 gcc_assert(!crtl->stack_realign_processed);
502 crtl->stack_alignment_estimated = align;
506 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
507 So here we only make sure stack_alignment_needed >= align. */
508 if (crtl->stack_alignment_needed < align)
509 crtl->stack_alignment_needed = align;
510 if (crtl->max_used_stack_slot_alignment < crtl->stack_alignment_needed)
511 crtl->max_used_stack_slot_alignment = crtl->stack_alignment_needed;
513 return align / BITS_PER_UNIT;
516 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
517 Return the frame offset. */
519 static HOST_WIDE_INT
520 alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align)
522 HOST_WIDE_INT offset, new_frame_offset;
524 new_frame_offset = frame_offset;
525 if (FRAME_GROWS_DOWNWARD)
527 new_frame_offset -= size + frame_phase;
528 new_frame_offset &= -align;
529 new_frame_offset += frame_phase;
530 offset = new_frame_offset;
532 else
534 new_frame_offset -= frame_phase;
535 new_frame_offset += align - 1;
536 new_frame_offset &= -align;
537 new_frame_offset += frame_phase;
538 offset = new_frame_offset;
539 new_frame_offset += size;
541 frame_offset = new_frame_offset;
543 if (frame_offset_overflow (frame_offset, cfun->decl))
544 frame_offset = offset = 0;
546 return offset;
549 /* Accumulate DECL into STACK_VARS. */
551 static void
552 add_stack_var (tree decl)
554 if (stack_vars_num >= stack_vars_alloc)
556 if (stack_vars_alloc)
557 stack_vars_alloc = stack_vars_alloc * 3 / 2;
558 else
559 stack_vars_alloc = 32;
560 stack_vars
561 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
563 stack_vars[stack_vars_num].decl = decl;
564 stack_vars[stack_vars_num].offset = 0;
565 stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
566 stack_vars[stack_vars_num].alignb = get_decl_align_unit (decl);
568 /* All variables are initially in their own partition. */
569 stack_vars[stack_vars_num].representative = stack_vars_num;
570 stack_vars[stack_vars_num].next = EOC;
572 /* Ensure that this decl doesn't get put onto the list twice. */
573 SET_DECL_RTL (decl, pc_rtx);
575 stack_vars_num++;
578 /* Compute the linear index of a lower-triangular coordinate (I, J). */
580 static size_t
581 triangular_index (size_t i, size_t j)
583 if (i < j)
585 size_t t;
586 t = i, i = j, j = t;
588 return (i * (i + 1)) / 2 + j;
591 /* Ensure that STACK_VARS_CONFLICT is large enough for N objects. */
593 static void
594 resize_stack_vars_conflict (size_t n)
596 size_t size = triangular_index (n-1, n-1) + 1;
598 if (size <= stack_vars_conflict_alloc)
599 return;
601 stack_vars_conflict = XRESIZEVEC (bool, stack_vars_conflict, size);
602 memset (stack_vars_conflict + stack_vars_conflict_alloc, 0,
603 (size - stack_vars_conflict_alloc) * sizeof (bool));
604 stack_vars_conflict_alloc = size;
607 /* Make the decls associated with luid's X and Y conflict. */
609 static void
610 add_stack_var_conflict (size_t x, size_t y)
612 size_t index = triangular_index (x, y);
613 gcc_assert (index < stack_vars_conflict_alloc);
614 stack_vars_conflict[index] = true;
617 /* Check whether the decls associated with luid's X and Y conflict. */
619 static bool
620 stack_var_conflict_p (size_t x, size_t y)
622 size_t index = triangular_index (x, y);
623 gcc_assert (index < stack_vars_conflict_alloc);
624 return stack_vars_conflict[index];
627 /* Returns true if TYPE is or contains a union type. */
629 static bool
630 aggregate_contains_union_type (tree type)
632 tree field;
634 if (TREE_CODE (type) == UNION_TYPE
635 || TREE_CODE (type) == QUAL_UNION_TYPE)
636 return true;
637 if (TREE_CODE (type) == ARRAY_TYPE)
638 return aggregate_contains_union_type (TREE_TYPE (type));
639 if (TREE_CODE (type) != RECORD_TYPE)
640 return false;
642 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
643 if (TREE_CODE (field) == FIELD_DECL)
644 if (aggregate_contains_union_type (TREE_TYPE (field)))
645 return true;
647 return false;
650 /* A subroutine of expand_used_vars. If two variables X and Y have alias
651 sets that do not conflict, then do add a conflict for these variables
652 in the interference graph. We also need to make sure to add conflicts
653 for union containing structures. Else RTL alias analysis comes along
654 and due to type based aliasing rules decides that for two overlapping
655 union temporaries { short s; int i; } accesses to the same mem through
656 different types may not alias and happily reorders stores across
657 life-time boundaries of the temporaries (See PR25654).
658 We also have to mind MEM_IN_STRUCT_P and MEM_SCALAR_P. */
660 static void
661 add_alias_set_conflicts (void)
663 size_t i, j, n = stack_vars_num;
665 for (i = 0; i < n; ++i)
667 tree type_i = TREE_TYPE (stack_vars[i].decl);
668 bool aggr_i = AGGREGATE_TYPE_P (type_i);
669 bool contains_union;
671 contains_union = aggregate_contains_union_type (type_i);
672 for (j = 0; j < i; ++j)
674 tree type_j = TREE_TYPE (stack_vars[j].decl);
675 bool aggr_j = AGGREGATE_TYPE_P (type_j);
676 if (aggr_i != aggr_j
677 /* Either the objects conflict by means of type based
678 aliasing rules, or we need to add a conflict. */
679 || !objects_must_conflict_p (type_i, type_j)
680 /* In case the types do not conflict ensure that access
681 to elements will conflict. In case of unions we have
682 to be careful as type based aliasing rules may say
683 access to the same memory does not conflict. So play
684 safe and add a conflict in this case. */
685 || contains_union)
686 add_stack_var_conflict (i, j);
691 /* A subroutine of partition_stack_vars. A comparison function for qsort,
692 sorting an array of indices by the size of the object. */
694 static int
695 stack_var_size_cmp (const void *a, const void *b)
697 HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size;
698 HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size;
699 unsigned int uida = DECL_UID (stack_vars[*(const size_t *)a].decl);
700 unsigned int uidb = DECL_UID (stack_vars[*(const size_t *)b].decl);
702 if (sa < sb)
703 return -1;
704 if (sa > sb)
705 return 1;
706 /* For stack variables of the same size use the uid of the decl
707 to make the sort stable. */
708 if (uida < uidb)
709 return -1;
710 if (uida > uidb)
711 return 1;
712 return 0;
715 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
716 partitioning algorithm. Partitions A and B are known to be non-conflicting.
717 Merge them into a single partition A.
719 At the same time, add OFFSET to all variables in partition B. At the end
720 of the partitioning process we've have a nice block easy to lay out within
721 the stack frame. */
723 static void
724 union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset)
726 size_t i, last;
728 /* Update each element of partition B with the given offset,
729 and merge them into partition A. */
730 for (last = i = b; i != EOC; last = i, i = stack_vars[i].next)
732 stack_vars[i].offset += offset;
733 stack_vars[i].representative = a;
735 stack_vars[last].next = stack_vars[a].next;
736 stack_vars[a].next = b;
738 /* Update the required alignment of partition A to account for B. */
739 if (stack_vars[a].alignb < stack_vars[b].alignb)
740 stack_vars[a].alignb = stack_vars[b].alignb;
742 /* Update the interference graph and merge the conflicts. */
743 for (last = stack_vars_num, i = 0; i < last; ++i)
744 if (stack_var_conflict_p (b, i))
745 add_stack_var_conflict (a, i);
748 /* A subroutine of expand_used_vars. Binpack the variables into
749 partitions constrained by the interference graph. The overall
750 algorithm used is as follows:
752 Sort the objects by size.
753 For each object A {
754 S = size(A)
755 O = 0
756 loop {
757 Look for the largest non-conflicting object B with size <= S.
758 UNION (A, B)
759 offset(B) = O
760 O += size(B)
761 S -= size(B)
766 static void
767 partition_stack_vars (void)
769 size_t si, sj, n = stack_vars_num;
771 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
772 for (si = 0; si < n; ++si)
773 stack_vars_sorted[si] = si;
775 if (n == 1)
776 return;
778 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp);
780 /* Special case: detect when all variables conflict, and thus we can't
781 do anything during the partitioning loop. It isn't uncommon (with
782 C code at least) to declare all variables at the top of the function,
783 and if we're not inlining, then all variables will be in the same scope.
784 Take advantage of very fast libc routines for this scan. */
785 gcc_assert (sizeof(bool) == sizeof(char));
786 if (memchr (stack_vars_conflict, false, stack_vars_conflict_alloc) == NULL)
787 return;
789 for (si = 0; si < n; ++si)
791 size_t i = stack_vars_sorted[si];
792 HOST_WIDE_INT isize = stack_vars[i].size;
793 HOST_WIDE_INT offset = 0;
795 for (sj = si; sj-- > 0; )
797 size_t j = stack_vars_sorted[sj];
798 HOST_WIDE_INT jsize = stack_vars[j].size;
799 unsigned int jalign = stack_vars[j].alignb;
801 /* Ignore objects that aren't partition representatives. */
802 if (stack_vars[j].representative != j)
803 continue;
805 /* Ignore objects too large for the remaining space. */
806 if (isize < jsize)
807 continue;
809 /* Ignore conflicting objects. */
810 if (stack_var_conflict_p (i, j))
811 continue;
813 /* Refine the remaining space check to include alignment. */
814 if (offset & (jalign - 1))
816 HOST_WIDE_INT toff = offset;
817 toff += jalign - 1;
818 toff &= -(HOST_WIDE_INT)jalign;
819 if (isize - (toff - offset) < jsize)
820 continue;
822 isize -= toff - offset;
823 offset = toff;
826 /* UNION the objects, placing J at OFFSET. */
827 union_stack_vars (i, j, offset);
829 isize -= jsize;
830 if (isize == 0)
831 break;
836 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
838 static void
839 dump_stack_var_partition (void)
841 size_t si, i, j, n = stack_vars_num;
843 for (si = 0; si < n; ++si)
845 i = stack_vars_sorted[si];
847 /* Skip variables that aren't partition representatives, for now. */
848 if (stack_vars[i].representative != i)
849 continue;
851 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
852 " align %u\n", (unsigned long) i, stack_vars[i].size,
853 stack_vars[i].alignb);
855 for (j = i; j != EOC; j = stack_vars[j].next)
857 fputc ('\t', dump_file);
858 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
859 fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n",
860 stack_vars[j].offset);
865 /* Assign rtl to DECL at frame offset OFFSET. */
867 static void
868 expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset)
870 HOST_WIDE_INT align;
871 rtx x;
873 /* If this fails, we've overflowed the stack frame. Error nicely? */
874 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
876 x = plus_constant (virtual_stack_vars_rtx, offset);
877 x = gen_rtx_MEM (DECL_MODE (decl), x);
879 /* Set alignment we actually gave this decl. */
880 offset -= frame_phase;
881 align = offset & -offset;
882 align *= BITS_PER_UNIT;
883 if (align > STACK_BOUNDARY || align == 0)
884 align = STACK_BOUNDARY;
885 DECL_ALIGN (decl) = align;
886 DECL_USER_ALIGN (decl) = 0;
888 set_mem_attributes (x, decl, true);
889 SET_DECL_RTL (decl, x);
892 /* A subroutine of expand_used_vars. Give each partition representative
893 a unique location within the stack frame. Update each partition member
894 with that location. */
896 static void
897 expand_stack_vars (bool (*pred) (tree))
899 size_t si, i, j, n = stack_vars_num;
901 for (si = 0; si < n; ++si)
903 HOST_WIDE_INT offset;
905 i = stack_vars_sorted[si];
907 /* Skip variables that aren't partition representatives, for now. */
908 if (stack_vars[i].representative != i)
909 continue;
911 /* Skip variables that have already had rtl assigned. See also
912 add_stack_var where we perpetrate this pc_rtx hack. */
913 if (DECL_RTL (stack_vars[i].decl) != pc_rtx)
914 continue;
916 /* Check the predicate to see whether this variable should be
917 allocated in this pass. */
918 if (pred && !pred (stack_vars[i].decl))
919 continue;
921 offset = alloc_stack_frame_space (stack_vars[i].size,
922 stack_vars[i].alignb);
924 /* Create rtl for each variable based on their location within the
925 partition. */
926 for (j = i; j != EOC; j = stack_vars[j].next)
928 gcc_assert (stack_vars[j].offset <= stack_vars[i].size);
929 expand_one_stack_var_at (stack_vars[j].decl,
930 stack_vars[j].offset + offset);
935 /* Take into account all sizes of partitions and reset DECL_RTLs. */
936 static HOST_WIDE_INT
937 account_stack_vars (void)
939 size_t si, j, i, n = stack_vars_num;
940 HOST_WIDE_INT size = 0;
942 for (si = 0; si < n; ++si)
944 i = stack_vars_sorted[si];
946 /* Skip variables that aren't partition representatives, for now. */
947 if (stack_vars[i].representative != i)
948 continue;
950 size += stack_vars[i].size;
951 for (j = i; j != EOC; j = stack_vars[j].next)
952 SET_DECL_RTL (stack_vars[j].decl, NULL);
954 return size;
957 /* A subroutine of expand_one_var. Called to immediately assign rtl
958 to a variable to be allocated in the stack frame. */
960 static void
961 expand_one_stack_var (tree var)
963 HOST_WIDE_INT size, offset, align;
965 size = tree_low_cst (DECL_SIZE_UNIT (var), 1);
966 align = get_decl_align_unit (var);
967 offset = alloc_stack_frame_space (size, align);
969 expand_one_stack_var_at (var, offset);
972 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
973 that will reside in a hard register. */
975 static void
976 expand_one_hard_reg_var (tree var)
978 rest_of_decl_compilation (var, 0, 0);
981 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
982 that will reside in a pseudo register. */
984 static void
985 expand_one_register_var (tree var)
987 tree type = TREE_TYPE (var);
988 int unsignedp = TYPE_UNSIGNED (type);
989 enum machine_mode reg_mode
990 = promote_mode (type, DECL_MODE (var), &unsignedp, 0);
991 rtx x = gen_reg_rtx (reg_mode);
993 SET_DECL_RTL (var, x);
995 /* Note if the object is a user variable. */
996 if (!DECL_ARTIFICIAL (var))
997 mark_user_reg (x);
999 if (POINTER_TYPE_P (type))
1000 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
1003 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1004 has some associated error, e.g. its type is error-mark. We just need
1005 to pick something that won't crash the rest of the compiler. */
1007 static void
1008 expand_one_error_var (tree var)
1010 enum machine_mode mode = DECL_MODE (var);
1011 rtx x;
1013 if (mode == BLKmode)
1014 x = gen_rtx_MEM (BLKmode, const0_rtx);
1015 else if (mode == VOIDmode)
1016 x = const0_rtx;
1017 else
1018 x = gen_reg_rtx (mode);
1020 SET_DECL_RTL (var, x);
1023 /* A subroutine of expand_one_var. VAR is a variable that will be
1024 allocated to the local stack frame. Return true if we wish to
1025 add VAR to STACK_VARS so that it will be coalesced with other
1026 variables. Return false to allocate VAR immediately.
1028 This function is used to reduce the number of variables considered
1029 for coalescing, which reduces the size of the quadratic problem. */
1031 static bool
1032 defer_stack_allocation (tree var, bool toplevel)
1034 /* If stack protection is enabled, *all* stack variables must be deferred,
1035 so that we can re-order the strings to the top of the frame. */
1036 if (flag_stack_protect)
1037 return true;
1039 /* Variables in the outermost scope automatically conflict with
1040 every other variable. The only reason to want to defer them
1041 at all is that, after sorting, we can more efficiently pack
1042 small variables in the stack frame. Continue to defer at -O2. */
1043 if (toplevel && optimize < 2)
1044 return false;
1046 /* Without optimization, *most* variables are allocated from the
1047 stack, which makes the quadratic problem large exactly when we
1048 want compilation to proceed as quickly as possible. On the
1049 other hand, we don't want the function's stack frame size to
1050 get completely out of hand. So we avoid adding scalars and
1051 "small" aggregates to the list at all. */
1052 if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32)
1053 return false;
1055 return true;
1058 /* A subroutine of expand_used_vars. Expand one variable according to
1059 its flavor. Variables to be placed on the stack are not actually
1060 expanded yet, merely recorded.
1061 When REALLY_EXPAND is false, only add stack values to be allocated.
1062 Return stack usage this variable is supposed to take.
1065 static HOST_WIDE_INT
1066 expand_one_var (tree var, bool toplevel, bool really_expand)
1068 if (SUPPORTS_STACK_ALIGNMENT
1069 && TREE_TYPE (var) != error_mark_node
1070 && TREE_CODE (var) == VAR_DECL)
1072 unsigned int align;
1074 /* Because we don't know if VAR will be in register or on stack,
1075 we conservatively assume it will be on stack even if VAR is
1076 eventually put into register after RA pass. For non-automatic
1077 variables, which won't be on stack, we collect alignment of
1078 type and ignore user specified alignment. */
1079 if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1080 align = TYPE_ALIGN (TREE_TYPE (var));
1081 else
1082 align = DECL_ALIGN (var);
1084 if (crtl->stack_alignment_estimated < align)
1086 /* stack_alignment_estimated shouldn't change after stack
1087 realign decision made */
1088 gcc_assert(!crtl->stack_realign_processed);
1089 crtl->stack_alignment_estimated = align;
1093 if (TREE_CODE (var) != VAR_DECL)
1095 else if (DECL_EXTERNAL (var))
1097 else if (DECL_HAS_VALUE_EXPR_P (var))
1099 else if (TREE_STATIC (var))
1101 else if (DECL_RTL_SET_P (var))
1103 else if (TREE_TYPE (var) == error_mark_node)
1105 if (really_expand)
1106 expand_one_error_var (var);
1108 else if (DECL_HARD_REGISTER (var))
1110 if (really_expand)
1111 expand_one_hard_reg_var (var);
1113 else if (use_register_for_decl (var))
1115 if (really_expand)
1116 expand_one_register_var (var);
1118 else if (defer_stack_allocation (var, toplevel))
1119 add_stack_var (var);
1120 else
1122 if (really_expand)
1123 expand_one_stack_var (var);
1124 return tree_low_cst (DECL_SIZE_UNIT (var), 1);
1126 return 0;
1129 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1130 expanding variables. Those variables that can be put into registers
1131 are allocated pseudos; those that can't are put on the stack.
1133 TOPLEVEL is true if this is the outermost BLOCK. */
1135 static void
1136 expand_used_vars_for_block (tree block, bool toplevel)
1138 size_t i, j, old_sv_num, this_sv_num, new_sv_num;
1139 tree t;
1141 old_sv_num = toplevel ? 0 : stack_vars_num;
1143 /* Expand all variables at this level. */
1144 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1145 if (TREE_USED (t))
1146 expand_one_var (t, toplevel, true);
1148 this_sv_num = stack_vars_num;
1150 /* Expand all variables at containing levels. */
1151 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1152 expand_used_vars_for_block (t, false);
1154 /* Since we do not track exact variable lifetimes (which is not even
1155 possible for variables whose address escapes), we mirror the block
1156 tree in the interference graph. Here we cause all variables at this
1157 level, and all sublevels, to conflict. Do make certain that a
1158 variable conflicts with itself. */
1159 if (old_sv_num < this_sv_num)
1161 new_sv_num = stack_vars_num;
1162 resize_stack_vars_conflict (new_sv_num);
1164 for (i = old_sv_num; i < new_sv_num; ++i)
1165 for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
1166 add_stack_var_conflict (i, j);
1170 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1171 and clear TREE_USED on all local variables. */
1173 static void
1174 clear_tree_used (tree block)
1176 tree t;
1178 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1179 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1180 TREE_USED (t) = 0;
1182 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1183 clear_tree_used (t);
1186 /* Examine TYPE and determine a bit mask of the following features. */
1188 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1189 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1190 #define SPCT_HAS_ARRAY 4
1191 #define SPCT_HAS_AGGREGATE 8
1193 static unsigned int
1194 stack_protect_classify_type (tree type)
1196 unsigned int ret = 0;
1197 tree t;
1199 switch (TREE_CODE (type))
1201 case ARRAY_TYPE:
1202 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1203 if (t == char_type_node
1204 || t == signed_char_type_node
1205 || t == unsigned_char_type_node)
1207 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1208 unsigned HOST_WIDE_INT len;
1210 if (!TYPE_SIZE_UNIT (type)
1211 || !host_integerp (TYPE_SIZE_UNIT (type), 1))
1212 len = max;
1213 else
1214 len = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
1216 if (len < max)
1217 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1218 else
1219 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1221 else
1222 ret = SPCT_HAS_ARRAY;
1223 break;
1225 case UNION_TYPE:
1226 case QUAL_UNION_TYPE:
1227 case RECORD_TYPE:
1228 ret = SPCT_HAS_AGGREGATE;
1229 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1230 if (TREE_CODE (t) == FIELD_DECL)
1231 ret |= stack_protect_classify_type (TREE_TYPE (t));
1232 break;
1234 default:
1235 break;
1238 return ret;
1241 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1242 part of the local stack frame. Remember if we ever return nonzero for
1243 any variable in this function. The return value is the phase number in
1244 which the variable should be allocated. */
1246 static int
1247 stack_protect_decl_phase (tree decl)
1249 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1250 int ret = 0;
1252 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1253 has_short_buffer = true;
1255 if (flag_stack_protect == 2)
1257 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1258 && !(bits & SPCT_HAS_AGGREGATE))
1259 ret = 1;
1260 else if (bits & SPCT_HAS_ARRAY)
1261 ret = 2;
1263 else
1264 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1266 if (ret)
1267 has_protected_decls = true;
1269 return ret;
1272 /* Two helper routines that check for phase 1 and phase 2. These are used
1273 as callbacks for expand_stack_vars. */
1275 static bool
1276 stack_protect_decl_phase_1 (tree decl)
1278 return stack_protect_decl_phase (decl) == 1;
1281 static bool
1282 stack_protect_decl_phase_2 (tree decl)
1284 return stack_protect_decl_phase (decl) == 2;
1287 /* Ensure that variables in different stack protection phases conflict
1288 so that they are not merged and share the same stack slot. */
1290 static void
1291 add_stack_protection_conflicts (void)
1293 size_t i, j, n = stack_vars_num;
1294 unsigned char *phase;
1296 phase = XNEWVEC (unsigned char, n);
1297 for (i = 0; i < n; ++i)
1298 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1300 for (i = 0; i < n; ++i)
1302 unsigned char ph_i = phase[i];
1303 for (j = 0; j < i; ++j)
1304 if (ph_i != phase[j])
1305 add_stack_var_conflict (i, j);
1308 XDELETEVEC (phase);
1311 /* Create a decl for the guard at the top of the stack frame. */
1313 static void
1314 create_stack_guard (void)
1316 tree guard = build_decl (VAR_DECL, NULL, ptr_type_node);
1317 TREE_THIS_VOLATILE (guard) = 1;
1318 TREE_USED (guard) = 1;
1319 expand_one_stack_var (guard);
1320 crtl->stack_protect_guard = guard;
1323 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1324 expanding variables. Those variables that can be put into registers
1325 are allocated pseudos; those that can't are put on the stack.
1327 TOPLEVEL is true if this is the outermost BLOCK. */
1329 static HOST_WIDE_INT
1330 account_used_vars_for_block (tree block, bool toplevel)
1332 size_t i, j, old_sv_num, this_sv_num, new_sv_num;
1333 tree t;
1334 HOST_WIDE_INT size = 0;
1336 old_sv_num = toplevel ? 0 : stack_vars_num;
1338 /* Expand all variables at this level. */
1339 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1340 if (TREE_USED (t))
1341 size += expand_one_var (t, toplevel, false);
1343 this_sv_num = stack_vars_num;
1345 /* Expand all variables at containing levels. */
1346 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1347 size += account_used_vars_for_block (t, false);
1349 /* Since we do not track exact variable lifetimes (which is not even
1350 possible for variables whose address escapes), we mirror the block
1351 tree in the interference graph. Here we cause all variables at this
1352 level, and all sublevels, to conflict. Do make certain that a
1353 variable conflicts with itself. */
1354 if (old_sv_num < this_sv_num)
1356 new_sv_num = stack_vars_num;
1357 resize_stack_vars_conflict (new_sv_num);
1359 for (i = old_sv_num; i < new_sv_num; ++i)
1360 for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
1361 add_stack_var_conflict (i, j);
1363 return size;
1366 /* Prepare for expanding variables. */
1367 static void
1368 init_vars_expansion (void)
1370 tree t;
1371 /* Set TREE_USED on all variables in the local_decls. */
1372 for (t = cfun->local_decls; t; t = TREE_CHAIN (t))
1373 TREE_USED (TREE_VALUE (t)) = 1;
1375 /* Clear TREE_USED on all variables associated with a block scope. */
1376 clear_tree_used (DECL_INITIAL (current_function_decl));
1378 /* Initialize local stack smashing state. */
1379 has_protected_decls = false;
1380 has_short_buffer = false;
1383 /* Free up stack variable graph data. */
1384 static void
1385 fini_vars_expansion (void)
1387 XDELETEVEC (stack_vars);
1388 XDELETEVEC (stack_vars_sorted);
1389 XDELETEVEC (stack_vars_conflict);
1390 stack_vars = NULL;
1391 stack_vars_alloc = stack_vars_num = 0;
1392 stack_vars_conflict = NULL;
1393 stack_vars_conflict_alloc = 0;
1396 /* Make a fair guess for the size of the stack frame of the current
1397 function. This doesn't have to be exact, the result is only used
1398 in the inline heuristics. So we don't want to run the full stack
1399 var packing algorithm (which is quadratic in the number of stack
1400 vars). Instead, we calculate the total size of all stack vars.
1401 This turns out to be a pretty fair estimate -- packing of stack
1402 vars doesn't happen very often. */
1404 HOST_WIDE_INT
1405 estimated_stack_frame_size (void)
1407 HOST_WIDE_INT size = 0;
1408 size_t i;
1409 tree t, outer_block = DECL_INITIAL (current_function_decl);
1411 init_vars_expansion ();
1413 for (t = cfun->local_decls; t; t = TREE_CHAIN (t))
1415 tree var = TREE_VALUE (t);
1417 if (TREE_USED (var))
1418 size += expand_one_var (var, true, false);
1419 TREE_USED (var) = 1;
1421 size += account_used_vars_for_block (outer_block, true);
1423 if (stack_vars_num > 0)
1425 /* Fake sorting the stack vars for account_stack_vars (). */
1426 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1427 for (i = 0; i < stack_vars_num; ++i)
1428 stack_vars_sorted[i] = i;
1429 size += account_stack_vars ();
1430 fini_vars_expansion ();
1433 return size;
1436 /* Expand all variables used in the function. */
1438 static void
1439 expand_used_vars (void)
1441 tree t, next, outer_block = DECL_INITIAL (current_function_decl);
1443 /* Compute the phase of the stack frame for this function. */
1445 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1446 int off = STARTING_FRAME_OFFSET % align;
1447 frame_phase = off ? align - off : 0;
1450 init_vars_expansion ();
1452 /* At this point all variables on the local_decls with TREE_USED
1453 set are not associated with any block scope. Lay them out. */
1454 t = cfun->local_decls;
1455 cfun->local_decls = NULL_TREE;
1456 for (; t; t = next)
1458 tree var = TREE_VALUE (t);
1459 bool expand_now = false;
1461 next = TREE_CHAIN (t);
1463 /* We didn't set a block for static or extern because it's hard
1464 to tell the difference between a global variable (re)declared
1465 in a local scope, and one that's really declared there to
1466 begin with. And it doesn't really matter much, since we're
1467 not giving them stack space. Expand them now. */
1468 if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1469 expand_now = true;
1471 /* Any variable that could have been hoisted into an SSA_NAME
1472 will have been propagated anywhere the optimizers chose,
1473 i.e. not confined to their original block. Allocate them
1474 as if they were defined in the outermost scope. */
1475 else if (is_gimple_reg (var))
1476 expand_now = true;
1478 /* If the variable is not associated with any block, then it
1479 was created by the optimizers, and could be live anywhere
1480 in the function. */
1481 else if (TREE_USED (var))
1482 expand_now = true;
1484 /* Finally, mark all variables on the list as used. We'll use
1485 this in a moment when we expand those associated with scopes. */
1486 TREE_USED (var) = 1;
1488 if (expand_now)
1490 expand_one_var (var, true, true);
1491 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1493 rtx rtl = DECL_RTL_IF_SET (var);
1495 /* Keep artificial non-ignored vars in cfun->local_decls
1496 chain until instantiate_decls. */
1497 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1499 TREE_CHAIN (t) = cfun->local_decls;
1500 cfun->local_decls = t;
1501 continue;
1506 ggc_free (t);
1509 /* At this point, all variables within the block tree with TREE_USED
1510 set are actually used by the optimized function. Lay them out. */
1511 expand_used_vars_for_block (outer_block, true);
1513 if (stack_vars_num > 0)
1515 /* Due to the way alias sets work, no variables with non-conflicting
1516 alias sets may be assigned the same address. Add conflicts to
1517 reflect this. */
1518 add_alias_set_conflicts ();
1520 /* If stack protection is enabled, we don't share space between
1521 vulnerable data and non-vulnerable data. */
1522 if (flag_stack_protect)
1523 add_stack_protection_conflicts ();
1525 /* Now that we have collected all stack variables, and have computed a
1526 minimal interference graph, attempt to save some stack space. */
1527 partition_stack_vars ();
1528 if (dump_file)
1529 dump_stack_var_partition ();
1532 /* There are several conditions under which we should create a
1533 stack guard: protect-all, alloca used, protected decls present. */
1534 if (flag_stack_protect == 2
1535 || (flag_stack_protect
1536 && (cfun->calls_alloca || has_protected_decls)))
1537 create_stack_guard ();
1539 /* Assign rtl to each variable based on these partitions. */
1540 if (stack_vars_num > 0)
1542 /* Reorder decls to be protected by iterating over the variables
1543 array multiple times, and allocating out of each phase in turn. */
1544 /* ??? We could probably integrate this into the qsort we did
1545 earlier, such that we naturally see these variables first,
1546 and thus naturally allocate things in the right order. */
1547 if (has_protected_decls)
1549 /* Phase 1 contains only character arrays. */
1550 expand_stack_vars (stack_protect_decl_phase_1);
1552 /* Phase 2 contains other kinds of arrays. */
1553 if (flag_stack_protect == 2)
1554 expand_stack_vars (stack_protect_decl_phase_2);
1557 expand_stack_vars (NULL);
1559 fini_vars_expansion ();
1562 /* If the target requires that FRAME_OFFSET be aligned, do it. */
1563 if (STACK_ALIGNMENT_NEEDED)
1565 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1566 if (!FRAME_GROWS_DOWNWARD)
1567 frame_offset += align - 1;
1568 frame_offset &= -align;
1573 /* If we need to produce a detailed dump, print the tree representation
1574 for STMT to the dump file. SINCE is the last RTX after which the RTL
1575 generated for STMT should have been appended. */
1577 static void
1578 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx since)
1580 if (dump_file && (dump_flags & TDF_DETAILS))
1582 fprintf (dump_file, "\n;; ");
1583 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1584 fprintf (dump_file, "\n");
1586 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1590 /* Maps the blocks that do not contain tree labels to rtx labels. */
1592 static struct pointer_map_t *lab_rtx_for_bb;
1594 /* Returns the label_rtx expression for a label starting basic block BB. */
1596 static rtx
1597 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
1599 gimple_stmt_iterator gsi;
1600 tree lab;
1601 gimple lab_stmt;
1602 void **elt;
1604 if (bb->flags & BB_RTL)
1605 return block_label (bb);
1607 elt = pointer_map_contains (lab_rtx_for_bb, bb);
1608 if (elt)
1609 return (rtx) *elt;
1611 /* Find the tree label if it is present. */
1613 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1615 lab_stmt = gsi_stmt (gsi);
1616 if (gimple_code (lab_stmt) != GIMPLE_LABEL)
1617 break;
1619 lab = gimple_label_label (lab_stmt);
1620 if (DECL_NONLOCAL (lab))
1621 break;
1623 return label_rtx (lab);
1626 elt = pointer_map_insert (lab_rtx_for_bb, bb);
1627 *elt = gen_label_rtx ();
1628 return (rtx) *elt;
1632 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
1633 Returns a new basic block if we've terminated the current basic
1634 block and created a new one. */
1636 static basic_block
1637 expand_gimple_cond (basic_block bb, gimple stmt)
1639 basic_block new_bb, dest;
1640 edge new_edge;
1641 edge true_edge;
1642 edge false_edge;
1643 tree pred = gimple_cond_pred_to_tree (stmt);
1644 rtx last2, last;
1646 last2 = last = get_last_insn ();
1648 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1649 if (gimple_has_location (stmt))
1651 set_curr_insn_source_location (gimple_location (stmt));
1652 set_curr_insn_block (gimple_block (stmt));
1655 /* These flags have no purpose in RTL land. */
1656 true_edge->flags &= ~EDGE_TRUE_VALUE;
1657 false_edge->flags &= ~EDGE_FALSE_VALUE;
1659 /* We can either have a pure conditional jump with one fallthru edge or
1660 two-way jump that needs to be decomposed into two basic blocks. */
1661 if (false_edge->dest == bb->next_bb)
1663 jumpif (pred, label_rtx_for_bb (true_edge->dest));
1664 add_reg_br_prob_note (last, true_edge->probability);
1665 maybe_dump_rtl_for_gimple_stmt (stmt, last);
1666 if (true_edge->goto_locus)
1668 set_curr_insn_source_location (true_edge->goto_locus);
1669 set_curr_insn_block (true_edge->goto_block);
1670 true_edge->goto_locus = curr_insn_locator ();
1672 true_edge->goto_block = NULL;
1673 false_edge->flags |= EDGE_FALLTHRU;
1674 ggc_free (pred);
1675 return NULL;
1677 if (true_edge->dest == bb->next_bb)
1679 jumpifnot (pred, label_rtx_for_bb (false_edge->dest));
1680 add_reg_br_prob_note (last, false_edge->probability);
1681 maybe_dump_rtl_for_gimple_stmt (stmt, last);
1682 if (false_edge->goto_locus)
1684 set_curr_insn_source_location (false_edge->goto_locus);
1685 set_curr_insn_block (false_edge->goto_block);
1686 false_edge->goto_locus = curr_insn_locator ();
1688 false_edge->goto_block = NULL;
1689 true_edge->flags |= EDGE_FALLTHRU;
1690 ggc_free (pred);
1691 return NULL;
1694 jumpif (pred, label_rtx_for_bb (true_edge->dest));
1695 add_reg_br_prob_note (last, true_edge->probability);
1696 last = get_last_insn ();
1697 if (false_edge->goto_locus)
1699 set_curr_insn_source_location (false_edge->goto_locus);
1700 set_curr_insn_block (false_edge->goto_block);
1701 false_edge->goto_locus = curr_insn_locator ();
1703 false_edge->goto_block = NULL;
1704 emit_jump (label_rtx_for_bb (false_edge->dest));
1706 BB_END (bb) = last;
1707 if (BARRIER_P (BB_END (bb)))
1708 BB_END (bb) = PREV_INSN (BB_END (bb));
1709 update_bb_for_insn (bb);
1711 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1712 dest = false_edge->dest;
1713 redirect_edge_succ (false_edge, new_bb);
1714 false_edge->flags |= EDGE_FALLTHRU;
1715 new_bb->count = false_edge->count;
1716 new_bb->frequency = EDGE_FREQUENCY (false_edge);
1717 new_edge = make_edge (new_bb, dest, 0);
1718 new_edge->probability = REG_BR_PROB_BASE;
1719 new_edge->count = new_bb->count;
1720 if (BARRIER_P (BB_END (new_bb)))
1721 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
1722 update_bb_for_insn (new_bb);
1724 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
1726 if (true_edge->goto_locus)
1728 set_curr_insn_source_location (true_edge->goto_locus);
1729 set_curr_insn_block (true_edge->goto_block);
1730 true_edge->goto_locus = curr_insn_locator ();
1732 true_edge->goto_block = NULL;
1734 ggc_free (pred);
1735 return new_bb;
1738 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
1739 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
1740 generated a tail call (something that might be denied by the ABI
1741 rules governing the call; see calls.c).
1743 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
1744 can still reach the rest of BB. The case here is __builtin_sqrt,
1745 where the NaN result goes through the external function (with a
1746 tailcall) and the normal result happens via a sqrt instruction. */
1748 static basic_block
1749 expand_gimple_tailcall (basic_block bb, gimple stmt, bool *can_fallthru)
1751 rtx last2, last;
1752 edge e;
1753 edge_iterator ei;
1754 int probability;
1755 gcov_type count;
1756 tree stmt_tree = gimple_to_tree (stmt);
1758 last2 = last = get_last_insn ();
1760 expand_expr_stmt (stmt_tree);
1762 release_stmt_tree (stmt, stmt_tree);
1764 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
1765 if (CALL_P (last) && SIBLING_CALL_P (last))
1766 goto found;
1768 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
1770 *can_fallthru = true;
1771 return NULL;
1773 found:
1774 /* ??? Wouldn't it be better to just reset any pending stack adjust?
1775 Any instructions emitted here are about to be deleted. */
1776 do_pending_stack_adjust ();
1778 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
1779 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
1780 EH or abnormal edges, we shouldn't have created a tail call in
1781 the first place. So it seems to me we should just be removing
1782 all edges here, or redirecting the existing fallthru edge to
1783 the exit block. */
1785 probability = 0;
1786 count = 0;
1788 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1790 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
1792 if (e->dest != EXIT_BLOCK_PTR)
1794 e->dest->count -= e->count;
1795 e->dest->frequency -= EDGE_FREQUENCY (e);
1796 if (e->dest->count < 0)
1797 e->dest->count = 0;
1798 if (e->dest->frequency < 0)
1799 e->dest->frequency = 0;
1801 count += e->count;
1802 probability += e->probability;
1803 remove_edge (e);
1805 else
1806 ei_next (&ei);
1809 /* This is somewhat ugly: the call_expr expander often emits instructions
1810 after the sibcall (to perform the function return). These confuse the
1811 find_many_sub_basic_blocks code, so we need to get rid of these. */
1812 last = NEXT_INSN (last);
1813 gcc_assert (BARRIER_P (last));
1815 *can_fallthru = false;
1816 while (NEXT_INSN (last))
1818 /* For instance an sqrt builtin expander expands if with
1819 sibcall in the then and label for `else`. */
1820 if (LABEL_P (NEXT_INSN (last)))
1822 *can_fallthru = true;
1823 break;
1825 delete_insn (NEXT_INSN (last));
1828 e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
1829 e->probability += probability;
1830 e->count += count;
1831 BB_END (bb) = last;
1832 update_bb_for_insn (bb);
1834 if (NEXT_INSN (last))
1836 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1838 last = BB_END (bb);
1839 if (BARRIER_P (last))
1840 BB_END (bb) = PREV_INSN (last);
1843 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
1845 return bb;
1848 /* Expand basic block BB from GIMPLE trees to RTL. */
1850 static basic_block
1851 expand_gimple_basic_block (basic_block bb)
1853 gimple_stmt_iterator gsi;
1854 gimple_seq stmts;
1855 gimple stmt = NULL;
1856 rtx note, last;
1857 edge e;
1858 edge_iterator ei;
1859 void **elt;
1861 if (dump_file)
1862 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
1863 bb->index);
1865 /* Note that since we are now transitioning from GIMPLE to RTL, we
1866 cannot use the gsi_*_bb() routines because they expect the basic
1867 block to be in GIMPLE, instead of RTL. Therefore, we need to
1868 access the BB sequence directly. */
1869 stmts = bb_seq (bb);
1870 bb->il.gimple = NULL;
1871 rtl_profile_for_bb (bb);
1872 init_rtl_bb_info (bb);
1873 bb->flags |= BB_RTL;
1875 /* Remove the RETURN_EXPR if we may fall though to the exit
1876 instead. */
1877 gsi = gsi_last (stmts);
1878 if (!gsi_end_p (gsi)
1879 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
1881 gimple ret_stmt = gsi_stmt (gsi);
1883 gcc_assert (single_succ_p (bb));
1884 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
1886 if (bb->next_bb == EXIT_BLOCK_PTR
1887 && !gimple_return_retval (ret_stmt))
1889 gsi_remove (&gsi, false);
1890 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
1894 gsi = gsi_start (stmts);
1895 if (!gsi_end_p (gsi))
1897 stmt = gsi_stmt (gsi);
1898 if (gimple_code (stmt) != GIMPLE_LABEL)
1899 stmt = NULL;
1902 elt = pointer_map_contains (lab_rtx_for_bb, bb);
1904 if (stmt || elt)
1906 last = get_last_insn ();
1908 if (stmt)
1910 tree stmt_tree = gimple_to_tree (stmt);
1911 expand_expr_stmt (stmt_tree);
1912 release_stmt_tree (stmt, stmt_tree);
1913 gsi_next (&gsi);
1916 if (elt)
1917 emit_label ((rtx) *elt);
1919 /* Java emits line number notes in the top of labels.
1920 ??? Make this go away once line number notes are obsoleted. */
1921 BB_HEAD (bb) = NEXT_INSN (last);
1922 if (NOTE_P (BB_HEAD (bb)))
1923 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
1924 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
1926 maybe_dump_rtl_for_gimple_stmt (stmt, last);
1928 else
1929 note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
1931 NOTE_BASIC_BLOCK (note) = bb;
1933 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1935 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
1936 e->flags &= ~EDGE_EXECUTABLE;
1938 /* At the moment not all abnormal edges match the RTL representation.
1939 It is safe to remove them here as find_many_sub_basic_blocks will
1940 rediscover them. In the future we should get this fixed properly. */
1941 if (e->flags & EDGE_ABNORMAL)
1942 remove_edge (e);
1943 else
1944 ei_next (&ei);
1947 for (; !gsi_end_p (gsi); gsi_next (&gsi))
1949 gimple stmt = gsi_stmt (gsi);
1950 basic_block new_bb;
1952 /* Expand this statement, then evaluate the resulting RTL and
1953 fixup the CFG accordingly. */
1954 if (gimple_code (stmt) == GIMPLE_COND)
1956 new_bb = expand_gimple_cond (bb, stmt);
1957 if (new_bb)
1958 return new_bb;
1960 else
1962 if (is_gimple_call (stmt) && gimple_call_tail_p (stmt))
1964 bool can_fallthru;
1965 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
1966 if (new_bb)
1968 if (can_fallthru)
1969 bb = new_bb;
1970 else
1971 return new_bb;
1974 else if (gimple_code (stmt) != GIMPLE_CHANGE_DYNAMIC_TYPE)
1976 tree stmt_tree = gimple_to_tree (stmt);
1977 last = get_last_insn ();
1978 expand_expr_stmt (stmt_tree);
1979 maybe_dump_rtl_for_gimple_stmt (stmt, last);
1980 release_stmt_tree (stmt, stmt_tree);
1985 /* Expand implicit goto and convert goto_locus. */
1986 FOR_EACH_EDGE (e, ei, bb->succs)
1988 if (e->goto_locus && e->goto_block)
1990 set_curr_insn_source_location (e->goto_locus);
1991 set_curr_insn_block (e->goto_block);
1992 e->goto_locus = curr_insn_locator ();
1994 e->goto_block = NULL;
1995 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
1997 emit_jump (label_rtx_for_bb (e->dest));
1998 e->flags &= ~EDGE_FALLTHRU;
2002 do_pending_stack_adjust ();
2004 /* Find the block tail. The last insn in the block is the insn
2005 before a barrier and/or table jump insn. */
2006 last = get_last_insn ();
2007 if (BARRIER_P (last))
2008 last = PREV_INSN (last);
2009 if (JUMP_TABLE_DATA_P (last))
2010 last = PREV_INSN (PREV_INSN (last));
2011 BB_END (bb) = last;
2013 update_bb_for_insn (bb);
2015 return bb;
2019 /* Create a basic block for initialization code. */
2021 static basic_block
2022 construct_init_block (void)
2024 basic_block init_block, first_block;
2025 edge e = NULL;
2026 int flags;
2028 /* Multiple entry points not supported yet. */
2029 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
2030 init_rtl_bb_info (ENTRY_BLOCK_PTR);
2031 init_rtl_bb_info (EXIT_BLOCK_PTR);
2032 ENTRY_BLOCK_PTR->flags |= BB_RTL;
2033 EXIT_BLOCK_PTR->flags |= BB_RTL;
2035 e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
2037 /* When entry edge points to first basic block, we don't need jump,
2038 otherwise we have to jump into proper target. */
2039 if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
2041 tree label = gimple_block_label (e->dest);
2043 emit_jump (label_rtx (label));
2044 flags = 0;
2046 else
2047 flags = EDGE_FALLTHRU;
2049 init_block = create_basic_block (NEXT_INSN (get_insns ()),
2050 get_last_insn (),
2051 ENTRY_BLOCK_PTR);
2052 init_block->frequency = ENTRY_BLOCK_PTR->frequency;
2053 init_block->count = ENTRY_BLOCK_PTR->count;
2054 if (e)
2056 first_block = e->dest;
2057 redirect_edge_succ (e, init_block);
2058 e = make_edge (init_block, first_block, flags);
2060 else
2061 e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
2062 e->probability = REG_BR_PROB_BASE;
2063 e->count = ENTRY_BLOCK_PTR->count;
2065 update_bb_for_insn (init_block);
2066 return init_block;
2069 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
2070 found in the block tree. */
2072 static void
2073 set_block_levels (tree block, int level)
2075 while (block)
2077 BLOCK_NUMBER (block) = level;
2078 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
2079 block = BLOCK_CHAIN (block);
2083 /* Create a block containing landing pads and similar stuff. */
2085 static void
2086 construct_exit_block (void)
2088 rtx head = get_last_insn ();
2089 rtx end;
2090 basic_block exit_block;
2091 edge e, e2;
2092 unsigned ix;
2093 edge_iterator ei;
2094 rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb);
2096 rtl_profile_for_bb (EXIT_BLOCK_PTR);
2098 /* Make sure the locus is set to the end of the function, so that
2099 epilogue line numbers and warnings are set properly. */
2100 if (cfun->function_end_locus != UNKNOWN_LOCATION)
2101 input_location = cfun->function_end_locus;
2103 /* The following insns belong to the top scope. */
2104 set_curr_insn_block (DECL_INITIAL (current_function_decl));
2106 /* Generate rtl for function exit. */
2107 expand_function_end ();
2109 end = get_last_insn ();
2110 if (head == end)
2111 return;
2112 /* While emitting the function end we could move end of the last basic block.
2114 BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end;
2115 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
2116 head = NEXT_INSN (head);
2117 exit_block = create_basic_block (NEXT_INSN (head), end,
2118 EXIT_BLOCK_PTR->prev_bb);
2119 exit_block->frequency = EXIT_BLOCK_PTR->frequency;
2120 exit_block->count = EXIT_BLOCK_PTR->count;
2122 ix = 0;
2123 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
2125 e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
2126 if (!(e->flags & EDGE_ABNORMAL))
2127 redirect_edge_succ (e, exit_block);
2128 else
2129 ix++;
2132 e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
2133 e->probability = REG_BR_PROB_BASE;
2134 e->count = EXIT_BLOCK_PTR->count;
2135 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
2136 if (e2 != e)
2138 e->count -= e2->count;
2139 exit_block->count -= e2->count;
2140 exit_block->frequency -= EDGE_FREQUENCY (e2);
2142 if (e->count < 0)
2143 e->count = 0;
2144 if (exit_block->count < 0)
2145 exit_block->count = 0;
2146 if (exit_block->frequency < 0)
2147 exit_block->frequency = 0;
2148 update_bb_for_insn (exit_block);
2151 /* Helper function for discover_nonconstant_array_refs.
2152 Look for ARRAY_REF nodes with non-constant indexes and mark them
2153 addressable. */
2155 static tree
2156 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
2157 void *data ATTRIBUTE_UNUSED)
2159 tree t = *tp;
2161 if (IS_TYPE_OR_DECL_P (t))
2162 *walk_subtrees = 0;
2163 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2165 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2166 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
2167 && (!TREE_OPERAND (t, 2)
2168 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
2169 || (TREE_CODE (t) == COMPONENT_REF
2170 && (!TREE_OPERAND (t,2)
2171 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
2172 || TREE_CODE (t) == BIT_FIELD_REF
2173 || TREE_CODE (t) == REALPART_EXPR
2174 || TREE_CODE (t) == IMAGPART_EXPR
2175 || TREE_CODE (t) == VIEW_CONVERT_EXPR
2176 || CONVERT_EXPR_P (t))
2177 t = TREE_OPERAND (t, 0);
2179 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2181 t = get_base_address (t);
2182 if (t && DECL_P (t))
2183 TREE_ADDRESSABLE (t) = 1;
2186 *walk_subtrees = 0;
2189 return NULL_TREE;
2192 /* RTL expansion is not able to compile array references with variable
2193 offsets for arrays stored in single register. Discover such
2194 expressions and mark variables as addressable to avoid this
2195 scenario. */
2197 static void
2198 discover_nonconstant_array_refs (void)
2200 basic_block bb;
2201 gimple_stmt_iterator gsi;
2203 FOR_EACH_BB (bb)
2204 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2206 gimple stmt = gsi_stmt (gsi);
2207 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
2211 /* This function sets crtl->args.internal_arg_pointer to a virtual
2212 register if DRAP is needed. Local register allocator will replace
2213 virtual_incoming_args_rtx with the virtual register. */
2215 static void
2216 expand_stack_alignment (void)
2218 rtx drap_rtx;
2219 unsigned int preferred_stack_boundary;
2221 if (! SUPPORTS_STACK_ALIGNMENT)
2222 return;
2224 if (cfun->calls_alloca
2225 || cfun->has_nonlocal_label
2226 || crtl->has_nonlocal_goto)
2227 crtl->need_drap = true;
2229 gcc_assert (crtl->stack_alignment_needed
2230 <= crtl->stack_alignment_estimated);
2232 /* Update crtl->stack_alignment_estimated and use it later to align
2233 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
2234 exceptions since callgraph doesn't collect incoming stack alignment
2235 in this case. */
2236 if (flag_non_call_exceptions
2237 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
2238 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2239 else
2240 preferred_stack_boundary = crtl->preferred_stack_boundary;
2241 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
2242 crtl->stack_alignment_estimated = preferred_stack_boundary;
2243 if (preferred_stack_boundary > crtl->stack_alignment_needed)
2244 crtl->stack_alignment_needed = preferred_stack_boundary;
2246 crtl->stack_realign_needed
2247 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
2248 crtl->stack_realign_tried = crtl->stack_realign_needed;
2250 crtl->stack_realign_processed = true;
2252 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
2253 alignment. */
2254 gcc_assert (targetm.calls.get_drap_rtx != NULL);
2255 drap_rtx = targetm.calls.get_drap_rtx ();
2257 /* stack_realign_drap and drap_rtx must match. */
2258 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
2260 /* Do nothing if NULL is returned, which means DRAP is not needed. */
2261 if (NULL != drap_rtx)
2263 crtl->args.internal_arg_pointer = drap_rtx;
2265 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
2266 needed. */
2267 fixup_tail_calls ();
2271 /* Translate the intermediate representation contained in the CFG
2272 from GIMPLE trees to RTL.
2274 We do conversion per basic block and preserve/update the tree CFG.
2275 This implies we have to do some magic as the CFG can simultaneously
2276 consist of basic blocks containing RTL and GIMPLE trees. This can
2277 confuse the CFG hooks, so be careful to not manipulate CFG during
2278 the expansion. */
2280 static unsigned int
2281 gimple_expand_cfg (void)
2283 basic_block bb, init_block;
2284 sbitmap blocks;
2285 edge_iterator ei;
2286 edge e;
2288 /* Some backends want to know that we are expanding to RTL. */
2289 currently_expanding_to_rtl = 1;
2291 rtl_profile_for_bb (ENTRY_BLOCK_PTR);
2293 insn_locators_alloc ();
2294 if (!DECL_BUILT_IN (current_function_decl))
2296 /* Eventually, all FEs should explicitly set function_start_locus. */
2297 if (cfun->function_start_locus == UNKNOWN_LOCATION)
2298 set_curr_insn_source_location
2299 (DECL_SOURCE_LOCATION (current_function_decl));
2300 else
2301 set_curr_insn_source_location (cfun->function_start_locus);
2303 set_curr_insn_block (DECL_INITIAL (current_function_decl));
2304 prologue_locator = curr_insn_locator ();
2306 /* Make sure first insn is a note even if we don't want linenums.
2307 This makes sure the first insn will never be deleted.
2308 Also, final expects a note to appear there. */
2309 emit_note (NOTE_INSN_DELETED);
2311 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
2312 discover_nonconstant_array_refs ();
2314 targetm.expand_to_rtl_hook ();
2315 crtl->stack_alignment_needed = STACK_BOUNDARY;
2316 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
2317 crtl->stack_alignment_estimated = STACK_BOUNDARY;
2318 crtl->preferred_stack_boundary = STACK_BOUNDARY;
2319 cfun->cfg->max_jumptable_ents = 0;
2322 /* Expand the variables recorded during gimple lowering. */
2323 expand_used_vars ();
2325 /* Honor stack protection warnings. */
2326 if (warn_stack_protect)
2328 if (cfun->calls_alloca)
2329 warning (OPT_Wstack_protector,
2330 "not protecting local variables: variable length buffer");
2331 if (has_short_buffer && !crtl->stack_protect_guard)
2332 warning (OPT_Wstack_protector,
2333 "not protecting function: no buffer at least %d bytes long",
2334 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
2337 /* Set up parameters and prepare for return, for the function. */
2338 expand_function_start (current_function_decl);
2340 /* If this function is `main', emit a call to `__main'
2341 to run global initializers, etc. */
2342 if (DECL_NAME (current_function_decl)
2343 && MAIN_NAME_P (DECL_NAME (current_function_decl))
2344 && DECL_FILE_SCOPE_P (current_function_decl))
2345 expand_main_function ();
2347 /* Initialize the stack_protect_guard field. This must happen after the
2348 call to __main (if any) so that the external decl is initialized. */
2349 if (crtl->stack_protect_guard)
2350 stack_protect_prologue ();
2352 /* Update stack boundary if needed. */
2353 if (SUPPORTS_STACK_ALIGNMENT)
2355 /* Call update_stack_boundary here to update incoming stack
2356 boundary before TARGET_FUNCTION_OK_FOR_SIBCALL is called.
2357 TARGET_FUNCTION_OK_FOR_SIBCALL needs to know the accurate
2358 incoming stack alignment to check if it is OK to perform
2359 sibcall optimization since sibcall optimization will only
2360 align the outgoing stack to incoming stack boundary. */
2361 if (targetm.calls.update_stack_boundary)
2362 targetm.calls.update_stack_boundary ();
2364 /* The incoming stack frame has to be aligned at least at
2365 parm_stack_boundary. */
2366 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
2369 /* Register rtl specific functions for cfg. */
2370 rtl_register_cfg_hooks ();
2372 init_block = construct_init_block ();
2374 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
2375 remaining edges in expand_gimple_basic_block. */
2376 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2377 e->flags &= ~EDGE_EXECUTABLE;
2379 lab_rtx_for_bb = pointer_map_create ();
2380 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
2381 bb = expand_gimple_basic_block (bb);
2383 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
2384 conservatively to true until they are all profile aware. */
2385 pointer_map_destroy (lab_rtx_for_bb);
2386 free_histograms ();
2388 construct_exit_block ();
2389 set_curr_insn_block (DECL_INITIAL (current_function_decl));
2390 insn_locators_finalize ();
2392 /* We're done expanding trees to RTL. */
2393 currently_expanding_to_rtl = 0;
2395 /* Convert tree EH labels to RTL EH labels and zap the tree EH table. */
2396 convert_from_eh_region_ranges ();
2397 set_eh_throw_stmt_table (cfun, NULL);
2399 rebuild_jump_labels (get_insns ());
2400 find_exception_handler_labels ();
2402 blocks = sbitmap_alloc (last_basic_block);
2403 sbitmap_ones (blocks);
2404 find_many_sub_basic_blocks (blocks);
2405 purge_all_dead_edges ();
2406 sbitmap_free (blocks);
2408 compact_blocks ();
2410 expand_stack_alignment ();
2412 #ifdef ENABLE_CHECKING
2413 verify_flow_info ();
2414 #endif
2416 /* There's no need to defer outputting this function any more; we
2417 know we want to output it. */
2418 DECL_DEFER_OUTPUT (current_function_decl) = 0;
2420 /* Now that we're done expanding trees to RTL, we shouldn't have any
2421 more CONCATs anywhere. */
2422 generating_concat_p = 0;
2424 if (dump_file)
2426 fprintf (dump_file,
2427 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
2428 /* And the pass manager will dump RTL for us. */
2431 /* If we're emitting a nested function, make sure its parent gets
2432 emitted as well. Doing otherwise confuses debug info. */
2434 tree parent;
2435 for (parent = DECL_CONTEXT (current_function_decl);
2436 parent != NULL_TREE;
2437 parent = get_containing_scope (parent))
2438 if (TREE_CODE (parent) == FUNCTION_DECL)
2439 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
2442 /* We are now committed to emitting code for this function. Do any
2443 preparation, such as emitting abstract debug info for the inline
2444 before it gets mangled by optimization. */
2445 if (cgraph_function_possibly_inlined_p (current_function_decl))
2446 (*debug_hooks->outlining_inline_function) (current_function_decl);
2448 TREE_ASM_WRITTEN (current_function_decl) = 1;
2450 /* After expanding, the return labels are no longer needed. */
2451 return_label = NULL;
2452 naked_return_label = NULL;
2453 /* Tag the blocks with a depth number so that change_scope can find
2454 the common parent easily. */
2455 set_block_levels (DECL_INITIAL (cfun->decl), 0);
2456 default_rtl_profile ();
2457 return 0;
2460 struct rtl_opt_pass pass_expand =
2463 RTL_PASS,
2464 "expand", /* name */
2465 NULL, /* gate */
2466 gimple_expand_cfg, /* execute */
2467 NULL, /* sub */
2468 NULL, /* next */
2469 0, /* static_pass_number */
2470 TV_EXPAND, /* tv_id */
2471 /* ??? If TER is enabled, we actually receive GENERIC. */
2472 PROP_gimple_leh | PROP_cfg, /* properties_required */
2473 PROP_rtl, /* properties_provided */
2474 PROP_trees, /* properties_destroyed */
2475 0, /* todo_flags_start */
2476 TODO_dump_func, /* todo_flags_finish */