1 /* Scalar Replacement of Aggregates (SRA) converts some structure
2 references into scalar references, exposing them to the scalar
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
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
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/>. */
26 #include "coretypes.h"
31 /* These RTL headers are needed for basic-block.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"
48 /* expr.h is needed for MOVE_RATIO. */
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
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. */
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. */
113 /* The type of the element. */
116 /* A VAR_DECL, for any sub-element we've decided to replace. */
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. */
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. */
130 /* True if this element is a group of members of its parent. */
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. */
141 /* True if everything under this element has been marked TREE_NO_WARNING. */
144 /* A flag for use with/after random access traversals. */
147 /* True if there is BIT_FIELD_REF on the lhs with a vector. */
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)) \
162 (CHILD) = (ELT)->is_group \
163 ? next_child_for_group ((CHILD), (ELT)) \
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. */
174 child
= child
->sibling
;
176 child
= group
->parent
->children
;
178 /* Skip siblings that do not belong to the group. */
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
))
191 child
= child
->sibling
;
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. */
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. */
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. */
243 sra_type_can_be_decomposed_p (tree type
)
245 unsigned int cache
= TYPE_UID (TYPE_MAIN_VARIANT (type
)) * 2;
248 /* Avoid searching the same type twice. */
249 if (bitmap_bit_p (sra_type_decomp_cache
, cache
+0))
251 if (bitmap_bit_p (sra_type_decomp_cache
, cache
+1))
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
)))
259 /* The type must be a non-union aggregate. */
260 switch (TREE_CODE (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 && (tree_low_cst (DECL_SIZE (t
), 1)
272 != TYPE_PRECISION (TREE_TYPE (t
))))
275 saw_one_field
= true;
278 /* Record types must have at least one field. */
285 /* Array types must have a fixed lower and upper bound. */
286 t
= TYPE_DOMAIN (type
);
289 if (TYPE_MIN_VALUE (t
) == NULL
|| !TREE_CONSTANT (TYPE_MIN_VALUE (t
)))
291 if (TYPE_MAX_VALUE (t
) == NULL
|| !TREE_CONSTANT (TYPE_MAX_VALUE (t
)))
302 bitmap_set_bit (sra_type_decomp_cache
, cache
+0);
306 bitmap_set_bit (sra_type_decomp_cache
, cache
+1);
310 /* Return true if DECL can be decomposed into a set of independent
311 (though not necessarily scalar) variables. */
314 decl_can_be_decomposed_p (tree var
)
316 /* Early out for scalars. */
317 if (is_sra_scalar_type (TREE_TYPE (var
)))
320 /* The variable must not be aliased. */
321 if (!is_gimple_non_addressable (var
))
323 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
325 fprintf (dump_file
, "Cannot scalarize variable ");
326 print_generic_expr (dump_file
, var
, dump_flags
);
327 fprintf (dump_file
, " because it must live in memory\n");
332 /* The variable must not be volatile. */
333 if (TREE_THIS_VOLATILE (var
))
335 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
337 fprintf (dump_file
, "Cannot scalarize variable ");
338 print_generic_expr (dump_file
, var
, dump_flags
);
339 fprintf (dump_file
, " because it is declared volatile\n");
344 /* We must be able to decompose the variable's type. */
345 if (!sra_type_can_be_decomposed_p (TREE_TYPE (var
)))
347 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
349 fprintf (dump_file
, "Cannot scalarize variable ");
350 print_generic_expr (dump_file
, var
, dump_flags
);
351 fprintf (dump_file
, " because its type cannot be decomposed\n");
356 /* HACK: if we decompose a va_list_type_node before inlining, then we'll
357 confuse tree-stdarg.c, and we won't be able to figure out which and
358 how many arguments are accessed. This really should be improved in
359 tree-stdarg.c, as the decomposition is truely a win. This could also
360 be fixed if the stdarg pass ran early, but this can't be done until
361 we've aliasing information early too. See PR 30791. */
363 && TYPE_MAIN_VARIANT (TREE_TYPE (var
))
364 == TYPE_MAIN_VARIANT (va_list_type_node
))
370 /* Return true if TYPE can be *completely* decomposed into scalars. */
373 type_can_instantiate_all_elements (tree type
)
375 if (is_sra_scalar_type (type
))
377 if (!sra_type_can_be_decomposed_p (type
))
380 switch (TREE_CODE (type
))
384 unsigned int cache
= TYPE_UID (TYPE_MAIN_VARIANT (type
)) * 2;
387 if (bitmap_bit_p (sra_type_inst_cache
, cache
+0))
389 if (bitmap_bit_p (sra_type_inst_cache
, cache
+1))
392 for (f
= TYPE_FIELDS (type
); f
; f
= TREE_CHAIN (f
))
393 if (TREE_CODE (f
) == FIELD_DECL
)
395 if (!type_can_instantiate_all_elements (TREE_TYPE (f
)))
397 bitmap_set_bit (sra_type_inst_cache
, cache
+1);
402 bitmap_set_bit (sra_type_inst_cache
, cache
+0);
407 return type_can_instantiate_all_elements (TREE_TYPE (type
));
417 /* Test whether ELT or some sub-element cannot be scalarized. */
420 can_completely_scalarize_p (struct sra_elt
*elt
)
424 if (elt
->cannot_scalarize
)
427 for (c
= elt
->children
; c
; c
= c
->sibling
)
428 if (!can_completely_scalarize_p (c
))
431 for (c
= elt
->groups
; c
; c
= c
->sibling
)
432 if (!can_completely_scalarize_p (c
))
439 /* A simplified tree hashing algorithm that only handles the types of
440 trees we expect to find in sra_elt->element. */
443 sra_hash_tree (tree t
)
447 switch (TREE_CODE (t
))
456 h
= TREE_INT_CST_LOW (t
) ^ TREE_INT_CST_HIGH (t
);
460 h
= iterative_hash_expr (TREE_OPERAND (t
, 0), 0);
461 h
= iterative_hash_expr (TREE_OPERAND (t
, 1), h
);
465 /* We can have types that are compatible, but have different member
466 lists, so we can't hash fields by ID. Use offsets instead. */
467 h
= iterative_hash_expr (DECL_FIELD_OFFSET (t
), 0);
468 h
= iterative_hash_expr (DECL_FIELD_BIT_OFFSET (t
), h
);
472 /* Don't take operand 0 into account, that's our parent. */
473 h
= iterative_hash_expr (TREE_OPERAND (t
, 1), 0);
474 h
= iterative_hash_expr (TREE_OPERAND (t
, 2), h
);
484 /* Hash function for type SRA_PAIR. */
487 sra_elt_hash (const void *x
)
489 const struct sra_elt
*e
= x
;
490 const struct sra_elt
*p
;
493 h
= sra_hash_tree (e
->element
);
495 /* Take into account everything except bitfield blocks back up the
496 chain. Given that chain lengths are rarely very long, this
497 should be acceptable. If we truly identify this as a performance
498 problem, it should work to hash the pointer value
500 for (p
= e
->parent
; p
; p
= p
->parent
)
501 if (!p
->in_bitfld_block
)
502 h
= (h
* 65521) ^ sra_hash_tree (p
->element
);
507 /* Equality function for type SRA_PAIR. */
510 sra_elt_eq (const void *x
, const void *y
)
512 const struct sra_elt
*a
= x
;
513 const struct sra_elt
*b
= y
;
515 const struct sra_elt
*ap
= a
->parent
;
516 const struct sra_elt
*bp
= b
->parent
;
519 while (ap
->in_bitfld_block
)
522 while (bp
->in_bitfld_block
)
533 if (TREE_CODE (ae
) != TREE_CODE (be
))
536 switch (TREE_CODE (ae
))
541 /* These are all pointer unique. */
545 /* Integers are not pointer unique, so compare their values. */
546 return tree_int_cst_equal (ae
, be
);
550 tree_int_cst_equal (TREE_OPERAND (ae
, 0), TREE_OPERAND (be
, 0))
551 && tree_int_cst_equal (TREE_OPERAND (ae
, 1), TREE_OPERAND (be
, 1));
554 /* Fields are unique within a record, but not between
555 compatible records. */
556 if (DECL_FIELD_CONTEXT (ae
) == DECL_FIELD_CONTEXT (be
))
558 return fields_compatible_p (ae
, be
);
562 tree_int_cst_equal (TREE_OPERAND (ae
, 1), TREE_OPERAND (be
, 1))
563 && tree_int_cst_equal (TREE_OPERAND (ae
, 2), TREE_OPERAND (be
, 2));
570 /* Create or return the SRA_ELT structure for CHILD in PARENT. PARENT
571 may be null, in which case CHILD must be a DECL. */
573 static struct sra_elt
*
574 lookup_element (struct sra_elt
*parent
, tree child
, tree type
,
575 enum insert_option insert
)
577 struct sra_elt dummy
;
578 struct sra_elt
**slot
;
582 dummy
.parent
= parent
->is_group
? parent
->parent
: parent
;
585 dummy
.element
= child
;
587 slot
= (struct sra_elt
**) htab_find_slot (sra_map
, &dummy
, insert
);
588 if (!slot
&& insert
== NO_INSERT
)
592 if (!elt
&& insert
== INSERT
)
594 *slot
= elt
= obstack_alloc (&sra_obstack
, sizeof (*elt
));
595 memset (elt
, 0, sizeof (*elt
));
597 elt
->parent
= parent
;
598 elt
->element
= child
;
600 elt
->is_scalar
= is_sra_scalar_type (type
);
604 if (IS_ELEMENT_FOR_GROUP (elt
->element
))
606 elt
->is_group
= true;
607 elt
->sibling
= parent
->groups
;
608 parent
->groups
= elt
;
612 elt
->sibling
= parent
->children
;
613 parent
->children
= elt
;
617 /* If this is a parameter, then if we want to scalarize, we have
618 one copy from the true function parameter. Count it now. */
619 if (TREE_CODE (child
) == PARM_DECL
)
622 bitmap_set_bit (needs_copy_in
, DECL_UID (child
));
629 /* Create or return the SRA_ELT structure for EXPR if the expression
630 refers to a scalarizable variable. */
632 static struct sra_elt
*
633 maybe_lookup_element_for_expr (tree expr
)
638 switch (TREE_CODE (expr
))
643 if (is_sra_candidate_decl (expr
))
644 return lookup_element (NULL
, expr
, TREE_TYPE (expr
), INSERT
);
648 /* We can't scalarize variable array indices. */
649 if (in_array_bounds_p (expr
))
650 child
= TREE_OPERAND (expr
, 1);
655 case ARRAY_RANGE_REF
:
656 /* We can't scalarize variable array indices. */
657 if (range_in_array_bounds_p (expr
))
659 tree domain
= TYPE_DOMAIN (TREE_TYPE (expr
));
660 child
= build2 (RANGE_EXPR
, integer_type_node
,
661 TYPE_MIN_VALUE (domain
), TYPE_MAX_VALUE (domain
));
669 tree type
= TREE_TYPE (TREE_OPERAND (expr
, 0));
670 /* Don't look through unions. */
671 if (TREE_CODE (type
) != RECORD_TYPE
)
673 /* Neither through variable-sized records. */
674 if (TYPE_SIZE (type
) == NULL_TREE
675 || TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
677 child
= TREE_OPERAND (expr
, 1);
682 child
= integer_zero_node
;
685 child
= integer_one_node
;
692 elt
= maybe_lookup_element_for_expr (TREE_OPERAND (expr
, 0));
694 return lookup_element (elt
, child
, TREE_TYPE (expr
), INSERT
);
699 /* Functions to walk just enough of the tree to see all scalarizable
700 references, and categorize them. */
702 /* A set of callbacks for phases 2 and 4. They'll be invoked for the
703 various kinds of references seen. In all cases, *BSI is an iterator
704 pointing to the statement being processed. */
707 /* Invoked when ELT is required as a unit. Note that ELT might refer to
708 a leaf node, in which case this is a simple scalar reference. *EXPR_P
709 points to the location of the expression. IS_OUTPUT is true if this
710 is a left-hand-side reference. USE_ALL is true if we saw something we
711 couldn't quite identify and had to force the use of the entire object. */
712 void (*use
) (struct sra_elt
*elt
, tree
*expr_p
,
713 block_stmt_iterator
*bsi
, bool is_output
, bool use_all
);
715 /* Invoked when we have a copy between two scalarizable references. */
716 void (*copy
) (struct sra_elt
*lhs_elt
, struct sra_elt
*rhs_elt
,
717 block_stmt_iterator
*bsi
);
719 /* Invoked when ELT is initialized from a constant. VALUE may be NULL,
720 in which case it should be treated as an empty CONSTRUCTOR. */
721 void (*init
) (struct sra_elt
*elt
, tree value
, block_stmt_iterator
*bsi
);
723 /* Invoked when we have a copy between one scalarizable reference ELT
724 and one non-scalarizable reference OTHER without side-effects.
725 IS_OUTPUT is true if ELT is on the left-hand side. */
726 void (*ldst
) (struct sra_elt
*elt
, tree other
,
727 block_stmt_iterator
*bsi
, bool is_output
);
729 /* True during phase 2, false during phase 4. */
730 /* ??? This is a hack. */
734 #ifdef ENABLE_CHECKING
735 /* Invoked via walk_tree, if *TP contains a candidate decl, return it. */
738 sra_find_candidate_decl (tree
*tp
, int *walk_subtrees
,
739 void *data ATTRIBUTE_UNUSED
)
742 enum tree_code code
= TREE_CODE (t
);
744 if (code
== VAR_DECL
|| code
== PARM_DECL
|| code
== RESULT_DECL
)
747 if (is_sra_candidate_decl (t
))
757 /* Walk most expressions looking for a scalarizable aggregate.
758 If we find one, invoke FNS->USE. */
761 sra_walk_expr (tree
*expr_p
, block_stmt_iterator
*bsi
, bool is_output
,
762 const struct sra_walk_fns
*fns
)
766 bool disable_scalarization
= false;
767 bool use_all_p
= false;
769 /* We're looking to collect a reference expression between EXPR and INNER,
770 such that INNER is a scalarizable decl and all other nodes through EXPR
771 are references that we can scalarize. If we come across something that
772 we can't scalarize, we reset EXPR. This has the effect of making it
773 appear that we're referring to the larger expression as a whole. */
776 switch (TREE_CODE (inner
))
781 /* If there is a scalarizable decl at the bottom, then process it. */
782 if (is_sra_candidate_decl (inner
))
784 struct sra_elt
*elt
= maybe_lookup_element_for_expr (expr
);
785 if (disable_scalarization
)
786 elt
->cannot_scalarize
= true;
788 fns
->use (elt
, expr_p
, bsi
, is_output
, use_all_p
);
793 /* Non-constant index means any member may be accessed. Prevent the
794 expression from being scalarized. If we were to treat this as a
795 reference to the whole array, we can wind up with a single dynamic
796 index reference inside a loop being overridden by several constant
797 index references during loop setup. It's possible that this could
798 be avoided by using dynamic usage counts based on BB trip counts
799 (based on loop analysis or profiling), but that hardly seems worth
801 /* ??? Hack. Figure out how to push this into the scan routines
802 without duplicating too much code. */
803 if (!in_array_bounds_p (inner
))
805 disable_scalarization
= true;
808 /* ??? Are we assured that non-constant bounds and stride will have
809 the same value everywhere? I don't think Fortran will... */
810 if (TREE_OPERAND (inner
, 2) || TREE_OPERAND (inner
, 3))
812 inner
= TREE_OPERAND (inner
, 0);
815 case ARRAY_RANGE_REF
:
816 if (!range_in_array_bounds_p (inner
))
818 disable_scalarization
= true;
821 /* ??? See above non-constant bounds and stride . */
822 if (TREE_OPERAND (inner
, 2) || TREE_OPERAND (inner
, 3))
824 inner
= TREE_OPERAND (inner
, 0);
829 tree type
= TREE_TYPE (TREE_OPERAND (inner
, 0));
830 /* Don't look through unions. */
831 if (TREE_CODE (type
) != RECORD_TYPE
)
833 /* Neither through variable-sized records. */
834 if (TYPE_SIZE (type
) == NULL_TREE
835 || TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
837 inner
= TREE_OPERAND (inner
, 0);
843 inner
= TREE_OPERAND (inner
, 0);
847 /* A bit field reference to a specific vector is scalarized but for
848 ones for inputs need to be marked as used on the left hand size so
849 when we scalarize it, we can mark that variable as non renamable. */
851 && TREE_CODE (TREE_TYPE (TREE_OPERAND (inner
, 0))) == VECTOR_TYPE
)
854 = maybe_lookup_element_for_expr (TREE_OPERAND (inner
, 0));
856 elt
->is_vector_lhs
= true;
858 /* A bit field reference (access to *multiple* fields simultaneously)
859 is not currently scalarized. Consider this an access to the
860 complete outer element, to which walk_tree will bring us next. */
864 case VIEW_CONVERT_EXPR
:
866 /* Similarly, a view/nop explicitly wants to look at an object in a
867 type other than the one we've scalarized. */
871 /* This is a transparent wrapper. The entire inner expression really
876 expr_p
= &TREE_OPERAND (inner
, 0);
877 inner
= expr
= *expr_p
;
882 #ifdef ENABLE_CHECKING
883 /* Validate that we're not missing any references. */
884 gcc_assert (!walk_tree (&inner
, sra_find_candidate_decl
, NULL
, NULL
));
890 /* Walk a TREE_LIST of values looking for scalarizable aggregates.
891 If we find one, invoke FNS->USE. */
894 sra_walk_tree_list (tree list
, block_stmt_iterator
*bsi
, bool is_output
,
895 const struct sra_walk_fns
*fns
)
898 for (op
= list
; op
; op
= TREE_CHAIN (op
))
899 sra_walk_expr (&TREE_VALUE (op
), bsi
, is_output
, fns
);
902 /* Walk the arguments of a CALL_EXPR looking for scalarizable aggregates.
903 If we find one, invoke FNS->USE. */
906 sra_walk_call_expr (tree expr
, block_stmt_iterator
*bsi
,
907 const struct sra_walk_fns
*fns
)
910 int nargs
= call_expr_nargs (expr
);
911 for (i
= 0; i
< nargs
; i
++)
912 sra_walk_expr (&CALL_EXPR_ARG (expr
, i
), bsi
, false, fns
);
915 /* Walk the inputs and outputs of an ASM_EXPR looking for scalarizable
916 aggregates. If we find one, invoke FNS->USE. */
919 sra_walk_asm_expr (tree expr
, block_stmt_iterator
*bsi
,
920 const struct sra_walk_fns
*fns
)
922 sra_walk_tree_list (ASM_INPUTS (expr
), bsi
, false, fns
);
923 sra_walk_tree_list (ASM_OUTPUTS (expr
), bsi
, true, fns
);
926 /* Walk a GIMPLE_MODIFY_STMT and categorize the assignment appropriately. */
929 sra_walk_gimple_modify_stmt (tree expr
, block_stmt_iterator
*bsi
,
930 const struct sra_walk_fns
*fns
)
932 struct sra_elt
*lhs_elt
, *rhs_elt
;
935 lhs
= GIMPLE_STMT_OPERAND (expr
, 0);
936 rhs
= GIMPLE_STMT_OPERAND (expr
, 1);
937 lhs_elt
= maybe_lookup_element_for_expr (lhs
);
938 rhs_elt
= maybe_lookup_element_for_expr (rhs
);
940 /* If both sides are scalarizable, this is a COPY operation. */
941 if (lhs_elt
&& rhs_elt
)
943 fns
->copy (lhs_elt
, rhs_elt
, bsi
);
947 /* If the RHS is scalarizable, handle it. There are only two cases. */
950 if (!rhs_elt
->is_scalar
&& !TREE_SIDE_EFFECTS (lhs
))
951 fns
->ldst (rhs_elt
, lhs
, bsi
, false);
953 fns
->use (rhs_elt
, &GIMPLE_STMT_OPERAND (expr
, 1), bsi
, false, false);
956 /* If it isn't scalarizable, there may be scalarizable variables within, so
957 check for a call or else walk the RHS to see if we need to do any
958 copy-in operations. We need to do it before the LHS is scalarized so
959 that the statements get inserted in the proper place, before any
960 copy-out operations. */
963 tree call
= get_call_expr_in (rhs
);
965 sra_walk_call_expr (call
, bsi
, fns
);
967 sra_walk_expr (&GIMPLE_STMT_OPERAND (expr
, 1), bsi
, false, fns
);
970 /* Likewise, handle the LHS being scalarizable. We have cases similar
971 to those above, but also want to handle RHS being constant. */
974 /* If this is an assignment from a constant, or constructor, then
975 we have access to all of the elements individually. Invoke INIT. */
976 if (TREE_CODE (rhs
) == COMPLEX_EXPR
977 || TREE_CODE (rhs
) == COMPLEX_CST
978 || TREE_CODE (rhs
) == CONSTRUCTOR
)
979 fns
->init (lhs_elt
, rhs
, bsi
);
981 /* If this is an assignment from read-only memory, treat this as if
982 we'd been passed the constructor directly. Invoke INIT. */
983 else if (TREE_CODE (rhs
) == VAR_DECL
985 && TREE_READONLY (rhs
)
986 && targetm
.binds_local_p (rhs
))
987 fns
->init (lhs_elt
, DECL_INITIAL (rhs
), bsi
);
989 /* If this is a copy from a non-scalarizable lvalue, invoke LDST.
990 The lvalue requirement prevents us from trying to directly scalarize
991 the result of a function call. Which would result in trying to call
992 the function multiple times, and other evil things. */
993 else if (!lhs_elt
->is_scalar
994 && !TREE_SIDE_EFFECTS (rhs
) && is_gimple_addressable (rhs
))
995 fns
->ldst (lhs_elt
, rhs
, bsi
, true);
997 /* Otherwise we're being used in some context that requires the
998 aggregate to be seen as a whole. Invoke USE. */
1000 fns
->use (lhs_elt
, &GIMPLE_STMT_OPERAND (expr
, 0), bsi
, true, false);
1003 /* Similarly to above, LHS_ELT being null only means that the LHS as a
1004 whole is not a scalarizable reference. There may be occurrences of
1005 scalarizable variables within, which implies a USE. */
1007 sra_walk_expr (&GIMPLE_STMT_OPERAND (expr
, 0), bsi
, true, fns
);
1010 /* Entry point to the walk functions. Search the entire function,
1011 invoking the callbacks in FNS on each of the references to
1012 scalarizable variables. */
1015 sra_walk_function (const struct sra_walk_fns
*fns
)
1018 block_stmt_iterator si
, ni
;
1020 /* ??? Phase 4 could derive some benefit to walking the function in
1021 dominator tree order. */
1024 for (si
= bsi_start (bb
); !bsi_end_p (si
); si
= ni
)
1029 stmt
= bsi_stmt (si
);
1030 ann
= stmt_ann (stmt
);
1035 /* If the statement has no virtual operands, then it doesn't
1036 make any structure references that we care about. */
1037 if (gimple_aliases_computed_p (cfun
)
1038 && ZERO_SSA_OPERANDS (stmt
, (SSA_OP_VIRTUAL_DEFS
| SSA_OP_VUSE
)))
1041 switch (TREE_CODE (stmt
))
1044 /* If we have "return <retval>" then the return value is
1045 already exposed for our pleasure. Walk it as a USE to
1046 force all the components back in place for the return.
1048 If we have an embedded assignment, then <retval> is of
1049 a type that gets returned in registers in this ABI, and
1050 we do not wish to extend their lifetimes. Treat this
1051 as a USE of the variable on the RHS of this assignment. */
1053 t
= TREE_OPERAND (stmt
, 0);
1056 else if (TREE_CODE (t
) == GIMPLE_MODIFY_STMT
)
1057 sra_walk_expr (&GIMPLE_STMT_OPERAND (t
, 1), &si
, false, fns
);
1059 sra_walk_expr (&TREE_OPERAND (stmt
, 0), &si
, false, fns
);
1062 case GIMPLE_MODIFY_STMT
:
1063 sra_walk_gimple_modify_stmt (stmt
, &si
, fns
);
1066 sra_walk_call_expr (stmt
, &si
, fns
);
1069 sra_walk_asm_expr (stmt
, &si
, fns
);
1078 /* Phase One: Scan all referenced variables in the program looking for
1079 structures that could be decomposed. */
1082 find_candidates_for_sra (void)
1084 bool any_set
= false;
1086 referenced_var_iterator rvi
;
1088 FOR_EACH_REFERENCED_VAR (var
, rvi
)
1090 if (decl_can_be_decomposed_p (var
))
1092 bitmap_set_bit (sra_candidates
, DECL_UID (var
));
1101 /* Phase Two: Scan all references to scalarizable variables. Count the
1102 number of times they are used or copied respectively. */
1104 /* Callbacks to fill in SRA_WALK_FNS. Everything but USE is
1105 considered a copy, because we can decompose the reference such that
1106 the sub-elements needn't be contiguous. */
1109 scan_use (struct sra_elt
*elt
, tree
*expr_p ATTRIBUTE_UNUSED
,
1110 block_stmt_iterator
*bsi ATTRIBUTE_UNUSED
,
1111 bool is_output ATTRIBUTE_UNUSED
, bool use_all ATTRIBUTE_UNUSED
)
1117 scan_copy (struct sra_elt
*lhs_elt
, struct sra_elt
*rhs_elt
,
1118 block_stmt_iterator
*bsi ATTRIBUTE_UNUSED
)
1120 lhs_elt
->n_copies
+= 1;
1121 rhs_elt
->n_copies
+= 1;
1125 scan_init (struct sra_elt
*lhs_elt
, tree rhs ATTRIBUTE_UNUSED
,
1126 block_stmt_iterator
*bsi ATTRIBUTE_UNUSED
)
1128 lhs_elt
->n_copies
+= 1;
1132 scan_ldst (struct sra_elt
*elt
, tree other ATTRIBUTE_UNUSED
,
1133 block_stmt_iterator
*bsi ATTRIBUTE_UNUSED
,
1134 bool is_output ATTRIBUTE_UNUSED
)
1139 /* Dump the values we collected during the scanning phase. */
1142 scan_dump (struct sra_elt
*elt
)
1146 dump_sra_elt_name (dump_file
, elt
);
1147 fprintf (dump_file
, ": n_uses=%u n_copies=%u\n", elt
->n_uses
, elt
->n_copies
);
1149 for (c
= elt
->children
; c
; c
= c
->sibling
)
1152 for (c
= elt
->groups
; c
; c
= c
->sibling
)
1156 /* Entry point to phase 2. Scan the entire function, building up
1157 scalarization data structures, recording copies and uses. */
1160 scan_function (void)
1162 static const struct sra_walk_fns fns
= {
1163 scan_use
, scan_copy
, scan_init
, scan_ldst
, true
1166 sra_walk_function (&fns
);
1168 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1170 referenced_var_iterator ri
;
1173 fputs ("\nScan results:\n", dump_file
);
1174 FOR_EACH_REFERENCED_VAR_IN_BITMAP (sra_candidates
, var
, ri
)
1176 struct sra_elt
*elt
= lookup_element (NULL
, var
, NULL
, NO_INSERT
);
1180 fputc ('\n', dump_file
);
1184 /* Phase Three: Make decisions about which variables to scalarize, if any.
1185 All elements to be scalarized have replacement variables made for them. */
1187 /* A subroutine of build_element_name. Recursively build the element
1188 name on the obstack. */
1191 build_element_name_1 (struct sra_elt
*elt
)
1198 build_element_name_1 (elt
->parent
);
1199 obstack_1grow (&sra_obstack
, '$');
1201 if (TREE_CODE (elt
->parent
->type
) == COMPLEX_TYPE
)
1203 if (elt
->element
== integer_zero_node
)
1204 obstack_grow (&sra_obstack
, "real", 4);
1206 obstack_grow (&sra_obstack
, "imag", 4);
1212 if (TREE_CODE (t
) == INTEGER_CST
)
1214 /* ??? Eh. Don't bother doing double-wide printing. */
1215 sprintf (buffer
, HOST_WIDE_INT_PRINT_DEC
, TREE_INT_CST_LOW (t
));
1216 obstack_grow (&sra_obstack
, buffer
, strlen (buffer
));
1218 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
1220 sprintf (buffer
, "B" HOST_WIDE_INT_PRINT_DEC
,
1221 tree_low_cst (TREE_OPERAND (t
, 2), 1));
1222 obstack_grow (&sra_obstack
, buffer
, strlen (buffer
));
1223 sprintf (buffer
, "F" HOST_WIDE_INT_PRINT_DEC
,
1224 tree_low_cst (TREE_OPERAND (t
, 1), 1));
1225 obstack_grow (&sra_obstack
, buffer
, strlen (buffer
));
1229 tree name
= DECL_NAME (t
);
1231 obstack_grow (&sra_obstack
, IDENTIFIER_POINTER (name
),
1232 IDENTIFIER_LENGTH (name
));
1235 sprintf (buffer
, "D%u", DECL_UID (t
));
1236 obstack_grow (&sra_obstack
, buffer
, strlen (buffer
));
1241 /* Construct a pretty variable name for an element's replacement variable.
1242 The name is built on the obstack. */
1245 build_element_name (struct sra_elt
*elt
)
1247 build_element_name_1 (elt
);
1248 obstack_1grow (&sra_obstack
, '\0');
1249 return XOBFINISH (&sra_obstack
, char *);
1252 /* Instantiate an element as an independent variable. */
1255 instantiate_element (struct sra_elt
*elt
)
1257 struct sra_elt
*base_elt
;
1259 bool nowarn
= TREE_NO_WARNING (elt
->element
);
1261 for (base_elt
= elt
; base_elt
->parent
; base_elt
= base_elt
->parent
)
1263 nowarn
= TREE_NO_WARNING (base_elt
->parent
->element
);
1264 base
= base_elt
->element
;
1266 elt
->replacement
= var
= make_rename_temp (elt
->type
, "SR");
1268 if (DECL_P (elt
->element
)
1269 && !tree_int_cst_equal (DECL_SIZE (var
), DECL_SIZE (elt
->element
)))
1271 DECL_SIZE (var
) = DECL_SIZE (elt
->element
);
1272 DECL_SIZE_UNIT (var
) = DECL_SIZE_UNIT (elt
->element
);
1274 elt
->in_bitfld_block
= 1;
1275 elt
->replacement
= build3 (BIT_FIELD_REF
, elt
->type
, var
,
1278 ? size_binop (MINUS_EXPR
,
1279 TYPE_SIZE (elt
->type
),
1282 if (!INTEGRAL_TYPE_P (elt
->type
)
1283 || TYPE_UNSIGNED (elt
->type
))
1284 BIT_FIELD_REF_UNSIGNED (elt
->replacement
) = 1;
1287 /* For vectors, if used on the left hand side with BIT_FIELD_REF,
1288 they are not a gimple register. */
1289 if (TREE_CODE (TREE_TYPE (var
)) == VECTOR_TYPE
&& elt
->is_vector_lhs
)
1290 DECL_GIMPLE_REG_P (var
) = 0;
1292 DECL_SOURCE_LOCATION (var
) = DECL_SOURCE_LOCATION (base
);
1293 DECL_ARTIFICIAL (var
) = 1;
1295 if (TREE_THIS_VOLATILE (elt
->type
))
1297 TREE_THIS_VOLATILE (var
) = 1;
1298 TREE_SIDE_EFFECTS (var
) = 1;
1301 if (DECL_NAME (base
) && !DECL_IGNORED_P (base
))
1303 char *pretty_name
= build_element_name (elt
);
1304 DECL_NAME (var
) = get_identifier (pretty_name
);
1305 obstack_free (&sra_obstack
, pretty_name
);
1307 SET_DECL_DEBUG_EXPR (var
, generate_element_ref (elt
));
1308 DECL_DEBUG_EXPR_IS_FROM (var
) = 1;
1310 DECL_IGNORED_P (var
) = 0;
1311 TREE_NO_WARNING (var
) = nowarn
;
1315 DECL_IGNORED_P (var
) = 1;
1316 /* ??? We can't generate any warning that would be meaningful. */
1317 TREE_NO_WARNING (var
) = 1;
1320 /* Zero-initialize bit-field scalarization variables, to avoid
1321 triggering undefined behavior. */
1322 if (TREE_CODE (elt
->element
) == BIT_FIELD_REF
1323 || (var
!= elt
->replacement
1324 && TREE_CODE (elt
->replacement
) == BIT_FIELD_REF
))
1326 tree init
= sra_build_assignment (var
, fold_convert (TREE_TYPE (var
),
1327 integer_zero_node
));
1328 insert_edge_copies (init
, ENTRY_BLOCK_PTR
);
1329 mark_all_v_defs (init
);
1334 fputs (" ", dump_file
);
1335 dump_sra_elt_name (dump_file
, elt
);
1336 fputs (" -> ", dump_file
);
1337 print_generic_expr (dump_file
, var
, dump_flags
);
1338 fputc ('\n', dump_file
);
1342 /* Make one pass across an element tree deciding whether or not it's
1343 profitable to instantiate individual leaf scalars.
1345 PARENT_USES and PARENT_COPIES are the sum of the N_USES and N_COPIES
1346 fields all the way up the tree. */
1349 decide_instantiation_1 (struct sra_elt
*elt
, unsigned int parent_uses
,
1350 unsigned int parent_copies
)
1352 if (dump_file
&& !elt
->parent
)
1354 fputs ("Initial instantiation for ", dump_file
);
1355 dump_sra_elt_name (dump_file
, elt
);
1356 fputc ('\n', dump_file
);
1359 if (elt
->cannot_scalarize
)
1364 /* The decision is simple: instantiate if we're used more frequently
1365 than the parent needs to be seen as a complete unit. */
1366 if (elt
->n_uses
+ elt
->n_copies
+ parent_copies
> parent_uses
)
1367 instantiate_element (elt
);
1371 struct sra_elt
*c
, *group
;
1372 unsigned int this_uses
= elt
->n_uses
+ parent_uses
;
1373 unsigned int this_copies
= elt
->n_copies
+ parent_copies
;
1375 /* Consider groups of sub-elements as weighing in favour of
1376 instantiation whatever their size. */
1377 for (group
= elt
->groups
; group
; group
= group
->sibling
)
1378 FOR_EACH_ACTUAL_CHILD (c
, group
)
1380 c
->n_uses
+= group
->n_uses
;
1381 c
->n_copies
+= group
->n_copies
;
1384 for (c
= elt
->children
; c
; c
= c
->sibling
)
1385 decide_instantiation_1 (c
, this_uses
, this_copies
);
1389 /* Compute the size and number of all instantiated elements below ELT.
1390 We will only care about this if the size of the complete structure
1391 fits in a HOST_WIDE_INT, so we don't have to worry about overflow. */
1394 sum_instantiated_sizes (struct sra_elt
*elt
, unsigned HOST_WIDE_INT
*sizep
)
1396 if (elt
->replacement
)
1398 *sizep
+= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (elt
->type
));
1404 unsigned int count
= 0;
1406 for (c
= elt
->children
; c
; c
= c
->sibling
)
1407 count
+= sum_instantiated_sizes (c
, sizep
);
1413 /* Instantiate fields in ELT->TYPE that are not currently present as
1416 static void instantiate_missing_elements (struct sra_elt
*elt
);
1418 static struct sra_elt
*
1419 instantiate_missing_elements_1 (struct sra_elt
*elt
, tree child
, tree type
)
1421 struct sra_elt
*sub
= lookup_element (elt
, child
, type
, INSERT
);
1424 if (sub
->replacement
== NULL
)
1425 instantiate_element (sub
);
1428 instantiate_missing_elements (sub
);
1432 /* Obtain the canonical type for field F of ELEMENT. */
1435 canon_type_for_field (tree f
, tree element
)
1437 tree field_type
= TREE_TYPE (f
);
1439 /* canonicalize_component_ref() unwidens some bit-field types (not
1440 marked as DECL_BIT_FIELD in C++), so we must do the same, lest we
1441 may introduce type mismatches. */
1442 if (INTEGRAL_TYPE_P (field_type
)
1443 && DECL_MODE (f
) != TYPE_MODE (field_type
))
1444 field_type
= TREE_TYPE (get_unwidened (build3 (COMPONENT_REF
,
1453 /* Look for adjacent fields of ELT starting at F that we'd like to
1454 scalarize as a single variable. Return the last field of the
1458 try_instantiate_multiple_fields (struct sra_elt
*elt
, tree f
)
1461 unsigned HOST_WIDE_INT align
, bit
, size
, alchk
;
1462 enum machine_mode mode
;
1463 tree first
= f
, prev
;
1465 struct sra_elt
*block
;
1467 if (!is_sra_scalar_type (TREE_TYPE (f
))
1468 || !host_integerp (DECL_FIELD_OFFSET (f
), 1)
1469 || !host_integerp (DECL_FIELD_BIT_OFFSET (f
), 1)
1470 || !host_integerp (DECL_SIZE (f
), 1)
1471 || lookup_element (elt
, f
, NULL
, NO_INSERT
))
1476 /* For complex and array objects, there are going to be integer
1477 literals as child elements. In this case, we can't just take the
1478 alignment and mode of the decl, so we instead rely on the element
1481 ??? We could try to infer additional alignment from the full
1482 object declaration and the location of the sub-elements we're
1484 for (count
= 0; !DECL_P (block
->element
); count
++)
1485 block
= block
->parent
;
1487 align
= DECL_ALIGN (block
->element
);
1488 alchk
= GET_MODE_BITSIZE (DECL_MODE (block
->element
));
1492 type
= TREE_TYPE (block
->element
);
1494 type
= TREE_TYPE (type
);
1496 align
= TYPE_ALIGN (type
);
1497 alchk
= GET_MODE_BITSIZE (TYPE_MODE (type
));
1503 /* Coalescing wider fields is probably pointless and
1505 if (align
> BITS_PER_WORD
)
1506 align
= BITS_PER_WORD
;
1508 bit
= tree_low_cst (DECL_FIELD_OFFSET (f
), 1) * BITS_PER_UNIT
1509 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f
), 1);
1510 size
= tree_low_cst (DECL_SIZE (f
), 1);
1515 if ((bit
& alchk
) != ((bit
+ size
- 1) & alchk
))
1518 /* Find adjacent fields in the same alignment word. */
1520 for (prev
= f
, f
= TREE_CHAIN (f
);
1521 f
&& TREE_CODE (f
) == FIELD_DECL
1522 && is_sra_scalar_type (TREE_TYPE (f
))
1523 && host_integerp (DECL_FIELD_OFFSET (f
), 1)
1524 && host_integerp (DECL_FIELD_BIT_OFFSET (f
), 1)
1525 && host_integerp (DECL_SIZE (f
), 1)
1526 && !lookup_element (elt
, f
, NULL
, NO_INSERT
);
1527 prev
= f
, f
= TREE_CHAIN (f
))
1529 unsigned HOST_WIDE_INT nbit
, nsize
;
1531 nbit
= tree_low_cst (DECL_FIELD_OFFSET (f
), 1) * BITS_PER_UNIT
1532 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f
), 1);
1533 nsize
= tree_low_cst (DECL_SIZE (f
), 1);
1535 if (bit
+ size
== nbit
)
1537 if ((bit
& alchk
) != ((nbit
+ nsize
- 1) & alchk
))
1539 /* If we're at an alignment boundary, don't bother
1540 growing alignment such that we can include this next
1543 || GET_MODE_BITSIZE (DECL_MODE (f
)) <= align
)
1546 align
= GET_MODE_BITSIZE (DECL_MODE (f
));
1550 if ((bit
& alchk
) != ((nbit
+ nsize
- 1) & alchk
))
1555 else if (nbit
+ nsize
== bit
)
1557 if ((nbit
& alchk
) != ((bit
+ size
- 1) & alchk
))
1560 || GET_MODE_BITSIZE (DECL_MODE (f
)) <= align
)
1563 align
= GET_MODE_BITSIZE (DECL_MODE (f
));
1567 if ((nbit
& alchk
) != ((bit
+ size
- 1) & alchk
))
1582 gcc_assert ((bit
& alchk
) == ((bit
+ size
- 1) & alchk
));
1584 /* Try to widen the bit range so as to cover padding bits as well. */
1586 if ((bit
& ~alchk
) || size
!= align
)
1588 unsigned HOST_WIDE_INT mbit
= bit
& alchk
;
1589 unsigned HOST_WIDE_INT msize
= align
;
1591 for (f
= TYPE_FIELDS (elt
->type
);
1592 f
; f
= TREE_CHAIN (f
))
1594 unsigned HOST_WIDE_INT fbit
, fsize
;
1596 /* Skip the fields from first to prev. */
1603 if (!(TREE_CODE (f
) == FIELD_DECL
1604 && host_integerp (DECL_FIELD_OFFSET (f
), 1)
1605 && host_integerp (DECL_FIELD_BIT_OFFSET (f
), 1)))
1608 fbit
= tree_low_cst (DECL_FIELD_OFFSET (f
), 1) * BITS_PER_UNIT
1609 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f
), 1);
1611 /* If we're past the selected word, we're fine. */
1612 if ((bit
& alchk
) < (fbit
& alchk
))
1615 if (host_integerp (DECL_SIZE (f
), 1))
1616 fsize
= tree_low_cst (DECL_SIZE (f
), 1);
1618 /* Assume a variable-sized field takes up all space till
1619 the end of the word. ??? Endianness issues? */
1620 fsize
= align
- (fbit
& alchk
);
1622 if ((fbit
& alchk
) < (bit
& alchk
))
1624 /* A large field might start at a previous word and
1625 extend into the selected word. Exclude those
1626 bits. ??? Endianness issues? */
1627 HOST_WIDE_INT diff
= fbit
+ fsize
- mbit
;
1637 /* Non-overlapping, great. */
1638 if (fbit
+ fsize
<= mbit
1639 || mbit
+ msize
<= fbit
)
1644 unsigned HOST_WIDE_INT diff
= fbit
+ fsize
- mbit
;
1648 else if (fbit
> mbit
)
1649 msize
-= (mbit
+ msize
- fbit
);
1659 /* Now we know the bit range we're interested in. Find the smallest
1660 machine mode we can use to access it. */
1662 for (mode
= smallest_mode_for_size (size
, MODE_INT
);
1664 mode
= GET_MODE_WIDER_MODE (mode
))
1666 gcc_assert (mode
!= VOIDmode
);
1668 alchk
= GET_MODE_PRECISION (mode
) - 1;
1671 if ((bit
& alchk
) == ((bit
+ size
- 1) & alchk
))
1675 gcc_assert (~alchk
< align
);
1677 /* Create the field group as a single variable. */
1679 type
= lang_hooks
.types
.type_for_mode (mode
, 1);
1681 var
= build3 (BIT_FIELD_REF
, type
, NULL_TREE
,
1684 BIT_FIELD_REF_UNSIGNED (var
) = 1;
1686 block
= instantiate_missing_elements_1 (elt
, var
, type
);
1687 gcc_assert (block
&& block
->is_scalar
);
1689 var
= block
->replacement
;
1692 || (HOST_WIDE_INT
)size
!= tree_low_cst (DECL_SIZE (var
), 1))
1694 block
->replacement
= build3 (BIT_FIELD_REF
,
1695 TREE_TYPE (block
->element
), var
,
1697 bitsize_int (bit
& ~alchk
));
1698 BIT_FIELD_REF_UNSIGNED (block
->replacement
) = 1;
1701 block
->in_bitfld_block
= 2;
1703 /* Add the member fields to the group, such that they access
1704 portions of the group variable. */
1706 for (f
= first
; f
!= TREE_CHAIN (prev
); f
= TREE_CHAIN (f
))
1708 tree field_type
= canon_type_for_field (f
, elt
->element
);
1709 struct sra_elt
*fld
= lookup_element (block
, f
, field_type
, INSERT
);
1711 gcc_assert (fld
&& fld
->is_scalar
&& !fld
->replacement
);
1713 fld
->replacement
= build3 (BIT_FIELD_REF
, field_type
, var
,
1716 ((TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f
))
1719 (DECL_FIELD_BIT_OFFSET (f
))))
1721 BIT_FIELD_REF_UNSIGNED (fld
->replacement
) = TYPE_UNSIGNED (field_type
);
1722 fld
->in_bitfld_block
= 1;
1729 instantiate_missing_elements (struct sra_elt
*elt
)
1731 tree type
= elt
->type
;
1733 switch (TREE_CODE (type
))
1738 for (f
= TYPE_FIELDS (type
); f
; f
= TREE_CHAIN (f
))
1739 if (TREE_CODE (f
) == FIELD_DECL
)
1741 tree last
= try_instantiate_multiple_fields (elt
, f
);
1749 instantiate_missing_elements_1 (elt
, f
,
1750 canon_type_for_field
1758 tree i
, max
, subtype
;
1760 i
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
1761 max
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
1762 subtype
= TREE_TYPE (type
);
1766 instantiate_missing_elements_1 (elt
, i
, subtype
);
1767 if (tree_int_cst_equal (i
, max
))
1769 i
= int_const_binop (PLUS_EXPR
, i
, integer_one_node
, true);
1776 type
= TREE_TYPE (type
);
1777 instantiate_missing_elements_1 (elt
, integer_zero_node
, type
);
1778 instantiate_missing_elements_1 (elt
, integer_one_node
, type
);
1786 /* Return true if there is only one non aggregate field in the record, TYPE.
1787 Return false otherwise. */
1790 single_scalar_field_in_record_p (tree type
)
1794 if (TREE_CODE (type
) != RECORD_TYPE
)
1797 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1798 if (TREE_CODE (field
) == FIELD_DECL
)
1802 if (num_fields
== 2)
1805 if (AGGREGATE_TYPE_P (TREE_TYPE (field
)))
1812 /* Make one pass across an element tree deciding whether to perform block
1813 or element copies. If we decide on element copies, instantiate all
1814 elements. Return true if there are any instantiated sub-elements. */
1817 decide_block_copy (struct sra_elt
*elt
)
1822 /* We shouldn't be invoked on groups of sub-elements as they must
1823 behave like their parent as far as block copy is concerned. */
1824 gcc_assert (!elt
->is_group
);
1826 /* If scalarization is disabled, respect it. */
1827 if (elt
->cannot_scalarize
)
1829 elt
->use_block_copy
= 1;
1833 fputs ("Scalarization disabled for ", dump_file
);
1834 dump_sra_elt_name (dump_file
, elt
);
1835 fputc ('\n', dump_file
);
1838 /* Disable scalarization of sub-elements */
1839 for (c
= elt
->children
; c
; c
= c
->sibling
)
1841 c
->cannot_scalarize
= 1;
1842 decide_block_copy (c
);
1845 /* Groups behave like their parent. */
1846 for (c
= elt
->groups
; c
; c
= c
->sibling
)
1848 c
->cannot_scalarize
= 1;
1849 c
->use_block_copy
= 1;
1855 /* Don't decide if we've no uses and no groups. */
1856 if (elt
->n_uses
== 0 && elt
->n_copies
== 0 && elt
->groups
== NULL
)
1859 else if (!elt
->is_scalar
)
1861 tree size_tree
= TYPE_SIZE_UNIT (elt
->type
);
1862 bool use_block_copy
= true;
1864 /* Tradeoffs for COMPLEX types pretty much always make it better
1865 to go ahead and split the components. */
1866 if (TREE_CODE (elt
->type
) == COMPLEX_TYPE
)
1867 use_block_copy
= false;
1869 /* Don't bother trying to figure out the rest if the structure is
1870 so large we can't do easy arithmetic. This also forces block
1871 copies for variable sized structures. */
1872 else if (host_integerp (size_tree
, 1))
1874 unsigned HOST_WIDE_INT full_size
, inst_size
= 0;
1875 unsigned int max_size
, max_count
, inst_count
, full_count
;
1877 /* If the sra-max-structure-size parameter is 0, then the
1878 user has not overridden the parameter and we can choose a
1879 sensible default. */
1880 max_size
= SRA_MAX_STRUCTURE_SIZE
1881 ? SRA_MAX_STRUCTURE_SIZE
1882 : MOVE_RATIO
* UNITS_PER_WORD
;
1883 max_count
= SRA_MAX_STRUCTURE_COUNT
1884 ? SRA_MAX_STRUCTURE_COUNT
1887 full_size
= tree_low_cst (size_tree
, 1);
1888 full_count
= count_type_elements (elt
->type
, false);
1889 inst_count
= sum_instantiated_sizes (elt
, &inst_size
);
1891 /* If there is only one scalar field in the record, don't block copy. */
1892 if (single_scalar_field_in_record_p (elt
->type
))
1893 use_block_copy
= false;
1895 /* ??? What to do here. If there are two fields, and we've only
1896 instantiated one, then instantiating the other is clearly a win.
1897 If there are a large number of fields then the size of the copy
1898 is much more of a factor. */
1900 /* If the structure is small, and we've made copies, go ahead
1901 and instantiate, hoping that the copies will go away. */
1902 if (full_size
<= max_size
1903 && (full_count
- inst_count
) <= max_count
1904 && elt
->n_copies
> elt
->n_uses
)
1905 use_block_copy
= false;
1906 else if (inst_count
* 100 >= full_count
* SRA_FIELD_STRUCTURE_RATIO
1907 && inst_size
* 100 >= full_size
* SRA_FIELD_STRUCTURE_RATIO
)
1908 use_block_copy
= false;
1910 /* In order to avoid block copy, we have to be able to instantiate
1911 all elements of the type. See if this is possible. */
1913 && (!can_completely_scalarize_p (elt
)
1914 || !type_can_instantiate_all_elements (elt
->type
)))
1915 use_block_copy
= true;
1918 elt
->use_block_copy
= use_block_copy
;
1920 /* Groups behave like their parent. */
1921 for (c
= elt
->groups
; c
; c
= c
->sibling
)
1922 c
->use_block_copy
= use_block_copy
;
1926 fprintf (dump_file
, "Using %s for ",
1927 use_block_copy
? "block-copy" : "element-copy");
1928 dump_sra_elt_name (dump_file
, elt
);
1929 fputc ('\n', dump_file
);
1932 if (!use_block_copy
)
1934 instantiate_missing_elements (elt
);
1939 any_inst
= elt
->replacement
!= NULL
;
1941 for (c
= elt
->children
; c
; c
= c
->sibling
)
1942 any_inst
|= decide_block_copy (c
);
1947 /* Entry point to phase 3. Instantiate scalar replacement variables. */
1950 decide_instantiations (void)
1953 bitmap_head done_head
;
1954 referenced_var_iterator ri
;
1957 /* We cannot clear bits from a bitmap we're iterating over,
1958 so save up all the bits to clear until the end. */
1959 bitmap_initialize (&done_head
, &bitmap_default_obstack
);
1960 cleared_any
= false;
1962 FOR_EACH_REFERENCED_VAR_IN_BITMAP (sra_candidates
, var
, ri
)
1964 struct sra_elt
*elt
= lookup_element (NULL
, var
, NULL
, NO_INSERT
);
1967 decide_instantiation_1 (elt
, 0, 0);
1968 if (!decide_block_copy (elt
))
1973 bitmap_set_bit (&done_head
, DECL_UID (var
));
1980 bitmap_and_compl_into (sra_candidates
, &done_head
);
1981 bitmap_and_compl_into (needs_copy_in
, &done_head
);
1983 bitmap_clear (&done_head
);
1985 mark_set_for_renaming (sra_candidates
);
1988 fputc ('\n', dump_file
);
1992 /* Phase Four: Update the function to match the replacements created. */
1994 /* Mark all the variables in VDEF/VUSE operators for STMT for
1995 renaming. This becomes necessary when we modify all of a
1999 mark_all_v_defs_1 (tree stmt
)
2004 update_stmt_if_modified (stmt
);
2006 FOR_EACH_SSA_TREE_OPERAND (sym
, stmt
, iter
, SSA_OP_ALL_VIRTUALS
)
2008 if (TREE_CODE (sym
) == SSA_NAME
)
2009 sym
= SSA_NAME_VAR (sym
);
2010 mark_sym_for_renaming (sym
);
2015 /* Mark all the variables in virtual operands in all the statements in
2016 LIST for renaming. */
2019 mark_all_v_defs (tree list
)
2021 if (TREE_CODE (list
) != STATEMENT_LIST
)
2022 mark_all_v_defs_1 (list
);
2025 tree_stmt_iterator i
;
2026 for (i
= tsi_start (list
); !tsi_end_p (i
); tsi_next (&i
))
2027 mark_all_v_defs_1 (tsi_stmt (i
));
2032 /* Mark every replacement under ELT with TREE_NO_WARNING. */
2035 mark_no_warning (struct sra_elt
*elt
)
2037 if (!elt
->all_no_warning
)
2039 if (elt
->replacement
)
2040 TREE_NO_WARNING (elt
->replacement
) = 1;
2044 FOR_EACH_ACTUAL_CHILD (c
, elt
)
2045 mark_no_warning (c
);
2047 elt
->all_no_warning
= true;
2051 /* Build a single level component reference to ELT rooted at BASE. */
2054 generate_one_element_ref (struct sra_elt
*elt
, tree base
)
2056 switch (TREE_CODE (TREE_TYPE (base
)))
2060 tree field
= elt
->element
;
2062 /* We can't test elt->in_bitfld_blk here because, when this is
2063 called from instantiate_element, we haven't set this field
2065 if (TREE_CODE (field
) == BIT_FIELD_REF
)
2067 tree ret
= unshare_expr (field
);
2068 TREE_OPERAND (ret
, 0) = base
;
2072 /* Watch out for compatible records with differing field lists. */
2073 if (DECL_FIELD_CONTEXT (field
) != TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
2074 field
= find_compatible_field (TREE_TYPE (base
), field
);
2076 return build3 (COMPONENT_REF
, elt
->type
, base
, field
, NULL
);
2080 if (TREE_CODE (elt
->element
) == RANGE_EXPR
)
2081 return build4 (ARRAY_RANGE_REF
, elt
->type
, base
,
2082 TREE_OPERAND (elt
->element
, 0), NULL
, NULL
);
2084 return build4 (ARRAY_REF
, elt
->type
, base
, elt
->element
, NULL
, NULL
);
2087 if (elt
->element
== integer_zero_node
)
2088 return build1 (REALPART_EXPR
, elt
->type
, base
);
2090 return build1 (IMAGPART_EXPR
, elt
->type
, base
);
2097 /* Build a full component reference to ELT rooted at its native variable. */
2100 generate_element_ref (struct sra_elt
*elt
)
2103 return generate_one_element_ref (elt
, generate_element_ref (elt
->parent
));
2105 return elt
->element
;
2108 /* Return true if BF is a bit-field that we can handle like a scalar. */
2111 scalar_bitfield_p (tree bf
)
2113 return (TREE_CODE (bf
) == BIT_FIELD_REF
2114 && (is_gimple_reg (TREE_OPERAND (bf
, 0))
2115 || (TYPE_MODE (TREE_TYPE (TREE_OPERAND (bf
, 0))) != BLKmode
2116 && (!TREE_SIDE_EFFECTS (TREE_OPERAND (bf
, 0))
2117 || (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE
2118 (TREE_OPERAND (bf
, 0))))
2119 <= BITS_PER_WORD
)))));
2122 /* Create an assignment statement from SRC to DST. */
2125 sra_build_assignment (tree dst
, tree src
)
2127 /* Turning BIT_FIELD_REFs into bit operations enables other passes
2128 to do a much better job at optimizing the code.
2129 From dst = BIT_FIELD_REF <var, sz, off> we produce
2131 SR.1 = (scalar type) var;
2133 SR.3 = SR.2 & ((1 << sz) - 1);
2134 ... possible sign extension of SR.3 ...
2135 dst = (destination type) SR.3;
2137 if (scalar_bitfield_p (src
))
2139 tree var
, shift
, width
;
2140 tree utype
, stype
, stmp
, utmp
;
2142 bool unsignedp
= BIT_FIELD_REF_UNSIGNED (src
);
2144 var
= TREE_OPERAND (src
, 0);
2145 width
= TREE_OPERAND (src
, 1);
2146 /* The offset needs to be adjusted to a right shift quantity
2147 depending on the endianess. */
2148 if (BYTES_BIG_ENDIAN
)
2150 tree tmp
= size_binop (PLUS_EXPR
, width
, TREE_OPERAND (src
, 2));
2151 shift
= size_binop (MINUS_EXPR
, TYPE_SIZE (TREE_TYPE (var
)), tmp
);
2154 shift
= TREE_OPERAND (src
, 2);
2156 /* In weird cases we have non-integral types for the source or
2158 ??? For unknown reasons we also want an unsigned scalar type. */
2159 stype
= TREE_TYPE (var
);
2160 if (!INTEGRAL_TYPE_P (stype
))
2161 stype
= lang_hooks
.types
.type_for_size (TREE_INT_CST_LOW
2162 (TYPE_SIZE (stype
)), 1);
2163 else if (!TYPE_UNSIGNED (stype
))
2164 stype
= unsigned_type_for (stype
);
2166 utype
= TREE_TYPE (dst
);
2167 if (!INTEGRAL_TYPE_P (utype
))
2168 utype
= lang_hooks
.types
.type_for_size (TREE_INT_CST_LOW
2169 (TYPE_SIZE (utype
)), 1);
2170 else if (!TYPE_UNSIGNED (utype
))
2171 utype
= unsigned_type_for (utype
);
2174 stmp
= make_rename_temp (stype
, "SR");
2176 /* Convert the base var of the BIT_FIELD_REF to the scalar type
2177 we use for computation if we cannot use it directly. */
2178 if (!useless_type_conversion_p (stype
, TREE_TYPE (var
)))
2180 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
2181 stmt
= build_gimple_modify_stmt (stmp
,
2182 fold_convert (stype
, var
));
2184 stmt
= build_gimple_modify_stmt (stmp
,
2185 fold_build1 (VIEW_CONVERT_EXPR
,
2187 append_to_statement_list (stmt
, &list
);
2191 if (!integer_zerop (shift
))
2193 stmt
= build_gimple_modify_stmt (stmp
,
2194 fold_build2 (RSHIFT_EXPR
, stype
,
2196 append_to_statement_list (stmt
, &list
);
2200 /* If we need a masking operation, produce one. */
2201 if (TREE_INT_CST_LOW (width
) == TYPE_PRECISION (stype
))
2205 tree one
= build_int_cst_wide (stype
, 1, 0);
2206 tree mask
= int_const_binop (LSHIFT_EXPR
, one
, width
, 0);
2207 mask
= int_const_binop (MINUS_EXPR
, mask
, one
, 0);
2209 stmt
= build_gimple_modify_stmt (stmp
,
2210 fold_build2 (BIT_AND_EXPR
, stype
,
2212 append_to_statement_list (stmt
, &list
);
2216 /* After shifting and masking, convert to the target type. */
2218 if (!useless_type_conversion_p (utype
, stype
))
2220 utmp
= make_rename_temp (utype
, "SR");
2222 stmt
= build_gimple_modify_stmt (utmp
, fold_convert (utype
, var
));
2223 append_to_statement_list (stmt
, &list
);
2228 /* Perform sign extension, if required.
2229 ??? This should never be necessary. */
2232 tree signbit
= int_const_binop (LSHIFT_EXPR
,
2233 build_int_cst_wide (utype
, 1, 0),
2234 size_binop (MINUS_EXPR
, width
,
2235 bitsize_int (1)), 0);
2237 stmt
= build_gimple_modify_stmt (utmp
,
2238 fold_build2 (BIT_XOR_EXPR
, utype
,
2240 append_to_statement_list (stmt
, &list
);
2242 stmt
= build_gimple_modify_stmt (utmp
,
2243 fold_build2 (MINUS_EXPR
, utype
,
2245 append_to_statement_list (stmt
, &list
);
2250 /* Finally, move and convert to the destination. */
2251 if (!useless_type_conversion_p (TREE_TYPE (dst
), TREE_TYPE (var
)))
2253 if (INTEGRAL_TYPE_P (TREE_TYPE (dst
)))
2254 var
= fold_convert (TREE_TYPE (dst
), var
);
2256 var
= fold_build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (dst
), var
);
2258 stmt
= build_gimple_modify_stmt (dst
, var
);
2259 append_to_statement_list (stmt
, &list
);
2264 /* It was hoped that we could perform some type sanity checking
2265 here, but since front-ends can emit accesses of fields in types
2266 different from their nominal types and copy structures containing
2267 them as a whole, we'd have to handle such differences here.
2268 Since such accesses under different types require compatibility
2269 anyway, there's little point in making tests and/or adding
2270 conversions to ensure the types of src and dst are the same.
2271 So we just assume type differences at this point are ok.
2272 The only exception we make here are pointer types, which can be different
2273 in e.g. structurally equal, but non-identical RECORD_TYPEs. */
2274 if (POINTER_TYPE_P (TREE_TYPE (dst
))
2275 && !useless_type_conversion_p (TREE_TYPE (dst
), TREE_TYPE (src
)))
2276 src
= fold_convert (TREE_TYPE (dst
), src
);
2278 return build_gimple_modify_stmt (dst
, src
);
2281 /* BIT_FIELD_REFs must not be shared. sra_build_elt_assignment()
2282 takes care of assignments, but we must create copies for uses. */
2283 #define REPLDUP(t) (TREE_CODE (t) != BIT_FIELD_REF ? (t) : unshare_expr (t))
2285 /* Emit an assignment from SRC to DST, but if DST is a scalarizable
2286 BIT_FIELD_REF, turn it into bit operations. */
2289 sra_build_bf_assignment (tree dst
, tree src
)
2291 tree var
, type
, utype
, tmp
, tmp2
, tmp3
;
2293 tree cst
, cst2
, mask
;
2294 tree minshift
, maxshift
;
2296 if (TREE_CODE (dst
) != BIT_FIELD_REF
)
2297 return sra_build_assignment (dst
, src
);
2299 var
= TREE_OPERAND (dst
, 0);
2301 if (!scalar_bitfield_p (dst
))
2302 return sra_build_assignment (REPLDUP (dst
), src
);
2306 cst
= fold_convert (bitsizetype
, TREE_OPERAND (dst
, 2));
2307 cst2
= size_binop (PLUS_EXPR
,
2308 fold_convert (bitsizetype
, TREE_OPERAND (dst
, 1)),
2311 if (BYTES_BIG_ENDIAN
)
2313 maxshift
= size_binop (MINUS_EXPR
, TYPE_SIZE (TREE_TYPE (var
)), cst
);
2314 minshift
= size_binop (MINUS_EXPR
, TYPE_SIZE (TREE_TYPE (var
)), cst2
);
2322 type
= TREE_TYPE (var
);
2323 if (!INTEGRAL_TYPE_P (type
))
2324 type
= lang_hooks
.types
.type_for_size
2325 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (var
))), 1);
2326 if (TYPE_UNSIGNED (type
))
2329 utype
= unsigned_type_for (type
);
2331 mask
= build_int_cst_wide (utype
, 1, 0);
2332 if (TREE_INT_CST_LOW (maxshift
) == TYPE_PRECISION (utype
))
2333 cst
= build_int_cst_wide (utype
, 0, 0);
2335 cst
= int_const_binop (LSHIFT_EXPR
, mask
, maxshift
, true);
2336 if (integer_zerop (minshift
))
2339 cst2
= int_const_binop (LSHIFT_EXPR
, mask
, minshift
, true);
2340 mask
= int_const_binop (MINUS_EXPR
, cst
, cst2
, true);
2341 mask
= fold_build1 (BIT_NOT_EXPR
, utype
, mask
);
2343 if (TYPE_MAIN_VARIANT (utype
) != TYPE_MAIN_VARIANT (TREE_TYPE (var
))
2344 && !integer_zerop (mask
))
2347 if (!is_gimple_variable (tmp
))
2348 tmp
= unshare_expr (var
);
2350 tmp2
= make_rename_temp (utype
, "SR");
2352 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
2353 stmt
= build_gimple_modify_stmt (tmp2
, fold_convert (utype
, tmp
));
2355 stmt
= build_gimple_modify_stmt (tmp2
, fold_build1 (VIEW_CONVERT_EXPR
,
2357 append_to_statement_list (stmt
, &list
);
2362 if (!integer_zerop (mask
))
2364 tmp
= make_rename_temp (utype
, "SR");
2365 stmt
= build_gimple_modify_stmt (tmp
,
2366 fold_build2 (BIT_AND_EXPR
, utype
,
2368 append_to_statement_list (stmt
, &list
);
2373 if (is_gimple_reg (src
) && INTEGRAL_TYPE_P (TREE_TYPE (src
)))
2375 else if (INTEGRAL_TYPE_P (TREE_TYPE (src
)))
2377 tmp2
= make_rename_temp (TREE_TYPE (src
), "SR");
2378 stmt
= sra_build_assignment (tmp2
, src
);
2379 append_to_statement_list (stmt
, &list
);
2383 tmp2
= make_rename_temp
2384 (lang_hooks
.types
.type_for_size
2385 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (src
))),
2387 stmt
= sra_build_assignment (tmp2
, fold_build1 (VIEW_CONVERT_EXPR
,
2388 TREE_TYPE (tmp2
), src
));
2389 append_to_statement_list (stmt
, &list
);
2392 if (!TYPE_UNSIGNED (TREE_TYPE (tmp2
)))
2394 tree ut
= unsigned_type_for (TREE_TYPE (tmp2
));
2395 tmp3
= make_rename_temp (ut
, "SR");
2396 tmp2
= fold_convert (ut
, tmp2
);
2397 stmt
= sra_build_assignment (tmp3
, tmp2
);
2398 append_to_statement_list (stmt
, &list
);
2400 tmp2
= fold_build1 (BIT_NOT_EXPR
, utype
, mask
);
2401 tmp2
= int_const_binop (RSHIFT_EXPR
, tmp2
, minshift
, true);
2402 tmp2
= fold_convert (ut
, tmp2
);
2403 tmp2
= fold_build2 (BIT_AND_EXPR
, ut
, tmp3
, tmp2
);
2407 tmp3
= make_rename_temp (ut
, "SR");
2408 stmt
= sra_build_assignment (tmp3
, tmp2
);
2409 append_to_statement_list (stmt
, &list
);
2415 if (TYPE_MAIN_VARIANT (TREE_TYPE (tmp2
)) != TYPE_MAIN_VARIANT (utype
))
2417 tmp3
= make_rename_temp (utype
, "SR");
2418 tmp2
= fold_convert (utype
, tmp2
);
2419 stmt
= sra_build_assignment (tmp3
, tmp2
);
2420 append_to_statement_list (stmt
, &list
);
2424 if (!integer_zerop (minshift
))
2426 tmp3
= make_rename_temp (utype
, "SR");
2427 stmt
= build_gimple_modify_stmt (tmp3
,
2428 fold_build2 (LSHIFT_EXPR
, utype
,
2430 append_to_statement_list (stmt
, &list
);
2434 if (utype
!= TREE_TYPE (var
))
2435 tmp3
= make_rename_temp (utype
, "SR");
2438 stmt
= build_gimple_modify_stmt (tmp3
,
2439 fold_build2 (BIT_IOR_EXPR
, utype
,
2441 append_to_statement_list (stmt
, &list
);
2445 if (TREE_TYPE (var
) == type
)
2446 stmt
= build_gimple_modify_stmt (var
,
2447 fold_convert (type
, tmp3
));
2449 stmt
= build_gimple_modify_stmt (var
,
2450 fold_build1 (VIEW_CONVERT_EXPR
,
2451 TREE_TYPE (var
), tmp3
));
2452 append_to_statement_list (stmt
, &list
);
2458 /* Expand an assignment of SRC to the scalarized representation of
2459 ELT. If it is a field group, try to widen the assignment to cover
2460 the full variable. */
2463 sra_build_elt_assignment (struct sra_elt
*elt
, tree src
)
2465 tree dst
= elt
->replacement
;
2466 tree var
, tmp
, cst
, cst2
, list
, stmt
;
2468 if (TREE_CODE (dst
) != BIT_FIELD_REF
2469 || !elt
->in_bitfld_block
)
2470 return sra_build_assignment (REPLDUP (dst
), src
);
2472 var
= TREE_OPERAND (dst
, 0);
2474 /* Try to widen the assignment to the entire variable.
2475 We need the source to be a BIT_FIELD_REF as well, such that, for
2476 BIT_FIELD_REF<d,sz,dp> = BIT_FIELD_REF<s,sz,sp>,
2477 by design, conditions are met such that we can turn it into
2478 d = BIT_FIELD_REF<s,dw,sp-dp>. */
2479 if (elt
->in_bitfld_block
== 2
2480 && TREE_CODE (src
) == BIT_FIELD_REF
)
2482 cst
= TYPE_SIZE (TREE_TYPE (var
));
2483 cst2
= size_binop (MINUS_EXPR
, TREE_OPERAND (src
, 2),
2484 TREE_OPERAND (dst
, 2));
2486 src
= TREE_OPERAND (src
, 0);
2488 /* Avoid full-width bit-fields. */
2489 if (integer_zerop (cst2
)
2490 && tree_int_cst_equal (cst
, TYPE_SIZE (TREE_TYPE (src
))))
2492 if (INTEGRAL_TYPE_P (TREE_TYPE (src
))
2493 && !TYPE_UNSIGNED (TREE_TYPE (src
)))
2494 src
= fold_convert (unsigned_type_for (TREE_TYPE (src
)), src
);
2496 /* If a single conversion won't do, we'll need a statement
2498 if (TYPE_MAIN_VARIANT (TREE_TYPE (var
))
2499 != TYPE_MAIN_VARIANT (TREE_TYPE (src
)))
2503 if (!INTEGRAL_TYPE_P (TREE_TYPE (src
)))
2504 src
= fold_build1 (VIEW_CONVERT_EXPR
,
2505 lang_hooks
.types
.type_for_size
2507 (TYPE_SIZE (TREE_TYPE (src
))),
2509 gcc_assert (TYPE_UNSIGNED (TREE_TYPE (src
)));
2511 tmp
= make_rename_temp (TREE_TYPE (src
), "SR");
2512 stmt
= build_gimple_modify_stmt (tmp
, src
);
2513 append_to_statement_list (stmt
, &list
);
2515 stmt
= sra_build_assignment (var
,
2516 fold_convert (TREE_TYPE (var
),
2518 append_to_statement_list (stmt
, &list
);
2523 src
= fold_convert (TREE_TYPE (var
), src
);
2527 src
= fold_build3 (BIT_FIELD_REF
, TREE_TYPE (var
), src
, cst
, cst2
);
2528 BIT_FIELD_REF_UNSIGNED (src
) = 1;
2531 return sra_build_assignment (var
, src
);
2534 return sra_build_bf_assignment (dst
, src
);
2537 /* Generate a set of assignment statements in *LIST_P to copy all
2538 instantiated elements under ELT to or from the equivalent structure
2539 rooted at EXPR. COPY_OUT controls the direction of the copy, with
2540 true meaning to copy out of EXPR into ELT. */
2543 generate_copy_inout (struct sra_elt
*elt
, bool copy_out
, tree expr
,
2549 if (!copy_out
&& TREE_CODE (expr
) == SSA_NAME
2550 && TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
2554 c
= lookup_element (elt
, integer_zero_node
, NULL
, NO_INSERT
);
2556 c
= lookup_element (elt
, integer_one_node
, NULL
, NO_INSERT
);
2559 t
= build2 (COMPLEX_EXPR
, elt
->type
, r
, i
);
2560 t
= sra_build_bf_assignment (expr
, t
);
2561 SSA_NAME_DEF_STMT (expr
) = t
;
2562 append_to_statement_list (t
, list_p
);
2564 else if (elt
->replacement
)
2567 t
= sra_build_elt_assignment (elt
, expr
);
2569 t
= sra_build_bf_assignment (expr
, REPLDUP (elt
->replacement
));
2570 append_to_statement_list (t
, list_p
);
2574 FOR_EACH_ACTUAL_CHILD (c
, elt
)
2576 t
= generate_one_element_ref (c
, unshare_expr (expr
));
2577 generate_copy_inout (c
, copy_out
, t
, list_p
);
2582 /* Generate a set of assignment statements in *LIST_P to copy all instantiated
2583 elements under SRC to their counterparts under DST. There must be a 1-1
2584 correspondence of instantiated elements. */
2587 generate_element_copy (struct sra_elt
*dst
, struct sra_elt
*src
, tree
*list_p
)
2589 struct sra_elt
*dc
, *sc
;
2591 FOR_EACH_ACTUAL_CHILD (dc
, dst
)
2593 sc
= lookup_element (src
, dc
->element
, NULL
, NO_INSERT
);
2594 if (!sc
&& dc
->in_bitfld_block
== 2)
2596 struct sra_elt
*dcs
;
2598 FOR_EACH_ACTUAL_CHILD (dcs
, dc
)
2600 sc
= lookup_element (src
, dcs
->element
, NULL
, NO_INSERT
);
2602 generate_element_copy (dcs
, sc
, list_p
);
2608 /* If DST and SRC are structs with the same elements, but do not have
2609 the same TYPE_MAIN_VARIANT, then lookup of DST FIELD_DECL in SRC
2610 will fail. Try harder by finding the corresponding FIELD_DECL
2616 gcc_assert (useless_type_conversion_p (dst
->type
, src
->type
));
2617 gcc_assert (TREE_CODE (dc
->element
) == FIELD_DECL
);
2618 for (f
= TYPE_FIELDS (src
->type
); f
; f
= TREE_CHAIN (f
))
2619 if (simple_cst_equal (DECL_FIELD_OFFSET (f
),
2620 DECL_FIELD_OFFSET (dc
->element
)) > 0
2621 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (f
),
2622 DECL_FIELD_BIT_OFFSET (dc
->element
)) > 0
2623 && simple_cst_equal (DECL_SIZE (f
),
2624 DECL_SIZE (dc
->element
)) > 0
2625 && (useless_type_conversion_p (TREE_TYPE (dc
->element
),
2627 || (POINTER_TYPE_P (TREE_TYPE (dc
->element
))
2628 && POINTER_TYPE_P (TREE_TYPE (f
)))))
2630 gcc_assert (f
!= NULL_TREE
);
2631 sc
= lookup_element (src
, f
, NULL
, NO_INSERT
);
2634 generate_element_copy (dc
, sc
, list_p
);
2637 if (dst
->replacement
)
2641 gcc_assert (src
->replacement
);
2643 t
= sra_build_elt_assignment (dst
, REPLDUP (src
->replacement
));
2644 append_to_statement_list (t
, list_p
);
2648 /* Generate a set of assignment statements in *LIST_P to zero all instantiated
2649 elements under ELT. In addition, do not assign to elements that have been
2650 marked VISITED but do reset the visited flag; this allows easy coordination
2651 with generate_element_init. */
2654 generate_element_zero (struct sra_elt
*elt
, tree
*list_p
)
2660 elt
->visited
= false;
2664 if (!elt
->in_bitfld_block
)
2665 FOR_EACH_ACTUAL_CHILD (c
, elt
)
2666 generate_element_zero (c
, list_p
);
2668 if (elt
->replacement
)
2672 gcc_assert (elt
->is_scalar
);
2673 t
= fold_convert (elt
->type
, integer_zero_node
);
2675 t
= sra_build_elt_assignment (elt
, t
);
2676 append_to_statement_list (t
, list_p
);
2680 /* Generate an assignment VAR = INIT, where INIT may need gimplification.
2681 Add the result to *LIST_P. */
2684 generate_one_element_init (struct sra_elt
*elt
, tree init
, tree
*list_p
)
2686 /* The replacement can be almost arbitrarily complex. Gimplify. */
2687 tree stmt
= sra_build_elt_assignment (elt
, init
);
2688 gimplify_and_add (stmt
, list_p
);
2691 /* Generate a set of assignment statements in *LIST_P to set all instantiated
2692 elements under ELT with the contents of the initializer INIT. In addition,
2693 mark all assigned elements VISITED; this allows easy coordination with
2694 generate_element_zero. Return false if we found a case we couldn't
2698 generate_element_init_1 (struct sra_elt
*elt
, tree init
, tree
*list_p
)
2701 enum tree_code init_code
;
2702 struct sra_elt
*sub
;
2704 unsigned HOST_WIDE_INT idx
;
2705 tree value
, purpose
;
2707 /* We can be passed DECL_INITIAL of a static variable. It might have a
2708 conversion, which we strip off here. */
2709 STRIP_USELESS_TYPE_CONVERSION (init
);
2710 init_code
= TREE_CODE (init
);
2714 if (elt
->replacement
)
2716 generate_one_element_init (elt
, init
, list_p
);
2717 elt
->visited
= true;
2726 FOR_EACH_ACTUAL_CHILD (sub
, elt
)
2728 if (sub
->element
== integer_zero_node
)
2729 t
= (init_code
== COMPLEX_EXPR
2730 ? TREE_OPERAND (init
, 0) : TREE_REALPART (init
));
2732 t
= (init_code
== COMPLEX_EXPR
2733 ? TREE_OPERAND (init
, 1) : TREE_IMAGPART (init
));
2734 result
&= generate_element_init_1 (sub
, t
, list_p
);
2739 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init
), idx
, purpose
, value
)
2741 if (TREE_CODE (purpose
) == RANGE_EXPR
)
2743 tree lower
= TREE_OPERAND (purpose
, 0);
2744 tree upper
= TREE_OPERAND (purpose
, 1);
2748 sub
= lookup_element (elt
, lower
, NULL
, NO_INSERT
);
2750 result
&= generate_element_init_1 (sub
, value
, list_p
);
2751 if (tree_int_cst_equal (lower
, upper
))
2753 lower
= int_const_binop (PLUS_EXPR
, lower
,
2754 integer_one_node
, true);
2759 sub
= lookup_element (elt
, purpose
, NULL
, NO_INSERT
);
2761 result
&= generate_element_init_1 (sub
, value
, list_p
);
2767 elt
->visited
= true;
2774 /* A wrapper function for generate_element_init_1 that handles cleanup after
2778 generate_element_init (struct sra_elt
*elt
, tree init
, tree
*list_p
)
2782 push_gimplify_context ();
2783 ret
= generate_element_init_1 (elt
, init
, list_p
);
2784 pop_gimplify_context (NULL
);
2786 /* The replacement can expose previously unreferenced variables. */
2789 tree_stmt_iterator i
;
2791 for (i
= tsi_start (*list_p
); !tsi_end_p (i
); tsi_next (&i
))
2792 find_new_referenced_vars (tsi_stmt_ptr (i
));
2798 /* Insert STMT on all the outgoing edges out of BB. Note that if BB
2799 has more than one edge, STMT will be replicated for each edge. Also,
2800 abnormal edges will be ignored. */
2803 insert_edge_copies (tree stmt
, basic_block bb
)
2810 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2812 /* We don't need to insert copies on abnormal edges. The
2813 value of the scalar replacement is not guaranteed to
2814 be valid through an abnormal edge. */
2815 if (!(e
->flags
& EDGE_ABNORMAL
))
2819 bsi_insert_on_edge (e
, stmt
);
2823 bsi_insert_on_edge (e
, unsave_expr_now (stmt
));
2828 /* Helper function to insert LIST before BSI, and set up line number info. */
2831 sra_insert_before (block_stmt_iterator
*bsi
, tree list
)
2833 tree stmt
= bsi_stmt (*bsi
);
2835 if (EXPR_HAS_LOCATION (stmt
))
2836 annotate_all_with_locus (&list
, EXPR_LOCATION (stmt
));
2837 bsi_insert_before (bsi
, list
, BSI_SAME_STMT
);
2840 /* Similarly, but insert after BSI. Handles insertion onto edges as well. */
2843 sra_insert_after (block_stmt_iterator
*bsi
, tree list
)
2845 tree stmt
= bsi_stmt (*bsi
);
2847 if (EXPR_HAS_LOCATION (stmt
))
2848 annotate_all_with_locus (&list
, EXPR_LOCATION (stmt
));
2850 if (stmt_ends_bb_p (stmt
))
2851 insert_edge_copies (list
, bsi
->bb
);
2853 bsi_insert_after (bsi
, list
, BSI_SAME_STMT
);
2856 /* Similarly, but replace the statement at BSI. */
2859 sra_replace (block_stmt_iterator
*bsi
, tree list
)
2861 sra_insert_before (bsi
, list
);
2862 bsi_remove (bsi
, false);
2863 if (bsi_end_p (*bsi
))
2864 *bsi
= bsi_last (bsi
->bb
);
2869 /* Data structure that bitfield_overlaps_p fills in with information
2870 about the element passed in and how much of it overlaps with the
2871 bit-range passed it to. */
2873 struct bitfield_overlap_info
2875 /* The bit-length of an element. */
2878 /* The bit-position of the element in its parent. */
2881 /* The number of bits of the element that overlap with the incoming
2885 /* The first bit of the element that overlaps with the incoming bit
2890 /* Return true if a BIT_FIELD_REF<(FLD->parent), BLEN, BPOS>
2891 expression (referenced as BF below) accesses any of the bits in FLD,
2892 false if it doesn't. If DATA is non-null, its field_len and
2893 field_pos are filled in such that BIT_FIELD_REF<(FLD->parent),
2894 field_len, field_pos> (referenced as BFLD below) represents the
2895 entire field FLD->element, and BIT_FIELD_REF<BFLD, overlap_len,
2896 overlap_pos> represents the portion of the entire field that
2897 overlaps with BF. */
2900 bitfield_overlaps_p (tree blen
, tree bpos
, struct sra_elt
*fld
,
2901 struct bitfield_overlap_info
*data
)
2906 if (TREE_CODE (fld
->element
) == FIELD_DECL
)
2908 flen
= fold_convert (bitsizetype
, DECL_SIZE (fld
->element
));
2909 fpos
= fold_convert (bitsizetype
, DECL_FIELD_OFFSET (fld
->element
));
2910 fpos
= size_binop (MULT_EXPR
, fpos
, bitsize_int (BITS_PER_UNIT
));
2911 fpos
= size_binop (PLUS_EXPR
, fpos
, DECL_FIELD_BIT_OFFSET (fld
->element
));
2913 else if (TREE_CODE (fld
->element
) == BIT_FIELD_REF
)
2915 flen
= fold_convert (bitsizetype
, TREE_OPERAND (fld
->element
, 1));
2916 fpos
= fold_convert (bitsizetype
, TREE_OPERAND (fld
->element
, 2));
2918 else if (TREE_CODE (fld
->element
) == INTEGER_CST
)
2920 flen
= fold_convert (bitsizetype
, TYPE_SIZE (fld
->type
));
2921 fpos
= fold_convert (bitsizetype
, fld
->element
);
2922 fpos
= size_binop (MULT_EXPR
, flen
, fpos
);
2927 gcc_assert (host_integerp (blen
, 1)
2928 && host_integerp (bpos
, 1)
2929 && host_integerp (flen
, 1)
2930 && host_integerp (fpos
, 1));
2932 ret
= ((!tree_int_cst_lt (fpos
, bpos
)
2933 && tree_int_cst_lt (size_binop (MINUS_EXPR
, fpos
, bpos
),
2935 || (!tree_int_cst_lt (bpos
, fpos
)
2936 && tree_int_cst_lt (size_binop (MINUS_EXPR
, bpos
, fpos
),
2946 data
->field_len
= flen
;
2947 data
->field_pos
= fpos
;
2949 fend
= size_binop (PLUS_EXPR
, fpos
, flen
);
2950 bend
= size_binop (PLUS_EXPR
, bpos
, blen
);
2952 if (tree_int_cst_lt (bend
, fend
))
2953 data
->overlap_len
= size_binop (MINUS_EXPR
, bend
, fpos
);
2955 data
->overlap_len
= NULL
;
2957 if (tree_int_cst_lt (fpos
, bpos
))
2959 data
->overlap_pos
= size_binop (MINUS_EXPR
, bpos
, fpos
);
2960 data
->overlap_len
= size_binop (MINUS_EXPR
,
2967 data
->overlap_pos
= NULL
;
2973 /* Add to LISTP a sequence of statements that copies BLEN bits between
2974 VAR and the scalarized elements of ELT, starting a bit VPOS of VAR
2975 and at bit BPOS of ELT. The direction of the copy is given by
2979 sra_explode_bitfield_assignment (tree var
, tree vpos
, bool to_var
,
2980 tree
*listp
, tree blen
, tree bpos
,
2981 struct sra_elt
*elt
)
2983 struct sra_elt
*fld
;
2984 struct bitfield_overlap_info flp
;
2986 FOR_EACH_ACTUAL_CHILD (fld
, elt
)
2990 if (!bitfield_overlaps_p (blen
, bpos
, fld
, &flp
))
2993 flen
= flp
.overlap_len
? flp
.overlap_len
: flp
.field_len
;
2994 fpos
= flp
.overlap_pos
? flp
.overlap_pos
: bitsize_int (0);
2996 if (fld
->replacement
)
2998 tree infld
, invar
, st
, type
;
3000 infld
= fld
->replacement
;
3002 type
= TREE_TYPE (infld
);
3003 if (TYPE_PRECISION (type
) != TREE_INT_CST_LOW (flen
))
3004 type
= lang_hooks
.types
.type_for_size (TREE_INT_CST_LOW (flen
), 1);
3006 if (TREE_CODE (infld
) == BIT_FIELD_REF
)
3008 fpos
= size_binop (PLUS_EXPR
, fpos
, TREE_OPERAND (infld
, 2));
3009 infld
= TREE_OPERAND (infld
, 0);
3011 else if (BYTES_BIG_ENDIAN
&& DECL_P (fld
->element
)
3012 && !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (infld
)),
3013 DECL_SIZE (fld
->element
)))
3015 fpos
= size_binop (PLUS_EXPR
, fpos
,
3016 TYPE_SIZE (TREE_TYPE (infld
)));
3017 fpos
= size_binop (MINUS_EXPR
, fpos
,
3018 DECL_SIZE (fld
->element
));
3021 infld
= fold_build3 (BIT_FIELD_REF
, type
, infld
, flen
, fpos
);
3022 BIT_FIELD_REF_UNSIGNED (infld
) = 1;
3024 invar
= size_binop (MINUS_EXPR
, flp
.field_pos
, bpos
);
3025 if (flp
.overlap_pos
)
3026 invar
= size_binop (PLUS_EXPR
, invar
, flp
.overlap_pos
);
3027 invar
= size_binop (PLUS_EXPR
, invar
, vpos
);
3029 invar
= fold_build3 (BIT_FIELD_REF
, type
, var
, flen
, invar
);
3030 BIT_FIELD_REF_UNSIGNED (invar
) = 1;
3033 st
= sra_build_bf_assignment (invar
, infld
);
3035 st
= sra_build_bf_assignment (infld
, invar
);
3037 append_to_statement_list (st
, listp
);
3041 tree sub
= size_binop (MINUS_EXPR
, flp
.field_pos
, bpos
);
3042 sub
= size_binop (PLUS_EXPR
, vpos
, sub
);
3043 if (flp
.overlap_pos
)
3044 sub
= size_binop (PLUS_EXPR
, sub
, flp
.overlap_pos
);
3046 sra_explode_bitfield_assignment (var
, sub
, to_var
, listp
,
3052 /* Add to LISTBEFOREP statements that copy scalarized members of ELT
3053 that overlap with BIT_FIELD_REF<(ELT->element), BLEN, BPOS> back
3054 into the full variable, and to LISTAFTERP, if non-NULL, statements
3055 that copy the (presumably modified) overlapping portions of the
3056 full variable back to the scalarized variables. */
3059 sra_sync_for_bitfield_assignment (tree
*listbeforep
, tree
*listafterp
,
3060 tree blen
, tree bpos
,
3061 struct sra_elt
*elt
)
3063 struct sra_elt
*fld
;
3064 struct bitfield_overlap_info flp
;
3066 FOR_EACH_ACTUAL_CHILD (fld
, elt
)
3067 if (bitfield_overlaps_p (blen
, bpos
, fld
, &flp
))
3069 if (fld
->replacement
|| (!flp
.overlap_len
&& !flp
.overlap_pos
))
3071 generate_copy_inout (fld
, false, generate_element_ref (fld
),
3073 mark_no_warning (fld
);
3075 generate_copy_inout (fld
, true, generate_element_ref (fld
),
3080 tree flen
= flp
.overlap_len
? flp
.overlap_len
: flp
.field_len
;
3081 tree fpos
= flp
.overlap_pos
? flp
.overlap_pos
: bitsize_int (0);
3083 sra_sync_for_bitfield_assignment (listbeforep
, listafterp
,
3089 /* Scalarize a USE. To recap, this is either a simple reference to ELT,
3090 if elt is scalar, or some occurrence of ELT that requires a complete
3091 aggregate. IS_OUTPUT is true if ELT is being modified. */
3094 scalarize_use (struct sra_elt
*elt
, tree
*expr_p
, block_stmt_iterator
*bsi
,
3095 bool is_output
, bool use_all
)
3097 tree stmt
= bsi_stmt (*bsi
);
3100 if (elt
->replacement
)
3102 tree replacement
= elt
->replacement
;
3104 /* If we have a replacement, then updating the reference is as
3105 simple as modifying the existing statement in place. */
3107 && TREE_CODE (elt
->replacement
) == BIT_FIELD_REF
3108 && is_gimple_reg (TREE_OPERAND (elt
->replacement
, 0))
3109 && TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
3110 && &GIMPLE_STMT_OPERAND (stmt
, 0) == expr_p
)
3112 tree newstmt
= sra_build_elt_assignment
3113 (elt
, GIMPLE_STMT_OPERAND (stmt
, 1));
3114 if (TREE_CODE (newstmt
) != STATEMENT_LIST
)
3117 append_to_statement_list (newstmt
, &list
);
3120 sra_replace (bsi
, newstmt
);
3124 && TREE_CODE (elt
->replacement
) == BIT_FIELD_REF
3125 && TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
3126 && &GIMPLE_STMT_OPERAND (stmt
, 1) == expr_p
)
3128 tree tmp
= make_rename_temp
3129 (TREE_TYPE (GIMPLE_STMT_OPERAND (stmt
, 0)), "SR");
3130 tree newstmt
= sra_build_assignment (tmp
, REPLDUP (elt
->replacement
));
3132 if (TREE_CODE (newstmt
) != STATEMENT_LIST
)
3135 append_to_statement_list (newstmt
, &list
);
3138 sra_insert_before (bsi
, newstmt
);
3142 mark_all_v_defs (stmt
);
3143 *expr_p
= REPLDUP (replacement
);
3146 else if (use_all
&& is_output
3147 && TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
3148 && TREE_CODE (bfexpr
3149 = GIMPLE_STMT_OPERAND (stmt
, 0)) == BIT_FIELD_REF
3150 && &TREE_OPERAND (bfexpr
, 0) == expr_p
3151 && INTEGRAL_TYPE_P (TREE_TYPE (bfexpr
))
3152 && TREE_CODE (TREE_TYPE (*expr_p
)) == RECORD_TYPE
)
3154 tree listbefore
= NULL
, listafter
= NULL
;
3155 tree blen
= fold_convert (bitsizetype
, TREE_OPERAND (bfexpr
, 1));
3156 tree bpos
= fold_convert (bitsizetype
, TREE_OPERAND (bfexpr
, 2));
3157 bool update
= false;
3159 if (!elt
->use_block_copy
)
3161 tree type
= TREE_TYPE (bfexpr
);
3162 tree var
= make_rename_temp (type
, "SR"), tmp
, st
, vpos
;
3164 GIMPLE_STMT_OPERAND (stmt
, 0) = var
;
3167 if (!TYPE_UNSIGNED (type
))
3169 type
= unsigned_type_for (type
);
3170 tmp
= make_rename_temp (type
, "SR");
3171 st
= build_gimple_modify_stmt (tmp
,
3172 fold_convert (type
, var
));
3173 append_to_statement_list (st
, &listafter
);
3177 /* If VAR is wider than BLEN bits, it is padded at the
3178 most-significant end. We want to set VPOS such that
3179 <BIT_FIELD_REF VAR BLEN VPOS> would refer to the
3180 least-significant BLEN bits of VAR. */
3181 if (BYTES_BIG_ENDIAN
)
3182 vpos
= size_binop (MINUS_EXPR
, TYPE_SIZE (type
), blen
);
3184 vpos
= bitsize_int (0);
3185 sra_explode_bitfield_assignment
3186 (var
, vpos
, false, &listafter
, blen
, bpos
, elt
);
3189 sra_sync_for_bitfield_assignment
3190 (&listbefore
, &listafter
, blen
, bpos
, elt
);
3194 mark_all_v_defs (listbefore
);
3195 sra_insert_before (bsi
, listbefore
);
3199 mark_all_v_defs (listafter
);
3200 sra_insert_after (bsi
, listafter
);
3206 else if (use_all
&& !is_output
3207 && TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
3208 && TREE_CODE (bfexpr
3209 = GIMPLE_STMT_OPERAND (stmt
, 1)) == BIT_FIELD_REF
3210 && &TREE_OPERAND (GIMPLE_STMT_OPERAND (stmt
, 1), 0) == expr_p
3211 && INTEGRAL_TYPE_P (TREE_TYPE (bfexpr
))
3212 && TREE_CODE (TREE_TYPE (*expr_p
)) == RECORD_TYPE
)
3215 tree blen
= fold_convert (bitsizetype
, TREE_OPERAND (bfexpr
, 1));
3216 tree bpos
= fold_convert (bitsizetype
, TREE_OPERAND (bfexpr
, 2));
3217 bool update
= false;
3219 if (!elt
->use_block_copy
)
3221 tree type
= TREE_TYPE (bfexpr
);
3224 if (!TYPE_UNSIGNED (type
))
3225 type
= unsigned_type_for (type
);
3227 var
= make_rename_temp (type
, "SR");
3229 append_to_statement_list (build_gimple_modify_stmt
3230 (var
, build_int_cst_wide (type
, 0, 0)),
3233 /* If VAR is wider than BLEN bits, it is padded at the
3234 most-significant end. We want to set VPOS such that
3235 <BIT_FIELD_REF VAR BLEN VPOS> would refer to the
3236 least-significant BLEN bits of VAR. */
3237 if (BYTES_BIG_ENDIAN
)
3238 vpos
= size_binop (MINUS_EXPR
, TYPE_SIZE (type
), blen
);
3240 vpos
= bitsize_int (0);
3241 sra_explode_bitfield_assignment
3242 (var
, vpos
, true, &list
, blen
, bpos
, elt
);
3244 GIMPLE_STMT_OPERAND (stmt
, 1) = var
;
3248 sra_sync_for_bitfield_assignment
3249 (&list
, NULL
, blen
, bpos
, elt
);
3253 mark_all_v_defs (list
);
3254 sra_insert_before (bsi
, list
);
3264 /* Otherwise we need some copies. If ELT is being read, then we
3265 want to store all (modified) sub-elements back into the
3266 structure before the reference takes place. If ELT is being
3267 written, then we want to load the changed values back into
3268 our shadow variables. */
3269 /* ??? We don't check modified for reads, we just always write all of
3270 the values. We should be able to record the SSA number of the VOP
3271 for which the values were last read. If that number matches the
3272 SSA number of the VOP in the current statement, then we needn't
3273 emit an assignment. This would also eliminate double writes when
3274 a structure is passed as more than one argument to a function call.
3275 This optimization would be most effective if sra_walk_function
3276 processed the blocks in dominator order. */
3278 generate_copy_inout (elt
, is_output
, generate_element_ref (elt
), &list
);
3281 mark_all_v_defs (list
);
3283 sra_insert_after (bsi
, list
);
3286 sra_insert_before (bsi
, list
);
3288 mark_no_warning (elt
);
3293 /* Scalarize a COPY. To recap, this is an assignment statement between
3294 two scalarizable references, LHS_ELT and RHS_ELT. */
3297 scalarize_copy (struct sra_elt
*lhs_elt
, struct sra_elt
*rhs_elt
,
3298 block_stmt_iterator
*bsi
)
3302 if (lhs_elt
->replacement
&& rhs_elt
->replacement
)
3304 /* If we have two scalar operands, modify the existing statement. */
3305 stmt
= bsi_stmt (*bsi
);
3307 /* See the commentary in sra_walk_function concerning
3308 RETURN_EXPR, and why we should never see one here. */
3309 gcc_assert (TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
);
3311 GIMPLE_STMT_OPERAND (stmt
, 0) = lhs_elt
->replacement
;
3312 GIMPLE_STMT_OPERAND (stmt
, 1) = REPLDUP (rhs_elt
->replacement
);
3315 else if (lhs_elt
->use_block_copy
|| rhs_elt
->use_block_copy
)
3317 /* If either side requires a block copy, then sync the RHS back
3318 to the original structure, leave the original assignment
3319 statement (which will perform the block copy), then load the
3320 LHS values out of its now-updated original structure. */
3321 /* ??? Could perform a modified pair-wise element copy. That
3322 would at least allow those elements that are instantiated in
3323 both structures to be optimized well. */
3326 generate_copy_inout (rhs_elt
, false,
3327 generate_element_ref (rhs_elt
), &list
);
3330 mark_all_v_defs (list
);
3331 sra_insert_before (bsi
, list
);
3335 generate_copy_inout (lhs_elt
, true,
3336 generate_element_ref (lhs_elt
), &list
);
3339 mark_all_v_defs (list
);
3340 sra_insert_after (bsi
, list
);
3345 /* Otherwise both sides must be fully instantiated. In which
3346 case perform pair-wise element assignments and replace the
3347 original block copy statement. */
3349 stmt
= bsi_stmt (*bsi
);
3350 mark_all_v_defs (stmt
);
3353 generate_element_copy (lhs_elt
, rhs_elt
, &list
);
3355 mark_all_v_defs (list
);
3356 sra_replace (bsi
, list
);
3360 /* Scalarize an INIT. To recap, this is an assignment to a scalarizable
3361 reference from some form of constructor: CONSTRUCTOR, COMPLEX_CST or
3362 COMPLEX_EXPR. If RHS is NULL, it should be treated as an empty
3366 scalarize_init (struct sra_elt
*lhs_elt
, tree rhs
, block_stmt_iterator
*bsi
)
3369 tree list
= NULL
, init_list
= NULL
;
3371 /* Generate initialization statements for all members extant in the RHS. */
3374 /* Unshare the expression just in case this is from a decl's initial. */
3375 rhs
= unshare_expr (rhs
);
3376 result
= generate_element_init (lhs_elt
, rhs
, &init_list
);
3379 /* CONSTRUCTOR is defined such that any member not mentioned is assigned
3380 a zero value. Initialize the rest of the instantiated elements. */
3381 generate_element_zero (lhs_elt
, &list
);
3382 append_to_statement_list (init_list
, &list
);
3386 /* If we failed to convert the entire initializer, then we must
3387 leave the structure assignment in place and must load values
3388 from the structure into the slots for which we did not find
3389 constants. The easiest way to do this is to generate a complete
3390 copy-out, and then follow that with the constant assignments
3391 that we were able to build. DCE will clean things up. */
3393 generate_copy_inout (lhs_elt
, true, generate_element_ref (lhs_elt
),
3395 append_to_statement_list (list
, &list0
);
3399 if (lhs_elt
->use_block_copy
|| !result
)
3401 /* Since LHS is not fully instantiated, we must leave the structure
3402 assignment in place. Treating this case differently from a USE
3403 exposes constants to later optimizations. */
3406 mark_all_v_defs (list
);
3407 sra_insert_after (bsi
, list
);
3412 /* The LHS is fully instantiated. The list of initializations
3413 replaces the original structure assignment. */
3415 mark_all_v_defs (bsi_stmt (*bsi
));
3416 mark_all_v_defs (list
);
3417 sra_replace (bsi
, list
);
3421 /* A subroutine of scalarize_ldst called via walk_tree. Set TREE_NO_TRAP
3422 on all INDIRECT_REFs. */
3425 mark_notrap (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
3429 if (TREE_CODE (t
) == INDIRECT_REF
)
3431 TREE_THIS_NOTRAP (t
) = 1;
3434 else if (IS_TYPE_OR_DECL_P (t
))
3440 /* Scalarize a LDST. To recap, this is an assignment between one scalarizable
3441 reference ELT and one non-scalarizable reference OTHER. IS_OUTPUT is true
3442 if ELT is on the left-hand side. */
3445 scalarize_ldst (struct sra_elt
*elt
, tree other
,
3446 block_stmt_iterator
*bsi
, bool is_output
)
3448 /* Shouldn't have gotten called for a scalar. */
3449 gcc_assert (!elt
->replacement
);
3451 if (elt
->use_block_copy
)
3453 /* Since ELT is not fully instantiated, we have to leave the
3454 block copy in place. Treat this as a USE. */
3455 scalarize_use (elt
, NULL
, bsi
, is_output
, false);
3459 /* The interesting case is when ELT is fully instantiated. In this
3460 case we can have each element stored/loaded directly to/from the
3461 corresponding slot in OTHER. This avoids a block copy. */
3463 tree list
= NULL
, stmt
= bsi_stmt (*bsi
);
3465 mark_all_v_defs (stmt
);
3466 generate_copy_inout (elt
, is_output
, other
, &list
);
3468 mark_all_v_defs (list
);
3470 /* Preserve EH semantics. */
3471 if (stmt_ends_bb_p (stmt
))
3473 tree_stmt_iterator tsi
;
3474 tree first
, blist
= NULL
;
3475 bool thr
= tree_could_throw_p (stmt
);
3477 /* If the last statement of this BB created an EH edge
3478 before scalarization, we have to locate the first
3479 statement that can throw in the new statement list and
3480 use that as the last statement of this BB, such that EH
3481 semantics is preserved. All statements up to this one
3482 are added to the same BB. All other statements in the
3483 list will be added to normal outgoing edges of the same
3484 BB. If they access any memory, it's the same memory, so
3485 we can assume they won't throw. */
3486 tsi
= tsi_start (list
);
3487 for (first
= tsi_stmt (tsi
);
3488 thr
&& !tsi_end_p (tsi
) && !tree_could_throw_p (first
);
3489 first
= tsi_stmt (tsi
))
3492 append_to_statement_list (first
, &blist
);
3495 /* Extract the first remaining statement from LIST, this is
3496 the EH statement if there is one. */
3500 sra_insert_before (bsi
, blist
);
3502 /* Replace the old statement with this new representative. */
3503 bsi_replace (bsi
, first
, true);
3505 if (!tsi_end_p (tsi
))
3507 /* If any reference would trap, then they all would. And more
3508 to the point, the first would. Therefore none of the rest
3509 will trap since the first didn't. Indicate this by
3510 iterating over the remaining statements and set
3511 TREE_THIS_NOTRAP in all INDIRECT_REFs. */
3514 walk_tree (tsi_stmt_ptr (tsi
), mark_notrap
, NULL
, NULL
);
3517 while (!tsi_end_p (tsi
));
3519 insert_edge_copies (list
, bsi
->bb
);
3523 sra_replace (bsi
, list
);
3527 /* Generate initializations for all scalarizable parameters. */
3530 scalarize_parms (void)
3533 referenced_var_iterator ri
;
3536 FOR_EACH_REFERENCED_VAR_IN_BITMAP (needs_copy_in
, var
, ri
)
3538 struct sra_elt
*elt
= lookup_element (NULL
, var
, NULL
, NO_INSERT
);
3539 generate_copy_inout (elt
, true, var
, &list
);
3544 insert_edge_copies (list
, ENTRY_BLOCK_PTR
);
3545 mark_all_v_defs (list
);
3549 /* Entry point to phase 4. Update the function to match replacements. */
3552 scalarize_function (void)
3554 static const struct sra_walk_fns fns
= {
3555 scalarize_use
, scalarize_copy
, scalarize_init
, scalarize_ldst
, false
3558 sra_walk_function (&fns
);
3560 bsi_commit_edge_inserts ();
3564 /* Debug helper function. Print ELT in a nice human-readable format. */
3567 dump_sra_elt_name (FILE *f
, struct sra_elt
*elt
)
3569 if (elt
->parent
&& TREE_CODE (elt
->parent
->type
) == COMPLEX_TYPE
)
3571 fputs (elt
->element
== integer_zero_node
? "__real__ " : "__imag__ ", f
);
3572 dump_sra_elt_name (f
, elt
->parent
);
3577 dump_sra_elt_name (f
, elt
->parent
);
3578 if (DECL_P (elt
->element
))
3580 if (TREE_CODE (elt
->element
) == FIELD_DECL
)
3582 print_generic_expr (f
, elt
->element
, dump_flags
);
3584 else if (TREE_CODE (elt
->element
) == BIT_FIELD_REF
)
3585 fprintf (f
, "$B" HOST_WIDE_INT_PRINT_DEC
"F" HOST_WIDE_INT_PRINT_DEC
,
3586 tree_low_cst (TREE_OPERAND (elt
->element
, 2), 1),
3587 tree_low_cst (TREE_OPERAND (elt
->element
, 1), 1));
3588 else if (TREE_CODE (elt
->element
) == RANGE_EXPR
)
3589 fprintf (f
, "["HOST_WIDE_INT_PRINT_DEC
".."HOST_WIDE_INT_PRINT_DEC
"]",
3590 TREE_INT_CST_LOW (TREE_OPERAND (elt
->element
, 0)),
3591 TREE_INT_CST_LOW (TREE_OPERAND (elt
->element
, 1)));
3593 fprintf (f
, "[" HOST_WIDE_INT_PRINT_DEC
"]",
3594 TREE_INT_CST_LOW (elt
->element
));
3598 /* Likewise, but callable from the debugger. */
3601 debug_sra_elt_name (struct sra_elt
*elt
)
3603 dump_sra_elt_name (stderr
, elt
);
3604 fputc ('\n', stderr
);
3608 sra_init_cache (void)
3610 if (sra_type_decomp_cache
)
3613 sra_type_decomp_cache
= BITMAP_ALLOC (NULL
);
3614 sra_type_inst_cache
= BITMAP_ALLOC (NULL
);
3617 /* Main entry point. */
3622 /* Initialize local variables. */
3624 gcc_obstack_init (&sra_obstack
);
3625 sra_candidates
= BITMAP_ALLOC (NULL
);
3626 needs_copy_in
= BITMAP_ALLOC (NULL
);
3628 sra_map
= htab_create (101, sra_elt_hash
, sra_elt_eq
, NULL
);
3630 /* Scan. If we find anything, instantiate and scalarize. */
3631 if (find_candidates_for_sra ())
3634 decide_instantiations ();
3635 scalarize_function ();
3636 if (!bitmap_empty_p (sra_candidates
))
3637 todoflags
|= TODO_rebuild_alias
;
3640 /* Free allocated memory. */
3641 htab_delete (sra_map
);
3643 BITMAP_FREE (sra_candidates
);
3644 BITMAP_FREE (needs_copy_in
);
3645 BITMAP_FREE (sra_type_decomp_cache
);
3646 BITMAP_FREE (sra_type_inst_cache
);
3647 obstack_free (&sra_obstack
, NULL
);
3652 tree_sra_early (void)
3660 return ret
& ~TODO_rebuild_alias
;
3666 return flag_tree_sra
!= 0;
3669 struct tree_opt_pass pass_sra_early
=
3672 gate_sra
, /* gate */
3673 tree_sra_early
, /* execute */
3676 0, /* static_pass_number */
3677 TV_TREE_SRA
, /* tv_id */
3678 PROP_cfg
| PROP_ssa
, /* properties_required */
3679 0, /* properties_provided */
3680 0, /* properties_destroyed */
3681 0, /* todo_flags_start */
3685 | TODO_verify_ssa
, /* todo_flags_finish */
3689 struct tree_opt_pass pass_sra
=
3692 gate_sra
, /* gate */
3693 tree_sra
, /* execute */
3696 0, /* static_pass_number */
3697 TV_TREE_SRA
, /* tv_id */
3698 PROP_cfg
| PROP_ssa
, /* properties_required */
3699 0, /* properties_provided */
3700 0, /* properties_destroyed */
3701 0, /* todo_flags_start */
3705 | TODO_verify_ssa
, /* todo_flags_finish */