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