OSDN Git Service

3f7b1d28ac6f9dfa1215acd939833b77bc6aca4e
[pf3gnuchains/gcc-fork.git] / gcc / cfgexpand.c
1 /* A pass for lowering trees to RTL.
2    Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
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"
45 #include "ssaexpand.h"
46
47
48 /* This variable holds information helping the rewriting of SSA trees
49    into RTL.  */
50 struct ssaexpand SA;
51
52 /* This variable holds the currently expanded gimple statement for purposes
53    of comminucating the profile info to the builtin expanders.  */
54 gimple currently_expanding_gimple_stmt;
55
56 /* Return an expression tree corresponding to the RHS of GIMPLE
57    statement STMT.  */
58
59 tree
60 gimple_assign_rhs_to_tree (gimple stmt)
61 {
62   tree t;
63   enum gimple_rhs_class grhs_class;
64     
65   grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
66
67   if (grhs_class == GIMPLE_BINARY_RHS)
68     t = build2 (gimple_assign_rhs_code (stmt),
69                 TREE_TYPE (gimple_assign_lhs (stmt)),
70                 gimple_assign_rhs1 (stmt),
71                 gimple_assign_rhs2 (stmt));
72   else if (grhs_class == GIMPLE_UNARY_RHS)
73     t = build1 (gimple_assign_rhs_code (stmt),
74                 TREE_TYPE (gimple_assign_lhs (stmt)),
75                 gimple_assign_rhs1 (stmt));
76   else if (grhs_class == GIMPLE_SINGLE_RHS)
77     {
78       t = gimple_assign_rhs1 (stmt);
79       /* Avoid modifying this tree in place below.  */
80       if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
81           && gimple_location (stmt) != EXPR_LOCATION (t))
82         t = copy_node (t);
83     }
84   else
85     gcc_unreachable ();
86
87   if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
88     SET_EXPR_LOCATION (t, gimple_location (stmt));
89
90   return t;
91 }
92
93
94 /* Verify that there is exactly single jump instruction since last and attach
95    REG_BR_PROB note specifying probability.
96    ??? We really ought to pass the probability down to RTL expanders and let it
97    re-distribute it when the conditional expands into multiple conditionals.
98    This is however difficult to do.  */
99 void
100 add_reg_br_prob_note (rtx last, int probability)
101 {
102   if (profile_status == PROFILE_ABSENT)
103     return;
104   for (last = NEXT_INSN (last); last && NEXT_INSN (last); last = NEXT_INSN (last))
105     if (JUMP_P (last))
106       {
107         /* It is common to emit condjump-around-jump sequence when we don't know
108            how to reverse the conditional.  Special case this.  */
109         if (!any_condjump_p (last)
110             || !JUMP_P (NEXT_INSN (last))
111             || !simplejump_p (NEXT_INSN (last))
112             || !NEXT_INSN (NEXT_INSN (last))
113             || !BARRIER_P (NEXT_INSN (NEXT_INSN (last)))
114             || !NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))
115             || !LABEL_P (NEXT_INSN (NEXT_INSN (NEXT_INSN (last))))
116             || NEXT_INSN (NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))))
117           goto failed;
118         gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
119         add_reg_note (last, REG_BR_PROB,
120                       GEN_INT (REG_BR_PROB_BASE - probability));
121         return;
122       }
123   if (!last || !JUMP_P (last) || !any_condjump_p (last))
124     goto failed;
125   gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
126   add_reg_note (last, REG_BR_PROB, GEN_INT (probability));
127   return;
128 failed:
129   if (dump_file)
130     fprintf (dump_file, "Failed to add probability note\n");
131 }
132
133
134 #ifndef STACK_ALIGNMENT_NEEDED
135 #define STACK_ALIGNMENT_NEEDED 1
136 #endif
137
138 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
139
140 /* Associate declaration T with storage space X.  If T is no
141    SSA name this is exactly SET_DECL_RTL, otherwise make the
142    partition of T associated with X.  */
143 static inline void
144 set_rtl (tree t, rtx x)
145 {
146   if (TREE_CODE (t) == SSA_NAME)
147     {
148       SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
149       if (x && !MEM_P (x))
150         set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
151       /* For the benefit of debug information at -O0 (where vartracking
152          doesn't run) record the place also in the base DECL if it's
153          a normal variable (not a parameter).  */
154       if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
155         {
156           tree var = SSA_NAME_VAR (t);
157           /* If we don't yet have something recorded, just record it now.  */
158           if (!DECL_RTL_SET_P (var))
159             SET_DECL_RTL (var, x);
160           /* If we have it set alrady to "multiple places" don't
161              change this.  */
162           else if (DECL_RTL (var) == pc_rtx)
163             ;
164           /* If we have something recorded and it's not the same place
165              as we want to record now, we have multiple partitions for the
166              same base variable, with different places.  We can't just
167              randomly chose one, hence we have to say that we don't know.
168              This only happens with optimization, and there var-tracking
169              will figure out the right thing.  */
170           else if (DECL_RTL (var) != x)
171             SET_DECL_RTL (var, pc_rtx);
172         }
173     }
174   else
175     SET_DECL_RTL (t, x);
176 }
177
178 /* This structure holds data relevant to one variable that will be
179    placed in a stack slot.  */
180 struct stack_var
181 {
182   /* The Variable.  */
183   tree decl;
184
185   /* The offset of the variable.  During partitioning, this is the
186      offset relative to the partition.  After partitioning, this
187      is relative to the stack frame.  */
188   HOST_WIDE_INT offset;
189
190   /* Initially, the size of the variable.  Later, the size of the partition,
191      if this variable becomes it's partition's representative.  */
192   HOST_WIDE_INT size;
193
194   /* The *byte* alignment required for this variable.  Or as, with the
195      size, the alignment for this partition.  */
196   unsigned int alignb;
197
198   /* The partition representative.  */
199   size_t representative;
200
201   /* The next stack variable in the partition, or EOC.  */
202   size_t next;
203 };
204
205 #define EOC  ((size_t)-1)
206
207 /* We have an array of such objects while deciding allocation.  */
208 static struct stack_var *stack_vars;
209 static size_t stack_vars_alloc;
210 static size_t stack_vars_num;
211
212 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
213    is non-decreasing.  */
214 static size_t *stack_vars_sorted;
215
216 /* We have an interference graph between such objects.  This graph
217    is lower triangular.  */
218 static bool *stack_vars_conflict;
219 static size_t stack_vars_conflict_alloc;
220
221 /* The phase of the stack frame.  This is the known misalignment of
222    virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY.  That is,
223    (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0.  */
224 static int frame_phase;
225
226 /* Used during expand_used_vars to remember if we saw any decls for
227    which we'd like to enable stack smashing protection.  */
228 static bool has_protected_decls;
229
230 /* Used during expand_used_vars.  Remember if we say a character buffer
231    smaller than our cutoff threshold.  Used for -Wstack-protector.  */
232 static bool has_short_buffer;
233
234 /* Discover the byte alignment to use for DECL.  Ignore alignment
235    we can't do with expected alignment of the stack boundary.  */
236
237 static unsigned int
238 get_decl_align_unit (tree decl)
239 {
240   unsigned int align;
241
242   align = LOCAL_DECL_ALIGNMENT (decl);
243
244   if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
245     align = MAX_SUPPORTED_STACK_ALIGNMENT;
246
247   if (SUPPORTS_STACK_ALIGNMENT)
248     {
249       if (crtl->stack_alignment_estimated < align)
250         {
251           gcc_assert(!crtl->stack_realign_processed);
252           crtl->stack_alignment_estimated = align;
253         }
254     }
255
256   /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
257      So here we only make sure stack_alignment_needed >= align.  */
258   if (crtl->stack_alignment_needed < align)
259     crtl->stack_alignment_needed = align;
260   if (crtl->max_used_stack_slot_alignment < align)
261     crtl->max_used_stack_slot_alignment = align;
262
263   return align / BITS_PER_UNIT;
264 }
265
266 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
267    Return the frame offset.  */
268
269 static HOST_WIDE_INT
270 alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align)
271 {
272   HOST_WIDE_INT offset, new_frame_offset;
273
274   new_frame_offset = frame_offset;
275   if (FRAME_GROWS_DOWNWARD)
276     {
277       new_frame_offset -= size + frame_phase;
278       new_frame_offset &= -align;
279       new_frame_offset += frame_phase;
280       offset = new_frame_offset;
281     }
282   else
283     {
284       new_frame_offset -= frame_phase;
285       new_frame_offset += align - 1;
286       new_frame_offset &= -align;
287       new_frame_offset += frame_phase;
288       offset = new_frame_offset;
289       new_frame_offset += size;
290     }
291   frame_offset = new_frame_offset;
292
293   if (frame_offset_overflow (frame_offset, cfun->decl))
294     frame_offset = offset = 0;
295
296   return offset;
297 }
298
299 /* Accumulate DECL into STACK_VARS.  */
300
301 static void
302 add_stack_var (tree decl)
303 {
304   if (stack_vars_num >= stack_vars_alloc)
305     {
306       if (stack_vars_alloc)
307         stack_vars_alloc = stack_vars_alloc * 3 / 2;
308       else
309         stack_vars_alloc = 32;
310       stack_vars
311         = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
312     }
313   stack_vars[stack_vars_num].decl = decl;
314   stack_vars[stack_vars_num].offset = 0;
315   stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (SSAVAR (decl)), 1);
316   stack_vars[stack_vars_num].alignb = get_decl_align_unit (SSAVAR (decl));
317
318   /* All variables are initially in their own partition.  */
319   stack_vars[stack_vars_num].representative = stack_vars_num;
320   stack_vars[stack_vars_num].next = EOC;
321
322   /* Ensure that this decl doesn't get put onto the list twice.  */
323   set_rtl (decl, pc_rtx);
324
325   stack_vars_num++;
326 }
327
328 /* Compute the linear index of a lower-triangular coordinate (I, J).  */
329
330 static size_t
331 triangular_index (size_t i, size_t j)
332 {
333   if (i < j)
334     {
335       size_t t;
336       t = i, i = j, j = t;
337     }
338   return (i * (i + 1)) / 2 + j;
339 }
340
341 /* Ensure that STACK_VARS_CONFLICT is large enough for N objects.  */
342
343 static void
344 resize_stack_vars_conflict (size_t n)
345 {
346   size_t size = triangular_index (n-1, n-1) + 1;
347
348   if (size <= stack_vars_conflict_alloc)
349     return;
350
351   stack_vars_conflict = XRESIZEVEC (bool, stack_vars_conflict, size);
352   memset (stack_vars_conflict + stack_vars_conflict_alloc, 0,
353           (size - stack_vars_conflict_alloc) * sizeof (bool));
354   stack_vars_conflict_alloc = size;
355 }
356
357 /* Make the decls associated with luid's X and Y conflict.  */
358
359 static void
360 add_stack_var_conflict (size_t x, size_t y)
361 {
362   size_t index = triangular_index (x, y);
363   gcc_assert (index < stack_vars_conflict_alloc);
364   stack_vars_conflict[index] = true;
365 }
366
367 /* Check whether the decls associated with luid's X and Y conflict.  */
368
369 static bool
370 stack_var_conflict_p (size_t x, size_t y)
371 {
372   size_t index = triangular_index (x, y);
373   gcc_assert (index < stack_vars_conflict_alloc);
374   return stack_vars_conflict[index];
375 }
376  
377 /* Returns true if TYPE is or contains a union type.  */
378
379 static bool
380 aggregate_contains_union_type (tree type)
381 {
382   tree field;
383
384   if (TREE_CODE (type) == UNION_TYPE
385       || TREE_CODE (type) == QUAL_UNION_TYPE)
386     return true;
387   if (TREE_CODE (type) == ARRAY_TYPE)
388     return aggregate_contains_union_type (TREE_TYPE (type));
389   if (TREE_CODE (type) != RECORD_TYPE)
390     return false;
391
392   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
393     if (TREE_CODE (field) == FIELD_DECL)
394       if (aggregate_contains_union_type (TREE_TYPE (field)))
395         return true;
396
397   return false;
398 }
399
400 /* A subroutine of expand_used_vars.  If two variables X and Y have alias
401    sets that do not conflict, then do add a conflict for these variables
402    in the interference graph.  We also need to make sure to add conflicts
403    for union containing structures.  Else RTL alias analysis comes along
404    and due to type based aliasing rules decides that for two overlapping
405    union temporaries { short s; int i; } accesses to the same mem through
406    different types may not alias and happily reorders stores across
407    life-time boundaries of the temporaries (See PR25654).
408    We also have to mind MEM_IN_STRUCT_P and MEM_SCALAR_P.  */
409
410 static void
411 add_alias_set_conflicts (void)
412 {
413   size_t i, j, n = stack_vars_num;
414
415   for (i = 0; i < n; ++i)
416     {
417       tree type_i = TREE_TYPE (stack_vars[i].decl);
418       bool aggr_i = AGGREGATE_TYPE_P (type_i);
419       bool contains_union;
420
421       contains_union = aggregate_contains_union_type (type_i);
422       for (j = 0; j < i; ++j)
423         {
424           tree type_j = TREE_TYPE (stack_vars[j].decl);
425           bool aggr_j = AGGREGATE_TYPE_P (type_j);
426           if (aggr_i != aggr_j
427               /* Either the objects conflict by means of type based
428                  aliasing rules, or we need to add a conflict.  */
429               || !objects_must_conflict_p (type_i, type_j)
430               /* In case the types do not conflict ensure that access
431                  to elements will conflict.  In case of unions we have
432                  to be careful as type based aliasing rules may say
433                  access to the same memory does not conflict.  So play
434                  safe and add a conflict in this case.  */
435               || contains_union)
436             add_stack_var_conflict (i, j);
437         }
438     }
439 }
440
441 /* A subroutine of partition_stack_vars.  A comparison function for qsort,
442    sorting an array of indices by the size and type of the object.  */
443
444 static int
445 stack_var_size_cmp (const void *a, const void *b)
446 {
447   HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size;
448   HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size;
449   tree decla, declb;
450   unsigned int uida, uidb;
451
452   if (sa < sb)
453     return -1;
454   if (sa > sb)
455     return 1;
456   decla = stack_vars[*(const size_t *)a].decl;
457   declb = stack_vars[*(const size_t *)b].decl;
458   /* For stack variables of the same size use and id of the decls
459      to make the sort stable.  Two SSA names are compared by their
460      version, SSA names come before non-SSA names, and two normal
461      decls are compared by their DECL_UID.  */
462   if (TREE_CODE (decla) == SSA_NAME)
463     {
464       if (TREE_CODE (declb) == SSA_NAME)
465         uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
466       else
467         return -1;
468     }
469   else if (TREE_CODE (declb) == SSA_NAME)
470     return 1;
471   else
472     uida = DECL_UID (decla), uidb = DECL_UID (declb);
473   if (uida < uidb)
474     return -1;
475   if (uida > uidb)
476     return 1;
477   return 0;
478 }
479
480
481 /* If the points-to solution *PI points to variables that are in a partition
482    together with other variables add all partition members to the pointed-to
483    variables bitmap.  */
484
485 static void
486 add_partitioned_vars_to_ptset (struct pt_solution *pt,
487                                struct pointer_map_t *decls_to_partitions,
488                                struct pointer_set_t *visited, bitmap temp)
489 {
490   bitmap_iterator bi;
491   unsigned i;
492   bitmap *part;
493
494   if (pt->anything
495       || pt->vars == NULL
496       /* The pointed-to vars bitmap is shared, it is enough to
497          visit it once.  */
498       || pointer_set_insert(visited, pt->vars))
499     return;
500
501   bitmap_clear (temp);
502
503   /* By using a temporary bitmap to store all members of the partitions
504      we have to add we make sure to visit each of the partitions only
505      once.  */
506   EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
507     if ((!temp
508          || !bitmap_bit_p (temp, i))
509         && (part = (bitmap *) pointer_map_contains (decls_to_partitions,
510                                                     (void *)(size_t) i)))
511       bitmap_ior_into (temp, *part);
512   if (!bitmap_empty_p (temp))
513     bitmap_ior_into (pt->vars, temp);
514 }
515
516 /* Update points-to sets based on partition info, so we can use them on RTL.
517    The bitmaps representing stack partitions will be saved until expand,
518    where partitioned decls used as bases in memory expressions will be
519    rewritten.  */
520
521 static void
522 update_alias_info_with_stack_vars (void)
523 {
524   struct pointer_map_t *decls_to_partitions = NULL;
525   size_t i, j;
526   tree var = NULL_TREE;
527
528   for (i = 0; i < stack_vars_num; i++)
529     {
530       bitmap part = NULL;
531       tree name;
532       struct ptr_info_def *pi;
533
534       /* Not interested in partitions with single variable.  */
535       if (stack_vars[i].representative != i
536           || stack_vars[i].next == EOC)
537         continue;
538
539       if (!decls_to_partitions)
540         {
541           decls_to_partitions = pointer_map_create ();
542           cfun->gimple_df->decls_to_pointers = pointer_map_create ();
543         }
544
545       /* Create an SSA_NAME that points to the partition for use
546          as base during alias-oracle queries on RTL for bases that
547          have been partitioned.  */
548       if (var == NULL_TREE)
549         var = create_tmp_var (ptr_type_node, NULL);
550       name = make_ssa_name (var, NULL);
551
552       /* Create bitmaps representing partitions.  They will be used for
553          points-to sets later, so use GGC alloc.  */
554       part = BITMAP_GGC_ALLOC ();
555       for (j = i; j != EOC; j = stack_vars[j].next)
556         {
557           tree decl = stack_vars[j].decl;
558           unsigned int uid = DECL_UID (decl);
559           /* We should never end up partitioning SSA names (though they
560              may end up on the stack).  Neither should we allocate stack
561              space to something that is unused and thus unreferenced.  */
562           gcc_assert (DECL_P (decl)
563                       && referenced_var_lookup (uid));
564           bitmap_set_bit (part, uid);
565           *((bitmap *) pointer_map_insert (decls_to_partitions,
566                                            (void *)(size_t) uid)) = part;
567           *((tree *) pointer_map_insert (cfun->gimple_df->decls_to_pointers,
568                                          decl)) = name;
569         }
570
571       /* Make the SSA name point to all partition members.  */
572       pi = get_ptr_info (name);
573       pt_solution_set (&pi->pt, part);
574     }
575
576   /* Make all points-to sets that contain one member of a partition
577      contain all members of the partition.  */
578   if (decls_to_partitions)
579     {
580       unsigned i;
581       struct pointer_set_t *visited = pointer_set_create ();
582       bitmap temp = BITMAP_ALLOC (NULL);
583
584       for (i = 1; i < num_ssa_names; i++)
585         {
586           tree name = ssa_name (i);
587           struct ptr_info_def *pi;
588
589           if (name
590               && POINTER_TYPE_P (TREE_TYPE (name))
591               && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
592             add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
593                                            visited, temp);
594         }
595
596       add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
597                                      decls_to_partitions, visited, temp);
598       add_partitioned_vars_to_ptset (&cfun->gimple_df->callused,
599                                      decls_to_partitions, visited, temp);
600
601       pointer_set_destroy (visited);
602       pointer_map_destroy (decls_to_partitions);
603       BITMAP_FREE (temp);
604     }
605 }
606
607 /* A subroutine of partition_stack_vars.  The UNION portion of a UNION/FIND
608    partitioning algorithm.  Partitions A and B are known to be non-conflicting.
609    Merge them into a single partition A.
610
611    At the same time, add OFFSET to all variables in partition B.  At the end
612    of the partitioning process we've have a nice block easy to lay out within
613    the stack frame.  */
614
615 static void
616 union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset)
617 {
618   size_t i, last;
619
620   /* Update each element of partition B with the given offset,
621      and merge them into partition A.  */
622   for (last = i = b; i != EOC; last = i, i = stack_vars[i].next)
623     {
624       stack_vars[i].offset += offset;
625       stack_vars[i].representative = a;
626     }
627   stack_vars[last].next = stack_vars[a].next;
628   stack_vars[a].next = b;
629
630   /* Update the required alignment of partition A to account for B.  */
631   if (stack_vars[a].alignb < stack_vars[b].alignb)
632     stack_vars[a].alignb = stack_vars[b].alignb;
633
634   /* Update the interference graph and merge the conflicts.  */
635   for (last = stack_vars_num, i = 0; i < last; ++i)
636     if (stack_var_conflict_p (b, i))
637       add_stack_var_conflict (a, i);
638 }
639
640 /* A subroutine of expand_used_vars.  Binpack the variables into
641    partitions constrained by the interference graph.  The overall
642    algorithm used is as follows:
643
644         Sort the objects by size.
645         For each object A {
646           S = size(A)
647           O = 0
648           loop {
649             Look for the largest non-conflicting object B with size <= S.
650             UNION (A, B)
651             offset(B) = O
652             O += size(B)
653             S -= size(B)
654           }
655         }
656 */
657
658 static void
659 partition_stack_vars (void)
660 {
661   size_t si, sj, n = stack_vars_num;
662
663   stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
664   for (si = 0; si < n; ++si)
665     stack_vars_sorted[si] = si;
666
667   if (n == 1)
668     return;
669
670   qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp);
671
672   /* Special case: detect when all variables conflict, and thus we can't
673      do anything during the partitioning loop.  It isn't uncommon (with
674      C code at least) to declare all variables at the top of the function,
675      and if we're not inlining, then all variables will be in the same scope.
676      Take advantage of very fast libc routines for this scan.  */
677   gcc_assert (sizeof(bool) == sizeof(char));
678   if (memchr (stack_vars_conflict, false, stack_vars_conflict_alloc) == NULL)
679     return;
680
681   for (si = 0; si < n; ++si)
682     {
683       size_t i = stack_vars_sorted[si];
684       HOST_WIDE_INT isize = stack_vars[i].size;
685       HOST_WIDE_INT offset = 0;
686
687       for (sj = si; sj-- > 0; )
688         {
689           size_t j = stack_vars_sorted[sj];
690           HOST_WIDE_INT jsize = stack_vars[j].size;
691           unsigned int jalign = stack_vars[j].alignb;
692
693           /* Ignore objects that aren't partition representatives.  */
694           if (stack_vars[j].representative != j)
695             continue;
696
697           /* Ignore objects too large for the remaining space.  */
698           if (isize < jsize)
699             continue;
700
701           /* Ignore conflicting objects.  */
702           if (stack_var_conflict_p (i, j))
703             continue;
704
705           /* Refine the remaining space check to include alignment.  */
706           if (offset & (jalign - 1))
707             {
708               HOST_WIDE_INT toff = offset;
709               toff += jalign - 1;
710               toff &= -(HOST_WIDE_INT)jalign;
711               if (isize - (toff - offset) < jsize)
712                 continue;
713
714               isize -= toff - offset;
715               offset = toff;
716             }
717
718           /* UNION the objects, placing J at OFFSET.  */
719           union_stack_vars (i, j, offset);
720
721           isize -= jsize;
722           if (isize == 0)
723             break;
724         }
725     }
726
727   if (optimize)
728     update_alias_info_with_stack_vars ();
729 }
730
731 /* A debugging aid for expand_used_vars.  Dump the generated partitions.  */
732
733 static void
734 dump_stack_var_partition (void)
735 {
736   size_t si, i, j, n = stack_vars_num;
737
738   for (si = 0; si < n; ++si)
739     {
740       i = stack_vars_sorted[si];
741
742       /* Skip variables that aren't partition representatives, for now.  */
743       if (stack_vars[i].representative != i)
744         continue;
745
746       fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
747                " align %u\n", (unsigned long) i, stack_vars[i].size,
748                stack_vars[i].alignb);
749
750       for (j = i; j != EOC; j = stack_vars[j].next)
751         {
752           fputc ('\t', dump_file);
753           print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
754           fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n",
755                    stack_vars[j].offset);
756         }
757     }
758 }
759
760 /* Assign rtl to DECL at frame offset OFFSET.  */
761
762 static void
763 expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset)
764 {
765   /* Alignment is unsigned.   */
766   unsigned HOST_WIDE_INT align;
767   rtx x;
768
769   /* If this fails, we've overflowed the stack frame.  Error nicely?  */
770   gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
771
772   x = plus_constant (virtual_stack_vars_rtx, offset);
773   x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
774
775   if (TREE_CODE (decl) != SSA_NAME)
776     {
777       /* Set alignment we actually gave this decl if it isn't an SSA name.
778          If it is we generate stack slots only accidentally so it isn't as
779          important, we'll simply use the alignment that is already set.  */
780       offset -= frame_phase;
781       align = offset & -offset;
782       align *= BITS_PER_UNIT;
783       if (align == 0)
784         align = STACK_BOUNDARY;
785       else if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
786         align = MAX_SUPPORTED_STACK_ALIGNMENT;
787
788       DECL_ALIGN (decl) = align;
789       DECL_USER_ALIGN (decl) = 0;
790     }
791
792   set_mem_attributes (x, SSAVAR (decl), true);
793   set_rtl (decl, x);
794 }
795
796 /* A subroutine of expand_used_vars.  Give each partition representative
797    a unique location within the stack frame.  Update each partition member
798    with that location.  */
799
800 static void
801 expand_stack_vars (bool (*pred) (tree))
802 {
803   size_t si, i, j, n = stack_vars_num;
804
805   for (si = 0; si < n; ++si)
806     {
807       HOST_WIDE_INT offset;
808
809       i = stack_vars_sorted[si];
810
811       /* Skip variables that aren't partition representatives, for now.  */
812       if (stack_vars[i].representative != i)
813         continue;
814
815       /* Skip variables that have already had rtl assigned.  See also
816          add_stack_var where we perpetrate this pc_rtx hack.  */
817       if ((TREE_CODE (stack_vars[i].decl) == SSA_NAME
818            ? SA.partition_to_pseudo[var_to_partition (SA.map, stack_vars[i].decl)]
819            : DECL_RTL (stack_vars[i].decl)) != pc_rtx)
820         continue;
821
822       /* Check the predicate to see whether this variable should be
823          allocated in this pass.  */
824       if (pred && !pred (stack_vars[i].decl))
825         continue;
826
827       offset = alloc_stack_frame_space (stack_vars[i].size,
828                                         stack_vars[i].alignb);
829
830       /* Create rtl for each variable based on their location within the
831          partition.  */
832       for (j = i; j != EOC; j = stack_vars[j].next)
833         {
834           gcc_assert (stack_vars[j].offset <= stack_vars[i].size);
835           expand_one_stack_var_at (stack_vars[j].decl,
836                                    stack_vars[j].offset + offset);
837         }
838     }
839 }
840
841 /* Take into account all sizes of partitions and reset DECL_RTLs.  */
842 static HOST_WIDE_INT
843 account_stack_vars (void)
844 {
845   size_t si, j, i, n = stack_vars_num;
846   HOST_WIDE_INT size = 0;
847
848   for (si = 0; si < n; ++si)
849     {
850       i = stack_vars_sorted[si];
851
852       /* Skip variables that aren't partition representatives, for now.  */
853       if (stack_vars[i].representative != i)
854         continue;
855
856       size += stack_vars[i].size;
857       for (j = i; j != EOC; j = stack_vars[j].next)
858         set_rtl (stack_vars[j].decl, NULL);
859     }
860   return size;
861 }
862
863 /* A subroutine of expand_one_var.  Called to immediately assign rtl
864    to a variable to be allocated in the stack frame.  */
865
866 static void
867 expand_one_stack_var (tree var)
868 {
869   HOST_WIDE_INT size, offset, align;
870
871   size = tree_low_cst (DECL_SIZE_UNIT (SSAVAR (var)), 1);
872   align = get_decl_align_unit (SSAVAR (var));
873   offset = alloc_stack_frame_space (size, align);
874
875   expand_one_stack_var_at (var, offset);
876 }
877
878 /* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL
879    that will reside in a hard register.  */
880
881 static void
882 expand_one_hard_reg_var (tree var)
883 {
884   rest_of_decl_compilation (var, 0, 0);
885 }
886
887 /* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL
888    that will reside in a pseudo register.  */
889
890 static void
891 expand_one_register_var (tree var)
892 {
893   tree decl = SSAVAR (var);
894   tree type = TREE_TYPE (decl);
895   enum machine_mode reg_mode = promote_decl_mode (decl, NULL);
896   rtx x = gen_reg_rtx (reg_mode);
897
898   set_rtl (var, x);
899
900   /* Note if the object is a user variable.  */
901   if (!DECL_ARTIFICIAL (decl))
902     mark_user_reg (x);
903
904   if (POINTER_TYPE_P (type))
905     mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (type)));
906 }
907
908 /* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL that
909    has some associated error, e.g. its type is error-mark.  We just need
910    to pick something that won't crash the rest of the compiler.  */
911
912 static void
913 expand_one_error_var (tree var)
914 {
915   enum machine_mode mode = DECL_MODE (var);
916   rtx x;
917
918   if (mode == BLKmode)
919     x = gen_rtx_MEM (BLKmode, const0_rtx);
920   else if (mode == VOIDmode)
921     x = const0_rtx;
922   else
923     x = gen_reg_rtx (mode);
924
925   SET_DECL_RTL (var, x);
926 }
927
928 /* A subroutine of expand_one_var.  VAR is a variable that will be
929    allocated to the local stack frame.  Return true if we wish to
930    add VAR to STACK_VARS so that it will be coalesced with other
931    variables.  Return false to allocate VAR immediately.
932
933    This function is used to reduce the number of variables considered
934    for coalescing, which reduces the size of the quadratic problem.  */
935
936 static bool
937 defer_stack_allocation (tree var, bool toplevel)
938 {
939   /* If stack protection is enabled, *all* stack variables must be deferred,
940      so that we can re-order the strings to the top of the frame.  */
941   if (flag_stack_protect)
942     return true;
943
944   /* Variables in the outermost scope automatically conflict with
945      every other variable.  The only reason to want to defer them
946      at all is that, after sorting, we can more efficiently pack
947      small variables in the stack frame.  Continue to defer at -O2.  */
948   if (toplevel && optimize < 2)
949     return false;
950
951   /* Without optimization, *most* variables are allocated from the
952      stack, which makes the quadratic problem large exactly when we
953      want compilation to proceed as quickly as possible.  On the
954      other hand, we don't want the function's stack frame size to
955      get completely out of hand.  So we avoid adding scalars and
956      "small" aggregates to the list at all.  */
957   if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32)
958     return false;
959
960   return true;
961 }
962
963 /* A subroutine of expand_used_vars.  Expand one variable according to
964    its flavor.  Variables to be placed on the stack are not actually
965    expanded yet, merely recorded.  
966    When REALLY_EXPAND is false, only add stack values to be allocated.
967    Return stack usage this variable is supposed to take.
968 */
969
970 static HOST_WIDE_INT
971 expand_one_var (tree var, bool toplevel, bool really_expand)
972 {
973   tree origvar = var;
974   var = SSAVAR (var);
975
976   if (SUPPORTS_STACK_ALIGNMENT
977       && TREE_TYPE (var) != error_mark_node
978       && TREE_CODE (var) == VAR_DECL)
979     {
980       unsigned int align;
981
982       /* Because we don't know if VAR will be in register or on stack,
983          we conservatively assume it will be on stack even if VAR is
984          eventually put into register after RA pass.  For non-automatic
985          variables, which won't be on stack, we collect alignment of
986          type and ignore user specified alignment.  */
987       if (TREE_STATIC (var) || DECL_EXTERNAL (var))
988         align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
989                                    TYPE_MODE (TREE_TYPE (var)),
990                                    TYPE_ALIGN (TREE_TYPE (var)));
991       else
992         align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
993
994       if (crtl->stack_alignment_estimated < align)
995         {
996           /* stack_alignment_estimated shouldn't change after stack
997              realign decision made */
998           gcc_assert(!crtl->stack_realign_processed);
999           crtl->stack_alignment_estimated = align;
1000         }
1001     }
1002
1003   if (TREE_CODE (origvar) == SSA_NAME)
1004     {
1005       gcc_assert (TREE_CODE (var) != VAR_DECL
1006                   || (!DECL_EXTERNAL (var)
1007                       && !DECL_HAS_VALUE_EXPR_P (var)
1008                       && !TREE_STATIC (var)
1009                       && TREE_TYPE (var) != error_mark_node
1010                       && !DECL_HARD_REGISTER (var)
1011                       && really_expand));
1012     }
1013   if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1014     ;
1015   else if (DECL_EXTERNAL (var))
1016     ;
1017   else if (DECL_HAS_VALUE_EXPR_P (var))
1018     ;
1019   else if (TREE_STATIC (var))
1020     ;
1021   else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1022     ;
1023   else if (TREE_TYPE (var) == error_mark_node)
1024     {
1025       if (really_expand)
1026         expand_one_error_var (var);
1027     }
1028   else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1029     {
1030       if (really_expand)
1031         expand_one_hard_reg_var (var);
1032     }
1033   else if (use_register_for_decl (var))
1034     {
1035       if (really_expand)
1036         expand_one_register_var (origvar);
1037     }
1038   else if (defer_stack_allocation (var, toplevel))
1039     add_stack_var (origvar);
1040   else
1041     {
1042       if (really_expand)
1043         expand_one_stack_var (origvar);
1044       return tree_low_cst (DECL_SIZE_UNIT (var), 1);
1045     }
1046   return 0;
1047 }
1048
1049 /* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
1050    expanding variables.  Those variables that can be put into registers
1051    are allocated pseudos; those that can't are put on the stack.
1052
1053    TOPLEVEL is true if this is the outermost BLOCK.  */
1054
1055 static void
1056 expand_used_vars_for_block (tree block, bool toplevel)
1057 {
1058   size_t i, j, old_sv_num, this_sv_num, new_sv_num;
1059   tree t;
1060
1061   old_sv_num = toplevel ? 0 : stack_vars_num;
1062
1063   /* Expand all variables at this level.  */
1064   for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1065     if (TREE_USED (t))
1066       expand_one_var (t, toplevel, true);
1067
1068   this_sv_num = stack_vars_num;
1069
1070   /* Expand all variables at containing levels.  */
1071   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1072     expand_used_vars_for_block (t, false);
1073
1074   /* Since we do not track exact variable lifetimes (which is not even
1075      possible for variables whose address escapes), we mirror the block
1076      tree in the interference graph.  Here we cause all variables at this
1077      level, and all sublevels, to conflict.  Do make certain that a
1078      variable conflicts with itself.  */
1079   if (old_sv_num < this_sv_num)
1080     {
1081       new_sv_num = stack_vars_num;
1082       resize_stack_vars_conflict (new_sv_num);
1083
1084       for (i = old_sv_num; i < new_sv_num; ++i)
1085         for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
1086           add_stack_var_conflict (i, j);
1087     }
1088 }
1089
1090 /* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
1091    and clear TREE_USED on all local variables.  */
1092
1093 static void
1094 clear_tree_used (tree block)
1095 {
1096   tree t;
1097
1098   for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1099     /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1100       TREE_USED (t) = 0;
1101
1102   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1103     clear_tree_used (t);
1104 }
1105
1106 /* Examine TYPE and determine a bit mask of the following features.  */
1107
1108 #define SPCT_HAS_LARGE_CHAR_ARRAY       1
1109 #define SPCT_HAS_SMALL_CHAR_ARRAY       2
1110 #define SPCT_HAS_ARRAY                  4
1111 #define SPCT_HAS_AGGREGATE              8
1112
1113 static unsigned int
1114 stack_protect_classify_type (tree type)
1115 {
1116   unsigned int ret = 0;
1117   tree t;
1118
1119   switch (TREE_CODE (type))
1120     {
1121     case ARRAY_TYPE:
1122       t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1123       if (t == char_type_node
1124           || t == signed_char_type_node
1125           || t == unsigned_char_type_node)
1126         {
1127           unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1128           unsigned HOST_WIDE_INT len;
1129
1130           if (!TYPE_SIZE_UNIT (type)
1131               || !host_integerp (TYPE_SIZE_UNIT (type), 1))
1132             len = max;
1133           else
1134             len = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
1135
1136           if (len < max)
1137             ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1138           else
1139             ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1140         }
1141       else
1142         ret = SPCT_HAS_ARRAY;
1143       break;
1144
1145     case UNION_TYPE:
1146     case QUAL_UNION_TYPE:
1147     case RECORD_TYPE:
1148       ret = SPCT_HAS_AGGREGATE;
1149       for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1150         if (TREE_CODE (t) == FIELD_DECL)
1151           ret |= stack_protect_classify_type (TREE_TYPE (t));
1152       break;
1153
1154     default:
1155       break;
1156     }
1157
1158   return ret;
1159 }
1160
1161 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1162    part of the local stack frame.  Remember if we ever return nonzero for
1163    any variable in this function.  The return value is the phase number in
1164    which the variable should be allocated.  */
1165
1166 static int
1167 stack_protect_decl_phase (tree decl)
1168 {
1169   unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1170   int ret = 0;
1171
1172   if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1173     has_short_buffer = true;
1174
1175   if (flag_stack_protect == 2)
1176     {
1177       if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1178           && !(bits & SPCT_HAS_AGGREGATE))
1179         ret = 1;
1180       else if (bits & SPCT_HAS_ARRAY)
1181         ret = 2;
1182     }
1183   else
1184     ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1185
1186   if (ret)
1187     has_protected_decls = true;
1188
1189   return ret;
1190 }
1191
1192 /* Two helper routines that check for phase 1 and phase 2.  These are used
1193    as callbacks for expand_stack_vars.  */
1194
1195 static bool
1196 stack_protect_decl_phase_1 (tree decl)
1197 {
1198   return stack_protect_decl_phase (decl) == 1;
1199 }
1200
1201 static bool
1202 stack_protect_decl_phase_2 (tree decl)
1203 {
1204   return stack_protect_decl_phase (decl) == 2;
1205 }
1206
1207 /* Ensure that variables in different stack protection phases conflict
1208    so that they are not merged and share the same stack slot.  */
1209
1210 static void
1211 add_stack_protection_conflicts (void)
1212 {
1213   size_t i, j, n = stack_vars_num;
1214   unsigned char *phase;
1215
1216   phase = XNEWVEC (unsigned char, n);
1217   for (i = 0; i < n; ++i)
1218     phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1219
1220   for (i = 0; i < n; ++i)
1221     {
1222       unsigned char ph_i = phase[i];
1223       for (j = 0; j < i; ++j)
1224         if (ph_i != phase[j])
1225           add_stack_var_conflict (i, j);
1226     }
1227
1228   XDELETEVEC (phase);
1229 }
1230
1231 /* Create a decl for the guard at the top of the stack frame.  */
1232
1233 static void
1234 create_stack_guard (void)
1235 {
1236   tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1237                            VAR_DECL, NULL, ptr_type_node);
1238   TREE_THIS_VOLATILE (guard) = 1;
1239   TREE_USED (guard) = 1;
1240   expand_one_stack_var (guard);
1241   crtl->stack_protect_guard = guard;
1242 }
1243
1244 /* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
1245    expanding variables.  Those variables that can be put into registers
1246    are allocated pseudos; those that can't are put on the stack.
1247
1248    TOPLEVEL is true if this is the outermost BLOCK.  */
1249
1250 static HOST_WIDE_INT
1251 account_used_vars_for_block (tree block, bool toplevel)
1252 {
1253   size_t i, j, old_sv_num, this_sv_num, new_sv_num;
1254   tree t;
1255   HOST_WIDE_INT size = 0;
1256
1257   old_sv_num = toplevel ? 0 : stack_vars_num;
1258
1259   /* Expand all variables at this level.  */
1260   for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
1261     if (TREE_USED (t))
1262       size += expand_one_var (t, toplevel, false);
1263
1264   this_sv_num = stack_vars_num;
1265
1266   /* Expand all variables at containing levels.  */
1267   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1268     size += account_used_vars_for_block (t, false);
1269
1270   /* Since we do not track exact variable lifetimes (which is not even
1271      possible for variables whose address escapes), we mirror the block
1272      tree in the interference graph.  Here we cause all variables at this
1273      level, and all sublevels, to conflict.  Do make certain that a
1274      variable conflicts with itself.  */
1275   if (old_sv_num < this_sv_num)
1276     {
1277       new_sv_num = stack_vars_num;
1278       resize_stack_vars_conflict (new_sv_num);
1279
1280       for (i = old_sv_num; i < new_sv_num; ++i)
1281         for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
1282           add_stack_var_conflict (i, j);
1283     }
1284   return size;
1285 }
1286
1287 /* Prepare for expanding variables.  */
1288 static void 
1289 init_vars_expansion (void)
1290 {
1291   tree t;
1292   /* Set TREE_USED on all variables in the local_decls.  */
1293   for (t = cfun->local_decls; t; t = TREE_CHAIN (t))
1294     TREE_USED (TREE_VALUE (t)) = 1;
1295
1296   /* Clear TREE_USED on all variables associated with a block scope.  */
1297   clear_tree_used (DECL_INITIAL (current_function_decl));
1298
1299   /* Initialize local stack smashing state.  */
1300   has_protected_decls = false;
1301   has_short_buffer = false;
1302 }
1303
1304 /* Free up stack variable graph data.  */
1305 static void
1306 fini_vars_expansion (void)
1307 {
1308   XDELETEVEC (stack_vars);
1309   XDELETEVEC (stack_vars_sorted);
1310   XDELETEVEC (stack_vars_conflict);
1311   stack_vars = NULL;
1312   stack_vars_alloc = stack_vars_num = 0;
1313   stack_vars_conflict = NULL;
1314   stack_vars_conflict_alloc = 0;
1315 }
1316
1317 /* Make a fair guess for the size of the stack frame of the current
1318    function.  This doesn't have to be exact, the result is only used
1319    in the inline heuristics.  So we don't want to run the full stack
1320    var packing algorithm (which is quadratic in the number of stack
1321    vars).  Instead, we calculate the total size of all stack vars.
1322    This turns out to be a pretty fair estimate -- packing of stack
1323    vars doesn't happen very often.  */
1324
1325 HOST_WIDE_INT
1326 estimated_stack_frame_size (void)
1327 {
1328   HOST_WIDE_INT size = 0;
1329   size_t i;
1330   tree t, outer_block = DECL_INITIAL (current_function_decl);
1331
1332   init_vars_expansion ();
1333
1334   for (t = cfun->local_decls; t; t = TREE_CHAIN (t))
1335     {
1336       tree var = TREE_VALUE (t);
1337
1338       if (TREE_USED (var))
1339         size += expand_one_var (var, true, false);
1340       TREE_USED (var) = 1;
1341     }
1342   size += account_used_vars_for_block (outer_block, true);
1343
1344   if (stack_vars_num > 0)
1345     {
1346       /* Fake sorting the stack vars for account_stack_vars ().  */
1347       stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1348       for (i = 0; i < stack_vars_num; ++i)
1349         stack_vars_sorted[i] = i;
1350       size += account_stack_vars ();
1351       fini_vars_expansion ();
1352     }
1353
1354   return size;
1355 }
1356
1357 /* Expand all variables used in the function.  */
1358
1359 static void
1360 expand_used_vars (void)
1361 {
1362   tree t, next, outer_block = DECL_INITIAL (current_function_decl);
1363   unsigned i;
1364
1365   /* Compute the phase of the stack frame for this function.  */
1366   {
1367     int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1368     int off = STARTING_FRAME_OFFSET % align;
1369     frame_phase = off ? align - off : 0;
1370   }
1371
1372   init_vars_expansion ();
1373
1374   for (i = 0; i < SA.map->num_partitions; i++)
1375     {
1376       tree var = partition_to_var (SA.map, i);
1377
1378       gcc_assert (is_gimple_reg (var));
1379       if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1380         expand_one_var (var, true, true);
1381       else
1382         {
1383           /* This is a PARM_DECL or RESULT_DECL.  For those partitions that
1384              contain the default def (representing the parm or result itself)
1385              we don't do anything here.  But those which don't contain the
1386              default def (representing a temporary based on the parm/result)
1387              we need to allocate space just like for normal VAR_DECLs.  */
1388           if (!bitmap_bit_p (SA.partition_has_default_def, i))
1389             {
1390               expand_one_var (var, true, true);
1391               gcc_assert (SA.partition_to_pseudo[i]);
1392             }
1393         }
1394     }
1395
1396   /* At this point all variables on the local_decls with TREE_USED
1397      set are not associated with any block scope.  Lay them out.  */
1398   t = cfun->local_decls;
1399   cfun->local_decls = NULL_TREE;
1400   for (; t; t = next)
1401     {
1402       tree var = TREE_VALUE (t);
1403       bool expand_now = false;
1404
1405       next = TREE_CHAIN (t);
1406
1407       /* Expanded above already.  */
1408       if (is_gimple_reg (var))
1409         {
1410           TREE_USED (var) = 0;
1411           ggc_free (t);
1412           continue;
1413         }
1414       /* We didn't set a block for static or extern because it's hard
1415          to tell the difference between a global variable (re)declared
1416          in a local scope, and one that's really declared there to
1417          begin with.  And it doesn't really matter much, since we're
1418          not giving them stack space.  Expand them now.  */
1419       else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1420         expand_now = true;
1421
1422       /* If the variable is not associated with any block, then it
1423          was created by the optimizers, and could be live anywhere
1424          in the function.  */
1425       else if (TREE_USED (var))
1426         expand_now = true;
1427
1428       /* Finally, mark all variables on the list as used.  We'll use
1429          this in a moment when we expand those associated with scopes.  */
1430       TREE_USED (var) = 1;
1431
1432       if (expand_now)
1433         {
1434           expand_one_var (var, true, true);
1435           if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1436             {
1437               rtx rtl = DECL_RTL_IF_SET (var);
1438
1439               /* Keep artificial non-ignored vars in cfun->local_decls
1440                  chain until instantiate_decls.  */
1441               if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1442                 {
1443                   TREE_CHAIN (t) = cfun->local_decls;
1444                   cfun->local_decls = t;
1445                   continue;
1446                 }
1447             }
1448         }
1449
1450       ggc_free (t);
1451     }
1452
1453   /* At this point, all variables within the block tree with TREE_USED
1454      set are actually used by the optimized function.  Lay them out.  */
1455   expand_used_vars_for_block (outer_block, true);
1456
1457   if (stack_vars_num > 0)
1458     {
1459       /* Due to the way alias sets work, no variables with non-conflicting
1460          alias sets may be assigned the same address.  Add conflicts to
1461          reflect this.  */
1462       add_alias_set_conflicts ();
1463
1464       /* If stack protection is enabled, we don't share space between
1465          vulnerable data and non-vulnerable data.  */
1466       if (flag_stack_protect)
1467         add_stack_protection_conflicts ();
1468
1469       /* Now that we have collected all stack variables, and have computed a
1470          minimal interference graph, attempt to save some stack space.  */
1471       partition_stack_vars ();
1472       if (dump_file)
1473         dump_stack_var_partition ();
1474     }
1475
1476   /* There are several conditions under which we should create a
1477      stack guard: protect-all, alloca used, protected decls present.  */
1478   if (flag_stack_protect == 2
1479       || (flag_stack_protect
1480           && (cfun->calls_alloca || has_protected_decls)))
1481     create_stack_guard ();
1482
1483   /* Assign rtl to each variable based on these partitions.  */
1484   if (stack_vars_num > 0)
1485     {
1486       /* Reorder decls to be protected by iterating over the variables
1487          array multiple times, and allocating out of each phase in turn.  */
1488       /* ??? We could probably integrate this into the qsort we did
1489          earlier, such that we naturally see these variables first,
1490          and thus naturally allocate things in the right order.  */
1491       if (has_protected_decls)
1492         {
1493           /* Phase 1 contains only character arrays.  */
1494           expand_stack_vars (stack_protect_decl_phase_1);
1495
1496           /* Phase 2 contains other kinds of arrays.  */
1497           if (flag_stack_protect == 2)
1498             expand_stack_vars (stack_protect_decl_phase_2);
1499         }
1500
1501       expand_stack_vars (NULL);
1502
1503       fini_vars_expansion ();
1504     }
1505
1506   /* If the target requires that FRAME_OFFSET be aligned, do it.  */
1507   if (STACK_ALIGNMENT_NEEDED)
1508     {
1509       HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1510       if (!FRAME_GROWS_DOWNWARD)
1511         frame_offset += align - 1;
1512       frame_offset &= -align;
1513     }
1514 }
1515
1516
1517 /* If we need to produce a detailed dump, print the tree representation
1518    for STMT to the dump file.  SINCE is the last RTX after which the RTL
1519    generated for STMT should have been appended.  */
1520
1521 static void
1522 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx since)
1523 {
1524   if (dump_file && (dump_flags & TDF_DETAILS))
1525     {
1526       fprintf (dump_file, "\n;; ");
1527       print_gimple_stmt (dump_file, stmt, 0,
1528                          TDF_SLIM | (dump_flags & TDF_LINENO));
1529       fprintf (dump_file, "\n");
1530
1531       print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1532     }
1533 }
1534
1535 /* Maps the blocks that do not contain tree labels to rtx labels.  */
1536
1537 static struct pointer_map_t *lab_rtx_for_bb;
1538
1539 /* Returns the label_rtx expression for a label starting basic block BB.  */
1540
1541 static rtx
1542 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
1543 {
1544   gimple_stmt_iterator gsi;
1545   tree lab;
1546   gimple lab_stmt;
1547   void **elt;
1548
1549   if (bb->flags & BB_RTL)
1550     return block_label (bb);
1551
1552   elt = pointer_map_contains (lab_rtx_for_bb, bb);
1553   if (elt)
1554     return (rtx) *elt;
1555
1556   /* Find the tree label if it is present.  */
1557      
1558   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1559     {
1560       lab_stmt = gsi_stmt (gsi);
1561       if (gimple_code (lab_stmt) != GIMPLE_LABEL)
1562         break;
1563
1564       lab = gimple_label_label (lab_stmt);
1565       if (DECL_NONLOCAL (lab))
1566         break;
1567
1568       return label_rtx (lab);
1569     }
1570
1571   elt = pointer_map_insert (lab_rtx_for_bb, bb);
1572   *elt = gen_label_rtx ();
1573   return (rtx) *elt;
1574 }
1575
1576
1577 /* A subroutine of expand_gimple_cond.  Given E, a fallthrough edge
1578    of a basic block where we just expanded the conditional at the end,
1579    possibly clean up the CFG and instruction sequence.  */
1580
1581 static void
1582 maybe_cleanup_end_of_block (edge e)
1583 {
1584   /* Special case: when jumpif decides that the condition is
1585      trivial it emits an unconditional jump (and the necessary
1586      barrier).  But we still have two edges, the fallthru one is
1587      wrong.  purge_dead_edges would clean this up later.  Unfortunately
1588      we have to insert insns (and split edges) before
1589      find_many_sub_basic_blocks and hence before purge_dead_edges.
1590      But splitting edges might create new blocks which depend on the
1591      fact that if there are two edges there's no barrier.  So the
1592      barrier would get lost and verify_flow_info would ICE.  Instead
1593      of auditing all edge splitters to care for the barrier (which
1594      normally isn't there in a cleaned CFG), fix it here.  */
1595   if (BARRIER_P (get_last_insn ()))
1596     {
1597       basic_block bb = e->src;
1598       rtx insn;
1599       remove_edge (e);
1600       /* Now, we have a single successor block, if we have insns to
1601          insert on the remaining edge we potentially will insert
1602          it at the end of this block (if the dest block isn't feasible)
1603          in order to avoid splitting the edge.  This insertion will take
1604          place in front of the last jump.  But we might have emitted
1605          multiple jumps (conditional and one unconditional) to the
1606          same destination.  Inserting in front of the last one then
1607          is a problem.  See PR 40021.  We fix this by deleting all
1608          jumps except the last unconditional one.  */
1609       insn = PREV_INSN (get_last_insn ());
1610       /* Make sure we have an unconditional jump.  Otherwise we're
1611          confused.  */
1612       gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
1613       for (insn = PREV_INSN (insn); insn != BB_HEAD (bb);)
1614         {
1615           insn = PREV_INSN (insn);
1616           if (JUMP_P (NEXT_INSN (insn)))
1617             delete_insn (NEXT_INSN (insn));
1618         }
1619     }
1620 }
1621
1622 /* A subroutine of expand_gimple_basic_block.  Expand one GIMPLE_COND.
1623    Returns a new basic block if we've terminated the current basic
1624    block and created a new one.  */
1625
1626 static basic_block
1627 expand_gimple_cond (basic_block bb, gimple stmt)
1628 {
1629   basic_block new_bb, dest;
1630   edge new_edge;
1631   edge true_edge;
1632   edge false_edge;
1633   rtx last2, last;
1634   enum tree_code code;
1635   tree op0, op1;
1636
1637   code = gimple_cond_code (stmt);
1638   op0 = gimple_cond_lhs (stmt);
1639   op1 = gimple_cond_rhs (stmt);
1640   /* We're sometimes presented with such code:
1641        D.123_1 = x < y;
1642        if (D.123_1 != 0)
1643          ...
1644      This would expand to two comparisons which then later might
1645      be cleaned up by combine.  But some pattern matchers like if-conversion
1646      work better when there's only one compare, so make up for this
1647      here as special exception if TER would have made the same change.  */
1648   if (gimple_cond_single_var_p (stmt)
1649       && SA.values
1650       && TREE_CODE (op0) == SSA_NAME
1651       && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
1652     {
1653       gimple second = SSA_NAME_DEF_STMT (op0);
1654       if (gimple_code (second) == GIMPLE_ASSIGN
1655           && TREE_CODE_CLASS (gimple_assign_rhs_code (second))
1656              == tcc_comparison)
1657         {
1658           code = gimple_assign_rhs_code (second);
1659           op0 = gimple_assign_rhs1 (second);
1660           op1 = gimple_assign_rhs2 (second);
1661         }
1662     }
1663
1664   last2 = last = get_last_insn ();
1665
1666   extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1667   if (gimple_has_location (stmt))
1668     {
1669       set_curr_insn_source_location (gimple_location (stmt));
1670       set_curr_insn_block (gimple_block (stmt));
1671     }
1672
1673   /* These flags have no purpose in RTL land.  */
1674   true_edge->flags &= ~EDGE_TRUE_VALUE;
1675   false_edge->flags &= ~EDGE_FALSE_VALUE;
1676
1677   /* We can either have a pure conditional jump with one fallthru edge or
1678      two-way jump that needs to be decomposed into two basic blocks.  */
1679   if (false_edge->dest == bb->next_bb)
1680     {
1681       jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest));
1682       add_reg_br_prob_note (last, true_edge->probability);
1683       maybe_dump_rtl_for_gimple_stmt (stmt, last);
1684       if (true_edge->goto_locus)
1685         {
1686           set_curr_insn_source_location (true_edge->goto_locus);
1687           set_curr_insn_block (true_edge->goto_block);
1688           true_edge->goto_locus = curr_insn_locator ();
1689         }
1690       true_edge->goto_block = NULL;
1691       false_edge->flags |= EDGE_FALLTHRU;
1692       maybe_cleanup_end_of_block (false_edge);
1693       return NULL;
1694     }
1695   if (true_edge->dest == bb->next_bb)
1696     {
1697       jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest));
1698       add_reg_br_prob_note (last, false_edge->probability);
1699       maybe_dump_rtl_for_gimple_stmt (stmt, last);
1700       if (false_edge->goto_locus)
1701         {
1702           set_curr_insn_source_location (false_edge->goto_locus);
1703           set_curr_insn_block (false_edge->goto_block);
1704           false_edge->goto_locus = curr_insn_locator ();
1705         }
1706       false_edge->goto_block = NULL;
1707       true_edge->flags |= EDGE_FALLTHRU;
1708       maybe_cleanup_end_of_block (true_edge);
1709       return NULL;
1710     }
1711
1712   jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest));
1713   add_reg_br_prob_note (last, true_edge->probability);
1714   last = get_last_insn ();
1715   if (false_edge->goto_locus)
1716     {
1717       set_curr_insn_source_location (false_edge->goto_locus);
1718       set_curr_insn_block (false_edge->goto_block);
1719       false_edge->goto_locus = curr_insn_locator ();
1720     }
1721   false_edge->goto_block = NULL;
1722   emit_jump (label_rtx_for_bb (false_edge->dest));
1723
1724   BB_END (bb) = last;
1725   if (BARRIER_P (BB_END (bb)))
1726     BB_END (bb) = PREV_INSN (BB_END (bb));
1727   update_bb_for_insn (bb);
1728
1729   new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1730   dest = false_edge->dest;
1731   redirect_edge_succ (false_edge, new_bb);
1732   false_edge->flags |= EDGE_FALLTHRU;
1733   new_bb->count = false_edge->count;
1734   new_bb->frequency = EDGE_FREQUENCY (false_edge);
1735   new_edge = make_edge (new_bb, dest, 0);
1736   new_edge->probability = REG_BR_PROB_BASE;
1737   new_edge->count = new_bb->count;
1738   if (BARRIER_P (BB_END (new_bb)))
1739     BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
1740   update_bb_for_insn (new_bb);
1741
1742   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
1743
1744   if (true_edge->goto_locus)
1745     {
1746       set_curr_insn_source_location (true_edge->goto_locus);
1747       set_curr_insn_block (true_edge->goto_block);
1748       true_edge->goto_locus = curr_insn_locator ();
1749     }
1750   true_edge->goto_block = NULL;
1751
1752   return new_bb;
1753 }
1754
1755 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
1756    statement STMT.  */
1757
1758 static void
1759 expand_call_stmt (gimple stmt)
1760 {
1761   tree exp;
1762   tree lhs = gimple_call_lhs (stmt);
1763   size_t i;
1764
1765   exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
1766
1767   CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
1768   TREE_TYPE (exp) = gimple_call_return_type (stmt);
1769   CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
1770
1771   for (i = 0; i < gimple_call_num_args (stmt); i++)
1772     CALL_EXPR_ARG (exp, i) = gimple_call_arg (stmt, i);
1773
1774   if (gimple_has_side_effects (stmt))
1775     TREE_SIDE_EFFECTS (exp) = 1;
1776
1777   if (gimple_call_nothrow_p (stmt))
1778     TREE_NOTHROW (exp) = 1;
1779
1780   CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
1781   CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
1782   CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
1783   CALL_CANNOT_INLINE_P (exp) = gimple_call_cannot_inline_p (stmt);
1784   CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
1785   SET_EXPR_LOCATION (exp, gimple_location (stmt));
1786   TREE_BLOCK (exp) = gimple_block (stmt);
1787
1788   if (lhs)
1789     expand_assignment (lhs, exp, false);
1790   else
1791     expand_expr_real_1 (exp, const0_rtx, VOIDmode, EXPAND_NORMAL, NULL);
1792 }
1793
1794 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
1795    STMT that doesn't require special handling for outgoing edges.  That
1796    is no tailcalls and no GIMPLE_COND.  */
1797
1798 static void
1799 expand_gimple_stmt_1 (gimple stmt)
1800 {
1801   tree op0;
1802   switch (gimple_code (stmt))
1803     {
1804     case GIMPLE_GOTO:
1805       op0 = gimple_goto_dest (stmt);
1806       if (TREE_CODE (op0) == LABEL_DECL)
1807         expand_goto (op0);
1808       else
1809         expand_computed_goto (op0);
1810       break;
1811     case GIMPLE_LABEL:
1812       expand_label (gimple_label_label (stmt));
1813       break;
1814     case GIMPLE_NOP:
1815     case GIMPLE_PREDICT:
1816       break;
1817     case GIMPLE_SWITCH:
1818       expand_case (stmt);
1819       break;
1820     case GIMPLE_ASM:
1821       expand_asm_stmt (stmt);
1822       break;
1823     case GIMPLE_CALL:
1824       expand_call_stmt (stmt);
1825       break;
1826
1827     case GIMPLE_RETURN:
1828       op0 = gimple_return_retval (stmt);
1829
1830       if (op0 && op0 != error_mark_node)
1831         {
1832           tree result = DECL_RESULT (current_function_decl);
1833
1834           /* If we are not returning the current function's RESULT_DECL,
1835              build an assignment to it.  */
1836           if (op0 != result)
1837             {
1838               /* I believe that a function's RESULT_DECL is unique.  */
1839               gcc_assert (TREE_CODE (op0) != RESULT_DECL);
1840
1841               /* ??? We'd like to use simply expand_assignment here,
1842                  but this fails if the value is of BLKmode but the return
1843                  decl is a register.  expand_return has special handling
1844                  for this combination, which eventually should move
1845                  to common code.  See comments there.  Until then, let's
1846                  build a modify expression :-/  */
1847               op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
1848                             result, op0);
1849             }
1850         }
1851       if (!op0)
1852         expand_null_return ();
1853       else
1854         expand_return (op0);
1855       break;
1856
1857     case GIMPLE_ASSIGN:
1858       {
1859         tree lhs = gimple_assign_lhs (stmt);
1860
1861         /* Tree expand used to fiddle with |= and &= of two bitfield
1862            COMPONENT_REFs here.  This can't happen with gimple, the LHS
1863            of binary assigns must be a gimple reg.  */
1864
1865         if (TREE_CODE (lhs) != SSA_NAME
1866             || get_gimple_rhs_class (gimple_expr_code (stmt))
1867                == GIMPLE_SINGLE_RHS)
1868           {
1869             tree rhs = gimple_assign_rhs1 (stmt);
1870             gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
1871                         == GIMPLE_SINGLE_RHS);
1872             if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
1873               SET_EXPR_LOCATION (rhs, gimple_location (stmt));
1874             expand_assignment (lhs, rhs,
1875                                gimple_assign_nontemporal_move_p (stmt));
1876           }
1877         else
1878           {
1879             rtx target, temp;
1880             bool nontemporal = gimple_assign_nontemporal_move_p (stmt);
1881             struct separate_ops ops;
1882             bool promoted = false;
1883
1884             target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
1885             if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
1886               promoted = true;
1887
1888             ops.code = gimple_assign_rhs_code (stmt);
1889             ops.type = TREE_TYPE (lhs);
1890             switch (get_gimple_rhs_class (gimple_expr_code (stmt)))
1891               {
1892                 case GIMPLE_BINARY_RHS:
1893                   ops.op1 = gimple_assign_rhs2 (stmt);
1894                   /* Fallthru */
1895                 case GIMPLE_UNARY_RHS:
1896                   ops.op0 = gimple_assign_rhs1 (stmt);
1897                   break;
1898                 default:
1899                   gcc_unreachable ();
1900               }
1901             ops.location = gimple_location (stmt);
1902
1903             /* If we want to use a nontemporal store, force the value to
1904                register first.  If we store into a promoted register,
1905                don't directly expand to target.  */
1906             temp = nontemporal || promoted ? NULL_RTX : target;
1907             temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
1908                                        EXPAND_NORMAL);
1909
1910             if (temp == target)
1911               ;
1912             else if (promoted)
1913               {
1914                 int unsignedp = SUBREG_PROMOTED_UNSIGNED_P (target);
1915                 /* If TEMP is a VOIDmode constant, use convert_modes to make
1916                    sure that we properly convert it.  */
1917                 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
1918                   {
1919                     temp = convert_modes (GET_MODE (target),
1920                                           TYPE_MODE (ops.type),
1921                                           temp, unsignedp);
1922                     temp = convert_modes (GET_MODE (SUBREG_REG (target)),
1923                                           GET_MODE (target), temp, unsignedp);
1924                   }
1925
1926                 convert_move (SUBREG_REG (target), temp, unsignedp);
1927               }
1928             else if (nontemporal && emit_storent_insn (target, temp))
1929               ;
1930             else
1931               {
1932                 temp = force_operand (temp, target);
1933                 if (temp != target)
1934                   emit_move_insn (target, temp);
1935               }
1936           }
1937       }
1938       break;
1939
1940     default:
1941       gcc_unreachable ();
1942     }
1943 }
1944
1945 /* Expand one gimple statement STMT and return the last RTL instruction
1946    before any of the newly generated ones.
1947
1948    In addition to generating the necessary RTL instructions this also
1949    sets REG_EH_REGION notes if necessary and sets the current source
1950    location for diagnostics.  */
1951
1952 static rtx
1953 expand_gimple_stmt (gimple stmt)
1954 {
1955   int lp_nr = 0;
1956   rtx last = NULL;
1957   location_t saved_location = input_location;
1958
1959   last = get_last_insn ();
1960
1961   /* If this is an expression of some kind and it has an associated line
1962      number, then emit the line number before expanding the expression.
1963
1964      We need to save and restore the file and line information so that
1965      errors discovered during expansion are emitted with the right
1966      information.  It would be better of the diagnostic routines
1967      used the file/line information embedded in the tree nodes rather
1968      than globals.  */
1969   gcc_assert (cfun);
1970
1971   if (gimple_has_location (stmt))
1972     {
1973       input_location = gimple_location (stmt);
1974       set_curr_insn_source_location (input_location);
1975
1976       /* Record where the insns produced belong.  */
1977       set_curr_insn_block (gimple_block (stmt));
1978     }
1979
1980   expand_gimple_stmt_1 (stmt);
1981   /* Free any temporaries used to evaluate this statement.  */
1982   free_temp_slots ();
1983
1984   input_location = saved_location;
1985
1986   /* Mark all insns that may trap.  */
1987   lp_nr = lookup_stmt_eh_lp (stmt);
1988   if (lp_nr)
1989     {
1990       rtx insn;
1991       for (insn = next_real_insn (last); insn;
1992            insn = next_real_insn (insn))
1993         {
1994           if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1995               /* If we want exceptions for non-call insns, any
1996                  may_trap_p instruction may throw.  */
1997               && GET_CODE (PATTERN (insn)) != CLOBBER
1998               && GET_CODE (PATTERN (insn)) != USE
1999               && insn_could_throw_p (insn))
2000             make_reg_eh_region_note (insn, 0, lp_nr);
2001         }
2002     }
2003
2004   return last;
2005 }
2006
2007 /* A subroutine of expand_gimple_basic_block.  Expand one GIMPLE_CALL
2008    that has CALL_EXPR_TAILCALL set.  Returns non-null if we actually
2009    generated a tail call (something that might be denied by the ABI
2010    rules governing the call; see calls.c).
2011
2012    Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
2013    can still reach the rest of BB.  The case here is __builtin_sqrt,
2014    where the NaN result goes through the external function (with a
2015    tailcall) and the normal result happens via a sqrt instruction.  */
2016
2017 static basic_block
2018 expand_gimple_tailcall (basic_block bb, gimple stmt, bool *can_fallthru)
2019 {
2020   rtx last2, last;
2021   edge e;
2022   edge_iterator ei;
2023   int probability;
2024   gcov_type count;
2025
2026   last2 = last = expand_gimple_stmt (stmt);
2027
2028   for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
2029     if (CALL_P (last) && SIBLING_CALL_P (last))
2030       goto found;
2031
2032   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2033
2034   *can_fallthru = true;
2035   return NULL;
2036
2037  found:
2038   /* ??? Wouldn't it be better to just reset any pending stack adjust?
2039      Any instructions emitted here are about to be deleted.  */
2040   do_pending_stack_adjust ();
2041
2042   /* Remove any non-eh, non-abnormal edges that don't go to exit.  */
2043   /* ??? I.e. the fallthrough edge.  HOWEVER!  If there were to be
2044      EH or abnormal edges, we shouldn't have created a tail call in
2045      the first place.  So it seems to me we should just be removing
2046      all edges here, or redirecting the existing fallthru edge to
2047      the exit block.  */
2048
2049   probability = 0;
2050   count = 0;
2051
2052   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2053     {
2054       if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
2055         {
2056           if (e->dest != EXIT_BLOCK_PTR)
2057             {
2058               e->dest->count -= e->count;
2059               e->dest->frequency -= EDGE_FREQUENCY (e);
2060               if (e->dest->count < 0)
2061                 e->dest->count = 0;
2062               if (e->dest->frequency < 0)
2063                 e->dest->frequency = 0;
2064             }
2065           count += e->count;
2066           probability += e->probability;
2067           remove_edge (e);
2068         }
2069       else
2070         ei_next (&ei);
2071     }
2072
2073   /* This is somewhat ugly: the call_expr expander often emits instructions
2074      after the sibcall (to perform the function return).  These confuse the
2075      find_many_sub_basic_blocks code, so we need to get rid of these.  */
2076   last = NEXT_INSN (last);
2077   gcc_assert (BARRIER_P (last));
2078
2079   *can_fallthru = false;
2080   while (NEXT_INSN (last))
2081     {
2082       /* For instance an sqrt builtin expander expands if with
2083          sibcall in the then and label for `else`.  */
2084       if (LABEL_P (NEXT_INSN (last)))
2085         {
2086           *can_fallthru = true;
2087           break;
2088         }
2089       delete_insn (NEXT_INSN (last));
2090     }
2091
2092   e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
2093   e->probability += probability;
2094   e->count += count;
2095   BB_END (bb) = last;
2096   update_bb_for_insn (bb);
2097
2098   if (NEXT_INSN (last))
2099     {
2100       bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2101
2102       last = BB_END (bb);
2103       if (BARRIER_P (last))
2104         BB_END (bb) = PREV_INSN (last);
2105     }
2106
2107   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2108
2109   return bb;
2110 }
2111
2112 /* Return the difference between the floor and the truncated result of
2113    a signed division by OP1 with remainder MOD.  */
2114 static rtx
2115 floor_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2116 {
2117   /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
2118   return gen_rtx_IF_THEN_ELSE
2119     (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2120      gen_rtx_IF_THEN_ELSE
2121      (mode, gen_rtx_LT (BImode,
2122                         gen_rtx_DIV (mode, op1, mod),
2123                         const0_rtx),
2124       constm1_rtx, const0_rtx),
2125      const0_rtx);
2126 }
2127
2128 /* Return the difference between the ceil and the truncated result of
2129    a signed division by OP1 with remainder MOD.  */
2130 static rtx
2131 ceil_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2132 {
2133   /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
2134   return gen_rtx_IF_THEN_ELSE
2135     (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2136      gen_rtx_IF_THEN_ELSE
2137      (mode, gen_rtx_GT (BImode,
2138                         gen_rtx_DIV (mode, op1, mod),
2139                         const0_rtx),
2140       const1_rtx, const0_rtx),
2141      const0_rtx);
2142 }
2143
2144 /* Return the difference between the ceil and the truncated result of
2145    an unsigned division by OP1 with remainder MOD.  */
2146 static rtx
2147 ceil_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
2148 {
2149   /* (mod != 0 ? 1 : 0) */
2150   return gen_rtx_IF_THEN_ELSE
2151     (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2152      const1_rtx, const0_rtx);
2153 }
2154
2155 /* Return the difference between the rounded and the truncated result
2156    of a signed division by OP1 with remainder MOD.  Halfway cases are
2157    rounded away from zero, rather than to the nearest even number.  */
2158 static rtx
2159 round_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2160 {
2161   /* (abs (mod) >= abs (op1) - abs (mod)
2162       ? (op1 / mod > 0 ? 1 : -1)
2163       : 0) */
2164   return gen_rtx_IF_THEN_ELSE
2165     (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
2166                        gen_rtx_MINUS (mode,
2167                                       gen_rtx_ABS (mode, op1),
2168                                       gen_rtx_ABS (mode, mod))),
2169      gen_rtx_IF_THEN_ELSE
2170      (mode, gen_rtx_GT (BImode,
2171                         gen_rtx_DIV (mode, op1, mod),
2172                         const0_rtx),
2173       const1_rtx, constm1_rtx),
2174      const0_rtx);
2175 }
2176
2177 /* Return the difference between the rounded and the truncated result
2178    of a unsigned division by OP1 with remainder MOD.  Halfway cases
2179    are rounded away from zero, rather than to the nearest even
2180    number.  */
2181 static rtx
2182 round_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2183 {
2184   /* (mod >= op1 - mod ? 1 : 0) */
2185   return gen_rtx_IF_THEN_ELSE
2186     (mode, gen_rtx_GE (BImode, mod,
2187                        gen_rtx_MINUS (mode, op1, mod)),
2188      const1_rtx, const0_rtx);
2189 }
2190
2191 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
2192    any rtl.  */
2193
2194 static rtx
2195 convert_debug_memory_address (enum machine_mode mode, rtx x)
2196 {
2197   enum machine_mode xmode = GET_MODE (x);
2198
2199 #ifndef POINTERS_EXTEND_UNSIGNED
2200   gcc_assert (mode == Pmode);
2201   gcc_assert (xmode == mode || xmode == VOIDmode);
2202 #else
2203   gcc_assert (mode == Pmode || mode == ptr_mode);
2204
2205   if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
2206     return x;
2207
2208   if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (xmode))
2209     x = simplify_gen_subreg (mode, x, xmode,
2210                              subreg_lowpart_offset
2211                              (mode, xmode));
2212   else if (POINTERS_EXTEND_UNSIGNED > 0)
2213     x = gen_rtx_ZERO_EXTEND (mode, x);
2214   else if (!POINTERS_EXTEND_UNSIGNED)
2215     x = gen_rtx_SIGN_EXTEND (mode, x);
2216   else
2217     gcc_unreachable ();
2218 #endif /* POINTERS_EXTEND_UNSIGNED */
2219
2220   return x;
2221 }
2222
2223 /* Return an RTX equivalent to the value of the tree expression
2224    EXP.  */
2225
2226 static rtx
2227 expand_debug_expr (tree exp)
2228 {
2229   rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
2230   enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2231   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
2232   addr_space_t as;
2233   enum machine_mode address_mode;
2234   enum machine_mode pointer_mode;
2235
2236   switch (TREE_CODE_CLASS (TREE_CODE (exp)))
2237     {
2238     case tcc_expression:
2239       switch (TREE_CODE (exp))
2240         {
2241         case COND_EXPR:
2242           goto ternary;
2243
2244         case TRUTH_ANDIF_EXPR:
2245         case TRUTH_ORIF_EXPR:
2246         case TRUTH_AND_EXPR:
2247         case TRUTH_OR_EXPR:
2248         case TRUTH_XOR_EXPR:
2249           goto binary;
2250
2251         case TRUTH_NOT_EXPR:
2252           goto unary;
2253
2254         default:
2255           break;
2256         }
2257       break;
2258
2259     ternary:
2260       op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
2261       if (!op2)
2262         return NULL_RTX;
2263       /* Fall through.  */
2264
2265     binary:
2266     case tcc_binary:
2267     case tcc_comparison:
2268       op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
2269       if (!op1)
2270         return NULL_RTX;
2271       /* Fall through.  */
2272
2273     unary:
2274     case tcc_unary:
2275       op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2276       if (!op0)
2277         return NULL_RTX;
2278       break;
2279
2280     case tcc_type:
2281     case tcc_statement:
2282       gcc_unreachable ();
2283
2284     case tcc_constant:
2285     case tcc_exceptional:
2286     case tcc_declaration:
2287     case tcc_reference:
2288     case tcc_vl_exp:
2289       break;
2290     }
2291
2292   switch (TREE_CODE (exp))
2293     {
2294     case STRING_CST:
2295       if (!lookup_constant_def (exp))
2296         {
2297           if (strlen (TREE_STRING_POINTER (exp)) + 1
2298               != (size_t) TREE_STRING_LENGTH (exp))
2299             return NULL_RTX;
2300           op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
2301           op0 = gen_rtx_MEM (BLKmode, op0);
2302           set_mem_attributes (op0, exp, 0);
2303           return op0;
2304         }
2305       /* Fall through...  */
2306
2307     case INTEGER_CST:
2308     case REAL_CST:
2309     case FIXED_CST:
2310       op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
2311       return op0;
2312
2313     case COMPLEX_CST:
2314       gcc_assert (COMPLEX_MODE_P (mode));
2315       op0 = expand_debug_expr (TREE_REALPART (exp));
2316       op1 = expand_debug_expr (TREE_IMAGPART (exp));
2317       return gen_rtx_CONCAT (mode, op0, op1);
2318
2319     case DEBUG_EXPR_DECL:
2320       op0 = DECL_RTL_IF_SET (exp);
2321
2322       if (op0)
2323         return op0;
2324
2325       op0 = gen_rtx_DEBUG_EXPR (mode);
2326       DEBUG_EXPR_TREE_DECL (op0) = exp;
2327       SET_DECL_RTL (exp, op0);
2328
2329       return op0;
2330
2331     case VAR_DECL:
2332     case PARM_DECL:
2333     case FUNCTION_DECL:
2334     case LABEL_DECL:
2335     case CONST_DECL:
2336     case RESULT_DECL:
2337       op0 = DECL_RTL_IF_SET (exp);
2338
2339       /* This decl was probably optimized away.  */
2340       if (!op0)
2341         {
2342           if (TREE_CODE (exp) != VAR_DECL
2343               || DECL_EXTERNAL (exp)
2344               || !TREE_STATIC (exp)
2345               || !DECL_NAME (exp)
2346               || DECL_HARD_REGISTER (exp)
2347               || mode == VOIDmode)
2348             return NULL;
2349
2350           op0 = DECL_RTL (exp);
2351           SET_DECL_RTL (exp, NULL);
2352           if (!MEM_P (op0)
2353               || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
2354               || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
2355             return NULL;
2356         }
2357       else
2358         op0 = copy_rtx (op0);
2359
2360       if (GET_MODE (op0) == BLKmode)
2361         {
2362           gcc_assert (MEM_P (op0));
2363           op0 = adjust_address_nv (op0, mode, 0);
2364           return op0;
2365         }
2366
2367       /* Fall through.  */
2368
2369     adjust_mode:
2370     case PAREN_EXPR:
2371     case NOP_EXPR:
2372     case CONVERT_EXPR:
2373       {
2374         enum machine_mode inner_mode = GET_MODE (op0);
2375
2376         if (mode == inner_mode)
2377           return op0;
2378
2379         if (inner_mode == VOIDmode)
2380           {
2381             inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
2382             if (mode == inner_mode)
2383               return op0;
2384           }
2385
2386         if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
2387           {
2388             if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
2389               op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
2390             else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
2391               op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
2392             else
2393               op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
2394           }
2395         else if (FLOAT_MODE_P (mode))
2396           {
2397             if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
2398               op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
2399             else
2400               op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
2401           }
2402         else if (FLOAT_MODE_P (inner_mode))
2403           {
2404             if (unsignedp)
2405               op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
2406             else
2407               op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
2408           }
2409         else if (CONSTANT_P (op0)
2410                  || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
2411           op0 = simplify_gen_subreg (mode, op0, inner_mode,
2412                                      subreg_lowpart_offset (mode,
2413                                                             inner_mode));
2414         else if (unsignedp)
2415           op0 = gen_rtx_ZERO_EXTEND (mode, op0);
2416         else
2417           op0 = gen_rtx_SIGN_EXTEND (mode, op0);
2418
2419         return op0;
2420       }
2421
2422     case INDIRECT_REF:
2423     case ALIGN_INDIRECT_REF:
2424     case MISALIGNED_INDIRECT_REF:
2425       op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2426       if (!op0)
2427         return NULL;
2428
2429       if (POINTER_TYPE_P (TREE_TYPE (exp)))
2430         as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
2431       else
2432         as = ADDR_SPACE_GENERIC;
2433
2434       address_mode = targetm.addr_space.address_mode (as);
2435       pointer_mode = targetm.addr_space.pointer_mode (as);
2436
2437       gcc_assert (GET_MODE (op0) == address_mode
2438                   || GET_MODE (op0) == pointer_mode
2439                   || GET_CODE (op0) == CONST_INT
2440                   || GET_CODE (op0) == CONST_DOUBLE);
2441
2442       if (TREE_CODE (exp) == ALIGN_INDIRECT_REF)
2443         {
2444           int align = TYPE_ALIGN_UNIT (TREE_TYPE (exp));
2445           op0 = gen_rtx_AND (address_mode, op0, GEN_INT (-align));
2446         }
2447
2448       op0 = gen_rtx_MEM (mode, op0);
2449
2450       set_mem_attributes (op0, exp, 0);
2451       set_mem_addr_space (op0, as);
2452
2453       return op0;
2454
2455     case TARGET_MEM_REF:
2456       if (TMR_SYMBOL (exp) && !DECL_RTL_SET_P (TMR_SYMBOL (exp)))
2457         return NULL;
2458
2459       op0 = expand_debug_expr
2460         (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)),
2461                             exp));
2462       if (!op0)
2463         return NULL;
2464
2465       as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
2466       address_mode = targetm.addr_space.address_mode (as);
2467       pointer_mode = targetm.addr_space.pointer_mode (as);
2468
2469       gcc_assert (GET_MODE (op0) == address_mode
2470                   || GET_MODE (op0) == pointer_mode
2471                   || GET_CODE (op0) == CONST_INT
2472                   || GET_CODE (op0) == CONST_DOUBLE);
2473
2474       op0 = gen_rtx_MEM (mode, op0);
2475
2476       set_mem_attributes (op0, exp, 0);
2477       set_mem_addr_space (op0, as);
2478
2479       return op0;
2480
2481     case ARRAY_REF:
2482     case ARRAY_RANGE_REF:
2483     case COMPONENT_REF:
2484     case BIT_FIELD_REF:
2485     case REALPART_EXPR:
2486     case IMAGPART_EXPR:
2487     case VIEW_CONVERT_EXPR:
2488       {
2489         enum machine_mode mode1;
2490         HOST_WIDE_INT bitsize, bitpos;
2491         tree offset;
2492         int volatilep = 0;
2493         tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
2494                                         &mode1, &unsignedp, &volatilep, false);
2495         rtx orig_op0;
2496
2497         if (bitsize == 0)
2498           return NULL;
2499
2500         orig_op0 = op0 = expand_debug_expr (tem);
2501
2502         if (!op0)
2503           return NULL;
2504
2505         if (offset)
2506           {
2507             enum machine_mode addrmode, offmode;
2508
2509             gcc_assert (MEM_P (op0));
2510
2511             op0 = XEXP (op0, 0);
2512             addrmode = GET_MODE (op0);
2513             if (addrmode == VOIDmode)
2514               addrmode = Pmode;
2515
2516             op1 = expand_debug_expr (offset);
2517             if (!op1)
2518               return NULL;
2519
2520             offmode = GET_MODE (op1);
2521             if (offmode == VOIDmode)
2522               offmode = TYPE_MODE (TREE_TYPE (offset));
2523
2524             if (addrmode != offmode)
2525               op1 = simplify_gen_subreg (addrmode, op1, offmode,
2526                                          subreg_lowpart_offset (addrmode,
2527                                                                 offmode));
2528
2529             /* Don't use offset_address here, we don't need a
2530                recognizable address, and we don't want to generate
2531                code.  */
2532             op0 = gen_rtx_MEM (mode, gen_rtx_PLUS (addrmode, op0, op1));
2533           }
2534
2535         if (MEM_P (op0))
2536           {
2537             if (mode1 == VOIDmode)
2538               /* Bitfield.  */
2539               mode1 = smallest_mode_for_size (bitsize, MODE_INT);
2540             if (bitpos >= BITS_PER_UNIT)
2541               {
2542                 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
2543                 bitpos %= BITS_PER_UNIT;
2544               }
2545             else if (bitpos < 0)
2546               {
2547                 HOST_WIDE_INT units
2548                   = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
2549                 op0 = adjust_address_nv (op0, mode1, units);
2550                 bitpos += units * BITS_PER_UNIT;
2551               }
2552             else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
2553               op0 = adjust_address_nv (op0, mode, 0);
2554             else if (GET_MODE (op0) != mode1)
2555               op0 = adjust_address_nv (op0, mode1, 0);
2556             else
2557               op0 = copy_rtx (op0);
2558             if (op0 == orig_op0)
2559               op0 = shallow_copy_rtx (op0);
2560             set_mem_attributes (op0, exp, 0);
2561           }
2562
2563         if (bitpos == 0 && mode == GET_MODE (op0))
2564           return op0;
2565
2566         if (bitpos < 0)
2567           return NULL;
2568
2569         if ((bitpos % BITS_PER_UNIT) == 0
2570             && bitsize == GET_MODE_BITSIZE (mode1))
2571           {
2572             enum machine_mode opmode = GET_MODE (op0);
2573
2574             gcc_assert (opmode != BLKmode);
2575
2576             if (opmode == VOIDmode)
2577               opmode = mode1;
2578
2579             /* This condition may hold if we're expanding the address
2580                right past the end of an array that turned out not to
2581                be addressable (i.e., the address was only computed in
2582                debug stmts).  The gen_subreg below would rightfully
2583                crash, and the address doesn't really exist, so just
2584                drop it.  */
2585             if (bitpos >= GET_MODE_BITSIZE (opmode))
2586               return NULL;
2587
2588             return simplify_gen_subreg (mode, op0, opmode,
2589                                         bitpos / BITS_PER_UNIT);
2590           }
2591
2592         return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
2593                                      && TYPE_UNSIGNED (TREE_TYPE (exp))
2594                                      ? SIGN_EXTRACT
2595                                      : ZERO_EXTRACT, mode,
2596                                      GET_MODE (op0) != VOIDmode
2597                                      ? GET_MODE (op0) : mode1,
2598                                      op0, GEN_INT (bitsize), GEN_INT (bitpos));
2599       }
2600
2601     case ABS_EXPR:
2602       return gen_rtx_ABS (mode, op0);
2603
2604     case NEGATE_EXPR:
2605       return gen_rtx_NEG (mode, op0);
2606
2607     case BIT_NOT_EXPR:
2608       return gen_rtx_NOT (mode, op0);
2609
2610     case FLOAT_EXPR:
2611       if (unsignedp)
2612         return gen_rtx_UNSIGNED_FLOAT (mode, op0);
2613       else
2614         return gen_rtx_FLOAT (mode, op0);
2615
2616     case FIX_TRUNC_EXPR:
2617       if (unsignedp)
2618         return gen_rtx_UNSIGNED_FIX (mode, op0);
2619       else
2620         return gen_rtx_FIX (mode, op0);
2621
2622     case POINTER_PLUS_EXPR:
2623     case PLUS_EXPR:
2624       return gen_rtx_PLUS (mode, op0, op1);
2625
2626     case MINUS_EXPR:
2627       return gen_rtx_MINUS (mode, op0, op1);
2628
2629     case MULT_EXPR:
2630       return gen_rtx_MULT (mode, op0, op1);
2631
2632     case RDIV_EXPR:
2633     case TRUNC_DIV_EXPR:
2634     case EXACT_DIV_EXPR:
2635       if (unsignedp)
2636         return gen_rtx_UDIV (mode, op0, op1);
2637       else
2638         return gen_rtx_DIV (mode, op0, op1);
2639
2640     case TRUNC_MOD_EXPR:
2641       if (unsignedp)
2642         return gen_rtx_UMOD (mode, op0, op1);
2643       else
2644         return gen_rtx_MOD (mode, op0, op1);
2645
2646     case FLOOR_DIV_EXPR:
2647       if (unsignedp)
2648         return gen_rtx_UDIV (mode, op0, op1);
2649       else
2650         {
2651           rtx div = gen_rtx_DIV (mode, op0, op1);
2652           rtx mod = gen_rtx_MOD (mode, op0, op1);
2653           rtx adj = floor_sdiv_adjust (mode, mod, op1);
2654           return gen_rtx_PLUS (mode, div, adj);
2655         }
2656
2657     case FLOOR_MOD_EXPR:
2658       if (unsignedp)
2659         return gen_rtx_UMOD (mode, op0, op1);
2660       else
2661         {
2662           rtx mod = gen_rtx_MOD (mode, op0, op1);
2663           rtx adj = floor_sdiv_adjust (mode, mod, op1);
2664           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2665           return gen_rtx_PLUS (mode, mod, adj);
2666         }
2667
2668     case CEIL_DIV_EXPR:
2669       if (unsignedp)
2670         {
2671           rtx div = gen_rtx_UDIV (mode, op0, op1);
2672           rtx mod = gen_rtx_UMOD (mode, op0, op1);
2673           rtx adj = ceil_udiv_adjust (mode, mod, op1);
2674           return gen_rtx_PLUS (mode, div, adj);
2675         }
2676       else
2677         {
2678           rtx div = gen_rtx_DIV (mode, op0, op1);
2679           rtx mod = gen_rtx_MOD (mode, op0, op1);
2680           rtx adj = ceil_sdiv_adjust (mode, mod, op1);
2681           return gen_rtx_PLUS (mode, div, adj);
2682         }
2683
2684     case CEIL_MOD_EXPR:
2685       if (unsignedp)
2686         {
2687           rtx mod = gen_rtx_UMOD (mode, op0, op1);
2688           rtx adj = ceil_udiv_adjust (mode, mod, op1);
2689           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2690           return gen_rtx_PLUS (mode, mod, adj);
2691         }
2692       else
2693         {
2694           rtx mod = gen_rtx_MOD (mode, op0, op1);
2695           rtx adj = ceil_sdiv_adjust (mode, mod, op1);
2696           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2697           return gen_rtx_PLUS (mode, mod, adj);
2698         }
2699
2700     case ROUND_DIV_EXPR:
2701       if (unsignedp)
2702         {
2703           rtx div = gen_rtx_UDIV (mode, op0, op1);
2704           rtx mod = gen_rtx_UMOD (mode, op0, op1);
2705           rtx adj = round_udiv_adjust (mode, mod, op1);
2706           return gen_rtx_PLUS (mode, div, adj);
2707         }
2708       else
2709         {
2710           rtx div = gen_rtx_DIV (mode, op0, op1);
2711           rtx mod = gen_rtx_MOD (mode, op0, op1);
2712           rtx adj = round_sdiv_adjust (mode, mod, op1);
2713           return gen_rtx_PLUS (mode, div, adj);
2714         }
2715
2716     case ROUND_MOD_EXPR:
2717       if (unsignedp)
2718         {
2719           rtx mod = gen_rtx_UMOD (mode, op0, op1);
2720           rtx adj = round_udiv_adjust (mode, mod, op1);
2721           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2722           return gen_rtx_PLUS (mode, mod, adj);
2723         }
2724       else
2725         {
2726           rtx mod = gen_rtx_MOD (mode, op0, op1);
2727           rtx adj = round_sdiv_adjust (mode, mod, op1);
2728           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2729           return gen_rtx_PLUS (mode, mod, adj);
2730         }
2731
2732     case LSHIFT_EXPR:
2733       return gen_rtx_ASHIFT (mode, op0, op1);
2734
2735     case RSHIFT_EXPR:
2736       if (unsignedp)
2737         return gen_rtx_LSHIFTRT (mode, op0, op1);
2738       else
2739         return gen_rtx_ASHIFTRT (mode, op0, op1);
2740
2741     case LROTATE_EXPR:
2742       return gen_rtx_ROTATE (mode, op0, op1);
2743
2744     case RROTATE_EXPR:
2745       return gen_rtx_ROTATERT (mode, op0, op1);
2746
2747     case MIN_EXPR:
2748       if (unsignedp)
2749         return gen_rtx_UMIN (mode, op0, op1);
2750       else
2751         return gen_rtx_SMIN (mode, op0, op1);
2752
2753     case MAX_EXPR:
2754       if (unsignedp)
2755         return gen_rtx_UMAX (mode, op0, op1);
2756       else
2757         return gen_rtx_SMAX (mode, op0, op1);
2758
2759     case BIT_AND_EXPR:
2760     case TRUTH_AND_EXPR:
2761       return gen_rtx_AND (mode, op0, op1);
2762
2763     case BIT_IOR_EXPR:
2764     case TRUTH_OR_EXPR:
2765       return gen_rtx_IOR (mode, op0, op1);
2766
2767     case BIT_XOR_EXPR:
2768     case TRUTH_XOR_EXPR:
2769       return gen_rtx_XOR (mode, op0, op1);
2770
2771     case TRUTH_ANDIF_EXPR:
2772       return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
2773
2774     case TRUTH_ORIF_EXPR:
2775       return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
2776
2777     case TRUTH_NOT_EXPR:
2778       return gen_rtx_EQ (mode, op0, const0_rtx);
2779
2780     case LT_EXPR:
2781       if (unsignedp)
2782         return gen_rtx_LTU (mode, op0, op1);
2783       else
2784         return gen_rtx_LT (mode, op0, op1);
2785
2786     case LE_EXPR:
2787       if (unsignedp)
2788         return gen_rtx_LEU (mode, op0, op1);
2789       else
2790         return gen_rtx_LE (mode, op0, op1);
2791
2792     case GT_EXPR:
2793       if (unsignedp)
2794         return gen_rtx_GTU (mode, op0, op1);
2795       else
2796         return gen_rtx_GT (mode, op0, op1);
2797
2798     case GE_EXPR:
2799       if (unsignedp)
2800         return gen_rtx_GEU (mode, op0, op1);
2801       else
2802         return gen_rtx_GE (mode, op0, op1);
2803
2804     case EQ_EXPR:
2805       return gen_rtx_EQ (mode, op0, op1);
2806
2807     case NE_EXPR:
2808       return gen_rtx_NE (mode, op0, op1);
2809
2810     case UNORDERED_EXPR:
2811       return gen_rtx_UNORDERED (mode, op0, op1);
2812
2813     case ORDERED_EXPR:
2814       return gen_rtx_ORDERED (mode, op0, op1);
2815
2816     case UNLT_EXPR:
2817       return gen_rtx_UNLT (mode, op0, op1);
2818
2819     case UNLE_EXPR:
2820       return gen_rtx_UNLE (mode, op0, op1);
2821
2822     case UNGT_EXPR:
2823       return gen_rtx_UNGT (mode, op0, op1);
2824
2825     case UNGE_EXPR:
2826       return gen_rtx_UNGE (mode, op0, op1);
2827
2828     case UNEQ_EXPR:
2829       return gen_rtx_UNEQ (mode, op0, op1);
2830
2831     case LTGT_EXPR:
2832       return gen_rtx_LTGT (mode, op0, op1);
2833
2834     case COND_EXPR:
2835       return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
2836
2837     case COMPLEX_EXPR:
2838       gcc_assert (COMPLEX_MODE_P (mode));
2839       if (GET_MODE (op0) == VOIDmode)
2840         op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
2841       if (GET_MODE (op1) == VOIDmode)
2842         op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
2843       return gen_rtx_CONCAT (mode, op0, op1);
2844
2845     case CONJ_EXPR:
2846       if (GET_CODE (op0) == CONCAT)
2847         return gen_rtx_CONCAT (mode, XEXP (op0, 0),
2848                                gen_rtx_NEG (GET_MODE_INNER (mode),
2849                                             XEXP (op0, 1)));
2850       else
2851         {
2852           enum machine_mode imode = GET_MODE_INNER (mode);
2853           rtx re, im;
2854
2855           if (MEM_P (op0))
2856             {
2857               re = adjust_address_nv (op0, imode, 0);
2858               im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
2859             }
2860           else
2861             {
2862               enum machine_mode ifmode = int_mode_for_mode (mode);
2863               enum machine_mode ihmode = int_mode_for_mode (imode);
2864               rtx halfsize;
2865               if (ifmode == BLKmode || ihmode == BLKmode)
2866                 return NULL;
2867               halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
2868               re = op0;
2869               if (mode != ifmode)
2870                 re = gen_rtx_SUBREG (ifmode, re, 0);
2871               re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
2872               if (imode != ihmode)
2873                 re = gen_rtx_SUBREG (imode, re, 0);
2874               im = copy_rtx (op0);
2875               if (mode != ifmode)
2876                 im = gen_rtx_SUBREG (ifmode, im, 0);
2877               im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
2878               if (imode != ihmode)
2879                 im = gen_rtx_SUBREG (imode, im, 0);
2880             }
2881           im = gen_rtx_NEG (imode, im);
2882           return gen_rtx_CONCAT (mode, re, im);
2883         }
2884
2885     case ADDR_EXPR:
2886       op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2887       if (!op0 || !MEM_P (op0))
2888         return NULL;
2889
2890       op0 = convert_debug_memory_address (mode, XEXP (op0, 0));
2891
2892       return op0;
2893
2894     case VECTOR_CST:
2895       exp = build_constructor_from_list (TREE_TYPE (exp),
2896                                          TREE_VECTOR_CST_ELTS (exp));
2897       /* Fall through.  */
2898
2899     case CONSTRUCTOR:
2900       if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
2901         {
2902           unsigned i;
2903           tree val;
2904
2905           op0 = gen_rtx_CONCATN
2906             (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
2907
2908           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
2909             {
2910               op1 = expand_debug_expr (val);
2911               if (!op1)
2912                 return NULL;
2913               XVECEXP (op0, 0, i) = op1;
2914             }
2915
2916           if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
2917             {
2918               op1 = expand_debug_expr
2919                 (fold_convert (TREE_TYPE (TREE_TYPE (exp)), integer_zero_node));
2920
2921               if (!op1)
2922                 return NULL;
2923
2924               for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
2925                 XVECEXP (op0, 0, i) = op1;
2926             }
2927
2928           return op0;
2929         }
2930       else
2931         goto flag_unsupported;
2932
2933     case CALL_EXPR:
2934       /* ??? Maybe handle some builtins?  */
2935       return NULL;
2936
2937     case SSA_NAME:
2938       {
2939         int part = var_to_partition (SA.map, exp);
2940
2941         if (part == NO_PARTITION)
2942           return NULL;
2943
2944         gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
2945
2946         op0 = SA.partition_to_pseudo[part];
2947         goto adjust_mode;
2948       }
2949
2950     case ERROR_MARK:
2951       return NULL;
2952
2953     default:
2954     flag_unsupported:
2955 #ifdef ENABLE_CHECKING
2956       debug_tree (exp);
2957       gcc_unreachable ();
2958 #else
2959       return NULL;
2960 #endif
2961     }
2962 }
2963
2964 /* Expand the _LOCs in debug insns.  We run this after expanding all
2965    regular insns, so that any variables referenced in the function
2966    will have their DECL_RTLs set.  */
2967
2968 static void
2969 expand_debug_locations (void)
2970 {
2971   rtx insn;
2972   rtx last = get_last_insn ();
2973   int save_strict_alias = flag_strict_aliasing;
2974
2975   /* New alias sets while setting up memory attributes cause
2976      -fcompare-debug failures, even though it doesn't bring about any
2977      codegen changes.  */
2978   flag_strict_aliasing = 0;
2979
2980   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2981     if (DEBUG_INSN_P (insn))
2982       {
2983         tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
2984         rtx val;
2985         enum machine_mode mode;
2986
2987         if (value == NULL_TREE)
2988           val = NULL_RTX;
2989         else
2990           {
2991             val = expand_debug_expr (value);
2992             gcc_assert (last == get_last_insn ());
2993           }
2994
2995         if (!val)
2996           val = gen_rtx_UNKNOWN_VAR_LOC ();
2997         else
2998           {
2999             mode = GET_MODE (INSN_VAR_LOCATION (insn));
3000
3001             gcc_assert (mode == GET_MODE (val)
3002                         || (GET_MODE (val) == VOIDmode
3003                             && (CONST_INT_P (val)
3004                                 || GET_CODE (val) == CONST_FIXED
3005                                 || GET_CODE (val) == CONST_DOUBLE
3006                                 || GET_CODE (val) == LABEL_REF)));
3007           }
3008
3009         INSN_VAR_LOCATION_LOC (insn) = val;
3010       }
3011
3012   flag_strict_aliasing = save_strict_alias;
3013 }
3014
3015 /* Expand basic block BB from GIMPLE trees to RTL.  */
3016
3017 static basic_block
3018 expand_gimple_basic_block (basic_block bb)
3019 {
3020   gimple_stmt_iterator gsi;
3021   gimple_seq stmts;
3022   gimple stmt = NULL;
3023   rtx note, last;
3024   edge e;
3025   edge_iterator ei;
3026   void **elt;
3027
3028   if (dump_file)
3029     fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
3030              bb->index);
3031
3032   /* Note that since we are now transitioning from GIMPLE to RTL, we
3033      cannot use the gsi_*_bb() routines because they expect the basic
3034      block to be in GIMPLE, instead of RTL.  Therefore, we need to
3035      access the BB sequence directly.  */
3036   stmts = bb_seq (bb);
3037   bb->il.gimple = NULL;
3038   rtl_profile_for_bb (bb);
3039   init_rtl_bb_info (bb);
3040   bb->flags |= BB_RTL;
3041
3042   /* Remove the RETURN_EXPR if we may fall though to the exit
3043      instead.  */
3044   gsi = gsi_last (stmts);
3045   if (!gsi_end_p (gsi)
3046       && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
3047     {
3048       gimple ret_stmt = gsi_stmt (gsi);
3049
3050       gcc_assert (single_succ_p (bb));
3051       gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
3052
3053       if (bb->next_bb == EXIT_BLOCK_PTR
3054           && !gimple_return_retval (ret_stmt))
3055         {
3056           gsi_remove (&gsi, false);
3057           single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
3058         }
3059     }
3060
3061   gsi = gsi_start (stmts);
3062   if (!gsi_end_p (gsi))
3063     {
3064       stmt = gsi_stmt (gsi);
3065       if (gimple_code (stmt) != GIMPLE_LABEL)
3066         stmt = NULL;
3067     }
3068
3069   elt = pointer_map_contains (lab_rtx_for_bb, bb);
3070
3071   if (stmt || elt)
3072     {
3073       last = get_last_insn ();
3074
3075       if (stmt)
3076         {
3077           expand_gimple_stmt (stmt);
3078           gsi_next (&gsi);
3079         }
3080
3081       if (elt)
3082         emit_label ((rtx) *elt);
3083
3084       /* Java emits line number notes in the top of labels.
3085          ??? Make this go away once line number notes are obsoleted.  */
3086       BB_HEAD (bb) = NEXT_INSN (last);
3087       if (NOTE_P (BB_HEAD (bb)))
3088         BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
3089       note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
3090
3091       maybe_dump_rtl_for_gimple_stmt (stmt, last);
3092     }
3093   else
3094     note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
3095
3096   NOTE_BASIC_BLOCK (note) = bb;
3097
3098   for (; !gsi_end_p (gsi); gsi_next (&gsi))
3099     {
3100       basic_block new_bb;
3101
3102       stmt = gsi_stmt (gsi);
3103       currently_expanding_gimple_stmt = stmt;
3104
3105       /* Expand this statement, then evaluate the resulting RTL and
3106          fixup the CFG accordingly.  */
3107       if (gimple_code (stmt) == GIMPLE_COND)
3108         {
3109           new_bb = expand_gimple_cond (bb, stmt);
3110           if (new_bb)
3111             return new_bb;
3112         }
3113       else if (gimple_debug_bind_p (stmt))
3114         {
3115           location_t sloc = get_curr_insn_source_location ();
3116           tree sblock = get_curr_insn_block ();
3117           gimple_stmt_iterator nsi = gsi;
3118
3119           for (;;)
3120             {
3121               tree var = gimple_debug_bind_get_var (stmt);
3122               tree value;
3123               rtx val;
3124               enum machine_mode mode;
3125
3126               if (gimple_debug_bind_has_value_p (stmt))
3127                 value = gimple_debug_bind_get_value (stmt);
3128               else
3129                 value = NULL_TREE;
3130
3131               last = get_last_insn ();
3132
3133               set_curr_insn_source_location (gimple_location (stmt));
3134               set_curr_insn_block (gimple_block (stmt));
3135
3136               if (DECL_P (var))
3137                 mode = DECL_MODE (var);
3138               else
3139                 mode = TYPE_MODE (TREE_TYPE (var));
3140
3141               val = gen_rtx_VAR_LOCATION
3142                 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3143
3144               val = emit_debug_insn (val);
3145
3146               if (dump_file && (dump_flags & TDF_DETAILS))
3147                 {
3148                   /* We can't dump the insn with a TREE where an RTX
3149                      is expected.  */
3150                   INSN_VAR_LOCATION_LOC (val) = const0_rtx;
3151                   maybe_dump_rtl_for_gimple_stmt (stmt, last);
3152                   INSN_VAR_LOCATION_LOC (val) = (rtx)value;
3153                 }
3154
3155               gsi = nsi;
3156               gsi_next (&nsi);
3157               if (gsi_end_p (nsi))
3158                 break;
3159               stmt = gsi_stmt (nsi);
3160               if (!gimple_debug_bind_p (stmt))
3161                 break;
3162             }
3163
3164           set_curr_insn_source_location (sloc);
3165           set_curr_insn_block (sblock);
3166         }
3167       else
3168         {
3169           if (is_gimple_call (stmt) && gimple_call_tail_p (stmt))
3170             {
3171               bool can_fallthru;
3172               new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
3173               if (new_bb)
3174                 {
3175                   if (can_fallthru)
3176                     bb = new_bb;
3177                   else
3178                     return new_bb;
3179                 }
3180             }
3181           else
3182             {
3183               def_operand_p def_p;
3184               def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
3185
3186               if (def_p != NULL)
3187                 {
3188                   /* Ignore this stmt if it is in the list of
3189                      replaceable expressions.  */
3190                   if (SA.values
3191                       && bitmap_bit_p (SA.values, 
3192                                        SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
3193                     continue;
3194                 }
3195               last = expand_gimple_stmt (stmt);
3196               maybe_dump_rtl_for_gimple_stmt (stmt, last);
3197             }
3198         }
3199     }
3200
3201   currently_expanding_gimple_stmt = NULL;
3202
3203   /* Expand implicit goto and convert goto_locus.  */
3204   FOR_EACH_EDGE (e, ei, bb->succs)
3205     {
3206       if (e->goto_locus && e->goto_block)
3207         {
3208           set_curr_insn_source_location (e->goto_locus);
3209           set_curr_insn_block (e->goto_block);
3210           e->goto_locus = curr_insn_locator ();
3211         }
3212       e->goto_block = NULL;
3213       if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
3214         {
3215           emit_jump (label_rtx_for_bb (e->dest));
3216           e->flags &= ~EDGE_FALLTHRU;
3217         }
3218     }
3219
3220   /* Expanded RTL can create a jump in the last instruction of block.
3221      This later might be assumed to be a jump to successor and break edge insertion.
3222      We need to insert dummy move to prevent this. PR41440. */
3223   if (single_succ_p (bb)
3224       && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
3225       && (last = get_last_insn ())
3226       && JUMP_P (last))
3227     {
3228       rtx dummy = gen_reg_rtx (SImode);
3229       emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
3230     }
3231
3232   do_pending_stack_adjust ();
3233
3234   /* Find the block tail.  The last insn in the block is the insn
3235      before a barrier and/or table jump insn.  */
3236   last = get_last_insn ();
3237   if (BARRIER_P (last))
3238     last = PREV_INSN (last);
3239   if (JUMP_TABLE_DATA_P (last))
3240     last = PREV_INSN (PREV_INSN (last));
3241   BB_END (bb) = last;
3242
3243   update_bb_for_insn (bb);
3244
3245   return bb;
3246 }
3247
3248
3249 /* Create a basic block for initialization code.  */
3250
3251 static basic_block
3252 construct_init_block (void)
3253 {
3254   basic_block init_block, first_block;
3255   edge e = NULL;
3256   int flags;
3257
3258   /* Multiple entry points not supported yet.  */
3259   gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
3260   init_rtl_bb_info (ENTRY_BLOCK_PTR);
3261   init_rtl_bb_info (EXIT_BLOCK_PTR);
3262   ENTRY_BLOCK_PTR->flags |= BB_RTL;
3263   EXIT_BLOCK_PTR->flags |= BB_RTL;
3264
3265   e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
3266
3267   /* When entry edge points to first basic block, we don't need jump,
3268      otherwise we have to jump into proper target.  */
3269   if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
3270     {
3271       tree label = gimple_block_label (e->dest);
3272
3273       emit_jump (label_rtx (label));
3274       flags = 0;
3275     }
3276   else
3277     flags = EDGE_FALLTHRU;
3278
3279   init_block = create_basic_block (NEXT_INSN (get_insns ()),
3280                                    get_last_insn (),
3281                                    ENTRY_BLOCK_PTR);
3282   init_block->frequency = ENTRY_BLOCK_PTR->frequency;
3283   init_block->count = ENTRY_BLOCK_PTR->count;
3284   if (e)
3285     {
3286       first_block = e->dest;
3287       redirect_edge_succ (e, init_block);
3288       e = make_edge (init_block, first_block, flags);
3289     }
3290   else
3291     e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
3292   e->probability = REG_BR_PROB_BASE;
3293   e->count = ENTRY_BLOCK_PTR->count;
3294
3295   update_bb_for_insn (init_block);
3296   return init_block;
3297 }
3298
3299 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
3300    found in the block tree.  */
3301
3302 static void
3303 set_block_levels (tree block, int level)
3304 {
3305   while (block)
3306     {
3307       BLOCK_NUMBER (block) = level;
3308       set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
3309       block = BLOCK_CHAIN (block);
3310     }
3311 }
3312
3313 /* Create a block containing landing pads and similar stuff.  */
3314
3315 static void
3316 construct_exit_block (void)
3317 {
3318   rtx head = get_last_insn ();
3319   rtx end;
3320   basic_block exit_block;
3321   edge e, e2;
3322   unsigned ix;
3323   edge_iterator ei;
3324   rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb);
3325
3326   rtl_profile_for_bb (EXIT_BLOCK_PTR);
3327
3328   /* Make sure the locus is set to the end of the function, so that
3329      epilogue line numbers and warnings are set properly.  */
3330   if (cfun->function_end_locus != UNKNOWN_LOCATION)
3331     input_location = cfun->function_end_locus;
3332
3333   /* The following insns belong to the top scope.  */
3334   set_curr_insn_block (DECL_INITIAL (current_function_decl));
3335
3336   /* Generate rtl for function exit.  */
3337   expand_function_end ();
3338
3339   end = get_last_insn ();
3340   if (head == end)
3341     return;
3342   /* While emitting the function end we could move end of the last basic block.
3343    */
3344   BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end;
3345   while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
3346     head = NEXT_INSN (head);
3347   exit_block = create_basic_block (NEXT_INSN (head), end,
3348                                    EXIT_BLOCK_PTR->prev_bb);
3349   exit_block->frequency = EXIT_BLOCK_PTR->frequency;
3350   exit_block->count = EXIT_BLOCK_PTR->count;
3351
3352   ix = 0;
3353   while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
3354     {
3355       e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
3356       if (!(e->flags & EDGE_ABNORMAL))
3357         redirect_edge_succ (e, exit_block);
3358       else
3359         ix++;
3360     }
3361
3362   e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
3363   e->probability = REG_BR_PROB_BASE;
3364   e->count = EXIT_BLOCK_PTR->count;
3365   FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
3366     if (e2 != e)
3367       {
3368         e->count -= e2->count;
3369         exit_block->count -= e2->count;
3370         exit_block->frequency -= EDGE_FREQUENCY (e2);
3371       }
3372   if (e->count < 0)
3373     e->count = 0;
3374   if (exit_block->count < 0)
3375     exit_block->count = 0;
3376   if (exit_block->frequency < 0)
3377     exit_block->frequency = 0;
3378   update_bb_for_insn (exit_block);
3379 }
3380
3381 /* Helper function for discover_nonconstant_array_refs.
3382    Look for ARRAY_REF nodes with non-constant indexes and mark them
3383    addressable.  */
3384
3385 static tree
3386 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
3387                                    void *data ATTRIBUTE_UNUSED)
3388 {
3389   tree t = *tp;
3390
3391   if (IS_TYPE_OR_DECL_P (t))
3392     *walk_subtrees = 0;
3393   else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3394     {
3395       while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3396               && is_gimple_min_invariant (TREE_OPERAND (t, 1))
3397               && (!TREE_OPERAND (t, 2)
3398                   || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
3399              || (TREE_CODE (t) == COMPONENT_REF
3400                  && (!TREE_OPERAND (t,2)
3401                      || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
3402              || TREE_CODE (t) == BIT_FIELD_REF
3403              || TREE_CODE (t) == REALPART_EXPR
3404              || TREE_CODE (t) == IMAGPART_EXPR
3405              || TREE_CODE (t) == VIEW_CONVERT_EXPR
3406              || CONVERT_EXPR_P (t))
3407         t = TREE_OPERAND (t, 0);
3408
3409       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3410         {
3411           t = get_base_address (t);
3412           if (t && DECL_P (t)
3413               && DECL_MODE (t) != BLKmode)
3414             TREE_ADDRESSABLE (t) = 1;
3415         }
3416
3417       *walk_subtrees = 0;
3418     }
3419
3420   return NULL_TREE;
3421 }
3422
3423 /* RTL expansion is not able to compile array references with variable
3424    offsets for arrays stored in single register.  Discover such
3425    expressions and mark variables as addressable to avoid this
3426    scenario.  */
3427
3428 static void
3429 discover_nonconstant_array_refs (void)
3430 {
3431   basic_block bb;
3432   gimple_stmt_iterator gsi;
3433
3434   FOR_EACH_BB (bb)
3435     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3436       {
3437         gimple stmt = gsi_stmt (gsi);
3438         walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
3439       }
3440 }
3441
3442 /* This function sets crtl->args.internal_arg_pointer to a virtual
3443    register if DRAP is needed.  Local register allocator will replace
3444    virtual_incoming_args_rtx with the virtual register.  */
3445
3446 static void
3447 expand_stack_alignment (void)
3448 {
3449   rtx drap_rtx;
3450   unsigned int preferred_stack_boundary;
3451
3452   if (! SUPPORTS_STACK_ALIGNMENT)
3453     return;
3454   
3455   if (cfun->calls_alloca
3456       || cfun->has_nonlocal_label
3457       || crtl->has_nonlocal_goto)
3458     crtl->need_drap = true;
3459
3460   /* Call update_stack_boundary here again to update incoming stack
3461      boundary.  It may set incoming stack alignment to a different
3462      value after RTL expansion.  TARGET_FUNCTION_OK_FOR_SIBCALL may
3463      use the minimum incoming stack alignment to check if it is OK
3464      to perform sibcall optimization since sibcall optimization will
3465      only align the outgoing stack to incoming stack boundary.  */
3466   if (targetm.calls.update_stack_boundary)
3467     targetm.calls.update_stack_boundary ();
3468
3469   /* The incoming stack frame has to be aligned at least at
3470      parm_stack_boundary.  */
3471   gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
3472
3473   /* Update crtl->stack_alignment_estimated and use it later to align
3474      stack.  We check PREFERRED_STACK_BOUNDARY if there may be non-call
3475      exceptions since callgraph doesn't collect incoming stack alignment
3476      in this case.  */
3477   if (flag_non_call_exceptions
3478       && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
3479     preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3480   else
3481     preferred_stack_boundary = crtl->preferred_stack_boundary;
3482   if (preferred_stack_boundary > crtl->stack_alignment_estimated)
3483     crtl->stack_alignment_estimated = preferred_stack_boundary;
3484   if (preferred_stack_boundary > crtl->stack_alignment_needed)
3485     crtl->stack_alignment_needed = preferred_stack_boundary;
3486
3487   gcc_assert (crtl->stack_alignment_needed
3488               <= crtl->stack_alignment_estimated);
3489
3490   crtl->stack_realign_needed
3491     = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
3492   crtl->stack_realign_tried = crtl->stack_realign_needed;
3493
3494   crtl->stack_realign_processed = true;
3495
3496   /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
3497      alignment.  */
3498   gcc_assert (targetm.calls.get_drap_rtx != NULL);
3499   drap_rtx = targetm.calls.get_drap_rtx (); 
3500
3501   /* stack_realign_drap and drap_rtx must match.  */
3502   gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
3503
3504   /* Do nothing if NULL is returned, which means DRAP is not needed.  */
3505   if (NULL != drap_rtx)
3506     {
3507       crtl->args.internal_arg_pointer = drap_rtx;
3508
3509       /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
3510          needed. */
3511       fixup_tail_calls ();
3512     }
3513 }
3514
3515 /* Translate the intermediate representation contained in the CFG
3516    from GIMPLE trees to RTL.
3517
3518    We do conversion per basic block and preserve/update the tree CFG.
3519    This implies we have to do some magic as the CFG can simultaneously
3520    consist of basic blocks containing RTL and GIMPLE trees.  This can
3521    confuse the CFG hooks, so be careful to not manipulate CFG during
3522    the expansion.  */
3523
3524 static unsigned int
3525 gimple_expand_cfg (void)
3526 {
3527   basic_block bb, init_block;
3528   sbitmap blocks;
3529   edge_iterator ei;
3530   edge e;
3531   unsigned i;
3532
3533   rewrite_out_of_ssa (&SA);
3534   SA.partition_to_pseudo = (rtx *)xcalloc (SA.map->num_partitions,
3535                                            sizeof (rtx));
3536
3537   /* Some backends want to know that we are expanding to RTL.  */
3538   currently_expanding_to_rtl = 1;
3539
3540   rtl_profile_for_bb (ENTRY_BLOCK_PTR);
3541
3542   insn_locators_alloc ();
3543   if (!DECL_IS_BUILTIN (current_function_decl))
3544     {
3545       /* Eventually, all FEs should explicitly set function_start_locus.  */
3546       if (cfun->function_start_locus == UNKNOWN_LOCATION)
3547        set_curr_insn_source_location
3548          (DECL_SOURCE_LOCATION (current_function_decl));
3549       else
3550        set_curr_insn_source_location (cfun->function_start_locus);
3551     }
3552   set_curr_insn_block (DECL_INITIAL (current_function_decl));
3553   prologue_locator = curr_insn_locator ();
3554
3555   /* Make sure first insn is a note even if we don't want linenums.
3556      This makes sure the first insn will never be deleted.
3557      Also, final expects a note to appear there.  */
3558   emit_note (NOTE_INSN_DELETED);
3559
3560   /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE.  */
3561   discover_nonconstant_array_refs ();
3562
3563   targetm.expand_to_rtl_hook ();
3564   crtl->stack_alignment_needed = STACK_BOUNDARY;
3565   crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
3566   crtl->stack_alignment_estimated = 0;
3567   crtl->preferred_stack_boundary = STACK_BOUNDARY;
3568   cfun->cfg->max_jumptable_ents = 0;
3569
3570
3571   /* Expand the variables recorded during gimple lowering.  */
3572   expand_used_vars ();
3573
3574   /* Honor stack protection warnings.  */
3575   if (warn_stack_protect)
3576     {
3577       if (cfun->calls_alloca)
3578         warning (OPT_Wstack_protector, 
3579                  "not protecting local variables: variable length buffer");
3580       if (has_short_buffer && !crtl->stack_protect_guard)
3581         warning (OPT_Wstack_protector, 
3582                  "not protecting function: no buffer at least %d bytes long",
3583                  (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
3584     }
3585
3586   /* Set up parameters and prepare for return, for the function.  */
3587   expand_function_start (current_function_decl);
3588
3589   /* Now that we also have the parameter RTXs, copy them over to our
3590      partitions.  */
3591   for (i = 0; i < SA.map->num_partitions; i++)
3592     {
3593       tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
3594
3595       if (TREE_CODE (var) != VAR_DECL
3596           && !SA.partition_to_pseudo[i])
3597         SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
3598       gcc_assert (SA.partition_to_pseudo[i]);
3599
3600       /* If this decl was marked as living in multiple places, reset
3601          this now to NULL.  */
3602       if (DECL_RTL_IF_SET (var) == pc_rtx)
3603         SET_DECL_RTL (var, NULL);
3604
3605       /* Some RTL parts really want to look at DECL_RTL(x) when x
3606          was a decl marked in REG_ATTR or MEM_ATTR.  We could use
3607          SET_DECL_RTL here making this available, but that would mean
3608          to select one of the potentially many RTLs for one DECL.  Instead
3609          of doing that we simply reset the MEM_EXPR of the RTL in question,
3610          then nobody can get at it and hence nobody can call DECL_RTL on it.  */
3611       if (!DECL_RTL_SET_P (var))
3612         {
3613           if (MEM_P (SA.partition_to_pseudo[i]))
3614             set_mem_expr (SA.partition_to_pseudo[i], NULL);
3615         }
3616     }
3617
3618   /* If this function is `main', emit a call to `__main'
3619      to run global initializers, etc.  */
3620   if (DECL_NAME (current_function_decl)
3621       && MAIN_NAME_P (DECL_NAME (current_function_decl))
3622       && DECL_FILE_SCOPE_P (current_function_decl))
3623     expand_main_function ();
3624
3625   /* Initialize the stack_protect_guard field.  This must happen after the
3626      call to __main (if any) so that the external decl is initialized.  */
3627   if (crtl->stack_protect_guard)
3628     stack_protect_prologue ();
3629
3630   expand_phi_nodes (&SA);
3631
3632   /* Register rtl specific functions for cfg.  */
3633   rtl_register_cfg_hooks ();
3634
3635   init_block = construct_init_block ();
3636
3637   /* Clear EDGE_EXECUTABLE on the entry edge(s).  It is cleaned from the
3638      remaining edges later.  */
3639   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3640     e->flags &= ~EDGE_EXECUTABLE;
3641
3642   lab_rtx_for_bb = pointer_map_create ();
3643   FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
3644     bb = expand_gimple_basic_block (bb);
3645
3646   if (MAY_HAVE_DEBUG_INSNS)
3647     expand_debug_locations ();
3648
3649   execute_free_datastructures ();
3650   finish_out_of_ssa (&SA);
3651
3652   /* Expansion is used by optimization passes too, set maybe_hot_insn_p
3653      conservatively to true until they are all profile aware.  */
3654   pointer_map_destroy (lab_rtx_for_bb);
3655   free_histograms ();
3656
3657   construct_exit_block ();
3658   set_curr_insn_block (DECL_INITIAL (current_function_decl));
3659   insn_locators_finalize ();
3660
3661   /* Zap the tree EH table.  */
3662   set_eh_throw_stmt_table (cfun, NULL);
3663
3664   rebuild_jump_labels (get_insns ());
3665
3666   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
3667     {
3668       edge e;
3669       edge_iterator ei;
3670       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3671         {
3672           if (e->insns.r)
3673             commit_one_edge_insertion (e);
3674           else
3675             ei_next (&ei);
3676         }
3677     }
3678
3679   /* We're done expanding trees to RTL.  */
3680   currently_expanding_to_rtl = 0;
3681
3682   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR, next_bb)
3683     {
3684       edge e;
3685       edge_iterator ei;
3686       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3687         {
3688           /* Clear EDGE_EXECUTABLE.  This flag is never used in the backend.  */
3689           e->flags &= ~EDGE_EXECUTABLE;
3690
3691           /* At the moment not all abnormal edges match the RTL
3692              representation.  It is safe to remove them here as
3693              find_many_sub_basic_blocks will rediscover them.
3694              In the future we should get this fixed properly.  */
3695           if ((e->flags & EDGE_ABNORMAL)
3696               && !(e->flags & EDGE_SIBCALL))
3697             remove_edge (e);
3698           else
3699             ei_next (&ei);
3700         }
3701     }
3702
3703   blocks = sbitmap_alloc (last_basic_block);
3704   sbitmap_ones (blocks);
3705   find_many_sub_basic_blocks (blocks);
3706   sbitmap_free (blocks);
3707   purge_all_dead_edges ();
3708
3709   compact_blocks ();
3710
3711   expand_stack_alignment ();
3712
3713 #ifdef ENABLE_CHECKING
3714   verify_flow_info ();
3715 #endif
3716
3717   /* There's no need to defer outputting this function any more; we
3718      know we want to output it.  */
3719   DECL_DEFER_OUTPUT (current_function_decl) = 0;
3720
3721   /* Now that we're done expanding trees to RTL, we shouldn't have any
3722      more CONCATs anywhere.  */
3723   generating_concat_p = 0;
3724
3725   if (dump_file)
3726     {
3727       fprintf (dump_file,
3728                "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
3729       /* And the pass manager will dump RTL for us.  */
3730     }
3731
3732   /* If we're emitting a nested function, make sure its parent gets
3733      emitted as well.  Doing otherwise confuses debug info.  */
3734   {
3735     tree parent;
3736     for (parent = DECL_CONTEXT (current_function_decl);
3737          parent != NULL_TREE;
3738          parent = get_containing_scope (parent))
3739       if (TREE_CODE (parent) == FUNCTION_DECL)
3740         TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
3741   }
3742
3743   /* We are now committed to emitting code for this function.  Do any
3744      preparation, such as emitting abstract debug info for the inline
3745      before it gets mangled by optimization.  */
3746   if (cgraph_function_possibly_inlined_p (current_function_decl))
3747     (*debug_hooks->outlining_inline_function) (current_function_decl);
3748
3749   TREE_ASM_WRITTEN (current_function_decl) = 1;
3750
3751   /* After expanding, the return labels are no longer needed. */
3752   return_label = NULL;
3753   naked_return_label = NULL;
3754   /* Tag the blocks with a depth number so that change_scope can find
3755      the common parent easily.  */
3756   set_block_levels (DECL_INITIAL (cfun->decl), 0);
3757   default_rtl_profile ();
3758   return 0;
3759 }
3760
3761 struct rtl_opt_pass pass_expand =
3762 {
3763  {
3764   RTL_PASS,
3765   "expand",                             /* name */
3766   NULL,                                 /* gate */
3767   gimple_expand_cfg,                    /* execute */
3768   NULL,                                 /* sub */
3769   NULL,                                 /* next */
3770   0,                                    /* static_pass_number */
3771   TV_EXPAND,                            /* tv_id */
3772   PROP_ssa | PROP_gimple_leh | PROP_cfg,/* properties_required */
3773   PROP_rtl,                             /* properties_provided */
3774   PROP_ssa | PROP_trees,                /* properties_destroyed */
3775   TODO_verify_ssa | TODO_verify_flow
3776     | TODO_verify_stmts,                /* todo_flags_start */
3777   TODO_dump_func
3778   | TODO_ggc_collect                    /* todo_flags_finish */
3779  }
3780 };