LCOV - code coverage report
Current view: top level - gcc - tree-sra.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.9 % 2329 2234
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 112 112
Legend: Lines:     hit not hit

            Line data    Source code
       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-2026 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 "backend.h"
      78              : #include "target.h"
      79              : #include "rtl.h"
      80              : #include "tree.h"
      81              : #include "gimple.h"
      82              : #include "predict.h"
      83              : #include "alloc-pool.h"
      84              : #include "tree-pass.h"
      85              : #include "ssa.h"
      86              : #include "cgraph.h"
      87              : #include "gimple-pretty-print.h"
      88              : #include "alias.h"
      89              : #include "fold-const.h"
      90              : #include "tree-eh.h"
      91              : #include "stor-layout.h"
      92              : #include "gimplify.h"
      93              : #include "gimple-iterator.h"
      94              : #include "gimplify-me.h"
      95              : #include "gimple-walk.h"
      96              : #include "tree-cfg.h"
      97              : #include "tree-dfa.h"
      98              : #include "tree-ssa.h"
      99              : #include "dbgcnt.h"
     100              : #include "builtins.h"
     101              : #include "tree-sra.h"
     102              : #include "opts.h"
     103              : #include "tree-ssa-alias-compare.h"
     104              : 
     105              : /* Enumeration of all aggregate reductions we can do.  */
     106              : enum sra_mode { SRA_MODE_EARLY_IPA,   /* early call regularization */
     107              :                 SRA_MODE_EARLY_INTRA, /* early intraprocedural SRA */
     108              :                 SRA_MODE_INTRA };     /* late intraprocedural SRA */
     109              : 
     110              : /* Global variable describing which aggregate reduction we are performing at
     111              :    the moment.  */
     112              : static enum sra_mode sra_mode;
     113              : 
     114              : struct assign_link;
     115              : 
     116              : /* ACCESS represents each access to an aggregate variable (as a whole or a
     117              :    part).  It can also represent a group of accesses that refer to exactly the
     118              :    same fragment of an aggregate (i.e. those that have exactly the same offset
     119              :    and size).  Such representatives for a single aggregate, once determined,
     120              :    are linked in a linked list and have the group fields set.
     121              : 
     122              :    Moreover, when doing intraprocedural SRA, a tree is built from those
     123              :    representatives (by the means of first_child and next_sibling pointers), in
     124              :    which all items in a subtree are "within" the root, i.e. their offset is
     125              :    greater or equal to offset of the root and offset+size is smaller or equal
     126              :    to offset+size of the root.  Children of an access are sorted by offset.
     127              : 
     128              :    Note that accesses to parts of vector and complex number types always
     129              :    represented by an access to the whole complex number or a vector.  It is a
     130              :    duty of the modifying functions to replace them appropriately.  */
     131              : 
     132              : struct access
     133              : {
     134              :   /* Values returned by  `get_ref_base_and_extent' for each component reference
     135              :      If EXPR isn't a component reference  just set `BASE = EXPR', `OFFSET = 0',
     136              :      `SIZE = TREE_SIZE (TREE_TYPE (expr))'.  */
     137              :   HOST_WIDE_INT offset;
     138              :   HOST_WIDE_INT size;
     139              :   tree base;
     140              : 
     141              :   /* Expression.  It is context dependent so do not use it to create new
     142              :      expressions to access the original aggregate.  See PR 42154 for a
     143              :      testcase.  */
     144              :   tree expr;
     145              :   /* Type.  */
     146              :   tree type;
     147              : 
     148              :   /* The statement this access belongs to.  */
     149              :   gimple *stmt;
     150              : 
     151              :   /* Next group representative for this aggregate. */
     152              :   struct access *next_grp;
     153              : 
     154              :   /* Pointer to the group representative.  Pointer to itself if the struct is
     155              :      the representative.  */
     156              :   struct access *group_representative;
     157              : 
     158              :   /* After access tree has been constructed, this points to the parent of the
     159              :      current access, if there is one.  NULL for roots.  */
     160              :   struct access *parent;
     161              : 
     162              :   /* If this access has any children (in terms of the definition above), this
     163              :      points to the first one.  */
     164              :   struct access *first_child;
     165              : 
     166              :   /* In intraprocedural SRA, pointer to the next sibling in the access tree as
     167              :      described above.  */
     168              :   struct access *next_sibling;
     169              : 
     170              :   /* Pointers to the first and last element in the linked list of assign
     171              :      links for propagation from LHS to RHS.  */
     172              :   struct assign_link *first_rhs_link, *last_rhs_link;
     173              : 
     174              :   /* Pointers to the first and last element in the linked list of assign
     175              :      links for propagation from LHS to RHS.  */
     176              :   struct assign_link *first_lhs_link, *last_lhs_link;
     177              : 
     178              :   /* Pointer to the next access in the work queues.  */
     179              :   struct access *next_rhs_queued, *next_lhs_queued;
     180              : 
     181              :   /* Replacement variable for this access "region."  Never to be accessed
     182              :      directly, always only by the means of get_access_replacement() and only
     183              :      when grp_to_be_replaced flag is set.  */
     184              :   tree replacement_decl;
     185              : 
     186              :   /* Is this access made in reverse storage order? */
     187              :   unsigned reverse : 1;
     188              : 
     189              :   /* Is this particular access write access? */
     190              :   unsigned write : 1;
     191              : 
     192              :   /* Is this access currently in the rhs work queue?  */
     193              :   unsigned grp_rhs_queued : 1;
     194              : 
     195              :   /* Is this access currently in the lhs work queue?  */
     196              :   unsigned grp_lhs_queued : 1;
     197              : 
     198              :   /* Does this group contain a write access?  This flag is propagated down the
     199              :      access tree.  */
     200              :   unsigned grp_write : 1;
     201              : 
     202              :   /* Does this group contain a read access?  This flag is propagated down the
     203              :      access tree.  */
     204              :   unsigned grp_read : 1;
     205              : 
     206              :   /* Does this group contain a read access that comes from an assignment
     207              :      statement?  This flag is propagated down the access tree.  */
     208              :   unsigned grp_assignment_read : 1;
     209              : 
     210              :   /* Does this group contain a write access that comes from an assignment
     211              :      statement?  This flag is propagated down the access tree.  */
     212              :   unsigned grp_assignment_write : 1;
     213              : 
     214              :   /* Does this group contain a read access through a scalar type?  This flag is
     215              :      not propagated in the access tree in any direction.  */
     216              :   unsigned grp_scalar_read : 1;
     217              : 
     218              :   /* Does this group contain a write access through a scalar type?  This flag
     219              :      is not propagated in the access tree in any direction.  */
     220              :   unsigned grp_scalar_write : 1;
     221              : 
     222              :   /* In a root of an access tree, true means that the entire tree should be
     223              :      totally scalarized - that all scalar leafs should be scalarized and
     224              :      non-root grp_total_scalarization accesses should be honored.  Otherwise,
     225              :      non-root accesses with grp_total_scalarization should never get scalar
     226              :      replacements.  */
     227              :   unsigned grp_total_scalarization : 1;
     228              : 
     229              :   /* Other passes of the analysis use this bit to make function
     230              :      analyze_access_subtree create scalar replacements for this group if
     231              :      possible.  */
     232              :   unsigned grp_hint : 1;
     233              : 
     234              :   /* Is the subtree rooted in this access fully covered by scalar
     235              :      replacements?  */
     236              :   unsigned grp_covered : 1;
     237              : 
     238              :   /* If set to true, this access and all below it in an access tree must not be
     239              :      scalarized.  */
     240              :   unsigned grp_unscalarizable_region : 1;
     241              : 
     242              :   /* Whether data have been written to parts of the aggregate covered by this
     243              :      access which is not to be scalarized.  This flag is propagated up in the
     244              :      access tree.  */
     245              :   unsigned grp_unscalarized_data : 1;
     246              : 
     247              :   /* Set if all accesses in the group consist of the same chain of
     248              :      COMPONENT_REFs and ARRAY_REFs.  */
     249              :   unsigned grp_same_access_path : 1;
     250              : 
     251              :   /* Does this access and/or group contain a write access through a
     252              :      BIT_FIELD_REF?  */
     253              :   unsigned grp_partial_lhs : 1;
     254              : 
     255              :   /* Set when a scalar replacement should be created for this variable.  */
     256              :   unsigned grp_to_be_replaced : 1;
     257              : 
     258              :   /* Set when we want a replacement for the sole purpose of having it in
     259              :      generated debug statements.  */
     260              :   unsigned grp_to_be_debug_replaced : 1;
     261              : 
     262              :   /* Should TREE_NO_WARNING of a replacement be set?  */
     263              :   unsigned grp_no_warning : 1;
     264              : 
     265              :   /* Result of propagation across link from LHS to RHS.  */
     266              :   unsigned grp_result_of_prop_from_lhs : 1;
     267              : };
     268              : 
     269              : typedef struct access *access_p;
     270              : 
     271              : 
     272              : /* Alloc pool for allocating access structures.  */
     273              : static object_allocator<struct access> access_pool ("SRA accesses");
     274              : 
     275              : /* A structure linking lhs and rhs accesses from an aggregate assignment.  They
     276              :    are used to propagate subaccesses from rhs to lhs and vice versa as long as
     277              :    they don't conflict with what is already there.  In the RHS->LHS direction,
     278              :    we also propagate grp_write flag to lazily mark that the access contains any
     279              :    meaningful data.  */
     280              : struct assign_link
     281              : {
     282              :   struct access *lacc, *racc;
     283              :   struct assign_link *next_rhs, *next_lhs;
     284              : };
     285              : 
     286              : /* Alloc pool for allocating assign link structures.  */
     287              : static object_allocator<assign_link> assign_link_pool ("SRA links");
     288              : 
     289              : /* Base (tree) -> Vector (vec<access_p> *) map.  */
     290              : static hash_map<tree, auto_vec<access_p> > *base_access_vec;
     291              : 
     292              : /* Hash to limit creation of artificial accesses */
     293              : static hash_map<tree, unsigned> *propagation_budget;
     294              : 
     295              : /* Candidate hash table helpers.  */
     296              : 
     297              : struct uid_decl_hasher : nofree_ptr_hash <tree_node>
     298              : {
     299              :   static inline hashval_t hash (const tree_node *);
     300              :   static inline bool equal (const tree_node *, const tree_node *);
     301              : };
     302              : 
     303              : /* Hash a tree in a uid_decl_map.  */
     304              : 
     305              : inline hashval_t
     306     82632626 : uid_decl_hasher::hash (const tree_node *item)
     307              : {
     308     82632626 :   return item->decl_minimal.uid;
     309              : }
     310              : 
     311              : /* Return true if the DECL_UID in both trees are equal.  */
     312              : 
     313              : inline bool
     314     95179273 : uid_decl_hasher::equal (const tree_node *a, const tree_node *b)
     315              : {
     316     95179273 :   return (a->decl_minimal.uid == b->decl_minimal.uid);
     317              : }
     318              : 
     319              : /* Set of candidates.  */
     320              : static bitmap candidate_bitmap;
     321              : static hash_table<uid_decl_hasher> *candidates;
     322              : 
     323              : /* For a candidate UID return the candidates decl.  */
     324              : 
     325              : static inline tree
     326     15235218 : candidate (unsigned uid)
     327              : {
     328     15235218 :  tree_node t;
     329     15235218 :  t.decl_minimal.uid = uid;
     330     15235218 :  return candidates->find_with_hash (&t, static_cast <hashval_t> (uid));
     331              : }
     332              : 
     333              : /* Bitmap of candidates which we should try to entirely scalarize away and
     334              :    those which cannot be (because they are and need be used as a whole).  */
     335              : static bitmap should_scalarize_away_bitmap, cannot_scalarize_away_bitmap;
     336              : 
     337              : /* Bitmap of candidates in the constant pool, which cannot be scalarized
     338              :    because this would produce non-constant expressions (e.g. Ada).  */
     339              : static bitmap disqualified_constants;
     340              : 
     341              : /* Bitmap of candidates which are passed by reference in call arguments.  */
     342              : static bitmap passed_by_ref_in_call;
     343              : 
     344              : /* Obstack for creation of fancy names.  */
     345              : static struct obstack name_obstack;
     346              : 
     347              : /* Head of a linked list of accesses that need to have its subaccesses
     348              :    propagated to their assignment counterparts. */
     349              : static struct access *rhs_work_queue_head, *lhs_work_queue_head;
     350              : 
     351              : /* Dump contents of ACCESS to file F in a human friendly way.  If GRP is true,
     352              :    representative fields are dumped, otherwise those which only describe the
     353              :    individual access are.  */
     354              : 
     355              : static struct
     356              : {
     357              :   /* Number of processed aggregates is readily available in
     358              :      analyze_all_variable_accesses and so is not stored here.  */
     359              : 
     360              :   /* Number of created scalar replacements.  */
     361              :   int replacements;
     362              : 
     363              :   /* Number of times sra_modify_expr or sra_modify_assign themselves changed an
     364              :      expression.  */
     365              :   int exprs;
     366              : 
     367              :   /* Number of statements created by generate_subtree_copies.  */
     368              :   int subtree_copies;
     369              : 
     370              :   /* Number of statements created by load_assign_lhs_subreplacements.  */
     371              :   int subreplacements;
     372              : 
     373              :   /* Number of times sra_modify_assign has deleted a statement.  */
     374              :   int deleted;
     375              : 
     376              :   /* Number of times sra_modify_assign has to deal with subaccesses of LHS and
     377              :      RHS reparately due to type conversions or nonexistent matching
     378              :      references.  */
     379              :   int separate_lhs_rhs_handling;
     380              : 
     381              :   /* Number of parameters that were removed because they were unused.  */
     382              :   int deleted_unused_parameters;
     383              : 
     384              :   /* Number of scalars passed as parameters by reference that have been
     385              :      converted to be passed by value.  */
     386              :   int scalar_by_ref_to_by_val;
     387              : 
     388              :   /* Number of aggregate parameters that were replaced by one or more of their
     389              :      components.  */
     390              :   int aggregate_params_reduced;
     391              : 
     392              :   /* Number of components created when splitting aggregate parameters.  */
     393              :   int param_reductions_created;
     394              : 
     395              :   /* Number of deferred_init calls that are modified.  */
     396              :   int deferred_init;
     397              : 
     398              :   /* Number of deferred_init calls that are created by
     399              :      generate_subtree_deferred_init.  */
     400              :   int subtree_deferred_init;
     401              : } sra_stats;
     402              : 
     403              : static void
     404          131 : dump_access (FILE *f, struct access *access, bool grp)
     405              : {
     406          131 :   fprintf (f, "access { ");
     407          131 :   fprintf (f, "base = (%d)'", DECL_UID (access->base));
     408          131 :   print_generic_expr (f, access->base);
     409          131 :   fprintf (f, "', offset = " HOST_WIDE_INT_PRINT_DEC, access->offset);
     410          131 :   fprintf (f, ", size = " HOST_WIDE_INT_PRINT_DEC, access->size);
     411          131 :   fprintf (f, ", expr = ");
     412          131 :   print_generic_expr (f, access->expr);
     413          131 :   fprintf (f, ", type = ");
     414          131 :   print_generic_expr (f, access->type);
     415          131 :   fprintf (f, ", reverse = %d", access->reverse);
     416          131 :   if (grp)
     417          131 :     fprintf (f, ", grp_read = %d, grp_write = %d, grp_assignment_read = %d, "
     418              :              "grp_assignment_write = %d, grp_scalar_read = %d, "
     419              :              "grp_scalar_write = %d, grp_total_scalarization = %d, "
     420              :              "grp_hint = %d, grp_covered = %d, "
     421              :              "grp_unscalarizable_region = %d, grp_unscalarized_data = %d, "
     422              :              "grp_same_access_path = %d, grp_partial_lhs = %d, "
     423              :              "grp_to_be_replaced = %d, grp_to_be_debug_replaced = %d}\n",
     424          131 :              access->grp_read, access->grp_write, access->grp_assignment_read,
     425          131 :              access->grp_assignment_write, access->grp_scalar_read,
     426          131 :              access->grp_scalar_write, access->grp_total_scalarization,
     427          131 :              access->grp_hint, access->grp_covered,
     428          131 :              access->grp_unscalarizable_region, access->grp_unscalarized_data,
     429          131 :              access->grp_same_access_path, access->grp_partial_lhs,
     430          131 :              access->grp_to_be_replaced, access->grp_to_be_debug_replaced);
     431              :   else
     432            0 :     fprintf (f, ", write = %d, grp_total_scalarization = %d, "
     433              :              "grp_partial_lhs = %d}\n",
     434            0 :              access->write, access->grp_total_scalarization,
     435            0 :              access->grp_partial_lhs);
     436          131 : }
     437              : 
     438              : /* Dump a subtree rooted in ACCESS to file F, indent by LEVEL.  */
     439              : 
     440              : static void
     441           61 : dump_access_tree_1 (FILE *f, struct access *access, int level)
     442              : {
     443          131 :   do
     444              :     {
     445          131 :       int i;
     446              : 
     447          319 :       for (i = 0; i < level; i++)
     448          188 :         fputs ("* ", f);
     449              : 
     450          131 :       dump_access (f, access, true);
     451              : 
     452          131 :       if (access->first_child)
     453           36 :         dump_access_tree_1 (f, access->first_child, level + 1);
     454              : 
     455          131 :       access = access->next_sibling;
     456              :     }
     457          131 :   while (access);
     458           61 : }
     459              : 
     460              : /* Dump all access trees for a variable, given the pointer to the first root in
     461              :    ACCESS.  */
     462              : 
     463              : static void
     464           24 : dump_access_tree (FILE *f, struct access *access)
     465              : {
     466           49 :   for (; access; access = access->next_grp)
     467           25 :     dump_access_tree_1 (f, access, 0);
     468           24 : }
     469              : 
     470              : /* Return true iff ACC is non-NULL and has subaccesses.  */
     471              : 
     472              : static inline bool
     473     16801784 : access_has_children_p (struct access *acc)
     474              : {
     475      8954946 :   return acc && acc->first_child;
     476              : }
     477              : 
     478              : /* Return true iff ACC is (partly) covered by at least one replacement.  */
     479              : 
     480              : static bool
     481          553 : access_has_replacements_p (struct access *acc)
     482              : {
     483          553 :   struct access *child;
     484          553 :   if (acc->grp_to_be_replaced)
     485              :     return true;
     486          559 :   for (child = acc->first_child; child; child = child->next_sibling)
     487            6 :     if (access_has_replacements_p (child))
     488              :       return true;
     489              :   return false;
     490              : }
     491              : 
     492              : /* Return a vector of pointers to accesses for the variable given in BASE or
     493              :    NULL if there is none.  */
     494              : 
     495              : static vec<access_p> *
     496     24556883 : get_base_access_vector (tree base)
     497              : {
     498            0 :   return base_access_vec->get (base);
     499              : }
     500              : 
     501              : /* Find an access with required OFFSET and SIZE in a subtree of accesses rooted
     502              :    in ACCESS.  Return NULL if it cannot be found.  */
     503              : 
     504              : static struct access *
     505     10793734 : find_access_in_subtree (struct access *access, HOST_WIDE_INT offset,
     506              :                         HOST_WIDE_INT size)
     507              : {
     508     16678706 :   while (access && (access->offset != offset || access->size != size))
     509              :     {
     510      5884972 :       struct access *child = access->first_child;
     511              : 
     512     13211783 :       while (child && (child->offset + child->size <= offset))
     513      7326811 :         child = child->next_sibling;
     514      5884972 :       access = child;
     515              :     }
     516              : 
     517              :   /* Total scalarization does not replace single field structures with their
     518              :      single field but rather creates an access for them underneath.  Look for
     519              :      it.  */
     520     10793734 :   if (access)
     521     10837035 :     while (access->first_child
     522      3224944 :            && access->first_child->offset == offset
     523     13969552 :            && access->first_child->size == size)
     524              :       access = access->first_child;
     525              : 
     526     10793734 :   return access;
     527              : }
     528              : 
     529              : /* Return the first group representative for DECL or NULL if none exists.  */
     530              : 
     531              : static struct access *
     532     20203757 : get_first_repr_for_decl (tree base)
     533              : {
     534     20203757 :   vec<access_p> *access_vec;
     535              : 
     536     20203757 :   access_vec = get_base_access_vector (base);
     537     20203757 :   if (!access_vec)
     538              :     return NULL;
     539              : 
     540     20203757 :   return (*access_vec)[0];
     541              : }
     542              : 
     543              : /* Find an access representative for the variable BASE and given OFFSET and
     544              :    SIZE.  Requires that access trees have already been built.  Return NULL if
     545              :    it cannot be found.  */
     546              : 
     547              : static struct access *
     548      9709931 : get_var_base_offset_size_access (tree base, HOST_WIDE_INT offset,
     549              :                                  HOST_WIDE_INT size)
     550              : {
     551      9709931 :   struct access *access;
     552              : 
     553      9709931 :   access = get_first_repr_for_decl (base);
     554     22549620 :   while (access && (access->offset + access->size <= offset))
     555      3129758 :     access = access->next_grp;
     556      9709931 :   if (!access)
     557              :     return NULL;
     558              : 
     559      9709931 :   return find_access_in_subtree (access, offset, size);
     560              : }
     561              : 
     562              : /* Add LINK to the linked list of assign links of RACC.  */
     563              : 
     564              : static void
     565      1367096 : add_link_to_rhs (struct access *racc, struct assign_link *link)
     566              : {
     567      1367096 :   gcc_assert (link->racc == racc);
     568              : 
     569      1367096 :   if (!racc->first_rhs_link)
     570              :     {
     571      1367096 :       gcc_assert (!racc->last_rhs_link);
     572      1367096 :       racc->first_rhs_link = link;
     573              :     }
     574              :   else
     575            0 :     racc->last_rhs_link->next_rhs = link;
     576              : 
     577      1367096 :   racc->last_rhs_link = link;
     578      1367096 :   link->next_rhs = NULL;
     579      1367096 : }
     580              : 
     581              : /* Add LINK to the linked list of lhs assign links of LACC.  */
     582              : 
     583              : static void
     584      1367096 : add_link_to_lhs (struct access *lacc, struct assign_link *link)
     585              : {
     586      1367096 :   gcc_assert (link->lacc == lacc);
     587              : 
     588      1367096 :   if (!lacc->first_lhs_link)
     589              :     {
     590      1367096 :       gcc_assert (!lacc->last_lhs_link);
     591      1367096 :       lacc->first_lhs_link = link;
     592              :     }
     593              :   else
     594            0 :     lacc->last_lhs_link->next_lhs = link;
     595              : 
     596      1367096 :   lacc->last_lhs_link = link;
     597      1367096 :   link->next_lhs = NULL;
     598      1367096 : }
     599              : 
     600              : /* Move all link structures in their linked list in OLD_ACC to the linked list
     601              :    in NEW_ACC.  */
     602              : static void
     603      5487022 : relink_to_new_repr (struct access *new_acc, struct access *old_acc)
     604              : {
     605      5487022 :   if (old_acc->first_rhs_link)
     606              :     {
     607              : 
     608       898000 :       if (new_acc->first_rhs_link)
     609              :         {
     610       286048 :           gcc_assert (!new_acc->last_rhs_link->next_rhs);
     611       286048 :           gcc_assert (!old_acc->last_rhs_link
     612              :                       || !old_acc->last_rhs_link->next_rhs);
     613              : 
     614       286048 :           new_acc->last_rhs_link->next_rhs = old_acc->first_rhs_link;
     615       286048 :           new_acc->last_rhs_link = old_acc->last_rhs_link;
     616              :         }
     617              :       else
     618              :         {
     619       611952 :           gcc_assert (!new_acc->last_rhs_link);
     620              : 
     621       611952 :           new_acc->first_rhs_link = old_acc->first_rhs_link;
     622       611952 :           new_acc->last_rhs_link = old_acc->last_rhs_link;
     623              :         }
     624       898000 :       old_acc->first_rhs_link = old_acc->last_rhs_link = NULL;
     625              :     }
     626              :   else
     627      4589022 :     gcc_assert (!old_acc->last_rhs_link);
     628              : 
     629      5487022 :   if (old_acc->first_lhs_link)
     630              :     {
     631              : 
     632       368180 :       if (new_acc->first_lhs_link)
     633              :         {
     634       152308 :           gcc_assert (!new_acc->last_lhs_link->next_lhs);
     635       152308 :           gcc_assert (!old_acc->last_lhs_link
     636              :                       || !old_acc->last_lhs_link->next_lhs);
     637              : 
     638       152308 :           new_acc->last_lhs_link->next_lhs = old_acc->first_lhs_link;
     639       152308 :           new_acc->last_lhs_link = old_acc->last_lhs_link;
     640              :         }
     641              :       else
     642              :         {
     643       215872 :           gcc_assert (!new_acc->last_lhs_link);
     644              : 
     645       215872 :           new_acc->first_lhs_link = old_acc->first_lhs_link;
     646       215872 :           new_acc->last_lhs_link = old_acc->last_lhs_link;
     647              :         }
     648       368180 :       old_acc->first_lhs_link = old_acc->last_lhs_link = NULL;
     649              :     }
     650              :   else
     651      5118842 :     gcc_assert (!old_acc->last_lhs_link);
     652              : 
     653      5487022 : }
     654              : 
     655              : /* Add ACCESS to the work to queue for propagation of subaccesses from RHS to
     656              :    LHS (which is actually a stack).  */
     657              : 
     658              : static void
     659      4788812 : add_access_to_rhs_work_queue (struct access *access)
     660              : {
     661      4788812 :   if (access->first_rhs_link && !access->grp_rhs_queued)
     662              :     {
     663      1576459 :       gcc_assert (!access->next_rhs_queued);
     664      1576459 :       access->next_rhs_queued = rhs_work_queue_head;
     665      1576459 :       access->grp_rhs_queued = 1;
     666      1576459 :       rhs_work_queue_head = access;
     667              :     }
     668      4788812 : }
     669              : 
     670              : /* Add ACCESS to the work to queue for propagation of subaccesses from LHS to
     671              :    RHS (which is actually a stack).  */
     672              : 
     673              : static void
     674      1713828 : add_access_to_lhs_work_queue (struct access *access)
     675              : {
     676      1713828 :   if (access->first_lhs_link && !access->grp_lhs_queued)
     677              :     {
     678      1368751 :       gcc_assert (!access->next_lhs_queued);
     679      1368751 :       access->next_lhs_queued = lhs_work_queue_head;
     680      1368751 :       access->grp_lhs_queued = 1;
     681      1368751 :       lhs_work_queue_head = access;
     682              :     }
     683      1713828 : }
     684              : 
     685              : /* Pop an access from the work queue for propagating from RHS to LHS, and
     686              :    return it, assuming there is one.  */
     687              : 
     688              : static struct access *
     689      1576459 : pop_access_from_rhs_work_queue (void)
     690              : {
     691      1576459 :   struct access *access = rhs_work_queue_head;
     692              : 
     693      1576459 :   rhs_work_queue_head = access->next_rhs_queued;
     694      1576459 :   access->next_rhs_queued = NULL;
     695      1576459 :   access->grp_rhs_queued = 0;
     696      1576459 :   return access;
     697              : }
     698              : 
     699              : /* Pop an access from the work queue for propagating from LHS to RHS, and
     700              :    return it, assuming there is one.  */
     701              : 
     702              : static struct access *
     703      1368751 : pop_access_from_lhs_work_queue (void)
     704              : {
     705      1368751 :   struct access *access = lhs_work_queue_head;
     706              : 
     707      1368751 :   lhs_work_queue_head = access->next_lhs_queued;
     708      1368751 :   access->next_lhs_queued = NULL;
     709      1368751 :   access->grp_lhs_queued = 0;
     710      1368751 :   return access;
     711              : }
     712              : 
     713              : /* Allocate necessary structures.  */
     714              : 
     715              : static void
     716      3591423 : sra_initialize (void)
     717              : {
     718      3591423 :   candidate_bitmap = BITMAP_ALLOC (NULL);
     719      7182846 :   candidates = new hash_table<uid_decl_hasher>
     720      6694538 :     (vec_safe_length (cfun->local_decls) / 2);
     721      3591423 :   should_scalarize_away_bitmap = BITMAP_ALLOC (NULL);
     722      3591423 :   cannot_scalarize_away_bitmap = BITMAP_ALLOC (NULL);
     723      3591423 :   disqualified_constants = BITMAP_ALLOC (NULL);
     724      3591423 :   passed_by_ref_in_call = BITMAP_ALLOC (NULL);
     725      3591423 :   gcc_obstack_init (&name_obstack);
     726      3591423 :   base_access_vec = new hash_map<tree, auto_vec<access_p> >;
     727      3591423 :   memset (&sra_stats, 0, sizeof (sra_stats));
     728      3591423 : }
     729              : 
     730              : /* Deallocate all general structures.  */
     731              : 
     732              : static void
     733      3591423 : sra_deinitialize (void)
     734              : {
     735      3591423 :   BITMAP_FREE (candidate_bitmap);
     736      3591423 :   delete candidates;
     737      3591423 :   candidates = NULL;
     738      3591423 :   BITMAP_FREE (should_scalarize_away_bitmap);
     739      3591423 :   BITMAP_FREE (cannot_scalarize_away_bitmap);
     740      3591423 :   BITMAP_FREE (disqualified_constants);
     741      3591423 :   BITMAP_FREE (passed_by_ref_in_call);
     742      3591423 :   access_pool.release ();
     743      3591423 :   assign_link_pool.release ();
     744      3591423 :   obstack_free (&name_obstack, NULL);
     745              : 
     746      7182846 :   delete base_access_vec;
     747      3591423 : }
     748              : 
     749              : /* Return true if DECL is a VAR_DECL in the constant pool, false otherwise.  */
     750              : 
     751     43960707 : static bool constant_decl_p (tree decl)
     752              : {
     753     37994287 :   return VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl);
     754              : }
     755              : 
     756              : /* Remove DECL from candidates for SRA and write REASON to the dump file if
     757              :    there is one.  */
     758              : 
     759              : static void
     760      4618895 : disqualify_candidate (tree decl, const char *reason)
     761              : {
     762      4618895 :   if (bitmap_clear_bit (candidate_bitmap, DECL_UID (decl)))
     763      2462176 :     candidates->remove_elt_with_hash (decl, DECL_UID (decl));
     764      4618895 :   if (constant_decl_p (decl))
     765         4303 :     bitmap_set_bit (disqualified_constants, DECL_UID (decl));
     766              : 
     767      4618895 :   if (dump_file && (dump_flags & TDF_DETAILS))
     768              :     {
     769           30 :       fprintf (dump_file, "! Disqualifying ");
     770           30 :       print_generic_expr (dump_file, decl);
     771           30 :       fprintf (dump_file, " - %s\n", reason);
     772              :     }
     773      4618895 : }
     774              : 
     775              : /* Return true iff the type contains a field or an element which does not allow
     776              :    scalarization.  Use VISITED_TYPES to avoid re-checking already checked
     777              :    (sub-)types.  */
     778              : 
     779              : static bool
     780      8774058 : type_internals_preclude_sra_p_1 (tree type, const char **msg,
     781              :                                  hash_set<tree> *visited_types)
     782              : {
     783      8774058 :   tree fld;
     784      8774058 :   tree et;
     785              : 
     786      8774058 :   if (visited_types->contains (type))
     787              :     return false;
     788      8456228 :   visited_types->add (type);
     789              : 
     790      8456228 :   switch (TREE_CODE (type))
     791              :     {
     792      7755971 :     case RECORD_TYPE:
     793      7755971 :     case UNION_TYPE:
     794      7755971 :     case QUAL_UNION_TYPE:
     795    160224846 :       for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
     796    152479417 :         if (TREE_CODE (fld) == FIELD_DECL)
     797              :           {
     798     17408119 :             if (TREE_CODE (fld) == FUNCTION_DECL)
     799              :               continue;
     800     17408119 :             tree ft = TREE_TYPE (fld);
     801              : 
     802     17408119 :             if (TREE_THIS_VOLATILE (fld))
     803              :               {
     804          933 :                 *msg = "volatile structure field";
     805          933 :                 return true;
     806              :               }
     807     17407186 :             if (!DECL_FIELD_OFFSET (fld))
     808              :               {
     809            0 :                 *msg = "no structure field offset";
     810            0 :                 return true;
     811              :               }
     812     17407186 :             if (!DECL_SIZE (fld))
     813              :               {
     814         8448 :                 *msg = "zero structure field size";
     815         8448 :                 return true;
     816              :               }
     817     17398738 :             if (!tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld)))
     818              :               {
     819            0 :                 *msg = "structure field offset not fixed";
     820            0 :                 return true;
     821              :               }
     822     17398738 :             if (!tree_fits_uhwi_p (DECL_SIZE (fld)))
     823              :               {
     824            0 :                 *msg = "structure field size not fixed";
     825            0 :                 return true;
     826              :               }
     827     17398738 :             if (!tree_fits_shwi_p (bit_position (fld)))
     828              :               {
     829            0 :                 *msg = "structure field size too big";
     830            0 :                 return true;
     831              :               }
     832     17398738 :             if (AGGREGATE_TYPE_P (ft)
     833     17398738 :                     && int_bit_position (fld) % BITS_PER_UNIT != 0)
     834              :               {
     835            0 :                 *msg = "structure field is bit field";
     836            0 :                 return true;
     837              :               }
     838              : 
     839     17398738 :             if (AGGREGATE_TYPE_P (ft)
     840     17398738 :               && type_internals_preclude_sra_p_1 (ft, msg, visited_types))
     841              :               return true;
     842              :           }
     843              : 
     844              :       return false;
     845              : 
     846       576378 :     case ARRAY_TYPE:
     847       576378 :       et = TREE_TYPE (type);
     848              : 
     849       576378 :       if (TYPE_VOLATILE (et))
     850              :         {
     851            0 :           *msg = "element type is volatile";
     852            0 :           return true;
     853              :         }
     854              : 
     855       576378 :       if (AGGREGATE_TYPE_P (et)
     856       576378 :           && type_internals_preclude_sra_p_1 (et, msg, visited_types))
     857              :         return true;
     858              : 
     859              :       return false;
     860              : 
     861              :     default:
     862              :       return false;
     863              :     }
     864              : }
     865              : 
     866              : /* Return true iff the type contains a field or an element which does not allow
     867              :    scalarization.  */
     868              : 
     869              : bool
     870      5046614 : type_internals_preclude_sra_p (tree type, const char **msg)
     871              : {
     872      5046614 :   hash_set<tree> visited_types;
     873      5046614 :   return type_internals_preclude_sra_p_1 (type, msg, &visited_types);
     874      5046614 : }
     875              : 
     876              : 
     877              : /* Allocate an access structure for BASE, OFFSET and SIZE, clear it, fill in
     878              :    the three fields.  Also add it to the vector of accesses corresponding to
     879              :    the base.  Finally, return the new access.  */
     880              : 
     881              : static struct access *
     882     15385922 : create_access_1 (tree base, HOST_WIDE_INT offset, HOST_WIDE_INT size)
     883              : {
     884     15385922 :   struct access *access = access_pool.allocate ();
     885              : 
     886     15385922 :   memset (access, 0, sizeof (struct access));
     887     15385922 :   access->base = base;
     888     15385922 :   access->offset = offset;
     889     15385922 :   access->size = size;
     890              : 
     891     15385922 :   base_access_vec->get_or_insert (base).safe_push (access);
     892              : 
     893     15385922 :   return access;
     894              : }
     895              : 
     896              : static bool maybe_add_sra_candidate (tree);
     897              : 
     898              : /* Create and insert access for EXPR. Return created access, or NULL if it is
     899              :    not possible.  Also scan for uses of constant pool as we go along and add
     900              :    to candidates.  */
     901              : 
     902              : static struct access *
     903     29641626 : create_access (tree expr, gimple *stmt, bool write)
     904              : {
     905     29641626 :   struct access *access;
     906     29641626 :   poly_int64 poffset, psize, pmax_size;
     907     29641626 :   tree base = expr;
     908     29641626 :   bool reverse, unscalarizable_region = false;
     909              : 
     910     29641626 :   base = get_ref_base_and_extent (expr, &poffset, &psize, &pmax_size,
     911              :                                   &reverse);
     912              : 
     913              :   /* For constant-pool entries, check we can substitute the constant value.  */
     914     29641626 :   if (constant_decl_p (base)
     915         3948 :       && !bitmap_bit_p (disqualified_constants, DECL_UID (base)))
     916              :     {
     917         3948 :       if (expr != base
     918          345 :           && !is_gimple_reg_type (TREE_TYPE (expr))
     919         4030 :           && dump_file && (dump_flags & TDF_DETAILS))
     920              :         {
     921              :           /* This occurs in Ada with accesses to ARRAY_RANGE_REFs,
     922              :              and elements of multidimensional arrays (which are
     923              :              multi-element arrays in their own right).  */
     924            0 :           fprintf (dump_file, "Allowing non-reg-type load of part"
     925              :                               " of constant-pool entry: ");
     926            0 :           print_generic_expr (dump_file, expr);
     927              :         }
     928         3948 :       maybe_add_sra_candidate (base);
     929              :     }
     930              : 
     931     29641626 :   if (!DECL_P (base) || !bitmap_bit_p (candidate_bitmap, DECL_UID (base)))
     932              :     return NULL;
     933              : 
     934     15397115 :   if (write && TREE_READONLY (base))
     935              :     {
     936        10520 :       disqualify_candidate (base, "Encountered a store to a read-only decl.");
     937        10520 :       return NULL;
     938              :     }
     939              : 
     940     15386595 :   HOST_WIDE_INT offset, size, max_size;
     941     15386595 :   if (!poffset.is_constant (&offset)
     942     15386595 :       || !psize.is_constant (&size)
     943     15386595 :       || !pmax_size.is_constant (&max_size))
     944              :     {
     945              :       disqualify_candidate (base, "Encountered a polynomial-sized access.");
     946              :       return NULL;
     947              :     }
     948              : 
     949     15386595 :   if (size != max_size)
     950              :     {
     951       391293 :       size = max_size;
     952       391293 :       unscalarizable_region = true;
     953              :     }
     954     15386595 :   if (size == 0)
     955              :     return NULL;
     956     15386593 :   if (offset < 0)
     957              :     {
     958           34 :       disqualify_candidate (base, "Encountered a negative offset access.");
     959           34 :       return NULL;
     960              :     }
     961     15386559 :   if (size < 0)
     962              :     {
     963           24 :       disqualify_candidate (base, "Encountered an unconstrained access.");
     964           24 :       return NULL;
     965              :     }
     966     15386535 :   if (offset + size > tree_to_shwi (DECL_SIZE (base)))
     967              :     {
     968          611 :       disqualify_candidate (base, "Encountered an access beyond the base.");
     969          611 :       return NULL;
     970              :     }
     971     15385924 :   if (BITINT_TYPE_P (TREE_TYPE (expr)) && size > WIDE_INT_MAX_PRECISION - 1)
     972              :     {
     973            2 :       disqualify_candidate (base, "Encountered too large _BitInt access.");
     974            2 :       return NULL;
     975              :     }
     976              : 
     977     15385922 :   access = create_access_1 (base, offset, size);
     978     15385922 :   access->expr = expr;
     979     15385922 :   access->type = TREE_TYPE (expr);
     980     15385922 :   access->write = write;
     981     15385922 :   access->grp_unscalarizable_region = unscalarizable_region;
     982     15385922 :   access->grp_same_access_path = true;
     983     15385922 :   access->stmt = stmt;
     984     15385922 :   access->reverse = reverse;
     985              : 
     986     15385922 :   return access;
     987              : }
     988              : 
     989              : /* Given an array type TYPE, extract element size to *EL_SIZE, minimum index to
     990              :    *IDX and maximum index to *MAX so that the caller can iterate over all
     991              :    elements and return true, except if the array is known to be zero-length,
     992              :    then return false.  */
     993              : 
     994              : static bool
     995        19981 : prepare_iteration_over_array_elts (tree type, HOST_WIDE_INT *el_size,
     996              :                                    offset_int *idx, offset_int *max)
     997              : {
     998        19981 :   tree elem_size = TYPE_SIZE (TREE_TYPE (type));
     999        19981 :   gcc_assert (elem_size && tree_fits_shwi_p (elem_size));
    1000        19981 :   *el_size = tree_to_shwi (elem_size);
    1001        19981 :   gcc_assert (*el_size > 0);
    1002              : 
    1003        19981 :   tree minidx = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
    1004        19981 :   gcc_assert (TREE_CODE (minidx) == INTEGER_CST);
    1005        19981 :   tree maxidx = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
    1006              :   /* Skip (some) zero-length arrays; others have MAXIDX == MINIDX - 1.  */
    1007        19981 :   if (!maxidx)
    1008              :     return false;
    1009        19981 :   gcc_assert (TREE_CODE (maxidx) == INTEGER_CST);
    1010        19981 :   tree domain = TYPE_DOMAIN (type);
    1011              :   /* MINIDX and MAXIDX are inclusive, and must be interpreted in
    1012              :      DOMAIN (e.g. signed int, whereas min/max may be size_int).  */
    1013        19981 :   *idx = wi::to_offset (minidx);
    1014        19981 :   *max = wi::to_offset (maxidx);
    1015        19981 :   if (!TYPE_UNSIGNED (domain))
    1016              :     {
    1017        19981 :       *idx = wi::sext (*idx, TYPE_PRECISION (domain));
    1018        19981 :       *max = wi::sext (*max, TYPE_PRECISION (domain));
    1019              :     }
    1020              :   return true;
    1021              : }
    1022              : 
    1023              : /* A structure to track collecting padding and hold collected padding
    1024              :    information.   */
    1025              : 
    1026        46836 : class sra_padding_collecting
    1027              : {
    1028              : public:
    1029              :   /* Given that there won't be any data until at least OFFSET, add an
    1030              :      appropriate entry to the list of paddings or extend the last one.  */
    1031              :   void record_padding (HOST_WIDE_INT offset);
    1032              :   /* Vector of pairs describing contiguous pieces of padding, each pair
    1033              :      consisting of offset and length.  */
    1034              :   auto_vec<std::pair<HOST_WIDE_INT, HOST_WIDE_INT>, 10> m_padding;
    1035              :   /* Offset where data should continue after the last seen actual bit of data
    1036              :      if there was no padding.  */
    1037              :   HOST_WIDE_INT m_data_until = 0;
    1038              : };
    1039              : 
    1040              : /* Given that there won't be any data until at least OFFSET, add an appropriate
    1041              :    entry to the list of paddings or extend the last one.  */
    1042              : 
    1043       125856 : void sra_padding_collecting::record_padding (HOST_WIDE_INT offset)
    1044              : {
    1045       125856 :   if (offset > m_data_until)
    1046              :     {
    1047        11532 :       HOST_WIDE_INT psz = offset - m_data_until;
    1048        11532 :       if (!m_padding.is_empty ()
    1049          574 :           && ((m_padding[m_padding.length () - 1].first
    1050          574 :                + m_padding[m_padding.length () - 1].second) == offset))
    1051            0 :         m_padding[m_padding.length () - 1].second += psz;
    1052              :       else
    1053        11532 :         m_padding.safe_push (std::make_pair (m_data_until, psz));
    1054              :     }
    1055       125856 : }
    1056              : 
    1057              : /* Return true iff TYPE is totally scalarizable - i.e. a RECORD_TYPE or
    1058              :    fixed-length ARRAY_TYPE with fields that are either of gimple register types
    1059              :    (excluding bit-fields) or (recursively) scalarizable types.  CONST_DECL must
    1060              :    be true if we are considering a decl from constant pool.  If it is false,
    1061              :    char arrays will be refused.
    1062              : 
    1063              :    TOTAL_OFFSET is the offset of TYPE within any outer type that is being
    1064              :    examined.
    1065              : 
    1066              :    If PC is non-NULL, collect padding information into the vector within the
    1067              :    structure.  The information is however only complete if the function returns
    1068              :    true and does not contain any padding at its end.  */
    1069              : 
    1070              : static bool
    1071      2869948 : totally_scalarizable_type_p (tree type, bool const_decl,
    1072              :                              HOST_WIDE_INT total_offset,
    1073              :                              sra_padding_collecting *pc)
    1074              : {
    1075      2869948 :   if (is_gimple_reg_type (type))
    1076              :     {
    1077      1852026 :       if (pc)
    1078              :         {
    1079        80108 :           pc->record_padding (total_offset);
    1080        80108 :           pc->m_data_until = total_offset + tree_to_shwi (TYPE_SIZE (type));
    1081              :         }
    1082              :       return true;
    1083              :     }
    1084      1017922 :   if (type_contains_placeholder_p (type))
    1085              :     return false;
    1086              : 
    1087      1017922 :   bool have_predecessor_field = false;
    1088      1017922 :   HOST_WIDE_INT prev_pos = 0;
    1089              : 
    1090      1017922 :   switch (TREE_CODE (type))
    1091              :   {
    1092       977449 :   case RECORD_TYPE:
    1093     15256697 :     for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
    1094     14305202 :       if (TREE_CODE (fld) == FIELD_DECL)
    1095              :         {
    1096      2087528 :           tree ft = TREE_TYPE (fld);
    1097              : 
    1098      2087528 :           if (!DECL_SIZE (fld))
    1099              :             return false;
    1100      2087528 :           if (zerop (DECL_SIZE (fld)))
    1101        64060 :             continue;
    1102              : 
    1103      2023468 :           HOST_WIDE_INT pos = int_bit_position (fld);
    1104      2023468 :           if (have_predecessor_field
    1105      2023468 :               && pos <= prev_pos)
    1106              :             return false;
    1107              : 
    1108      2023468 :           have_predecessor_field = true;
    1109      2023468 :           prev_pos = pos;
    1110              : 
    1111      2023468 :           if (DECL_BIT_FIELD (fld))
    1112              :             return false;
    1113              : 
    1114      2021757 :           if (!totally_scalarizable_type_p (ft, const_decl, total_offset + pos,
    1115              :                                             pc))
    1116              :             return false;
    1117              :         }
    1118              : 
    1119              :     return true;
    1120              : 
    1121        26866 :   case ARRAY_TYPE:
    1122        26866 :     {
    1123        26866 :       HOST_WIDE_INT min_elem_size;
    1124        26866 :       if (const_decl)
    1125              :         min_elem_size = 0;
    1126              :       else
    1127        23149 :         min_elem_size = BITS_PER_UNIT;
    1128              : 
    1129        26866 :       if (TYPE_DOMAIN (type) == NULL_TREE
    1130        26866 :           || !tree_fits_shwi_p (TYPE_SIZE (type))
    1131        26866 :           || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (type)))
    1132        26866 :           || (tree_to_shwi (TYPE_SIZE (TREE_TYPE (type))) <= min_elem_size)
    1133        48037 :           || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
    1134              :         return false;
    1135        21171 :       if (tree_to_shwi (TYPE_SIZE (type)) == 0
    1136        21171 :           && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
    1137              :         /* Zero-element array, should not prevent scalarization.  */
    1138              :         ;
    1139        21171 :       else if ((tree_to_shwi (TYPE_SIZE (type)) <= 0)
    1140        21171 :                || !tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
    1141              :         /* Variable-length array, do not allow scalarization.  */
    1142              :         return false;
    1143              : 
    1144        21135 :       unsigned old_padding_len = 0;
    1145        21135 :       if (pc)
    1146         3556 :         old_padding_len = pc->m_padding.length ();
    1147        21135 :       tree elem = TREE_TYPE (type);
    1148        21135 :       if (!totally_scalarizable_type_p (elem, const_decl, total_offset, pc))
    1149              :         return false;
    1150        20971 :       if (pc)
    1151              :         {
    1152         3556 :           unsigned new_padding_len = pc->m_padding.length ();
    1153         3556 :           HOST_WIDE_INT el_size;
    1154         3556 :           offset_int idx, max;
    1155         3556 :           if (!prepare_iteration_over_array_elts (type, &el_size, &idx, &max))
    1156            0 :             return true;
    1157         3556 :           pc->record_padding (total_offset + el_size);
    1158         3556 :           ++idx;
    1159         3556 :           for (HOST_WIDE_INT pos = total_offset + el_size;
    1160       124646 :                idx <= max;
    1161       121090 :                pos += el_size, ++idx)
    1162              :             {
    1163       121117 :               for (unsigned i = old_padding_len; i < new_padding_len; i++)
    1164              :                 {
    1165           27 :                   HOST_WIDE_INT pp
    1166           27 :                     = pos + pc->m_padding[i].first - total_offset;
    1167           27 :                   HOST_WIDE_INT psz = pc->m_padding[i].second;
    1168           27 :                   pc->m_padding.safe_push (std::make_pair (pp, psz));
    1169              :                 }
    1170              :             }
    1171         3556 :           pc->m_data_until = total_offset + tree_to_shwi (TYPE_SIZE (type));
    1172              :         }
    1173              :       return true;
    1174              :     }
    1175              :   default:
    1176              :     return false;
    1177              :   }
    1178              : }
    1179              : 
    1180              : /* Return true if REF has an VIEW_CONVERT_EXPR somewhere in it.  */
    1181              : 
    1182              : static inline bool
    1183     61336476 : contains_view_convert_expr_p (const_tree ref)
    1184              : {
    1185     84473412 :   while (handled_component_p (ref))
    1186              :     {
    1187     23149843 :       if (TREE_CODE (ref) == VIEW_CONVERT_EXPR)
    1188              :         return true;
    1189     23136936 :       ref = TREE_OPERAND (ref, 0);
    1190              :     }
    1191              : 
    1192              :   return false;
    1193              : }
    1194              : 
    1195              : /* Return true if REF contains a VIEW_CONVERT_EXPR or a COMPONENT_REF with a
    1196              :    bit-field field declaration.  If TYPE_CHANGING_P is non-NULL, set the bool
    1197              :    it points to will be set if REF contains any of the above or a MEM_REF
    1198              :    expression that effectively performs type conversion.  */
    1199              : 
    1200              : static bool
    1201      7977162 : contains_vce_or_bfcref_p (const_tree ref, bool *type_changing_p = NULL)
    1202              : {
    1203     10347594 :   while (handled_component_p (ref))
    1204              :     {
    1205      2756004 :       if (TREE_CODE (ref) == VIEW_CONVERT_EXPR
    1206      2756004 :           || (TREE_CODE (ref) == COMPONENT_REF
    1207      2011740 :               && DECL_BIT_FIELD (TREE_OPERAND (ref, 1))))
    1208              :         {
    1209       385572 :           if (type_changing_p)
    1210       200701 :             *type_changing_p = true;
    1211              :           return true;
    1212              :         }
    1213      2370432 :       ref = TREE_OPERAND (ref, 0);
    1214              :     }
    1215              : 
    1216      7591590 :   if (!type_changing_p
    1217      3668884 :       || TREE_CODE (ref) != MEM_REF
    1218      7722350 :       || TREE_CODE (TREE_OPERAND (ref, 0)) != ADDR_EXPR)
    1219              :     return false;
    1220              : 
    1221       130760 :   tree mem = TREE_OPERAND (TREE_OPERAND (ref, 0), 0);
    1222       130760 :   if (TYPE_MAIN_VARIANT (TREE_TYPE (ref))
    1223       130760 :       != TYPE_MAIN_VARIANT (TREE_TYPE (mem)))
    1224       100399 :     *type_changing_p = true;
    1225              : 
    1226              :   return false;
    1227              : }
    1228              : 
    1229              : /* Search the given tree for a declaration by skipping handled components and
    1230              :    exclude it from the candidates.  */
    1231              : 
    1232              : static void
    1233      1100454 : disqualify_base_of_expr (tree t, const char *reason)
    1234              : {
    1235      1100454 :   t = get_base_address (t);
    1236      1100454 :   if (t && DECL_P (t))
    1237       869190 :     disqualify_candidate (t, reason);
    1238      1100454 : }
    1239              : 
    1240              : /* Return true if the BIT_FIELD_REF read EXPR is handled by SRA.  */
    1241              : 
    1242              : static bool
    1243       239436 : sra_handled_bf_read_p (tree expr)
    1244              : {
    1245       239436 :   uint64_t size, offset;
    1246       239436 :   if (bit_field_size (expr).is_constant (&size)
    1247       239436 :       && bit_field_offset (expr).is_constant (&offset)
    1248       239436 :       && size % BITS_PER_UNIT == 0
    1249       239436 :       && offset % BITS_PER_UNIT == 0
    1250       239504 :       && pow2p_hwi (size))
    1251       239344 :     return true;
    1252              :   return false;
    1253              : }
    1254              : 
    1255              : /* Scan expression EXPR and create access structures for all accesses to
    1256              :    candidates for scalarization.  Return the created access or NULL if none is
    1257              :    created.  */
    1258              : 
    1259              : static struct access *
    1260     63444134 : build_access_from_expr_1 (tree expr, gimple *stmt, bool write)
    1261              : {
    1262              :   /* We only allow ADDR_EXPRs in arguments of function calls and those must
    1263              :      have been dealt with in build_access_from_call_arg.  Any other address
    1264              :      taking should have been caught by scan_visit_addr.   */
    1265     63444134 :   if (TREE_CODE (expr) == ADDR_EXPR)
    1266              :     {
    1267      2107657 :       tree base = get_base_address (TREE_OPERAND (expr, 0));
    1268      2107657 :       gcc_assert (!DECL_P (base)
    1269              :                   || !bitmap_bit_p (candidate_bitmap, DECL_UID (base)));
    1270              :       return NULL;
    1271              :     }
    1272              : 
    1273     61336477 :   struct access *ret = NULL;
    1274     61336477 :   bool partial_ref;
    1275              : 
    1276     61336477 :   if ((TREE_CODE (expr) == BIT_FIELD_REF
    1277       130917 :        && (write || !sra_handled_bf_read_p (expr)))
    1278     61335299 :       || TREE_CODE (expr) == IMAGPART_EXPR
    1279    122637933 :       || TREE_CODE (expr) == REALPART_EXPR)
    1280              :     {
    1281        67973 :       expr = TREE_OPERAND (expr, 0);
    1282        67973 :       partial_ref = true;
    1283              :     }
    1284              :   else
    1285              :     partial_ref = false;
    1286              : 
    1287     61336477 :   if (storage_order_barrier_p (expr))
    1288              :     {
    1289            1 :       disqualify_base_of_expr (expr, "storage order barrier.");
    1290            1 :       return NULL;
    1291              :     }
    1292              : 
    1293              :   /* We are capable of handling the topmost V_C_E but not any of those
    1294              :      buried in other handled components.  */
    1295     61640597 :   if (contains_view_convert_expr_p (TREE_CODE (expr) == VIEW_CONVERT_EXPR
    1296       304121 :                                     ? TREE_OPERAND (expr, 0) : expr))
    1297              :     {
    1298        12907 :       disqualify_base_of_expr (expr, "V_C_E under a different handled "
    1299              :                                "component.");
    1300        12907 :       return NULL;
    1301              :     }
    1302              : 
    1303     61323569 :   if (TREE_THIS_VOLATILE (expr))
    1304              :     {
    1305        22195 :       disqualify_base_of_expr (expr, "part of a volatile reference.");
    1306        22195 :       return NULL;
    1307              :     }
    1308              : 
    1309     61301374 :   switch (TREE_CODE (expr))
    1310              :     {
    1311      3699101 :     case MEM_REF:
    1312      3699101 :       if (TREE_CODE (TREE_OPERAND (expr, 0)) != ADDR_EXPR)
    1313              :         return NULL;
    1314              :       /* fall through */
    1315     29641626 :     case VAR_DECL:
    1316     29641626 :     case PARM_DECL:
    1317     29641626 :     case RESULT_DECL:
    1318     29641626 :     case COMPONENT_REF:
    1319     29641626 :     case ARRAY_REF:
    1320     29641626 :     case ARRAY_RANGE_REF:
    1321     29641626 :     case BIT_FIELD_REF:
    1322     29641626 :     case VIEW_CONVERT_EXPR:
    1323     29641626 :       ret = create_access (expr, stmt, write);
    1324     29641626 :       break;
    1325              : 
    1326              :     default:
    1327              :       break;
    1328              :     }
    1329              : 
    1330     59449339 :   if (write && partial_ref && ret)
    1331         5292 :     ret->grp_partial_lhs = 1;
    1332              : 
    1333              :   return ret;
    1334              : }
    1335              : 
    1336              : /* Scan expression EXPR and create access structures for all accesses to
    1337              :    candidates for scalarization.  Return true if any access has been inserted.
    1338              :    STMT must be the statement from which the expression is taken, WRITE must be
    1339              :    true if the expression is a store and false otherwise. */
    1340              : 
    1341              : static bool
    1342     17817044 : build_access_from_expr (tree expr, gimple *stmt, bool write)
    1343              : {
    1344     17817044 :   struct access *access;
    1345              : 
    1346     17817044 :   access = build_access_from_expr_1 (expr, stmt, write);
    1347     17817044 :   if (access)
    1348              :     {
    1349              :       /* This means the aggregate is accesses as a whole in a way other than an
    1350              :          assign statement and thus cannot be removed even if we had a scalar
    1351              :          replacement for everything.  */
    1352      2661185 :       if (cannot_scalarize_away_bitmap)
    1353      2661185 :         bitmap_set_bit (cannot_scalarize_away_bitmap, DECL_UID (access->base));
    1354              :       return true;
    1355              :     }
    1356              :   return false;
    1357              : }
    1358              : 
    1359              : enum out_edge_check { SRA_OUTGOING_EDGES_UNCHECKED, SRA_OUTGOING_EDGES_OK,
    1360              :                       SRA_OUTGOING_EDGES_FAIL };
    1361              : 
    1362              : /* Return true if STMT terminates BB and there is an abnormal edge going out of
    1363              :    the BB and remember the decision in OE_CHECK.  */
    1364              : 
    1365              : static bool
    1366      3269545 : abnormal_edge_after_stmt_p (gimple *stmt, enum out_edge_check *oe_check)
    1367              : {
    1368      3269545 :   if (*oe_check == SRA_OUTGOING_EDGES_OK)
    1369              :     return false;
    1370      1906905 :   if (*oe_check == SRA_OUTGOING_EDGES_FAIL)
    1371              :     return true;
    1372      1906679 :   if (stmt_ends_bb_p (stmt))
    1373              :     {
    1374       783262 :       edge e;
    1375       783262 :       edge_iterator ei;
    1376      2043066 :       FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
    1377      1260307 :         if (e->flags & EDGE_ABNORMAL)
    1378              :           {
    1379          503 :             *oe_check = SRA_OUTGOING_EDGES_FAIL;
    1380          503 :             return true;
    1381              :           }
    1382              :     }
    1383      1906176 :   *oe_check = SRA_OUTGOING_EDGES_OK;
    1384      1906176 :   return false;
    1385              : }
    1386              : 
    1387              : /* Scan expression EXPR which is an argument of a call and create access
    1388              :    structures for all accesses to candidates for scalarization.  Return true
    1389              :    if any access has been inserted.  STMT must be the statement from which the
    1390              :    expression is taken.  CAN_BE_RETURNED must be true if call argument flags
    1391              :    do not rule out that the argument is directly returned.  OE_CHECK is used
    1392              :    to remember result of a test for abnormal outgoing edges after this
    1393              :    statement.  */
    1394              : 
    1395              : static bool
    1396     12633619 : build_access_from_call_arg (tree expr, gimple *stmt, bool can_be_returned,
    1397              :                             enum out_edge_check *oe_check)
    1398              : {
    1399     12633619 :   if (gimple_call_flags (stmt) & ECF_RETURNS_TWICE)
    1400              :     {
    1401           57 :       tree base = expr;
    1402           57 :       if (TREE_CODE (expr) == ADDR_EXPR)
    1403           10 :         base = get_base_address (TREE_OPERAND (expr, 0));
    1404           57 :       disqualify_base_of_expr (base, "Passed to a returns_twice call.");
    1405           57 :       return false;
    1406              :     }
    1407              : 
    1408     12633562 :   if (TREE_CODE (expr) == ADDR_EXPR)
    1409              :     {
    1410      4333506 :       tree base = get_base_address (TREE_OPERAND (expr, 0));
    1411              : 
    1412      4333506 :       if (can_be_returned)
    1413              :         {
    1414      1063961 :           disqualify_base_of_expr (base, "Address possibly returned, "
    1415              :                                    "leading to an alias SRA may not know.");
    1416      1063961 :           return false;
    1417              :         }
    1418      3269545 :       if (abnormal_edge_after_stmt_p (stmt, oe_check))
    1419              :         {
    1420          729 :           disqualify_base_of_expr (base, "May lead to need to add statements "
    1421              :                                    "to abnormal edge.");
    1422          729 :           return false;
    1423              :         }
    1424              : 
    1425      3268816 :       bool read =  build_access_from_expr (base, stmt, false);
    1426      3268816 :       bool write =  build_access_from_expr (base, stmt, true);
    1427      3268816 :       if (read || write)
    1428              :         {
    1429       310418 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1430              :             {
    1431            0 :               fprintf (dump_file, "Allowed ADDR_EXPR of ");
    1432            0 :               print_generic_expr (dump_file, base);
    1433            0 :               fprintf (dump_file, " because of ");
    1434            0 :               print_gimple_stmt (dump_file, stmt, 0);
    1435            0 :               fprintf (dump_file, "\n");
    1436              :             }
    1437       310418 :           bitmap_set_bit (passed_by_ref_in_call, DECL_UID (base));
    1438       310418 :           return true;
    1439              :         }
    1440              :       else
    1441              :         return false;
    1442              :     }
    1443              : 
    1444      8300056 :   return build_access_from_expr (expr, stmt, false);
    1445              : }
    1446              : 
    1447              : 
    1448              : /* Return the single non-EH successor edge of BB or NULL if there is none or
    1449              :    more than one.  */
    1450              : 
    1451              : static edge
    1452      1543902 : single_non_eh_succ (basic_block bb)
    1453              : {
    1454      1543902 :   edge e, res = NULL;
    1455      1543902 :   edge_iterator ei;
    1456              : 
    1457      4630226 :   FOR_EACH_EDGE (e, ei, bb->succs)
    1458      3086755 :     if (!(e->flags & EDGE_EH))
    1459              :       {
    1460      1544216 :         if (res)
    1461              :           return NULL;
    1462              :         res = e;
    1463              :       }
    1464              : 
    1465              :   return res;
    1466              : }
    1467              : 
    1468              : /* Disqualify LHS and RHS for scalarization if STMT has to terminate its BB and
    1469              :    there is no alternative spot where to put statements SRA might need to
    1470              :    generate after it.  The spot we are looking for is an edge leading to a
    1471              :    single non-EH successor, if it exists and is indeed single.  RHS may be
    1472              :    NULL, in that case ignore it.  */
    1473              : 
    1474              : static bool
    1475     25349580 : disqualify_if_bad_bb_terminating_stmt (gimple *stmt, tree lhs, tree rhs)
    1476              : {
    1477     25349580 :   if (stmt_ends_bb_p (stmt))
    1478              :     {
    1479      1423402 :       if (single_non_eh_succ (gimple_bb (stmt)))
    1480              :         return false;
    1481              : 
    1482          548 :       disqualify_base_of_expr (lhs, "LHS of a throwing stmt.");
    1483          548 :       if (rhs)
    1484            0 :         disqualify_base_of_expr (rhs, "RHS of a throwing stmt.");
    1485              :       return true;
    1486              :     }
    1487              :   return false;
    1488              : }
    1489              : 
    1490              : /* Return true if the nature of BASE is such that it contains data even if
    1491              :    there is no write to it in the function.  */
    1492              : 
    1493              : static bool
    1494      4177242 : comes_initialized_p (tree base)
    1495              : {
    1496            0 :   return TREE_CODE (base) == PARM_DECL || constant_decl_p (base);
    1497              : }
    1498              : 
    1499              : /* Scan expressions occurring in STMT, create access structures for all accesses
    1500              :    to candidates for scalarization and remove those candidates which occur in
    1501              :    statements or expressions that prevent them from being split apart.  Return
    1502              :    true if any access has been inserted.  */
    1503              : 
    1504              : static bool
    1505     34808978 : build_accesses_from_assign (gimple *stmt)
    1506              : {
    1507     34808978 :   tree lhs, rhs;
    1508     34808978 :   struct access *lacc, *racc;
    1509              : 
    1510     34808978 :   if (!gimple_assign_single_p (stmt)
    1511              :       /* Scope clobbers don't influence scalarization.  */
    1512     34808978 :       || gimple_clobber_p (stmt))
    1513              :     return false;
    1514              : 
    1515     22739744 :   lhs = gimple_assign_lhs (stmt);
    1516     22739744 :   rhs = gimple_assign_rhs1 (stmt);
    1517              : 
    1518     22739744 :   if (disqualify_if_bad_bb_terminating_stmt (stmt, lhs, rhs))
    1519              :     return false;
    1520              : 
    1521     22739744 :   racc = build_access_from_expr_1 (rhs, stmt, false);
    1522     22739744 :   lacc = build_access_from_expr_1 (lhs, stmt, true);
    1523              : 
    1524     22739744 :   bool tbaa_hazard
    1525     22739744 :     = !types_equal_for_same_type_for_tbaa_p (TREE_TYPE (lhs), TREE_TYPE (rhs));
    1526              : 
    1527     22739744 :   if (lacc)
    1528              :     {
    1529      6946322 :       lacc->grp_assignment_write = 1;
    1530      6946322 :       if (storage_order_barrier_p (rhs))
    1531            1 :         lacc->grp_unscalarizable_region = 1;
    1532              : 
    1533      6946322 :       if (should_scalarize_away_bitmap && !is_gimple_reg_type (lacc->type))
    1534              :         {
    1535      2011870 :           bool type_changing_p = false;
    1536      2011870 :           contains_vce_or_bfcref_p (lhs, &type_changing_p);
    1537      2011870 :           if (type_changing_p)
    1538       153212 :             bitmap_set_bit (cannot_scalarize_away_bitmap,
    1539        76606 :                             DECL_UID (lacc->base));
    1540              :         }
    1541      6946322 :       if (tbaa_hazard)
    1542       884856 :         lacc->grp_same_access_path = false;
    1543              :     }
    1544              : 
    1545     22739744 :   if (racc)
    1546              :     {
    1547      5714866 :       racc->grp_assignment_read = 1;
    1548      5714866 :       if (should_scalarize_away_bitmap && !is_gimple_reg_type (racc->type))
    1549              :         {
    1550      1857715 :           bool type_changing_p = false;
    1551      1857715 :           contains_vce_or_bfcref_p (rhs, &type_changing_p);
    1552              : 
    1553      3490936 :           if (type_changing_p || gimple_has_volatile_ops (stmt))
    1554       449734 :             bitmap_set_bit (cannot_scalarize_away_bitmap,
    1555       224867 :                             DECL_UID (racc->base));
    1556              :           else
    1557      3265696 :             bitmap_set_bit (should_scalarize_away_bitmap,
    1558      1632848 :                             DECL_UID (racc->base));
    1559              :         }
    1560      5714866 :       if (storage_order_barrier_p (lhs))
    1561            0 :         racc->grp_unscalarizable_region = 1;
    1562      5714866 :       if (tbaa_hazard)
    1563        71357 :         racc->grp_same_access_path = false;
    1564              :     }
    1565              : 
    1566     22739744 :   if (lacc && racc
    1567      1368734 :       && (sra_mode == SRA_MODE_EARLY_INTRA || sra_mode == SRA_MODE_INTRA)
    1568      1368734 :       && !lacc->grp_unscalarizable_region
    1569      1368110 :       && !racc->grp_unscalarizable_region
    1570      1367302 :       && AGGREGATE_TYPE_P (TREE_TYPE (lhs))
    1571      1367302 :       && lacc->size == racc->size
    1572     24106840 :       && useless_type_conversion_p (lacc->type, racc->type))
    1573              :     {
    1574      1367096 :       struct assign_link *link;
    1575              : 
    1576      1367096 :       link = assign_link_pool.allocate ();
    1577      1367096 :       memset (link, 0, sizeof (struct assign_link));
    1578              : 
    1579      1367096 :       link->lacc = lacc;
    1580      1367096 :       link->racc = racc;
    1581      1367096 :       add_link_to_rhs (racc, link);
    1582      1367096 :       add_link_to_lhs (lacc, link);
    1583      1367096 :       add_access_to_rhs_work_queue (racc);
    1584      1367096 :       add_access_to_lhs_work_queue (lacc);
    1585              : 
    1586              :       /* Let's delay marking the areas as written until propagation of accesses
    1587              :          across link, unless the nature of rhs tells us that its data comes
    1588              :          from elsewhere.  */
    1589      1367096 :       if (!comes_initialized_p (racc->base))
    1590      1267937 :         lacc->write = false;
    1591              :     }
    1592              : 
    1593     22739744 :   return lacc || racc;
    1594              : }
    1595              : 
    1596              : /* Callback of walk_stmt_load_store_addr_ops visit_addr used to detect taking
    1597              :    addresses of candidates a places which are not call arguments.  Such
    1598              :    candidates are disqalified from SRA.  This also applies to GIMPLE_ASM
    1599              :    operands with memory constrains which cannot be scalarized.  */
    1600              : 
    1601              : static bool
    1602      2469354 : scan_visit_addr (gimple *, tree op, tree, void *)
    1603              : {
    1604      2469354 :   op = get_base_address (op);
    1605      2469354 :   if (op
    1606      2469354 :       && DECL_P (op))
    1607      1337746 :     disqualify_candidate (op, "Address taken in a non-call-argument context.");
    1608              : 
    1609      2469354 :   return false;
    1610              : }
    1611              : 
    1612              : /* Scan function and look for interesting expressions and create access
    1613              :    structures for them.  Return true iff any access is created.  */
    1614              : 
    1615              : static bool
    1616       828652 : scan_function (void)
    1617              : {
    1618       828652 :   basic_block bb;
    1619       828652 :   bool ret = false;
    1620              : 
    1621     14044656 :   FOR_EACH_BB_FN (bb, cfun)
    1622              :     {
    1623     13216004 :       gimple_stmt_iterator gsi;
    1624     18009048 :       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    1625      4793044 :         walk_stmt_load_store_addr_ops (gsi_stmt (gsi), NULL, NULL, NULL,
    1626              :                                        scan_visit_addr);
    1627              : 
    1628    131837470 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    1629              :         {
    1630    105405462 :           gimple *stmt = gsi_stmt (gsi);
    1631    105405462 :           tree t;
    1632    105405462 :           unsigned i;
    1633              : 
    1634    105405462 :           if (gimple_code (stmt) != GIMPLE_CALL)
    1635     99094817 :             walk_stmt_load_store_addr_ops (stmt, NULL, NULL, NULL,
    1636              :                                            scan_visit_addr);
    1637              : 
    1638    105405462 :           switch (gimple_code (stmt))
    1639              :             {
    1640       821403 :             case GIMPLE_RETURN:
    1641       821403 :               t = gimple_return_retval (as_a <greturn *> (stmt));
    1642       821403 :               if (t != NULL_TREE)
    1643       485874 :                 ret |= build_access_from_expr (t, stmt, false);
    1644              :               break;
    1645              : 
    1646     34808978 :             case GIMPLE_ASSIGN:
    1647     34808978 :               ret |= build_accesses_from_assign (stmt);
    1648     34808978 :               break;
    1649              : 
    1650      6310645 :             case GIMPLE_CALL:
    1651      6310645 :               {
    1652      6310645 :                 enum out_edge_check oe_check = SRA_OUTGOING_EDGES_UNCHECKED;
    1653      6310645 :                 gcall *call = as_a <gcall *> (stmt);
    1654     25214125 :                 for (i = 0; i < gimple_call_num_args (call); i++)
    1655              :                   {
    1656     12592835 :                     bool can_be_returned;
    1657     12592835 :                     if (gimple_call_lhs (call))
    1658              :                       {
    1659      5140830 :                         int af = gimple_call_arg_flags (call, i);
    1660      5140830 :                         can_be_returned = !(af & EAF_NOT_RETURNED_DIRECTLY);
    1661              :                       }
    1662              :                     else
    1663              :                       can_be_returned = false;
    1664     12592835 :                     ret |= build_access_from_call_arg (gimple_call_arg (call,
    1665              :                                                                         i),
    1666              :                                                        stmt, can_be_returned,
    1667              :                                                        &oe_check);
    1668              :                   }
    1669      6310645 :                 if (gimple_call_chain(stmt))
    1670        40784 :                   ret |= build_access_from_call_arg (gimple_call_chain(call),
    1671              :                                                      stmt, false,  &oe_check);
    1672              :               }
    1673              : 
    1674      6310645 :               t = gimple_call_lhs (stmt);
    1675      6310645 :               if (t && !disqualify_if_bad_bb_terminating_stmt (stmt, t, NULL))
    1676              :                 {
    1677              :                   /* If the STMT is a call to DEFERRED_INIT, avoid setting
    1678              :                      cannot_scalarize_away_bitmap.  */
    1679      2609288 :                   if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
    1680              :                     {
    1681       147602 :                       struct access *access
    1682       147602 :                         = build_access_from_expr_1 (t, stmt, true);
    1683       147602 :                       if (access)
    1684        63549 :                         access->grp_assignment_write = 1;
    1685       147602 :                       ret |= access != NULL;
    1686              :                     }
    1687              :                   else
    1688      2461686 :                     ret |= build_access_from_expr (t, stmt, true);
    1689              :                 }
    1690              :               break;
    1691              : 
    1692        15602 :             case GIMPLE_ASM:
    1693        15602 :               {
    1694        15602 :                 gasm *asm_stmt = as_a <gasm *> (stmt);
    1695        15602 :                 if (stmt_ends_bb_p (asm_stmt)
    1696        15619 :                     && !single_succ_p (gimple_bb (asm_stmt)))
    1697              :                   {
    1698           38 :                     for (i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
    1699              :                       {
    1700           21 :                         t = TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
    1701           21 :                         disqualify_base_of_expr (t, "OP of asm goto.");
    1702              :                       }
    1703           52 :                     for (i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
    1704              :                       {
    1705           35 :                         t = TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
    1706           35 :                         disqualify_base_of_expr (t, "OP of asm goto.");
    1707              :                       }
    1708              :                   }
    1709              :                 else
    1710              :                   {
    1711        31969 :                     for (i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
    1712              :                       {
    1713        16384 :                         t = TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
    1714        16384 :                         ret |= build_access_from_expr (t, asm_stmt, false);
    1715              :                       }
    1716        30997 :                     for (i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
    1717              :                       {
    1718        15412 :                         t = TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
    1719        15412 :                         ret |= build_access_from_expr (t, asm_stmt, true);
    1720              :                       }
    1721              :                   }
    1722              :               }
    1723              :               break;
    1724              : 
    1725              :             default:
    1726              :               break;
    1727              :             }
    1728              :         }
    1729              :     }
    1730              : 
    1731       828652 :   return ret;
    1732              : }
    1733              : 
    1734              : /* Helper of QSORT function. There are pointers to accesses in the array.  An
    1735              :    access is considered smaller than another if it has smaller offset or if the
    1736              :    offsets are the same but is size is bigger. */
    1737              : 
    1738              : static int
    1739    134381481 : compare_access_positions (const void *a, const void *b)
    1740              : {
    1741    134381481 :   const access_p *fp1 = (const access_p *) a;
    1742    134381481 :   const access_p *fp2 = (const access_p *) b;
    1743    134381481 :   const access_p f1 = *fp1;
    1744    134381481 :   const access_p f2 = *fp2;
    1745              : 
    1746    134381481 :   if (f1->offset != f2->offset)
    1747     82258981 :     return f1->offset < f2->offset ? -1 : 1;
    1748              : 
    1749     52122500 :   if (f1->size == f2->size)
    1750              :     {
    1751     35993551 :       if (f1->type == f2->type)
    1752              :         return 0;
    1753              :       /* Put any non-aggregate type before any aggregate type.  */
    1754      6073771 :       else if (!is_gimple_reg_type (f1->type)
    1755      6073771 :           && is_gimple_reg_type (f2->type))
    1756              :         return 1;
    1757      4599867 :       else if (is_gimple_reg_type (f1->type)
    1758      4599867 :                && !is_gimple_reg_type (f2->type))
    1759              :         return -1;
    1760              :       /* Put any complex or vector type before any other scalar type.  */
    1761      2707476 :       else if (TREE_CODE (f1->type) != COMPLEX_TYPE
    1762      2707476 :                && TREE_CODE (f1->type) != VECTOR_TYPE
    1763      2615371 :                && (TREE_CODE (f2->type) == COMPLEX_TYPE
    1764      2615371 :                    || VECTOR_TYPE_P (f2->type)))
    1765              :         return 1;
    1766      2662041 :       else if ((TREE_CODE (f1->type) == COMPLEX_TYPE
    1767              :                 || VECTOR_TYPE_P (f1->type))
    1768        92105 :                && TREE_CODE (f2->type) != COMPLEX_TYPE
    1769        89609 :                && TREE_CODE (f2->type) != VECTOR_TYPE)
    1770              :         return -1;
    1771              :       /* Put any integral type before any non-integral type.  When splicing, we
    1772              :          make sure that those with insufficient precision and occupying the
    1773              :          same space are not scalarized.  */
    1774      2597228 :       else if (INTEGRAL_TYPE_P (f1->type)
    1775       399660 :                && !INTEGRAL_TYPE_P (f2->type))
    1776              :         return -1;
    1777      2483141 :       else if (!INTEGRAL_TYPE_P (f1->type)
    1778      2197568 :                && INTEGRAL_TYPE_P (f2->type))
    1779              :         return 1;
    1780              :       /* Put the integral type with the bigger precision first.  */
    1781      2371103 :       else if (INTEGRAL_TYPE_P (f1->type)
    1782       285573 :                && INTEGRAL_TYPE_P (f2->type)
    1783      2656676 :                && (TYPE_PRECISION (f2->type) != TYPE_PRECISION (f1->type)))
    1784        44358 :         return TYPE_PRECISION (f2->type) - TYPE_PRECISION (f1->type);
    1785              :       /* Stabilize the sort.  */
    1786      2326745 :       return TYPE_UID (f1->type) - TYPE_UID (f2->type);
    1787              :     }
    1788              : 
    1789              :   /* We want the bigger accesses first, thus the opposite operator in the next
    1790              :      line: */
    1791     16128949 :   return f1->size > f2->size ? -1 : 1;
    1792              : }
    1793              : 
    1794              : 
    1795              : /* Append a name of the declaration to the name obstack.  A helper function for
    1796              :    make_fancy_name.  */
    1797              : 
    1798              : static void
    1799      2165424 : make_fancy_decl_name (tree decl)
    1800              : {
    1801      2165424 :   char buffer[32];
    1802              : 
    1803      2165424 :   tree name = DECL_NAME (decl);
    1804      2165424 :   if (name)
    1805      2098838 :     obstack_grow (&name_obstack, IDENTIFIER_POINTER (name),
    1806              :                   IDENTIFIER_LENGTH (name));
    1807              :   else
    1808              :     {
    1809        66586 :       sprintf (buffer, "D%u", DECL_UID (decl));
    1810        66586 :       obstack_grow (&name_obstack, buffer, strlen (buffer));
    1811              :     }
    1812      2165424 : }
    1813              : 
    1814              : /* Helper for make_fancy_name.  */
    1815              : 
    1816              : static void
    1817      2463957 : make_fancy_name_1 (tree expr)
    1818              : {
    1819      2698682 :   char buffer[32];
    1820      2698682 :   tree index;
    1821              : 
    1822      2698682 :   if (DECL_P (expr))
    1823              :     {
    1824      1070358 :       make_fancy_decl_name (expr);
    1825      1070358 :       return;
    1826              :     }
    1827              : 
    1828      1628324 :   switch (TREE_CODE (expr))
    1829              :     {
    1830      1095066 :     case COMPONENT_REF:
    1831      1095066 :       make_fancy_name_1 (TREE_OPERAND (expr, 0));
    1832      1095066 :       obstack_1grow (&name_obstack, '$');
    1833      1095066 :       make_fancy_decl_name (TREE_OPERAND (expr, 1));
    1834      1095066 :       break;
    1835              : 
    1836        65575 :     case ARRAY_REF:
    1837        65575 :       make_fancy_name_1 (TREE_OPERAND (expr, 0));
    1838        65575 :       obstack_1grow (&name_obstack, '$');
    1839              :       /* Arrays with only one element may not have a constant as their
    1840              :          index. */
    1841        65575 :       index = TREE_OPERAND (expr, 1);
    1842        65575 :       if (TREE_CODE (index) != INTEGER_CST)
    1843              :         break;
    1844        65446 :       sprintf (buffer, HOST_WIDE_INT_PRINT_DEC, TREE_INT_CST_LOW (index));
    1845        65446 :       obstack_grow (&name_obstack, buffer, strlen (buffer));
    1846        65446 :       break;
    1847              : 
    1848       234725 :     case BIT_FIELD_REF:
    1849       234725 :     case ADDR_EXPR:
    1850       234725 :       make_fancy_name_1 (TREE_OPERAND (expr, 0));
    1851       234725 :       break;
    1852              : 
    1853       232672 :     case MEM_REF:
    1854       232672 :       make_fancy_name_1 (TREE_OPERAND (expr, 0));
    1855       232672 :       if (!integer_zerop (TREE_OPERAND (expr, 1)))
    1856              :         {
    1857        75089 :           obstack_1grow (&name_obstack, '$');
    1858       150178 :           sprintf (buffer, HOST_WIDE_INT_PRINT_DEC,
    1859        75089 :                    TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)));
    1860        75089 :           obstack_grow (&name_obstack, buffer, strlen (buffer));
    1861              :         }
    1862              :       break;
    1863              : 
    1864            0 :     case REALPART_EXPR:
    1865            0 :     case IMAGPART_EXPR:
    1866            0 :       gcc_unreachable ();       /* we treat these as scalars.  */
    1867              :       break;
    1868              :     default:
    1869              :       break;
    1870              :     }
    1871              : }
    1872              : 
    1873              : /* Create a human readable name for replacement variable of ACCESS.  */
    1874              : 
    1875              : static char *
    1876      1070644 : make_fancy_name (tree expr)
    1877              : {
    1878      1070644 :   make_fancy_name_1 (expr);
    1879      1070644 :   obstack_1grow (&name_obstack, '\0');
    1880      1070644 :   return XOBFINISH (&name_obstack, char *);
    1881              : }
    1882              : 
    1883              : /* Construct a MEM_REF that would reference a part of aggregate BASE of type
    1884              :    EXP_TYPE at the given OFFSET and with storage order REVERSE.  If BASE is
    1885              :    something for which get_addr_base_and_unit_offset returns NULL, gsi must
    1886              :    be non-NULL and is used to insert new statements either before or below
    1887              :    the current one as specified by INSERT_AFTER.  This function is not capable
    1888              :    of handling bitfields.  If FORCE_REF_ALL is true then the memory access
    1889              :    will use alias-set zero.  */
    1890              : 
    1891              : static tree
    1892      2594308 : build_ref_for_offset (location_t loc, tree base, poly_int64 offset,
    1893              :                       bool reverse, tree exp_type, gimple_stmt_iterator *gsi,
    1894              :                       bool insert_after, bool force_ref_all = false)
    1895              : {
    1896      2594308 :   tree prev_base = base;
    1897      2594308 :   tree off;
    1898      2594308 :   tree mem_ref;
    1899      2594308 :   poly_int64 base_offset;
    1900      2594308 :   unsigned HOST_WIDE_INT misalign;
    1901      2594308 :   unsigned int align;
    1902              : 
    1903              :   /* Preserve address-space information.  */
    1904      2594308 :   addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (base));
    1905      2594308 :   if (as != TYPE_ADDR_SPACE (exp_type))
    1906            2 :     exp_type = build_qualified_type (exp_type,
    1907            2 :                                      TYPE_QUALS (exp_type).with_as (as));
    1908              : 
    1909      2594308 :   poly_int64 byte_offset = exact_div (offset, BITS_PER_UNIT);
    1910      2594308 :   get_object_alignment_1 (base, &align, &misalign);
    1911      2594308 :   base = get_addr_base_and_unit_offset (base, &base_offset);
    1912              : 
    1913              :   /* get_addr_base_and_unit_offset returns NULL for references with a variable
    1914              :      offset such as array[var_index].  */
    1915      2594308 :   if (!base)
    1916              :     {
    1917        37029 :       gassign *stmt;
    1918        37029 :       tree tmp, addr;
    1919              : 
    1920        37029 :       gcc_checking_assert (gsi);
    1921        37029 :       tmp = make_ssa_name (build_pointer_type (TREE_TYPE (prev_base)));
    1922        37029 :       addr = build_fold_addr_expr (unshare_expr (prev_base));
    1923        37029 :       STRIP_USELESS_TYPE_CONVERSION (addr);
    1924        37029 :       stmt = gimple_build_assign (tmp, addr);
    1925        37029 :       gimple_set_location (stmt, loc);
    1926        37029 :       if (insert_after)
    1927        10293 :         gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
    1928              :       else
    1929        26736 :         gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
    1930              : 
    1931        37029 :       off = build_int_cst (force_ref_all ? ptr_type_node
    1932        37029 :                            : reference_alias_ptr_type (prev_base), byte_offset);
    1933        37029 :       base = tmp;
    1934              :     }
    1935      2557279 :   else if (TREE_CODE (base) == MEM_REF)
    1936              :     {
    1937       434790 :       off = build_int_cst (force_ref_all ? ptr_type_node
    1938       217395 :                            : TREE_TYPE (TREE_OPERAND (base, 1)),
    1939              :                            base_offset + byte_offset);
    1940       217395 :       off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1), off);
    1941       217395 :       base = unshare_expr (TREE_OPERAND (base, 0));
    1942              :     }
    1943              :   else
    1944              :     {
    1945      4318063 :       off = build_int_cst (force_ref_all ? ptr_type_node
    1946      1978179 :                            : reference_alias_ptr_type (prev_base),
    1947              :                            base_offset + byte_offset);
    1948      2339884 :       base = build_fold_addr_expr (unshare_expr (base));
    1949              :     }
    1950              : 
    1951      2594308 :   unsigned int align_bound = known_alignment (misalign + offset);
    1952      2594308 :   if (align_bound != 0)
    1953      1694020 :     align = MIN (align, align_bound);
    1954      2594308 :   if (align != TYPE_ALIGN (exp_type))
    1955       525671 :     exp_type = build_aligned_type (exp_type, align);
    1956              : 
    1957      2594308 :   mem_ref = fold_build2_loc (loc, MEM_REF, exp_type, base, off);
    1958      2594308 :   REF_REVERSE_STORAGE_ORDER (mem_ref) = reverse;
    1959      2594308 :   if (TREE_THIS_VOLATILE (prev_base))
    1960            6 :     TREE_THIS_VOLATILE (mem_ref) = 1;
    1961      2594308 :   if (TREE_SIDE_EFFECTS (prev_base))
    1962          126 :     TREE_SIDE_EFFECTS (mem_ref) = 1;
    1963      2594308 :   return mem_ref;
    1964              : }
    1965              : 
    1966              : /* Construct and return a memory reference that is equal to a portion of
    1967              :    MODEL->expr but is based on BASE.  If this cannot be done, return NULL.  */
    1968              : 
    1969              : static tree
    1970      1697764 : build_reconstructed_reference (location_t, tree base, struct access *model)
    1971              : {
    1972      1697764 :   tree expr = model->expr;
    1973              :   /* We have to make sure to start just below the outermost union.  */
    1974      1697764 :   tree start_expr = expr;
    1975      3499076 :   while (handled_component_p (expr))
    1976              :     {
    1977      1801312 :       if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == UNION_TYPE)
    1978         8408 :         start_expr = expr;
    1979      1801312 :       expr = TREE_OPERAND (expr, 0);
    1980              :     }
    1981              : 
    1982              :   expr = start_expr;
    1983              :   tree prev_expr = NULL_TREE;
    1984      3473088 :   while (!types_compatible_p (TREE_TYPE (expr), TREE_TYPE (base)))
    1985              :     {
    1986      1860359 :       if (!handled_component_p (expr))
    1987              :         return NULL_TREE;
    1988      1775324 :       prev_expr = expr;
    1989      1775324 :       expr = TREE_OPERAND (expr, 0);
    1990              :     }
    1991              : 
    1992              :   /* Guard against broken VIEW_CONVERT_EXPRs...  */
    1993      1612729 :   if (!prev_expr)
    1994              :     return NULL_TREE;
    1995              : 
    1996      1611753 :   TREE_OPERAND (prev_expr, 0) = base;
    1997      1611753 :   tree ref = unshare_expr (model->expr);
    1998      1611753 :   TREE_OPERAND (prev_expr, 0) = expr;
    1999      1611753 :   return ref;
    2000              : }
    2001              : 
    2002              : /* Construct a memory reference to a part of an aggregate BASE at the given
    2003              :    OFFSET and of the same type as MODEL.  In case this is a reference to a
    2004              :    bit-field, the function will replicate the last component_ref of model's
    2005              :    expr to access it.  INSERT_AFTER and GSI have the same meaning as in
    2006              :    build_ref_for_offset, furthermore, when GSI is NULL, the function expects
    2007              :    that it re-builds the entire reference from a DECL to the final access and
    2008              :    so will create a MEM_REF when OFFSET does not exactly match offset of
    2009              :    MODEL.  If FORCE_REF_ALL is true then the memory access will use
    2010              :    alias-set zero.  */
    2011              : 
    2012              : static tree
    2013      4136524 : build_ref_for_model (location_t loc, tree base, HOST_WIDE_INT offset,
    2014              :                      struct access *model, gimple_stmt_iterator *gsi,
    2015              :                      bool insert_after, bool force_ref_all = false)
    2016              : {
    2017      4136524 :   gcc_assert (offset >= 0);
    2018      4136524 :   if (TREE_CODE (model->expr) == COMPONENT_REF
    2019      4136524 :       && DECL_BIT_FIELD (TREE_OPERAND (model->expr, 1)))
    2020              :     {
    2021              :       /* This access represents a bit-field.  */
    2022        33017 :       tree t, exp_type, fld = TREE_OPERAND (model->expr, 1);
    2023              : 
    2024        33017 :       offset -= int_bit_position (fld);
    2025        33017 :       exp_type = TREE_TYPE (TREE_OPERAND (model->expr, 0));
    2026        33017 :       t = build_ref_for_offset (loc, base, offset, model->reverse, exp_type,
    2027              :                                 gsi, insert_after, force_ref_all);
    2028              :       /* The flag will be set on the record type.  */
    2029        33017 :       REF_REVERSE_STORAGE_ORDER (t) = 0;
    2030        33017 :       return fold_build3_loc (loc, COMPONENT_REF, TREE_TYPE (fld), t, fld,
    2031        33017 :                               NULL_TREE);
    2032              :     }
    2033              :   else
    2034              :     {
    2035      4103507 :       tree res;
    2036      4103507 :       if (model->grp_same_access_path
    2037      1785212 :           && !force_ref_all
    2038      1697797 :           && !TREE_THIS_VOLATILE (base)
    2039      1697791 :           && (TYPE_ADDR_SPACE (TREE_TYPE (base))
    2040      1697791 :               == TYPE_ADDR_SPACE (TREE_TYPE (model->expr)))
    2041      1697790 :           && (offset == model->offset
    2042        11757 :               || (gsi && offset <= model->offset))
    2043              :           /* build_reconstructed_reference can still fail if we have already
    2044              :              massaged BASE because of another type incompatibility.  */
    2045      5801271 :           && (res = build_reconstructed_reference (loc, base, model)))
    2046              :         return res;
    2047              :       else
    2048      2491754 :         return build_ref_for_offset (loc, base, offset, model->reverse,
    2049              :                                      model->type, gsi, insert_after,
    2050              :                                      force_ref_all);
    2051              :     }
    2052              : }
    2053              : 
    2054              : /* Attempt to build a memory reference that we could but into a gimple
    2055              :    debug_bind statement.  Similar to build_ref_for_model but punts if it has to
    2056              :    create statements and return s NULL instead.  This function also ignores
    2057              :    alignment issues and so its results should never end up in non-debug
    2058              :    statements.  */
    2059              : 
    2060              : static tree
    2061         5994 : build_debug_ref_for_model (location_t loc, tree base, HOST_WIDE_INT offset,
    2062              :                            struct access *model)
    2063              : {
    2064         5994 :   poly_int64 base_offset;
    2065         5994 :   tree off;
    2066              : 
    2067         5994 :   if (TREE_CODE (model->expr) == COMPONENT_REF
    2068         5994 :       && DECL_BIT_FIELD (TREE_OPERAND (model->expr, 1)))
    2069              :     return NULL_TREE;
    2070              : 
    2071         5994 :   base = get_addr_base_and_unit_offset (base, &base_offset);
    2072         5994 :   if (!base)
    2073              :     return NULL_TREE;
    2074         5994 :   if (TREE_CODE (base) == MEM_REF)
    2075              :     {
    2076          188 :       off = build_int_cst (TREE_TYPE (TREE_OPERAND (base, 1)),
    2077          188 :                            base_offset + offset / BITS_PER_UNIT);
    2078          188 :       off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1), off);
    2079          188 :       base = unshare_expr (TREE_OPERAND (base, 0));
    2080              :     }
    2081              :   else
    2082              :     {
    2083         5806 :       off = build_int_cst (reference_alias_ptr_type (base),
    2084         5806 :                            base_offset + offset / BITS_PER_UNIT);
    2085         5806 :       base = build_fold_addr_expr (unshare_expr (base));
    2086              :     }
    2087              : 
    2088         5994 :   return fold_build2_loc (loc, MEM_REF, model->type, base, off);
    2089              : }
    2090              : 
    2091              : /* Construct a memory reference consisting of component_refs and array_refs to
    2092              :    a part of an aggregate *RES which is of type TYPE.  The requested part
    2093              :    should have type EXP_TYPE at the given OFFSET.  CUR_SIZE must be the size of
    2094              :    *RES unless it is known that *RES alone cannot be the result.  This function
    2095              :    might not succeed, it returns true when it does and only then *RES points to
    2096              :    something meaningful.
    2097              : 
    2098              :    This function should be used only to build expressions that we might need to
    2099              :    present to user (e.g. in warnings).  In all other situations,
    2100              :    build_ref_for_model or build_ref_for_offset should be used instead.  */
    2101              : 
    2102              : static bool
    2103      4030840 : build_user_friendly_ref_for_offset (tree *res, tree type, HOST_WIDE_INT offset,
    2104              :                                     HOST_WIDE_INT cur_size, tree exp_type,
    2105              :                                     HOST_WIDE_INT exp_size)
    2106              : {
    2107      4095982 :   while (1)
    2108              :     {
    2109      4063411 :       tree fld;
    2110      4063411 :       tree tr_size, index, minidx;
    2111      4063411 :       HOST_WIDE_INT el_size;
    2112              : 
    2113      4063411 :       if (offset == 0
    2114      4063411 :           && cur_size == exp_size
    2115      4063411 :           && types_compatible_p (exp_type, type))
    2116              :         return true;
    2117              : 
    2118      2473846 :       switch (TREE_CODE (type))
    2119              :         {
    2120      2407248 :         case UNION_TYPE:
    2121      2407248 :         case QUAL_UNION_TYPE:
    2122      2407248 :         case RECORD_TYPE:
    2123     15113939 :           for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
    2124              :             {
    2125     15016671 :               HOST_WIDE_INT pos, size;
    2126     15016671 :               tree tr_pos, expr, *expr_ptr;
    2127              : 
    2128     15016671 :               if (TREE_CODE (fld) != FIELD_DECL)
    2129     12609576 :                 continue;
    2130              : 
    2131      3988366 :               tr_pos = bit_position (fld);
    2132      3988366 :               if (!tr_pos || !tree_fits_uhwi_p (tr_pos))
    2133            0 :                 continue;
    2134      3988366 :               pos = tree_to_uhwi (tr_pos);
    2135      3988366 :               gcc_assert (TREE_CODE (type) == RECORD_TYPE || pos == 0);
    2136      3988366 :               tr_size = DECL_SIZE (fld);
    2137      3988366 :               if (!tr_size || !tree_fits_uhwi_p (tr_size))
    2138            0 :                 continue;
    2139      3988366 :               size = tree_to_uhwi (tr_size);
    2140      3988366 :               if (size == 0)
    2141              :                 {
    2142        80715 :                   if (pos != offset)
    2143        32705 :                     continue;
    2144              :                 }
    2145      3907651 :               else if (pos > offset || (pos + size) <= offset)
    2146      1548566 :                 continue;
    2147              : 
    2148      2407095 :               expr = build3 (COMPONENT_REF, TREE_TYPE (fld), *res, fld,
    2149              :                              NULL_TREE);
    2150      2407095 :               expr_ptr = &expr;
    2151      2407095 :               if (build_user_friendly_ref_for_offset (expr_ptr, TREE_TYPE (fld),
    2152              :                                                       offset - pos, size,
    2153              :                                                       exp_type, exp_size))
    2154              :                 {
    2155      2309980 :                   *res = expr;
    2156      2309980 :                   return true;
    2157              :                 }
    2158              :             }
    2159              :           return false;
    2160              : 
    2161        32571 :         case ARRAY_TYPE:
    2162        32571 :           tr_size = TYPE_SIZE (TREE_TYPE (type));
    2163        32571 :           if (!tr_size || !tree_fits_uhwi_p (tr_size))
    2164              :             return false;
    2165        32571 :           el_size = tree_to_uhwi (tr_size);
    2166              : 
    2167        32571 :           minidx = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
    2168        32571 :           if (TREE_CODE (minidx) != INTEGER_CST || el_size == 0)
    2169              :             return false;
    2170        32571 :           index = build_int_cst (TYPE_DOMAIN (type), offset / el_size);
    2171        32571 :           if (!integer_zerop (minidx))
    2172          595 :             index = int_const_binop (PLUS_EXPR, index, minidx);
    2173        32571 :           *res = build4 (ARRAY_REF, TREE_TYPE (type), *res, index,
    2174              :                          NULL_TREE, NULL_TREE);
    2175        32571 :           offset = offset % el_size;
    2176        32571 :           cur_size = el_size;
    2177        32571 :           type = TREE_TYPE (type);
    2178        32571 :           break;
    2179              : 
    2180              :         default:
    2181              :           return false;
    2182              :         }
    2183        32571 :     }
    2184              : }
    2185              : 
    2186              : /* Print message to dump file why a variable was rejected. */
    2187              : 
    2188              : static void
    2189     15351041 : reject (tree var, const char *msg)
    2190              : {
    2191     15351041 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2192              :     {
    2193           92 :       fprintf (dump_file, "Rejected (%d): %s: ", DECL_UID (var), msg);
    2194           92 :       print_generic_expr (dump_file, var);
    2195           92 :       fprintf (dump_file, "\n");
    2196              :     }
    2197     15351041 : }
    2198              : 
    2199              : /* Return true if VAR is a candidate for SRA.  */
    2200              : 
    2201              : static bool
    2202     19762326 : maybe_add_sra_candidate (tree var)
    2203              : {
    2204     19762326 :   tree type = TREE_TYPE (var);
    2205     19762326 :   const char *msg;
    2206     19762326 :   tree_node **slot;
    2207              : 
    2208     19762326 :   if (!AGGREGATE_TYPE_P (type))
    2209              :     {
    2210     13742456 :       reject (var, "not aggregate");
    2211     13742456 :       return false;
    2212              :     }
    2213              : 
    2214      6019870 :   if ((is_global_var (var)
    2215              :        /* There are cases where non-addressable variables fail the
    2216              :           pt_solutions_check test, e.g in gcc.dg/uninit-40.c. */
    2217      5787377 :        || (TREE_ADDRESSABLE (var)
    2218      1604821 :            && pt_solution_includes (&cfun->gimple_df->escaped_return, var))
    2219      4415591 :        || (TREE_CODE (var) == RESULT_DECL
    2220            0 :            && !DECL_BY_REFERENCE (var)
    2221            0 :            && aggregate_value_p (var, current_function_decl)))
    2222              :       /* Allow constant-pool entries that "need to live in memory".  */
    2223      7391656 :       && !constant_decl_p (var))
    2224              :     {
    2225      1601697 :       reject (var, "needs to live in memory and escapes or global");
    2226      1601697 :       return false;
    2227              :     }
    2228      4418173 :   if (TREE_THIS_VOLATILE (var))
    2229              :     {
    2230          592 :       reject (var, "is volatile");
    2231          592 :       return false;
    2232              :     }
    2233      4417581 :   if (!COMPLETE_TYPE_P (type))
    2234              :     {
    2235            0 :       reject (var, "has incomplete type");
    2236            0 :       return false;
    2237              :     }
    2238      4417581 :   if (!tree_fits_shwi_p (TYPE_SIZE (type)))
    2239              :     {
    2240           43 :       reject (var, "type size not fixed");
    2241           43 :       return false;
    2242              :     }
    2243      4417538 :   if (tree_to_shwi (TYPE_SIZE (type)) == 0)
    2244              :     {
    2245         5995 :       reject (var, "type size is zero");
    2246         5995 :       return false;
    2247              :     }
    2248      4411543 :   if (type_internals_preclude_sra_p (type, &msg))
    2249              :     {
    2250          258 :       reject (var, msg);
    2251          258 :       return false;
    2252              :     }
    2253      4411285 :   if (/* Fix for PR 41089.  tree-stdarg.cc needs to have va_lists intact but
    2254              :          we also want to schedule it rather late.  Thus we ignore it in
    2255              :          the early pass. */
    2256      4411285 :       (sra_mode == SRA_MODE_EARLY_INTRA
    2257      4411285 :        && is_va_list_type (type)))
    2258              :     {
    2259            0 :       reject (var, "is va_list");
    2260            0 :       return false;
    2261              :     }
    2262              : 
    2263      4411285 :   bitmap_set_bit (candidate_bitmap, DECL_UID (var));
    2264      4411285 :   slot = candidates->find_slot_with_hash (var, DECL_UID (var), INSERT);
    2265      4411285 :   *slot = var;
    2266              : 
    2267      4411285 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2268              :     {
    2269           47 :       fprintf (dump_file, "Candidate (%d): ", DECL_UID (var));
    2270           47 :       print_generic_expr (dump_file, var);
    2271           47 :       fprintf (dump_file, "\n");
    2272              :     }
    2273              : 
    2274              :   return true;
    2275              : }
    2276              : 
    2277              : /* The very first phase of intraprocedural SRA.  It marks in candidate_bitmap
    2278              :    those with type which is suitable for scalarization.  */
    2279              : 
    2280              : static bool
    2281      3591423 : find_var_candidates (void)
    2282              : {
    2283      3591423 :   tree var, parm;
    2284      3591423 :   unsigned int i;
    2285      3591423 :   bool ret = false;
    2286              : 
    2287      3591423 :   for (parm = DECL_ARGUMENTS (current_function_decl);
    2288     11083794 :        parm;
    2289      7492371 :        parm = DECL_CHAIN (parm))
    2290      7492371 :     ret |= maybe_add_sra_candidate (parm);
    2291              : 
    2292     18960545 :   FOR_EACH_LOCAL_DECL (cfun, i, var)
    2293              :     {
    2294     12266007 :       if (!VAR_P (var))
    2295            0 :         continue;
    2296              : 
    2297     12266007 :       ret |= maybe_add_sra_candidate (var);
    2298              :     }
    2299              : 
    2300      3591423 :   return ret;
    2301              : }
    2302              : 
    2303              : /* Return true if EXP is a reference chain of COMPONENT_REFs and AREAY_REFs
    2304              :    ending either with a DECL or a MEM_REF with zero offset.  */
    2305              : 
    2306              : static bool
    2307      8876464 : path_comparable_for_same_access (tree expr)
    2308              : {
    2309     15356960 :   while (handled_component_p (expr))
    2310              :     {
    2311      6606681 :       if (TREE_CODE (expr) == ARRAY_REF)
    2312              :         {
    2313              :           /* SSA name indices can occur here too when the array is of size one.
    2314              :              But we cannot just re-use array_refs with SSA names elsewhere in
    2315              :              the function, so disallow non-constant indices.  TODO: Remove this
    2316              :              limitation after teaching build_reconstructed_reference to replace
    2317              :              the index with the index type lower bound.  */
    2318       700641 :           if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST)
    2319              :             return false;
    2320              :         }
    2321      6480496 :       expr = TREE_OPERAND (expr, 0);
    2322              :     }
    2323              : 
    2324      8750279 :   if (TREE_CODE (expr) == MEM_REF)
    2325              :     {
    2326      1094977 :       if (!zerop (TREE_OPERAND (expr, 1)))
    2327              :         return false;
    2328       630927 :       gcc_assert (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
    2329              :                   && DECL_P (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)));
    2330       630927 :       if (TYPE_MAIN_VARIANT (TREE_TYPE (expr))
    2331       630927 :           != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (expr, 0), 0))))
    2332       378824 :         return false;
    2333              :     }
    2334              :   else
    2335      7655302 :     gcc_assert (DECL_P (expr));
    2336              : 
    2337              :   return true;
    2338              : }
    2339              : 
    2340              : /* Assuming that EXP1 consists of only COMPONENT_REFs and ARRAY_REFs, return
    2341              :    true if the chain of these handled components are exactly the same as EXP2
    2342              :    and the expression under them is the same DECL or an equivalent MEM_REF.
    2343              :    The reference picked by compare_access_positions must go to EXP1.  */
    2344              : 
    2345              : static bool
    2346      4442662 : same_access_path_p (tree exp1, tree exp2)
    2347              : {
    2348      4442662 :   if (TREE_CODE (exp1) != TREE_CODE (exp2))
    2349              :     {
    2350              :       /* Special case single-field structures loaded sometimes as the field
    2351              :          and sometimes as the structure.  If the field is of a scalar type,
    2352              :          compare_access_positions will put it into exp1.
    2353              : 
    2354              :          TODO: The gimple register type condition can be removed if teach
    2355              :          compare_access_positions to put inner types first.  */
    2356       643009 :       if (is_gimple_reg_type (TREE_TYPE (exp1))
    2357       420858 :           && TREE_CODE (exp1) == COMPONENT_REF
    2358      1058412 :           && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp1, 0)))
    2359       415403 :               == TYPE_MAIN_VARIANT (TREE_TYPE (exp2))))
    2360       388567 :         exp1 = TREE_OPERAND (exp1, 0);
    2361              :       else
    2362              :         return false;
    2363              :     }
    2364              : 
    2365      4188220 :   if (!operand_equal_p (exp1, exp2, OEP_ADDRESS_OF))
    2366              :     return false;
    2367              : 
    2368              :   return true;
    2369              : }
    2370              : 
    2371              : /* Return true when either T1 is a type that, when loaded into a register and
    2372              :    stored back to memory will yield the same bits or when both T1 and T2 are
    2373              :    compatible.  */
    2374              : 
    2375              : static bool
    2376      5642844 : types_risk_mangled_binary_repr_p (tree t1, tree t2)
    2377              : {
    2378      5642844 :   if (mode_can_transfer_bits (TYPE_MODE (t1)))
    2379              :     return false;
    2380              : 
    2381         2742 :   return !types_compatible_p (t1, t2);
    2382              : }
    2383              : 
    2384              : /* Sort all accesses for the given variable, check for partial overlaps and
    2385              :    return NULL if there are any.  If there are none, pick a representative for
    2386              :    each combination of offset and size and create a linked list out of them.
    2387              :    Return the pointer to the first representative and make sure it is the first
    2388              :    one in the vector of accesses.  */
    2389              : 
    2390              : static struct access *
    2391      4285331 : sort_and_splice_var_accesses (tree var)
    2392              : {
    2393      4285331 :   int i, j, access_count;
    2394      4285331 :   struct access *res, **prev_acc_ptr = &res;
    2395      4285331 :   vec<access_p> *access_vec;
    2396      4285331 :   bool first = true;
    2397      4285331 :   HOST_WIDE_INT low = -1, high = 0;
    2398              : 
    2399      4285331 :   access_vec = get_base_access_vector (var);
    2400      4285331 :   if (!access_vec)
    2401              :     return NULL;
    2402      4107515 :   access_count = access_vec->length ();
    2403              : 
    2404              :   /* Sort by <OFFSET, SIZE>.  */
    2405      4107515 :   access_vec->qsort (compare_access_positions);
    2406              : 
    2407      4107515 :   i = 0;
    2408     13801508 :   while (i < access_count)
    2409              :     {
    2410      9699165 :       struct access *access = (*access_vec)[i];
    2411      9699165 :       bool grp_write = access->write;
    2412      9699165 :       bool grp_read = !access->write;
    2413      9699165 :       bool grp_scalar_write = access->write
    2414      9699165 :         && is_gimple_reg_type (access->type);
    2415      9699165 :       bool grp_scalar_read = !access->write
    2416      9699165 :         && is_gimple_reg_type (access->type);
    2417      9699165 :       bool grp_assignment_read = access->grp_assignment_read;
    2418      9699165 :       bool grp_assignment_write = access->grp_assignment_write;
    2419      9699165 :       bool multiple_scalar_reads = false;
    2420      9699165 :       bool grp_partial_lhs = access->grp_partial_lhs;
    2421      9699165 :       bool first_scalar = is_gimple_reg_type (access->type);
    2422      9699165 :       bool unscalarizable_region = access->grp_unscalarizable_region;
    2423      9699165 :       bool grp_same_access_path = access->grp_same_access_path;
    2424      9699165 :       bool bf_non_full_precision
    2425      9699165 :         = (INTEGRAL_TYPE_P (access->type)
    2426      3286403 :            && TYPE_PRECISION (access->type) != access->size
    2427       169357 :            && TREE_CODE (access->expr) == COMPONENT_REF
    2428      9774819 :            && DECL_BIT_FIELD (TREE_OPERAND (access->expr, 1)));
    2429              : 
    2430      9699165 :       if (first || access->offset >= high)
    2431              :         {
    2432      4575668 :           first = false;
    2433      4575668 :           low = access->offset;
    2434      4575668 :           high = access->offset + access->size;
    2435              :         }
    2436      5123497 :       else if (access->offset > low && access->offset + access->size > high)
    2437              :         return NULL;
    2438              :       else
    2439      5122863 :         gcc_assert (access->offset >= low
    2440              :                     && access->offset + access->size <= high);
    2441              : 
    2442      9698531 :       if (INTEGRAL_TYPE_P (access->type)
    2443      3285946 :           && TYPE_PRECISION (access->type) != access->size
    2444      9867469 :           && bitmap_bit_p (passed_by_ref_in_call, DECL_UID (access->base)))
    2445              :         {
    2446              :           /* This can lead to performance regressions because we can generate
    2447              :              excessive zero extensions.  */
    2448         4538 :           if (dump_file && (dump_flags & TDF_DETAILS))
    2449              :             {
    2450            0 :               fprintf (dump_file, "Won't scalarize ");
    2451            0 :               print_generic_expr (dump_file, access->base);
    2452            0 :               fprintf (dump_file, "(%d), it is passed by reference to a call "
    2453              :                        "and there are accesses with precision not covering "
    2454            0 :                        "their type size.", DECL_UID (access->base));
    2455              :             }
    2456              :           return NULL;
    2457              :         }
    2458              : 
    2459      9693993 :       if (grp_same_access_path)
    2460      8876464 :         grp_same_access_path = path_comparable_for_same_access (access->expr);
    2461              : 
    2462      9693993 :       j = i + 1;
    2463     15181015 :       while (j < access_count)
    2464              :         {
    2465     11078672 :           struct access *ac2 = (*access_vec)[j];
    2466     11078672 :           if (ac2->offset != access->offset || ac2->size != access->size)
    2467              :             break;
    2468      5487022 :           if (ac2->write)
    2469              :             {
    2470      1346874 :               grp_write = true;
    2471      1346874 :               grp_scalar_write = (grp_scalar_write
    2472      1346874 :                                   || is_gimple_reg_type (ac2->type));
    2473              :             }
    2474              :           else
    2475              :             {
    2476      4140148 :               grp_read = true;
    2477      4140148 :               if (is_gimple_reg_type (ac2->type))
    2478              :                 {
    2479      1799706 :                   if (grp_scalar_read)
    2480              :                     multiple_scalar_reads = true;
    2481              :                   else
    2482       390746 :                     grp_scalar_read = true;
    2483              :                 }
    2484              :             }
    2485      5487022 :           grp_assignment_read |= ac2->grp_assignment_read;
    2486      5487022 :           grp_assignment_write |= ac2->grp_assignment_write;
    2487      5487022 :           grp_partial_lhs |= ac2->grp_partial_lhs;
    2488      5487022 :           unscalarizable_region |= ac2->grp_unscalarizable_region;
    2489      5487022 :           relink_to_new_repr (access, ac2);
    2490              : 
    2491              :           /* If there are both aggregate-type and scalar-type accesses with
    2492              :              this combination of size and offset, the comparison function
    2493              :              should have put the scalars first.  */
    2494      5487022 :           gcc_assert (first_scalar || !is_gimple_reg_type (ac2->type));
    2495              :           /* It also prefers integral types to non-integral.  However, when the
    2496              :              precision of the selected type does not span the entire area and
    2497              :              should also be used for a non-integer (i.e. float), we must not
    2498              :              let that happen.  Normally analyze_access_subtree expands the type
    2499              :              to cover the entire area but for bit-fields it doesn't.  */
    2500      5487022 :           if (bf_non_full_precision && !INTEGRAL_TYPE_P (ac2->type))
    2501              :             {
    2502            0 :               if (dump_file && (dump_flags & TDF_DETAILS))
    2503              :                 {
    2504            0 :                   fprintf (dump_file, "Cannot scalarize the following access "
    2505              :                            "because insufficient precision integer type was "
    2506              :                            "selected.\n  ");
    2507            0 :                   dump_access (dump_file, access, false);
    2508              :                 }
    2509              :               unscalarizable_region = true;
    2510              :             }
    2511      5487022 :           else if (types_risk_mangled_binary_repr_p (access->type, ac2->type))
    2512              :             {
    2513          856 :               if (dump_file && (dump_flags & TDF_DETAILS))
    2514              :                 {
    2515            0 :                   fprintf (dump_file, "Cannot scalarize the following access "
    2516              :                            "because data would be held in a mode which is not "
    2517              :                            "guaranteed to preserve all bits.\n  ");
    2518            0 :                   dump_access (dump_file, access, false);
    2519              :                 }
    2520              :               unscalarizable_region = true;
    2521              :             }
    2522              :           /* If there the same place is accessed with two incompatible
    2523              :              aggregate types, trying to base total scalarization on either of
    2524              :              them can be wrong.  */
    2525      5487022 :           if (!first_scalar && !types_compatible_p (access->type, ac2->type))
    2526       450806 :             bitmap_set_bit (cannot_scalarize_away_bitmap,
    2527       225403 :                             DECL_UID (access->base));
    2528              : 
    2529      5487022 :           if (grp_same_access_path
    2530      5487022 :               && (!ac2->grp_same_access_path
    2531      4442662 :                   || !same_access_path_p (access->expr, ac2->expr)))
    2532              :             grp_same_access_path = false;
    2533              : 
    2534      5487022 :           ac2->group_representative = access;
    2535      5487022 :           j++;
    2536              :         }
    2537              : 
    2538      9693993 :       i = j;
    2539              : 
    2540      9693993 :       access->group_representative = access;
    2541      9693993 :       access->grp_write = grp_write;
    2542      9693993 :       access->grp_read = grp_read;
    2543      9693993 :       access->grp_scalar_read = grp_scalar_read;
    2544      9693993 :       access->grp_scalar_write = grp_scalar_write;
    2545      9693993 :       access->grp_assignment_read = grp_assignment_read;
    2546      9693993 :       access->grp_assignment_write = grp_assignment_write;
    2547      9693993 :       access->grp_hint = multiple_scalar_reads && !constant_decl_p (var);
    2548      9693993 :       access->grp_partial_lhs = grp_partial_lhs;
    2549      9693993 :       access->grp_unscalarizable_region = unscalarizable_region;
    2550      9693993 :       access->grp_same_access_path = grp_same_access_path;
    2551              : 
    2552      9693993 :       *prev_acc_ptr = access;
    2553      9693993 :       prev_acc_ptr = &access->next_grp;
    2554              :     }
    2555              : 
    2556      4102343 :   gcc_assert (res == (*access_vec)[0]);
    2557              :   return res;
    2558              : }
    2559              : 
    2560              : /* Create a variable for the given ACCESS which determines the type, name and a
    2561              :    few other properties.  Return the variable declaration and store it also to
    2562              :    ACCESS->replacement.  REG_TREE is used when creating a declaration to base a
    2563              :    default-definition SSA name on in order to facilitate an uninitialized
    2564              :    warning.  It is used instead of the actual ACCESS type if that is not of a
    2565              :    gimple register type.  */
    2566              : 
    2567              : static tree
    2568      4048701 : create_access_replacement (struct access *access, tree reg_type = NULL_TREE)
    2569              : {
    2570      4048701 :   tree repl;
    2571              : 
    2572      4048701 :   tree type = access->type;
    2573      4048701 :   if (reg_type && !is_gimple_reg_type (type))
    2574              :     type = reg_type;
    2575              : 
    2576      4048701 :   if (access->grp_to_be_debug_replaced)
    2577              :     {
    2578       276406 :       repl = create_tmp_var_raw (access->type);
    2579       276406 :       DECL_CONTEXT (repl) = current_function_decl;
    2580              :     }
    2581              :   else
    2582              :     /* Drop any special alignment on the type if it's not on the main
    2583              :        variant.  This avoids issues with weirdo ABIs like AAPCS.  */
    2584      3772295 :     repl = create_tmp_var (build_qualified_type (TYPE_MAIN_VARIANT (type),
    2585      3772295 :                                                  TYPE_QUALS (type)), "SR");
    2586      4048701 :   if (access->grp_partial_lhs
    2587      4048701 :       && is_gimple_reg_type (type))
    2588          998 :     DECL_NOT_GIMPLE_REG_P (repl) = 1;
    2589              : 
    2590      4048701 :   DECL_SOURCE_LOCATION (repl) = DECL_SOURCE_LOCATION (access->base);
    2591      4048701 :   DECL_ARTIFICIAL (repl) = 1;
    2592      4048701 :   DECL_IGNORED_P (repl) = DECL_IGNORED_P (access->base);
    2593              : 
    2594      4048701 :   if (DECL_NAME (access->base)
    2595      4048701 :       && ((!DECL_IGNORED_P (access->base) && !DECL_ARTIFICIAL (access->base))
    2596      1907123 :           || (VAR_P (access->base) && DECL_NONLOCAL_FRAME (access->base))))
    2597              :     {
    2598      1070644 :       char *pretty_name = make_fancy_name (access->expr);
    2599      1070644 :       tree debug_expr = unshare_expr_without_location (access->expr), d;
    2600      1070644 :       bool fail = false;
    2601              : 
    2602      1070644 :       DECL_NAME (repl) = get_identifier (pretty_name);
    2603      1070644 :       DECL_NAMELESS (repl) = 1;
    2604      1070644 :       obstack_free (&name_obstack, pretty_name);
    2605              : 
    2606              :       /* Get rid of any SSA_NAMEs embedded in debug_expr,
    2607              :          as DECL_DEBUG_EXPR isn't considered when looking for still
    2608              :          used SSA_NAMEs and thus they could be freed.  All debug info
    2609              :          generation cares is whether something is constant or variable
    2610              :          and that get_ref_base_and_extent works properly on the
    2611              :          expression.  It cannot handle accesses at a non-constant offset
    2612              :          though, so just give up in those cases.  */
    2613      1070644 :       for (d = debug_expr;
    2614      3769679 :            !fail && (handled_component_p (d) || TREE_CODE (d) == MEM_REF);
    2615      1395847 :            d = TREE_OPERAND (d, 0))
    2616      1395847 :         switch (TREE_CODE (d))
    2617              :           {
    2618        65691 :           case ARRAY_REF:
    2619        65691 :           case ARRAY_RANGE_REF:
    2620        65691 :             if (TREE_OPERAND (d, 1)
    2621        65691 :                 && TREE_CODE (TREE_OPERAND (d, 1)) != INTEGER_CST)
    2622              :               fail = true;
    2623        65691 :             if (TREE_OPERAND (d, 3)
    2624        65691 :                 && TREE_CODE (TREE_OPERAND (d, 3)) != INTEGER_CST)
    2625              :               fail = true;
    2626              :             /* FALLTHRU */
    2627      1160953 :           case COMPONENT_REF:
    2628      1160953 :             if (TREE_OPERAND (d, 2)
    2629      1160953 :                 && TREE_CODE (TREE_OPERAND (d, 2)) != INTEGER_CST)
    2630              :               fail = true;
    2631              :             break;
    2632       232672 :           case MEM_REF:
    2633       232672 :             if (TREE_CODE (TREE_OPERAND (d, 0)) != ADDR_EXPR)
    2634              :               fail = true;
    2635              :             else
    2636       232672 :               d = TREE_OPERAND (d, 0);
    2637              :             break;
    2638              :           default:
    2639              :             break;
    2640              :           }
    2641      1070644 :       if (!fail)
    2642              :         {
    2643      1070516 :           SET_DECL_DEBUG_EXPR (repl, debug_expr);
    2644      1070516 :           DECL_HAS_DEBUG_EXPR_P (repl) = 1;
    2645              :         }
    2646      1070644 :       if (access->grp_no_warning)
    2647          517 :         suppress_warning (repl /* Be more selective! */);
    2648              :       else
    2649      1070127 :         copy_warning (repl, access->base);
    2650              :     }
    2651              :   else
    2652      2978057 :     suppress_warning (repl /* Be more selective! */);
    2653              : 
    2654      4048701 :   if (dump_file)
    2655              :     {
    2656          221 :       if (access->grp_to_be_debug_replaced)
    2657              :         {
    2658            4 :           fprintf (dump_file, "Created a debug-only replacement for ");
    2659            4 :           print_generic_expr (dump_file, access->base);
    2660            4 :           fprintf (dump_file, " offset: %u, size: %u\n",
    2661            4 :                    (unsigned) access->offset, (unsigned) access->size);
    2662              :         }
    2663              :       else
    2664              :         {
    2665          217 :           fprintf (dump_file, "Created a replacement for ");
    2666          217 :           print_generic_expr (dump_file, access->base);
    2667          217 :           fprintf (dump_file, " offset: %u, size: %u: ",
    2668          217 :                    (unsigned) access->offset, (unsigned) access->size);
    2669          217 :           print_generic_expr (dump_file, repl, TDF_UID);
    2670          217 :           fprintf (dump_file, "\n");
    2671              :         }
    2672              :     }
    2673      4048701 :   sra_stats.replacements++;
    2674              : 
    2675      4048701 :   return repl;
    2676              : }
    2677              : 
    2678              : /* Return ACCESS scalar replacement, which must exist.  */
    2679              : 
    2680              : static inline tree
    2681     13839922 : get_access_replacement (struct access *access)
    2682              : {
    2683     13839922 :   gcc_checking_assert (access->replacement_decl);
    2684     13839922 :   return access->replacement_decl;
    2685              : }
    2686              : 
    2687              : 
    2688              : /* Build a subtree of accesses rooted in *ACCESS, and move the pointer in the
    2689              :    linked list along the way.  Stop when *ACCESS is NULL or the access pointed
    2690              :    to it is not "within" the root.  Return false iff some accesses partially
    2691              :    overlap.  */
    2692              : 
    2693              : static bool
    2694      9673106 : build_access_subtree (struct access **access)
    2695              : {
    2696      9673106 :   struct access *root = *access, *last_child = NULL;
    2697      9673106 :   HOST_WIDE_INT limit = root->offset + root->size;
    2698              : 
    2699      9673106 :   *access = (*access)->next_grp;
    2700     14773082 :   while  (*access && (*access)->offset + (*access)->size <= limit)
    2701              :     {
    2702      5102623 :       if (!last_child)
    2703      2038223 :         root->first_child = *access;
    2704              :       else
    2705      3064400 :         last_child->next_sibling = *access;
    2706      5102623 :       last_child = *access;
    2707      5102623 :       (*access)->parent = root;
    2708      5102623 :       (*access)->grp_write |= root->grp_write;
    2709              : 
    2710      5102623 :       if (!build_access_subtree (access))
    2711              :         return false;
    2712              :     }
    2713              : 
    2714      9670459 :   if (*access && (*access)->offset < limit)
    2715         2452 :     return false;
    2716              : 
    2717              :   return true;
    2718              : }
    2719              : 
    2720              : /* Build a tree of access representatives, ACCESS is the pointer to the first
    2721              :    one, others are linked in a list by the next_grp field.  Return false iff
    2722              :    some accesses partially overlap.  */
    2723              : 
    2724              : static bool
    2725      4102343 : build_access_trees (struct access *access)
    2726              : {
    2727      8670374 :   while (access)
    2728              :     {
    2729      4570483 :       struct access *root = access;
    2730              : 
    2731      4570483 :       if (!build_access_subtree (&access))
    2732              :         return false;
    2733      4568031 :       root->next_grp = access;
    2734              :     }
    2735              :   return true;
    2736              : }
    2737              : 
    2738              : /* Traverse the access forest where ROOT is the first root and verify that
    2739              :    various important invariants hold true.  */
    2740              : 
    2741              : DEBUG_FUNCTION void
    2742      4099891 : verify_sra_access_forest (struct access *root)
    2743              : {
    2744      4099891 :   struct access *access = root;
    2745      4099891 :   tree first_base = root->base;
    2746      4099891 :   gcc_assert (DECL_P (first_base));
    2747     11647033 :   do
    2748              :     {
    2749     11647033 :       gcc_assert (access->base == first_base);
    2750     11647033 :       if (access->parent)
    2751      7079017 :         gcc_assert (access->offset >= access->parent->offset
    2752              :                     && access->size <= access->parent->size);
    2753     11647033 :       if (access->next_sibling)
    2754      4180691 :         gcc_assert (access->next_sibling->offset
    2755              :                     >= access->offset + access->size);
    2756              : 
    2757     11647033 :       poly_int64 poffset, psize, pmax_size;
    2758     11647033 :       bool reverse;
    2759     11647033 :       tree base = get_ref_base_and_extent (access->expr, &poffset, &psize,
    2760              :                                            &pmax_size, &reverse);
    2761     11647033 :       HOST_WIDE_INT offset, size, max_size;
    2762     11647033 :       if (!poffset.is_constant (&offset)
    2763     11647033 :           || !psize.is_constant (&size)
    2764     11647033 :           || !pmax_size.is_constant (&max_size))
    2765              :         gcc_unreachable ();
    2766     11647033 :       gcc_assert (base == first_base);
    2767     11647033 :       gcc_assert (offset == access->offset);
    2768     11647033 :       gcc_assert (access->grp_unscalarizable_region
    2769              :                   || access->grp_total_scalarization
    2770              :                   || size == max_size);
    2771     11647033 :       gcc_assert (access->grp_unscalarizable_region
    2772              :                   || !is_gimple_reg_type (access->type)
    2773              :                   || size == access->size);
    2774     11647033 :       gcc_assert (reverse == access->reverse);
    2775              : 
    2776     11647033 :       if (access->first_child)
    2777              :         {
    2778      2898326 :           gcc_assert (access->first_child->parent == access);
    2779              :           access = access->first_child;
    2780              :         }
    2781      8748707 :       else if (access->next_sibling)
    2782              :         {
    2783      3991966 :           gcc_assert (access->next_sibling->parent == access->parent);
    2784              :           access = access->next_sibling;
    2785              :         }
    2786              :       else
    2787              :         {
    2788      7655067 :           while (access->parent && !access->next_sibling)
    2789              :             access = access->parent;
    2790      4756741 :           if (access->next_sibling)
    2791              :             access = access->next_sibling;
    2792              :           else
    2793              :             {
    2794      4568016 :               gcc_assert (access == root);
    2795      4568016 :               root = root->next_grp;
    2796      4568016 :               access = root;
    2797              :             }
    2798              :         }
    2799              :     }
    2800     11647033 :   while (access);
    2801      4099891 : }
    2802              : 
    2803              : /* Verify access forests of all candidates with accesses by calling
    2804              :    verify_access_forest on each on them.  */
    2805              : 
    2806              : DEBUG_FUNCTION void
    2807       771505 : verify_all_sra_access_forests (void)
    2808              : {
    2809       771505 :   bitmap_iterator bi;
    2810       771505 :   unsigned i;
    2811      4871396 :   EXECUTE_IF_SET_IN_BITMAP (candidate_bitmap, 0, i, bi)
    2812              :     {
    2813      4099891 :       tree var = candidate (i);
    2814      4099891 :       struct access *access = get_first_repr_for_decl (var);
    2815      4099891 :       if (access)
    2816              :         {
    2817      4099891 :           gcc_assert (access->base == var);
    2818      4099891 :           verify_sra_access_forest (access);
    2819              :         }
    2820              :     }
    2821       771505 : }
    2822              : 
    2823              : /* Return true if expr contains some ARRAY_REFs into a variable bounded
    2824              :    array.  */
    2825              : 
    2826              : static bool
    2827     11253927 : expr_with_var_bounded_array_refs_p (tree expr)
    2828              : {
    2829     21307060 :   while (handled_component_p (expr))
    2830              :     {
    2831     10053133 :       if (TREE_CODE (expr) == ARRAY_REF
    2832     10053133 :           && !tree_fits_shwi_p (array_ref_low_bound (expr)))
    2833              :         return true;
    2834     10053133 :       expr = TREE_OPERAND (expr, 0);
    2835              :     }
    2836              :   return false;
    2837              : }
    2838              : 
    2839              : /* Return true if loading a value of MODE into a register and storing it back
    2840              :    preserves all bits of its precision.  Unlike mode_can_transfer_bits, do not
    2841              :    consider padding bits outside of the precision, such as those of XFmode,
    2842              :    which are not part of the value.  */
    2843              : 
    2844              : static bool
    2845      7079954 : mode_preserves_value_bits_p (machine_mode mode)
    2846              : {
    2847      7079954 :   if (mode == BLKmode || !targetm.mode_can_transfer_bits)
    2848              :     return true;
    2849      7079888 :   return targetm.mode_can_transfer_bits (mode);
    2850              : }
    2851              : 
    2852              : /* Return true if the data of scalar ACCESS, a child of PARENT, may both come
    2853              :    from an aggregate copy or from outside of the function and be copied out as
    2854              :    a part of an aggregate, so that its bit pattern rather than only its value
    2855              :    has to be preserved by a replacement.  All accesses in the group of ACCESS
    2856              :    have compatible types if the mode of its type cannot transfer bits (see
    2857              :    sort_and_splice_var_accesses), so copies of aggregates are only visible in
    2858              :    the flags of PARENT which have been inherited from all its ancestors.  */
    2859              : 
    2860              : static bool
    2861         1578 : access_bits_copied_through_p (struct access *access, struct access *parent)
    2862              : {
    2863         1578 :   if (!parent || !parent->grp_read)
    2864              :     return false;
    2865         1534 :   return parent->grp_write || comes_initialized_p (access->base);
    2866              : }
    2867              : 
    2868              : /* Analyze the subtree of accesses rooted in ROOT, scheduling replacements when
    2869              :    both seeming beneficial and when ALLOW_REPLACEMENTS allows it.  If TOTALLY
    2870              :    is set, we are totally scalarizing the aggregate.  Also set all sorts of
    2871              :    access flags appropriately along the way, notably always set grp_read and
    2872              :    grp_assign_read according to MARK_READ and grp_write when MARK_WRITE is
    2873              :    true.
    2874              : 
    2875              :    Creating a replacement for a scalar access is considered beneficial if its
    2876              :    grp_hint ot TOTALLY is set (this means either that there is more than one
    2877              :    direct read access or that we are attempting total scalarization) or
    2878              :    according to the following table:
    2879              : 
    2880              :    Access written to through a scalar type (once or more times)
    2881              :    |
    2882              :    |    Written to in an assignment statement
    2883              :    |    |
    2884              :    |    |       Access read as scalar _once_
    2885              :    |    |       |
    2886              :    |    |       |       Read in an assignment statement
    2887              :    |    |       |       |
    2888              :    |    |       |       |       Scalarize       Comment
    2889              : -----------------------------------------------------------------------------
    2890              :    0    0       0       0                       No access for the scalar
    2891              :    0    0       0       1                       No access for the scalar
    2892              :    0    0       1       0       No              Single read - won't help
    2893              :    0    0       1       1       No              The same case
    2894              :    0    1       0       0                       No access for the scalar
    2895              :    0    1       0       1                       No access for the scalar
    2896              :    0    1       1       0       Yes             s = *g; return s.i;
    2897              :    0    1       1       1       Yes             The same case as above
    2898              :    1    0       0       0       No              Won't help
    2899              :    1    0       0       1       Yes             s.i = 1; *g = s;
    2900              :    1    0       1       0       Yes             s.i = 5; g = s.i;
    2901              :    1    0       1       1       Yes             The same case as above
    2902              :    1    1       0       0       No              Won't help.
    2903              :    1    1       0       1       Yes             s.i = 1; *g = s;
    2904              :    1    1       1       0       Yes             s = *g; return s.i;
    2905              :    1    1       1       1       Yes             Any of the above yeses  */
    2906              : 
    2907              : static bool
    2908     11647033 : analyze_access_subtree (struct access *root, struct access *parent,
    2909              :                         bool allow_replacements, bool totally)
    2910              : {
    2911     11647033 :   struct access *child;
    2912     11647033 :   HOST_WIDE_INT limit = root->offset + root->size;
    2913     11647033 :   HOST_WIDE_INT covered_to = root->offset;
    2914     11647033 :   bool scalar = is_gimple_reg_type (root->type);
    2915     11647033 :   bool hole = false, sth_created = false;
    2916              : 
    2917     11647033 :   if (parent)
    2918              :     {
    2919      7079017 :       if (parent->grp_read)
    2920      6271595 :         root->grp_read = 1;
    2921      7079017 :       if (parent->grp_assignment_read)
    2922      2959751 :         root->grp_assignment_read = 1;
    2923      7079017 :       if (parent->grp_write)
    2924      4151679 :         root->grp_write = 1;
    2925      7079017 :       if (parent->grp_assignment_write)
    2926      3040528 :         root->grp_assignment_write = 1;
    2927      7079017 :       if (!parent->grp_same_access_path)
    2928      1199307 :         root->grp_same_access_path = 0;
    2929              :     }
    2930              : 
    2931     11647033 :   if (root->grp_unscalarizable_region)
    2932              :     allow_replacements = false;
    2933              : 
    2934     11518754 :   if (allow_replacements && expr_with_var_bounded_array_refs_p (root->expr))
    2935              :     allow_replacements = false;
    2936              : 
    2937     11647033 :   if (!totally && root->grp_result_of_prop_from_lhs)
    2938     11647033 :     allow_replacements = false;
    2939              : 
    2940     18726050 :   for (child = root->first_child; child; child = child->next_sibling)
    2941              :     {
    2942      7079017 :       if (totally)
    2943      1879488 :         covered_to = child->offset;
    2944              :       else
    2945      5199529 :         hole |= covered_to < child->offset;
    2946      7079017 :       sth_created |= analyze_access_subtree (child, root,
    2947      7079017 :                                              allow_replacements && !scalar
    2948      6788133 :                                              && !root->grp_partial_lhs,
    2949              :                                              totally);
    2950              : 
    2951      7079017 :       root->grp_unscalarized_data |= child->grp_unscalarized_data;
    2952      7079017 :       if (child->grp_covered)
    2953      3369429 :         covered_to += child->size;
    2954              :       else
    2955              :         hole = true;
    2956      7079017 :       if (totally && !hole)
    2957      1878108 :         covered_to = limit;
    2958              :     }
    2959              : 
    2960              :   /* Data copied in between aggregates must not be held in a replacement
    2961              :      whose mode cannot transfer all bits.  Use a bitwise type of the same size
    2962              :      for the replacement instead.  Direct scalar accesses then either
    2963              :      view-convert the replacement or store it to or load it from the
    2964              :      aggregate, which preserves the semantics of the original loads and
    2965              :      stores.  If there is no suitable bitwise type, do not create a
    2966              :      replacement.  */
    2967     11647033 :   tree bitwise_type = NULL_TREE;
    2968      7089867 :   if (allow_replacements && scalar && !root->first_child
    2969      7079954 :       && !mode_preserves_value_bits_p (TYPE_MODE (root->type))
    2970     11648611 :       && access_bits_copied_through_p (root, parent))
    2971              :     {
    2972          900 :       bitwise_type = bitwise_type_for_mode (TYPE_MODE (root->type));
    2973          900 :       if (!bitwise_type)
    2974              :         {
    2975            0 :           if (dump_file && (dump_flags & TDF_DETAILS))
    2976              :             {
    2977            0 :               fprintf (dump_file, "Not creating a replacement for ");
    2978            0 :               print_generic_expr (dump_file, root->base);
    2979            0 :               fprintf (dump_file, " offset: %u, size: %u because data would be "
    2980              :                        "held in a mode which is not guaranteed to preserve all "
    2981            0 :                        "bits.\n", (unsigned) root->offset, (unsigned) root->size);
    2982              :             }
    2983              :           allow_replacements = false;
    2984              :         }
    2985              :     }
    2986              : 
    2987     11647033 :   if (allow_replacements && scalar && !root->first_child
    2988      7079954 :       && (totally || !root->grp_total_scalarization)
    2989              :       && (totally
    2990      5291435 :           || root->grp_hint
    2991      4462988 :           || ((root->grp_scalar_read || root->grp_assignment_read)
    2992      1545710 :               && (root->grp_scalar_write || root->grp_assignment_write))))
    2993              :     {
    2994      3771748 :       if (bitwise_type)
    2995              :         {
    2996          832 :           root->type = bitwise_type;
    2997         1664 :           root->expr = build_ref_for_offset (UNKNOWN_LOCATION, root->base,
    2998          832 :                                              root->offset, root->reverse,
    2999              :                                              root->type, NULL, false);
    3000              : 
    3001          832 :           if (dump_file && (dump_flags & TDF_DETAILS))
    3002              :             {
    3003           34 :               fprintf (dump_file, "Changing the type of a replacement for ");
    3004           34 :               print_generic_expr (dump_file, root->base);
    3005           34 :               fprintf (dump_file, " offset: %u, size: %u ",
    3006           34 :                        (unsigned) root->offset, (unsigned) root->size);
    3007           34 :               fprintf (dump_file, " to a bitwise type.\n");
    3008              :             }
    3009              :         }
    3010              :       /* Always create access replacements that cover the whole access.
    3011              :          For integral types this means the precision has to match.
    3012              :          Avoid assumptions based on the integral type kind, too.  */
    3013      3770916 :       else if (INTEGRAL_TYPE_P (root->type)
    3014      1708977 :           && ((TREE_CODE (root->type) != INTEGER_TYPE
    3015      1708977 :                && TREE_CODE (root->type) != BITINT_TYPE)
    3016      1639992 :               || TYPE_PRECISION (root->type) != root->size)
    3017              :           /* But leave bitfield accesses alone.  */
    3018      3839914 :           && (TREE_CODE (root->expr) != COMPONENT_REF
    3019        67907 :               || !DECL_BIT_FIELD (TREE_OPERAND (root->expr, 1))))
    3020              :         {
    3021        68705 :           tree rt = root->type;
    3022        68705 :           gcc_assert ((root->offset % BITS_PER_UNIT) == 0
    3023              :                       && (root->size % BITS_PER_UNIT) == 0);
    3024        68705 :           if (BITINT_TYPE_P (root->type))
    3025           13 :             root->type = build_bitint_type (root->size, TYPE_UNSIGNED (rt));
    3026              :           else
    3027        68692 :             root->type = build_nonstandard_integer_type (root->size,
    3028        68692 :                                                          TYPE_UNSIGNED (rt));
    3029       137410 :           root->expr = build_ref_for_offset (UNKNOWN_LOCATION, root->base,
    3030        68705 :                                              root->offset, root->reverse,
    3031              :                                              root->type, NULL, false);
    3032              : 
    3033        68705 :           if (dump_file && (dump_flags & TDF_DETAILS))
    3034              :             {
    3035            0 :               fprintf (dump_file, "Changing the type of a replacement for ");
    3036            0 :               print_generic_expr (dump_file, root->base);
    3037            0 :               fprintf (dump_file, " offset: %u, size: %u ",
    3038            0 :                        (unsigned) root->offset, (unsigned) root->size);
    3039            0 :               fprintf (dump_file, " to an integer.\n");
    3040              :             }
    3041              :         }
    3042              : 
    3043      3771748 :       root->grp_to_be_replaced = 1;
    3044      3771748 :       root->replacement_decl = create_access_replacement (root);
    3045      3771748 :       sth_created = true;
    3046      3771748 :       hole = false;
    3047              :     }
    3048              :   else
    3049              :     {
    3050      7875285 :       if (allow_replacements
    3051      3318119 :           && scalar && !root->first_child
    3052      3308206 :           && !root->grp_total_scalarization
    3053      3308066 :           && (root->grp_scalar_write || root->grp_assignment_write)
    3054     10792563 :           && !bitmap_bit_p (cannot_scalarize_away_bitmap,
    3055      2917278 :                             DECL_UID (root->base)))
    3056              :         {
    3057       537331 :           gcc_checking_assert (!root->grp_scalar_read
    3058              :                                && !root->grp_assignment_read);
    3059       537331 :           sth_created = true;
    3060       537331 :           if (MAY_HAVE_DEBUG_BIND_STMTS)
    3061              :             {
    3062       276406 :               root->grp_to_be_debug_replaced = 1;
    3063       276406 :               root->replacement_decl = create_access_replacement (root);
    3064              :             }
    3065              :         }
    3066              : 
    3067      7875285 :       if (covered_to < limit)
    3068      6619850 :         hole = true;
    3069      7875285 :       if (scalar || !allow_replacements)
    3070      4288988 :         root->grp_total_scalarization = 0;
    3071              :     }
    3072              : 
    3073     11647033 :   if (!hole)
    3074      5026709 :     root->grp_covered = 1;
    3075      6620324 :   else if (root->grp_write || comes_initialized_p (root->base))
    3076      5827951 :     root->grp_unscalarized_data = 1; /* not covered and written to */
    3077     11647033 :   return sth_created;
    3078              : }
    3079              : 
    3080              : /* Analyze all access trees linked by next_grp by the means of
    3081              :    analyze_access_subtree.  */
    3082              : static bool
    3083      4099891 : analyze_access_trees (struct access *access)
    3084              : {
    3085      4099891 :   bool ret = false;
    3086              : 
    3087      8667907 :   while (access)
    3088              :     {
    3089      4568016 :       if (analyze_access_subtree (access, NULL, true,
    3090      4568016 :                                   access->grp_total_scalarization))
    3091      2269645 :         ret = true;
    3092      4568016 :       access = access->next_grp;
    3093              :     }
    3094              : 
    3095      4099891 :   return ret;
    3096              : }
    3097              : 
    3098              : /* Return true iff a potential new child of ACC at offset OFFSET and with size
    3099              :    SIZE would conflict with an already existing one.  If exactly such a child
    3100              :    already exists in ACC, store a pointer to it in EXACT_MATCH.  */
    3101              : 
    3102              : static bool
    3103      6904141 : child_would_conflict_in_acc (struct access *acc, HOST_WIDE_INT norm_offset,
    3104              :                               HOST_WIDE_INT size, struct access **exact_match)
    3105              : {
    3106      6904141 :   struct access *child;
    3107              : 
    3108     12195899 :   for (child = acc->first_child; child; child = child->next_sibling)
    3109              :     {
    3110     10723230 :       if (child->offset == norm_offset && child->size == size)
    3111              :         {
    3112      5392105 :           *exact_match = child;
    3113      5392105 :           return true;
    3114              :         }
    3115              : 
    3116      5331125 :       if (child->offset < norm_offset + size
    3117      5253782 :           && child->offset + child->size > norm_offset)
    3118              :         return true;
    3119              :     }
    3120              : 
    3121              :   return false;
    3122              : }
    3123              : 
    3124              : /* Create a new child access of PARENT, with all properties just like MODEL
    3125              :    except for its offset and with its grp_write false and grp_read true.
    3126              :    Return the new access or NULL if it cannot be created.  Note that this
    3127              :    access is created long after all splicing and sorting, it's not located in
    3128              :    any access vector and is automatically a representative of its group.  Set
    3129              :    the gpr_write flag of the new access if SET_GRP_WRITE is true.  */
    3130              : 
    3131              : static struct access *
    3132      1467923 : create_artificial_child_access (struct access *parent, struct access *model,
    3133              :                                 HOST_WIDE_INT new_offset,
    3134              :                                 bool set_grp_read, bool set_grp_write)
    3135              : {
    3136      1467923 :   struct access **child;
    3137      1467923 :   tree expr = parent->base;
    3138              : 
    3139      1467923 :   gcc_assert (!model->grp_unscalarizable_region);
    3140              : 
    3141      1467923 :   struct access *access = access_pool.allocate ();
    3142      1467923 :   memset (access, 0, sizeof (struct access));
    3143      1467923 :   if (!build_user_friendly_ref_for_offset (&expr, TREE_TYPE (expr), new_offset,
    3144              :                                            parent->size, model->type,
    3145              :                                            model->size))
    3146              :     {
    3147        33374 :       access->grp_no_warning = true;
    3148        33374 :       expr = build_ref_for_model (EXPR_LOCATION (parent->base), parent->base,
    3149              :                                   new_offset, model, NULL, false);
    3150              :     }
    3151              : 
    3152      1467923 :   access->base = parent->base;
    3153      1467923 :   access->expr = expr;
    3154      1467923 :   access->offset = new_offset;
    3155      1467923 :   access->size = model->size;
    3156      1467923 :   access->type = model->type;
    3157      1467923 :   access->parent = parent;
    3158      1467923 :   access->grp_read = set_grp_read;
    3159      1467923 :   access->grp_write = set_grp_write;
    3160      1467923 :   access->reverse = model->reverse;
    3161              : 
    3162      1467923 :   child = &parent->first_child;
    3163      2703883 :   while (*child && (*child)->offset < new_offset)
    3164      1235960 :     child = &(*child)->next_sibling;
    3165              : 
    3166      1467923 :   access->next_sibling = *child;
    3167      1467923 :   *child = access;
    3168              : 
    3169      1467923 :   return access;
    3170              : }
    3171              : 
    3172              : 
    3173              : /* Beginning with ACCESS, traverse its whole access subtree and mark all
    3174              :    sub-trees as written to.  If any of them has not been marked so previously
    3175              :    and has assignment links leading from it, re-enqueue it.  */
    3176              : 
    3177              : static void
    3178      1623797 : subtree_mark_written_and_rhs_enqueue (struct access *access)
    3179              : {
    3180      1623797 :   if (access->grp_write)
    3181              :     return;
    3182      1549779 :   access->grp_write = true;
    3183      1549779 :   add_access_to_rhs_work_queue (access);
    3184              : 
    3185      1549779 :   struct access *child;
    3186      2171803 :   for (child = access->first_child; child; child = child->next_sibling)
    3187       622024 :     subtree_mark_written_and_rhs_enqueue (child);
    3188              : }
    3189              : 
    3190              : /* If there is still budget to create a propagation access for DECL, return
    3191              :    true and decrement the budget.  Otherwise return false.  */
    3192              : 
    3193              : static bool
    3194      1471533 : budget_for_propagation_access (tree decl)
    3195              : {
    3196      1471533 :   unsigned b, *p = propagation_budget->get (decl);
    3197      1471533 :   if (p)
    3198       889964 :     b = *p;
    3199              :   else
    3200       581569 :     b = param_sra_max_propagations;
    3201              : 
    3202      1471533 :   if (b == 0)
    3203              :     return false;
    3204      1467929 :   b--;
    3205              : 
    3206      1467929 :   if (b == 0 && dump_file && (dump_flags & TDF_DETAILS))
    3207              :     {
    3208            0 :       fprintf (dump_file, "The propagation budget of ");
    3209            0 :       print_generic_expr (dump_file, decl);
    3210            0 :       fprintf (dump_file, " (UID: %u) has been exhausted.\n", DECL_UID (decl));
    3211              :     }
    3212      1467929 :   propagation_budget->put (decl, b);
    3213      1467929 :   return true;
    3214              : }
    3215              : 
    3216              : /* Return true if ACC or any of its subaccesses has grp_child set.  */
    3217              : 
    3218              : static bool
    3219         2128 : access_or_its_child_written (struct access *acc)
    3220              : {
    3221         2128 :   if (acc->grp_write)
    3222              :     return true;
    3223         2346 :   for (struct access *sub = acc->first_child; sub; sub = sub->next_sibling)
    3224          602 :     if (access_or_its_child_written (sub))
    3225              :       return true;
    3226              :   return false;
    3227              : }
    3228              : 
    3229              : /* Propagate subaccesses and grp_write flags of RACC across an assignment link
    3230              :    to LACC.  Enqueue sub-accesses as necessary so that the write flag is
    3231              :    propagated transitively.  Return true if anything changed.  Additionally, if
    3232              :    RACC is a scalar access but LACC is not, change the type of the latter, if
    3233              :    possible.  */
    3234              : 
    3235              : static bool
    3236      3671578 : propagate_subaccesses_from_rhs (struct access *lacc, struct access *racc)
    3237              : {
    3238      3671578 :   struct access *rchild;
    3239      3671578 :   HOST_WIDE_INT norm_delta = lacc->offset - racc->offset;
    3240      3671578 :   bool ret = false;
    3241              : 
    3242              :   /* IF the LHS is still not marked as being written to, we only need to do so
    3243              :      if the RHS at this level actually was.  */
    3244      3671578 :   if (!lacc->grp_write)
    3245              :     {
    3246      1634332 :       gcc_checking_assert (!comes_initialized_p (racc->base));
    3247      1634332 :       if (racc->grp_write)
    3248              :         {
    3249       798919 :           subtree_mark_written_and_rhs_enqueue (lacc);
    3250       798919 :           ret = true;
    3251              :         }
    3252              :     }
    3253              : 
    3254      3671578 :   if (is_gimple_reg_type (lacc->type)
    3255      2836756 :       || lacc->grp_unscalarizable_region
    3256      6507716 :       || racc->grp_unscalarizable_region)
    3257              :     {
    3258       836626 :       if (!lacc->grp_write)
    3259              :         {
    3260        17038 :           ret = true;
    3261        17038 :           subtree_mark_written_and_rhs_enqueue (lacc);
    3262              :         }
    3263              :       return ret;
    3264              :     }
    3265              : 
    3266      2834952 :   if (is_gimple_reg_type (racc->type))
    3267              :     {
    3268       156514 :       if (!lacc->grp_write)
    3269              :         {
    3270         2818 :           ret = true;
    3271         2818 :           subtree_mark_written_and_rhs_enqueue (lacc);
    3272              :         }
    3273       156514 :       if (!lacc->first_child
    3274       156202 :           && !racc->first_child
    3275       312336 :           && !types_risk_mangled_binary_repr_p (racc->type, lacc->type))
    3276              :         {
    3277              :           /* We are about to change the access type from aggregate to scalar,
    3278              :              so we need to put the reverse flag onto the access, if any.  */
    3279       155822 :           const bool reverse
    3280       155822 :             = TYPE_REVERSE_STORAGE_ORDER (lacc->type)
    3281            1 :               && !POINTER_TYPE_P (racc->type)
    3282       155822 :               && !VECTOR_TYPE_P (racc->type);
    3283       155822 :           tree t = lacc->base;
    3284              : 
    3285       155822 :           lacc->type = racc->type;
    3286              :           /* We know racc and lacc are of different types so can pass -1 as
    3287              :              cur_size.  */
    3288       155822 :           if (build_user_friendly_ref_for_offset (&t, TREE_TYPE (t),
    3289              :                                                   lacc->offset, -1,
    3290              :                                                   racc->type, racc->size))
    3291              :             {
    3292       155016 :               lacc->expr = t;
    3293       155016 :               lacc->grp_same_access_path = true;
    3294              :             }
    3295              :           else
    3296              :             {
    3297          806 :               lacc->expr = build_ref_for_model (EXPR_LOCATION (lacc->base),
    3298              :                                                 lacc->base, lacc->offset,
    3299              :                                                 racc, NULL, false);
    3300          806 :               if (TREE_CODE (lacc->expr) == MEM_REF)
    3301          806 :                 REF_REVERSE_STORAGE_ORDER (lacc->expr) = reverse;
    3302          806 :               lacc->grp_no_warning = true;
    3303          806 :               lacc->grp_same_access_path = false;
    3304              :             }
    3305       155822 :           lacc->reverse = reverse;
    3306              :         }
    3307              :       return ret;
    3308              :     }
    3309              : 
    3310      5773919 :   for (rchild = racc->first_child; rchild; rchild = rchild->next_sibling)
    3311              :     {
    3312      3095481 :       struct access *new_acc = NULL;
    3313      3095481 :       HOST_WIDE_INT norm_offset = rchild->offset + norm_delta;
    3314              : 
    3315      3095481 :       if (child_would_conflict_in_acc (lacc, norm_offset, rchild->size,
    3316              :                                         &new_acc))
    3317              :         {
    3318      2499057 :           if (new_acc)
    3319              :             {
    3320      2473836 :               if (!new_acc->grp_write && rchild->grp_write)
    3321              :                 {
    3322       176988 :                   gcc_assert (!lacc->grp_write);
    3323       176988 :                   subtree_mark_written_and_rhs_enqueue (new_acc);
    3324       176988 :                   ret = true;
    3325              :                 }
    3326              : 
    3327      2473836 :               rchild->grp_hint = 1;
    3328      2473836 :               new_acc->grp_hint |= new_acc->grp_read;
    3329      2473836 :               if (rchild->first_child
    3330      2473836 :                   && propagate_subaccesses_from_rhs (new_acc, rchild))
    3331              :                 {
    3332         1465 :                   ret = 1;
    3333         1465 :                   add_access_to_rhs_work_queue (new_acc);
    3334              :                 }
    3335              :             }
    3336              :           else
    3337              :             {
    3338        25221 :               if (!lacc->grp_write)
    3339              :                 {
    3340         5266 :                   ret = true;
    3341         5266 :                   subtree_mark_written_and_rhs_enqueue (lacc);
    3342              :                 }
    3343              :             }
    3344      2503771 :           continue;
    3345              :         }
    3346              : 
    3347       601138 :       if (rchild->grp_unscalarizable_region
    3348       596424 :           || !budget_for_propagation_access (lacc->base))
    3349              :         {
    3350         4714 :           if (!lacc->grp_write && access_or_its_child_written (rchild))
    3351              :             {
    3352          350 :               ret = true;
    3353          350 :               subtree_mark_written_and_rhs_enqueue (lacc);
    3354              :             }
    3355         4714 :           continue;
    3356              :         }
    3357              : 
    3358       591710 :       rchild->grp_hint = 1;
    3359              :       /* Because get_ref_base_and_extent always includes padding in size for
    3360              :          accesses to DECLs but not necessarily for COMPONENT_REFs of the same
    3361              :          type, we might be actually attempting to here to create a child of the
    3362              :          same type as the parent.  */
    3363       591710 :       if (!types_compatible_p (lacc->type, rchild->type))
    3364       591710 :         new_acc = create_artificial_child_access (lacc, rchild, norm_offset,
    3365              :                                                   false,
    3366       591710 :                                                   (lacc->grp_write
    3367       289683 :                                                    || rchild->grp_write));
    3368              :       else
    3369            0 :         new_acc = lacc;
    3370       591710 :       gcc_checking_assert (new_acc);
    3371       591710 :       if (racc->first_child)
    3372       591710 :         propagate_subaccesses_from_rhs (new_acc, rchild);
    3373              : 
    3374       591710 :       add_access_to_rhs_work_queue (lacc);
    3375       591710 :       ret = true;
    3376              :     }
    3377              : 
    3378              :   return ret;
    3379              : }
    3380              : 
    3381              : /* Propagate subaccesses of LACC across an assignment link to RACC if they
    3382              :    should inhibit total scalarization of the corresponding area.  No flags are
    3383              :    being propagated in the process.  Return true if anything changed.  */
    3384              : 
    3385              : static bool
    3386      6109870 : propagate_subaccesses_from_lhs (struct access *lacc, struct access *racc)
    3387              : {
    3388      6109870 :   if (is_gimple_reg_type (racc->type)
    3389      2146745 :       || lacc->grp_unscalarizable_region
    3390      8255517 :       || racc->grp_unscalarizable_region)
    3391              :     return false;
    3392              : 
    3393              :   /* TODO: Do we want set some new racc flag to stop potential total
    3394              :      scalarization if lacc is a scalar access (and none fo the two have
    3395              :      children)?  */
    3396              : 
    3397      2145616 :   bool ret = false;
    3398      2145616 :   HOST_WIDE_INT norm_delta = racc->offset - lacc->offset;
    3399      2145616 :   for (struct access *lchild = lacc->first_child;
    3400      5958792 :        lchild;
    3401      3813176 :        lchild = lchild->next_sibling)
    3402              :     {
    3403      3813176 :       struct access *matching_acc = NULL;
    3404      3813176 :       HOST_WIDE_INT norm_offset = lchild->offset + norm_delta;
    3405              : 
    3406      6750133 :       if (lchild->grp_unscalarizable_region
    3407      3808660 :           || child_would_conflict_in_acc (racc, norm_offset, lchild->size,
    3408              :                                           &matching_acc)
    3409      4689421 :           || !budget_for_propagation_access (racc->base))
    3410              :         {
    3411      2936957 :           if (matching_acc
    3412      2936957 :               && propagate_subaccesses_from_lhs (lchild, matching_acc))
    3413          198 :             add_access_to_lhs_work_queue (matching_acc);
    3414      2936957 :           continue;
    3415              :         }
    3416              : 
    3417              :       /* Because get_ref_base_and_extent always includes padding in size for
    3418              :          accesses to DECLs but not necessarily for COMPONENT_REFs of the same
    3419              :          type, we might be actually attempting to here to create a child of the
    3420              :          same type as the parent.  */
    3421       876219 :       if (!types_compatible_p (racc->type, lchild->type))
    3422              :         {
    3423       876213 :           struct access *new_acc
    3424       876213 :             = create_artificial_child_access (racc, lchild, norm_offset,
    3425              :                                               true, false);
    3426       876213 :           new_acc->grp_result_of_prop_from_lhs = 1;
    3427       876213 :           propagate_subaccesses_from_lhs (lchild, new_acc);
    3428              :         }
    3429              :       else
    3430            6 :         propagate_subaccesses_from_lhs (lchild, racc);
    3431       876219 :       ret = true;
    3432              :     }
    3433              :   return ret;
    3434              : }
    3435              : 
    3436              : /* Propagate all subaccesses across assignment links.  */
    3437              : 
    3438              : static void
    3439       771505 : propagate_all_subaccesses (void)
    3440              : {
    3441       771505 :   propagation_budget = new hash_map<tree, unsigned>;
    3442      2347964 :   while (rhs_work_queue_head)
    3443              :     {
    3444      1576459 :       struct access *racc = pop_access_from_rhs_work_queue ();
    3445      1576459 :       struct assign_link *link;
    3446              : 
    3447      1576459 :       if (racc->group_representative)
    3448      1575841 :         racc= racc->group_representative;
    3449      1576459 :       gcc_assert (racc->first_rhs_link);
    3450              : 
    3451      4642942 :       for (link = racc->first_rhs_link; link; link = link->next_rhs)
    3452              :         {
    3453      3066483 :           struct access *lacc = link->lacc;
    3454              : 
    3455      3066483 :           if (!bitmap_bit_p (candidate_bitmap, DECL_UID (lacc->base)))
    3456         5810 :             continue;
    3457      3060673 :           lacc = lacc->group_representative;
    3458              : 
    3459      3060673 :           bool reque_parents = false;
    3460      3060673 :           if (!bitmap_bit_p (candidate_bitmap, DECL_UID (racc->base)))
    3461              :             {
    3462         1607 :               if (!lacc->grp_write)
    3463              :                 {
    3464          394 :                   subtree_mark_written_and_rhs_enqueue (lacc);
    3465          394 :                   reque_parents = true;
    3466              :                 }
    3467              :             }
    3468      3059066 :           else if (propagate_subaccesses_from_rhs (lacc, racc))
    3469              :             reque_parents = true;
    3470              : 
    3471      1130003 :           if (reque_parents)
    3472      1278762 :             do
    3473              :               {
    3474      1278762 :                 add_access_to_rhs_work_queue (lacc);
    3475      1278762 :                 lacc = lacc->parent;
    3476              :               }
    3477      1278762 :             while (lacc);
    3478              :         }
    3479              :     }
    3480              : 
    3481      2140256 :   while (lhs_work_queue_head)
    3482              :     {
    3483      1368751 :       struct access *lacc = pop_access_from_lhs_work_queue ();
    3484      1368751 :       struct assign_link *link;
    3485              : 
    3486      1368751 :       if (lacc->group_representative)
    3487      1365305 :         lacc = lacc->group_representative;
    3488      1368751 :       gcc_assert (lacc->first_lhs_link);
    3489              : 
    3490      1368751 :       if (!bitmap_bit_p (candidate_bitmap, DECL_UID (lacc->base)))
    3491         3791 :         continue;
    3492              : 
    3493      3681104 :       for (link = lacc->first_lhs_link; link; link = link->next_lhs)
    3494              :         {
    3495      2316144 :           struct access *racc = link->racc;
    3496              : 
    3497      2316144 :           if (racc->group_representative)
    3498      2315637 :             racc = racc->group_representative;
    3499      2316144 :           if (!bitmap_bit_p (candidate_bitmap, DECL_UID (racc->base)))
    3500          762 :             continue;
    3501      2315382 :           if (propagate_subaccesses_from_lhs (lacc, racc))
    3502       346534 :             add_access_to_lhs_work_queue (racc);
    3503              :         }
    3504              :     }
    3505      1543010 :   delete propagation_budget;
    3506       771505 : }
    3507              : 
    3508              : /* Return true if the forest beginning with ROOT does not contain
    3509              :    unscalarizable regions or non-byte aligned accesses.  */
    3510              : 
    3511              : static bool
    3512       786893 : can_totally_scalarize_forest_p (struct access *root)
    3513              : {
    3514       786893 :   struct access *access = root;
    3515      2248776 :   do
    3516              :     {
    3517      2248776 :       if (access->grp_unscalarizable_region
    3518      2246682 :           || (access->offset % BITS_PER_UNIT) != 0
    3519      2246297 :           || (access->size % BITS_PER_UNIT) != 0
    3520      4491264 :           || (is_gimple_reg_type (access->type)
    3521      1482899 :               && access->first_child))
    3522              :         return false;
    3523              : 
    3524      2242103 :       if (access->first_child)
    3525              :         access = access->first_child;
    3526      1604849 :       else if (access->next_sibling)
    3527              :         access = access->next_sibling;
    3528              :       else
    3529              :         {
    3530      1490735 :           while (access->parent && !access->next_sibling)
    3531              :             access = access->parent;
    3532       858501 :           if (access->next_sibling)
    3533              :             access = access->next_sibling;
    3534              :           else
    3535              :             {
    3536       804573 :               gcc_assert (access == root);
    3537       804573 :               root = root->next_grp;
    3538       804573 :               access = root;
    3539              :             }
    3540              :         }
    3541              :     }
    3542      2242103 :   while (access);
    3543              :   return true;
    3544              : }
    3545              : 
    3546              : /* Create and return an ACCESS in PARENT spanning from POS with SIZE, TYPE and
    3547              :    reference EXPR for total scalarization purposes and mark it as such.  Within
    3548              :    the children of PARENT, link it in between PTR and NEXT_SIBLING.  */
    3549              : 
    3550              : static struct access *
    3551       512582 : create_total_scalarization_access (struct access *parent, HOST_WIDE_INT pos,
    3552              :                                    HOST_WIDE_INT size, tree type, tree expr,
    3553              :                                    struct access **ptr,
    3554              :                                    struct access *next_sibling)
    3555              : {
    3556       512582 :   struct access *access = access_pool.allocate ();
    3557       512582 :   memset (access, 0, sizeof (struct access));
    3558       512582 :   access->base = parent->base;
    3559       512582 :   access->offset = pos;
    3560       512582 :   access->size = size;
    3561       512582 :   access->expr = expr;
    3562       512582 :   access->type = type;
    3563       512582 :   access->parent = parent;
    3564       512582 :   access->grp_write = parent->grp_write;
    3565       512582 :   access->grp_total_scalarization = 1;
    3566       512582 :   access->grp_hint = 1;
    3567       512582 :   access->grp_same_access_path = 0;
    3568       512582 :   access->reverse = reverse_storage_order_for_component_p (expr);
    3569              : 
    3570       512582 :   access->next_sibling = next_sibling;
    3571       512582 :   *ptr = access;
    3572       512582 :   return access;
    3573              : }
    3574              : 
    3575              : /* Create and return an ACCESS in PARENT spanning from POS with SIZE, TYPE and
    3576              :    reference EXPR for total scalarization purposes and mark it as such, link it
    3577              :    at *PTR and reshape the tree so that those elements at *PTR and their
    3578              :    siblings which fall within the part described by POS and SIZE are moved to
    3579              :    be children of the new access.  If a partial overlap is detected, return
    3580              :    NULL.  */
    3581              : 
    3582              : static struct access *
    3583       512582 : create_total_access_and_reshape (struct access *parent, HOST_WIDE_INT pos,
    3584              :                                  HOST_WIDE_INT size, tree type, tree expr,
    3585              :                                  struct access **ptr)
    3586              : {
    3587       512582 :   struct access **p = ptr;
    3588              : 
    3589       724910 :   while (*p && (*p)->offset < pos + size)
    3590              :     {
    3591       212328 :       if ((*p)->offset + (*p)->size > pos + size)
    3592              :         return NULL;
    3593       212328 :       p = &(*p)->next_sibling;
    3594              :     }
    3595              : 
    3596       512582 :   struct access *next_child = *ptr;
    3597       512582 :   struct access *new_acc
    3598       512582 :     = create_total_scalarization_access (parent, pos, size, type, expr,
    3599              :                                          ptr, *p);
    3600       512582 :   if (p != ptr)
    3601              :     {
    3602        84940 :       new_acc->first_child = next_child;
    3603        84940 :       *p = NULL;
    3604       297268 :       for (struct access *a = next_child; a; a = a->next_sibling)
    3605       212328 :         a->parent = new_acc;
    3606              :     }
    3607              :   return new_acc;
    3608              : }
    3609              : 
    3610              : static bool totally_scalarize_subtree (struct access *root);
    3611              : 
    3612              : /* Return true if INNER is either the same type as OUTER or if it is the type
    3613              :    of a record field in OUTER at offset zero, possibly in nested
    3614              :    sub-records.  */
    3615              : 
    3616              : static bool
    3617       182483 : access_and_field_type_match_p (tree outer, tree inner)
    3618              : {
    3619       182483 :   if (TYPE_MAIN_VARIANT (outer) == TYPE_MAIN_VARIANT (inner))
    3620              :     return true;
    3621          549 :   if (TREE_CODE (outer) != RECORD_TYPE)
    3622              :     return false;
    3623          537 :   tree fld = TYPE_FIELDS (outer);
    3624        11218 :   while (fld)
    3625              :     {
    3626        11023 :      if (TREE_CODE (fld) == FIELD_DECL)
    3627              :        {
    3628          602 :         if (!zerop (DECL_FIELD_OFFSET (fld)))
    3629              :           return false;
    3630          602 :         if (TYPE_MAIN_VARIANT (TREE_TYPE (fld)) == inner)
    3631              :           return true;
    3632          444 :         if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE)
    3633          260 :           fld = TYPE_FIELDS (TREE_TYPE (fld));
    3634              :         else
    3635              :           return false;
    3636              :        }
    3637              :      else
    3638        10421 :        fld = DECL_CHAIN (fld);
    3639              :     }
    3640              :   return false;
    3641              : }
    3642              : 
    3643              : /* Return type of total_should_skip_creating_access indicating whether a total
    3644              :    scalarization access for a field/element should be created, whether it
    3645              :    already exists or whether the entire total scalarization has to fail.  */
    3646              : 
    3647              : enum total_sra_field_state {TOTAL_FLD_CREATE, TOTAL_FLD_DONE, TOTAL_FLD_FAILED};
    3648              : 
    3649              : /* Do all the necessary steps in total scalarization when the given aggregate
    3650              :    type has a TYPE at POS with the given SIZE should be put into PARENT and
    3651              :    when we have processed all its siblings with smaller offsets up until and
    3652              :    including LAST_SEEN_SIBLING (which can be NULL).
    3653              : 
    3654              :    If some further siblings are to be skipped, set *LAST_SEEN_SIBLING as
    3655              :    appropriate.  Return TOTAL_FLD_CREATE id the caller should carry on with
    3656              :    creating a new access, TOTAL_FLD_DONE if access or accesses capable of
    3657              :    representing the described part of the aggregate for the purposes of total
    3658              :    scalarization already exist or TOTAL_FLD_FAILED if there is a problem which
    3659              :    prevents total scalarization from happening at all.  */
    3660              : 
    3661              : static enum total_sra_field_state
    3662      1879797 : total_should_skip_creating_access (struct access *parent,
    3663              :                                    struct access **last_seen_sibling,
    3664              :                                    tree type, HOST_WIDE_INT pos,
    3665              :                                    HOST_WIDE_INT size)
    3666              : {
    3667      1879797 :   struct access *next_child;
    3668      1879797 :   if (!*last_seen_sibling)
    3669       854348 :     next_child = parent->first_child;
    3670              :   else
    3671      1025449 :     next_child = (*last_seen_sibling)->next_sibling;
    3672              : 
    3673              :   /* First, traverse the chain of siblings until it points to an access with
    3674              :      offset at least equal to POS.  Check all skipped accesses whether they
    3675              :      span the POS boundary and if so, return with a failure.  */
    3676      1879880 :   while (next_child && next_child->offset < pos)
    3677              :     {
    3678           83 :       if (next_child->offset + next_child->size > pos)
    3679              :         return TOTAL_FLD_FAILED;
    3680           83 :       *last_seen_sibling = next_child;
    3681           83 :       next_child = next_child->next_sibling;
    3682              :     }
    3683              : 
    3684              :   /* Now check whether next_child has exactly the right POS and SIZE and if so,
    3685              :      whether it can represent what we need and can be totally scalarized
    3686              :      itself.  */
    3687      1879797 :   if (next_child && next_child->offset == pos
    3688      1448349 :       && next_child->size == size)
    3689              :     {
    3690      1366945 :       if (!is_gimple_reg_type (next_child->type)
    3691      1366945 :           && (!access_and_field_type_match_p (type, next_child->type)
    3692       182092 :               || !totally_scalarize_subtree (next_child)))
    3693              :         return TOTAL_FLD_FAILED;
    3694              : 
    3695      1366508 :       *last_seen_sibling = next_child;
    3696      1366508 :       return TOTAL_FLD_DONE;
    3697              :     }
    3698              : 
    3699              :   /* If the child we're looking at would partially overlap, we just cannot
    3700              :      totally scalarize.  */
    3701              :   if (next_child
    3702       115428 :       && next_child->offset < pos + size
    3703        85210 :       && next_child->offset + next_child->size > pos + size)
    3704              :     return TOTAL_FLD_FAILED;
    3705              : 
    3706       512770 :   if (is_gimple_reg_type (type))
    3707              :     {
    3708              :       /* We don't scalarize accesses that are children of other scalar type
    3709              :          accesses, so if we go on and create an access for a register type,
    3710              :          there should not be any pre-existing children.  There are rare cases
    3711              :          where the requested type is a vector but we already have register
    3712              :          accesses for all its elements which is equally good.  Detect that
    3713              :          situation or whether we need to bail out.  */
    3714              : 
    3715              :       HOST_WIDE_INT covered = pos;
    3716              :       bool skipping = false;
    3717              :       while (next_child
    3718       385345 :              && next_child->offset + next_child->size <= pos + size)
    3719              :         {
    3720          748 :           if (next_child->offset != covered
    3721          748 :               || !is_gimple_reg_type (next_child->type))
    3722              :             return TOTAL_FLD_FAILED;
    3723              : 
    3724          748 :           covered += next_child->size;
    3725          748 :           *last_seen_sibling = next_child;
    3726          748 :           next_child = next_child->next_sibling;
    3727          748 :           skipping = true;
    3728              :         }
    3729              : 
    3730       384597 :       if (skipping)
    3731              :         {
    3732          188 :           if (covered != pos + size)
    3733              :             return TOTAL_FLD_FAILED;
    3734              :           else
    3735          168 :             return TOTAL_FLD_DONE;
    3736              :         }
    3737              :     }
    3738              : 
    3739              :   return TOTAL_FLD_CREATE;
    3740              : }
    3741              : 
    3742              : /* Go over sub-tree rooted in ROOT and attempt to create scalar accesses
    3743              :    spanning all uncovered areas covered by ROOT, return false if the attempt
    3744              :    failed.  All created accesses will have grp_unscalarizable_region set (and
    3745              :    should be ignored if the function returns false).  */
    3746              : 
    3747              : static bool
    3748       854870 : totally_scalarize_subtree (struct access *root)
    3749              : {
    3750       854870 :   gcc_checking_assert (!root->grp_unscalarizable_region);
    3751       854870 :   gcc_checking_assert (!is_gimple_reg_type (root->type));
    3752              : 
    3753       854870 :   struct access *last_seen_sibling = NULL;
    3754              : 
    3755       854870 :   switch (TREE_CODE (root->type))
    3756              :     {
    3757       838445 :     case RECORD_TYPE:
    3758     10866586 :       for (tree fld = TYPE_FIELDS (root->type); fld; fld = DECL_CHAIN (fld))
    3759     10028656 :         if (TREE_CODE (fld) == FIELD_DECL)
    3760              :           {
    3761      1884357 :             tree ft = TREE_TYPE (fld);
    3762      1884357 :             HOST_WIDE_INT fsize = tree_to_uhwi (DECL_SIZE (fld));
    3763      1884357 :             if (!fsize)
    3764        41304 :               continue;
    3765              : 
    3766      1843053 :             HOST_WIDE_INT pos = root->offset + int_bit_position (fld);
    3767      1843053 :             if (pos + fsize > root->offset + root->size)
    3768              :               return false;
    3769      1843053 :             enum total_sra_field_state
    3770      1843053 :               state = total_should_skip_creating_access (root,
    3771              :                                                          &last_seen_sibling,
    3772              :                                                          ft, pos, fsize);
    3773      1843053 :             switch (state)
    3774              :               {
    3775              :               case TOTAL_FLD_FAILED:
    3776              :                 return false;
    3777      1357373 :               case TOTAL_FLD_DONE:
    3778      1357373 :                 continue;
    3779       485241 :               case TOTAL_FLD_CREATE:
    3780       485241 :                 break;
    3781            0 :               default:
    3782            0 :                 gcc_unreachable ();
    3783              :               }
    3784              : 
    3785       485241 :             struct access **p = (last_seen_sibling
    3786       485241 :                                  ? &last_seen_sibling->next_sibling
    3787              :                                  : &root->first_child);
    3788       485241 :             tree nref = build3 (COMPONENT_REF, ft, root->expr, fld, NULL_TREE);
    3789       485241 :             struct access *new_child
    3790       485241 :               = create_total_access_and_reshape (root, pos, fsize, ft, nref, p);
    3791       485241 :             if (!new_child)
    3792              :               return false;
    3793              : 
    3794       485241 :             if (!is_gimple_reg_type (ft)
    3795       485241 :                 && !totally_scalarize_subtree (new_child))
    3796              :               return false;
    3797       485165 :             last_seen_sibling = new_child;
    3798              :           }
    3799              :       break;
    3800        16425 :     case ARRAY_TYPE:
    3801        16425 :       {
    3802        16425 :         tree elemtype = TREE_TYPE (root->type);
    3803        16425 :         HOST_WIDE_INT el_size;
    3804        16425 :         offset_int idx, max;
    3805        16425 :         if (!prepare_iteration_over_array_elts (root->type, &el_size,
    3806              :                                                 &idx, &max))
    3807              :           break;
    3808              : 
    3809        16425 :         for (HOST_WIDE_INT pos = root->offset;
    3810        53069 :              idx <= max;
    3811        36644 :              pos += el_size, ++idx)
    3812              :           {
    3813        36744 :             enum total_sra_field_state
    3814        36744 :               state = total_should_skip_creating_access (root,
    3815              :                                                          &last_seen_sibling,
    3816              :                                                          elemtype, pos,
    3817              :                                                          el_size);
    3818        36744 :             switch (state)
    3819              :               {
    3820              :               case TOTAL_FLD_FAILED:
    3821          100 :                 return false;
    3822         9303 :               case TOTAL_FLD_DONE:
    3823         9303 :                 continue;
    3824        27341 :               case TOTAL_FLD_CREATE:
    3825        27341 :                 break;
    3826            0 :               default:
    3827            0 :                 gcc_unreachable ();
    3828              :               }
    3829              : 
    3830        27341 :             struct access **p = (last_seen_sibling
    3831        27341 :                                  ? &last_seen_sibling->next_sibling
    3832              :                                  : &root->first_child);
    3833        54682 :             tree nref = build4 (ARRAY_REF, elemtype, root->expr,
    3834        27341 :                                 wide_int_to_tree (TYPE_DOMAIN (root->type),
    3835        27341 :                                                   idx),
    3836              :                                 NULL_TREE, NULL_TREE);
    3837        27341 :             struct access *new_child
    3838        27341 :               = create_total_access_and_reshape (root, pos, el_size, elemtype,
    3839              :                                                  nref, p);
    3840        27341 :             if (!new_child)
    3841              :               return false;
    3842              : 
    3843        27341 :             if (!is_gimple_reg_type (elemtype)
    3844        27341 :                 && !totally_scalarize_subtree (new_child))
    3845              :               return false;
    3846        27341 :             last_seen_sibling = new_child;
    3847              :           }
    3848              :       }
    3849        16325 :       break;
    3850            0 :     default:
    3851            0 :       gcc_unreachable ();
    3852              :     }
    3853              :   return true;
    3854              : }
    3855              : 
    3856              : /* Get the total total scalarization size limit in the current function.  */
    3857              : 
    3858              : unsigned HOST_WIDE_INT
    3859       776267 : sra_get_max_scalarization_size (void)
    3860              : {
    3861       776267 :   bool optimize_speed_p = !optimize_function_for_size_p (cfun);
    3862              :   /* If the user didn't set PARAM_SRA_MAX_SCALARIZATION_SIZE_<...>,
    3863              :      fall back to a target default.  */
    3864       776267 :   unsigned HOST_WIDE_INT max_scalarization_size
    3865       776267 :     = get_move_ratio (optimize_speed_p) * MOVE_MAX;
    3866              : 
    3867       776267 :   if (optimize_speed_p)
    3868              :     {
    3869       754809 :       if (OPTION_SET_P (param_sra_max_scalarization_size_speed))
    3870           16 :         max_scalarization_size = param_sra_max_scalarization_size_speed;
    3871              :     }
    3872              :   else
    3873              :     {
    3874        21458 :       if (OPTION_SET_P (param_sra_max_scalarization_size_size))
    3875            0 :         max_scalarization_size = param_sra_max_scalarization_size_size;
    3876              :     }
    3877       776267 :   max_scalarization_size *= BITS_PER_UNIT;
    3878       776267 :   return max_scalarization_size;
    3879              : }
    3880              : 
    3881              : /* Go through all accesses collected throughout the (intraprocedural) analysis
    3882              :    stage, exclude overlapping ones, identify representatives and build trees
    3883              :    out of them, making decisions about scalarization on the way.  Return true
    3884              :    iff there are any to-be-scalarized variables after this stage. */
    3885              : 
    3886              : static bool
    3887       771505 : analyze_all_variable_accesses (void)
    3888              : {
    3889       771505 :   int res = 0;
    3890       771505 :   bitmap tmp = BITMAP_ALLOC (NULL);
    3891       771505 :   bitmap_iterator bi;
    3892       771505 :   unsigned i;
    3893              : 
    3894       771505 :   bitmap_copy (tmp, candidate_bitmap);
    3895      5056836 :   EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
    3896              :     {
    3897      4285331 :       tree var = candidate (i);
    3898      4285331 :       struct access *access;
    3899              : 
    3900      4285331 :       access = sort_and_splice_var_accesses (var);
    3901      4285331 :       if (!access || !build_access_trees (access))
    3902       185440 :         disqualify_candidate (var,
    3903              :                               "No or inhibitingly overlapping accesses.");
    3904              :     }
    3905              : 
    3906       771505 :   propagate_all_subaccesses ();
    3907              : 
    3908       771505 :   unsigned HOST_WIDE_INT max_scalarization_size
    3909       771505 :     = sra_get_max_scalarization_size ();
    3910      4871396 :   EXECUTE_IF_SET_IN_BITMAP (candidate_bitmap, 0, i, bi)
    3911      4099891 :     if (bitmap_bit_p (should_scalarize_away_bitmap, i)
    3912      4099891 :         && !bitmap_bit_p (cannot_scalarize_away_bitmap, i))
    3913              :       {
    3914       865542 :         tree var = candidate (i);
    3915       865542 :         if (!VAR_P (var))
    3916        77645 :           continue;
    3917              : 
    3918       787897 :         if (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (var))) > max_scalarization_size)
    3919              :           {
    3920         7693 :             if (dump_file && (dump_flags & TDF_DETAILS))
    3921              :               {
    3922            0 :                 fprintf (dump_file, "Too big to totally scalarize: ");
    3923            0 :                 print_generic_expr (dump_file, var);
    3924            0 :                 fprintf (dump_file, " (UID: %u)\n", DECL_UID (var));
    3925              :               }
    3926         7693 :             continue;
    3927              :           }
    3928              : 
    3929       780204 :         bool all_types_ok = true;
    3930       780204 :         for (struct access *access = get_first_repr_for_decl (var);
    3931      1544019 :              access;
    3932       763815 :              access = access->next_grp)
    3933       786893 :           if (!can_totally_scalarize_forest_p (access)
    3934      1567113 :               || !totally_scalarizable_type_p (access->type,
    3935              :                                                constant_decl_p (var),
    3936              :                                                0, nullptr))
    3937              :             {
    3938              :               all_types_ok = false;
    3939              :               break;
    3940              :             }
    3941       780204 :         if (!all_types_ok)
    3942        23078 :           continue;
    3943              : 
    3944       757126 :         if (dump_file && (dump_flags & TDF_DETAILS))
    3945              :           {
    3946           17 :             fprintf (dump_file, "Will attempt to totally scalarize ");
    3947           17 :             print_generic_expr (dump_file, var);
    3948           17 :             fprintf (dump_file, " (UID: %u): \n", DECL_UID (var));
    3949              :           }
    3950       757126 :         bool scalarized = true;
    3951       757126 :         for (struct access *access = get_first_repr_for_decl (var);
    3952      1520446 :              access;
    3953       763320 :              access = access->next_grp)
    3954       763813 :           if (!is_gimple_reg_type (access->type)
    3955       763813 :               && !totally_scalarize_subtree (access))
    3956              :             {
    3957              :               scalarized = false;
    3958              :               break;
    3959              :             }
    3960              : 
    3961       757126 :         if (scalarized)
    3962       756633 :           for (struct access *access = get_first_repr_for_decl (var);
    3963      1519953 :                access;
    3964       763320 :                access = access->next_grp)
    3965       763320 :             access->grp_total_scalarization = true;
    3966              :       }
    3967              : 
    3968       771505 :   if (flag_checking)
    3969       771505 :     verify_all_sra_access_forests ();
    3970              : 
    3971       771505 :   bitmap_copy (tmp, candidate_bitmap);
    3972      4871396 :   EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
    3973              :     {
    3974      4099891 :       tree var = candidate (i);
    3975      4099891 :       struct access *access = get_first_repr_for_decl (var);
    3976              : 
    3977      4099891 :       if (analyze_access_trees (access))
    3978              :         {
    3979      1884563 :           res++;
    3980      1884563 :           if (dump_file && (dump_flags & TDF_DETAILS))
    3981              :             {
    3982           24 :               fprintf (dump_file, "\nAccess trees for ");
    3983           24 :               print_generic_expr (dump_file, var);
    3984           24 :               fprintf (dump_file, " (UID: %u): \n", DECL_UID (var));
    3985           24 :               dump_access_tree (dump_file, access);
    3986           24 :               fprintf (dump_file, "\n");
    3987              :             }
    3988              :         }
    3989              :       else
    3990      2215328 :         disqualify_candidate (var, "No scalar replacements to be created.");
    3991              :     }
    3992              : 
    3993       771505 :   BITMAP_FREE (tmp);
    3994              : 
    3995       771505 :   if (res)
    3996              :     {
    3997       461621 :       statistics_counter_event (cfun, "Scalarized aggregates", res);
    3998       461621 :       return true;
    3999              :     }
    4000              :   else
    4001              :     return false;
    4002              : }
    4003              : 
    4004              : /* Generate statements copying scalar replacements of accesses within a subtree
    4005              :    into or out of AGG.  ACCESS, all its children, siblings and their children
    4006              :    are to be processed.  AGG is an aggregate type expression (can be a
    4007              :    declaration but does not have to be, it can for example also be a mem_ref or
    4008              :    a series of handled components).  TOP_OFFSET is the offset of the processed
    4009              :    subtree which has to be subtracted from offsets of individual accesses to
    4010              :    get corresponding offsets for AGG.  If CHUNK_SIZE is non-null, copy only
    4011              :    replacements in the interval <start_offset, start_offset + chunk_size>,
    4012              :    otherwise copy all.  GSI is a statement iterator used to place the new
    4013              :    statements.  WRITE should be true when the statements should write from AGG
    4014              :    to the replacement and false if vice versa.  If INSERT_AFTER is true, new
    4015              :    statements will be added after the current statement in GSI, they will be
    4016              :    added before the statement otherwise.  If FORCE_REF_ALL is true then
    4017              :    memory accesses will use alias-set zero.  */
    4018              : 
    4019              : static void
    4020      1989600 : generate_subtree_copies (struct access *access, tree agg,
    4021              :                          HOST_WIDE_INT top_offset,
    4022              :                          HOST_WIDE_INT start_offset, HOST_WIDE_INT chunk_size,
    4023              :                          gimple_stmt_iterator *gsi, bool write,
    4024              :                          bool insert_after, location_t loc,
    4025              :                          bool force_ref_all = false)
    4026              : {
    4027              :   /* Never write anything into constant pool decls.  See PR70602.  */
    4028      3076763 :   if (!write && constant_decl_p (agg))
    4029              :     return;
    4030      4821817 :   do
    4031              :     {
    4032      4821817 :       if (chunk_size && access->offset >= start_offset + chunk_size)
    4033              :         return;
    4034              : 
    4035      4821817 :       if (access->grp_to_be_replaced
    4036      3777740 :           && (chunk_size == 0
    4037            0 :               || access->offset + access->size > start_offset))
    4038              :         {
    4039      3777740 :           tree expr, repl = get_access_replacement (access);
    4040      3777740 :           gassign *stmt;
    4041              : 
    4042      3777740 :           expr = build_ref_for_model (loc, agg, access->offset - top_offset,
    4043              :                                       access, gsi, insert_after, force_ref_all);
    4044              : 
    4045      3777740 :           if (write)
    4046              :             {
    4047      1677271 :               if (access->grp_partial_lhs)
    4048            6 :                 expr = force_gimple_operand_gsi (gsi, expr, true, NULL_TREE,
    4049              :                                                  !insert_after,
    4050              :                                                  insert_after ? GSI_NEW_STMT
    4051              :                                                  : GSI_SAME_STMT);
    4052      1677271 :               stmt = gimple_build_assign (repl, expr);
    4053              :             }
    4054              :           else
    4055              :             {
    4056      2100469 :               suppress_warning (repl /* Be more selective! */);
    4057      2100469 :               if (access->grp_partial_lhs)
    4058          156 :                 repl = force_gimple_operand_gsi (gsi, repl, true, NULL_TREE,
    4059              :                                                  !insert_after,
    4060              :                                                  insert_after ? GSI_NEW_STMT
    4061              :                                                  : GSI_SAME_STMT);
    4062      2100469 :               stmt = gimple_build_assign (expr, repl);
    4063              :             }
    4064      3777740 :           gimple_set_location (stmt, loc);
    4065              : 
    4066      3777740 :           if (insert_after)
    4067      1677271 :             gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
    4068              :           else
    4069      2100469 :             gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
    4070      3777740 :           update_stmt (stmt);
    4071      3777740 :           sra_stats.subtree_copies++;
    4072      3777740 :         }
    4073      1044077 :       else if (write
    4074       403130 :                && access->grp_to_be_debug_replaced
    4075         5793 :                && (chunk_size == 0
    4076            0 :                    || access->offset + access->size > start_offset))
    4077              :         {
    4078         5793 :           gdebug *ds;
    4079        11586 :           tree drhs = build_debug_ref_for_model (loc, agg,
    4080         5793 :                                                  access->offset - top_offset,
    4081              :                                                  access);
    4082         5793 :           ds = gimple_build_debug_bind (get_access_replacement (access),
    4083              :                                         drhs, gsi_stmt (*gsi));
    4084         5793 :           if (insert_after)
    4085         5793 :             gsi_insert_after (gsi, ds, GSI_NEW_STMT);
    4086              :           else
    4087            0 :             gsi_insert_before (gsi, ds, GSI_SAME_STMT);
    4088              :         }
    4089              : 
    4090      4821817 :       if (access->first_child)
    4091       487730 :         generate_subtree_copies (access->first_child, agg, top_offset,
    4092              :                                  start_offset, chunk_size, gsi,
    4093              :                                  write, insert_after, loc, force_ref_all);
    4094              : 
    4095      4821817 :       access = access->next_sibling;
    4096              :     }
    4097      4821817 :   while (access);
    4098              : }
    4099              : 
    4100              : /* Assign zero to all scalar replacements in an access subtree.  ACCESS is the
    4101              :    root of the subtree to be processed.  GSI is the statement iterator used
    4102              :    for inserting statements which are added after the current statement if
    4103              :    INSERT_AFTER is true or before it otherwise.  */
    4104              : 
    4105              : static void
    4106       579056 : init_subtree_with_zero (struct access *access, gimple_stmt_iterator *gsi,
    4107              :                         bool insert_after, location_t loc)
    4108              : 
    4109              : {
    4110       579056 :   struct access *child;
    4111              : 
    4112       579056 :   if (access->grp_to_be_replaced)
    4113              :     {
    4114       259956 :       gassign *stmt;
    4115              : 
    4116       259956 :       stmt = gimple_build_assign (get_access_replacement (access),
    4117              :                                   build_zero_cst (access->type));
    4118       259956 :       if (insert_after)
    4119        39692 :         gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
    4120              :       else
    4121       220264 :         gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
    4122       259956 :       update_stmt (stmt);
    4123       259956 :       gimple_set_location (stmt, loc);
    4124              :     }
    4125       319100 :   else if (access->grp_to_be_debug_replaced)
    4126              :     {
    4127        32364 :       gdebug *ds
    4128        32364 :         = gimple_build_debug_bind (get_access_replacement (access),
    4129              :                                    build_zero_cst (access->type),
    4130              :                                    gsi_stmt (*gsi));
    4131        32364 :       if (insert_after)
    4132        32364 :         gsi_insert_after (gsi, ds, GSI_NEW_STMT);
    4133              :       else
    4134            0 :         gsi_insert_before (gsi, ds, GSI_SAME_STMT);
    4135              :     }
    4136              : 
    4137      1002304 :   for (child = access->first_child; child; child = child->next_sibling)
    4138       423248 :     init_subtree_with_zero (child, gsi, insert_after, loc);
    4139       579056 : }
    4140              : 
    4141              : /* Clobber all scalar replacements in an access subtree.  ACCESS is the
    4142              :    root of the subtree to be processed.  GSI is the statement iterator used
    4143              :    for inserting statements which are added after the current statement if
    4144              :    INSERT_AFTER is true or before it otherwise.  */
    4145              : 
    4146              : static void
    4147      3061777 : clobber_subtree (struct access *access, gimple_stmt_iterator *gsi,
    4148              :                 bool insert_after, location_t loc)
    4149              : 
    4150              : {
    4151      3061777 :   struct access *child;
    4152              : 
    4153      3061777 :   if (access->grp_to_be_replaced)
    4154              :     {
    4155      1954350 :       tree rep = get_access_replacement (access);
    4156      1954350 :       tree clobber = build_clobber (access->type);
    4157      1954350 :       gimple *stmt = gimple_build_assign (rep, clobber);
    4158              : 
    4159      1954350 :       if (insert_after)
    4160       396916 :         gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
    4161              :       else
    4162      1557434 :         gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
    4163      1954350 :       update_stmt (stmt);
    4164      1954350 :       gimple_set_location (stmt, loc);
    4165              :     }
    4166              : 
    4167      5038985 :   for (child = access->first_child; child; child = child->next_sibling)
    4168      1977208 :     clobber_subtree (child, gsi, insert_after, loc);
    4169      3061777 : }
    4170              : 
    4171              : /* Search for an access representative for the given expression EXPR and
    4172              :    return it or NULL if it cannot be found.  */
    4173              : 
    4174              : static struct access *
    4175     49249393 : get_access_for_expr (tree expr)
    4176              : {
    4177     49249393 :   poly_int64 poffset, psize, pmax_size;
    4178     49249393 :   HOST_WIDE_INT offset, max_size;
    4179     49249393 :   tree base;
    4180     49249393 :   bool reverse;
    4181              : 
    4182     49249393 :   base = get_ref_base_and_extent (expr, &poffset, &psize, &pmax_size,
    4183              :                                   &reverse);
    4184     49249393 :   if (!known_size_p (pmax_size)
    4185     49094649 :       || !pmax_size.is_constant (&max_size)
    4186     49094649 :       || !poffset.is_constant (&offset)
    4187     49249393 :       || !DECL_P (base))
    4188              :     return NULL;
    4189              : 
    4190     23009127 :   if (tree basesize = DECL_SIZE (base))
    4191              :     {
    4192     22960529 :       poly_int64 sz;
    4193     22960529 :       if (offset < 0
    4194     22960513 :           || !poly_int_tree_p (basesize, &sz)
    4195     45921042 :           || known_le (sz, offset))
    4196         7046 :         return NULL;
    4197              :     }
    4198              : 
    4199     23002081 :   if (max_size == 0
    4200     23002081 :       || !bitmap_bit_p (candidate_bitmap, DECL_UID (base)))
    4201              :     return NULL;
    4202              : 
    4203      9709931 :   return get_var_base_offset_size_access (base, offset, max_size);
    4204              : }
    4205              : 
    4206              : /* Replace the expression EXPR with a scalar replacement if there is one and
    4207              :    generate other statements to do type conversion or subtree copying if
    4208              :    necessary.  WRITE is true if the expression is being written to (it is on a
    4209              :    LHS of a statement or output in an assembly statement).  STMT_GSI is used to
    4210              :    place newly created statements before the processed statement, REFRESH_GSI
    4211              :    is used to place them afterwards - unless the processed statement must end a
    4212              :    BB in which case it is placed on the outgoing non-EH edge.  REFRESH_GSI and
    4213              :    is then used to continue iteration over the BB.  If sra_modify_expr is
    4214              :    called only once with WRITE equal to true on a given statement, both
    4215              :    iterator parameters can point to the same one.  */
    4216              : 
    4217              : static bool
    4218      7976679 : sra_modify_expr (tree *expr, bool write, gimple_stmt_iterator *stmt_gsi,
    4219              :                  gimple_stmt_iterator *refresh_gsi)
    4220              : {
    4221      7976679 :   location_t loc;
    4222      7976679 :   struct access *access;
    4223      7976679 :   tree type, bfr, orig_expr;
    4224      7976679 :   bool partial_cplx_access = false;
    4225              : 
    4226      7976679 :   if (TREE_CODE (*expr) == BIT_FIELD_REF
    4227      7976679 :       && (write || !sra_handled_bf_read_p (*expr)))
    4228              :     {
    4229          590 :       bfr = *expr;
    4230          590 :       expr = &TREE_OPERAND (*expr, 0);
    4231              :     }
    4232              :   else
    4233              :     bfr = NULL_TREE;
    4234              : 
    4235      7976679 :   if (TREE_CODE (*expr) == REALPART_EXPR || TREE_CODE (*expr) == IMAGPART_EXPR)
    4236              :     {
    4237        31536 :       expr = &TREE_OPERAND (*expr, 0);
    4238        31536 :       partial_cplx_access = true;
    4239              :     }
    4240      7976679 :   access = get_access_for_expr (*expr);
    4241      7976679 :   if (!access)
    4242              :     return false;
    4243       211629 :   type = TREE_TYPE (*expr);
    4244       211629 :   orig_expr = *expr;
    4245              : 
    4246       211629 :   loc = gimple_location (gsi_stmt (*stmt_gsi));
    4247       211629 :   gimple_stmt_iterator alt_gsi = gsi_none ();
    4248       211629 :   if (write && stmt_ends_bb_p (gsi_stmt (*stmt_gsi)))
    4249              :     {
    4250        45463 :       alt_gsi = gsi_start_edge (single_non_eh_succ (gsi_bb (*stmt_gsi)));
    4251        45463 :       refresh_gsi = &alt_gsi;
    4252              :     }
    4253              : 
    4254       211629 :   if (access->grp_to_be_replaced)
    4255              :     {
    4256        56811 :       tree repl = get_access_replacement (access);
    4257              :       /* If we replace a non-register typed access simply use the original
    4258              :          access expression to extract the scalar component afterwards.
    4259              :          This happens if scalarizing a function return value or parameter
    4260              :          like in gcc.c-torture/execute/20041124-1.c, 20050316-1.c and
    4261              :          gcc.c-torture/compile/20011217-1.c.
    4262              : 
    4263              :          We also want to use this when accessing a complex or vector which can
    4264              :          be accessed as a different type too, potentially creating a need for
    4265              :          type conversion (see PR42196) and when scalarized unions are involved
    4266              :          in assembler statements (see PR42398).  */
    4267        56811 :       if (!bfr && !useless_type_conversion_p (type, access->type))
    4268              :         {
    4269        52825 :           tree ref;
    4270              : 
    4271        52825 :           ref = build_ref_for_model (loc, orig_expr, 0, access, stmt_gsi,
    4272              :                                      false);
    4273              : 
    4274        52825 :           if (partial_cplx_access)
    4275              :             {
    4276              :             /* VIEW_CONVERT_EXPRs in partial complex access are always fine in
    4277              :                the case of a write because in such case the replacement cannot
    4278              :                be a gimple register.  In the case of a load, we have to
    4279              :                differentiate in between a register an non-register
    4280              :                replacement.  */
    4281          343 :               tree t = build1 (VIEW_CONVERT_EXPR, type, repl);
    4282          343 :               gcc_checking_assert (!write || access->grp_partial_lhs);
    4283          343 :               if (!access->grp_partial_lhs)
    4284              :                 {
    4285          340 :                   tree tmp = make_ssa_name (type);
    4286          340 :                   gassign *stmt = gimple_build_assign (tmp, t);
    4287              :                   /* This is always a read. */
    4288          340 :                   gsi_insert_before (stmt_gsi, stmt, GSI_SAME_STMT);
    4289          340 :                   t = tmp;
    4290              :                 }
    4291          343 :               *expr = t;
    4292              :             }
    4293        52482 :           else if (write)
    4294              :             {
    4295        16104 :               gassign *stmt;
    4296              : 
    4297        16104 :               if (access->grp_partial_lhs)
    4298            6 :                 ref = force_gimple_operand_gsi (refresh_gsi, ref, true,
    4299              :                                                 NULL_TREE, false, GSI_NEW_STMT);
    4300        16104 :               stmt = gimple_build_assign (repl, ref);
    4301        16104 :               gimple_set_location (stmt, loc);
    4302        16104 :               gsi_insert_after (refresh_gsi, stmt, GSI_NEW_STMT);
    4303              :             }
    4304              :           else
    4305              :             {
    4306        36378 :               if (TREE_READONLY (access->base))
    4307              :                 return false;
    4308              : 
    4309        36345 :               gassign *stmt;
    4310        36345 :               if (access->grp_partial_lhs)
    4311           49 :                 repl = force_gimple_operand_gsi (stmt_gsi, repl, true,
    4312              :                                                  NULL_TREE, true,
    4313              :                                                  GSI_SAME_STMT);
    4314        36345 :               stmt = gimple_build_assign (ref, repl);
    4315        36345 :               gimple_set_location (stmt, loc);
    4316        36345 :               gsi_insert_before (stmt_gsi, stmt, GSI_SAME_STMT);
    4317              :             }
    4318              :         }
    4319              :       else
    4320              :         {
    4321              :           /* If we are going to replace a scalar field in a structure with
    4322              :              reverse storage order by a stand-alone scalar, we are going to
    4323              :              effectively byte-swap the scalar and we also need to byte-swap
    4324              :              the portion of it represented by the bit-field.  */
    4325         3986 :           if (bfr && REF_REVERSE_STORAGE_ORDER (bfr))
    4326              :             {
    4327            0 :               REF_REVERSE_STORAGE_ORDER (bfr) = 0;
    4328            0 :               TREE_OPERAND (bfr, 2)
    4329            0 :                 = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (repl)),
    4330              :                               size_binop (PLUS_EXPR, TREE_OPERAND (bfr, 1),
    4331              :                                                      TREE_OPERAND (bfr, 2)));
    4332              :             }
    4333              : 
    4334         3986 :           *expr = repl;
    4335              :         }
    4336              : 
    4337        56778 :       sra_stats.exprs++;
    4338              :     }
    4339       154818 :   else if (write && access->grp_to_be_debug_replaced)
    4340              :     {
    4341           12 :       gdebug *ds = gimple_build_debug_bind (get_access_replacement (access),
    4342              :                                             NULL_TREE,
    4343              :                                             gsi_stmt (*stmt_gsi));
    4344           12 :       gsi_insert_after (stmt_gsi, ds, GSI_NEW_STMT);
    4345              :     }
    4346              : 
    4347       211596 :   if (access->first_child && !TREE_READONLY (access->base))
    4348              :     {
    4349       151099 :       HOST_WIDE_INT start_offset, chunk_size;
    4350       151099 :       if (bfr
    4351            0 :           && tree_fits_uhwi_p (TREE_OPERAND (bfr, 1))
    4352       151099 :           && tree_fits_uhwi_p (TREE_OPERAND (bfr, 2)))
    4353              :         {
    4354            0 :           chunk_size = tree_to_uhwi (TREE_OPERAND (bfr, 1));
    4355            0 :           start_offset = access->offset
    4356            0 :             + tree_to_uhwi (TREE_OPERAND (bfr, 2));
    4357              :         }
    4358              :       else
    4359              :         start_offset = chunk_size = 0;
    4360              : 
    4361       250525 :       generate_subtree_copies (access->first_child, orig_expr, access->offset,
    4362              :                                start_offset, chunk_size,
    4363              :                                write ? refresh_gsi : stmt_gsi,
    4364              :                                write, write, loc);
    4365              :     }
    4366              :   return true;
    4367              : }
    4368              : 
    4369              : /* If EXPR, which must be a call argument, is an ADDR_EXPR, generate writes and
    4370              :    reads from its base before and after the call statement given in CALL_GSI
    4371              :    and return true if any copying took place.  Otherwise call sra_modify_expr
    4372              :    on EXPR and return its value.  FLAGS is what the gimple_call_arg_flags
    4373              :    return for the given parameter.  */
    4374              : 
    4375              : static bool
    4376      8776894 : sra_modify_call_arg (tree *expr, gimple_stmt_iterator *call_gsi,
    4377              :                      gimple_stmt_iterator *refresh_gsi, int flags)
    4378              : {
    4379      8776894 :   if (TREE_CODE (*expr) != ADDR_EXPR)
    4380      5751590 :     return sra_modify_expr (expr, false, call_gsi, refresh_gsi);
    4381              : 
    4382      3025304 :   if (flags & EAF_UNUSED)
    4383              :     return false;
    4384              : 
    4385      3021557 :   tree base = get_base_address (TREE_OPERAND (*expr, 0));
    4386      3021557 :   if (!DECL_P (base))
    4387              :     return false;
    4388      2263632 :   struct access *access = get_access_for_expr (base);
    4389      2263632 :   if (!access)
    4390              :     return false;
    4391              : 
    4392        56414 :   gimple *stmt = gsi_stmt (*call_gsi);
    4393        56414 :   location_t loc = gimple_location (stmt);
    4394        56414 :   generate_subtree_copies (access, base, 0, 0, 0, call_gsi, false, false,
    4395              :                            loc, true);
    4396              : 
    4397        56414 :   if (flags & EAF_NO_DIRECT_CLOBBER)
    4398              :     return true;
    4399              : 
    4400        39421 :   if (!stmt_ends_bb_p (stmt))
    4401        29197 :     generate_subtree_copies (access, base, 0, 0, 0, refresh_gsi, true,
    4402              :                              true, loc, true);
    4403              :   else
    4404              :     {
    4405        10224 :       edge e;
    4406        10224 :       edge_iterator ei;
    4407        30649 :       FOR_EACH_EDGE (e, ei, gsi_bb (*call_gsi)->succs)
    4408              :         {
    4409        20425 :           gimple_stmt_iterator alt_gsi = gsi_start_edge (e);
    4410        20425 :           generate_subtree_copies (access, base, 0, 0, 0, &alt_gsi, true,
    4411              :                                    true, loc, true);
    4412              :         }
    4413              :     }
    4414              :   return true;
    4415              : }
    4416              : 
    4417              : /* Where scalar replacements of the RHS have been written to when a replacement
    4418              :    of a LHS of an assignments cannot be directly loaded from a replacement of
    4419              :    the RHS. */
    4420              : enum unscalarized_data_handling { SRA_UDH_NONE,  /* Nothing done so far. */
    4421              :                                   SRA_UDH_RIGHT, /* Data flushed to the RHS. */
    4422              :                                   SRA_UDH_LEFT }; /* Data flushed to the LHS. */
    4423              : 
    4424              : struct subreplacement_assignment_data
    4425              : {
    4426              :   /* Offset of the access representing the lhs of the assignment.  */
    4427              :   HOST_WIDE_INT left_offset;
    4428              : 
    4429              :   /* LHS and RHS of the original assignment.  */
    4430              :   tree assignment_lhs, assignment_rhs;
    4431              : 
    4432              :   /* Access representing the rhs of the whole assignment.  */
    4433              :   struct access *top_racc;
    4434              : 
    4435              :   /* Stmt iterator used for statement insertions after the original assignment.
    4436              :    It points to the main GSI used to traverse a BB during function body
    4437              :    modification.  */
    4438              :   gimple_stmt_iterator *new_gsi;
    4439              : 
    4440              :   /* Stmt iterator used for statement insertions before the original
    4441              :    assignment.  Keeps on pointing to the original statement.  */
    4442              :   gimple_stmt_iterator old_gsi;
    4443              : 
    4444              :   /* Location of the assignment.   */
    4445              :   location_t loc;
    4446              : 
    4447              :   /* Keeps the information whether we have needed to refresh replacements of
    4448              :    the LHS and from which side of the assignments this takes place.  */
    4449              :   enum unscalarized_data_handling refreshed;
    4450              : };
    4451              : 
    4452              : /* Store all replacements in the access tree rooted in TOP_RACC either to their
    4453              :    base aggregate if there are unscalarized data or directly to LHS of the
    4454              :    statement that is pointed to by GSI otherwise.  */
    4455              : 
    4456              : static void
    4457       118765 : handle_unscalarized_data_in_subtree (struct subreplacement_assignment_data *sad)
    4458              : {
    4459       118765 :   tree src;
    4460              :   /* If the RHS is a load from a constant, we do not need to (and must not)
    4461              :      flush replacements to it and can use it directly as if we did.  */
    4462       118765 :   if (TREE_READONLY (sad->top_racc->base))
    4463              :     {
    4464           16 :       sad->refreshed = SRA_UDH_RIGHT;
    4465           16 :       return;
    4466              :     }
    4467       118749 :   if (sad->top_racc->grp_unscalarized_data)
    4468              :     {
    4469        26569 :       src = sad->assignment_rhs;
    4470        26569 :       sad->refreshed = SRA_UDH_RIGHT;
    4471              :     }
    4472              :   else
    4473              :     {
    4474        92180 :       src = sad->assignment_lhs;
    4475        92180 :       sad->refreshed = SRA_UDH_LEFT;
    4476              :     }
    4477       118749 :   generate_subtree_copies (sad->top_racc->first_child, src,
    4478              :                            sad->top_racc->offset, 0, 0,
    4479              :                            &sad->old_gsi, false, false, sad->loc);
    4480              : }
    4481              : 
    4482              : /* Try to generate statements to load all sub-replacements in an access subtree
    4483              :    formed by children of LACC from scalar replacements in the SAD->top_racc
    4484              :    subtree.  If that is not possible, refresh the SAD->top_racc base aggregate
    4485              :    and load the accesses from it.  */
    4486              : 
    4487              : static void
    4488       509827 : load_assign_lhs_subreplacements (struct access *lacc,
    4489              :                                  struct subreplacement_assignment_data *sad)
    4490              : {
    4491      1682468 :   for (lacc = lacc->first_child; lacc; lacc = lacc->next_sibling)
    4492              :     {
    4493      1172641 :       HOST_WIDE_INT offset;
    4494      1172641 :       offset = lacc->offset - sad->left_offset + sad->top_racc->offset;
    4495              : 
    4496      1172641 :       if (lacc->grp_to_be_replaced)
    4497              :         {
    4498       960983 :           struct access *racc;
    4499       960983 :           gassign *stmt;
    4500       960983 :           tree rhs;
    4501              : 
    4502       960983 :           racc = find_access_in_subtree (sad->top_racc, offset, lacc->size);
    4503       960983 :           if (racc && racc->grp_to_be_replaced)
    4504              :             {
    4505       934722 :               rhs = get_access_replacement (racc);
    4506       934722 :               bool vce = false;
    4507       934722 :               if (!useless_type_conversion_p (lacc->type, racc->type))
    4508              :                 {
    4509           31 :                   rhs = fold_build1_loc (sad->loc, VIEW_CONVERT_EXPR,
    4510              :                                          lacc->type, rhs);
    4511           31 :                   vce = true;
    4512              :                 }
    4513              : 
    4514       934722 :               if (lacc->grp_partial_lhs && (vce || racc->grp_partial_lhs))
    4515            3 :                 rhs = force_gimple_operand_gsi (&sad->old_gsi, rhs, true,
    4516              :                                                 NULL_TREE, true, GSI_SAME_STMT);
    4517              :             }
    4518              :           else
    4519              :             {
    4520              :               /* No suitable access on the right hand side, need to load from
    4521              :                  the aggregate.  See if we have to update it first... */
    4522        26261 :               if (sad->refreshed == SRA_UDH_NONE)
    4523        13151 :                 handle_unscalarized_data_in_subtree (sad);
    4524              : 
    4525        26261 :               if (sad->refreshed == SRA_UDH_LEFT)
    4526          604 :                 rhs = build_ref_for_model (sad->loc, sad->assignment_lhs,
    4527          604 :                                            lacc->offset - sad->left_offset,
    4528              :                                            lacc, sad->new_gsi, true);
    4529              :               else
    4530        25657 :                 rhs = build_ref_for_model (sad->loc, sad->assignment_rhs,
    4531        25657 :                                            lacc->offset - sad->left_offset,
    4532              :                                            lacc, sad->new_gsi, true);
    4533        26261 :               if (lacc->grp_partial_lhs)
    4534            1 :                 rhs = force_gimple_operand_gsi (sad->new_gsi,
    4535              :                                                 rhs, true, NULL_TREE,
    4536              :                                                 false, GSI_NEW_STMT);
    4537              :             }
    4538              : 
    4539       960983 :           stmt = gimple_build_assign (get_access_replacement (lacc), rhs);
    4540       960983 :           gsi_insert_after (sad->new_gsi, stmt, GSI_NEW_STMT);
    4541       960983 :           gimple_set_location (stmt, sad->loc);
    4542       960983 :           update_stmt (stmt);
    4543       960983 :           sra_stats.subreplacements++;
    4544              :         }
    4545              :       else
    4546              :         {
    4547       211658 :           if (sad->refreshed == SRA_UDH_NONE
    4548        28530 :               && lacc->grp_read && !lacc->grp_covered)
    4549           24 :             handle_unscalarized_data_in_subtree (sad);
    4550              : 
    4551       211658 :           if (lacc && lacc->grp_to_be_debug_replaced)
    4552              :             {
    4553       122820 :               gdebug *ds;
    4554       122820 :               tree drhs;
    4555       122820 :               struct access *racc = find_access_in_subtree (sad->top_racc,
    4556              :                                                             offset,
    4557              :                                                             lacc->size);
    4558              : 
    4559       122820 :               if (racc && racc->grp_to_be_replaced)
    4560              :                 {
    4561       122675 :                   if (racc->grp_write || constant_decl_p (racc->base))
    4562       119265 :                     drhs = get_access_replacement (racc);
    4563              :                   else
    4564              :                     drhs = NULL;
    4565              :                 }
    4566          145 :               else if (sad->refreshed == SRA_UDH_LEFT)
    4567            0 :                 drhs = build_debug_ref_for_model (sad->loc, lacc->base,
    4568              :                                                   lacc->offset, lacc);
    4569          145 :               else if (sad->refreshed == SRA_UDH_RIGHT)
    4570          143 :                 drhs = build_debug_ref_for_model (sad->loc, sad->top_racc->base,
    4571              :                                                   offset, lacc);
    4572              :               else
    4573              :                 drhs = NULL_TREE;
    4574       119265 :               if (drhs
    4575       119408 :                   && !useless_type_conversion_p (lacc->type, TREE_TYPE (drhs)))
    4576         2321 :                 drhs = fold_build1_loc (sad->loc, VIEW_CONVERT_EXPR,
    4577              :                                         lacc->type, drhs);
    4578       122820 :               ds = gimple_build_debug_bind (get_access_replacement (lacc),
    4579              :                                             drhs, gsi_stmt (sad->old_gsi));
    4580       122820 :               gsi_insert_after (sad->new_gsi, ds, GSI_NEW_STMT);
    4581              :             }
    4582              :         }
    4583              : 
    4584      1172641 :       if (lacc->first_child)
    4585        38506 :         load_assign_lhs_subreplacements (lacc, sad);
    4586              :     }
    4587       509827 : }
    4588              : 
    4589              : /* Result code for SRA assignment modification.  */
    4590              : enum assignment_mod_result { SRA_AM_NONE,       /* nothing done for the stmt */
    4591              :                              SRA_AM_MODIFIED,  /* stmt changed but not
    4592              :                                                   removed */
    4593              :                              SRA_AM_REMOVED };  /* stmt eliminated */
    4594              : 
    4595              : /* Modify assignments with a CONSTRUCTOR on their RHS.  STMT contains a pointer
    4596              :    to the assignment and GSI is the statement iterator pointing at it.  Returns
    4597              :    the same values as sra_modify_assign.  */
    4598              : 
    4599              : static enum assignment_mod_result
    4600      3103239 : sra_modify_constructor_assign (gimple *stmt, gimple_stmt_iterator *gsi)
    4601              : {
    4602      3103239 :   tree lhs = gimple_assign_lhs (stmt);
    4603      3103239 :   struct access *acc = get_access_for_expr (lhs);
    4604      3103239 :   if (!acc)
    4605              :     return SRA_AM_NONE;
    4606      1240377 :   location_t loc = gimple_location (stmt);
    4607              : 
    4608      1240377 :   if (gimple_clobber_p (stmt))
    4609              :     {
    4610              :       /* Clobber the replacement variable.  */
    4611      1084569 :       clobber_subtree (acc, gsi, !acc->grp_covered, loc);
    4612              :       /* Remove clobbers of fully scalarized variables, they are dead.  */
    4613      1084569 :       if (acc->grp_covered)
    4614              :         {
    4615       823940 :           unlink_stmt_vdef (stmt);
    4616       823940 :           gsi_remove (gsi, true);
    4617       823940 :           release_defs (stmt);
    4618       823940 :           return SRA_AM_REMOVED;
    4619              :         }
    4620              :       else
    4621              :         return SRA_AM_MODIFIED;
    4622              :     }
    4623              : 
    4624       155808 :   if (CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt)) > 0)
    4625              :     {
    4626              :       /* I have never seen this code path trigger but if it can happen the
    4627              :          following should handle it gracefully.  */
    4628            0 :       if (access_has_children_p (acc))
    4629            0 :         generate_subtree_copies (acc->first_child, lhs, acc->offset, 0, 0, gsi,
    4630              :                                  true, true, loc);
    4631              :       return SRA_AM_MODIFIED;
    4632              :     }
    4633              : 
    4634       155808 :   if (acc->grp_covered)
    4635              :     {
    4636        82047 :       init_subtree_with_zero (acc, gsi, false, loc);
    4637        82047 :       unlink_stmt_vdef (stmt);
    4638        82047 :       gsi_remove (gsi, true);
    4639        82047 :       release_defs (stmt);
    4640        82047 :       return SRA_AM_REMOVED;
    4641              :     }
    4642              :   else
    4643              :     {
    4644        73761 :       init_subtree_with_zero (acc, gsi, true, loc);
    4645        73761 :       return SRA_AM_MODIFIED;
    4646              :     }
    4647              : }
    4648              : 
    4649              : /* Create and return a new suitable default definition SSA_NAME for RACC which
    4650              :    is an access describing an uninitialized part of an aggregate that is being
    4651              :    loaded.  REG_TREE is used instead of the actual RACC type if that is not of
    4652              :    a gimple register type.  */
    4653              : 
    4654              : static tree
    4655          547 : get_repl_default_def_ssa_name (struct access *racc, tree reg_type)
    4656              : {
    4657          547 :   gcc_checking_assert (!racc->grp_to_be_replaced
    4658              :                        && !racc->grp_to_be_debug_replaced);
    4659          547 :   if (!racc->replacement_decl)
    4660          547 :     racc->replacement_decl = create_access_replacement (racc, reg_type);
    4661          547 :   return get_or_create_ssa_default_def (cfun, racc->replacement_decl);
    4662              : }
    4663              : 
    4664              : 
    4665              : /* Generate statements to call .DEFERRED_INIT to initialize scalar replacements
    4666              :    of accesses within a subtree ACCESS; all its children, siblings and their
    4667              :    children are to be processed.
    4668              :    GSI is a statement iterator used to place the new statements.  */
    4669              : static void
    4670        34672 : generate_subtree_deferred_init (struct access *access,
    4671              :                                 tree init_type,
    4672              :                                 tree decl_name,
    4673              :                                 gimple_stmt_iterator *gsi,
    4674              :                                 location_t loc)
    4675              : {
    4676        79763 :   do
    4677              :     {
    4678        79763 :       if (access->grp_to_be_replaced)
    4679              :         {
    4680        62593 :           tree repl = get_access_replacement (access);
    4681        62593 :           gimple *call
    4682        62593 :             = gimple_build_call_internal (IFN_DEFERRED_INIT, 3,
    4683        62593 :                                           TYPE_SIZE_UNIT (TREE_TYPE (repl)),
    4684              :                                           init_type, decl_name);
    4685        62593 :           gimple_call_set_lhs (call, repl);
    4686        62593 :           gsi_insert_before (gsi, call, GSI_SAME_STMT);
    4687        62593 :           update_stmt (call);
    4688        62593 :           gimple_set_location (call, loc);
    4689        62593 :           sra_stats.subtree_deferred_init++;
    4690              :         }
    4691        79763 :       if (access->first_child)
    4692         4842 :         generate_subtree_deferred_init (access->first_child, init_type,
    4693              :                                         decl_name, gsi, loc);
    4694              : 
    4695        79763 :       access = access ->next_sibling;
    4696              :     }
    4697        79763 :   while (access);
    4698        34672 : }
    4699              : 
    4700              : /* For a call to .DEFERRED_INIT:
    4701              :    var = .DEFERRED_INIT (size_of_var, init_type, name_of_var);
    4702              :    examine the LHS variable VAR and replace it with a scalar replacement if
    4703              :    there is one, also replace the RHS call to a call to .DEFERRED_INIT of
    4704              :    the corresponding scalar relacement variable.  Examine the subtree and
    4705              :    do the scalar replacements in the subtree too.  STMT is the call, GSI is
    4706              :    the statement iterator to place newly created statement.  */
    4707              : 
    4708              : static enum assignment_mod_result
    4709       120005 : sra_modify_deferred_init (gimple *stmt, gimple_stmt_iterator *gsi)
    4710              : {
    4711       120005 :   tree lhs = gimple_call_lhs (stmt);
    4712       120005 :   tree init_type = gimple_call_arg (stmt, 1);
    4713       120005 :   tree decl_name = gimple_call_arg (stmt, 2);
    4714              : 
    4715       120005 :   struct access *lhs_access = get_access_for_expr (lhs);
    4716       120005 :   if (!lhs_access)
    4717              :     return SRA_AM_NONE;
    4718              : 
    4719        45587 :   location_t loc = gimple_location (stmt);
    4720              : 
    4721        45587 :   if (lhs_access->grp_to_be_replaced)
    4722              :     {
    4723        15188 :       tree lhs_repl = get_access_replacement (lhs_access);
    4724        15188 :       gimple_call_set_lhs (stmt, lhs_repl);
    4725        15188 :       tree arg0_repl = TYPE_SIZE_UNIT (TREE_TYPE (lhs_repl));
    4726        15188 :       gimple_call_set_arg (stmt, 0, arg0_repl);
    4727        15188 :       sra_stats.deferred_init++;
    4728        15188 :       gcc_assert (!lhs_access->first_child);
    4729              :       return SRA_AM_MODIFIED;
    4730              :     }
    4731              : 
    4732        30399 :   if (lhs_access->first_child)
    4733        29830 :     generate_subtree_deferred_init (lhs_access->first_child,
    4734              :                                     init_type, decl_name, gsi, loc);
    4735        30399 :   if (lhs_access->grp_covered)
    4736              :     {
    4737        19211 :       unlink_stmt_vdef (stmt);
    4738        19211 :       gsi_remove (gsi, true);
    4739        19211 :       release_defs (stmt);
    4740        19211 :       return SRA_AM_REMOVED;
    4741              :     }
    4742              : 
    4743              :   return SRA_AM_MODIFIED;
    4744              : }
    4745              : 
    4746              : /* Examine both sides of the assignment statement pointed to by STMT, replace
    4747              :    them with a scalare replacement if there is one and generate copying of
    4748              :    replacements if scalarized aggregates have been used in the assignment.  GSI
    4749              :    is used to hold generated statements for type conversions and subtree
    4750              :    copying.  */
    4751              : 
    4752              : static enum assignment_mod_result
    4753     26897158 : sra_modify_assign (gimple *stmt, gimple_stmt_iterator *gsi)
    4754              : {
    4755     26897158 :   struct access *lacc, *racc;
    4756     26897158 :   tree lhs, rhs;
    4757     26897158 :   bool modify_this_stmt = false;
    4758     26897158 :   bool force_gimple_rhs = false;
    4759     26897158 :   location_t loc;
    4760     26897158 :   gimple_stmt_iterator orig_gsi = *gsi;
    4761              : 
    4762     26897158 :   if (!gimple_assign_single_p (stmt))
    4763              :     return SRA_AM_NONE;
    4764     21028284 :   lhs = gimple_assign_lhs (stmt);
    4765     21028284 :   rhs = gimple_assign_rhs1 (stmt);
    4766              : 
    4767     21028284 :   if (TREE_CODE (rhs) == CONSTRUCTOR)
    4768      3103239 :     return sra_modify_constructor_assign (stmt, gsi);
    4769              : 
    4770     17913675 :   if (TREE_CODE (rhs) == REALPART_EXPR || TREE_CODE (lhs) == REALPART_EXPR
    4771     17909633 :       || TREE_CODE (rhs) == IMAGPART_EXPR || TREE_CODE (lhs) == IMAGPART_EXPR
    4772     17893509 :       || (TREE_CODE (rhs) == BIT_FIELD_REF && !sra_handled_bf_read_p (rhs))
    4773     35818550 :       || TREE_CODE (lhs) == BIT_FIELD_REF)
    4774              :     {
    4775        32126 :       modify_this_stmt = sra_modify_expr (gimple_assign_rhs1_ptr (stmt),
    4776              :                                           false, gsi, gsi);
    4777        32126 :       modify_this_stmt |= sra_modify_expr (gimple_assign_lhs_ptr (stmt),
    4778              :                                            true, gsi, gsi);
    4779        32126 :       return modify_this_stmt ? SRA_AM_MODIFIED : SRA_AM_NONE;
    4780              :     }
    4781              : 
    4782     17892919 :   lacc = get_access_for_expr (lhs);
    4783     17892919 :   racc = get_access_for_expr (rhs);
    4784     17892919 :   if (!lacc && !racc)
    4785              :     return SRA_AM_NONE;
    4786              :   /* Avoid modifying initializations of constant-pool replacements.  */
    4787      7343321 :   if (racc && (racc->replacement_decl == lhs))
    4788              :     return SRA_AM_NONE;
    4789              : 
    4790      7338340 :   loc = gimple_location (stmt);
    4791      7338340 :   if (lacc && lacc->grp_to_be_replaced)
    4792              :     {
    4793      2051477 :       lhs = get_access_replacement (lacc);
    4794      2051477 :       gimple_assign_set_lhs (stmt, lhs);
    4795      2051477 :       modify_this_stmt = true;
    4796      2051477 :       if (lacc->grp_partial_lhs)
    4797           87 :         force_gimple_rhs = true;
    4798      2051477 :       sra_stats.exprs++;
    4799              :     }
    4800              : 
    4801      7338340 :   if (racc && racc->grp_to_be_replaced)
    4802              :     {
    4803      3314563 :       rhs = get_access_replacement (racc);
    4804      3314563 :       modify_this_stmt = true;
    4805      3314563 :       if (racc->grp_partial_lhs)
    4806          879 :         force_gimple_rhs = true;
    4807      3314563 :       sra_stats.exprs++;
    4808              :     }
    4809      1128028 :   else if (racc
    4810      1128028 :            && !racc->grp_unscalarized_data
    4811       900791 :            && !racc->grp_unscalarizable_region
    4812       900789 :            && TREE_CODE (lhs) == SSA_NAME
    4813          547 :            && !access_has_replacements_p (racc))
    4814              :     {
    4815          547 :       rhs = get_repl_default_def_ssa_name (racc, TREE_TYPE (lhs));
    4816          547 :       modify_this_stmt = true;
    4817          547 :       sra_stats.exprs++;
    4818              :     }
    4819              : 
    4820      3315110 :   if (modify_this_stmt
    4821      7338340 :       && !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
    4822              :     {
    4823              :       /* If we can avoid creating a VIEW_CONVERT_EXPR, then do so.
    4824              :          ??? This should move to fold_stmt which we simply should
    4825              :          call after building a VIEW_CONVERT_EXPR here.  */
    4826       620008 :       if (AGGREGATE_TYPE_P (TREE_TYPE (lhs))
    4827       173511 :           && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (lhs)) == racc->reverse
    4828       484020 :           && !contains_bitfld_component_ref_p (lhs))
    4829              :         {
    4830       173510 :           lhs = build_ref_for_model (loc, lhs, 0, racc, gsi, false);
    4831       173510 :           gimple_assign_set_lhs (stmt, lhs);
    4832              :         }
    4833       137000 :       else if (lacc
    4834       104735 :                && AGGREGATE_TYPE_P (TREE_TYPE (rhs))
    4835        72664 :                && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (rhs)) == lacc->reverse
    4836       209664 :                && !contains_vce_or_bfcref_p (rhs))
    4837        72008 :         rhs = build_ref_for_model (loc, rhs, 0, lacc, gsi, false);
    4838              : 
    4839       310510 :       if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
    4840              :         {
    4841        64992 :           rhs = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
    4842        64992 :           if (is_gimple_reg_type (TREE_TYPE (lhs))
    4843        64992 :               && TREE_CODE (lhs) != SSA_NAME)
    4844      7338340 :             force_gimple_rhs = true;
    4845              :         }
    4846              :     }
    4847              : 
    4848      7338340 :   if (lacc && lacc->grp_to_be_debug_replaced)
    4849              :     {
    4850       166304 :       tree dlhs = get_access_replacement (lacc);
    4851       166304 :       tree drhs = unshare_expr (rhs);
    4852       166304 :       if (!useless_type_conversion_p (TREE_TYPE (dlhs), TREE_TYPE (drhs)))
    4853              :         {
    4854        10588 :           if (AGGREGATE_TYPE_P (TREE_TYPE (drhs))
    4855         5352 :               && !contains_vce_or_bfcref_p (drhs))
    4856           58 :             drhs = build_debug_ref_for_model (loc, drhs, 0, lacc);
    4857         5294 :           if (drhs
    4858        10588 :               && !useless_type_conversion_p (TREE_TYPE (dlhs),
    4859         5294 :                                              TREE_TYPE (drhs)))
    4860         5236 :             drhs = fold_build1_loc (loc, VIEW_CONVERT_EXPR,
    4861         5236 :                                     TREE_TYPE (dlhs), drhs);
    4862              :         }
    4863       166304 :       gdebug *ds = gimple_build_debug_bind (dlhs, drhs, stmt);
    4864       166304 :       gsi_insert_before (gsi, ds, GSI_SAME_STMT);
    4865              :     }
    4866              : 
    4867              :   /* From this point on, the function deals with assignments in between
    4868              :      aggregates when at least one has scalar reductions of some of its
    4869              :      components.  There are three possible scenarios: Both the LHS and RHS have
    4870              :      to-be-scalarized components, 2) only the RHS has or 3) only the LHS has.
    4871              : 
    4872              :      In the first case, we would like to load the LHS components from RHS
    4873              :      components whenever possible.  If that is not possible, we would like to
    4874              :      read it directly from the RHS (after updating it by storing in it its own
    4875              :      components).  If there are some necessary unscalarized data in the LHS,
    4876              :      those will be loaded by the original assignment too.  If neither of these
    4877              :      cases happen, the original statement can be removed.  Most of this is done
    4878              :      by load_assign_lhs_subreplacements.
    4879              : 
    4880              :      In the second case, we would like to store all RHS scalarized components
    4881              :      directly into LHS and if they cover the aggregate completely, remove the
    4882              :      statement too.  In the third case, we want the LHS components to be loaded
    4883              :      directly from the RHS (DSE will remove the original statement if it
    4884              :      becomes redundant).
    4885              : 
    4886              :      This is a bit complex but manageable when types match and when unions do
    4887              :      not cause confusion in a way that we cannot really load a component of LHS
    4888              :      from the RHS or vice versa (the access representing this level can have
    4889              :      subaccesses that are accessible only through a different union field at a
    4890              :      higher level - different from the one used in the examined expression).
    4891              :      Unions are fun.
    4892              : 
    4893              :      Therefore, I specially handle a fourth case, happening when there is a
    4894              :      specific type cast or it is impossible to locate a scalarized subaccess on
    4895              :      the other side of the expression.  If that happens, I simply "refresh" the
    4896              :      RHS by storing in it is scalarized components leave the original statement
    4897              :      there to do the copying and then load the scalar replacements of the LHS.
    4898              :      This is what the first branch does.  */
    4899              : 
    4900      7338340 :   if (modify_this_stmt
    4901      2108464 :       || gimple_has_volatile_ops (stmt)
    4902      2108084 :       || contains_vce_or_bfcref_p (rhs)
    4903      1926771 :       || contains_vce_or_bfcref_p (lhs)
    4904      9262209 :       || stmt_ends_bb_p (stmt))
    4905              :     {
    4906              :       /* No need to copy into a constant, it comes pre-initialized.  */
    4907      5512395 :       if (access_has_children_p (racc) && !TREE_READONLY (racc->base))
    4908        22023 :         generate_subtree_copies (racc->first_child, rhs, racc->offset, 0, 0,
    4909              :                                  gsi, false, false, loc);
    4910      5490372 :       if (access_has_children_p (lacc))
    4911              :         {
    4912       252300 :           gimple_stmt_iterator alt_gsi = gsi_none ();
    4913       252300 :           if (stmt_ends_bb_p (stmt))
    4914              :             {
    4915        75037 :               alt_gsi = gsi_start_edge (single_non_eh_succ (gsi_bb (*gsi)));
    4916        75037 :               gsi = &alt_gsi;
    4917              :             }
    4918       252300 :           generate_subtree_copies (lacc->first_child, lhs, lacc->offset, 0, 0,
    4919              :                                    gsi, true, true, loc);
    4920              :         }
    4921      5490372 :       sra_stats.separate_lhs_rhs_handling++;
    4922              : 
    4923              :       /* This gimplification must be done after generate_subtree_copies,
    4924              :          lest we insert the subtree copies in the middle of the gimplified
    4925              :          sequence.  */
    4926      5490372 :       if (force_gimple_rhs)
    4927        33676 :         rhs = force_gimple_operand_gsi (&orig_gsi, rhs, true, NULL_TREE,
    4928              :                                         true, GSI_SAME_STMT);
    4929      5490372 :       if (gimple_assign_rhs1 (stmt) != rhs)
    4930              :         {
    4931      3419722 :           modify_this_stmt = true;
    4932      3419722 :           gimple_assign_set_rhs_from_tree (&orig_gsi, rhs);
    4933      3419722 :           gcc_assert (stmt == gsi_stmt (orig_gsi));
    4934              :         }
    4935              : 
    4936      2070650 :       return modify_this_stmt ? SRA_AM_MODIFIED : SRA_AM_NONE;
    4937              :     }
    4938              :   else
    4939              :     {
    4940      2566395 :       if (access_has_children_p (lacc)
    4941      1847977 :           && access_has_children_p (racc)
    4942              :           /* When an access represents an unscalarizable region, it usually
    4943              :              represents accesses with variable offset and thus must not be used
    4944              :              to generate new memory accesses.  */
    4945       471330 :           && !lacc->grp_unscalarizable_region
    4946       471327 :           && !racc->grp_unscalarizable_region)
    4947              :         {
    4948       471321 :           struct subreplacement_assignment_data sad;
    4949              : 
    4950       471321 :           sad.left_offset = lacc->offset;
    4951       471321 :           sad.assignment_lhs = lhs;
    4952       471321 :           sad.assignment_rhs = rhs;
    4953       471321 :           sad.top_racc = racc;
    4954       471321 :           sad.old_gsi = *gsi;
    4955       471321 :           sad.new_gsi = gsi;
    4956       471321 :           sad.loc = gimple_location (stmt);
    4957       471321 :           sad.refreshed = SRA_UDH_NONE;
    4958              : 
    4959       471321 :           if (lacc->grp_read && !lacc->grp_covered)
    4960       105590 :             handle_unscalarized_data_in_subtree (&sad);
    4961              : 
    4962       471321 :           load_assign_lhs_subreplacements (lacc, &sad);
    4963       471321 :           if (sad.refreshed != SRA_UDH_RIGHT)
    4964              :             {
    4965       444736 :               gsi_next (gsi);
    4966       444736 :               unlink_stmt_vdef (stmt);
    4967       444736 :               gsi_remove (&sad.old_gsi, true);
    4968       444736 :               release_defs (stmt);
    4969       444736 :               sra_stats.deleted++;
    4970       444736 :               return SRA_AM_REMOVED;
    4971              :             }
    4972              :         }
    4973              :       else
    4974              :         {
    4975      1376647 :           if (access_has_children_p (racc)
    4976       499414 :               && !racc->grp_unscalarized_data
    4977       437649 :               && TREE_CODE (lhs) != SSA_NAME)
    4978              :             {
    4979       437648 :               if (dump_file)
    4980              :                 {
    4981           19 :                   fprintf (dump_file, "Removing load: ");
    4982           19 :                   print_gimple_stmt (dump_file, stmt, 0);
    4983              :                 }
    4984       437648 :               generate_subtree_copies (racc->first_child, lhs,
    4985              :                                        racc->offset, 0, 0, gsi,
    4986              :                                        false, false, loc);
    4987       437648 :               gcc_assert (stmt == gsi_stmt (*gsi));
    4988       437648 :               unlink_stmt_vdef (stmt);
    4989       437648 :               gsi_remove (gsi, true);
    4990       437648 :               release_defs (stmt);
    4991       437648 :               sra_stats.deleted++;
    4992       437648 :               return SRA_AM_REMOVED;
    4993              :             }
    4994              :           /* Restore the aggregate RHS from its components so the
    4995              :              prevailing aggregate copy does the right thing.  */
    4996      1000765 :           if (access_has_children_p (racc) && !TREE_READONLY (racc->base))
    4997        61758 :             generate_subtree_copies (racc->first_child, rhs, racc->offset, 0, 0,
    4998              :                                      gsi, false, false, loc);
    4999              :           /* Re-load the components of the aggregate copy destination.
    5000              :              But use the RHS aggregate to load from to expose more
    5001              :              optimization opportunities.  */
    5002       938999 :           if (access_has_children_p (lacc))
    5003              :             {
    5004       247103 :               generate_subtree_copies (lacc->first_child, rhs, lacc->offset,
    5005              :                                        0, 0, gsi, true, true, loc);
    5006       247103 :               if (lacc->grp_covered)
    5007              :                 {
    5008       182186 :                   unlink_stmt_vdef (stmt);
    5009       182186 :                   gsi_remove (& orig_gsi, true);
    5010       182186 :                   release_defs (stmt);
    5011       182186 :                   sra_stats.deleted++;
    5012       182186 :                   return SRA_AM_REMOVED;
    5013              :                 }
    5014              :             }
    5015              :         }
    5016              : 
    5017              :       return SRA_AM_NONE;
    5018              :     }
    5019              : }
    5020              : 
    5021              : /* Set any scalar replacements of values in the constant pool to the initial
    5022              :    value of the constant.  (Constant-pool decls like *.LC0 have effectively
    5023              :    been initialized before the program starts, we must do the same for their
    5024              :    replacements.)  Thus, we output statements like 'SR.1 = *.LC0[0];' into
    5025              :    the function's entry block.  */
    5026              : 
    5027              : static void
    5028       461621 : initialize_constant_pool_replacements (void)
    5029              : {
    5030       461621 :   gimple_seq seq = NULL;
    5031       461621 :   gimple_stmt_iterator gsi = gsi_start (seq);
    5032       461621 :   bitmap_iterator bi;
    5033       461621 :   unsigned i;
    5034              : 
    5035      2346184 :   EXECUTE_IF_SET_IN_BITMAP (candidate_bitmap, 0, i, bi)
    5036              :     {
    5037      1884563 :       tree var = candidate (i);
    5038      1884563 :       if (!constant_decl_p (var))
    5039      1884482 :         continue;
    5040              : 
    5041           81 :       struct access *access = get_first_repr_for_decl (var);
    5042              : 
    5043         6386 :       while (access)
    5044              :         {
    5045         6224 :           if (access->replacement_decl)
    5046              :             {
    5047         4981 :               gassign *stmt
    5048         4981 :                 = gimple_build_assign (get_access_replacement (access),
    5049              :                                        unshare_expr (access->expr));
    5050         4981 :               if (dump_file && (dump_flags & TDF_DETAILS))
    5051              :                 {
    5052            0 :                   fprintf (dump_file, "Generating constant initializer: ");
    5053            0 :                   print_gimple_stmt (dump_file, stmt, 0);
    5054            0 :                   fprintf (dump_file, "\n");
    5055              :                 }
    5056         4981 :               gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
    5057         4981 :               update_stmt (stmt);
    5058              :             }
    5059              : 
    5060         6224 :           if (access->first_child)
    5061              :             access = access->first_child;
    5062         4981 :           else if (access->next_sibling)
    5063              :             access = access->next_sibling;
    5064              :           else
    5065              :             {
    5066         2351 :               while (access->parent && !access->next_sibling)
    5067              :                 access = access->parent;
    5068         1108 :               if (access->next_sibling)
    5069              :                 access = access->next_sibling;
    5070              :               else
    5071           81 :                 access = access->next_grp;
    5072              :             }
    5073              :         }
    5074              :     }
    5075              : 
    5076       461621 :   seq = gsi_seq (gsi);
    5077       461621 :   if (seq)
    5078           76 :     gsi_insert_seq_on_edge_immediate (
    5079           76 :       single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)), seq);
    5080       461621 : }
    5081              : 
    5082              : /* Traverse the function body and all modifications as decided in
    5083              :    analyze_all_variable_accesses.  Return true iff the CFG has been
    5084              :    changed.  */
    5085              : 
    5086              : static bool
    5087       461621 : sra_modify_function_body (void)
    5088              : {
    5089       461621 :   bool cfg_changed = false;
    5090       461621 :   basic_block bb;
    5091              : 
    5092       461621 :   initialize_constant_pool_replacements ();
    5093              : 
    5094     10377271 :   FOR_EACH_BB_FN (bb, cfun)
    5095              :     {
    5096      9915650 :       gimple_stmt_iterator gsi = gsi_start_bb (bb);
    5097     88440728 :       while (!gsi_end_p (gsi))
    5098              :         {
    5099     78525078 :           gimple *stmt = gsi_stmt (gsi);
    5100     78525078 :           enum assignment_mod_result assign_result;
    5101     78525078 :           bool modified = false, deleted = false;
    5102     78525078 :           tree *t;
    5103     78525078 :           unsigned i;
    5104              : 
    5105     78525078 :           switch (gimple_code (stmt))
    5106              :             {
    5107       459844 :             case GIMPLE_RETURN:
    5108       459844 :               t = gimple_return_retval_ptr (as_a <greturn *> (stmt));
    5109       459844 :               if (*t != NULL_TREE)
    5110       297948 :                 modified |= sra_modify_expr (t, false, &gsi, &gsi);
    5111              :               break;
    5112              : 
    5113     26897158 :             case GIMPLE_ASSIGN:
    5114     26897158 :               assign_result = sra_modify_assign (stmt, &gsi);
    5115     26897158 :               modified |= assign_result == SRA_AM_MODIFIED;
    5116     26897158 :               deleted = assign_result == SRA_AM_REMOVED;
    5117     26897158 :               break;
    5118              : 
    5119      4540139 :             case GIMPLE_CALL:
    5120              :               /* Handle calls to .DEFERRED_INIT specially.  */
    5121      4540139 :               if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
    5122              :                 {
    5123       120005 :                   assign_result = sra_modify_deferred_init (stmt, &gsi);
    5124       120005 :                   modified |= assign_result == SRA_AM_MODIFIED;
    5125       120005 :                   deleted = assign_result == SRA_AM_REMOVED;
    5126              :                 }
    5127              :               else
    5128              :                 {
    5129      4420134 :                   gcall *call = as_a <gcall *> (stmt);
    5130      4420134 :                   gimple_stmt_iterator call_gsi = gsi;
    5131              : 
    5132              :                   /* Operands must be processed before the lhs.  */
    5133     13167489 :                   for (i = 0; i < gimple_call_num_args (call); i++)
    5134              :                     {
    5135      8747355 :                       int flags = gimple_call_arg_flags (call, i);
    5136      8747355 :                       t = gimple_call_arg_ptr (call, i);
    5137      8747355 :                       modified |= sra_modify_call_arg (t, &call_gsi, &gsi, flags);
    5138              :                     }
    5139      4420134 :                   if (gimple_call_chain (call))
    5140              :                     {
    5141        29539 :                       t = gimple_call_chain_ptr (call);
    5142        29539 :                       int flags = gimple_call_static_chain_flags (call);
    5143        29539 :                       modified |= sra_modify_call_arg (t, &call_gsi, &gsi,
    5144              :                                                        flags);
    5145              :                     }
    5146      4420134 :                   if (gimple_call_lhs (call))
    5147              :                     {
    5148      1853176 :                       t = gimple_call_lhs_ptr (call);
    5149      1853176 :                       modified |= sra_modify_expr (t, true, &call_gsi, &gsi);
    5150              :                     }
    5151              :                 }
    5152              :               break;
    5153              : 
    5154         7950 :             case GIMPLE_ASM:
    5155         7950 :               {
    5156         7950 :                 gimple_stmt_iterator stmt_gsi = gsi;
    5157         7950 :                 gasm *asm_stmt = as_a <gasm *> (stmt);
    5158        20606 :                 for (i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
    5159              :                   {
    5160         4706 :                     t = &TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
    5161         4706 :                     modified |= sra_modify_expr (t, false, &stmt_gsi, &gsi);
    5162              :                   }
    5163        12957 :                 for (i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
    5164              :                   {
    5165         5007 :                     t = &TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
    5166         5007 :                     modified |= sra_modify_expr (t, true, &stmt_gsi, &gsi);
    5167              :                   }
    5168              :               }
    5169         7950 :               break;
    5170              : 
    5171              :             default:
    5172              :               break;
    5173              :             }
    5174              : 
    5175     31743195 :           if (modified)
    5176              :             {
    5177      5836674 :               update_stmt (stmt);
    5178      5836674 :               if (maybe_clean_eh_stmt (stmt)
    5179      5836674 :                   && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
    5180              :                 cfg_changed = true;
    5181              :             }
    5182     78525078 :           if (!deleted)
    5183     76535310 :             gsi_next (&gsi);
    5184              :         }
    5185              :     }
    5186              : 
    5187       461621 :   gsi_commit_edge_inserts ();
    5188       461621 :   return cfg_changed;
    5189              : }
    5190              : 
    5191              : /* Generate statements initializing scalar replacements of parts of function
    5192              :    parameters.  */
    5193              : 
    5194              : static void
    5195       461621 : initialize_parameter_reductions (void)
    5196              : {
    5197       461621 :   gimple_stmt_iterator gsi;
    5198       461621 :   gimple_seq seq = NULL;
    5199       461621 :   tree parm;
    5200              : 
    5201       461621 :   gsi = gsi_start (seq);
    5202       461621 :   for (parm = DECL_ARGUMENTS (current_function_decl);
    5203      1395675 :        parm;
    5204       934054 :        parm = DECL_CHAIN (parm))
    5205              :     {
    5206       934054 :       vec<access_p> *access_vec;
    5207       934054 :       struct access *access;
    5208              : 
    5209       934054 :       if (!bitmap_bit_p (candidate_bitmap, DECL_UID (parm)))
    5210       866259 :         continue;
    5211        67795 :       access_vec = get_base_access_vector (parm);
    5212        67795 :       if (!access_vec)
    5213            0 :         continue;
    5214              : 
    5215        67795 :       for (access = (*access_vec)[0];
    5216       172949 :            access;
    5217       105154 :            access = access->next_grp)
    5218       105154 :         generate_subtree_copies (access, parm, 0, 0, 0, &gsi, true, true,
    5219       105154 :                                  EXPR_LOCATION (parm));
    5220              :     }
    5221              : 
    5222       461621 :   seq = gsi_seq (gsi);
    5223       461621 :   if (seq)
    5224        53758 :     gsi_insert_seq_on_edge_immediate (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)), seq);
    5225       461621 : }
    5226              : 
    5227              : /* The "main" function of intraprocedural SRA passes.  Runs the analysis and if
    5228              :    it reveals there are components of some aggregates to be scalarized, it runs
    5229              :    the required transformations.  */
    5230              : static unsigned int
    5231      3591423 : perform_intra_sra (void)
    5232              : {
    5233      3591423 :   int ret = 0;
    5234      3591423 :   sra_initialize ();
    5235              : 
    5236      3591423 :   if (!find_var_candidates ())
    5237      2762771 :     goto out;
    5238              : 
    5239       828652 :   if (!scan_function ())
    5240        57147 :     goto out;
    5241              : 
    5242       771505 :   if (!analyze_all_variable_accesses ())
    5243       309884 :     goto out;
    5244              : 
    5245       461621 :   if (sra_modify_function_body ())
    5246              :     ret = TODO_update_ssa | TODO_cleanup_cfg;
    5247              :   else
    5248       461599 :     ret = TODO_update_ssa;
    5249       461621 :   initialize_parameter_reductions ();
    5250              : 
    5251       461621 :   statistics_counter_event (cfun, "Scalar replacements created",
    5252              :                             sra_stats.replacements);
    5253       461621 :   statistics_counter_event (cfun, "Modified expressions", sra_stats.exprs);
    5254       461621 :   statistics_counter_event (cfun, "Subtree copy stmts",
    5255              :                             sra_stats.subtree_copies);
    5256       461621 :   statistics_counter_event (cfun, "Subreplacement stmts",
    5257              :                             sra_stats.subreplacements);
    5258       461621 :   statistics_counter_event (cfun, "Deleted stmts", sra_stats.deleted);
    5259       461621 :   statistics_counter_event (cfun, "Separate LHS and RHS handling",
    5260              :                             sra_stats.separate_lhs_rhs_handling);
    5261              : 
    5262      3591423 :  out:
    5263      3591423 :   sra_deinitialize ();
    5264      3591423 :   return ret;
    5265              : }
    5266              : 
    5267              : /* Perform early intraprocedural SRA.  */
    5268              : static unsigned int
    5269      2523619 : early_intra_sra (void)
    5270              : {
    5271      2523619 :   sra_mode = SRA_MODE_EARLY_INTRA;
    5272            0 :   return perform_intra_sra ();
    5273              : }
    5274              : 
    5275              : /* Perform "late" intraprocedural SRA.  */
    5276              : static unsigned int
    5277      1067804 : late_intra_sra (void)
    5278              : {
    5279      1067804 :   sra_mode = SRA_MODE_INTRA;
    5280            0 :   return perform_intra_sra ();
    5281              : }
    5282              : 
    5283              : 
    5284              : static bool
    5285      3595533 : gate_intra_sra (void)
    5286              : {
    5287      3595533 :   return flag_tree_sra != 0 && dbg_cnt (tree_sra);
    5288              : }
    5289              : 
    5290              : 
    5291              : namespace {
    5292              : 
    5293              : const pass_data pass_data_sra_early =
    5294              : {
    5295              :   GIMPLE_PASS, /* type */
    5296              :   "esra", /* name */
    5297              :   OPTGROUP_NONE, /* optinfo_flags */
    5298              :   TV_TREE_SRA, /* tv_id */
    5299              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
    5300              :   0, /* properties_provided */
    5301              :   0, /* properties_destroyed */
    5302              :   0, /* todo_flags_start */
    5303              :   TODO_update_ssa, /* todo_flags_finish */
    5304              : };
    5305              : 
    5306              : class pass_sra_early : public gimple_opt_pass
    5307              : {
    5308              : public:
    5309       302985 :   pass_sra_early (gcc::context *ctxt)
    5310       605970 :     : gimple_opt_pass (pass_data_sra_early, ctxt)
    5311              :   {}
    5312              : 
    5313              :   /* opt_pass methods: */
    5314      2527232 :   bool gate (function *) final override { return gate_intra_sra (); }
    5315      2523619 :   unsigned int execute (function *) final override
    5316              :   {
    5317      2523619 :     return early_intra_sra ();
    5318              :   }
    5319              : 
    5320              : }; // class pass_sra_early
    5321              : 
    5322              : } // anon namespace
    5323              : 
    5324              : gimple_opt_pass *
    5325       302985 : make_pass_sra_early (gcc::context *ctxt)
    5326              : {
    5327       302985 :   return new pass_sra_early (ctxt);
    5328              : }
    5329              : 
    5330              : namespace {
    5331              : 
    5332              : const pass_data pass_data_sra =
    5333              : {
    5334              :   GIMPLE_PASS, /* type */
    5335              :   "sra", /* name */
    5336              :   OPTGROUP_NONE, /* optinfo_flags */
    5337              :   TV_TREE_SRA, /* tv_id */
    5338              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
    5339              :   0, /* properties_provided */
    5340              :   0, /* properties_destroyed */
    5341              :   TODO_update_address_taken, /* todo_flags_start */
    5342              :   TODO_update_ssa, /* todo_flags_finish */
    5343              : };
    5344              : 
    5345              : class pass_sra : public gimple_opt_pass
    5346              : {
    5347              : public:
    5348       302985 :   pass_sra (gcc::context *ctxt)
    5349       605970 :     : gimple_opt_pass (pass_data_sra, ctxt)
    5350              :   {}
    5351              : 
    5352              :   /* opt_pass methods: */
    5353      1068301 :   bool gate (function *) final override { return gate_intra_sra (); }
    5354      1067804 :   unsigned int execute (function *) final override { return late_intra_sra (); }
    5355              : 
    5356              : }; // class pass_sra
    5357              : 
    5358              : } // anon namespace
    5359              : 
    5360              : gimple_opt_pass *
    5361       302985 : make_pass_sra (gcc::context *ctxt)
    5362              : {
    5363       302985 :   return new pass_sra (ctxt);
    5364              : }
    5365              : 
    5366              : 
    5367              : /* If type T cannot be totally scalarized, return false.  Otherwise return true
    5368              :    and push to the vector within PC offsets and lengths of all padding in the
    5369              :    type as total scalarization would encounter it.  */
    5370              : 
    5371              : static bool
    5372        46836 : check_ts_and_push_padding_to_vec (tree type, sra_padding_collecting *pc)
    5373              : {
    5374        46836 :   if (!totally_scalarizable_type_p (type, true /* optimistic value */,
    5375              :                                     0, pc))
    5376              :     return false;
    5377              : 
    5378        42192 :   pc->record_padding (tree_to_shwi (TYPE_SIZE (type)));
    5379        42192 :   return true;
    5380              : }
    5381              : 
    5382              : /* Given two types in an assignment, return true either if any one cannot be
    5383              :    totally scalarized or if they have padding (i.e. not copied bits)  */
    5384              : 
    5385              : bool
    5386        25740 : sra_total_scalarization_would_copy_same_data_p (tree t1, tree t2)
    5387              : {
    5388        25740 :   sra_padding_collecting p1;
    5389        25740 :   if (!check_ts_and_push_padding_to_vec (t1, &p1))
    5390              :     return true;
    5391              : 
    5392        21096 :   sra_padding_collecting p2;
    5393        21096 :   if (!check_ts_and_push_padding_to_vec (t2, &p2))
    5394              :     return true;
    5395              : 
    5396        21096 :   unsigned l = p1.m_padding.length ();
    5397        42192 :   if (l != p2.m_padding.length ())
    5398              :     return false;
    5399        26569 :   for (unsigned i = 0; i < l; i++)
    5400         5476 :     if (p1.m_padding[i].first != p2.m_padding[i].first
    5401         5476 :         || p1.m_padding[i].second != p2.m_padding[i].second)
    5402              :       return false;
    5403              : 
    5404              :   return true;
    5405        21096 : }
    5406              : 
        

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.