c++: catch parm initialization tweak
[official-gcc.git] / gcc / tree-sra.cc
blob1a3e12f18cc7fe58fd8046ff7e11b4039eff9df3
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-2022 Free Software Foundation, Inc.
5 Contributed by Martin Jambor <mjambor@suse.cz>
7 This file is part of GCC.
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.
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.
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/>. */
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.
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.
33 Both passes operate in four stages:
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.
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.
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.
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.
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).
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.
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.
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. */
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"
104 /* Enumeration of all aggregate reductions we can do. */
105 enum sra_mode { SRA_MODE_EARLY_IPA, /* early call regularization */
106 SRA_MODE_EARLY_INTRA, /* early intraprocedural SRA */
107 SRA_MODE_INTRA }; /* late intraprocedural SRA */
109 /* Global variable describing which aggregate reduction we are performing at
110 the moment. */
111 static enum sra_mode sra_mode;
113 struct assign_link;
115 /* ACCESS represents each access to an aggregate variable (as a whole or a
116 part). It can also represent a group of accesses that refer to exactly the
117 same fragment of an aggregate (i.e. those that have exactly the same offset
118 and size). Such representatives for a single aggregate, once determined,
119 are linked in a linked list and have the group fields set.
121 Moreover, when doing intraprocedural SRA, a tree is built from those
122 representatives (by the means of first_child and next_sibling pointers), in
123 which all items in a subtree are "within" the root, i.e. their offset is
124 greater or equal to offset of the root and offset+size is smaller or equal
125 to offset+size of the root. Children of an access are sorted by offset.
127 Note that accesses to parts of vector and complex number types always
128 represented by an access to the whole complex number or a vector. It is a
129 duty of the modifying functions to replace them appropriately. */
131 struct access
133 /* Values returned by `get_ref_base_and_extent' for each component reference
134 If EXPR isn't a component reference just set `BASE = EXPR', `OFFSET = 0',
135 `SIZE = TREE_SIZE (TREE_TYPE (expr))'. */
136 HOST_WIDE_INT offset;
137 HOST_WIDE_INT size;
138 tree base;
140 /* Expression. It is context dependent so do not use it to create new
141 expressions to access the original aggregate. See PR 42154 for a
142 testcase. */
143 tree expr;
144 /* Type. */
145 tree type;
147 /* The statement this access belongs to. */
148 gimple *stmt;
150 /* Next group representative for this aggregate. */
151 struct access *next_grp;
153 /* Pointer to the group representative. Pointer to itself if the struct is
154 the representative. */
155 struct access *group_representative;
157 /* After access tree has been constructed, this points to the parent of the
158 current access, if there is one. NULL for roots. */
159 struct access *parent;
161 /* If this access has any children (in terms of the definition above), this
162 points to the first one. */
163 struct access *first_child;
165 /* In intraprocedural SRA, pointer to the next sibling in the access tree as
166 described above. */
167 struct access *next_sibling;
169 /* Pointers to the first and last element in the linked list of assign
170 links for propagation from LHS to RHS. */
171 struct assign_link *first_rhs_link, *last_rhs_link;
173 /* Pointers to the first and last element in the linked list of assign
174 links for propagation from LHS to RHS. */
175 struct assign_link *first_lhs_link, *last_lhs_link;
177 /* Pointer to the next access in the work queues. */
178 struct access *next_rhs_queued, *next_lhs_queued;
180 /* Replacement variable for this access "region." Never to be accessed
181 directly, always only by the means of get_access_replacement() and only
182 when grp_to_be_replaced flag is set. */
183 tree replacement_decl;
185 /* Is this access made in reverse storage order? */
186 unsigned reverse : 1;
188 /* Is this particular access write access? */
189 unsigned write : 1;
191 /* Is this access currently in the rhs work queue? */
192 unsigned grp_rhs_queued : 1;
194 /* Is this access currently in the lhs work queue? */
195 unsigned grp_lhs_queued : 1;
197 /* Does this group contain a write access? This flag is propagated down the
198 access tree. */
199 unsigned grp_write : 1;
201 /* Does this group contain a read access? This flag is propagated down the
202 access tree. */
203 unsigned grp_read : 1;
205 /* Does this group contain a read access that comes from an assignment
206 statement? This flag is propagated down the access tree. */
207 unsigned grp_assignment_read : 1;
209 /* Does this group contain a write access that comes from an assignment
210 statement? This flag is propagated down the access tree. */
211 unsigned grp_assignment_write : 1;
213 /* Does this group contain a read access through a scalar type? This flag is
214 not propagated in the access tree in any direction. */
215 unsigned grp_scalar_read : 1;
217 /* Does this group contain a write access through a scalar type? This flag
218 is not propagated in the access tree in any direction. */
219 unsigned grp_scalar_write : 1;
221 /* In a root of an access tree, true means that the entire tree should be
222 totally scalarized - that all scalar leafs should be scalarized and
223 non-root grp_total_scalarization accesses should be honored. Otherwise,
224 non-root accesses with grp_total_scalarization should never get scalar
225 replacements. */
226 unsigned grp_total_scalarization : 1;
228 /* Other passes of the analysis use this bit to make function
229 analyze_access_subtree create scalar replacements for this group if
230 possible. */
231 unsigned grp_hint : 1;
233 /* Is the subtree rooted in this access fully covered by scalar
234 replacements? */
235 unsigned grp_covered : 1;
237 /* If set to true, this access and all below it in an access tree must not be
238 scalarized. */
239 unsigned grp_unscalarizable_region : 1;
241 /* Whether data have been written to parts of the aggregate covered by this
242 access which is not to be scalarized. This flag is propagated up in the
243 access tree. */
244 unsigned grp_unscalarized_data : 1;
246 /* Set if all accesses in the group consist of the same chain of
247 COMPONENT_REFs and ARRAY_REFs. */
248 unsigned grp_same_access_path : 1;
250 /* Does this access and/or group contain a write access through a
251 BIT_FIELD_REF? */
252 unsigned grp_partial_lhs : 1;
254 /* Set when a scalar replacement should be created for this variable. */
255 unsigned grp_to_be_replaced : 1;
257 /* Set when we want a replacement for the sole purpose of having it in
258 generated debug statements. */
259 unsigned grp_to_be_debug_replaced : 1;
261 /* Should TREE_NO_WARNING of a replacement be set? */
262 unsigned grp_no_warning : 1;
265 typedef struct access *access_p;
268 /* Alloc pool for allocating access structures. */
269 static object_allocator<struct access> access_pool ("SRA accesses");
271 /* A structure linking lhs and rhs accesses from an aggregate assignment. They
272 are used to propagate subaccesses from rhs to lhs and vice versa as long as
273 they don't conflict with what is already there. In the RHS->LHS direction,
274 we also propagate grp_write flag to lazily mark that the access contains any
275 meaningful data. */
276 struct assign_link
278 struct access *lacc, *racc;
279 struct assign_link *next_rhs, *next_lhs;
282 /* Alloc pool for allocating assign link structures. */
283 static object_allocator<assign_link> assign_link_pool ("SRA links");
285 /* Base (tree) -> Vector (vec<access_p> *) map. */
286 static hash_map<tree, auto_vec<access_p> > *base_access_vec;
288 /* Hash to limit creation of artificial accesses */
289 static hash_map<tree, unsigned> *propagation_budget;
291 /* Candidate hash table helpers. */
293 struct uid_decl_hasher : nofree_ptr_hash <tree_node>
295 static inline hashval_t hash (const tree_node *);
296 static inline bool equal (const tree_node *, const tree_node *);
299 /* Hash a tree in a uid_decl_map. */
301 inline hashval_t
302 uid_decl_hasher::hash (const tree_node *item)
304 return item->decl_minimal.uid;
307 /* Return true if the DECL_UID in both trees are equal. */
309 inline bool
310 uid_decl_hasher::equal (const tree_node *a, const tree_node *b)
312 return (a->decl_minimal.uid == b->decl_minimal.uid);
315 /* Set of candidates. */
316 static bitmap candidate_bitmap;
317 static hash_table<uid_decl_hasher> *candidates;
319 /* For a candidate UID return the candidates decl. */
321 static inline tree
322 candidate (unsigned uid)
324 tree_node t;
325 t.decl_minimal.uid = uid;
326 return candidates->find_with_hash (&t, static_cast <hashval_t> (uid));
329 /* Bitmap of candidates which we should try to entirely scalarize away and
330 those which cannot be (because they are and need be used as a whole). */
331 static bitmap should_scalarize_away_bitmap, cannot_scalarize_away_bitmap;
333 /* Bitmap of candidates in the constant pool, which cannot be scalarized
334 because this would produce non-constant expressions (e.g. Ada). */
335 static bitmap disqualified_constants;
337 /* Obstack for creation of fancy names. */
338 static struct obstack name_obstack;
340 /* Head of a linked list of accesses that need to have its subaccesses
341 propagated to their assignment counterparts. */
342 static struct access *rhs_work_queue_head, *lhs_work_queue_head;
344 /* Dump contents of ACCESS to file F in a human friendly way. If GRP is true,
345 representative fields are dumped, otherwise those which only describe the
346 individual access are. */
348 static struct
350 /* Number of processed aggregates is readily available in
351 analyze_all_variable_accesses and so is not stored here. */
353 /* Number of created scalar replacements. */
354 int replacements;
356 /* Number of times sra_modify_expr or sra_modify_assign themselves changed an
357 expression. */
358 int exprs;
360 /* Number of statements created by generate_subtree_copies. */
361 int subtree_copies;
363 /* Number of statements created by load_assign_lhs_subreplacements. */
364 int subreplacements;
366 /* Number of times sra_modify_assign has deleted a statement. */
367 int deleted;
369 /* Number of times sra_modify_assign has to deal with subaccesses of LHS and
370 RHS reparately due to type conversions or nonexistent matching
371 references. */
372 int separate_lhs_rhs_handling;
374 /* Number of parameters that were removed because they were unused. */
375 int deleted_unused_parameters;
377 /* Number of scalars passed as parameters by reference that have been
378 converted to be passed by value. */
379 int scalar_by_ref_to_by_val;
381 /* Number of aggregate parameters that were replaced by one or more of their
382 components. */
383 int aggregate_params_reduced;
385 /* Numbber of components created when splitting aggregate parameters. */
386 int param_reductions_created;
388 /* Number of deferred_init calls that are modified. */
389 int deferred_init;
391 /* Number of deferred_init calls that are created by
392 generate_subtree_deferred_init. */
393 int subtree_deferred_init;
394 } sra_stats;
396 static void
397 dump_access (FILE *f, struct access *access, bool grp)
399 fprintf (f, "access { ");
400 fprintf (f, "base = (%d)'", DECL_UID (access->base));
401 print_generic_expr (f, access->base);
402 fprintf (f, "', offset = " HOST_WIDE_INT_PRINT_DEC, access->offset);
403 fprintf (f, ", size = " HOST_WIDE_INT_PRINT_DEC, access->size);
404 fprintf (f, ", expr = ");
405 print_generic_expr (f, access->expr);
406 fprintf (f, ", type = ");
407 print_generic_expr (f, access->type);
408 fprintf (f, ", reverse = %d", access->reverse);
409 if (grp)
410 fprintf (f, ", grp_read = %d, grp_write = %d, grp_assignment_read = %d, "
411 "grp_assignment_write = %d, grp_scalar_read = %d, "
412 "grp_scalar_write = %d, grp_total_scalarization = %d, "
413 "grp_hint = %d, grp_covered = %d, "
414 "grp_unscalarizable_region = %d, grp_unscalarized_data = %d, "
415 "grp_same_access_path = %d, grp_partial_lhs = %d, "
416 "grp_to_be_replaced = %d, grp_to_be_debug_replaced = %d}\n",
417 access->grp_read, access->grp_write, access->grp_assignment_read,
418 access->grp_assignment_write, access->grp_scalar_read,
419 access->grp_scalar_write, access->grp_total_scalarization,
420 access->grp_hint, access->grp_covered,
421 access->grp_unscalarizable_region, access->grp_unscalarized_data,
422 access->grp_same_access_path, access->grp_partial_lhs,
423 access->grp_to_be_replaced, access->grp_to_be_debug_replaced);
424 else
425 fprintf (f, ", write = %d, grp_total_scalarization = %d, "
426 "grp_partial_lhs = %d}\n",
427 access->write, access->grp_total_scalarization,
428 access->grp_partial_lhs);
431 /* Dump a subtree rooted in ACCESS to file F, indent by LEVEL. */
433 static void
434 dump_access_tree_1 (FILE *f, struct access *access, int level)
438 int i;
440 for (i = 0; i < level; i++)
441 fputs ("* ", f);
443 dump_access (f, access, true);
445 if (access->first_child)
446 dump_access_tree_1 (f, access->first_child, level + 1);
448 access = access->next_sibling;
450 while (access);
453 /* Dump all access trees for a variable, given the pointer to the first root in
454 ACCESS. */
456 static void
457 dump_access_tree (FILE *f, struct access *access)
459 for (; access; access = access->next_grp)
460 dump_access_tree_1 (f, access, 0);
463 /* Return true iff ACC is non-NULL and has subaccesses. */
465 static inline bool
466 access_has_children_p (struct access *acc)
468 return acc && acc->first_child;
471 /* Return true iff ACC is (partly) covered by at least one replacement. */
473 static bool
474 access_has_replacements_p (struct access *acc)
476 struct access *child;
477 if (acc->grp_to_be_replaced)
478 return true;
479 for (child = acc->first_child; child; child = child->next_sibling)
480 if (access_has_replacements_p (child))
481 return true;
482 return false;
485 /* Return a vector of pointers to accesses for the variable given in BASE or
486 NULL if there is none. */
488 static vec<access_p> *
489 get_base_access_vector (tree base)
491 return base_access_vec->get (base);
494 /* Find an access with required OFFSET and SIZE in a subtree of accesses rooted
495 in ACCESS. Return NULL if it cannot be found. */
497 static struct access *
498 find_access_in_subtree (struct access *access, HOST_WIDE_INT offset,
499 HOST_WIDE_INT size)
501 while (access && (access->offset != offset || access->size != size))
503 struct access *child = access->first_child;
505 while (child && (child->offset + child->size <= offset))
506 child = child->next_sibling;
507 access = child;
510 /* Total scalarization does not replace single field structures with their
511 single field but rather creates an access for them underneath. Look for
512 it. */
513 if (access)
514 while (access->first_child
515 && access->first_child->offset == offset
516 && access->first_child->size == size)
517 access = access->first_child;
519 return access;
522 /* Return the first group representative for DECL or NULL if none exists. */
524 static struct access *
525 get_first_repr_for_decl (tree base)
527 vec<access_p> *access_vec;
529 access_vec = get_base_access_vector (base);
530 if (!access_vec)
531 return NULL;
533 return (*access_vec)[0];
536 /* Find an access representative for the variable BASE and given OFFSET and
537 SIZE. Requires that access trees have already been built. Return NULL if
538 it cannot be found. */
540 static struct access *
541 get_var_base_offset_size_access (tree base, HOST_WIDE_INT offset,
542 HOST_WIDE_INT size)
544 struct access *access;
546 access = get_first_repr_for_decl (base);
547 while (access && (access->offset + access->size <= offset))
548 access = access->next_grp;
549 if (!access)
550 return NULL;
552 return find_access_in_subtree (access, offset, size);
555 /* Add LINK to the linked list of assign links of RACC. */
557 static void
558 add_link_to_rhs (struct access *racc, struct assign_link *link)
560 gcc_assert (link->racc == racc);
562 if (!racc->first_rhs_link)
564 gcc_assert (!racc->last_rhs_link);
565 racc->first_rhs_link = link;
567 else
568 racc->last_rhs_link->next_rhs = link;
570 racc->last_rhs_link = link;
571 link->next_rhs = NULL;
574 /* Add LINK to the linked list of lhs assign links of LACC. */
576 static void
577 add_link_to_lhs (struct access *lacc, struct assign_link *link)
579 gcc_assert (link->lacc == lacc);
581 if (!lacc->first_lhs_link)
583 gcc_assert (!lacc->last_lhs_link);
584 lacc->first_lhs_link = link;
586 else
587 lacc->last_lhs_link->next_lhs = link;
589 lacc->last_lhs_link = link;
590 link->next_lhs = NULL;
593 /* Move all link structures in their linked list in OLD_ACC to the linked list
594 in NEW_ACC. */
595 static void
596 relink_to_new_repr (struct access *new_acc, struct access *old_acc)
598 if (old_acc->first_rhs_link)
601 if (new_acc->first_rhs_link)
603 gcc_assert (!new_acc->last_rhs_link->next_rhs);
604 gcc_assert (!old_acc->last_rhs_link
605 || !old_acc->last_rhs_link->next_rhs);
607 new_acc->last_rhs_link->next_rhs = old_acc->first_rhs_link;
608 new_acc->last_rhs_link = old_acc->last_rhs_link;
610 else
612 gcc_assert (!new_acc->last_rhs_link);
614 new_acc->first_rhs_link = old_acc->first_rhs_link;
615 new_acc->last_rhs_link = old_acc->last_rhs_link;
617 old_acc->first_rhs_link = old_acc->last_rhs_link = NULL;
619 else
620 gcc_assert (!old_acc->last_rhs_link);
622 if (old_acc->first_lhs_link)
625 if (new_acc->first_lhs_link)
627 gcc_assert (!new_acc->last_lhs_link->next_lhs);
628 gcc_assert (!old_acc->last_lhs_link
629 || !old_acc->last_lhs_link->next_lhs);
631 new_acc->last_lhs_link->next_lhs = old_acc->first_lhs_link;
632 new_acc->last_lhs_link = old_acc->last_lhs_link;
634 else
636 gcc_assert (!new_acc->last_lhs_link);
638 new_acc->first_lhs_link = old_acc->first_lhs_link;
639 new_acc->last_lhs_link = old_acc->last_lhs_link;
641 old_acc->first_lhs_link = old_acc->last_lhs_link = NULL;
643 else
644 gcc_assert (!old_acc->last_lhs_link);
648 /* Add ACCESS to the work to queue for propagation of subaccesses from RHS to
649 LHS (which is actually a stack). */
651 static void
652 add_access_to_rhs_work_queue (struct access *access)
654 if (access->first_rhs_link && !access->grp_rhs_queued)
656 gcc_assert (!access->next_rhs_queued);
657 access->next_rhs_queued = rhs_work_queue_head;
658 access->grp_rhs_queued = 1;
659 rhs_work_queue_head = access;
663 /* Add ACCESS to the work to queue for propagation of subaccesses from LHS to
664 RHS (which is actually a stack). */
666 static void
667 add_access_to_lhs_work_queue (struct access *access)
669 if (access->first_lhs_link && !access->grp_lhs_queued)
671 gcc_assert (!access->next_lhs_queued);
672 access->next_lhs_queued = lhs_work_queue_head;
673 access->grp_lhs_queued = 1;
674 lhs_work_queue_head = access;
678 /* Pop an access from the work queue for propagating from RHS to LHS, and
679 return it, assuming there is one. */
681 static struct access *
682 pop_access_from_rhs_work_queue (void)
684 struct access *access = rhs_work_queue_head;
686 rhs_work_queue_head = access->next_rhs_queued;
687 access->next_rhs_queued = NULL;
688 access->grp_rhs_queued = 0;
689 return access;
692 /* Pop an access from the work queue for propagating from LHS to RHS, and
693 return it, assuming there is one. */
695 static struct access *
696 pop_access_from_lhs_work_queue (void)
698 struct access *access = lhs_work_queue_head;
700 lhs_work_queue_head = access->next_lhs_queued;
701 access->next_lhs_queued = NULL;
702 access->grp_lhs_queued = 0;
703 return access;
706 /* Allocate necessary structures. */
708 static void
709 sra_initialize (void)
711 candidate_bitmap = BITMAP_ALLOC (NULL);
712 candidates = new hash_table<uid_decl_hasher>
713 (vec_safe_length (cfun->local_decls) / 2);
714 should_scalarize_away_bitmap = BITMAP_ALLOC (NULL);
715 cannot_scalarize_away_bitmap = BITMAP_ALLOC (NULL);
716 disqualified_constants = BITMAP_ALLOC (NULL);
717 gcc_obstack_init (&name_obstack);
718 base_access_vec = new hash_map<tree, auto_vec<access_p> >;
719 memset (&sra_stats, 0, sizeof (sra_stats));
722 /* Deallocate all general structures. */
724 static void
725 sra_deinitialize (void)
727 BITMAP_FREE (candidate_bitmap);
728 delete candidates;
729 candidates = NULL;
730 BITMAP_FREE (should_scalarize_away_bitmap);
731 BITMAP_FREE (cannot_scalarize_away_bitmap);
732 BITMAP_FREE (disqualified_constants);
733 access_pool.release ();
734 assign_link_pool.release ();
735 obstack_free (&name_obstack, NULL);
737 delete base_access_vec;
740 /* Return true if DECL is a VAR_DECL in the constant pool, false otherwise. */
742 static bool constant_decl_p (tree decl)
744 return VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl);
747 /* Remove DECL from candidates for SRA and write REASON to the dump file if
748 there is one. */
750 static void
751 disqualify_candidate (tree decl, const char *reason)
753 if (bitmap_clear_bit (candidate_bitmap, DECL_UID (decl)))
754 candidates->remove_elt_with_hash (decl, DECL_UID (decl));
755 if (constant_decl_p (decl))
756 bitmap_set_bit (disqualified_constants, DECL_UID (decl));
758 if (dump_file && (dump_flags & TDF_DETAILS))
760 fprintf (dump_file, "! Disqualifying ");
761 print_generic_expr (dump_file, decl);
762 fprintf (dump_file, " - %s\n", reason);
766 /* Return true iff the type contains a field or an element which does not allow
767 scalarization. Use VISITED_TYPES to avoid re-checking already checked
768 (sub-)types. */
770 static bool
771 type_internals_preclude_sra_p_1 (tree type, const char **msg,
772 hash_set<tree> *visited_types)
774 tree fld;
775 tree et;
777 if (visited_types->contains (type))
778 return false;
779 visited_types->add (type);
781 switch (TREE_CODE (type))
783 case RECORD_TYPE:
784 case UNION_TYPE:
785 case QUAL_UNION_TYPE:
786 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
787 if (TREE_CODE (fld) == FIELD_DECL)
789 if (TREE_CODE (fld) == FUNCTION_DECL)
790 continue;
791 tree ft = TREE_TYPE (fld);
793 if (TREE_THIS_VOLATILE (fld))
795 *msg = "volatile structure field";
796 return true;
798 if (!DECL_FIELD_OFFSET (fld))
800 *msg = "no structure field offset";
801 return true;
803 if (!DECL_SIZE (fld))
805 *msg = "zero structure field size";
806 return true;
808 if (!tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld)))
810 *msg = "structure field offset not fixed";
811 return true;
813 if (!tree_fits_uhwi_p (DECL_SIZE (fld)))
815 *msg = "structure field size not fixed";
816 return true;
818 if (!tree_fits_shwi_p (bit_position (fld)))
820 *msg = "structure field size too big";
821 return true;
823 if (AGGREGATE_TYPE_P (ft)
824 && int_bit_position (fld) % BITS_PER_UNIT != 0)
826 *msg = "structure field is bit field";
827 return true;
830 if (AGGREGATE_TYPE_P (ft)
831 && type_internals_preclude_sra_p_1 (ft, msg, visited_types))
832 return true;
835 return false;
837 case ARRAY_TYPE:
838 et = TREE_TYPE (type);
840 if (TYPE_VOLATILE (et))
842 *msg = "element type is volatile";
843 return true;
846 if (AGGREGATE_TYPE_P (et)
847 && type_internals_preclude_sra_p_1 (et, msg, visited_types))
848 return true;
850 return false;
852 default:
853 return false;
857 /* Return true iff the type contains a field or an element which does not allow
858 scalarization. */
860 bool
861 type_internals_preclude_sra_p (tree type, const char **msg)
863 hash_set<tree> visited_types;
864 return type_internals_preclude_sra_p_1 (type, msg, &visited_types);
868 /* Allocate an access structure for BASE, OFFSET and SIZE, clear it, fill in
869 the three fields. Also add it to the vector of accesses corresponding to
870 the base. Finally, return the new access. */
872 static struct access *
873 create_access_1 (tree base, HOST_WIDE_INT offset, HOST_WIDE_INT size)
875 struct access *access = access_pool.allocate ();
877 memset (access, 0, sizeof (struct access));
878 access->base = base;
879 access->offset = offset;
880 access->size = size;
882 base_access_vec->get_or_insert (base).safe_push (access);
884 return access;
887 static bool maybe_add_sra_candidate (tree);
889 /* Create and insert access for EXPR. Return created access, or NULL if it is
890 not possible. Also scan for uses of constant pool as we go along and add
891 to candidates. */
893 static struct access *
894 create_access (tree expr, gimple *stmt, bool write)
896 struct access *access;
897 poly_int64 poffset, psize, pmax_size;
898 tree base = expr;
899 bool reverse, unscalarizable_region = false;
901 base = get_ref_base_and_extent (expr, &poffset, &psize, &pmax_size,
902 &reverse);
904 /* For constant-pool entries, check we can substitute the constant value. */
905 if (constant_decl_p (base))
907 gcc_assert (!bitmap_bit_p (disqualified_constants, DECL_UID (base)));
908 if (expr != base
909 && !is_gimple_reg_type (TREE_TYPE (expr))
910 && dump_file && (dump_flags & TDF_DETAILS))
912 /* This occurs in Ada with accesses to ARRAY_RANGE_REFs,
913 and elements of multidimensional arrays (which are
914 multi-element arrays in their own right). */
915 fprintf (dump_file, "Allowing non-reg-type load of part"
916 " of constant-pool entry: ");
917 print_generic_expr (dump_file, expr);
919 maybe_add_sra_candidate (base);
922 if (!DECL_P (base) || !bitmap_bit_p (candidate_bitmap, DECL_UID (base)))
923 return NULL;
925 if (write && TREE_READONLY (base))
927 disqualify_candidate (base, "Encountered a store to a read-only decl.");
928 return NULL;
931 HOST_WIDE_INT offset, size, max_size;
932 if (!poffset.is_constant (&offset)
933 || !psize.is_constant (&size)
934 || !pmax_size.is_constant (&max_size))
936 disqualify_candidate (base, "Encountered a polynomial-sized access.");
937 return NULL;
940 if (size != max_size)
942 size = max_size;
943 unscalarizable_region = true;
945 if (size == 0)
946 return NULL;
947 if (offset < 0)
949 disqualify_candidate (base, "Encountered a negative offset access.");
950 return NULL;
952 if (size < 0)
954 disqualify_candidate (base, "Encountered an unconstrained access.");
955 return NULL;
957 if (offset + size > tree_to_shwi (DECL_SIZE (base)))
959 disqualify_candidate (base, "Encountered an access beyond the base.");
960 return NULL;
963 access = create_access_1 (base, offset, size);
964 access->expr = expr;
965 access->type = TREE_TYPE (expr);
966 access->write = write;
967 access->grp_unscalarizable_region = unscalarizable_region;
968 access->stmt = stmt;
969 access->reverse = reverse;
971 return access;
975 /* Return true iff TYPE is scalarizable - i.e. a RECORD_TYPE or fixed-length
976 ARRAY_TYPE with fields that are either of gimple register types (excluding
977 bit-fields) or (recursively) scalarizable types. CONST_DECL must be true if
978 we are considering a decl from constant pool. If it is false, char arrays
979 will be refused. */
981 static bool
982 scalarizable_type_p (tree type, bool const_decl)
984 if (is_gimple_reg_type (type))
985 return true;
986 if (type_contains_placeholder_p (type))
987 return false;
989 bool have_predecessor_field = false;
990 HOST_WIDE_INT prev_pos = 0;
992 switch (TREE_CODE (type))
994 case RECORD_TYPE:
995 for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
996 if (TREE_CODE (fld) == FIELD_DECL)
998 tree ft = TREE_TYPE (fld);
1000 if (zerop (DECL_SIZE (fld)))
1001 continue;
1003 HOST_WIDE_INT pos = int_bit_position (fld);
1004 if (have_predecessor_field
1005 && pos <= prev_pos)
1006 return false;
1008 have_predecessor_field = true;
1009 prev_pos = pos;
1011 if (DECL_BIT_FIELD (fld))
1012 return false;
1014 if (!scalarizable_type_p (ft, const_decl))
1015 return false;
1018 return true;
1020 case ARRAY_TYPE:
1022 HOST_WIDE_INT min_elem_size;
1023 if (const_decl)
1024 min_elem_size = 0;
1025 else
1026 min_elem_size = BITS_PER_UNIT;
1028 if (TYPE_DOMAIN (type) == NULL_TREE
1029 || !tree_fits_shwi_p (TYPE_SIZE (type))
1030 || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (type)))
1031 || (tree_to_shwi (TYPE_SIZE (TREE_TYPE (type))) <= min_elem_size)
1032 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
1033 return false;
1034 if (tree_to_shwi (TYPE_SIZE (type)) == 0
1035 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
1036 /* Zero-element array, should not prevent scalarization. */
1038 else if ((tree_to_shwi (TYPE_SIZE (type)) <= 0)
1039 || !tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1040 /* Variable-length array, do not allow scalarization. */
1041 return false;
1043 tree elem = TREE_TYPE (type);
1044 if (!scalarizable_type_p (elem, const_decl))
1045 return false;
1046 return true;
1048 default:
1049 return false;
1053 /* Return true if REF has an VIEW_CONVERT_EXPR somewhere in it. */
1055 static inline bool
1056 contains_view_convert_expr_p (const_tree ref)
1058 while (handled_component_p (ref))
1060 if (TREE_CODE (ref) == VIEW_CONVERT_EXPR)
1061 return true;
1062 ref = TREE_OPERAND (ref, 0);
1065 return false;
1068 /* Return true if REF contains a VIEW_CONVERT_EXPR or a COMPONENT_REF with a
1069 bit-field field declaration. If TYPE_CHANGING_P is non-NULL, set the bool
1070 it points to will be set if REF contains any of the above or a MEM_REF
1071 expression that effectively performs type conversion. */
1073 static bool
1074 contains_vce_or_bfcref_p (const_tree ref, bool *type_changing_p = NULL)
1076 while (handled_component_p (ref))
1078 if (TREE_CODE (ref) == VIEW_CONVERT_EXPR
1079 || (TREE_CODE (ref) == COMPONENT_REF
1080 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1))))
1082 if (type_changing_p)
1083 *type_changing_p = true;
1084 return true;
1086 ref = TREE_OPERAND (ref, 0);
1089 if (!type_changing_p
1090 || TREE_CODE (ref) != MEM_REF
1091 || TREE_CODE (TREE_OPERAND (ref, 0)) != ADDR_EXPR)
1092 return false;
1094 tree mem = TREE_OPERAND (TREE_OPERAND (ref, 0), 0);
1095 if (TYPE_MAIN_VARIANT (TREE_TYPE (ref))
1096 != TYPE_MAIN_VARIANT (TREE_TYPE (mem)))
1097 *type_changing_p = true;
1099 return false;
1102 /* Search the given tree for a declaration by skipping handled components and
1103 exclude it from the candidates. */
1105 static void
1106 disqualify_base_of_expr (tree t, const char *reason)
1108 t = get_base_address (t);
1109 if (t && DECL_P (t))
1110 disqualify_candidate (t, reason);
1113 /* Scan expression EXPR and create access structures for all accesses to
1114 candidates for scalarization. Return the created access or NULL if none is
1115 created. */
1117 static struct access *
1118 build_access_from_expr_1 (tree expr, gimple *stmt, bool write)
1120 struct access *ret = NULL;
1121 bool partial_ref;
1123 if (TREE_CODE (expr) == BIT_FIELD_REF
1124 || TREE_CODE (expr) == IMAGPART_EXPR
1125 || TREE_CODE (expr) == REALPART_EXPR)
1127 expr = TREE_OPERAND (expr, 0);
1128 partial_ref = true;
1130 else
1131 partial_ref = false;
1133 if (storage_order_barrier_p (expr))
1135 disqualify_base_of_expr (expr, "storage order barrier.");
1136 return NULL;
1139 /* We need to dive through V_C_Es in order to get the size of its parameter
1140 and not the result type. Ada produces such statements. We are also
1141 capable of handling the topmost V_C_E but not any of those buried in other
1142 handled components. */
1143 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR)
1144 expr = TREE_OPERAND (expr, 0);
1146 if (contains_view_convert_expr_p (expr))
1148 disqualify_base_of_expr (expr, "V_C_E under a different handled "
1149 "component.");
1150 return NULL;
1152 if (TREE_THIS_VOLATILE (expr))
1154 disqualify_base_of_expr (expr, "part of a volatile reference.");
1155 return NULL;
1158 switch (TREE_CODE (expr))
1160 case MEM_REF:
1161 if (TREE_CODE (TREE_OPERAND (expr, 0)) != ADDR_EXPR)
1162 return NULL;
1163 /* fall through */
1164 case VAR_DECL:
1165 case PARM_DECL:
1166 case RESULT_DECL:
1167 case COMPONENT_REF:
1168 case ARRAY_REF:
1169 case ARRAY_RANGE_REF:
1170 ret = create_access (expr, stmt, write);
1171 break;
1173 default:
1174 break;
1177 if (write && partial_ref && ret)
1178 ret->grp_partial_lhs = 1;
1180 return ret;
1183 /* Scan expression EXPR and create access structures for all accesses to
1184 candidates for scalarization. Return true if any access has been inserted.
1185 STMT must be the statement from which the expression is taken, WRITE must be
1186 true if the expression is a store and false otherwise. */
1188 static bool
1189 build_access_from_expr (tree expr, gimple *stmt, bool write)
1191 struct access *access;
1193 access = build_access_from_expr_1 (expr, stmt, write);
1194 if (access)
1196 /* This means the aggregate is accesses as a whole in a way other than an
1197 assign statement and thus cannot be removed even if we had a scalar
1198 replacement for everything. */
1199 if (cannot_scalarize_away_bitmap)
1200 bitmap_set_bit (cannot_scalarize_away_bitmap, DECL_UID (access->base));
1201 return true;
1203 return false;
1206 /* Return the single non-EH successor edge of BB or NULL if there is none or
1207 more than one. */
1209 static edge
1210 single_non_eh_succ (basic_block bb)
1212 edge e, res = NULL;
1213 edge_iterator ei;
1215 FOR_EACH_EDGE (e, ei, bb->succs)
1216 if (!(e->flags & EDGE_EH))
1218 if (res)
1219 return NULL;
1220 res = e;
1223 return res;
1226 /* Disqualify LHS and RHS for scalarization if STMT has to terminate its BB and
1227 there is no alternative spot where to put statements SRA might need to
1228 generate after it. The spot we are looking for is an edge leading to a
1229 single non-EH successor, if it exists and is indeed single. RHS may be
1230 NULL, in that case ignore it. */
1232 static bool
1233 disqualify_if_bad_bb_terminating_stmt (gimple *stmt, tree lhs, tree rhs)
1235 if (stmt_ends_bb_p (stmt))
1237 if (single_non_eh_succ (gimple_bb (stmt)))
1238 return false;
1240 disqualify_base_of_expr (lhs, "LHS of a throwing stmt.");
1241 if (rhs)
1242 disqualify_base_of_expr (rhs, "RHS of a throwing stmt.");
1243 return true;
1245 return false;
1248 /* Return true if the nature of BASE is such that it contains data even if
1249 there is no write to it in the function. */
1251 static bool
1252 comes_initialized_p (tree base)
1254 return TREE_CODE (base) == PARM_DECL || constant_decl_p (base);
1257 /* Scan expressions occurring in STMT, create access structures for all accesses
1258 to candidates for scalarization and remove those candidates which occur in
1259 statements or expressions that prevent them from being split apart. Return
1260 true if any access has been inserted. */
1262 static bool
1263 build_accesses_from_assign (gimple *stmt)
1265 tree lhs, rhs;
1266 struct access *lacc, *racc;
1268 if (!gimple_assign_single_p (stmt)
1269 /* Scope clobbers don't influence scalarization. */
1270 || gimple_clobber_p (stmt))
1271 return false;
1273 lhs = gimple_assign_lhs (stmt);
1274 rhs = gimple_assign_rhs1 (stmt);
1276 if (disqualify_if_bad_bb_terminating_stmt (stmt, lhs, rhs))
1277 return false;
1279 racc = build_access_from_expr_1 (rhs, stmt, false);
1280 lacc = build_access_from_expr_1 (lhs, stmt, true);
1282 if (lacc)
1284 lacc->grp_assignment_write = 1;
1285 if (storage_order_barrier_p (rhs))
1286 lacc->grp_unscalarizable_region = 1;
1288 if (should_scalarize_away_bitmap && !is_gimple_reg_type (lacc->type))
1290 bool type_changing_p = false;
1291 contains_vce_or_bfcref_p (lhs, &type_changing_p);
1292 if (type_changing_p)
1293 bitmap_set_bit (cannot_scalarize_away_bitmap,
1294 DECL_UID (lacc->base));
1298 if (racc)
1300 racc->grp_assignment_read = 1;
1301 if (should_scalarize_away_bitmap && !is_gimple_reg_type (racc->type))
1303 bool type_changing_p = false;
1304 contains_vce_or_bfcref_p (rhs, &type_changing_p);
1306 if (type_changing_p || gimple_has_volatile_ops (stmt))
1307 bitmap_set_bit (cannot_scalarize_away_bitmap,
1308 DECL_UID (racc->base));
1309 else
1310 bitmap_set_bit (should_scalarize_away_bitmap,
1311 DECL_UID (racc->base));
1313 if (storage_order_barrier_p (lhs))
1314 racc->grp_unscalarizable_region = 1;
1317 if (lacc && racc
1318 && (sra_mode == SRA_MODE_EARLY_INTRA || sra_mode == SRA_MODE_INTRA)
1319 && !lacc->grp_unscalarizable_region
1320 && !racc->grp_unscalarizable_region
1321 && AGGREGATE_TYPE_P (TREE_TYPE (lhs))
1322 && lacc->size == racc->size
1323 && useless_type_conversion_p (lacc->type, racc->type))
1325 struct assign_link *link;
1327 link = assign_link_pool.allocate ();
1328 memset (link, 0, sizeof (struct assign_link));
1330 link->lacc = lacc;
1331 link->racc = racc;
1332 add_link_to_rhs (racc, link);
1333 add_link_to_lhs (lacc, link);
1334 add_access_to_rhs_work_queue (racc);
1335 add_access_to_lhs_work_queue (lacc);
1337 /* Let's delay marking the areas as written until propagation of accesses
1338 across link, unless the nature of rhs tells us that its data comes
1339 from elsewhere. */
1340 if (!comes_initialized_p (racc->base))
1341 lacc->write = false;
1344 return lacc || racc;
1347 /* Callback of walk_stmt_load_store_addr_ops visit_addr used to determine
1348 GIMPLE_ASM operands with memory constrains which cannot be scalarized. */
1350 static bool
1351 asm_visit_addr (gimple *, tree op, tree, void *)
1353 op = get_base_address (op);
1354 if (op
1355 && DECL_P (op))
1356 disqualify_candidate (op, "Non-scalarizable GIMPLE_ASM operand.");
1358 return false;
1361 /* Scan function and look for interesting expressions and create access
1362 structures for them. Return true iff any access is created. */
1364 static bool
1365 scan_function (void)
1367 basic_block bb;
1368 bool ret = false;
1370 FOR_EACH_BB_FN (bb, cfun)
1372 gimple_stmt_iterator gsi;
1373 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1375 gimple *stmt = gsi_stmt (gsi);
1376 tree t;
1377 unsigned i;
1379 switch (gimple_code (stmt))
1381 case GIMPLE_RETURN:
1382 t = gimple_return_retval (as_a <greturn *> (stmt));
1383 if (t != NULL_TREE)
1384 ret |= build_access_from_expr (t, stmt, false);
1385 break;
1387 case GIMPLE_ASSIGN:
1388 ret |= build_accesses_from_assign (stmt);
1389 break;
1391 case GIMPLE_CALL:
1392 for (i = 0; i < gimple_call_num_args (stmt); i++)
1393 ret |= build_access_from_expr (gimple_call_arg (stmt, i),
1394 stmt, false);
1396 t = gimple_call_lhs (stmt);
1397 if (t && !disqualify_if_bad_bb_terminating_stmt (stmt, t, NULL))
1399 /* If the STMT is a call to DEFERRED_INIT, avoid setting
1400 cannot_scalarize_away_bitmap. */
1401 if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
1402 ret |= !!build_access_from_expr_1 (t, stmt, true);
1403 else
1404 ret |= build_access_from_expr (t, stmt, true);
1406 break;
1408 case GIMPLE_ASM:
1410 gasm *asm_stmt = as_a <gasm *> (stmt);
1411 walk_stmt_load_store_addr_ops (asm_stmt, NULL, NULL, NULL,
1412 asm_visit_addr);
1413 for (i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
1415 t = TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
1416 ret |= build_access_from_expr (t, asm_stmt, false);
1418 for (i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
1420 t = TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
1421 ret |= build_access_from_expr (t, asm_stmt, true);
1424 break;
1426 default:
1427 break;
1432 return ret;
1435 /* Helper of QSORT function. There are pointers to accesses in the array. An
1436 access is considered smaller than another if it has smaller offset or if the
1437 offsets are the same but is size is bigger. */
1439 static int
1440 compare_access_positions (const void *a, const void *b)
1442 const access_p *fp1 = (const access_p *) a;
1443 const access_p *fp2 = (const access_p *) b;
1444 const access_p f1 = *fp1;
1445 const access_p f2 = *fp2;
1447 if (f1->offset != f2->offset)
1448 return f1->offset < f2->offset ? -1 : 1;
1450 if (f1->size == f2->size)
1452 if (f1->type == f2->type)
1453 return 0;
1454 /* Put any non-aggregate type before any aggregate type. */
1455 else if (!is_gimple_reg_type (f1->type)
1456 && is_gimple_reg_type (f2->type))
1457 return 1;
1458 else if (is_gimple_reg_type (f1->type)
1459 && !is_gimple_reg_type (f2->type))
1460 return -1;
1461 /* Put any complex or vector type before any other scalar type. */
1462 else if (TREE_CODE (f1->type) != COMPLEX_TYPE
1463 && TREE_CODE (f1->type) != VECTOR_TYPE
1464 && (TREE_CODE (f2->type) == COMPLEX_TYPE
1465 || TREE_CODE (f2->type) == VECTOR_TYPE))
1466 return 1;
1467 else if ((TREE_CODE (f1->type) == COMPLEX_TYPE
1468 || TREE_CODE (f1->type) == VECTOR_TYPE)
1469 && TREE_CODE (f2->type) != COMPLEX_TYPE
1470 && TREE_CODE (f2->type) != VECTOR_TYPE)
1471 return -1;
1472 /* Put any integral type before any non-integral type. When splicing, we
1473 make sure that those with insufficient precision and occupying the
1474 same space are not scalarized. */
1475 else if (INTEGRAL_TYPE_P (f1->type)
1476 && !INTEGRAL_TYPE_P (f2->type))
1477 return -1;
1478 else if (!INTEGRAL_TYPE_P (f1->type)
1479 && INTEGRAL_TYPE_P (f2->type))
1480 return 1;
1481 /* Put the integral type with the bigger precision first. */
1482 else if (INTEGRAL_TYPE_P (f1->type)
1483 && INTEGRAL_TYPE_P (f2->type)
1484 && (TYPE_PRECISION (f2->type) != TYPE_PRECISION (f1->type)))
1485 return TYPE_PRECISION (f2->type) - TYPE_PRECISION (f1->type);
1486 /* Stabilize the sort. */
1487 return TYPE_UID (f1->type) - TYPE_UID (f2->type);
1490 /* We want the bigger accesses first, thus the opposite operator in the next
1491 line: */
1492 return f1->size > f2->size ? -1 : 1;
1496 /* Append a name of the declaration to the name obstack. A helper function for
1497 make_fancy_name. */
1499 static void
1500 make_fancy_decl_name (tree decl)
1502 char buffer[32];
1504 tree name = DECL_NAME (decl);
1505 if (name)
1506 obstack_grow (&name_obstack, IDENTIFIER_POINTER (name),
1507 IDENTIFIER_LENGTH (name));
1508 else
1510 sprintf (buffer, "D%u", DECL_UID (decl));
1511 obstack_grow (&name_obstack, buffer, strlen (buffer));
1515 /* Helper for make_fancy_name. */
1517 static void
1518 make_fancy_name_1 (tree expr)
1520 char buffer[32];
1521 tree index;
1523 if (DECL_P (expr))
1525 make_fancy_decl_name (expr);
1526 return;
1529 switch (TREE_CODE (expr))
1531 case COMPONENT_REF:
1532 make_fancy_name_1 (TREE_OPERAND (expr, 0));
1533 obstack_1grow (&name_obstack, '$');
1534 make_fancy_decl_name (TREE_OPERAND (expr, 1));
1535 break;
1537 case ARRAY_REF:
1538 make_fancy_name_1 (TREE_OPERAND (expr, 0));
1539 obstack_1grow (&name_obstack, '$');
1540 /* Arrays with only one element may not have a constant as their
1541 index. */
1542 index = TREE_OPERAND (expr, 1);
1543 if (TREE_CODE (index) != INTEGER_CST)
1544 break;
1545 sprintf (buffer, HOST_WIDE_INT_PRINT_DEC, TREE_INT_CST_LOW (index));
1546 obstack_grow (&name_obstack, buffer, strlen (buffer));
1547 break;
1549 case ADDR_EXPR:
1550 make_fancy_name_1 (TREE_OPERAND (expr, 0));
1551 break;
1553 case MEM_REF:
1554 make_fancy_name_1 (TREE_OPERAND (expr, 0));
1555 if (!integer_zerop (TREE_OPERAND (expr, 1)))
1557 obstack_1grow (&name_obstack, '$');
1558 sprintf (buffer, HOST_WIDE_INT_PRINT_DEC,
1559 TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)));
1560 obstack_grow (&name_obstack, buffer, strlen (buffer));
1562 break;
1564 case BIT_FIELD_REF:
1565 case REALPART_EXPR:
1566 case IMAGPART_EXPR:
1567 gcc_unreachable (); /* we treat these as scalars. */
1568 break;
1569 default:
1570 break;
1574 /* Create a human readable name for replacement variable of ACCESS. */
1576 static char *
1577 make_fancy_name (tree expr)
1579 make_fancy_name_1 (expr);
1580 obstack_1grow (&name_obstack, '\0');
1581 return XOBFINISH (&name_obstack, char *);
1584 /* Construct a MEM_REF that would reference a part of aggregate BASE of type
1585 EXP_TYPE at the given OFFSET and with storage order REVERSE. If BASE is
1586 something for which get_addr_base_and_unit_offset returns NULL, gsi must
1587 be non-NULL and is used to insert new statements either before or below
1588 the current one as specified by INSERT_AFTER. This function is not capable
1589 of handling bitfields. */
1591 tree
1592 build_ref_for_offset (location_t loc, tree base, poly_int64 offset,
1593 bool reverse, tree exp_type, gimple_stmt_iterator *gsi,
1594 bool insert_after)
1596 tree prev_base = base;
1597 tree off;
1598 tree mem_ref;
1599 poly_int64 base_offset;
1600 unsigned HOST_WIDE_INT misalign;
1601 unsigned int align;
1603 /* Preserve address-space information. */
1604 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (base));
1605 if (as != TYPE_ADDR_SPACE (exp_type))
1606 exp_type = build_qualified_type (exp_type,
1607 TYPE_QUALS (exp_type)
1608 | ENCODE_QUAL_ADDR_SPACE (as));
1610 poly_int64 byte_offset = exact_div (offset, BITS_PER_UNIT);
1611 get_object_alignment_1 (base, &align, &misalign);
1612 base = get_addr_base_and_unit_offset (base, &base_offset);
1614 /* get_addr_base_and_unit_offset returns NULL for references with a variable
1615 offset such as array[var_index]. */
1616 if (!base)
1618 gassign *stmt;
1619 tree tmp, addr;
1621 gcc_checking_assert (gsi);
1622 tmp = make_ssa_name (build_pointer_type (TREE_TYPE (prev_base)));
1623 addr = build_fold_addr_expr (unshare_expr (prev_base));
1624 STRIP_USELESS_TYPE_CONVERSION (addr);
1625 stmt = gimple_build_assign (tmp, addr);
1626 gimple_set_location (stmt, loc);
1627 if (insert_after)
1628 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
1629 else
1630 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1632 off = build_int_cst (reference_alias_ptr_type (prev_base), byte_offset);
1633 base = tmp;
1635 else if (TREE_CODE (base) == MEM_REF)
1637 off = build_int_cst (TREE_TYPE (TREE_OPERAND (base, 1)),
1638 base_offset + byte_offset);
1639 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1), off);
1640 base = unshare_expr (TREE_OPERAND (base, 0));
1642 else
1644 off = build_int_cst (reference_alias_ptr_type (prev_base),
1645 base_offset + byte_offset);
1646 base = build_fold_addr_expr (unshare_expr (base));
1649 unsigned int align_bound = known_alignment (misalign + offset);
1650 if (align_bound != 0)
1651 align = MIN (align, align_bound);
1652 if (align != TYPE_ALIGN (exp_type))
1653 exp_type = build_aligned_type (exp_type, align);
1655 mem_ref = fold_build2_loc (loc, MEM_REF, exp_type, base, off);
1656 REF_REVERSE_STORAGE_ORDER (mem_ref) = reverse;
1657 if (TREE_THIS_VOLATILE (prev_base))
1658 TREE_THIS_VOLATILE (mem_ref) = 1;
1659 if (TREE_SIDE_EFFECTS (prev_base))
1660 TREE_SIDE_EFFECTS (mem_ref) = 1;
1661 return mem_ref;
1664 /* Construct and return a memory reference that is equal to a portion of
1665 MODEL->expr but is based on BASE. If this cannot be done, return NULL. */
1667 static tree
1668 build_reconstructed_reference (location_t, tree base, struct access *model)
1670 tree expr = model->expr;
1671 /* We have to make sure to start just below the outermost union. */
1672 tree start_expr = expr;
1673 while (handled_component_p (expr))
1675 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == UNION_TYPE)
1676 start_expr = expr;
1677 expr = TREE_OPERAND (expr, 0);
1680 expr = start_expr;
1681 tree prev_expr = NULL_TREE;
1682 while (!types_compatible_p (TREE_TYPE (expr), TREE_TYPE (base)))
1684 if (!handled_component_p (expr))
1685 return NULL_TREE;
1686 prev_expr = expr;
1687 expr = TREE_OPERAND (expr, 0);
1690 /* Guard against broken VIEW_CONVERT_EXPRs... */
1691 if (!prev_expr)
1692 return NULL_TREE;
1694 TREE_OPERAND (prev_expr, 0) = base;
1695 tree ref = unshare_expr (model->expr);
1696 TREE_OPERAND (prev_expr, 0) = expr;
1697 return ref;
1700 /* Construct a memory reference to a part of an aggregate BASE at the given
1701 OFFSET and of the same type as MODEL. In case this is a reference to a
1702 bit-field, the function will replicate the last component_ref of model's
1703 expr to access it. GSI and INSERT_AFTER have the same meaning as in
1704 build_ref_for_offset. */
1706 static tree
1707 build_ref_for_model (location_t loc, tree base, HOST_WIDE_INT offset,
1708 struct access *model, gimple_stmt_iterator *gsi,
1709 bool insert_after)
1711 gcc_assert (offset >= 0);
1712 if (TREE_CODE (model->expr) == COMPONENT_REF
1713 && DECL_BIT_FIELD (TREE_OPERAND (model->expr, 1)))
1715 /* This access represents a bit-field. */
1716 tree t, exp_type, fld = TREE_OPERAND (model->expr, 1);
1718 offset -= int_bit_position (fld);
1719 exp_type = TREE_TYPE (TREE_OPERAND (model->expr, 0));
1720 t = build_ref_for_offset (loc, base, offset, model->reverse, exp_type,
1721 gsi, insert_after);
1722 /* The flag will be set on the record type. */
1723 REF_REVERSE_STORAGE_ORDER (t) = 0;
1724 return fold_build3_loc (loc, COMPONENT_REF, TREE_TYPE (fld), t, fld,
1725 NULL_TREE);
1727 else
1729 tree res;
1730 if (model->grp_same_access_path
1731 && !TREE_THIS_VOLATILE (base)
1732 && (TYPE_ADDR_SPACE (TREE_TYPE (base))
1733 == TYPE_ADDR_SPACE (TREE_TYPE (model->expr)))
1734 && offset <= model->offset
1735 /* build_reconstructed_reference can still fail if we have already
1736 massaged BASE because of another type incompatibility. */
1737 && (res = build_reconstructed_reference (loc, base, model)))
1738 return res;
1739 else
1740 return build_ref_for_offset (loc, base, offset, model->reverse,
1741 model->type, gsi, insert_after);
1745 /* Attempt to build a memory reference that we could but into a gimple
1746 debug_bind statement. Similar to build_ref_for_model but punts if it has to
1747 create statements and return s NULL instead. This function also ignores
1748 alignment issues and so its results should never end up in non-debug
1749 statements. */
1751 static tree
1752 build_debug_ref_for_model (location_t loc, tree base, HOST_WIDE_INT offset,
1753 struct access *model)
1755 poly_int64 base_offset;
1756 tree off;
1758 if (TREE_CODE (model->expr) == COMPONENT_REF
1759 && DECL_BIT_FIELD (TREE_OPERAND (model->expr, 1)))
1760 return NULL_TREE;
1762 base = get_addr_base_and_unit_offset (base, &base_offset);
1763 if (!base)
1764 return NULL_TREE;
1765 if (TREE_CODE (base) == MEM_REF)
1767 off = build_int_cst (TREE_TYPE (TREE_OPERAND (base, 1)),
1768 base_offset + offset / BITS_PER_UNIT);
1769 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1), off);
1770 base = unshare_expr (TREE_OPERAND (base, 0));
1772 else
1774 off = build_int_cst (reference_alias_ptr_type (base),
1775 base_offset + offset / BITS_PER_UNIT);
1776 base = build_fold_addr_expr (unshare_expr (base));
1779 return fold_build2_loc (loc, MEM_REF, model->type, base, off);
1782 /* Construct a memory reference consisting of component_refs and array_refs to
1783 a part of an aggregate *RES (which is of type TYPE). The requested part
1784 should have type EXP_TYPE at be the given OFFSET. This function might not
1785 succeed, it returns true when it does and only then *RES points to something
1786 meaningful. This function should be used only to build expressions that we
1787 might need to present to user (e.g. in warnings). In all other situations,
1788 build_ref_for_model or build_ref_for_offset should be used instead. */
1790 static bool
1791 build_user_friendly_ref_for_offset (tree *res, tree type, HOST_WIDE_INT offset,
1792 tree exp_type)
1794 while (1)
1796 tree fld;
1797 tree tr_size, index, minidx;
1798 HOST_WIDE_INT el_size;
1800 if (offset == 0 && exp_type
1801 && types_compatible_p (exp_type, type))
1802 return true;
1804 switch (TREE_CODE (type))
1806 case UNION_TYPE:
1807 case QUAL_UNION_TYPE:
1808 case RECORD_TYPE:
1809 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
1811 HOST_WIDE_INT pos, size;
1812 tree tr_pos, expr, *expr_ptr;
1814 if (TREE_CODE (fld) != FIELD_DECL)
1815 continue;
1817 tr_pos = bit_position (fld);
1818 if (!tr_pos || !tree_fits_uhwi_p (tr_pos))
1819 continue;
1820 pos = tree_to_uhwi (tr_pos);
1821 gcc_assert (TREE_CODE (type) == RECORD_TYPE || pos == 0);
1822 tr_size = DECL_SIZE (fld);
1823 if (!tr_size || !tree_fits_uhwi_p (tr_size))
1824 continue;
1825 size = tree_to_uhwi (tr_size);
1826 if (size == 0)
1828 if (pos != offset)
1829 continue;
1831 else if (pos > offset || (pos + size) <= offset)
1832 continue;
1834 expr = build3 (COMPONENT_REF, TREE_TYPE (fld), *res, fld,
1835 NULL_TREE);
1836 expr_ptr = &expr;
1837 if (build_user_friendly_ref_for_offset (expr_ptr, TREE_TYPE (fld),
1838 offset - pos, exp_type))
1840 *res = expr;
1841 return true;
1844 return false;
1846 case ARRAY_TYPE:
1847 tr_size = TYPE_SIZE (TREE_TYPE (type));
1848 if (!tr_size || !tree_fits_uhwi_p (tr_size))
1849 return false;
1850 el_size = tree_to_uhwi (tr_size);
1852 minidx = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
1853 if (TREE_CODE (minidx) != INTEGER_CST || el_size == 0)
1854 return false;
1855 index = build_int_cst (TYPE_DOMAIN (type), offset / el_size);
1856 if (!integer_zerop (minidx))
1857 index = int_const_binop (PLUS_EXPR, index, minidx);
1858 *res = build4 (ARRAY_REF, TREE_TYPE (type), *res, index,
1859 NULL_TREE, NULL_TREE);
1860 offset = offset % el_size;
1861 type = TREE_TYPE (type);
1862 break;
1864 default:
1865 if (offset != 0)
1866 return false;
1868 if (exp_type)
1869 return false;
1870 else
1871 return true;
1876 /* Print message to dump file why a variable was rejected. */
1878 static void
1879 reject (tree var, const char *msg)
1881 if (dump_file && (dump_flags & TDF_DETAILS))
1883 fprintf (dump_file, "Rejected (%d): %s: ", DECL_UID (var), msg);
1884 print_generic_expr (dump_file, var);
1885 fprintf (dump_file, "\n");
1889 /* Return true if VAR is a candidate for SRA. */
1891 static bool
1892 maybe_add_sra_candidate (tree var)
1894 tree type = TREE_TYPE (var);
1895 const char *msg;
1896 tree_node **slot;
1898 if (!AGGREGATE_TYPE_P (type))
1900 reject (var, "not aggregate");
1901 return false;
1903 /* Allow constant-pool entries that "need to live in memory". */
1904 if (needs_to_live_in_memory (var) && !constant_decl_p (var))
1906 reject (var, "needs to live in memory");
1907 return false;
1909 if (TREE_THIS_VOLATILE (var))
1911 reject (var, "is volatile");
1912 return false;
1914 if (!COMPLETE_TYPE_P (type))
1916 reject (var, "has incomplete type");
1917 return false;
1919 if (!tree_fits_shwi_p (TYPE_SIZE (type)))
1921 reject (var, "type size not fixed");
1922 return false;
1924 if (tree_to_shwi (TYPE_SIZE (type)) == 0)
1926 reject (var, "type size is zero");
1927 return false;
1929 if (type_internals_preclude_sra_p (type, &msg))
1931 reject (var, msg);
1932 return false;
1934 if (/* Fix for PR 41089. tree-stdarg.cc needs to have va_lists intact but
1935 we also want to schedule it rather late. Thus we ignore it in
1936 the early pass. */
1937 (sra_mode == SRA_MODE_EARLY_INTRA
1938 && is_va_list_type (type)))
1940 reject (var, "is va_list");
1941 return false;
1944 bitmap_set_bit (candidate_bitmap, DECL_UID (var));
1945 slot = candidates->find_slot_with_hash (var, DECL_UID (var), INSERT);
1946 *slot = var;
1948 if (dump_file && (dump_flags & TDF_DETAILS))
1950 fprintf (dump_file, "Candidate (%d): ", DECL_UID (var));
1951 print_generic_expr (dump_file, var);
1952 fprintf (dump_file, "\n");
1955 return true;
1958 /* The very first phase of intraprocedural SRA. It marks in candidate_bitmap
1959 those with type which is suitable for scalarization. */
1961 static bool
1962 find_var_candidates (void)
1964 tree var, parm;
1965 unsigned int i;
1966 bool ret = false;
1968 for (parm = DECL_ARGUMENTS (current_function_decl);
1969 parm;
1970 parm = DECL_CHAIN (parm))
1971 ret |= maybe_add_sra_candidate (parm);
1973 FOR_EACH_LOCAL_DECL (cfun, i, var)
1975 if (!VAR_P (var))
1976 continue;
1978 ret |= maybe_add_sra_candidate (var);
1981 return ret;
1984 /* Return true if EXP is a reference chain of COMPONENT_REFs and AREAY_REFs
1985 ending either with a DECL or a MEM_REF with zero offset. */
1987 static bool
1988 path_comparable_for_same_access (tree expr)
1990 while (handled_component_p (expr))
1992 if (TREE_CODE (expr) == ARRAY_REF)
1994 /* SSA name indices can occur here too when the array is of sie one.
1995 But we cannot just re-use array_refs with SSA names elsewhere in
1996 the function, so disallow non-constant indices. TODO: Remove this
1997 limitation after teaching build_reconstructed_reference to replace
1998 the index with the index type lower bound. */
1999 if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST)
2000 return false;
2002 expr = TREE_OPERAND (expr, 0);
2005 if (TREE_CODE (expr) == MEM_REF)
2007 if (!zerop (TREE_OPERAND (expr, 1)))
2008 return false;
2010 else
2011 gcc_assert (DECL_P (expr));
2013 return true;
2016 /* Assuming that EXP1 consists of only COMPONENT_REFs and ARRAY_REFs, return
2017 true if the chain of these handled components are exactly the same as EXP2
2018 and the expression under them is the same DECL or an equivalent MEM_REF.
2019 The reference picked by compare_access_positions must go to EXP1. */
2021 static bool
2022 same_access_path_p (tree exp1, tree exp2)
2024 if (TREE_CODE (exp1) != TREE_CODE (exp2))
2026 /* Special case single-field structures loaded sometimes as the field
2027 and sometimes as the structure. If the field is of a scalar type,
2028 compare_access_positions will put it into exp1.
2030 TODO: The gimple register type condition can be removed if teach
2031 compare_access_positions to put inner types first. */
2032 if (is_gimple_reg_type (TREE_TYPE (exp1))
2033 && TREE_CODE (exp1) == COMPONENT_REF
2034 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp1, 0)))
2035 == TYPE_MAIN_VARIANT (TREE_TYPE (exp2))))
2036 exp1 = TREE_OPERAND (exp1, 0);
2037 else
2038 return false;
2041 if (!operand_equal_p (exp1, exp2, OEP_ADDRESS_OF))
2042 return false;
2044 return true;
2047 /* Sort all accesses for the given variable, check for partial overlaps and
2048 return NULL if there are any. If there are none, pick a representative for
2049 each combination of offset and size and create a linked list out of them.
2050 Return the pointer to the first representative and make sure it is the first
2051 one in the vector of accesses. */
2053 static struct access *
2054 sort_and_splice_var_accesses (tree var)
2056 int i, j, access_count;
2057 struct access *res, **prev_acc_ptr = &res;
2058 vec<access_p> *access_vec;
2059 bool first = true;
2060 HOST_WIDE_INT low = -1, high = 0;
2062 access_vec = get_base_access_vector (var);
2063 if (!access_vec)
2064 return NULL;
2065 access_count = access_vec->length ();
2067 /* Sort by <OFFSET, SIZE>. */
2068 access_vec->qsort (compare_access_positions);
2070 i = 0;
2071 while (i < access_count)
2073 struct access *access = (*access_vec)[i];
2074 bool grp_write = access->write;
2075 bool grp_read = !access->write;
2076 bool grp_scalar_write = access->write
2077 && is_gimple_reg_type (access->type);
2078 bool grp_scalar_read = !access->write
2079 && is_gimple_reg_type (access->type);
2080 bool grp_assignment_read = access->grp_assignment_read;
2081 bool grp_assignment_write = access->grp_assignment_write;
2082 bool multiple_scalar_reads = false;
2083 bool grp_partial_lhs = access->grp_partial_lhs;
2084 bool first_scalar = is_gimple_reg_type (access->type);
2085 bool unscalarizable_region = access->grp_unscalarizable_region;
2086 bool grp_same_access_path = true;
2087 bool bf_non_full_precision
2088 = (INTEGRAL_TYPE_P (access->type)
2089 && TYPE_PRECISION (access->type) != access->size
2090 && TREE_CODE (access->expr) == COMPONENT_REF
2091 && DECL_BIT_FIELD (TREE_OPERAND (access->expr, 1)));
2093 if (first || access->offset >= high)
2095 first = false;
2096 low = access->offset;
2097 high = access->offset + access->size;
2099 else if (access->offset > low && access->offset + access->size > high)
2100 return NULL;
2101 else
2102 gcc_assert (access->offset >= low
2103 && access->offset + access->size <= high);
2105 grp_same_access_path = path_comparable_for_same_access (access->expr);
2107 j = i + 1;
2108 while (j < access_count)
2110 struct access *ac2 = (*access_vec)[j];
2111 if (ac2->offset != access->offset || ac2->size != access->size)
2112 break;
2113 if (ac2->write)
2115 grp_write = true;
2116 grp_scalar_write = (grp_scalar_write
2117 || is_gimple_reg_type (ac2->type));
2119 else
2121 grp_read = true;
2122 if (is_gimple_reg_type (ac2->type))
2124 if (grp_scalar_read)
2125 multiple_scalar_reads = true;
2126 else
2127 grp_scalar_read = true;
2130 grp_assignment_read |= ac2->grp_assignment_read;
2131 grp_assignment_write |= ac2->grp_assignment_write;
2132 grp_partial_lhs |= ac2->grp_partial_lhs;
2133 unscalarizable_region |= ac2->grp_unscalarizable_region;
2134 relink_to_new_repr (access, ac2);
2136 /* If there are both aggregate-type and scalar-type accesses with
2137 this combination of size and offset, the comparison function
2138 should have put the scalars first. */
2139 gcc_assert (first_scalar || !is_gimple_reg_type (ac2->type));
2140 /* It also prefers integral types to non-integral. However, when the
2141 precision of the selected type does not span the entire area and
2142 should also be used for a non-integer (i.e. float), we must not
2143 let that happen. Normally analyze_access_subtree expands the type
2144 to cover the entire area but for bit-fields it doesn't. */
2145 if (bf_non_full_precision && !INTEGRAL_TYPE_P (ac2->type))
2147 if (dump_file && (dump_flags & TDF_DETAILS))
2149 fprintf (dump_file, "Cannot scalarize the following access "
2150 "because insufficient precision integer type was "
2151 "selected.\n ");
2152 dump_access (dump_file, access, false);
2154 unscalarizable_region = true;
2157 if (grp_same_access_path
2158 && !same_access_path_p (access->expr, ac2->expr))
2159 grp_same_access_path = false;
2161 ac2->group_representative = access;
2162 j++;
2165 i = j;
2167 access->group_representative = access;
2168 access->grp_write = grp_write;
2169 access->grp_read = grp_read;
2170 access->grp_scalar_read = grp_scalar_read;
2171 access->grp_scalar_write = grp_scalar_write;
2172 access->grp_assignment_read = grp_assignment_read;
2173 access->grp_assignment_write = grp_assignment_write;
2174 access->grp_hint = multiple_scalar_reads && !constant_decl_p (var);
2175 access->grp_partial_lhs = grp_partial_lhs;
2176 access->grp_unscalarizable_region = unscalarizable_region;
2177 access->grp_same_access_path = grp_same_access_path;
2179 *prev_acc_ptr = access;
2180 prev_acc_ptr = &access->next_grp;
2183 gcc_assert (res == (*access_vec)[0]);
2184 return res;
2187 /* Create a variable for the given ACCESS which determines the type, name and a
2188 few other properties. Return the variable declaration and store it also to
2189 ACCESS->replacement. REG_TREE is used when creating a declaration to base a
2190 default-definition SSA name on in order to facilitate an uninitialized
2191 warning. It is used instead of the actual ACCESS type if that is not of a
2192 gimple register type. */
2194 static tree
2195 create_access_replacement (struct access *access, tree reg_type = NULL_TREE)
2197 tree repl;
2199 tree type = access->type;
2200 if (reg_type && !is_gimple_reg_type (type))
2201 type = reg_type;
2203 if (access->grp_to_be_debug_replaced)
2205 repl = create_tmp_var_raw (access->type);
2206 DECL_CONTEXT (repl) = current_function_decl;
2208 else
2209 /* Drop any special alignment on the type if it's not on the main
2210 variant. This avoids issues with weirdo ABIs like AAPCS. */
2211 repl = create_tmp_var (build_qualified_type (TYPE_MAIN_VARIANT (type),
2212 TYPE_QUALS (type)), "SR");
2213 if (access->grp_partial_lhs
2214 && is_gimple_reg_type (type))
2215 DECL_NOT_GIMPLE_REG_P (repl) = 1;
2217 DECL_SOURCE_LOCATION (repl) = DECL_SOURCE_LOCATION (access->base);
2218 DECL_ARTIFICIAL (repl) = 1;
2219 DECL_IGNORED_P (repl) = DECL_IGNORED_P (access->base);
2221 if (DECL_NAME (access->base)
2222 && !DECL_IGNORED_P (access->base)
2223 && !DECL_ARTIFICIAL (access->base))
2225 char *pretty_name = make_fancy_name (access->expr);
2226 tree debug_expr = unshare_expr_without_location (access->expr), d;
2227 bool fail = false;
2229 DECL_NAME (repl) = get_identifier (pretty_name);
2230 DECL_NAMELESS (repl) = 1;
2231 obstack_free (&name_obstack, pretty_name);
2233 /* Get rid of any SSA_NAMEs embedded in debug_expr,
2234 as DECL_DEBUG_EXPR isn't considered when looking for still
2235 used SSA_NAMEs and thus they could be freed. All debug info
2236 generation cares is whether something is constant or variable
2237 and that get_ref_base_and_extent works properly on the
2238 expression. It cannot handle accesses at a non-constant offset
2239 though, so just give up in those cases. */
2240 for (d = debug_expr;
2241 !fail && (handled_component_p (d) || TREE_CODE (d) == MEM_REF);
2242 d = TREE_OPERAND (d, 0))
2243 switch (TREE_CODE (d))
2245 case ARRAY_REF:
2246 case ARRAY_RANGE_REF:
2247 if (TREE_OPERAND (d, 1)
2248 && TREE_CODE (TREE_OPERAND (d, 1)) != INTEGER_CST)
2249 fail = true;
2250 if (TREE_OPERAND (d, 3)
2251 && TREE_CODE (TREE_OPERAND (d, 3)) != INTEGER_CST)
2252 fail = true;
2253 /* FALLTHRU */
2254 case COMPONENT_REF:
2255 if (TREE_OPERAND (d, 2)
2256 && TREE_CODE (TREE_OPERAND (d, 2)) != INTEGER_CST)
2257 fail = true;
2258 break;
2259 case MEM_REF:
2260 if (TREE_CODE (TREE_OPERAND (d, 0)) != ADDR_EXPR)
2261 fail = true;
2262 else
2263 d = TREE_OPERAND (d, 0);
2264 break;
2265 default:
2266 break;
2268 if (!fail)
2270 SET_DECL_DEBUG_EXPR (repl, debug_expr);
2271 DECL_HAS_DEBUG_EXPR_P (repl) = 1;
2273 if (access->grp_no_warning)
2274 suppress_warning (repl /* Be more selective! */);
2275 else
2276 copy_warning (repl, access->base);
2278 else
2279 suppress_warning (repl /* Be more selective! */);
2281 if (dump_file)
2283 if (access->grp_to_be_debug_replaced)
2285 fprintf (dump_file, "Created a debug-only replacement for ");
2286 print_generic_expr (dump_file, access->base);
2287 fprintf (dump_file, " offset: %u, size: %u\n",
2288 (unsigned) access->offset, (unsigned) access->size);
2290 else
2292 fprintf (dump_file, "Created a replacement for ");
2293 print_generic_expr (dump_file, access->base);
2294 fprintf (dump_file, " offset: %u, size: %u: ",
2295 (unsigned) access->offset, (unsigned) access->size);
2296 print_generic_expr (dump_file, repl, TDF_UID);
2297 fprintf (dump_file, "\n");
2300 sra_stats.replacements++;
2302 return repl;
2305 /* Return ACCESS scalar replacement, which must exist. */
2307 static inline tree
2308 get_access_replacement (struct access *access)
2310 gcc_checking_assert (access->replacement_decl);
2311 return access->replacement_decl;
2315 /* Build a subtree of accesses rooted in *ACCESS, and move the pointer in the
2316 linked list along the way. Stop when *ACCESS is NULL or the access pointed
2317 to it is not "within" the root. Return false iff some accesses partially
2318 overlap. */
2320 static bool
2321 build_access_subtree (struct access **access)
2323 struct access *root = *access, *last_child = NULL;
2324 HOST_WIDE_INT limit = root->offset + root->size;
2326 *access = (*access)->next_grp;
2327 while (*access && (*access)->offset + (*access)->size <= limit)
2329 if (!last_child)
2330 root->first_child = *access;
2331 else
2332 last_child->next_sibling = *access;
2333 last_child = *access;
2334 (*access)->parent = root;
2335 (*access)->grp_write |= root->grp_write;
2337 if (!build_access_subtree (access))
2338 return false;
2341 if (*access && (*access)->offset < limit)
2342 return false;
2344 return true;
2347 /* Build a tree of access representatives, ACCESS is the pointer to the first
2348 one, others are linked in a list by the next_grp field. Return false iff
2349 some accesses partially overlap. */
2351 static bool
2352 build_access_trees (struct access *access)
2354 while (access)
2356 struct access *root = access;
2358 if (!build_access_subtree (&access))
2359 return false;
2360 root->next_grp = access;
2362 return true;
2365 /* Traverse the access forest where ROOT is the first root and verify that
2366 various important invariants hold true. */
2368 DEBUG_FUNCTION void
2369 verify_sra_access_forest (struct access *root)
2371 struct access *access = root;
2372 tree first_base = root->base;
2373 gcc_assert (DECL_P (first_base));
2376 gcc_assert (access->base == first_base);
2377 if (access->parent)
2378 gcc_assert (access->offset >= access->parent->offset
2379 && access->size <= access->parent->size);
2380 if (access->next_sibling)
2381 gcc_assert (access->next_sibling->offset
2382 >= access->offset + access->size);
2384 poly_int64 poffset, psize, pmax_size;
2385 bool reverse;
2386 tree base = get_ref_base_and_extent (access->expr, &poffset, &psize,
2387 &pmax_size, &reverse);
2388 HOST_WIDE_INT offset, size, max_size;
2389 if (!poffset.is_constant (&offset)
2390 || !psize.is_constant (&size)
2391 || !pmax_size.is_constant (&max_size))
2392 gcc_unreachable ();
2393 gcc_assert (base == first_base);
2394 gcc_assert (offset == access->offset);
2395 gcc_assert (access->grp_unscalarizable_region
2396 || access->grp_total_scalarization
2397 || size == max_size);
2398 gcc_assert (access->grp_unscalarizable_region
2399 || !is_gimple_reg_type (access->type)
2400 || size == access->size);
2401 gcc_assert (reverse == access->reverse);
2403 if (access->first_child)
2405 gcc_assert (access->first_child->parent == access);
2406 access = access->first_child;
2408 else if (access->next_sibling)
2410 gcc_assert (access->next_sibling->parent == access->parent);
2411 access = access->next_sibling;
2413 else
2415 while (access->parent && !access->next_sibling)
2416 access = access->parent;
2417 if (access->next_sibling)
2418 access = access->next_sibling;
2419 else
2421 gcc_assert (access == root);
2422 root = root->next_grp;
2423 access = root;
2427 while (access);
2430 /* Verify access forests of all candidates with accesses by calling
2431 verify_access_forest on each on them. */
2433 DEBUG_FUNCTION void
2434 verify_all_sra_access_forests (void)
2436 bitmap_iterator bi;
2437 unsigned i;
2438 EXECUTE_IF_SET_IN_BITMAP (candidate_bitmap, 0, i, bi)
2440 tree var = candidate (i);
2441 struct access *access = get_first_repr_for_decl (var);
2442 if (access)
2444 gcc_assert (access->base == var);
2445 verify_sra_access_forest (access);
2450 /* Return true if expr contains some ARRAY_REFs into a variable bounded
2451 array. */
2453 static bool
2454 expr_with_var_bounded_array_refs_p (tree expr)
2456 while (handled_component_p (expr))
2458 if (TREE_CODE (expr) == ARRAY_REF
2459 && !tree_fits_shwi_p (array_ref_low_bound (expr)))
2460 return true;
2461 expr = TREE_OPERAND (expr, 0);
2463 return false;
2466 /* Analyze the subtree of accesses rooted in ROOT, scheduling replacements when
2467 both seeming beneficial and when ALLOW_REPLACEMENTS allows it. If TOTALLY
2468 is set, we are totally scalarizing the aggregate. Also set all sorts of
2469 access flags appropriately along the way, notably always set grp_read and
2470 grp_assign_read according to MARK_READ and grp_write when MARK_WRITE is
2471 true.
2473 Creating a replacement for a scalar access is considered beneficial if its
2474 grp_hint ot TOTALLY is set (this means either that there is more than one
2475 direct read access or that we are attempting total scalarization) or
2476 according to the following table:
2478 Access written to through a scalar type (once or more times)
2480 | Written to in an assignment statement
2482 | | Access read as scalar _once_
2483 | | |
2484 | | | Read in an assignment statement
2485 | | | |
2486 | | | | Scalarize Comment
2487 -----------------------------------------------------------------------------
2488 0 0 0 0 No access for the scalar
2489 0 0 0 1 No access for the scalar
2490 0 0 1 0 No Single read - won't help
2491 0 0 1 1 No The same case
2492 0 1 0 0 No access for the scalar
2493 0 1 0 1 No access for the scalar
2494 0 1 1 0 Yes s = *g; return s.i;
2495 0 1 1 1 Yes The same case as above
2496 1 0 0 0 No Won't help
2497 1 0 0 1 Yes s.i = 1; *g = s;
2498 1 0 1 0 Yes s.i = 5; g = s.i;
2499 1 0 1 1 Yes The same case as above
2500 1 1 0 0 No Won't help.
2501 1 1 0 1 Yes s.i = 1; *g = s;
2502 1 1 1 0 Yes s = *g; return s.i;
2503 1 1 1 1 Yes Any of the above yeses */
2505 static bool
2506 analyze_access_subtree (struct access *root, struct access *parent,
2507 bool allow_replacements, bool totally)
2509 struct access *child;
2510 HOST_WIDE_INT limit = root->offset + root->size;
2511 HOST_WIDE_INT covered_to = root->offset;
2512 bool scalar = is_gimple_reg_type (root->type);
2513 bool hole = false, sth_created = false;
2515 if (parent)
2517 if (parent->grp_read)
2518 root->grp_read = 1;
2519 if (parent->grp_assignment_read)
2520 root->grp_assignment_read = 1;
2521 if (parent->grp_write)
2522 root->grp_write = 1;
2523 if (parent->grp_assignment_write)
2524 root->grp_assignment_write = 1;
2525 if (!parent->grp_same_access_path)
2526 root->grp_same_access_path = 0;
2529 if (root->grp_unscalarizable_region)
2530 allow_replacements = false;
2532 if (allow_replacements && expr_with_var_bounded_array_refs_p (root->expr))
2533 allow_replacements = false;
2535 for (child = root->first_child; child; child = child->next_sibling)
2537 hole |= covered_to < child->offset;
2538 sth_created |= analyze_access_subtree (child, root,
2539 allow_replacements && !scalar,
2540 totally);
2542 root->grp_unscalarized_data |= child->grp_unscalarized_data;
2543 if (child->grp_covered)
2544 covered_to += child->size;
2545 else
2546 hole = true;
2549 if (allow_replacements && scalar && !root->first_child
2550 && (totally || !root->grp_total_scalarization)
2551 && (totally
2552 || root->grp_hint
2553 || ((root->grp_scalar_read || root->grp_assignment_read)
2554 && (root->grp_scalar_write || root->grp_assignment_write))))
2556 /* Always create access replacements that cover the whole access.
2557 For integral types this means the precision has to match.
2558 Avoid assumptions based on the integral type kind, too. */
2559 if (INTEGRAL_TYPE_P (root->type)
2560 && (TREE_CODE (root->type) != INTEGER_TYPE
2561 || TYPE_PRECISION (root->type) != root->size)
2562 /* But leave bitfield accesses alone. */
2563 && (TREE_CODE (root->expr) != COMPONENT_REF
2564 || !DECL_BIT_FIELD (TREE_OPERAND (root->expr, 1))))
2566 tree rt = root->type;
2567 gcc_assert ((root->offset % BITS_PER_UNIT) == 0
2568 && (root->size % BITS_PER_UNIT) == 0);
2569 root->type = build_nonstandard_integer_type (root->size,
2570 TYPE_UNSIGNED (rt));
2571 root->expr = build_ref_for_offset (UNKNOWN_LOCATION, root->base,
2572 root->offset, root->reverse,
2573 root->type, NULL, false);
2575 if (dump_file && (dump_flags & TDF_DETAILS))
2577 fprintf (dump_file, "Changing the type of a replacement for ");
2578 print_generic_expr (dump_file, root->base);
2579 fprintf (dump_file, " offset: %u, size: %u ",
2580 (unsigned) root->offset, (unsigned) root->size);
2581 fprintf (dump_file, " to an integer.\n");
2585 root->grp_to_be_replaced = 1;
2586 root->replacement_decl = create_access_replacement (root);
2587 sth_created = true;
2588 hole = false;
2590 else
2592 if (allow_replacements
2593 && scalar && !root->first_child
2594 && !root->grp_total_scalarization
2595 && (root->grp_scalar_write || root->grp_assignment_write)
2596 && !bitmap_bit_p (cannot_scalarize_away_bitmap,
2597 DECL_UID (root->base)))
2599 gcc_checking_assert (!root->grp_scalar_read
2600 && !root->grp_assignment_read);
2601 sth_created = true;
2602 if (MAY_HAVE_DEBUG_BIND_STMTS)
2604 root->grp_to_be_debug_replaced = 1;
2605 root->replacement_decl = create_access_replacement (root);
2609 if (covered_to < limit)
2610 hole = true;
2611 if (scalar || !allow_replacements)
2612 root->grp_total_scalarization = 0;
2615 if (!hole || totally)
2616 root->grp_covered = 1;
2617 else if (root->grp_write || comes_initialized_p (root->base))
2618 root->grp_unscalarized_data = 1; /* not covered and written to */
2619 return sth_created;
2622 /* Analyze all access trees linked by next_grp by the means of
2623 analyze_access_subtree. */
2624 static bool
2625 analyze_access_trees (struct access *access)
2627 bool ret = false;
2629 while (access)
2631 if (analyze_access_subtree (access, NULL, true,
2632 access->grp_total_scalarization))
2633 ret = true;
2634 access = access->next_grp;
2637 return ret;
2640 /* Return true iff a potential new child of ACC at offset OFFSET and with size
2641 SIZE would conflict with an already existing one. If exactly such a child
2642 already exists in ACC, store a pointer to it in EXACT_MATCH. */
2644 static bool
2645 child_would_conflict_in_acc (struct access *acc, HOST_WIDE_INT norm_offset,
2646 HOST_WIDE_INT size, struct access **exact_match)
2648 struct access *child;
2650 for (child = acc->first_child; child; child = child->next_sibling)
2652 if (child->offset == norm_offset && child->size == size)
2654 *exact_match = child;
2655 return true;
2658 if (child->offset < norm_offset + size
2659 && child->offset + child->size > norm_offset)
2660 return true;
2663 return false;
2666 /* Create a new child access of PARENT, with all properties just like MODEL
2667 except for its offset and with its grp_write false and grp_read true.
2668 Return the new access or NULL if it cannot be created. Note that this
2669 access is created long after all splicing and sorting, it's not located in
2670 any access vector and is automatically a representative of its group. Set
2671 the gpr_write flag of the new accesss if SET_GRP_WRITE is true. */
2673 static struct access *
2674 create_artificial_child_access (struct access *parent, struct access *model,
2675 HOST_WIDE_INT new_offset,
2676 bool set_grp_read, bool set_grp_write)
2678 struct access **child;
2679 tree expr = parent->base;
2681 gcc_assert (!model->grp_unscalarizable_region);
2683 struct access *access = access_pool.allocate ();
2684 memset (access, 0, sizeof (struct access));
2685 if (!build_user_friendly_ref_for_offset (&expr, TREE_TYPE (expr), new_offset,
2686 model->type))
2688 access->grp_no_warning = true;
2689 expr = build_ref_for_model (EXPR_LOCATION (parent->base), parent->base,
2690 new_offset, model, NULL, false);
2693 access->base = parent->base;
2694 access->expr = expr;
2695 access->offset = new_offset;
2696 access->size = model->size;
2697 access->type = model->type;
2698 access->parent = parent;
2699 access->grp_read = set_grp_read;
2700 access->grp_write = set_grp_write;
2701 access->reverse = model->reverse;
2703 child = &parent->first_child;
2704 while (*child && (*child)->offset < new_offset)
2705 child = &(*child)->next_sibling;
2707 access->next_sibling = *child;
2708 *child = access;
2710 return access;
2714 /* Beginning with ACCESS, traverse its whole access subtree and mark all
2715 sub-trees as written to. If any of them has not been marked so previously
2716 and has assignment links leading from it, re-enqueue it. */
2718 static void
2719 subtree_mark_written_and_rhs_enqueue (struct access *access)
2721 if (access->grp_write)
2722 return;
2723 access->grp_write = true;
2724 add_access_to_rhs_work_queue (access);
2726 struct access *child;
2727 for (child = access->first_child; child; child = child->next_sibling)
2728 subtree_mark_written_and_rhs_enqueue (child);
2731 /* If there is still budget to create a propagation access for DECL, return
2732 true and decrement the budget. Otherwise return false. */
2734 static bool
2735 budget_for_propagation_access (tree decl)
2737 unsigned b, *p = propagation_budget->get (decl);
2738 if (p)
2739 b = *p;
2740 else
2741 b = param_sra_max_propagations;
2743 if (b == 0)
2744 return false;
2745 b--;
2747 if (b == 0 && dump_file && (dump_flags & TDF_DETAILS))
2749 fprintf (dump_file, "The propagation budget of ");
2750 print_generic_expr (dump_file, decl);
2751 fprintf (dump_file, " (UID: %u) has been exhausted.\n", DECL_UID (decl));
2753 propagation_budget->put (decl, b);
2754 return true;
2757 /* Return true if ACC or any of its subaccesses has grp_child set. */
2759 static bool
2760 access_or_its_child_written (struct access *acc)
2762 if (acc->grp_write)
2763 return true;
2764 for (struct access *sub = acc->first_child; sub; sub = sub->next_sibling)
2765 if (access_or_its_child_written (sub))
2766 return true;
2767 return false;
2770 /* Propagate subaccesses and grp_write flags of RACC across an assignment link
2771 to LACC. Enqueue sub-accesses as necessary so that the write flag is
2772 propagated transitively. Return true if anything changed. Additionally, if
2773 RACC is a scalar access but LACC is not, change the type of the latter, if
2774 possible. */
2776 static bool
2777 propagate_subaccesses_from_rhs (struct access *lacc, struct access *racc)
2779 struct access *rchild;
2780 HOST_WIDE_INT norm_delta = lacc->offset - racc->offset;
2781 bool ret = false;
2783 /* IF the LHS is still not marked as being written to, we only need to do so
2784 if the RHS at this level actually was. */
2785 if (!lacc->grp_write)
2787 gcc_checking_assert (!comes_initialized_p (racc->base));
2788 if (racc->grp_write)
2790 subtree_mark_written_and_rhs_enqueue (lacc);
2791 ret = true;
2795 if (is_gimple_reg_type (lacc->type)
2796 || lacc->grp_unscalarizable_region
2797 || racc->grp_unscalarizable_region)
2799 if (!lacc->grp_write)
2801 ret = true;
2802 subtree_mark_written_and_rhs_enqueue (lacc);
2804 return ret;
2807 if (is_gimple_reg_type (racc->type))
2809 if (!lacc->grp_write)
2811 ret = true;
2812 subtree_mark_written_and_rhs_enqueue (lacc);
2814 if (!lacc->first_child && !racc->first_child)
2816 /* We are about to change the access type from aggregate to scalar,
2817 so we need to put the reverse flag onto the access, if any. */
2818 const bool reverse
2819 = TYPE_REVERSE_STORAGE_ORDER (lacc->type)
2820 && !POINTER_TYPE_P (racc->type)
2821 && !VECTOR_TYPE_P (racc->type);
2822 tree t = lacc->base;
2824 lacc->type = racc->type;
2825 if (build_user_friendly_ref_for_offset (&t, TREE_TYPE (t),
2826 lacc->offset, racc->type))
2828 lacc->expr = t;
2829 lacc->grp_same_access_path = true;
2831 else
2833 lacc->expr = build_ref_for_model (EXPR_LOCATION (lacc->base),
2834 lacc->base, lacc->offset,
2835 racc, NULL, false);
2836 if (TREE_CODE (lacc->expr) == MEM_REF)
2837 REF_REVERSE_STORAGE_ORDER (lacc->expr) = reverse;
2838 lacc->grp_no_warning = true;
2839 lacc->grp_same_access_path = false;
2841 lacc->reverse = reverse;
2843 return ret;
2846 for (rchild = racc->first_child; rchild; rchild = rchild->next_sibling)
2848 struct access *new_acc = NULL;
2849 HOST_WIDE_INT norm_offset = rchild->offset + norm_delta;
2851 if (child_would_conflict_in_acc (lacc, norm_offset, rchild->size,
2852 &new_acc))
2854 if (new_acc)
2856 if (!new_acc->grp_write && rchild->grp_write)
2858 gcc_assert (!lacc->grp_write);
2859 subtree_mark_written_and_rhs_enqueue (new_acc);
2860 ret = true;
2863 rchild->grp_hint = 1;
2864 new_acc->grp_hint |= new_acc->grp_read;
2865 if (rchild->first_child
2866 && propagate_subaccesses_from_rhs (new_acc, rchild))
2868 ret = 1;
2869 add_access_to_rhs_work_queue (new_acc);
2872 else
2874 if (!lacc->grp_write)
2876 ret = true;
2877 subtree_mark_written_and_rhs_enqueue (lacc);
2880 continue;
2883 if (rchild->grp_unscalarizable_region
2884 || !budget_for_propagation_access (lacc->base))
2886 if (!lacc->grp_write && access_or_its_child_written (rchild))
2888 ret = true;
2889 subtree_mark_written_and_rhs_enqueue (lacc);
2891 continue;
2894 rchild->grp_hint = 1;
2895 /* Because get_ref_base_and_extent always includes padding in size for
2896 accesses to DECLs but not necessarily for COMPONENT_REFs of the same
2897 type, we might be actually attempting to here to create a child of the
2898 same type as the parent. */
2899 if (!types_compatible_p (lacc->type, rchild->type))
2900 new_acc = create_artificial_child_access (lacc, rchild, norm_offset,
2901 false,
2902 (lacc->grp_write
2903 || rchild->grp_write));
2904 else
2905 new_acc = lacc;
2906 gcc_checking_assert (new_acc);
2907 if (racc->first_child)
2908 propagate_subaccesses_from_rhs (new_acc, rchild);
2910 add_access_to_rhs_work_queue (lacc);
2911 ret = true;
2914 return ret;
2917 /* Propagate subaccesses of LACC across an assignment link to RACC if they
2918 should inhibit total scalarization of the corresponding area. No flags are
2919 being propagated in the process. Return true if anything changed. */
2921 static bool
2922 propagate_subaccesses_from_lhs (struct access *lacc, struct access *racc)
2924 if (is_gimple_reg_type (racc->type)
2925 || lacc->grp_unscalarizable_region
2926 || racc->grp_unscalarizable_region)
2927 return false;
2929 /* TODO: Do we want set some new racc flag to stop potential total
2930 scalarization if lacc is a scalar access (and none fo the two have
2931 children)? */
2933 bool ret = false;
2934 HOST_WIDE_INT norm_delta = racc->offset - lacc->offset;
2935 for (struct access *lchild = lacc->first_child;
2936 lchild;
2937 lchild = lchild->next_sibling)
2939 struct access *matching_acc = NULL;
2940 HOST_WIDE_INT norm_offset = lchild->offset + norm_delta;
2942 if (lchild->grp_unscalarizable_region
2943 || child_would_conflict_in_acc (racc, norm_offset, lchild->size,
2944 &matching_acc)
2945 || !budget_for_propagation_access (racc->base))
2947 if (matching_acc
2948 && propagate_subaccesses_from_lhs (lchild, matching_acc))
2949 add_access_to_lhs_work_queue (matching_acc);
2950 continue;
2953 /* Because get_ref_base_and_extent always includes padding in size for
2954 accesses to DECLs but not necessarily for COMPONENT_REFs of the same
2955 type, we might be actually attempting to here to create a child of the
2956 same type as the parent. */
2957 if (!types_compatible_p (racc->type, lchild->type))
2959 struct access *new_acc
2960 = create_artificial_child_access (racc, lchild, norm_offset,
2961 true, false);
2962 propagate_subaccesses_from_lhs (lchild, new_acc);
2964 else
2965 propagate_subaccesses_from_lhs (lchild, racc);
2966 ret = true;
2968 return ret;
2971 /* Propagate all subaccesses across assignment links. */
2973 static void
2974 propagate_all_subaccesses (void)
2976 propagation_budget = new hash_map<tree, unsigned>;
2977 while (rhs_work_queue_head)
2979 struct access *racc = pop_access_from_rhs_work_queue ();
2980 struct assign_link *link;
2982 if (racc->group_representative)
2983 racc= racc->group_representative;
2984 gcc_assert (racc->first_rhs_link);
2986 for (link = racc->first_rhs_link; link; link = link->next_rhs)
2988 struct access *lacc = link->lacc;
2990 if (!bitmap_bit_p (candidate_bitmap, DECL_UID (lacc->base)))
2991 continue;
2992 lacc = lacc->group_representative;
2994 bool reque_parents = false;
2995 if (!bitmap_bit_p (candidate_bitmap, DECL_UID (racc->base)))
2997 if (!lacc->grp_write)
2999 subtree_mark_written_and_rhs_enqueue (lacc);
3000 reque_parents = true;
3003 else if (propagate_subaccesses_from_rhs (lacc, racc))
3004 reque_parents = true;
3006 if (reque_parents)
3009 add_access_to_rhs_work_queue (lacc);
3010 lacc = lacc->parent;
3012 while (lacc);
3016 while (lhs_work_queue_head)
3018 struct access *lacc = pop_access_from_lhs_work_queue ();
3019 struct assign_link *link;
3021 if (lacc->group_representative)
3022 lacc = lacc->group_representative;
3023 gcc_assert (lacc->first_lhs_link);
3025 if (!bitmap_bit_p (candidate_bitmap, DECL_UID (lacc->base)))
3026 continue;
3028 for (link = lacc->first_lhs_link; link; link = link->next_lhs)
3030 struct access *racc = link->racc;
3032 if (racc->group_representative)
3033 racc = racc->group_representative;
3034 if (!bitmap_bit_p (candidate_bitmap, DECL_UID (racc->base)))
3035 continue;
3036 if (propagate_subaccesses_from_lhs (lacc, racc))
3037 add_access_to_lhs_work_queue (racc);
3040 delete propagation_budget;
3043 /* Return true if the forest beginning with ROOT does not contain
3044 unscalarizable regions or non-byte aligned accesses. */
3046 static bool
3047 can_totally_scalarize_forest_p (struct access *root)
3049 struct access *access = root;
3052 if (access->grp_unscalarizable_region
3053 || (access->offset % BITS_PER_UNIT) != 0
3054 || (access->size % BITS_PER_UNIT) != 0
3055 || (is_gimple_reg_type (access->type)
3056 && access->first_child))
3057 return false;
3059 if (access->first_child)
3060 access = access->first_child;
3061 else if (access->next_sibling)
3062 access = access->next_sibling;
3063 else
3065 while (access->parent && !access->next_sibling)
3066 access = access->parent;
3067 if (access->next_sibling)
3068 access = access->next_sibling;
3069 else
3071 gcc_assert (access == root);
3072 root = root->next_grp;
3073 access = root;
3077 while (access);
3078 return true;
3081 /* Create and return an ACCESS in PARENT spanning from POS with SIZE, TYPE and
3082 reference EXPR for total scalarization purposes and mark it as such. Within
3083 the children of PARENT, link it in between PTR and NEXT_SIBLING. */
3085 static struct access *
3086 create_total_scalarization_access (struct access *parent, HOST_WIDE_INT pos,
3087 HOST_WIDE_INT size, tree type, tree expr,
3088 struct access **ptr,
3089 struct access *next_sibling)
3091 struct access *access = access_pool.allocate ();
3092 memset (access, 0, sizeof (struct access));
3093 access->base = parent->base;
3094 access->offset = pos;
3095 access->size = size;
3096 access->expr = expr;
3097 access->type = type;
3098 access->parent = parent;
3099 access->grp_write = parent->grp_write;
3100 access->grp_total_scalarization = 1;
3101 access->grp_hint = 1;
3102 access->grp_same_access_path = path_comparable_for_same_access (expr);
3103 access->reverse = reverse_storage_order_for_component_p (expr);
3105 access->next_sibling = next_sibling;
3106 *ptr = access;
3107 return access;
3110 /* Create and return an ACCESS in PARENT spanning from POS with SIZE, TYPE and
3111 reference EXPR for total scalarization purposes and mark it as such, link it
3112 at *PTR and reshape the tree so that those elements at *PTR and their
3113 siblings which fall within the part described by POS and SIZE are moved to
3114 be children of the new access. If a partial overlap is detected, return
3115 NULL. */
3117 static struct access *
3118 create_total_access_and_reshape (struct access *parent, HOST_WIDE_INT pos,
3119 HOST_WIDE_INT size, tree type, tree expr,
3120 struct access **ptr)
3122 struct access **p = ptr;
3124 while (*p && (*p)->offset < pos + size)
3126 if ((*p)->offset + (*p)->size > pos + size)
3127 return NULL;
3128 p = &(*p)->next_sibling;
3131 struct access *next_child = *ptr;
3132 struct access *new_acc
3133 = create_total_scalarization_access (parent, pos, size, type, expr,
3134 ptr, *p);
3135 if (p != ptr)
3137 new_acc->first_child = next_child;
3138 *p = NULL;
3139 for (struct access *a = next_child; a; a = a->next_sibling)
3140 a->parent = new_acc;
3142 return new_acc;
3145 static bool totally_scalarize_subtree (struct access *root);
3147 /* Return true if INNER is either the same type as OUTER or if it is the type
3148 of a record field in OUTER at offset zero, possibly in nested
3149 sub-records. */
3151 static bool
3152 access_and_field_type_match_p (tree outer, tree inner)
3154 if (TYPE_MAIN_VARIANT (outer) == TYPE_MAIN_VARIANT (inner))
3155 return true;
3156 if (TREE_CODE (outer) != RECORD_TYPE)
3157 return false;
3158 tree fld = TYPE_FIELDS (outer);
3159 while (fld)
3161 if (TREE_CODE (fld) == FIELD_DECL)
3163 if (!zerop (DECL_FIELD_OFFSET (fld)))
3164 return false;
3165 if (TYPE_MAIN_VARIANT (TREE_TYPE (fld)) == inner)
3166 return true;
3167 if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE)
3168 fld = TYPE_FIELDS (TREE_TYPE (fld));
3169 else
3170 return false;
3172 else
3173 fld = DECL_CHAIN (fld);
3175 return false;
3178 /* Return type of total_should_skip_creating_access indicating whether a total
3179 scalarization access for a field/element should be created, whether it
3180 already exists or whether the entire total scalarization has to fail. */
3182 enum total_sra_field_state {TOTAL_FLD_CREATE, TOTAL_FLD_DONE, TOTAL_FLD_FAILED};
3184 /* Do all the necessary steps in total scalarization when the given aggregate
3185 type has a TYPE at POS with the given SIZE should be put into PARENT and
3186 when we have processed all its siblings with smaller offsets up until and
3187 including LAST_SEEN_SIBLING (which can be NULL).
3189 If some further siblings are to be skipped, set *LAST_SEEN_SIBLING as
3190 appropriate. Return TOTAL_FLD_CREATE id the caller should carry on with
3191 creating a new access, TOTAL_FLD_DONE if access or accesses capable of
3192 representing the described part of the aggregate for the purposes of total
3193 scalarization already exist or TOTAL_FLD_FAILED if there is a problem which
3194 prevents total scalarization from happening at all. */
3196 static enum total_sra_field_state
3197 total_should_skip_creating_access (struct access *parent,
3198 struct access **last_seen_sibling,
3199 tree type, HOST_WIDE_INT pos,
3200 HOST_WIDE_INT size)
3202 struct access *next_child;
3203 if (!*last_seen_sibling)
3204 next_child = parent->first_child;
3205 else
3206 next_child = (*last_seen_sibling)->next_sibling;
3208 /* First, traverse the chain of siblings until it points to an access with
3209 offset at least equal to POS. Check all skipped accesses whether they
3210 span the POS boundary and if so, return with a failure. */
3211 while (next_child && next_child->offset < pos)
3213 if (next_child->offset + next_child->size > pos)
3214 return TOTAL_FLD_FAILED;
3215 *last_seen_sibling = next_child;
3216 next_child = next_child->next_sibling;
3219 /* Now check whether next_child has exactly the right POS and SIZE and if so,
3220 whether it can represent what we need and can be totally scalarized
3221 itself. */
3222 if (next_child && next_child->offset == pos
3223 && next_child->size == size)
3225 if (!is_gimple_reg_type (next_child->type)
3226 && (!access_and_field_type_match_p (type, next_child->type)
3227 || !totally_scalarize_subtree (next_child)))
3228 return TOTAL_FLD_FAILED;
3230 *last_seen_sibling = next_child;
3231 return TOTAL_FLD_DONE;
3234 /* If the child we're looking at would partially overlap, we just cannot
3235 totally scalarize. */
3236 if (next_child
3237 && next_child->offset < pos + size
3238 && next_child->offset + next_child->size > pos + size)
3239 return TOTAL_FLD_FAILED;
3241 if (is_gimple_reg_type (type))
3243 /* We don't scalarize accesses that are children of other scalar type
3244 accesses, so if we go on and create an access for a register type,
3245 there should not be any pre-existing children. There are rare cases
3246 where the requested type is a vector but we already have register
3247 accesses for all its elements which is equally good. Detect that
3248 situation or whether we need to bail out. */
3250 HOST_WIDE_INT covered = pos;
3251 bool skipping = false;
3252 while (next_child
3253 && next_child->offset + next_child->size <= pos + size)
3255 if (next_child->offset != covered
3256 || !is_gimple_reg_type (next_child->type))
3257 return TOTAL_FLD_FAILED;
3259 covered += next_child->size;
3260 *last_seen_sibling = next_child;
3261 next_child = next_child->next_sibling;
3262 skipping = true;
3265 if (skipping)
3267 if (covered != pos + size)
3268 return TOTAL_FLD_FAILED;
3269 else
3270 return TOTAL_FLD_DONE;
3274 return TOTAL_FLD_CREATE;
3277 /* Go over sub-tree rooted in ROOT and attempt to create scalar accesses
3278 spanning all uncovered areas covered by ROOT, return false if the attempt
3279 failed. All created accesses will have grp_unscalarizable_region set (and
3280 should be ignored if the function returns false). */
3282 static bool
3283 totally_scalarize_subtree (struct access *root)
3285 gcc_checking_assert (!root->grp_unscalarizable_region);
3286 gcc_checking_assert (!is_gimple_reg_type (root->type));
3288 struct access *last_seen_sibling = NULL;
3290 switch (TREE_CODE (root->type))
3292 case RECORD_TYPE:
3293 for (tree fld = TYPE_FIELDS (root->type); fld; fld = DECL_CHAIN (fld))
3294 if (TREE_CODE (fld) == FIELD_DECL)
3296 tree ft = TREE_TYPE (fld);
3297 HOST_WIDE_INT fsize = tree_to_uhwi (DECL_SIZE (fld));
3298 if (!fsize)
3299 continue;
3301 HOST_WIDE_INT pos = root->offset + int_bit_position (fld);
3302 if (pos + fsize > root->offset + root->size)
3303 return false;
3304 enum total_sra_field_state
3305 state = total_should_skip_creating_access (root,
3306 &last_seen_sibling,
3307 ft, pos, fsize);
3308 switch (state)
3310 case TOTAL_FLD_FAILED:
3311 return false;
3312 case TOTAL_FLD_DONE:
3313 continue;
3314 case TOTAL_FLD_CREATE:
3315 break;
3316 default:
3317 gcc_unreachable ();
3320 struct access **p = (last_seen_sibling
3321 ? &last_seen_sibling->next_sibling
3322 : &root->first_child);
3323 tree nref = build3 (COMPONENT_REF, ft, root->expr, fld, NULL_TREE);
3324 struct access *new_child
3325 = create_total_access_and_reshape (root, pos, fsize, ft, nref, p);
3326 if (!new_child)
3327 return false;
3329 if (!is_gimple_reg_type (ft)
3330 && !totally_scalarize_subtree (new_child))
3331 return false;
3332 last_seen_sibling = new_child;
3334 break;
3335 case ARRAY_TYPE:
3337 tree elemtype = TREE_TYPE (root->type);
3338 tree elem_size = TYPE_SIZE (elemtype);
3339 gcc_assert (elem_size && tree_fits_shwi_p (elem_size));
3340 HOST_WIDE_INT el_size = tree_to_shwi (elem_size);
3341 gcc_assert (el_size > 0);
3343 tree minidx = TYPE_MIN_VALUE (TYPE_DOMAIN (root->type));
3344 gcc_assert (TREE_CODE (minidx) == INTEGER_CST);
3345 tree maxidx = TYPE_MAX_VALUE (TYPE_DOMAIN (root->type));
3346 /* Skip (some) zero-length arrays; others have MAXIDX == MINIDX - 1. */
3347 if (!maxidx)
3348 goto out;
3349 gcc_assert (TREE_CODE (maxidx) == INTEGER_CST);
3350 tree domain = TYPE_DOMAIN (root->type);
3351 /* MINIDX and MAXIDX are inclusive, and must be interpreted in
3352 DOMAIN (e.g. signed int, whereas min/max may be size_int). */
3353 offset_int idx = wi::to_offset (minidx);
3354 offset_int max = wi::to_offset (maxidx);
3355 if (!TYPE_UNSIGNED (domain))
3357 idx = wi::sext (idx, TYPE_PRECISION (domain));
3358 max = wi::sext (max, TYPE_PRECISION (domain));
3360 for (HOST_WIDE_INT pos = root->offset;
3361 idx <= max;
3362 pos += el_size, ++idx)
3364 enum total_sra_field_state
3365 state = total_should_skip_creating_access (root,
3366 &last_seen_sibling,
3367 elemtype, pos,
3368 el_size);
3369 switch (state)
3371 case TOTAL_FLD_FAILED:
3372 return false;
3373 case TOTAL_FLD_DONE:
3374 continue;
3375 case TOTAL_FLD_CREATE:
3376 break;
3377 default:
3378 gcc_unreachable ();
3381 struct access **p = (last_seen_sibling
3382 ? &last_seen_sibling->next_sibling
3383 : &root->first_child);
3384 tree nref = build4 (ARRAY_REF, elemtype, root->expr,
3385 wide_int_to_tree (domain, idx),
3386 NULL_TREE, NULL_TREE);
3387 struct access *new_child
3388 = create_total_access_and_reshape (root, pos, el_size, elemtype,
3389 nref, p);
3390 if (!new_child)
3391 return false;
3393 if (!is_gimple_reg_type (elemtype)
3394 && !totally_scalarize_subtree (new_child))
3395 return false;
3396 last_seen_sibling = new_child;
3399 break;
3400 default:
3401 gcc_unreachable ();
3404 out:
3405 return true;
3408 /* Go through all accesses collected throughout the (intraprocedural) analysis
3409 stage, exclude overlapping ones, identify representatives and build trees
3410 out of them, making decisions about scalarization on the way. Return true
3411 iff there are any to-be-scalarized variables after this stage. */
3413 static bool
3414 analyze_all_variable_accesses (void)
3416 int res = 0;
3417 bitmap tmp = BITMAP_ALLOC (NULL);
3418 bitmap_iterator bi;
3419 unsigned i;
3421 bitmap_copy (tmp, candidate_bitmap);
3422 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
3424 tree var = candidate (i);
3425 struct access *access;
3427 access = sort_and_splice_var_accesses (var);
3428 if (!access || !build_access_trees (access))
3429 disqualify_candidate (var,
3430 "No or inhibitingly overlapping accesses.");
3433 propagate_all_subaccesses ();
3435 bool optimize_speed_p = !optimize_function_for_size_p (cfun);
3436 /* If the user didn't set PARAM_SRA_MAX_SCALARIZATION_SIZE_<...>,
3437 fall back to a target default. */
3438 unsigned HOST_WIDE_INT max_scalarization_size
3439 = get_move_ratio (optimize_speed_p) * UNITS_PER_WORD;
3441 if (optimize_speed_p)
3443 if (OPTION_SET_P (param_sra_max_scalarization_size_speed))
3444 max_scalarization_size = param_sra_max_scalarization_size_speed;
3446 else
3448 if (OPTION_SET_P (param_sra_max_scalarization_size_size))
3449 max_scalarization_size = param_sra_max_scalarization_size_size;
3451 max_scalarization_size *= BITS_PER_UNIT;
3453 EXECUTE_IF_SET_IN_BITMAP (candidate_bitmap, 0, i, bi)
3454 if (bitmap_bit_p (should_scalarize_away_bitmap, i)
3455 && !bitmap_bit_p (cannot_scalarize_away_bitmap, i))
3457 tree var = candidate (i);
3458 if (!VAR_P (var))
3459 continue;
3461 if (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (var))) > max_scalarization_size)
3463 if (dump_file && (dump_flags & TDF_DETAILS))
3465 fprintf (dump_file, "Too big to totally scalarize: ");
3466 print_generic_expr (dump_file, var);
3467 fprintf (dump_file, " (UID: %u)\n", DECL_UID (var));
3469 continue;
3472 bool all_types_ok = true;
3473 for (struct access *access = get_first_repr_for_decl (var);
3474 access;
3475 access = access->next_grp)
3476 if (!can_totally_scalarize_forest_p (access)
3477 || !scalarizable_type_p (access->type, constant_decl_p (var)))
3479 all_types_ok = false;
3480 break;
3482 if (!all_types_ok)
3483 continue;
3485 if (dump_file && (dump_flags & TDF_DETAILS))
3487 fprintf (dump_file, "Will attempt to totally scalarize ");
3488 print_generic_expr (dump_file, var);
3489 fprintf (dump_file, " (UID: %u): \n", DECL_UID (var));
3491 bool scalarized = true;
3492 for (struct access *access = get_first_repr_for_decl (var);
3493 access;
3494 access = access->next_grp)
3495 if (!is_gimple_reg_type (access->type)
3496 && !totally_scalarize_subtree (access))
3498 scalarized = false;
3499 break;
3502 if (scalarized)
3503 for (struct access *access = get_first_repr_for_decl (var);
3504 access;
3505 access = access->next_grp)
3506 access->grp_total_scalarization = true;
3509 if (flag_checking)
3510 verify_all_sra_access_forests ();
3512 bitmap_copy (tmp, candidate_bitmap);
3513 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
3515 tree var = candidate (i);
3516 struct access *access = get_first_repr_for_decl (var);
3518 if (analyze_access_trees (access))
3520 res++;
3521 if (dump_file && (dump_flags & TDF_DETAILS))
3523 fprintf (dump_file, "\nAccess trees for ");
3524 print_generic_expr (dump_file, var);
3525 fprintf (dump_file, " (UID: %u): \n", DECL_UID (var));
3526 dump_access_tree (dump_file, access);
3527 fprintf (dump_file, "\n");
3530 else
3531 disqualify_candidate (var, "No scalar replacements to be created.");
3534 BITMAP_FREE (tmp);
3536 if (res)
3538 statistics_counter_event (cfun, "Scalarized aggregates", res);
3539 return true;
3541 else
3542 return false;
3545 /* Generate statements copying scalar replacements of accesses within a subtree
3546 into or out of AGG. ACCESS, all its children, siblings and their children
3547 are to be processed. AGG is an aggregate type expression (can be a
3548 declaration but does not have to be, it can for example also be a mem_ref or
3549 a series of handled components). TOP_OFFSET is the offset of the processed
3550 subtree which has to be subtracted from offsets of individual accesses to
3551 get corresponding offsets for AGG. If CHUNK_SIZE is non-null, copy only
3552 replacements in the interval <start_offset, start_offset + chunk_size>,
3553 otherwise copy all. GSI is a statement iterator used to place the new
3554 statements. WRITE should be true when the statements should write from AGG
3555 to the replacement and false if vice versa. if INSERT_AFTER is true, new
3556 statements will be added after the current statement in GSI, they will be
3557 added before the statement otherwise. */
3559 static void
3560 generate_subtree_copies (struct access *access, tree agg,
3561 HOST_WIDE_INT top_offset,
3562 HOST_WIDE_INT start_offset, HOST_WIDE_INT chunk_size,
3563 gimple_stmt_iterator *gsi, bool write,
3564 bool insert_after, location_t loc)
3566 /* Never write anything into constant pool decls. See PR70602. */
3567 if (!write && constant_decl_p (agg))
3568 return;
3571 if (chunk_size && access->offset >= start_offset + chunk_size)
3572 return;
3574 if (access->grp_to_be_replaced
3575 && (chunk_size == 0
3576 || access->offset + access->size > start_offset))
3578 tree expr, repl = get_access_replacement (access);
3579 gassign *stmt;
3581 expr = build_ref_for_model (loc, agg, access->offset - top_offset,
3582 access, gsi, insert_after);
3584 if (write)
3586 if (access->grp_partial_lhs)
3587 expr = force_gimple_operand_gsi (gsi, expr, true, NULL_TREE,
3588 !insert_after,
3589 insert_after ? GSI_NEW_STMT
3590 : GSI_SAME_STMT);
3591 stmt = gimple_build_assign (repl, expr);
3593 else
3595 suppress_warning (repl /* Be more selective! */);
3596 if (access->grp_partial_lhs)
3597 repl = force_gimple_operand_gsi (gsi, repl, true, NULL_TREE,
3598 !insert_after,
3599 insert_after ? GSI_NEW_STMT
3600 : GSI_SAME_STMT);
3601 stmt = gimple_build_assign (expr, repl);
3603 gimple_set_location (stmt, loc);
3605 if (insert_after)
3606 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
3607 else
3608 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
3609 update_stmt (stmt);
3610 sra_stats.subtree_copies++;
3612 else if (write
3613 && access->grp_to_be_debug_replaced
3614 && (chunk_size == 0
3615 || access->offset + access->size > start_offset))
3617 gdebug *ds;
3618 tree drhs = build_debug_ref_for_model (loc, agg,
3619 access->offset - top_offset,
3620 access);
3621 ds = gimple_build_debug_bind (get_access_replacement (access),
3622 drhs, gsi_stmt (*gsi));
3623 if (insert_after)
3624 gsi_insert_after (gsi, ds, GSI_NEW_STMT);
3625 else
3626 gsi_insert_before (gsi, ds, GSI_SAME_STMT);
3629 if (access->first_child)
3630 generate_subtree_copies (access->first_child, agg, top_offset,
3631 start_offset, chunk_size, gsi,
3632 write, insert_after, loc);
3634 access = access->next_sibling;
3636 while (access);
3639 /* Assign zero to all scalar replacements in an access subtree. ACCESS is the
3640 root of the subtree to be processed. GSI is the statement iterator used
3641 for inserting statements which are added after the current statement if
3642 INSERT_AFTER is true or before it otherwise. */
3644 static void
3645 init_subtree_with_zero (struct access *access, gimple_stmt_iterator *gsi,
3646 bool insert_after, location_t loc)
3649 struct access *child;
3651 if (access->grp_to_be_replaced)
3653 gassign *stmt;
3655 stmt = gimple_build_assign (get_access_replacement (access),
3656 build_zero_cst (access->type));
3657 if (insert_after)
3658 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
3659 else
3660 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
3661 update_stmt (stmt);
3662 gimple_set_location (stmt, loc);
3664 else if (access->grp_to_be_debug_replaced)
3666 gdebug *ds
3667 = gimple_build_debug_bind (get_access_replacement (access),
3668 build_zero_cst (access->type),
3669 gsi_stmt (*gsi));
3670 if (insert_after)
3671 gsi_insert_after (gsi, ds, GSI_NEW_STMT);
3672 else
3673 gsi_insert_before (gsi, ds, GSI_SAME_STMT);
3676 for (child = access->first_child; child; child = child->next_sibling)
3677 init_subtree_with_zero (child, gsi, insert_after, loc);
3680 /* Clobber all scalar replacements in an access subtree. ACCESS is the
3681 root of the subtree to be processed. GSI is the statement iterator used
3682 for inserting statements which are added after the current statement if
3683 INSERT_AFTER is true or before it otherwise. */
3685 static void
3686 clobber_subtree (struct access *access, gimple_stmt_iterator *gsi,
3687 bool insert_after, location_t loc)
3690 struct access *child;
3692 if (access->grp_to_be_replaced)
3694 tree rep = get_access_replacement (access);
3695 tree clobber = build_clobber (access->type);
3696 gimple *stmt = gimple_build_assign (rep, clobber);
3698 if (insert_after)
3699 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
3700 else
3701 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
3702 update_stmt (stmt);
3703 gimple_set_location (stmt, loc);
3706 for (child = access->first_child; child; child = child->next_sibling)
3707 clobber_subtree (child, gsi, insert_after, loc);
3710 /* Search for an access representative for the given expression EXPR and
3711 return it or NULL if it cannot be found. */
3713 static struct access *
3714 get_access_for_expr (tree expr)
3716 poly_int64 poffset, psize, pmax_size;
3717 HOST_WIDE_INT offset, max_size;
3718 tree base;
3719 bool reverse;
3721 /* FIXME: This should not be necessary but Ada produces V_C_Es with a type of
3722 a different size than the size of its argument and we need the latter
3723 one. */
3724 if (TREE_CODE (expr) == VIEW_CONVERT_EXPR)
3725 expr = TREE_OPERAND (expr, 0);
3727 base = get_ref_base_and_extent (expr, &poffset, &psize, &pmax_size,
3728 &reverse);
3729 if (!known_size_p (pmax_size)
3730 || !pmax_size.is_constant (&max_size)
3731 || !poffset.is_constant (&offset)
3732 || !DECL_P (base))
3733 return NULL;
3735 if (tree basesize = DECL_SIZE (base))
3737 poly_int64 sz;
3738 if (offset < 0
3739 || !poly_int_tree_p (basesize, &sz)
3740 || known_le (sz, offset))
3741 return NULL;
3744 if (max_size == 0
3745 || !bitmap_bit_p (candidate_bitmap, DECL_UID (base)))
3746 return NULL;
3748 return get_var_base_offset_size_access (base, offset, max_size);
3751 /* Replace the expression EXPR with a scalar replacement if there is one and
3752 generate other statements to do type conversion or subtree copying if
3753 necessary. GSI is used to place newly created statements, WRITE is true if
3754 the expression is being written to (it is on a LHS of a statement or output
3755 in an assembly statement). */
3757 static bool
3758 sra_modify_expr (tree *expr, gimple_stmt_iterator *gsi, bool write)
3760 location_t loc;
3761 struct access *access;
3762 tree type, bfr, orig_expr;
3763 bool partial_cplx_access = false;
3765 if (TREE_CODE (*expr) == BIT_FIELD_REF)
3767 bfr = *expr;
3768 expr = &TREE_OPERAND (*expr, 0);
3770 else
3771 bfr = NULL_TREE;
3773 if (TREE_CODE (*expr) == REALPART_EXPR || TREE_CODE (*expr) == IMAGPART_EXPR)
3775 expr = &TREE_OPERAND (*expr, 0);
3776 partial_cplx_access = true;
3778 access = get_access_for_expr (*expr);
3779 if (!access)
3780 return false;
3781 type = TREE_TYPE (*expr);
3782 orig_expr = *expr;
3784 loc = gimple_location (gsi_stmt (*gsi));
3785 gimple_stmt_iterator alt_gsi = gsi_none ();
3786 if (write && stmt_ends_bb_p (gsi_stmt (*gsi)))
3788 alt_gsi = gsi_start_edge (single_non_eh_succ (gsi_bb (*gsi)));
3789 gsi = &alt_gsi;
3792 if (access->grp_to_be_replaced)
3794 tree repl = get_access_replacement (access);
3795 /* If we replace a non-register typed access simply use the original
3796 access expression to extract the scalar component afterwards.
3797 This happens if scalarizing a function return value or parameter
3798 like in gcc.c-torture/execute/20041124-1.c, 20050316-1.c and
3799 gcc.c-torture/compile/20011217-1.c.
3801 We also want to use this when accessing a complex or vector which can
3802 be accessed as a different type too, potentially creating a need for
3803 type conversion (see PR42196) and when scalarized unions are involved
3804 in assembler statements (see PR42398). */
3805 if (!bfr && !useless_type_conversion_p (type, access->type))
3807 tree ref;
3809 ref = build_ref_for_model (loc, orig_expr, 0, access, gsi, false);
3811 if (partial_cplx_access)
3813 /* VIEW_CONVERT_EXPRs in partial complex access are always fine in
3814 the case of a write because in such case the replacement cannot
3815 be a gimple register. In the case of a load, we have to
3816 differentiate in between a register an non-register
3817 replacement. */
3818 tree t = build1 (VIEW_CONVERT_EXPR, type, repl);
3819 gcc_checking_assert (!write || access->grp_partial_lhs);
3820 if (!access->grp_partial_lhs)
3822 tree tmp = make_ssa_name (type);
3823 gassign *stmt = gimple_build_assign (tmp, t);
3824 /* This is always a read. */
3825 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
3826 t = tmp;
3828 *expr = t;
3830 else if (write)
3832 gassign *stmt;
3834 if (access->grp_partial_lhs)
3835 ref = force_gimple_operand_gsi (gsi, ref, true, NULL_TREE,
3836 false, GSI_NEW_STMT);
3837 stmt = gimple_build_assign (repl, ref);
3838 gimple_set_location (stmt, loc);
3839 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
3841 else
3843 gassign *stmt;
3845 if (access->grp_partial_lhs)
3846 repl = force_gimple_operand_gsi (gsi, repl, true, NULL_TREE,
3847 true, GSI_SAME_STMT);
3848 stmt = gimple_build_assign (ref, repl);
3849 gimple_set_location (stmt, loc);
3850 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
3853 else
3854 *expr = repl;
3855 sra_stats.exprs++;
3857 else if (write && access->grp_to_be_debug_replaced)
3859 gdebug *ds = gimple_build_debug_bind (get_access_replacement (access),
3860 NULL_TREE,
3861 gsi_stmt (*gsi));
3862 gsi_insert_after (gsi, ds, GSI_NEW_STMT);
3865 if (access->first_child && !TREE_READONLY (access->base))
3867 HOST_WIDE_INT start_offset, chunk_size;
3868 if (bfr
3869 && tree_fits_uhwi_p (TREE_OPERAND (bfr, 1))
3870 && tree_fits_uhwi_p (TREE_OPERAND (bfr, 2)))
3872 chunk_size = tree_to_uhwi (TREE_OPERAND (bfr, 1));
3873 start_offset = access->offset
3874 + tree_to_uhwi (TREE_OPERAND (bfr, 2));
3876 else
3877 start_offset = chunk_size = 0;
3879 generate_subtree_copies (access->first_child, orig_expr, access->offset,
3880 start_offset, chunk_size, gsi, write, write,
3881 loc);
3883 return true;
3886 /* Where scalar replacements of the RHS have been written to when a replacement
3887 of a LHS of an assigments cannot be direclty loaded from a replacement of
3888 the RHS. */
3889 enum unscalarized_data_handling { SRA_UDH_NONE, /* Nothing done so far. */
3890 SRA_UDH_RIGHT, /* Data flushed to the RHS. */
3891 SRA_UDH_LEFT }; /* Data flushed to the LHS. */
3893 struct subreplacement_assignment_data
3895 /* Offset of the access representing the lhs of the assignment. */
3896 HOST_WIDE_INT left_offset;
3898 /* LHS and RHS of the original assignment. */
3899 tree assignment_lhs, assignment_rhs;
3901 /* Access representing the rhs of the whole assignment. */
3902 struct access *top_racc;
3904 /* Stmt iterator used for statement insertions after the original assignment.
3905 It points to the main GSI used to traverse a BB during function body
3906 modification. */
3907 gimple_stmt_iterator *new_gsi;
3909 /* Stmt iterator used for statement insertions before the original
3910 assignment. Keeps on pointing to the original statement. */
3911 gimple_stmt_iterator old_gsi;
3913 /* Location of the assignment. */
3914 location_t loc;
3916 /* Keeps the information whether we have needed to refresh replacements of
3917 the LHS and from which side of the assignments this takes place. */
3918 enum unscalarized_data_handling refreshed;
3921 /* Store all replacements in the access tree rooted in TOP_RACC either to their
3922 base aggregate if there are unscalarized data or directly to LHS of the
3923 statement that is pointed to by GSI otherwise. */
3925 static void
3926 handle_unscalarized_data_in_subtree (struct subreplacement_assignment_data *sad)
3928 tree src;
3929 /* If the RHS is a load from a constant, we do not need to (and must not)
3930 flush replacements to it and can use it directly as if we did. */
3931 if (TREE_READONLY (sad->top_racc->base))
3933 sad->refreshed = SRA_UDH_RIGHT;
3934 return;
3936 if (sad->top_racc->grp_unscalarized_data)
3938 src = sad->assignment_rhs;
3939 sad->refreshed = SRA_UDH_RIGHT;
3941 else
3943 src = sad->assignment_lhs;
3944 sad->refreshed = SRA_UDH_LEFT;
3946 generate_subtree_copies (sad->top_racc->first_child, src,
3947 sad->top_racc->offset, 0, 0,
3948 &sad->old_gsi, false, false, sad->loc);
3951 /* Try to generate statements to load all sub-replacements in an access subtree
3952 formed by children of LACC from scalar replacements in the SAD->top_racc
3953 subtree. If that is not possible, refresh the SAD->top_racc base aggregate
3954 and load the accesses from it. */
3956 static void
3957 load_assign_lhs_subreplacements (struct access *lacc,
3958 struct subreplacement_assignment_data *sad)
3960 for (lacc = lacc->first_child; lacc; lacc = lacc->next_sibling)
3962 HOST_WIDE_INT offset;
3963 offset = lacc->offset - sad->left_offset + sad->top_racc->offset;
3965 if (lacc->grp_to_be_replaced)
3967 struct access *racc;
3968 gassign *stmt;
3969 tree rhs;
3971 racc = find_access_in_subtree (sad->top_racc, offset, lacc->size);
3972 if (racc && racc->grp_to_be_replaced)
3974 rhs = get_access_replacement (racc);
3975 if (!useless_type_conversion_p (lacc->type, racc->type))
3976 rhs = fold_build1_loc (sad->loc, VIEW_CONVERT_EXPR,
3977 lacc->type, rhs);
3979 if (racc->grp_partial_lhs && lacc->grp_partial_lhs)
3980 rhs = force_gimple_operand_gsi (&sad->old_gsi, rhs, true,
3981 NULL_TREE, true, GSI_SAME_STMT);
3983 else
3985 /* No suitable access on the right hand side, need to load from
3986 the aggregate. See if we have to update it first... */
3987 if (sad->refreshed == SRA_UDH_NONE)
3988 handle_unscalarized_data_in_subtree (sad);
3990 if (sad->refreshed == SRA_UDH_LEFT)
3991 rhs = build_ref_for_model (sad->loc, sad->assignment_lhs,
3992 lacc->offset - sad->left_offset,
3993 lacc, sad->new_gsi, true);
3994 else
3995 rhs = build_ref_for_model (sad->loc, sad->assignment_rhs,
3996 lacc->offset - sad->left_offset,
3997 lacc, sad->new_gsi, true);
3998 if (lacc->grp_partial_lhs)
3999 rhs = force_gimple_operand_gsi (sad->new_gsi,
4000 rhs, true, NULL_TREE,
4001 false, GSI_NEW_STMT);
4004 stmt = gimple_build_assign (get_access_replacement (lacc), rhs);
4005 gsi_insert_after (sad->new_gsi, stmt, GSI_NEW_STMT);
4006 gimple_set_location (stmt, sad->loc);
4007 update_stmt (stmt);
4008 sra_stats.subreplacements++;
4010 else
4012 if (sad->refreshed == SRA_UDH_NONE
4013 && lacc->grp_read && !lacc->grp_covered)
4014 handle_unscalarized_data_in_subtree (sad);
4016 if (lacc && lacc->grp_to_be_debug_replaced)
4018 gdebug *ds;
4019 tree drhs;
4020 struct access *racc = find_access_in_subtree (sad->top_racc,
4021 offset,
4022 lacc->size);
4024 if (racc && racc->grp_to_be_replaced)
4026 if (racc->grp_write || constant_decl_p (racc->base))
4027 drhs = get_access_replacement (racc);
4028 else
4029 drhs = NULL;
4031 else if (sad->refreshed == SRA_UDH_LEFT)
4032 drhs = build_debug_ref_for_model (sad->loc, lacc->base,
4033 lacc->offset, lacc);
4034 else if (sad->refreshed == SRA_UDH_RIGHT)
4035 drhs = build_debug_ref_for_model (sad->loc, sad->top_racc->base,
4036 offset, lacc);
4037 else
4038 drhs = NULL_TREE;
4039 if (drhs
4040 && !useless_type_conversion_p (lacc->type, TREE_TYPE (drhs)))
4041 drhs = fold_build1_loc (sad->loc, VIEW_CONVERT_EXPR,
4042 lacc->type, drhs);
4043 ds = gimple_build_debug_bind (get_access_replacement (lacc),
4044 drhs, gsi_stmt (sad->old_gsi));
4045 gsi_insert_after (sad->new_gsi, ds, GSI_NEW_STMT);
4049 if (lacc->first_child)
4050 load_assign_lhs_subreplacements (lacc, sad);
4054 /* Result code for SRA assignment modification. */
4055 enum assignment_mod_result { SRA_AM_NONE, /* nothing done for the stmt */
4056 SRA_AM_MODIFIED, /* stmt changed but not
4057 removed */
4058 SRA_AM_REMOVED }; /* stmt eliminated */
4060 /* Modify assignments with a CONSTRUCTOR on their RHS. STMT contains a pointer
4061 to the assignment and GSI is the statement iterator pointing at it. Returns
4062 the same values as sra_modify_assign. */
4064 static enum assignment_mod_result
4065 sra_modify_constructor_assign (gimple *stmt, gimple_stmt_iterator *gsi)
4067 tree lhs = gimple_assign_lhs (stmt);
4068 struct access *acc = get_access_for_expr (lhs);
4069 if (!acc)
4070 return SRA_AM_NONE;
4071 location_t loc = gimple_location (stmt);
4073 if (gimple_clobber_p (stmt))
4075 /* Clobber the replacement variable. */
4076 clobber_subtree (acc, gsi, !acc->grp_covered, loc);
4077 /* Remove clobbers of fully scalarized variables, they are dead. */
4078 if (acc->grp_covered)
4080 unlink_stmt_vdef (stmt);
4081 gsi_remove (gsi, true);
4082 release_defs (stmt);
4083 return SRA_AM_REMOVED;
4085 else
4086 return SRA_AM_MODIFIED;
4089 if (CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt)) > 0)
4091 /* I have never seen this code path trigger but if it can happen the
4092 following should handle it gracefully. */
4093 if (access_has_children_p (acc))
4094 generate_subtree_copies (acc->first_child, lhs, acc->offset, 0, 0, gsi,
4095 true, true, loc);
4096 return SRA_AM_MODIFIED;
4099 if (acc->grp_covered)
4101 init_subtree_with_zero (acc, gsi, false, loc);
4102 unlink_stmt_vdef (stmt);
4103 gsi_remove (gsi, true);
4104 release_defs (stmt);
4105 return SRA_AM_REMOVED;
4107 else
4109 init_subtree_with_zero (acc, gsi, true, loc);
4110 return SRA_AM_MODIFIED;
4114 /* Create and return a new suitable default definition SSA_NAME for RACC which
4115 is an access describing an uninitialized part of an aggregate that is being
4116 loaded. REG_TREE is used instead of the actual RACC type if that is not of
4117 a gimple register type. */
4119 static tree
4120 get_repl_default_def_ssa_name (struct access *racc, tree reg_type)
4122 gcc_checking_assert (!racc->grp_to_be_replaced
4123 && !racc->grp_to_be_debug_replaced);
4124 if (!racc->replacement_decl)
4125 racc->replacement_decl = create_access_replacement (racc, reg_type);
4126 return get_or_create_ssa_default_def (cfun, racc->replacement_decl);
4130 /* Generate statements to call .DEFERRED_INIT to initialize scalar replacements
4131 of accesses within a subtree ACCESS; all its children, siblings and their
4132 children are to be processed.
4133 GSI is a statement iterator used to place the new statements. */
4134 static void
4135 generate_subtree_deferred_init (struct access *access,
4136 tree init_type,
4137 tree decl_name,
4138 gimple_stmt_iterator *gsi,
4139 location_t loc)
4143 if (access->grp_to_be_replaced)
4145 tree repl = get_access_replacement (access);
4146 gimple *call
4147 = gimple_build_call_internal (IFN_DEFERRED_INIT, 3,
4148 TYPE_SIZE_UNIT (TREE_TYPE (repl)),
4149 init_type, decl_name);
4150 gimple_call_set_lhs (call, repl);
4151 gsi_insert_before (gsi, call, GSI_SAME_STMT);
4152 update_stmt (call);
4153 gimple_set_location (call, loc);
4154 sra_stats.subtree_deferred_init++;
4156 if (access->first_child)
4157 generate_subtree_deferred_init (access->first_child, init_type,
4158 decl_name, gsi, loc);
4160 access = access ->next_sibling;
4162 while (access);
4165 /* For a call to .DEFERRED_INIT:
4166 var = .DEFERRED_INIT (size_of_var, init_type, name_of_var);
4167 examine the LHS variable VAR and replace it with a scalar replacement if
4168 there is one, also replace the RHS call to a call to .DEFERRED_INIT of
4169 the corresponding scalar relacement variable. Examine the subtree and
4170 do the scalar replacements in the subtree too. STMT is the call, GSI is
4171 the statment iterator to place newly created statement. */
4173 static enum assignment_mod_result
4174 sra_modify_deferred_init (gimple *stmt, gimple_stmt_iterator *gsi)
4176 tree lhs = gimple_call_lhs (stmt);
4177 tree init_type = gimple_call_arg (stmt, 1);
4178 tree decl_name = gimple_call_arg (stmt, 2);
4180 struct access *lhs_access = get_access_for_expr (lhs);
4181 if (!lhs_access)
4182 return SRA_AM_NONE;
4184 location_t loc = gimple_location (stmt);
4186 if (lhs_access->grp_to_be_replaced)
4188 tree lhs_repl = get_access_replacement (lhs_access);
4189 gimple_call_set_lhs (stmt, lhs_repl);
4190 tree arg0_repl = TYPE_SIZE_UNIT (TREE_TYPE (lhs_repl));
4191 gimple_call_set_arg (stmt, 0, arg0_repl);
4192 sra_stats.deferred_init++;
4193 gcc_assert (!lhs_access->first_child);
4194 return SRA_AM_MODIFIED;
4197 if (lhs_access->first_child)
4198 generate_subtree_deferred_init (lhs_access->first_child,
4199 init_type, decl_name, gsi, loc);
4200 if (lhs_access->grp_covered)
4202 unlink_stmt_vdef (stmt);
4203 gsi_remove (gsi, true);
4204 release_defs (stmt);
4205 return SRA_AM_REMOVED;
4208 return SRA_AM_MODIFIED;
4211 /* Examine both sides of the assignment statement pointed to by STMT, replace
4212 them with a scalare replacement if there is one and generate copying of
4213 replacements if scalarized aggregates have been used in the assignment. GSI
4214 is used to hold generated statements for type conversions and subtree
4215 copying. */
4217 static enum assignment_mod_result
4218 sra_modify_assign (gimple *stmt, gimple_stmt_iterator *gsi)
4220 struct access *lacc, *racc;
4221 tree lhs, rhs;
4222 bool modify_this_stmt = false;
4223 bool force_gimple_rhs = false;
4224 location_t loc;
4225 gimple_stmt_iterator orig_gsi = *gsi;
4227 if (!gimple_assign_single_p (stmt))
4228 return SRA_AM_NONE;
4229 lhs = gimple_assign_lhs (stmt);
4230 rhs = gimple_assign_rhs1 (stmt);
4232 if (TREE_CODE (rhs) == CONSTRUCTOR)
4233 return sra_modify_constructor_assign (stmt, gsi);
4235 if (TREE_CODE (rhs) == REALPART_EXPR || TREE_CODE (lhs) == REALPART_EXPR
4236 || TREE_CODE (rhs) == IMAGPART_EXPR || TREE_CODE (lhs) == IMAGPART_EXPR
4237 || TREE_CODE (rhs) == BIT_FIELD_REF || TREE_CODE (lhs) == BIT_FIELD_REF)
4239 modify_this_stmt = sra_modify_expr (gimple_assign_rhs1_ptr (stmt),
4240 gsi, false);
4241 modify_this_stmt |= sra_modify_expr (gimple_assign_lhs_ptr (stmt),
4242 gsi, true);
4243 return modify_this_stmt ? SRA_AM_MODIFIED : SRA_AM_NONE;
4246 lacc = get_access_for_expr (lhs);
4247 racc = get_access_for_expr (rhs);
4248 if (!lacc && !racc)
4249 return SRA_AM_NONE;
4250 /* Avoid modifying initializations of constant-pool replacements. */
4251 if (racc && (racc->replacement_decl == lhs))
4252 return SRA_AM_NONE;
4254 loc = gimple_location (stmt);
4255 if (lacc && lacc->grp_to_be_replaced)
4257 lhs = get_access_replacement (lacc);
4258 gimple_assign_set_lhs (stmt, lhs);
4259 modify_this_stmt = true;
4260 if (lacc->grp_partial_lhs)
4261 force_gimple_rhs = true;
4262 sra_stats.exprs++;
4265 if (racc && racc->grp_to_be_replaced)
4267 rhs = get_access_replacement (racc);
4268 modify_this_stmt = true;
4269 if (racc->grp_partial_lhs)
4270 force_gimple_rhs = true;
4271 sra_stats.exprs++;
4273 else if (racc
4274 && !racc->grp_unscalarized_data
4275 && !racc->grp_unscalarizable_region
4276 && TREE_CODE (lhs) == SSA_NAME
4277 && !access_has_replacements_p (racc))
4279 rhs = get_repl_default_def_ssa_name (racc, TREE_TYPE (lhs));
4280 modify_this_stmt = true;
4281 sra_stats.exprs++;
4284 if (modify_this_stmt
4285 && !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
4287 /* If we can avoid creating a VIEW_CONVERT_EXPR, then do so.
4288 ??? This should move to fold_stmt which we simply should
4289 call after building a VIEW_CONVERT_EXPR here. */
4290 if (AGGREGATE_TYPE_P (TREE_TYPE (lhs))
4291 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (lhs)) == racc->reverse
4292 && !contains_bitfld_component_ref_p (lhs))
4294 lhs = build_ref_for_model (loc, lhs, 0, racc, gsi, false);
4295 gimple_assign_set_lhs (stmt, lhs);
4297 else if (lacc
4298 && AGGREGATE_TYPE_P (TREE_TYPE (rhs))
4299 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (rhs)) == lacc->reverse
4300 && !contains_vce_or_bfcref_p (rhs))
4301 rhs = build_ref_for_model (loc, rhs, 0, lacc, gsi, false);
4303 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
4305 rhs = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
4306 if (is_gimple_reg_type (TREE_TYPE (lhs))
4307 && TREE_CODE (lhs) != SSA_NAME)
4308 force_gimple_rhs = true;
4312 if (lacc && lacc->grp_to_be_debug_replaced)
4314 tree dlhs = get_access_replacement (lacc);
4315 tree drhs = unshare_expr (rhs);
4316 if (!useless_type_conversion_p (TREE_TYPE (dlhs), TREE_TYPE (drhs)))
4318 if (AGGREGATE_TYPE_P (TREE_TYPE (drhs))
4319 && !contains_vce_or_bfcref_p (drhs))
4320 drhs = build_debug_ref_for_model (loc, drhs, 0, lacc);
4321 if (drhs
4322 && !useless_type_conversion_p (TREE_TYPE (dlhs),
4323 TREE_TYPE (drhs)))
4324 drhs = fold_build1_loc (loc, VIEW_CONVERT_EXPR,
4325 TREE_TYPE (dlhs), drhs);
4327 gdebug *ds = gimple_build_debug_bind (dlhs, drhs, stmt);
4328 gsi_insert_before (gsi, ds, GSI_SAME_STMT);
4331 /* From this point on, the function deals with assignments in between
4332 aggregates when at least one has scalar reductions of some of its
4333 components. There are three possible scenarios: Both the LHS and RHS have
4334 to-be-scalarized components, 2) only the RHS has or 3) only the LHS has.
4336 In the first case, we would like to load the LHS components from RHS
4337 components whenever possible. If that is not possible, we would like to
4338 read it directly from the RHS (after updating it by storing in it its own
4339 components). If there are some necessary unscalarized data in the LHS,
4340 those will be loaded by the original assignment too. If neither of these
4341 cases happen, the original statement can be removed. Most of this is done
4342 by load_assign_lhs_subreplacements.
4344 In the second case, we would like to store all RHS scalarized components
4345 directly into LHS and if they cover the aggregate completely, remove the
4346 statement too. In the third case, we want the LHS components to be loaded
4347 directly from the RHS (DSE will remove the original statement if it
4348 becomes redundant).
4350 This is a bit complex but manageable when types match and when unions do
4351 not cause confusion in a way that we cannot really load a component of LHS
4352 from the RHS or vice versa (the access representing this level can have
4353 subaccesses that are accessible only through a different union field at a
4354 higher level - different from the one used in the examined expression).
4355 Unions are fun.
4357 Therefore, I specially handle a fourth case, happening when there is a
4358 specific type cast or it is impossible to locate a scalarized subaccess on
4359 the other side of the expression. If that happens, I simply "refresh" the
4360 RHS by storing in it is scalarized components leave the original statement
4361 there to do the copying and then load the scalar replacements of the LHS.
4362 This is what the first branch does. */
4364 if (modify_this_stmt
4365 || gimple_has_volatile_ops (stmt)
4366 || contains_vce_or_bfcref_p (rhs)
4367 || contains_vce_or_bfcref_p (lhs)
4368 || stmt_ends_bb_p (stmt))
4370 /* No need to copy into a constant, it comes pre-initialized. */
4371 if (access_has_children_p (racc) && !TREE_READONLY (racc->base))
4372 generate_subtree_copies (racc->first_child, rhs, racc->offset, 0, 0,
4373 gsi, false, false, loc);
4374 if (access_has_children_p (lacc))
4376 gimple_stmt_iterator alt_gsi = gsi_none ();
4377 if (stmt_ends_bb_p (stmt))
4379 alt_gsi = gsi_start_edge (single_non_eh_succ (gsi_bb (*gsi)));
4380 gsi = &alt_gsi;
4382 generate_subtree_copies (lacc->first_child, lhs, lacc->offset, 0, 0,
4383 gsi, true, true, loc);
4385 sra_stats.separate_lhs_rhs_handling++;
4387 /* This gimplification must be done after generate_subtree_copies,
4388 lest we insert the subtree copies in the middle of the gimplified
4389 sequence. */
4390 if (force_gimple_rhs)
4391 rhs = force_gimple_operand_gsi (&orig_gsi, rhs, true, NULL_TREE,
4392 true, GSI_SAME_STMT);
4393 if (gimple_assign_rhs1 (stmt) != rhs)
4395 modify_this_stmt = true;
4396 gimple_assign_set_rhs_from_tree (&orig_gsi, rhs);
4397 gcc_assert (stmt == gsi_stmt (orig_gsi));
4400 return modify_this_stmt ? SRA_AM_MODIFIED : SRA_AM_NONE;
4402 else
4404 if (access_has_children_p (lacc)
4405 && access_has_children_p (racc)
4406 /* When an access represents an unscalarizable region, it usually
4407 represents accesses with variable offset and thus must not be used
4408 to generate new memory accesses. */
4409 && !lacc->grp_unscalarizable_region
4410 && !racc->grp_unscalarizable_region)
4412 struct subreplacement_assignment_data sad;
4414 sad.left_offset = lacc->offset;
4415 sad.assignment_lhs = lhs;
4416 sad.assignment_rhs = rhs;
4417 sad.top_racc = racc;
4418 sad.old_gsi = *gsi;
4419 sad.new_gsi = gsi;
4420 sad.loc = gimple_location (stmt);
4421 sad.refreshed = SRA_UDH_NONE;
4423 if (lacc->grp_read && !lacc->grp_covered)
4424 handle_unscalarized_data_in_subtree (&sad);
4426 load_assign_lhs_subreplacements (lacc, &sad);
4427 if (sad.refreshed != SRA_UDH_RIGHT)
4429 gsi_next (gsi);
4430 unlink_stmt_vdef (stmt);
4431 gsi_remove (&sad.old_gsi, true);
4432 release_defs (stmt);
4433 sra_stats.deleted++;
4434 return SRA_AM_REMOVED;
4437 else
4439 if (access_has_children_p (racc)
4440 && !racc->grp_unscalarized_data
4441 && TREE_CODE (lhs) != SSA_NAME)
4443 if (dump_file)
4445 fprintf (dump_file, "Removing load: ");
4446 print_gimple_stmt (dump_file, stmt, 0);
4448 generate_subtree_copies (racc->first_child, lhs,
4449 racc->offset, 0, 0, gsi,
4450 false, false, loc);
4451 gcc_assert (stmt == gsi_stmt (*gsi));
4452 unlink_stmt_vdef (stmt);
4453 gsi_remove (gsi, true);
4454 release_defs (stmt);
4455 sra_stats.deleted++;
4456 return SRA_AM_REMOVED;
4458 /* Restore the aggregate RHS from its components so the
4459 prevailing aggregate copy does the right thing. */
4460 if (access_has_children_p (racc) && !TREE_READONLY (racc->base))
4461 generate_subtree_copies (racc->first_child, rhs, racc->offset, 0, 0,
4462 gsi, false, false, loc);
4463 /* Re-load the components of the aggregate copy destination.
4464 But use the RHS aggregate to load from to expose more
4465 optimization opportunities. */
4466 if (access_has_children_p (lacc))
4467 generate_subtree_copies (lacc->first_child, rhs, lacc->offset,
4468 0, 0, gsi, true, true, loc);
4471 return SRA_AM_NONE;
4475 /* Set any scalar replacements of values in the constant pool to the initial
4476 value of the constant. (Constant-pool decls like *.LC0 have effectively
4477 been initialized before the program starts, we must do the same for their
4478 replacements.) Thus, we output statements like 'SR.1 = *.LC0[0];' into
4479 the function's entry block. */
4481 static void
4482 initialize_constant_pool_replacements (void)
4484 gimple_seq seq = NULL;
4485 gimple_stmt_iterator gsi = gsi_start (seq);
4486 bitmap_iterator bi;
4487 unsigned i;
4489 EXECUTE_IF_SET_IN_BITMAP (candidate_bitmap, 0, i, bi)
4491 tree var = candidate (i);
4492 if (!constant_decl_p (var))
4493 continue;
4495 struct access *access = get_first_repr_for_decl (var);
4497 while (access)
4499 if (access->replacement_decl)
4501 gassign *stmt
4502 = gimple_build_assign (get_access_replacement (access),
4503 unshare_expr (access->expr));
4504 if (dump_file && (dump_flags & TDF_DETAILS))
4506 fprintf (dump_file, "Generating constant initializer: ");
4507 print_gimple_stmt (dump_file, stmt, 0);
4508 fprintf (dump_file, "\n");
4510 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
4511 update_stmt (stmt);
4514 if (access->first_child)
4515 access = access->first_child;
4516 else if (access->next_sibling)
4517 access = access->next_sibling;
4518 else
4520 while (access->parent && !access->next_sibling)
4521 access = access->parent;
4522 if (access->next_sibling)
4523 access = access->next_sibling;
4524 else
4525 access = access->next_grp;
4530 seq = gsi_seq (gsi);
4531 if (seq)
4532 gsi_insert_seq_on_edge_immediate (
4533 single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)), seq);
4536 /* Traverse the function body and all modifications as decided in
4537 analyze_all_variable_accesses. Return true iff the CFG has been
4538 changed. */
4540 static bool
4541 sra_modify_function_body (void)
4543 bool cfg_changed = false;
4544 basic_block bb;
4546 initialize_constant_pool_replacements ();
4548 FOR_EACH_BB_FN (bb, cfun)
4550 gimple_stmt_iterator gsi = gsi_start_bb (bb);
4551 while (!gsi_end_p (gsi))
4553 gimple *stmt = gsi_stmt (gsi);
4554 enum assignment_mod_result assign_result;
4555 bool modified = false, deleted = false;
4556 tree *t;
4557 unsigned i;
4559 switch (gimple_code (stmt))
4561 case GIMPLE_RETURN:
4562 t = gimple_return_retval_ptr (as_a <greturn *> (stmt));
4563 if (*t != NULL_TREE)
4564 modified |= sra_modify_expr (t, &gsi, false);
4565 break;
4567 case GIMPLE_ASSIGN:
4568 assign_result = sra_modify_assign (stmt, &gsi);
4569 modified |= assign_result == SRA_AM_MODIFIED;
4570 deleted = assign_result == SRA_AM_REMOVED;
4571 break;
4573 case GIMPLE_CALL:
4574 /* Handle calls to .DEFERRED_INIT specially. */
4575 if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
4577 assign_result = sra_modify_deferred_init (stmt, &gsi);
4578 modified |= assign_result == SRA_AM_MODIFIED;
4579 deleted = assign_result == SRA_AM_REMOVED;
4581 else
4583 /* Operands must be processed before the lhs. */
4584 for (i = 0; i < gimple_call_num_args (stmt); i++)
4586 t = gimple_call_arg_ptr (stmt, i);
4587 modified |= sra_modify_expr (t, &gsi, false);
4590 if (gimple_call_lhs (stmt))
4592 t = gimple_call_lhs_ptr (stmt);
4593 modified |= sra_modify_expr (t, &gsi, true);
4596 break;
4598 case GIMPLE_ASM:
4600 gasm *asm_stmt = as_a <gasm *> (stmt);
4601 for (i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
4603 t = &TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
4604 modified |= sra_modify_expr (t, &gsi, false);
4606 for (i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
4608 t = &TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
4609 modified |= sra_modify_expr (t, &gsi, true);
4612 break;
4614 default:
4615 break;
4618 if (modified)
4620 update_stmt (stmt);
4621 if (maybe_clean_eh_stmt (stmt)
4622 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
4623 cfg_changed = true;
4625 if (!deleted)
4626 gsi_next (&gsi);
4630 gsi_commit_edge_inserts ();
4631 return cfg_changed;
4634 /* Generate statements initializing scalar replacements of parts of function
4635 parameters. */
4637 static void
4638 initialize_parameter_reductions (void)
4640 gimple_stmt_iterator gsi;
4641 gimple_seq seq = NULL;
4642 tree parm;
4644 gsi = gsi_start (seq);
4645 for (parm = DECL_ARGUMENTS (current_function_decl);
4646 parm;
4647 parm = DECL_CHAIN (parm))
4649 vec<access_p> *access_vec;
4650 struct access *access;
4652 if (!bitmap_bit_p (candidate_bitmap, DECL_UID (parm)))
4653 continue;
4654 access_vec = get_base_access_vector (parm);
4655 if (!access_vec)
4656 continue;
4658 for (access = (*access_vec)[0];
4659 access;
4660 access = access->next_grp)
4661 generate_subtree_copies (access, parm, 0, 0, 0, &gsi, true, true,
4662 EXPR_LOCATION (parm));
4665 seq = gsi_seq (gsi);
4666 if (seq)
4667 gsi_insert_seq_on_edge_immediate (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)), seq);
4670 /* The "main" function of intraprocedural SRA passes. Runs the analysis and if
4671 it reveals there are components of some aggregates to be scalarized, it runs
4672 the required transformations. */
4673 static unsigned int
4674 perform_intra_sra (void)
4676 int ret = 0;
4677 sra_initialize ();
4679 if (!find_var_candidates ())
4680 goto out;
4682 if (!scan_function ())
4683 goto out;
4685 if (!analyze_all_variable_accesses ())
4686 goto out;
4688 if (sra_modify_function_body ())
4689 ret = TODO_update_ssa | TODO_cleanup_cfg;
4690 else
4691 ret = TODO_update_ssa;
4692 initialize_parameter_reductions ();
4694 statistics_counter_event (cfun, "Scalar replacements created",
4695 sra_stats.replacements);
4696 statistics_counter_event (cfun, "Modified expressions", sra_stats.exprs);
4697 statistics_counter_event (cfun, "Subtree copy stmts",
4698 sra_stats.subtree_copies);
4699 statistics_counter_event (cfun, "Subreplacement stmts",
4700 sra_stats.subreplacements);
4701 statistics_counter_event (cfun, "Deleted stmts", sra_stats.deleted);
4702 statistics_counter_event (cfun, "Separate LHS and RHS handling",
4703 sra_stats.separate_lhs_rhs_handling);
4705 out:
4706 sra_deinitialize ();
4707 return ret;
4710 /* Perform early intraprocedural SRA. */
4711 static unsigned int
4712 early_intra_sra (void)
4714 sra_mode = SRA_MODE_EARLY_INTRA;
4715 return perform_intra_sra ();
4718 /* Perform "late" intraprocedural SRA. */
4719 static unsigned int
4720 late_intra_sra (void)
4722 sra_mode = SRA_MODE_INTRA;
4723 return perform_intra_sra ();
4727 static bool
4728 gate_intra_sra (void)
4730 return flag_tree_sra != 0 && dbg_cnt (tree_sra);
4734 namespace {
4736 const pass_data pass_data_sra_early =
4738 GIMPLE_PASS, /* type */
4739 "esra", /* name */
4740 OPTGROUP_NONE, /* optinfo_flags */
4741 TV_TREE_SRA, /* tv_id */
4742 ( PROP_cfg | PROP_ssa ), /* properties_required */
4743 0, /* properties_provided */
4744 0, /* properties_destroyed */
4745 0, /* todo_flags_start */
4746 TODO_update_ssa, /* todo_flags_finish */
4749 class pass_sra_early : public gimple_opt_pass
4751 public:
4752 pass_sra_early (gcc::context *ctxt)
4753 : gimple_opt_pass (pass_data_sra_early, ctxt)
4756 /* opt_pass methods: */
4757 bool gate (function *) final override { return gate_intra_sra (); }
4758 unsigned int execute (function *) final override
4760 return early_intra_sra ();
4763 }; // class pass_sra_early
4765 } // anon namespace
4767 gimple_opt_pass *
4768 make_pass_sra_early (gcc::context *ctxt)
4770 return new pass_sra_early (ctxt);
4773 namespace {
4775 const pass_data pass_data_sra =
4777 GIMPLE_PASS, /* type */
4778 "sra", /* name */
4779 OPTGROUP_NONE, /* optinfo_flags */
4780 TV_TREE_SRA, /* tv_id */
4781 ( PROP_cfg | PROP_ssa ), /* properties_required */
4782 0, /* properties_provided */
4783 0, /* properties_destroyed */
4784 TODO_update_address_taken, /* todo_flags_start */
4785 TODO_update_ssa, /* todo_flags_finish */
4788 class pass_sra : public gimple_opt_pass
4790 public:
4791 pass_sra (gcc::context *ctxt)
4792 : gimple_opt_pass (pass_data_sra, ctxt)
4795 /* opt_pass methods: */
4796 bool gate (function *) final override { return gate_intra_sra (); }
4797 unsigned int execute (function *) final override { return late_intra_sra (); }
4799 }; // class pass_sra
4801 } // anon namespace
4803 gimple_opt_pass *
4804 make_pass_sra (gcc::context *ctxt)
4806 return new pass_sra (ctxt);