OSDN Git Service

* builtins.c (expand_builtin_profile_func): Avoid C++ keywords.
[pf3gnuchains/gcc-fork.git] / gcc / cfgexpand.c
1 /* A pass for lowering trees to RTL.
2    Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "basic-block.h"
28 #include "function.h"
29 #include "expr.h"
30 #include "langhooks.h"
31 #include "tree-flow.h"
32 #include "timevar.h"
33 #include "tree-dump.h"
34 #include "tree-pass.h"
35 #include "except.h"
36 #include "flags.h"
37 #include "diagnostic.h"
38 #include "toplev.h"
39 #include "debug.h"
40 #include "params.h"
41 #include "tree-inline.h"
42 #include "value-prof.h"
43 #include "target.h"
44
45
46 /* Return an expression tree corresponding to the RHS of GIMPLE
47    statement STMT.  */
48
49 tree
50 gimple_assign_rhs_to_tree (gimple stmt)
51 {
52   tree t;
53   enum gimple_rhs_class grhs_class;
54     
55   grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
56
57   if (grhs_class == GIMPLE_BINARY_RHS)
58     t = build2 (gimple_assign_rhs_code (stmt),
59                 TREE_TYPE (gimple_assign_lhs (stmt)),
60                 gimple_assign_rhs1 (stmt),
61                 gimple_assign_rhs2 (stmt));
62   else if (grhs_class == GIMPLE_UNARY_RHS)
63     t = build1 (gimple_assign_rhs_code (stmt),
64                 TREE_TYPE (gimple_assign_lhs (stmt)),
65                 gimple_assign_rhs1 (stmt));
66   else if (grhs_class == GIMPLE_SINGLE_RHS)
67     t = gimple_assign_rhs1 (stmt);
68   else
69     gcc_unreachable ();
70
71   return t;
72 }
73
74 /* Return an expression tree corresponding to the PREDICATE of GIMPLE_COND
75    statement STMT.  */
76
77 static tree
78 gimple_cond_pred_to_tree (gimple stmt)
79 {
80   return build2 (gimple_cond_code (stmt), boolean_type_node,
81                  gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
82 }
83
84 /* Helper for gimple_to_tree.  Set EXPR_LOCATION for every expression
85    inside *TP.  DATA is the location to set.  */
86
87 static tree
88 set_expr_location_r (tree *tp, int *ws ATTRIBUTE_UNUSED, void *data)
89 {
90   location_t *loc = (location_t *) data;
91   if (EXPR_P (*tp))
92     SET_EXPR_LOCATION (*tp, *loc);
93
94   return NULL_TREE;
95 }
96
97
98 /* RTL expansion has traditionally been done on trees, so the
99    transition to doing it on GIMPLE tuples is very invasive to the RTL
100    expander.  To facilitate the transition, this function takes a
101    GIMPLE tuple STMT and returns the same statement in the form of a
102    tree.  */
103
104 static tree
105 gimple_to_tree (gimple stmt)
106 {
107   tree t;
108   int rn;
109   tree_ann_common_t ann;
110   location_t loc;
111
112   switch (gimple_code (stmt))
113     {
114     case GIMPLE_ASSIGN:
115       {
116         tree lhs = gimple_assign_lhs (stmt);
117
118         t = gimple_assign_rhs_to_tree (stmt);
119         t = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, t);
120         if (gimple_assign_nontemporal_move_p (stmt))
121           MOVE_NONTEMPORAL (t) = true;
122       }
123       break;
124                                          
125     case GIMPLE_COND:
126       t = gimple_cond_pred_to_tree (stmt);
127       t = build3 (COND_EXPR, void_type_node, t, NULL_TREE, NULL_TREE);
128       break;
129
130     case GIMPLE_GOTO:
131       t = build1 (GOTO_EXPR, void_type_node, gimple_goto_dest (stmt));
132       break;
133
134     case GIMPLE_LABEL:
135       t = build1 (LABEL_EXPR, void_type_node, gimple_label_label (stmt));
136       break;
137
138     case GIMPLE_RETURN:
139       {
140         tree retval = gimple_return_retval (stmt);
141
142         if (retval && retval != error_mark_node)
143           {
144             tree result = DECL_RESULT (current_function_decl);
145
146             /* If we are not returning the current function's RESULT_DECL,
147                build an assignment to it.  */
148             if (retval != result)
149               {
150                 /* I believe that a function's RESULT_DECL is unique.  */
151                 gcc_assert (TREE_CODE (retval) != RESULT_DECL);
152
153                 retval = build2 (MODIFY_EXPR, TREE_TYPE (result),
154                                  result, retval);
155               }
156           }
157         t = build1 (RETURN_EXPR, void_type_node, retval);
158       }
159       break;
160
161     case GIMPLE_ASM:
162       {
163         size_t i, n;
164         tree out, in, cl;
165         const char *s;
166
167         out = NULL_TREE;
168         n = gimple_asm_noutputs (stmt);
169         if (n > 0)
170           {
171             t = out = gimple_asm_output_op (stmt, 0);
172             for (i = 1; i < n; i++)
173               {
174                 TREE_CHAIN (t) = gimple_asm_output_op (stmt, i);
175                 t = gimple_asm_output_op (stmt, i);
176               }
177           }
178
179         in = NULL_TREE;
180         n = gimple_asm_ninputs (stmt);
181         if (n > 0)
182           {
183             t = in = gimple_asm_input_op (stmt, 0);
184             for (i = 1; i < n; i++)
185               {
186                 TREE_CHAIN (t) = gimple_asm_input_op (stmt, i);
187                 t = gimple_asm_input_op (stmt, i);
188               }
189           }
190
191         cl = NULL_TREE;
192         n = gimple_asm_nclobbers (stmt);
193         if (n > 0)
194           {
195             t = cl = gimple_asm_clobber_op (stmt, 0);
196             for (i = 1; i < n; i++)
197               {
198                 TREE_CHAIN (t) = gimple_asm_clobber_op (stmt, i);
199                 t = gimple_asm_clobber_op (stmt, i);
200               }
201           }
202
203         s = gimple_asm_string (stmt);
204         t = build4 (ASM_EXPR, void_type_node, build_string (strlen (s), s),
205                     out, in, cl);
206         ASM_VOLATILE_P (t) = gimple_asm_volatile_p (stmt);
207         ASM_INPUT_P (t) = gimple_asm_input_p (stmt);
208       }
209     break;
210
211     case GIMPLE_CALL:
212       {
213         size_t i;
214         tree fn;
215         tree_ann_common_t ann;
216         
217         t = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
218
219         fn = gimple_call_fn (stmt);
220         if (TREE_CODE (fn) == FUNCTION_DECL)
221           CALL_EXPR_FN (t) = build1 (ADDR_EXPR,
222                                      build_pointer_type (TREE_TYPE (fn)),
223                                      fn);
224         else
225           CALL_EXPR_FN (t) = fn;
226         
227         TREE_TYPE (t) = gimple_call_return_type (stmt);
228
229         CALL_EXPR_STATIC_CHAIN (t) = gimple_call_chain (stmt);
230
231         for (i = 0; i < gimple_call_num_args (stmt); i++)
232           CALL_EXPR_ARG (t, i) = gimple_call_arg (stmt, i);
233
234         if (!(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
235           TREE_SIDE_EFFECTS (t) = 1;
236
237         if (gimple_call_flags (stmt) & ECF_NOTHROW)
238           TREE_NOTHROW (t) = 1;
239
240         CALL_EXPR_TAILCALL (t) = gimple_call_tail_p (stmt);
241         CALL_EXPR_RETURN_SLOT_OPT (t) = gimple_call_return_slot_opt_p (stmt);
242         CALL_FROM_THUNK_P (t) = gimple_call_from_thunk_p (stmt);
243         CALL_CANNOT_INLINE_P (t) = gimple_call_cannot_inline_p (stmt);
244         CALL_EXPR_VA_ARG_PACK (t) = gimple_call_va_arg_pack_p (stmt);
245
246         /* If the call has a LHS then create a MODIFY_EXPR to hold it.  */
247         {
248           tree lhs = gimple_call_lhs (stmt);
249
250           if (lhs)
251             t = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, t);
252         }
253
254         /* Record the original call statement, as it may be used
255            to retrieve profile information during expansion.  */
256         if (TREE_CODE (fn) == FUNCTION_DECL && DECL_BUILT_IN (fn))
257           {
258             ann = get_tree_common_ann (t);
259             ann->stmt = stmt;
260           }
261       }
262     break;
263
264     case GIMPLE_SWITCH:
265       {
266         tree label_vec;
267         size_t i;
268         tree elt = gimple_switch_label (stmt, 0);
269
270         label_vec = make_tree_vec (gimple_switch_num_labels (stmt));
271
272         if (!CASE_LOW (elt) && !CASE_HIGH (elt))
273           {
274             for (i = 1; i < gimple_switch_num_labels (stmt); i++)
275               TREE_VEC_ELT (label_vec, i - 1) = gimple_switch_label (stmt, i);
276
277             /* The default case in a SWITCH_EXPR must be at the end of
278                the label vector.  */
279             TREE_VEC_ELT (label_vec, i - 1) = gimple_switch_label (stmt, 0);
280           }
281         else
282           {
283             for (i = 0; i < gimple_switch_num_labels (stmt); i++)
284               TREE_VEC_ELT (label_vec, i) = gimple_switch_label (stmt, i);
285           }
286
287         t = build3 (SWITCH_EXPR, void_type_node, gimple_switch_index (stmt),
288                     NULL, label_vec);
289       }
290     break;
291
292     case GIMPLE_NOP:
293     case GIMPLE_PREDICT:
294       t = build1 (NOP_EXPR, void_type_node, size_zero_node);
295       break;
296
297     case GIMPLE_RESX:
298       t = build_resx (gimple_resx_region (stmt));
299       break;
300         
301     default:
302       if (errorcount == 0)
303         {
304           error ("Unrecognized GIMPLE statement during RTL expansion");
305           print_gimple_stmt (stderr, stmt, 4, 0);
306           gcc_unreachable ();
307         }
308       else
309         {
310           /* Ignore any bad gimple codes if we're going to die anyhow,
311              so we can at least set TREE_ASM_WRITTEN and have the rest
312              of compilation advance without sudden ICE death.  */
313           t = build1 (NOP_EXPR, void_type_node, size_zero_node);
314           break;
315         }
316     }
317
318   /* If STMT is inside an exception region, record it in the generated
319      expression.  */
320   rn = lookup_stmt_eh_region (stmt);
321   if (rn >= 0)
322     {
323       tree call = get_call_expr_in (t);
324
325       ann = get_tree_common_ann (t);
326       ann->rn = rn;
327       
328       /* For a CALL_EXPR on the RHS of an assignment, calls.c looks up
329          the CALL_EXPR not the assignment statment for EH region number. */
330       if (call && call != t)
331         {
332           ann = get_tree_common_ann (call);
333           ann->rn = rn;
334         }
335     }
336
337   /* Set EXPR_LOCATION in all the embedded expressions.  */
338   loc = gimple_location (stmt);
339   walk_tree (&t, set_expr_location_r, (void *) &loc, NULL);
340
341   TREE_BLOCK (t) = gimple_block (stmt);
342
343   return t;
344 }
345
346
347 /* Release back to GC memory allocated by gimple_to_tree.  */
348
349 static void
350 release_stmt_tree (gimple stmt, tree stmt_tree)
351 {
352   tree_ann_common_t ann;
353
354   switch (gimple_code (stmt))
355     {
356     case GIMPLE_ASSIGN:
357       if (get_gimple_rhs_class (gimple_expr_code (stmt)) != GIMPLE_SINGLE_RHS)
358         ggc_free (TREE_OPERAND (stmt_tree, 1));
359       break;
360     case GIMPLE_COND:
361       ggc_free (COND_EXPR_COND (stmt_tree));
362       break;
363     case GIMPLE_RETURN:
364       if (TREE_OPERAND (stmt_tree, 0)
365           && TREE_CODE (TREE_OPERAND (stmt_tree, 0)) == MODIFY_EXPR)
366         ggc_free (TREE_OPERAND (stmt_tree, 0));
367       break;
368     case GIMPLE_CALL:
369       if (gimple_call_lhs (stmt))
370         {
371           if (TREE_CODE (gimple_call_fn (stmt)) == FUNCTION_DECL)
372             ggc_free (CALL_EXPR_FN (TREE_OPERAND (stmt_tree, 1)));
373           ann = tree_common_ann (TREE_OPERAND (stmt_tree, 1));
374           if (ann)
375             ggc_free (ann);
376           ggc_free (TREE_OPERAND (stmt_tree, 1));
377         }
378       else if (TREE_CODE (gimple_call_fn (stmt)) == FUNCTION_DECL)
379         ggc_free (CALL_EXPR_FN (stmt_tree));
380       break;
381     default:
382       break;
383     }
384   ann = tree_common_ann (stmt_tree);
385   if (ann)
386     ggc_free (ann);
387   ggc_free (stmt_tree);
388 }
389
390
391 /* Verify that there is exactly single jump instruction since last and attach
392    REG_BR_PROB note specifying probability.
393    ??? We really ought to pass the probability down to RTL expanders and let it
394    re-distribute it when the conditional expands into multiple conditionals.
395    This is however difficult to do.  */
396 void
397 add_reg_br_prob_note (rtx last, int probability)
398 {
399   if (profile_status == PROFILE_ABSENT)
400     return;
401   for (last = NEXT_INSN (last); last && NEXT_INSN (last); last = NEXT_INSN (last))
402     if (JUMP_P (last))
403       {
404         /* It is common to emit condjump-around-jump sequence when we don't know
405            how to reverse the conditional.  Special case this.  */
406         if (!any_condjump_p (last)
407             || !JUMP_P (NEXT_INSN (last))
408             || !simplejump_p (NEXT_INSN (last))
409             || !NEXT_INSN (NEXT_INSN (last))
410             || !BARRIER_P (NEXT_INSN (NEXT_INSN (last)))
411             || !NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))
412             || !LABEL_P (NEXT_INSN (NEXT_INSN (NEXT_INSN (last))))
413             || NEXT_INSN (NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))))
414           goto failed;
415         gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
416         add_reg_note (last, REG_BR_PROB,
417                       GEN_INT (REG_BR_PROB_BASE - probability));
418         return;
419       }
420   if (!last || !JUMP_P (last) || !any_condjump_p (last))
421     goto failed;
422   gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
423   add_reg_note (last, REG_BR_PROB, GEN_INT (probability));
424   return;
425 failed:
426   if (dump_file)
427     fprintf (dump_file, "Failed to add probability note\n");
428 }
429
430
431 #ifndef STACK_ALIGNMENT_NEEDED
432 #define STACK_ALIGNMENT_NEEDED 1
433 #endif
434
435
436 /* This structure holds data relevant to one variable that will be
437    placed in a stack slot.  */
438 struct stack_var
439 {
440   /* The Variable.  */
441   tree decl;
442
443   /* The offset of the variable.  During partitioning, this is the
444      offset relative to the partition.  After partitioning, this
445      is relative to the stack frame.  */
446   HOST_WIDE_INT offset;
447
448   /* Initially, the size of the variable.  Later, the size of the partition,
449      if this variable becomes it's partition's representative.  */
450   HOST_WIDE_INT size;
451
452   /* The *byte* alignment required for this variable.  Or as, with the
453      size, the alignment for this partition.  */
454   unsigned int alignb;
455
456   /* The partition representative.  */
457   size_t representative;
458
459   /* The next stack variable in the partition, or EOC.  */
460   size_t next;
461 };
462
463 #define EOC  ((size_t)-1)
464
465 /* We have an array of such objects while deciding allocation.  */
466 static struct stack_var *stack_vars;
467 static size_t stack_vars_alloc;
468 static size_t stack_vars_num;
469
470 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
471    is non-decreasing.  */
472 static size_t *stack_vars_sorted;
473
474 /* We have an interference graph between such objects.  This graph
475    is lower triangular.  */
476 static bool *stack_vars_conflict;
477 static size_t stack_vars_conflict_alloc;
478
479 /* The phase of the stack frame.  This is the known misalignment of
480    virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY.  That is,
481    (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0.  */
482 static int frame_phase;
483
484 /* Used during expand_used_vars to remember if we saw any decls for
485    which we'd like to enable stack smashing protection.  */
486 static bool has_protected_decls;
487
488 /* Used during expand_used_vars.  Remember if we say a character buffer
489    smaller than our cutoff threshold.  Used for -Wstack-protector.  */
490 static bool has_short_buffer;
491
492 /* Discover the byte alignment to use for DECL.  Ignore alignment
493    we can't do with expected alignment of the stack boundary.  */
494
495 static unsigned int
496 get_decl_align_unit (tree decl)
497 {
498   unsigned int align;
499
500   align = DECL_ALIGN (decl);
501   align = LOCAL_ALIGNMENT (TREE_TYPE (decl), align);
502
503   if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
504     align = MAX_SUPPORTED_STACK_ALIGNMENT;
505
506   if (SUPPORTS_STACK_ALIGNMENT)
507     {
508       if (crtl->stack_alignment_estimated < align)
509         {
510           gcc_assert(!crtl->stack_realign_processed);
511           crtl->stack_alignment_estimated = align;
512         }
513     }
514
515   /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
516      So here we only make sure stack_alignment_needed >= align.  */
517   if (crtl->stack_alignment_needed < align)
518     crtl->stack_alignment_needed = align;
519   if (crtl->max_used_stack_slot_alignment < crtl->stack_alignment_needed)
520     crtl->max_used_stack_slot_alignment = crtl->stack_alignment_needed;
521
522   return align / BITS_PER_UNIT;
523 }
524
525 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
526    Return the frame offset.  */
527
528 static HOST_WIDE_INT
529 alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align)
530 {
531   HOST_WIDE_INT offset, new_frame_offset;
532
533   new_frame_offset = frame_offset;
534   if (FRAME_GROWS_DOWNWARD)
535     {
536       new_frame_offset -= size + frame_phase;
537       new_frame_offset &= -align;
538       new_frame_offset += frame_phase;
539       offset = new_frame_offset;
540     }
541   else
542     {
543       new_frame_offset -= frame_phase;
544       new_frame_offset += align - 1;
545       new_frame_offset &= -align;
546       new_frame_offset += frame_phase;
547       offset = new_frame_offset;
548       new_frame_offset += size;
549     }
550   frame_offset = new_frame_offset;
551
552   if (frame_offset_overflow (frame_offset, cfun->decl))
553     frame_offset = offset = 0;
554
555   return offset;
556 }
557
558 /* Accumulate DECL into STACK_VARS.  */
559
560 static void
561 add_stack_var (tree decl)
562 {
563   if (stack_vars_num >= stack_vars_alloc)
564     {
565       if (stack_vars_alloc)
566         stack_vars_alloc = stack_vars_alloc * 3 / 2;
567       else
568         stack_vars_alloc = 32;
569       stack_vars
570         = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
571     }
572   stack_vars[stack_vars_num].decl = decl;
573   stack_vars[stack_vars_num].offset = 0;
574   stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
575   stack_vars[stack_vars_num].alignb = get_decl_align_unit (decl);
576
577   /* All variables are initially in their own partition.  */
578   stack_vars[stack_vars_num].representative = stack_vars_num;
579   stack_vars[stack_vars_num].next = EOC;
580
581   /* Ensure that this decl doesn't get put onto the list twice.  */
582   SET_DECL_RTL (decl, pc_rtx);
583
584   stack_vars_num++;
585 }
586
587 /* Compute the linear index of a lower-triangular coordinate (I, J).  */
588
589 static size_t
590 triangular_index (size_t i, size_t j)
591 {
592   if (i < j)
593     {
594       size_t t;
595       t = i, i = j, j = t;
596     }
597   return (i * (i + 1)) / 2 + j;
598 }
599
600 /* Ensure that STACK_VARS_CONFLICT is large enough for N objects.  */
601
602 static void
603 resize_stack_vars_conflict (size_t n)
604 {
605   size_t size = triangular_index (n-1, n-1) + 1;
606
607   if (size <= stack_vars_conflict_alloc)
608     return;
609
610   stack_vars_conflict = XRESIZEVEC (bool, stack_vars_conflict, size);
611   memset (stack_vars_conflict + stack_vars_conflict_alloc, 0,
612           (size - stack_vars_conflict_alloc) * sizeof (bool));
613   stack_vars_conflict_alloc = size;
614 }
615
616 /* Make the decls associated with luid's X and Y conflict.  */
617
618 static void
619 add_stack_var_conflict (size_t x, size_t y)
620 {
621   size_t index = triangular_index (x, y);
622   gcc_assert (index < stack_vars_conflict_alloc);
623   stack_vars_conflict[index] = true;
624 }
625
626 /* Check whether the decls associated with luid's X and Y conflict.  */
627
628 static bool
629 stack_var_conflict_p (size_t x, size_t y)
630 {
631   size_t index = triangular_index (x, y);
632   gcc_assert (index < stack_vars_conflict_alloc);
633   return stack_vars_conflict[index];
634 }
635  
636 /* Returns true if TYPE is or contains a union type.  */
637
638 static bool
639 aggregate_contains_union_type (tree type)
640 {
641   tree field;
642
643   if (TREE_CODE (type) == UNION_TYPE
644       || TREE_CODE (type) == QUAL_UNION_TYPE)
645     return true;
646   if (TREE_CODE (type) == ARRAY_TYPE)
647     return aggregate_contains_union_type (TREE_TYPE (type));
648   if (TREE_CODE (type) != RECORD_TYPE)
649     return false;
650
651   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
652     if (TREE_CODE (field) == FIELD_DECL)
653       if (aggregate_contains_union_type (TREE_TYPE (field)))
654         return true;
655
656   return false;
657 }
658
659 /* A subroutine of expand_used_vars.  If two variables X and Y have alias
660    sets that do not conflict, then do add a conflict for these variables
661    in the interference graph.  We also need to make sure to add conflicts
662    for union containing structures.  Else RTL alias analysis comes along
663    and due to type based aliasing rules decides that for two overlapping
664    union temporaries { short s; int i; } accesses to the same mem through
665    different types may not alias and happily reorders stores across
666    life-time boundaries of the temporaries (See PR25654).
667    We also have to mind MEM_IN_STRUCT_P and MEM_SCALAR_P.  */
668
669 static void
670 add_alias_set_conflicts (void)
671 {
672   size_t i, j, n = stack_vars_num;
673
674   for (i = 0; i < n; ++i)
675     {
676       tree type_i = TREE_TYPE (stack_vars[i].decl);
677       bool aggr_i = AGGREGATE_TYPE_P (type_i);
678       bool contains_union;
679
680       contains_union = aggregate_contains_union_type (type_i);
681       for (j = 0; j < i; ++j)
682         {
683           tree type_j = TREE_TYPE (stack_vars[j].decl);
684           bool aggr_j = AGGREGATE_TYPE_P (type_j);
685           if (aggr_i != aggr_j
686               /* Either the objects conflict by means of type based
687                  aliasing rules, or we need to add a conflict.  */
688               || !objects_must_conflict_p (type_i, type_j)
689               /* In case the types do not conflict ensure that access
690                  to elements will conflict.  In case of unions we have
691                  to be careful as type based aliasing rules may say
692                  access to the same memory does not conflict.  So play
693                  safe and add a conflict in this case.  */
694               || contains_union)
695             add_stack_var_conflict (i, j);
696         }
697     }
698 }
699
700 /* A subroutine of partition_stack_vars.  A comparison function for qsort,
701    sorting an array of indices by the size of the object.  */
702
703 static int
704 stack_var_size_cmp (const void *a, const void *b)
705 {
706   HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size;
707   HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size;
708   unsigned int uida = DECL_UID (stack_vars[*(const size_t *)a].decl);
709   unsigned int uidb = DECL_UID (stack_vars[*(const size_t *)b].decl);
710
711   if (sa < sb)
712     return -1;
713   if (sa > sb)
714     return 1;
715   /* For stack variables of the same size use the uid of the decl
716      to make the sort stable.  */
717   if (uida < uidb)
718     return -1;
719   if (uida > uidb)
720     return 1;
721   return 0;
722 }
723
724 /* A subroutine of partition_stack_vars.  The UNION portion of a UNION/FIND
725    partitioning algorithm.  Partitions A and B are known to be non-conflicting.
726    Merge them into a single partition A.
727
728    At the same time, add OFFSET to all variables in partition B.  At the end
729    of the partitioning process we've have a nice block easy to lay out within
730    the stack frame.  */
731
732 static void
733 union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset)
734 {
735   size_t i, last;
736
737   /* Update each element of partition B with the given offset,
738      and merge them into partition A.  */
739   for (last = i = b; i != EOC; last = i, i = stack_vars[i].next)
740     {
741       stack_vars[i].offset += offset;
742       stack_vars[i].representative = a;
743     }
744   stack_vars[last].next = stack_vars[a].next;
745   stack_vars[a].next = b;
746
747   /* Update the required alignment of partition A to account for B.  */
748   if (stack_vars[a].alignb < stack_vars[b].alignb)
749     stack_vars[a].alignb = stack_vars[b].alignb;
750
751   /* Update the interference graph and merge the conflicts.  */
752   for (last = stack_vars_num, i = 0; i < last; ++i)
753     if (stack_var_conflict_p (b, i))
754       add_stack_var_conflict (a, i);
755 }
756
757 /* A subroutine of expand_used_vars.  Binpack the variables into
758    partitions constrained by the interference graph.  The overall
759    algorithm used is as follows:
760
761         Sort the objects by size.
762         For each object A {
763           S = size(A)
764           O = 0
765           loop {
766             Look for the largest non-conflicting object B with size <= S.
767             UNION (A, B)
768             offset(B) = O
769             O += size(B)
770             S -= size(B)
771           }
772         }
773 */
774
775 static void
776 partition_stack_vars (void)
777 {
778   size_t si, sj, n = stack_vars_num;
779
780   stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
781   for (si = 0; si < n; ++si)
782     stack_vars_sorted[si] = si;
783
784   if (n == 1)
785     return;
786
787   qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp);
788
789   /* Special case: detect when all variables conflict, and thus we can't
790      do anything during the partitioning loop.  It isn't uncommon (with
791      C code at least) to declare all variables at the top of the function,
792      and if we're not inlining, then all variables will be in the same scope.
793      Take advantage of very fast libc routines for this scan.  */
794   gcc_assert (sizeof(bool) == sizeof(char));
795   if (memchr (stack_vars_conflict, false, stack_vars_conflict_alloc) == NULL)
796     return;
797
798   for (si = 0; si < n; ++si)
799     {
800       size_t i = stack_vars_sorted[si];
801       HOST_WIDE_INT isize = stack_vars[i].size;
802       HOST_WIDE_INT offset = 0;
803
804       for (sj = si; sj-- > 0; )
805         {
806           size_t j = stack_vars_sorted[sj];
807           HOST_WIDE_INT jsize = stack_vars[j].size;
808           unsigned int jalign = stack_vars[j].alignb;
809
810           /* Ignore objects that aren't partition representatives.  */
811           if (stack_vars[j].representative != j)
812             continue;
813
814           /* Ignore objects too large for the remaining space.  */
815           if (isize < jsize)
816             continue;
817
818           /* Ignore conflicting objects.  */
819           if (stack_var_conflict_p (i, j))
820             continue;
821
822           /* Refine the remaining space check to include alignment.  */
823           if (offset & (jalign - 1))
824             {
825               HOST_WIDE_INT toff = offset;
826               toff += jalign - 1;
827               toff &= -(HOST_WIDE_INT)jalign;
828               if (isize - (toff - offset) < jsize)
829                 continue;
830
831               isize -= toff - offset;
832               offset = toff;
833             }
834
835           /* UNION the objects, placing J at OFFSET.  */
836           union_stack_vars (i, j, offset);
837
838           isize -= jsize;
839           if (isize == 0)
840             break;
841         }
842     }
843 }
844
845 /* A debugging aid for expand_used_vars.  Dump the generated partitions.  */
846
847 static void
848 dump_stack_var_partition (void)
849 {
850   size_t si, i, j, n = stack_vars_num;
851
852   for (si = 0; si < n; ++si)
853     {
854       i = stack_vars_sorted[si];
855
856       /* Skip variables that aren't partition representatives, for now.  */
857       if (stack_vars[i].representative != i)
858         continue;
859
860       fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
861                " align %u\n", (unsigned long) i, stack_vars[i].size,
862                stack_vars[i].alignb);
863
864       for (j = i; j != EOC; j = stack_vars[j].next)
865         {
866           fputc ('\t', dump_file);
867           print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
868           fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n",
869                    stack_vars[j].offset);
870         }
871     }
872 }
873
874 /* Assign rtl to DECL at frame offset OFFSET.  */
875
876 static void
877 expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset)
878 {
879   HOST_WIDE_INT align;
880   rtx x;
881
882   /* If this fails, we've overflowed the stack frame.  Error nicely?  */
883   gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
884
885   x = plus_constant (virtual_stack_vars_rtx, offset);
886   x = gen_rtx_MEM (DECL_MODE (decl), x);
887
888   /* Set alignment we actually gave this decl.  */
889   offset -= frame_phase;
890   align = offset & -offset;
891   align *= BITS_PER_UNIT;
892   if (align > STACK_BOUNDARY || align == 0)
893     align = STACK_BOUNDARY;
894   DECL_ALIGN (decl) = align;
895   DECL_USER_ALIGN (decl) = 0;
896
897   set_mem_attributes (x, decl, true);
898   SET_DECL_RTL (decl, x);
899 }
900
901 /* A subroutine of expand_used_vars.  Give each partition representative
902    a unique location within the stack frame.  Update each partition member
903    with that location.  */
904
905 static void
906 expand_stack_vars (bool (*pred) (tree))
907 {
908   size_t si, i, j, n = stack_vars_num;
909
910   for (si = 0; si < n; ++si)
911     {
912       HOST_WIDE_INT offset;
913
914       i = stack_vars_sorted[si];
915
916       /* Skip variables that aren't partition representatives, for now.  */
917       if (stack_vars[i].representative != i)
918         continue;
919
920       /* Skip variables that have already had rtl assigned.  See also
921          add_stack_var where we perpetrate this pc_rtx hack.  */
922       if (DECL_RTL (stack_vars[i].decl) != pc_rtx)
923         continue;
924
925       /* Check the predicate to see whether this variable should be
926          allocated in this pass.  */
927       if (pred && !pred (stack_vars[i].decl))
928         continue;
929
930       offset = alloc_stack_frame_space (stack_vars[i].size,
931                                         stack_vars[i].alignb);
932
933       /* Create rtl for each variable based on their location within the
934          partition.  */
935       for (j = i; j != EOC; j = stack_vars[j].next)
936         {
937           gcc_assert (stack_vars[j].offset <= stack_vars[i].size);
938           expand_one_stack_var_at (stack_vars[j].decl,
939                                    stack_vars[j].offset + offset);
940         }
941     }
942 }
943
944 /* Take into account all sizes of partitions and reset DECL_RTLs.  */
945 static HOST_WIDE_INT
946 account_stack_vars (void)
947 {
948   size_t si, j, i, n = stack_vars_num;
949   HOST_WIDE_INT size = 0;
950
951   for (si = 0; si < n; ++si)
952     {
953       i = stack_vars_sorted[si];
954
955       /* Skip variables that aren't partition representatives, for now.  */
956       if (stack_vars[i].representative != i)
957         continue;
958
959       size += stack_vars[i].size;
960       for (j = i; j != EOC; j = stack_vars[j].next)
961         SET_DECL_RTL (stack_vars[j].decl, NULL);
962     }
963   return size;
964 }
965
966 /* A subroutine of expand_one_var.  Called to immediately assign rtl
967    to a variable to be allocated in the stack frame.  */
968
969 static void
970 expand_one_stack_var (tree var)
971 {
972   HOST_WIDE_INT size, offset, align;
973
974   size = tree_low_cst (DECL_SIZE_UNIT (var), 1);
975   align = get_decl_align_unit (var);
976   offset = alloc_stack_frame_space (size, align);
977
978   expand_one_stack_var_at (var, offset);
979 }
980
981 /* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL
982    that will reside in a hard register.  */
983
984 static void
985 expand_one_hard_reg_var (tree var)
986 {
987   rest_of_decl_compilation (var, 0, 0);
988 }
989
990 /* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL
991    that will reside in a pseudo register.  */
992
993 static void
994 expand_one_register_var (tree var)
995 {
996   tree type = TREE_TYPE (var);
997   int unsignedp = TYPE_UNSIGNED (type);
998   enum machine_mode reg_mode
999     = promote_mode (type, DECL_MODE (var), &unsignedp, 0);
1000   rtx x = gen_reg_rtx (reg_mode);
1001
1002   SET_DECL_RTL (var, x);
1003
1004   /* Note if the object is a user variable.  */
1005   if (!DECL_ARTIFICIAL (var))
1006       mark_user_reg (x);
1007
1008   if (POINTER_TYPE_P (type))
1009     mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
1010 }
1011
1012 /* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL that
1013    has some associated error, e.g. its type is error-mark.  We just need
1014    to pick something that won't crash the rest of the compiler.  */
1015
1016 static void
1017 expand_one_error_var (tree var)
1018 {
1019   enum machine_mode mode = DECL_MODE (var);
1020   rtx x;
1021
1022   if (mode == BLKmode)
1023     x = gen_rtx_MEM (BLKmode, const0_rtx);
1024   else if (mode == VOIDmode)
1025     x = const0_rtx;
1026   else
1027     x = gen_reg_rtx (mode);
1028
1029   SET_DECL_RTL (var, x);
1030 }
1031
1032 /* A subroutine of expand_one_var.  VAR is a variable that will be
1033    allocated to the local stack frame.  Return true if we wish to
1034    add VAR to STACK_VARS so that it will be coalesced with other
1035    variables.  Return false to allocate VAR immediately.
1036
1037    This function is used to reduce the number of variables considered
1038    for coalescing, which reduces the size of the quadratic problem.  */
1039
1040 static bool
1041 defer_stack_allocation (tree var, bool toplevel)
1042 {
1043   /* If stack protection is enabled, *all* stack variables must be deferred,
1044      so that we can re-order the strings to the top of the frame.  */
1045   if (flag_stack_protect)
1046     return true;
1047
1048   /* Variables in the outermost scope automatically conflict with
1049      every other variable.  The only reason to want to defer them
1050      at all is that, after sorting, we can more efficiently pack
1051      small variables in the stack frame.  Continue to defer at -O2.  */
1052   if (toplevel && optimize < 2)
1053     return false;
1054
1055   /* Without optimization, *most* variables are allocated from the
1056      stack, which makes the quadratic problem large exactly when we
1057      want compilation to proceed as quickly as possible.  On the
1058      other hand, we don't want the function's stack frame size to
1059      get completely out of hand.  So we avoid adding scalars and
1060      "small" aggregates to the list at all.  */
1061   if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32)
1062     return false;
1063
1064   return true;
1065 }
1066
1067 /* A subroutine of expand_used_vars.  Expand one variable according to
1068    its flavor.  Variables to be placed on the stack are not actually
1069    expanded yet, merely recorded.  
1070    When REALLY_EXPAND is false, only add stack values to be allocated.
1071    Return stack usage this variable is supposed to take.
1072 */
1073
1074 static HOST_WIDE_INT
1075 expand_one_var (tree var, bool toplevel, bool really_expand)
1076 {
1077   if (SUPPORTS_STACK_ALIGNMENT
1078       && TREE_TYPE (var) != error_mark_node
1079       && TREE_CODE (var) == VAR_DECL)
1080     {
1081       unsigned int align;
1082
1083       /* Because we don't know if VAR will be in register or on stack,
1084          we conservatively assume it will be on stack even if VAR is
1085          eventually put into register after RA pass.  For non-automatic
1086          variables, which won't be on stack, we collect alignment of
1087          type and ignore user specified alignment.  */
1088       if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1089         align = TYPE_ALIGN (TREE_TYPE (var));
1090       else
1091         align = DECL_ALIGN (var);
1092
1093       if (crtl->stack_alignment_estimated < align)
1094         {
1095           /* stack_alignment_estimated shouldn't change after stack
1096              realign decision made */
1097           gcc_assert(!crtl->stack_realign_processed);
1098           crtl->stack_alignment_estimated = align;
1099         }
1100     }
1101
1102   if (TREE_CODE (var) != VAR_DECL)
1103     ;
1104   else if (DECL_EXTERNAL (var))
1105     ;
1106   else if (DECL_HAS_VALUE_EXPR_P (var))
1107     ;
1108   else if (TREE_STATIC (var))
1109     ;
1110   else if (DECL_RTL_SET_P (var))
1111     ;
1112   else if (TREE_TYPE (var) == error_mark_node)
1113     {
1114       if (really_expand)
1115         expand_one_error_var (var);
1116     }
1117   else if (DECL_HARD_REGISTER (var))
1118     {
1119       if (really_expand)
1120         expand_one_hard_reg_var (var);
1121     }
1122   else if (use_register_for_decl (var))
1123     {
1124       if (really_expand)
1125         expand_one_register_var (var);
1126     }
1127   else if (defer_stack_allocation (var, toplevel))
1128     add_stack_var (var);
1129   else
1130     {
1131       if (really_expand)
1132         expand_one_stack_var (var);
1133       return tree_low_cst (DECL_SIZE_UNIT (var), 1);
1134     }
1135   return 0;
1136 }
1137
1138 /* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
1139    expanding variables.  Those variables that can be put into registers
1140    are allocated pseudos; those that can't are put on the stack.
1141
1142    TOPLEVEL is true if this is the outermost BLOCK.  */
1143
1144 static void
1145 expand_used_vars_for_block (tree block, bool toplevel)
1146 {
1147   size_t i, j, old_sv_num, this_sv_num, new_sv_num;
1148   tree t;
1149
1150   old_sv_num = toplevel ? 0 : stack_vars_num;
1151
1152   /* Expand all variables at this level.  */
1153   for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1154     if (TREE_USED (t))
1155       expand_one_var (t, toplevel, true);
1156
1157   this_sv_num = stack_vars_num;
1158
1159   /* Expand all variables at containing levels.  */
1160   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1161     expand_used_vars_for_block (t, false);
1162
1163   /* Since we do not track exact variable lifetimes (which is not even
1164      possible for variables whose address escapes), we mirror the block
1165      tree in the interference graph.  Here we cause all variables at this
1166      level, and all sublevels, to conflict.  Do make certain that a
1167      variable conflicts with itself.  */
1168   if (old_sv_num < this_sv_num)
1169     {
1170       new_sv_num = stack_vars_num;
1171       resize_stack_vars_conflict (new_sv_num);
1172
1173       for (i = old_sv_num; i < new_sv_num; ++i)
1174         for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
1175           add_stack_var_conflict (i, j);
1176     }
1177 }
1178
1179 /* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
1180    and clear TREE_USED on all local variables.  */
1181
1182 static void
1183 clear_tree_used (tree block)
1184 {
1185   tree t;
1186
1187   for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1188     /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1189       TREE_USED (t) = 0;
1190
1191   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1192     clear_tree_used (t);
1193 }
1194
1195 /* Examine TYPE and determine a bit mask of the following features.  */
1196
1197 #define SPCT_HAS_LARGE_CHAR_ARRAY       1
1198 #define SPCT_HAS_SMALL_CHAR_ARRAY       2
1199 #define SPCT_HAS_ARRAY                  4
1200 #define SPCT_HAS_AGGREGATE              8
1201
1202 static unsigned int
1203 stack_protect_classify_type (tree type)
1204 {
1205   unsigned int ret = 0;
1206   tree t;
1207
1208   switch (TREE_CODE (type))
1209     {
1210     case ARRAY_TYPE:
1211       t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1212       if (t == char_type_node
1213           || t == signed_char_type_node
1214           || t == unsigned_char_type_node)
1215         {
1216           unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1217           unsigned HOST_WIDE_INT len;
1218
1219           if (!TYPE_SIZE_UNIT (type)
1220               || !host_integerp (TYPE_SIZE_UNIT (type), 1))
1221             len = max;
1222           else
1223             len = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
1224
1225           if (len < max)
1226             ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1227           else
1228             ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1229         }
1230       else
1231         ret = SPCT_HAS_ARRAY;
1232       break;
1233
1234     case UNION_TYPE:
1235     case QUAL_UNION_TYPE:
1236     case RECORD_TYPE:
1237       ret = SPCT_HAS_AGGREGATE;
1238       for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1239         if (TREE_CODE (t) == FIELD_DECL)
1240           ret |= stack_protect_classify_type (TREE_TYPE (t));
1241       break;
1242
1243     default:
1244       break;
1245     }
1246
1247   return ret;
1248 }
1249
1250 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1251    part of the local stack frame.  Remember if we ever return nonzero for
1252    any variable in this function.  The return value is the phase number in
1253    which the variable should be allocated.  */
1254
1255 static int
1256 stack_protect_decl_phase (tree decl)
1257 {
1258   unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1259   int ret = 0;
1260
1261   if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1262     has_short_buffer = true;
1263
1264   if (flag_stack_protect == 2)
1265     {
1266       if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1267           && !(bits & SPCT_HAS_AGGREGATE))
1268         ret = 1;
1269       else if (bits & SPCT_HAS_ARRAY)
1270         ret = 2;
1271     }
1272   else
1273     ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1274
1275   if (ret)
1276     has_protected_decls = true;
1277
1278   return ret;
1279 }
1280
1281 /* Two helper routines that check for phase 1 and phase 2.  These are used
1282    as callbacks for expand_stack_vars.  */
1283
1284 static bool
1285 stack_protect_decl_phase_1 (tree decl)
1286 {
1287   return stack_protect_decl_phase (decl) == 1;
1288 }
1289
1290 static bool
1291 stack_protect_decl_phase_2 (tree decl)
1292 {
1293   return stack_protect_decl_phase (decl) == 2;
1294 }
1295
1296 /* Ensure that variables in different stack protection phases conflict
1297    so that they are not merged and share the same stack slot.  */
1298
1299 static void
1300 add_stack_protection_conflicts (void)
1301 {
1302   size_t i, j, n = stack_vars_num;
1303   unsigned char *phase;
1304
1305   phase = XNEWVEC (unsigned char, n);
1306   for (i = 0; i < n; ++i)
1307     phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1308
1309   for (i = 0; i < n; ++i)
1310     {
1311       unsigned char ph_i = phase[i];
1312       for (j = 0; j < i; ++j)
1313         if (ph_i != phase[j])
1314           add_stack_var_conflict (i, j);
1315     }
1316
1317   XDELETEVEC (phase);
1318 }
1319
1320 /* Create a decl for the guard at the top of the stack frame.  */
1321
1322 static void
1323 create_stack_guard (void)
1324 {
1325   tree guard = build_decl (VAR_DECL, NULL, ptr_type_node);
1326   TREE_THIS_VOLATILE (guard) = 1;
1327   TREE_USED (guard) = 1;
1328   expand_one_stack_var (guard);
1329   crtl->stack_protect_guard = guard;
1330 }
1331
1332 /* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
1333    expanding variables.  Those variables that can be put into registers
1334    are allocated pseudos; those that can't are put on the stack.
1335
1336    TOPLEVEL is true if this is the outermost BLOCK.  */
1337
1338 static HOST_WIDE_INT
1339 account_used_vars_for_block (tree block, bool toplevel)
1340 {
1341   size_t i, j, old_sv_num, this_sv_num, new_sv_num;
1342   tree t;
1343   HOST_WIDE_INT size = 0;
1344
1345   old_sv_num = toplevel ? 0 : stack_vars_num;
1346
1347   /* Expand all variables at this level.  */
1348   for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1349     if (TREE_USED (t))
1350       size += expand_one_var (t, toplevel, false);
1351
1352   this_sv_num = stack_vars_num;
1353
1354   /* Expand all variables at containing levels.  */
1355   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1356     size += account_used_vars_for_block (t, false);
1357
1358   /* Since we do not track exact variable lifetimes (which is not even
1359      possible for variables whose address escapes), we mirror the block
1360      tree in the interference graph.  Here we cause all variables at this
1361      level, and all sublevels, to conflict.  Do make certain that a
1362      variable conflicts with itself.  */
1363   if (old_sv_num < this_sv_num)
1364     {
1365       new_sv_num = stack_vars_num;
1366       resize_stack_vars_conflict (new_sv_num);
1367
1368       for (i = old_sv_num; i < new_sv_num; ++i)
1369         for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
1370           add_stack_var_conflict (i, j);
1371     }
1372   return size;
1373 }
1374
1375 /* Prepare for expanding variables.  */
1376 static void 
1377 init_vars_expansion (void)
1378 {
1379   tree t;
1380   /* Set TREE_USED on all variables in the local_decls.  */
1381   for (t = cfun->local_decls; t; t = TREE_CHAIN (t))
1382     TREE_USED (TREE_VALUE (t)) = 1;
1383
1384   /* Clear TREE_USED on all variables associated with a block scope.  */
1385   clear_tree_used (DECL_INITIAL (current_function_decl));
1386
1387   /* Initialize local stack smashing state.  */
1388   has_protected_decls = false;
1389   has_short_buffer = false;
1390 }
1391
1392 /* Free up stack variable graph data.  */
1393 static void
1394 fini_vars_expansion (void)
1395 {
1396   XDELETEVEC (stack_vars);
1397   XDELETEVEC (stack_vars_sorted);
1398   XDELETEVEC (stack_vars_conflict);
1399   stack_vars = NULL;
1400   stack_vars_alloc = stack_vars_num = 0;
1401   stack_vars_conflict = NULL;
1402   stack_vars_conflict_alloc = 0;
1403 }
1404
1405 HOST_WIDE_INT
1406 estimated_stack_frame_size (void)
1407 {
1408   HOST_WIDE_INT size = 0;
1409   tree t, outer_block = DECL_INITIAL (current_function_decl);
1410
1411   init_vars_expansion ();
1412
1413   /* At this point all variables on the local_decls with TREE_USED
1414      set are not associated with any block scope.  Lay them out.  */
1415   for (t = cfun->local_decls; t; t = TREE_CHAIN (t))
1416     {
1417       tree var = TREE_VALUE (t);
1418
1419       if (TREE_USED (var))
1420         size += expand_one_var (var, true, false);
1421       TREE_USED (var) = 1;
1422     }
1423   size += account_used_vars_for_block (outer_block, true);
1424   if (stack_vars_num > 0)
1425     {
1426       /* Due to the way alias sets work, no variables with non-conflicting
1427          alias sets may be assigned the same address.  Add conflicts to
1428          reflect this.  */
1429       add_alias_set_conflicts ();
1430
1431       /* If stack protection is enabled, we don't share space between
1432          vulnerable data and non-vulnerable data.  */
1433       if (flag_stack_protect)
1434         add_stack_protection_conflicts ();
1435
1436       /* Now that we have collected all stack variables, and have computed a
1437          minimal interference graph, attempt to save some stack space.  */
1438       partition_stack_vars ();
1439       if (dump_file)
1440         dump_stack_var_partition ();
1441
1442       size += account_stack_vars ();
1443       fini_vars_expansion ();
1444     }
1445   return size;
1446 }
1447
1448 /* Expand all variables used in the function.  */
1449
1450 static void
1451 expand_used_vars (void)
1452 {
1453   tree t, outer_block = DECL_INITIAL (current_function_decl);
1454
1455   /* Compute the phase of the stack frame for this function.  */
1456   {
1457     int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1458     int off = STARTING_FRAME_OFFSET % align;
1459     frame_phase = off ? align - off : 0;
1460   }
1461
1462   init_vars_expansion ();
1463
1464   /* At this point all variables on the local_decls with TREE_USED
1465      set are not associated with any block scope.  Lay them out.  */
1466   for (t = cfun->local_decls; t; t = TREE_CHAIN (t))
1467     {
1468       tree var = TREE_VALUE (t);
1469       bool expand_now = false;
1470
1471       /* We didn't set a block for static or extern because it's hard
1472          to tell the difference between a global variable (re)declared
1473          in a local scope, and one that's really declared there to
1474          begin with.  And it doesn't really matter much, since we're
1475          not giving them stack space.  Expand them now.  */
1476       if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1477         expand_now = true;
1478
1479       /* Any variable that could have been hoisted into an SSA_NAME
1480          will have been propagated anywhere the optimizers chose,
1481          i.e. not confined to their original block.  Allocate them
1482          as if they were defined in the outermost scope.  */
1483       else if (is_gimple_reg (var))
1484         expand_now = true;
1485
1486       /* If the variable is not associated with any block, then it
1487          was created by the optimizers, and could be live anywhere
1488          in the function.  */
1489       else if (TREE_USED (var))
1490         expand_now = true;
1491
1492       /* Finally, mark all variables on the list as used.  We'll use
1493          this in a moment when we expand those associated with scopes.  */
1494       TREE_USED (var) = 1;
1495
1496       if (expand_now)
1497         expand_one_var (var, true, true);
1498     }
1499   cfun->local_decls = NULL_TREE;
1500
1501   /* At this point, all variables within the block tree with TREE_USED
1502      set are actually used by the optimized function.  Lay them out.  */
1503   expand_used_vars_for_block (outer_block, true);
1504
1505   if (stack_vars_num > 0)
1506     {
1507       /* Due to the way alias sets work, no variables with non-conflicting
1508          alias sets may be assigned the same address.  Add conflicts to
1509          reflect this.  */
1510       add_alias_set_conflicts ();
1511
1512       /* If stack protection is enabled, we don't share space between
1513          vulnerable data and non-vulnerable data.  */
1514       if (flag_stack_protect)
1515         add_stack_protection_conflicts ();
1516
1517       /* Now that we have collected all stack variables, and have computed a
1518          minimal interference graph, attempt to save some stack space.  */
1519       partition_stack_vars ();
1520       if (dump_file)
1521         dump_stack_var_partition ();
1522     }
1523
1524   /* There are several conditions under which we should create a
1525      stack guard: protect-all, alloca used, protected decls present.  */
1526   if (flag_stack_protect == 2
1527       || (flag_stack_protect
1528           && (cfun->calls_alloca || has_protected_decls)))
1529     create_stack_guard ();
1530
1531   /* Assign rtl to each variable based on these partitions.  */
1532   if (stack_vars_num > 0)
1533     {
1534       /* Reorder decls to be protected by iterating over the variables
1535          array multiple times, and allocating out of each phase in turn.  */
1536       /* ??? We could probably integrate this into the qsort we did
1537          earlier, such that we naturally see these variables first,
1538          and thus naturally allocate things in the right order.  */
1539       if (has_protected_decls)
1540         {
1541           /* Phase 1 contains only character arrays.  */
1542           expand_stack_vars (stack_protect_decl_phase_1);
1543
1544           /* Phase 2 contains other kinds of arrays.  */
1545           if (flag_stack_protect == 2)
1546             expand_stack_vars (stack_protect_decl_phase_2);
1547         }
1548
1549       expand_stack_vars (NULL);
1550
1551       fini_vars_expansion ();
1552     }
1553
1554   /* If the target requires that FRAME_OFFSET be aligned, do it.  */
1555   if (STACK_ALIGNMENT_NEEDED)
1556     {
1557       HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1558       if (!FRAME_GROWS_DOWNWARD)
1559         frame_offset += align - 1;
1560       frame_offset &= -align;
1561     }
1562 }
1563
1564
1565 /* If we need to produce a detailed dump, print the tree representation
1566    for STMT to the dump file.  SINCE is the last RTX after which the RTL
1567    generated for STMT should have been appended.  */
1568
1569 static void
1570 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx since)
1571 {
1572   if (dump_file && (dump_flags & TDF_DETAILS))
1573     {
1574       fprintf (dump_file, "\n;; ");
1575       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1576       fprintf (dump_file, "\n");
1577
1578       print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1579     }
1580 }
1581
1582 /* Maps the blocks that do not contain tree labels to rtx labels.  */
1583
1584 static struct pointer_map_t *lab_rtx_for_bb;
1585
1586 /* Returns the label_rtx expression for a label starting basic block BB.  */
1587
1588 static rtx
1589 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
1590 {
1591   gimple_stmt_iterator gsi;
1592   tree lab;
1593   gimple lab_stmt;
1594   void **elt;
1595
1596   if (bb->flags & BB_RTL)
1597     return block_label (bb);
1598
1599   elt = pointer_map_contains (lab_rtx_for_bb, bb);
1600   if (elt)
1601     return (rtx) *elt;
1602
1603   /* Find the tree label if it is present.  */
1604      
1605   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1606     {
1607       lab_stmt = gsi_stmt (gsi);
1608       if (gimple_code (lab_stmt) != GIMPLE_LABEL)
1609         break;
1610
1611       lab = gimple_label_label (lab_stmt);
1612       if (DECL_NONLOCAL (lab))
1613         break;
1614
1615       return label_rtx (lab);
1616     }
1617
1618   elt = pointer_map_insert (lab_rtx_for_bb, bb);
1619   *elt = gen_label_rtx ();
1620   return (rtx) *elt;
1621 }
1622
1623
1624 /* A subroutine of expand_gimple_basic_block.  Expand one GIMPLE_COND.
1625    Returns a new basic block if we've terminated the current basic
1626    block and created a new one.  */
1627
1628 static basic_block
1629 expand_gimple_cond (basic_block bb, gimple stmt)
1630 {
1631   basic_block new_bb, dest;
1632   edge new_edge;
1633   edge true_edge;
1634   edge false_edge;
1635   tree pred = gimple_cond_pred_to_tree (stmt);
1636   rtx last2, last;
1637
1638   last2 = last = get_last_insn ();
1639
1640   extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1641   if (gimple_has_location (stmt))
1642     {
1643       set_curr_insn_source_location (gimple_location (stmt));
1644       set_curr_insn_block (gimple_block (stmt));
1645     }
1646
1647   /* These flags have no purpose in RTL land.  */
1648   true_edge->flags &= ~EDGE_TRUE_VALUE;
1649   false_edge->flags &= ~EDGE_FALSE_VALUE;
1650
1651   /* We can either have a pure conditional jump with one fallthru edge or
1652      two-way jump that needs to be decomposed into two basic blocks.  */
1653   if (false_edge->dest == bb->next_bb)
1654     {
1655       jumpif (pred, label_rtx_for_bb (true_edge->dest));
1656       add_reg_br_prob_note (last, true_edge->probability);
1657       maybe_dump_rtl_for_gimple_stmt (stmt, last);
1658       if (true_edge->goto_locus)
1659         set_curr_insn_source_location (true_edge->goto_locus);
1660       false_edge->flags |= EDGE_FALLTHRU;
1661       ggc_free (pred);
1662       return NULL;
1663     }
1664   if (true_edge->dest == bb->next_bb)
1665     {
1666       jumpifnot (pred, label_rtx_for_bb (false_edge->dest));
1667       add_reg_br_prob_note (last, false_edge->probability);
1668       maybe_dump_rtl_for_gimple_stmt (stmt, last);
1669       if (false_edge->goto_locus)
1670         set_curr_insn_source_location (false_edge->goto_locus);
1671       true_edge->flags |= EDGE_FALLTHRU;
1672       ggc_free (pred);
1673       return NULL;
1674     }
1675
1676   jumpif (pred, label_rtx_for_bb (true_edge->dest));
1677   add_reg_br_prob_note (last, true_edge->probability);
1678   last = get_last_insn ();
1679   emit_jump (label_rtx_for_bb (false_edge->dest));
1680
1681   BB_END (bb) = last;
1682   if (BARRIER_P (BB_END (bb)))
1683     BB_END (bb) = PREV_INSN (BB_END (bb));
1684   update_bb_for_insn (bb);
1685
1686   new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1687   dest = false_edge->dest;
1688   redirect_edge_succ (false_edge, new_bb);
1689   false_edge->flags |= EDGE_FALLTHRU;
1690   new_bb->count = false_edge->count;
1691   new_bb->frequency = EDGE_FREQUENCY (false_edge);
1692   new_edge = make_edge (new_bb, dest, 0);
1693   new_edge->probability = REG_BR_PROB_BASE;
1694   new_edge->count = new_bb->count;
1695   if (BARRIER_P (BB_END (new_bb)))
1696     BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
1697   update_bb_for_insn (new_bb);
1698
1699   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
1700
1701   if (false_edge->goto_locus)
1702     set_curr_insn_source_location (false_edge->goto_locus);
1703
1704   ggc_free (pred);
1705   return new_bb;
1706 }
1707
1708 /* A subroutine of expand_gimple_basic_block.  Expand one GIMPLE_CALL
1709    that has CALL_EXPR_TAILCALL set.  Returns non-null if we actually
1710    generated a tail call (something that might be denied by the ABI
1711    rules governing the call; see calls.c).
1712
1713    Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
1714    can still reach the rest of BB.  The case here is __builtin_sqrt,
1715    where the NaN result goes through the external function (with a
1716    tailcall) and the normal result happens via a sqrt instruction.  */
1717
1718 static basic_block
1719 expand_gimple_tailcall (basic_block bb, gimple stmt, bool *can_fallthru)
1720 {
1721   rtx last2, last;
1722   edge e;
1723   edge_iterator ei;
1724   int probability;
1725   gcov_type count;
1726   tree stmt_tree = gimple_to_tree (stmt);
1727
1728   last2 = last = get_last_insn ();
1729
1730   expand_expr_stmt (stmt_tree);
1731
1732   release_stmt_tree (stmt, stmt_tree);
1733
1734   for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
1735     if (CALL_P (last) && SIBLING_CALL_P (last))
1736       goto found;
1737
1738   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
1739
1740   *can_fallthru = true;
1741   return NULL;
1742
1743  found:
1744   /* ??? Wouldn't it be better to just reset any pending stack adjust?
1745      Any instructions emitted here are about to be deleted.  */
1746   do_pending_stack_adjust ();
1747
1748   /* Remove any non-eh, non-abnormal edges that don't go to exit.  */
1749   /* ??? I.e. the fallthrough edge.  HOWEVER!  If there were to be
1750      EH or abnormal edges, we shouldn't have created a tail call in
1751      the first place.  So it seems to me we should just be removing
1752      all edges here, or redirecting the existing fallthru edge to
1753      the exit block.  */
1754
1755   probability = 0;
1756   count = 0;
1757
1758   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1759     {
1760       if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
1761         {
1762           if (e->dest != EXIT_BLOCK_PTR)
1763             {
1764               e->dest->count -= e->count;
1765               e->dest->frequency -= EDGE_FREQUENCY (e);
1766               if (e->dest->count < 0)
1767                 e->dest->count = 0;
1768               if (e->dest->frequency < 0)
1769                 e->dest->frequency = 0;
1770             }
1771           count += e->count;
1772           probability += e->probability;
1773           remove_edge (e);
1774         }
1775       else
1776         ei_next (&ei);
1777     }
1778
1779   /* This is somewhat ugly: the call_expr expander often emits instructions
1780      after the sibcall (to perform the function return).  These confuse the
1781      find_many_sub_basic_blocks code, so we need to get rid of these.  */
1782   last = NEXT_INSN (last);
1783   gcc_assert (BARRIER_P (last));
1784
1785   *can_fallthru = false;
1786   while (NEXT_INSN (last))
1787     {
1788       /* For instance an sqrt builtin expander expands if with
1789          sibcall in the then and label for `else`.  */
1790       if (LABEL_P (NEXT_INSN (last)))
1791         {
1792           *can_fallthru = true;
1793           break;
1794         }
1795       delete_insn (NEXT_INSN (last));
1796     }
1797
1798   e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
1799   e->probability += probability;
1800   e->count += count;
1801   BB_END (bb) = last;
1802   update_bb_for_insn (bb);
1803
1804   if (NEXT_INSN (last))
1805     {
1806       bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1807
1808       last = BB_END (bb);
1809       if (BARRIER_P (last))
1810         BB_END (bb) = PREV_INSN (last);
1811     }
1812
1813   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
1814
1815   return bb;
1816 }
1817
1818 /* Expand basic block BB from GIMPLE trees to RTL.  */
1819
1820 static basic_block
1821 expand_gimple_basic_block (basic_block bb)
1822 {
1823   gimple_stmt_iterator gsi;
1824   gimple_seq stmts;
1825   gimple stmt = NULL;
1826   rtx note, last;
1827   edge e;
1828   edge_iterator ei;
1829   void **elt;
1830
1831   if (dump_file)
1832     fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
1833              bb->index);
1834
1835   /* Note that since we are now transitioning from GIMPLE to RTL, we
1836      cannot use the gsi_*_bb() routines because they expect the basic
1837      block to be in GIMPLE, instead of RTL.  Therefore, we need to
1838      access the BB sequence directly.  */
1839   stmts = bb_seq (bb);
1840   bb->il.gimple = NULL;
1841   rtl_profile_for_bb (bb);
1842   init_rtl_bb_info (bb);
1843   bb->flags |= BB_RTL;
1844
1845   /* Remove the RETURN_EXPR if we may fall though to the exit
1846      instead.  */
1847   gsi = gsi_last (stmts);
1848   if (!gsi_end_p (gsi)
1849       && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
1850     {
1851       gimple ret_stmt = gsi_stmt (gsi);
1852
1853       gcc_assert (single_succ_p (bb));
1854       gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
1855
1856       if (bb->next_bb == EXIT_BLOCK_PTR
1857           && !gimple_return_retval (ret_stmt))
1858         {
1859           gsi_remove (&gsi, false);
1860           single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
1861         }
1862     }
1863
1864   gsi = gsi_start (stmts);
1865   if (!gsi_end_p (gsi))
1866     {
1867       stmt = gsi_stmt (gsi);
1868       if (gimple_code (stmt) != GIMPLE_LABEL)
1869         stmt = NULL;
1870     }
1871
1872   elt = pointer_map_contains (lab_rtx_for_bb, bb);
1873
1874   if (stmt || elt)
1875     {
1876       last = get_last_insn ();
1877
1878       if (stmt)
1879         {
1880           tree stmt_tree = gimple_to_tree (stmt);
1881           expand_expr_stmt (stmt_tree);
1882           release_stmt_tree (stmt, stmt_tree);
1883           gsi_next (&gsi);
1884         }
1885
1886       if (elt)
1887         emit_label ((rtx) *elt);
1888
1889       /* Java emits line number notes in the top of labels.
1890          ??? Make this go away once line number notes are obsoleted.  */
1891       BB_HEAD (bb) = NEXT_INSN (last);
1892       if (NOTE_P (BB_HEAD (bb)))
1893         BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
1894       note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
1895
1896       maybe_dump_rtl_for_gimple_stmt (stmt, last);
1897     }
1898   else
1899     note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
1900
1901   NOTE_BASIC_BLOCK (note) = bb;
1902
1903   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1904     {
1905       /* Clear EDGE_EXECUTABLE.  This flag is never used in the backend.  */
1906       e->flags &= ~EDGE_EXECUTABLE;
1907
1908       /* At the moment not all abnormal edges match the RTL representation.
1909          It is safe to remove them here as find_many_sub_basic_blocks will
1910          rediscover them.  In the future we should get this fixed properly.  */
1911       if (e->flags & EDGE_ABNORMAL)
1912         remove_edge (e);
1913       else
1914         ei_next (&ei);
1915     }
1916
1917   for (; !gsi_end_p (gsi); gsi_next (&gsi))
1918     {
1919       gimple stmt = gsi_stmt (gsi);
1920       basic_block new_bb;
1921
1922       /* Expand this statement, then evaluate the resulting RTL and
1923          fixup the CFG accordingly.  */
1924       if (gimple_code (stmt) == GIMPLE_COND)
1925         {
1926           new_bb = expand_gimple_cond (bb, stmt);
1927           if (new_bb)
1928             return new_bb;
1929         }
1930       else
1931         {
1932           if (is_gimple_call (stmt) && gimple_call_tail_p (stmt))
1933             {
1934               bool can_fallthru;
1935               new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
1936               if (new_bb)
1937                 {
1938                   if (can_fallthru)
1939                     bb = new_bb;
1940                   else
1941                     return new_bb;
1942                 }
1943             }
1944           else
1945             {
1946               tree stmt_tree = gimple_to_tree (stmt);
1947               last = get_last_insn ();
1948               expand_expr_stmt (stmt_tree);
1949               maybe_dump_rtl_for_gimple_stmt (stmt, last);
1950               release_stmt_tree (stmt, stmt_tree);
1951             }
1952         }
1953     }
1954
1955   /* Expand implicit goto.  */
1956   FOR_EACH_EDGE (e, ei, bb->succs)
1957     {
1958       if (e->flags & EDGE_FALLTHRU)
1959         break;
1960     }
1961
1962   if (e && e->dest != bb->next_bb)
1963     {
1964       emit_jump (label_rtx_for_bb (e->dest));
1965       if (e->goto_locus)
1966         set_curr_insn_source_location (e->goto_locus);
1967       e->flags &= ~EDGE_FALLTHRU;
1968     }
1969
1970   do_pending_stack_adjust ();
1971
1972   /* Find the block tail.  The last insn in the block is the insn
1973      before a barrier and/or table jump insn.  */
1974   last = get_last_insn ();
1975   if (BARRIER_P (last))
1976     last = PREV_INSN (last);
1977   if (JUMP_TABLE_DATA_P (last))
1978     last = PREV_INSN (PREV_INSN (last));
1979   BB_END (bb) = last;
1980
1981   update_bb_for_insn (bb);
1982
1983   return bb;
1984 }
1985
1986
1987 /* Create a basic block for initialization code.  */
1988
1989 static basic_block
1990 construct_init_block (void)
1991 {
1992   basic_block init_block, first_block;
1993   edge e = NULL;
1994   int flags;
1995
1996   /* Multiple entry points not supported yet.  */
1997   gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
1998   init_rtl_bb_info (ENTRY_BLOCK_PTR);
1999   init_rtl_bb_info (EXIT_BLOCK_PTR);
2000   ENTRY_BLOCK_PTR->flags |= BB_RTL;
2001   EXIT_BLOCK_PTR->flags |= BB_RTL;
2002
2003   e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
2004
2005   /* When entry edge points to first basic block, we don't need jump,
2006      otherwise we have to jump into proper target.  */
2007   if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
2008     {
2009       tree label = gimple_block_label (e->dest);
2010
2011       emit_jump (label_rtx (label));
2012       flags = 0;
2013     }
2014   else
2015     flags = EDGE_FALLTHRU;
2016
2017   init_block = create_basic_block (NEXT_INSN (get_insns ()),
2018                                    get_last_insn (),
2019                                    ENTRY_BLOCK_PTR);
2020   init_block->frequency = ENTRY_BLOCK_PTR->frequency;
2021   init_block->count = ENTRY_BLOCK_PTR->count;
2022   if (e)
2023     {
2024       first_block = e->dest;
2025       redirect_edge_succ (e, init_block);
2026       e = make_edge (init_block, first_block, flags);
2027     }
2028   else
2029     e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
2030   e->probability = REG_BR_PROB_BASE;
2031   e->count = ENTRY_BLOCK_PTR->count;
2032
2033   update_bb_for_insn (init_block);
2034   return init_block;
2035 }
2036
2037 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
2038    found in the block tree.  */
2039
2040 static void
2041 set_block_levels (tree block, int level)
2042 {
2043   while (block)
2044     {
2045       BLOCK_NUMBER (block) = level;
2046       set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
2047       block = BLOCK_CHAIN (block);
2048     }
2049 }
2050
2051 /* Create a block containing landing pads and similar stuff.  */
2052
2053 static void
2054 construct_exit_block (void)
2055 {
2056   rtx head = get_last_insn ();
2057   rtx end;
2058   basic_block exit_block;
2059   edge e, e2;
2060   unsigned ix;
2061   edge_iterator ei;
2062   rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb);
2063
2064   rtl_profile_for_bb (EXIT_BLOCK_PTR);
2065
2066   /* Make sure the locus is set to the end of the function, so that
2067      epilogue line numbers and warnings are set properly.  */
2068   if (cfun->function_end_locus != UNKNOWN_LOCATION)
2069     input_location = cfun->function_end_locus;
2070
2071   /* The following insns belong to the top scope.  */
2072   set_curr_insn_block (DECL_INITIAL (current_function_decl));
2073
2074   /* Generate rtl for function exit.  */
2075   expand_function_end ();
2076
2077   end = get_last_insn ();
2078   if (head == end)
2079     return;
2080   /* While emitting the function end we could move end of the last basic block.
2081    */
2082   BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end;
2083   while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
2084     head = NEXT_INSN (head);
2085   exit_block = create_basic_block (NEXT_INSN (head), end,
2086                                    EXIT_BLOCK_PTR->prev_bb);
2087   exit_block->frequency = EXIT_BLOCK_PTR->frequency;
2088   exit_block->count = EXIT_BLOCK_PTR->count;
2089
2090   ix = 0;
2091   while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
2092     {
2093       e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
2094       if (!(e->flags & EDGE_ABNORMAL))
2095         redirect_edge_succ (e, exit_block);
2096       else
2097         ix++;
2098     }
2099
2100   e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
2101   e->probability = REG_BR_PROB_BASE;
2102   e->count = EXIT_BLOCK_PTR->count;
2103   FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
2104     if (e2 != e)
2105       {
2106         e->count -= e2->count;
2107         exit_block->count -= e2->count;
2108         exit_block->frequency -= EDGE_FREQUENCY (e2);
2109       }
2110   if (e->count < 0)
2111     e->count = 0;
2112   if (exit_block->count < 0)
2113     exit_block->count = 0;
2114   if (exit_block->frequency < 0)
2115     exit_block->frequency = 0;
2116   update_bb_for_insn (exit_block);
2117 }
2118
2119 /* Helper function for discover_nonconstant_array_refs.
2120    Look for ARRAY_REF nodes with non-constant indexes and mark them
2121    addressable.  */
2122
2123 static tree
2124 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
2125                                    void *data ATTRIBUTE_UNUSED)
2126 {
2127   tree t = *tp;
2128
2129   if (IS_TYPE_OR_DECL_P (t))
2130     *walk_subtrees = 0;
2131   else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2132     {
2133       while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2134               && is_gimple_min_invariant (TREE_OPERAND (t, 1))
2135               && (!TREE_OPERAND (t, 2)
2136                   || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
2137              || (TREE_CODE (t) == COMPONENT_REF
2138                  && (!TREE_OPERAND (t,2)
2139                      || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
2140              || TREE_CODE (t) == BIT_FIELD_REF
2141              || TREE_CODE (t) == REALPART_EXPR
2142              || TREE_CODE (t) == IMAGPART_EXPR
2143              || TREE_CODE (t) == VIEW_CONVERT_EXPR
2144              || CONVERT_EXPR_P (t))
2145         t = TREE_OPERAND (t, 0);
2146
2147       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2148         {
2149           t = get_base_address (t);
2150           if (t && DECL_P (t))
2151             TREE_ADDRESSABLE (t) = 1;
2152         }
2153
2154       *walk_subtrees = 0;
2155     }
2156
2157   return NULL_TREE;
2158 }
2159
2160 /* RTL expansion is not able to compile array references with variable
2161    offsets for arrays stored in single register.  Discover such
2162    expressions and mark variables as addressable to avoid this
2163    scenario.  */
2164
2165 static void
2166 discover_nonconstant_array_refs (void)
2167 {
2168   basic_block bb;
2169   gimple_stmt_iterator gsi;
2170
2171   FOR_EACH_BB (bb)
2172     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2173       {
2174         gimple stmt = gsi_stmt (gsi);
2175         walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
2176       }
2177 }
2178
2179 /* This function sets crtl->args.internal_arg_pointer to a virtual
2180    register if DRAP is needed.  Local register allocator will replace
2181    virtual_incoming_args_rtx with the virtual register.  */
2182
2183 static void
2184 expand_stack_alignment (void)
2185 {
2186   rtx drap_rtx;
2187   unsigned int preferred_stack_boundary, incoming_stack_boundary;
2188
2189   if (! SUPPORTS_STACK_ALIGNMENT)
2190     return;
2191   
2192   if (cfun->calls_alloca
2193       || cfun->has_nonlocal_label
2194       || crtl->has_nonlocal_goto)
2195     crtl->need_drap = true;
2196
2197   gcc_assert (crtl->stack_alignment_needed
2198               <= crtl->stack_alignment_estimated);
2199
2200   /* Update stack boundary if needed.  */
2201   if (targetm.calls.update_stack_boundary)
2202     targetm.calls.update_stack_boundary (); 
2203
2204   /* Update crtl->stack_alignment_estimated and use it later to align
2205      stack.  We check PREFERRED_STACK_BOUNDARY if there may be non-call
2206      exceptions since callgraph doesn't collect incoming stack alignment
2207      in this case.  */
2208   if (flag_non_call_exceptions
2209       && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
2210     preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2211   else
2212     preferred_stack_boundary = crtl->preferred_stack_boundary;
2213   if (preferred_stack_boundary > crtl->stack_alignment_estimated)
2214     crtl->stack_alignment_estimated = preferred_stack_boundary;
2215   if (preferred_stack_boundary > crtl->stack_alignment_needed)
2216     crtl->stack_alignment_needed = preferred_stack_boundary;
2217
2218   /* The incoming stack frame has to be aligned at least at
2219      parm_stack_boundary.  */
2220   if (crtl->parm_stack_boundary > INCOMING_STACK_BOUNDARY)
2221     incoming_stack_boundary = crtl->parm_stack_boundary;
2222   else
2223     incoming_stack_boundary = INCOMING_STACK_BOUNDARY;
2224
2225   crtl->stack_realign_needed
2226     = incoming_stack_boundary < crtl->stack_alignment_estimated;
2227   crtl->stack_realign_tried = crtl->stack_realign_needed;
2228
2229   crtl->stack_realign_processed = true;
2230
2231   /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
2232      alignment.  */
2233   gcc_assert (targetm.calls.get_drap_rtx != NULL);
2234   drap_rtx = targetm.calls.get_drap_rtx (); 
2235
2236   /* stack_realign_drap and drap_rtx must match.  */
2237   gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
2238
2239   /* Do nothing if NULL is returned, which means DRAP is not needed.  */
2240   if (NULL != drap_rtx)
2241     {
2242       crtl->args.internal_arg_pointer = drap_rtx;
2243
2244       /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
2245          needed. */
2246       fixup_tail_calls ();
2247     }
2248 }
2249
2250 /* Translate the intermediate representation contained in the CFG
2251    from GIMPLE trees to RTL.
2252
2253    We do conversion per basic block and preserve/update the tree CFG.
2254    This implies we have to do some magic as the CFG can simultaneously
2255    consist of basic blocks containing RTL and GIMPLE trees.  This can
2256    confuse the CFG hooks, so be careful to not manipulate CFG during
2257    the expansion.  */
2258
2259 static unsigned int
2260 gimple_expand_cfg (void)
2261 {
2262   basic_block bb, init_block;
2263   sbitmap blocks;
2264   edge_iterator ei;
2265   edge e;
2266
2267   /* Some backends want to know that we are expanding to RTL.  */
2268   currently_expanding_to_rtl = 1;
2269
2270   rtl_profile_for_bb (ENTRY_BLOCK_PTR);
2271
2272   insn_locators_alloc ();
2273   if (!DECL_BUILT_IN (current_function_decl))
2274     set_curr_insn_source_location (DECL_SOURCE_LOCATION (current_function_decl));
2275   set_curr_insn_block (DECL_INITIAL (current_function_decl));
2276   prologue_locator = curr_insn_locator ();
2277
2278   /* Make sure first insn is a note even if we don't want linenums.
2279      This makes sure the first insn will never be deleted.
2280      Also, final expects a note to appear there.  */
2281   emit_note (NOTE_INSN_DELETED);
2282
2283   /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE.  */
2284   discover_nonconstant_array_refs ();
2285
2286   targetm.expand_to_rtl_hook ();
2287   crtl->stack_alignment_needed = STACK_BOUNDARY;
2288   crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
2289   crtl->stack_alignment_estimated = STACK_BOUNDARY;
2290   crtl->preferred_stack_boundary = STACK_BOUNDARY;
2291   cfun->cfg->max_jumptable_ents = 0;
2292
2293
2294   /* Expand the variables recorded during gimple lowering.  */
2295   expand_used_vars ();
2296
2297   /* Honor stack protection warnings.  */
2298   if (warn_stack_protect)
2299     {
2300       if (cfun->calls_alloca)
2301         warning (OPT_Wstack_protector, 
2302                  "not protecting local variables: variable length buffer");
2303       if (has_short_buffer && !crtl->stack_protect_guard)
2304         warning (OPT_Wstack_protector, 
2305                  "not protecting function: no buffer at least %d bytes long",
2306                  (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
2307     }
2308
2309   /* Set up parameters and prepare for return, for the function.  */
2310   expand_function_start (current_function_decl);
2311
2312   /* If this function is `main', emit a call to `__main'
2313      to run global initializers, etc.  */
2314   if (DECL_NAME (current_function_decl)
2315       && MAIN_NAME_P (DECL_NAME (current_function_decl))
2316       && DECL_FILE_SCOPE_P (current_function_decl))
2317     expand_main_function ();
2318
2319   /* Initialize the stack_protect_guard field.  This must happen after the
2320      call to __main (if any) so that the external decl is initialized.  */
2321   if (crtl->stack_protect_guard)
2322     stack_protect_prologue ();
2323
2324   /* Register rtl specific functions for cfg.  */
2325   rtl_register_cfg_hooks ();
2326
2327   init_block = construct_init_block ();
2328
2329   /* Clear EDGE_EXECUTABLE on the entry edge(s).  It is cleaned from the
2330      remaining edges in expand_gimple_basic_block.  */
2331   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2332     e->flags &= ~EDGE_EXECUTABLE;
2333
2334   lab_rtx_for_bb = pointer_map_create ();
2335   FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
2336     bb = expand_gimple_basic_block (bb);
2337
2338   /* Expansion is used by optimization passes too, set maybe_hot_insn_p
2339      conservatively to true until they are all profile aware.  */
2340   pointer_map_destroy (lab_rtx_for_bb);
2341   free_histograms ();
2342
2343   construct_exit_block ();
2344   set_curr_insn_block (DECL_INITIAL (current_function_decl));
2345   insn_locators_finalize ();
2346
2347   /* We're done expanding trees to RTL.  */
2348   currently_expanding_to_rtl = 0;
2349
2350   /* Convert tree EH labels to RTL EH labels and zap the tree EH table.  */
2351   convert_from_eh_region_ranges ();
2352   set_eh_throw_stmt_table (cfun, NULL);
2353
2354   rebuild_jump_labels (get_insns ());
2355   find_exception_handler_labels ();
2356
2357   blocks = sbitmap_alloc (last_basic_block);
2358   sbitmap_ones (blocks);
2359   find_many_sub_basic_blocks (blocks);
2360   purge_all_dead_edges ();
2361   sbitmap_free (blocks);
2362
2363   compact_blocks ();
2364
2365   expand_stack_alignment ();
2366
2367 #ifdef ENABLE_CHECKING
2368   verify_flow_info ();
2369 #endif
2370
2371   /* There's no need to defer outputting this function any more; we
2372      know we want to output it.  */
2373   DECL_DEFER_OUTPUT (current_function_decl) = 0;
2374
2375   /* Now that we're done expanding trees to RTL, we shouldn't have any
2376      more CONCATs anywhere.  */
2377   generating_concat_p = 0;
2378
2379   if (dump_file)
2380     {
2381       fprintf (dump_file,
2382                "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
2383       /* And the pass manager will dump RTL for us.  */
2384     }
2385
2386   /* If we're emitting a nested function, make sure its parent gets
2387      emitted as well.  Doing otherwise confuses debug info.  */
2388   {
2389     tree parent;
2390     for (parent = DECL_CONTEXT (current_function_decl);
2391          parent != NULL_TREE;
2392          parent = get_containing_scope (parent))
2393       if (TREE_CODE (parent) == FUNCTION_DECL)
2394         TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
2395   }
2396
2397   /* We are now committed to emitting code for this function.  Do any
2398      preparation, such as emitting abstract debug info for the inline
2399      before it gets mangled by optimization.  */
2400   if (cgraph_function_possibly_inlined_p (current_function_decl))
2401     (*debug_hooks->outlining_inline_function) (current_function_decl);
2402
2403   TREE_ASM_WRITTEN (current_function_decl) = 1;
2404
2405   /* After expanding, the return labels are no longer needed. */
2406   return_label = NULL;
2407   naked_return_label = NULL;
2408   /* Tag the blocks with a depth number so that change_scope can find
2409      the common parent easily.  */
2410   set_block_levels (DECL_INITIAL (cfun->decl), 0);
2411   default_rtl_profile ();
2412   return 0;
2413 }
2414
2415 struct rtl_opt_pass pass_expand =
2416 {
2417  {
2418   RTL_PASS,
2419   "expand",                             /* name */
2420   NULL,                                 /* gate */
2421   gimple_expand_cfg,                    /* execute */
2422   NULL,                                 /* sub */
2423   NULL,                                 /* next */
2424   0,                                    /* static_pass_number */
2425   TV_EXPAND,                            /* tv_id */
2426   /* ??? If TER is enabled, we actually receive GENERIC.  */
2427   PROP_gimple_leh | PROP_cfg,           /* properties_required */
2428   PROP_rtl,                             /* properties_provided */
2429   PROP_trees,                           /* properties_destroyed */
2430   0,                                    /* todo_flags_start */
2431   TODO_dump_func,                       /* todo_flags_finish */
2432  }
2433 };