Debian Codegraph @

gcc/tree-sra.cc

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