re PR target/35659 (Miscompiled code with -O2 (but not with -O2 -funroll-loops) on...
[official-gcc.git] / gcc / tree-sra.c
blob01c0e121e0035351fc509e20570f25db0e08469f
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
5 Free Software Foundation, Inc.
6 Contributed by Diego Novillo <dnovillo@redhat.com>
8 This file is part of GCC.
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.
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.
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/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "ggc.h"
29 #include "tree.h"
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"
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.
59 This substitution is done globally. More localized substitutions would
60 be the purvey of a load-store motion pass.
62 The optimization proceeds in phases:
64 (1) Identify variables that have types that are candidates for
65 decomposition.
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.
72 (3) Based on the usage profile, instantiate substitution variables.
74 (4) Scan the function making replacements.
78 /* True if this is the "early" pass, before inlining. */
79 static bool early_sra;
81 /* The set of todo flags to return from tree_sra. */
82 static unsigned int todoflags;
84 /* The set of aggregate variables that are candidates for scalarization. */
85 static bitmap sra_candidates;
87 /* Set of scalarizable PARM_DECLs that need copy-in operations at the
88 beginning of the function. */
89 static bitmap needs_copy_in;
91 /* Sets of bit pairs that cache type decomposition and instantiation. */
92 static bitmap sra_type_decomp_cache;
93 static bitmap sra_type_inst_cache;
95 /* One of these structures is created for each candidate aggregate and
96 each (accessed) member or group of members of such an aggregate. */
97 struct sra_elt
99 /* A tree of the elements. Used when we want to traverse everything. */
100 struct sra_elt *parent;
101 struct sra_elt *groups;
102 struct sra_elt *children;
103 struct sra_elt *sibling;
105 /* If this element is a root, then this is the VAR_DECL. If this is
106 a sub-element, this is some token used to identify the reference.
107 In the case of COMPONENT_REF, this is the FIELD_DECL. In the case
108 of an ARRAY_REF, this is the (constant) index. In the case of an
109 ARRAY_RANGE_REF, this is the (constant) RANGE_EXPR. In the case
110 of a complex number, this is a zero or one. */
111 tree element;
113 /* The type of the element. */
114 tree type;
116 /* A VAR_DECL, for any sub-element we've decided to replace. */
117 tree replacement;
119 /* The number of times the element is referenced as a whole. I.e.
120 given "a.b.c", this would be incremented for C, but not for A or B. */
121 unsigned int n_uses;
123 /* The number of times the element is copied to or from another
124 scalarizable element. */
125 unsigned int n_copies;
127 /* True if TYPE is scalar. */
128 bool is_scalar;
130 /* True if this element is a group of members of its parent. */
131 bool is_group;
133 /* True if we saw something about this element that prevents scalarization,
134 such as non-constant indexing. */
135 bool cannot_scalarize;
137 /* True if we've decided that structure-to-structure assignment
138 should happen via memcpy and not per-element. */
139 bool use_block_copy;
141 /* True if everything under this element has been marked TREE_NO_WARNING. */
142 bool all_no_warning;
144 /* A flag for use with/after random access traversals. */
145 bool visited;
147 /* True if there is BIT_FIELD_REF on the lhs with a vector. */
148 bool is_vector_lhs;
150 /* 1 if the element is a field that is part of a block, 2 if the field
151 is the block itself, 0 if it's neither. */
152 char in_bitfld_block;
155 #define IS_ELEMENT_FOR_GROUP(ELEMENT) (TREE_CODE (ELEMENT) == RANGE_EXPR)
157 #define FOR_EACH_ACTUAL_CHILD(CHILD, ELT) \
158 for ((CHILD) = (ELT)->is_group \
159 ? next_child_for_group (NULL, (ELT)) \
160 : (ELT)->children; \
161 (CHILD); \
162 (CHILD) = (ELT)->is_group \
163 ? next_child_for_group ((CHILD), (ELT)) \
164 : (CHILD)->sibling)
166 /* Helper function for above macro. Return next child in group. */
167 static struct sra_elt *
168 next_child_for_group (struct sra_elt *child, struct sra_elt *group)
170 gcc_assert (group->is_group);
172 /* Find the next child in the parent. */
173 if (child)
174 child = child->sibling;
175 else
176 child = group->parent->children;
178 /* Skip siblings that do not belong to the group. */
179 while (child)
181 tree g_elt = group->element;
182 if (TREE_CODE (g_elt) == RANGE_EXPR)
184 if (!tree_int_cst_lt (child->element, TREE_OPERAND (g_elt, 0))
185 && !tree_int_cst_lt (TREE_OPERAND (g_elt, 1), child->element))
186 break;
188 else
189 gcc_unreachable ();
191 child = child->sibling;
194 return child;
197 /* Random access to the child of a parent is performed by hashing.
198 This prevents quadratic behavior, and allows SRA to function
199 reasonably on larger records. */
200 static htab_t sra_map;
202 /* All structures are allocated out of the following obstack. */
203 static struct obstack sra_obstack;
205 /* Debugging functions. */
206 static void dump_sra_elt_name (FILE *, struct sra_elt *);
207 extern void debug_sra_elt_name (struct sra_elt *);
209 /* Forward declarations. */
210 static tree generate_element_ref (struct sra_elt *);
211 static gimple_seq sra_build_assignment (tree dst, tree src);
212 static void mark_all_v_defs_seq (gimple_seq);
213 static void mark_all_v_defs_stmt (gimple);
216 /* Return true if DECL is an SRA candidate. */
218 static bool
219 is_sra_candidate_decl (tree decl)
221 return DECL_P (decl) && bitmap_bit_p (sra_candidates, DECL_UID (decl));
224 /* Return true if TYPE is a scalar type. */
226 static bool
227 is_sra_scalar_type (tree type)
229 enum tree_code code = TREE_CODE (type);
230 return (code == INTEGER_TYPE || code == REAL_TYPE || code == VECTOR_TYPE
231 || code == FIXED_POINT_TYPE
232 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
233 || code == POINTER_TYPE || code == OFFSET_TYPE
234 || code == REFERENCE_TYPE);
237 /* Return true if TYPE can be decomposed into a set of independent variables.
239 Note that this doesn't imply that all elements of TYPE can be
240 instantiated, just that if we decide to break up the type into
241 separate pieces that it can be done. */
243 bool
244 sra_type_can_be_decomposed_p (tree type)
246 unsigned int cache = TYPE_UID (TYPE_MAIN_VARIANT (type)) * 2;
247 tree t;
249 /* Avoid searching the same type twice. */
250 if (bitmap_bit_p (sra_type_decomp_cache, cache+0))
251 return true;
252 if (bitmap_bit_p (sra_type_decomp_cache, cache+1))
253 return false;
255 /* The type must have a definite nonzero size. */
256 if (TYPE_SIZE (type) == NULL || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
257 || integer_zerop (TYPE_SIZE (type)))
258 goto fail;
260 /* The type must be a non-union aggregate. */
261 switch (TREE_CODE (type))
263 case RECORD_TYPE:
265 bool saw_one_field = false;
267 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
268 if (TREE_CODE (t) == FIELD_DECL)
270 /* Reject incorrectly represented bit fields. */
271 if (DECL_BIT_FIELD (t)
272 && INTEGRAL_TYPE_P (TREE_TYPE (t))
273 && (tree_low_cst (DECL_SIZE (t), 1)
274 != TYPE_PRECISION (TREE_TYPE (t))))
275 goto fail;
277 saw_one_field = true;
280 /* Record types must have at least one field. */
281 if (!saw_one_field)
282 goto fail;
284 break;
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;
297 case COMPLEX_TYPE:
298 break;
300 default:
301 goto fail;
304 bitmap_set_bit (sra_type_decomp_cache, cache+0);
305 return true;
307 fail:
308 bitmap_set_bit (sra_type_decomp_cache, cache+1);
309 return false;
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. */
317 static bool
318 is_va_list_type (tree type)
320 tree h;
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;
332 /* Return true if DECL can be decomposed into a set of independent
333 (though not necessarily scalar) variables. */
335 static bool
336 decl_can_be_decomposed_p (tree var)
338 /* Early out for scalars. */
339 if (is_sra_scalar_type (TREE_TYPE (var)))
340 return false;
342 /* The variable must not be aliased. */
343 if (!is_gimple_non_addressable (var))
345 if (dump_file && (dump_flags & TDF_DETAILS))
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");
351 return false;
354 /* The variable must not be volatile. */
355 if (TREE_THIS_VOLATILE (var))
357 if (dump_file && (dump_flags & TDF_DETAILS))
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");
363 return false;
366 /* We must be able to decompose the variable's type. */
367 if (!sra_type_can_be_decomposed_p (TREE_TYPE (var)))
369 if (dump_file && (dump_flags & TDF_DETAILS))
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");
375 return false;
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;
387 return true;
390 /* Return true if TYPE can be *completely* decomposed into scalars. */
392 static bool
393 type_can_instantiate_all_elements (tree type)
395 if (is_sra_scalar_type (type))
396 return true;
397 if (!sra_type_can_be_decomposed_p (type))
398 return false;
400 switch (TREE_CODE (type))
402 case RECORD_TYPE:
404 unsigned int cache = TYPE_UID (TYPE_MAIN_VARIANT (type)) * 2;
405 tree f;
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;
412 for (f = TYPE_FIELDS (type); f ; f = TREE_CHAIN (f))
413 if (TREE_CODE (f) == FIELD_DECL)
415 if (!type_can_instantiate_all_elements (TREE_TYPE (f)))
417 bitmap_set_bit (sra_type_inst_cache, cache+1);
418 return false;
422 bitmap_set_bit (sra_type_inst_cache, cache+0);
423 return true;
426 case ARRAY_TYPE:
427 return type_can_instantiate_all_elements (TREE_TYPE (type));
429 case COMPLEX_TYPE:
430 return true;
432 default:
433 gcc_unreachable ();
437 /* Test whether ELT or some sub-element cannot be scalarized. */
439 static bool
440 can_completely_scalarize_p (struct sra_elt *elt)
442 struct sra_elt *c;
444 if (elt->cannot_scalarize)
445 return false;
447 for (c = elt->children; c; c = c->sibling)
448 if (!can_completely_scalarize_p (c))
449 return false;
451 for (c = elt->groups; c; c = c->sibling)
452 if (!can_completely_scalarize_p (c))
453 return false;
455 return true;
459 /* A simplified tree hashing algorithm that only handles the types of
460 trees we expect to find in sra_elt->element. */
462 static hashval_t
463 sra_hash_tree (tree t)
465 hashval_t h;
467 switch (TREE_CODE (t))
469 case VAR_DECL:
470 case PARM_DECL:
471 case RESULT_DECL:
472 h = DECL_UID (t);
473 break;
475 case INTEGER_CST:
476 h = TREE_INT_CST_LOW (t) ^ TREE_INT_CST_HIGH (t);
477 break;
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;
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;
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;
497 default:
498 gcc_unreachable ();
501 return h;
504 /* Hash function for type SRA_PAIR. */
506 static hashval_t
507 sra_elt_hash (const void *x)
509 const struct sra_elt *const e = (const struct sra_elt *) x;
510 const struct sra_elt *p;
511 hashval_t h;
513 h = sra_hash_tree (e->element);
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);
524 return h;
527 /* Equality function for type SRA_PAIR. */
529 static int
530 sra_elt_eq (const void *x, const void *y)
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;
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;
545 if (ap != bp)
546 return false;
548 ae = a->element;
549 be = b->element;
551 if (ae == be)
552 return true;
553 if (TREE_CODE (ae) != TREE_CODE (be))
554 return false;
556 switch (TREE_CODE (ae))
558 case VAR_DECL:
559 case PARM_DECL:
560 case RESULT_DECL:
561 /* These are all pointer unique. */
562 return false;
564 case INTEGER_CST:
565 /* Integers are not pointer unique, so compare their values. */
566 return tree_int_cst_equal (ae, be);
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));
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);
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));
585 default:
586 gcc_unreachable ();
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. */
593 static struct sra_elt *
594 lookup_element (struct sra_elt *parent, tree child, tree type,
595 enum insert_option insert)
597 struct sra_elt dummy;
598 struct sra_elt **slot;
599 struct sra_elt *elt;
601 if (parent)
602 dummy.parent = parent->is_group ? parent->parent : parent;
603 else
604 dummy.parent = NULL;
605 dummy.element = child;
607 slot = (struct sra_elt **) htab_find_slot (sra_map, &dummy, insert);
608 if (!slot && insert == NO_INSERT)
609 return NULL;
611 elt = *slot;
612 if (!elt && insert == INSERT)
614 *slot = elt = XOBNEW (&sra_obstack, struct sra_elt);
615 memset (elt, 0, sizeof (*elt));
617 elt->parent = parent;
618 elt->element = child;
619 elt->type = type;
620 elt->is_scalar = is_sra_scalar_type (type);
622 if (parent)
624 if (IS_ELEMENT_FOR_GROUP (elt->element))
626 elt->is_group = true;
627 elt->sibling = parent->groups;
628 parent->groups = elt;
630 else
632 elt->sibling = parent->children;
633 parent->children = elt;
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)
641 elt->n_copies = 1;
642 bitmap_set_bit (needs_copy_in, DECL_UID (child));
646 return elt;
649 /* Create or return the SRA_ELT structure for EXPR if the expression
650 refers to a scalarizable variable. */
652 static struct sra_elt *
653 maybe_lookup_element_for_expr (tree expr)
655 struct sra_elt *elt;
656 tree child;
658 switch (TREE_CODE (expr))
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;
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;
675 case ARRAY_RANGE_REF:
676 /* We can't scalarize variable array indices. */
677 if (range_in_array_bounds_p (expr))
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));
683 else
684 return NULL;
685 break;
687 case COMPONENT_REF:
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);
699 break;
701 case REALPART_EXPR:
702 child = integer_zero_node;
703 break;
704 case IMAGPART_EXPR:
705 child = integer_one_node;
706 break;
708 default:
709 return NULL;
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;
719 /* Functions to walk just enough of the tree to see all scalarizable
720 references, and categorize them. */
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
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);
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);
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);
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);
749 /* True during phase 2, false during phase 4. */
750 /* ??? This is a hack. */
751 bool initial_scan;
754 #ifdef ENABLE_CHECKING
755 /* Invoked via walk_tree, if *TP contains a candidate decl, return it. */
757 static tree
758 sra_find_candidate_decl (tree *tp, int *walk_subtrees,
759 void *data ATTRIBUTE_UNUSED)
761 tree t = *tp;
762 enum tree_code code = TREE_CODE (t);
764 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
766 *walk_subtrees = 0;
767 if (is_sra_candidate_decl (t))
768 return t;
770 else if (TYPE_P (t))
771 *walk_subtrees = 0;
773 return NULL;
775 #endif
777 /* Walk most expressions looking for a scalarizable aggregate.
778 If we find one, invoke FNS->USE. */
780 static void
781 sra_walk_expr (tree *expr_p, gimple_stmt_iterator *gsi, bool is_output,
782 const struct sra_walk_fns *fns)
784 tree expr = *expr_p;
785 tree inner = expr;
786 bool disable_scalarization = false;
787 bool use_all_p = false;
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. */
795 while (1)
796 switch (TREE_CODE (inner))
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))
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);
810 return;
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))
825 disable_scalarization = true;
826 goto use_all;
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;
835 case ARRAY_RANGE_REF:
836 if (!range_in_array_bounds_p (inner))
838 disable_scalarization = true;
839 goto use_all;
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;
847 case COMPONENT_REF:
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);
859 break;
861 case REALPART_EXPR:
862 case IMAGPART_EXPR:
863 inner = TREE_OPERAND (inner, 0);
864 break;
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)
873 struct sra_elt *elt
874 = maybe_lookup_element_for_expr (TREE_OPERAND (inner, 0));
875 if (elt)
876 elt->is_vector_lhs = true;
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;
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;
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;
900 case WITH_SIZE_EXPR:
901 /* This is a transparent wrapper. The entire inner expression really
902 is being used. */
903 goto use_all;
905 use_all:
906 expr_p = &TREE_OPERAND (inner, 0);
907 inner = expr = *expr_p;
908 use_all_p = true;
909 break;
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;
920 /* Walk the arguments of a GIMPLE_CALL looking for scalarizable aggregates.
921 If we find one, invoke FNS->USE. */
923 static void
924 sra_walk_gimple_call (gimple stmt, gimple_stmt_iterator *gsi,
925 const struct sra_walk_fns *fns)
927 int i;
928 int nargs = gimple_call_num_args (stmt);
930 for (i = 0; i < nargs; i++)
931 sra_walk_expr (gimple_call_arg_ptr (stmt, i), gsi, false, fns);
933 if (gimple_call_lhs (stmt))
934 sra_walk_expr (gimple_call_lhs_ptr (stmt), gsi, true, fns);
937 /* Walk the inputs and outputs of a GIMPLE_ASM looking for scalarizable
938 aggregates. If we find one, invoke FNS->USE. */
940 static void
941 sra_walk_gimple_asm (gimple stmt, gimple_stmt_iterator *gsi,
942 const struct sra_walk_fns *fns)
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);
951 /* Walk a GIMPLE_ASSIGN and categorize the assignment appropriately. */
953 static void
954 sra_walk_gimple_assign (gimple stmt, gimple_stmt_iterator *gsi,
955 const struct sra_walk_fns *fns)
957 struct sra_elt *lhs_elt = NULL, *rhs_elt = NULL;
958 tree lhs, rhs;
960 /* If there is more than 1 element on the RHS, only walk the lhs. */
961 if (!gimple_assign_single_p (stmt))
963 sra_walk_expr (gimple_assign_lhs_ptr (stmt), gsi, true, fns);
964 return;
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);
972 /* If both sides are scalarizable, this is a COPY operation. */
973 if (lhs_elt && rhs_elt)
975 fns->copy (lhs_elt, rhs_elt, gsi);
976 return;
979 /* If the RHS is scalarizable, handle it. There are only two cases. */
980 if (rhs_elt)
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);
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);
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)
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);
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 && TREE_READONLY (rhs)
1012 && targetm.binds_local_p (rhs))
1013 fns->init (lhs_elt, DECL_INITIAL (rhs), gsi);
1015 /* If this is a copy from a non-scalarizable lvalue, invoke LDST.
1016 The lvalue requirement prevents us from trying to directly scalarize
1017 the result of a function call. Which would result in trying to call
1018 the function multiple times, and other evil things. */
1019 else if (!lhs_elt->is_scalar
1020 && !TREE_SIDE_EFFECTS (rhs) && is_gimple_addressable (rhs))
1021 fns->ldst (lhs_elt, rhs, gsi, true);
1023 /* Otherwise we're being used in some context that requires the
1024 aggregate to be seen as a whole. Invoke USE. */
1025 else
1026 fns->use (lhs_elt, gimple_assign_lhs_ptr (stmt), gsi, true, false);
1029 /* Similarly to above, LHS_ELT being null only means that the LHS as a
1030 whole is not a scalarizable reference. There may be occurrences of
1031 scalarizable variables within, which implies a USE. */
1032 else
1033 sra_walk_expr (gimple_assign_lhs_ptr (stmt), gsi, true, fns);
1036 /* Entry point to the walk functions. Search the entire function,
1037 invoking the callbacks in FNS on each of the references to
1038 scalarizable variables. */
1040 static void
1041 sra_walk_function (const struct sra_walk_fns *fns)
1043 basic_block bb;
1044 gimple_stmt_iterator si, ni;
1046 /* ??? Phase 4 could derive some benefit to walking the function in
1047 dominator tree order. */
1049 FOR_EACH_BB (bb)
1050 for (si = gsi_start_bb (bb); !gsi_end_p (si); si = ni)
1052 gimple stmt;
1054 stmt = gsi_stmt (si);
1056 ni = si;
1057 gsi_next (&ni);
1059 /* If the statement has no virtual operands, then it doesn't
1060 make any structure references that we care about. */
1061 if (gimple_aliases_computed_p (cfun)
1062 && ZERO_SSA_OPERANDS (stmt, (SSA_OP_VIRTUAL_DEFS | SSA_OP_VUSE)))
1063 continue;
1065 switch (gimple_code (stmt))
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.
1072 if (gimple_return_retval (stmt) == NULL_TREE)
1074 else
1075 sra_walk_expr (gimple_return_retval_ptr (stmt), &si, false,
1076 fns);
1077 break;
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;
1089 default:
1090 break;
1095 /* Phase One: Scan all referenced variables in the program looking for
1096 structures that could be decomposed. */
1098 static bool
1099 find_candidates_for_sra (void)
1101 bool any_set = false;
1102 tree var;
1103 referenced_var_iterator rvi;
1105 FOR_EACH_REFERENCED_VAR (var, rvi)
1107 if (decl_can_be_decomposed_p (var))
1109 bitmap_set_bit (sra_candidates, DECL_UID (var));
1110 any_set = true;
1114 return any_set;
1118 /* Phase Two: Scan all references to scalarizable variables. Count the
1119 number of times they are used or copied respectively. */
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. */
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)
1130 elt->n_uses += 1;
1133 static void
1134 scan_copy (struct sra_elt *lhs_elt, struct sra_elt *rhs_elt,
1135 gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED)
1137 lhs_elt->n_copies += 1;
1138 rhs_elt->n_copies += 1;
1141 static void
1142 scan_init (struct sra_elt *lhs_elt, tree rhs ATTRIBUTE_UNUSED,
1143 gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED)
1145 lhs_elt->n_copies += 1;
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)
1153 elt->n_copies += 1;
1156 /* Dump the values we collected during the scanning phase. */
1158 static void
1159 scan_dump (struct sra_elt *elt)
1161 struct sra_elt *c;
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);
1166 for (c = elt->children; c ; c = c->sibling)
1167 scan_dump (c);
1169 for (c = elt->groups; c ; c = c->sibling)
1170 scan_dump (c);
1173 /* Entry point to phase 2. Scan the entire function, building up
1174 scalarization data structures, recording copies and uses. */
1176 static void
1177 scan_function (void)
1179 static const struct sra_walk_fns fns = {
1180 scan_use, scan_copy, scan_init, scan_ldst, true
1182 bitmap_iterator bi;
1184 sra_walk_function (&fns);
1186 if (dump_file && (dump_flags & TDF_DETAILS))
1188 unsigned i;
1190 fputs ("\nScan results:\n", dump_file);
1191 EXECUTE_IF_SET_IN_BITMAP (sra_candidates, 0, i, bi)
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);
1198 fputc ('\n', dump_file);
1202 /* Phase Three: Make decisions about which variables to scalarize, if any.
1203 All elements to be scalarized have replacement variables made for them. */
1205 /* A subroutine of build_element_name. Recursively build the element
1206 name on the obstack. */
1208 static void
1209 build_element_name_1 (struct sra_elt *elt)
1211 tree t;
1212 char buffer[32];
1214 if (elt->parent)
1216 build_element_name_1 (elt->parent);
1217 obstack_1grow (&sra_obstack, '$');
1219 if (TREE_CODE (elt->parent->type) == COMPLEX_TYPE)
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;
1229 t = elt->element;
1230 if (TREE_CODE (t) == INTEGER_CST)
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));
1236 else if (TREE_CODE (t) == BIT_FIELD_REF)
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));
1245 else
1247 tree name = DECL_NAME (t);
1248 if (name)
1249 obstack_grow (&sra_obstack, IDENTIFIER_POINTER (name),
1250 IDENTIFIER_LENGTH (name));
1251 else
1253 sprintf (buffer, "D%u", DECL_UID (t));
1254 obstack_grow (&sra_obstack, buffer, strlen (buffer));
1259 /* Construct a pretty variable name for an element's replacement variable.
1260 The name is built on the obstack. */
1262 static char *
1263 build_element_name (struct sra_elt *elt)
1265 build_element_name_1 (elt);
1266 obstack_1grow (&sra_obstack, '\0');
1267 return XOBFINISH (&sra_obstack, char *);
1270 /* Instantiate an element as an independent variable. */
1272 static void
1273 instantiate_element (struct sra_elt *elt)
1275 struct sra_elt *base_elt;
1276 tree var, base;
1277 bool nowarn = TREE_NO_WARNING (elt->element);
1279 for (base_elt = elt; base_elt->parent; base_elt = base_elt->parent)
1280 if (!nowarn)
1281 nowarn = TREE_NO_WARNING (base_elt->parent->element);
1282 base = base_elt->element;
1284 elt->replacement = var = make_rename_temp (elt->type, "SR");
1286 if (DECL_P (elt->element)
1287 && !tree_int_cst_equal (DECL_SIZE (var), DECL_SIZE (elt->element)))
1289 DECL_SIZE (var) = DECL_SIZE (elt->element);
1290 DECL_SIZE_UNIT (var) = DECL_SIZE_UNIT (elt->element);
1292 elt->in_bitfld_block = 1;
1293 elt->replacement = fold_build3 (BIT_FIELD_REF, elt->type, var,
1294 DECL_SIZE (var),
1295 BYTES_BIG_ENDIAN
1296 ? size_binop (MINUS_EXPR,
1297 TYPE_SIZE (elt->type),
1298 DECL_SIZE (var))
1299 : bitsize_int (0));
1302 /* For vectors, if used on the left hand side with BIT_FIELD_REF,
1303 they are not a gimple register. */
1304 if (TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE && elt->is_vector_lhs)
1305 DECL_GIMPLE_REG_P (var) = 0;
1307 DECL_SOURCE_LOCATION (var) = DECL_SOURCE_LOCATION (base);
1308 DECL_ARTIFICIAL (var) = 1;
1310 if (TREE_THIS_VOLATILE (elt->type))
1312 TREE_THIS_VOLATILE (var) = 1;
1313 TREE_SIDE_EFFECTS (var) = 1;
1316 if (DECL_NAME (base) && !DECL_IGNORED_P (base))
1318 char *pretty_name = build_element_name (elt);
1319 DECL_NAME (var) = get_identifier (pretty_name);
1320 obstack_free (&sra_obstack, pretty_name);
1322 SET_DECL_DEBUG_EXPR (var, generate_element_ref (elt));
1323 DECL_DEBUG_EXPR_IS_FROM (var) = 1;
1325 DECL_IGNORED_P (var) = 0;
1326 TREE_NO_WARNING (var) = nowarn;
1328 else
1330 DECL_IGNORED_P (var) = 1;
1331 /* ??? We can't generate any warning that would be meaningful. */
1332 TREE_NO_WARNING (var) = 1;
1335 /* Zero-initialize bit-field scalarization variables, to avoid
1336 triggering undefined behavior. */
1337 if (TREE_CODE (elt->element) == BIT_FIELD_REF
1338 || (var != elt->replacement
1339 && TREE_CODE (elt->replacement) == BIT_FIELD_REF))
1341 gimple_seq init = sra_build_assignment (var,
1342 fold_convert (TREE_TYPE (var),
1343 integer_zero_node)
1345 insert_edge_copies_seq (init, ENTRY_BLOCK_PTR);
1346 mark_all_v_defs_seq (init);
1349 if (dump_file)
1351 fputs (" ", dump_file);
1352 dump_sra_elt_name (dump_file, elt);
1353 fputs (" -> ", dump_file);
1354 print_generic_expr (dump_file, var, dump_flags);
1355 fputc ('\n', dump_file);
1359 /* Make one pass across an element tree deciding whether or not it's
1360 profitable to instantiate individual leaf scalars.
1362 PARENT_USES and PARENT_COPIES are the sum of the N_USES and N_COPIES
1363 fields all the way up the tree. */
1365 static void
1366 decide_instantiation_1 (struct sra_elt *elt, unsigned int parent_uses,
1367 unsigned int parent_copies)
1369 if (dump_file && !elt->parent)
1371 fputs ("Initial instantiation for ", dump_file);
1372 dump_sra_elt_name (dump_file, elt);
1373 fputc ('\n', dump_file);
1376 if (elt->cannot_scalarize)
1377 return;
1379 if (elt->is_scalar)
1381 /* The decision is simple: instantiate if we're used more frequently
1382 than the parent needs to be seen as a complete unit. */
1383 if (elt->n_uses + elt->n_copies + parent_copies > parent_uses)
1384 instantiate_element (elt);
1386 else
1388 struct sra_elt *c, *group;
1389 unsigned int this_uses = elt->n_uses + parent_uses;
1390 unsigned int this_copies = elt->n_copies + parent_copies;
1392 /* Consider groups of sub-elements as weighing in favour of
1393 instantiation whatever their size. */
1394 for (group = elt->groups; group ; group = group->sibling)
1395 FOR_EACH_ACTUAL_CHILD (c, group)
1397 c->n_uses += group->n_uses;
1398 c->n_copies += group->n_copies;
1401 for (c = elt->children; c ; c = c->sibling)
1402 decide_instantiation_1 (c, this_uses, this_copies);
1406 /* Compute the size and number of all instantiated elements below ELT.
1407 We will only care about this if the size of the complete structure
1408 fits in a HOST_WIDE_INT, so we don't have to worry about overflow. */
1410 static unsigned int
1411 sum_instantiated_sizes (struct sra_elt *elt, unsigned HOST_WIDE_INT *sizep)
1413 if (elt->replacement)
1415 *sizep += TREE_INT_CST_LOW (TYPE_SIZE_UNIT (elt->type));
1416 return 1;
1418 else
1420 struct sra_elt *c;
1421 unsigned int count = 0;
1423 for (c = elt->children; c ; c = c->sibling)
1424 count += sum_instantiated_sizes (c, sizep);
1426 return count;
1430 /* Instantiate fields in ELT->TYPE that are not currently present as
1431 children of ELT. */
1433 static void instantiate_missing_elements (struct sra_elt *elt);
1435 static struct sra_elt *
1436 instantiate_missing_elements_1 (struct sra_elt *elt, tree child, tree type)
1438 struct sra_elt *sub = lookup_element (elt, child, type, INSERT);
1439 if (sub->is_scalar)
1441 if (sub->replacement == NULL)
1442 instantiate_element (sub);
1444 else
1445 instantiate_missing_elements (sub);
1446 return sub;
1449 /* Obtain the canonical type for field F of ELEMENT. */
1451 static tree
1452 canon_type_for_field (tree f, tree element)
1454 tree field_type = TREE_TYPE (f);
1456 /* canonicalize_component_ref() unwidens some bit-field types (not
1457 marked as DECL_BIT_FIELD in C++), so we must do the same, lest we
1458 may introduce type mismatches. */
1459 if (INTEGRAL_TYPE_P (field_type)
1460 && DECL_MODE (f) != TYPE_MODE (field_type))
1461 field_type = TREE_TYPE (get_unwidened (build3 (COMPONENT_REF,
1462 field_type,
1463 element,
1464 f, NULL_TREE),
1465 NULL_TREE));
1467 return field_type;
1470 /* Look for adjacent fields of ELT starting at F that we'd like to
1471 scalarize as a single variable. Return the last field of the
1472 group. */
1474 static tree
1475 try_instantiate_multiple_fields (struct sra_elt *elt, tree f)
1477 int count;
1478 unsigned HOST_WIDE_INT align, bit, size, alchk;
1479 enum machine_mode mode;
1480 tree first = f, prev;
1481 tree type, var;
1482 struct sra_elt *block;
1484 /* Point fields are typically best handled as standalone entities. */
1485 if (POINTER_TYPE_P (TREE_TYPE (f)))
1486 return f;
1488 if (!is_sra_scalar_type (TREE_TYPE (f))
1489 || !host_integerp (DECL_FIELD_OFFSET (f), 1)
1490 || !host_integerp (DECL_FIELD_BIT_OFFSET (f), 1)
1491 || !host_integerp (DECL_SIZE (f), 1)
1492 || lookup_element (elt, f, NULL, NO_INSERT))
1493 return f;
1495 block = elt;
1497 /* For complex and array objects, there are going to be integer
1498 literals as child elements. In this case, we can't just take the
1499 alignment and mode of the decl, so we instead rely on the element
1500 type.
1502 ??? We could try to infer additional alignment from the full
1503 object declaration and the location of the sub-elements we're
1504 accessing. */
1505 for (count = 0; !DECL_P (block->element); count++)
1506 block = block->parent;
1508 align = DECL_ALIGN (block->element);
1509 alchk = GET_MODE_BITSIZE (DECL_MODE (block->element));
1511 if (count)
1513 type = TREE_TYPE (block->element);
1514 while (count--)
1515 type = TREE_TYPE (type);
1517 align = TYPE_ALIGN (type);
1518 alchk = GET_MODE_BITSIZE (TYPE_MODE (type));
1521 if (align < alchk)
1522 align = alchk;
1524 /* Coalescing wider fields is probably pointless and
1525 inefficient. */
1526 if (align > BITS_PER_WORD)
1527 align = BITS_PER_WORD;
1529 bit = tree_low_cst (DECL_FIELD_OFFSET (f), 1) * BITS_PER_UNIT
1530 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f), 1);
1531 size = tree_low_cst (DECL_SIZE (f), 1);
1533 alchk = align - 1;
1534 alchk = ~alchk;
1536 if ((bit & alchk) != ((bit + size - 1) & alchk))
1537 return f;
1539 /* Find adjacent fields in the same alignment word. */
1541 for (prev = f, f = TREE_CHAIN (f);
1542 f && TREE_CODE (f) == FIELD_DECL
1543 && is_sra_scalar_type (TREE_TYPE (f))
1544 && host_integerp (DECL_FIELD_OFFSET (f), 1)
1545 && host_integerp (DECL_FIELD_BIT_OFFSET (f), 1)
1546 && host_integerp (DECL_SIZE (f), 1)
1547 && !lookup_element (elt, f, NULL, NO_INSERT);
1548 prev = f, f = TREE_CHAIN (f))
1550 unsigned HOST_WIDE_INT nbit, nsize;
1552 nbit = tree_low_cst (DECL_FIELD_OFFSET (f), 1) * BITS_PER_UNIT
1553 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f), 1);
1554 nsize = tree_low_cst (DECL_SIZE (f), 1);
1556 if (bit + size == nbit)
1558 if ((bit & alchk) != ((nbit + nsize - 1) & alchk))
1560 /* If we're at an alignment boundary, don't bother
1561 growing alignment such that we can include this next
1562 field. */
1563 if ((nbit & alchk)
1564 || GET_MODE_BITSIZE (DECL_MODE (f)) <= align)
1565 break;
1567 align = GET_MODE_BITSIZE (DECL_MODE (f));
1568 alchk = align - 1;
1569 alchk = ~alchk;
1571 if ((bit & alchk) != ((nbit + nsize - 1) & alchk))
1572 break;
1574 size += nsize;
1576 else if (nbit + nsize == bit)
1578 if ((nbit & alchk) != ((bit + size - 1) & alchk))
1580 if ((bit & alchk)
1581 || GET_MODE_BITSIZE (DECL_MODE (f)) <= align)
1582 break;
1584 align = GET_MODE_BITSIZE (DECL_MODE (f));
1585 alchk = align - 1;
1586 alchk = ~alchk;
1588 if ((nbit & alchk) != ((bit + size - 1) & alchk))
1589 break;
1591 bit = nbit;
1592 size += nsize;
1594 else
1595 break;
1598 f = prev;
1600 if (f == first)
1601 return f;
1603 gcc_assert ((bit & alchk) == ((bit + size - 1) & alchk));
1605 /* Try to widen the bit range so as to cover padding bits as well. */
1607 if ((bit & ~alchk) || size != align)
1609 unsigned HOST_WIDE_INT mbit = bit & alchk;
1610 unsigned HOST_WIDE_INT msize = align;
1612 for (f = TYPE_FIELDS (elt->type);
1613 f; f = TREE_CHAIN (f))
1615 unsigned HOST_WIDE_INT fbit, fsize;
1617 /* Skip the fields from first to prev. */
1618 if (f == first)
1620 f = prev;
1621 continue;
1624 if (!(TREE_CODE (f) == FIELD_DECL
1625 && host_integerp (DECL_FIELD_OFFSET (f), 1)
1626 && host_integerp (DECL_FIELD_BIT_OFFSET (f), 1)))
1627 continue;
1629 fbit = tree_low_cst (DECL_FIELD_OFFSET (f), 1) * BITS_PER_UNIT
1630 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f), 1);
1632 /* If we're past the selected word, we're fine. */
1633 if ((bit & alchk) < (fbit & alchk))
1634 continue;
1636 if (host_integerp (DECL_SIZE (f), 1))
1637 fsize = tree_low_cst (DECL_SIZE (f), 1);
1638 else
1639 /* Assume a variable-sized field takes up all space till
1640 the end of the word. ??? Endianness issues? */
1641 fsize = align - (fbit & alchk);
1643 if ((fbit & alchk) < (bit & alchk))
1645 /* A large field might start at a previous word and
1646 extend into the selected word. Exclude those
1647 bits. ??? Endianness issues? */
1648 HOST_WIDE_INT diff = fbit + fsize - mbit;
1650 if (diff <= 0)
1651 continue;
1653 mbit += diff;
1654 msize -= diff;
1656 else
1658 /* Non-overlapping, great. */
1659 if (fbit + fsize <= mbit
1660 || mbit + msize <= fbit)
1661 continue;
1663 if (fbit <= mbit)
1665 unsigned HOST_WIDE_INT diff = fbit + fsize - mbit;
1666 mbit += diff;
1667 msize -= diff;
1669 else if (fbit > mbit)
1670 msize -= (mbit + msize - fbit);
1671 else
1672 gcc_unreachable ();
1676 bit = mbit;
1677 size = msize;
1680 /* Now we know the bit range we're interested in. Find the smallest
1681 machine mode we can use to access it. */
1683 for (mode = smallest_mode_for_size (size, MODE_INT);
1685 mode = GET_MODE_WIDER_MODE (mode))
1687 gcc_assert (mode != VOIDmode);
1689 alchk = GET_MODE_PRECISION (mode) - 1;
1690 alchk = ~alchk;
1692 if ((bit & alchk) == ((bit + size - 1) & alchk))
1693 break;
1696 gcc_assert (~alchk < align);
1698 /* Create the field group as a single variable. */
1700 /* We used to create a type for the mode above, but size turns
1701 to be out not of mode-size. As we need a matching type
1702 to build a BIT_FIELD_REF, use a nonstandard integer type as
1703 fallback. */
1704 type = lang_hooks.types.type_for_size (size, 1);
1705 if (!type || TYPE_PRECISION (type) != size)
1706 type = build_nonstandard_integer_type (size, 1);
1707 gcc_assert (type);
1708 var = build3 (BIT_FIELD_REF, type, NULL_TREE,
1709 bitsize_int (size), bitsize_int (bit));
1711 block = instantiate_missing_elements_1 (elt, var, type);
1712 gcc_assert (block && block->is_scalar);
1714 var = block->replacement;
1716 if ((bit & ~alchk)
1717 || (HOST_WIDE_INT)size != tree_low_cst (DECL_SIZE (var), 1))
1719 block->replacement = fold_build3 (BIT_FIELD_REF,
1720 TREE_TYPE (block->element), var,
1721 bitsize_int (size),
1722 bitsize_int (bit & ~alchk));
1725 block->in_bitfld_block = 2;
1727 /* Add the member fields to the group, such that they access
1728 portions of the group variable. */
1730 for (f = first; f != TREE_CHAIN (prev); f = TREE_CHAIN (f))
1732 tree field_type = canon_type_for_field (f, elt->element);
1733 struct sra_elt *fld = lookup_element (block, f, field_type, INSERT);
1735 gcc_assert (fld && fld->is_scalar && !fld->replacement);
1737 fld->replacement = fold_build3 (BIT_FIELD_REF, field_type, var,
1738 DECL_SIZE (f),
1739 bitsize_int
1740 ((TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f))
1741 * BITS_PER_UNIT
1742 + (TREE_INT_CST_LOW
1743 (DECL_FIELD_BIT_OFFSET (f))))
1744 & ~alchk));
1745 fld->in_bitfld_block = 1;
1748 return prev;
1751 static void
1752 instantiate_missing_elements (struct sra_elt *elt)
1754 tree type = elt->type;
1756 switch (TREE_CODE (type))
1758 case RECORD_TYPE:
1760 tree f;
1761 for (f = TYPE_FIELDS (type); f ; f = TREE_CHAIN (f))
1762 if (TREE_CODE (f) == FIELD_DECL)
1764 tree last = try_instantiate_multiple_fields (elt, f);
1766 if (last != f)
1768 f = last;
1769 continue;
1772 instantiate_missing_elements_1 (elt, f,
1773 canon_type_for_field
1774 (f, elt->element));
1776 break;
1779 case ARRAY_TYPE:
1781 tree i, max, subtype;
1783 i = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
1784 max = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1785 subtype = TREE_TYPE (type);
1787 while (1)
1789 instantiate_missing_elements_1 (elt, i, subtype);
1790 if (tree_int_cst_equal (i, max))
1791 break;
1792 i = int_const_binop (PLUS_EXPR, i, integer_one_node, true);
1795 break;
1798 case COMPLEX_TYPE:
1799 type = TREE_TYPE (type);
1800 instantiate_missing_elements_1 (elt, integer_zero_node, type);
1801 instantiate_missing_elements_1 (elt, integer_one_node, type);
1802 break;
1804 default:
1805 gcc_unreachable ();
1809 /* Return true if there is only one non aggregate field in the record, TYPE.
1810 Return false otherwise. */
1812 static bool
1813 single_scalar_field_in_record_p (tree type)
1815 int num_fields = 0;
1816 tree field;
1817 if (TREE_CODE (type) != RECORD_TYPE)
1818 return false;
1820 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1821 if (TREE_CODE (field) == FIELD_DECL)
1823 num_fields++;
1825 if (num_fields == 2)
1826 return false;
1828 if (AGGREGATE_TYPE_P (TREE_TYPE (field)))
1829 return false;
1832 return true;
1835 /* Make one pass across an element tree deciding whether to perform block
1836 or element copies. If we decide on element copies, instantiate all
1837 elements. Return true if there are any instantiated sub-elements. */
1839 static bool
1840 decide_block_copy (struct sra_elt *elt)
1842 struct sra_elt *c;
1843 bool any_inst;
1845 /* We shouldn't be invoked on groups of sub-elements as they must
1846 behave like their parent as far as block copy is concerned. */
1847 gcc_assert (!elt->is_group);
1849 /* If scalarization is disabled, respect it. */
1850 if (elt->cannot_scalarize)
1852 elt->use_block_copy = 1;
1854 if (dump_file)
1856 fputs ("Scalarization disabled for ", dump_file);
1857 dump_sra_elt_name (dump_file, elt);
1858 fputc ('\n', dump_file);
1861 /* Disable scalarization of sub-elements */
1862 for (c = elt->children; c; c = c->sibling)
1864 c->cannot_scalarize = 1;
1865 decide_block_copy (c);
1868 /* Groups behave like their parent. */
1869 for (c = elt->groups; c; c = c->sibling)
1871 c->cannot_scalarize = 1;
1872 c->use_block_copy = 1;
1875 return false;
1878 /* Don't decide if we've no uses and no groups. */
1879 if (elt->n_uses == 0 && elt->n_copies == 0 && elt->groups == NULL)
1882 else if (!elt->is_scalar)
1884 tree size_tree = TYPE_SIZE_UNIT (elt->type);
1885 bool use_block_copy = true;
1887 /* Tradeoffs for COMPLEX types pretty much always make it better
1888 to go ahead and split the components. */
1889 if (TREE_CODE (elt->type) == COMPLEX_TYPE)
1890 use_block_copy = false;
1892 /* Don't bother trying to figure out the rest if the structure is
1893 so large we can't do easy arithmetic. This also forces block
1894 copies for variable sized structures. */
1895 else if (host_integerp (size_tree, 1))
1897 unsigned HOST_WIDE_INT full_size, inst_size = 0;
1898 unsigned int max_size, max_count, inst_count, full_count;
1900 /* If the sra-max-structure-size parameter is 0, then the
1901 user has not overridden the parameter and we can choose a
1902 sensible default. */
1903 max_size = SRA_MAX_STRUCTURE_SIZE
1904 ? SRA_MAX_STRUCTURE_SIZE
1905 : MOVE_RATIO (optimize_function_for_speed_p (cfun)) * UNITS_PER_WORD;
1906 max_count = SRA_MAX_STRUCTURE_COUNT
1907 ? SRA_MAX_STRUCTURE_COUNT
1908 : MOVE_RATIO (optimize_function_for_speed_p (cfun));
1910 full_size = tree_low_cst (size_tree, 1);
1911 full_count = count_type_elements (elt->type, false);
1912 inst_count = sum_instantiated_sizes (elt, &inst_size);
1914 /* If there is only one scalar field in the record, don't block copy. */
1915 if (single_scalar_field_in_record_p (elt->type))
1916 use_block_copy = false;
1918 /* ??? What to do here. If there are two fields, and we've only
1919 instantiated one, then instantiating the other is clearly a win.
1920 If there are a large number of fields then the size of the copy
1921 is much more of a factor. */
1923 /* If the structure is small, and we've made copies, go ahead
1924 and instantiate, hoping that the copies will go away. */
1925 if (full_size <= max_size
1926 && (full_count - inst_count) <= max_count
1927 && elt->n_copies > elt->n_uses)
1928 use_block_copy = false;
1929 else if (inst_count * 100 >= full_count * SRA_FIELD_STRUCTURE_RATIO
1930 && inst_size * 100 >= full_size * SRA_FIELD_STRUCTURE_RATIO)
1931 use_block_copy = false;
1933 /* In order to avoid block copy, we have to be able to instantiate
1934 all elements of the type. See if this is possible. */
1935 if (!use_block_copy
1936 && (!can_completely_scalarize_p (elt)
1937 || !type_can_instantiate_all_elements (elt->type)))
1938 use_block_copy = true;
1941 elt->use_block_copy = use_block_copy;
1943 /* Groups behave like their parent. */
1944 for (c = elt->groups; c; c = c->sibling)
1945 c->use_block_copy = use_block_copy;
1947 if (dump_file)
1949 fprintf (dump_file, "Using %s for ",
1950 use_block_copy ? "block-copy" : "element-copy");
1951 dump_sra_elt_name (dump_file, elt);
1952 fputc ('\n', dump_file);
1955 if (!use_block_copy)
1957 instantiate_missing_elements (elt);
1958 return true;
1962 any_inst = elt->replacement != NULL;
1964 for (c = elt->children; c ; c = c->sibling)
1965 any_inst |= decide_block_copy (c);
1967 return any_inst;
1970 /* Entry point to phase 3. Instantiate scalar replacement variables. */
1972 static void
1973 decide_instantiations (void)
1975 unsigned int i;
1976 bool cleared_any;
1977 bitmap_head done_head;
1978 bitmap_iterator bi;
1980 /* We cannot clear bits from a bitmap we're iterating over,
1981 so save up all the bits to clear until the end. */
1982 bitmap_initialize (&done_head, &bitmap_default_obstack);
1983 cleared_any = false;
1985 EXECUTE_IF_SET_IN_BITMAP (sra_candidates, 0, i, bi)
1987 tree var = referenced_var (i);
1988 struct sra_elt *elt = lookup_element (NULL, var, NULL, NO_INSERT);
1989 if (elt)
1991 decide_instantiation_1 (elt, 0, 0);
1992 if (!decide_block_copy (elt))
1993 elt = NULL;
1995 if (!elt)
1997 bitmap_set_bit (&done_head, i);
1998 cleared_any = true;
2002 if (cleared_any)
2004 bitmap_and_compl_into (sra_candidates, &done_head);
2005 bitmap_and_compl_into (needs_copy_in, &done_head);
2007 bitmap_clear (&done_head);
2009 mark_set_for_renaming (sra_candidates);
2011 if (dump_file)
2012 fputc ('\n', dump_file);
2016 /* Phase Four: Update the function to match the replacements created. */
2018 /* Mark all the variables in VDEF/VUSE operators for STMT for
2019 renaming. This becomes necessary when we modify all of a
2020 non-scalar. */
2022 static void
2023 mark_all_v_defs_stmt (gimple stmt)
2025 tree sym;
2026 ssa_op_iter iter;
2028 update_stmt_if_modified (stmt);
2030 FOR_EACH_SSA_TREE_OPERAND (sym, stmt, iter, SSA_OP_ALL_VIRTUALS)
2032 if (TREE_CODE (sym) == SSA_NAME)
2033 sym = SSA_NAME_VAR (sym);
2034 mark_sym_for_renaming (sym);
2039 /* Mark all the variables in virtual operands in all the statements in
2040 LIST for renaming. */
2042 static void
2043 mark_all_v_defs_seq (gimple_seq seq)
2045 gimple_stmt_iterator gsi;
2047 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
2048 mark_all_v_defs_stmt (gsi_stmt (gsi));
2051 /* Mark every replacement under ELT with TREE_NO_WARNING. */
2053 static void
2054 mark_no_warning (struct sra_elt *elt)
2056 if (!elt->all_no_warning)
2058 if (elt->replacement)
2059 TREE_NO_WARNING (elt->replacement) = 1;
2060 else
2062 struct sra_elt *c;
2063 FOR_EACH_ACTUAL_CHILD (c, elt)
2064 mark_no_warning (c);
2066 elt->all_no_warning = true;
2070 /* Build a single level component reference to ELT rooted at BASE. */
2072 static tree
2073 generate_one_element_ref (struct sra_elt *elt, tree base)
2075 switch (TREE_CODE (TREE_TYPE (base)))
2077 case RECORD_TYPE:
2079 tree field = elt->element;
2081 /* We can't test elt->in_bitfld_block here because, when this is
2082 called from instantiate_element, we haven't set this field
2083 yet. */
2084 if (TREE_CODE (field) == BIT_FIELD_REF)
2086 tree ret = unshare_expr (field);
2087 TREE_OPERAND (ret, 0) = base;
2088 return ret;
2091 /* Watch out for compatible records with differing field lists. */
2092 if (DECL_FIELD_CONTEXT (field) != TYPE_MAIN_VARIANT (TREE_TYPE (base)))
2093 field = find_compatible_field (TREE_TYPE (base), field);
2095 return build3 (COMPONENT_REF, elt->type, base, field, NULL);
2098 case ARRAY_TYPE:
2099 if (TREE_CODE (elt->element) == RANGE_EXPR)
2100 return build4 (ARRAY_RANGE_REF, elt->type, base,
2101 TREE_OPERAND (elt->element, 0), NULL, NULL);
2102 else
2103 return build4 (ARRAY_REF, elt->type, base, elt->element, NULL, NULL);
2105 case COMPLEX_TYPE:
2106 if (elt->element == integer_zero_node)
2107 return build1 (REALPART_EXPR, elt->type, base);
2108 else
2109 return build1 (IMAGPART_EXPR, elt->type, base);
2111 default:
2112 gcc_unreachable ();
2116 /* Build a full component reference to ELT rooted at its native variable. */
2118 static tree
2119 generate_element_ref (struct sra_elt *elt)
2121 if (elt->parent)
2122 return generate_one_element_ref (elt, generate_element_ref (elt->parent));
2123 else
2124 return elt->element;
2127 /* Return true if BF is a bit-field that we can handle like a scalar. */
2129 static bool
2130 scalar_bitfield_p (tree bf)
2132 return (TREE_CODE (bf) == BIT_FIELD_REF
2133 && (is_gimple_reg (TREE_OPERAND (bf, 0))
2134 || (TYPE_MODE (TREE_TYPE (TREE_OPERAND (bf, 0))) != BLKmode
2135 && (!TREE_SIDE_EFFECTS (TREE_OPERAND (bf, 0))
2136 || (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE
2137 (TREE_OPERAND (bf, 0))))
2138 <= BITS_PER_WORD)))));
2141 /* Create an assignment statement from SRC to DST. */
2143 static gimple_seq
2144 sra_build_assignment (tree dst, tree src)
2146 gimple stmt;
2147 gimple_seq seq = NULL, seq2 = NULL;
2148 /* Turning BIT_FIELD_REFs into bit operations enables other passes
2149 to do a much better job at optimizing the code.
2150 From dst = BIT_FIELD_REF <var, sz, off> we produce
2152 SR.1 = (scalar type) var;
2153 SR.2 = SR.1 >> off;
2154 SR.3 = SR.2 & ((1 << sz) - 1);
2155 ... possible sign extension of SR.3 ...
2156 dst = (destination type) SR.3;
2158 if (scalar_bitfield_p (src))
2160 tree var, shift, width;
2161 tree utype, stype;
2162 bool unsignedp = (INTEGRAL_TYPE_P (TREE_TYPE (src))
2163 ? TYPE_UNSIGNED (TREE_TYPE (src)) : true);
2164 struct gimplify_ctx gctx;
2166 var = TREE_OPERAND (src, 0);
2167 width = TREE_OPERAND (src, 1);
2168 /* The offset needs to be adjusted to a right shift quantity
2169 depending on the endianness. */
2170 if (BYTES_BIG_ENDIAN)
2172 tree tmp = size_binop (PLUS_EXPR, width, TREE_OPERAND (src, 2));
2173 shift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), tmp);
2175 else
2176 shift = TREE_OPERAND (src, 2);
2178 /* In weird cases we have non-integral types for the source or
2179 destination object.
2180 ??? For unknown reasons we also want an unsigned scalar type. */
2181 stype = TREE_TYPE (var);
2182 if (!INTEGRAL_TYPE_P (stype))
2183 stype = lang_hooks.types.type_for_size (TREE_INT_CST_LOW
2184 (TYPE_SIZE (stype)), 1);
2185 else if (!TYPE_UNSIGNED (stype))
2186 stype = unsigned_type_for (stype);
2188 utype = TREE_TYPE (dst);
2189 if (!INTEGRAL_TYPE_P (utype))
2190 utype = lang_hooks.types.type_for_size (TREE_INT_CST_LOW
2191 (TYPE_SIZE (utype)), 1);
2192 else if (!TYPE_UNSIGNED (utype))
2193 utype = unsigned_type_for (utype);
2195 /* Convert the base var of the BIT_FIELD_REF to the scalar type
2196 we use for computation if we cannot use it directly. */
2197 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
2198 var = fold_convert (stype, var);
2199 else
2200 var = fold_build1 (VIEW_CONVERT_EXPR, stype, var);
2202 if (!integer_zerop (shift))
2203 var = fold_build2 (RSHIFT_EXPR, stype, var, shift);
2205 /* If we need a masking operation, produce one. */
2206 if (TREE_INT_CST_LOW (width) == TYPE_PRECISION (stype))
2207 unsignedp = true;
2208 else
2210 tree one = build_int_cst_wide (stype, 1, 0);
2211 tree mask = int_const_binop (LSHIFT_EXPR, one, width, 0);
2212 mask = int_const_binop (MINUS_EXPR, mask, one, 0);
2213 var = fold_build2 (BIT_AND_EXPR, stype, var, mask);
2216 /* After shifting and masking, convert to the target type. */
2217 var = fold_convert (utype, var);
2219 /* Perform sign extension, if required.
2220 ??? This should never be necessary. */
2221 if (!unsignedp)
2223 tree signbit = int_const_binop (LSHIFT_EXPR,
2224 build_int_cst_wide (utype, 1, 0),
2225 size_binop (MINUS_EXPR, width,
2226 bitsize_int (1)), 0);
2228 var = fold_build2 (BIT_XOR_EXPR, utype, var, signbit);
2229 var = fold_build2 (MINUS_EXPR, utype, var, signbit);
2232 /* fold_build3 (BIT_FIELD_REF, ...) sometimes returns a cast. */
2233 STRIP_NOPS (dst);
2235 /* Finally, move and convert to the destination. */
2236 if (INTEGRAL_TYPE_P (TREE_TYPE (dst)))
2237 var = fold_convert (TREE_TYPE (dst), var);
2238 else
2239 var = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (dst), var);
2241 push_gimplify_context (&gctx);
2242 gctx.into_ssa = true;
2243 gctx.allow_rhs_cond_expr = true;
2245 gimplify_assign (dst, var, &seq);
2247 if (gimple_referenced_vars (cfun))
2248 for (var = gctx.temps; var; var = TREE_CHAIN (var))
2249 add_referenced_var (var);
2250 pop_gimplify_context (NULL);
2252 return seq;
2255 /* fold_build3 (BIT_FIELD_REF, ...) sometimes returns a cast. */
2256 if (CONVERT_EXPR_P (dst))
2258 STRIP_NOPS (dst);
2259 src = fold_convert (TREE_TYPE (dst), src);
2261 /* It was hoped that we could perform some type sanity checking
2262 here, but since front-ends can emit accesses of fields in types
2263 different from their nominal types and copy structures containing
2264 them as a whole, we'd have to handle such differences here.
2265 Since such accesses under different types require compatibility
2266 anyway, there's little point in making tests and/or adding
2267 conversions to ensure the types of src and dst are the same.
2268 So we just assume type differences at this point are ok.
2269 The only exception we make here are pointer types, which can be different
2270 in e.g. structurally equal, but non-identical RECORD_TYPEs. */
2271 else if (POINTER_TYPE_P (TREE_TYPE (dst))
2272 && !useless_type_conversion_p (TREE_TYPE (dst), TREE_TYPE (src)))
2273 src = fold_convert (TREE_TYPE (dst), src);
2275 /* ??? Only call the gimplifier if we need to. Otherwise we may
2276 end up substituting with DECL_VALUE_EXPR - see PR37380. */
2277 if (!handled_component_p (src)
2278 && !SSA_VAR_P (src))
2280 src = force_gimple_operand (src, &seq2, false, NULL_TREE);
2281 gimple_seq_add_seq (&seq, seq2);
2283 stmt = gimple_build_assign (dst, src);
2284 gimple_seq_add_stmt (&seq, stmt);
2285 return seq;
2288 /* BIT_FIELD_REFs must not be shared. sra_build_elt_assignment()
2289 takes care of assignments, but we must create copies for uses. */
2290 #define REPLDUP(t) (TREE_CODE (t) != BIT_FIELD_REF ? (t) : unshare_expr (t))
2292 /* Emit an assignment from SRC to DST, but if DST is a scalarizable
2293 BIT_FIELD_REF, turn it into bit operations. */
2295 static gimple_seq
2296 sra_build_bf_assignment (tree dst, tree src)
2298 tree var, type, utype, tmp, tmp2, tmp3;
2299 gimple_seq seq;
2300 gimple stmt;
2301 tree cst, cst2, mask;
2302 tree minshift, maxshift;
2304 if (TREE_CODE (dst) != BIT_FIELD_REF)
2305 return sra_build_assignment (dst, src);
2307 var = TREE_OPERAND (dst, 0);
2309 if (!scalar_bitfield_p (dst))
2310 return sra_build_assignment (REPLDUP (dst), src);
2312 seq = NULL;
2314 cst = fold_convert (bitsizetype, TREE_OPERAND (dst, 2));
2315 cst2 = size_binop (PLUS_EXPR,
2316 fold_convert (bitsizetype, TREE_OPERAND (dst, 1)),
2317 cst);
2319 if (BYTES_BIG_ENDIAN)
2321 maxshift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), cst);
2322 minshift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), cst2);
2324 else
2326 maxshift = cst2;
2327 minshift = cst;
2330 type = TREE_TYPE (var);
2331 if (!INTEGRAL_TYPE_P (type))
2332 type = lang_hooks.types.type_for_size
2333 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (var))), 1);
2334 if (TYPE_UNSIGNED (type))
2335 utype = type;
2336 else
2337 utype = unsigned_type_for (type);
2339 mask = build_int_cst_wide (utype, 1, 0);
2340 if (TREE_INT_CST_LOW (maxshift) == TYPE_PRECISION (utype))
2341 cst = build_int_cst_wide (utype, 0, 0);
2342 else
2343 cst = int_const_binop (LSHIFT_EXPR, mask, maxshift, true);
2344 if (integer_zerop (minshift))
2345 cst2 = mask;
2346 else
2347 cst2 = int_const_binop (LSHIFT_EXPR, mask, minshift, true);
2348 mask = int_const_binop (MINUS_EXPR, cst, cst2, true);
2349 mask = fold_build1 (BIT_NOT_EXPR, utype, mask);
2351 if (TYPE_MAIN_VARIANT (utype) != TYPE_MAIN_VARIANT (TREE_TYPE (var))
2352 && !integer_zerop (mask))
2354 tmp = var;
2355 if (!is_gimple_variable (tmp))
2356 tmp = unshare_expr (var);
2357 else
2358 TREE_NO_WARNING (var) = true;
2360 tmp2 = make_rename_temp (utype, "SR");
2362 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
2363 tmp = fold_convert (utype, tmp);
2364 else
2365 tmp = fold_build1 (VIEW_CONVERT_EXPR, utype, tmp);
2367 stmt = gimple_build_assign (tmp2, tmp);
2368 gimple_seq_add_stmt (&seq, stmt);
2370 else
2371 tmp2 = var;
2373 if (!integer_zerop (mask))
2375 tmp = make_rename_temp (utype, "SR");
2376 stmt = gimple_build_assign (tmp, fold_build2 (BIT_AND_EXPR, utype,
2377 tmp2, mask));
2378 gimple_seq_add_stmt (&seq, stmt);
2380 else
2381 tmp = mask;
2383 if (is_gimple_reg (src) && INTEGRAL_TYPE_P (TREE_TYPE (src)))
2384 tmp2 = src;
2385 else if (INTEGRAL_TYPE_P (TREE_TYPE (src)))
2387 gimple_seq tmp_seq;
2388 tmp2 = make_rename_temp (TREE_TYPE (src), "SR");
2389 tmp_seq = sra_build_assignment (tmp2, src);
2390 gimple_seq_add_seq (&seq, tmp_seq);
2392 else
2394 gimple_seq tmp_seq;
2395 tmp2 = make_rename_temp
2396 (lang_hooks.types.type_for_size
2397 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (src))),
2398 1), "SR");
2399 tmp_seq = sra_build_assignment (tmp2, fold_build1 (VIEW_CONVERT_EXPR,
2400 TREE_TYPE (tmp2), src));
2401 gimple_seq_add_seq (&seq, tmp_seq);
2404 if (!TYPE_UNSIGNED (TREE_TYPE (tmp2)))
2406 gimple_seq tmp_seq;
2407 tree ut = unsigned_type_for (TREE_TYPE (tmp2));
2408 tmp3 = make_rename_temp (ut, "SR");
2409 tmp2 = fold_convert (ut, tmp2);
2410 tmp_seq = sra_build_assignment (tmp3, tmp2);
2411 gimple_seq_add_seq (&seq, tmp_seq);
2413 tmp2 = fold_build1 (BIT_NOT_EXPR, utype, mask);
2414 tmp2 = int_const_binop (RSHIFT_EXPR, tmp2, minshift, true);
2415 tmp2 = fold_convert (ut, tmp2);
2416 tmp2 = fold_build2 (BIT_AND_EXPR, ut, tmp3, tmp2);
2418 if (tmp3 != tmp2)
2420 tmp3 = make_rename_temp (ut, "SR");
2421 tmp_seq = sra_build_assignment (tmp3, tmp2);
2422 gimple_seq_add_seq (&seq, tmp_seq);
2425 tmp2 = tmp3;
2428 if (TYPE_MAIN_VARIANT (TREE_TYPE (tmp2)) != TYPE_MAIN_VARIANT (utype))
2430 gimple_seq tmp_seq;
2431 tmp3 = make_rename_temp (utype, "SR");
2432 tmp2 = fold_convert (utype, tmp2);
2433 tmp_seq = sra_build_assignment (tmp3, tmp2);
2434 gimple_seq_add_seq (&seq, tmp_seq);
2435 tmp2 = tmp3;
2438 if (!integer_zerop (minshift))
2440 tmp3 = make_rename_temp (utype, "SR");
2441 stmt = gimple_build_assign (tmp3, fold_build2 (LSHIFT_EXPR, utype,
2442 tmp2, minshift));
2443 gimple_seq_add_stmt (&seq, stmt);
2444 tmp2 = tmp3;
2447 if (utype != TREE_TYPE (var))
2448 tmp3 = make_rename_temp (utype, "SR");
2449 else
2450 tmp3 = var;
2451 stmt = gimple_build_assign (tmp3, fold_build2 (BIT_IOR_EXPR, utype,
2452 tmp, tmp2));
2453 gimple_seq_add_stmt (&seq, stmt);
2455 if (tmp3 != var)
2457 if (TREE_TYPE (var) == type)
2458 stmt = gimple_build_assign (var, fold_convert (type, tmp3));
2459 else
2460 stmt = gimple_build_assign (var, fold_build1 (VIEW_CONVERT_EXPR,
2461 TREE_TYPE (var), tmp3));
2462 gimple_seq_add_stmt (&seq, stmt);
2465 return seq;
2468 /* Expand an assignment of SRC to the scalarized representation of
2469 ELT. If it is a field group, try to widen the assignment to cover
2470 the full variable. */
2472 static gimple_seq
2473 sra_build_elt_assignment (struct sra_elt *elt, tree src)
2475 tree dst = elt->replacement;
2476 tree var, tmp, cst, cst2;
2477 gimple stmt;
2478 gimple_seq seq;
2480 if (TREE_CODE (dst) != BIT_FIELD_REF
2481 || !elt->in_bitfld_block)
2482 return sra_build_assignment (REPLDUP (dst), src);
2484 var = TREE_OPERAND (dst, 0);
2486 /* Try to widen the assignment to the entire variable.
2487 We need the source to be a BIT_FIELD_REF as well, such that, for
2488 BIT_FIELD_REF<d,sz,dp> = BIT_FIELD_REF<s,sz,sp>,
2489 by design, conditions are met such that we can turn it into
2490 d = BIT_FIELD_REF<s,dw,sp-dp>. */
2491 if (elt->in_bitfld_block == 2
2492 && TREE_CODE (src) == BIT_FIELD_REF)
2494 tmp = src;
2495 cst = TYPE_SIZE (TREE_TYPE (var));
2496 cst2 = size_binop (MINUS_EXPR, TREE_OPERAND (src, 2),
2497 TREE_OPERAND (dst, 2));
2499 src = TREE_OPERAND (src, 0);
2501 /* Avoid full-width bit-fields. */
2502 if (integer_zerop (cst2)
2503 && tree_int_cst_equal (cst, TYPE_SIZE (TREE_TYPE (src))))
2505 if (INTEGRAL_TYPE_P (TREE_TYPE (src))
2506 && !TYPE_UNSIGNED (TREE_TYPE (src)))
2507 src = fold_convert (unsigned_type_for (TREE_TYPE (src)), src);
2509 /* If a single conversion won't do, we'll need a statement
2510 list. */
2511 if (TYPE_MAIN_VARIANT (TREE_TYPE (var))
2512 != TYPE_MAIN_VARIANT (TREE_TYPE (src)))
2514 gimple_seq tmp_seq;
2515 seq = NULL;
2517 if (!INTEGRAL_TYPE_P (TREE_TYPE (src)))
2518 src = fold_build1 (VIEW_CONVERT_EXPR,
2519 lang_hooks.types.type_for_size
2520 (TREE_INT_CST_LOW
2521 (TYPE_SIZE (TREE_TYPE (src))),
2522 1), src);
2523 gcc_assert (TYPE_UNSIGNED (TREE_TYPE (src)));
2525 tmp = make_rename_temp (TREE_TYPE (src), "SR");
2526 stmt = gimple_build_assign (tmp, src);
2527 gimple_seq_add_stmt (&seq, stmt);
2529 tmp_seq = sra_build_assignment (var,
2530 fold_convert (TREE_TYPE (var),
2531 tmp));
2532 gimple_seq_add_seq (&seq, tmp_seq);
2534 return seq;
2537 src = fold_convert (TREE_TYPE (var), src);
2539 else
2541 src = fold_convert (TREE_TYPE (var), tmp);
2544 return sra_build_assignment (var, src);
2547 return sra_build_bf_assignment (dst, src);
2550 /* Generate a set of assignment statements in *LIST_P to copy all
2551 instantiated elements under ELT to or from the equivalent structure
2552 rooted at EXPR. COPY_OUT controls the direction of the copy, with
2553 true meaning to copy out of EXPR into ELT. */
2555 static void
2556 generate_copy_inout (struct sra_elt *elt, bool copy_out, tree expr,
2557 gimple_seq *seq_p)
2559 struct sra_elt *c;
2560 gimple_seq tmp_seq;
2561 tree t;
2563 if (!copy_out && TREE_CODE (expr) == SSA_NAME
2564 && TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2566 tree r, i;
2568 c = lookup_element (elt, integer_zero_node, NULL, NO_INSERT);
2569 r = c->replacement;
2570 c = lookup_element (elt, integer_one_node, NULL, NO_INSERT);
2571 i = c->replacement;
2573 t = build2 (COMPLEX_EXPR, elt->type, r, i);
2574 tmp_seq = sra_build_bf_assignment (expr, t);
2575 SSA_NAME_DEF_STMT (expr) = gimple_seq_last_stmt (tmp_seq);
2576 gimple_seq_add_seq (seq_p, tmp_seq);
2578 else if (elt->replacement)
2580 if (copy_out)
2581 tmp_seq = sra_build_elt_assignment (elt, expr);
2582 else
2583 tmp_seq = sra_build_bf_assignment (expr, REPLDUP (elt->replacement));
2584 gimple_seq_add_seq (seq_p, tmp_seq);
2586 else
2588 FOR_EACH_ACTUAL_CHILD (c, elt)
2590 t = generate_one_element_ref (c, unshare_expr (expr));
2591 generate_copy_inout (c, copy_out, t, seq_p);
2596 /* Generate a set of assignment statements in *LIST_P to copy all instantiated
2597 elements under SRC to their counterparts under DST. There must be a 1-1
2598 correspondence of instantiated elements. */
2600 static void
2601 generate_element_copy (struct sra_elt *dst, struct sra_elt *src, gimple_seq *seq_p)
2603 struct sra_elt *dc, *sc;
2605 FOR_EACH_ACTUAL_CHILD (dc, dst)
2607 sc = lookup_element (src, dc->element, NULL, NO_INSERT);
2608 if (!sc && dc->in_bitfld_block == 2)
2610 struct sra_elt *dcs;
2612 FOR_EACH_ACTUAL_CHILD (dcs, dc)
2614 sc = lookup_element (src, dcs->element, NULL, NO_INSERT);
2615 gcc_assert (sc);
2616 generate_element_copy (dcs, sc, seq_p);
2619 continue;
2622 /* If DST and SRC are structs with the same elements, but do not have
2623 the same TYPE_MAIN_VARIANT, then lookup of DST FIELD_DECL in SRC
2624 will fail. Try harder by finding the corresponding FIELD_DECL
2625 in SRC. */
2626 if (!sc)
2628 tree f;
2630 gcc_assert (useless_type_conversion_p (dst->type, src->type));
2631 gcc_assert (TREE_CODE (dc->element) == FIELD_DECL);
2632 for (f = TYPE_FIELDS (src->type); f ; f = TREE_CHAIN (f))
2633 if (simple_cst_equal (DECL_FIELD_OFFSET (f),
2634 DECL_FIELD_OFFSET (dc->element)) > 0
2635 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (f),
2636 DECL_FIELD_BIT_OFFSET (dc->element)) > 0
2637 && simple_cst_equal (DECL_SIZE (f),
2638 DECL_SIZE (dc->element)) > 0
2639 && (useless_type_conversion_p (TREE_TYPE (dc->element),
2640 TREE_TYPE (f))
2641 || (POINTER_TYPE_P (TREE_TYPE (dc->element))
2642 && POINTER_TYPE_P (TREE_TYPE (f)))))
2643 break;
2644 gcc_assert (f != NULL_TREE);
2645 sc = lookup_element (src, f, NULL, NO_INSERT);
2648 generate_element_copy (dc, sc, seq_p);
2651 if (dst->replacement)
2653 gimple_seq tmp_seq;
2655 gcc_assert (src->replacement);
2657 tmp_seq = sra_build_elt_assignment (dst, REPLDUP (src->replacement));
2658 gimple_seq_add_seq (seq_p, tmp_seq);
2662 /* Generate a set of assignment statements in *LIST_P to zero all instantiated
2663 elements under ELT. In addition, do not assign to elements that have been
2664 marked VISITED but do reset the visited flag; this allows easy coordination
2665 with generate_element_init. */
2667 static void
2668 generate_element_zero (struct sra_elt *elt, gimple_seq *seq_p)
2670 struct sra_elt *c;
2672 if (elt->visited)
2674 elt->visited = false;
2675 return;
2678 if (!elt->in_bitfld_block)
2679 FOR_EACH_ACTUAL_CHILD (c, elt)
2680 generate_element_zero (c, seq_p);
2682 if (elt->replacement)
2684 tree t;
2685 gimple_seq tmp_seq;
2687 gcc_assert (elt->is_scalar);
2688 t = fold_convert (elt->type, integer_zero_node);
2690 tmp_seq = sra_build_elt_assignment (elt, t);
2691 gimple_seq_add_seq (seq_p, tmp_seq);
2695 /* Generate an assignment VAR = INIT, where INIT may need gimplification.
2696 Add the result to *LIST_P. */
2698 static void
2699 generate_one_element_init (struct sra_elt *elt, tree init, gimple_seq *seq_p)
2701 gimple_seq tmp_seq = sra_build_elt_assignment (elt, init);
2702 gimple_seq_add_seq (seq_p, tmp_seq);
2705 /* Generate a set of assignment statements in *LIST_P to set all instantiated
2706 elements under ELT with the contents of the initializer INIT. In addition,
2707 mark all assigned elements VISITED; this allows easy coordination with
2708 generate_element_zero. Return false if we found a case we couldn't
2709 handle. */
2711 static bool
2712 generate_element_init_1 (struct sra_elt *elt, tree init, gimple_seq *seq_p)
2714 bool result = true;
2715 enum tree_code init_code;
2716 struct sra_elt *sub;
2717 tree t;
2718 unsigned HOST_WIDE_INT idx;
2719 tree value, purpose;
2721 /* We can be passed DECL_INITIAL of a static variable. It might have a
2722 conversion, which we strip off here. */
2723 STRIP_USELESS_TYPE_CONVERSION (init);
2724 init_code = TREE_CODE (init);
2726 if (elt->is_scalar)
2728 if (elt->replacement)
2730 generate_one_element_init (elt, init, seq_p);
2731 elt->visited = true;
2733 return result;
2736 switch (init_code)
2738 case COMPLEX_CST:
2739 case COMPLEX_EXPR:
2740 FOR_EACH_ACTUAL_CHILD (sub, elt)
2742 if (sub->element == integer_zero_node)
2743 t = (init_code == COMPLEX_EXPR
2744 ? TREE_OPERAND (init, 0) : TREE_REALPART (init));
2745 else
2746 t = (init_code == COMPLEX_EXPR
2747 ? TREE_OPERAND (init, 1) : TREE_IMAGPART (init));
2748 result &= generate_element_init_1 (sub, t, seq_p);
2750 break;
2752 case CONSTRUCTOR:
2753 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, purpose, value)
2755 /* Array constructors are routinely created with NULL indices. */
2756 if (purpose == NULL_TREE)
2758 result = false;
2759 break;
2761 if (TREE_CODE (purpose) == RANGE_EXPR)
2763 tree lower = TREE_OPERAND (purpose, 0);
2764 tree upper = TREE_OPERAND (purpose, 1);
2766 while (1)
2768 sub = lookup_element (elt, lower, NULL, NO_INSERT);
2769 if (sub != NULL)
2770 result &= generate_element_init_1 (sub, value, seq_p);
2771 if (tree_int_cst_equal (lower, upper))
2772 break;
2773 lower = int_const_binop (PLUS_EXPR, lower,
2774 integer_one_node, true);
2777 else
2779 sub = lookup_element (elt, purpose, NULL, NO_INSERT);
2780 if (sub != NULL)
2781 result &= generate_element_init_1 (sub, value, seq_p);
2784 break;
2786 default:
2787 elt->visited = true;
2788 result = false;
2791 return result;
2794 /* A wrapper function for generate_element_init_1 that handles cleanup after
2795 gimplification. */
2797 static bool
2798 generate_element_init (struct sra_elt *elt, tree init, gimple_seq *seq_p)
2800 bool ret;
2801 struct gimplify_ctx gctx;
2803 push_gimplify_context (&gctx);
2804 ret = generate_element_init_1 (elt, init, seq_p);
2805 pop_gimplify_context (NULL);
2807 /* The replacement can expose previously unreferenced variables. */
2808 if (ret && *seq_p)
2810 gimple_stmt_iterator i;
2812 for (i = gsi_start (*seq_p); !gsi_end_p (i); gsi_next (&i))
2813 find_new_referenced_vars (gsi_stmt (i));
2816 return ret;
2819 /* Insert a gimple_seq SEQ on all the outgoing edges out of BB. Note that
2820 if BB has more than one edge, STMT will be replicated for each edge.
2821 Also, abnormal edges will be ignored. */
2823 void
2824 insert_edge_copies_seq (gimple_seq seq, basic_block bb)
2826 edge e;
2827 edge_iterator ei;
2828 unsigned n_copies = -1;
2830 FOR_EACH_EDGE (e, ei, bb->succs)
2831 if (!(e->flags & EDGE_ABNORMAL))
2832 n_copies++;
2834 FOR_EACH_EDGE (e, ei, bb->succs)
2835 if (!(e->flags & EDGE_ABNORMAL))
2836 gsi_insert_seq_on_edge (e, n_copies-- > 0 ? gimple_seq_copy (seq) : seq);
2839 /* Helper function to insert LIST before GSI, and set up line number info. */
2841 void
2842 sra_insert_before (gimple_stmt_iterator *gsi, gimple_seq seq)
2844 gimple stmt = gsi_stmt (*gsi);
2846 if (gimple_has_location (stmt))
2847 annotate_all_with_location (seq, gimple_location (stmt));
2848 gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
2851 /* Similarly, but insert after GSI. Handles insertion onto edges as well. */
2853 void
2854 sra_insert_after (gimple_stmt_iterator *gsi, gimple_seq seq)
2856 gimple stmt = gsi_stmt (*gsi);
2858 if (gimple_has_location (stmt))
2859 annotate_all_with_location (seq, gimple_location (stmt));
2861 if (stmt_ends_bb_p (stmt))
2862 insert_edge_copies_seq (seq, gsi_bb (*gsi));
2863 else
2864 gsi_insert_seq_after (gsi, seq, GSI_SAME_STMT);
2867 /* Similarly, but replace the statement at GSI. */
2869 static void
2870 sra_replace (gimple_stmt_iterator *gsi, gimple_seq seq)
2872 sra_insert_before (gsi, seq);
2873 gsi_remove (gsi, false);
2874 if (gsi_end_p (*gsi))
2875 *gsi = gsi_last (gsi_seq (*gsi));
2876 else
2877 gsi_prev (gsi);
2880 /* Data structure that bitfield_overlaps_p fills in with information
2881 about the element passed in and how much of it overlaps with the
2882 bit-range passed it to. */
2884 struct bitfield_overlap_info
2886 /* The bit-length of an element. */
2887 tree field_len;
2889 /* The bit-position of the element in its parent. */
2890 tree field_pos;
2892 /* The number of bits of the element that overlap with the incoming
2893 bit range. */
2894 tree overlap_len;
2896 /* The first bit of the element that overlaps with the incoming bit
2897 range. */
2898 tree overlap_pos;
2901 /* Return true if a BIT_FIELD_REF<(FLD->parent), BLEN, BPOS>
2902 expression (referenced as BF below) accesses any of the bits in FLD,
2903 false if it doesn't. If DATA is non-null, its field_len and
2904 field_pos are filled in such that BIT_FIELD_REF<(FLD->parent),
2905 field_len, field_pos> (referenced as BFLD below) represents the
2906 entire field FLD->element, and BIT_FIELD_REF<BFLD, overlap_len,
2907 overlap_pos> represents the portion of the entire field that
2908 overlaps with BF. */
2910 static bool
2911 bitfield_overlaps_p (tree blen, tree bpos, struct sra_elt *fld,
2912 struct bitfield_overlap_info *data)
2914 tree flen, fpos;
2915 bool ret;
2917 if (TREE_CODE (fld->element) == FIELD_DECL)
2919 flen = fold_convert (bitsizetype, DECL_SIZE (fld->element));
2920 fpos = fold_convert (bitsizetype, DECL_FIELD_OFFSET (fld->element));
2921 fpos = size_binop (MULT_EXPR, fpos, bitsize_int (BITS_PER_UNIT));
2922 fpos = size_binop (PLUS_EXPR, fpos, DECL_FIELD_BIT_OFFSET (fld->element));
2924 else if (TREE_CODE (fld->element) == BIT_FIELD_REF)
2926 flen = fold_convert (bitsizetype, TREE_OPERAND (fld->element, 1));
2927 fpos = fold_convert (bitsizetype, TREE_OPERAND (fld->element, 2));
2929 else if (TREE_CODE (fld->element) == INTEGER_CST)
2931 tree domain_type = TYPE_DOMAIN (TREE_TYPE (fld->parent->element));
2932 flen = fold_convert (bitsizetype, TYPE_SIZE (fld->type));
2933 fpos = fold_convert (bitsizetype, fld->element);
2934 if (domain_type && TYPE_MIN_VALUE (domain_type))
2935 fpos = size_binop (MINUS_EXPR, fpos,
2936 fold_convert (bitsizetype,
2937 TYPE_MIN_VALUE (domain_type)));
2938 fpos = size_binop (MULT_EXPR, flen, fpos);
2940 else
2941 gcc_unreachable ();
2943 gcc_assert (host_integerp (blen, 1)
2944 && host_integerp (bpos, 1)
2945 && host_integerp (flen, 1)
2946 && host_integerp (fpos, 1));
2948 ret = ((!tree_int_cst_lt (fpos, bpos)
2949 && tree_int_cst_lt (size_binop (MINUS_EXPR, fpos, bpos),
2950 blen))
2951 || (!tree_int_cst_lt (bpos, fpos)
2952 && tree_int_cst_lt (size_binop (MINUS_EXPR, bpos, fpos),
2953 flen)));
2955 if (!ret)
2956 return ret;
2958 if (data)
2960 tree bend, fend;
2962 data->field_len = flen;
2963 data->field_pos = fpos;
2965 fend = size_binop (PLUS_EXPR, fpos, flen);
2966 bend = size_binop (PLUS_EXPR, bpos, blen);
2968 if (tree_int_cst_lt (bend, fend))
2969 data->overlap_len = size_binop (MINUS_EXPR, bend, fpos);
2970 else
2971 data->overlap_len = NULL;
2973 if (tree_int_cst_lt (fpos, bpos))
2975 data->overlap_pos = size_binop (MINUS_EXPR, bpos, fpos);
2976 data->overlap_len = size_binop (MINUS_EXPR,
2977 data->overlap_len
2978 ? data->overlap_len
2979 : data->field_len,
2980 data->overlap_pos);
2982 else
2983 data->overlap_pos = NULL;
2986 return ret;
2989 /* Add to LISTP a sequence of statements that copies BLEN bits between
2990 VAR and the scalarized elements of ELT, starting a bit VPOS of VAR
2991 and at bit BPOS of ELT. The direction of the copy is given by
2992 TO_VAR. */
2994 static void
2995 sra_explode_bitfield_assignment (tree var, tree vpos, bool to_var,
2996 gimple_seq *seq_p, tree blen, tree bpos,
2997 struct sra_elt *elt)
2999 struct sra_elt *fld;
3000 struct bitfield_overlap_info flp;
3002 FOR_EACH_ACTUAL_CHILD (fld, elt)
3004 tree flen, fpos;
3006 if (!bitfield_overlaps_p (blen, bpos, fld, &flp))
3007 continue;
3009 flen = flp.overlap_len ? flp.overlap_len : flp.field_len;
3010 fpos = flp.overlap_pos ? flp.overlap_pos : bitsize_int (0);
3012 if (fld->replacement)
3014 tree infld, invar, type;
3015 gimple_seq st;
3017 infld = fld->replacement;
3019 type = unsigned_type_for (TREE_TYPE (infld));
3020 if (TYPE_PRECISION (type) != TREE_INT_CST_LOW (flen))
3021 type = build_nonstandard_integer_type (TREE_INT_CST_LOW (flen), 1);
3023 if (TREE_CODE (infld) == BIT_FIELD_REF)
3025 fpos = size_binop (PLUS_EXPR, fpos, TREE_OPERAND (infld, 2));
3026 infld = TREE_OPERAND (infld, 0);
3028 else if (BYTES_BIG_ENDIAN && DECL_P (fld->element)
3029 && !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (infld)),
3030 DECL_SIZE (fld->element)))
3032 fpos = size_binop (PLUS_EXPR, fpos,
3033 TYPE_SIZE (TREE_TYPE (infld)));
3034 fpos = size_binop (MINUS_EXPR, fpos,
3035 DECL_SIZE (fld->element));
3038 infld = fold_build3 (BIT_FIELD_REF, type, infld, flen, fpos);
3040 invar = size_binop (MINUS_EXPR, flp.field_pos, bpos);
3041 if (flp.overlap_pos)
3042 invar = size_binop (PLUS_EXPR, invar, flp.overlap_pos);
3043 invar = size_binop (PLUS_EXPR, invar, vpos);
3045 invar = fold_build3 (BIT_FIELD_REF, type, var, flen, invar);
3047 if (to_var)
3048 st = sra_build_bf_assignment (invar, infld);
3049 else
3050 st = sra_build_bf_assignment (infld, invar);
3052 gimple_seq_add_seq (seq_p, st);
3054 else
3056 tree sub = size_binop (MINUS_EXPR, flp.field_pos, bpos);
3057 sub = size_binop (PLUS_EXPR, vpos, sub);
3058 if (flp.overlap_pos)
3059 sub = size_binop (PLUS_EXPR, sub, flp.overlap_pos);
3061 sra_explode_bitfield_assignment (var, sub, to_var, seq_p,
3062 flen, fpos, fld);
3067 /* Add to LISTBEFOREP statements that copy scalarized members of ELT
3068 that overlap with BIT_FIELD_REF<(ELT->element), BLEN, BPOS> back
3069 into the full variable, and to LISTAFTERP, if non-NULL, statements
3070 that copy the (presumably modified) overlapping portions of the
3071 full variable back to the scalarized variables. */
3073 static void
3074 sra_sync_for_bitfield_assignment (gimple_seq *seq_before_p,
3075 gimple_seq *seq_after_p,
3076 tree blen, tree bpos,
3077 struct sra_elt *elt)
3079 struct sra_elt *fld;
3080 struct bitfield_overlap_info flp;
3082 FOR_EACH_ACTUAL_CHILD (fld, elt)
3083 if (bitfield_overlaps_p (blen, bpos, fld, &flp))
3085 if (fld->replacement || (!flp.overlap_len && !flp.overlap_pos))
3087 generate_copy_inout (fld, false, generate_element_ref (fld),
3088 seq_before_p);
3089 mark_no_warning (fld);
3090 if (seq_after_p)
3091 generate_copy_inout (fld, true, generate_element_ref (fld),
3092 seq_after_p);
3094 else
3096 tree flen = flp.overlap_len ? flp.overlap_len : flp.field_len;
3097 tree fpos = flp.overlap_pos ? flp.overlap_pos : bitsize_int (0);
3099 sra_sync_for_bitfield_assignment (seq_before_p, seq_after_p,
3100 flen, fpos, fld);
3105 /* Scalarize a USE. To recap, this is either a simple reference to ELT,
3106 if elt is scalar, or some occurrence of ELT that requires a complete
3107 aggregate. IS_OUTPUT is true if ELT is being modified. */
3109 static void
3110 scalarize_use (struct sra_elt *elt, tree *expr_p, gimple_stmt_iterator *gsi,
3111 bool is_output, bool use_all)
3113 gimple stmt = gsi_stmt (*gsi);
3114 tree bfexpr;
3116 if (elt->replacement)
3118 tree replacement = elt->replacement;
3120 /* If we have a replacement, then updating the reference is as
3121 simple as modifying the existing statement in place. */
3122 if (is_output
3123 && TREE_CODE (elt->replacement) == BIT_FIELD_REF
3124 && is_gimple_reg (TREE_OPERAND (elt->replacement, 0))
3125 && is_gimple_assign (stmt)
3126 && gimple_assign_lhs_ptr (stmt) == expr_p)
3128 gimple_seq newseq;
3129 /* RHS must be a single operand. */
3130 gcc_assert (gimple_assign_single_p (stmt));
3131 newseq = sra_build_elt_assignment (elt, gimple_assign_rhs1 (stmt));
3132 sra_replace (gsi, newseq);
3133 return;
3135 else if (!is_output
3136 && TREE_CODE (elt->replacement) == BIT_FIELD_REF
3137 && is_gimple_assign (stmt)
3138 && gimple_assign_rhs1_ptr (stmt) == expr_p)
3140 tree tmp = make_rename_temp
3141 (TREE_TYPE (gimple_assign_lhs (stmt)), "SR");
3142 gimple_seq newseq = sra_build_assignment (tmp, REPLDUP (elt->replacement));
3144 sra_insert_before (gsi, newseq);
3145 replacement = tmp;
3147 if (is_output)
3148 mark_all_v_defs_stmt (stmt);
3149 *expr_p = REPLDUP (replacement);
3150 update_stmt (stmt);
3152 else if (use_all && is_output
3153 && is_gimple_assign (stmt)
3154 && TREE_CODE (bfexpr
3155 = gimple_assign_lhs (stmt)) == BIT_FIELD_REF
3156 && &TREE_OPERAND (bfexpr, 0) == expr_p
3157 && INTEGRAL_TYPE_P (TREE_TYPE (bfexpr))
3158 && TREE_CODE (TREE_TYPE (*expr_p)) == RECORD_TYPE)
3160 gimple_seq seq_before = NULL;
3161 gimple_seq seq_after = NULL;
3162 tree blen = fold_convert (bitsizetype, TREE_OPERAND (bfexpr, 1));
3163 tree bpos = fold_convert (bitsizetype, TREE_OPERAND (bfexpr, 2));
3164 bool update = false;
3166 if (!elt->use_block_copy)
3168 tree type = TREE_TYPE (bfexpr);
3169 tree var = make_rename_temp (type, "SR"), tmp, vpos;
3170 gimple st;
3172 gimple_assign_set_lhs (stmt, var);
3173 update = true;
3175 if (!TYPE_UNSIGNED (type))
3177 type = unsigned_type_for (type);
3178 tmp = make_rename_temp (type, "SR");
3179 st = gimple_build_assign (tmp, fold_convert (type, var));
3180 gimple_seq_add_stmt (&seq_after, st);
3181 var = tmp;
3184 /* If VAR is wider than BLEN bits, it is padded at the
3185 most-significant end. We want to set VPOS such that
3186 <BIT_FIELD_REF VAR BLEN VPOS> would refer to the
3187 least-significant BLEN bits of VAR. */
3188 if (BYTES_BIG_ENDIAN)
3189 vpos = size_binop (MINUS_EXPR, TYPE_SIZE (type), blen);
3190 else
3191 vpos = bitsize_int (0);
3192 sra_explode_bitfield_assignment
3193 (var, vpos, false, &seq_after, blen, bpos, elt);
3195 else
3196 sra_sync_for_bitfield_assignment
3197 (&seq_before, &seq_after, blen, bpos, elt);
3199 if (seq_before)
3201 mark_all_v_defs_seq (seq_before);
3202 sra_insert_before (gsi, seq_before);
3204 if (seq_after)
3206 mark_all_v_defs_seq (seq_after);
3207 sra_insert_after (gsi, seq_after);
3210 if (update)
3211 update_stmt (stmt);
3213 else if (use_all && !is_output
3214 && is_gimple_assign (stmt)
3215 && TREE_CODE (bfexpr
3216 = gimple_assign_rhs1 (stmt)) == BIT_FIELD_REF
3217 && &TREE_OPERAND (gimple_assign_rhs1 (stmt), 0) == expr_p
3218 && INTEGRAL_TYPE_P (TREE_TYPE (bfexpr))
3219 && TREE_CODE (TREE_TYPE (*expr_p)) == RECORD_TYPE)
3221 gimple_seq seq = NULL;
3222 tree blen = fold_convert (bitsizetype, TREE_OPERAND (bfexpr, 1));
3223 tree bpos = fold_convert (bitsizetype, TREE_OPERAND (bfexpr, 2));
3224 bool update = false;
3226 if (!elt->use_block_copy)
3228 tree type = TREE_TYPE (bfexpr);
3229 tree var = make_rename_temp (type, "SR"), tmp, vpos;
3230 gimple st = NULL;
3232 gimple_assign_set_rhs1 (stmt, var);
3233 update = true;
3235 if (!TYPE_UNSIGNED (type))
3237 type = unsigned_type_for (type);
3238 tmp = make_rename_temp (type, "SR");
3239 st = gimple_build_assign (var,
3240 fold_convert (TREE_TYPE (var), tmp));
3241 var = tmp;
3244 gimple_seq_add_stmt (&seq,
3245 gimple_build_assign
3246 (var, build_int_cst_wide (type, 0, 0)));
3248 /* If VAR is wider than BLEN bits, it is padded at the
3249 most-significant end. We want to set VPOS such that
3250 <BIT_FIELD_REF VAR BLEN VPOS> would refer to the
3251 least-significant BLEN bits of VAR. */
3252 if (BYTES_BIG_ENDIAN)
3253 vpos = size_binop (MINUS_EXPR, TYPE_SIZE (type), blen);
3254 else
3255 vpos = bitsize_int (0);
3256 sra_explode_bitfield_assignment
3257 (var, vpos, true, &seq, blen, bpos, elt);
3259 if (st)
3260 gimple_seq_add_stmt (&seq, st);
3262 else
3263 sra_sync_for_bitfield_assignment
3264 (&seq, NULL, blen, bpos, elt);
3266 if (seq)
3268 mark_all_v_defs_seq (seq);
3269 sra_insert_before (gsi, seq);
3272 if (update)
3273 update_stmt (stmt);
3275 else
3277 gimple_seq seq = NULL;
3279 /* Otherwise we need some copies. If ELT is being read, then we
3280 want to store all (modified) sub-elements back into the
3281 structure before the reference takes place. If ELT is being
3282 written, then we want to load the changed values back into
3283 our shadow variables. */
3284 /* ??? We don't check modified for reads, we just always write all of
3285 the values. We should be able to record the SSA number of the VOP
3286 for which the values were last read. If that number matches the
3287 SSA number of the VOP in the current statement, then we needn't
3288 emit an assignment. This would also eliminate double writes when
3289 a structure is passed as more than one argument to a function call.
3290 This optimization would be most effective if sra_walk_function
3291 processed the blocks in dominator order. */
3293 generate_copy_inout (elt, is_output, generate_element_ref (elt), &seq);
3294 if (seq == NULL)
3295 return;
3296 mark_all_v_defs_seq (seq);
3297 if (is_output)
3298 sra_insert_after (gsi, seq);
3299 else
3301 sra_insert_before (gsi, seq);
3302 if (use_all)
3303 mark_no_warning (elt);
3308 /* Scalarize a COPY. To recap, this is an assignment statement between
3309 two scalarizable references, LHS_ELT and RHS_ELT. */
3311 static void
3312 scalarize_copy (struct sra_elt *lhs_elt, struct sra_elt *rhs_elt,
3313 gimple_stmt_iterator *gsi)
3315 gimple_seq seq;
3316 gimple stmt;
3318 if (lhs_elt->replacement && rhs_elt->replacement)
3320 /* If we have two scalar operands, modify the existing statement. */
3321 stmt = gsi_stmt (*gsi);
3323 /* See the commentary in sra_walk_function concerning
3324 RETURN_EXPR, and why we should never see one here. */
3325 gcc_assert (is_gimple_assign (stmt));
3326 gcc_assert (gimple_assign_copy_p (stmt));
3329 gimple_assign_set_lhs (stmt, lhs_elt->replacement);
3330 gimple_assign_set_rhs1 (stmt, REPLDUP (rhs_elt->replacement));
3331 update_stmt (stmt);
3333 else if (lhs_elt->use_block_copy || rhs_elt->use_block_copy)
3335 /* If either side requires a block copy, then sync the RHS back
3336 to the original structure, leave the original assignment
3337 statement (which will perform the block copy), then load the
3338 LHS values out of its now-updated original structure. */
3339 /* ??? Could perform a modified pair-wise element copy. That
3340 would at least allow those elements that are instantiated in
3341 both structures to be optimized well. */
3343 seq = NULL;
3344 generate_copy_inout (rhs_elt, false,
3345 generate_element_ref (rhs_elt), &seq);
3346 if (seq)
3348 mark_all_v_defs_seq (seq);
3349 sra_insert_before (gsi, seq);
3352 seq = NULL;
3353 generate_copy_inout (lhs_elt, true,
3354 generate_element_ref (lhs_elt), &seq);
3355 if (seq)
3357 mark_all_v_defs_seq (seq);
3358 sra_insert_after (gsi, seq);
3361 else
3363 /* Otherwise both sides must be fully instantiated. In which
3364 case perform pair-wise element assignments and replace the
3365 original block copy statement. */
3367 stmt = gsi_stmt (*gsi);
3368 mark_all_v_defs_stmt (stmt);
3370 seq = NULL;
3371 generate_element_copy (lhs_elt, rhs_elt, &seq);
3372 gcc_assert (seq);
3373 mark_all_v_defs_seq (seq);
3374 sra_replace (gsi, seq);
3378 /* Scalarize an INIT. To recap, this is an assignment to a scalarizable
3379 reference from some form of constructor: CONSTRUCTOR, COMPLEX_CST or
3380 COMPLEX_EXPR. If RHS is NULL, it should be treated as an empty
3381 CONSTRUCTOR. */
3383 static void
3384 scalarize_init (struct sra_elt *lhs_elt, tree rhs, gimple_stmt_iterator *gsi)
3386 bool result = true;
3387 gimple_seq seq = NULL, init_seq = NULL;
3389 /* Generate initialization statements for all members extant in the RHS. */
3390 if (rhs)
3392 /* Unshare the expression just in case this is from a decl's initial. */
3393 rhs = unshare_expr (rhs);
3394 result = generate_element_init (lhs_elt, rhs, &init_seq);
3397 if (!result)
3399 /* If we failed to convert the entire initializer, then we must
3400 leave the structure assignment in place and must load values
3401 from the structure into the slots for which we did not find
3402 constants. The easiest way to do this is to generate a complete
3403 copy-out, and then follow that with the constant assignments
3404 that we were able to build. DCE will clean things up. */
3405 gimple_seq seq0 = NULL;
3406 generate_copy_inout (lhs_elt, true, generate_element_ref (lhs_elt),
3407 &seq0);
3408 gimple_seq_add_seq (&seq0, seq);
3409 seq = seq0;
3411 else
3413 /* CONSTRUCTOR is defined such that any member not mentioned is assigned
3414 a zero value. Initialize the rest of the instantiated elements. */
3415 generate_element_zero (lhs_elt, &seq);
3416 gimple_seq_add_seq (&seq, init_seq);
3419 if (lhs_elt->use_block_copy || !result)
3421 /* Since LHS is not fully instantiated, we must leave the structure
3422 assignment in place. Treating this case differently from a USE
3423 exposes constants to later optimizations. */
3424 if (seq)
3426 mark_all_v_defs_seq (seq);
3427 sra_insert_after (gsi, seq);
3430 else
3432 /* The LHS is fully instantiated. The list of initializations
3433 replaces the original structure assignment. */
3434 gcc_assert (seq);
3435 mark_all_v_defs_stmt (gsi_stmt (*gsi));
3436 mark_all_v_defs_seq (seq);
3437 sra_replace (gsi, seq);
3441 /* A subroutine of scalarize_ldst called via walk_tree. Set TREE_NO_TRAP
3442 on all INDIRECT_REFs. */
3444 static tree
3445 mark_notrap (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3447 tree t = *tp;
3449 if (TREE_CODE (t) == INDIRECT_REF)
3451 TREE_THIS_NOTRAP (t) = 1;
3452 *walk_subtrees = 0;
3454 else if (IS_TYPE_OR_DECL_P (t))
3455 *walk_subtrees = 0;
3457 return NULL;
3460 /* Scalarize a LDST. To recap, this is an assignment between one scalarizable
3461 reference ELT and one non-scalarizable reference OTHER. IS_OUTPUT is true
3462 if ELT is on the left-hand side. */
3464 static void
3465 scalarize_ldst (struct sra_elt *elt, tree other,
3466 gimple_stmt_iterator *gsi, bool is_output)
3468 /* Shouldn't have gotten called for a scalar. */
3469 gcc_assert (!elt->replacement);
3471 if (elt->use_block_copy)
3473 /* Since ELT is not fully instantiated, we have to leave the
3474 block copy in place. Treat this as a USE. */
3475 scalarize_use (elt, NULL, gsi, is_output, false);
3477 else
3479 /* The interesting case is when ELT is fully instantiated. In this
3480 case we can have each element stored/loaded directly to/from the
3481 corresponding slot in OTHER. This avoids a block copy. */
3483 gimple_seq seq = NULL;
3484 gimple stmt = gsi_stmt (*gsi);
3486 mark_all_v_defs_stmt (stmt);
3487 generate_copy_inout (elt, is_output, other, &seq);
3488 gcc_assert (seq);
3489 mark_all_v_defs_seq (seq);
3491 /* Preserve EH semantics. */
3492 if (stmt_ends_bb_p (stmt))
3494 gimple_stmt_iterator si;
3495 gimple first;
3496 gimple_seq blist = NULL;
3497 bool thr = stmt_could_throw_p (stmt);
3499 /* If the last statement of this BB created an EH edge
3500 before scalarization, we have to locate the first
3501 statement that can throw in the new statement list and
3502 use that as the last statement of this BB, such that EH
3503 semantics is preserved. All statements up to this one
3504 are added to the same BB. All other statements in the
3505 list will be added to normal outgoing edges of the same
3506 BB. If they access any memory, it's the same memory, so
3507 we can assume they won't throw. */
3508 si = gsi_start (seq);
3509 for (first = gsi_stmt (si);
3510 thr && !gsi_end_p (si) && !stmt_could_throw_p (first);
3511 first = gsi_stmt (si))
3513 gsi_remove (&si, false);
3514 gimple_seq_add_stmt (&blist, first);
3517 /* Extract the first remaining statement from LIST, this is
3518 the EH statement if there is one. */
3519 gsi_remove (&si, false);
3521 if (blist)
3522 sra_insert_before (gsi, blist);
3524 /* Replace the old statement with this new representative. */
3525 gsi_replace (gsi, first, true);
3527 if (!gsi_end_p (si))
3529 /* If any reference would trap, then they all would. And more
3530 to the point, the first would. Therefore none of the rest
3531 will trap since the first didn't. Indicate this by
3532 iterating over the remaining statements and set
3533 TREE_THIS_NOTRAP in all INDIRECT_REFs. */
3536 walk_gimple_stmt (&si, NULL, mark_notrap, NULL);
3537 gsi_next (&si);
3539 while (!gsi_end_p (si));
3541 insert_edge_copies_seq (seq, gsi_bb (*gsi));
3544 else
3545 sra_replace (gsi, seq);
3549 /* Generate initializations for all scalarizable parameters. */
3551 static void
3552 scalarize_parms (void)
3554 gimple_seq seq = NULL;
3555 unsigned i;
3556 bitmap_iterator bi;
3558 EXECUTE_IF_SET_IN_BITMAP (needs_copy_in, 0, i, bi)
3560 tree var = referenced_var (i);
3561 struct sra_elt *elt = lookup_element (NULL, var, NULL, NO_INSERT);
3562 generate_copy_inout (elt, true, var, &seq);
3565 if (seq)
3567 insert_edge_copies_seq (seq, ENTRY_BLOCK_PTR);
3568 mark_all_v_defs_seq (seq);
3572 /* Entry point to phase 4. Update the function to match replacements. */
3574 static void
3575 scalarize_function (void)
3577 static const struct sra_walk_fns fns = {
3578 scalarize_use, scalarize_copy, scalarize_init, scalarize_ldst, false
3581 sra_walk_function (&fns);
3582 scalarize_parms ();
3583 gsi_commit_edge_inserts ();
3587 /* Debug helper function. Print ELT in a nice human-readable format. */
3589 static void
3590 dump_sra_elt_name (FILE *f, struct sra_elt *elt)
3592 if (elt->parent && TREE_CODE (elt->parent->type) == COMPLEX_TYPE)
3594 fputs (elt->element == integer_zero_node ? "__real__ " : "__imag__ ", f);
3595 dump_sra_elt_name (f, elt->parent);
3597 else
3599 if (elt->parent)
3600 dump_sra_elt_name (f, elt->parent);
3601 if (DECL_P (elt->element))
3603 if (TREE_CODE (elt->element) == FIELD_DECL)
3604 fputc ('.', f);
3605 print_generic_expr (f, elt->element, dump_flags);
3607 else if (TREE_CODE (elt->element) == BIT_FIELD_REF)
3608 fprintf (f, "$B" HOST_WIDE_INT_PRINT_DEC "F" HOST_WIDE_INT_PRINT_DEC,
3609 tree_low_cst (TREE_OPERAND (elt->element, 2), 1),
3610 tree_low_cst (TREE_OPERAND (elt->element, 1), 1));
3611 else if (TREE_CODE (elt->element) == RANGE_EXPR)
3612 fprintf (f, "["HOST_WIDE_INT_PRINT_DEC".."HOST_WIDE_INT_PRINT_DEC"]",
3613 TREE_INT_CST_LOW (TREE_OPERAND (elt->element, 0)),
3614 TREE_INT_CST_LOW (TREE_OPERAND (elt->element, 1)));
3615 else
3616 fprintf (f, "[" HOST_WIDE_INT_PRINT_DEC "]",
3617 TREE_INT_CST_LOW (elt->element));
3621 /* Likewise, but callable from the debugger. */
3623 void
3624 debug_sra_elt_name (struct sra_elt *elt)
3626 dump_sra_elt_name (stderr, elt);
3627 fputc ('\n', stderr);
3630 void
3631 sra_init_cache (void)
3633 if (sra_type_decomp_cache)
3634 return;
3636 sra_type_decomp_cache = BITMAP_ALLOC (NULL);
3637 sra_type_inst_cache = BITMAP_ALLOC (NULL);
3641 /* Main entry point. */
3643 static unsigned int
3644 tree_sra (void)
3646 /* Initialize local variables. */
3647 todoflags = 0;
3648 gcc_obstack_init (&sra_obstack);
3649 sra_candidates = BITMAP_ALLOC (NULL);
3650 needs_copy_in = BITMAP_ALLOC (NULL);
3651 sra_init_cache ();
3652 sra_map = htab_create (101, sra_elt_hash, sra_elt_eq, NULL);
3654 /* Scan. If we find anything, instantiate and scalarize. */
3655 if (find_candidates_for_sra ())
3657 scan_function ();
3658 decide_instantiations ();
3659 scalarize_function ();
3660 if (!bitmap_empty_p (sra_candidates))
3661 todoflags |= TODO_rebuild_alias;
3664 /* Free allocated memory. */
3665 htab_delete (sra_map);
3666 sra_map = NULL;
3667 BITMAP_FREE (sra_candidates);
3668 BITMAP_FREE (needs_copy_in);
3669 BITMAP_FREE (sra_type_decomp_cache);
3670 BITMAP_FREE (sra_type_inst_cache);
3671 obstack_free (&sra_obstack, NULL);
3672 return todoflags;
3675 static unsigned int
3676 tree_sra_early (void)
3678 unsigned int ret;
3680 early_sra = true;
3681 ret = tree_sra ();
3682 early_sra = false;
3684 return ret & ~TODO_rebuild_alias;
3687 static bool
3688 gate_sra (void)
3690 return flag_tree_sra != 0;
3693 struct gimple_opt_pass pass_sra_early =
3696 GIMPLE_PASS,
3697 "esra", /* name */
3698 gate_sra, /* gate */
3699 tree_sra_early, /* execute */
3700 NULL, /* sub */
3701 NULL, /* next */
3702 0, /* static_pass_number */
3703 TV_TREE_SRA, /* tv_id */
3704 PROP_cfg | PROP_ssa, /* properties_required */
3705 0, /* properties_provided */
3706 0, /* properties_destroyed */
3707 0, /* todo_flags_start */
3708 TODO_dump_func
3709 | TODO_update_ssa
3710 | TODO_ggc_collect
3711 | TODO_verify_ssa /* todo_flags_finish */
3715 struct gimple_opt_pass pass_sra =
3718 GIMPLE_PASS,
3719 "sra", /* name */
3720 gate_sra, /* gate */
3721 tree_sra, /* execute */
3722 NULL, /* sub */
3723 NULL, /* next */
3724 0, /* static_pass_number */
3725 TV_TREE_SRA, /* tv_id */
3726 PROP_cfg | PROP_ssa, /* properties_required */
3727 0, /* properties_provided */
3728 0, /* properties_destroyed */
3729 0, /* todo_flags_start */
3730 TODO_dump_func
3731 | TODO_update_ssa
3732 | TODO_ggc_collect
3733 | TODO_verify_ssa /* todo_flags_finish */