OSDN Git Service

2011-01-27 Martin Jambor <mjambor@suse.cz>
[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) 2008, 2009, 2010 Free Software Foundation, Inc.
5    Contributed by Martin Jambor <mjambor@suse.cz>
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 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 COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 /* This file implements Scalar Reduction of Aggregates (SRA).  SRA is run
24    twice, once in the early stages of compilation (early SRA) and once in the
25    late stages (late SRA).  The aim of both is to turn references to scalar
26    parts of aggregates into uses of independent scalar variables.
27
28    The two passes are nearly identical, the only difference is that early SRA
29    does not scalarize unions which are used as the result in a GIMPLE_RETURN
30    statement because together with inlining this can lead to weird type
31    conversions.
32
33    Both passes operate in four stages:
34
35    1. The declarations that have properties which make them candidates for
36       scalarization are identified in function find_var_candidates().  The
37       candidates are stored in candidate_bitmap.
38
39    2. The function body is scanned.  In the process, declarations which are
40       used in a manner that prevent their scalarization are removed from the
41       candidate bitmap.  More importantly, for every access into an aggregate,
42       an access structure (struct access) is created by create_access() and
43       stored in a vector associated with the aggregate.  Among other
44       information, the aggregate declaration, the offset and size of the access
45       and its type are stored in the structure.
46
47       On a related note, assign_link structures are created for every assign
48       statement between candidate aggregates and attached to the related
49       accesses.
50
51    3. The vectors of accesses are analyzed.  They are first sorted according to
52       their offset and size and then scanned for partially overlapping accesses
53       (i.e. those which overlap but one is not entirely within another).  Such
54       an access disqualifies the whole aggregate from being scalarized.
55
56       If there is no such inhibiting overlap, a representative access structure
57       is chosen for every unique combination of offset and size.  Afterwards,
58       the pass builds a set of trees from these structures, in which children
59       of an access are within their parent (in terms of offset and size).
60
61       Then accesses  are propagated  whenever possible (i.e.  in cases  when it
62       does not create a partially overlapping access) across assign_links from
63       the right hand side to the left hand side.
64
65       Then the set of trees for each declaration is traversed again and those
66       accesses which should be replaced by a scalar are identified.
67
68    4. The function is traversed again, and for every reference into an
69       aggregate that has some component which is about to be scalarized,
70       statements are amended and new statements are created as necessary.
71       Finally, if a parameter got scalarized, the scalar replacements are
72       initialized with values from respective parameter aggregates.  */
73
74 #include "config.h"
75 #include "system.h"
76 #include "coretypes.h"
77 #include "alloc-pool.h"
78 #include "tm.h"
79 #include "tree.h"
80 #include "gimple.h"
81 #include "cgraph.h"
82 #include "tree-flow.h"
83 #include "ipa-prop.h"
84 #include "tree-pretty-print.h"
85 #include "statistics.h"
86 #include "tree-dump.h"
87 #include "timevar.h"
88 #include "params.h"
89 #include "target.h"
90 #include "flags.h"
91 #include "dbgcnt.h"
92 #include "tree-inline.h"
93 #include "gimple-pretty-print.h"
94
95 /* Enumeration of all aggregate reductions we can do.  */
96 enum sra_mode { SRA_MODE_EARLY_IPA,   /* early call regularization */
97                 SRA_MODE_EARLY_INTRA, /* early intraprocedural SRA */
98                 SRA_MODE_INTRA };     /* late intraprocedural SRA */
99
100 /* Global variable describing which aggregate reduction we are performing at
101    the moment.  */
102 static enum sra_mode sra_mode;
103
104 struct assign_link;
105
106 /* ACCESS represents each access to an aggregate variable (as a whole or a
107    part).  It can also represent a group of accesses that refer to exactly the
108    same fragment of an aggregate (i.e. those that have exactly the same offset
109    and size).  Such representatives for a single aggregate, once determined,
110    are linked in a linked list and have the group fields set.
111
112    Moreover, when doing intraprocedural SRA, a tree is built from those
113    representatives (by the means of first_child and next_sibling pointers), in
114    which all items in a subtree are "within" the root, i.e. their offset is
115    greater or equal to offset of the root and offset+size is smaller or equal
116    to offset+size of the root.  Children of an access are sorted by offset.
117
118    Note that accesses to parts of vector and complex number types always
119    represented by an access to the whole complex number or a vector.  It is a
120    duty of the modifying functions to replace them appropriately.  */
121
122 struct access
123 {
124   /* Values returned by  `get_ref_base_and_extent' for each component reference
125      If EXPR isn't a component reference  just set `BASE = EXPR', `OFFSET = 0',
126      `SIZE = TREE_SIZE (TREE_TYPE (expr))'.  */
127   HOST_WIDE_INT offset;
128   HOST_WIDE_INT size;
129   tree base;
130
131   /* Expression.  It is context dependent so do not use it to create new
132      expressions to access the original aggregate.  See PR 42154 for a
133      testcase.  */
134   tree expr;
135   /* Type.  */
136   tree type;
137
138   /* The statement this access belongs to.  */
139   gimple stmt;
140
141   /* Next group representative for this aggregate. */
142   struct access *next_grp;
143
144   /* Pointer to the group representative.  Pointer to itself if the struct is
145      the representative.  */
146   struct access *group_representative;
147
148   /* If this access has any children (in terms of the definition above), this
149      points to the first one.  */
150   struct access *first_child;
151
152   /* In intraprocedural SRA, pointer to the next sibling in the access tree as
153      described above.  In IPA-SRA this is a pointer to the next access
154      belonging to the same group (having the same representative).  */
155   struct access *next_sibling;
156
157   /* Pointers to the first and last element in the linked list of assign
158      links.  */
159   struct assign_link *first_link, *last_link;
160
161   /* Pointer to the next access in the work queue.  */
162   struct access *next_queued;
163
164   /* Replacement variable for this access "region."  Never to be accessed
165      directly, always only by the means of get_access_replacement() and only
166      when grp_to_be_replaced flag is set.  */
167   tree replacement_decl;
168
169   /* Is this particular access write access? */
170   unsigned write : 1;
171
172   /* Is this access an artificial one created to scalarize some record
173      entirely? */
174   unsigned total_scalarization : 1;
175
176   /* Is this access an access to a non-addressable field? */
177   unsigned non_addressable : 1;
178
179   /* Is this access currently in the work queue?  */
180   unsigned grp_queued : 1;
181
182   /* Does this group contain a write access?  This flag is propagated down the
183      access tree.  */
184   unsigned grp_write : 1;
185
186   /* Does this group contain a read access?  This flag is propagated down the
187      access tree.  */
188   unsigned grp_read : 1;
189
190   /* Does this group contain a read access that comes from an assignment
191      statement?  This flag is propagated down the access tree.  */
192   unsigned grp_assignment_read : 1;
193
194   /* Does this group contain a write access that comes from an assignment
195      statement?  This flag is propagated down the access tree.  */
196   unsigned grp_assignment_write : 1;
197
198   /* Other passes of the analysis use this bit to make function
199      analyze_access_subtree create scalar replacements for this group if
200      possible.  */
201   unsigned grp_hint : 1;
202
203   /* Is the subtree rooted in this access fully covered by scalar
204      replacements?  */
205   unsigned grp_covered : 1;
206
207   /* If set to true, this access and all below it in an access tree must not be
208      scalarized.  */
209   unsigned grp_unscalarizable_region : 1;
210
211   /* Whether data have been written to parts of the aggregate covered by this
212      access which is not to be scalarized.  This flag is propagated up in the
213      access tree.  */
214   unsigned grp_unscalarized_data : 1;
215
216   /* Does this access and/or group contain a write access through a
217      BIT_FIELD_REF?  */
218   unsigned grp_partial_lhs : 1;
219
220   /* Set when a scalar replacement should be created for this variable.  We do
221      the decision and creation at different places because create_tmp_var
222      cannot be called from within FOR_EACH_REFERENCED_VAR. */
223   unsigned grp_to_be_replaced : 1;
224
225   /* Should TREE_NO_WARNING of a replacement be set?  */
226   unsigned grp_no_warning : 1;
227
228   /* Is it possible that the group refers to data which might be (directly or
229      otherwise) modified?  */
230   unsigned grp_maybe_modified : 1;
231
232   /* Set when this is a representative of a pointer to scalar (i.e. by
233      reference) parameter which we consider for turning into a plain scalar
234      (i.e. a by value parameter).  */
235   unsigned grp_scalar_ptr : 1;
236
237   /* Set when we discover that this pointer is not safe to dereference in the
238      caller.  */
239   unsigned grp_not_necessarilly_dereferenced : 1;
240 };
241
242 typedef struct access *access_p;
243
244 DEF_VEC_P (access_p);
245 DEF_VEC_ALLOC_P (access_p, heap);
246
247 /* Alloc pool for allocating access structures.  */
248 static alloc_pool access_pool;
249
250 /* A structure linking lhs and rhs accesses from an aggregate assignment.  They
251    are used to propagate subaccesses from rhs to lhs as long as they don't
252    conflict with what is already there.  */
253 struct assign_link
254 {
255   struct access *lacc, *racc;
256   struct assign_link *next;
257 };
258
259 /* Alloc pool for allocating assign link structures.  */
260 static alloc_pool link_pool;
261
262 /* Base (tree) -> Vector (VEC(access_p,heap) *) map.  */
263 static struct pointer_map_t *base_access_vec;
264
265 /* Bitmap of candidates.  */
266 static bitmap candidate_bitmap;
267
268 /* Bitmap of candidates which we should try to entirely scalarize away and
269    those which cannot be (because they are and need be used as a whole).  */
270 static bitmap should_scalarize_away_bitmap, cannot_scalarize_away_bitmap;
271
272 /* Obstack for creation of fancy names.  */
273 static struct obstack name_obstack;
274
275 /* Head of a linked list of accesses that need to have its subaccesses
276    propagated to their assignment counterparts. */
277 static struct access *work_queue_head;
278
279 /* Number of parameters of the analyzed function when doing early ipa SRA.  */
280 static int func_param_count;
281
282 /* scan_function sets the following to true if it encounters a call to
283    __builtin_apply_args.  */
284 static bool encountered_apply_args;
285
286 /* Set by scan_function when it finds a recursive call.  */
287 static bool encountered_recursive_call;
288
289 /* Set by scan_function when it finds a recursive call with less actual
290    arguments than formal parameters..  */
291 static bool encountered_unchangable_recursive_call;
292
293 /* This is a table in which for each basic block and parameter there is a
294    distance (offset + size) in that parameter which is dereferenced and
295    accessed in that BB.  */
296 static HOST_WIDE_INT *bb_dereferences;
297 /* Bitmap of BBs that can cause the function to "stop" progressing by
298    returning, throwing externally, looping infinitely or calling a function
299    which might abort etc.. */
300 static bitmap final_bbs;
301
302 /* Representative of no accesses at all. */
303 static struct access  no_accesses_representant;
304
305 /* Predicate to test the special value.  */
306
307 static inline bool
308 no_accesses_p (struct access *access)
309 {
310   return access == &no_accesses_representant;
311 }
312
313 /* Dump contents of ACCESS to file F in a human friendly way.  If GRP is true,
314    representative fields are dumped, otherwise those which only describe the
315    individual access are.  */
316
317 static struct
318 {
319   /* Number of processed aggregates is readily available in
320      analyze_all_variable_accesses and so is not stored here.  */
321
322   /* Number of created scalar replacements.  */
323   int replacements;
324
325   /* Number of times sra_modify_expr or sra_modify_assign themselves changed an
326      expression.  */
327   int exprs;
328
329   /* Number of statements created by generate_subtree_copies.  */
330   int subtree_copies;
331
332   /* Number of statements created by load_assign_lhs_subreplacements.  */
333   int subreplacements;
334
335   /* Number of times sra_modify_assign has deleted a statement.  */
336   int deleted;
337
338   /* Number of times sra_modify_assign has to deal with subaccesses of LHS and
339      RHS reparately due to type conversions or nonexistent matching
340      references.  */
341   int separate_lhs_rhs_handling;
342
343   /* Number of parameters that were removed because they were unused.  */
344   int deleted_unused_parameters;
345
346   /* Number of scalars passed as parameters by reference that have been
347      converted to be passed by value.  */
348   int scalar_by_ref_to_by_val;
349
350   /* Number of aggregate parameters that were replaced by one or more of their
351      components.  */
352   int aggregate_params_reduced;
353
354   /* Numbber of components created when splitting aggregate parameters.  */
355   int param_reductions_created;
356 } sra_stats;
357
358 static void
359 dump_access (FILE *f, struct access *access, bool grp)
360 {
361   fprintf (f, "access { ");
362   fprintf (f, "base = (%d)'", DECL_UID (access->base));
363   print_generic_expr (f, access->base, 0);
364   fprintf (f, "', offset = " HOST_WIDE_INT_PRINT_DEC, access->offset);
365   fprintf (f, ", size = " HOST_WIDE_INT_PRINT_DEC, access->size);
366   fprintf (f, ", expr = ");
367   print_generic_expr (f, access->expr, 0);
368   fprintf (f, ", type = ");
369   print_generic_expr (f, access->type, 0);
370   if (grp)
371     fprintf (f, ", grp_write = %d, total_scalarization = %d, "
372              "grp_read = %d, grp_hint = %d, grp_assignment_read = %d,"
373              "grp_assignment_write = %d, grp_covered = %d, "
374              "grp_unscalarizable_region = %d, grp_unscalarized_data = %d, "
375              "grp_partial_lhs = %d, grp_to_be_replaced = %d, "
376              "grp_maybe_modified = %d, "
377              "grp_not_necessarilly_dereferenced = %d\n",
378              access->grp_write, access->total_scalarization,
379              access->grp_read, access->grp_hint, access->grp_assignment_read,
380              access->grp_assignment_write, access->grp_covered,
381              access->grp_unscalarizable_region, access->grp_unscalarized_data,
382              access->grp_partial_lhs, access->grp_to_be_replaced,
383              access->grp_maybe_modified,
384              access->grp_not_necessarilly_dereferenced);
385   else
386     fprintf (f, ", write = %d, total_scalarization = %d, "
387              "grp_partial_lhs = %d\n",
388              access->write, access->total_scalarization,
389              access->grp_partial_lhs);
390 }
391
392 /* Dump a subtree rooted in ACCESS to file F, indent by LEVEL.  */
393
394 static void
395 dump_access_tree_1 (FILE *f, struct access *access, int level)
396 {
397   do
398     {
399       int i;
400
401       for (i = 0; i < level; i++)
402         fputs ("* ", dump_file);
403
404       dump_access (f, access, true);
405
406       if (access->first_child)
407         dump_access_tree_1 (f, access->first_child, level + 1);
408
409       access = access->next_sibling;
410     }
411   while (access);
412 }
413
414 /* Dump all access trees for a variable, given the pointer to the first root in
415    ACCESS.  */
416
417 static void
418 dump_access_tree (FILE *f, struct access *access)
419 {
420   for (; access; access = access->next_grp)
421     dump_access_tree_1 (f, access, 0);
422 }
423
424 /* Return true iff ACC is non-NULL and has subaccesses.  */
425
426 static inline bool
427 access_has_children_p (struct access *acc)
428 {
429   return acc && acc->first_child;
430 }
431
432 /* Return a vector of pointers to accesses for the variable given in BASE or
433    NULL if there is none.  */
434
435 static VEC (access_p, heap) *
436 get_base_access_vector (tree base)
437 {
438   void **slot;
439
440   slot = pointer_map_contains (base_access_vec, base);
441   if (!slot)
442     return NULL;
443   else
444     return *(VEC (access_p, heap) **) slot;
445 }
446
447 /* Find an access with required OFFSET and SIZE in a subtree of accesses rooted
448    in ACCESS.  Return NULL if it cannot be found.  */
449
450 static struct access *
451 find_access_in_subtree (struct access *access, HOST_WIDE_INT offset,
452                         HOST_WIDE_INT size)
453 {
454   while (access && (access->offset != offset || access->size != size))
455     {
456       struct access *child = access->first_child;
457
458       while (child && (child->offset + child->size <= offset))
459         child = child->next_sibling;
460       access = child;
461     }
462
463   return access;
464 }
465
466 /* Return the first group representative for DECL or NULL if none exists.  */
467
468 static struct access *
469 get_first_repr_for_decl (tree base)
470 {
471   VEC (access_p, heap) *access_vec;
472
473   access_vec = get_base_access_vector (base);
474   if (!access_vec)
475     return NULL;
476
477   return VEC_index (access_p, access_vec, 0);
478 }
479
480 /* Find an access representative for the variable BASE and given OFFSET and
481    SIZE.  Requires that access trees have already been built.  Return NULL if
482    it cannot be found.  */
483
484 static struct access *
485 get_var_base_offset_size_access (tree base, HOST_WIDE_INT offset,
486                                  HOST_WIDE_INT size)
487 {
488   struct access *access;
489
490   access = get_first_repr_for_decl (base);
491   while (access && (access->offset + access->size <= offset))
492     access = access->next_grp;
493   if (!access)
494     return NULL;
495
496   return find_access_in_subtree (access, offset, size);
497 }
498
499 /* Add LINK to the linked list of assign links of RACC.  */
500 static void
501 add_link_to_rhs (struct access *racc, struct assign_link *link)
502 {
503   gcc_assert (link->racc == racc);
504
505   if (!racc->first_link)
506     {
507       gcc_assert (!racc->last_link);
508       racc->first_link = link;
509     }
510   else
511     racc->last_link->next = link;
512
513   racc->last_link = link;
514   link->next = NULL;
515 }
516
517 /* Move all link structures in their linked list in OLD_RACC to the linked list
518    in NEW_RACC.  */
519 static void
520 relink_to_new_repr (struct access *new_racc, struct access *old_racc)
521 {
522   if (!old_racc->first_link)
523     {
524       gcc_assert (!old_racc->last_link);
525       return;
526     }
527
528   if (new_racc->first_link)
529     {
530       gcc_assert (!new_racc->last_link->next);
531       gcc_assert (!old_racc->last_link || !old_racc->last_link->next);
532
533       new_racc->last_link->next = old_racc->first_link;
534       new_racc->last_link = old_racc->last_link;
535     }
536   else
537     {
538       gcc_assert (!new_racc->last_link);
539
540       new_racc->first_link = old_racc->first_link;
541       new_racc->last_link = old_racc->last_link;
542     }
543   old_racc->first_link = old_racc->last_link = NULL;
544 }
545
546 /* Add ACCESS to the work queue (which is actually a stack).  */
547
548 static void
549 add_access_to_work_queue (struct access *access)
550 {
551   if (!access->grp_queued)
552     {
553       gcc_assert (!access->next_queued);
554       access->next_queued = work_queue_head;
555       access->grp_queued = 1;
556       work_queue_head = access;
557     }
558 }
559
560 /* Pop an access from the work queue, and return it, assuming there is one.  */
561
562 static struct access *
563 pop_access_from_work_queue (void)
564 {
565   struct access *access = work_queue_head;
566
567   work_queue_head = access->next_queued;
568   access->next_queued = NULL;
569   access->grp_queued = 0;
570   return access;
571 }
572
573
574 /* Allocate necessary structures.  */
575
576 static void
577 sra_initialize (void)
578 {
579   candidate_bitmap = BITMAP_ALLOC (NULL);
580   should_scalarize_away_bitmap = BITMAP_ALLOC (NULL);
581   cannot_scalarize_away_bitmap = BITMAP_ALLOC (NULL);
582   gcc_obstack_init (&name_obstack);
583   access_pool = create_alloc_pool ("SRA accesses", sizeof (struct access), 16);
584   link_pool = create_alloc_pool ("SRA links", sizeof (struct assign_link), 16);
585   base_access_vec = pointer_map_create ();
586   memset (&sra_stats, 0, sizeof (sra_stats));
587   encountered_apply_args = false;
588   encountered_recursive_call = false;
589   encountered_unchangable_recursive_call = false;
590 }
591
592 /* Hook fed to pointer_map_traverse, deallocate stored vectors.  */
593
594 static bool
595 delete_base_accesses (const void *key ATTRIBUTE_UNUSED, void **value,
596                      void *data ATTRIBUTE_UNUSED)
597 {
598   VEC (access_p, heap) *access_vec;
599   access_vec = (VEC (access_p, heap) *) *value;
600   VEC_free (access_p, heap, access_vec);
601
602   return true;
603 }
604
605 /* Deallocate all general structures.  */
606
607 static void
608 sra_deinitialize (void)
609 {
610   BITMAP_FREE (candidate_bitmap);
611   BITMAP_FREE (should_scalarize_away_bitmap);
612   BITMAP_FREE (cannot_scalarize_away_bitmap);
613   free_alloc_pool (access_pool);
614   free_alloc_pool (link_pool);
615   obstack_free (&name_obstack, NULL);
616
617   pointer_map_traverse (base_access_vec, delete_base_accesses, NULL);
618   pointer_map_destroy (base_access_vec);
619 }
620
621 /* Remove DECL from candidates for SRA and write REASON to the dump file if
622    there is one.  */
623 static void
624 disqualify_candidate (tree decl, const char *reason)
625 {
626   bitmap_clear_bit (candidate_bitmap, DECL_UID (decl));
627
628   if (dump_file && (dump_flags & TDF_DETAILS))
629     {
630       fprintf (dump_file, "! Disqualifying ");
631       print_generic_expr (dump_file, decl, 0);
632       fprintf (dump_file, " - %s\n", reason);
633     }
634 }
635
636 /* Return true iff the type contains a field or an element which does not allow
637    scalarization.  */
638
639 static bool
640 type_internals_preclude_sra_p (tree type)
641 {
642   tree fld;
643   tree et;
644
645   switch (TREE_CODE (type))
646     {
647     case RECORD_TYPE:
648     case UNION_TYPE:
649     case QUAL_UNION_TYPE:
650       for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
651         if (TREE_CODE (fld) == FIELD_DECL)
652           {
653             tree ft = TREE_TYPE (fld);
654
655             if (TREE_THIS_VOLATILE (fld)
656                 || !DECL_FIELD_OFFSET (fld) || !DECL_SIZE (fld)
657                 || !host_integerp (DECL_FIELD_OFFSET (fld), 1)
658                 || !host_integerp (DECL_SIZE (fld), 1)
659                 || (AGGREGATE_TYPE_P (ft)
660                     && int_bit_position (fld) % BITS_PER_UNIT != 0))
661               return true;
662
663             if (AGGREGATE_TYPE_P (ft)
664                 && type_internals_preclude_sra_p (ft))
665               return true;
666           }
667
668       return false;
669
670     case ARRAY_TYPE:
671       et = TREE_TYPE (type);
672
673       if (AGGREGATE_TYPE_P (et))
674         return type_internals_preclude_sra_p (et);
675       else
676         return false;
677
678     default:
679       return false;
680     }
681 }
682
683 /* If T is an SSA_NAME, return NULL if it is not a default def or return its
684    base variable if it is.  Return T if it is not an SSA_NAME.  */
685
686 static tree
687 get_ssa_base_param (tree t)
688 {
689   if (TREE_CODE (t) == SSA_NAME)
690     {
691       if (SSA_NAME_IS_DEFAULT_DEF (t))
692         return SSA_NAME_VAR (t);
693       else
694         return NULL_TREE;
695     }
696   return t;
697 }
698
699 /* Mark a dereference of BASE of distance DIST in a basic block tht STMT
700    belongs to, unless the BB has already been marked as a potentially
701    final.  */
702
703 static void
704 mark_parm_dereference (tree base, HOST_WIDE_INT dist, gimple stmt)
705 {
706   basic_block bb = gimple_bb (stmt);
707   int idx, parm_index = 0;
708   tree parm;
709
710   if (bitmap_bit_p (final_bbs, bb->index))
711     return;
712
713   for (parm = DECL_ARGUMENTS (current_function_decl);
714        parm && parm != base;
715        parm = DECL_CHAIN (parm))
716     parm_index++;
717
718   gcc_assert (parm_index < func_param_count);
719
720   idx = bb->index * func_param_count + parm_index;
721   if (bb_dereferences[idx] < dist)
722     bb_dereferences[idx] = dist;
723 }
724
725 /* Allocate an access structure for BASE, OFFSET and SIZE, clear it, fill in
726    the three fields.  Also add it to the vector of accesses corresponding to
727    the base.  Finally, return the new access.  */
728
729 static struct access *
730 create_access_1 (tree base, HOST_WIDE_INT offset, HOST_WIDE_INT size)
731 {
732   VEC (access_p, heap) *vec;
733   struct access *access;
734   void **slot;
735
736   access = (struct access *) pool_alloc (access_pool);
737   memset (access, 0, sizeof (struct access));
738   access->base = base;
739   access->offset = offset;
740   access->size = size;
741
742   slot = pointer_map_contains (base_access_vec, base);
743   if (slot)
744     vec = (VEC (access_p, heap) *) *slot;
745   else
746     vec = VEC_alloc (access_p, heap, 32);
747
748   VEC_safe_push (access_p, heap, vec, access);
749
750   *((struct VEC (access_p,heap) **)
751         pointer_map_insert (base_access_vec, base)) = vec;
752
753   return access;
754 }
755
756 /* Create and insert access for EXPR. Return created access, or NULL if it is
757    not possible.  */
758
759 static struct access *
760 create_access (tree expr, gimple stmt, bool write)
761 {
762   struct access *access;
763   HOST_WIDE_INT offset, size, max_size;
764   tree base = expr;
765   bool ptr, unscalarizable_region = false;
766
767   base = get_ref_base_and_extent (expr, &offset, &size, &max_size);
768
769   if (sra_mode == SRA_MODE_EARLY_IPA
770       && TREE_CODE (base) == MEM_REF)
771     {
772       base = get_ssa_base_param (TREE_OPERAND (base, 0));
773       if (!base)
774         return NULL;
775       ptr = true;
776     }
777   else
778     ptr = false;
779
780   if (!DECL_P (base) || !bitmap_bit_p (candidate_bitmap, DECL_UID (base)))
781     return NULL;
782
783   if (sra_mode == SRA_MODE_EARLY_IPA)
784     {
785       if (size < 0 || size != max_size)
786         {
787           disqualify_candidate (base, "Encountered a variable sized access.");
788           return NULL;
789         }
790       if (TREE_CODE (expr) == COMPONENT_REF
791           && DECL_BIT_FIELD (TREE_OPERAND (expr, 1)))
792         {
793           disqualify_candidate (base, "Encountered a bit-field access.");
794           return NULL;
795         }
796       gcc_checking_assert ((offset % BITS_PER_UNIT) == 0);
797
798       if (ptr)
799         mark_parm_dereference (base, offset + size, stmt);
800     }
801   else
802     {
803       if (size != max_size)
804         {
805           size = max_size;
806           unscalarizable_region = true;
807         }
808       if (size < 0)
809         {
810           disqualify_candidate (base, "Encountered an unconstrained access.");
811           return NULL;
812         }
813     }
814
815   access = create_access_1 (base, offset, size);
816   access->expr = expr;
817   access->type = TREE_TYPE (expr);
818   access->write = write;
819   access->grp_unscalarizable_region = unscalarizable_region;
820   access->stmt = stmt;
821
822   if (TREE_CODE (expr) == COMPONENT_REF
823       && DECL_NONADDRESSABLE_P (TREE_OPERAND (expr, 1)))
824     access->non_addressable = 1;
825
826   return access;
827 }
828
829
830 /* Return true iff TYPE is a RECORD_TYPE with fields that are either of gimple
831    register types or (recursively) records with only these two kinds of fields.
832    It also returns false if any of these records contains a bit-field.  */
833
834 static bool
835 type_consists_of_records_p (tree type)
836 {
837   tree fld;
838
839   if (TREE_CODE (type) != RECORD_TYPE)
840     return false;
841
842   for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
843     if (TREE_CODE (fld) == FIELD_DECL)
844       {
845         tree ft = TREE_TYPE (fld);
846
847         if (DECL_BIT_FIELD (fld))
848           return false;
849
850         if (!is_gimple_reg_type (ft)
851             && !type_consists_of_records_p (ft))
852           return false;
853       }
854
855   return true;
856 }
857
858 /* Create total_scalarization accesses for all scalar type fields in DECL that
859    must be of a RECORD_TYPE conforming to type_consists_of_records_p.  BASE
860    must be the top-most VAR_DECL representing the variable, OFFSET must be the
861    offset of DECL within BASE.  REF must be the memory reference expression for
862    the given decl.  */
863
864 static void
865 completely_scalarize_record (tree base, tree decl, HOST_WIDE_INT offset,
866                              tree ref)
867 {
868   tree fld, decl_type = TREE_TYPE (decl);
869
870   for (fld = TYPE_FIELDS (decl_type); fld; fld = DECL_CHAIN (fld))
871     if (TREE_CODE (fld) == FIELD_DECL)
872       {
873         HOST_WIDE_INT pos = offset + int_bit_position (fld);
874         tree ft = TREE_TYPE (fld);
875         tree nref = build3 (COMPONENT_REF, TREE_TYPE (fld), ref, fld,
876                             NULL_TREE);
877
878         if (is_gimple_reg_type (ft))
879           {
880             struct access *access;
881             HOST_WIDE_INT size;
882
883             size = tree_low_cst (DECL_SIZE (fld), 1);
884             access = create_access_1 (base, pos, size);
885             access->expr = nref;
886             access->type = ft;
887             access->total_scalarization = 1;
888             /* Accesses for intraprocedural SRA can have their stmt NULL.  */
889           }
890         else
891           completely_scalarize_record (base, fld, pos, nref);
892       }
893 }
894
895
896 /* Search the given tree for a declaration by skipping handled components and
897    exclude it from the candidates.  */
898
899 static void
900 disqualify_base_of_expr (tree t, const char *reason)
901 {
902   t = get_base_address (t);
903   if (sra_mode == SRA_MODE_EARLY_IPA
904       && TREE_CODE (t) == MEM_REF)
905     t = get_ssa_base_param (TREE_OPERAND (t, 0));
906
907   if (t && DECL_P (t))
908     disqualify_candidate (t, reason);
909 }
910
911 /* Scan expression EXPR and create access structures for all accesses to
912    candidates for scalarization.  Return the created access or NULL if none is
913    created.  */
914
915 static struct access *
916 build_access_from_expr_1 (tree expr, gimple stmt, bool write)
917 {
918   struct access *ret = NULL;
919   bool partial_ref;
920
921   if (TREE_CODE (expr) == BIT_FIELD_REF
922       || TREE_CODE (expr) == IMAGPART_EXPR
923       || TREE_CODE (expr) == REALPART_EXPR)
924     {
925       expr = TREE_OPERAND (expr, 0);
926       partial_ref = true;
927     }
928   else
929     partial_ref = false;
930
931   /* We need to dive through V_C_Es in order to get the size of its parameter
932      and not the result type.  Ada produces such statements.  We are also
933      capable of handling the topmost V_C_E but not any of those buried in other
934      handled components.  */
935   if (TREE_CODE (expr) == VIEW_CONVERT_EXPR)
936     expr = TREE_OPERAND (expr, 0);
937
938   if (contains_view_convert_expr_p (expr))
939     {
940       disqualify_base_of_expr (expr, "V_C_E under a different handled "
941                                "component.");
942       return NULL;
943     }
944
945   switch (TREE_CODE (expr))
946     {
947     case MEM_REF:
948       if (TREE_CODE (TREE_OPERAND (expr, 0)) != ADDR_EXPR
949           && sra_mode != SRA_MODE_EARLY_IPA)
950         return NULL;
951       /* fall through */
952     case VAR_DECL:
953     case PARM_DECL:
954     case RESULT_DECL:
955     case COMPONENT_REF:
956     case ARRAY_REF:
957     case ARRAY_RANGE_REF:
958       ret = create_access (expr, stmt, write);
959       break;
960
961     default:
962       break;
963     }
964
965   if (write && partial_ref && ret)
966     ret->grp_partial_lhs = 1;
967
968   return ret;
969 }
970
971 /* Scan expression EXPR and create access structures for all accesses to
972    candidates for scalarization.  Return true if any access has been inserted.
973    STMT must be the statement from which the expression is taken, WRITE must be
974    true if the expression is a store and false otherwise. */
975
976 static bool
977 build_access_from_expr (tree expr, gimple stmt, bool write)
978 {
979   struct access *access;
980
981   access = build_access_from_expr_1 (expr, stmt, write);
982   if (access)
983     {
984       /* This means the aggregate is accesses as a whole in a way other than an
985          assign statement and thus cannot be removed even if we had a scalar
986          replacement for everything.  */
987       if (cannot_scalarize_away_bitmap)
988         bitmap_set_bit (cannot_scalarize_away_bitmap, DECL_UID (access->base));
989       return true;
990     }
991   return false;
992 }
993
994 /* Disqualify LHS and RHS for scalarization if STMT must end its basic block in
995    modes in which it matters, return true iff they have been disqualified.  RHS
996    may be NULL, in that case ignore it.  If we scalarize an aggregate in
997    intra-SRA we may need to add statements after each statement.  This is not
998    possible if a statement unconditionally has to end the basic block.  */
999 static bool
1000 disqualify_ops_if_throwing_stmt (gimple stmt, tree lhs, tree rhs)
1001 {
1002   if ((sra_mode == SRA_MODE_EARLY_INTRA || sra_mode == SRA_MODE_INTRA)
1003       && (stmt_can_throw_internal (stmt) || stmt_ends_bb_p (stmt)))
1004     {
1005       disqualify_base_of_expr (lhs, "LHS of a throwing stmt.");
1006       if (rhs)
1007         disqualify_base_of_expr (rhs, "RHS of a throwing stmt.");
1008       return true;
1009     }
1010   return false;
1011 }
1012
1013 /* Scan expressions occuring in STMT, create access structures for all accesses
1014    to candidates for scalarization and remove those candidates which occur in
1015    statements or expressions that prevent them from being split apart.  Return
1016    true if any access has been inserted.  */
1017
1018 static bool
1019 build_accesses_from_assign (gimple stmt)
1020 {
1021   tree lhs, rhs;
1022   struct access *lacc, *racc;
1023
1024   if (!gimple_assign_single_p (stmt))
1025     return false;
1026
1027   lhs = gimple_assign_lhs (stmt);
1028   rhs = gimple_assign_rhs1 (stmt);
1029
1030   if (disqualify_ops_if_throwing_stmt (stmt, lhs, rhs))
1031     return false;
1032
1033   racc = build_access_from_expr_1 (rhs, stmt, false);
1034   lacc = build_access_from_expr_1 (lhs, stmt, true);
1035
1036   if (lacc)
1037     lacc->grp_assignment_write = 1;
1038
1039   if (racc)
1040     {
1041       racc->grp_assignment_read = 1;
1042       if (should_scalarize_away_bitmap && !gimple_has_volatile_ops (stmt)
1043           && !is_gimple_reg_type (racc->type))
1044         bitmap_set_bit (should_scalarize_away_bitmap, DECL_UID (racc->base));
1045     }
1046
1047   if (lacc && racc
1048       && (sra_mode == SRA_MODE_EARLY_INTRA || sra_mode == SRA_MODE_INTRA)
1049       && !lacc->grp_unscalarizable_region
1050       && !racc->grp_unscalarizable_region
1051       && AGGREGATE_TYPE_P (TREE_TYPE (lhs))
1052       /* FIXME: Turn the following line into an assert after PR 40058 is
1053          fixed.  */
1054       && lacc->size == racc->size
1055       && useless_type_conversion_p (lacc->type, racc->type))
1056     {
1057       struct assign_link *link;
1058
1059       link = (struct assign_link *) pool_alloc (link_pool);
1060       memset (link, 0, sizeof (struct assign_link));
1061
1062       link->lacc = lacc;
1063       link->racc = racc;
1064
1065       add_link_to_rhs (racc, link);
1066     }
1067
1068   return lacc || racc;
1069 }
1070
1071 /* Callback of walk_stmt_load_store_addr_ops visit_addr used to determine
1072    GIMPLE_ASM operands with memory constrains which cannot be scalarized.  */
1073
1074 static bool
1075 asm_visit_addr (gimple stmt ATTRIBUTE_UNUSED, tree op,
1076                 void *data ATTRIBUTE_UNUSED)
1077 {
1078   op = get_base_address (op);
1079   if (op
1080       && DECL_P (op))
1081     disqualify_candidate (op, "Non-scalarizable GIMPLE_ASM operand.");
1082
1083   return false;
1084 }
1085
1086 /* Return true iff callsite CALL has at least as many actual arguments as there
1087    are formal parameters of the function currently processed by IPA-SRA.  */
1088
1089 static inline bool
1090 callsite_has_enough_arguments_p (gimple call)
1091 {
1092   return gimple_call_num_args (call) >= (unsigned) func_param_count;
1093 }
1094
1095 /* Scan function and look for interesting expressions and create access
1096    structures for them.  Return true iff any access is created.  */
1097
1098 static bool
1099 scan_function (void)
1100 {
1101   basic_block bb;
1102   bool ret = false;
1103
1104   FOR_EACH_BB (bb)
1105     {
1106       gimple_stmt_iterator gsi;
1107       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1108         {
1109           gimple stmt = gsi_stmt (gsi);
1110           tree t;
1111           unsigned i;
1112
1113           if (final_bbs && stmt_can_throw_external (stmt))
1114             bitmap_set_bit (final_bbs, bb->index);
1115           switch (gimple_code (stmt))
1116             {
1117             case GIMPLE_RETURN:
1118               t = gimple_return_retval (stmt);
1119               if (t != NULL_TREE)
1120                 ret |= build_access_from_expr (t, stmt, false);
1121               if (final_bbs)
1122                 bitmap_set_bit (final_bbs, bb->index);
1123               break;
1124
1125             case GIMPLE_ASSIGN:
1126               ret |= build_accesses_from_assign (stmt);
1127               break;
1128
1129             case GIMPLE_CALL:
1130               for (i = 0; i < gimple_call_num_args (stmt); i++)
1131                 ret |= build_access_from_expr (gimple_call_arg (stmt, i),
1132                                                stmt, false);
1133
1134               if (sra_mode == SRA_MODE_EARLY_IPA)
1135                 {
1136                   tree dest = gimple_call_fndecl (stmt);
1137                   int flags = gimple_call_flags (stmt);
1138
1139                   if (dest)
1140                     {
1141                       if (DECL_BUILT_IN_CLASS (dest) == BUILT_IN_NORMAL
1142                           && DECL_FUNCTION_CODE (dest) == BUILT_IN_APPLY_ARGS)
1143                         encountered_apply_args = true;
1144                       if (cgraph_get_node (dest)
1145                           == cgraph_get_node (current_function_decl))
1146                         {
1147                           encountered_recursive_call = true;
1148                           if (!callsite_has_enough_arguments_p (stmt))
1149                             encountered_unchangable_recursive_call = true;
1150                         }
1151                     }
1152
1153                   if (final_bbs
1154                       && (flags & (ECF_CONST | ECF_PURE)) == 0)
1155                     bitmap_set_bit (final_bbs, bb->index);
1156                 }
1157
1158               t = gimple_call_lhs (stmt);
1159               if (t && !disqualify_ops_if_throwing_stmt (stmt, t, NULL))
1160                 ret |= build_access_from_expr (t, stmt, true);
1161               break;
1162
1163             case GIMPLE_ASM:
1164               walk_stmt_load_store_addr_ops (stmt, NULL, NULL, NULL,
1165                                              asm_visit_addr);
1166               if (final_bbs)
1167                 bitmap_set_bit (final_bbs, bb->index);
1168
1169               for (i = 0; i < gimple_asm_ninputs (stmt); i++)
1170                 {
1171                   t = TREE_VALUE (gimple_asm_input_op (stmt, i));
1172                   ret |= build_access_from_expr (t, stmt, false);
1173                 }
1174               for (i = 0; i < gimple_asm_noutputs (stmt); i++)
1175                 {
1176                   t = TREE_VALUE (gimple_asm_output_op (stmt, i));
1177                   ret |= build_access_from_expr (t, stmt, true);
1178                 }
1179               break;
1180
1181             default:
1182               break;
1183             }
1184         }
1185     }
1186
1187   return ret;
1188 }
1189
1190 /* Helper of QSORT function. There are pointers to accesses in the array.  An
1191    access is considered smaller than another if it has smaller offset or if the
1192    offsets are the same but is size is bigger. */
1193
1194 static int
1195 compare_access_positions (const void *a, const void *b)
1196 {
1197   const access_p *fp1 = (const access_p *) a;
1198   const access_p *fp2 = (const access_p *) b;
1199   const access_p f1 = *fp1;
1200   const access_p f2 = *fp2;
1201
1202   if (f1->offset != f2->offset)
1203     return f1->offset < f2->offset ? -1 : 1;
1204
1205   if (f1->size == f2->size)
1206     {
1207       if (f1->type == f2->type)
1208         return 0;
1209       /* Put any non-aggregate type before any aggregate type.  */
1210       else if (!is_gimple_reg_type (f1->type)
1211           && is_gimple_reg_type (f2->type))
1212         return 1;
1213       else if (is_gimple_reg_type (f1->type)
1214                && !is_gimple_reg_type (f2->type))
1215         return -1;
1216       /* Put any complex or vector type before any other scalar type.  */
1217       else if (TREE_CODE (f1->type) != COMPLEX_TYPE
1218                && TREE_CODE (f1->type) != VECTOR_TYPE
1219                && (TREE_CODE (f2->type) == COMPLEX_TYPE
1220                    || TREE_CODE (f2->type) == VECTOR_TYPE))
1221         return 1;
1222       else if ((TREE_CODE (f1->type) == COMPLEX_TYPE
1223                 || TREE_CODE (f1->type) == VECTOR_TYPE)
1224                && TREE_CODE (f2->type) != COMPLEX_TYPE
1225                && TREE_CODE (f2->type) != VECTOR_TYPE)
1226         return -1;
1227       /* Put the integral type with the bigger precision first.  */
1228       else if (INTEGRAL_TYPE_P (f1->type)
1229                && INTEGRAL_TYPE_P (f2->type))
1230         return TYPE_PRECISION (f2->type) - TYPE_PRECISION (f1->type);
1231       /* Put any integral type with non-full precision last.  */
1232       else if (INTEGRAL_TYPE_P (f1->type)
1233                && (TREE_INT_CST_LOW (TYPE_SIZE (f1->type))
1234                    != TYPE_PRECISION (f1->type)))
1235         return 1;
1236       else if (INTEGRAL_TYPE_P (f2->type)
1237                && (TREE_INT_CST_LOW (TYPE_SIZE (f2->type))
1238                    != TYPE_PRECISION (f2->type)))
1239         return -1;
1240       /* Stabilize the sort.  */
1241       return TYPE_UID (f1->type) - TYPE_UID (f2->type);
1242     }
1243
1244   /* We want the bigger accesses first, thus the opposite operator in the next
1245      line: */
1246   return f1->size > f2->size ? -1 : 1;
1247 }
1248
1249
1250 /* Append a name of the declaration to the name obstack.  A helper function for
1251    make_fancy_name.  */
1252
1253 static void
1254 make_fancy_decl_name (tree decl)
1255 {
1256   char buffer[32];
1257
1258   tree name = DECL_NAME (decl);
1259   if (name)
1260     obstack_grow (&name_obstack, IDENTIFIER_POINTER (name),
1261                   IDENTIFIER_LENGTH (name));
1262   else
1263     {
1264       sprintf (buffer, "D%u", DECL_UID (decl));
1265       obstack_grow (&name_obstack, buffer, strlen (buffer));
1266     }
1267 }
1268
1269 /* Helper for make_fancy_name.  */
1270
1271 static void
1272 make_fancy_name_1 (tree expr)
1273 {
1274   char buffer[32];
1275   tree index;
1276
1277   if (DECL_P (expr))
1278     {
1279       make_fancy_decl_name (expr);
1280       return;
1281     }
1282
1283   switch (TREE_CODE (expr))
1284     {
1285     case COMPONENT_REF:
1286       make_fancy_name_1 (TREE_OPERAND (expr, 0));
1287       obstack_1grow (&name_obstack, '$');
1288       make_fancy_decl_name (TREE_OPERAND (expr, 1));
1289       break;
1290
1291     case ARRAY_REF:
1292       make_fancy_name_1 (TREE_OPERAND (expr, 0));
1293       obstack_1grow (&name_obstack, '$');
1294       /* Arrays with only one element may not have a constant as their
1295          index. */
1296       index = TREE_OPERAND (expr, 1);
1297       if (TREE_CODE (index) != INTEGER_CST)
1298         break;
1299       sprintf (buffer, HOST_WIDE_INT_PRINT_DEC, TREE_INT_CST_LOW (index));
1300       obstack_grow (&name_obstack, buffer, strlen (buffer));
1301       break;
1302
1303     case ADDR_EXPR:
1304       make_fancy_name_1 (TREE_OPERAND (expr, 0));
1305       break;
1306
1307     case MEM_REF:
1308       make_fancy_name_1 (TREE_OPERAND (expr, 0));
1309       if (!integer_zerop (TREE_OPERAND (expr, 1)))
1310         {
1311           obstack_1grow (&name_obstack, '$');
1312           sprintf (buffer, HOST_WIDE_INT_PRINT_DEC,
1313                    TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)));
1314           obstack_grow (&name_obstack, buffer, strlen (buffer));
1315         }
1316       break;
1317
1318     case BIT_FIELD_REF:
1319     case REALPART_EXPR:
1320     case IMAGPART_EXPR:
1321       gcc_unreachable ();       /* we treat these as scalars.  */
1322       break;
1323     default:
1324       break;
1325     }
1326 }
1327
1328 /* Create a human readable name for replacement variable of ACCESS.  */
1329
1330 static char *
1331 make_fancy_name (tree expr)
1332 {
1333   make_fancy_name_1 (expr);
1334   obstack_1grow (&name_obstack, '\0');
1335   return XOBFINISH (&name_obstack, char *);
1336 }
1337
1338 /* Construct a MEM_REF that would reference a part of aggregate BASE of type
1339    EXP_TYPE at the given OFFSET.  If BASE is something for which
1340    get_addr_base_and_unit_offset returns NULL, gsi must be non-NULL and is used
1341    to insert new statements either before or below the current one as specified
1342    by INSERT_AFTER.  This function is not capable of handling bitfields.  */
1343
1344 tree
1345 build_ref_for_offset (location_t loc, tree base, HOST_WIDE_INT offset,
1346                       tree exp_type, gimple_stmt_iterator *gsi,
1347                       bool insert_after)
1348 {
1349   tree prev_base = base;
1350   tree off;
1351   HOST_WIDE_INT base_offset;
1352
1353   gcc_checking_assert (offset % BITS_PER_UNIT == 0);
1354
1355   base = get_addr_base_and_unit_offset (base, &base_offset);
1356
1357   /* get_addr_base_and_unit_offset returns NULL for references with a variable
1358      offset such as array[var_index].  */
1359   if (!base)
1360     {
1361       gimple stmt;
1362       tree tmp, addr;
1363
1364       gcc_checking_assert (gsi);
1365       tmp = create_tmp_reg (build_pointer_type (TREE_TYPE (prev_base)), NULL);
1366       add_referenced_var (tmp);
1367       tmp = make_ssa_name (tmp, NULL);
1368       addr = build_fold_addr_expr (unshare_expr (prev_base));
1369       stmt = gimple_build_assign (tmp, addr);
1370       gimple_set_location (stmt, loc);
1371       SSA_NAME_DEF_STMT (tmp) = stmt;
1372       if (insert_after)
1373         gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
1374       else
1375         gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1376       update_stmt (stmt);
1377
1378       off = build_int_cst (reference_alias_ptr_type (prev_base),
1379                            offset / BITS_PER_UNIT);
1380       base = tmp;
1381     }
1382   else if (TREE_CODE (base) == MEM_REF)
1383     {
1384       off = build_int_cst (TREE_TYPE (TREE_OPERAND (base, 1)),
1385                            base_offset + offset / BITS_PER_UNIT);
1386       off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1), off, 0);
1387       base = unshare_expr (TREE_OPERAND (base, 0));
1388     }
1389   else
1390     {
1391       off = build_int_cst (reference_alias_ptr_type (base),
1392                            base_offset + offset / BITS_PER_UNIT);
1393       base = build_fold_addr_expr (unshare_expr (base));
1394     }
1395
1396   return fold_build2_loc (loc, MEM_REF, exp_type, base, off);
1397 }
1398
1399 /* Construct a memory reference to a part of an aggregate BASE at the given
1400    OFFSET and of the same type as MODEL.  In case this is a reference to a
1401    component, the function will replicate the last COMPONENT_REF of model's
1402    expr to access it.  GSI and INSERT_AFTER have the same meaning as in
1403    build_ref_for_offset.  */
1404
1405 static tree
1406 build_ref_for_model (location_t loc, tree base, HOST_WIDE_INT offset,
1407                      struct access *model, gimple_stmt_iterator *gsi,
1408                      bool insert_after)
1409 {
1410   if (TREE_CODE (model->expr) == COMPONENT_REF)
1411     {
1412       tree t, exp_type;
1413       offset -= int_bit_position (TREE_OPERAND (model->expr, 1));
1414       exp_type = TREE_TYPE (TREE_OPERAND (model->expr, 0));
1415       t = build_ref_for_offset (loc, base, offset, exp_type, gsi, insert_after);
1416       return fold_build3_loc (loc, COMPONENT_REF, model->type, t,
1417                               TREE_OPERAND (model->expr, 1), NULL_TREE);
1418     }
1419   else
1420     return build_ref_for_offset (loc, base, offset, model->type,
1421                                  gsi, insert_after);
1422 }
1423
1424 /* Construct a memory reference consisting of component_refs and array_refs to
1425    a part of an aggregate *RES (which is of type TYPE).  The requested part
1426    should have type EXP_TYPE at be the given OFFSET.  This function might not
1427    succeed, it returns true when it does and only then *RES points to something
1428    meaningful.  This function should be used only to build expressions that we
1429    might need to present to user (e.g. in warnings).  In all other situations,
1430    build_ref_for_model or build_ref_for_offset should be used instead.  */
1431
1432 static bool
1433 build_user_friendly_ref_for_offset (tree *res, tree type, HOST_WIDE_INT offset,
1434                                     tree exp_type)
1435 {
1436   while (1)
1437     {
1438       tree fld;
1439       tree tr_size, index, minidx;
1440       HOST_WIDE_INT el_size;
1441
1442       if (offset == 0 && exp_type
1443           && types_compatible_p (exp_type, type))
1444         return true;
1445
1446       switch (TREE_CODE (type))
1447         {
1448         case UNION_TYPE:
1449         case QUAL_UNION_TYPE:
1450         case RECORD_TYPE:
1451           for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
1452             {
1453               HOST_WIDE_INT pos, size;
1454               tree expr, *expr_ptr;
1455
1456               if (TREE_CODE (fld) != FIELD_DECL)
1457                 continue;
1458
1459               pos = int_bit_position (fld);
1460               gcc_assert (TREE_CODE (type) == RECORD_TYPE || pos == 0);
1461               tr_size = DECL_SIZE (fld);
1462               if (!tr_size || !host_integerp (tr_size, 1))
1463                 continue;
1464               size = tree_low_cst (tr_size, 1);
1465               if (size == 0)
1466                 {
1467                   if (pos != offset)
1468                     continue;
1469                 }
1470               else if (pos > offset || (pos + size) <= offset)
1471                 continue;
1472
1473               expr = build3 (COMPONENT_REF, TREE_TYPE (fld), *res, fld,
1474                              NULL_TREE);
1475               expr_ptr = &expr;
1476               if (build_user_friendly_ref_for_offset (expr_ptr, TREE_TYPE (fld),
1477                                                       offset - pos, exp_type))
1478                 {
1479                   *res = expr;
1480                   return true;
1481                 }
1482             }
1483           return false;
1484
1485         case ARRAY_TYPE:
1486           tr_size = TYPE_SIZE (TREE_TYPE (type));
1487           if (!tr_size || !host_integerp (tr_size, 1))
1488             return false;
1489           el_size = tree_low_cst (tr_size, 1);
1490
1491           minidx = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
1492           if (TREE_CODE (minidx) != INTEGER_CST || el_size == 0)
1493             return false;
1494           index = build_int_cst (TYPE_DOMAIN (type), offset / el_size);
1495           if (!integer_zerop (minidx))
1496             index = int_const_binop (PLUS_EXPR, index, minidx, 0);
1497           *res = build4 (ARRAY_REF, TREE_TYPE (type), *res, index,
1498                          NULL_TREE, NULL_TREE);
1499           offset = offset % el_size;
1500           type = TREE_TYPE (type);
1501           break;
1502
1503         default:
1504           if (offset != 0)
1505             return false;
1506
1507           if (exp_type)
1508             return false;
1509           else
1510             return true;
1511         }
1512     }
1513 }
1514
1515 /* Return true iff TYPE is stdarg va_list type.  */
1516
1517 static inline bool
1518 is_va_list_type (tree type)
1519 {
1520   return TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (va_list_type_node);
1521 }
1522
1523 /* The very first phase of intraprocedural SRA.  It marks in candidate_bitmap
1524    those with type which is suitable for scalarization.  */
1525
1526 static bool
1527 find_var_candidates (void)
1528 {
1529   tree var, type;
1530   referenced_var_iterator rvi;
1531   bool ret = false;
1532
1533   FOR_EACH_REFERENCED_VAR (var, rvi)
1534     {
1535       if (TREE_CODE (var) != VAR_DECL && TREE_CODE (var) != PARM_DECL)
1536         continue;
1537       type = TREE_TYPE (var);
1538
1539       if (!AGGREGATE_TYPE_P (type)
1540           || needs_to_live_in_memory (var)
1541           || TREE_THIS_VOLATILE (var)
1542           || !COMPLETE_TYPE_P (type)
1543           || !host_integerp (TYPE_SIZE (type), 1)
1544           || tree_low_cst (TYPE_SIZE (type), 1) == 0
1545           || type_internals_preclude_sra_p (type)
1546           /* Fix for PR 41089.  tree-stdarg.c needs to have va_lists intact but
1547               we also want to schedule it rather late.  Thus we ignore it in
1548               the early pass. */
1549           || (sra_mode == SRA_MODE_EARLY_INTRA
1550               && is_va_list_type (type)))
1551         continue;
1552
1553       bitmap_set_bit (candidate_bitmap, DECL_UID (var));
1554
1555       if (dump_file && (dump_flags & TDF_DETAILS))
1556         {
1557           fprintf (dump_file, "Candidate (%d): ", DECL_UID (var));
1558           print_generic_expr (dump_file, var, 0);
1559           fprintf (dump_file, "\n");
1560         }
1561       ret = true;
1562     }
1563
1564   return ret;
1565 }
1566
1567 /* Sort all accesses for the given variable, check for partial overlaps and
1568    return NULL if there are any.  If there are none, pick a representative for
1569    each combination of offset and size and create a linked list out of them.
1570    Return the pointer to the first representative and make sure it is the first
1571    one in the vector of accesses.  */
1572
1573 static struct access *
1574 sort_and_splice_var_accesses (tree var)
1575 {
1576   int i, j, access_count;
1577   struct access *res, **prev_acc_ptr = &res;
1578   VEC (access_p, heap) *access_vec;
1579   bool first = true;
1580   HOST_WIDE_INT low = -1, high = 0;
1581
1582   access_vec = get_base_access_vector (var);
1583   if (!access_vec)
1584     return NULL;
1585   access_count = VEC_length (access_p, access_vec);
1586
1587   /* Sort by <OFFSET, SIZE>.  */
1588   VEC_qsort (access_p, access_vec, compare_access_positions);
1589
1590   i = 0;
1591   while (i < access_count)
1592     {
1593       struct access *access = VEC_index (access_p, access_vec, i);
1594       bool grp_write = access->write;
1595       bool grp_read = !access->write;
1596       bool grp_assignment_read = access->grp_assignment_read;
1597       bool grp_assignment_write = access->grp_assignment_write;
1598       bool multiple_reads = false;
1599       bool total_scalarization = access->total_scalarization;
1600       bool grp_partial_lhs = access->grp_partial_lhs;
1601       bool first_scalar = is_gimple_reg_type (access->type);
1602       bool unscalarizable_region = access->grp_unscalarizable_region;
1603
1604       if (first || access->offset >= high)
1605         {
1606           first = false;
1607           low = access->offset;
1608           high = access->offset + access->size;
1609         }
1610       else if (access->offset > low && access->offset + access->size > high)
1611         return NULL;
1612       else
1613         gcc_assert (access->offset >= low
1614                     && access->offset + access->size <= high);
1615
1616       j = i + 1;
1617       while (j < access_count)
1618         {
1619           struct access *ac2 = VEC_index (access_p, access_vec, j);
1620           if (ac2->offset != access->offset || ac2->size != access->size)
1621             break;
1622           if (ac2->write)
1623             grp_write = true;
1624           else
1625             {
1626               if (grp_read)
1627                 multiple_reads = true;
1628               else
1629                 grp_read = true;
1630             }
1631           grp_assignment_read |= ac2->grp_assignment_read;
1632           grp_assignment_write |= ac2->grp_assignment_write;
1633           grp_partial_lhs |= ac2->grp_partial_lhs;
1634           unscalarizable_region |= ac2->grp_unscalarizable_region;
1635           total_scalarization |= ac2->total_scalarization;
1636           relink_to_new_repr (access, ac2);
1637
1638           /* If there are both aggregate-type and scalar-type accesses with
1639              this combination of size and offset, the comparison function
1640              should have put the scalars first.  */
1641           gcc_assert (first_scalar || !is_gimple_reg_type (ac2->type));
1642           ac2->group_representative = access;
1643           j++;
1644         }
1645
1646       i = j;
1647
1648       access->group_representative = access;
1649       access->grp_write = grp_write;
1650       access->grp_read = grp_read;
1651       access->grp_assignment_read = grp_assignment_read;
1652       access->grp_assignment_write = grp_assignment_write;
1653       access->grp_hint = multiple_reads || total_scalarization;
1654       access->grp_partial_lhs = grp_partial_lhs;
1655       access->grp_unscalarizable_region = unscalarizable_region;
1656       if (access->first_link)
1657         add_access_to_work_queue (access);
1658
1659       *prev_acc_ptr = access;
1660       prev_acc_ptr = &access->next_grp;
1661     }
1662
1663   gcc_assert (res == VEC_index (access_p, access_vec, 0));
1664   return res;
1665 }
1666
1667 /* Create a variable for the given ACCESS which determines the type, name and a
1668    few other properties.  Return the variable declaration and store it also to
1669    ACCESS->replacement.  */
1670
1671 static tree
1672 create_access_replacement (struct access *access, bool rename)
1673 {
1674   tree repl;
1675
1676   repl = create_tmp_var (access->type, "SR");
1677   get_var_ann (repl);
1678   add_referenced_var (repl);
1679   if (rename)
1680     mark_sym_for_renaming (repl);
1681
1682   if (!access->grp_partial_lhs
1683       && (TREE_CODE (access->type) == COMPLEX_TYPE
1684           || TREE_CODE (access->type) == VECTOR_TYPE))
1685     DECL_GIMPLE_REG_P (repl) = 1;
1686
1687   DECL_SOURCE_LOCATION (repl) = DECL_SOURCE_LOCATION (access->base);
1688   DECL_ARTIFICIAL (repl) = 1;
1689   DECL_IGNORED_P (repl) = DECL_IGNORED_P (access->base);
1690
1691   if (DECL_NAME (access->base)
1692       && !DECL_IGNORED_P (access->base)
1693       && !DECL_ARTIFICIAL (access->base))
1694     {
1695       char *pretty_name = make_fancy_name (access->expr);
1696       tree debug_expr = unshare_expr (access->expr), d;
1697
1698       DECL_NAME (repl) = get_identifier (pretty_name);
1699       obstack_free (&name_obstack, pretty_name);
1700
1701       /* Get rid of any SSA_NAMEs embedded in debug_expr,
1702          as DECL_DEBUG_EXPR isn't considered when looking for still
1703          used SSA_NAMEs and thus they could be freed.  All debug info
1704          generation cares is whether something is constant or variable
1705          and that get_ref_base_and_extent works properly on the
1706          expression.  */
1707       for (d = debug_expr; handled_component_p (d); d = TREE_OPERAND (d, 0))
1708         switch (TREE_CODE (d))
1709           {
1710           case ARRAY_REF:
1711           case ARRAY_RANGE_REF:
1712             if (TREE_OPERAND (d, 1)
1713                 && TREE_CODE (TREE_OPERAND (d, 1)) == SSA_NAME)
1714               TREE_OPERAND (d, 1) = SSA_NAME_VAR (TREE_OPERAND (d, 1));
1715             if (TREE_OPERAND (d, 3)
1716                 && TREE_CODE (TREE_OPERAND (d, 3)) == SSA_NAME)
1717               TREE_OPERAND (d, 3) = SSA_NAME_VAR (TREE_OPERAND (d, 3));
1718             /* FALLTHRU */
1719           case COMPONENT_REF:
1720             if (TREE_OPERAND (d, 2)
1721                 && TREE_CODE (TREE_OPERAND (d, 2)) == SSA_NAME)
1722               TREE_OPERAND (d, 2) = SSA_NAME_VAR (TREE_OPERAND (d, 2));
1723             break;
1724           default:
1725             break;
1726           }
1727       SET_DECL_DEBUG_EXPR (repl, debug_expr);
1728       DECL_DEBUG_EXPR_IS_FROM (repl) = 1;
1729       if (access->grp_no_warning)
1730         TREE_NO_WARNING (repl) = 1;
1731       else
1732         TREE_NO_WARNING (repl) = TREE_NO_WARNING (access->base);
1733     }
1734   else
1735     TREE_NO_WARNING (repl) = 1;
1736
1737   if (dump_file)
1738     {
1739       fprintf (dump_file, "Created a replacement for ");
1740       print_generic_expr (dump_file, access->base, 0);
1741       fprintf (dump_file, " offset: %u, size: %u: ",
1742                (unsigned) access->offset, (unsigned) access->size);
1743       print_generic_expr (dump_file, repl, 0);
1744       fprintf (dump_file, "\n");
1745     }
1746   sra_stats.replacements++;
1747
1748   return repl;
1749 }
1750
1751 /* Return ACCESS scalar replacement, create it if it does not exist yet.  */
1752
1753 static inline tree
1754 get_access_replacement (struct access *access)
1755 {
1756   gcc_assert (access->grp_to_be_replaced);
1757
1758   if (!access->replacement_decl)
1759     access->replacement_decl = create_access_replacement (access, true);
1760   return access->replacement_decl;
1761 }
1762
1763 /* Return ACCESS scalar replacement, create it if it does not exist yet but do
1764    not mark it for renaming.  */
1765
1766 static inline tree
1767 get_unrenamed_access_replacement (struct access *access)
1768 {
1769   gcc_assert (!access->grp_to_be_replaced);
1770
1771   if (!access->replacement_decl)
1772     access->replacement_decl = create_access_replacement (access, false);
1773   return access->replacement_decl;
1774 }
1775
1776
1777 /* Build a subtree of accesses rooted in *ACCESS, and move the pointer in the
1778    linked list along the way.  Stop when *ACCESS is NULL or the access pointed
1779    to it is not "within" the root.  Return false iff some accesses partially
1780    overlap.  */
1781
1782 static bool
1783 build_access_subtree (struct access **access)
1784 {
1785   struct access *root = *access, *last_child = NULL;
1786   HOST_WIDE_INT limit = root->offset + root->size;
1787
1788   *access = (*access)->next_grp;
1789   while  (*access && (*access)->offset + (*access)->size <= limit)
1790     {
1791       if (!last_child)
1792         root->first_child = *access;
1793       else
1794         last_child->next_sibling = *access;
1795       last_child = *access;
1796
1797       if (!build_access_subtree (access))
1798         return false;
1799     }
1800
1801   if (*access && (*access)->offset < limit)
1802     return false;
1803
1804   return true;
1805 }
1806
1807 /* Build a tree of access representatives, ACCESS is the pointer to the first
1808    one, others are linked in a list by the next_grp field.  Return false iff
1809    some accesses partially overlap.  */
1810
1811 static bool
1812 build_access_trees (struct access *access)
1813 {
1814   while (access)
1815     {
1816       struct access *root = access;
1817
1818       if (!build_access_subtree (&access))
1819         return false;
1820       root->next_grp = access;
1821     }
1822   return true;
1823 }
1824
1825 /* Return true if expr contains some ARRAY_REFs into a variable bounded
1826    array.  */
1827
1828 static bool
1829 expr_with_var_bounded_array_refs_p (tree expr)
1830 {
1831   while (handled_component_p (expr))
1832     {
1833       if (TREE_CODE (expr) == ARRAY_REF
1834           && !host_integerp (array_ref_low_bound (expr), 0))
1835         return true;
1836       expr = TREE_OPERAND (expr, 0);
1837     }
1838   return false;
1839 }
1840
1841 enum mark_rw_status { SRA_MRRW_NOTHING, SRA_MRRW_DIRECT, SRA_MRRW_ASSIGN};
1842
1843 /* Analyze the subtree of accesses rooted in ROOT, scheduling replacements when
1844    both seeming beneficial and when ALLOW_REPLACEMENTS allows it.  Also set all
1845    sorts of access flags appropriately along the way, notably always set
1846    grp_read and grp_assign_read according to MARK_READ and grp_write when
1847    MARK_WRITE is true.
1848
1849    Creating a replacement for a scalar access is considered beneficial if its
1850    grp_hint is set (this means we are either attempting total scalarization or
1851    there is more than one direct read access) or according to the following
1852    table:
1853
1854    Access written to individually (once or more times)
1855    |
1856    |    Parent written to in an assignment statement
1857    |    |
1858    |    |       Access read individually _once_
1859    |    |       |
1860    |    |       |       Parent read in an assignment statement
1861    |    |       |       |
1862    |    |       |       |       Scalarize       Comment
1863 -----------------------------------------------------------------------------
1864    0    0       0       0                       No access for the scalar
1865    0    0       0       1                       No access for the scalar
1866    0    0       1       0       No              Single read - won't help
1867    0    0       1       1       No              The same case
1868    0    1       0       0                       No access for the scalar
1869    0    1       0       1                       No access for the scalar
1870    0    1       1       0       Yes             s = *g; return s.i;
1871    0    1       1       1       Yes             The same case as above
1872    1    0       0       0       No              Won't help
1873    1    0       0       1       Yes             s.i = 1; *g = s;
1874    1    0       1       0       Yes             s.i = 5; g = s.i;
1875    1    0       1       1       Yes             The same case as above
1876    1    1       0       0       No              Won't help.
1877    1    1       0       1       Yes             s.i = 1; *g = s;
1878    1    1       1       0       Yes             s = *g; return s.i;
1879    1    1       1       1       Yes             Any of the above yeses  */
1880
1881 static bool
1882 analyze_access_subtree (struct access *root, bool allow_replacements,
1883                         enum mark_rw_status mark_read,
1884                         enum mark_rw_status mark_write)
1885 {
1886   struct access *child;
1887   HOST_WIDE_INT limit = root->offset + root->size;
1888   HOST_WIDE_INT covered_to = root->offset;
1889   bool scalar = is_gimple_reg_type (root->type);
1890   bool hole = false, sth_created = false;
1891   bool direct_read = root->grp_read;
1892   bool direct_write = root->grp_write;
1893
1894   if (root->grp_assignment_read)
1895     mark_read = SRA_MRRW_ASSIGN;
1896   else if (mark_read == SRA_MRRW_ASSIGN)
1897     {
1898       root->grp_read = 1;
1899       root->grp_assignment_read = 1;
1900     }
1901   else if (mark_read == SRA_MRRW_DIRECT)
1902     root->grp_read = 1;
1903   else if (root->grp_read)
1904     mark_read = SRA_MRRW_DIRECT;
1905
1906   if (root->grp_assignment_write)
1907     mark_write = SRA_MRRW_ASSIGN;
1908   else if (mark_write == SRA_MRRW_ASSIGN)
1909     {
1910       root->grp_write = 1;
1911       root->grp_assignment_write = 1;
1912     }
1913   else if (mark_write == SRA_MRRW_DIRECT)
1914     root->grp_write = 1;
1915   else if (root->grp_write)
1916     mark_write = SRA_MRRW_DIRECT;
1917
1918   if (root->grp_unscalarizable_region)
1919     allow_replacements = false;
1920
1921   if (allow_replacements && expr_with_var_bounded_array_refs_p (root->expr))
1922     allow_replacements = false;
1923
1924   for (child = root->first_child; child; child = child->next_sibling)
1925     {
1926       if (!hole && child->offset < covered_to)
1927         hole = true;
1928       else
1929         covered_to += child->size;
1930
1931       sth_created |= analyze_access_subtree (child,
1932                                              allow_replacements && !scalar,
1933                                              mark_read, mark_write);
1934
1935       root->grp_unscalarized_data |= child->grp_unscalarized_data;
1936       hole |= !child->grp_covered;
1937     }
1938
1939   if (allow_replacements && scalar && !root->first_child
1940       && (root->grp_hint
1941           || ((direct_write || root->grp_assignment_write)
1942               && (direct_read || root->grp_assignment_read))))
1943     {
1944       if (dump_file && (dump_flags & TDF_DETAILS))
1945         {
1946           fprintf (dump_file, "Marking ");
1947           print_generic_expr (dump_file, root->base, 0);
1948           fprintf (dump_file, " offset: %u, size: %u: ",
1949                    (unsigned) root->offset, (unsigned) root->size);
1950           fprintf (dump_file, " to be replaced.\n");
1951         }
1952
1953       root->grp_to_be_replaced = 1;
1954       sth_created = true;
1955       hole = false;
1956     }
1957   else if (covered_to < limit)
1958     hole = true;
1959
1960   if (sth_created && !hole)
1961     {
1962       root->grp_covered = 1;
1963       return true;
1964     }
1965   if (root->grp_write || TREE_CODE (root->base) == PARM_DECL)
1966     root->grp_unscalarized_data = 1; /* not covered and written to */
1967   if (sth_created)
1968     return true;
1969   return false;
1970 }
1971
1972 /* Analyze all access trees linked by next_grp by the means of
1973    analyze_access_subtree.  */
1974 static bool
1975 analyze_access_trees (struct access *access)
1976 {
1977   bool ret = false;
1978
1979   while (access)
1980     {
1981       if (analyze_access_subtree (access, true,
1982                                   SRA_MRRW_NOTHING, SRA_MRRW_NOTHING))
1983         ret = true;
1984       access = access->next_grp;
1985     }
1986
1987   return ret;
1988 }
1989
1990 /* Return true iff a potential new child of LACC at offset OFFSET and with size
1991    SIZE would conflict with an already existing one.  If exactly such a child
1992    already exists in LACC, store a pointer to it in EXACT_MATCH.  */
1993
1994 static bool
1995 child_would_conflict_in_lacc (struct access *lacc, HOST_WIDE_INT norm_offset,
1996                               HOST_WIDE_INT size, struct access **exact_match)
1997 {
1998   struct access *child;
1999
2000   for (child = lacc->first_child; child; child = child->next_sibling)
2001     {
2002       if (child->offset == norm_offset && child->size == size)
2003         {
2004           *exact_match = child;
2005           return true;
2006         }
2007
2008       if (child->offset < norm_offset + size
2009           && child->offset + child->size > norm_offset)
2010         return true;
2011     }
2012
2013   return false;
2014 }
2015
2016 /* Create a new child access of PARENT, with all properties just like MODEL
2017    except for its offset and with its grp_write false and grp_read true.
2018    Return the new access or NULL if it cannot be created.  Note that this access
2019    is created long after all splicing and sorting, it's not located in any
2020    access vector and is automatically a representative of its group.  */
2021
2022 static struct access *
2023 create_artificial_child_access (struct access *parent, struct access *model,
2024                                 HOST_WIDE_INT new_offset)
2025 {
2026   struct access *access;
2027   struct access **child;
2028   tree expr = parent->base;
2029
2030   gcc_assert (!model->grp_unscalarizable_region);
2031
2032   access = (struct access *) pool_alloc (access_pool);
2033   memset (access, 0, sizeof (struct access));
2034   if (!build_user_friendly_ref_for_offset (&expr, TREE_TYPE (expr), new_offset,
2035                                            model->type))
2036     {
2037       access->grp_no_warning = true;
2038       expr = build_ref_for_model (EXPR_LOCATION (parent->base), parent->base,
2039                                   new_offset, model, NULL, false);
2040     }
2041
2042   access->base = parent->base;
2043   access->expr = expr;
2044   access->offset = new_offset;
2045   access->size = model->size;
2046   access->type = model->type;
2047   access->grp_write = true;
2048   access->grp_read = false;
2049
2050   child = &parent->first_child;
2051   while (*child && (*child)->offset < new_offset)
2052     child = &(*child)->next_sibling;
2053
2054   access->next_sibling = *child;
2055   *child = access;
2056
2057   return access;
2058 }
2059
2060
2061 /* Propagate all subaccesses of RACC across an assignment link to LACC. Return
2062    true if any new subaccess was created.  Additionally, if RACC is a scalar
2063    access but LACC is not, change the type of the latter, if possible.  */
2064
2065 static bool
2066 propagate_subaccesses_across_link (struct access *lacc, struct access *racc)
2067 {
2068   struct access *rchild;
2069   HOST_WIDE_INT norm_delta = lacc->offset - racc->offset;
2070   bool ret = false;
2071
2072   if (is_gimple_reg_type (lacc->type)
2073       || lacc->grp_unscalarizable_region
2074       || racc->grp_unscalarizable_region)
2075     return false;
2076
2077   if (!lacc->first_child && !racc->first_child
2078       && is_gimple_reg_type (racc->type))
2079     {
2080       tree t = lacc->base;
2081
2082       lacc->type = racc->type;
2083       if (build_user_friendly_ref_for_offset (&t, TREE_TYPE (t), lacc->offset,
2084                                               racc->type))
2085         lacc->expr = t;
2086       else
2087         {
2088           lacc->expr = build_ref_for_model (EXPR_LOCATION (lacc->base),
2089                                             lacc->base, lacc->offset,
2090                                             racc, NULL, false);
2091           lacc->grp_no_warning = true;
2092         }
2093       return false;
2094     }
2095
2096   for (rchild = racc->first_child; rchild; rchild = rchild->next_sibling)
2097     {
2098       struct access *new_acc = NULL;
2099       HOST_WIDE_INT norm_offset = rchild->offset + norm_delta;
2100
2101       if (rchild->grp_unscalarizable_region)
2102         continue;
2103
2104       if (child_would_conflict_in_lacc (lacc, norm_offset, rchild->size,
2105                                         &new_acc))
2106         {
2107           if (new_acc)
2108             {
2109               rchild->grp_hint = 1;
2110               new_acc->grp_hint |= new_acc->grp_read;
2111               if (rchild->first_child)
2112                 ret |= propagate_subaccesses_across_link (new_acc, rchild);
2113             }
2114           continue;
2115         }
2116
2117       rchild->grp_hint = 1;
2118       new_acc = create_artificial_child_access (lacc, rchild, norm_offset);
2119       if (new_acc)
2120         {
2121           ret = true;
2122           if (racc->first_child)
2123             propagate_subaccesses_across_link (new_acc, rchild);
2124         }
2125     }
2126
2127   return ret;
2128 }
2129
2130 /* Propagate all subaccesses across assignment links.  */
2131
2132 static void
2133 propagate_all_subaccesses (void)
2134 {
2135   while (work_queue_head)
2136     {
2137       struct access *racc = pop_access_from_work_queue ();
2138       struct assign_link *link;
2139
2140       gcc_assert (racc->first_link);
2141
2142       for (link = racc->first_link; link; link = link->next)
2143         {
2144           struct access *lacc = link->lacc;
2145
2146           if (!bitmap_bit_p (candidate_bitmap, DECL_UID (lacc->base)))
2147             continue;
2148           lacc = lacc->group_representative;
2149           if (propagate_subaccesses_across_link (lacc, racc)
2150               && lacc->first_link)
2151             add_access_to_work_queue (lacc);
2152         }
2153     }
2154 }
2155
2156 /* Go through all accesses collected throughout the (intraprocedural) analysis
2157    stage, exclude overlapping ones, identify representatives and build trees
2158    out of them, making decisions about scalarization on the way.  Return true
2159    iff there are any to-be-scalarized variables after this stage. */
2160
2161 static bool
2162 analyze_all_variable_accesses (void)
2163 {
2164   int res = 0;
2165   bitmap tmp = BITMAP_ALLOC (NULL);
2166   bitmap_iterator bi;
2167   unsigned i, max_total_scalarization_size;
2168
2169   max_total_scalarization_size = UNITS_PER_WORD * BITS_PER_UNIT
2170     * MOVE_RATIO (optimize_function_for_speed_p (cfun));
2171
2172   EXECUTE_IF_SET_IN_BITMAP (candidate_bitmap, 0, i, bi)
2173     if (bitmap_bit_p (should_scalarize_away_bitmap, i)
2174         && !bitmap_bit_p (cannot_scalarize_away_bitmap, i))
2175       {
2176         tree var = referenced_var (i);
2177
2178         if (TREE_CODE (var) == VAR_DECL
2179             && ((unsigned) tree_low_cst (TYPE_SIZE (TREE_TYPE (var)), 1)
2180                 <= max_total_scalarization_size)
2181             && type_consists_of_records_p (TREE_TYPE (var)))
2182           {
2183             completely_scalarize_record (var, var, 0, var);
2184             if (dump_file && (dump_flags & TDF_DETAILS))
2185               {
2186                 fprintf (dump_file, "Will attempt to totally scalarize ");
2187                 print_generic_expr (dump_file, var, 0);
2188                 fprintf (dump_file, " (UID: %u): \n", DECL_UID (var));
2189               }
2190           }
2191       }
2192
2193   bitmap_copy (tmp, candidate_bitmap);
2194   EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
2195     {
2196       tree var = referenced_var (i);
2197       struct access *access;
2198
2199       access = sort_and_splice_var_accesses (var);
2200       if (!access || !build_access_trees (access))
2201         disqualify_candidate (var,
2202                               "No or inhibitingly overlapping accesses.");
2203     }
2204
2205   propagate_all_subaccesses ();
2206
2207   bitmap_copy (tmp, candidate_bitmap);
2208   EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
2209     {
2210       tree var = referenced_var (i);
2211       struct access *access = get_first_repr_for_decl (var);
2212
2213       if (analyze_access_trees (access))
2214         {
2215           res++;
2216           if (dump_file && (dump_flags & TDF_DETAILS))
2217             {
2218               fprintf (dump_file, "\nAccess trees for ");
2219               print_generic_expr (dump_file, var, 0);
2220               fprintf (dump_file, " (UID: %u): \n", DECL_UID (var));
2221               dump_access_tree (dump_file, access);
2222               fprintf (dump_file, "\n");
2223             }
2224         }
2225       else
2226         disqualify_candidate (var, "No scalar replacements to be created.");
2227     }
2228
2229   BITMAP_FREE (tmp);
2230
2231   if (res)
2232     {
2233       statistics_counter_event (cfun, "Scalarized aggregates", res);
2234       return true;
2235     }
2236   else
2237     return false;
2238 }
2239
2240 /* Generate statements copying scalar replacements of accesses within a subtree
2241    into or out of AGG.  ACCESS, all its children, siblings and their children
2242    are to be processed.  AGG is an aggregate type expression (can be a
2243    declaration but does not have to be, it can for example also be a mem_ref or
2244    a series of handled components).  TOP_OFFSET is the offset of the processed
2245    subtree which has to be subtracted from offsets of individual accesses to
2246    get corresponding offsets for AGG.  If CHUNK_SIZE is non-null, copy only
2247    replacements in the interval <start_offset, start_offset + chunk_size>,
2248    otherwise copy all.  GSI is a statement iterator used to place the new
2249    statements.  WRITE should be true when the statements should write from AGG
2250    to the replacement and false if vice versa.  if INSERT_AFTER is true, new
2251    statements will be added after the current statement in GSI, they will be
2252    added before the statement otherwise.  */
2253
2254 static void
2255 generate_subtree_copies (struct access *access, tree agg,
2256                          HOST_WIDE_INT top_offset,
2257                          HOST_WIDE_INT start_offset, HOST_WIDE_INT chunk_size,
2258                          gimple_stmt_iterator *gsi, bool write,
2259                          bool insert_after, location_t loc)
2260 {
2261   do
2262     {
2263       if (chunk_size && access->offset >= start_offset + chunk_size)
2264         return;
2265
2266       if (access->grp_to_be_replaced
2267           && (chunk_size == 0
2268               || access->offset + access->size > start_offset))
2269         {
2270           tree expr, repl = get_access_replacement (access);
2271           gimple stmt;
2272
2273           expr = build_ref_for_model (loc, agg, access->offset - top_offset,
2274                                       access, gsi, insert_after);
2275
2276           if (write)
2277             {
2278               if (access->grp_partial_lhs)
2279                 expr = force_gimple_operand_gsi (gsi, expr, true, NULL_TREE,
2280                                                  !insert_after,
2281                                                  insert_after ? GSI_NEW_STMT
2282                                                  : GSI_SAME_STMT);
2283               stmt = gimple_build_assign (repl, expr);
2284             }
2285           else
2286             {
2287               TREE_NO_WARNING (repl) = 1;
2288               if (access->grp_partial_lhs)
2289                 repl = force_gimple_operand_gsi (gsi, repl, true, NULL_TREE,
2290                                                  !insert_after,
2291                                                  insert_after ? GSI_NEW_STMT
2292                                                  : GSI_SAME_STMT);
2293               stmt = gimple_build_assign (expr, repl);
2294             }
2295           gimple_set_location (stmt, loc);
2296
2297           if (insert_after)
2298             gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
2299           else
2300             gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2301           update_stmt (stmt);
2302           sra_stats.subtree_copies++;
2303         }
2304
2305       if (access->first_child)
2306         generate_subtree_copies (access->first_child, agg, top_offset,
2307                                  start_offset, chunk_size, gsi,
2308                                  write, insert_after, loc);
2309
2310       access = access->next_sibling;
2311     }
2312   while (access);
2313 }
2314
2315 /* Assign zero to all scalar replacements in an access subtree.  ACCESS is the
2316    the root of the subtree to be processed.  GSI is the statement iterator used
2317    for inserting statements which are added after the current statement if
2318    INSERT_AFTER is true or before it otherwise.  */
2319
2320 static void
2321 init_subtree_with_zero (struct access *access, gimple_stmt_iterator *gsi,
2322                         bool insert_after, location_t loc)
2323
2324 {
2325   struct access *child;
2326
2327   if (access->grp_to_be_replaced)
2328     {
2329       gimple stmt;
2330
2331       stmt = gimple_build_assign (get_access_replacement (access),
2332                                   build_zero_cst (access->type));
2333       if (insert_after)
2334         gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
2335       else
2336         gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2337       update_stmt (stmt);
2338       gimple_set_location (stmt, loc);
2339     }
2340
2341   for (child = access->first_child; child; child = child->next_sibling)
2342     init_subtree_with_zero (child, gsi, insert_after, loc);
2343 }
2344
2345 /* Search for an access representative for the given expression EXPR and
2346    return it or NULL if it cannot be found.  */
2347
2348 static struct access *
2349 get_access_for_expr (tree expr)
2350 {
2351   HOST_WIDE_INT offset, size, max_size;
2352   tree base;
2353
2354   /* FIXME: This should not be necessary but Ada produces V_C_Es with a type of
2355      a different size than the size of its argument and we need the latter
2356      one.  */
2357   if (TREE_CODE (expr) == VIEW_CONVERT_EXPR)
2358     expr = TREE_OPERAND (expr, 0);
2359
2360   base = get_ref_base_and_extent (expr, &offset, &size, &max_size);
2361   if (max_size == -1 || !DECL_P (base))
2362     return NULL;
2363
2364   if (!bitmap_bit_p (candidate_bitmap, DECL_UID (base)))
2365     return NULL;
2366
2367   return get_var_base_offset_size_access (base, offset, max_size);
2368 }
2369
2370 /* Replace the expression EXPR with a scalar replacement if there is one and
2371    generate other statements to do type conversion or subtree copying if
2372    necessary.  GSI is used to place newly created statements, WRITE is true if
2373    the expression is being written to (it is on a LHS of a statement or output
2374    in an assembly statement).  */
2375
2376 static bool
2377 sra_modify_expr (tree *expr, gimple_stmt_iterator *gsi, bool write)
2378 {
2379   location_t loc;
2380   struct access *access;
2381   tree type, bfr;
2382
2383   if (TREE_CODE (*expr) == BIT_FIELD_REF)
2384     {
2385       bfr = *expr;
2386       expr = &TREE_OPERAND (*expr, 0);
2387     }
2388   else
2389     bfr = NULL_TREE;
2390
2391   if (TREE_CODE (*expr) == REALPART_EXPR || TREE_CODE (*expr) == IMAGPART_EXPR)
2392     expr = &TREE_OPERAND (*expr, 0);
2393   access = get_access_for_expr (*expr);
2394   if (!access)
2395     return false;
2396   type = TREE_TYPE (*expr);
2397
2398   loc = gimple_location (gsi_stmt (*gsi));
2399   if (access->grp_to_be_replaced)
2400     {
2401       tree repl = get_access_replacement (access);
2402       /* If we replace a non-register typed access simply use the original
2403          access expression to extract the scalar component afterwards.
2404          This happens if scalarizing a function return value or parameter
2405          like in gcc.c-torture/execute/20041124-1.c, 20050316-1.c and
2406          gcc.c-torture/compile/20011217-1.c.
2407
2408          We also want to use this when accessing a complex or vector which can
2409          be accessed as a different type too, potentially creating a need for
2410          type conversion (see PR42196) and when scalarized unions are involved
2411          in assembler statements (see PR42398).  */
2412       if (!useless_type_conversion_p (type, access->type))
2413         {
2414           tree ref;
2415
2416           ref = build_ref_for_model (loc, access->base, access->offset, access,
2417                                      NULL, false);
2418
2419           if (write)
2420             {
2421               gimple stmt;
2422
2423               if (access->grp_partial_lhs)
2424                 ref = force_gimple_operand_gsi (gsi, ref, true, NULL_TREE,
2425                                                  false, GSI_NEW_STMT);
2426               stmt = gimple_build_assign (repl, ref);
2427               gimple_set_location (stmt, loc);
2428               gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
2429             }
2430           else
2431             {
2432               gimple stmt;
2433
2434               if (access->grp_partial_lhs)
2435                 repl = force_gimple_operand_gsi (gsi, repl, true, NULL_TREE,
2436                                                  true, GSI_SAME_STMT);
2437               stmt = gimple_build_assign (ref, repl);
2438               gimple_set_location (stmt, loc);
2439               gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
2440             }
2441         }
2442       else
2443         *expr = repl;
2444       sra_stats.exprs++;
2445     }
2446
2447   if (access->first_child)
2448     {
2449       HOST_WIDE_INT start_offset, chunk_size;
2450       if (bfr
2451           && host_integerp (TREE_OPERAND (bfr, 1), 1)
2452           && host_integerp (TREE_OPERAND (bfr, 2), 1))
2453         {
2454           chunk_size = tree_low_cst (TREE_OPERAND (bfr, 1), 1);
2455           start_offset = access->offset
2456             + tree_low_cst (TREE_OPERAND (bfr, 2), 1);
2457         }
2458       else
2459         start_offset = chunk_size = 0;
2460
2461       generate_subtree_copies (access->first_child, access->base, 0,
2462                                start_offset, chunk_size, gsi, write, write,
2463                                loc);
2464     }
2465   return true;
2466 }
2467
2468 /* Where scalar replacements of the RHS have been written to when a replacement
2469    of a LHS of an assigments cannot be direclty loaded from a replacement of
2470    the RHS. */
2471 enum unscalarized_data_handling { SRA_UDH_NONE,  /* Nothing done so far. */
2472                                   SRA_UDH_RIGHT, /* Data flushed to the RHS. */
2473                                   SRA_UDH_LEFT }; /* Data flushed to the LHS. */
2474
2475 /* Store all replacements in the access tree rooted in TOP_RACC either to their
2476    base aggregate if there are unscalarized data or directly to LHS of the
2477    statement that is pointed to by GSI otherwise.  */
2478
2479 static enum unscalarized_data_handling
2480 handle_unscalarized_data_in_subtree (struct access *top_racc,
2481                                      gimple_stmt_iterator *gsi)
2482 {
2483   if (top_racc->grp_unscalarized_data)
2484     {
2485       generate_subtree_copies (top_racc->first_child, top_racc->base, 0, 0, 0,
2486                                gsi, false, false,
2487                                gimple_location (gsi_stmt (*gsi)));
2488       return SRA_UDH_RIGHT;
2489     }
2490   else
2491     {
2492       tree lhs = gimple_assign_lhs (gsi_stmt (*gsi));
2493       generate_subtree_copies (top_racc->first_child, lhs, top_racc->offset,
2494                                0, 0, gsi, false, false,
2495                                gimple_location (gsi_stmt (*gsi)));
2496       return SRA_UDH_LEFT;
2497     }
2498 }
2499
2500
2501 /* Try to generate statements to load all sub-replacements in an access subtree
2502    formed by children of LACC from scalar replacements in the TOP_RACC subtree.
2503    If that is not possible, refresh the TOP_RACC base aggregate and load the
2504    accesses from it.  LEFT_OFFSET is the offset of the left whole subtree being
2505    copied. NEW_GSI is stmt iterator used for statement insertions after the
2506    original assignment, OLD_GSI is used to insert statements before the
2507    assignment.  *REFRESHED keeps the information whether we have needed to
2508    refresh replacements of the LHS and from which side of the assignments this
2509    takes place.  */
2510
2511 static void
2512 load_assign_lhs_subreplacements (struct access *lacc, struct access *top_racc,
2513                                  HOST_WIDE_INT left_offset,
2514                                  gimple_stmt_iterator *old_gsi,
2515                                  gimple_stmt_iterator *new_gsi,
2516                                  enum unscalarized_data_handling *refreshed)
2517 {
2518   location_t loc = gimple_location (gsi_stmt (*old_gsi));
2519   for (lacc = lacc->first_child; lacc; lacc = lacc->next_sibling)
2520     {
2521       if (lacc->grp_to_be_replaced)
2522         {
2523           struct access *racc;
2524           HOST_WIDE_INT offset = lacc->offset - left_offset + top_racc->offset;
2525           gimple stmt;
2526           tree rhs;
2527
2528           racc = find_access_in_subtree (top_racc, offset, lacc->size);
2529           if (racc && racc->grp_to_be_replaced)
2530             {
2531               rhs = get_access_replacement (racc);
2532               if (!useless_type_conversion_p (lacc->type, racc->type))
2533                 rhs = fold_build1_loc (loc, VIEW_CONVERT_EXPR, lacc->type, rhs);
2534             }
2535           else
2536             {
2537               /* No suitable access on the right hand side, need to load from
2538                  the aggregate.  See if we have to update it first... */
2539               if (*refreshed == SRA_UDH_NONE)
2540                 *refreshed = handle_unscalarized_data_in_subtree (top_racc,
2541                                                                   old_gsi);
2542
2543               if (*refreshed == SRA_UDH_LEFT)
2544                 rhs = build_ref_for_model (loc, lacc->base, lacc->offset, lacc,
2545                                             new_gsi, true);
2546               else
2547                 rhs = build_ref_for_model (loc, top_racc->base, offset, lacc,
2548                                             new_gsi, true);
2549             }
2550
2551           stmt = gimple_build_assign (get_access_replacement (lacc), rhs);
2552           gsi_insert_after (new_gsi, stmt, GSI_NEW_STMT);
2553           gimple_set_location (stmt, loc);
2554           update_stmt (stmt);
2555           sra_stats.subreplacements++;
2556         }
2557       else if (*refreshed == SRA_UDH_NONE
2558                && lacc->grp_read && !lacc->grp_covered)
2559         *refreshed = handle_unscalarized_data_in_subtree (top_racc,
2560                                                           old_gsi);
2561
2562       if (lacc->first_child)
2563         load_assign_lhs_subreplacements (lacc, top_racc, left_offset,
2564                                          old_gsi, new_gsi, refreshed);
2565     }
2566 }
2567
2568 /* Result code for SRA assignment modification.  */
2569 enum assignment_mod_result { SRA_AM_NONE,       /* nothing done for the stmt */
2570                              SRA_AM_MODIFIED,  /* stmt changed but not
2571                                                   removed */
2572                              SRA_AM_REMOVED };  /* stmt eliminated */
2573
2574 /* Modify assignments with a CONSTRUCTOR on their RHS.  STMT contains a pointer
2575    to the assignment and GSI is the statement iterator pointing at it.  Returns
2576    the same values as sra_modify_assign.  */
2577
2578 static enum assignment_mod_result
2579 sra_modify_constructor_assign (gimple *stmt, gimple_stmt_iterator *gsi)
2580 {
2581   tree lhs = gimple_assign_lhs (*stmt);
2582   struct access *acc;
2583   location_t loc;
2584
2585   acc = get_access_for_expr (lhs);
2586   if (!acc)
2587     return SRA_AM_NONE;
2588
2589   loc = gimple_location (*stmt);
2590   if (VEC_length (constructor_elt,
2591                   CONSTRUCTOR_ELTS (gimple_assign_rhs1 (*stmt))) > 0)
2592     {
2593       /* I have never seen this code path trigger but if it can happen the
2594          following should handle it gracefully.  */
2595       if (access_has_children_p (acc))
2596         generate_subtree_copies (acc->first_child, acc->base, 0, 0, 0, gsi,
2597                                  true, true, loc);
2598       return SRA_AM_MODIFIED;
2599     }
2600
2601   if (acc->grp_covered)
2602     {
2603       init_subtree_with_zero (acc, gsi, false, loc);
2604       unlink_stmt_vdef (*stmt);
2605       gsi_remove (gsi, true);
2606       return SRA_AM_REMOVED;
2607     }
2608   else
2609     {
2610       init_subtree_with_zero (acc, gsi, true, loc);
2611       return SRA_AM_MODIFIED;
2612     }
2613 }
2614
2615 /* Create and return a new suitable default definition SSA_NAME for RACC which
2616    is an access describing an uninitialized part of an aggregate that is being
2617    loaded.  */
2618
2619 static tree
2620 get_repl_default_def_ssa_name (struct access *racc)
2621 {
2622   tree repl, decl;
2623
2624   decl = get_unrenamed_access_replacement (racc);
2625
2626   repl = gimple_default_def (cfun, decl);
2627   if (!repl)
2628     {
2629       repl = make_ssa_name (decl, gimple_build_nop ());
2630       set_default_def (decl, repl);
2631     }
2632
2633   return repl;
2634 }
2635
2636 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
2637    somewhere in it.  */
2638
2639 static inline bool
2640 contains_bitfld_comp_ref_p (const_tree ref)
2641 {
2642   while (handled_component_p (ref))
2643     {
2644       if (TREE_CODE (ref) == COMPONENT_REF
2645           && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
2646         return true;
2647       ref = TREE_OPERAND (ref, 0);
2648     }
2649
2650   return false;
2651 }
2652
2653 /* Return true if REF has an VIEW_CONVERT_EXPR or a COMPONENT_REF with a
2654    bit-field field declaration somewhere in it.  */
2655
2656 static inline bool
2657 contains_vce_or_bfcref_p (const_tree ref)
2658 {
2659   while (handled_component_p (ref))
2660     {
2661       if (TREE_CODE (ref) == VIEW_CONVERT_EXPR
2662           || (TREE_CODE (ref) == COMPONENT_REF
2663               && DECL_BIT_FIELD (TREE_OPERAND (ref, 1))))
2664         return true;
2665       ref = TREE_OPERAND (ref, 0);
2666     }
2667
2668   return false;
2669 }
2670
2671 /* Examine both sides of the assignment statement pointed to by STMT, replace
2672    them with a scalare replacement if there is one and generate copying of
2673    replacements if scalarized aggregates have been used in the assignment.  GSI
2674    is used to hold generated statements for type conversions and subtree
2675    copying.  */
2676
2677 static enum assignment_mod_result
2678 sra_modify_assign (gimple *stmt, gimple_stmt_iterator *gsi)
2679 {
2680   struct access *lacc, *racc;
2681   tree lhs, rhs;
2682   bool modify_this_stmt = false;
2683   bool force_gimple_rhs = false;
2684   location_t loc;
2685   gimple_stmt_iterator orig_gsi = *gsi;
2686
2687   if (!gimple_assign_single_p (*stmt))
2688     return SRA_AM_NONE;
2689   lhs = gimple_assign_lhs (*stmt);
2690   rhs = gimple_assign_rhs1 (*stmt);
2691
2692   if (TREE_CODE (rhs) == CONSTRUCTOR)
2693     return sra_modify_constructor_assign (stmt, gsi);
2694
2695   if (TREE_CODE (rhs) == REALPART_EXPR || TREE_CODE (lhs) == REALPART_EXPR
2696       || TREE_CODE (rhs) == IMAGPART_EXPR || TREE_CODE (lhs) == IMAGPART_EXPR
2697       || TREE_CODE (rhs) == BIT_FIELD_REF || TREE_CODE (lhs) == BIT_FIELD_REF)
2698     {
2699       modify_this_stmt = sra_modify_expr (gimple_assign_rhs1_ptr (*stmt),
2700                                           gsi, false);
2701       modify_this_stmt |= sra_modify_expr (gimple_assign_lhs_ptr (*stmt),
2702                                            gsi, true);
2703       return modify_this_stmt ? SRA_AM_MODIFIED : SRA_AM_NONE;
2704     }
2705
2706   lacc = get_access_for_expr (lhs);
2707   racc = get_access_for_expr (rhs);
2708   if (!lacc && !racc)
2709     return SRA_AM_NONE;
2710
2711   loc = gimple_location (*stmt);
2712   if (lacc && lacc->grp_to_be_replaced)
2713     {
2714       lhs = get_access_replacement (lacc);
2715       gimple_assign_set_lhs (*stmt, lhs);
2716       modify_this_stmt = true;
2717       if (lacc->grp_partial_lhs)
2718         force_gimple_rhs = true;
2719       sra_stats.exprs++;
2720     }
2721
2722   if (racc && racc->grp_to_be_replaced)
2723     {
2724       rhs = get_access_replacement (racc);
2725       modify_this_stmt = true;
2726       if (racc->grp_partial_lhs)
2727         force_gimple_rhs = true;
2728       sra_stats.exprs++;
2729     }
2730
2731   if (modify_this_stmt)
2732     {
2733       if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
2734         {
2735           /* If we can avoid creating a VIEW_CONVERT_EXPR do so.
2736              ???  This should move to fold_stmt which we simply should
2737              call after building a VIEW_CONVERT_EXPR here.  */
2738           if (AGGREGATE_TYPE_P (TREE_TYPE (lhs))
2739               && !contains_bitfld_comp_ref_p (lhs)
2740               && !access_has_children_p (lacc))
2741             {
2742               lhs = build_ref_for_model (loc, lhs, 0, racc, gsi, false);
2743               gimple_assign_set_lhs (*stmt, lhs);
2744             }
2745           else if (AGGREGATE_TYPE_P (TREE_TYPE (rhs))
2746                    && !contains_vce_or_bfcref_p (rhs)
2747                    && !access_has_children_p (racc))
2748             rhs = build_ref_for_model (loc, rhs, 0, lacc, gsi, false);
2749
2750           if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
2751             {
2752               rhs = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (lhs),
2753                                      rhs);
2754               if (is_gimple_reg_type (TREE_TYPE (lhs))
2755                   && TREE_CODE (lhs) != SSA_NAME)
2756                 force_gimple_rhs = true;
2757             }
2758         }
2759     }
2760
2761   /* From this point on, the function deals with assignments in between
2762      aggregates when at least one has scalar reductions of some of its
2763      components.  There are three possible scenarios: Both the LHS and RHS have
2764      to-be-scalarized components, 2) only the RHS has or 3) only the LHS has.
2765
2766      In the first case, we would like to load the LHS components from RHS
2767      components whenever possible.  If that is not possible, we would like to
2768      read it directly from the RHS (after updating it by storing in it its own
2769      components).  If there are some necessary unscalarized data in the LHS,
2770      those will be loaded by the original assignment too.  If neither of these
2771      cases happen, the original statement can be removed.  Most of this is done
2772      by load_assign_lhs_subreplacements.
2773
2774      In the second case, we would like to store all RHS scalarized components
2775      directly into LHS and if they cover the aggregate completely, remove the
2776      statement too.  In the third case, we want the LHS components to be loaded
2777      directly from the RHS (DSE will remove the original statement if it
2778      becomes redundant).
2779
2780      This is a bit complex but manageable when types match and when unions do
2781      not cause confusion in a way that we cannot really load a component of LHS
2782      from the RHS or vice versa (the access representing this level can have
2783      subaccesses that are accessible only through a different union field at a
2784      higher level - different from the one used in the examined expression).
2785      Unions are fun.
2786
2787      Therefore, I specially handle a fourth case, happening when there is a
2788      specific type cast or it is impossible to locate a scalarized subaccess on
2789      the other side of the expression.  If that happens, I simply "refresh" the
2790      RHS by storing in it is scalarized components leave the original statement
2791      there to do the copying and then load the scalar replacements of the LHS.
2792      This is what the first branch does.  */
2793
2794   if (gimple_has_volatile_ops (*stmt)
2795       || contains_vce_or_bfcref_p (rhs)
2796       || contains_vce_or_bfcref_p (lhs))
2797     {
2798       if (access_has_children_p (racc))
2799         generate_subtree_copies (racc->first_child, racc->base, 0, 0, 0,
2800                                  gsi, false, false, loc);
2801       if (access_has_children_p (lacc))
2802         generate_subtree_copies (lacc->first_child, lacc->base, 0, 0, 0,
2803                                  gsi, true, true, loc);
2804       sra_stats.separate_lhs_rhs_handling++;
2805     }
2806   else
2807     {
2808       if (access_has_children_p (lacc) && access_has_children_p (racc))
2809         {
2810           gimple_stmt_iterator orig_gsi = *gsi;
2811           enum unscalarized_data_handling refreshed;
2812
2813           if (lacc->grp_read && !lacc->grp_covered)
2814             refreshed = handle_unscalarized_data_in_subtree (racc, gsi);
2815           else
2816             refreshed = SRA_UDH_NONE;
2817
2818           load_assign_lhs_subreplacements (lacc, racc, lacc->offset,
2819                                            &orig_gsi, gsi, &refreshed);
2820           if (refreshed != SRA_UDH_RIGHT)
2821             {
2822               gsi_next (gsi);
2823               unlink_stmt_vdef (*stmt);
2824               gsi_remove (&orig_gsi, true);
2825               sra_stats.deleted++;
2826               return SRA_AM_REMOVED;
2827             }
2828         }
2829       else
2830         {
2831           if (racc)
2832             {
2833               if (!racc->grp_to_be_replaced && !racc->grp_unscalarized_data)
2834                 {
2835                   if (dump_file)
2836                     {
2837                       fprintf (dump_file, "Removing load: ");
2838                       print_gimple_stmt (dump_file, *stmt, 0, 0);
2839                     }
2840
2841                   if (TREE_CODE (lhs) == SSA_NAME)
2842                     {
2843                       rhs = get_repl_default_def_ssa_name (racc);
2844                       if (!useless_type_conversion_p (TREE_TYPE (lhs),
2845                                                       TREE_TYPE (rhs)))
2846                         rhs = fold_build1_loc (loc, VIEW_CONVERT_EXPR,
2847                                                TREE_TYPE (lhs), rhs);
2848                     }
2849                   else
2850                     {
2851                       if (racc->first_child)
2852                         generate_subtree_copies (racc->first_child, lhs,
2853                                                  racc->offset, 0, 0, gsi,
2854                                                  false, false, loc);
2855
2856                       gcc_assert (*stmt == gsi_stmt (*gsi));
2857                       unlink_stmt_vdef (*stmt);
2858                       gsi_remove (gsi, true);
2859                       sra_stats.deleted++;
2860                       return SRA_AM_REMOVED;
2861                     }
2862                 }
2863               else if (racc->first_child)
2864                 generate_subtree_copies (racc->first_child, lhs, racc->offset,
2865                                          0, 0, gsi, false, true, loc);
2866             }
2867           if (access_has_children_p (lacc))
2868             generate_subtree_copies (lacc->first_child, rhs, lacc->offset,
2869                                      0, 0, gsi, true, true, loc);
2870         }
2871     }
2872
2873   /* This gimplification must be done after generate_subtree_copies, lest we
2874      insert the subtree copies in the middle of the gimplified sequence.  */
2875   if (force_gimple_rhs)
2876     rhs = force_gimple_operand_gsi (&orig_gsi, rhs, true, NULL_TREE,
2877                                     true, GSI_SAME_STMT);
2878   if (gimple_assign_rhs1 (*stmt) != rhs)
2879     {
2880       modify_this_stmt = true;
2881       gimple_assign_set_rhs_from_tree (&orig_gsi, rhs);
2882       gcc_assert (*stmt == gsi_stmt (orig_gsi));
2883     }
2884
2885   return modify_this_stmt ? SRA_AM_MODIFIED : SRA_AM_NONE;
2886 }
2887
2888 /* Traverse the function body and all modifications as decided in
2889    analyze_all_variable_accesses.  Return true iff the CFG has been
2890    changed.  */
2891
2892 static bool
2893 sra_modify_function_body (void)
2894 {
2895   bool cfg_changed = false;
2896   basic_block bb;
2897
2898   FOR_EACH_BB (bb)
2899     {
2900       gimple_stmt_iterator gsi = gsi_start_bb (bb);
2901       while (!gsi_end_p (gsi))
2902         {
2903           gimple stmt = gsi_stmt (gsi);
2904           enum assignment_mod_result assign_result;
2905           bool modified = false, deleted = false;
2906           tree *t;
2907           unsigned i;
2908
2909           switch (gimple_code (stmt))
2910             {
2911             case GIMPLE_RETURN:
2912               t = gimple_return_retval_ptr (stmt);
2913               if (*t != NULL_TREE)
2914                 modified |= sra_modify_expr (t, &gsi, false);
2915               break;
2916
2917             case GIMPLE_ASSIGN:
2918               assign_result = sra_modify_assign (&stmt, &gsi);
2919               modified |= assign_result == SRA_AM_MODIFIED;
2920               deleted = assign_result == SRA_AM_REMOVED;
2921               break;
2922
2923             case GIMPLE_CALL:
2924               /* Operands must be processed before the lhs.  */
2925               for (i = 0; i < gimple_call_num_args (stmt); i++)
2926                 {
2927                   t = gimple_call_arg_ptr (stmt, i);
2928                   modified |= sra_modify_expr (t, &gsi, false);
2929                 }
2930
2931               if (gimple_call_lhs (stmt))
2932                 {
2933                   t = gimple_call_lhs_ptr (stmt);
2934                   modified |= sra_modify_expr (t, &gsi, true);
2935                 }
2936               break;
2937
2938             case GIMPLE_ASM:
2939               for (i = 0; i < gimple_asm_ninputs (stmt); i++)
2940                 {
2941                   t = &TREE_VALUE (gimple_asm_input_op (stmt, i));
2942                   modified |= sra_modify_expr (t, &gsi, false);
2943                 }
2944               for (i = 0; i < gimple_asm_noutputs (stmt); i++)
2945                 {
2946                   t = &TREE_VALUE (gimple_asm_output_op (stmt, i));
2947                   modified |= sra_modify_expr (t, &gsi, true);
2948                 }
2949               break;
2950
2951             default:
2952               break;
2953             }
2954
2955           if (modified)
2956             {
2957               update_stmt (stmt);
2958               if (maybe_clean_eh_stmt (stmt)
2959                   && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
2960                 cfg_changed = true;
2961             }
2962           if (!deleted)
2963             gsi_next (&gsi);
2964         }
2965     }
2966
2967   return cfg_changed;
2968 }
2969
2970 /* Generate statements initializing scalar replacements of parts of function
2971    parameters.  */
2972
2973 static void
2974 initialize_parameter_reductions (void)
2975 {
2976   gimple_stmt_iterator gsi;
2977   gimple_seq seq = NULL;
2978   tree parm;
2979
2980   for (parm = DECL_ARGUMENTS (current_function_decl);
2981        parm;
2982        parm = DECL_CHAIN (parm))
2983     {
2984       VEC (access_p, heap) *access_vec;
2985       struct access *access;
2986
2987       if (!bitmap_bit_p (candidate_bitmap, DECL_UID (parm)))
2988         continue;
2989       access_vec = get_base_access_vector (parm);
2990       if (!access_vec)
2991         continue;
2992
2993       if (!seq)
2994         {
2995           seq = gimple_seq_alloc ();
2996           gsi = gsi_start (seq);
2997         }
2998
2999       for (access = VEC_index (access_p, access_vec, 0);
3000            access;
3001            access = access->next_grp)
3002         generate_subtree_copies (access, parm, 0, 0, 0, &gsi, true, true,
3003                                  EXPR_LOCATION (parm));
3004     }
3005
3006   if (seq)
3007     gsi_insert_seq_on_edge_immediate (single_succ_edge (ENTRY_BLOCK_PTR), seq);
3008 }
3009
3010 /* The "main" function of intraprocedural SRA passes.  Runs the analysis and if
3011    it reveals there are components of some aggregates to be scalarized, it runs
3012    the required transformations.  */
3013 static unsigned int
3014 perform_intra_sra (void)
3015 {
3016   int ret = 0;
3017   sra_initialize ();
3018
3019   if (!find_var_candidates ())
3020     goto out;
3021
3022   if (!scan_function ())
3023     goto out;
3024
3025   if (!analyze_all_variable_accesses ())
3026     goto out;
3027
3028   if (sra_modify_function_body ())
3029     ret = TODO_update_ssa | TODO_cleanup_cfg;
3030   else
3031     ret = TODO_update_ssa;
3032   initialize_parameter_reductions ();
3033
3034   statistics_counter_event (cfun, "Scalar replacements created",
3035                             sra_stats.replacements);
3036   statistics_counter_event (cfun, "Modified expressions", sra_stats.exprs);
3037   statistics_counter_event (cfun, "Subtree copy stmts",
3038                             sra_stats.subtree_copies);
3039   statistics_counter_event (cfun, "Subreplacement stmts",
3040                             sra_stats.subreplacements);
3041   statistics_counter_event (cfun, "Deleted stmts", sra_stats.deleted);
3042   statistics_counter_event (cfun, "Separate LHS and RHS handling",
3043                             sra_stats.separate_lhs_rhs_handling);
3044
3045  out:
3046   sra_deinitialize ();
3047   return ret;
3048 }
3049
3050 /* Perform early intraprocedural SRA.  */
3051 static unsigned int
3052 early_intra_sra (void)
3053 {
3054   sra_mode = SRA_MODE_EARLY_INTRA;
3055   return perform_intra_sra ();
3056 }
3057
3058 /* Perform "late" intraprocedural SRA.  */
3059 static unsigned int
3060 late_intra_sra (void)
3061 {
3062   sra_mode = SRA_MODE_INTRA;
3063   return perform_intra_sra ();
3064 }
3065
3066
3067 static bool
3068 gate_intra_sra (void)
3069 {
3070   return flag_tree_sra != 0 && dbg_cnt (tree_sra);
3071 }
3072
3073
3074 struct gimple_opt_pass pass_sra_early =
3075 {
3076  {
3077   GIMPLE_PASS,
3078   "esra",                               /* name */
3079   gate_intra_sra,                       /* gate */
3080   early_intra_sra,                      /* execute */
3081   NULL,                                 /* sub */
3082   NULL,                                 /* next */
3083   0,                                    /* static_pass_number */
3084   TV_TREE_SRA,                          /* tv_id */
3085   PROP_cfg | PROP_ssa,                  /* properties_required */
3086   0,                                    /* properties_provided */
3087   0,                                    /* properties_destroyed */
3088   0,                                    /* todo_flags_start */
3089   TODO_dump_func
3090   | TODO_update_ssa
3091   | TODO_ggc_collect
3092   | TODO_verify_ssa                     /* todo_flags_finish */
3093  }
3094 };
3095
3096 struct gimple_opt_pass pass_sra =
3097 {
3098  {
3099   GIMPLE_PASS,
3100   "sra",                                /* name */
3101   gate_intra_sra,                       /* gate */
3102   late_intra_sra,                       /* execute */
3103   NULL,                                 /* sub */
3104   NULL,                                 /* next */
3105   0,                                    /* static_pass_number */
3106   TV_TREE_SRA,                          /* tv_id */
3107   PROP_cfg | PROP_ssa,                  /* properties_required */
3108   0,                                    /* properties_provided */
3109   0,                                    /* properties_destroyed */
3110   TODO_update_address_taken,            /* todo_flags_start */
3111   TODO_dump_func
3112   | TODO_update_ssa
3113   | TODO_ggc_collect
3114   | TODO_verify_ssa                     /* todo_flags_finish */
3115  }
3116 };
3117
3118
3119 /* Return true iff PARM (which must be a parm_decl) is an unused scalar
3120    parameter.  */
3121
3122 static bool
3123 is_unused_scalar_param (tree parm)
3124 {
3125   tree name;
3126   return (is_gimple_reg (parm)
3127           && (!(name = gimple_default_def (cfun, parm))
3128               || has_zero_uses (name)));
3129 }
3130
3131 /* Scan immediate uses of a default definition SSA name of a parameter PARM and
3132    examine whether there are any direct or otherwise infeasible ones.  If so,
3133    return true, otherwise return false.  PARM must be a gimple register with a
3134    non-NULL default definition.  */
3135
3136 static bool
3137 ptr_parm_has_direct_uses (tree parm)
3138 {
3139   imm_use_iterator ui;
3140   gimple stmt;
3141   tree name = gimple_default_def (cfun, parm);
3142   bool ret = false;
3143
3144   FOR_EACH_IMM_USE_STMT (stmt, ui, name)
3145     {
3146       int uses_ok = 0;
3147       use_operand_p use_p;
3148
3149       if (is_gimple_debug (stmt))
3150         continue;
3151
3152       /* Valid uses include dereferences on the lhs and the rhs.  */
3153       if (gimple_has_lhs (stmt))
3154         {
3155           tree lhs = gimple_get_lhs (stmt);
3156           while (handled_component_p (lhs))
3157             lhs = TREE_OPERAND (lhs, 0);
3158           if (TREE_CODE (lhs) == MEM_REF
3159               && TREE_OPERAND (lhs, 0) == name
3160               && integer_zerop (TREE_OPERAND (lhs, 1))
3161               && types_compatible_p (TREE_TYPE (lhs),
3162                                      TREE_TYPE (TREE_TYPE (name))))
3163             uses_ok++;
3164         }
3165       if (gimple_assign_single_p (stmt))
3166         {
3167           tree rhs = gimple_assign_rhs1 (stmt);
3168           while (handled_component_p (rhs))
3169             rhs = TREE_OPERAND (rhs, 0);
3170           if (TREE_CODE (rhs) == MEM_REF
3171               && TREE_OPERAND (rhs, 0) == name
3172               && integer_zerop (TREE_OPERAND (rhs, 1))
3173               && types_compatible_p (TREE_TYPE (rhs),
3174                                      TREE_TYPE (TREE_TYPE (name))))
3175             uses_ok++;
3176         }
3177       else if (is_gimple_call (stmt))
3178         {
3179           unsigned i;
3180           for (i = 0; i < gimple_call_num_args (stmt); ++i)
3181             {
3182               tree arg = gimple_call_arg (stmt, i);
3183               while (handled_component_p (arg))
3184                 arg = TREE_OPERAND (arg, 0);
3185               if (TREE_CODE (arg) == MEM_REF
3186                   && TREE_OPERAND (arg, 0) == name
3187                   && integer_zerop (TREE_OPERAND (arg, 1))
3188                   && types_compatible_p (TREE_TYPE (arg),
3189                                          TREE_TYPE (TREE_TYPE (name))))
3190                 uses_ok++;
3191             }
3192         }
3193
3194       /* If the number of valid uses does not match the number of
3195          uses in this stmt there is an unhandled use.  */
3196       FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
3197         --uses_ok;
3198
3199       if (uses_ok != 0)
3200         ret = true;
3201
3202       if (ret)
3203         BREAK_FROM_IMM_USE_STMT (ui);
3204     }
3205
3206   return ret;
3207 }
3208
3209 /* Identify candidates for reduction for IPA-SRA based on their type and mark
3210    them in candidate_bitmap.  Note that these do not necessarily include
3211    parameter which are unused and thus can be removed.  Return true iff any
3212    such candidate has been found.  */
3213
3214 static bool
3215 find_param_candidates (void)
3216 {
3217   tree parm;
3218   int count = 0;
3219   bool ret = false;
3220
3221   for (parm = DECL_ARGUMENTS (current_function_decl);
3222        parm;
3223        parm = DECL_CHAIN (parm))
3224     {
3225       tree type = TREE_TYPE (parm);
3226
3227       count++;
3228
3229       if (TREE_THIS_VOLATILE (parm)
3230           || TREE_ADDRESSABLE (parm)
3231           || (!is_gimple_reg_type (type) && is_va_list_type (type)))
3232         continue;
3233
3234       if (is_unused_scalar_param (parm))
3235         {
3236           ret = true;
3237           continue;
3238         }
3239
3240       if (POINTER_TYPE_P (type))
3241         {
3242           type = TREE_TYPE (type);
3243
3244           if (TREE_CODE (type) == FUNCTION_TYPE
3245               || TYPE_VOLATILE (type)
3246               || (TREE_CODE (type) == ARRAY_TYPE
3247                   && TYPE_NONALIASED_COMPONENT (type))
3248               || !is_gimple_reg (parm)
3249               || is_va_list_type (type)
3250               || ptr_parm_has_direct_uses (parm))
3251             continue;
3252         }
3253       else if (!AGGREGATE_TYPE_P (type))
3254         continue;
3255
3256       if (!COMPLETE_TYPE_P (type)
3257           || !host_integerp (TYPE_SIZE (type), 1)
3258           || tree_low_cst (TYPE_SIZE (type), 1) == 0
3259           || (AGGREGATE_TYPE_P (type)
3260               && type_internals_preclude_sra_p (type)))
3261         continue;
3262
3263       bitmap_set_bit (candidate_bitmap, DECL_UID (parm));
3264       ret = true;
3265       if (dump_file && (dump_flags & TDF_DETAILS))
3266         {
3267           fprintf (dump_file, "Candidate (%d): ", DECL_UID (parm));
3268           print_generic_expr (dump_file, parm, 0);
3269           fprintf (dump_file, "\n");
3270         }
3271     }
3272
3273   func_param_count = count;
3274   return ret;
3275 }
3276
3277 /* Callback of walk_aliased_vdefs, marks the access passed as DATA as
3278    maybe_modified. */
3279
3280 static bool
3281 mark_maybe_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
3282                      void *data)
3283 {
3284   struct access *repr = (struct access *) data;
3285
3286   repr->grp_maybe_modified = 1;
3287   return true;
3288 }
3289
3290 /* Analyze what representatives (in linked lists accessible from
3291    REPRESENTATIVES) can be modified by side effects of statements in the
3292    current function.  */
3293
3294 static void
3295 analyze_modified_params (VEC (access_p, heap) *representatives)
3296 {
3297   int i;
3298
3299   for (i = 0; i < func_param_count; i++)
3300     {
3301       struct access *repr;
3302
3303       for (repr = VEC_index (access_p, representatives, i);
3304            repr;
3305            repr = repr->next_grp)
3306         {
3307           struct access *access;
3308           bitmap visited;
3309           ao_ref ar;
3310
3311           if (no_accesses_p (repr))
3312             continue;
3313           if (!POINTER_TYPE_P (TREE_TYPE (repr->base))
3314               || repr->grp_maybe_modified)
3315             continue;
3316
3317           ao_ref_init (&ar, repr->expr);
3318           visited = BITMAP_ALLOC (NULL);
3319           for (access = repr; access; access = access->next_sibling)
3320             {
3321               /* All accesses are read ones, otherwise grp_maybe_modified would
3322                  be trivially set.  */
3323               walk_aliased_vdefs (&ar, gimple_vuse (access->stmt),
3324                                   mark_maybe_modified, repr, &visited);
3325               if (repr->grp_maybe_modified)
3326                 break;
3327             }
3328           BITMAP_FREE (visited);
3329         }
3330     }
3331 }
3332
3333 /* Propagate distances in bb_dereferences in the opposite direction than the
3334    control flow edges, in each step storing the maximum of the current value
3335    and the minimum of all successors.  These steps are repeated until the table
3336    stabilizes.  Note that BBs which might terminate the functions (according to
3337    final_bbs bitmap) never updated in this way.  */
3338
3339 static void
3340 propagate_dereference_distances (void)
3341 {
3342   VEC (basic_block, heap) *queue;
3343   basic_block bb;
3344
3345   queue = VEC_alloc (basic_block, heap, last_basic_block_for_function (cfun));
3346   VEC_quick_push (basic_block, queue, ENTRY_BLOCK_PTR);
3347   FOR_EACH_BB (bb)
3348     {
3349       VEC_quick_push (basic_block, queue, bb);
3350       bb->aux = bb;
3351     }
3352
3353   while (!VEC_empty (basic_block, queue))
3354     {
3355       edge_iterator ei;
3356       edge e;
3357       bool change = false;
3358       int i;
3359
3360       bb = VEC_pop (basic_block, queue);
3361       bb->aux = NULL;
3362
3363       if (bitmap_bit_p (final_bbs, bb->index))
3364         continue;
3365
3366       for (i = 0; i < func_param_count; i++)
3367         {
3368           int idx = bb->index * func_param_count + i;
3369           bool first = true;
3370           HOST_WIDE_INT inh = 0;
3371
3372           FOR_EACH_EDGE (e, ei, bb->succs)
3373           {
3374             int succ_idx = e->dest->index * func_param_count + i;
3375
3376             if (e->src == EXIT_BLOCK_PTR)
3377               continue;
3378
3379             if (first)
3380               {
3381                 first = false;
3382                 inh = bb_dereferences [succ_idx];
3383               }
3384             else if (bb_dereferences [succ_idx] < inh)
3385               inh = bb_dereferences [succ_idx];
3386           }
3387
3388           if (!first && bb_dereferences[idx] < inh)
3389             {
3390               bb_dereferences[idx] = inh;
3391               change = true;
3392             }
3393         }
3394
3395       if (change && !bitmap_bit_p (final_bbs, bb->index))
3396         FOR_EACH_EDGE (e, ei, bb->preds)
3397           {
3398             if (e->src->aux)
3399               continue;
3400
3401             e->src->aux = e->src;
3402             VEC_quick_push (basic_block, queue, e->src);
3403           }
3404     }
3405
3406   VEC_free (basic_block, heap, queue);
3407 }
3408
3409 /* Dump a dereferences TABLE with heading STR to file F.  */
3410
3411 static void
3412 dump_dereferences_table (FILE *f, const char *str, HOST_WIDE_INT *table)
3413 {
3414   basic_block bb;
3415
3416   fprintf (dump_file, str);
3417   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
3418     {
3419       fprintf (f, "%4i  %i   ", bb->index, bitmap_bit_p (final_bbs, bb->index));
3420       if (bb != EXIT_BLOCK_PTR)
3421         {
3422           int i;
3423           for (i = 0; i < func_param_count; i++)
3424             {
3425               int idx = bb->index * func_param_count + i;
3426               fprintf (f, " %4" HOST_WIDE_INT_PRINT "d", table[idx]);
3427             }
3428         }
3429       fprintf (f, "\n");
3430     }
3431   fprintf (dump_file, "\n");
3432 }
3433
3434 /* Determine what (parts of) parameters passed by reference that are not
3435    assigned to are not certainly dereferenced in this function and thus the
3436    dereferencing cannot be safely moved to the caller without potentially
3437    introducing a segfault.  Mark such REPRESENTATIVES as
3438    grp_not_necessarilly_dereferenced.
3439
3440    The dereferenced maximum "distance," i.e. the offset + size of the accessed
3441    part is calculated rather than simple booleans are calculated for each
3442    pointer parameter to handle cases when only a fraction of the whole
3443    aggregate is allocated (see testsuite/gcc.c-torture/execute/ipa-sra-2.c for
3444    an example).
3445
3446    The maximum dereference distances for each pointer parameter and BB are
3447    already stored in bb_dereference.  This routine simply propagates these
3448    values upwards by propagate_dereference_distances and then compares the
3449    distances of individual parameters in the ENTRY BB to the equivalent
3450    distances of each representative of a (fraction of a) parameter.  */
3451
3452 static void
3453 analyze_caller_dereference_legality (VEC (access_p, heap) *representatives)
3454 {
3455   int i;
3456
3457   if (dump_file && (dump_flags & TDF_DETAILS))
3458     dump_dereferences_table (dump_file,
3459                              "Dereference table before propagation:\n",
3460                              bb_dereferences);
3461
3462   propagate_dereference_distances ();
3463
3464   if (dump_file && (dump_flags & TDF_DETAILS))
3465     dump_dereferences_table (dump_file,
3466                              "Dereference table after propagation:\n",
3467                              bb_dereferences);
3468
3469   for (i = 0; i < func_param_count; i++)
3470     {
3471       struct access *repr = VEC_index (access_p, representatives, i);
3472       int idx = ENTRY_BLOCK_PTR->index * func_param_count + i;
3473
3474       if (!repr || no_accesses_p (repr))
3475         continue;
3476
3477       do
3478         {
3479           if ((repr->offset + repr->size) > bb_dereferences[idx])
3480             repr->grp_not_necessarilly_dereferenced = 1;
3481           repr = repr->next_grp;
3482         }
3483       while (repr);
3484     }
3485 }
3486
3487 /* Return the representative access for the parameter declaration PARM if it is
3488    a scalar passed by reference which is not written to and the pointer value
3489    is not used directly.  Thus, if it is legal to dereference it in the caller
3490    and we can rule out modifications through aliases, such parameter should be
3491    turned into one passed by value.  Return NULL otherwise.  */
3492
3493 static struct access *
3494 unmodified_by_ref_scalar_representative (tree parm)
3495 {
3496   int i, access_count;
3497   struct access *repr;
3498   VEC (access_p, heap) *access_vec;
3499
3500   access_vec = get_base_access_vector (parm);
3501   gcc_assert (access_vec);
3502   repr = VEC_index (access_p, access_vec, 0);
3503   if (repr->write)
3504     return NULL;
3505   repr->group_representative = repr;
3506
3507   access_count = VEC_length (access_p, access_vec);
3508   for (i = 1; i < access_count; i++)
3509     {
3510       struct access *access = VEC_index (access_p, access_vec, i);
3511       if (access->write)
3512         return NULL;
3513       access->group_representative = repr;
3514       access->next_sibling = repr->next_sibling;
3515       repr->next_sibling = access;
3516     }
3517
3518   repr->grp_read = 1;
3519   repr->grp_scalar_ptr = 1;
3520   return repr;
3521 }
3522
3523 /* Return true iff this access precludes IPA-SRA of the parameter it is
3524    associated with. */
3525
3526 static bool
3527 access_precludes_ipa_sra_p (struct access *access)
3528 {
3529   /* Avoid issues such as the second simple testcase in PR 42025.  The problem
3530      is incompatible assign in a call statement (and possibly even in asm
3531      statements).  This can be relaxed by using a new temporary but only for
3532      non-TREE_ADDRESSABLE types and is probably not worth the complexity. (In
3533      intraprocedural SRA we deal with this by keeping the old aggregate around,
3534      something we cannot do in IPA-SRA.)  */
3535   if (access->write
3536       && (is_gimple_call (access->stmt)
3537           || gimple_code (access->stmt) == GIMPLE_ASM))
3538     return true;
3539
3540   return false;
3541 }
3542
3543
3544 /* Sort collected accesses for parameter PARM, identify representatives for
3545    each accessed region and link them together.  Return NULL if there are
3546    different but overlapping accesses, return the special ptr value meaning
3547    there are no accesses for this parameter if that is the case and return the
3548    first representative otherwise.  Set *RO_GRP if there is a group of accesses
3549    with only read (i.e. no write) accesses.  */
3550
3551 static struct access *
3552 splice_param_accesses (tree parm, bool *ro_grp)
3553 {
3554   int i, j, access_count, group_count;
3555   int agg_size, total_size = 0;
3556   struct access *access, *res, **prev_acc_ptr = &res;
3557   VEC (access_p, heap) *access_vec;
3558
3559   access_vec = get_base_access_vector (parm);
3560   if (!access_vec)
3561     return &no_accesses_representant;
3562   access_count = VEC_length (access_p, access_vec);
3563
3564   VEC_qsort (access_p, access_vec, compare_access_positions);
3565
3566   i = 0;
3567   total_size = 0;
3568   group_count = 0;
3569   while (i < access_count)
3570     {
3571       bool modification;
3572       tree a1_alias_type;
3573       access = VEC_index (access_p, access_vec, i);
3574       modification = access->write;
3575       if (access_precludes_ipa_sra_p (access))
3576         return NULL;
3577       a1_alias_type = reference_alias_ptr_type (access->expr);
3578
3579       /* Access is about to become group representative unless we find some
3580          nasty overlap which would preclude us from breaking this parameter
3581          apart. */
3582
3583       j = i + 1;
3584       while (j < access_count)
3585         {
3586           struct access *ac2 = VEC_index (access_p, access_vec, j);
3587           if (ac2->offset != access->offset)
3588             {
3589               /* All or nothing law for parameters. */
3590               if (access->offset + access->size > ac2->offset)
3591                 return NULL;
3592               else
3593                 break;
3594             }
3595           else if (ac2->size != access->size)
3596             return NULL;
3597
3598           if (access_precludes_ipa_sra_p (ac2)
3599               || (ac2->type != access->type
3600                   && (TREE_ADDRESSABLE (ac2->type)
3601                       || TREE_ADDRESSABLE (access->type)))
3602               || (reference_alias_ptr_type (ac2->expr) != a1_alias_type))
3603             return NULL;
3604
3605           modification |= ac2->write;
3606           ac2->group_representative = access;
3607           ac2->next_sibling = access->next_sibling;
3608           access->next_sibling = ac2;
3609           j++;
3610         }
3611
3612       group_count++;
3613       access->grp_maybe_modified = modification;
3614       if (!modification)
3615         *ro_grp = true;
3616       *prev_acc_ptr = access;
3617       prev_acc_ptr = &access->next_grp;
3618       total_size += access->size;
3619       i = j;
3620     }
3621
3622   if (POINTER_TYPE_P (TREE_TYPE (parm)))
3623     agg_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (parm))), 1);
3624   else
3625     agg_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (parm)), 1);
3626   if (total_size >= agg_size)
3627     return NULL;
3628
3629   gcc_assert (group_count > 0);
3630   return res;
3631 }
3632
3633 /* Decide whether parameters with representative accesses given by REPR should
3634    be reduced into components.  */
3635
3636 static int
3637 decide_one_param_reduction (struct access *repr)
3638 {
3639   int total_size, cur_parm_size, agg_size, new_param_count, parm_size_limit;
3640   bool by_ref;
3641   tree parm;
3642
3643   parm = repr->base;
3644   cur_parm_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (parm)), 1);
3645   gcc_assert (cur_parm_size > 0);
3646
3647   if (POINTER_TYPE_P (TREE_TYPE (parm)))
3648     {
3649       by_ref = true;
3650       agg_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (parm))), 1);
3651     }
3652   else
3653     {
3654       by_ref = false;
3655       agg_size = cur_parm_size;
3656     }
3657
3658   if (dump_file)
3659     {
3660       struct access *acc;
3661       fprintf (dump_file, "Evaluating PARAM group sizes for ");
3662       print_generic_expr (dump_file, parm, 0);
3663       fprintf (dump_file, " (UID: %u): \n", DECL_UID (parm));
3664       for (acc = repr; acc; acc = acc->next_grp)
3665         dump_access (dump_file, acc, true);
3666     }
3667
3668   total_size = 0;
3669   new_param_count = 0;
3670
3671   for (; repr; repr = repr->next_grp)
3672     {
3673       gcc_assert (parm == repr->base);
3674
3675       /* Taking the address of a non-addressable field is verboten.  */
3676       if (by_ref && repr->non_addressable)
3677         return 0;
3678
3679       if (!by_ref || (!repr->grp_maybe_modified
3680                       && !repr->grp_not_necessarilly_dereferenced))
3681         total_size += repr->size;
3682       else
3683         total_size += cur_parm_size;
3684
3685       new_param_count++;
3686     }
3687
3688   gcc_assert (new_param_count > 0);
3689
3690   if (optimize_function_for_size_p (cfun))
3691     parm_size_limit = cur_parm_size;
3692   else
3693     parm_size_limit = (PARAM_VALUE (PARAM_IPA_SRA_PTR_GROWTH_FACTOR)
3694                        * cur_parm_size);
3695
3696   if (total_size < agg_size
3697       && total_size <= parm_size_limit)
3698     {
3699       if (dump_file)
3700         fprintf (dump_file, "    ....will be split into %i components\n",
3701                  new_param_count);
3702       return new_param_count;
3703     }
3704   else
3705     return 0;
3706 }
3707
3708 /* The order of the following enums is important, we need to do extra work for
3709    UNUSED_PARAMS, BY_VAL_ACCESSES and UNMODIF_BY_REF_ACCESSES.  */
3710 enum ipa_splicing_result { NO_GOOD_ACCESS, UNUSED_PARAMS, BY_VAL_ACCESSES,
3711                           MODIF_BY_REF_ACCESSES, UNMODIF_BY_REF_ACCESSES };
3712
3713 /* Identify representatives of all accesses to all candidate parameters for
3714    IPA-SRA.  Return result based on what representatives have been found. */
3715
3716 static enum ipa_splicing_result
3717 splice_all_param_accesses (VEC (access_p, heap) **representatives)
3718 {
3719   enum ipa_splicing_result result = NO_GOOD_ACCESS;
3720   tree parm;
3721   struct access *repr;
3722
3723   *representatives = VEC_alloc (access_p, heap, func_param_count);
3724
3725   for (parm = DECL_ARGUMENTS (current_function_decl);
3726        parm;
3727        parm = DECL_CHAIN (parm))
3728     {
3729       if (is_unused_scalar_param (parm))
3730         {
3731           VEC_quick_push (access_p, *representatives,
3732                           &no_accesses_representant);
3733           if (result == NO_GOOD_ACCESS)
3734             result = UNUSED_PARAMS;
3735         }
3736       else if (POINTER_TYPE_P (TREE_TYPE (parm))
3737                && is_gimple_reg_type (TREE_TYPE (TREE_TYPE (parm)))
3738                && bitmap_bit_p (candidate_bitmap, DECL_UID (parm)))
3739         {
3740           repr = unmodified_by_ref_scalar_representative (parm);
3741           VEC_quick_push (access_p, *representatives, repr);
3742           if (repr)
3743             result = UNMODIF_BY_REF_ACCESSES;
3744         }
3745       else if (bitmap_bit_p (candidate_bitmap, DECL_UID (parm)))
3746         {
3747           bool ro_grp = false;
3748           repr = splice_param_accesses (parm, &ro_grp);
3749           VEC_quick_push (access_p, *representatives, repr);
3750
3751           if (repr && !no_accesses_p (repr))
3752             {
3753               if (POINTER_TYPE_P (TREE_TYPE (parm)))
3754                 {
3755                   if (ro_grp)
3756                     result = UNMODIF_BY_REF_ACCESSES;
3757                   else if (result < MODIF_BY_REF_ACCESSES)
3758                     result = MODIF_BY_REF_ACCESSES;
3759                 }
3760               else if (result < BY_VAL_ACCESSES)
3761                 result = BY_VAL_ACCESSES;
3762             }
3763           else if (no_accesses_p (repr) && (result == NO_GOOD_ACCESS))
3764             result = UNUSED_PARAMS;
3765         }
3766       else
3767         VEC_quick_push (access_p, *representatives, NULL);
3768     }
3769
3770   if (result == NO_GOOD_ACCESS)
3771     {
3772       VEC_free (access_p, heap, *representatives);
3773       *representatives = NULL;
3774       return NO_GOOD_ACCESS;
3775     }
3776
3777   return result;
3778 }
3779
3780 /* Return the index of BASE in PARMS.  Abort if it is not found.  */
3781
3782 static inline int
3783 get_param_index (tree base, VEC(tree, heap) *parms)
3784 {
3785   int i, len;
3786
3787   len = VEC_length (tree, parms);
3788   for (i = 0; i < len; i++)
3789     if (VEC_index (tree, parms, i) == base)
3790       return i;
3791   gcc_unreachable ();
3792 }
3793
3794 /* Convert the decisions made at the representative level into compact
3795    parameter adjustments.  REPRESENTATIVES are pointers to first
3796    representatives of each param accesses, ADJUSTMENTS_COUNT is the expected
3797    final number of adjustments.  */
3798
3799 static ipa_parm_adjustment_vec
3800 turn_representatives_into_adjustments (VEC (access_p, heap) *representatives,
3801                                        int adjustments_count)
3802 {
3803   VEC (tree, heap) *parms;
3804   ipa_parm_adjustment_vec adjustments;
3805   tree parm;
3806   int i;
3807
3808   gcc_assert (adjustments_count > 0);
3809   parms = ipa_get_vector_of_formal_parms (current_function_decl);
3810   adjustments = VEC_alloc (ipa_parm_adjustment_t, heap, adjustments_count);
3811   parm = DECL_ARGUMENTS (current_function_decl);
3812   for (i = 0; i < func_param_count; i++, parm = DECL_CHAIN (parm))
3813     {
3814       struct access *repr = VEC_index (access_p, representatives, i);
3815
3816       if (!repr || no_accesses_p (repr))
3817         {
3818           struct ipa_parm_adjustment *adj;
3819
3820           adj = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
3821           memset (adj, 0, sizeof (*adj));
3822           adj->base_index = get_param_index (parm, parms);
3823           adj->base = parm;
3824           if (!repr)
3825             adj->copy_param = 1;
3826           else
3827             adj->remove_param = 1;
3828         }
3829       else
3830         {
3831           struct ipa_parm_adjustment *adj;
3832           int index = get_param_index (parm, parms);
3833
3834           for (; repr; repr = repr->next_grp)
3835             {
3836               adj = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
3837               memset (adj, 0, sizeof (*adj));
3838               gcc_assert (repr->base == parm);
3839               adj->base_index = index;
3840               adj->base = repr->base;
3841               adj->type = repr->type;
3842               adj->alias_ptr_type = reference_alias_ptr_type (repr->expr);
3843               adj->offset = repr->offset;
3844               adj->by_ref = (POINTER_TYPE_P (TREE_TYPE (repr->base))
3845                              && (repr->grp_maybe_modified
3846                                  || repr->grp_not_necessarilly_dereferenced));
3847
3848             }
3849         }
3850     }
3851   VEC_free (tree, heap, parms);
3852   return adjustments;
3853 }
3854
3855 /* Analyze the collected accesses and produce a plan what to do with the
3856    parameters in the form of adjustments, NULL meaning nothing.  */
3857
3858 static ipa_parm_adjustment_vec
3859 analyze_all_param_acesses (void)
3860 {
3861   enum ipa_splicing_result repr_state;
3862   bool proceed = false;
3863   int i, adjustments_count = 0;
3864   VEC (access_p, heap) *representatives;
3865   ipa_parm_adjustment_vec adjustments;
3866
3867   repr_state = splice_all_param_accesses (&representatives);
3868   if (repr_state == NO_GOOD_ACCESS)
3869     return NULL;
3870
3871   /* If there are any parameters passed by reference which are not modified
3872      directly, we need to check whether they can be modified indirectly.  */
3873   if (repr_state == UNMODIF_BY_REF_ACCESSES)
3874     {
3875       analyze_caller_dereference_legality (representatives);
3876       analyze_modified_params (representatives);
3877     }
3878
3879   for (i = 0; i < func_param_count; i++)
3880     {
3881       struct access *repr = VEC_index (access_p, representatives, i);
3882
3883       if (repr && !no_accesses_p (repr))
3884         {
3885           if (repr->grp_scalar_ptr)
3886             {
3887               adjustments_count++;
3888               if (repr->grp_not_necessarilly_dereferenced
3889                   || repr->grp_maybe_modified)
3890                 VEC_replace (access_p, representatives, i, NULL);
3891               else
3892                 {
3893                   proceed = true;
3894                   sra_stats.scalar_by_ref_to_by_val++;
3895                 }
3896             }
3897           else
3898             {
3899               int new_components = decide_one_param_reduction (repr);
3900
3901               if (new_components == 0)
3902                 {
3903                   VEC_replace (access_p, representatives, i, NULL);
3904                   adjustments_count++;
3905                 }
3906               else
3907                 {
3908                   adjustments_count += new_components;
3909                   sra_stats.aggregate_params_reduced++;
3910                   sra_stats.param_reductions_created += new_components;
3911                   proceed = true;
3912                 }
3913             }
3914         }
3915       else
3916         {
3917           if (no_accesses_p (repr))
3918             {
3919               proceed = true;
3920               sra_stats.deleted_unused_parameters++;
3921             }
3922           adjustments_count++;
3923         }
3924     }
3925
3926   if (!proceed && dump_file)
3927     fprintf (dump_file, "NOT proceeding to change params.\n");
3928
3929   if (proceed)
3930     adjustments = turn_representatives_into_adjustments (representatives,
3931                                                          adjustments_count);
3932   else
3933     adjustments = NULL;
3934
3935   VEC_free (access_p, heap, representatives);
3936   return adjustments;
3937 }
3938
3939 /* If a parameter replacement identified by ADJ does not yet exist in the form
3940    of declaration, create it and record it, otherwise return the previously
3941    created one.  */
3942
3943 static tree
3944 get_replaced_param_substitute (struct ipa_parm_adjustment *adj)
3945 {
3946   tree repl;
3947   if (!adj->new_ssa_base)
3948     {
3949       char *pretty_name = make_fancy_name (adj->base);
3950
3951       repl = create_tmp_reg (TREE_TYPE (adj->base), "ISR");
3952       DECL_NAME (repl) = get_identifier (pretty_name);
3953       obstack_free (&name_obstack, pretty_name);
3954
3955       get_var_ann (repl);
3956       add_referenced_var (repl);
3957       adj->new_ssa_base = repl;
3958     }
3959   else
3960     repl = adj->new_ssa_base;
3961   return repl;
3962 }
3963
3964 /* Find the first adjustment for a particular parameter BASE in a vector of
3965    ADJUSTMENTS which is not a copy_param.  Return NULL if there is no such
3966    adjustment. */
3967
3968 static struct ipa_parm_adjustment *
3969 get_adjustment_for_base (ipa_parm_adjustment_vec adjustments, tree base)
3970 {
3971   int i, len;
3972
3973   len = VEC_length (ipa_parm_adjustment_t, adjustments);
3974   for (i = 0; i < len; i++)
3975     {
3976       struct ipa_parm_adjustment *adj;
3977
3978       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
3979       if (!adj->copy_param && adj->base == base)
3980         return adj;
3981     }
3982
3983   return NULL;
3984 }
3985
3986 /* If the statement STMT defines an SSA_NAME of a parameter which is to be
3987    removed because its value is not used, replace the SSA_NAME with a one
3988    relating to a created VAR_DECL together all of its uses and return true.
3989    ADJUSTMENTS is a pointer to an adjustments vector.  */
3990
3991 static bool
3992 replace_removed_params_ssa_names (gimple stmt,
3993                                   ipa_parm_adjustment_vec adjustments)
3994 {
3995   struct ipa_parm_adjustment *adj;
3996   tree lhs, decl, repl, name;
3997
3998   if (gimple_code (stmt) == GIMPLE_PHI)
3999     lhs = gimple_phi_result (stmt);
4000   else if (is_gimple_assign (stmt))
4001     lhs = gimple_assign_lhs (stmt);
4002   else if (is_gimple_call (stmt))
4003     lhs = gimple_call_lhs (stmt);
4004   else
4005     gcc_unreachable ();
4006
4007   if (TREE_CODE (lhs) != SSA_NAME)
4008     return false;
4009   decl = SSA_NAME_VAR (lhs);
4010   if (TREE_CODE (decl) != PARM_DECL)
4011     return false;
4012
4013   adj = get_adjustment_for_base (adjustments, decl);
4014   if (!adj)
4015     return false;
4016
4017   repl = get_replaced_param_substitute (adj);
4018   name = make_ssa_name (repl, stmt);
4019
4020   if (dump_file)
4021     {
4022       fprintf (dump_file, "replacing an SSA name of a removed param ");
4023       print_generic_expr (dump_file, lhs, 0);
4024       fprintf (dump_file, " with ");
4025       print_generic_expr (dump_file, name, 0);
4026       fprintf (dump_file, "\n");
4027     }
4028
4029   if (is_gimple_assign (stmt))
4030     gimple_assign_set_lhs (stmt, name);
4031   else if (is_gimple_call (stmt))
4032     gimple_call_set_lhs (stmt, name);
4033   else
4034     gimple_phi_set_result (stmt, name);
4035
4036   replace_uses_by (lhs, name);
4037   release_ssa_name (lhs);
4038   return true;
4039 }
4040
4041 /* If the expression *EXPR should be replaced by a reduction of a parameter, do
4042    so.  ADJUSTMENTS is a pointer to a vector of adjustments.  CONVERT
4043    specifies whether the function should care about type incompatibility the
4044    current and new expressions.  If it is false, the function will leave
4045    incompatibility issues to the caller.  Return true iff the expression
4046    was modified. */
4047
4048 static bool
4049 sra_ipa_modify_expr (tree *expr, bool convert,
4050                      ipa_parm_adjustment_vec adjustments)
4051 {
4052   int i, len;
4053   struct ipa_parm_adjustment *adj, *cand = NULL;
4054   HOST_WIDE_INT offset, size, max_size;
4055   tree base, src;
4056
4057   len = VEC_length (ipa_parm_adjustment_t, adjustments);
4058
4059   if (TREE_CODE (*expr) == BIT_FIELD_REF
4060       || TREE_CODE (*expr) == IMAGPART_EXPR
4061       || TREE_CODE (*expr) == REALPART_EXPR)
4062     {
4063       expr = &TREE_OPERAND (*expr, 0);
4064       convert = true;
4065     }
4066
4067   base = get_ref_base_and_extent (*expr, &offset, &size, &max_size);
4068   if (!base || size == -1 || max_size == -1)
4069     return false;
4070
4071   if (TREE_CODE (base) == MEM_REF)
4072     {
4073       offset += mem_ref_offset (base).low * BITS_PER_UNIT;
4074       base = TREE_OPERAND (base, 0);
4075     }
4076
4077   base = get_ssa_base_param (base);
4078   if (!base || TREE_CODE (base) != PARM_DECL)
4079     return false;
4080
4081   for (i = 0; i < len; i++)
4082     {
4083       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
4084
4085       if (adj->base == base &&
4086           (adj->offset == offset || adj->remove_param))
4087         {
4088           cand = adj;
4089           break;
4090         }
4091     }
4092   if (!cand || cand->copy_param || cand->remove_param)
4093     return false;
4094
4095   if (cand->by_ref)
4096     src = build_simple_mem_ref (cand->reduction);
4097   else
4098     src = cand->reduction;
4099
4100   if (dump_file && (dump_flags & TDF_DETAILS))
4101     {
4102       fprintf (dump_file, "About to replace expr ");
4103       print_generic_expr (dump_file, *expr, 0);
4104       fprintf (dump_file, " with ");
4105       print_generic_expr (dump_file, src, 0);
4106       fprintf (dump_file, "\n");
4107     }
4108
4109   if (convert && !useless_type_conversion_p (TREE_TYPE (*expr), cand->type))
4110     {
4111       tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (*expr), src);
4112       *expr = vce;
4113     }
4114   else
4115     *expr = src;
4116   return true;
4117 }
4118
4119 /* If the statement pointed to by STMT_PTR contains any expressions that need
4120    to replaced with a different one as noted by ADJUSTMENTS, do so.  Handle any
4121    potential type incompatibilities (GSI is used to accommodate conversion
4122    statements and must point to the statement).  Return true iff the statement
4123    was modified.  */
4124
4125 static bool
4126 sra_ipa_modify_assign (gimple *stmt_ptr, gimple_stmt_iterator *gsi,
4127                        ipa_parm_adjustment_vec adjustments)
4128 {
4129   gimple stmt = *stmt_ptr;
4130   tree *lhs_p, *rhs_p;
4131   bool any;
4132
4133   if (!gimple_assign_single_p (stmt))
4134     return false;
4135
4136   rhs_p = gimple_assign_rhs1_ptr (stmt);
4137   lhs_p = gimple_assign_lhs_ptr (stmt);
4138
4139   any = sra_ipa_modify_expr (rhs_p, false, adjustments);
4140   any |= sra_ipa_modify_expr (lhs_p, false, adjustments);
4141   if (any)
4142     {
4143       tree new_rhs = NULL_TREE;
4144
4145       if (!useless_type_conversion_p (TREE_TYPE (*lhs_p), TREE_TYPE (*rhs_p)))
4146         {
4147           if (TREE_CODE (*rhs_p) == CONSTRUCTOR)
4148             {
4149               /* V_C_Es of constructors can cause trouble (PR 42714).  */
4150               if (is_gimple_reg_type (TREE_TYPE (*lhs_p)))
4151                 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
4152               else
4153                 *rhs_p = build_constructor (TREE_TYPE (*lhs_p), 0);
4154             }
4155           else
4156             new_rhs = fold_build1_loc (gimple_location (stmt),
4157                                        VIEW_CONVERT_EXPR, TREE_TYPE (*lhs_p),
4158                                        *rhs_p);
4159         }
4160       else if (REFERENCE_CLASS_P (*rhs_p)
4161                && is_gimple_reg_type (TREE_TYPE (*lhs_p))
4162                && !is_gimple_reg (*lhs_p))
4163         /* This can happen when an assignment in between two single field
4164            structures is turned into an assignment in between two pointers to
4165            scalars (PR 42237).  */
4166         new_rhs = *rhs_p;
4167
4168       if (new_rhs)
4169         {
4170           tree tmp = force_gimple_operand_gsi (gsi, new_rhs, true, NULL_TREE,
4171                                                true, GSI_SAME_STMT);
4172
4173           gimple_assign_set_rhs_from_tree (gsi, tmp);
4174         }
4175
4176       return true;
4177     }
4178
4179   return false;
4180 }
4181
4182 /* Traverse the function body and all modifications as described in
4183    ADJUSTMENTS.  Return true iff the CFG has been changed.  */
4184
4185 static bool
4186 ipa_sra_modify_function_body (ipa_parm_adjustment_vec adjustments)
4187 {
4188   bool cfg_changed = false;
4189   basic_block bb;
4190
4191   FOR_EACH_BB (bb)
4192     {
4193       gimple_stmt_iterator gsi;
4194
4195       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4196         replace_removed_params_ssa_names (gsi_stmt (gsi), adjustments);
4197
4198       gsi = gsi_start_bb (bb);
4199       while (!gsi_end_p (gsi))
4200         {
4201           gimple stmt = gsi_stmt (gsi);
4202           bool modified = false;
4203           tree *t;
4204           unsigned i;
4205
4206           switch (gimple_code (stmt))
4207             {
4208             case GIMPLE_RETURN:
4209               t = gimple_return_retval_ptr (stmt);
4210               if (*t != NULL_TREE)
4211                 modified |= sra_ipa_modify_expr (t, true, adjustments);
4212               break;
4213
4214             case GIMPLE_ASSIGN:
4215               modified |= sra_ipa_modify_assign (&stmt, &gsi, adjustments);
4216               modified |= replace_removed_params_ssa_names (stmt, adjustments);
4217               break;
4218
4219             case GIMPLE_CALL:
4220               /* Operands must be processed before the lhs.  */
4221               for (i = 0; i < gimple_call_num_args (stmt); i++)
4222                 {
4223                   t = gimple_call_arg_ptr (stmt, i);
4224                   modified |= sra_ipa_modify_expr (t, true, adjustments);
4225                 }
4226
4227               if (gimple_call_lhs (stmt))
4228                 {
4229                   t = gimple_call_lhs_ptr (stmt);
4230                   modified |= sra_ipa_modify_expr (t, false, adjustments);
4231                   modified |= replace_removed_params_ssa_names (stmt,
4232                                                                 adjustments);
4233                 }
4234               break;
4235
4236             case GIMPLE_ASM:
4237               for (i = 0; i < gimple_asm_ninputs (stmt); i++)
4238                 {
4239                   t = &TREE_VALUE (gimple_asm_input_op (stmt, i));
4240                   modified |= sra_ipa_modify_expr (t, true, adjustments);
4241                 }
4242               for (i = 0; i < gimple_asm_noutputs (stmt); i++)
4243                 {
4244                   t = &TREE_VALUE (gimple_asm_output_op (stmt, i));
4245                   modified |= sra_ipa_modify_expr (t, false, adjustments);
4246                 }
4247               break;
4248
4249             default:
4250               break;
4251             }
4252
4253           if (modified)
4254             {
4255               update_stmt (stmt);
4256               if (maybe_clean_eh_stmt (stmt)
4257                   && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
4258                 cfg_changed = true;
4259             }
4260           gsi_next (&gsi);
4261         }
4262     }
4263
4264   return cfg_changed;
4265 }
4266
4267 /* Call gimple_debug_bind_reset_value on all debug statements describing
4268    gimple register parameters that are being removed or replaced.  */
4269
4270 static void
4271 sra_ipa_reset_debug_stmts (ipa_parm_adjustment_vec adjustments)
4272 {
4273   int i, len;
4274
4275   len = VEC_length (ipa_parm_adjustment_t, adjustments);
4276   for (i = 0; i < len; i++)
4277     {
4278       struct ipa_parm_adjustment *adj;
4279       imm_use_iterator ui;
4280       gimple stmt;
4281       tree name;
4282
4283       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
4284       if (adj->copy_param || !is_gimple_reg (adj->base))
4285         continue;
4286       name = gimple_default_def (cfun, adj->base);
4287       if (!name)
4288         continue;
4289       FOR_EACH_IMM_USE_STMT (stmt, ui, name)
4290         {
4291           /* All other users must have been removed by
4292              ipa_sra_modify_function_body.  */
4293           gcc_assert (is_gimple_debug (stmt));
4294           gimple_debug_bind_reset_value (stmt);
4295           update_stmt (stmt);
4296         }
4297     }
4298 }
4299
4300 /* Return true iff all callers have at least as many actual arguments as there
4301    are formal parameters in the current function.  */
4302
4303 static bool
4304 all_callers_have_enough_arguments_p (struct cgraph_node *node)
4305 {
4306   struct cgraph_edge *cs;
4307   for (cs = node->callers; cs; cs = cs->next_caller)
4308     if (!callsite_has_enough_arguments_p (cs->call_stmt))
4309       return false;
4310
4311   return true;
4312 }
4313
4314
4315 /* Convert all callers of NODE to pass parameters as given in ADJUSTMENTS.  */
4316
4317 static void
4318 convert_callers (struct cgraph_node *node, tree old_decl,
4319                  ipa_parm_adjustment_vec adjustments)
4320 {
4321   tree old_cur_fndecl = current_function_decl;
4322   struct cgraph_edge *cs;
4323   basic_block this_block;
4324   bitmap recomputed_callers = BITMAP_ALLOC (NULL);
4325
4326   for (cs = node->callers; cs; cs = cs->next_caller)
4327     {
4328       current_function_decl = cs->caller->decl;
4329       push_cfun (DECL_STRUCT_FUNCTION (cs->caller->decl));
4330
4331       if (dump_file)
4332         fprintf (dump_file, "Adjusting call (%i -> %i) %s -> %s\n",
4333                  cs->caller->uid, cs->callee->uid,
4334                  cgraph_node_name (cs->caller),
4335                  cgraph_node_name (cs->callee));
4336
4337       ipa_modify_call_arguments (cs, cs->call_stmt, adjustments);
4338
4339       pop_cfun ();
4340     }
4341
4342   for (cs = node->callers; cs; cs = cs->next_caller)
4343     if (bitmap_set_bit (recomputed_callers, cs->caller->uid))
4344       compute_inline_parameters (cs->caller);
4345   BITMAP_FREE (recomputed_callers);
4346
4347   current_function_decl = old_cur_fndecl;
4348
4349   if (!encountered_recursive_call)
4350     return;
4351
4352   FOR_EACH_BB (this_block)
4353     {
4354       gimple_stmt_iterator gsi;
4355
4356       for (gsi = gsi_start_bb (this_block); !gsi_end_p (gsi); gsi_next (&gsi))
4357         {
4358           gimple stmt = gsi_stmt (gsi);
4359           tree call_fndecl;
4360           if (gimple_code (stmt) != GIMPLE_CALL)
4361             continue;
4362           call_fndecl = gimple_call_fndecl (stmt);
4363           if (call_fndecl == old_decl)
4364             {
4365               if (dump_file)
4366                 fprintf (dump_file, "Adjusting recursive call");
4367               gimple_call_set_fndecl (stmt, node->decl);
4368               ipa_modify_call_arguments (NULL, stmt, adjustments);
4369             }
4370         }
4371     }
4372
4373   return;
4374 }
4375
4376 /* Perform all the modification required in IPA-SRA for NODE to have parameters
4377    as given in ADJUSTMENTS.  Return true iff the CFG has been changed.  */
4378
4379 static bool
4380 modify_function (struct cgraph_node *node, ipa_parm_adjustment_vec adjustments)
4381 {
4382   struct cgraph_node *new_node;
4383   struct cgraph_edge *cs;
4384   bool cfg_changed;
4385   VEC (cgraph_edge_p, heap) * redirect_callers;
4386   int node_callers;
4387
4388   node_callers = 0;
4389   for (cs = node->callers; cs != NULL; cs = cs->next_caller)
4390     node_callers++;
4391   redirect_callers = VEC_alloc (cgraph_edge_p, heap, node_callers);
4392   for (cs = node->callers; cs != NULL; cs = cs->next_caller)
4393     VEC_quick_push (cgraph_edge_p, redirect_callers, cs);
4394
4395   rebuild_cgraph_edges ();
4396   pop_cfun ();
4397   current_function_decl = NULL_TREE;
4398
4399   new_node = cgraph_function_versioning (node, redirect_callers, NULL, NULL,
4400                                          NULL, NULL, "isra");
4401   current_function_decl = new_node->decl;
4402   push_cfun (DECL_STRUCT_FUNCTION (new_node->decl));
4403
4404   ipa_modify_formal_parameters (current_function_decl, adjustments, "ISRA");
4405   cfg_changed = ipa_sra_modify_function_body (adjustments);
4406   sra_ipa_reset_debug_stmts (adjustments);
4407   convert_callers (new_node, node->decl, adjustments);
4408   cgraph_make_node_local (new_node);
4409   return cfg_changed;
4410 }
4411
4412 /* Return false the function is apparently unsuitable for IPA-SRA based on it's
4413    attributes, return true otherwise.  NODE is the cgraph node of the current
4414    function.  */
4415
4416 static bool
4417 ipa_sra_preliminary_function_checks (struct cgraph_node *node)
4418 {
4419   if (!cgraph_node_can_be_local_p (node))
4420     {
4421       if (dump_file)
4422         fprintf (dump_file, "Function not local to this compilation unit.\n");
4423       return false;
4424     }
4425
4426   if (!node->local.can_change_signature)
4427     {
4428       if (dump_file)
4429         fprintf (dump_file, "Function can not change signature.\n");
4430       return false;
4431     }
4432
4433   if (!tree_versionable_function_p (node->decl))
4434     {
4435       if (dump_file)
4436         fprintf (dump_file, "Function is not versionable.\n");
4437       return false;
4438     }
4439
4440   if (DECL_VIRTUAL_P (current_function_decl))
4441     {
4442       if (dump_file)
4443         fprintf (dump_file, "Function is a virtual method.\n");
4444       return false;
4445     }
4446
4447   if ((DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
4448       && node->global.size >= MAX_INLINE_INSNS_AUTO)
4449     {
4450       if (dump_file)
4451         fprintf (dump_file, "Function too big to be made truly local.\n");
4452       return false;
4453     }
4454
4455   if (!node->callers)
4456     {
4457       if (dump_file)
4458         fprintf (dump_file,
4459                  "Function has no callers in this compilation unit.\n");
4460       return false;
4461     }
4462
4463   if (cfun->stdarg)
4464     {
4465       if (dump_file)
4466         fprintf (dump_file, "Function uses stdarg. \n");
4467       return false;
4468     }
4469
4470   if (TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
4471     return false;
4472
4473   return true;
4474 }
4475
4476 /* Perform early interprocedural SRA.  */
4477
4478 static unsigned int
4479 ipa_early_sra (void)
4480 {
4481   struct cgraph_node *node = cgraph_node (current_function_decl);
4482   ipa_parm_adjustment_vec adjustments;
4483   int ret = 0;
4484
4485   if (!ipa_sra_preliminary_function_checks (node))
4486     return 0;
4487
4488   sra_initialize ();
4489   sra_mode = SRA_MODE_EARLY_IPA;
4490
4491   if (!find_param_candidates ())
4492     {
4493       if (dump_file)
4494         fprintf (dump_file, "Function has no IPA-SRA candidates.\n");
4495       goto simple_out;
4496     }
4497
4498   if (!all_callers_have_enough_arguments_p (node))
4499     {
4500       if (dump_file)
4501         fprintf (dump_file, "There are callers with insufficient number of "
4502                  "arguments.\n");
4503       goto simple_out;
4504     }
4505
4506   bb_dereferences = XCNEWVEC (HOST_WIDE_INT,
4507                                  func_param_count
4508                                  * last_basic_block_for_function (cfun));
4509   final_bbs = BITMAP_ALLOC (NULL);
4510
4511   scan_function ();
4512   if (encountered_apply_args)
4513     {
4514       if (dump_file)
4515         fprintf (dump_file, "Function calls  __builtin_apply_args().\n");
4516       goto out;
4517     }
4518
4519   if (encountered_unchangable_recursive_call)
4520     {
4521       if (dump_file)
4522         fprintf (dump_file, "Function calls itself with insufficient "
4523                  "number of arguments.\n");
4524       goto out;
4525     }
4526
4527   adjustments = analyze_all_param_acesses ();
4528   if (!adjustments)
4529     goto out;
4530   if (dump_file)
4531     ipa_dump_param_adjustments (dump_file, adjustments, current_function_decl);
4532
4533   if (modify_function (node, adjustments))
4534     ret = TODO_update_ssa | TODO_cleanup_cfg;
4535   else
4536     ret = TODO_update_ssa;
4537   VEC_free (ipa_parm_adjustment_t, heap, adjustments);
4538
4539   statistics_counter_event (cfun, "Unused parameters deleted",
4540                             sra_stats.deleted_unused_parameters);
4541   statistics_counter_event (cfun, "Scalar parameters converted to by-value",
4542                             sra_stats.scalar_by_ref_to_by_val);
4543   statistics_counter_event (cfun, "Aggregate parameters broken up",
4544                             sra_stats.aggregate_params_reduced);
4545   statistics_counter_event (cfun, "Aggregate parameter components created",
4546                             sra_stats.param_reductions_created);
4547
4548  out:
4549   BITMAP_FREE (final_bbs);
4550   free (bb_dereferences);
4551  simple_out:
4552   sra_deinitialize ();
4553   return ret;
4554 }
4555
4556 /* Return if early ipa sra shall be performed.  */
4557 static bool
4558 ipa_early_sra_gate (void)
4559 {
4560   return flag_ipa_sra && dbg_cnt (eipa_sra);
4561 }
4562
4563 struct gimple_opt_pass pass_early_ipa_sra =
4564 {
4565  {
4566   GIMPLE_PASS,
4567   "eipa_sra",                           /* name */
4568   ipa_early_sra_gate,                   /* gate */
4569   ipa_early_sra,                        /* execute */
4570   NULL,                                 /* sub */
4571   NULL,                                 /* next */
4572   0,                                    /* static_pass_number */
4573   TV_IPA_SRA,                           /* tv_id */
4574   0,                                    /* properties_required */
4575   0,                                    /* properties_provided */
4576   0,                                    /* properties_destroyed */
4577   0,                                    /* todo_flags_start */
4578   TODO_dump_func | TODO_dump_cgraph     /* todo_flags_finish */
4579  }
4580 };
4581
4582