2008-06-04 Xinliang David Li <davidxl@google.com>
[official-gcc.git] / gcc / tree-sra.c
blob244219ffe79175d07ef0821c945fb623c47de703
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 "tree-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 tree sra_build_assignment (tree dst, tree src);
212 static void mark_all_v_defs (tree list);
215 /* Return true if DECL is an SRA candidate. */
217 static bool
218 is_sra_candidate_decl (tree decl)
220 return DECL_P (decl) && bitmap_bit_p (sra_candidates, DECL_UID (decl));
223 /* Return true if TYPE is a scalar type. */
225 static bool
226 is_sra_scalar_type (tree type)
228 enum tree_code code = TREE_CODE (type);
229 return (code == INTEGER_TYPE || code == REAL_TYPE || code == VECTOR_TYPE
230 || code == FIXED_POINT_TYPE
231 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
232 || code == POINTER_TYPE || code == OFFSET_TYPE
233 || code == REFERENCE_TYPE);
236 /* Return true if TYPE can be decomposed into a set of independent variables.
238 Note that this doesn't imply that all elements of TYPE can be
239 instantiated, just that if we decide to break up the type into
240 separate pieces that it can be done. */
242 bool
243 sra_type_can_be_decomposed_p (tree type)
245 unsigned int cache = TYPE_UID (TYPE_MAIN_VARIANT (type)) * 2;
246 tree t;
248 /* Avoid searching the same type twice. */
249 if (bitmap_bit_p (sra_type_decomp_cache, cache+0))
250 return true;
251 if (bitmap_bit_p (sra_type_decomp_cache, cache+1))
252 return false;
254 /* The type must have a definite nonzero size. */
255 if (TYPE_SIZE (type) == NULL || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
256 || integer_zerop (TYPE_SIZE (type)))
257 goto fail;
259 /* The type must be a non-union aggregate. */
260 switch (TREE_CODE (type))
262 case RECORD_TYPE:
264 bool saw_one_field = false;
266 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
267 if (TREE_CODE (t) == FIELD_DECL)
269 /* Reject incorrectly represented bit fields. */
270 if (DECL_BIT_FIELD (t)
271 && INTEGRAL_TYPE_P (TREE_TYPE (t))
272 && (tree_low_cst (DECL_SIZE (t), 1)
273 != TYPE_PRECISION (TREE_TYPE (t))))
274 goto fail;
276 saw_one_field = true;
279 /* Record types must have at least one field. */
280 if (!saw_one_field)
281 goto fail;
283 break;
285 case ARRAY_TYPE:
286 /* Array types must have a fixed lower and upper bound. */
287 t = TYPE_DOMAIN (type);
288 if (t == NULL)
289 goto fail;
290 if (TYPE_MIN_VALUE (t) == NULL || !TREE_CONSTANT (TYPE_MIN_VALUE (t)))
291 goto fail;
292 if (TYPE_MAX_VALUE (t) == NULL || !TREE_CONSTANT (TYPE_MAX_VALUE (t)))
293 goto fail;
294 break;
296 case COMPLEX_TYPE:
297 break;
299 default:
300 goto fail;
303 bitmap_set_bit (sra_type_decomp_cache, cache+0);
304 return true;
306 fail:
307 bitmap_set_bit (sra_type_decomp_cache, cache+1);
308 return false;
311 /* Return true if DECL can be decomposed into a set of independent
312 (though not necessarily scalar) variables. */
314 static bool
315 decl_can_be_decomposed_p (tree var)
317 /* Early out for scalars. */
318 if (is_sra_scalar_type (TREE_TYPE (var)))
319 return false;
321 /* The variable must not be aliased. */
322 if (!is_gimple_non_addressable (var))
324 if (dump_file && (dump_flags & TDF_DETAILS))
326 fprintf (dump_file, "Cannot scalarize variable ");
327 print_generic_expr (dump_file, var, dump_flags);
328 fprintf (dump_file, " because it must live in memory\n");
330 return false;
333 /* The variable must not be volatile. */
334 if (TREE_THIS_VOLATILE (var))
336 if (dump_file && (dump_flags & TDF_DETAILS))
338 fprintf (dump_file, "Cannot scalarize variable ");
339 print_generic_expr (dump_file, var, dump_flags);
340 fprintf (dump_file, " because it is declared volatile\n");
342 return false;
345 /* We must be able to decompose the variable's type. */
346 if (!sra_type_can_be_decomposed_p (TREE_TYPE (var)))
348 if (dump_file && (dump_flags & TDF_DETAILS))
350 fprintf (dump_file, "Cannot scalarize variable ");
351 print_generic_expr (dump_file, var, dump_flags);
352 fprintf (dump_file, " because its type cannot be decomposed\n");
354 return false;
357 /* HACK: if we decompose a va_list_type_node before inlining, then we'll
358 confuse tree-stdarg.c, and we won't be able to figure out which and
359 how many arguments are accessed. This really should be improved in
360 tree-stdarg.c, as the decomposition is truely a win. This could also
361 be fixed if the stdarg pass ran early, but this can't be done until
362 we've aliasing information early too. See PR 30791. */
363 if (early_sra
364 && TYPE_MAIN_VARIANT (TREE_TYPE (var))
365 == TYPE_MAIN_VARIANT (va_list_type_node))
366 return false;
368 return true;
371 /* Return true if TYPE can be *completely* decomposed into scalars. */
373 static bool
374 type_can_instantiate_all_elements (tree type)
376 if (is_sra_scalar_type (type))
377 return true;
378 if (!sra_type_can_be_decomposed_p (type))
379 return false;
381 switch (TREE_CODE (type))
383 case RECORD_TYPE:
385 unsigned int cache = TYPE_UID (TYPE_MAIN_VARIANT (type)) * 2;
386 tree f;
388 if (bitmap_bit_p (sra_type_inst_cache, cache+0))
389 return true;
390 if (bitmap_bit_p (sra_type_inst_cache, cache+1))
391 return false;
393 for (f = TYPE_FIELDS (type); f ; f = TREE_CHAIN (f))
394 if (TREE_CODE (f) == FIELD_DECL)
396 if (!type_can_instantiate_all_elements (TREE_TYPE (f)))
398 bitmap_set_bit (sra_type_inst_cache, cache+1);
399 return false;
403 bitmap_set_bit (sra_type_inst_cache, cache+0);
404 return true;
407 case ARRAY_TYPE:
408 return type_can_instantiate_all_elements (TREE_TYPE (type));
410 case COMPLEX_TYPE:
411 return true;
413 default:
414 gcc_unreachable ();
418 /* Test whether ELT or some sub-element cannot be scalarized. */
420 static bool
421 can_completely_scalarize_p (struct sra_elt *elt)
423 struct sra_elt *c;
425 if (elt->cannot_scalarize)
426 return false;
428 for (c = elt->children; c; c = c->sibling)
429 if (!can_completely_scalarize_p (c))
430 return false;
432 for (c = elt->groups; c; c = c->sibling)
433 if (!can_completely_scalarize_p (c))
434 return false;
436 return true;
440 /* A simplified tree hashing algorithm that only handles the types of
441 trees we expect to find in sra_elt->element. */
443 static hashval_t
444 sra_hash_tree (tree t)
446 hashval_t h;
448 switch (TREE_CODE (t))
450 case VAR_DECL:
451 case PARM_DECL:
452 case RESULT_DECL:
453 h = DECL_UID (t);
454 break;
456 case INTEGER_CST:
457 h = TREE_INT_CST_LOW (t) ^ TREE_INT_CST_HIGH (t);
458 break;
460 case RANGE_EXPR:
461 h = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
462 h = iterative_hash_expr (TREE_OPERAND (t, 1), h);
463 break;
465 case FIELD_DECL:
466 /* We can have types that are compatible, but have different member
467 lists, so we can't hash fields by ID. Use offsets instead. */
468 h = iterative_hash_expr (DECL_FIELD_OFFSET (t), 0);
469 h = iterative_hash_expr (DECL_FIELD_BIT_OFFSET (t), h);
470 break;
472 case BIT_FIELD_REF:
473 /* Don't take operand 0 into account, that's our parent. */
474 h = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
475 h = iterative_hash_expr (TREE_OPERAND (t, 2), h);
476 break;
478 default:
479 gcc_unreachable ();
482 return h;
485 /* Hash function for type SRA_PAIR. */
487 static hashval_t
488 sra_elt_hash (const void *x)
490 const struct sra_elt *e = x;
491 const struct sra_elt *p;
492 hashval_t h;
494 h = sra_hash_tree (e->element);
496 /* Take into account everything except bitfield blocks back up the
497 chain. Given that chain lengths are rarely very long, this
498 should be acceptable. If we truly identify this as a performance
499 problem, it should work to hash the pointer value
500 "e->parent". */
501 for (p = e->parent; p ; p = p->parent)
502 if (!p->in_bitfld_block)
503 h = (h * 65521) ^ sra_hash_tree (p->element);
505 return h;
508 /* Equality function for type SRA_PAIR. */
510 static int
511 sra_elt_eq (const void *x, const void *y)
513 const struct sra_elt *a = x;
514 const struct sra_elt *b = y;
515 tree ae, be;
516 const struct sra_elt *ap = a->parent;
517 const struct sra_elt *bp = b->parent;
519 if (ap)
520 while (ap->in_bitfld_block)
521 ap = ap->parent;
522 if (bp)
523 while (bp->in_bitfld_block)
524 bp = bp->parent;
526 if (ap != bp)
527 return false;
529 ae = a->element;
530 be = b->element;
532 if (ae == be)
533 return true;
534 if (TREE_CODE (ae) != TREE_CODE (be))
535 return false;
537 switch (TREE_CODE (ae))
539 case VAR_DECL:
540 case PARM_DECL:
541 case RESULT_DECL:
542 /* These are all pointer unique. */
543 return false;
545 case INTEGER_CST:
546 /* Integers are not pointer unique, so compare their values. */
547 return tree_int_cst_equal (ae, be);
549 case RANGE_EXPR:
550 return
551 tree_int_cst_equal (TREE_OPERAND (ae, 0), TREE_OPERAND (be, 0))
552 && tree_int_cst_equal (TREE_OPERAND (ae, 1), TREE_OPERAND (be, 1));
554 case FIELD_DECL:
555 /* Fields are unique within a record, but not between
556 compatible records. */
557 if (DECL_FIELD_CONTEXT (ae) == DECL_FIELD_CONTEXT (be))
558 return false;
559 return fields_compatible_p (ae, be);
561 case BIT_FIELD_REF:
562 return
563 tree_int_cst_equal (TREE_OPERAND (ae, 1), TREE_OPERAND (be, 1))
564 && tree_int_cst_equal (TREE_OPERAND (ae, 2), TREE_OPERAND (be, 2));
566 default:
567 gcc_unreachable ();
571 /* Create or return the SRA_ELT structure for CHILD in PARENT. PARENT
572 may be null, in which case CHILD must be a DECL. */
574 static struct sra_elt *
575 lookup_element (struct sra_elt *parent, tree child, tree type,
576 enum insert_option insert)
578 struct sra_elt dummy;
579 struct sra_elt **slot;
580 struct sra_elt *elt;
582 if (parent)
583 dummy.parent = parent->is_group ? parent->parent : parent;
584 else
585 dummy.parent = NULL;
586 dummy.element = child;
588 slot = (struct sra_elt **) htab_find_slot (sra_map, &dummy, insert);
589 if (!slot && insert == NO_INSERT)
590 return NULL;
592 elt = *slot;
593 if (!elt && insert == INSERT)
595 *slot = elt = obstack_alloc (&sra_obstack, sizeof (*elt));
596 memset (elt, 0, sizeof (*elt));
598 elt->parent = parent;
599 elt->element = child;
600 elt->type = type;
601 elt->is_scalar = is_sra_scalar_type (type);
603 if (parent)
605 if (IS_ELEMENT_FOR_GROUP (elt->element))
607 elt->is_group = true;
608 elt->sibling = parent->groups;
609 parent->groups = elt;
611 else
613 elt->sibling = parent->children;
614 parent->children = elt;
618 /* If this is a parameter, then if we want to scalarize, we have
619 one copy from the true function parameter. Count it now. */
620 if (TREE_CODE (child) == PARM_DECL)
622 elt->n_copies = 1;
623 bitmap_set_bit (needs_copy_in, DECL_UID (child));
627 return elt;
630 /* Create or return the SRA_ELT structure for EXPR if the expression
631 refers to a scalarizable variable. */
633 static struct sra_elt *
634 maybe_lookup_element_for_expr (tree expr)
636 struct sra_elt *elt;
637 tree child;
639 switch (TREE_CODE (expr))
641 case VAR_DECL:
642 case PARM_DECL:
643 case RESULT_DECL:
644 if (is_sra_candidate_decl (expr))
645 return lookup_element (NULL, expr, TREE_TYPE (expr), INSERT);
646 return NULL;
648 case ARRAY_REF:
649 /* We can't scalarize variable array indices. */
650 if (in_array_bounds_p (expr))
651 child = TREE_OPERAND (expr, 1);
652 else
653 return NULL;
654 break;
656 case ARRAY_RANGE_REF:
657 /* We can't scalarize variable array indices. */
658 if (range_in_array_bounds_p (expr))
660 tree domain = TYPE_DOMAIN (TREE_TYPE (expr));
661 child = build2 (RANGE_EXPR, integer_type_node,
662 TYPE_MIN_VALUE (domain), TYPE_MAX_VALUE (domain));
664 else
665 return NULL;
666 break;
668 case COMPONENT_REF:
670 tree type = TREE_TYPE (TREE_OPERAND (expr, 0));
671 /* Don't look through unions. */
672 if (TREE_CODE (type) != RECORD_TYPE)
673 return NULL;
674 /* Neither through variable-sized records. */
675 if (TYPE_SIZE (type) == NULL_TREE
676 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
677 return NULL;
678 child = TREE_OPERAND (expr, 1);
680 break;
682 case REALPART_EXPR:
683 child = integer_zero_node;
684 break;
685 case IMAGPART_EXPR:
686 child = integer_one_node;
687 break;
689 default:
690 return NULL;
693 elt = maybe_lookup_element_for_expr (TREE_OPERAND (expr, 0));
694 if (elt)
695 return lookup_element (elt, child, TREE_TYPE (expr), INSERT);
696 return NULL;
700 /* Functions to walk just enough of the tree to see all scalarizable
701 references, and categorize them. */
703 /* A set of callbacks for phases 2 and 4. They'll be invoked for the
704 various kinds of references seen. In all cases, *BSI is an iterator
705 pointing to the statement being processed. */
706 struct sra_walk_fns
708 /* Invoked when ELT is required as a unit. Note that ELT might refer to
709 a leaf node, in which case this is a simple scalar reference. *EXPR_P
710 points to the location of the expression. IS_OUTPUT is true if this
711 is a left-hand-side reference. USE_ALL is true if we saw something we
712 couldn't quite identify and had to force the use of the entire object. */
713 void (*use) (struct sra_elt *elt, tree *expr_p,
714 block_stmt_iterator *bsi, bool is_output, bool use_all);
716 /* Invoked when we have a copy between two scalarizable references. */
717 void (*copy) (struct sra_elt *lhs_elt, struct sra_elt *rhs_elt,
718 block_stmt_iterator *bsi);
720 /* Invoked when ELT is initialized from a constant. VALUE may be NULL,
721 in which case it should be treated as an empty CONSTRUCTOR. */
722 void (*init) (struct sra_elt *elt, tree value, block_stmt_iterator *bsi);
724 /* Invoked when we have a copy between one scalarizable reference ELT
725 and one non-scalarizable reference OTHER without side-effects.
726 IS_OUTPUT is true if ELT is on the left-hand side. */
727 void (*ldst) (struct sra_elt *elt, tree other,
728 block_stmt_iterator *bsi, bool is_output);
730 /* True during phase 2, false during phase 4. */
731 /* ??? This is a hack. */
732 bool initial_scan;
735 #ifdef ENABLE_CHECKING
736 /* Invoked via walk_tree, if *TP contains a candidate decl, return it. */
738 static tree
739 sra_find_candidate_decl (tree *tp, int *walk_subtrees,
740 void *data ATTRIBUTE_UNUSED)
742 tree t = *tp;
743 enum tree_code code = TREE_CODE (t);
745 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
747 *walk_subtrees = 0;
748 if (is_sra_candidate_decl (t))
749 return t;
751 else if (TYPE_P (t))
752 *walk_subtrees = 0;
754 return NULL;
756 #endif
758 /* Walk most expressions looking for a scalarizable aggregate.
759 If we find one, invoke FNS->USE. */
761 static void
762 sra_walk_expr (tree *expr_p, block_stmt_iterator *bsi, bool is_output,
763 const struct sra_walk_fns *fns)
765 tree expr = *expr_p;
766 tree inner = expr;
767 bool disable_scalarization = false;
768 bool use_all_p = false;
770 /* We're looking to collect a reference expression between EXPR and INNER,
771 such that INNER is a scalarizable decl and all other nodes through EXPR
772 are references that we can scalarize. If we come across something that
773 we can't scalarize, we reset EXPR. This has the effect of making it
774 appear that we're referring to the larger expression as a whole. */
776 while (1)
777 switch (TREE_CODE (inner))
779 case VAR_DECL:
780 case PARM_DECL:
781 case RESULT_DECL:
782 /* If there is a scalarizable decl at the bottom, then process it. */
783 if (is_sra_candidate_decl (inner))
785 struct sra_elt *elt = maybe_lookup_element_for_expr (expr);
786 if (disable_scalarization)
787 elt->cannot_scalarize = true;
788 else
789 fns->use (elt, expr_p, bsi, is_output, use_all_p);
791 return;
793 case ARRAY_REF:
794 /* Non-constant index means any member may be accessed. Prevent the
795 expression from being scalarized. If we were to treat this as a
796 reference to the whole array, we can wind up with a single dynamic
797 index reference inside a loop being overridden by several constant
798 index references during loop setup. It's possible that this could
799 be avoided by using dynamic usage counts based on BB trip counts
800 (based on loop analysis or profiling), but that hardly seems worth
801 the effort. */
802 /* ??? Hack. Figure out how to push this into the scan routines
803 without duplicating too much code. */
804 if (!in_array_bounds_p (inner))
806 disable_scalarization = true;
807 goto use_all;
809 /* ??? Are we assured that non-constant bounds and stride will have
810 the same value everywhere? I don't think Fortran will... */
811 if (TREE_OPERAND (inner, 2) || TREE_OPERAND (inner, 3))
812 goto use_all;
813 inner = TREE_OPERAND (inner, 0);
814 break;
816 case ARRAY_RANGE_REF:
817 if (!range_in_array_bounds_p (inner))
819 disable_scalarization = true;
820 goto use_all;
822 /* ??? See above non-constant bounds and stride . */
823 if (TREE_OPERAND (inner, 2) || TREE_OPERAND (inner, 3))
824 goto use_all;
825 inner = TREE_OPERAND (inner, 0);
826 break;
828 case COMPONENT_REF:
830 tree type = TREE_TYPE (TREE_OPERAND (inner, 0));
831 /* Don't look through unions. */
832 if (TREE_CODE (type) != RECORD_TYPE)
833 goto use_all;
834 /* Neither through variable-sized records. */
835 if (TYPE_SIZE (type) == NULL_TREE
836 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
837 goto use_all;
838 inner = TREE_OPERAND (inner, 0);
840 break;
842 case REALPART_EXPR:
843 case IMAGPART_EXPR:
844 inner = TREE_OPERAND (inner, 0);
845 break;
847 case BIT_FIELD_REF:
848 /* A bit field reference to a specific vector is scalarized but for
849 ones for inputs need to be marked as used on the left hand size so
850 when we scalarize it, we can mark that variable as non renamable. */
851 if (is_output
852 && TREE_CODE (TREE_TYPE (TREE_OPERAND (inner, 0))) == VECTOR_TYPE)
854 struct sra_elt *elt
855 = maybe_lookup_element_for_expr (TREE_OPERAND (inner, 0));
856 if (elt)
857 elt->is_vector_lhs = true;
860 /* A bit field reference (access to *multiple* fields simultaneously)
861 is not currently scalarized. Consider this an access to the full
862 outer element, to which walk_tree will bring us next. */
863 goto use_all;
865 case NOP_EXPR:
866 /* Similarly, a nop explicitly wants to look at an object in a
867 type other than the one we've scalarized. */
868 goto use_all;
870 case VIEW_CONVERT_EXPR:
871 /* Likewise for a view conversion, but with an additional twist:
872 it can be on the LHS and, in this case, an access to the full
873 outer element would mean a killing def. So we need to punt
874 if we haven't already a full access to the current element,
875 because we cannot pretend to have a killing def if we only
876 have a partial access at some level. */
877 if (is_output && !use_all_p && inner != expr)
878 disable_scalarization = true;
879 goto use_all;
881 case WITH_SIZE_EXPR:
882 /* This is a transparent wrapper. The entire inner expression really
883 is being used. */
884 goto use_all;
886 use_all:
887 expr_p = &TREE_OPERAND (inner, 0);
888 inner = expr = *expr_p;
889 use_all_p = true;
890 break;
892 default:
893 #ifdef ENABLE_CHECKING
894 /* Validate that we're not missing any references. */
895 gcc_assert (!walk_tree (&inner, sra_find_candidate_decl, NULL, NULL));
896 #endif
897 return;
901 /* Walk a TREE_LIST of values looking for scalarizable aggregates.
902 If we find one, invoke FNS->USE. */
904 static void
905 sra_walk_tree_list (tree list, block_stmt_iterator *bsi, bool is_output,
906 const struct sra_walk_fns *fns)
908 tree op;
909 for (op = list; op ; op = TREE_CHAIN (op))
910 sra_walk_expr (&TREE_VALUE (op), bsi, is_output, fns);
913 /* Walk the arguments of a CALL_EXPR looking for scalarizable aggregates.
914 If we find one, invoke FNS->USE. */
916 static void
917 sra_walk_call_expr (tree expr, block_stmt_iterator *bsi,
918 const struct sra_walk_fns *fns)
920 int i;
921 int nargs = call_expr_nargs (expr);
922 for (i = 0; i < nargs; i++)
923 sra_walk_expr (&CALL_EXPR_ARG (expr, i), bsi, false, fns);
926 /* Walk the inputs and outputs of an ASM_EXPR looking for scalarizable
927 aggregates. If we find one, invoke FNS->USE. */
929 static void
930 sra_walk_asm_expr (tree expr, block_stmt_iterator *bsi,
931 const struct sra_walk_fns *fns)
933 sra_walk_tree_list (ASM_INPUTS (expr), bsi, false, fns);
934 sra_walk_tree_list (ASM_OUTPUTS (expr), bsi, true, fns);
937 /* Walk a GIMPLE_MODIFY_STMT and categorize the assignment appropriately. */
939 static void
940 sra_walk_gimple_modify_stmt (tree expr, block_stmt_iterator *bsi,
941 const struct sra_walk_fns *fns)
943 struct sra_elt *lhs_elt, *rhs_elt;
944 tree lhs, rhs;
946 lhs = GIMPLE_STMT_OPERAND (expr, 0);
947 rhs = GIMPLE_STMT_OPERAND (expr, 1);
948 lhs_elt = maybe_lookup_element_for_expr (lhs);
949 rhs_elt = maybe_lookup_element_for_expr (rhs);
951 /* If both sides are scalarizable, this is a COPY operation. */
952 if (lhs_elt && rhs_elt)
954 fns->copy (lhs_elt, rhs_elt, bsi);
955 return;
958 /* If the RHS is scalarizable, handle it. There are only two cases. */
959 if (rhs_elt)
961 if (!rhs_elt->is_scalar && !TREE_SIDE_EFFECTS (lhs))
962 fns->ldst (rhs_elt, lhs, bsi, false);
963 else
964 fns->use (rhs_elt, &GIMPLE_STMT_OPERAND (expr, 1), bsi, false, false);
967 /* If it isn't scalarizable, there may be scalarizable variables within, so
968 check for a call or else walk the RHS to see if we need to do any
969 copy-in operations. We need to do it before the LHS is scalarized so
970 that the statements get inserted in the proper place, before any
971 copy-out operations. */
972 else
974 tree call = get_call_expr_in (rhs);
975 if (call)
976 sra_walk_call_expr (call, bsi, fns);
977 else
978 sra_walk_expr (&GIMPLE_STMT_OPERAND (expr, 1), bsi, false, fns);
981 /* Likewise, handle the LHS being scalarizable. We have cases similar
982 to those above, but also want to handle RHS being constant. */
983 if (lhs_elt)
985 /* If this is an assignment from a constant, or constructor, then
986 we have access to all of the elements individually. Invoke INIT. */
987 if (TREE_CODE (rhs) == COMPLEX_EXPR
988 || TREE_CODE (rhs) == COMPLEX_CST
989 || TREE_CODE (rhs) == CONSTRUCTOR)
990 fns->init (lhs_elt, rhs, bsi);
992 /* If this is an assignment from read-only memory, treat this as if
993 we'd been passed the constructor directly. Invoke INIT. */
994 else if (TREE_CODE (rhs) == VAR_DECL
995 && TREE_STATIC (rhs)
996 && TREE_READONLY (rhs)
997 && targetm.binds_local_p (rhs))
998 fns->init (lhs_elt, DECL_INITIAL (rhs), bsi);
1000 /* If this is a copy from a non-scalarizable lvalue, invoke LDST.
1001 The lvalue requirement prevents us from trying to directly scalarize
1002 the result of a function call. Which would result in trying to call
1003 the function multiple times, and other evil things. */
1004 else if (!lhs_elt->is_scalar
1005 && !TREE_SIDE_EFFECTS (rhs) && is_gimple_addressable (rhs))
1006 fns->ldst (lhs_elt, rhs, bsi, true);
1008 /* Otherwise we're being used in some context that requires the
1009 aggregate to be seen as a whole. Invoke USE. */
1010 else
1011 fns->use (lhs_elt, &GIMPLE_STMT_OPERAND (expr, 0), bsi, true, false);
1014 /* Similarly to above, LHS_ELT being null only means that the LHS as a
1015 whole is not a scalarizable reference. There may be occurrences of
1016 scalarizable variables within, which implies a USE. */
1017 else
1018 sra_walk_expr (&GIMPLE_STMT_OPERAND (expr, 0), bsi, true, fns);
1021 /* Entry point to the walk functions. Search the entire function,
1022 invoking the callbacks in FNS on each of the references to
1023 scalarizable variables. */
1025 static void
1026 sra_walk_function (const struct sra_walk_fns *fns)
1028 basic_block bb;
1029 block_stmt_iterator si, ni;
1031 /* ??? Phase 4 could derive some benefit to walking the function in
1032 dominator tree order. */
1034 FOR_EACH_BB (bb)
1035 for (si = bsi_start (bb); !bsi_end_p (si); si = ni)
1037 tree stmt, t;
1038 stmt_ann_t ann;
1040 stmt = bsi_stmt (si);
1041 ann = stmt_ann (stmt);
1043 ni = si;
1044 bsi_next (&ni);
1046 /* If the statement has no virtual operands, then it doesn't
1047 make any structure references that we care about. */
1048 if (gimple_aliases_computed_p (cfun)
1049 && ZERO_SSA_OPERANDS (stmt, (SSA_OP_VIRTUAL_DEFS | SSA_OP_VUSE)))
1050 continue;
1052 switch (TREE_CODE (stmt))
1054 case RETURN_EXPR:
1055 /* If we have "return <retval>" then the return value is
1056 already exposed for our pleasure. Walk it as a USE to
1057 force all the components back in place for the return.
1059 If we have an embedded assignment, then <retval> is of
1060 a type that gets returned in registers in this ABI, and
1061 we do not wish to extend their lifetimes. Treat this
1062 as a USE of the variable on the RHS of this assignment. */
1064 t = TREE_OPERAND (stmt, 0);
1065 if (t == NULL_TREE)
1067 else if (TREE_CODE (t) == GIMPLE_MODIFY_STMT)
1068 sra_walk_expr (&GIMPLE_STMT_OPERAND (t, 1), &si, false, fns);
1069 else
1070 sra_walk_expr (&TREE_OPERAND (stmt, 0), &si, false, fns);
1071 break;
1073 case GIMPLE_MODIFY_STMT:
1074 sra_walk_gimple_modify_stmt (stmt, &si, fns);
1075 break;
1076 case CALL_EXPR:
1077 sra_walk_call_expr (stmt, &si, fns);
1078 break;
1079 case ASM_EXPR:
1080 sra_walk_asm_expr (stmt, &si, fns);
1081 break;
1083 default:
1084 break;
1089 /* Phase One: Scan all referenced variables in the program looking for
1090 structures that could be decomposed. */
1092 static bool
1093 find_candidates_for_sra (void)
1095 bool any_set = false;
1096 tree var;
1097 referenced_var_iterator rvi;
1099 FOR_EACH_REFERENCED_VAR (var, rvi)
1101 if (decl_can_be_decomposed_p (var))
1103 bitmap_set_bit (sra_candidates, DECL_UID (var));
1104 any_set = true;
1108 return any_set;
1112 /* Phase Two: Scan all references to scalarizable variables. Count the
1113 number of times they are used or copied respectively. */
1115 /* Callbacks to fill in SRA_WALK_FNS. Everything but USE is
1116 considered a copy, because we can decompose the reference such that
1117 the sub-elements needn't be contiguous. */
1119 static void
1120 scan_use (struct sra_elt *elt, tree *expr_p ATTRIBUTE_UNUSED,
1121 block_stmt_iterator *bsi ATTRIBUTE_UNUSED,
1122 bool is_output ATTRIBUTE_UNUSED, bool use_all ATTRIBUTE_UNUSED)
1124 elt->n_uses += 1;
1127 static void
1128 scan_copy (struct sra_elt *lhs_elt, struct sra_elt *rhs_elt,
1129 block_stmt_iterator *bsi ATTRIBUTE_UNUSED)
1131 lhs_elt->n_copies += 1;
1132 rhs_elt->n_copies += 1;
1135 static void
1136 scan_init (struct sra_elt *lhs_elt, tree rhs ATTRIBUTE_UNUSED,
1137 block_stmt_iterator *bsi ATTRIBUTE_UNUSED)
1139 lhs_elt->n_copies += 1;
1142 static void
1143 scan_ldst (struct sra_elt *elt, tree other ATTRIBUTE_UNUSED,
1144 block_stmt_iterator *bsi ATTRIBUTE_UNUSED,
1145 bool is_output ATTRIBUTE_UNUSED)
1147 elt->n_copies += 1;
1150 /* Dump the values we collected during the scanning phase. */
1152 static void
1153 scan_dump (struct sra_elt *elt)
1155 struct sra_elt *c;
1157 dump_sra_elt_name (dump_file, elt);
1158 fprintf (dump_file, ": n_uses=%u n_copies=%u\n", elt->n_uses, elt->n_copies);
1160 for (c = elt->children; c ; c = c->sibling)
1161 scan_dump (c);
1163 for (c = elt->groups; c ; c = c->sibling)
1164 scan_dump (c);
1167 /* Entry point to phase 2. Scan the entire function, building up
1168 scalarization data structures, recording copies and uses. */
1170 static void
1171 scan_function (void)
1173 static const struct sra_walk_fns fns = {
1174 scan_use, scan_copy, scan_init, scan_ldst, true
1176 bitmap_iterator bi;
1178 sra_walk_function (&fns);
1180 if (dump_file && (dump_flags & TDF_DETAILS))
1182 unsigned i;
1184 fputs ("\nScan results:\n", dump_file);
1185 EXECUTE_IF_SET_IN_BITMAP (sra_candidates, 0, i, bi)
1187 tree var = referenced_var (i);
1188 struct sra_elt *elt = lookup_element (NULL, var, NULL, NO_INSERT);
1189 if (elt)
1190 scan_dump (elt);
1192 fputc ('\n', dump_file);
1196 /* Phase Three: Make decisions about which variables to scalarize, if any.
1197 All elements to be scalarized have replacement variables made for them. */
1199 /* A subroutine of build_element_name. Recursively build the element
1200 name on the obstack. */
1202 static void
1203 build_element_name_1 (struct sra_elt *elt)
1205 tree t;
1206 char buffer[32];
1208 if (elt->parent)
1210 build_element_name_1 (elt->parent);
1211 obstack_1grow (&sra_obstack, '$');
1213 if (TREE_CODE (elt->parent->type) == COMPLEX_TYPE)
1215 if (elt->element == integer_zero_node)
1216 obstack_grow (&sra_obstack, "real", 4);
1217 else
1218 obstack_grow (&sra_obstack, "imag", 4);
1219 return;
1223 t = elt->element;
1224 if (TREE_CODE (t) == INTEGER_CST)
1226 /* ??? Eh. Don't bother doing double-wide printing. */
1227 sprintf (buffer, HOST_WIDE_INT_PRINT_DEC, TREE_INT_CST_LOW (t));
1228 obstack_grow (&sra_obstack, buffer, strlen (buffer));
1230 else if (TREE_CODE (t) == BIT_FIELD_REF)
1232 sprintf (buffer, "B" HOST_WIDE_INT_PRINT_DEC,
1233 tree_low_cst (TREE_OPERAND (t, 2), 1));
1234 obstack_grow (&sra_obstack, buffer, strlen (buffer));
1235 sprintf (buffer, "F" HOST_WIDE_INT_PRINT_DEC,
1236 tree_low_cst (TREE_OPERAND (t, 1), 1));
1237 obstack_grow (&sra_obstack, buffer, strlen (buffer));
1239 else
1241 tree name = DECL_NAME (t);
1242 if (name)
1243 obstack_grow (&sra_obstack, IDENTIFIER_POINTER (name),
1244 IDENTIFIER_LENGTH (name));
1245 else
1247 sprintf (buffer, "D%u", DECL_UID (t));
1248 obstack_grow (&sra_obstack, buffer, strlen (buffer));
1253 /* Construct a pretty variable name for an element's replacement variable.
1254 The name is built on the obstack. */
1256 static char *
1257 build_element_name (struct sra_elt *elt)
1259 build_element_name_1 (elt);
1260 obstack_1grow (&sra_obstack, '\0');
1261 return XOBFINISH (&sra_obstack, char *);
1264 /* Instantiate an element as an independent variable. */
1266 static void
1267 instantiate_element (struct sra_elt *elt)
1269 struct sra_elt *base_elt;
1270 tree var, base;
1271 bool nowarn = TREE_NO_WARNING (elt->element);
1273 for (base_elt = elt; base_elt->parent; base_elt = base_elt->parent)
1274 if (!nowarn)
1275 nowarn = TREE_NO_WARNING (base_elt->parent->element);
1276 base = base_elt->element;
1278 elt->replacement = var = make_rename_temp (elt->type, "SR");
1280 if (DECL_P (elt->element)
1281 && !tree_int_cst_equal (DECL_SIZE (var), DECL_SIZE (elt->element)))
1283 DECL_SIZE (var) = DECL_SIZE (elt->element);
1284 DECL_SIZE_UNIT (var) = DECL_SIZE_UNIT (elt->element);
1286 elt->in_bitfld_block = 1;
1287 elt->replacement = build3 (BIT_FIELD_REF, elt->type, var,
1288 DECL_SIZE (var),
1289 BYTES_BIG_ENDIAN
1290 ? size_binop (MINUS_EXPR,
1291 TYPE_SIZE (elt->type),
1292 DECL_SIZE (var))
1293 : bitsize_int (0));
1296 /* For vectors, if used on the left hand side with BIT_FIELD_REF,
1297 they are not a gimple register. */
1298 if (TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE && elt->is_vector_lhs)
1299 DECL_GIMPLE_REG_P (var) = 0;
1301 DECL_SOURCE_LOCATION (var) = DECL_SOURCE_LOCATION (base);
1302 DECL_ARTIFICIAL (var) = 1;
1304 if (TREE_THIS_VOLATILE (elt->type))
1306 TREE_THIS_VOLATILE (var) = 1;
1307 TREE_SIDE_EFFECTS (var) = 1;
1310 if (DECL_NAME (base) && !DECL_IGNORED_P (base))
1312 char *pretty_name = build_element_name (elt);
1313 DECL_NAME (var) = get_identifier (pretty_name);
1314 obstack_free (&sra_obstack, pretty_name);
1316 SET_DECL_DEBUG_EXPR (var, generate_element_ref (elt));
1317 DECL_DEBUG_EXPR_IS_FROM (var) = 1;
1319 DECL_IGNORED_P (var) = 0;
1320 TREE_NO_WARNING (var) = nowarn;
1322 else
1324 DECL_IGNORED_P (var) = 1;
1325 /* ??? We can't generate any warning that would be meaningful. */
1326 TREE_NO_WARNING (var) = 1;
1329 /* Zero-initialize bit-field scalarization variables, to avoid
1330 triggering undefined behavior. */
1331 if (TREE_CODE (elt->element) == BIT_FIELD_REF
1332 || (var != elt->replacement
1333 && TREE_CODE (elt->replacement) == BIT_FIELD_REF))
1335 tree init = sra_build_assignment (var, fold_convert (TREE_TYPE (var),
1336 integer_zero_node));
1337 insert_edge_copies (init, ENTRY_BLOCK_PTR);
1338 mark_all_v_defs (init);
1341 if (dump_file)
1343 fputs (" ", dump_file);
1344 dump_sra_elt_name (dump_file, elt);
1345 fputs (" -> ", dump_file);
1346 print_generic_expr (dump_file, var, dump_flags);
1347 fputc ('\n', dump_file);
1351 /* Make one pass across an element tree deciding whether or not it's
1352 profitable to instantiate individual leaf scalars.
1354 PARENT_USES and PARENT_COPIES are the sum of the N_USES and N_COPIES
1355 fields all the way up the tree. */
1357 static void
1358 decide_instantiation_1 (struct sra_elt *elt, unsigned int parent_uses,
1359 unsigned int parent_copies)
1361 if (dump_file && !elt->parent)
1363 fputs ("Initial instantiation for ", dump_file);
1364 dump_sra_elt_name (dump_file, elt);
1365 fputc ('\n', dump_file);
1368 if (elt->cannot_scalarize)
1369 return;
1371 if (elt->is_scalar)
1373 /* The decision is simple: instantiate if we're used more frequently
1374 than the parent needs to be seen as a complete unit. */
1375 if (elt->n_uses + elt->n_copies + parent_copies > parent_uses)
1376 instantiate_element (elt);
1378 else
1380 struct sra_elt *c, *group;
1381 unsigned int this_uses = elt->n_uses + parent_uses;
1382 unsigned int this_copies = elt->n_copies + parent_copies;
1384 /* Consider groups of sub-elements as weighing in favour of
1385 instantiation whatever their size. */
1386 for (group = elt->groups; group ; group = group->sibling)
1387 FOR_EACH_ACTUAL_CHILD (c, group)
1389 c->n_uses += group->n_uses;
1390 c->n_copies += group->n_copies;
1393 for (c = elt->children; c ; c = c->sibling)
1394 decide_instantiation_1 (c, this_uses, this_copies);
1398 /* Compute the size and number of all instantiated elements below ELT.
1399 We will only care about this if the size of the complete structure
1400 fits in a HOST_WIDE_INT, so we don't have to worry about overflow. */
1402 static unsigned int
1403 sum_instantiated_sizes (struct sra_elt *elt, unsigned HOST_WIDE_INT *sizep)
1405 if (elt->replacement)
1407 *sizep += TREE_INT_CST_LOW (TYPE_SIZE_UNIT (elt->type));
1408 return 1;
1410 else
1412 struct sra_elt *c;
1413 unsigned int count = 0;
1415 for (c = elt->children; c ; c = c->sibling)
1416 count += sum_instantiated_sizes (c, sizep);
1418 return count;
1422 /* Instantiate fields in ELT->TYPE that are not currently present as
1423 children of ELT. */
1425 static void instantiate_missing_elements (struct sra_elt *elt);
1427 static struct sra_elt *
1428 instantiate_missing_elements_1 (struct sra_elt *elt, tree child, tree type)
1430 struct sra_elt *sub = lookup_element (elt, child, type, INSERT);
1431 if (sub->is_scalar)
1433 if (sub->replacement == NULL)
1434 instantiate_element (sub);
1436 else
1437 instantiate_missing_elements (sub);
1438 return sub;
1441 /* Obtain the canonical type for field F of ELEMENT. */
1443 static tree
1444 canon_type_for_field (tree f, tree element)
1446 tree field_type = TREE_TYPE (f);
1448 /* canonicalize_component_ref() unwidens some bit-field types (not
1449 marked as DECL_BIT_FIELD in C++), so we must do the same, lest we
1450 may introduce type mismatches. */
1451 if (INTEGRAL_TYPE_P (field_type)
1452 && DECL_MODE (f) != TYPE_MODE (field_type))
1453 field_type = TREE_TYPE (get_unwidened (build3 (COMPONENT_REF,
1454 field_type,
1455 element,
1456 f, NULL_TREE),
1457 NULL_TREE));
1459 return field_type;
1462 /* Look for adjacent fields of ELT starting at F that we'd like to
1463 scalarize as a single variable. Return the last field of the
1464 group. */
1466 static tree
1467 try_instantiate_multiple_fields (struct sra_elt *elt, tree f)
1469 int count;
1470 unsigned HOST_WIDE_INT align, bit, size, alchk;
1471 enum machine_mode mode;
1472 tree first = f, prev;
1473 tree type, var;
1474 struct sra_elt *block;
1476 /* Point fields are typically best handled as standalone entities. */
1477 if (POINTER_TYPE_P (TREE_TYPE (f)))
1478 return f;
1480 if (!is_sra_scalar_type (TREE_TYPE (f))
1481 || !host_integerp (DECL_FIELD_OFFSET (f), 1)
1482 || !host_integerp (DECL_FIELD_BIT_OFFSET (f), 1)
1483 || !host_integerp (DECL_SIZE (f), 1)
1484 || lookup_element (elt, f, NULL, NO_INSERT))
1485 return f;
1487 block = elt;
1489 /* For complex and array objects, there are going to be integer
1490 literals as child elements. In this case, we can't just take the
1491 alignment and mode of the decl, so we instead rely on the element
1492 type.
1494 ??? We could try to infer additional alignment from the full
1495 object declaration and the location of the sub-elements we're
1496 accessing. */
1497 for (count = 0; !DECL_P (block->element); count++)
1498 block = block->parent;
1500 align = DECL_ALIGN (block->element);
1501 alchk = GET_MODE_BITSIZE (DECL_MODE (block->element));
1503 if (count)
1505 type = TREE_TYPE (block->element);
1506 while (count--)
1507 type = TREE_TYPE (type);
1509 align = TYPE_ALIGN (type);
1510 alchk = GET_MODE_BITSIZE (TYPE_MODE (type));
1513 if (align < alchk)
1514 align = alchk;
1516 /* Coalescing wider fields is probably pointless and
1517 inefficient. */
1518 if (align > BITS_PER_WORD)
1519 align = BITS_PER_WORD;
1521 bit = tree_low_cst (DECL_FIELD_OFFSET (f), 1) * BITS_PER_UNIT
1522 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f), 1);
1523 size = tree_low_cst (DECL_SIZE (f), 1);
1525 alchk = align - 1;
1526 alchk = ~alchk;
1528 if ((bit & alchk) != ((bit + size - 1) & alchk))
1529 return f;
1531 /* Find adjacent fields in the same alignment word. */
1533 for (prev = f, f = TREE_CHAIN (f);
1534 f && TREE_CODE (f) == FIELD_DECL
1535 && is_sra_scalar_type (TREE_TYPE (f))
1536 && host_integerp (DECL_FIELD_OFFSET (f), 1)
1537 && host_integerp (DECL_FIELD_BIT_OFFSET (f), 1)
1538 && host_integerp (DECL_SIZE (f), 1)
1539 && !lookup_element (elt, f, NULL, NO_INSERT);
1540 prev = f, f = TREE_CHAIN (f))
1542 unsigned HOST_WIDE_INT nbit, nsize;
1544 nbit = tree_low_cst (DECL_FIELD_OFFSET (f), 1) * BITS_PER_UNIT
1545 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f), 1);
1546 nsize = tree_low_cst (DECL_SIZE (f), 1);
1548 if (bit + size == nbit)
1550 if ((bit & alchk) != ((nbit + nsize - 1) & alchk))
1552 /* If we're at an alignment boundary, don't bother
1553 growing alignment such that we can include this next
1554 field. */
1555 if ((nbit & alchk)
1556 || GET_MODE_BITSIZE (DECL_MODE (f)) <= align)
1557 break;
1559 align = GET_MODE_BITSIZE (DECL_MODE (f));
1560 alchk = align - 1;
1561 alchk = ~alchk;
1563 if ((bit & alchk) != ((nbit + nsize - 1) & alchk))
1564 break;
1566 size += nsize;
1568 else if (nbit + nsize == bit)
1570 if ((nbit & alchk) != ((bit + size - 1) & alchk))
1572 if ((bit & alchk)
1573 || GET_MODE_BITSIZE (DECL_MODE (f)) <= align)
1574 break;
1576 align = GET_MODE_BITSIZE (DECL_MODE (f));
1577 alchk = align - 1;
1578 alchk = ~alchk;
1580 if ((nbit & alchk) != ((bit + size - 1) & alchk))
1581 break;
1583 bit = nbit;
1584 size += nsize;
1586 else
1587 break;
1590 f = prev;
1592 if (f == first)
1593 return f;
1595 gcc_assert ((bit & alchk) == ((bit + size - 1) & alchk));
1597 /* Try to widen the bit range so as to cover padding bits as well. */
1599 if ((bit & ~alchk) || size != align)
1601 unsigned HOST_WIDE_INT mbit = bit & alchk;
1602 unsigned HOST_WIDE_INT msize = align;
1604 for (f = TYPE_FIELDS (elt->type);
1605 f; f = TREE_CHAIN (f))
1607 unsigned HOST_WIDE_INT fbit, fsize;
1609 /* Skip the fields from first to prev. */
1610 if (f == first)
1612 f = prev;
1613 continue;
1616 if (!(TREE_CODE (f) == FIELD_DECL
1617 && host_integerp (DECL_FIELD_OFFSET (f), 1)
1618 && host_integerp (DECL_FIELD_BIT_OFFSET (f), 1)))
1619 continue;
1621 fbit = tree_low_cst (DECL_FIELD_OFFSET (f), 1) * BITS_PER_UNIT
1622 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f), 1);
1624 /* If we're past the selected word, we're fine. */
1625 if ((bit & alchk) < (fbit & alchk))
1626 continue;
1628 if (host_integerp (DECL_SIZE (f), 1))
1629 fsize = tree_low_cst (DECL_SIZE (f), 1);
1630 else
1631 /* Assume a variable-sized field takes up all space till
1632 the end of the word. ??? Endianness issues? */
1633 fsize = align - (fbit & alchk);
1635 if ((fbit & alchk) < (bit & alchk))
1637 /* A large field might start at a previous word and
1638 extend into the selected word. Exclude those
1639 bits. ??? Endianness issues? */
1640 HOST_WIDE_INT diff = fbit + fsize - mbit;
1642 if (diff <= 0)
1643 continue;
1645 mbit += diff;
1646 msize -= diff;
1648 else
1650 /* Non-overlapping, great. */
1651 if (fbit + fsize <= mbit
1652 || mbit + msize <= fbit)
1653 continue;
1655 if (fbit <= mbit)
1657 unsigned HOST_WIDE_INT diff = fbit + fsize - mbit;
1658 mbit += diff;
1659 msize -= diff;
1661 else if (fbit > mbit)
1662 msize -= (mbit + msize - fbit);
1663 else
1664 gcc_unreachable ();
1668 bit = mbit;
1669 size = msize;
1672 /* Now we know the bit range we're interested in. Find the smallest
1673 machine mode we can use to access it. */
1675 for (mode = smallest_mode_for_size (size, MODE_INT);
1677 mode = GET_MODE_WIDER_MODE (mode))
1679 gcc_assert (mode != VOIDmode);
1681 alchk = GET_MODE_PRECISION (mode) - 1;
1682 alchk = ~alchk;
1684 if ((bit & alchk) == ((bit + size - 1) & alchk))
1685 break;
1688 gcc_assert (~alchk < align);
1690 /* Create the field group as a single variable. */
1692 /* We used to create a type for the mode above, but size turns
1693 to be out not of mode-size. As we need a matching type
1694 to build a BIT_FIELD_REF, use a nonstandard integer type as
1695 fallback. */
1696 type = lang_hooks.types.type_for_size (size, 1);
1697 if (!type || TYPE_PRECISION (type) != size)
1698 type = build_nonstandard_integer_type (size, 1);
1699 gcc_assert (type);
1700 var = build3 (BIT_FIELD_REF, type, NULL_TREE,
1701 bitsize_int (size),
1702 bitsize_int (bit));
1704 block = instantiate_missing_elements_1 (elt, var, type);
1705 gcc_assert (block && block->is_scalar);
1707 var = block->replacement;
1709 if ((bit & ~alchk)
1710 || (HOST_WIDE_INT)size != tree_low_cst (DECL_SIZE (var), 1))
1712 block->replacement = build3 (BIT_FIELD_REF,
1713 TREE_TYPE (block->element), var,
1714 bitsize_int (size),
1715 bitsize_int (bit & ~alchk));
1718 block->in_bitfld_block = 2;
1720 /* Add the member fields to the group, such that they access
1721 portions of the group variable. */
1723 for (f = first; f != TREE_CHAIN (prev); f = TREE_CHAIN (f))
1725 tree field_type = canon_type_for_field (f, elt->element);
1726 struct sra_elt *fld = lookup_element (block, f, field_type, INSERT);
1728 gcc_assert (fld && fld->is_scalar && !fld->replacement);
1730 fld->replacement = build3 (BIT_FIELD_REF, field_type, var,
1731 DECL_SIZE (f),
1732 bitsize_int
1733 ((TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f))
1734 * BITS_PER_UNIT
1735 + (TREE_INT_CST_LOW
1736 (DECL_FIELD_BIT_OFFSET (f))))
1737 & ~alchk));
1738 fld->in_bitfld_block = 1;
1741 return prev;
1744 static void
1745 instantiate_missing_elements (struct sra_elt *elt)
1747 tree type = elt->type;
1749 switch (TREE_CODE (type))
1751 case RECORD_TYPE:
1753 tree f;
1754 for (f = TYPE_FIELDS (type); f ; f = TREE_CHAIN (f))
1755 if (TREE_CODE (f) == FIELD_DECL)
1757 tree last = try_instantiate_multiple_fields (elt, f);
1759 if (last != f)
1761 f = last;
1762 continue;
1765 instantiate_missing_elements_1 (elt, f,
1766 canon_type_for_field
1767 (f, elt->element));
1769 break;
1772 case ARRAY_TYPE:
1774 tree i, max, subtype;
1776 i = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
1777 max = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1778 subtype = TREE_TYPE (type);
1780 while (1)
1782 instantiate_missing_elements_1 (elt, i, subtype);
1783 if (tree_int_cst_equal (i, max))
1784 break;
1785 i = int_const_binop (PLUS_EXPR, i, integer_one_node, true);
1788 break;
1791 case COMPLEX_TYPE:
1792 type = TREE_TYPE (type);
1793 instantiate_missing_elements_1 (elt, integer_zero_node, type);
1794 instantiate_missing_elements_1 (elt, integer_one_node, type);
1795 break;
1797 default:
1798 gcc_unreachable ();
1802 /* Return true if there is only one non aggregate field in the record, TYPE.
1803 Return false otherwise. */
1805 static bool
1806 single_scalar_field_in_record_p (tree type)
1808 int num_fields = 0;
1809 tree field;
1810 if (TREE_CODE (type) != RECORD_TYPE)
1811 return false;
1813 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1814 if (TREE_CODE (field) == FIELD_DECL)
1816 num_fields++;
1818 if (num_fields == 2)
1819 return false;
1821 if (AGGREGATE_TYPE_P (TREE_TYPE (field)))
1822 return false;
1825 return true;
1828 /* Make one pass across an element tree deciding whether to perform block
1829 or element copies. If we decide on element copies, instantiate all
1830 elements. Return true if there are any instantiated sub-elements. */
1832 static bool
1833 decide_block_copy (struct sra_elt *elt)
1835 struct sra_elt *c;
1836 bool any_inst;
1838 /* We shouldn't be invoked on groups of sub-elements as they must
1839 behave like their parent as far as block copy is concerned. */
1840 gcc_assert (!elt->is_group);
1842 /* If scalarization is disabled, respect it. */
1843 if (elt->cannot_scalarize)
1845 elt->use_block_copy = 1;
1847 if (dump_file)
1849 fputs ("Scalarization disabled for ", dump_file);
1850 dump_sra_elt_name (dump_file, elt);
1851 fputc ('\n', dump_file);
1854 /* Disable scalarization of sub-elements */
1855 for (c = elt->children; c; c = c->sibling)
1857 c->cannot_scalarize = 1;
1858 decide_block_copy (c);
1861 /* Groups behave like their parent. */
1862 for (c = elt->groups; c; c = c->sibling)
1864 c->cannot_scalarize = 1;
1865 c->use_block_copy = 1;
1868 return false;
1871 /* Don't decide if we've no uses and no groups. */
1872 if (elt->n_uses == 0 && elt->n_copies == 0 && elt->groups == NULL)
1875 else if (!elt->is_scalar)
1877 tree size_tree = TYPE_SIZE_UNIT (elt->type);
1878 bool use_block_copy = true;
1880 /* Tradeoffs for COMPLEX types pretty much always make it better
1881 to go ahead and split the components. */
1882 if (TREE_CODE (elt->type) == COMPLEX_TYPE)
1883 use_block_copy = false;
1885 /* Don't bother trying to figure out the rest if the structure is
1886 so large we can't do easy arithmetic. This also forces block
1887 copies for variable sized structures. */
1888 else if (host_integerp (size_tree, 1))
1890 unsigned HOST_WIDE_INT full_size, inst_size = 0;
1891 unsigned int max_size, max_count, inst_count, full_count;
1893 /* If the sra-max-structure-size parameter is 0, then the
1894 user has not overridden the parameter and we can choose a
1895 sensible default. */
1896 max_size = SRA_MAX_STRUCTURE_SIZE
1897 ? SRA_MAX_STRUCTURE_SIZE
1898 : MOVE_RATIO * UNITS_PER_WORD;
1899 max_count = SRA_MAX_STRUCTURE_COUNT
1900 ? SRA_MAX_STRUCTURE_COUNT
1901 : MOVE_RATIO;
1903 full_size = tree_low_cst (size_tree, 1);
1904 full_count = count_type_elements (elt->type, false);
1905 inst_count = sum_instantiated_sizes (elt, &inst_size);
1907 /* If there is only one scalar field in the record, don't block copy. */
1908 if (single_scalar_field_in_record_p (elt->type))
1909 use_block_copy = false;
1911 /* ??? What to do here. If there are two fields, and we've only
1912 instantiated one, then instantiating the other is clearly a win.
1913 If there are a large number of fields then the size of the copy
1914 is much more of a factor. */
1916 /* If the structure is small, and we've made copies, go ahead
1917 and instantiate, hoping that the copies will go away. */
1918 if (full_size <= max_size
1919 && (full_count - inst_count) <= max_count
1920 && elt->n_copies > elt->n_uses)
1921 use_block_copy = false;
1922 else if (inst_count * 100 >= full_count * SRA_FIELD_STRUCTURE_RATIO
1923 && inst_size * 100 >= full_size * SRA_FIELD_STRUCTURE_RATIO)
1924 use_block_copy = false;
1926 /* In order to avoid block copy, we have to be able to instantiate
1927 all elements of the type. See if this is possible. */
1928 if (!use_block_copy
1929 && (!can_completely_scalarize_p (elt)
1930 || !type_can_instantiate_all_elements (elt->type)))
1931 use_block_copy = true;
1934 elt->use_block_copy = use_block_copy;
1936 /* Groups behave like their parent. */
1937 for (c = elt->groups; c; c = c->sibling)
1938 c->use_block_copy = use_block_copy;
1940 if (dump_file)
1942 fprintf (dump_file, "Using %s for ",
1943 use_block_copy ? "block-copy" : "element-copy");
1944 dump_sra_elt_name (dump_file, elt);
1945 fputc ('\n', dump_file);
1948 if (!use_block_copy)
1950 instantiate_missing_elements (elt);
1951 return true;
1955 any_inst = elt->replacement != NULL;
1957 for (c = elt->children; c ; c = c->sibling)
1958 any_inst |= decide_block_copy (c);
1960 return any_inst;
1963 /* Entry point to phase 3. Instantiate scalar replacement variables. */
1965 static void
1966 decide_instantiations (void)
1968 unsigned int i;
1969 bool cleared_any;
1970 bitmap_head done_head;
1971 bitmap_iterator bi;
1973 /* We cannot clear bits from a bitmap we're iterating over,
1974 so save up all the bits to clear until the end. */
1975 bitmap_initialize (&done_head, &bitmap_default_obstack);
1976 cleared_any = false;
1978 EXECUTE_IF_SET_IN_BITMAP (sra_candidates, 0, i, bi)
1980 tree var = referenced_var (i);
1981 struct sra_elt *elt = lookup_element (NULL, var, NULL, NO_INSERT);
1982 if (elt)
1984 decide_instantiation_1 (elt, 0, 0);
1985 if (!decide_block_copy (elt))
1986 elt = NULL;
1988 if (!elt)
1990 bitmap_set_bit (&done_head, i);
1991 cleared_any = true;
1995 if (cleared_any)
1997 bitmap_and_compl_into (sra_candidates, &done_head);
1998 bitmap_and_compl_into (needs_copy_in, &done_head);
2000 bitmap_clear (&done_head);
2002 mark_set_for_renaming (sra_candidates);
2004 if (dump_file)
2005 fputc ('\n', dump_file);
2009 /* Phase Four: Update the function to match the replacements created. */
2011 /* Mark all the variables in VDEF/VUSE operators for STMT for
2012 renaming. This becomes necessary when we modify all of a
2013 non-scalar. */
2015 static void
2016 mark_all_v_defs_1 (tree stmt)
2018 tree sym;
2019 ssa_op_iter iter;
2021 update_stmt_if_modified (stmt);
2023 FOR_EACH_SSA_TREE_OPERAND (sym, stmt, iter, SSA_OP_ALL_VIRTUALS)
2025 if (TREE_CODE (sym) == SSA_NAME)
2026 sym = SSA_NAME_VAR (sym);
2027 mark_sym_for_renaming (sym);
2032 /* Mark all the variables in virtual operands in all the statements in
2033 LIST for renaming. */
2035 static void
2036 mark_all_v_defs (tree list)
2038 if (TREE_CODE (list) != STATEMENT_LIST)
2039 mark_all_v_defs_1 (list);
2040 else
2042 tree_stmt_iterator i;
2043 for (i = tsi_start (list); !tsi_end_p (i); tsi_next (&i))
2044 mark_all_v_defs_1 (tsi_stmt (i));
2049 /* Mark every replacement under ELT with TREE_NO_WARNING. */
2051 static void
2052 mark_no_warning (struct sra_elt *elt)
2054 if (!elt->all_no_warning)
2056 if (elt->replacement)
2057 TREE_NO_WARNING (elt->replacement) = 1;
2058 else
2060 struct sra_elt *c;
2061 FOR_EACH_ACTUAL_CHILD (c, elt)
2062 mark_no_warning (c);
2064 elt->all_no_warning = true;
2068 /* Build a single level component reference to ELT rooted at BASE. */
2070 static tree
2071 generate_one_element_ref (struct sra_elt *elt, tree base)
2073 switch (TREE_CODE (TREE_TYPE (base)))
2075 case RECORD_TYPE:
2077 tree field = elt->element;
2079 /* We can't test elt->in_bitfld_blk here because, when this is
2080 called from instantiate_element, we haven't set this field
2081 yet. */
2082 if (TREE_CODE (field) == BIT_FIELD_REF)
2084 tree ret = unshare_expr (field);
2085 TREE_OPERAND (ret, 0) = base;
2086 return ret;
2089 /* Watch out for compatible records with differing field lists. */
2090 if (DECL_FIELD_CONTEXT (field) != TYPE_MAIN_VARIANT (TREE_TYPE (base)))
2091 field = find_compatible_field (TREE_TYPE (base), field);
2093 return build3 (COMPONENT_REF, elt->type, base, field, NULL);
2096 case ARRAY_TYPE:
2097 if (TREE_CODE (elt->element) == RANGE_EXPR)
2098 return build4 (ARRAY_RANGE_REF, elt->type, base,
2099 TREE_OPERAND (elt->element, 0), NULL, NULL);
2100 else
2101 return build4 (ARRAY_REF, elt->type, base, elt->element, NULL, NULL);
2103 case COMPLEX_TYPE:
2104 if (elt->element == integer_zero_node)
2105 return build1 (REALPART_EXPR, elt->type, base);
2106 else
2107 return build1 (IMAGPART_EXPR, elt->type, base);
2109 default:
2110 gcc_unreachable ();
2114 /* Build a full component reference to ELT rooted at its native variable. */
2116 static tree
2117 generate_element_ref (struct sra_elt *elt)
2119 if (elt->parent)
2120 return generate_one_element_ref (elt, generate_element_ref (elt->parent));
2121 else
2122 return elt->element;
2125 /* Return true if BF is a bit-field that we can handle like a scalar. */
2127 static bool
2128 scalar_bitfield_p (tree bf)
2130 return (TREE_CODE (bf) == BIT_FIELD_REF
2131 && (is_gimple_reg (TREE_OPERAND (bf, 0))
2132 || (TYPE_MODE (TREE_TYPE (TREE_OPERAND (bf, 0))) != BLKmode
2133 && (!TREE_SIDE_EFFECTS (TREE_OPERAND (bf, 0))
2134 || (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE
2135 (TREE_OPERAND (bf, 0))))
2136 <= BITS_PER_WORD)))));
2139 /* Create an assignment statement from SRC to DST. */
2141 static tree
2142 sra_build_assignment (tree dst, tree src)
2144 /* Turning BIT_FIELD_REFs into bit operations enables other passes
2145 to do a much better job at optimizing the code.
2146 From dst = BIT_FIELD_REF <var, sz, off> we produce
2148 SR.1 = (scalar type) var;
2149 SR.2 = SR.1 >> off;
2150 SR.3 = SR.2 & ((1 << sz) - 1);
2151 ... possible sign extension of SR.3 ...
2152 dst = (destination type) SR.3;
2154 if (scalar_bitfield_p (src))
2156 tree var, shift, width;
2157 tree utype, stype, stmp, utmp, dtmp;
2158 tree list, stmt;
2159 bool unsignedp = (INTEGRAL_TYPE_P (TREE_TYPE (src))
2160 ? TYPE_UNSIGNED (TREE_TYPE (src)) : true);
2162 var = TREE_OPERAND (src, 0);
2163 width = TREE_OPERAND (src, 1);
2164 /* The offset needs to be adjusted to a right shift quantity
2165 depending on the endianess. */
2166 if (BYTES_BIG_ENDIAN)
2168 tree tmp = size_binop (PLUS_EXPR, width, TREE_OPERAND (src, 2));
2169 shift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), tmp);
2171 else
2172 shift = TREE_OPERAND (src, 2);
2174 /* In weird cases we have non-integral types for the source or
2175 destination object.
2176 ??? For unknown reasons we also want an unsigned scalar type. */
2177 stype = TREE_TYPE (var);
2178 if (!INTEGRAL_TYPE_P (stype))
2179 stype = lang_hooks.types.type_for_size (TREE_INT_CST_LOW
2180 (TYPE_SIZE (stype)), 1);
2181 else if (!TYPE_UNSIGNED (stype))
2182 stype = unsigned_type_for (stype);
2184 utype = TREE_TYPE (dst);
2185 if (!INTEGRAL_TYPE_P (utype))
2186 utype = lang_hooks.types.type_for_size (TREE_INT_CST_LOW
2187 (TYPE_SIZE (utype)), 1);
2188 else if (!TYPE_UNSIGNED (utype))
2189 utype = unsigned_type_for (utype);
2191 list = NULL;
2192 stmp = make_rename_temp (stype, "SR");
2194 /* Convert the base var of the BIT_FIELD_REF to the scalar type
2195 we use for computation if we cannot use it directly. */
2196 if (!useless_type_conversion_p (stype, TREE_TYPE (var)))
2198 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
2199 stmt = build_gimple_modify_stmt (stmp,
2200 fold_convert (stype, var));
2201 else
2202 stmt = build_gimple_modify_stmt (stmp,
2203 fold_build1 (VIEW_CONVERT_EXPR,
2204 stype, var));
2205 append_to_statement_list (stmt, &list);
2206 var = stmp;
2209 if (!integer_zerop (shift))
2211 stmt = build_gimple_modify_stmt (stmp,
2212 fold_build2 (RSHIFT_EXPR, stype,
2213 var, shift));
2214 append_to_statement_list (stmt, &list);
2215 var = stmp;
2218 /* If we need a masking operation, produce one. */
2219 if (TREE_INT_CST_LOW (width) == TYPE_PRECISION (stype))
2220 unsignedp = true;
2221 else
2223 tree one = build_int_cst_wide (stype, 1, 0);
2224 tree mask = int_const_binop (LSHIFT_EXPR, one, width, 0);
2225 mask = int_const_binop (MINUS_EXPR, mask, one, 0);
2227 stmt = build_gimple_modify_stmt (stmp,
2228 fold_build2 (BIT_AND_EXPR, stype,
2229 var, mask));
2230 append_to_statement_list (stmt, &list);
2231 var = stmp;
2234 /* After shifting and masking, convert to the target type. */
2235 utmp = stmp;
2236 if (!useless_type_conversion_p (utype, stype))
2238 utmp = make_rename_temp (utype, "SR");
2240 stmt = build_gimple_modify_stmt (utmp, fold_convert (utype, var));
2241 append_to_statement_list (stmt, &list);
2243 var = utmp;
2246 /* Perform sign extension, if required.
2247 ??? This should never be necessary. */
2248 if (!unsignedp)
2250 tree signbit = int_const_binop (LSHIFT_EXPR,
2251 build_int_cst_wide (utype, 1, 0),
2252 size_binop (MINUS_EXPR, width,
2253 bitsize_int (1)), 0);
2255 stmt = build_gimple_modify_stmt (utmp,
2256 fold_build2 (BIT_XOR_EXPR, utype,
2257 var, signbit));
2258 append_to_statement_list (stmt, &list);
2260 stmt = build_gimple_modify_stmt (utmp,
2261 fold_build2 (MINUS_EXPR, utype,
2262 utmp, signbit));
2263 append_to_statement_list (stmt, &list);
2265 var = utmp;
2268 /* Finally, move and convert to the destination. */
2269 if (!useless_type_conversion_p (TREE_TYPE (dst), TREE_TYPE (var)))
2271 if (INTEGRAL_TYPE_P (TREE_TYPE (dst)))
2272 var = fold_convert (TREE_TYPE (dst), var);
2273 else
2274 var = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (dst), var);
2276 /* If the destination is not a register the conversion needs
2277 to be a separate statement. */
2278 if (!is_gimple_reg (dst))
2280 dtmp = make_rename_temp (TREE_TYPE (dst), "SR");
2281 stmt = build_gimple_modify_stmt (dtmp, var);
2282 append_to_statement_list (stmt, &list);
2283 var = dtmp;
2286 stmt = build_gimple_modify_stmt (dst, var);
2287 append_to_statement_list (stmt, &list);
2289 return list;
2292 /* It was hoped that we could perform some type sanity checking
2293 here, but since front-ends can emit accesses of fields in types
2294 different from their nominal types and copy structures containing
2295 them as a whole, we'd have to handle such differences here.
2296 Since such accesses under different types require compatibility
2297 anyway, there's little point in making tests and/or adding
2298 conversions to ensure the types of src and dst are the same.
2299 So we just assume type differences at this point are ok.
2300 The only exception we make here are pointer types, which can be different
2301 in e.g. structurally equal, but non-identical RECORD_TYPEs. */
2302 if (POINTER_TYPE_P (TREE_TYPE (dst))
2303 && !useless_type_conversion_p (TREE_TYPE (dst), TREE_TYPE (src)))
2304 src = fold_convert (TREE_TYPE (dst), src);
2306 return build_gimple_modify_stmt (dst, src);
2309 /* BIT_FIELD_REFs must not be shared. sra_build_elt_assignment()
2310 takes care of assignments, but we must create copies for uses. */
2311 #define REPLDUP(t) (TREE_CODE (t) != BIT_FIELD_REF ? (t) : unshare_expr (t))
2313 /* Emit an assignment from SRC to DST, but if DST is a scalarizable
2314 BIT_FIELD_REF, turn it into bit operations. */
2316 static tree
2317 sra_build_bf_assignment (tree dst, tree src)
2319 tree var, type, utype, tmp, tmp2, tmp3;
2320 tree list, stmt;
2321 tree cst, cst2, mask;
2322 tree minshift, maxshift;
2324 if (TREE_CODE (dst) != BIT_FIELD_REF)
2325 return sra_build_assignment (dst, src);
2327 var = TREE_OPERAND (dst, 0);
2329 if (!scalar_bitfield_p (dst))
2330 return sra_build_assignment (REPLDUP (dst), src);
2332 list = NULL;
2334 cst = fold_convert (bitsizetype, TREE_OPERAND (dst, 2));
2335 cst2 = size_binop (PLUS_EXPR,
2336 fold_convert (bitsizetype, TREE_OPERAND (dst, 1)),
2337 cst);
2339 if (BYTES_BIG_ENDIAN)
2341 maxshift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), cst);
2342 minshift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), cst2);
2344 else
2346 maxshift = cst2;
2347 minshift = cst;
2350 type = TREE_TYPE (var);
2351 if (!INTEGRAL_TYPE_P (type))
2352 type = lang_hooks.types.type_for_size
2353 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (var))), 1);
2354 if (TYPE_UNSIGNED (type))
2355 utype = type;
2356 else
2357 utype = unsigned_type_for (type);
2359 mask = build_int_cst_wide (utype, 1, 0);
2360 if (TREE_INT_CST_LOW (maxshift) == TYPE_PRECISION (utype))
2361 cst = build_int_cst_wide (utype, 0, 0);
2362 else
2363 cst = int_const_binop (LSHIFT_EXPR, mask, maxshift, true);
2364 if (integer_zerop (minshift))
2365 cst2 = mask;
2366 else
2367 cst2 = int_const_binop (LSHIFT_EXPR, mask, minshift, true);
2368 mask = int_const_binop (MINUS_EXPR, cst, cst2, true);
2369 mask = fold_build1 (BIT_NOT_EXPR, utype, mask);
2371 if (TYPE_MAIN_VARIANT (utype) != TYPE_MAIN_VARIANT (TREE_TYPE (var))
2372 && !integer_zerop (mask))
2374 tmp = var;
2375 if (!is_gimple_variable (tmp))
2376 tmp = unshare_expr (var);
2378 tmp2 = make_rename_temp (utype, "SR");
2380 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
2381 stmt = build_gimple_modify_stmt (tmp2, fold_convert (utype, tmp));
2382 else
2383 stmt = build_gimple_modify_stmt (tmp2, fold_build1 (VIEW_CONVERT_EXPR,
2384 utype, tmp));
2385 append_to_statement_list (stmt, &list);
2387 else
2388 tmp2 = var;
2390 if (!integer_zerop (mask))
2392 tmp = make_rename_temp (utype, "SR");
2393 stmt = build_gimple_modify_stmt (tmp,
2394 fold_build2 (BIT_AND_EXPR, utype,
2395 tmp2, mask));
2396 append_to_statement_list (stmt, &list);
2398 else
2399 tmp = mask;
2401 if (is_gimple_reg (src) && INTEGRAL_TYPE_P (TREE_TYPE (src)))
2402 tmp2 = src;
2403 else if (INTEGRAL_TYPE_P (TREE_TYPE (src)))
2405 tmp2 = make_rename_temp (TREE_TYPE (src), "SR");
2406 stmt = sra_build_assignment (tmp2, src);
2407 append_to_statement_list (stmt, &list);
2409 else
2411 tmp2 = make_rename_temp
2412 (lang_hooks.types.type_for_size
2413 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (src))),
2414 1), "SR");
2415 stmt = sra_build_assignment (tmp2, fold_build1 (VIEW_CONVERT_EXPR,
2416 TREE_TYPE (tmp2), src));
2417 append_to_statement_list (stmt, &list);
2420 if (!TYPE_UNSIGNED (TREE_TYPE (tmp2)))
2422 tree ut = unsigned_type_for (TREE_TYPE (tmp2));
2423 tmp3 = make_rename_temp (ut, "SR");
2424 tmp2 = fold_convert (ut, tmp2);
2425 stmt = sra_build_assignment (tmp3, tmp2);
2426 append_to_statement_list (stmt, &list);
2428 tmp2 = fold_build1 (BIT_NOT_EXPR, utype, mask);
2429 tmp2 = int_const_binop (RSHIFT_EXPR, tmp2, minshift, true);
2430 tmp2 = fold_convert (ut, tmp2);
2431 tmp2 = fold_build2 (BIT_AND_EXPR, ut, tmp3, tmp2);
2433 if (tmp3 != tmp2)
2435 tmp3 = make_rename_temp (ut, "SR");
2436 stmt = sra_build_assignment (tmp3, tmp2);
2437 append_to_statement_list (stmt, &list);
2440 tmp2 = tmp3;
2443 if (TYPE_MAIN_VARIANT (TREE_TYPE (tmp2)) != TYPE_MAIN_VARIANT (utype))
2445 tmp3 = make_rename_temp (utype, "SR");
2446 tmp2 = fold_convert (utype, tmp2);
2447 stmt = sra_build_assignment (tmp3, tmp2);
2448 append_to_statement_list (stmt, &list);
2449 tmp2 = tmp3;
2452 if (!integer_zerop (minshift))
2454 tmp3 = make_rename_temp (utype, "SR");
2455 stmt = build_gimple_modify_stmt (tmp3,
2456 fold_build2 (LSHIFT_EXPR, utype,
2457 tmp2, minshift));
2458 append_to_statement_list (stmt, &list);
2459 tmp2 = tmp3;
2462 if (utype != TREE_TYPE (var))
2463 tmp3 = make_rename_temp (utype, "SR");
2464 else
2465 tmp3 = var;
2466 stmt = build_gimple_modify_stmt (tmp3,
2467 fold_build2 (BIT_IOR_EXPR, utype,
2468 tmp, tmp2));
2469 append_to_statement_list (stmt, &list);
2471 if (tmp3 != var)
2473 if (TREE_TYPE (var) == type)
2474 stmt = build_gimple_modify_stmt (var,
2475 fold_convert (type, tmp3));
2476 else
2477 stmt = build_gimple_modify_stmt (var,
2478 fold_build1 (VIEW_CONVERT_EXPR,
2479 TREE_TYPE (var), tmp3));
2480 append_to_statement_list (stmt, &list);
2483 return list;
2486 /* Expand an assignment of SRC to the scalarized representation of
2487 ELT. If it is a field group, try to widen the assignment to cover
2488 the full variable. */
2490 static tree
2491 sra_build_elt_assignment (struct sra_elt *elt, tree src)
2493 tree dst = elt->replacement;
2494 tree var, tmp, cst, cst2, list, stmt;
2496 if (TREE_CODE (dst) != BIT_FIELD_REF
2497 || !elt->in_bitfld_block)
2498 return sra_build_assignment (REPLDUP (dst), src);
2500 var = TREE_OPERAND (dst, 0);
2502 /* Try to widen the assignment to the entire variable.
2503 We need the source to be a BIT_FIELD_REF as well, such that, for
2504 BIT_FIELD_REF<d,sz,dp> = BIT_FIELD_REF<s,sz,sp>,
2505 by design, conditions are met such that we can turn it into
2506 d = BIT_FIELD_REF<s,dw,sp-dp>. */
2507 if (elt->in_bitfld_block == 2
2508 && TREE_CODE (src) == BIT_FIELD_REF)
2510 tmp = src;
2511 cst = TYPE_SIZE (TREE_TYPE (var));
2512 cst2 = size_binop (MINUS_EXPR, TREE_OPERAND (src, 2),
2513 TREE_OPERAND (dst, 2));
2515 src = TREE_OPERAND (src, 0);
2517 /* Avoid full-width bit-fields. */
2518 if (integer_zerop (cst2)
2519 && tree_int_cst_equal (cst, TYPE_SIZE (TREE_TYPE (src))))
2521 if (INTEGRAL_TYPE_P (TREE_TYPE (src))
2522 && !TYPE_UNSIGNED (TREE_TYPE (src)))
2523 src = fold_convert (unsigned_type_for (TREE_TYPE (src)), src);
2525 /* If a single conversion won't do, we'll need a statement
2526 list. */
2527 if (TYPE_MAIN_VARIANT (TREE_TYPE (var))
2528 != TYPE_MAIN_VARIANT (TREE_TYPE (src)))
2530 list = NULL;
2532 if (!INTEGRAL_TYPE_P (TREE_TYPE (src)))
2533 src = fold_build1 (VIEW_CONVERT_EXPR,
2534 lang_hooks.types.type_for_size
2535 (TREE_INT_CST_LOW
2536 (TYPE_SIZE (TREE_TYPE (src))),
2537 1), src);
2538 gcc_assert (TYPE_UNSIGNED (TREE_TYPE (src)));
2540 tmp = make_rename_temp (TREE_TYPE (src), "SR");
2541 stmt = build_gimple_modify_stmt (tmp, src);
2542 append_to_statement_list (stmt, &list);
2544 stmt = sra_build_assignment (var,
2545 fold_convert (TREE_TYPE (var),
2546 tmp));
2547 append_to_statement_list (stmt, &list);
2549 return list;
2552 src = fold_convert (TREE_TYPE (var), src);
2554 else
2556 src = fold_convert (TREE_TYPE (var), tmp);
2559 return sra_build_assignment (var, src);
2562 return sra_build_bf_assignment (dst, src);
2565 /* Generate a set of assignment statements in *LIST_P to copy all
2566 instantiated elements under ELT to or from the equivalent structure
2567 rooted at EXPR. COPY_OUT controls the direction of the copy, with
2568 true meaning to copy out of EXPR into ELT. */
2570 static void
2571 generate_copy_inout (struct sra_elt *elt, bool copy_out, tree expr,
2572 tree *list_p)
2574 struct sra_elt *c;
2575 tree t;
2577 if (!copy_out && TREE_CODE (expr) == SSA_NAME
2578 && TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2580 tree r, i;
2582 c = lookup_element (elt, integer_zero_node, NULL, NO_INSERT);
2583 r = c->replacement;
2584 c = lookup_element (elt, integer_one_node, NULL, NO_INSERT);
2585 i = c->replacement;
2587 t = build2 (COMPLEX_EXPR, elt->type, r, i);
2588 t = sra_build_bf_assignment (expr, t);
2589 SSA_NAME_DEF_STMT (expr) = t;
2590 append_to_statement_list (t, list_p);
2592 else if (elt->replacement)
2594 if (copy_out)
2595 t = sra_build_elt_assignment (elt, expr);
2596 else
2597 t = sra_build_bf_assignment (expr, REPLDUP (elt->replacement));
2598 append_to_statement_list (t, list_p);
2600 else
2602 FOR_EACH_ACTUAL_CHILD (c, elt)
2604 t = generate_one_element_ref (c, unshare_expr (expr));
2605 generate_copy_inout (c, copy_out, t, list_p);
2610 /* Generate a set of assignment statements in *LIST_P to copy all instantiated
2611 elements under SRC to their counterparts under DST. There must be a 1-1
2612 correspondence of instantiated elements. */
2614 static void
2615 generate_element_copy (struct sra_elt *dst, struct sra_elt *src, tree *list_p)
2617 struct sra_elt *dc, *sc;
2619 FOR_EACH_ACTUAL_CHILD (dc, dst)
2621 sc = lookup_element (src, dc->element, NULL, NO_INSERT);
2622 if (!sc && dc->in_bitfld_block == 2)
2624 struct sra_elt *dcs;
2626 FOR_EACH_ACTUAL_CHILD (dcs, dc)
2628 sc = lookup_element (src, dcs->element, NULL, NO_INSERT);
2629 gcc_assert (sc);
2630 generate_element_copy (dcs, sc, list_p);
2633 continue;
2636 /* If DST and SRC are structs with the same elements, but do not have
2637 the same TYPE_MAIN_VARIANT, then lookup of DST FIELD_DECL in SRC
2638 will fail. Try harder by finding the corresponding FIELD_DECL
2639 in SRC. */
2640 if (!sc)
2642 tree f;
2644 gcc_assert (useless_type_conversion_p (dst->type, src->type));
2645 gcc_assert (TREE_CODE (dc->element) == FIELD_DECL);
2646 for (f = TYPE_FIELDS (src->type); f ; f = TREE_CHAIN (f))
2647 if (simple_cst_equal (DECL_FIELD_OFFSET (f),
2648 DECL_FIELD_OFFSET (dc->element)) > 0
2649 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (f),
2650 DECL_FIELD_BIT_OFFSET (dc->element)) > 0
2651 && simple_cst_equal (DECL_SIZE (f),
2652 DECL_SIZE (dc->element)) > 0
2653 && (useless_type_conversion_p (TREE_TYPE (dc->element),
2654 TREE_TYPE (f))
2655 || (POINTER_TYPE_P (TREE_TYPE (dc->element))
2656 && POINTER_TYPE_P (TREE_TYPE (f)))))
2657 break;
2658 gcc_assert (f != NULL_TREE);
2659 sc = lookup_element (src, f, NULL, NO_INSERT);
2662 generate_element_copy (dc, sc, list_p);
2665 if (dst->replacement)
2667 tree t;
2669 gcc_assert (src->replacement);
2671 t = sra_build_elt_assignment (dst, REPLDUP (src->replacement));
2672 append_to_statement_list (t, list_p);
2676 /* Generate a set of assignment statements in *LIST_P to zero all instantiated
2677 elements under ELT. In addition, do not assign to elements that have been
2678 marked VISITED but do reset the visited flag; this allows easy coordination
2679 with generate_element_init. */
2681 static void
2682 generate_element_zero (struct sra_elt *elt, tree *list_p)
2684 struct sra_elt *c;
2686 if (elt->visited)
2688 elt->visited = false;
2689 return;
2692 if (!elt->in_bitfld_block)
2693 FOR_EACH_ACTUAL_CHILD (c, elt)
2694 generate_element_zero (c, list_p);
2696 if (elt->replacement)
2698 tree t;
2700 gcc_assert (elt->is_scalar);
2701 t = fold_convert (elt->type, integer_zero_node);
2703 t = sra_build_elt_assignment (elt, t);
2704 append_to_statement_list (t, list_p);
2708 /* Generate an assignment VAR = INIT, where INIT may need gimplification.
2709 Add the result to *LIST_P. */
2711 static void
2712 generate_one_element_init (struct sra_elt *elt, tree init, tree *list_p)
2714 /* The replacement can be almost arbitrarily complex. Gimplify. */
2715 tree stmt = sra_build_elt_assignment (elt, init);
2716 gimplify_and_add (stmt, list_p);
2719 /* Generate a set of assignment statements in *LIST_P to set all instantiated
2720 elements under ELT with the contents of the initializer INIT. In addition,
2721 mark all assigned elements VISITED; this allows easy coordination with
2722 generate_element_zero. Return false if we found a case we couldn't
2723 handle. */
2725 static bool
2726 generate_element_init_1 (struct sra_elt *elt, tree init, tree *list_p)
2728 bool result = true;
2729 enum tree_code init_code;
2730 struct sra_elt *sub;
2731 tree t;
2732 unsigned HOST_WIDE_INT idx;
2733 tree value, purpose;
2735 /* We can be passed DECL_INITIAL of a static variable. It might have a
2736 conversion, which we strip off here. */
2737 STRIP_USELESS_TYPE_CONVERSION (init);
2738 init_code = TREE_CODE (init);
2740 if (elt->is_scalar)
2742 if (elt->replacement)
2744 generate_one_element_init (elt, init, list_p);
2745 elt->visited = true;
2747 return result;
2750 switch (init_code)
2752 case COMPLEX_CST:
2753 case COMPLEX_EXPR:
2754 FOR_EACH_ACTUAL_CHILD (sub, elt)
2756 if (sub->element == integer_zero_node)
2757 t = (init_code == COMPLEX_EXPR
2758 ? TREE_OPERAND (init, 0) : TREE_REALPART (init));
2759 else
2760 t = (init_code == COMPLEX_EXPR
2761 ? TREE_OPERAND (init, 1) : TREE_IMAGPART (init));
2762 result &= generate_element_init_1 (sub, t, list_p);
2764 break;
2766 case CONSTRUCTOR:
2767 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, purpose, value)
2769 if (TREE_CODE (purpose) == RANGE_EXPR)
2771 tree lower = TREE_OPERAND (purpose, 0);
2772 tree upper = TREE_OPERAND (purpose, 1);
2774 while (1)
2776 sub = lookup_element (elt, lower, NULL, NO_INSERT);
2777 if (sub != NULL)
2778 result &= generate_element_init_1 (sub, value, list_p);
2779 if (tree_int_cst_equal (lower, upper))
2780 break;
2781 lower = int_const_binop (PLUS_EXPR, lower,
2782 integer_one_node, true);
2785 else
2787 sub = lookup_element (elt, purpose, NULL, NO_INSERT);
2788 if (sub != NULL)
2789 result &= generate_element_init_1 (sub, value, list_p);
2792 break;
2794 default:
2795 elt->visited = true;
2796 result = false;
2799 return result;
2802 /* A wrapper function for generate_element_init_1 that handles cleanup after
2803 gimplification. */
2805 static bool
2806 generate_element_init (struct sra_elt *elt, tree init, tree *list_p)
2808 bool ret;
2810 push_gimplify_context ();
2811 ret = generate_element_init_1 (elt, init, list_p);
2812 pop_gimplify_context (NULL);
2814 /* The replacement can expose previously unreferenced variables. */
2815 if (ret && *list_p)
2817 tree_stmt_iterator i;
2819 for (i = tsi_start (*list_p); !tsi_end_p (i); tsi_next (&i))
2820 find_new_referenced_vars (tsi_stmt_ptr (i));
2823 return ret;
2826 /* Insert STMT on all the outgoing edges out of BB. Note that if BB
2827 has more than one edge, STMT will be replicated for each edge. Also,
2828 abnormal edges will be ignored. */
2830 void
2831 insert_edge_copies (tree stmt, basic_block bb)
2833 edge e;
2834 edge_iterator ei;
2835 bool first_copy;
2837 first_copy = true;
2838 FOR_EACH_EDGE (e, ei, bb->succs)
2840 /* We don't need to insert copies on abnormal edges. The
2841 value of the scalar replacement is not guaranteed to
2842 be valid through an abnormal edge. */
2843 if (!(e->flags & EDGE_ABNORMAL))
2845 if (first_copy)
2847 bsi_insert_on_edge (e, stmt);
2848 first_copy = false;
2850 else
2851 bsi_insert_on_edge (e, unsave_expr_now (stmt));
2856 /* Helper function to insert LIST before BSI, and set up line number info. */
2858 void
2859 sra_insert_before (block_stmt_iterator *bsi, tree list)
2861 tree stmt = bsi_stmt (*bsi);
2863 if (EXPR_HAS_LOCATION (stmt))
2864 annotate_all_with_locus (&list, EXPR_LOCATION (stmt));
2865 bsi_insert_before (bsi, list, BSI_SAME_STMT);
2868 /* Similarly, but insert after BSI. Handles insertion onto edges as well. */
2870 void
2871 sra_insert_after (block_stmt_iterator *bsi, tree list)
2873 tree stmt = bsi_stmt (*bsi);
2875 if (EXPR_HAS_LOCATION (stmt))
2876 annotate_all_with_locus (&list, EXPR_LOCATION (stmt));
2878 if (stmt_ends_bb_p (stmt))
2879 insert_edge_copies (list, bsi->bb);
2880 else
2881 bsi_insert_after (bsi, list, BSI_SAME_STMT);
2884 /* Similarly, but replace the statement at BSI. */
2886 static void
2887 sra_replace (block_stmt_iterator *bsi, tree list)
2889 sra_insert_before (bsi, list);
2890 bsi_remove (bsi, false);
2891 if (bsi_end_p (*bsi))
2892 *bsi = bsi_last (bsi->bb);
2893 else
2894 bsi_prev (bsi);
2897 /* Data structure that bitfield_overlaps_p fills in with information
2898 about the element passed in and how much of it overlaps with the
2899 bit-range passed it to. */
2901 struct bitfield_overlap_info
2903 /* The bit-length of an element. */
2904 tree field_len;
2906 /* The bit-position of the element in its parent. */
2907 tree field_pos;
2909 /* The number of bits of the element that overlap with the incoming
2910 bit range. */
2911 tree overlap_len;
2913 /* The first bit of the element that overlaps with the incoming bit
2914 range. */
2915 tree overlap_pos;
2918 /* Return true if a BIT_FIELD_REF<(FLD->parent), BLEN, BPOS>
2919 expression (referenced as BF below) accesses any of the bits in FLD,
2920 false if it doesn't. If DATA is non-null, its field_len and
2921 field_pos are filled in such that BIT_FIELD_REF<(FLD->parent),
2922 field_len, field_pos> (referenced as BFLD below) represents the
2923 entire field FLD->element, and BIT_FIELD_REF<BFLD, overlap_len,
2924 overlap_pos> represents the portion of the entire field that
2925 overlaps with BF. */
2927 static bool
2928 bitfield_overlaps_p (tree blen, tree bpos, struct sra_elt *fld,
2929 struct bitfield_overlap_info *data)
2931 tree flen, fpos;
2932 bool ret;
2934 if (TREE_CODE (fld->element) == FIELD_DECL)
2936 flen = fold_convert (bitsizetype, DECL_SIZE (fld->element));
2937 fpos = fold_convert (bitsizetype, DECL_FIELD_OFFSET (fld->element));
2938 fpos = size_binop (MULT_EXPR, fpos, bitsize_int (BITS_PER_UNIT));
2939 fpos = size_binop (PLUS_EXPR, fpos, DECL_FIELD_BIT_OFFSET (fld->element));
2941 else if (TREE_CODE (fld->element) == BIT_FIELD_REF)
2943 flen = fold_convert (bitsizetype, TREE_OPERAND (fld->element, 1));
2944 fpos = fold_convert (bitsizetype, TREE_OPERAND (fld->element, 2));
2946 else if (TREE_CODE (fld->element) == INTEGER_CST)
2948 flen = fold_convert (bitsizetype, TYPE_SIZE (fld->type));
2949 fpos = fold_convert (bitsizetype, fld->element);
2950 fpos = size_binop (MULT_EXPR, flen, fpos);
2952 else
2953 gcc_unreachable ();
2955 gcc_assert (host_integerp (blen, 1)
2956 && host_integerp (bpos, 1)
2957 && host_integerp (flen, 1)
2958 && host_integerp (fpos, 1));
2960 ret = ((!tree_int_cst_lt (fpos, bpos)
2961 && tree_int_cst_lt (size_binop (MINUS_EXPR, fpos, bpos),
2962 blen))
2963 || (!tree_int_cst_lt (bpos, fpos)
2964 && tree_int_cst_lt (size_binop (MINUS_EXPR, bpos, fpos),
2965 flen)));
2967 if (!ret)
2968 return ret;
2970 if (data)
2972 tree bend, fend;
2974 data->field_len = flen;
2975 data->field_pos = fpos;
2977 fend = size_binop (PLUS_EXPR, fpos, flen);
2978 bend = size_binop (PLUS_EXPR, bpos, blen);
2980 if (tree_int_cst_lt (bend, fend))
2981 data->overlap_len = size_binop (MINUS_EXPR, bend, fpos);
2982 else
2983 data->overlap_len = NULL;
2985 if (tree_int_cst_lt (fpos, bpos))
2987 data->overlap_pos = size_binop (MINUS_EXPR, bpos, fpos);
2988 data->overlap_len = size_binop (MINUS_EXPR,
2989 data->overlap_len
2990 ? data->overlap_len
2991 : data->field_len,
2992 data->overlap_pos);
2994 else
2995 data->overlap_pos = NULL;
2998 return ret;
3001 /* Add to LISTP a sequence of statements that copies BLEN bits between
3002 VAR and the scalarized elements of ELT, starting a bit VPOS of VAR
3003 and at bit BPOS of ELT. The direction of the copy is given by
3004 TO_VAR. */
3006 static void
3007 sra_explode_bitfield_assignment (tree var, tree vpos, bool to_var,
3008 tree *listp, tree blen, tree bpos,
3009 struct sra_elt *elt)
3011 struct sra_elt *fld;
3012 struct bitfield_overlap_info flp;
3014 FOR_EACH_ACTUAL_CHILD (fld, elt)
3016 tree flen, fpos;
3018 if (!bitfield_overlaps_p (blen, bpos, fld, &flp))
3019 continue;
3021 flen = flp.overlap_len ? flp.overlap_len : flp.field_len;
3022 fpos = flp.overlap_pos ? flp.overlap_pos : bitsize_int (0);
3024 if (fld->replacement)
3026 tree infld, invar, st, type;
3028 infld = fld->replacement;
3030 type = TREE_TYPE (infld);
3031 if (TYPE_PRECISION (type) != TREE_INT_CST_LOW (flen))
3032 type = lang_hooks.types.type_for_size (TREE_INT_CST_LOW (flen), 1);
3033 else
3034 type = unsigned_type_for (type);
3036 if (TREE_CODE (infld) == BIT_FIELD_REF)
3038 fpos = size_binop (PLUS_EXPR, fpos, TREE_OPERAND (infld, 2));
3039 infld = TREE_OPERAND (infld, 0);
3041 else if (BYTES_BIG_ENDIAN && DECL_P (fld->element)
3042 && !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (infld)),
3043 DECL_SIZE (fld->element)))
3045 fpos = size_binop (PLUS_EXPR, fpos,
3046 TYPE_SIZE (TREE_TYPE (infld)));
3047 fpos = size_binop (MINUS_EXPR, fpos,
3048 DECL_SIZE (fld->element));
3051 infld = fold_build3 (BIT_FIELD_REF, type, infld, flen, fpos);
3053 invar = size_binop (MINUS_EXPR, flp.field_pos, bpos);
3054 if (flp.overlap_pos)
3055 invar = size_binop (PLUS_EXPR, invar, flp.overlap_pos);
3056 invar = size_binop (PLUS_EXPR, invar, vpos);
3058 invar = fold_build3 (BIT_FIELD_REF, type, var, flen, invar);
3060 if (to_var)
3061 st = sra_build_bf_assignment (invar, infld);
3062 else
3063 st = sra_build_bf_assignment (infld, invar);
3065 append_to_statement_list (st, listp);
3067 else
3069 tree sub = size_binop (MINUS_EXPR, flp.field_pos, bpos);
3070 sub = size_binop (PLUS_EXPR, vpos, sub);
3071 if (flp.overlap_pos)
3072 sub = size_binop (PLUS_EXPR, sub, flp.overlap_pos);
3074 sra_explode_bitfield_assignment (var, sub, to_var, listp,
3075 flen, fpos, fld);
3080 /* Add to LISTBEFOREP statements that copy scalarized members of ELT
3081 that overlap with BIT_FIELD_REF<(ELT->element), BLEN, BPOS> back
3082 into the full variable, and to LISTAFTERP, if non-NULL, statements
3083 that copy the (presumably modified) overlapping portions of the
3084 full variable back to the scalarized variables. */
3086 static void
3087 sra_sync_for_bitfield_assignment (tree *listbeforep, tree *listafterp,
3088 tree blen, tree bpos,
3089 struct sra_elt *elt)
3091 struct sra_elt *fld;
3092 struct bitfield_overlap_info flp;
3094 FOR_EACH_ACTUAL_CHILD (fld, elt)
3095 if (bitfield_overlaps_p (blen, bpos, fld, &flp))
3097 if (fld->replacement || (!flp.overlap_len && !flp.overlap_pos))
3099 generate_copy_inout (fld, false, generate_element_ref (fld),
3100 listbeforep);
3101 mark_no_warning (fld);
3102 if (listafterp)
3103 generate_copy_inout (fld, true, generate_element_ref (fld),
3104 listafterp);
3106 else
3108 tree flen = flp.overlap_len ? flp.overlap_len : flp.field_len;
3109 tree fpos = flp.overlap_pos ? flp.overlap_pos : bitsize_int (0);
3111 sra_sync_for_bitfield_assignment (listbeforep, listafterp,
3112 flen, fpos, fld);
3117 /* Scalarize a USE. To recap, this is either a simple reference to ELT,
3118 if elt is scalar, or some occurrence of ELT that requires a complete
3119 aggregate. IS_OUTPUT is true if ELT is being modified. */
3121 static void
3122 scalarize_use (struct sra_elt *elt, tree *expr_p, block_stmt_iterator *bsi,
3123 bool is_output, bool use_all)
3125 tree stmt = bsi_stmt (*bsi);
3126 tree bfexpr;
3128 if (elt->replacement)
3130 tree replacement = elt->replacement;
3132 /* If we have a replacement, then updating the reference is as
3133 simple as modifying the existing statement in place. */
3134 if (is_output
3135 && TREE_CODE (elt->replacement) == BIT_FIELD_REF
3136 && is_gimple_reg (TREE_OPERAND (elt->replacement, 0))
3137 && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3138 && &GIMPLE_STMT_OPERAND (stmt, 0) == expr_p)
3140 tree newstmt = sra_build_elt_assignment
3141 (elt, GIMPLE_STMT_OPERAND (stmt, 1));
3142 if (TREE_CODE (newstmt) != STATEMENT_LIST)
3144 tree list = NULL;
3145 append_to_statement_list (newstmt, &list);
3146 newstmt = list;
3148 sra_replace (bsi, newstmt);
3149 return;
3151 else if (!is_output
3152 && TREE_CODE (elt->replacement) == BIT_FIELD_REF
3153 && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3154 && &GIMPLE_STMT_OPERAND (stmt, 1) == expr_p)
3156 tree tmp = make_rename_temp
3157 (TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 0)), "SR");
3158 tree newstmt = sra_build_assignment (tmp, REPLDUP (elt->replacement));
3160 if (TREE_CODE (newstmt) != STATEMENT_LIST)
3162 tree list = NULL;
3163 append_to_statement_list (newstmt, &list);
3164 newstmt = list;
3166 sra_insert_before (bsi, newstmt);
3167 replacement = tmp;
3169 if (is_output)
3170 mark_all_v_defs (stmt);
3171 *expr_p = REPLDUP (replacement);
3172 update_stmt (stmt);
3174 else if (use_all && is_output
3175 && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3176 && TREE_CODE (bfexpr
3177 = GIMPLE_STMT_OPERAND (stmt, 0)) == BIT_FIELD_REF
3178 && &TREE_OPERAND (bfexpr, 0) == expr_p
3179 && INTEGRAL_TYPE_P (TREE_TYPE (bfexpr))
3180 && TREE_CODE (TREE_TYPE (*expr_p)) == RECORD_TYPE)
3182 tree listbefore = NULL, listafter = NULL;
3183 tree blen = fold_convert (bitsizetype, TREE_OPERAND (bfexpr, 1));
3184 tree bpos = fold_convert (bitsizetype, TREE_OPERAND (bfexpr, 2));
3185 bool update = false;
3187 if (!elt->use_block_copy)
3189 tree type = TREE_TYPE (bfexpr);
3190 tree var = make_rename_temp (type, "SR"), tmp, st, vpos;
3192 GIMPLE_STMT_OPERAND (stmt, 0) = var;
3193 update = true;
3195 if (!TYPE_UNSIGNED (type))
3197 type = unsigned_type_for (type);
3198 tmp = make_rename_temp (type, "SR");
3199 st = build_gimple_modify_stmt (tmp,
3200 fold_convert (type, var));
3201 append_to_statement_list (st, &listafter);
3202 var = tmp;
3205 /* If VAR is wider than BLEN bits, it is padded at the
3206 most-significant end. We want to set VPOS such that
3207 <BIT_FIELD_REF VAR BLEN VPOS> would refer to the
3208 least-significant BLEN bits of VAR. */
3209 if (BYTES_BIG_ENDIAN)
3210 vpos = size_binop (MINUS_EXPR, TYPE_SIZE (type), blen);
3211 else
3212 vpos = bitsize_int (0);
3213 sra_explode_bitfield_assignment
3214 (var, vpos, false, &listafter, blen, bpos, elt);
3216 else
3217 sra_sync_for_bitfield_assignment
3218 (&listbefore, &listafter, blen, bpos, elt);
3220 if (listbefore)
3222 mark_all_v_defs (listbefore);
3223 sra_insert_before (bsi, listbefore);
3225 if (listafter)
3227 mark_all_v_defs (listafter);
3228 sra_insert_after (bsi, listafter);
3231 if (update)
3232 update_stmt (stmt);
3234 else if (use_all && !is_output
3235 && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3236 && TREE_CODE (bfexpr
3237 = GIMPLE_STMT_OPERAND (stmt, 1)) == BIT_FIELD_REF
3238 && &TREE_OPERAND (GIMPLE_STMT_OPERAND (stmt, 1), 0) == expr_p
3239 && INTEGRAL_TYPE_P (TREE_TYPE (bfexpr))
3240 && TREE_CODE (TREE_TYPE (*expr_p)) == RECORD_TYPE)
3242 tree list = NULL;
3243 tree blen = fold_convert (bitsizetype, TREE_OPERAND (bfexpr, 1));
3244 tree bpos = fold_convert (bitsizetype, TREE_OPERAND (bfexpr, 2));
3245 bool update = false;
3247 if (!elt->use_block_copy)
3249 tree type = TREE_TYPE (bfexpr);
3250 tree var, vpos;
3252 if (!TYPE_UNSIGNED (type))
3253 type = unsigned_type_for (type);
3255 var = make_rename_temp (type, "SR");
3257 append_to_statement_list (build_gimple_modify_stmt
3258 (var, build_int_cst_wide (type, 0, 0)),
3259 &list);
3261 /* If VAR is wider than BLEN bits, it is padded at the
3262 most-significant end. We want to set VPOS such that
3263 <BIT_FIELD_REF VAR BLEN VPOS> would refer to the
3264 least-significant BLEN bits of VAR. */
3265 if (BYTES_BIG_ENDIAN)
3266 vpos = size_binop (MINUS_EXPR, TYPE_SIZE (type), blen);
3267 else
3268 vpos = bitsize_int (0);
3269 sra_explode_bitfield_assignment
3270 (var, vpos, true, &list, blen, bpos, elt);
3272 GIMPLE_STMT_OPERAND (stmt, 1) = var;
3273 update = true;
3275 else
3276 sra_sync_for_bitfield_assignment
3277 (&list, NULL, blen, bpos, elt);
3279 if (list)
3281 mark_all_v_defs (list);
3282 sra_insert_before (bsi, list);
3285 if (update)
3286 update_stmt (stmt);
3288 else
3290 tree list = NULL;
3292 /* Otherwise we need some copies. If ELT is being read, then we
3293 want to store all (modified) sub-elements back into the
3294 structure before the reference takes place. If ELT is being
3295 written, then we want to load the changed values back into
3296 our shadow variables. */
3297 /* ??? We don't check modified for reads, we just always write all of
3298 the values. We should be able to record the SSA number of the VOP
3299 for which the values were last read. If that number matches the
3300 SSA number of the VOP in the current statement, then we needn't
3301 emit an assignment. This would also eliminate double writes when
3302 a structure is passed as more than one argument to a function call.
3303 This optimization would be most effective if sra_walk_function
3304 processed the blocks in dominator order. */
3306 generate_copy_inout (elt, is_output, generate_element_ref (elt), &list);
3307 if (list == NULL)
3308 return;
3309 mark_all_v_defs (list);
3310 if (is_output)
3311 sra_insert_after (bsi, list);
3312 else
3314 sra_insert_before (bsi, list);
3315 if (use_all)
3316 mark_no_warning (elt);
3321 /* Scalarize a COPY. To recap, this is an assignment statement between
3322 two scalarizable references, LHS_ELT and RHS_ELT. */
3324 static void
3325 scalarize_copy (struct sra_elt *lhs_elt, struct sra_elt *rhs_elt,
3326 block_stmt_iterator *bsi)
3328 tree list, stmt;
3330 if (lhs_elt->replacement && rhs_elt->replacement)
3332 /* If we have two scalar operands, modify the existing statement. */
3333 stmt = bsi_stmt (*bsi);
3335 /* See the commentary in sra_walk_function concerning
3336 RETURN_EXPR, and why we should never see one here. */
3337 gcc_assert (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT);
3339 GIMPLE_STMT_OPERAND (stmt, 0) = lhs_elt->replacement;
3340 GIMPLE_STMT_OPERAND (stmt, 1) = REPLDUP (rhs_elt->replacement);
3341 update_stmt (stmt);
3343 else if (lhs_elt->use_block_copy || rhs_elt->use_block_copy)
3345 /* If either side requires a block copy, then sync the RHS back
3346 to the original structure, leave the original assignment
3347 statement (which will perform the block copy), then load the
3348 LHS values out of its now-updated original structure. */
3349 /* ??? Could perform a modified pair-wise element copy. That
3350 would at least allow those elements that are instantiated in
3351 both structures to be optimized well. */
3353 list = NULL;
3354 generate_copy_inout (rhs_elt, false,
3355 generate_element_ref (rhs_elt), &list);
3356 if (list)
3358 mark_all_v_defs (list);
3359 sra_insert_before (bsi, list);
3362 list = NULL;
3363 generate_copy_inout (lhs_elt, true,
3364 generate_element_ref (lhs_elt), &list);
3365 if (list)
3367 mark_all_v_defs (list);
3368 sra_insert_after (bsi, list);
3371 else
3373 /* Otherwise both sides must be fully instantiated. In which
3374 case perform pair-wise element assignments and replace the
3375 original block copy statement. */
3377 stmt = bsi_stmt (*bsi);
3378 mark_all_v_defs (stmt);
3380 list = NULL;
3381 generate_element_copy (lhs_elt, rhs_elt, &list);
3382 gcc_assert (list);
3383 mark_all_v_defs (list);
3384 sra_replace (bsi, list);
3388 /* Scalarize an INIT. To recap, this is an assignment to a scalarizable
3389 reference from some form of constructor: CONSTRUCTOR, COMPLEX_CST or
3390 COMPLEX_EXPR. If RHS is NULL, it should be treated as an empty
3391 CONSTRUCTOR. */
3393 static void
3394 scalarize_init (struct sra_elt *lhs_elt, tree rhs, block_stmt_iterator *bsi)
3396 bool result = true;
3397 tree list = NULL, init_list = NULL;
3399 /* Generate initialization statements for all members extant in the RHS. */
3400 if (rhs)
3402 /* Unshare the expression just in case this is from a decl's initial. */
3403 rhs = unshare_expr (rhs);
3404 result = generate_element_init (lhs_elt, rhs, &init_list);
3407 /* CONSTRUCTOR is defined such that any member not mentioned is assigned
3408 a zero value. Initialize the rest of the instantiated elements. */
3409 generate_element_zero (lhs_elt, &list);
3410 append_to_statement_list (init_list, &list);
3412 if (!result)
3414 /* If we failed to convert the entire initializer, then we must
3415 leave the structure assignment in place and must load values
3416 from the structure into the slots for which we did not find
3417 constants. The easiest way to do this is to generate a complete
3418 copy-out, and then follow that with the constant assignments
3419 that we were able to build. DCE will clean things up. */
3420 tree list0 = NULL;
3421 generate_copy_inout (lhs_elt, true, generate_element_ref (lhs_elt),
3422 &list0);
3423 append_to_statement_list (list, &list0);
3424 list = list0;
3427 if (lhs_elt->use_block_copy || !result)
3429 /* Since LHS is not fully instantiated, we must leave the structure
3430 assignment in place. Treating this case differently from a USE
3431 exposes constants to later optimizations. */
3432 if (list)
3434 mark_all_v_defs (list);
3435 sra_insert_after (bsi, list);
3438 else
3440 /* The LHS is fully instantiated. The list of initializations
3441 replaces the original structure assignment. */
3442 gcc_assert (list);
3443 mark_all_v_defs (bsi_stmt (*bsi));
3444 mark_all_v_defs (list);
3445 sra_replace (bsi, list);
3449 /* A subroutine of scalarize_ldst called via walk_tree. Set TREE_NO_TRAP
3450 on all INDIRECT_REFs. */
3452 static tree
3453 mark_notrap (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3455 tree t = *tp;
3457 if (TREE_CODE (t) == INDIRECT_REF)
3459 TREE_THIS_NOTRAP (t) = 1;
3460 *walk_subtrees = 0;
3462 else if (IS_TYPE_OR_DECL_P (t))
3463 *walk_subtrees = 0;
3465 return NULL;
3468 /* Scalarize a LDST. To recap, this is an assignment between one scalarizable
3469 reference ELT and one non-scalarizable reference OTHER. IS_OUTPUT is true
3470 if ELT is on the left-hand side. */
3472 static void
3473 scalarize_ldst (struct sra_elt *elt, tree other,
3474 block_stmt_iterator *bsi, bool is_output)
3476 /* Shouldn't have gotten called for a scalar. */
3477 gcc_assert (!elt->replacement);
3479 if (elt->use_block_copy)
3481 /* Since ELT is not fully instantiated, we have to leave the
3482 block copy in place. Treat this as a USE. */
3483 scalarize_use (elt, NULL, bsi, is_output, false);
3485 else
3487 /* The interesting case is when ELT is fully instantiated. In this
3488 case we can have each element stored/loaded directly to/from the
3489 corresponding slot in OTHER. This avoids a block copy. */
3491 tree list = NULL, stmt = bsi_stmt (*bsi);
3493 mark_all_v_defs (stmt);
3494 generate_copy_inout (elt, is_output, other, &list);
3495 gcc_assert (list);
3496 mark_all_v_defs (list);
3498 /* Preserve EH semantics. */
3499 if (stmt_ends_bb_p (stmt))
3501 tree_stmt_iterator tsi;
3502 tree first, blist = NULL;
3503 bool thr = tree_could_throw_p (stmt);
3505 /* If the last statement of this BB created an EH edge
3506 before scalarization, we have to locate the first
3507 statement that can throw in the new statement list and
3508 use that as the last statement of this BB, such that EH
3509 semantics is preserved. All statements up to this one
3510 are added to the same BB. All other statements in the
3511 list will be added to normal outgoing edges of the same
3512 BB. If they access any memory, it's the same memory, so
3513 we can assume they won't throw. */
3514 tsi = tsi_start (list);
3515 for (first = tsi_stmt (tsi);
3516 thr && !tsi_end_p (tsi) && !tree_could_throw_p (first);
3517 first = tsi_stmt (tsi))
3519 tsi_delink (&tsi);
3520 append_to_statement_list (first, &blist);
3523 /* Extract the first remaining statement from LIST, this is
3524 the EH statement if there is one. */
3525 tsi_delink (&tsi);
3527 if (blist)
3528 sra_insert_before (bsi, blist);
3530 /* Replace the old statement with this new representative. */
3531 bsi_replace (bsi, first, true);
3533 if (!tsi_end_p (tsi))
3535 /* If any reference would trap, then they all would. And more
3536 to the point, the first would. Therefore none of the rest
3537 will trap since the first didn't. Indicate this by
3538 iterating over the remaining statements and set
3539 TREE_THIS_NOTRAP in all INDIRECT_REFs. */
3542 walk_tree (tsi_stmt_ptr (tsi), mark_notrap, NULL, NULL);
3543 tsi_next (&tsi);
3545 while (!tsi_end_p (tsi));
3547 insert_edge_copies (list, bsi->bb);
3550 else
3551 sra_replace (bsi, list);
3555 /* Generate initializations for all scalarizable parameters. */
3557 static void
3558 scalarize_parms (void)
3560 tree list = NULL;
3561 unsigned i;
3562 bitmap_iterator bi;
3564 EXECUTE_IF_SET_IN_BITMAP (needs_copy_in, 0, i, bi)
3566 tree var = referenced_var (i);
3567 struct sra_elt *elt = lookup_element (NULL, var, NULL, NO_INSERT);
3568 generate_copy_inout (elt, true, var, &list);
3571 if (list)
3573 insert_edge_copies (list, ENTRY_BLOCK_PTR);
3574 mark_all_v_defs (list);
3578 /* Entry point to phase 4. Update the function to match replacements. */
3580 static void
3581 scalarize_function (void)
3583 static const struct sra_walk_fns fns = {
3584 scalarize_use, scalarize_copy, scalarize_init, scalarize_ldst, false
3587 sra_walk_function (&fns);
3588 scalarize_parms ();
3589 bsi_commit_edge_inserts ();
3593 /* Debug helper function. Print ELT in a nice human-readable format. */
3595 static void
3596 dump_sra_elt_name (FILE *f, struct sra_elt *elt)
3598 if (elt->parent && TREE_CODE (elt->parent->type) == COMPLEX_TYPE)
3600 fputs (elt->element == integer_zero_node ? "__real__ " : "__imag__ ", f);
3601 dump_sra_elt_name (f, elt->parent);
3603 else
3605 if (elt->parent)
3606 dump_sra_elt_name (f, elt->parent);
3607 if (DECL_P (elt->element))
3609 if (TREE_CODE (elt->element) == FIELD_DECL)
3610 fputc ('.', f);
3611 print_generic_expr (f, elt->element, dump_flags);
3613 else if (TREE_CODE (elt->element) == BIT_FIELD_REF)
3614 fprintf (f, "$B" HOST_WIDE_INT_PRINT_DEC "F" HOST_WIDE_INT_PRINT_DEC,
3615 tree_low_cst (TREE_OPERAND (elt->element, 2), 1),
3616 tree_low_cst (TREE_OPERAND (elt->element, 1), 1));
3617 else if (TREE_CODE (elt->element) == RANGE_EXPR)
3618 fprintf (f, "["HOST_WIDE_INT_PRINT_DEC".."HOST_WIDE_INT_PRINT_DEC"]",
3619 TREE_INT_CST_LOW (TREE_OPERAND (elt->element, 0)),
3620 TREE_INT_CST_LOW (TREE_OPERAND (elt->element, 1)));
3621 else
3622 fprintf (f, "[" HOST_WIDE_INT_PRINT_DEC "]",
3623 TREE_INT_CST_LOW (elt->element));
3627 /* Likewise, but callable from the debugger. */
3629 void
3630 debug_sra_elt_name (struct sra_elt *elt)
3632 dump_sra_elt_name (stderr, elt);
3633 fputc ('\n', stderr);
3636 void
3637 sra_init_cache (void)
3639 if (sra_type_decomp_cache)
3640 return;
3642 sra_type_decomp_cache = BITMAP_ALLOC (NULL);
3643 sra_type_inst_cache = BITMAP_ALLOC (NULL);
3646 /* Main entry point. */
3648 static unsigned int
3649 tree_sra (void)
3651 /* Initialize local variables. */
3652 todoflags = 0;
3653 gcc_obstack_init (&sra_obstack);
3654 sra_candidates = BITMAP_ALLOC (NULL);
3655 needs_copy_in = BITMAP_ALLOC (NULL);
3656 sra_init_cache ();
3657 sra_map = htab_create (101, sra_elt_hash, sra_elt_eq, NULL);
3659 /* Scan. If we find anything, instantiate and scalarize. */
3660 if (find_candidates_for_sra ())
3662 scan_function ();
3663 decide_instantiations ();
3664 scalarize_function ();
3665 if (!bitmap_empty_p (sra_candidates))
3666 todoflags |= TODO_rebuild_alias;
3669 /* Free allocated memory. */
3670 htab_delete (sra_map);
3671 sra_map = NULL;
3672 BITMAP_FREE (sra_candidates);
3673 BITMAP_FREE (needs_copy_in);
3674 BITMAP_FREE (sra_type_decomp_cache);
3675 BITMAP_FREE (sra_type_inst_cache);
3676 obstack_free (&sra_obstack, NULL);
3677 return todoflags;
3680 static unsigned int
3681 tree_sra_early (void)
3683 unsigned int ret;
3685 early_sra = true;
3686 ret = tree_sra ();
3687 early_sra = false;
3689 return ret & ~TODO_rebuild_alias;
3692 static bool
3693 gate_sra (void)
3695 return flag_tree_sra != 0;
3698 struct gimple_opt_pass pass_sra_early =
3701 GIMPLE_PASS,
3702 "esra", /* name */
3703 gate_sra, /* gate */
3704 tree_sra_early, /* execute */
3705 NULL, /* sub */
3706 NULL, /* next */
3707 0, /* static_pass_number */
3708 TV_TREE_SRA, /* tv_id */
3709 PROP_cfg | PROP_ssa, /* properties_required */
3710 0, /* properties_provided */
3711 0, /* properties_destroyed */
3712 0, /* todo_flags_start */
3713 TODO_dump_func
3714 | TODO_update_ssa
3715 | TODO_ggc_collect
3716 | TODO_verify_ssa /* todo_flags_finish */
3720 struct gimple_opt_pass pass_sra =
3723 GIMPLE_PASS,
3724 "sra", /* name */
3725 gate_sra, /* gate */
3726 tree_sra, /* execute */
3727 NULL, /* sub */
3728 NULL, /* next */
3729 0, /* static_pass_number */
3730 TV_TREE_SRA, /* tv_id */
3731 PROP_cfg | PROP_ssa, /* properties_required */
3732 0, /* properties_provided */
3733 0, /* properties_destroyed */
3734 0, /* todo_flags_start */
3735 TODO_dump_func
3736 | TODO_update_ssa
3737 | TODO_ggc_collect
3738 | TODO_verify_ssa /* todo_flags_finish */