OSDN Git Service

CFG transparent RTL expansion:
[pf3gnuchains/gcc-fork.git] / gcc / tree-sra.c
1 /* Scalar Replacement of Aggregates (SRA) converts some structure
2    references into scalar references, exposing them to the scalar
3    optimizers.
4    Copyright (C) 2003, 2004 Free Software Foundation, Inc.
5    Contributed by Diego Novillo <dnovillo@redhat.com>
6
7 This file is part of GCC.
8    
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
12 later version.
13    
14 GCC is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18    
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "errors.h"
29 #include "ggc.h"
30 #include "tree.h"
31
32 /* These RTL headers are needed for basic-block.h.  */
33 #include "rtl.h"
34 #include "tm_p.h"
35 #include "hard-reg-set.h"
36 #include "basic-block.h"
37 #include "diagnostic.h"
38 #include "langhooks.h"
39 #include "tree-inline.h"
40 #include "tree-flow.h"
41 #include "tree-gimple.h"
42 #include "tree-dump.h"
43 #include "tree-pass.h"
44 #include "timevar.h"
45 #include "flags.h"
46 #include "bitmap.h"
47
48
49 /* Maximum number of fields that a structure should have to be scalarized.
50    FIXME  This limit has been arbitrarily set to 5.  Experiment to find a
51           sensible setting.  */
52 #define MAX_NFIELDS_FOR_SRA     5
53
54 /* Codes indicating how to copy one structure into another.  */
55 enum sra_copy_mode { SCALAR_SCALAR, FIELD_SCALAR, SCALAR_FIELD };
56
57 /* Local functions.  */
58 static inline bool can_be_scalarized_p (tree);
59 static tree create_scalar_copies (tree lhs, tree rhs, enum sra_copy_mode mode);
60 static inline void scalarize_component_ref (tree, tree *tp);
61 static void scalarize_structures (void);
62 static void scalarize_stmt (block_stmt_iterator *);
63 static void scalarize_modify_expr (block_stmt_iterator *);
64 static void scalarize_call_expr (block_stmt_iterator *);
65 static void scalarize_asm_expr (block_stmt_iterator *);
66 static void scalarize_return_expr (block_stmt_iterator *);
67
68 /* The set of aggregate variables that are candidates for scalarization.  */
69 static bitmap sra_candidates;
70
71 /* Set of scalarizable PARM_DECLs that need copy-in operations at the
72    beginning of the function.  */
73 static bitmap needs_copy_in;
74
75 /* This structure holds the mapping between and element of an aggregate
76    and the scalar replacement variable.  */
77 struct sra_elt
78 {
79   enum tree_code kind;
80   tree base;
81   tree field;
82   tree replace;
83 };
84     
85 static htab_t sra_map;
86
87 static hashval_t
88 sra_elt_hash (const void *x)
89 {
90   const struct sra_elt *e = x;
91   hashval_t h = (size_t) e->base * e->kind;
92   if (e->kind == COMPONENT_REF)
93     h ^= (size_t) e->field;
94   return h;
95 }
96
97 static int
98 sra_elt_eq (const void *x, const void *y)
99 {
100   const struct sra_elt *a = x;
101   const struct sra_elt *b = y;
102
103   if (a->kind != b->kind)
104     return false;
105   if (a->base != b->base)
106     return false;
107   if (a->kind == COMPONENT_REF)
108     if (a->field != b->field)
109       return false;
110
111   return true;
112 }
113
114 /* Mark all the variables in V_MAY_DEF operands for STMT for renaming.
115    This becomes necessary when we modify all of a non-scalar.  */
116
117 static void
118 mark_all_v_may_defs (tree stmt)
119 {
120   v_may_def_optype v_may_defs;
121   size_t i, n;
122
123   get_stmt_operands (stmt);
124   v_may_defs = V_MAY_DEF_OPS (stmt_ann (stmt));
125   n = NUM_V_MAY_DEFS (v_may_defs);
126
127   for (i = 0; i < n; i++)
128     {
129       tree sym = V_MAY_DEF_RESULT (v_may_defs, i);
130       bitmap_set_bit (vars_to_rename, var_ann (sym)->uid);
131     }
132 }
133
134 /* Mark all the variables in V_MUST_DEF operands for STMT for renaming.
135    This becomes necessary when we modify all of a non-scalar.  */
136
137 static void
138 mark_all_v_must_defs (tree stmt)
139 {
140   v_must_def_optype v_must_defs;
141   size_t i, n;
142
143   get_stmt_operands (stmt);
144   v_must_defs = V_MUST_DEF_OPS (stmt_ann (stmt));
145   n = NUM_V_MUST_DEFS (v_must_defs);
146
147   for (i = 0; i < n; i++)
148     {
149       tree sym = V_MUST_DEF_OP (v_must_defs, i);
150       bitmap_set_bit (vars_to_rename, var_ann (sym)->uid);
151     }
152 }
153
154 /* Return true if DECL is an SRA candidate.  */
155
156 static bool
157 is_sra_candidate_decl (tree decl)
158 {
159   return DECL_P (decl) && bitmap_bit_p (sra_candidates, var_ann (decl)->uid);
160 }
161
162 /* Return true if EXP is of the form <ref decl>, where REF is one of the
163    field access references we handle and DECL is an SRA candidate. 
164
165    Set ALLOW_BIT_FIELD_REF to accept BIT_FIELD_REF as well.  This is
166    normally false, except when we're trying to work around it.  */
167
168 static bool
169 is_sra_candidate_ref (tree exp, bool allow_bit_field_ref)
170 {
171   switch (TREE_CODE (exp))
172     {
173     case BIT_FIELD_REF:
174       if (!allow_bit_field_ref)
175         break;
176       /* FALLTHRU */
177
178     case COMPONENT_REF:
179     case REALPART_EXPR:
180     case IMAGPART_EXPR:
181       return is_sra_candidate_decl (TREE_OPERAND (exp, 0));
182
183     default:
184       break;
185     }
186
187   return false;
188 }
189
190 /* Return the scalar in SRA_MAP[VAR_IX][FIELD_IX].  If none exists, create
191    a new scalar with type TYPE.  */
192
193 static tree
194 lookup_scalar (struct sra_elt *key, tree type)
195 {
196   struct sra_elt **slot, *res;
197
198   slot = (struct sra_elt **) htab_find_slot (sra_map, key, INSERT);
199   res = *slot;
200   if (!res)
201     {
202       res = xmalloc (sizeof (*res));
203       *slot = res;
204       *res = *key;
205       res->replace = make_rename_temp (type, "SR");
206
207       if (DECL_NAME (key->base) && !DECL_IGNORED_P (key->base))
208         {
209           char *name = NULL;
210           switch (key->kind)
211             {
212             case COMPONENT_REF:
213               if (!DECL_NAME (key->field))
214                 break;
215               name = concat (IDENTIFIER_POINTER (DECL_NAME (key->base)),
216                              "$",
217                              IDENTIFIER_POINTER (DECL_NAME (key->field)),
218                              NULL);
219               break;
220             case REALPART_EXPR:
221               name = concat (IDENTIFIER_POINTER (DECL_NAME (key->base)),
222                              "$real", NULL);
223               break;
224             case IMAGPART_EXPR:
225               name = concat (IDENTIFIER_POINTER (DECL_NAME (key->base)),
226                              "$imag", NULL);
227               break;
228             default:
229               abort ();
230             }
231           if (name)
232             {
233               DECL_NAME (res->replace) = get_identifier (name);
234               free (name);
235             }
236         }
237
238       DECL_SOURCE_LOCATION (res->replace) = DECL_SOURCE_LOCATION (key->base);
239       TREE_NO_WARNING (res->replace) = TREE_NO_WARNING (key->base);
240       DECL_ARTIFICIAL (res->replace) = DECL_ARTIFICIAL (key->base);
241     }
242
243   return res->replace;
244 }
245
246
247 /* Given a structure reference VAR.FIELD, return a scalar representing it.
248    If no scalar is found, a new one is created and added to the SRA_MAP
249    matrix.  */
250
251 static tree
252 get_scalar_for_field (tree var, tree field)
253 {
254   struct sra_elt key;
255
256 #ifdef ENABLE_CHECKING
257   /* Validate that FIELD actually exists in VAR's type.  */
258   {
259     tree f;
260     for (f = TYPE_FIELDS (TREE_TYPE (var)); f ; f = TREE_CHAIN (f))
261       if (f == field)
262         goto found;
263     abort ();
264    found:;
265   }
266 #endif
267
268   key.kind = COMPONENT_REF;
269   key.base = var;
270   key.field = field;
271
272   return lookup_scalar (&key, TREE_TYPE (field));
273 }
274
275
276 /* Similarly for the parts of a complex type.  */
277
278 static tree
279 get_scalar_for_complex_part (tree var, enum tree_code part)
280 {
281   struct sra_elt key;
282
283   key.kind = part;
284   key.base = var;
285
286   return lookup_scalar (&key, TREE_TYPE (TREE_TYPE (var)));
287 }
288
289 /* Return true if the fields of VAR can be replaced by scalar temporaries.
290    This only happens if VAR is not call-clobbered and it contains less
291    than MAX_NFIELDS_FOR_SRA scalar fields.  */
292
293 static inline bool
294 can_be_scalarized_p (tree var)
295 {
296   tree field, type;
297   int nfields;
298
299   if (!is_gimple_non_addressable (var))
300     {
301       if (dump_file && (dump_flags & TDF_DETAILS))
302         {
303           fprintf (dump_file, "Cannot scalarize variable ");
304           print_generic_expr (dump_file, var, dump_flags);
305           fprintf (dump_file, " because it must live in memory\n");
306         }
307       return false;
308     }
309
310   if (TREE_THIS_VOLATILE (var))
311     {
312       if (dump_file && (dump_flags & TDF_DETAILS))
313         {
314           fprintf (dump_file, "Cannot scalarize variable ");
315           print_generic_expr (dump_file, var, dump_flags);
316           fprintf (dump_file, " because it is declared volatile\n");
317         }
318       return false;
319     }
320
321   /* Any COMPLEX_TYPE that has reached this point can be scalarized.  */
322   if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE)
323     return true;
324
325   type = TREE_TYPE (var);
326   nfields = 0;
327   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
328     {
329       if (TREE_CODE (field) != FIELD_DECL)
330         continue;
331
332       /* FIXME: We really should recurse down the type hierarchy and
333          scalarize the fields at the leaves.  */
334       if (AGGREGATE_TYPE_P (TREE_TYPE (field)))
335         {
336           if (dump_file && (dump_flags & TDF_DETAILS))
337             {
338               fprintf (dump_file, "Cannot scalarize variable ");
339               print_generic_expr (dump_file, var, dump_flags);
340               fprintf (dump_file,
341                        " because it contains an aggregate type field, ");
342               print_generic_expr (dump_file, field, dump_flags);
343               fprintf (dump_file, "\n");
344             }
345           return false;
346         }
347
348       /* FIXME: Similarly.  Indeed, considering that we treat complex
349          as an aggregate, this is exactly the same problem.
350          Structures with __complex__ fields are tested in the libstdc++
351          testsuite: 26_numerics/complex_inserters_extractors.cc.  */
352       if (TREE_CODE (TREE_TYPE (field)) == COMPLEX_TYPE)
353         {
354           if (dump_file && (dump_flags & TDF_DETAILS))
355             {
356               fprintf (dump_file, "Cannot scalarize variable ");
357               print_generic_expr (dump_file, var, dump_flags);
358               fprintf (dump_file,
359                        " because it contains a __complex__ field, ");
360               print_generic_expr (dump_file, field, dump_flags);
361               fprintf (dump_file, "\n");
362             }
363           return false;
364         }
365
366       /* FIXME.  We don't scalarize structures with bit fields yet.  To
367          support this, we should make sure that all the fields fit in one
368          word and modify every operation done on the scalarized bit fields
369          to mask them properly.  */
370       if (DECL_BIT_FIELD (field))
371         {
372           if (dump_file && (dump_flags & TDF_DETAILS))
373             {
374               fprintf (dump_file, "Cannot scalarize variable ");
375               print_generic_expr (dump_file, var, dump_flags);
376               fprintf (dump_file,
377                        " because it contains a bit-field, ");
378               print_generic_expr (dump_file, field, dump_flags);
379               fprintf (dump_file, "\n");
380             }
381           return false;
382         }
383
384       nfields++;
385       if (nfields > MAX_NFIELDS_FOR_SRA)
386         {
387           if (dump_file && (dump_flags & TDF_DETAILS))
388             {
389               fprintf (dump_file, "Cannot scalarize variable ");
390               print_generic_expr (dump_file, var, dump_flags);
391               fprintf (dump_file,
392                        " because it contains more than %d fields\n", 
393                        MAX_NFIELDS_FOR_SRA);
394             }
395           return false;
396         }
397     }
398
399   /* If the structure had no FIELD_DECLs, then don't bother
400      scalarizing it.  */
401   return nfields > 0;
402 }
403
404
405 /* Replace the COMPONENT_REF, REALPART_EXPR or IMAGPART_EXPR pointed-to by
406    TP inside STMT with the corresponding scalar replacement from SRA_MAP.  */
407
408 static inline void
409 scalarize_component_ref (tree stmt, tree *tp)
410 {
411   tree t = *tp, obj = TREE_OPERAND (t, 0);
412
413   /* When scalarizing a function argument, we will need to insert copy-in
414      operations from the original PARM_DECLs. Note that these copy-in
415      operations may end up being dead, but we won't know until we rename
416      the new variables into SSA.  */
417   if (TREE_CODE (obj) == PARM_DECL)
418     bitmap_set_bit (needs_copy_in, var_ann (obj)->uid);
419
420   switch (TREE_CODE (t))
421     {
422     case COMPONENT_REF:
423       t = get_scalar_for_field (obj, TREE_OPERAND (t, 1));
424       break;
425     case REALPART_EXPR:
426     case IMAGPART_EXPR:
427       t = get_scalar_for_complex_part (obj, TREE_CODE (t));
428       break;
429     default:
430       abort ();
431     }
432
433   *tp = t;
434   modify_stmt (stmt);
435 }
436
437
438 /* Scalarize the structure assignment for the statement pointed by SI_P.  */
439
440 static void
441 scalarize_structure_assignment (block_stmt_iterator *si_p)
442 {
443   var_ann_t lhs_ann, rhs_ann;
444   tree lhs, rhs, list, orig_stmt;
445   bool lhs_can, rhs_can;
446
447   orig_stmt = bsi_stmt (*si_p);
448   lhs = TREE_OPERAND (orig_stmt, 0);
449   rhs = TREE_OPERAND (orig_stmt, 1);
450   list = NULL_TREE;
451
452 #if defined ENABLE_CHECKING
453   if (TREE_CODE (orig_stmt) != MODIFY_EXPR)
454     abort ();
455 #endif
456
457   /* Remove all type casts from RHS.  This may seem heavy handed but
458      it's actually safe and it is necessary in the presence of C++
459      reinterpret_cast<> where structure assignments of different
460      structures will be present in the IL.  This was the case of PR
461      13347 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13347) which
462      had something like this:
463
464         struct A f;
465         struct B g;
466         f = (struct A)g;
467
468      Both 'f' and 'g' were scalarizable, but the presence of the type
469      cast was causing SRA to not replace the RHS of the assignment
470      with g's scalar replacements.  Furthermore, the fact that this
471      assignment reached this point without causing syntax errors means
472      that the type cast is safe and that a field-by-field assignment
473      from 'g' into 'f' is the right thing to do.  */
474   STRIP_NOPS (rhs);
475
476   lhs_ann = DECL_P (lhs) ? var_ann (lhs) : NULL;
477   rhs_ann = DECL_P (rhs) ? var_ann (rhs) : NULL;
478
479 #if defined ENABLE_CHECKING
480   /* Two different variables should not have the same UID.  */
481   if (lhs_ann
482       && rhs_ann
483       && lhs != rhs
484       && lhs_ann->uid == rhs_ann->uid)
485     abort ();
486 #endif
487
488   lhs_can = lhs_ann && bitmap_bit_p (sra_candidates, lhs_ann->uid);
489   rhs_can = rhs_ann && bitmap_bit_p (sra_candidates, rhs_ann->uid);
490
491   /* Both LHS and RHS are scalarizable.  */
492   if (lhs_can && rhs_can)
493     list = create_scalar_copies (lhs, rhs, SCALAR_SCALAR);
494
495   /* Only RHS is scalarizable.  */
496   else if (rhs_can)
497     list = create_scalar_copies (lhs, rhs, FIELD_SCALAR);
498
499   /* Only LHS is scalarizable.  */
500   else if (lhs_can)
501     list = create_scalar_copies (lhs, rhs, SCALAR_FIELD);
502
503   /* If neither side is scalarizable, do nothing else.  */
504   else
505     return;
506
507   /* Set line number information for our replacements.  */
508   if (EXPR_HAS_LOCATION (orig_stmt))
509     annotate_all_with_locus (&list, EXPR_LOCATION (orig_stmt));
510
511   /* Replace the existing statement with the newly created list of
512      scalarized copies.  When replacing the original statement, the first
513      copy replaces it and the remaining copies are inserted either after
514      the first copy or on the outgoing edges of the original statement's
515      block.  */
516   {
517     tree_stmt_iterator tsi = tsi_start (list);
518     bsi_replace (si_p, tsi_stmt (tsi), true);
519     tsi_delink (&tsi);
520     if (stmt_ends_bb_p (orig_stmt))
521       insert_edge_copies (list, bb_for_stmt (orig_stmt));
522     else
523       bsi_insert_after (si_p, list, BSI_CONTINUE_LINKING);
524   }
525 }
526
527
528 /* Traverse all the referenced variables in the program looking for
529    structures that could be replaced with scalars.  */
530
531 static bool
532 find_candidates_for_sra (void)
533 {
534   size_t i;
535   bool any_set = false;
536
537   for (i = 0; i < num_referenced_vars; i++)
538     {
539       tree var = referenced_var (i);
540
541       if ((TREE_CODE (TREE_TYPE (var)) == RECORD_TYPE
542            || TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE)
543           && can_be_scalarized_p (var))
544         {
545           bitmap_set_bit (sra_candidates, var_ann (var)->uid);
546           any_set = true;
547         }
548     }
549
550   return any_set;
551 }
552
553
554 /* Insert STMT on all the outgoing edges out of BB.  Note that if BB
555    has more than one edge, STMT will be replicated for each edge.  Also,
556    abnormal edges will be ignored.  */
557
558 void
559 insert_edge_copies (tree stmt, basic_block bb)
560 {
561   edge e;
562   bool first_copy;
563
564   first_copy = true;
565   for (e = bb->succ; e; e = e->succ_next)
566     {
567       /* We don't need to insert copies on abnormal edges.  The
568          value of the scalar replacement is not guaranteed to
569          be valid through an abnormal edge.  */
570       if (!(e->flags & EDGE_ABNORMAL))
571         {
572           if (first_copy)
573             {
574               bsi_insert_on_edge (e, stmt);
575               first_copy = false;
576             }
577           else
578             bsi_insert_on_edge (e, lhd_unsave_expr_now (stmt));
579         }
580     }
581 }
582
583
584 /* Append a new assignment statement to TSI.  */
585
586 static tree
587 csc_assign (tree_stmt_iterator *tsi, tree lhs, tree rhs)
588 {
589   tree stmt = build (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
590   modify_stmt (stmt);
591   tsi_link_after (tsi, stmt, TSI_NEW_STMT);
592   return stmt;
593 }
594
595 /* A subroutine of create_scalar_copies.  Construct a COMPONENT_REF
596    expression for BASE referencing FIELD.  INDEX is the field index.  */
597
598 static tree
599 csc_build_component_ref (tree base, tree field)
600 {
601   switch (TREE_CODE (base))
602     {
603     case CONSTRUCTOR:
604       /* Only appears on RHS.  The only remaining CONSTRUCTORS for
605          record types that should remain are empty, and imply that
606          the entire structure should be zeroed.  */
607       if (CONSTRUCTOR_ELTS (base))
608         abort ();
609       return fold_convert (TREE_TYPE (field), integer_zero_node);
610
611     default:
612       /* Avoid sharing BASE when building the different COMPONENT_REFs.
613          We let the first field have the original version.  */
614       if (field != TYPE_FIELDS (TREE_TYPE (base)))
615         base = unshare_expr (base);
616       break;
617
618     case VAR_DECL:
619     case PARM_DECL:
620       /* Special case for the above -- decls are always shared.  */
621       break;
622     }
623
624   return build (COMPONENT_REF, TREE_TYPE (field), base, field);
625 }
626
627 /* Similarly for REALPART_EXPR and IMAGPART_EXPR for complex types.  */
628
629 static tree
630 csc_build_complex_part (tree base, enum tree_code part)
631 {
632   switch (TREE_CODE (base))
633     {
634     case COMPLEX_CST:
635       if (part == REALPART_EXPR)
636         return TREE_REALPART (base);
637       else
638         return TREE_IMAGPART (base);
639
640     case COMPLEX_EXPR:
641       if (part == REALPART_EXPR)
642         return TREE_OPERAND (base, 0);
643       else
644         return TREE_OPERAND (base, 1);
645
646     default:
647       /* Avoid sharing BASE when building the different references.
648          We let the real part have the original version.  */
649       if (part != REALPART_EXPR)
650         base = unshare_expr (base);
651       break;
652
653     case VAR_DECL:
654     case PARM_DECL:
655       /* Special case for the above -- decls are always shared.  */
656       break;
657     }
658
659   return build1 (part, TREE_TYPE (TREE_TYPE (base)), base);
660 }
661
662 /* Create and return a list of assignments to perform a scalarized
663    structure assignment 'LHS = RHS'.  Both LHS and RHS are assumed to be
664    of an aggregate or complex type.  Three types of copies may be specified:
665
666    SCALAR_SCALAR will emit assignments for all the scalar temporaries
667       corresponding to the fields of LHS and RHS.
668
669    FIELD_SCALAR will emit assignments from the scalar replacements of
670       RHS into each of the fields of the LHS.
671
672    SCALAR_FIELD will emit assignments from each field of the RHS into
673       the scalar replacements of the LHS.  */
674
675 static tree
676 create_scalar_copies (tree lhs, tree rhs, enum sra_copy_mode mode)
677 {
678   tree type, list;
679   tree_stmt_iterator tsi;
680
681 #if defined ENABLE_CHECKING
682   /* Sanity checking.  Check that we are not trying to scalarize a
683      non-decl.  */
684   if (!DECL_P (lhs) && (mode == SCALAR_FIELD || mode == SCALAR_SCALAR))
685     abort ();
686   if (!DECL_P (rhs) && (mode == FIELD_SCALAR || mode == SCALAR_SCALAR))
687     abort ();
688 #endif
689
690   type = TREE_TYPE (lhs);
691   list = alloc_stmt_list ();
692   tsi = tsi_start (list);
693
694   /* VA_ARG_EXPRs have side effects, so we need to copy it first to a
695      temporary before scalarizing.  FIXME: This should disappear once
696      VA_ARG_EXPRs are properly lowered.  */
697   if (TREE_CODE (rhs) == VA_ARG_EXPR)
698     {
699       tree stmt, tmp;
700
701       /* Add TMP = VA_ARG_EXPR <>  */
702       tmp = make_rename_temp (TREE_TYPE (rhs), NULL);
703       stmt = csc_assign (&tsi, tmp, rhs);
704
705       /* Mark all the variables in VDEF operands for renaming, because
706          the VA_ARG_EXPR will now be in a different statement.  */
707       mark_all_v_may_defs (stmt);
708       mark_all_v_must_defs (stmt);
709
710       /* Set RHS to be the new temporary TMP.  */
711       rhs = tmp;
712     }
713
714   /* When making *_SCALAR copies from PARM_DECLs, we will need to insert
715      copy-in operations from the original PARM_DECLs.  Note that these
716      copy-in operations may end up being dead, but we won't know until
717      we rename the new variables into SSA.  */
718   if ((mode == SCALAR_SCALAR || mode == FIELD_SCALAR)
719       && TREE_CODE (rhs) == PARM_DECL)
720     bitmap_set_bit (needs_copy_in, var_ann (rhs)->uid);
721
722   /* Now create scalar copies for each individual field according to MODE.  */
723   if (TREE_CODE (type) == COMPLEX_TYPE)
724     {
725       /* Create scalar copies of both the real and imaginary parts.  */
726       tree real_lhs, real_rhs, imag_lhs, imag_rhs;
727
728       if (mode == SCALAR_FIELD)
729         {
730           real_rhs = csc_build_complex_part (rhs, REALPART_EXPR);
731           imag_rhs = csc_build_complex_part (rhs, IMAGPART_EXPR);
732         }
733       else
734         {
735           real_rhs = get_scalar_for_complex_part (rhs, REALPART_EXPR);
736           imag_rhs = get_scalar_for_complex_part (rhs, IMAGPART_EXPR);
737         }
738
739       if (mode == FIELD_SCALAR)
740         {
741           /* In this case we do not need to create but one statement,
742              since we can create a new complex value whole.  */
743
744           if (TREE_CONSTANT (real_rhs) && TREE_CONSTANT (imag_rhs))
745             rhs = build_complex (type, real_rhs, imag_rhs);
746           else
747             rhs = build (COMPLEX_EXPR, type, real_rhs, imag_rhs);
748           csc_assign (&tsi, lhs, rhs);
749         }
750       else
751         {
752           real_lhs = get_scalar_for_complex_part (lhs, REALPART_EXPR);
753           imag_lhs = get_scalar_for_complex_part (lhs, IMAGPART_EXPR);
754
755           csc_assign (&tsi, real_lhs, real_rhs);
756           csc_assign (&tsi, imag_lhs, imag_rhs);
757         }
758     }
759   else
760     {
761       tree lf, rf;
762
763       /* ??? C++ generates copies between different pointer-to-member
764          structures of different types.  To combat this, we must track
765          the field of both the left and right structures, so that we
766          index the variables with fields of their own type.  */
767
768       for (lf = TYPE_FIELDS (type), rf = TYPE_FIELDS (TREE_TYPE (rhs));
769            lf;
770            lf = TREE_CHAIN (lf), rf = TREE_CHAIN (rf))
771         {
772           tree lhs_var, rhs_var;
773
774           /* Only copy FIELD_DECLs.  */
775           if (TREE_CODE (lf) != FIELD_DECL)
776             continue;
777
778           if (mode == FIELD_SCALAR)
779             lhs_var = csc_build_component_ref (lhs, lf);
780           else
781             lhs_var = get_scalar_for_field (lhs, lf);
782
783           if (mode == SCALAR_FIELD)
784             rhs_var = csc_build_component_ref (rhs, rf);
785           else
786             rhs_var = get_scalar_for_field (rhs, rf);
787
788           csc_assign (&tsi, lhs_var, rhs_var);
789         }
790     }
791
792   /* All the scalar copies just created will either create new definitions
793      or remove existing definitions of LHS, so we need to mark it for
794      renaming.  */
795   if (TREE_SIDE_EFFECTS (list))
796     {
797       if (mode == SCALAR_FIELD || mode == SCALAR_SCALAR)
798         {
799           /* If the LHS has been scalarized, mark it for renaming.  */
800           bitmap_set_bit (vars_to_rename, var_ann (lhs)->uid);
801         }
802       else if (mode == FIELD_SCALAR)
803         {
804           /* Otherwise, mark all the symbols in the VDEFs for the last
805              scalarized statement just created.  Since all the statements
806              introduce the same VDEFs, we only need to check the last one.  */
807           mark_all_v_may_defs (tsi_stmt (tsi));
808           mark_all_v_must_defs (tsi_stmt (tsi));
809         }
810       else
811         abort ();
812     }
813
814   return list;
815 }
816
817 /* A helper function that creates the copies, updates line info,
818    and emits the code either before or after BSI.  */
819
820 static void
821 emit_scalar_copies (block_stmt_iterator *bsi, tree lhs, tree rhs,
822                     enum sra_copy_mode mode)
823 {
824   tree list = create_scalar_copies (lhs, rhs, mode);
825   tree stmt = bsi_stmt (*bsi);
826
827   if (EXPR_HAS_LOCATION (stmt))
828     annotate_all_with_locus (&list, EXPR_LOCATION (stmt));
829
830   bsi_insert_before (bsi, list, BSI_SAME_STMT);
831 }
832
833 /* Traverse all the statements in the function replacing references to
834    scalarizable structures with their corresponding scalar temporaries.  */
835
836 static void
837 scalarize_structures (void)
838 {
839   basic_block bb;
840   block_stmt_iterator si;
841   size_t i;
842
843   FOR_EACH_BB (bb)
844     for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
845       {
846         tree stmt;
847         stmt_ann_t ann;
848
849         stmt = bsi_stmt (si);
850         ann = stmt_ann (stmt);
851
852         /* If the statement has no virtual operands, then it doesn't make
853            structure references that we care about.  */
854         if (NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann)) == 0
855             && NUM_VUSES (VUSE_OPS (ann)) == 0
856             && NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann)) == 0)
857           continue;
858
859         /* Structure references may only appear in certain statements.  */
860         if (TREE_CODE (stmt) != MODIFY_EXPR
861             && TREE_CODE (stmt) != CALL_EXPR
862             && TREE_CODE (stmt) != RETURN_EXPR
863             && TREE_CODE (stmt) != ASM_EXPR)
864           continue;
865
866         scalarize_stmt (&si);
867       }
868
869   /* Initialize the scalar replacements for every structure that is a
870      function argument.  */
871   EXECUTE_IF_SET_IN_BITMAP (needs_copy_in, 0, i,
872     {
873       tree var = referenced_var (i);
874       tree list = create_scalar_copies (var, var, SCALAR_FIELD);
875       bsi_insert_on_edge (ENTRY_BLOCK_PTR->succ, list);
876     });
877
878   /* Commit edge insertions.  */
879   bsi_commit_edge_inserts (NULL);
880 }
881
882
883 /* Scalarize structure references in the statement pointed by SI_P.  */
884
885 static void
886 scalarize_stmt (block_stmt_iterator *si_p)
887 {
888   tree stmt = bsi_stmt (*si_p);
889
890   /* Handle assignments.  */
891   if (TREE_CODE (stmt) == MODIFY_EXPR
892       && TREE_CODE (TREE_OPERAND (stmt, 1)) != CALL_EXPR)
893     scalarize_modify_expr (si_p);
894
895   /* Handle RETURN_EXPR.  */
896   else if (TREE_CODE (stmt) == RETURN_EXPR)
897     scalarize_return_expr (si_p);
898
899   /* Handle function calls (note that this must be handled after
900      MODIFY_EXPR and RETURN_EXPR because a function call can appear in
901      both).  */
902   else if (get_call_expr_in (stmt) != NULL_TREE)
903     scalarize_call_expr (si_p);
904
905   /* Handle ASM_EXPRs.  */
906   else if (TREE_CODE (stmt) == ASM_EXPR)
907     scalarize_asm_expr (si_p);
908 }
909
910
911 /* Helper for scalarize_stmt to handle assignments.  */
912
913 static void
914 scalarize_modify_expr (block_stmt_iterator *si_p)
915 {
916   tree stmt = bsi_stmt (*si_p);
917   tree lhs = TREE_OPERAND (stmt, 0);
918   tree rhs = TREE_OPERAND (stmt, 1);
919
920   /* Found AGGREGATE.FIELD = ...  */
921   if (is_sra_candidate_ref (lhs, false))
922     {
923       tree sym;
924       v_may_def_optype v_may_defs;
925
926       scalarize_component_ref (stmt, &TREE_OPERAND (stmt, 0));
927
928       /* Mark the LHS to be renamed, as we have just removed the previous
929          V_MAY_DEF for AGGREGATE.  The statement should have exactly one 
930          V_MAY_DEF for variable AGGREGATE.  */
931       v_may_defs = STMT_V_MAY_DEF_OPS (stmt);
932       if (NUM_V_MAY_DEFS (v_may_defs) != 1)
933         abort ();
934       sym = SSA_NAME_VAR (V_MAY_DEF_RESULT (v_may_defs, 0));
935       bitmap_set_bit (vars_to_rename, var_ann (sym)->uid);
936     }
937
938   /* Found ... = AGGREGATE.FIELD  */
939   else if (is_sra_candidate_ref (rhs, false))
940     scalarize_component_ref (stmt, &TREE_OPERAND (stmt, 1));
941
942   /* Found ... = BIT_FIELD_REF <>.  This is similar to a CALL_EXPR, if the
943      operand of the BIT_FIELD_REF is a scalarizable structure, we need to
944      copy from its scalar replacements before doing the bitfield operation.
945
946      FIXME: BIT_FIELD_REFs are often generated by fold-const.c.  This is
947      not always desirable because they obfuscate the original predicates,
948      limiting what the tree optimizers may do.  For instance, in
949      testsuite/g++.dg/opt/nrv4.C the use of SRA allows the optimizers to
950      optimize function main() to 'return 0;'.  However, the folder
951      generates a BIT_FIELD_REF operation for one of the comparisons,
952      preventing the optimizers from removing all the redundant
953      operations.  */
954   else if (is_sra_candidate_ref (rhs, true))
955     {
956       tree var = TREE_OPERAND (rhs, 0);
957       emit_scalar_copies (si_p, var, var, FIELD_SCALAR);
958     }
959
960   /* Found AGGREGATE = ... or ... = AGGREGATE  */
961   else if (DECL_P (lhs) || DECL_P (rhs))
962     scalarize_structure_assignment (si_p);
963 }
964
965
966 /* Scalarize structure references in LIST.  Use DONE to avoid duplicates.  */
967
968 static inline void
969 scalarize_tree_list (tree list, block_stmt_iterator *si_p, bitmap done)
970 {
971   tree op;
972
973   for (op = list; op; op = TREE_CHAIN (op))
974     {
975       tree arg = TREE_VALUE (op);
976
977       if (is_sra_candidate_decl (arg))
978         {
979           int index = var_ann (arg)->uid;
980           if (!bitmap_bit_p (done, index))
981             {
982               emit_scalar_copies (si_p, arg, arg, FIELD_SCALAR);
983               bitmap_set_bit (done, index);
984             }
985         }
986       else if (is_sra_candidate_ref (arg, false))
987         {
988           tree stmt = bsi_stmt (*si_p);
989           scalarize_component_ref (stmt, &TREE_VALUE (op));
990         }
991     }
992 }
993
994
995 /* Helper for scalarize_stmt to handle function calls.  */
996
997 static void
998 scalarize_call_expr (block_stmt_iterator *si_p)
999 {
1000   tree stmt = bsi_stmt (*si_p);
1001   tree call = (TREE_CODE (stmt) == MODIFY_EXPR) ? TREE_OPERAND (stmt, 1) : stmt;
1002   struct bitmap_head_def done_head;
1003
1004   /* First scalarize the arguments.  Order is important, because the copy
1005      operations for the arguments need to go before the call.
1006      Scalarization of the return value needs to go after the call.  */
1007   bitmap_initialize (&done_head, 1);
1008   scalarize_tree_list (TREE_OPERAND (call, 1), si_p, &done_head);
1009   bitmap_clear (&done_head);
1010
1011   /* Scalarize the return value, if any.  */
1012   if (TREE_CODE (stmt) == MODIFY_EXPR)
1013     {
1014       tree var = TREE_OPERAND (stmt, 0);
1015
1016       /* If the LHS of the assignment is a scalarizable structure, insert
1017          copies into the scalar replacements after the call.  */
1018       if (is_sra_candidate_decl (var))
1019         {
1020           tree list = create_scalar_copies (var, var, SCALAR_FIELD);
1021           if (EXPR_HAS_LOCATION (stmt))
1022             annotate_all_with_locus (&list, EXPR_LOCATION (stmt));
1023           if (stmt_ends_bb_p (stmt))
1024             insert_edge_copies (list, bb_for_stmt (stmt));
1025           else
1026             bsi_insert_after (si_p, list, BSI_NEW_STMT);
1027         }
1028     }
1029 }
1030
1031
1032 /* Helper for scalarize_stmt to handle ASM_EXPRs.  */
1033
1034 static void
1035 scalarize_asm_expr (block_stmt_iterator *si_p)
1036 {
1037   tree stmt = bsi_stmt (*si_p);
1038   struct bitmap_head_def done_head;
1039
1040   bitmap_initialize (&done_head, 1);
1041   scalarize_tree_list (ASM_INPUTS (stmt), si_p, &done_head);
1042   scalarize_tree_list (ASM_OUTPUTS (stmt), si_p, &done_head);
1043   bitmap_clear (&done_head);
1044
1045   /* ??? Process outputs after the asm.  */
1046 }
1047
1048
1049 /* Helper for scalarize_stmt to handle return expressions.  */
1050
1051 static void
1052 scalarize_return_expr (block_stmt_iterator *si_p)
1053 {
1054   tree stmt = bsi_stmt (*si_p);
1055   tree op = TREE_OPERAND (stmt, 0);
1056
1057   if (op == NULL_TREE)
1058     return;
1059
1060   /* Handle a bare RESULT_DECL.  This will handle for types needed
1061      constructors, or possibly after NRV type optimizations.  */
1062   if (is_sra_candidate_decl (op))
1063     emit_scalar_copies (si_p, op, op, FIELD_SCALAR);
1064   else if (TREE_CODE (op) == MODIFY_EXPR)
1065     {
1066       tree *rhs_p = &TREE_OPERAND (op, 1);
1067       tree rhs = *rhs_p;
1068
1069       /* Handle 'return STRUCTURE;'  */
1070       if (is_sra_candidate_decl (rhs))
1071         emit_scalar_copies (si_p, rhs, rhs, FIELD_SCALAR);
1072
1073       /* Handle 'return STRUCTURE.FIELD;'  */
1074       else if (is_sra_candidate_ref (rhs, false))
1075         scalarize_component_ref (stmt, rhs_p);
1076
1077       /* Handle 'return CALL_EXPR;'  */
1078       else if (TREE_CODE (rhs) == CALL_EXPR)
1079         {
1080           struct bitmap_head_def done_head;
1081           bitmap_initialize (&done_head, 1);
1082           scalarize_tree_list (TREE_OPERAND (rhs, 1), si_p, &done_head);
1083           bitmap_clear (&done_head);
1084         }
1085     }
1086 }
1087
1088
1089 /* Debugging dump for the scalar replacement map.  */
1090
1091 static int
1092 dump_sra_map_trav (void **slot, void *data)
1093 {
1094   struct sra_elt *e = *slot;
1095   FILE *f = data;
1096
1097   switch (e->kind)
1098     {
1099     case REALPART_EXPR:
1100       fputs ("__real__ ", f);
1101       print_generic_expr (dump_file, e->base, dump_flags);
1102       fprintf (f, " -> %s\n", get_name (e->replace));
1103       break;
1104     case IMAGPART_EXPR:
1105       fputs ("__imag__ ", f);
1106       print_generic_expr (dump_file, e->base, dump_flags);
1107       fprintf (f, " -> %s\n", get_name (e->replace));
1108       break;
1109     case COMPONENT_REF:
1110       print_generic_expr (dump_file, e->base, dump_flags);
1111       fprintf (f, ".%s -> %s\n", get_name (e->field), get_name (e->replace));
1112       break;
1113     default:
1114       abort ();
1115     }
1116
1117   return 1;
1118 }
1119
1120 static void
1121 dump_sra_map (FILE *f)
1122 {
1123   fputs ("Scalar replacements:\n", f);
1124   htab_traverse_noresize (sra_map, dump_sra_map_trav, f);
1125   fputs ("\n\n", f);
1126 }
1127
1128 /* Main entry point to Scalar Replacement of Aggregates (SRA).  This pass
1129    re-writes non-aliased structure references into scalar temporaries.  The
1130    goal is to expose some/all structures to the scalar optimizers.
1131
1132    Scalarization proceeds in two main phases.  First, every structure
1133    referenced in the program that complies with can_be_scalarized_p is
1134    marked for scalarization (find_candidates_for_sra).
1135    
1136    Second, a mapping between structure fields and scalar temporaries so
1137    that every time a particular field of a particular structure is
1138    referenced in the code, we replace it with its corresponding scalar
1139    temporary (scalarize_structures).
1140
1141    TODO
1142
1143    1- Scalarize COMPLEX_TYPEs
1144    2- Scalarize ARRAY_REFs that are always referenced with a
1145       constant index.
1146    3- Timings to determine when scalarization is not profitable.
1147    4- Determine what's a good value for MAX_NFIELDS_FOR_SRA.  */
1148
1149 static void
1150 tree_sra (void)
1151 {
1152   /* Initialize local variables.  */
1153   sra_candidates = BITMAP_XMALLOC ();
1154   sra_map = NULL;
1155   needs_copy_in = NULL;
1156
1157   /* Find structures to be scalarized.  */
1158   if (!find_candidates_for_sra ())
1159     {
1160       BITMAP_XFREE (sra_candidates);
1161       return;
1162     }
1163
1164   /* If we found any, re-write structure references with their
1165      corresponding scalar replacement.  */
1166   sra_map = htab_create (101, sra_elt_hash, sra_elt_eq, free);
1167   needs_copy_in = BITMAP_XMALLOC ();
1168
1169   scalarize_structures ();
1170
1171   if (dump_file)
1172     dump_sra_map (dump_file);
1173
1174   /* Free allocated memory.  */
1175   htab_delete (sra_map);
1176   sra_map = NULL;
1177   BITMAP_XFREE (needs_copy_in);
1178   BITMAP_XFREE (sra_candidates);
1179 }
1180
1181 static bool
1182 gate_sra (void)
1183 {
1184   return flag_tree_sra != 0;
1185 }
1186
1187 struct tree_opt_pass pass_sra = 
1188 {
1189   "sra",                                /* name */
1190   gate_sra,                             /* gate */
1191   tree_sra,                             /* execute */
1192   NULL,                                 /* sub */
1193   NULL,                                 /* next */
1194   0,                                    /* static_pass_number */
1195   TV_TREE_SRA,                          /* tv_id */
1196   PROP_cfg | PROP_ssa,                  /* properties_required */
1197   0,                                    /* properties_provided */
1198   0,                                    /* properties_destroyed */
1199   0,                                    /* todo_flags_start */
1200   TODO_dump_func | TODO_rename_vars
1201     | TODO_ggc_collect | TODO_verify_ssa  /* todo_flags_finish */
1202 };