0f68941716373cfbe8ecde5aecce3e50b0b5f22a
[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) 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
6 Contributed by Diego Novillo <dnovillo@redhat.com>
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 3, or (at your option) any
13 later version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "ggc.h"
29 #include "tree.h"
30
31 /* These RTL headers are needed for basic-block.h. */
32 #include "rtl.h"
33 #include "tm_p.h"
34 #include "hard-reg-set.h"
35 #include "basic-block.h"
36 #include "diagnostic.h"
37 #include "langhooks.h"
38 #include "tree-inline.h"
39 #include "tree-flow.h"
40 #include "gimple.h"
41 #include "tree-dump.h"
42 #include "tree-pass.h"
43 #include "timevar.h"
44 #include "flags.h"
45 #include "bitmap.h"
46 #include "obstack.h"
47 #include "target.h"
48 /* expr.h is needed for MOVE_RATIO. */
49 #include "expr.h"
50 #include "params.h"
51
52
53 /* This object of this pass is to replace a non-addressable aggregate with a
54 set of independent variables. Most of the time, all of these variables
55 will be scalars. But a secondary objective is to break up larger
56 aggregates into smaller aggregates. In the process we may find that some
57 bits of the larger aggregate can be deleted as unreferenced.
58
59 This substitution is done globally. More localized substitutions would
60 be the purvey of a load-store motion pass.
61
62 The optimization proceeds in phases:
63
64 (1) Identify variables that have types that are candidates for
65 decomposition.
66
67 (2) Scan the function looking for the ways these variables are used.
68 In particular we're interested in the number of times a variable
69 (or member) is needed as a complete unit, and the number of times
70 a variable (or member) is copied.
71
72 (3) Based on the usage profile, instantiate substitution variables.
73
74 (4) Scan the function making replacements.
75 */
76
77
78 /* True if this is the "early" pass, before inlining. */
79 static bool early_sra;
80
81 /* The set of aggregate variables that are candidates for scalarization. */
82 static bitmap sra_candidates;
83
84 /* Set of scalarizable PARM_DECLs that need copy-in operations at the
85 beginning of the function. */
86 static bitmap needs_copy_in;
87
88 /* Sets of bit pairs that cache type decomposition and instantiation. */
89 static bitmap sra_type_decomp_cache;
90 static bitmap sra_type_inst_cache;
91
92 /* One of these structures is created for each candidate aggregate and
93 each (accessed) member or group of members of such an aggregate. */
94 struct sra_elt
95 {
96 /* A tree of the elements. Used when we want to traverse everything. */
97 struct sra_elt *parent;
98 struct sra_elt *groups;
99 struct sra_elt *children;
100 struct sra_elt *sibling;
101
102 /* If this element is a root, then this is the VAR_DECL. If this is
103 a sub-element, this is some token used to identify the reference.
104 In the case of COMPONENT_REF, this is the FIELD_DECL. In the case
105 of an ARRAY_REF, this is the (constant) index. In the case of an
106 ARRAY_RANGE_REF, this is the (constant) RANGE_EXPR. In the case
107 of a complex number, this is a zero or one. */
108 tree element;
109
110 /* The type of the element. */
111 tree type;
112
113 /* A VAR_DECL, for any sub-element we've decided to replace. */
114 tree replacement;
115
116 /* The number of times the element is referenced as a whole. I.e.
117 given "a.b.c", this would be incremented for C, but not for A or B. */
118 unsigned int n_uses;
119
120 /* The number of times the element is copied to or from another
121 scalarizable element. */
122 unsigned int n_copies;
123
124 /* True if TYPE is scalar. */
125 bool is_scalar;
126
127 /* True if this element is a group of members of its parent. */
128 bool is_group;
129
130 /* True if we saw something about this element that prevents scalarization,
131 such as non-constant indexing. */
132 bool cannot_scalarize;
133
134 /* True if we've decided that structure-to-structure assignment
135 should happen via memcpy and not per-element. */
136 bool use_block_copy;
137
138 /* True if everything under this element has been marked TREE_NO_WARNING. */
139 bool all_no_warning;
140
141 /* A flag for use with/after random access traversals. */
142 bool visited;
143
144 /* True if there is BIT_FIELD_REF on the lhs with a vector. */
145 bool is_vector_lhs;
146
147 /* 1 if the element is a field that is part of a block, 2 if the field
148 is the block itself, 0 if it's neither. */
149 char in_bitfld_block;
150 };
151
152 #define IS_ELEMENT_FOR_GROUP(ELEMENT) (TREE_CODE (ELEMENT) == RANGE_EXPR)
153
154 #define FOR_EACH_ACTUAL_CHILD(CHILD, ELT) \
155 for ((CHILD) = (ELT)->is_group \
156 ? next_child_for_group (NULL, (ELT)) \
157 : (ELT)->children; \
158 (CHILD); \
159 (CHILD) = (ELT)->is_group \
160 ? next_child_for_group ((CHILD), (ELT)) \
161 : (CHILD)->sibling)
162
163 /* Helper function for above macro. Return next child in group. */
164 static struct sra_elt *
165 next_child_for_group (struct sra_elt *child, struct sra_elt *group)
166 {
167 gcc_assert (group->is_group);
168
169 /* Find the next child in the parent. */
170 if (child)
171 child = child->sibling;
172 else
173 child = group->parent->children;
174
175 /* Skip siblings that do not belong to the group. */
176 while (child)
177 {
178 tree g_elt = group->element;
179 if (TREE_CODE (g_elt) == RANGE_EXPR)
180 {
181 if (!tree_int_cst_lt (child->element, TREE_OPERAND (g_elt, 0))
182 && !tree_int_cst_lt (TREE_OPERAND (g_elt, 1), child->element))
183 break;
184 }
185 else
186 gcc_unreachable ();
187
188 child = child->sibling;
189 }
190
191 return child;
192 }
193
194 /* Random access to the child of a parent is performed by hashing.
195 This prevents quadratic behavior, and allows SRA to function
196 reasonably on larger records. */
197 static htab_t sra_map;
198
199 /* All structures are allocated out of the following obstack. */
200 static struct obstack sra_obstack;
201
202 /* Debugging functions. */
203 static void dump_sra_elt_name (FILE *, struct sra_elt *);
204 extern void debug_sra_elt_name (struct sra_elt *);
205
206 /* Forward declarations. */
207 static tree generate_element_ref (struct sra_elt *);
208 static gimple_seq sra_build_assignment (tree dst, tree src);
209 static void mark_all_v_defs_seq (gimple_seq);
210
211 \f
212 /* Return true if DECL is an SRA candidate. */
213
214 static bool
215 is_sra_candidate_decl (tree decl)
216 {
217 return DECL_P (decl) && bitmap_bit_p (sra_candidates, DECL_UID (decl));
218 }
219
220 /* Return true if TYPE is a scalar type. */
221
222 static bool
223 is_sra_scalar_type (tree type)
224 {
225 enum tree_code code = TREE_CODE (type);
226 return (code == INTEGER_TYPE || code == REAL_TYPE || code == VECTOR_TYPE
227 || code == FIXED_POINT_TYPE
228 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
229 || code == POINTER_TYPE || code == OFFSET_TYPE
230 || code == REFERENCE_TYPE);
231 }
232
233 /* Return true if TYPE can be decomposed into a set of independent variables.
234
235 Note that this doesn't imply that all elements of TYPE can be
236 instantiated, just that if we decide to break up the type into
237 separate pieces that it can be done. */
238
239 static bool
240 sra_type_can_be_decomposed_p (tree type)
241 {
242 unsigned int cache = TYPE_UID (TYPE_MAIN_VARIANT (type)) * 2;
243 tree t;
244
245 /* Avoid searching the same type twice. */
246 if (bitmap_bit_p (sra_type_decomp_cache, cache+0))
247 return true;
248 if (bitmap_bit_p (sra_type_decomp_cache, cache+1))
249 return false;
250
251 /* The type must have a definite nonzero size. */
252 if (TYPE_SIZE (type) == NULL || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
253 || integer_zerop (TYPE_SIZE (type)))
254 goto fail;
255
256 /* The type must be a non-union aggregate. */
257 switch (TREE_CODE (type))
258 {
259 case RECORD_TYPE:
260 {
261 bool saw_one_field = false;
262
263 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
264 if (TREE_CODE (t) == FIELD_DECL)
265 {
266 /* Reject incorrectly represented bit fields. */
267 if (DECL_BIT_FIELD (t)
268 && INTEGRAL_TYPE_P (TREE_TYPE (t))
269 && (tree_low_cst (DECL_SIZE (t), 1)
270 != TYPE_PRECISION (TREE_TYPE (t))))
271 goto fail;
272
273 /* And volatile fields. */
274 if (TREE_THIS_VOLATILE (t))
275 goto fail;
276
277 saw_one_field = true;
278 }
279
280 /* Record types must have at least one field. */
281 if (!saw_one_field)
282 goto fail;
283 }
284 break;
285
286 case ARRAY_TYPE:
287 /* Array types must have a fixed lower and upper bound. */
288 t = TYPE_DOMAIN (type);
289 if (t == NULL)
290 goto fail;
291 if (TYPE_MIN_VALUE (t) == NULL || !TREE_CONSTANT (TYPE_MIN_VALUE (t)))
292 goto fail;
293 if (TYPE_MAX_VALUE (t) == NULL || !TREE_CONSTANT (TYPE_MAX_VALUE (t)))
294 goto fail;
295 break;
296
297 case COMPLEX_TYPE:
298 break;
299
300 default:
301 goto fail;
302 }
303
304 bitmap_set_bit (sra_type_decomp_cache, cache+0);
305 return true;
306
307 fail:
308 bitmap_set_bit (sra_type_decomp_cache, cache+1);
309 return false;
310 }
311
312 /* Returns true if the TYPE is one of the available va_list types.
313 Otherwise it returns false.
314 Note, that for multiple calling conventions there can be more
315 than just one va_list type present. */
316
317 static bool
318 is_va_list_type (tree type)
319 {
320 tree h;
321
322 if (type == NULL_TREE)
323 return false;
324 h = targetm.canonical_va_list_type (type);
325 if (h == NULL_TREE)
326 return false;
327 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (h))
328 return true;
329 return false;
330 }
331
332 /* Return true if DECL can be decomposed into a set of independent
333 (though not necessarily scalar) variables. */
334
335 static bool
336 decl_can_be_decomposed_p (tree var)
337 {
338 /* Early out for scalars. */
339 if (is_sra_scalar_type (TREE_TYPE (var)))
340 return false;
341
342 /* The variable must not be aliased. */
343 if (!is_gimple_non_addressable (var))
344 {
345 if (dump_file && (dump_flags & TDF_DETAILS))
346 {
347 fprintf (dump_file, "Cannot scalarize variable ");
348 print_generic_expr (dump_file, var, dump_flags);
349 fprintf (dump_file, " because it must live in memory\n");
350 }
351 return false;
352 }
353
354 /* The variable must not be volatile. */
355 if (TREE_THIS_VOLATILE (var))
356 {
357 if (dump_file && (dump_flags & TDF_DETAILS))
358 {
359 fprintf (dump_file, "Cannot scalarize variable ");
360 print_generic_expr (dump_file, var, dump_flags);
361 fprintf (dump_file, " because it is declared volatile\n");
362 }
363 return false;
364 }
365
366 /* We must be able to decompose the variable's type. */
367 if (!sra_type_can_be_decomposed_p (TREE_TYPE (var)))
368 {
369 if (dump_file && (dump_flags & TDF_DETAILS))
370 {
371 fprintf (dump_file, "Cannot scalarize variable ");
372 print_generic_expr (dump_file, var, dump_flags);
373 fprintf (dump_file, " because its type cannot be decomposed\n");
374 }
375 return false;
376 }
377
378 /* HACK: if we decompose a va_list_type_node before inlining, then we'll
379 confuse tree-stdarg.c, and we won't be able to figure out which and
380 how many arguments are accessed. This really should be improved in
381 tree-stdarg.c, as the decomposition is truly a win. This could also
382 be fixed if the stdarg pass ran early, but this can't be done until
383 we've aliasing information early too. See PR 30791. */
384 if (early_sra && is_va_list_type (TREE_TYPE (var)))
385 return false;
386
387 return true;
388 }
389
390 /* Return true if TYPE can be *completely* decomposed into scalars. */
391
392 static bool
393 type_can_instantiate_all_elements (tree type)
394 {
395 if (is_sra_scalar_type (type))
396 return true;
397 if (!sra_type_can_be_decomposed_p (type))
398 return false;
399
400 switch (TREE_CODE (type))
401 {
402 case RECORD_TYPE:
403 {
404 unsigned int cache = TYPE_UID (TYPE_MAIN_VARIANT (type)) * 2;
405 tree f;
406
407 if (bitmap_bit_p (sra_type_inst_cache, cache+0))
408 return true;
409 if (bitmap_bit_p (sra_type_inst_cache, cache+1))
410 return false;
411
412 for (f = TYPE_FIELDS (type); f ; f = TREE_CHAIN (f))
413 if (TREE_CODE (f) == FIELD_DECL)
414 {
415 if (!type_can_instantiate_all_elements (TREE_TYPE (f)))
416 {
417 bitmap_set_bit (sra_type_inst_cache, cache+1);
418 return false;
419 }
420 }
421
422 bitmap_set_bit (sra_type_inst_cache, cache+0);
423 return true;
424 }
425
426 case ARRAY_TYPE:
427 return type_can_instantiate_all_elements (TREE_TYPE (type));
428
429 case COMPLEX_TYPE:
430 return true;
431
432 default:
433 gcc_unreachable ();
434 }
435 }
436
437 /* Test whether ELT or some sub-element cannot be scalarized. */
438
439 static bool
440 can_completely_scalarize_p (struct sra_elt *elt)
441 {
442 struct sra_elt *c;
443
444 if (elt->cannot_scalarize)
445 return false;
446
447 for (c = elt->children; c; c = c->sibling)
448 if (!can_completely_scalarize_p (c))
449 return false;
450
451 for (c = elt->groups; c; c = c->sibling)
452 if (!can_completely_scalarize_p (c))
453 return false;
454
455 return true;
456 }
457
458 \f
459 /* A simplified tree hashing algorithm that only handles the types of
460 trees we expect to find in sra_elt->element. */
461
462 static hashval_t
463 sra_hash_tree (tree t)
464 {
465 hashval_t h;
466
467 switch (TREE_CODE (t))
468 {
469 case VAR_DECL:
470 case PARM_DECL:
471 case RESULT_DECL:
472 h = DECL_UID (t);
473 break;
474
475 case INTEGER_CST:
476 h = TREE_INT_CST_LOW (t) ^ TREE_INT_CST_HIGH (t);
477 break;
478
479 case RANGE_EXPR:
480 h = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
481 h = iterative_hash_expr (TREE_OPERAND (t, 1), h);
482 break;
483
484 case FIELD_DECL:
485 /* We can have types that are compatible, but have different member
486 lists, so we can't hash fields by ID. Use offsets instead. */
487 h = iterative_hash_expr (DECL_FIELD_OFFSET (t), 0);
488 h = iterative_hash_expr (DECL_FIELD_BIT_OFFSET (t), h);
489 break;
490
491 case BIT_FIELD_REF:
492 /* Don't take operand 0 into account, that's our parent. */
493 h = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
494 h = iterative_hash_expr (TREE_OPERAND (t, 2), h);
495 break;
496
497 default:
498 gcc_unreachable ();
499 }
500
501 return h;
502 }
503
504 /* Hash function for type SRA_PAIR. */
505
506 static hashval_t
507 sra_elt_hash (const void *x)
508 {
509 const struct sra_elt *const e = (const struct sra_elt *) x;
510 const struct sra_elt *p;
511 hashval_t h;
512
513 h = sra_hash_tree (e->element);
514
515 /* Take into account everything except bitfield blocks back up the
516 chain. Given that chain lengths are rarely very long, this
517 should be acceptable. If we truly identify this as a performance
518 problem, it should work to hash the pointer value
519 "e->parent". */
520 for (p = e->parent; p ; p = p->parent)
521 if (!p->in_bitfld_block)
522 h = (h * 65521) ^ sra_hash_tree (p->element);
523
524 return h;
525 }
526
527 /* Equality function for type SRA_PAIR. */
528
529 static int
530 sra_elt_eq (const void *x, const void *y)
531 {
532 const struct sra_elt *const a = (const struct sra_elt *) x;
533 const struct sra_elt *const b = (const struct sra_elt *) y;
534 tree ae, be;
535 const struct sra_elt *ap = a->parent;
536 const struct sra_elt *bp = b->parent;
537
538 if (ap)
539 while (ap->in_bitfld_block)
540 ap = ap->parent;
541 if (bp)
542 while (bp->in_bitfld_block)
543 bp = bp->parent;
544
545 if (ap != bp)
546 return false;
547
548 ae = a->element;
549 be = b->element;
550
551 if (ae == be)
552 return true;
553 if (TREE_CODE (ae) != TREE_CODE (be))
554 return false;
555
556 switch (TREE_CODE (ae))
557 {
558 case VAR_DECL:
559 case PARM_DECL:
560 case RESULT_DECL:
561 /* These are all pointer unique. */
562 return false;
563
564 case INTEGER_CST:
565 /* Integers are not pointer unique, so compare their values. */
566 return tree_int_cst_equal (ae, be);
567
568 case RANGE_EXPR:
569 return
570 tree_int_cst_equal (TREE_OPERAND (ae, 0), TREE_OPERAND (be, 0))
571 && tree_int_cst_equal (TREE_OPERAND (ae, 1), TREE_OPERAND (be, 1));
572
573 case FIELD_DECL:
574 /* Fields are unique within a record, but not between
575 compatible records. */
576 if (DECL_FIELD_CONTEXT (ae) == DECL_FIELD_CONTEXT (be))
577 return false;
578 return fields_compatible_p (ae, be);
579
580 case BIT_FIELD_REF:
581 return
582 tree_int_cst_equal (TREE_OPERAND (ae, 1), TREE_OPERAND (be, 1))
583 && tree_int_cst_equal (TREE_OPERAND (ae, 2), TREE_OPERAND (be, 2));
584
585 default:
586 gcc_unreachable ();
587 }
588 }
589
590 /* Create or return the SRA_ELT structure for CHILD in PARENT. PARENT
591 may be null, in which case CHILD must be a DECL. */
592
593 static struct sra_elt *
594 lookup_element (struct sra_elt *parent, tree child, tree type,
595 enum insert_option insert)
596 {
597 struct sra_elt dummy;
598 struct sra_elt **slot;
599 struct sra_elt *elt;
600
601 if (parent)
602 dummy.parent = parent->is_group ? parent->parent : parent;
603 else
604 dummy.parent = NULL;
605 dummy.element = child;
606
607 slot = (struct sra_elt **) htab_find_slot (sra_map, &dummy, insert);
608 if (!slot && insert == NO_INSERT)
609 return NULL;
610
611 elt = *slot;
612 if (!elt && insert == INSERT)
613 {
614 *slot = elt = XOBNEW (&sra_obstack, struct sra_elt);
615 memset (elt, 0, sizeof (*elt));
616
617 elt->parent = parent;
618 elt->element = child;
619 elt->type = type;
620 elt->is_scalar = is_sra_scalar_type (type);
621
622 if (parent)
623 {
624 if (IS_ELEMENT_FOR_GROUP (elt->element))
625 {
626 elt->is_group = true;
627 elt->sibling = parent->groups;
628 parent->groups = elt;
629 }
630 else
631 {
632 elt->sibling = parent->children;
633 parent->children = elt;
634 }
635 }
636
637 /* If this is a parameter, then if we want to scalarize, we have
638 one copy from the true function parameter. Count it now. */
639 if (TREE_CODE (child) == PARM_DECL)
640 {
641 elt->n_copies = 1;
642 bitmap_set_bit (needs_copy_in, DECL_UID (child));
643 }
644 }
645
646 return elt;
647 }
648
649 /* Create or return the SRA_ELT structure for EXPR if the expression
650 refers to a scalarizable variable. */
651
652 static struct sra_elt *
653 maybe_lookup_element_for_expr (tree expr)
654 {
655 struct sra_elt *elt;
656 tree child;
657
658 switch (TREE_CODE (expr))
659 {
660 case VAR_DECL:
661 case PARM_DECL:
662 case RESULT_DECL:
663 if (is_sra_candidate_decl (expr))
664 return lookup_element (NULL, expr, TREE_TYPE (expr), INSERT);
665 return NULL;
666
667 case ARRAY_REF:
668 /* We can't scalarize variable array indices. */
669 if (in_array_bounds_p (expr))
670 child = TREE_OPERAND (expr, 1);
671 else
672 return NULL;
673 break;
674
675 case ARRAY_RANGE_REF:
676 /* We can't scalarize variable array indices. */
677 if (range_in_array_bounds_p (expr))
678 {
679 tree domain = TYPE_DOMAIN (TREE_TYPE (expr));
680 child = build2 (RANGE_EXPR, integer_type_node,
681 TYPE_MIN_VALUE (domain), TYPE_MAX_VALUE (domain));
682 }
683 else
684 return NULL;
685 break;
686
687 case COMPONENT_REF:
688 {
689 tree type = TREE_TYPE (TREE_OPERAND (expr, 0));
690 /* Don't look through unions. */
691 if (TREE_CODE (type) != RECORD_TYPE)
692 return NULL;
693 /* Neither through variable-sized records. */
694 if (TYPE_SIZE (type) == NULL_TREE
695 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
696 return NULL;
697 child = TREE_OPERAND (expr, 1);
698 }
699 break;
700
701 case REALPART_EXPR:
702 child = integer_zero_node;
703 break;
704 case IMAGPART_EXPR:
705 child = integer_one_node;
706 break;
707
708 default:
709 return NULL;
710 }
711
712 elt = maybe_lookup_element_for_expr (TREE_OPERAND (expr, 0));
713 if (elt)
714 return lookup_element (elt, child, TREE_TYPE (expr), INSERT);
715 return NULL;
716 }
717
718 \f
719 /* Functions to walk just enough of the tree to see all scalarizable
720 references, and categorize them. */
721
722 /* A set of callbacks for phases 2 and 4. They'll be invoked for the
723 various kinds of references seen. In all cases, *GSI is an iterator
724 pointing to the statement being processed. */
725 struct sra_walk_fns
726 {
727 /* Invoked when ELT is required as a unit. Note that ELT might refer to
728 a leaf node, in which case this is a simple scalar reference. *EXPR_P
729 points to the location of the expression. IS_OUTPUT is true if this
730 is a left-hand-side reference. USE_ALL is true if we saw something we
731 couldn't quite identify and had to force the use of the entire object. */
732 void (*use) (struct sra_elt *elt, tree *expr_p,
733 gimple_stmt_iterator *gsi, bool is_output, bool use_all);
734
735 /* Invoked when we have a copy between two scalarizable references. */
736 void (*copy) (struct sra_elt *lhs_elt, struct sra_elt *rhs_elt,
737 gimple_stmt_iterator *gsi);
738
739 /* Invoked when ELT is initialized from a constant. VALUE may be NULL,
740 in which case it should be treated as an empty CONSTRUCTOR. */
741 void (*init) (struct sra_elt *elt, tree value, gimple_stmt_iterator *gsi);
742
743 /* Invoked when we have a copy between one scalarizable reference ELT
744 and one non-scalarizable reference OTHER without side-effects.
745 IS_OUTPUT is true if ELT is on the left-hand side. */
746 void (*ldst) (struct sra_elt *elt, tree other,
747 gimple_stmt_iterator *gsi, bool is_output);
748
749 /* True during phase 2, false during phase 4. */
750 /* ??? This is a hack. */
751 bool initial_scan;
752 };
753
754 #ifdef ENABLE_CHECKING
755 /* Invoked via walk_tree, if *TP contains a candidate decl, return it. */
756
757 static tree
758 sra_find_candidate_decl (tree *tp, int *walk_subtrees,
759 void *data ATTRIBUTE_UNUSED)
760 {
761 tree t = *tp;
762 enum tree_code code = TREE_CODE (t);
763
764 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
765 {
766 *walk_subtrees = 0;
767 if (is_sra_candidate_decl (t))
768 return t;
769 }
770 else if (TYPE_P (t))
771 *walk_subtrees = 0;
772
773 return NULL;
774 }
775 #endif
776
777 /* Walk most expressions looking for a scalarizable aggregate.
778 If we find one, invoke FNS->USE. */
779
780 static void
781 sra_walk_expr (tree *expr_p, gimple_stmt_iterator *gsi, bool is_output,
782 const struct sra_walk_fns *fns)
783 {
784 tree expr = *expr_p;
785 tree inner = expr;
786 bool disable_scalarization = false;
787 bool use_all_p = false;
788
789 /* We're looking to collect a reference expression between EXPR and INNER,
790 such that INNER is a scalarizable decl and all other nodes through EXPR
791 are references that we can scalarize. If we come across something that
792 we can't scalarize, we reset EXPR. This has the effect of making it
793 appear that we're referring to the larger expression as a whole. */
794
795 while (1)
796 switch (TREE_CODE (inner))
797 {
798 case VAR_DECL:
799 case PARM_DECL:
800 case RESULT_DECL:
801 /* If there is a scalarizable decl at the bottom, then process it. */
802 if (is_sra_candidate_decl (inner))
803 {
804 struct sra_elt *elt = maybe_lookup_element_for_expr (expr);
805 if (disable_scalarization)
806 elt->cannot_scalarize = true;
807 else
808 fns->use (elt, expr_p, gsi, is_output, use_all_p);
809 }
810 return;
811
812 case ARRAY_REF:
813 /* Non-constant index means any member may be accessed. Prevent the
814 expression from being scalarized. If we were to treat this as a
815 reference to the whole array, we can wind up with a single dynamic
816 index reference inside a loop being overridden by several constant
817 index references during loop setup. It's possible that this could
818 be avoided by using dynamic usage counts based on BB trip counts
819 (based on loop analysis or profiling), but that hardly seems worth
820 the effort. */
821 /* ??? Hack. Figure out how to push this into the scan routines
822 without duplicating too much code. */
823 if (!in_array_bounds_p (inner))
824 {
825 disable_scalarization = true;
826 goto use_all;
827 }
828 /* ??? Are we assured that non-constant bounds and stride will have
829 the same value everywhere? I don't think Fortran will... */
830 if (TREE_OPERAND (inner, 2) || TREE_OPERAND (inner, 3))
831 goto use_all;
832 inner = TREE_OPERAND (inner, 0);
833 break;
834
835 case ARRAY_RANGE_REF:
836 if (!range_in_array_bounds_p (inner))
837 {
838 disable_scalarization = true;
839 goto use_all;
840 }
841 /* ??? See above non-constant bounds and stride . */
842 if (TREE_OPERAND (inner, 2) || TREE_OPERAND (inner, 3))
843 goto use_all;
844 inner = TREE_OPERAND (inner, 0);
845 break;
846
847 case COMPONENT_REF:
848 {
849 tree type = TREE_TYPE (TREE_OPERAND (inner, 0));
850 /* Don't look through unions. */
851 if (TREE_CODE (type) != RECORD_TYPE)
852 goto use_all;
853 /* Neither through variable-sized records. */
854 if (TYPE_SIZE (type) == NULL_TREE
855 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
856 goto use_all;
857 inner = TREE_OPERAND (inner, 0);
858 }
859 break;
860
861 case REALPART_EXPR:
862 case IMAGPART_EXPR:
863 inner = TREE_OPERAND (inner, 0);
864 break;
865
866 case BIT_FIELD_REF:
867 /* A bit field reference to a specific vector is scalarized but for
868 ones for inputs need to be marked as used on the left hand size so
869 when we scalarize it, we can mark that variable as non renamable. */
870 if (is_output
871 && TREE_CODE (TREE_TYPE (TREE_OPERAND (inner, 0))) == VECTOR_TYPE)
872 {
873 struct sra_elt *elt
874 = maybe_lookup_element_for_expr (TREE_OPERAND (inner, 0));
875 if (elt)
876 elt->is_vector_lhs = true;
877 }
878
879 /* A bit field reference (access to *multiple* fields simultaneously)
880 is not currently scalarized. Consider this an access to the full
881 outer element, to which walk_tree will bring us next. */
882 goto use_all;
883
884 CASE_CONVERT:
885 /* Similarly, a nop explicitly wants to look at an object in a
886 type other than the one we've scalarized. */
887 goto use_all;
888
889 case VIEW_CONVERT_EXPR:
890 /* Likewise for a view conversion, but with an additional twist:
891 it can be on the LHS and, in this case, an access to the full
892 outer element would mean a killing def. So we need to punt
893 if we haven't already a full access to the current element,
894 because we cannot pretend to have a killing def if we only
895 have a partial access at some level. */
896 if (is_output && !use_all_p && inner != expr)
897 disable_scalarization = true;
898 goto use_all;
899
900 case WITH_SIZE_EXPR:
901 /* This is a transparent wrapper. The entire inner expression really
902 is being used. */
903 goto use_all;
904
905 use_all:
906 expr_p = &TREE_OPERAND (inner, 0);
907 inner = expr = *expr_p;
908 use_all_p = true;
909 break;
910
911 default:
912 #ifdef ENABLE_CHECKING
913 /* Validate that we're not missing any references. */
914 gcc_assert (!walk_tree (&inner, sra_find_candidate_decl, NULL, NULL));
915 #endif
916 return;
917 }
918 }
919
920 /* Walk the arguments of a GIMPLE_CALL looking for scalarizable aggregates.
921 If we find one, invoke FNS->USE. */
922
923 static void
924 sra_walk_gimple_call (gimple stmt, gimple_stmt_iterator *gsi,
925 const struct sra_walk_fns *fns)
926 {
927 int i;
928 int nargs = gimple_call_num_args (stmt);
929
930 for (i = 0; i < nargs; i++)
931 sra_walk_expr (gimple_call_arg_ptr (stmt, i), gsi, false, fns);
932
933 if (gimple_call_lhs (stmt))
934 sra_walk_expr (gimple_call_lhs_ptr (stmt), gsi, true, fns);
935 }
936
937 /* Walk the inputs and outputs of a GIMPLE_ASM looking for scalarizable
938 aggregates. If we find one, invoke FNS->USE. */
939
940 static void
941 sra_walk_gimple_asm (gimple stmt, gimple_stmt_iterator *gsi,
942 const struct sra_walk_fns *fns)
943 {
944 size_t i;
945 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
946 sra_walk_expr (&TREE_VALUE (gimple_asm_input_op (stmt, i)), gsi, false, fns);
947 for (i = 0; i < gimple_asm_noutputs (stmt); i++)
948 sra_walk_expr (&TREE_VALUE (gimple_asm_output_op (stmt, i)), gsi, true, fns);
949 }
950
951 /* Walk a GIMPLE_ASSIGN and categorize the assignment appropriately. */
952
953 static void
954 sra_walk_gimple_assign (gimple stmt, gimple_stmt_iterator *gsi,
955 const struct sra_walk_fns *fns)
956 {
957 struct sra_elt *lhs_elt = NULL, *rhs_elt = NULL;
958 tree lhs, rhs;
959
960 /* If there is more than 1 element on the RHS, only walk the lhs. */
961 if (!gimple_assign_single_p (stmt))
962 {
963 sra_walk_expr (gimple_assign_lhs_ptr (stmt), gsi, true, fns);
964 return;
965 }
966
967 lhs = gimple_assign_lhs (stmt);
968 rhs = gimple_assign_rhs1 (stmt);
969 lhs_elt = maybe_lookup_element_for_expr (lhs);
970 rhs_elt = maybe_lookup_element_for_expr (rhs);
971
972 /* If both sides are scalarizable, this is a COPY operation. */
973 if (lhs_elt && rhs_elt)
974 {
975 fns->copy (lhs_elt, rhs_elt, gsi);
976 return;
977 }
978
979 /* If the RHS is scalarizable, handle it. There are only two cases. */
980 if (rhs_elt)
981 {
982 if (!rhs_elt->is_scalar && !TREE_SIDE_EFFECTS (lhs))
983 fns->ldst (rhs_elt, lhs, gsi, false);
984 else
985 fns->use (rhs_elt, gimple_assign_rhs1_ptr (stmt), gsi, false, false);
986 }
987
988 /* If it isn't scalarizable, there may be scalarizable variables within, so
989 check for a call or else walk the RHS to see if we need to do any
990 copy-in operations. We need to do it before the LHS is scalarized so
991 that the statements get inserted in the proper place, before any
992 copy-out operations. */
993 else
994 sra_walk_expr (gimple_assign_rhs1_ptr (stmt), gsi, false, fns);
995
996 /* Likewise, handle the LHS being scalarizable. We have cases similar
997 to those above, but also want to handle RHS being constant. */
998 if (lhs_elt)
999 {
1000 /* If this is an assignment from a constant, or constructor, then
1001 we have access to all of the elements individually. Invoke INIT. */
1002 if (TREE_CODE (rhs) == COMPLEX_EXPR
1003 || TREE_CODE (rhs) == COMPLEX_CST
1004 || TREE_CODE (rhs) == CONSTRUCTOR)
1005 fns->init (lhs_elt, rhs, gsi);
1006
1007 /* If this is an assignment from read-only memory, treat this as if
1008 we'd been passed the constructor directly. Invoke INIT. */
1009 else if (TREE_CODE (rhs) == VAR_DECL
1010 && TREE_STATIC (rhs)
1011 && !DECL_EXTERNAL (rhs)
1012 && TREE_READONLY (rhs)
1013 && targetm.binds_local_p (rhs))
1014 fns->init (lhs_elt, DECL_INITIAL (rhs), gsi);
1015
1016 /* If this is a copy from a non-scalarizable lvalue, invoke LDST.
1017 The lvalue requirement prevents us from trying to directly scalarize
1018 the result of a function call. Which would result in trying to call
1019 the function multiple times, and other evil things. */
1020 else if (!lhs_elt->is_scalar
1021 && !TREE_SIDE_EFFECTS (rhs) && is_gimple_addressable (rhs))
1022 fns->ldst (lhs_elt, rhs, gsi, true);
1023
1024 /* Otherwise we're being used in some context that requires the
1025 aggregate to be seen as a whole. Invoke USE. */
1026 else
1027 fns->use (lhs_elt, gimple_assign_lhs_ptr (stmt), gsi, true, false);
1028 }
1029
1030 /* Similarly to above, LHS_ELT being null only means that the LHS as a
1031 whole is not a scalarizable reference. There may be occurrences of
1032 scalarizable variables within, which implies a USE. */
1033 else
1034 sra_walk_expr (gimple_assign_lhs_ptr (stmt), gsi, true, fns);
1035 }
1036
1037 /* Entry point to the walk functions. Search the entire function,
1038 invoking the callbacks in FNS on each of the references to
1039 scalarizable variables. */
1040
1041 static void
1042 sra_walk_function (const struct sra_walk_fns *fns)
1043 {
1044 basic_block bb;
1045 gimple_stmt_iterator si, ni;
1046
1047 /* ??? Phase 4 could derive some benefit to walking the function in
1048 dominator tree order. */
1049
1050 FOR_EACH_BB (bb)
1051 for (si = gsi_start_bb (bb); !gsi_end_p (si); si = ni)
1052 {
1053 gimple stmt;
1054
1055 stmt = gsi_stmt (si);
1056
1057 ni = si;
1058 gsi_next (&ni);
1059
1060 /* If the statement does not reference memory, then it doesn't
1061 make any structure references that we care about. */
1062 if (!gimple_references_memory_p (stmt))
1063 continue;
1064
1065 switch (gimple_code (stmt))
1066 {
1067 case GIMPLE_RETURN:
1068 /* If we have "return <retval>" then the return value is
1069 already exposed for our pleasure. Walk it as a USE to
1070 force all the components back in place for the return.
1071 */
1072 if (gimple_return_retval (stmt) == NULL_TREE)
1073 ;
1074 else
1075 sra_walk_expr (gimple_return_retval_ptr (stmt), &si, false,
1076 fns);
1077 break;
1078
1079 case GIMPLE_ASSIGN:
1080 sra_walk_gimple_assign (stmt, &si, fns);
1081 break;
1082 case GIMPLE_CALL:
1083 sra_walk_gimple_call (stmt, &si, fns);
1084 break;
1085 case GIMPLE_ASM:
1086 sra_walk_gimple_asm (stmt, &si, fns);
1087 break;
1088
1089 default:
1090 break;
1091 }
1092 }
1093 }
1094 \f
1095 /* Phase One: Scan all referenced variables in the program looking for
1096 structures that could be decomposed. */
1097
1098 static bool
1099 find_candidates_for_sra (void)
1100 {
1101 bool any_set = false;
1102 tree var;
1103 referenced_var_iterator rvi;
1104
1105 FOR_EACH_REFERENCED_VAR (var, rvi)
1106 {
1107 if (decl_can_be_decomposed_p (var))
1108 {
1109 bitmap_set_bit (sra_candidates, DECL_UID (var));
1110 any_set = true;
1111 }
1112 }
1113
1114 return any_set;
1115 }
1116
1117 \f
1118 /* Phase Two: Scan all references to scalarizable variables. Count the
1119 number of times they are used or copied respectively. */
1120
1121 /* Callbacks to fill in SRA_WALK_FNS. Everything but USE is
1122 considered a copy, because we can decompose the reference such that
1123 the sub-elements needn't be contiguous. */
1124
1125 static void
1126 scan_use (struct sra_elt *elt, tree *expr_p ATTRIBUTE_UNUSED,
1127 gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED,
1128 bool is_output ATTRIBUTE_UNUSED, bool use_all ATTRIBUTE_UNUSED)
1129 {
1130 elt->n_uses += 1;
1131 }
1132
1133 static void
1134 scan_copy (struct sra_elt *lhs_elt, struct sra_elt *rhs_elt,
1135 gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED)
1136 {
1137 lhs_elt->n_copies += 1;
1138 rhs_elt->n_copies += 1;
1139 }
1140
1141 static void
1142 scan_init (struct sra_elt *lhs_elt, tree rhs ATTRIBUTE_UNUSED,
1143 gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED)
1144 {
1145 lhs_elt->n_copies += 1;
1146 }
1147
1148 static void
1149 scan_ldst (struct sra_elt *elt, tree other ATTRIBUTE_UNUSED,
1150 gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED,
1151 bool is_output ATTRIBUTE_UNUSED)
1152 {
1153 elt->n_copies += 1;
1154 }
1155
1156 /* Dump the values we collected during the scanning phase. */
1157
1158 static void
1159 scan_dump (struct sra_elt *elt)
1160 {
1161 struct sra_elt *c;
1162
1163 dump_sra_elt_name (dump_file, elt);
1164 fprintf (dump_file, ": n_uses=%u n_copies=%u\n", elt->n_uses, elt->n_copies);
1165
1166 for (c = elt->children; c ; c = c->sibling)
1167 scan_dump (c);
1168
1169 for (c = elt->groups; c ; c = c->sibling)
1170 scan_dump (c);
1171 }
1172
1173 /* Entry point to phase 2. Scan the entire function, building up
1174 scalarization data structures, recording copies and uses. */
1175
1176 static void
1177 scan_function (void)
1178 {
1179 static const struct sra_walk_fns fns = {
1180 scan_use, scan_copy, scan_init, scan_ldst, true
1181 };
1182 bitmap_iterator bi;
1183
1184 sra_walk_function (&fns);
1185
1186 if (dump_file && (dump_flags & TDF_DETAILS))
1187 {
1188 unsigned i;
1189
1190 fputs ("\nScan results:\n", dump_file);
1191 EXECUTE_IF_SET_IN_BITMAP (sra_candidates, 0, i, bi)
1192 {
1193 tree var = referenced_var (i);
1194 struct sra_elt *elt = lookup_element (NULL, var, NULL, NO_INSERT);
1195 if (elt)
1196 scan_dump (elt);
1197 }
1198 fputc ('\n', dump_file);
1199 }
1200 }
1201 \f
1202 /* Phase Three: Make decisions about which variables to scalarize, if any.
1203 All elements to be scalarized have replacement variables made for them. */
1204
1205 /* A subroutine of build_element_name. Recursively build the element
1206 name on the obstack. */
1207
1208 static void
1209 build_element_name_1 (struct sra_elt *elt)
1210 {
1211 tree t;
1212 char buffer[32];
1213
1214 if (elt->parent)
1215 {
1216 build_element_name_1 (elt->parent);
1217 obstack_1grow (&sra_obstack, '$');
1218
1219 if (TREE_CODE (elt->parent->type) == COMPLEX_TYPE)
1220 {
1221 if (elt->element == integer_zero_node)
1222 obstack_grow (&sra_obstack, "real", 4);
1223 else
1224 obstack_grow (&sra_obstack, "imag", 4);
1225 return;
1226 }
1227 }
1228
1229 t = elt->element;
1230 if (TREE_CODE (t) == INTEGER_CST)
1231 {
1232 /* ??? Eh. Don't bother doing double-wide printing. */
1233 sprintf (buffer, HOST_WIDE_INT_PRINT_DEC, TREE_INT_CST_LOW (t));
1234 obstack_grow (&sra_obstack, buffer, strlen (buffer));
1235 }
1236 else if (TREE_CODE (t) == BIT_FIELD_REF)
1237 {
1238 sprintf (buffer, "B" HOST_WIDE_INT_PRINT_DEC,
1239 tree_low_cst (TREE_OPERAND (t, 2), 1));
1240 obstack_grow (&sra_obstack, buffer, strlen (buffer));
1241 sprintf (buffer, "F" HOST_WIDE_INT_PRINT_DEC,
1242 tree_low_cst (TREE_OPERAND (t, 1), 1));
1243 obstack_grow (&sra_obstack, buffer, strlen (buffer));
1244 }
1245 else
1246 {
1247 tree name = DECL_NAME (t);
1248 if (name)
1249 obstack_grow (&sra_obstack, IDENTIFIER_POINTER (name),
1250 IDENTIFIER_LENGTH (name));
1251 else
1252 {
1253 sprintf (buffer, "D%u", DECL_UID (t));
1254 obstack_grow (&sra_obstack, buffer, strlen (buffer));
1255 }
1256 }
1257 }
1258
1259 /* Construct a pretty variable name for an element's replacement variable.
1260 The name is built on the obstack. */
1261
1262 static char *
1263 build_element_name (struct sra_elt *elt)
1264 {
1265 build_element_name_1 (elt);
1266 obstack_1grow (&sra_obstack, '\0');
1267 return XOBFINISH (&sra_obstack, char *);
1268 }
1269
1270 /* Insert a gimple_seq SEQ on all the outgoing edges out of BB. Note that
1271 if BB has more than one edge, STMT will be replicated for each edge.
1272 Also, abnormal edges will be ignored. */
1273
1274 static void
1275 insert_edge_copies_seq (gimple_seq seq, basic_block bb)
1276 {
1277 edge e;
1278 edge_iterator ei;
1279 unsigned n_copies = -1;
1280
1281 FOR_EACH_EDGE (e, ei, bb->succs)
1282 if (!(e->flags & EDGE_ABNORMAL))
1283 n_copies++;
1284
1285 FOR_EACH_EDGE (e, ei, bb->succs)
1286 if (!(e->flags & EDGE_ABNORMAL))
1287 gsi_insert_seq_on_edge (e, n_copies-- > 0 ? gimple_seq_copy (seq) : seq);
1288 }
1289
1290 /* Instantiate an element as an independent variable. */
1291
1292 static void
1293 instantiate_element (struct sra_elt *elt)
1294 {
1295 struct sra_elt *base_elt;
1296 tree var, base;
1297 bool nowarn = TREE_NO_WARNING (elt->element);
1298
1299 for (base_elt = elt; base_elt->parent; base_elt = base_elt->parent)
1300 if (!nowarn)
1301 nowarn = TREE_NO_WARNING (base_elt->parent->element);
1302 base = base_elt->element;
1303
1304 elt->replacement = var = make_rename_temp (elt->type, "SR");
1305
1306 if (DECL_P (elt->element)
1307 && !tree_int_cst_equal (DECL_SIZE (var), DECL_SIZE (elt->element)))
1308 {
1309 DECL_SIZE (var) = DECL_SIZE (elt->element);
1310 DECL_SIZE_UNIT (var) = DECL_SIZE_UNIT (elt->element);
1311
1312 elt->in_bitfld_block = 1;
1313 elt->replacement = fold_build3 (BIT_FIELD_REF, elt->type, var,
1314 DECL_SIZE (var),
1315 BYTES_BIG_ENDIAN
1316 ? size_binop (MINUS_EXPR,
1317 TYPE_SIZE (elt->type),
1318 DECL_SIZE (var))
1319 : bitsize_int (0));
1320 }
1321
1322 /* For vectors, if used on the left hand side with BIT_FIELD_REF,
1323 they are not a gimple register. */
1324 if (TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE && elt->is_vector_lhs)
1325 DECL_GIMPLE_REG_P (var) = 0;
1326
1327 DECL_SOURCE_LOCATION (var) = DECL_SOURCE_LOCATION (base);
1328 DECL_ARTIFICIAL (var) = 1;
1329
1330 if (TREE_THIS_VOLATILE (elt->type))
1331 {
1332 TREE_THIS_VOLATILE (var) = 1;
1333 TREE_SIDE_EFFECTS (var) = 1;
1334 }
1335
1336 if (DECL_NAME (base) && !DECL_IGNORED_P (base))
1337 {
1338 char *pretty_name = build_element_name (elt);
1339 DECL_NAME (var) = get_identifier (pretty_name);
1340 obstack_free (&sra_obstack, pretty_name);
1341
1342 SET_DECL_DEBUG_EXPR (var, generate_element_ref (elt));
1343 DECL_DEBUG_EXPR_IS_FROM (var) = 1;
1344
1345 DECL_IGNORED_P (var) = 0;
1346 TREE_NO_WARNING (var) = nowarn;
1347 }
1348 else
1349 {
1350 DECL_IGNORED_P (var) = 1;
1351 /* ??? We can't generate any warning that would be meaningful. */
1352 TREE_NO_WARNING (var) = 1;
1353 }
1354
1355 /* Zero-initialize bit-field scalarization variables, to avoid
1356 triggering undefined behavior. */
1357 if (TREE_CODE (elt->element) == BIT_FIELD_REF
1358 || (var != elt->replacement
1359 && TREE_CODE (elt->replacement) == BIT_FIELD_REF))
1360 {
1361 gimple_seq init = sra_build_assignment (var,
1362 fold_convert (TREE_TYPE (var),
1363 integer_zero_node)
1364 );
1365 insert_edge_copies_seq (init, ENTRY_BLOCK_PTR);
1366 mark_all_v_defs_seq (init);
1367 }
1368
1369 if (dump_file)
1370 {
1371 fputs (" ", dump_file);
1372 dump_sra_elt_name (dump_file, elt);
1373 fputs (" -> ", dump_file);
1374 print_generic_expr (dump_file, var, dump_flags);
1375 fputc ('\n', dump_file);
1376 }
1377 }
1378
1379 /* Make one pass across an element tree deciding whether or not it's
1380 profitable to instantiate individual leaf scalars.
1381
1382 PARENT_USES and PARENT_COPIES are the sum of the N_USES and N_COPIES
1383 fields all the way up the tree. */
1384
1385 static void
1386 decide_instantiation_1 (struct sra_elt *elt, unsigned int parent_uses,
1387 unsigned int parent_copies)
1388 {
1389 if (dump_file && !elt->parent)
1390 {
1391 fputs ("Initial instantiation for ", dump_file);
1392 dump_sra_elt_name (dump_file, elt);
1393 fputc ('\n', dump_file);
1394 }
1395
1396 if (elt->cannot_scalarize)
1397 return;
1398
1399 if (elt->is_scalar)
1400 {
1401 /* The decision is simple: instantiate if we're used more frequently
1402 than the parent needs to be seen as a complete unit. */
1403 if (elt->n_uses + elt->n_copies + parent_copies > parent_uses)
1404 instantiate_element (elt);
1405 }
1406 else
1407 {
1408 struct sra_elt *c, *group;
1409 unsigned int this_uses = elt->n_uses + parent_uses;
1410 unsigned int this_copies = elt->n_copies + parent_copies;
1411
1412 /* Consider groups of sub-elements as weighing in favour of
1413 instantiation whatever their size. */
1414 for (group = elt->groups; group ; group = group->sibling)
1415 FOR_EACH_ACTUAL_CHILD (c, group)
1416 {
1417 c->n_uses += group->n_uses;
1418 c->n_copies += group->n_copies;
1419 }
1420
1421 for (c = elt->children; c ; c = c->sibling)
1422 decide_instantiation_1 (c, this_uses, this_copies);
1423 }
1424 }
1425
1426 /* Compute the size and number of all instantiated elements below ELT.
1427 We will only care about this if the size of the complete structure
1428 fits in a HOST_WIDE_INT, so we don't have to worry about overflow. */
1429
1430 static unsigned int
1431 sum_instantiated_sizes (struct sra_elt *elt, unsigned HOST_WIDE_INT *sizep)
1432 {
1433 if (elt->replacement)
1434 {
1435 *sizep += TREE_INT_CST_LOW (TYPE_SIZE_UNIT (elt->type));
1436 return 1;
1437 }
1438 else
1439 {
1440 struct sra_elt *c;
1441 unsigned int count = 0;
1442
1443 for (c = elt->children; c ; c = c->sibling)
1444 count += sum_instantiated_sizes (c, sizep);
1445
1446 return count;
1447 }
1448 }
1449
1450 /* Instantiate fields in ELT->TYPE that are not currently present as
1451 children of ELT. */
1452
1453 static void instantiate_missing_elements (struct sra_elt *elt);
1454
1455 static struct sra_elt *
1456 instantiate_missing_elements_1 (struct sra_elt *elt, tree child, tree type)
1457 {
1458 struct sra_elt *sub = lookup_element (elt, child, type, INSERT);
1459 if (sub->is_scalar)
1460 {
1461 if (sub->replacement == NULL)
1462 instantiate_element (sub);
1463 }
1464 else
1465 instantiate_missing_elements (sub);
1466 return sub;
1467 }
1468
1469 /* Obtain the canonical type for field F of ELEMENT. */
1470
1471 static tree
1472 canon_type_for_field (tree f, tree element)
1473 {
1474 tree field_type = TREE_TYPE (f);
1475
1476 /* canonicalize_component_ref() unwidens some bit-field types (not
1477 marked as DECL_BIT_FIELD in C++), so we must do the same, lest we
1478 may introduce type mismatches. */
1479 if (INTEGRAL_TYPE_P (field_type)
1480 && DECL_MODE (f) != TYPE_MODE (field_type))
1481 field_type = TREE_TYPE (get_unwidened (build3 (COMPONENT_REF,
1482 field_type,
1483 element,
1484 f, NULL_TREE),
1485 NULL_TREE));
1486
1487 return field_type;
1488 }
1489
1490 /* Look for adjacent fields of ELT starting at F that we'd like to
1491 scalarize as a single variable. Return the last field of the
1492 group. */
1493
1494 static tree
1495 try_instantiate_multiple_fields (struct sra_elt *elt, tree f)
1496 {
1497 int count;
1498 unsigned HOST_WIDE_INT align, bit, size, alchk;
1499 enum machine_mode mode;
1500 tree first = f, prev;
1501 tree type, var;
1502 struct sra_elt *block;
1503
1504 /* Point fields are typically best handled as standalone entities. */
1505 if (POINTER_TYPE_P (TREE_TYPE (f)))
1506 return f;
1507
1508 if (!is_sra_scalar_type (TREE_TYPE (f))
1509 || !host_integerp (DECL_FIELD_OFFSET (f), 1)
1510 || !host_integerp (DECL_FIELD_BIT_OFFSET (f), 1)
1511 || !host_integerp (DECL_SIZE (f), 1)
1512 || lookup_element (elt, f, NULL, NO_INSERT))
1513 return f;
1514
1515 block = elt;
1516
1517 /* For complex and array objects, there are going to be integer
1518 literals as child elements. In this case, we can't just take the
1519 alignment and mode of the decl, so we instead rely on the element
1520 type.
1521
1522 ??? We could try to infer additional alignment from the full
1523 object declaration and the location of the sub-elements we're
1524 accessing. */
1525 for (count = 0; !DECL_P (block->element); count++)
1526 block = block->parent;
1527
1528 align = DECL_ALIGN (block->element);
1529 alchk = GET_MODE_BITSIZE (DECL_MODE (block->element));
1530
1531 if (count)
1532 {
1533 type = TREE_TYPE (block->element);
1534 while (count--)
1535 type = TREE_TYPE (type);
1536
1537 align = TYPE_ALIGN (type);
1538 alchk = GET_MODE_BITSIZE (TYPE_MODE (type));
1539 }
1540
1541 if (align < alchk)
1542 align = alchk;
1543
1544 /* Coalescing wider fields is probably pointless and
1545 inefficient. */
1546 if (align > BITS_PER_WORD)
1547 align = BITS_PER_WORD;
1548
1549 bit = tree_low_cst (DECL_FIELD_OFFSET (f), 1) * BITS_PER_UNIT
1550 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f), 1);
1551 size = tree_low_cst (DECL_SIZE (f), 1);
1552
1553 alchk = align - 1;
1554 alchk = ~alchk;
1555
1556 if ((bit & alchk) != ((bit + size - 1) & alchk))
1557 return f;
1558
1559 /* Find adjacent fields in the same alignment word. */
1560
1561 for (prev = f, f = TREE_CHAIN (f);
1562 f && TREE_CODE (f) == FIELD_DECL
1563 && is_sra_scalar_type (TREE_TYPE (f))
1564 && host_integerp (DECL_FIELD_OFFSET (f), 1)
1565 && host_integerp (DECL_FIELD_BIT_OFFSET (f), 1)
1566 && host_integerp (DECL_SIZE (f), 1)
1567 && !lookup_element (elt, f, NULL, NO_INSERT);
1568 prev = f, f = TREE_CHAIN (f))
1569 {
1570 unsigned HOST_WIDE_INT nbit, nsize;
1571
1572 nbit = tree_low_cst (DECL_FIELD_OFFSET (f), 1) * BITS_PER_UNIT
1573 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f), 1);
1574 nsize = tree_low_cst (DECL_SIZE (f), 1);
1575
1576 if (bit + size == nbit)
1577 {
1578 if ((bit & alchk) != ((nbit + nsize - 1) & alchk))
1579 {
1580 /* If we're at an alignment boundary, don't bother
1581 growing alignment such that we can include this next
1582 field. */
1583 if ((nbit & alchk)
1584 || GET_MODE_BITSIZE (DECL_MODE (f)) <= align)
1585 break;
1586
1587 align = GET_MODE_BITSIZE (DECL_MODE (f));
1588 alchk = align - 1;
1589 alchk = ~alchk;
1590
1591 if ((bit & alchk) != ((nbit + nsize - 1) & alchk))
1592 break;
1593 }
1594 size += nsize;
1595 }
1596 else if (nbit + nsize == bit)
1597 {
1598 if ((nbit & alchk) != ((bit + size - 1) & alchk))
1599 {
1600 if ((bit & alchk)
1601 || GET_MODE_BITSIZE (DECL_MODE (f)) <= align)
1602 break;
1603
1604 align = GET_MODE_BITSIZE (DECL_MODE (f));
1605 alchk = align - 1;
1606 alchk = ~alchk;
1607
1608 if ((nbit & alchk) != ((bit + size - 1) & alchk))
1609 break;
1610 }
1611 bit = nbit;
1612 size += nsize;
1613 }
1614 else
1615 break;
1616 }
1617
1618 f = prev;
1619
1620 if (f == first)
1621 return f;
1622
1623 gcc_assert ((bit & alchk) == ((bit + size - 1) & alchk));
1624
1625 /* Try to widen the bit range so as to cover padding bits as well. */
1626
1627 if ((bit & ~alchk) || size != align)
1628 {
1629 unsigned HOST_WIDE_INT mbit = bit & alchk;
1630 unsigned HOST_WIDE_INT msize = align;
1631
1632 for (f = TYPE_FIELDS (elt->type);
1633 f; f = TREE_CHAIN (f))
1634 {
1635 unsigned HOST_WIDE_INT fbit, fsize;
1636
1637 /* Skip the fields from first to prev. */
1638 if (f == first)
1639 {
1640 f = prev;
1641 continue;
1642 }
1643
1644 if (!(TREE_CODE (f) == FIELD_DECL
1645 && host_integerp (DECL_FIELD_OFFSET (f), 1)
1646 && host_integerp (DECL_FIELD_BIT_OFFSET (f), 1)))
1647 continue;
1648
1649 fbit = tree_low_cst (DECL_FIELD_OFFSET (f), 1) * BITS_PER_UNIT
1650 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f), 1);
1651
1652 /* If we're past the selected word, we're fine. */
1653 if ((bit & alchk) < (fbit & alchk))
1654 continue;
1655
1656 if (host_integerp (DECL_SIZE (f), 1))
1657 fsize = tree_low_cst (DECL_SIZE (f), 1);
1658 else
1659 /* Assume a variable-sized field takes up all space till
1660 the end of the word. ??? Endianness issues? */
1661 fsize = align - (fbit & alchk);
1662
1663 if ((fbit & alchk) < (bit & alchk))
1664 {
1665 /* A large field might start at a previous word and
1666 extend into the selected word. Exclude those
1667 bits. ??? Endianness issues? */
1668 HOST_WIDE_INT diff = fbit + fsize - mbit;
1669
1670 if (diff <= 0)
1671 continue;
1672
1673 mbit += diff;
1674 msize -= diff;
1675 }
1676 else
1677 {
1678 /* Non-overlapping, great. */
1679 if (fbit + fsize <= mbit
1680 || mbit + msize <= fbit)
1681 continue;
1682
1683 if (fbit <= mbit)
1684 {
1685 unsigned HOST_WIDE_INT diff = fbit + fsize - mbit;
1686 mbit += diff;
1687 msize -= diff;
1688 }
1689 else if (fbit > mbit)
1690 msize -= (mbit + msize - fbit);
1691 else
1692 gcc_unreachable ();
1693 }
1694 }
1695
1696 bit = mbit;
1697 size = msize;
1698 }
1699
1700 /* Now we know the bit range we're interested in. Find the smallest
1701 machine mode we can use to access it. */
1702
1703 for (mode = smallest_mode_for_size (size, MODE_INT);
1704 ;
1705 mode = GET_MODE_WIDER_MODE (mode))
1706 {
1707 gcc_assert (mode != VOIDmode);
1708
1709 alchk = GET_MODE_PRECISION (mode) - 1;
1710 alchk = ~alchk;
1711
1712 if ((bit & alchk) == ((bit + size - 1) & alchk))
1713 break;
1714 }
1715
1716 gcc_assert (~alchk < align);
1717
1718 /* Create the field group as a single variable. */
1719
1720 /* We used to create a type for the mode above, but size turns
1721 to be out not of mode-size. As we need a matching type
1722 to build a BIT_FIELD_REF, use a nonstandard integer type as
1723 fallback. */
1724 type = lang_hooks.types.type_for_size (size, 1);
1725 if (!type || TYPE_PRECISION (type) != size)
1726 type = build_nonstandard_integer_type (size, 1);
1727 gcc_assert (type);
1728 var = build3 (BIT_FIELD_REF, type, NULL_TREE,
1729 bitsize_int (size), bitsize_int (bit));
1730
1731 block = instantiate_missing_elements_1 (elt, var, type);
1732 gcc_assert (block && block->is_scalar);
1733
1734 var = block->replacement;
1735 block->in_bitfld_block = 2;
1736
1737 /* Add the member fields to the group, such that they access
1738 portions of the group variable. */
1739
1740 for (f = first; f != TREE_CHAIN (prev); f = TREE_CHAIN (f))
1741 {
1742 tree field_type = canon_type_for_field (f, elt->element);
1743 struct sra_elt *fld = lookup_element (block, f, field_type, INSERT);
1744
1745 gcc_assert (fld && fld->is_scalar && !fld->replacement);
1746
1747 fld->replacement = fold_build3 (BIT_FIELD_REF, field_type, var,
1748 bitsize_int (TYPE_PRECISION (field_type)),
1749 bitsize_int
1750 ((TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f))
1751 * BITS_PER_UNIT
1752 + (TREE_INT_CST_LOW
1753 (DECL_FIELD_BIT_OFFSET (f)))
1754 - (TREE_INT_CST_LOW
1755 (TREE_OPERAND (block->element, 2))))
1756 & ~alchk));
1757 fld->in_bitfld_block = 1;
1758 }
1759
1760 return prev;
1761 }
1762
1763 static void
1764 instantiate_missing_elements (struct sra_elt *elt)
1765 {
1766 tree type = elt->type;
1767
1768 switch (TREE_CODE (type))
1769 {
1770 case RECORD_TYPE:
1771 {
1772 tree f;
1773 for (f = TYPE_FIELDS (type); f ; f = TREE_CHAIN (f))
1774 if (TREE_CODE (f) == FIELD_DECL)
1775 {
1776 tree last = try_instantiate_multiple_fields (elt, f);
1777
1778 if (last != f)
1779 {
1780 f = last;
1781 continue;
1782 }
1783
1784 instantiate_missing_elements_1 (elt, f,
1785 canon_type_for_field
1786 (f, elt->element));
1787 }
1788 break;
1789 }
1790
1791 case ARRAY_TYPE:
1792 {
1793 tree i, max, subtype;
1794
1795 i = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
1796 max = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1797 subtype = TREE_TYPE (type);
1798
1799 while (1)
1800 {
1801 instantiate_missing_elements_1 (elt, i, subtype);
1802 if (tree_int_cst_equal (i, max))
1803 break;
1804 i = int_const_binop (PLUS_EXPR, i, integer_one_node, true);
1805 }
1806
1807 break;
1808 }
1809
1810 case COMPLEX_TYPE:
1811 type = TREE_TYPE (type);
1812 instantiate_missing_elements_1 (elt, integer_zero_node, type);
1813 instantiate_missing_elements_1 (elt, integer_one_node, type);
1814 break;
1815
1816 default:
1817 gcc_unreachable ();
1818 }
1819 }
1820
1821 /* Return true if there is only one non aggregate field in the record, TYPE.
1822 Return false otherwise. */
1823
1824 static bool
1825 single_scalar_field_in_record_p (tree type)
1826 {
1827 int num_fields = 0;
1828 tree field;
1829 if (TREE_CODE (type) != RECORD_TYPE)
1830 return false;
1831
1832 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1833 if (TREE_CODE (field) == FIELD_DECL)
1834 {
1835 num_fields++;
1836
1837 if (num_fields == 2)
1838 return false;
1839
1840 if (AGGREGATE_TYPE_P (TREE_TYPE (field)))
1841 return false;
1842 }
1843
1844 return true;
1845 }
1846
1847 /* Make one pass across an element tree deciding whether to perform block
1848 or element copies. If we decide on element copies, instantiate all
1849 elements. Return true if there are any instantiated sub-elements. */
1850
1851 static bool
1852 decide_block_copy (struct sra_elt *elt)
1853 {
1854 struct sra_elt *c;
1855 bool any_inst;
1856
1857 /* We shouldn't be invoked on groups of sub-elements as they must
1858 behave like their parent as far as block copy is concerned. */
1859 gcc_assert (!elt->is_group);
1860
1861 /* If scalarization is disabled, respect it. */
1862 if (elt->cannot_scalarize)
1863 {
1864 elt->use_block_copy = 1;
1865
1866 if (dump_file)
1867 {
1868 fputs ("Scalarization disabled for ", dump_file);
1869 dump_sra_elt_name (dump_file, elt);
1870 fputc ('\n', dump_file);
1871 }
1872
1873 /* Disable scalarization of sub-elements */
1874 for (c = elt->children; c; c = c->sibling)
1875 {
1876 c->cannot_scalarize = 1;
1877 decide_block_copy (c);
1878 }
1879
1880 /* Groups behave like their parent. */
1881 for (c = elt->groups; c; c = c->sibling)
1882 {
1883 c->cannot_scalarize = 1;
1884 c->use_block_copy = 1;
1885 }
1886
1887 return false;
1888 }
1889
1890 /* Don't decide if we've no uses and no groups. */
1891 if (elt->n_uses == 0 && elt->n_copies == 0 && elt->groups == NULL)
1892 ;
1893
1894 else if (!elt->is_scalar)
1895 {
1896 tree size_tree = TYPE_SIZE_UNIT (elt->type);
1897 bool use_block_copy = true;
1898
1899 /* Tradeoffs for COMPLEX types pretty much always make it better
1900 to go ahead and split the components. */
1901 if (TREE_CODE (elt->type) == COMPLEX_TYPE)
1902 use_block_copy = false;
1903
1904 /* Don't bother trying to figure out the rest if the structure is
1905 so large we can't do easy arithmetic. This also forces block
1906 copies for variable sized structures. */
1907 else if (host_integerp (size_tree, 1))
1908 {
1909 unsigned HOST_WIDE_INT full_size, inst_size = 0;
1910 unsigned int max_size, max_count, inst_count, full_count;
1911
1912 /* If the sra-max-structure-size parameter is 0, then the
1913 user has not overridden the parameter and we can choose a
1914 sensible default. */
1915 max_size = SRA_MAX_STRUCTURE_SIZE
1916 ? SRA_MAX_STRUCTURE_SIZE
1917 : MOVE_RATIO (optimize_function_for_speed_p (cfun)) * UNITS_PER_WORD;
1918 max_count = SRA_MAX_STRUCTURE_COUNT
1919 ? SRA_MAX_STRUCTURE_COUNT
1920 : MOVE_RATIO (optimize_function_for_speed_p (cfun));
1921
1922 full_size = tree_low_cst (size_tree, 1);
1923 full_count = count_type_elements (elt->type, false);
1924 inst_count = sum_instantiated_sizes (elt, &inst_size);
1925
1926 /* If there is only one scalar field in the record, don't block copy. */
1927 if (single_scalar_field_in_record_p (elt->type))
1928 use_block_copy = false;
1929
1930 /* ??? What to do here. If there are two fields, and we've only
1931 instantiated one, then instantiating the other is clearly a win.
1932 If there are a large number of fields then the size of the copy
1933 is much more of a factor. */
1934
1935 /* If the structure is small, and we've made copies, go ahead
1936 and instantiate, hoping that the copies will go away. */
1937 if (full_size <= max_size
1938 && (full_count - inst_count) <= max_count
1939 && elt->n_copies > elt->n_uses)
1940 use_block_copy = false;
1941 else if (inst_count * 100 >= full_count * SRA_FIELD_STRUCTURE_RATIO
1942 && inst_size * 100 >= full_size * SRA_FIELD_STRUCTURE_RATIO)
1943 use_block_copy = false;
1944
1945 /* In order to avoid block copy, we have to be able to instantiate
1946 all elements of the type. See if this is possible. */
1947 if (!use_block_copy
1948 && (!can_completely_scalarize_p (elt)
1949 || !type_can_instantiate_all_elements (elt->type)))
1950 use_block_copy = true;
1951 }
1952
1953 elt->use_block_copy = use_block_copy;
1954
1955 /* Groups behave like their parent. */
1956 for (c = elt->groups; c; c = c->sibling)
1957 c->use_block_copy = use_block_copy;
1958
1959 if (dump_file)
1960 {
1961 fprintf (dump_file, "Using %s for ",
1962 use_block_copy ? "block-copy" : "element-copy");
1963 dump_sra_elt_name (dump_file, elt);
1964 fputc ('\n', dump_file);
1965 }
1966
1967 if (!use_block_copy)
1968 {
1969 instantiate_missing_elements (elt);
1970 return true;
1971 }
1972 }
1973
1974 any_inst = elt->replacement != NULL;
1975
1976 for (c = elt->children; c ; c = c->sibling)
1977 any_inst |= decide_block_copy (c);
1978
1979 return any_inst;
1980 }
1981
1982 /* Entry point to phase 3. Instantiate scalar replacement variables. */
1983
1984 static void
1985 decide_instantiations (void)
1986 {
1987 unsigned int i;
1988 bool cleared_any;
1989 bitmap_head done_head;
1990 bitmap_iterator bi;
1991
1992 /* We cannot clear bits from a bitmap we're iterating over,
1993 so save up all the bits to clear until the end. */
1994 bitmap_initialize (&done_head, &bitmap_default_obstack);
1995 cleared_any = false;
1996
1997 EXECUTE_IF_SET_IN_BITMAP (sra_candidates, 0, i, bi)
1998 {
1999 tree var = referenced_var (i);
2000 struct sra_elt *elt = lookup_element (NULL, var, NULL, NO_INSERT);
2001 if (elt)
2002 {
2003 decide_instantiation_1 (elt, 0, 0);
2004 if (!decide_block_copy (elt))
2005 elt = NULL;
2006 }
2007 if (!elt)
2008 {
2009 bitmap_set_bit (&done_head, i);
2010 cleared_any = true;
2011 }
2012 }
2013
2014 if (cleared_any)
2015 {
2016 bitmap_and_compl_into (sra_candidates, &done_head);
2017 bitmap_and_compl_into (needs_copy_in, &done_head);
2018 }
2019 bitmap_clear (&done_head);
2020
2021 mark_set_for_renaming (sra_candidates);
2022
2023 if (dump_file)
2024 fputc ('\n', dump_file);
2025 }
2026
2027 \f
2028 /* Phase Four: Update the function to match the replacements created. */
2029
2030 /* Mark all the variables in virtual operands in all the statements in
2031 LIST for renaming. */
2032
2033 static void
2034 mark_all_v_defs_seq (gimple_seq seq)
2035 {
2036 gimple_stmt_iterator gsi;
2037
2038 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
2039 update_stmt_if_modified (gsi_stmt (gsi));
2040 }
2041
2042 /* Mark every replacement under ELT with TREE_NO_WARNING. */
2043
2044 static void
2045 mark_no_warning (struct sra_elt *elt)
2046 {
2047 if (!elt->all_no_warning)
2048 {
2049 if (elt->replacement)
2050 TREE_NO_WARNING (elt->replacement) = 1;
2051 else
2052 {
2053 struct sra_elt *c;
2054 FOR_EACH_ACTUAL_CHILD (c, elt)
2055 mark_no_warning (c);
2056 }
2057 elt->all_no_warning = true;
2058 }
2059 }
2060
2061 /* Build a single level component reference to ELT rooted at BASE. */
2062
2063 static tree
2064 generate_one_element_ref (struct sra_elt *elt, tree base)
2065 {
2066 switch (TREE_CODE (TREE_TYPE (base)))
2067 {
2068 case RECORD_TYPE:
2069 {
2070 tree field = elt->element;
2071
2072 /* We can't test elt->in_bitfld_block here because, when this is
2073 called from instantiate_element, we haven't set this field
2074 yet. */
2075 if (TREE_CODE (field) == BIT_FIELD_REF)
2076 {
2077 tree ret = unshare_expr (field);
2078 TREE_OPERAND (ret, 0) = base;
2079 return ret;
2080 }
2081
2082 /* Watch out for compatible records with differing field lists. */
2083 if (DECL_FIELD_CONTEXT (field) != TYPE_MAIN_VARIANT (TREE_TYPE (base)))
2084 field = find_compatible_field (TREE_TYPE (base), field);
2085
2086 return build3 (COMPONENT_REF, elt->type, base, field, NULL);
2087 }
2088
2089 case ARRAY_TYPE:
2090 if (TREE_CODE (elt->element) == RANGE_EXPR)
2091 return build4 (ARRAY_RANGE_REF, elt->type, base,
2092 TREE_OPERAND (elt->element, 0), NULL, NULL);
2093 else
2094 return build4 (ARRAY_REF, elt->type, base, elt->element, NULL, NULL);
2095
2096 case COMPLEX_TYPE:
2097 if (elt->element == integer_zero_node)
2098 return build1 (REALPART_EXPR, elt->type, base);
2099 else
2100 return build1 (IMAGPART_EXPR, elt->type, base);
2101
2102 default:
2103 gcc_unreachable ();
2104 }
2105 }
2106
2107 /* Build a full component reference to ELT rooted at its native variable. */
2108
2109 static tree
2110 generate_element_ref (struct sra_elt *elt)
2111 {
2112 if (elt->parent)
2113 return generate_one_element_ref (elt, generate_element_ref (elt->parent));
2114 else
2115 return elt->element;
2116 }
2117
2118 /* Return true if BF is a bit-field that we can handle like a scalar. */
2119
2120 static bool
2121 scalar_bitfield_p (tree bf)
2122 {
2123 return (TREE_CODE (bf) == BIT_FIELD_REF
2124 && (is_gimple_reg (TREE_OPERAND (bf, 0))
2125 || (TYPE_MODE (TREE_TYPE (TREE_OPERAND (bf, 0))) != BLKmode
2126 && (!TREE_SIDE_EFFECTS (TREE_OPERAND (bf, 0))
2127 || (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE
2128 (TREE_OPERAND (bf, 0))))
2129 <= BITS_PER_WORD)))));
2130 }
2131
2132 /* Create an assignment statement from SRC to DST. */
2133
2134 static gimple_seq
2135 sra_build_assignment (tree dst, tree src)
2136 {
2137 gimple stmt;
2138 gimple_seq seq = NULL, seq2 = NULL;
2139 /* Turning BIT_FIELD_REFs into bit operations enables other passes
2140 to do a much better job at optimizing the code.
2141 From dst = BIT_FIELD_REF <var, sz, off> we produce
2142
2143 SR.1 = (scalar type) var;
2144 SR.2 = SR.1 >> off;
2145 SR.3 = SR.2 & ((1 << sz) - 1);
2146 ... possible sign extension of SR.3 ...
2147 dst = (destination type) SR.3;
2148 */
2149 if (scalar_bitfield_p (src))
2150 {
2151 tree var, shift, width;
2152 tree utype, stype;
2153 bool unsignedp = (INTEGRAL_TYPE_P (TREE_TYPE (src))
2154 ? TYPE_UNSIGNED (TREE_TYPE (src)) : true);
2155 struct gimplify_ctx gctx;
2156
2157 var = TREE_OPERAND (src, 0);
2158 width = TREE_OPERAND (src, 1);
2159 /* The offset needs to be adjusted to a right shift quantity
2160 depending on the endianness. */
2161 if (BYTES_BIG_ENDIAN)
2162 {
2163 tree tmp = size_binop (PLUS_EXPR, width, TREE_OPERAND (src, 2));
2164 shift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), tmp);
2165 }
2166 else
2167 shift = TREE_OPERAND (src, 2);
2168
2169 /* In weird cases we have non-integral types for the source or
2170 destination object.
2171 ??? For unknown reasons we also want an unsigned scalar type. */
2172 stype = TREE_TYPE (var);
2173 if (!INTEGRAL_TYPE_P (stype))
2174 stype = lang_hooks.types.type_for_size (TREE_INT_CST_LOW
2175 (TYPE_SIZE (stype)), 1);
2176 else if (!TYPE_UNSIGNED (stype))
2177 stype = unsigned_type_for (stype);
2178
2179 utype = TREE_TYPE (dst);
2180 if (!INTEGRAL_TYPE_P (utype))
2181 utype = lang_hooks.types.type_for_size (TREE_INT_CST_LOW
2182 (TYPE_SIZE (utype)), 1);
2183 else if (!TYPE_UNSIGNED (utype))
2184 utype = unsigned_type_for (utype);
2185
2186 /* Convert the base var of the BIT_FIELD_REF to the scalar type
2187 we use for computation if we cannot use it directly. */
2188 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
2189 var = fold_convert (stype, var);
2190 else
2191 var = fold_build1 (VIEW_CONVERT_EXPR, stype, var);
2192
2193 if (!integer_zerop (shift))
2194 var = fold_build2 (RSHIFT_EXPR, stype, var, shift);
2195
2196 /* If we need a masking operation, produce one. */
2197 if (TREE_INT_CST_LOW (width) == TYPE_PRECISION (stype))
2198 unsignedp = true;
2199 else
2200 {
2201 tree one = build_int_cst_wide (stype, 1, 0);
2202 tree mask = int_const_binop (LSHIFT_EXPR, one, width, 0);
2203 mask = int_const_binop (MINUS_EXPR, mask, one, 0);
2204 var = fold_build2 (BIT_AND_EXPR, stype, var, mask);
2205 }
2206
2207 /* After shifting and masking, convert to the target type. */
2208 var = fold_convert (utype, var);
2209
2210 /* Perform sign extension, if required.
2211 ??? This should never be necessary. */
2212 if (!unsignedp)
2213 {
2214 tree signbit = int_const_binop (LSHIFT_EXPR,
2215 build_int_cst_wide (utype, 1, 0),
2216 size_binop (MINUS_EXPR, width,
2217 bitsize_int (1)), 0);
2218
2219 var = fold_build2 (BIT_XOR_EXPR, utype, var, signbit);
2220 var = fold_build2 (MINUS_EXPR, utype, var, signbit);
2221 }
2222
2223 /* fold_build3 (BIT_FIELD_REF, ...) sometimes returns a cast. */
2224 STRIP_NOPS (dst);
2225
2226 /* Finally, move and convert to the destination. */
2227 if (INTEGRAL_TYPE_P (TREE_TYPE (dst)))
2228 var = fold_convert (TREE_TYPE (dst), var);
2229 else
2230 var = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (dst), var);
2231
2232 push_gimplify_context (&gctx);
2233 gctx.allow_rhs_cond_expr = true;
2234
2235 gimplify_assign (dst, var, &seq);
2236
2237 if (gimple_referenced_vars (cfun))
2238 for (var = gctx.temps; var; var = TREE_CHAIN (var))
2239 {
2240 add_referenced_var (var);
2241 mark_sym_for_renaming (var);
2242 }
2243 pop_gimplify_context (NULL);
2244
2245 return seq;
2246 }
2247
2248 /* fold_build3 (BIT_FIELD_REF, ...) sometimes returns a cast. */
2249 if (CONVERT_EXPR_P (dst))
2250 {
2251 STRIP_NOPS (dst);
2252 src = fold_convert (TREE_TYPE (dst), src);
2253 }
2254 /* It was hoped that we could perform some type sanity checking
2255 here, but since front-ends can emit accesses of fields in types
2256 different from their nominal types and copy structures containing
2257 them as a whole, we'd have to handle such differences here.
2258 Since such accesses under different types require compatibility
2259 anyway, there's little point in making tests and/or adding
2260 conversions to ensure the types of src and dst are the same.
2261 So we just assume type differences at this point are ok.
2262 The only exception we make here are pointer types, which can be different
2263 in e.g. structurally equal, but non-identical RECORD_TYPEs. */
2264 else if (POINTER_TYPE_P (TREE_TYPE (dst))
2265 && !useless_type_conversion_p (TREE_TYPE (dst), TREE_TYPE (src)))
2266 src = fold_convert (TREE_TYPE (dst), src);
2267
2268 /* ??? Only call the gimplifier if we need to. Otherwise we may
2269 end up substituting with DECL_VALUE_EXPR - see PR37380. */
2270 if (!handled_component_p (src)
2271 && !SSA_VAR_P (src))
2272 {
2273 src = force_gimple_operand (src, &seq2, false, NULL_TREE);
2274 gimple_seq_add_seq (&seq, seq2);
2275 }
2276 stmt = gimple_build_assign (dst, src);
2277 gimple_seq_add_stmt (&seq, stmt);
2278 return seq;
2279 }
2280
2281 /* BIT_FIELD_REFs must not be shared. sra_build_elt_assignment()
2282 takes care of assignments, but we must create copies for uses. */
2283 #define REPLDUP(t) (TREE_CODE (t) != BIT_FIELD_REF ? (t) : unshare_expr (t))
2284
2285 /* Emit an assignment from SRC to DST, but if DST is a scalarizable
2286 BIT_FIELD_REF, turn it into bit operations. */
2287
2288 static gimple_seq
2289 sra_build_bf_assignment (tree dst, tree src)
2290 {
2291 tree var, type, utype, tmp, tmp2, tmp3;
2292 gimple_seq seq;
2293 gimple stmt;
2294 tree cst, cst2, mask;
2295 tree minshift, maxshift;
2296
2297 if (TREE_CODE (dst) != BIT_FIELD_REF)
2298 return sra_build_assignment (dst, src);
2299
2300 var = TREE_OPERAND (dst, 0);
2301
2302 if (!scalar_bitfield_p (dst))
2303 return sra_build_assignment (REPLDUP (dst), src);
2304
2305 seq = NULL;
2306
2307 cst = fold_convert (bitsizetype, TREE_OPERAND (dst, 2));
2308 cst2 = size_binop (PLUS_EXPR,
2309 fold_convert (bitsizetype, TREE_OPERAND (dst, 1)),
2310 cst);
2311
2312 if (BYTES_BIG_ENDIAN)
2313 {
2314 maxshift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), cst);
2315 minshift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), cst2);
2316 }
2317 else
2318 {
2319 maxshift = cst2;
2320 minshift = cst;
2321 }
2322
2323 type = TREE_TYPE (var);
2324 if (!INTEGRAL_TYPE_P (type))
2325 type = lang_hooks.types.type_for_size
2326 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (var))), 1);
2327 if (TYPE_UNSIGNED (type))
2328 utype = type;
2329 else
2330 utype = unsigned_type_for (type);
2331
2332 mask = build_int_cst_wide (utype, 1, 0);
2333 if (TREE_INT_CST_LOW (maxshift) == TYPE_PRECISION (utype))
2334 cst = build_int_cst_wide (utype, 0, 0);
2335 else
2336 cst = int_const_binop (LSHIFT_EXPR, mask, maxshift, true);
2337 if (integer_zerop (minshift))
2338 cst2 = mask;
2339 else
2340 cst2 = int_const_binop (LSHIFT_EXPR, mask, minshift, true);
2341 mask = int_const_binop (MINUS_EXPR, cst, cst2, true);
2342 mask = fold_build1 (BIT_NOT_EXPR, utype, mask);
2343
2344 if (TYPE_MAIN_VARIANT (utype) != TYPE_MAIN_VARIANT (TREE_TYPE (var))
2345 && !integer_zerop (mask))
2346 {
2347 tmp = var;
2348 if (!is_gimple_variable (tmp))
2349 tmp = unshare_expr (var);
2350 else
2351 TREE_NO_WARNING (var) = true;
2352
2353 tmp2 = make_rename_temp (utype, "SR");
2354
2355 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
2356 tmp = fold_convert (utype, tmp);
2357 else
2358 tmp = fold_build1 (VIEW_CONVERT_EXPR, utype, tmp);
2359
2360 stmt = gimple_build_assign (tmp2, tmp);
2361 gimple_seq_add_stmt (&seq, stmt);
2362 }
2363 else
2364 tmp2 = var;
2365
2366 if (!integer_zerop (mask))
2367 {
2368 tmp = make_rename_temp (utype, "SR");
2369 stmt = gimple_build_assign (tmp, fold_build2 (BIT_AND_EXPR, utype,
2370 tmp2, mask));
2371 gimple_seq_add_stmt (&seq, stmt);
2372 }
2373 else
2374 tmp = mask;
2375
2376 if (is_gimple_reg (src) && INTEGRAL_TYPE_P (TREE_TYPE (src)))
2377 tmp2 = src;
2378 else if (INTEGRAL_TYPE_P (TREE_TYPE (src)))
2379 {
2380 gimple_seq tmp_seq;
2381 tmp2 = make_rename_temp (TREE_TYPE (src), "SR");
2382 tmp_seq = sra_build_assignment (tmp2, src);
2383 gimple_seq_add_seq (&seq, tmp_seq);
2384 }
2385 else
2386 {
2387 gimple_seq tmp_seq;
2388 tmp2 = make_rename_temp
2389 (lang_hooks.types.type_for_size
2390 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (src))),
2391 1), "SR");
2392 tmp_seq = sra_build_assignment (tmp2, fold_build1 (VIEW_CONVERT_EXPR,
2393 TREE_TYPE (tmp2), src));
2394 gimple_seq_add_seq (&seq, tmp_seq);
2395 }
2396
2397 if (!TYPE_UNSIGNED (TREE_TYPE (tmp2)))
2398 {
2399 gimple_seq tmp_seq;
2400 tree ut = unsigned_type_for (TREE_TYPE (tmp2));
2401 tmp3 = make_rename_temp (ut, "SR");
2402 tmp2 = fold_convert (ut, tmp2);
2403 tmp_seq = sra_build_assignment (tmp3, tmp2);
2404 gimple_seq_add_seq (&seq, tmp_seq);
2405
2406 tmp2 = fold_build1 (BIT_NOT_EXPR, utype, mask);
2407 tmp2 = int_const_binop (RSHIFT_EXPR, tmp2, minshift, true);
2408 tmp2 = fold_convert (ut, tmp2);
2409 tmp2 = fold_build2 (BIT_AND_EXPR, ut, tmp3, tmp2);
2410
2411 if (tmp3 != tmp2)
2412 {
2413 tmp3 = make_rename_temp (ut, "SR");
2414 tmp_seq = sra_build_assignment (tmp3, tmp2);
2415 gimple_seq_add_seq (&seq, tmp_seq);
2416 }
2417
2418 tmp2 = tmp3;
2419 }
2420
2421 if (TYPE_MAIN_VARIANT (TREE_TYPE (tmp2)) != TYPE_MAIN_VARIANT (utype))
2422 {
2423 gimple_seq tmp_seq;
2424 tmp3 = make_rename_temp (utype, "SR");
2425 tmp2 = fold_convert (utype, tmp2);
2426 tmp_seq = sra_build_assignment (tmp3, tmp2);
2427 gimple_seq_add_seq (&seq, tmp_seq);
2428 tmp2 = tmp3;
2429 }
2430
2431 if (!integer_zerop (minshift))
2432 {
2433 tmp3 = make_rename_temp (utype, "SR");
2434 stmt = gimple_build_assign (tmp3, fold_build2 (LSHIFT_EXPR, utype,
2435 tmp2, minshift));
2436 gimple_seq_add_stmt (&seq, stmt);
2437 tmp2 = tmp3;
2438 }
2439
2440 if (utype != TREE_TYPE (var))
2441 tmp3 = make_rename_temp (utype, "SR");
2442 else
2443 tmp3 = var;
2444 stmt = gimple_build_assign (tmp3, fold_build2 (BIT_IOR_EXPR, utype,
2445 tmp, tmp2));
2446 gimple_seq_add_stmt (&seq, stmt);
2447
2448 if (tmp3 != var)
2449 {
2450 if (TREE_TYPE (var) == type)
2451 stmt = gimple_build_assign (var, fold_convert (type, tmp3));
2452 else
2453 stmt = gimple_build_assign (var, fold_build1 (VIEW_CONVERT_EXPR,
2454 TREE_TYPE (var), tmp3));
2455 gimple_seq_add_stmt (&seq, stmt);
2456 }
2457
2458 return seq;
2459 }
2460
2461 /* Expand an assignment of SRC to the scalarized representation of
2462 ELT. If it is a field group, try to widen the assignment to cover
2463 the full variable. */
2464
2465 static gimple_seq
2466 sra_build_elt_assignment (struct sra_elt *elt, tree src)
2467 {
2468 tree dst = elt->replacement;
2469 tree var, tmp, cst, cst2;
2470 gimple stmt;
2471 gimple_seq seq;
2472
2473 if (TREE_CODE (dst) != BIT_FIELD_REF
2474 || !elt->in_bitfld_block)
2475 return sra_build_assignment (REPLDUP (dst), src);
2476
2477 var = TREE_OPERAND (dst, 0);
2478
2479 /* Try to widen the assignment to the entire variable.
2480 We need the source to be a BIT_FIELD_REF as well, such that, for
2481 BIT_FIELD_REF<d,sz,dp> = BIT_FIELD_REF<s,sz,sp>,
2482 by design, conditions are met such that we can turn it into
2483 d = BIT_FIELD_REF<s,dw,sp-dp>. */
2484 if (elt->in_bitfld_block == 2
2485 && TREE_CODE (src) == BIT_FIELD_REF)
2486 {
2487 tmp = src;
2488 cst = TYPE_SIZE (TREE_TYPE (var));
2489 cst2 = size_binop (MINUS_EXPR, TREE_OPERAND (src, 2),
2490 TREE_OPERAND (dst, 2));
2491
2492 src = TREE_OPERAND (src, 0);
2493
2494 /* Avoid full-width bit-fields. */
2495 if (integer_zerop (cst2)
2496 && tree_int_cst_equal (cst, TYPE_SIZE (TREE_TYPE (src))))
2497 {
2498 if (INTEGRAL_TYPE_P (TREE_TYPE (src))
2499 && !TYPE_UNSIGNED (TREE_TYPE (src)))
2500 src = fold_convert (unsigned_type_for (TREE_TYPE (src)), src);
2501
2502 /* If a single conversion won't do, we'll need a statement
2503 list. */
2504 if (TYPE_MAIN_VARIANT (TREE_TYPE (var))
2505 != TYPE_MAIN_VARIANT (TREE_TYPE (src)))
2506 {
2507 gimple_seq tmp_seq;
2508 seq = NULL;
2509
2510 if (!INTEGRAL_TYPE_P (TREE_TYPE (src)))
2511 src = fold_build1 (VIEW_CONVERT_EXPR,
2512 lang_hooks.types.type_for_size
2513 (TREE_INT_CST_LOW
2514 (TYPE_SIZE (TREE_TYPE (src))),
2515 1), src);
2516 gcc_assert (TYPE_UNSIGNED (TREE_TYPE (src)));
2517
2518 tmp = make_rename_temp (TREE_TYPE (src), "SR");
2519 stmt = gimple_build_assign (tmp, src);
2520 gimple_seq_add_stmt (&seq, stmt);
2521
2522 tmp_seq = sra_build_assignment (var,
2523 fold_convert (TREE_TYPE (var),
2524 tmp));
2525 gimple_seq_add_seq (&seq, tmp_seq);
2526
2527 return seq;
2528 }
2529
2530 src = fold_convert (TREE_TYPE (var), src);
2531 }
2532 else
2533 {
2534 src = fold_convert (TREE_TYPE (var), tmp);
2535 }
2536
2537 return sra_build_assignment (var, src);
2538 }
2539
2540 return sra_build_bf_assignment (dst, src);
2541 }
2542
2543 /* Generate a set of assignment statements in *LIST_P to copy all
2544 instantiated elements under ELT to or from the equivalent structure
2545 rooted at EXPR. COPY_OUT controls the direction of the copy, with
2546 true meaning to copy out of EXPR into ELT. */
2547
2548 static void
2549 generate_copy_inout (struct sra_elt *elt, bool copy_out, tree expr,
2550 gimple_seq *seq_p)
2551 {
2552 struct sra_elt *c;
2553 gimple_seq tmp_seq;
2554 tree t;
2555
2556 if (!copy_out && TREE_CODE (expr) == SSA_NAME
2557 && TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2558 {
2559 tree r, i;
2560
2561 c = lookup_element (elt, integer_zero_node, NULL, NO_INSERT);
2562 r = c->replacement;
2563 c = lookup_element (elt, integer_one_node, NULL, NO_INSERT);
2564 i = c->replacement;
2565
2566 t = build2 (COMPLEX_EXPR, elt->type, r, i);
2567 tmp_seq = sra_build_bf_assignment (expr, t);
2568 SSA_NAME_DEF_STMT (expr) = gimple_seq_last_stmt (tmp_seq);
2569 gimple_seq_add_seq (seq_p, tmp_seq);
2570 }
2571 else if (elt->replacement)
2572 {
2573 if (copy_out)
2574 tmp_seq = sra_build_elt_assignment (elt, expr);
2575 else
2576 tmp_seq = sra_build_bf_assignment (expr, REPLDUP (elt->replacement));
2577 gimple_seq_add_seq (seq_p, tmp_seq);
2578 }
2579 else
2580 {
2581 FOR_EACH_ACTUAL_CHILD (c, elt)
2582 {
2583 t = generate_one_element_ref (c, unshare_expr (expr));
2584 generate_copy_inout (c, copy_out, t, seq_p);
2585 }
2586 }
2587 }
2588
2589 /* Generate a set of assignment statements in *LIST_P to copy all instantiated
2590 elements under SRC to their counterparts under DST. There must be a 1-1
2591 correspondence of instantiated elements. */
2592
2593 static void
2594 generate_element_copy (struct sra_elt *dst, struct sra_elt *src, gimple_seq *seq_p)
2595 {
2596 struct sra_elt *dc, *sc;
2597
2598 FOR_EACH_ACTUAL_CHILD (dc, dst)
2599 {
2600 sc = lookup_element (src, dc->element, NULL, NO_INSERT);
2601 if (!sc && dc->in_bitfld_block == 2)
2602 {
2603 struct sra_elt *dcs;
2604
2605 FOR_EACH_ACTUAL_CHILD (dcs, dc)
2606 {
2607 sc = lookup_element (src, dcs->element, NULL, NO_INSERT);
2608 gcc_assert (sc);
2609 generate_element_copy (dcs, sc, seq_p);
2610 }
2611
2612 continue;
2613 }
2614
2615 /* If DST and SRC are structs with the same elements, but do not have
2616 the same TYPE_MAIN_VARIANT, then lookup of DST FIELD_DECL in SRC
2617 will fail. Try harder by finding the corresponding FIELD_DECL
2618 in SRC. */
2619 if (!sc)
2620 {
2621 tree f;
2622
2623 gcc_assert (useless_type_conversion_p (dst->type, src->type));
2624 gcc_assert (TREE_CODE (dc->element) == FIELD_DECL);
2625 for (f = TYPE_FIELDS (src->type); f ; f = TREE_CHAIN (f))
2626 if (simple_cst_equal (DECL_FIELD_OFFSET (f),
2627 DECL_FIELD_OFFSET (dc->element)) > 0
2628 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (f),
2629 DECL_FIELD_BIT_OFFSET (dc->element)) > 0
2630 && simple_cst_equal (DECL_SIZE (f),
2631 DECL_SIZE (dc->element)) > 0
2632 && (useless_type_conversion_p (TREE_TYPE (dc->element),
2633 TREE_TYPE (f))
2634 || (POINTER_TYPE_P (TREE_TYPE (dc->element))
2635 && POINTER_TYPE_P (TREE_TYPE (f)))))
2636 break;
2637 gcc_assert (f != NULL_TREE);
2638 sc = lookup_element (src, f, NULL, NO_INSERT);
2639 }
2640
2641 generate_element_copy (dc, sc, seq_p);
2642 }
2643
2644 if (dst->replacement)
2645 {
2646 gimple_seq tmp_seq;
2647
2648 gcc_assert (src->replacement);
2649
2650 tmp_seq = sra_build_elt_assignment (dst, REPLDUP (src->replacement));
2651 gimple_seq_add_seq (seq_p, tmp_seq);
2652 }
2653 }
2654
2655 /* Generate a set of assignment statements in *LIST_P to zero all instantiated
2656 elements under ELT. In addition, do not assign to elements that have been
2657 marked VISITED but do reset the visited flag; this allows easy coordination
2658 with generate_element_init. */
2659
2660 static void
2661 generate_element_zero (struct sra_elt *elt, gimple_seq *seq_p)
2662 {
2663 struct sra_elt *c;
2664
2665 if (elt->visited)
2666 {
2667 elt->visited = false;
2668 return;
2669 }
2670
2671 if (!elt->in_bitfld_block)
2672 FOR_EACH_ACTUAL_CHILD (c, elt)
2673 generate_element_zero (c, seq_p);
2674
2675 if (elt->replacement)
2676 {
2677 tree t;
2678 gimple_seq tmp_seq;
2679
2680 gcc_assert (elt->is_scalar);
2681 t = fold_convert (elt->type, integer_zero_node);
2682
2683 tmp_seq = sra_build_elt_assignment (elt, t);
2684 gimple_seq_add_seq (seq_p, tmp_seq);
2685 }
2686 }
2687
2688 /* Generate an assignment VAR = INIT, where INIT may need gimplification.
2689 Add the result to *LIST_P. */
2690
2691 static void
2692 generate_one_element_init (struct sra_elt *elt, tree init, gimple_seq *seq_p)
2693 {
2694 gimple_seq tmp_seq = sra_build_elt_assignment (elt, init);
2695 gimple_seq_add_seq (seq_p, tmp_seq);
2696 }
2697
2698 /* Generate a set of assignment statements in *LIST_P to set all instantiated
2699 elements under ELT with the contents of the initializer INIT. In addition,
2700 mark all assigned elements VISITED; this allows easy coordination with
2701 generate_element_zero. Return false if we found a case we couldn't
2702 handle. */
2703
2704 static bool
2705 generate_element_init_1 (struct sra_elt *elt, tree init, gimple_seq *seq_p)
2706 {
2707 bool result = true;
2708 enum tree_code init_code;
2709 struct sra_elt *sub;
2710 tree t;
2711 unsigned HOST_WIDE_INT idx;
2712 tree value, purpose;
2713
2714 /* We can be passed DECL_INITIAL of a static variable. It might have a
2715 conversion, which we strip off here. */
2716 STRIP_USELESS_TYPE_CONVERSION (init);
2717 init_code = TREE_CODE (init);
2718
2719 if (elt->is_scalar)
2720 {
2721 if (elt->replacement)
2722 {
2723 generate_one_element_init (elt, init, seq_p);
2724 elt->visited = true;
2725 }
2726 return result;
2727 }
2728
2729 switch (init_code)
2730 {
2731 case COMPLEX_CST:
2732 case COMPLEX_EXPR:
2733 FOR_EACH_ACTUAL_CHILD (sub, elt)
2734 {
2735 if (sub->element == integer_zero_node)
2736 t = (init_code == COMPLEX_EXPR
2737 ? TREE_OPERAND (init, 0) : TREE_REALPART (init));
2738 else
2739 t = (init_code == COMPLEX_EXPR
2740 ? TREE_OPERAND (init, 1) : TREE_IMAGPART (init));
2741 result &= generate_element_init_1 (sub, t, seq_p);
2742 }
2743 break;
2744
2745 case CONSTRUCTOR:
2746 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, purpose, value)
2747 {
2748 /* Array constructors are routinely created with NULL indices. */
2749 if (purpose == NULL_TREE)
2750 {
2751 result = false;
2752 break;
2753 }
2754 if (TREE_CODE (purpose) == RANGE_EXPR)
2755 {
2756 tree lower = TREE_OPERAND (purpose, 0);
2757 tree upper = TREE_OPERAND (purpose, 1);
2758
2759 while (1)
2760 {
2761 sub = lookup_element (elt, lower, NULL, NO_INSERT);
2762 if (sub != NULL)
2763 result &= generate_element_init_1 (sub, value, seq_p);
2764 if (tree_int_cst_equal (lower, upper))
2765 break;
2766 lower = int_const_binop (PLUS_EXPR, lower,
2767 integer_one_node, true);
2768 }
2769 }
2770 else
2771 {
2772 sub = lookup_element (elt, purpose, NULL, NO_INSERT);
2773 if (sub != NULL)
2774 result &= generate_element_init_1 (sub, value, seq_p);
2775 }
2776 }
2777 break;
2778
2779 default:
2780 elt->visited = true;
2781 result = false;
2782 }
2783
2784 return result;
2785 }
2786
2787 /* A wrapper function for generate_element_init_1 that handles cleanup after
2788 gimplification. */
2789
2790 static bool
2791 generate_element_init (struct sra_elt *elt, tree init, gimple_seq *seq_p)
2792 {
2793 bool ret;
2794 struct gimplify_ctx gctx;
2795
2796 push_gimplify_context (&gctx);
2797 ret = generate_element_init_1 (elt, init, seq_p);
2798 pop_gimplify_context (NULL);
2799
2800 /* The replacement can expose previously unreferenced variables. */
2801 if (ret && *seq_p)
2802 {
2803 gimple_stmt_iterator i;
2804
2805 for (i = gsi_start (*seq_p); !gsi_end_p (i); gsi_next (&i))
2806 find_new_referenced_vars (gsi_stmt (i));
2807 }
2808
2809 return ret;
2810 }
2811
2812 /* Helper function to insert LIST before GSI, and set up line number info. */
2813
2814 static void
2815 sra_insert_before (gimple_stmt_iterator *gsi, gimple_seq seq)
2816 {
2817 gimple stmt = gsi_stmt (*gsi);
2818
2819 if (gimple_has_location (stmt))
2820 annotate_all_with_location (seq, gimple_location (stmt));
2821 gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
2822 }
2823
2824 /* Similarly, but insert after GSI. Handles insertion onto edges as well. */
2825
2826 static void
2827 sra_insert_after (gimple_stmt_iterator *gsi, gimple_seq seq)
2828 {
2829 gimple stmt = gsi_stmt (*gsi);
2830
2831 if (gimple_has_location (stmt))
2832 annotate_all_with_location (seq, gimple_location (stmt));
2833
2834 if (stmt_ends_bb_p (stmt))
2835 insert_edge_copies_seq (seq, gsi_bb (*gsi));
2836 else
2837 gsi_insert_seq_after (gsi, seq, GSI_SAME_STMT);
2838 }
2839
2840 /* Similarly, but replace the statement at GSI. */
2841
2842 static void
2843 sra_replace (gimple_stmt_iterator *gsi, gimple_seq seq)
2844 {
2845 sra_insert_before (gsi, seq);
2846 unlink_stmt_vdef (gsi_stmt (*gsi));
2847 gsi_remove (gsi, false);
2848 if (gsi_end_p (*gsi))
2849 *gsi = gsi_last (gsi_seq (*gsi));
2850 else
2851 gsi_prev (gsi);
2852 }
2853
2854 /* Data structure that bitfield_overlaps_p fills in with information
2855 about the element passed in and how much of it overlaps with the
2856 bit-range passed it to. */
2857
2858 struct bitfield_overlap_info
2859 {
2860 /* The bit-length of an element. */
2861 tree field_len;
2862
2863 /* The bit-position of the element in its parent. */
2864 tree field_pos;
2865
2866 /* The number of bits of the element that overlap with the incoming
2867 bit range. */
2868 tree overlap_len;
2869
2870 /* The first bit of the element that overlaps with the incoming bit
2871 range. */
2872 tree overlap_pos;
2873 };
2874
2875 /* Return true if a BIT_FIELD_REF<(FLD->parent), BLEN, BPOS>
2876 expression (referenced as BF below) accesses any of the bits in FLD,
2877 false if it doesn't. If DATA is non-null, its field_len and
2878 field_pos are filled in such that BIT_FIELD_REF<(FLD->parent),
2879 field_len, field_pos> (referenced as BFLD below) represents the
2880 entire field FLD->element, and BIT_FIELD_REF<BFLD, overlap_len,
2881 overlap_pos> represents the portion of the entire field that
2882 overlaps with BF. */
2883
2884 static bool
2885 bitfield_overlaps_p (tree blen, tree bpos, struct sra_elt *fld,
2886 struct bitfield_overlap_info *data)
2887 {
2888 tree flen, fpos;
2889 bool ret;
2890
2891 if (TREE_CODE (fld->element) == FIELD_DECL)
2892 {
2893 flen = fold_convert (bitsizetype, DECL_SIZE (fld->element));
2894 fpos = fold_convert (bitsizetype, DECL_FIELD_OFFSET (fld->element));
2895 fpos = size_binop (MULT_EXPR, fpos, bitsize_int (BITS_PER_UNIT));
2896 fpos = size_binop (PLUS_EXPR, fpos, DECL_FIELD_BIT_OFFSET (fld->element));
2897 }
2898 else if (TREE_CODE (fld->element) == BIT_FIELD_REF)
2899 {
2900 flen = fold_convert (bitsizetype, TREE_OPERAND (fld->element, 1));
2901 fpos = fold_convert (bitsizetype, TREE_OPERAND (fld->element, 2));
2902 }
2903 else if (TREE_CODE (fld->element) == INTEGER_CST)
2904 {
2905 tree domain_type = TYPE_DOMAIN (TREE_TYPE (fld->parent->element));
2906 flen = fold_convert (bitsizetype, TYPE_SIZE (fld->type));
2907 fpos = fold_convert (bitsizetype, fld->element);
2908 if (domain_type && TYPE_MIN_VALUE (domain_type))
2909 fpos = size_binop (MINUS_EXPR, fpos,
2910 fold_convert (bitsizetype,
2911 TYPE_MIN_VALUE (domain_type)));
2912 fpos = size_binop (MULT_EXPR, flen, fpos);
2913 }
2914 else
2915 gcc_unreachable ();
2916
2917 gcc_assert (host_integerp (blen, 1)
2918 && host_integerp (bpos, 1)
2919 && host_integerp (flen, 1)
2920 && host_integerp (fpos, 1));
2921
2922 ret = ((!tree_int_cst_lt (fpos, bpos)
2923 && tree_int_cst_lt (size_binop (MINUS_EXPR, fpos, bpos),
2924 blen))
2925 || (!tree_int_cst_lt (bpos, fpos)
2926 && tree_int_cst_lt (size_binop (MINUS_EXPR, bpos, fpos),
2927 flen)));
2928
2929 if (!ret)
2930 return ret;
2931
2932 if (data)
2933 {
2934 tree bend, fend;
2935
2936 data->field_len = flen;
2937 data->field_pos = fpos;
2938
2939 fend = size_binop (PLUS_EXPR, fpos, flen);
2940 bend = size_binop (PLUS_EXPR, bpos, blen);
2941
2942 if (tree_int_cst_lt (bend, fend))
2943 data->overlap_len = size_binop (MINUS_EXPR, bend, fpos);
2944 else
2945 data->overlap_len = NULL;
2946
2947 if (tree_int_cst_lt (fpos, bpos))
2948 {
2949 data->overlap_pos = size_binop (MINUS_EXPR, bpos, fpos);
2950 data->overlap_len = size_binop (MINUS_EXPR,
2951 data->overlap_len
2952 ? data->overlap_len
2953 : data->field_len,
2954 data->overlap_pos);
2955 }
2956 else
2957 data->overlap_pos = NULL;
2958 }
2959
2960 return ret;
2961 }
2962
2963 /* Add to LISTP a sequence of statements that copies BLEN bits between
2964 VAR and the scalarized elements of ELT, starting a bit VPOS of VAR
2965 and at bit BPOS of ELT. The direction of the copy is given by
2966 TO_VAR. */
2967
2968 static void
2969 sra_explode_bitfield_assignment (tree var, tree vpos, bool to_var,
2970 gimple_seq *seq_p, tree blen, tree bpos,
2971 struct sra_elt *elt)
2972 {
2973 struct sra_elt *fld;
2974 struct bitfield_overlap_info flp;
2975
2976 FOR_EACH_ACTUAL_CHILD (fld, elt)
2977 {
2978 tree flen, fpos;
2979
2980 if (!bitfield_overlaps_p (blen, bpos, fld, &flp))
2981 continue;
2982
2983 flen = flp.overlap_len ? flp.overlap_len : flp.field_len;
2984 fpos = flp.overlap_pos ? flp.overlap_pos : bitsize_int (0);
2985
2986 if (fld->replacement)
2987 {
2988 tree infld, invar, type;
2989 gimple_seq st;
2990
2991 infld = fld->replacement;
2992
2993 type = unsigned_type_for (TREE_TYPE (infld));
2994 if (TYPE_PRECISION (type) != TREE_INT_CST_LOW (flen))
2995 type = build_nonstandard_integer_type (TREE_INT_CST_LOW (flen), 1);
2996
2997 if (TREE_CODE (infld) == BIT_FIELD_REF)
2998 {
2999 fpos = size_binop (PLUS_EXPR, fpos, TREE_OPERAND (infld, 2));
3000 infld = TREE_OPERAND (infld, 0);
3001 }
3002 else if (BYTES_BIG_ENDIAN && DECL_P (fld->element)
3003 && !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (infld)),
3004 DECL_SIZE (fld->element)))
3005 {
3006 fpos = size_binop (PLUS_EXPR, fpos,
3007 TYPE_SIZE (TREE_TYPE (infld)));
3008 fpos = size_binop (MINUS_EXPR, fpos,
3009 DECL_SIZE (fld->element));
3010 }
3011
3012 infld = fold_build3 (BIT_FIELD_REF, type, infld, flen, fpos);
3013
3014 invar = size_binop (MINUS_EXPR, flp.field_pos, bpos);
3015 if (flp.overlap_pos)
3016 invar = size_binop (PLUS_EXPR, invar, flp.overlap_pos);
3017 invar = size_binop (PLUS_EXPR, invar, vpos);
3018
3019 invar = fold_build3 (BIT_FIELD_REF, type, var, flen, invar);
3020
3021 if (to_var)
3022 st = sra_build_bf_assignment (invar, infld);
3023 else
3024 st = sra_build_bf_assignment (infld, invar);
3025
3026 gimple_seq_add_seq (seq_p, st);
3027 }
3028 else
3029 {
3030 tree sub = size_binop (MINUS_EXPR, flp.field_pos, bpos);
3031 sub = size_binop (PLUS_EXPR, vpos, sub);
3032 if (flp.overlap_pos)
3033 sub = size_binop (PLUS_EXPR, sub, flp.overlap_pos);
3034
3035 sra_explode_bitfield_assignment (var, sub, to_var, seq_p,
3036 flen, fpos, fld);
3037 }
3038 }
3039 }
3040
3041 /* Add to LISTBEFOREP statements that copy scalarized members of ELT
3042 that overlap with BIT_FIELD_REF<(ELT->element), BLEN, BPOS> back
3043 into the full variable, and to LISTAFTERP, if non-NULL, statements
3044 that copy the (presumably modified) overlapping portions of the
3045 full variable back to the scalarized variables. */
3046
3047 static void
3048 sra_sync_for_bitfield_assignment (gimple_seq *seq_before_p,
3049 gimple_seq *seq_after_p,
3050 tree blen, tree bpos,
3051 struct sra_elt *elt)
3052 {
3053 struct sra_elt *fld;
3054 struct bitfield_overlap_info flp;
3055
3056 FOR_EACH_ACTUAL_CHILD (fld, elt)
3057 if (bitfield_overlaps_p (blen, bpos, fld, &flp))
3058 {
3059 if (fld->replacement || (!flp.overlap_len && !flp.overlap_pos))
3060 {
3061 generate_copy_inout (fld, false, generate_element_ref (fld),
3062 seq_before_p);
3063 mark_no_warning (fld);
3064 if (seq_after_p)
3065 generate_copy_inout (fld, true, generate_element_ref (fld),
3066 seq_after_p);
3067 }
3068 else
3069 {
3070 tree flen = flp.overlap_len ? flp.overlap_len : flp.field_len;
3071 tree fpos = flp.overlap_pos ? flp.overlap_pos : bitsize_int (0);
3072
3073 sra_sync_for_bitfield_assignment (seq_before_p, seq_after_p,
3074 flen, fpos, fld);
3075 }
3076 }
3077 }
3078
3079 /* Scalarize a USE. To recap, this is either a simple reference to ELT,
3080 if elt is scalar, or some occurrence of ELT that requires a complete
3081 aggregate. IS_OUTPUT is true if ELT is being modified. */
3082
3083 static void
3084 scalarize_use (struct sra_elt *elt, tree *expr_p, gimple_stmt_iterator *gsi,
3085 bool is_output, bool use_all)
3086 {
3087 gimple stmt = gsi_stmt (*gsi);
3088 tree bfexpr;
3089
3090 if (elt->replacement)
3091 {
3092 tree replacement = elt->replacement;
3093
3094 /* If we have a replacement, then updating the reference is as
3095 simple as modifying the existing statement in place. */
3096 if (is_output
3097 && TREE_CODE (elt->replacement) == BIT_FIELD_REF
3098 && is_gimple_reg (TREE_OPERAND (elt->replacement, 0))
3099 && is_gimple_assign (stmt)
3100 && gimple_assign_lhs_ptr (stmt) == expr_p)
3101 {
3102 gimple_seq newseq;
3103 /* RHS must be a single operand. */
3104 gcc_assert (gimple_assign_single_p (stmt));
3105 newseq = sra_build_elt_assignment (elt, gimple_assign_rhs1 (stmt));
3106 sra_replace (gsi, newseq);
3107 return;
3108 }
3109 else if (!is_output
3110 && TREE_CODE (elt->replacement) == BIT_FIELD_REF
3111 && is_gimple_assign (stmt)
3112 && gimple_assign_rhs1_ptr (stmt) == expr_p)
3113 {
3114 tree tmp = make_rename_temp
3115 (TREE_TYPE (gimple_assign_lhs (stmt)), "SR");
3116 gimple_seq newseq = sra_build_assignment (tmp, REPLDUP (elt->replacement));
3117
3118 sra_insert_before (gsi, newseq);
3119 replacement = tmp;
3120 }
3121 if (is_output)
3122 update_stmt_if_modified (stmt);
3123 *expr_p = REPLDUP (replacement);
3124 update_stmt (stmt);
3125 }
3126 else if (use_all && is_output
3127 && is_gimple_assign (stmt)
3128 && TREE_CODE (bfexpr
3129 = gimple_assign_lhs (stmt)) == BIT_FIELD_REF
3130 && &TREE_OPERAND (bfexpr, 0) == expr_p
3131 && INTEGRAL_TYPE_P (TREE_TYPE (bfexpr))
3132 && TREE_CODE (TREE_TYPE (*expr_p)) == RECORD_TYPE)
3133 {
3134 gimple_seq seq_before = NULL;
3135 gimple_seq seq_after = NULL;
3136 tree blen = fold_convert (bitsizetype, TREE_OPERAND (bfexpr, 1));
3137 tree bpos = fold_convert (bitsizetype, TREE_OPERAND (bfexpr, 2));
3138 bool update = false;
3139
3140 if (!elt->use_block_copy)
3141 {
3142 tree type = TREE_TYPE (bfexpr);
3143 tree var = make_rename_temp (type, "SR"), tmp, vpos;
3144 gimple st;
3145
3146 gimple_assign_set_lhs (stmt, var);
3147 update = true;
3148
3149 if (!TYPE_UNSIGNED (type))
3150 {
3151 type = unsigned_type_for (type);
3152 tmp = make_rename_temp (type, "SR");
3153 st = gimple_build_assign (tmp, fold_convert (type, var));
3154 gimple_seq_add_stmt (&seq_after, st);
3155 var = tmp;
3156 }
3157
3158 /* If VAR is wider than BLEN bits, it is padded at the
3159 most-significant end. We want to set VPOS such that
3160 <BIT_FIELD_REF VAR BLEN VPOS> would refer to the
3161 least-significant BLEN bits of VAR. */
3162 if (BYTES_BIG_ENDIAN)
3163 vpos = size_binop (MINUS_EXPR, TYPE_SIZE (type), blen);
3164 else
3165 vpos = bitsize_int (0);
3166 sra_explode_bitfield_assignment
3167 (var, vpos, false, &seq_after, blen, bpos, elt);
3168 }
3169 else
3170 sra_sync_for_bitfield_assignment
3171 (&seq_before, &seq_after, blen, bpos, elt);
3172
3173 if (seq_before)
3174 {
3175 mark_all_v_defs_seq (seq_before);
3176 sra_insert_before (gsi, seq_before);
3177 }
3178 if (seq_after)
3179 {
3180 mark_all_v_defs_seq (seq_after);
3181 sra_insert_after (gsi, seq_after);
3182 }
3183
3184 if (update)
3185 update_stmt (stmt);
3186 }
3187 else if (use_all && !is_output
3188 && is_gimple_assign (stmt)
3189 && TREE_CODE (bfexpr
3190 = gimple_assign_rhs1 (stmt)) == BIT_FIELD_REF
3191 && &TREE_OPERAND (gimple_assign_rhs1 (stmt), 0) == expr_p
3192 && INTEGRAL_TYPE_P (TREE_TYPE (bfexpr))
3193 && TREE_CODE (TREE_TYPE (*expr_p)) == RECORD_TYPE)
3194 {
3195 gimple_seq seq = NULL;
3196 tree blen = fold_convert (bitsizetype, TREE_OPERAND (bfexpr, 1));
3197 tree bpos = fold_convert (bitsizetype, TREE_OPERAND (bfexpr, 2));
3198 bool update = false;
3199
3200 if (!elt->use_block_copy)
3201 {
3202 tree type = TREE_TYPE (bfexpr);
3203 tree var = make_rename_temp (type, "SR"), tmp, vpos;
3204 gimple st = NULL;
3205
3206 gimple_assign_set_rhs1 (stmt, var);
3207 update = true;
3208
3209 if (!TYPE_UNSIGNED (type))
3210 {
3211 type = unsigned_type_for (type);
3212 tmp = make_rename_temp (type, "SR");
3213 st = gimple_build_assign (var,
3214 fold_convert (TREE_TYPE (var), tmp));
3215 var = tmp;
3216 }
3217
3218 gimple_seq_add_stmt (&seq,
3219 gimple_build_assign
3220 (var, build_int_cst_wide (type, 0, 0)));
3221
3222 /* If VAR is wider than BLEN bits, it is padded at the
3223 most-significant end. We want to set VPOS such that
3224 <BIT_FIELD_REF VAR BLEN VPOS> would refer to the
3225 least-significant BLEN bits of VAR. */
3226 if (BYTES_BIG_ENDIAN)
3227 vpos = size_binop (MINUS_EXPR, TYPE_SIZE (type), blen);
3228 else
3229 vpos = bitsize_int (0);
3230 sra_explode_bitfield_assignment
3231 (var, vpos, true, &seq, blen, bpos, elt);
3232
3233 if (st)
3234 gimple_seq_add_stmt (&seq, st);
3235 }
3236 else
3237 sra_sync_for_bitfield_assignment
3238 (&seq, NULL, blen, bpos, elt);
3239
3240 if (seq)
3241 {
3242 mark_all_v_defs_seq (seq);
3243 sra_insert_before (gsi, seq);
3244 }
3245
3246 if (update)
3247 update_stmt (stmt);
3248 }
3249 else
3250 {
3251 gimple_seq seq = NULL;
3252
3253 /* Otherwise we need some copies. If ELT is being read, then we
3254 want to store all (modified) sub-elements back into the
3255 structure before the reference takes place. If ELT is being
3256 written, then we want to load the changed values back into
3257 our shadow variables. */
3258 /* ??? We don't check modified for reads, we just always write all of
3259 the values. We should be able to record the SSA number of the VOP
3260 for which the values were last read. If that number matches the
3261 SSA number of the VOP in the current statement, then we needn't
3262 emit an assignment. This would also eliminate double writes when
3263 a structure is passed as more than one argument to a function call.
3264 This optimization would be most effective if sra_walk_function
3265 processed the blocks in dominator order. */
3266
3267 generate_copy_inout (elt, is_output, generate_element_ref (elt), &seq);
3268 if (seq == NULL)
3269 return;
3270 mark_all_v_defs_seq (seq);
3271 if (is_output)
3272 sra_insert_after (gsi, seq);
3273 else
3274 {
3275 sra_insert_before (gsi, seq);
3276 if (use_all)
3277 mark_no_warning (elt);
3278 }
3279 }
3280 }
3281
3282 /* Scalarize a COPY. To recap, this is an assignment statement between
3283 two scalarizable references, LHS_ELT and RHS_ELT. */
3284
3285 static void
3286 scalarize_copy (struct sra_elt *lhs_elt, struct sra_elt *rhs_elt,
3287 gimple_stmt_iterator *gsi)
3288 {
3289 gimple_seq seq;
3290 gimple stmt;
3291
3292 if (lhs_elt->replacement && rhs_elt->replacement)
3293 {
3294 /* If we have two scalar operands, modify the existing statement. */
3295 stmt = gsi_stmt (*gsi);
3296
3297 /* See the commentary in sra_walk_function concerning
3298 RETURN_EXPR, and why we should never see one here. */
3299 gcc_assert (is_gimple_assign (stmt));
3300 gcc_assert (gimple_assign_copy_p (stmt));
3301
3302
3303 gimple_assign_set_lhs (stmt, lhs_elt->replacement);
3304 gimple_assign_set_rhs1 (stmt, REPLDUP (rhs_elt->replacement));
3305 update_stmt (stmt);
3306 }
3307 else if (lhs_elt->use_block_copy || rhs_elt->use_block_copy)
3308 {
3309 /* If either side requires a block copy, then sync the RHS back
3310 to the original structure, leave the original assignment
3311 statement (which will perform the block copy), then load the
3312 LHS values out of its now-updated original structure. */
3313 /* ??? Could perform a modified pair-wise element copy. That
3314 would at least allow those elements that are instantiated in
3315 both structures to be optimized well. */
3316
3317 seq = NULL;
3318 generate_copy_inout (rhs_elt, false,
3319 generate_element_ref (rhs_elt), &seq);
3320 if (seq)
3321 {
3322 mark_all_v_defs_seq (seq);
3323 sra_insert_before (gsi, seq);
3324 }
3325
3326 seq = NULL;
3327 generate_copy_inout (lhs_elt, true,
3328 generate_element_ref (lhs_elt), &seq);
3329 if (seq)
3330 {
3331 mark_all_v_defs_seq (seq);
3332 sra_insert_after (gsi, seq);
3333 }
3334 }
3335 else
3336 {
3337 /* Otherwise both sides must be fully instantiated. In which
3338 case perform pair-wise element assignments and replace the
3339 original block copy statement. */
3340
3341 stmt = gsi_stmt (*gsi);
3342 update_stmt_if_modified (stmt);
3343
3344 seq = NULL;
3345 generate_element_copy (lhs_elt, rhs_elt, &seq);
3346 gcc_assert (seq);
3347 mark_all_v_defs_seq (seq);
3348 sra_replace (gsi, seq);
3349 }
3350 }
3351
3352 /* Scalarize an INIT. To recap, this is an assignment to a scalarizable
3353 reference from some form of constructor: CONSTRUCTOR, COMPLEX_CST or
3354 COMPLEX_EXPR. If RHS is NULL, it should be treated as an empty
3355 CONSTRUCTOR. */
3356
3357 static void
3358 scalarize_init (struct sra_elt *lhs_elt, tree rhs, gimple_stmt_iterator *gsi)
3359 {
3360 bool result = true;
3361 gimple_seq seq = NULL, init_seq = NULL;
3362
3363 /* Generate initialization statements for all members extant in the RHS. */
3364 if (rhs)
3365 {
3366 /* Unshare the expression just in case this is from a decl's initial. */
3367 rhs = unshare_expr (rhs);
3368 result = generate_element_init (lhs_elt, rhs, &init_seq);
3369 }
3370
3371 if (!result)
3372 {
3373 /* If we failed to convert the entire initializer, then we must
3374 leave the structure assignment in place and must load values
3375 from the structure into the slots for which we did not find
3376 constants. The easiest way to do this is to generate a complete
3377 copy-out, and then follow that with the constant assignments
3378 that we were able to build. DCE will clean things up. */
3379 gimple_seq seq0 = NULL;
3380 generate_copy_inout (lhs_elt, true, generate_element_ref (lhs_elt),
3381 &seq0);
3382 gimple_seq_add_seq (&seq0, seq);
3383 seq = seq0;
3384 }
3385 else
3386 {
3387 /* CONSTRUCTOR is defined such that any member not mentioned is assigned
3388 a zero value. Initialize the rest of the instantiated elements. */
3389 generate_element_zero (lhs_elt, &seq);
3390 gimple_seq_add_seq (&seq, init_seq);
3391 }
3392
3393 if (lhs_elt->use_block_copy || !result)
3394 {
3395 /* Since LHS is not fully instantiated, we must leave the structure
3396 assignment in place. Treating this case differently from a USE
3397 exposes constants to later optimizations. */
3398 if (seq)
3399 {
3400 mark_all_v_defs_seq (seq);
3401 sra_insert_after (gsi, seq);
3402 }
3403 }
3404 else
3405 {
3406 /* The LHS is fully instantiated. The list of initializations
3407 replaces the original structure assignment. */
3408 gcc_assert (seq);
3409 update_stmt_if_modified (gsi_stmt (*gsi));
3410 mark_all_v_defs_seq (seq);
3411 sra_replace (gsi, seq);
3412 }
3413 }
3414
3415 /* A subroutine of scalarize_ldst called via walk_tree. Set TREE_NO_TRAP
3416 on all INDIRECT_REFs. */
3417
3418 static tree
3419 mark_notrap (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3420 {
3421 tree t = *tp;
3422
3423 if (TREE_CODE (t) == INDIRECT_REF)
3424 {
3425 TREE_THIS_NOTRAP (t) = 1;
3426 *walk_subtrees = 0;
3427 }
3428 else if (IS_TYPE_OR_DECL_P (t))
3429 *walk_subtrees = 0;
3430
3431 return NULL;
3432 }
3433
3434 /* Scalarize a LDST. To recap, this is an assignment between one scalarizable
3435 reference ELT and one non-scalarizable reference OTHER. IS_OUTPUT is true
3436 if ELT is on the left-hand side. */
3437
3438 static void
3439 scalarize_ldst (struct sra_elt *elt, tree other,
3440 gimple_stmt_iterator *gsi, bool is_output)
3441 {
3442 /* Shouldn't have gotten called for a scalar. */
3443 gcc_assert (!elt->replacement);
3444
3445 if (elt->use_block_copy)
3446 {
3447 /* Since ELT is not fully instantiated, we have to leave the
3448 block copy in place. Treat this as a USE. */
3449 scalarize_use (elt, NULL, gsi, is_output, false);
3450 }
3451 else
3452 {
3453 /* The interesting case is when ELT is fully instantiated. In this
3454 case we can have each element stored/loaded directly to/from the
3455 corresponding slot in OTHER. This avoids a block copy. */
3456
3457 gimple_seq seq = NULL;
3458 gimple stmt = gsi_stmt (*gsi);
3459
3460 update_stmt_if_modified (stmt);
3461 generate_copy_inout (elt, is_output, other, &seq);
3462 gcc_assert (seq);
3463 mark_all_v_defs_seq (seq);
3464
3465 /* Preserve EH semantics. */
3466 if (stmt_ends_bb_p (stmt))
3467 {
3468 gimple_stmt_iterator si;
3469 gimple first;
3470 gimple_seq blist = NULL;
3471 bool thr = stmt_could_throw_p (stmt);
3472
3473 /* If the last statement of this BB created an EH edge
3474 before scalarization, we have to locate the first
3475 statement that can throw in the new statement list and
3476 use that as the last statement of this BB, such that EH
3477 semantics is preserved. All statements up to this one
3478 are added to the same BB. All other statements in the
3479 list will be added to normal outgoing edges of the same
3480 BB. If they access any memory, it's the same memory, so
3481 we can assume they won't throw. */
3482 si = gsi_start (seq);
3483 for (first = gsi_stmt (si);
3484 thr && !gsi_end_p (si) && !stmt_could_throw_p (first);
3485 first = gsi_stmt (si))
3486 {
3487 gsi_remove (&si, false);
3488 gimple_seq_add_stmt (&blist, first);
3489 }
3490
3491 /* Extract the first remaining statement from LIST, this is
3492 the EH statement if there is one. */
3493 gsi_remove (&si, false);
3494
3495 if (blist)
3496 sra_insert_before (gsi, blist);
3497
3498 /* Replace the old statement with this new representative. */
3499 gsi_replace (gsi, first, true);
3500
3501 if (!gsi_end_p (si))
3502 {
3503 /* If any reference would trap, then they all would. And more
3504 to the point, the first would. Therefore none of the rest
3505 will trap since the first didn't. Indicate this by
3506 iterating over the remaining statements and set
3507 TREE_THIS_NOTRAP in all INDIRECT_REFs. */
3508 do
3509 {
3510 walk_gimple_stmt (&si, NULL, mark_notrap, NULL);
3511 gsi_next (&si);
3512 }
3513 while (!gsi_end_p (si));
3514
3515 insert_edge_copies_seq (seq, gsi_bb (*gsi));
3516 }
3517 }
3518 else
3519 sra_replace (gsi, seq);
3520 }
3521 }
3522
3523 /* Generate initializations for all scalarizable parameters. */
3524
3525 static void
3526 scalarize_parms (void)
3527 {
3528 gimple_seq seq = NULL;
3529 unsigned i;
3530 bitmap_iterator bi;
3531
3532 EXECUTE_IF_SET_IN_BITMAP (needs_copy_in, 0, i, bi)
3533 {
3534 tree var = referenced_var (i);
3535 struct sra_elt *elt = lookup_element (NULL, var, NULL, NO_INSERT);
3536 generate_copy_inout (elt, true, var, &seq);
3537 }
3538
3539 if (seq)
3540 {
3541 insert_edge_copies_seq (seq, ENTRY_BLOCK_PTR);
3542 mark_all_v_defs_seq (seq);
3543 }
3544 }
3545
3546 /* Entry point to phase 4. Update the function to match replacements. */
3547
3548 static void
3549 scalarize_function (void)
3550 {
3551 static const struct sra_walk_fns fns = {
3552 scalarize_use, scalarize_copy, scalarize_init, scalarize_ldst, false
3553 };
3554
3555 sra_walk_function (&fns);
3556 scalarize_parms ();
3557 gsi_commit_edge_inserts ();
3558 }
3559
3560 \f
3561 /* Debug helper function. Print ELT in a nice human-readable format. */
3562
3563 static void
3564 dump_sra_elt_name (FILE *f, struct sra_elt *elt)
3565 {
3566 if (elt->parent && TREE_CODE (elt->parent->type) == COMPLEX_TYPE)
3567 {
3568 fputs (elt->element == integer_zero_node ? "__real__ " : "__imag__ ", f);
3569 dump_sra_elt_name (f, elt->parent);
3570 }
3571 else
3572 {
3573 if (elt->parent)
3574 dump_sra_elt_name (f, elt->parent);
3575 if (DECL_P (elt->element))
3576 {
3577 if (TREE_CODE (elt->element) == FIELD_DECL)
3578 fputc ('.', f);
3579 print_generic_expr (f, elt->element, dump_flags);
3580 }
3581 else if (TREE_CODE (elt->element) == BIT_FIELD_REF)
3582 fprintf (f, "$B" HOST_WIDE_INT_PRINT_DEC "F" HOST_WIDE_INT_PRINT_DEC,
3583 tree_low_cst (TREE_OPERAND (elt->element, 2), 1),
3584 tree_low_cst (TREE_OPERAND (elt->element, 1), 1));
3585 else if (TREE_CODE (elt->element) == RANGE_EXPR)
3586 fprintf (f, "["HOST_WIDE_INT_PRINT_DEC".."HOST_WIDE_INT_PRINT_DEC"]",
3587 TREE_INT_CST_LOW (TREE_OPERAND (elt->element, 0)),
3588 TREE_INT_CST_LOW (TREE_OPERAND (elt->element, 1)));
3589 else
3590 fprintf (f, "[" HOST_WIDE_INT_PRINT_DEC "]",
3591 TREE_INT_CST_LOW (elt->element));
3592 }
3593 }
3594
3595 /* Likewise, but callable from the debugger. */
3596
3597 void
3598 debug_sra_elt_name (struct sra_elt *elt)
3599 {
3600 dump_sra_elt_name (stderr, elt);
3601 fputc ('\n', stderr);
3602 }
3603
3604 static void
3605 sra_init_cache (void)
3606 {
3607 if (sra_type_decomp_cache)
3608 return;
3609
3610 sra_type_decomp_cache = BITMAP_ALLOC (NULL);
3611 sra_type_inst_cache = BITMAP_ALLOC (NULL);
3612 }
3613
3614
3615 /* Main entry point. */
3616
3617 static unsigned int
3618 tree_sra (void)
3619 {
3620 /* Initialize local variables. */
3621 gcc_obstack_init (&sra_obstack);
3622 sra_candidates = BITMAP_ALLOC (NULL);
3623 needs_copy_in = BITMAP_ALLOC (NULL);
3624 sra_init_cache ();
3625 sra_map = htab_create (101, sra_elt_hash, sra_elt_eq, NULL);
3626
3627 /* Scan. If we find anything, instantiate and scalarize. */
3628 if (find_candidates_for_sra ())
3629 {
3630 scan_function ();
3631 decide_instantiations ();
3632 scalarize_function ();
3633 }
3634
3635 /* Free allocated memory. */
3636 htab_delete (sra_map);
3637 sra_map = NULL;
3638 BITMAP_FREE (sra_candidates);
3639 BITMAP_FREE (needs_copy_in);
3640 BITMAP_FREE (sra_type_decomp_cache);
3641 BITMAP_FREE (sra_type_inst_cache);
3642 obstack_free (&sra_obstack, NULL);
3643 return 0;
3644 }
3645
3646 static unsigned int
3647 tree_sra_early (void)
3648 {
3649 unsigned int ret;
3650
3651 early_sra = true;
3652 ret = tree_sra ();
3653 early_sra = false;
3654
3655 return ret;
3656 }
3657
3658 static bool
3659 gate_sra (void)
3660 {
3661 return flag_tree_sra != 0;
3662 }
3663
3664 struct gimple_opt_pass pass_sra_early =
3665 {
3666 {
3667 GIMPLE_PASS,
3668 "esra", /* name */
3669 gate_sra, /* gate */
3670 tree_sra_early, /* execute */
3671 NULL, /* sub */
3672 NULL, /* next */
3673 0, /* static_pass_number */
3674 TV_TREE_SRA, /* tv_id */
3675 PROP_cfg | PROP_ssa, /* properties_required */
3676 0, /* properties_provided */
3677 0, /* properties_destroyed */
3678 0, /* todo_flags_start */
3679 TODO_dump_func
3680 | TODO_update_ssa
3681 | TODO_ggc_collect
3682 | TODO_verify_ssa /* todo_flags_finish */
3683 }
3684 };
3685
3686 struct gimple_opt_pass pass_sra =
3687 {
3688 {
3689 GIMPLE_PASS,
3690 "sra", /* name */
3691 gate_sra, /* gate */
3692 tree_sra, /* execute */
3693 NULL, /* sub */
3694 NULL, /* next */
3695 0, /* static_pass_number */
3696 TV_TREE_SRA, /* tv_id */
3697 PROP_cfg | PROP_ssa, /* properties_required */
3698 0, /* properties_provided */
3699 0, /* properties_destroyed */
3700 TODO_update_address_taken, /* todo_flags_start */
3701 TODO_dump_func
3702 | TODO_update_ssa
3703 | TODO_ggc_collect
3704 | TODO_verify_ssa /* todo_flags_finish */
3705 }
3706 };